nl-mac.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349
  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 <[email protected]>
  17. * Dmitry Eremin-Solenikov <[email protected]>
  18. * Maxim Osipov <[email protected]>
  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. }