nl-mac.c 35 KB

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