nl-mac.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  1. /*
  2. * Netlink inteface for IEEE 802.15.4 stack
  3. *
  4. * Copyright 2007, 2008 Siemens AG
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2
  8. * as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * Written by:
  16. * Sergey Lapin <slapin@ossfans.org>
  17. * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
  18. * Maxim Osipov <maxim.osipov@siemens.com>
  19. */
  20. #include <linux/gfp.h>
  21. #include <linux/kernel.h>
  22. #include <linux/if_arp.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/ieee802154.h>
  25. #include <net/netlink.h>
  26. #include <net/genetlink.h>
  27. #include <net/sock.h>
  28. #include <linux/nl802154.h>
  29. #include <linux/export.h>
  30. #include <net/af_ieee802154.h>
  31. #include <net/ieee802154_netdev.h>
  32. #include <net/cfg802154.h>
  33. #include "ieee802154.h"
  34. static int nla_put_hwaddr(struct sk_buff *msg, int type, __le64 hwaddr)
  35. {
  36. return nla_put_u64(msg, type, swab64((__force u64)hwaddr));
  37. }
  38. static __le64 nla_get_hwaddr(const struct nlattr *nla)
  39. {
  40. return ieee802154_devaddr_from_raw(nla_data(nla));
  41. }
  42. static int nla_put_shortaddr(struct sk_buff *msg, int type, __le16 addr)
  43. {
  44. return nla_put_u16(msg, type, le16_to_cpu(addr));
  45. }
  46. static __le16 nla_get_shortaddr(const struct nlattr *nla)
  47. {
  48. return cpu_to_le16(nla_get_u16(nla));
  49. }
  50. static int ieee802154_nl_start_confirm(struct net_device *dev, u8 status)
  51. {
  52. struct sk_buff *msg;
  53. pr_debug("%s\n", __func__);
  54. msg = ieee802154_nl_create(0, IEEE802154_START_CONF);
  55. if (!msg)
  56. return -ENOBUFS;
  57. if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
  58. nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
  59. nla_put(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
  60. dev->dev_addr) ||
  61. nla_put_u8(msg, IEEE802154_ATTR_STATUS, status))
  62. goto nla_put_failure;
  63. return ieee802154_nl_mcast(msg, IEEE802154_COORD_MCGRP);
  64. nla_put_failure:
  65. nlmsg_free(msg);
  66. return -ENOBUFS;
  67. }
  68. EXPORT_SYMBOL(ieee802154_nl_start_confirm);
  69. static int ieee802154_nl_fill_iface(struct sk_buff *msg, u32 portid,
  70. u32 seq, int flags, struct net_device *dev)
  71. {
  72. void *hdr;
  73. struct wpan_phy *phy;
  74. struct ieee802154_mlme_ops *ops;
  75. __le16 short_addr, pan_id;
  76. pr_debug("%s\n", __func__);
  77. hdr = genlmsg_put(msg, 0, seq, &nl802154_family, flags,
  78. IEEE802154_LIST_IFACE);
  79. if (!hdr)
  80. goto out;
  81. ops = ieee802154_mlme_ops(dev);
  82. phy = dev->ieee802154_ptr->wpan_phy;
  83. BUG_ON(!phy);
  84. get_device(&phy->dev);
  85. short_addr = ops->get_short_addr(dev);
  86. pan_id = ops->get_pan_id(dev);
  87. if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
  88. nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
  89. nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
  90. nla_put(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
  91. dev->dev_addr) ||
  92. nla_put_shortaddr(msg, IEEE802154_ATTR_SHORT_ADDR, short_addr) ||
  93. nla_put_shortaddr(msg, IEEE802154_ATTR_PAN_ID, pan_id))
  94. goto nla_put_failure;
  95. if (ops->get_mac_params) {
  96. struct ieee802154_mac_params params;
  97. rtnl_lock();
  98. ops->get_mac_params(dev, &params);
  99. rtnl_unlock();
  100. if (nla_put_s8(msg, IEEE802154_ATTR_TXPOWER,
  101. params.transmit_power) ||
  102. nla_put_u8(msg, IEEE802154_ATTR_LBT_ENABLED, params.lbt) ||
  103. nla_put_u8(msg, IEEE802154_ATTR_CCA_MODE,
  104. params.cca_mode) ||
  105. nla_put_s32(msg, IEEE802154_ATTR_CCA_ED_LEVEL,
  106. params.cca_ed_level) ||
  107. nla_put_u8(msg, IEEE802154_ATTR_CSMA_RETRIES,
  108. params.csma_retries) ||
  109. nla_put_u8(msg, IEEE802154_ATTR_CSMA_MIN_BE,
  110. params.min_be) ||
  111. nla_put_u8(msg, IEEE802154_ATTR_CSMA_MAX_BE,
  112. params.max_be) ||
  113. nla_put_s8(msg, IEEE802154_ATTR_FRAME_RETRIES,
  114. params.frame_retries))
  115. goto nla_put_failure;
  116. }
  117. wpan_phy_put(phy);
  118. return genlmsg_end(msg, hdr);
  119. nla_put_failure:
  120. wpan_phy_put(phy);
  121. genlmsg_cancel(msg, hdr);
  122. out:
  123. return -EMSGSIZE;
  124. }
  125. /* Requests from userspace */
  126. static struct net_device *ieee802154_nl_get_dev(struct genl_info *info)
  127. {
  128. struct net_device *dev;
  129. if (info->attrs[IEEE802154_ATTR_DEV_NAME]) {
  130. char name[IFNAMSIZ + 1];
  131. nla_strlcpy(name, info->attrs[IEEE802154_ATTR_DEV_NAME],
  132. sizeof(name));
  133. dev = dev_get_by_name(&init_net, name);
  134. } else if (info->attrs[IEEE802154_ATTR_DEV_INDEX]) {
  135. dev = dev_get_by_index(&init_net,
  136. nla_get_u32(info->attrs[IEEE802154_ATTR_DEV_INDEX]));
  137. } else {
  138. return NULL;
  139. }
  140. if (!dev)
  141. return NULL;
  142. /* Check on mtu is currently a hacked solution because lowpan
  143. * and wpan have the same ARPHRD type.
  144. */
  145. if (dev->type != ARPHRD_IEEE802154 || dev->mtu != IEEE802154_MTU) {
  146. dev_put(dev);
  147. return NULL;
  148. }
  149. return dev;
  150. }
  151. int ieee802154_associate_req(struct sk_buff *skb, struct genl_info *info)
  152. {
  153. struct net_device *dev;
  154. struct ieee802154_addr addr;
  155. u8 page;
  156. int ret = -EOPNOTSUPP;
  157. if (!info->attrs[IEEE802154_ATTR_CHANNEL] ||
  158. !info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
  159. (!info->attrs[IEEE802154_ATTR_COORD_HW_ADDR] &&
  160. !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]) ||
  161. !info->attrs[IEEE802154_ATTR_CAPABILITY])
  162. return -EINVAL;
  163. dev = ieee802154_nl_get_dev(info);
  164. if (!dev)
  165. return -ENODEV;
  166. if (!ieee802154_mlme_ops(dev)->assoc_req)
  167. goto out;
  168. if (info->attrs[IEEE802154_ATTR_COORD_HW_ADDR]) {
  169. addr.mode = IEEE802154_ADDR_LONG;
  170. addr.extended_addr = nla_get_hwaddr(
  171. info->attrs[IEEE802154_ATTR_COORD_HW_ADDR]);
  172. } else {
  173. addr.mode = IEEE802154_ADDR_SHORT;
  174. addr.short_addr = nla_get_shortaddr(
  175. info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
  176. }
  177. addr.pan_id = nla_get_shortaddr(
  178. info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
  179. if (info->attrs[IEEE802154_ATTR_PAGE])
  180. page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
  181. else
  182. page = 0;
  183. ret = ieee802154_mlme_ops(dev)->assoc_req(dev, &addr,
  184. nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]),
  185. page,
  186. nla_get_u8(info->attrs[IEEE802154_ATTR_CAPABILITY]));
  187. out:
  188. dev_put(dev);
  189. return ret;
  190. }
  191. int ieee802154_associate_resp(struct sk_buff *skb, struct genl_info *info)
  192. {
  193. struct net_device *dev;
  194. struct ieee802154_addr addr;
  195. int ret = -EOPNOTSUPP;
  196. if (!info->attrs[IEEE802154_ATTR_STATUS] ||
  197. !info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] ||
  198. !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR])
  199. return -EINVAL;
  200. dev = ieee802154_nl_get_dev(info);
  201. if (!dev)
  202. return -ENODEV;
  203. if (!ieee802154_mlme_ops(dev)->assoc_resp)
  204. goto out;
  205. addr.mode = IEEE802154_ADDR_LONG;
  206. addr.extended_addr = nla_get_hwaddr(
  207. info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]);
  208. addr.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
  209. ret = ieee802154_mlme_ops(dev)->assoc_resp(dev, &addr,
  210. nla_get_shortaddr(info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]),
  211. nla_get_u8(info->attrs[IEEE802154_ATTR_STATUS]));
  212. out:
  213. dev_put(dev);
  214. return ret;
  215. }
  216. int ieee802154_disassociate_req(struct sk_buff *skb, struct genl_info *info)
  217. {
  218. struct net_device *dev;
  219. struct ieee802154_addr addr;
  220. int ret = -EOPNOTSUPP;
  221. if ((!info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] &&
  222. !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]) ||
  223. !info->attrs[IEEE802154_ATTR_REASON])
  224. return -EINVAL;
  225. dev = ieee802154_nl_get_dev(info);
  226. if (!dev)
  227. return -ENODEV;
  228. if (!ieee802154_mlme_ops(dev)->disassoc_req)
  229. goto out;
  230. if (info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]) {
  231. addr.mode = IEEE802154_ADDR_LONG;
  232. addr.extended_addr = nla_get_hwaddr(
  233. info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]);
  234. } else {
  235. addr.mode = IEEE802154_ADDR_SHORT;
  236. addr.short_addr = nla_get_shortaddr(
  237. info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]);
  238. }
  239. addr.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
  240. ret = ieee802154_mlme_ops(dev)->disassoc_req(dev, &addr,
  241. nla_get_u8(info->attrs[IEEE802154_ATTR_REASON]));
  242. out:
  243. dev_put(dev);
  244. return ret;
  245. }
  246. /* PANid, channel, beacon_order = 15, superframe_order = 15,
  247. * PAN_coordinator, battery_life_extension = 0,
  248. * coord_realignment = 0, security_enable = 0
  249. */
  250. int ieee802154_start_req(struct sk_buff *skb, struct genl_info *info)
  251. {
  252. struct net_device *dev;
  253. struct ieee802154_addr addr;
  254. u8 channel, bcn_ord, sf_ord;
  255. u8 page;
  256. int pan_coord, blx, coord_realign;
  257. int ret = -EBUSY;
  258. if (!info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
  259. !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR] ||
  260. !info->attrs[IEEE802154_ATTR_CHANNEL] ||
  261. !info->attrs[IEEE802154_ATTR_BCN_ORD] ||
  262. !info->attrs[IEEE802154_ATTR_SF_ORD] ||
  263. !info->attrs[IEEE802154_ATTR_PAN_COORD] ||
  264. !info->attrs[IEEE802154_ATTR_BAT_EXT] ||
  265. !info->attrs[IEEE802154_ATTR_COORD_REALIGN]
  266. )
  267. return -EINVAL;
  268. dev = ieee802154_nl_get_dev(info);
  269. if (!dev)
  270. return -ENODEV;
  271. if (netif_running(dev))
  272. goto out;
  273. if (!ieee802154_mlme_ops(dev)->start_req) {
  274. ret = -EOPNOTSUPP;
  275. goto out;
  276. }
  277. addr.mode = IEEE802154_ADDR_SHORT;
  278. addr.short_addr = nla_get_shortaddr(
  279. info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
  280. addr.pan_id = nla_get_shortaddr(
  281. info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
  282. channel = nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]);
  283. bcn_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_BCN_ORD]);
  284. sf_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_SF_ORD]);
  285. pan_coord = nla_get_u8(info->attrs[IEEE802154_ATTR_PAN_COORD]);
  286. blx = nla_get_u8(info->attrs[IEEE802154_ATTR_BAT_EXT]);
  287. coord_realign = nla_get_u8(info->attrs[IEEE802154_ATTR_COORD_REALIGN]);
  288. if (info->attrs[IEEE802154_ATTR_PAGE])
  289. page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
  290. else
  291. page = 0;
  292. if (addr.short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST)) {
  293. ieee802154_nl_start_confirm(dev, IEEE802154_NO_SHORT_ADDRESS);
  294. dev_put(dev);
  295. return -EINVAL;
  296. }
  297. rtnl_lock();
  298. ret = ieee802154_mlme_ops(dev)->start_req(dev, &addr, channel, page,
  299. bcn_ord, sf_ord, pan_coord, blx, coord_realign);
  300. rtnl_unlock();
  301. /* FIXME: add validation for unused parameters to be sane
  302. * for SoftMAC
  303. */
  304. ieee802154_nl_start_confirm(dev, IEEE802154_SUCCESS);
  305. out:
  306. dev_put(dev);
  307. return ret;
  308. }
  309. int ieee802154_scan_req(struct sk_buff *skb, struct genl_info *info)
  310. {
  311. struct net_device *dev;
  312. int ret = -EOPNOTSUPP;
  313. u8 type;
  314. u32 channels;
  315. u8 duration;
  316. u8 page;
  317. if (!info->attrs[IEEE802154_ATTR_SCAN_TYPE] ||
  318. !info->attrs[IEEE802154_ATTR_CHANNELS] ||
  319. !info->attrs[IEEE802154_ATTR_DURATION])
  320. return -EINVAL;
  321. dev = ieee802154_nl_get_dev(info);
  322. if (!dev)
  323. return -ENODEV;
  324. if (!ieee802154_mlme_ops(dev)->scan_req)
  325. goto out;
  326. type = nla_get_u8(info->attrs[IEEE802154_ATTR_SCAN_TYPE]);
  327. channels = nla_get_u32(info->attrs[IEEE802154_ATTR_CHANNELS]);
  328. duration = nla_get_u8(info->attrs[IEEE802154_ATTR_DURATION]);
  329. if (info->attrs[IEEE802154_ATTR_PAGE])
  330. page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
  331. else
  332. page = 0;
  333. ret = ieee802154_mlme_ops(dev)->scan_req(dev, type, channels,
  334. page, duration);
  335. out:
  336. dev_put(dev);
  337. return ret;
  338. }
  339. int ieee802154_list_iface(struct sk_buff *skb, struct genl_info *info)
  340. {
  341. /* Request for interface name, index, type, IEEE address,
  342. * PAN Id, short address
  343. */
  344. struct sk_buff *msg;
  345. struct net_device *dev = NULL;
  346. int rc = -ENOBUFS;
  347. pr_debug("%s\n", __func__);
  348. dev = ieee802154_nl_get_dev(info);
  349. if (!dev)
  350. return -ENODEV;
  351. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  352. if (!msg)
  353. goto out_dev;
  354. rc = ieee802154_nl_fill_iface(msg, info->snd_portid, info->snd_seq,
  355. 0, dev);
  356. if (rc < 0)
  357. goto out_free;
  358. dev_put(dev);
  359. return genlmsg_reply(msg, info);
  360. out_free:
  361. nlmsg_free(msg);
  362. out_dev:
  363. dev_put(dev);
  364. return rc;
  365. }
  366. int ieee802154_dump_iface(struct sk_buff *skb, struct netlink_callback *cb)
  367. {
  368. struct net *net = sock_net(skb->sk);
  369. struct net_device *dev;
  370. int idx;
  371. int s_idx = cb->args[0];
  372. pr_debug("%s\n", __func__);
  373. idx = 0;
  374. for_each_netdev(net, dev) {
  375. /* Check on mtu is currently a hacked solution because lowpan
  376. * and wpan have the same ARPHRD type.
  377. */
  378. if (idx < s_idx || dev->type != ARPHRD_IEEE802154 ||
  379. dev->mtu != IEEE802154_MTU)
  380. goto cont;
  381. if (ieee802154_nl_fill_iface(skb, NETLINK_CB(cb->skb).portid,
  382. cb->nlh->nlmsg_seq,
  383. NLM_F_MULTI, dev) < 0)
  384. break;
  385. cont:
  386. idx++;
  387. }
  388. cb->args[0] = idx;
  389. return skb->len;
  390. }
  391. int ieee802154_set_macparams(struct sk_buff *skb, struct genl_info *info)
  392. {
  393. struct net_device *dev = NULL;
  394. struct ieee802154_mlme_ops *ops;
  395. struct ieee802154_mac_params params;
  396. struct wpan_phy *phy;
  397. int rc = -EINVAL;
  398. pr_debug("%s\n", __func__);
  399. dev = ieee802154_nl_get_dev(info);
  400. if (!dev)
  401. return -ENODEV;
  402. ops = ieee802154_mlme_ops(dev);
  403. if (!ops->get_mac_params || !ops->set_mac_params) {
  404. rc = -EOPNOTSUPP;
  405. goto out;
  406. }
  407. if (netif_running(dev)) {
  408. rc = -EBUSY;
  409. goto out;
  410. }
  411. if (!info->attrs[IEEE802154_ATTR_LBT_ENABLED] &&
  412. !info->attrs[IEEE802154_ATTR_CCA_MODE] &&
  413. !info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL] &&
  414. !info->attrs[IEEE802154_ATTR_CSMA_RETRIES] &&
  415. !info->attrs[IEEE802154_ATTR_CSMA_MIN_BE] &&
  416. !info->attrs[IEEE802154_ATTR_CSMA_MAX_BE] &&
  417. !info->attrs[IEEE802154_ATTR_FRAME_RETRIES])
  418. goto out;
  419. phy = dev->ieee802154_ptr->wpan_phy;
  420. get_device(&phy->dev);
  421. rtnl_lock();
  422. ops->get_mac_params(dev, &params);
  423. if (info->attrs[IEEE802154_ATTR_TXPOWER])
  424. params.transmit_power = nla_get_s8(info->attrs[IEEE802154_ATTR_TXPOWER]);
  425. if (info->attrs[IEEE802154_ATTR_LBT_ENABLED])
  426. params.lbt = nla_get_u8(info->attrs[IEEE802154_ATTR_LBT_ENABLED]);
  427. if (info->attrs[IEEE802154_ATTR_CCA_MODE])
  428. params.cca_mode = nla_get_u8(info->attrs[IEEE802154_ATTR_CCA_MODE]);
  429. if (info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL])
  430. params.cca_ed_level = nla_get_s32(info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL]);
  431. if (info->attrs[IEEE802154_ATTR_CSMA_RETRIES])
  432. params.csma_retries = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_RETRIES]);
  433. if (info->attrs[IEEE802154_ATTR_CSMA_MIN_BE])
  434. params.min_be = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_MIN_BE]);
  435. if (info->attrs[IEEE802154_ATTR_CSMA_MAX_BE])
  436. params.max_be = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_MAX_BE]);
  437. if (info->attrs[IEEE802154_ATTR_FRAME_RETRIES])
  438. params.frame_retries = nla_get_s8(info->attrs[IEEE802154_ATTR_FRAME_RETRIES]);
  439. rc = ops->set_mac_params(dev, &params);
  440. rtnl_unlock();
  441. wpan_phy_put(phy);
  442. dev_put(dev);
  443. return 0;
  444. out:
  445. dev_put(dev);
  446. return rc;
  447. }
  448. static int
  449. ieee802154_llsec_parse_key_id(struct genl_info *info,
  450. struct ieee802154_llsec_key_id *desc)
  451. {
  452. memset(desc, 0, sizeof(*desc));
  453. if (!info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE])
  454. return -EINVAL;
  455. desc->mode = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE]);
  456. if (desc->mode == IEEE802154_SCF_KEY_IMPLICIT) {
  457. if (!info->attrs[IEEE802154_ATTR_PAN_ID] &&
  458. !(info->attrs[IEEE802154_ATTR_SHORT_ADDR] ||
  459. info->attrs[IEEE802154_ATTR_HW_ADDR]))
  460. return -EINVAL;
  461. desc->device_addr.pan_id = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_PAN_ID]);
  462. if (info->attrs[IEEE802154_ATTR_SHORT_ADDR]) {
  463. desc->device_addr.mode = IEEE802154_ADDR_SHORT;
  464. desc->device_addr.short_addr = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_SHORT_ADDR]);
  465. } else {
  466. desc->device_addr.mode = IEEE802154_ADDR_LONG;
  467. desc->device_addr.extended_addr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
  468. }
  469. }
  470. if (desc->mode != IEEE802154_SCF_KEY_IMPLICIT &&
  471. !info->attrs[IEEE802154_ATTR_LLSEC_KEY_ID])
  472. return -EINVAL;
  473. if (desc->mode == IEEE802154_SCF_KEY_SHORT_INDEX &&
  474. !info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_SHORT])
  475. return -EINVAL;
  476. if (desc->mode == IEEE802154_SCF_KEY_HW_INDEX &&
  477. !info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_EXTENDED])
  478. return -EINVAL;
  479. if (desc->mode != IEEE802154_SCF_KEY_IMPLICIT)
  480. desc->id = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_KEY_ID]);
  481. switch (desc->mode) {
  482. case IEEE802154_SCF_KEY_SHORT_INDEX:
  483. {
  484. u32 source = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_SHORT]);
  485. desc->short_source = cpu_to_le32(source);
  486. break;
  487. }
  488. case IEEE802154_SCF_KEY_HW_INDEX:
  489. desc->extended_source = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_EXTENDED]);
  490. break;
  491. }
  492. return 0;
  493. }
  494. static int
  495. ieee802154_llsec_fill_key_id(struct sk_buff *msg,
  496. const struct ieee802154_llsec_key_id *desc)
  497. {
  498. if (nla_put_u8(msg, IEEE802154_ATTR_LLSEC_KEY_MODE, desc->mode))
  499. return -EMSGSIZE;
  500. if (desc->mode == IEEE802154_SCF_KEY_IMPLICIT) {
  501. if (nla_put_shortaddr(msg, IEEE802154_ATTR_PAN_ID,
  502. desc->device_addr.pan_id))
  503. return -EMSGSIZE;
  504. if (desc->device_addr.mode == IEEE802154_ADDR_SHORT &&
  505. nla_put_shortaddr(msg, IEEE802154_ATTR_SHORT_ADDR,
  506. desc->device_addr.short_addr))
  507. return -EMSGSIZE;
  508. if (desc->device_addr.mode == IEEE802154_ADDR_LONG &&
  509. nla_put_hwaddr(msg, IEEE802154_ATTR_HW_ADDR,
  510. desc->device_addr.extended_addr))
  511. return -EMSGSIZE;
  512. }
  513. if (desc->mode != IEEE802154_SCF_KEY_IMPLICIT &&
  514. nla_put_u8(msg, IEEE802154_ATTR_LLSEC_KEY_ID, desc->id))
  515. return -EMSGSIZE;
  516. if (desc->mode == IEEE802154_SCF_KEY_SHORT_INDEX &&
  517. nla_put_u32(msg, IEEE802154_ATTR_LLSEC_KEY_SOURCE_SHORT,
  518. le32_to_cpu(desc->short_source)))
  519. return -EMSGSIZE;
  520. if (desc->mode == IEEE802154_SCF_KEY_HW_INDEX &&
  521. nla_put_hwaddr(msg, IEEE802154_ATTR_LLSEC_KEY_SOURCE_EXTENDED,
  522. desc->extended_source))
  523. return -EMSGSIZE;
  524. return 0;
  525. }
  526. int ieee802154_llsec_getparams(struct sk_buff *skb, struct genl_info *info)
  527. {
  528. struct sk_buff *msg;
  529. struct net_device *dev = NULL;
  530. int rc = -ENOBUFS;
  531. struct ieee802154_mlme_ops *ops;
  532. void *hdr;
  533. struct ieee802154_llsec_params params;
  534. pr_debug("%s\n", __func__);
  535. dev = ieee802154_nl_get_dev(info);
  536. if (!dev)
  537. return -ENODEV;
  538. ops = ieee802154_mlme_ops(dev);
  539. if (!ops->llsec) {
  540. rc = -EOPNOTSUPP;
  541. goto out_dev;
  542. }
  543. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  544. if (!msg)
  545. goto out_dev;
  546. hdr = genlmsg_put(msg, 0, info->snd_seq, &nl802154_family, 0,
  547. IEEE802154_LLSEC_GETPARAMS);
  548. if (!hdr)
  549. goto out_free;
  550. rc = ops->llsec->get_params(dev, &params);
  551. if (rc < 0)
  552. goto out_free;
  553. if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
  554. nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
  555. nla_put_u8(msg, IEEE802154_ATTR_LLSEC_ENABLED, params.enabled) ||
  556. nla_put_u8(msg, IEEE802154_ATTR_LLSEC_SECLEVEL, params.out_level) ||
  557. nla_put_u32(msg, IEEE802154_ATTR_LLSEC_FRAME_COUNTER,
  558. be32_to_cpu(params.frame_counter)) ||
  559. ieee802154_llsec_fill_key_id(msg, &params.out_key))
  560. goto out_free;
  561. dev_put(dev);
  562. return ieee802154_nl_reply(msg, info);
  563. out_free:
  564. nlmsg_free(msg);
  565. out_dev:
  566. dev_put(dev);
  567. return rc;
  568. }
  569. int ieee802154_llsec_setparams(struct sk_buff *skb, struct genl_info *info)
  570. {
  571. struct net_device *dev = NULL;
  572. int rc = -EINVAL;
  573. struct ieee802154_mlme_ops *ops;
  574. struct ieee802154_llsec_params params;
  575. int changed = 0;
  576. pr_debug("%s\n", __func__);
  577. dev = ieee802154_nl_get_dev(info);
  578. if (!dev)
  579. return -ENODEV;
  580. if (!info->attrs[IEEE802154_ATTR_LLSEC_ENABLED] &&
  581. !info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE] &&
  582. !info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL])
  583. goto out;
  584. ops = ieee802154_mlme_ops(dev);
  585. if (!ops->llsec) {
  586. rc = -EOPNOTSUPP;
  587. goto out;
  588. }
  589. if (info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL] &&
  590. nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL]) > 7)
  591. goto out;
  592. if (info->attrs[IEEE802154_ATTR_LLSEC_ENABLED]) {
  593. params.enabled = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_ENABLED]);
  594. changed |= IEEE802154_LLSEC_PARAM_ENABLED;
  595. }
  596. if (info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE]) {
  597. if (ieee802154_llsec_parse_key_id(info, &params.out_key))
  598. goto out;
  599. changed |= IEEE802154_LLSEC_PARAM_OUT_KEY;
  600. }
  601. if (info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL]) {
  602. params.out_level = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL]);
  603. changed |= IEEE802154_LLSEC_PARAM_OUT_LEVEL;
  604. }
  605. if (info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]) {
  606. u32 fc = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]);
  607. params.frame_counter = cpu_to_be32(fc);
  608. changed |= IEEE802154_LLSEC_PARAM_FRAME_COUNTER;
  609. }
  610. rc = ops->llsec->set_params(dev, &params, changed);
  611. dev_put(dev);
  612. return rc;
  613. out:
  614. dev_put(dev);
  615. return rc;
  616. }
  617. struct llsec_dump_data {
  618. struct sk_buff *skb;
  619. int s_idx, s_idx2;
  620. int portid;
  621. int nlmsg_seq;
  622. struct net_device *dev;
  623. struct ieee802154_mlme_ops *ops;
  624. struct ieee802154_llsec_table *table;
  625. };
  626. static int
  627. ieee802154_llsec_dump_table(struct sk_buff *skb, struct netlink_callback *cb,
  628. int (*step)(struct llsec_dump_data *))
  629. {
  630. struct net *net = sock_net(skb->sk);
  631. struct net_device *dev;
  632. struct llsec_dump_data data;
  633. int idx = 0;
  634. int first_dev = cb->args[0];
  635. int rc;
  636. for_each_netdev(net, dev) {
  637. /* Check on mtu is currently a hacked solution because lowpan
  638. * and wpan have the same ARPHRD type.
  639. */
  640. if (idx < first_dev || dev->type != ARPHRD_IEEE802154 ||
  641. dev->mtu != IEEE802154_MTU)
  642. goto skip;
  643. data.ops = ieee802154_mlme_ops(dev);
  644. if (!data.ops->llsec)
  645. goto skip;
  646. data.skb = skb;
  647. data.s_idx = cb->args[1];
  648. data.s_idx2 = cb->args[2];
  649. data.dev = dev;
  650. data.portid = NETLINK_CB(cb->skb).portid;
  651. data.nlmsg_seq = cb->nlh->nlmsg_seq;
  652. data.ops->llsec->lock_table(dev);
  653. data.ops->llsec->get_table(data.dev, &data.table);
  654. rc = step(&data);
  655. data.ops->llsec->unlock_table(dev);
  656. if (rc < 0)
  657. break;
  658. skip:
  659. idx++;
  660. }
  661. cb->args[0] = idx;
  662. return skb->len;
  663. }
  664. static int
  665. ieee802154_nl_llsec_change(struct sk_buff *skb, struct genl_info *info,
  666. int (*fn)(struct net_device*, struct genl_info*))
  667. {
  668. struct net_device *dev = NULL;
  669. int rc = -EINVAL;
  670. dev = ieee802154_nl_get_dev(info);
  671. if (!dev)
  672. return -ENODEV;
  673. if (!ieee802154_mlme_ops(dev)->llsec)
  674. rc = -EOPNOTSUPP;
  675. else
  676. rc = fn(dev, info);
  677. dev_put(dev);
  678. return rc;
  679. }
  680. static int
  681. ieee802154_llsec_parse_key(struct genl_info *info,
  682. struct ieee802154_llsec_key *key)
  683. {
  684. u8 frames;
  685. u32 commands[256 / 32];
  686. memset(key, 0, sizeof(*key));
  687. if (!info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_FRAME_TYPES] ||
  688. !info->attrs[IEEE802154_ATTR_LLSEC_KEY_BYTES])
  689. return -EINVAL;
  690. frames = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_FRAME_TYPES]);
  691. if ((frames & BIT(IEEE802154_FC_TYPE_MAC_CMD)) &&
  692. !info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS])
  693. return -EINVAL;
  694. if (info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS]) {
  695. nla_memcpy(commands,
  696. info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS],
  697. 256 / 8);
  698. if (commands[0] || commands[1] || commands[2] || commands[3] ||
  699. commands[4] || commands[5] || commands[6] ||
  700. commands[7] >= BIT(IEEE802154_CMD_GTS_REQ + 1))
  701. return -EINVAL;
  702. key->cmd_frame_ids = commands[7];
  703. }
  704. key->frame_types = frames;
  705. nla_memcpy(key->key, info->attrs[IEEE802154_ATTR_LLSEC_KEY_BYTES],
  706. IEEE802154_LLSEC_KEY_SIZE);
  707. return 0;
  708. }
  709. static int llsec_add_key(struct net_device *dev, struct genl_info *info)
  710. {
  711. struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
  712. struct ieee802154_llsec_key key;
  713. struct ieee802154_llsec_key_id id;
  714. if (ieee802154_llsec_parse_key(info, &key) ||
  715. ieee802154_llsec_parse_key_id(info, &id))
  716. return -EINVAL;
  717. return ops->llsec->add_key(dev, &id, &key);
  718. }
  719. int ieee802154_llsec_add_key(struct sk_buff *skb, struct genl_info *info)
  720. {
  721. if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
  722. (NLM_F_CREATE | NLM_F_EXCL))
  723. return -EINVAL;
  724. return ieee802154_nl_llsec_change(skb, info, llsec_add_key);
  725. }
  726. static int llsec_remove_key(struct net_device *dev, struct genl_info *info)
  727. {
  728. struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
  729. struct ieee802154_llsec_key_id id;
  730. if (ieee802154_llsec_parse_key_id(info, &id))
  731. return -EINVAL;
  732. return ops->llsec->del_key(dev, &id);
  733. }
  734. int ieee802154_llsec_del_key(struct sk_buff *skb, struct genl_info *info)
  735. {
  736. return ieee802154_nl_llsec_change(skb, info, llsec_remove_key);
  737. }
  738. static int
  739. ieee802154_nl_fill_key(struct sk_buff *msg, u32 portid, u32 seq,
  740. const struct ieee802154_llsec_key_entry *key,
  741. const struct net_device *dev)
  742. {
  743. void *hdr;
  744. u32 commands[256 / 32];
  745. hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
  746. IEEE802154_LLSEC_LIST_KEY);
  747. if (!hdr)
  748. goto out;
  749. if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
  750. nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
  751. ieee802154_llsec_fill_key_id(msg, &key->id) ||
  752. nla_put_u8(msg, IEEE802154_ATTR_LLSEC_KEY_USAGE_FRAME_TYPES,
  753. key->key->frame_types))
  754. goto nla_put_failure;
  755. if (key->key->frame_types & BIT(IEEE802154_FC_TYPE_MAC_CMD)) {
  756. memset(commands, 0, sizeof(commands));
  757. commands[7] = key->key->cmd_frame_ids;
  758. if (nla_put(msg, IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS,
  759. sizeof(commands), commands))
  760. goto nla_put_failure;
  761. }
  762. if (nla_put(msg, IEEE802154_ATTR_LLSEC_KEY_BYTES,
  763. IEEE802154_LLSEC_KEY_SIZE, key->key->key))
  764. goto nla_put_failure;
  765. genlmsg_end(msg, hdr);
  766. return 0;
  767. nla_put_failure:
  768. genlmsg_cancel(msg, hdr);
  769. out:
  770. return -EMSGSIZE;
  771. }
  772. static int llsec_iter_keys(struct llsec_dump_data *data)
  773. {
  774. struct ieee802154_llsec_key_entry *pos;
  775. int rc = 0, idx = 0;
  776. list_for_each_entry(pos, &data->table->keys, list) {
  777. if (idx++ < data->s_idx)
  778. continue;
  779. if (ieee802154_nl_fill_key(data->skb, data->portid,
  780. data->nlmsg_seq, pos, data->dev)) {
  781. rc = -EMSGSIZE;
  782. break;
  783. }
  784. data->s_idx++;
  785. }
  786. return rc;
  787. }
  788. int ieee802154_llsec_dump_keys(struct sk_buff *skb, struct netlink_callback *cb)
  789. {
  790. return ieee802154_llsec_dump_table(skb, cb, llsec_iter_keys);
  791. }
  792. static int
  793. llsec_parse_dev(struct genl_info *info,
  794. struct ieee802154_llsec_device *dev)
  795. {
  796. memset(dev, 0, sizeof(*dev));
  797. if (!info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER] ||
  798. !info->attrs[IEEE802154_ATTR_HW_ADDR] ||
  799. !info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE] ||
  800. !info->attrs[IEEE802154_ATTR_LLSEC_DEV_KEY_MODE] ||
  801. (!!info->attrs[IEEE802154_ATTR_PAN_ID] !=
  802. !!info->attrs[IEEE802154_ATTR_SHORT_ADDR]))
  803. return -EINVAL;
  804. if (info->attrs[IEEE802154_ATTR_PAN_ID]) {
  805. dev->pan_id = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_PAN_ID]);
  806. dev->short_addr = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_SHORT_ADDR]);
  807. } else {
  808. dev->short_addr = cpu_to_le16(IEEE802154_ADDR_UNDEF);
  809. }
  810. dev->hwaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
  811. dev->frame_counter = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]);
  812. dev->seclevel_exempt = !!nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE]);
  813. dev->key_mode = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_DEV_KEY_MODE]);
  814. if (dev->key_mode >= __IEEE802154_LLSEC_DEVKEY_MAX)
  815. return -EINVAL;
  816. return 0;
  817. }
  818. static int llsec_add_dev(struct net_device *dev, struct genl_info *info)
  819. {
  820. struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
  821. struct ieee802154_llsec_device desc;
  822. if (llsec_parse_dev(info, &desc))
  823. return -EINVAL;
  824. return ops->llsec->add_dev(dev, &desc);
  825. }
  826. int ieee802154_llsec_add_dev(struct sk_buff *skb, struct genl_info *info)
  827. {
  828. if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
  829. (NLM_F_CREATE | NLM_F_EXCL))
  830. return -EINVAL;
  831. return ieee802154_nl_llsec_change(skb, info, llsec_add_dev);
  832. }
  833. static int llsec_del_dev(struct net_device *dev, struct genl_info *info)
  834. {
  835. struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
  836. __le64 devaddr;
  837. if (!info->attrs[IEEE802154_ATTR_HW_ADDR])
  838. return -EINVAL;
  839. devaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
  840. return ops->llsec->del_dev(dev, devaddr);
  841. }
  842. int ieee802154_llsec_del_dev(struct sk_buff *skb, struct genl_info *info)
  843. {
  844. return ieee802154_nl_llsec_change(skb, info, llsec_del_dev);
  845. }
  846. static int
  847. ieee802154_nl_fill_dev(struct sk_buff *msg, u32 portid, u32 seq,
  848. const struct ieee802154_llsec_device *desc,
  849. const struct net_device *dev)
  850. {
  851. void *hdr;
  852. hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
  853. IEEE802154_LLSEC_LIST_DEV);
  854. if (!hdr)
  855. goto out;
  856. if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
  857. nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
  858. nla_put_shortaddr(msg, IEEE802154_ATTR_PAN_ID, desc->pan_id) ||
  859. nla_put_shortaddr(msg, IEEE802154_ATTR_SHORT_ADDR,
  860. desc->short_addr) ||
  861. nla_put_hwaddr(msg, IEEE802154_ATTR_HW_ADDR, desc->hwaddr) ||
  862. nla_put_u32(msg, IEEE802154_ATTR_LLSEC_FRAME_COUNTER,
  863. desc->frame_counter) ||
  864. nla_put_u8(msg, IEEE802154_ATTR_LLSEC_DEV_OVERRIDE,
  865. desc->seclevel_exempt) ||
  866. nla_put_u8(msg, IEEE802154_ATTR_LLSEC_DEV_KEY_MODE, desc->key_mode))
  867. goto nla_put_failure;
  868. genlmsg_end(msg, hdr);
  869. return 0;
  870. nla_put_failure:
  871. genlmsg_cancel(msg, hdr);
  872. out:
  873. return -EMSGSIZE;
  874. }
  875. static int llsec_iter_devs(struct llsec_dump_data *data)
  876. {
  877. struct ieee802154_llsec_device *pos;
  878. int rc = 0, idx = 0;
  879. list_for_each_entry(pos, &data->table->devices, list) {
  880. if (idx++ < data->s_idx)
  881. continue;
  882. if (ieee802154_nl_fill_dev(data->skb, data->portid,
  883. data->nlmsg_seq, pos, data->dev)) {
  884. rc = -EMSGSIZE;
  885. break;
  886. }
  887. data->s_idx++;
  888. }
  889. return rc;
  890. }
  891. int ieee802154_llsec_dump_devs(struct sk_buff *skb, struct netlink_callback *cb)
  892. {
  893. return ieee802154_llsec_dump_table(skb, cb, llsec_iter_devs);
  894. }
  895. static int llsec_add_devkey(struct net_device *dev, struct genl_info *info)
  896. {
  897. struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
  898. struct ieee802154_llsec_device_key key;
  899. __le64 devaddr;
  900. if (!info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER] ||
  901. !info->attrs[IEEE802154_ATTR_HW_ADDR] ||
  902. ieee802154_llsec_parse_key_id(info, &key.key_id))
  903. return -EINVAL;
  904. devaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
  905. key.frame_counter = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]);
  906. return ops->llsec->add_devkey(dev, devaddr, &key);
  907. }
  908. int ieee802154_llsec_add_devkey(struct sk_buff *skb, struct genl_info *info)
  909. {
  910. if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
  911. (NLM_F_CREATE | NLM_F_EXCL))
  912. return -EINVAL;
  913. return ieee802154_nl_llsec_change(skb, info, llsec_add_devkey);
  914. }
  915. static int llsec_del_devkey(struct net_device *dev, struct genl_info *info)
  916. {
  917. struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
  918. struct ieee802154_llsec_device_key key;
  919. __le64 devaddr;
  920. if (!info->attrs[IEEE802154_ATTR_HW_ADDR] ||
  921. ieee802154_llsec_parse_key_id(info, &key.key_id))
  922. return -EINVAL;
  923. devaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]);
  924. return ops->llsec->del_devkey(dev, devaddr, &key);
  925. }
  926. int ieee802154_llsec_del_devkey(struct sk_buff *skb, struct genl_info *info)
  927. {
  928. return ieee802154_nl_llsec_change(skb, info, llsec_del_devkey);
  929. }
  930. static int
  931. ieee802154_nl_fill_devkey(struct sk_buff *msg, u32 portid, u32 seq,
  932. __le64 devaddr,
  933. const struct ieee802154_llsec_device_key *devkey,
  934. const struct net_device *dev)
  935. {
  936. void *hdr;
  937. hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
  938. IEEE802154_LLSEC_LIST_DEVKEY);
  939. if (!hdr)
  940. goto out;
  941. if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
  942. nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
  943. nla_put_hwaddr(msg, IEEE802154_ATTR_HW_ADDR, devaddr) ||
  944. nla_put_u32(msg, IEEE802154_ATTR_LLSEC_FRAME_COUNTER,
  945. devkey->frame_counter) ||
  946. ieee802154_llsec_fill_key_id(msg, &devkey->key_id))
  947. goto nla_put_failure;
  948. genlmsg_end(msg, hdr);
  949. return 0;
  950. nla_put_failure:
  951. genlmsg_cancel(msg, hdr);
  952. out:
  953. return -EMSGSIZE;
  954. }
  955. static int llsec_iter_devkeys(struct llsec_dump_data *data)
  956. {
  957. struct ieee802154_llsec_device *dpos;
  958. struct ieee802154_llsec_device_key *kpos;
  959. int rc = 0, idx = 0, idx2;
  960. list_for_each_entry(dpos, &data->table->devices, list) {
  961. if (idx++ < data->s_idx)
  962. continue;
  963. idx2 = 0;
  964. list_for_each_entry(kpos, &dpos->keys, list) {
  965. if (idx2++ < data->s_idx2)
  966. continue;
  967. if (ieee802154_nl_fill_devkey(data->skb, data->portid,
  968. data->nlmsg_seq,
  969. dpos->hwaddr, kpos,
  970. data->dev)) {
  971. return rc = -EMSGSIZE;
  972. }
  973. data->s_idx2++;
  974. }
  975. data->s_idx++;
  976. }
  977. return rc;
  978. }
  979. int ieee802154_llsec_dump_devkeys(struct sk_buff *skb,
  980. struct netlink_callback *cb)
  981. {
  982. return ieee802154_llsec_dump_table(skb, cb, llsec_iter_devkeys);
  983. }
  984. static int
  985. llsec_parse_seclevel(struct genl_info *info,
  986. struct ieee802154_llsec_seclevel *sl)
  987. {
  988. memset(sl, 0, sizeof(*sl));
  989. if (!info->attrs[IEEE802154_ATTR_LLSEC_FRAME_TYPE] ||
  990. !info->attrs[IEEE802154_ATTR_LLSEC_SECLEVELS] ||
  991. !info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE])
  992. return -EINVAL;
  993. sl->frame_type = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_TYPE]);
  994. if (sl->frame_type == IEEE802154_FC_TYPE_MAC_CMD) {
  995. if (!info->attrs[IEEE802154_ATTR_LLSEC_CMD_FRAME_ID])
  996. return -EINVAL;
  997. sl->cmd_frame_id = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_CMD_FRAME_ID]);
  998. }
  999. sl->sec_levels = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_SECLEVELS]);
  1000. sl->device_override = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE]);
  1001. return 0;
  1002. }
  1003. static int llsec_add_seclevel(struct net_device *dev, struct genl_info *info)
  1004. {
  1005. struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
  1006. struct ieee802154_llsec_seclevel sl;
  1007. if (llsec_parse_seclevel(info, &sl))
  1008. return -EINVAL;
  1009. return ops->llsec->add_seclevel(dev, &sl);
  1010. }
  1011. int ieee802154_llsec_add_seclevel(struct sk_buff *skb, struct genl_info *info)
  1012. {
  1013. if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) !=
  1014. (NLM_F_CREATE | NLM_F_EXCL))
  1015. return -EINVAL;
  1016. return ieee802154_nl_llsec_change(skb, info, llsec_add_seclevel);
  1017. }
  1018. static int llsec_del_seclevel(struct net_device *dev, struct genl_info *info)
  1019. {
  1020. struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
  1021. struct ieee802154_llsec_seclevel sl;
  1022. if (llsec_parse_seclevel(info, &sl))
  1023. return -EINVAL;
  1024. return ops->llsec->del_seclevel(dev, &sl);
  1025. }
  1026. int ieee802154_llsec_del_seclevel(struct sk_buff *skb, struct genl_info *info)
  1027. {
  1028. return ieee802154_nl_llsec_change(skb, info, llsec_del_seclevel);
  1029. }
  1030. static int
  1031. ieee802154_nl_fill_seclevel(struct sk_buff *msg, u32 portid, u32 seq,
  1032. const struct ieee802154_llsec_seclevel *sl,
  1033. const struct net_device *dev)
  1034. {
  1035. void *hdr;
  1036. hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI,
  1037. IEEE802154_LLSEC_LIST_SECLEVEL);
  1038. if (!hdr)
  1039. goto out;
  1040. if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) ||
  1041. nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) ||
  1042. nla_put_u8(msg, IEEE802154_ATTR_LLSEC_FRAME_TYPE, sl->frame_type) ||
  1043. nla_put_u8(msg, IEEE802154_ATTR_LLSEC_SECLEVELS, sl->sec_levels) ||
  1044. nla_put_u8(msg, IEEE802154_ATTR_LLSEC_DEV_OVERRIDE,
  1045. sl->device_override))
  1046. goto nla_put_failure;
  1047. if (sl->frame_type == IEEE802154_FC_TYPE_MAC_CMD &&
  1048. nla_put_u8(msg, IEEE802154_ATTR_LLSEC_CMD_FRAME_ID,
  1049. sl->cmd_frame_id))
  1050. goto nla_put_failure;
  1051. genlmsg_end(msg, hdr);
  1052. return 0;
  1053. nla_put_failure:
  1054. genlmsg_cancel(msg, hdr);
  1055. out:
  1056. return -EMSGSIZE;
  1057. }
  1058. static int llsec_iter_seclevels(struct llsec_dump_data *data)
  1059. {
  1060. struct ieee802154_llsec_seclevel *pos;
  1061. int rc = 0, idx = 0;
  1062. list_for_each_entry(pos, &data->table->security_levels, list) {
  1063. if (idx++ < data->s_idx)
  1064. continue;
  1065. if (ieee802154_nl_fill_seclevel(data->skb, data->portid,
  1066. data->nlmsg_seq, pos,
  1067. data->dev)) {
  1068. rc = -EMSGSIZE;
  1069. break;
  1070. }
  1071. data->s_idx++;
  1072. }
  1073. return rc;
  1074. }
  1075. int ieee802154_llsec_dump_seclevels(struct sk_buff *skb,
  1076. struct netlink_callback *cb)
  1077. {
  1078. return ieee802154_llsec_dump_table(skb, cb, llsec_iter_seclevels);
  1079. }