nl-mac.c 34 KB

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