nl-mac.c 40 KB

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