nl-mac.c 35 KB

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