nl-mac.c 40 KB

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