nl802154.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489
  1. /* This program is free software; you can redistribute it and/or modify
  2. * it under the terms of the GNU General Public License version 2
  3. * as published by the Free Software Foundation.
  4. *
  5. * This program is distributed in the hope that it will be useful,
  6. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. * GNU General Public License for more details.
  9. *
  10. * Authors:
  11. * Alexander Aring <aar@pengutronix.de>
  12. *
  13. * Based on: net/wireless/nl80211.c
  14. */
  15. #include <linux/rtnetlink.h>
  16. #include <net/cfg802154.h>
  17. #include <net/genetlink.h>
  18. #include <net/mac802154.h>
  19. #include <net/netlink.h>
  20. #include <net/nl802154.h>
  21. #include <net/sock.h>
  22. #include "nl802154.h"
  23. #include "rdev-ops.h"
  24. #include "core.h"
  25. /* the netlink family */
  26. static struct genl_family nl802154_fam;
  27. /* multicast groups */
  28. enum nl802154_multicast_groups {
  29. NL802154_MCGRP_CONFIG,
  30. };
  31. static const struct genl_multicast_group nl802154_mcgrps[] = {
  32. [NL802154_MCGRP_CONFIG] = { .name = "config", },
  33. };
  34. /* returns ERR_PTR values */
  35. static struct wpan_dev *
  36. __cfg802154_wpan_dev_from_attrs(struct net *netns, struct nlattr **attrs)
  37. {
  38. struct cfg802154_registered_device *rdev;
  39. struct wpan_dev *result = NULL;
  40. bool have_ifidx = attrs[NL802154_ATTR_IFINDEX];
  41. bool have_wpan_dev_id = attrs[NL802154_ATTR_WPAN_DEV];
  42. u64 wpan_dev_id;
  43. int wpan_phy_idx = -1;
  44. int ifidx = -1;
  45. ASSERT_RTNL();
  46. if (!have_ifidx && !have_wpan_dev_id)
  47. return ERR_PTR(-EINVAL);
  48. if (have_ifidx)
  49. ifidx = nla_get_u32(attrs[NL802154_ATTR_IFINDEX]);
  50. if (have_wpan_dev_id) {
  51. wpan_dev_id = nla_get_u64(attrs[NL802154_ATTR_WPAN_DEV]);
  52. wpan_phy_idx = wpan_dev_id >> 32;
  53. }
  54. list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
  55. struct wpan_dev *wpan_dev;
  56. if (wpan_phy_net(&rdev->wpan_phy) != netns)
  57. continue;
  58. if (have_wpan_dev_id && rdev->wpan_phy_idx != wpan_phy_idx)
  59. continue;
  60. list_for_each_entry(wpan_dev, &rdev->wpan_dev_list, list) {
  61. if (have_ifidx && wpan_dev->netdev &&
  62. wpan_dev->netdev->ifindex == ifidx) {
  63. result = wpan_dev;
  64. break;
  65. }
  66. if (have_wpan_dev_id &&
  67. wpan_dev->identifier == (u32)wpan_dev_id) {
  68. result = wpan_dev;
  69. break;
  70. }
  71. }
  72. if (result)
  73. break;
  74. }
  75. if (result)
  76. return result;
  77. return ERR_PTR(-ENODEV);
  78. }
  79. static struct cfg802154_registered_device *
  80. __cfg802154_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
  81. {
  82. struct cfg802154_registered_device *rdev = NULL, *tmp;
  83. struct net_device *netdev;
  84. ASSERT_RTNL();
  85. if (!attrs[NL802154_ATTR_WPAN_PHY] &&
  86. !attrs[NL802154_ATTR_IFINDEX] &&
  87. !attrs[NL802154_ATTR_WPAN_DEV])
  88. return ERR_PTR(-EINVAL);
  89. if (attrs[NL802154_ATTR_WPAN_PHY])
  90. rdev = cfg802154_rdev_by_wpan_phy_idx(
  91. nla_get_u32(attrs[NL802154_ATTR_WPAN_PHY]));
  92. if (attrs[NL802154_ATTR_WPAN_DEV]) {
  93. u64 wpan_dev_id = nla_get_u64(attrs[NL802154_ATTR_WPAN_DEV]);
  94. struct wpan_dev *wpan_dev;
  95. bool found = false;
  96. tmp = cfg802154_rdev_by_wpan_phy_idx(wpan_dev_id >> 32);
  97. if (tmp) {
  98. /* make sure wpan_dev exists */
  99. list_for_each_entry(wpan_dev, &tmp->wpan_dev_list, list) {
  100. if (wpan_dev->identifier != (u32)wpan_dev_id)
  101. continue;
  102. found = true;
  103. break;
  104. }
  105. if (!found)
  106. tmp = NULL;
  107. if (rdev && tmp != rdev)
  108. return ERR_PTR(-EINVAL);
  109. rdev = tmp;
  110. }
  111. }
  112. if (attrs[NL802154_ATTR_IFINDEX]) {
  113. int ifindex = nla_get_u32(attrs[NL802154_ATTR_IFINDEX]);
  114. netdev = __dev_get_by_index(netns, ifindex);
  115. if (netdev) {
  116. if (netdev->ieee802154_ptr)
  117. tmp = wpan_phy_to_rdev(
  118. netdev->ieee802154_ptr->wpan_phy);
  119. else
  120. tmp = NULL;
  121. /* not wireless device -- return error */
  122. if (!tmp)
  123. return ERR_PTR(-EINVAL);
  124. /* mismatch -- return error */
  125. if (rdev && tmp != rdev)
  126. return ERR_PTR(-EINVAL);
  127. rdev = tmp;
  128. }
  129. }
  130. if (!rdev)
  131. return ERR_PTR(-ENODEV);
  132. if (netns != wpan_phy_net(&rdev->wpan_phy))
  133. return ERR_PTR(-ENODEV);
  134. return rdev;
  135. }
  136. /* This function returns a pointer to the driver
  137. * that the genl_info item that is passed refers to.
  138. *
  139. * The result of this can be a PTR_ERR and hence must
  140. * be checked with IS_ERR() for errors.
  141. */
  142. static struct cfg802154_registered_device *
  143. cfg802154_get_dev_from_info(struct net *netns, struct genl_info *info)
  144. {
  145. return __cfg802154_rdev_from_attrs(netns, info->attrs);
  146. }
  147. /* policy for the attributes */
  148. static const struct nla_policy nl802154_policy[NL802154_ATTR_MAX+1] = {
  149. [NL802154_ATTR_WPAN_PHY] = { .type = NLA_U32 },
  150. [NL802154_ATTR_WPAN_PHY_NAME] = { .type = NLA_NUL_STRING,
  151. .len = 20-1 },
  152. [NL802154_ATTR_IFINDEX] = { .type = NLA_U32 },
  153. [NL802154_ATTR_IFTYPE] = { .type = NLA_U32 },
  154. [NL802154_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
  155. [NL802154_ATTR_WPAN_DEV] = { .type = NLA_U64 },
  156. [NL802154_ATTR_PAGE] = { .type = NLA_U8, },
  157. [NL802154_ATTR_CHANNEL] = { .type = NLA_U8, },
  158. [NL802154_ATTR_TX_POWER] = { .type = NLA_S32, },
  159. [NL802154_ATTR_CCA_MODE] = { .type = NLA_U32, },
  160. [NL802154_ATTR_CCA_OPT] = { .type = NLA_U32, },
  161. [NL802154_ATTR_CCA_ED_LEVEL] = { .type = NLA_S32, },
  162. [NL802154_ATTR_SUPPORTED_CHANNEL] = { .type = NLA_U32, },
  163. [NL802154_ATTR_PAN_ID] = { .type = NLA_U16, },
  164. [NL802154_ATTR_EXTENDED_ADDR] = { .type = NLA_U64 },
  165. [NL802154_ATTR_SHORT_ADDR] = { .type = NLA_U16, },
  166. [NL802154_ATTR_MIN_BE] = { .type = NLA_U8, },
  167. [NL802154_ATTR_MAX_BE] = { .type = NLA_U8, },
  168. [NL802154_ATTR_MAX_CSMA_BACKOFFS] = { .type = NLA_U8, },
  169. [NL802154_ATTR_MAX_FRAME_RETRIES] = { .type = NLA_S8, },
  170. [NL802154_ATTR_LBT_MODE] = { .type = NLA_U8, },
  171. [NL802154_ATTR_WPAN_PHY_CAPS] = { .type = NLA_NESTED },
  172. [NL802154_ATTR_SUPPORTED_COMMANDS] = { .type = NLA_NESTED },
  173. [NL802154_ATTR_ACKREQ_DEFAULT] = { .type = NLA_U8 },
  174. [NL802154_ATTR_PID] = { .type = NLA_U32 },
  175. [NL802154_ATTR_NETNS_FD] = { .type = NLA_U32 },
  176. #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
  177. [NL802154_ATTR_SEC_ENABLED] = { .type = NLA_U8, },
  178. [NL802154_ATTR_SEC_OUT_LEVEL] = { .type = NLA_U32, },
  179. [NL802154_ATTR_SEC_OUT_KEY_ID] = { .type = NLA_NESTED, },
  180. [NL802154_ATTR_SEC_FRAME_COUNTER] = { .type = NLA_U32 },
  181. [NL802154_ATTR_SEC_LEVEL] = { .type = NLA_NESTED },
  182. [NL802154_ATTR_SEC_DEVICE] = { .type = NLA_NESTED },
  183. [NL802154_ATTR_SEC_DEVKEY] = { .type = NLA_NESTED },
  184. [NL802154_ATTR_SEC_KEY] = { .type = NLA_NESTED },
  185. #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
  186. };
  187. #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
  188. static int
  189. nl802154_prepare_wpan_dev_dump(struct sk_buff *skb,
  190. struct netlink_callback *cb,
  191. struct cfg802154_registered_device **rdev,
  192. struct wpan_dev **wpan_dev)
  193. {
  194. int err;
  195. rtnl_lock();
  196. if (!cb->args[0]) {
  197. err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl802154_fam.hdrsize,
  198. genl_family_attrbuf(&nl802154_fam),
  199. nl802154_fam.maxattr, nl802154_policy, NULL);
  200. if (err)
  201. goto out_unlock;
  202. *wpan_dev = __cfg802154_wpan_dev_from_attrs(sock_net(skb->sk),
  203. genl_family_attrbuf(&nl802154_fam));
  204. if (IS_ERR(*wpan_dev)) {
  205. err = PTR_ERR(*wpan_dev);
  206. goto out_unlock;
  207. }
  208. *rdev = wpan_phy_to_rdev((*wpan_dev)->wpan_phy);
  209. /* 0 is the first index - add 1 to parse only once */
  210. cb->args[0] = (*rdev)->wpan_phy_idx + 1;
  211. cb->args[1] = (*wpan_dev)->identifier;
  212. } else {
  213. /* subtract the 1 again here */
  214. struct wpan_phy *wpan_phy = wpan_phy_idx_to_wpan_phy(cb->args[0] - 1);
  215. struct wpan_dev *tmp;
  216. if (!wpan_phy) {
  217. err = -ENODEV;
  218. goto out_unlock;
  219. }
  220. *rdev = wpan_phy_to_rdev(wpan_phy);
  221. *wpan_dev = NULL;
  222. list_for_each_entry(tmp, &(*rdev)->wpan_dev_list, list) {
  223. if (tmp->identifier == cb->args[1]) {
  224. *wpan_dev = tmp;
  225. break;
  226. }
  227. }
  228. if (!*wpan_dev) {
  229. err = -ENODEV;
  230. goto out_unlock;
  231. }
  232. }
  233. return 0;
  234. out_unlock:
  235. rtnl_unlock();
  236. return err;
  237. }
  238. static void
  239. nl802154_finish_wpan_dev_dump(struct cfg802154_registered_device *rdev)
  240. {
  241. rtnl_unlock();
  242. }
  243. #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
  244. /* message building helper */
  245. static inline void *nl802154hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
  246. int flags, u8 cmd)
  247. {
  248. /* since there is no private header just add the generic one */
  249. return genlmsg_put(skb, portid, seq, &nl802154_fam, flags, cmd);
  250. }
  251. static int
  252. nl802154_put_flags(struct sk_buff *msg, int attr, u32 mask)
  253. {
  254. struct nlattr *nl_flags = nla_nest_start(msg, attr);
  255. int i;
  256. if (!nl_flags)
  257. return -ENOBUFS;
  258. i = 0;
  259. while (mask) {
  260. if ((mask & 1) && nla_put_flag(msg, i))
  261. return -ENOBUFS;
  262. mask >>= 1;
  263. i++;
  264. }
  265. nla_nest_end(msg, nl_flags);
  266. return 0;
  267. }
  268. static int
  269. nl802154_send_wpan_phy_channels(struct cfg802154_registered_device *rdev,
  270. struct sk_buff *msg)
  271. {
  272. struct nlattr *nl_page;
  273. unsigned long page;
  274. nl_page = nla_nest_start(msg, NL802154_ATTR_CHANNELS_SUPPORTED);
  275. if (!nl_page)
  276. return -ENOBUFS;
  277. for (page = 0; page <= IEEE802154_MAX_PAGE; page++) {
  278. if (nla_put_u32(msg, NL802154_ATTR_SUPPORTED_CHANNEL,
  279. rdev->wpan_phy.supported.channels[page]))
  280. return -ENOBUFS;
  281. }
  282. nla_nest_end(msg, nl_page);
  283. return 0;
  284. }
  285. static int
  286. nl802154_put_capabilities(struct sk_buff *msg,
  287. struct cfg802154_registered_device *rdev)
  288. {
  289. const struct wpan_phy_supported *caps = &rdev->wpan_phy.supported;
  290. struct nlattr *nl_caps, *nl_channels;
  291. int i;
  292. nl_caps = nla_nest_start(msg, NL802154_ATTR_WPAN_PHY_CAPS);
  293. if (!nl_caps)
  294. return -ENOBUFS;
  295. nl_channels = nla_nest_start(msg, NL802154_CAP_ATTR_CHANNELS);
  296. if (!nl_channels)
  297. return -ENOBUFS;
  298. for (i = 0; i <= IEEE802154_MAX_PAGE; i++) {
  299. if (caps->channels[i]) {
  300. if (nl802154_put_flags(msg, i, caps->channels[i]))
  301. return -ENOBUFS;
  302. }
  303. }
  304. nla_nest_end(msg, nl_channels);
  305. if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_ED_LEVEL) {
  306. struct nlattr *nl_ed_lvls;
  307. nl_ed_lvls = nla_nest_start(msg,
  308. NL802154_CAP_ATTR_CCA_ED_LEVELS);
  309. if (!nl_ed_lvls)
  310. return -ENOBUFS;
  311. for (i = 0; i < caps->cca_ed_levels_size; i++) {
  312. if (nla_put_s32(msg, i, caps->cca_ed_levels[i]))
  313. return -ENOBUFS;
  314. }
  315. nla_nest_end(msg, nl_ed_lvls);
  316. }
  317. if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER) {
  318. struct nlattr *nl_tx_pwrs;
  319. nl_tx_pwrs = nla_nest_start(msg, NL802154_CAP_ATTR_TX_POWERS);
  320. if (!nl_tx_pwrs)
  321. return -ENOBUFS;
  322. for (i = 0; i < caps->tx_powers_size; i++) {
  323. if (nla_put_s32(msg, i, caps->tx_powers[i]))
  324. return -ENOBUFS;
  325. }
  326. nla_nest_end(msg, nl_tx_pwrs);
  327. }
  328. if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE) {
  329. if (nl802154_put_flags(msg, NL802154_CAP_ATTR_CCA_MODES,
  330. caps->cca_modes) ||
  331. nl802154_put_flags(msg, NL802154_CAP_ATTR_CCA_OPTS,
  332. caps->cca_opts))
  333. return -ENOBUFS;
  334. }
  335. if (nla_put_u8(msg, NL802154_CAP_ATTR_MIN_MINBE, caps->min_minbe) ||
  336. nla_put_u8(msg, NL802154_CAP_ATTR_MAX_MINBE, caps->max_minbe) ||
  337. nla_put_u8(msg, NL802154_CAP_ATTR_MIN_MAXBE, caps->min_maxbe) ||
  338. nla_put_u8(msg, NL802154_CAP_ATTR_MAX_MAXBE, caps->max_maxbe) ||
  339. nla_put_u8(msg, NL802154_CAP_ATTR_MIN_CSMA_BACKOFFS,
  340. caps->min_csma_backoffs) ||
  341. nla_put_u8(msg, NL802154_CAP_ATTR_MAX_CSMA_BACKOFFS,
  342. caps->max_csma_backoffs) ||
  343. nla_put_s8(msg, NL802154_CAP_ATTR_MIN_FRAME_RETRIES,
  344. caps->min_frame_retries) ||
  345. nla_put_s8(msg, NL802154_CAP_ATTR_MAX_FRAME_RETRIES,
  346. caps->max_frame_retries) ||
  347. nl802154_put_flags(msg, NL802154_CAP_ATTR_IFTYPES,
  348. caps->iftypes) ||
  349. nla_put_u32(msg, NL802154_CAP_ATTR_LBT, caps->lbt))
  350. return -ENOBUFS;
  351. nla_nest_end(msg, nl_caps);
  352. return 0;
  353. }
  354. static int nl802154_send_wpan_phy(struct cfg802154_registered_device *rdev,
  355. enum nl802154_commands cmd,
  356. struct sk_buff *msg, u32 portid, u32 seq,
  357. int flags)
  358. {
  359. struct nlattr *nl_cmds;
  360. void *hdr;
  361. int i;
  362. hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
  363. if (!hdr)
  364. return -ENOBUFS;
  365. if (nla_put_u32(msg, NL802154_ATTR_WPAN_PHY, rdev->wpan_phy_idx) ||
  366. nla_put_string(msg, NL802154_ATTR_WPAN_PHY_NAME,
  367. wpan_phy_name(&rdev->wpan_phy)) ||
  368. nla_put_u32(msg, NL802154_ATTR_GENERATION,
  369. cfg802154_rdev_list_generation))
  370. goto nla_put_failure;
  371. if (cmd != NL802154_CMD_NEW_WPAN_PHY)
  372. goto finish;
  373. /* DUMP PHY PIB */
  374. /* current channel settings */
  375. if (nla_put_u8(msg, NL802154_ATTR_PAGE,
  376. rdev->wpan_phy.current_page) ||
  377. nla_put_u8(msg, NL802154_ATTR_CHANNEL,
  378. rdev->wpan_phy.current_channel))
  379. goto nla_put_failure;
  380. /* TODO remove this behaviour, we still keep support it for a while
  381. * so users can change the behaviour to the new one.
  382. */
  383. if (nl802154_send_wpan_phy_channels(rdev, msg))
  384. goto nla_put_failure;
  385. /* cca mode */
  386. if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE) {
  387. if (nla_put_u32(msg, NL802154_ATTR_CCA_MODE,
  388. rdev->wpan_phy.cca.mode))
  389. goto nla_put_failure;
  390. if (rdev->wpan_phy.cca.mode == NL802154_CCA_ENERGY_CARRIER) {
  391. if (nla_put_u32(msg, NL802154_ATTR_CCA_OPT,
  392. rdev->wpan_phy.cca.opt))
  393. goto nla_put_failure;
  394. }
  395. }
  396. if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER) {
  397. if (nla_put_s32(msg, NL802154_ATTR_TX_POWER,
  398. rdev->wpan_phy.transmit_power))
  399. goto nla_put_failure;
  400. }
  401. if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_ED_LEVEL) {
  402. if (nla_put_s32(msg, NL802154_ATTR_CCA_ED_LEVEL,
  403. rdev->wpan_phy.cca_ed_level))
  404. goto nla_put_failure;
  405. }
  406. if (nl802154_put_capabilities(msg, rdev))
  407. goto nla_put_failure;
  408. nl_cmds = nla_nest_start(msg, NL802154_ATTR_SUPPORTED_COMMANDS);
  409. if (!nl_cmds)
  410. goto nla_put_failure;
  411. i = 0;
  412. #define CMD(op, n) \
  413. do { \
  414. if (rdev->ops->op) { \
  415. i++; \
  416. if (nla_put_u32(msg, i, NL802154_CMD_ ## n)) \
  417. goto nla_put_failure; \
  418. } \
  419. } while (0)
  420. CMD(add_virtual_intf, NEW_INTERFACE);
  421. CMD(del_virtual_intf, DEL_INTERFACE);
  422. CMD(set_channel, SET_CHANNEL);
  423. CMD(set_pan_id, SET_PAN_ID);
  424. CMD(set_short_addr, SET_SHORT_ADDR);
  425. CMD(set_backoff_exponent, SET_BACKOFF_EXPONENT);
  426. CMD(set_max_csma_backoffs, SET_MAX_CSMA_BACKOFFS);
  427. CMD(set_max_frame_retries, SET_MAX_FRAME_RETRIES);
  428. CMD(set_lbt_mode, SET_LBT_MODE);
  429. CMD(set_ackreq_default, SET_ACKREQ_DEFAULT);
  430. if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER)
  431. CMD(set_tx_power, SET_TX_POWER);
  432. if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_ED_LEVEL)
  433. CMD(set_cca_ed_level, SET_CCA_ED_LEVEL);
  434. if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE)
  435. CMD(set_cca_mode, SET_CCA_MODE);
  436. #undef CMD
  437. nla_nest_end(msg, nl_cmds);
  438. finish:
  439. genlmsg_end(msg, hdr);
  440. return 0;
  441. nla_put_failure:
  442. genlmsg_cancel(msg, hdr);
  443. return -EMSGSIZE;
  444. }
  445. struct nl802154_dump_wpan_phy_state {
  446. s64 filter_wpan_phy;
  447. long start;
  448. };
  449. static int nl802154_dump_wpan_phy_parse(struct sk_buff *skb,
  450. struct netlink_callback *cb,
  451. struct nl802154_dump_wpan_phy_state *state)
  452. {
  453. struct nlattr **tb = genl_family_attrbuf(&nl802154_fam);
  454. int ret = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl802154_fam.hdrsize, tb,
  455. nl802154_fam.maxattr, nl802154_policy, NULL);
  456. /* TODO check if we can handle error here,
  457. * we have no backward compatibility
  458. */
  459. if (ret)
  460. return 0;
  461. if (tb[NL802154_ATTR_WPAN_PHY])
  462. state->filter_wpan_phy = nla_get_u32(tb[NL802154_ATTR_WPAN_PHY]);
  463. if (tb[NL802154_ATTR_WPAN_DEV])
  464. state->filter_wpan_phy = nla_get_u64(tb[NL802154_ATTR_WPAN_DEV]) >> 32;
  465. if (tb[NL802154_ATTR_IFINDEX]) {
  466. struct net_device *netdev;
  467. struct cfg802154_registered_device *rdev;
  468. int ifidx = nla_get_u32(tb[NL802154_ATTR_IFINDEX]);
  469. netdev = __dev_get_by_index(&init_net, ifidx);
  470. if (!netdev)
  471. return -ENODEV;
  472. if (netdev->ieee802154_ptr) {
  473. rdev = wpan_phy_to_rdev(
  474. netdev->ieee802154_ptr->wpan_phy);
  475. state->filter_wpan_phy = rdev->wpan_phy_idx;
  476. }
  477. }
  478. return 0;
  479. }
  480. static int
  481. nl802154_dump_wpan_phy(struct sk_buff *skb, struct netlink_callback *cb)
  482. {
  483. int idx = 0, ret;
  484. struct nl802154_dump_wpan_phy_state *state = (void *)cb->args[0];
  485. struct cfg802154_registered_device *rdev;
  486. rtnl_lock();
  487. if (!state) {
  488. state = kzalloc(sizeof(*state), GFP_KERNEL);
  489. if (!state) {
  490. rtnl_unlock();
  491. return -ENOMEM;
  492. }
  493. state->filter_wpan_phy = -1;
  494. ret = nl802154_dump_wpan_phy_parse(skb, cb, state);
  495. if (ret) {
  496. kfree(state);
  497. rtnl_unlock();
  498. return ret;
  499. }
  500. cb->args[0] = (long)state;
  501. }
  502. list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
  503. if (!net_eq(wpan_phy_net(&rdev->wpan_phy), sock_net(skb->sk)))
  504. continue;
  505. if (++idx <= state->start)
  506. continue;
  507. if (state->filter_wpan_phy != -1 &&
  508. state->filter_wpan_phy != rdev->wpan_phy_idx)
  509. continue;
  510. /* attempt to fit multiple wpan_phy data chunks into the skb */
  511. ret = nl802154_send_wpan_phy(rdev,
  512. NL802154_CMD_NEW_WPAN_PHY,
  513. skb,
  514. NETLINK_CB(cb->skb).portid,
  515. cb->nlh->nlmsg_seq, NLM_F_MULTI);
  516. if (ret < 0) {
  517. if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
  518. !skb->len && cb->min_dump_alloc < 4096) {
  519. cb->min_dump_alloc = 4096;
  520. rtnl_unlock();
  521. return 1;
  522. }
  523. idx--;
  524. break;
  525. }
  526. break;
  527. }
  528. rtnl_unlock();
  529. state->start = idx;
  530. return skb->len;
  531. }
  532. static int nl802154_dump_wpan_phy_done(struct netlink_callback *cb)
  533. {
  534. kfree((void *)cb->args[0]);
  535. return 0;
  536. }
  537. static int nl802154_get_wpan_phy(struct sk_buff *skb, struct genl_info *info)
  538. {
  539. struct sk_buff *msg;
  540. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  541. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  542. if (!msg)
  543. return -ENOMEM;
  544. if (nl802154_send_wpan_phy(rdev, NL802154_CMD_NEW_WPAN_PHY, msg,
  545. info->snd_portid, info->snd_seq, 0) < 0) {
  546. nlmsg_free(msg);
  547. return -ENOBUFS;
  548. }
  549. return genlmsg_reply(msg, info);
  550. }
  551. static inline u64 wpan_dev_id(struct wpan_dev *wpan_dev)
  552. {
  553. return (u64)wpan_dev->identifier |
  554. ((u64)wpan_phy_to_rdev(wpan_dev->wpan_phy)->wpan_phy_idx << 32);
  555. }
  556. #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
  557. #include <net/ieee802154_netdev.h>
  558. static int
  559. ieee802154_llsec_send_key_id(struct sk_buff *msg,
  560. const struct ieee802154_llsec_key_id *desc)
  561. {
  562. struct nlattr *nl_dev_addr;
  563. if (nla_put_u32(msg, NL802154_KEY_ID_ATTR_MODE, desc->mode))
  564. return -ENOBUFS;
  565. switch (desc->mode) {
  566. case NL802154_KEY_ID_MODE_IMPLICIT:
  567. nl_dev_addr = nla_nest_start(msg, NL802154_KEY_ID_ATTR_IMPLICIT);
  568. if (!nl_dev_addr)
  569. return -ENOBUFS;
  570. if (nla_put_le16(msg, NL802154_DEV_ADDR_ATTR_PAN_ID,
  571. desc->device_addr.pan_id) ||
  572. nla_put_u32(msg, NL802154_DEV_ADDR_ATTR_MODE,
  573. desc->device_addr.mode))
  574. return -ENOBUFS;
  575. switch (desc->device_addr.mode) {
  576. case NL802154_DEV_ADDR_SHORT:
  577. if (nla_put_le16(msg, NL802154_DEV_ADDR_ATTR_SHORT,
  578. desc->device_addr.short_addr))
  579. return -ENOBUFS;
  580. break;
  581. case NL802154_DEV_ADDR_EXTENDED:
  582. if (nla_put_le64(msg, NL802154_DEV_ADDR_ATTR_EXTENDED,
  583. desc->device_addr.extended_addr,
  584. NL802154_DEV_ADDR_ATTR_PAD))
  585. return -ENOBUFS;
  586. break;
  587. default:
  588. /* userspace should handle unknown */
  589. break;
  590. }
  591. nla_nest_end(msg, nl_dev_addr);
  592. break;
  593. case NL802154_KEY_ID_MODE_INDEX:
  594. break;
  595. case NL802154_KEY_ID_MODE_INDEX_SHORT:
  596. /* TODO renmae short_source? */
  597. if (nla_put_le32(msg, NL802154_KEY_ID_ATTR_SOURCE_SHORT,
  598. desc->short_source))
  599. return -ENOBUFS;
  600. break;
  601. case NL802154_KEY_ID_MODE_INDEX_EXTENDED:
  602. if (nla_put_le64(msg, NL802154_KEY_ID_ATTR_SOURCE_EXTENDED,
  603. desc->extended_source,
  604. NL802154_KEY_ID_ATTR_PAD))
  605. return -ENOBUFS;
  606. break;
  607. default:
  608. /* userspace should handle unknown */
  609. break;
  610. }
  611. /* TODO key_id to key_idx ? Check naming */
  612. if (desc->mode != NL802154_KEY_ID_MODE_IMPLICIT) {
  613. if (nla_put_u8(msg, NL802154_KEY_ID_ATTR_INDEX, desc->id))
  614. return -ENOBUFS;
  615. }
  616. return 0;
  617. }
  618. static int nl802154_get_llsec_params(struct sk_buff *msg,
  619. struct cfg802154_registered_device *rdev,
  620. struct wpan_dev *wpan_dev)
  621. {
  622. struct nlattr *nl_key_id;
  623. struct ieee802154_llsec_params params;
  624. int ret;
  625. ret = rdev_get_llsec_params(rdev, wpan_dev, &params);
  626. if (ret < 0)
  627. return ret;
  628. if (nla_put_u8(msg, NL802154_ATTR_SEC_ENABLED, params.enabled) ||
  629. nla_put_u32(msg, NL802154_ATTR_SEC_OUT_LEVEL, params.out_level) ||
  630. nla_put_be32(msg, NL802154_ATTR_SEC_FRAME_COUNTER,
  631. params.frame_counter))
  632. return -ENOBUFS;
  633. nl_key_id = nla_nest_start(msg, NL802154_ATTR_SEC_OUT_KEY_ID);
  634. if (!nl_key_id)
  635. return -ENOBUFS;
  636. ret = ieee802154_llsec_send_key_id(msg, &params.out_key);
  637. if (ret < 0)
  638. return ret;
  639. nla_nest_end(msg, nl_key_id);
  640. return 0;
  641. }
  642. #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
  643. static int
  644. nl802154_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
  645. struct cfg802154_registered_device *rdev,
  646. struct wpan_dev *wpan_dev)
  647. {
  648. struct net_device *dev = wpan_dev->netdev;
  649. void *hdr;
  650. hdr = nl802154hdr_put(msg, portid, seq, flags,
  651. NL802154_CMD_NEW_INTERFACE);
  652. if (!hdr)
  653. return -1;
  654. if (dev &&
  655. (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex) ||
  656. nla_put_string(msg, NL802154_ATTR_IFNAME, dev->name)))
  657. goto nla_put_failure;
  658. if (nla_put_u32(msg, NL802154_ATTR_WPAN_PHY, rdev->wpan_phy_idx) ||
  659. nla_put_u32(msg, NL802154_ATTR_IFTYPE, wpan_dev->iftype) ||
  660. nla_put_u64_64bit(msg, NL802154_ATTR_WPAN_DEV,
  661. wpan_dev_id(wpan_dev), NL802154_ATTR_PAD) ||
  662. nla_put_u32(msg, NL802154_ATTR_GENERATION,
  663. rdev->devlist_generation ^
  664. (cfg802154_rdev_list_generation << 2)))
  665. goto nla_put_failure;
  666. /* address settings */
  667. if (nla_put_le64(msg, NL802154_ATTR_EXTENDED_ADDR,
  668. wpan_dev->extended_addr,
  669. NL802154_ATTR_PAD) ||
  670. nla_put_le16(msg, NL802154_ATTR_SHORT_ADDR,
  671. wpan_dev->short_addr) ||
  672. nla_put_le16(msg, NL802154_ATTR_PAN_ID, wpan_dev->pan_id))
  673. goto nla_put_failure;
  674. /* ARET handling */
  675. if (nla_put_s8(msg, NL802154_ATTR_MAX_FRAME_RETRIES,
  676. wpan_dev->frame_retries) ||
  677. nla_put_u8(msg, NL802154_ATTR_MAX_BE, wpan_dev->max_be) ||
  678. nla_put_u8(msg, NL802154_ATTR_MAX_CSMA_BACKOFFS,
  679. wpan_dev->csma_retries) ||
  680. nla_put_u8(msg, NL802154_ATTR_MIN_BE, wpan_dev->min_be))
  681. goto nla_put_failure;
  682. /* listen before transmit */
  683. if (nla_put_u8(msg, NL802154_ATTR_LBT_MODE, wpan_dev->lbt))
  684. goto nla_put_failure;
  685. /* ackreq default behaviour */
  686. if (nla_put_u8(msg, NL802154_ATTR_ACKREQ_DEFAULT, wpan_dev->ackreq))
  687. goto nla_put_failure;
  688. #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
  689. if (nl802154_get_llsec_params(msg, rdev, wpan_dev) < 0)
  690. goto nla_put_failure;
  691. #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
  692. genlmsg_end(msg, hdr);
  693. return 0;
  694. nla_put_failure:
  695. genlmsg_cancel(msg, hdr);
  696. return -EMSGSIZE;
  697. }
  698. static int
  699. nl802154_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
  700. {
  701. int wp_idx = 0;
  702. int if_idx = 0;
  703. int wp_start = cb->args[0];
  704. int if_start = cb->args[1];
  705. struct cfg802154_registered_device *rdev;
  706. struct wpan_dev *wpan_dev;
  707. rtnl_lock();
  708. list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
  709. if (!net_eq(wpan_phy_net(&rdev->wpan_phy), sock_net(skb->sk)))
  710. continue;
  711. if (wp_idx < wp_start) {
  712. wp_idx++;
  713. continue;
  714. }
  715. if_idx = 0;
  716. list_for_each_entry(wpan_dev, &rdev->wpan_dev_list, list) {
  717. if (if_idx < if_start) {
  718. if_idx++;
  719. continue;
  720. }
  721. if (nl802154_send_iface(skb, NETLINK_CB(cb->skb).portid,
  722. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  723. rdev, wpan_dev) < 0) {
  724. goto out;
  725. }
  726. if_idx++;
  727. }
  728. wp_idx++;
  729. }
  730. out:
  731. rtnl_unlock();
  732. cb->args[0] = wp_idx;
  733. cb->args[1] = if_idx;
  734. return skb->len;
  735. }
  736. static int nl802154_get_interface(struct sk_buff *skb, struct genl_info *info)
  737. {
  738. struct sk_buff *msg;
  739. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  740. struct wpan_dev *wdev = info->user_ptr[1];
  741. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  742. if (!msg)
  743. return -ENOMEM;
  744. if (nl802154_send_iface(msg, info->snd_portid, info->snd_seq, 0,
  745. rdev, wdev) < 0) {
  746. nlmsg_free(msg);
  747. return -ENOBUFS;
  748. }
  749. return genlmsg_reply(msg, info);
  750. }
  751. static int nl802154_new_interface(struct sk_buff *skb, struct genl_info *info)
  752. {
  753. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  754. enum nl802154_iftype type = NL802154_IFTYPE_UNSPEC;
  755. __le64 extended_addr = cpu_to_le64(0x0000000000000000ULL);
  756. /* TODO avoid failing a new interface
  757. * creation due to pending removal?
  758. */
  759. if (!info->attrs[NL802154_ATTR_IFNAME])
  760. return -EINVAL;
  761. if (info->attrs[NL802154_ATTR_IFTYPE]) {
  762. type = nla_get_u32(info->attrs[NL802154_ATTR_IFTYPE]);
  763. if (type > NL802154_IFTYPE_MAX ||
  764. !(rdev->wpan_phy.supported.iftypes & BIT(type)))
  765. return -EINVAL;
  766. }
  767. if (info->attrs[NL802154_ATTR_EXTENDED_ADDR])
  768. extended_addr = nla_get_le64(info->attrs[NL802154_ATTR_EXTENDED_ADDR]);
  769. if (!rdev->ops->add_virtual_intf)
  770. return -EOPNOTSUPP;
  771. return rdev_add_virtual_intf(rdev,
  772. nla_data(info->attrs[NL802154_ATTR_IFNAME]),
  773. NET_NAME_USER, type, extended_addr);
  774. }
  775. static int nl802154_del_interface(struct sk_buff *skb, struct genl_info *info)
  776. {
  777. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  778. struct wpan_dev *wpan_dev = info->user_ptr[1];
  779. if (!rdev->ops->del_virtual_intf)
  780. return -EOPNOTSUPP;
  781. /* If we remove a wpan device without a netdev then clear
  782. * user_ptr[1] so that nl802154_post_doit won't dereference it
  783. * to check if it needs to do dev_put(). Otherwise it crashes
  784. * since the wpan_dev has been freed, unlike with a netdev where
  785. * we need the dev_put() for the netdev to really be freed.
  786. */
  787. if (!wpan_dev->netdev)
  788. info->user_ptr[1] = NULL;
  789. return rdev_del_virtual_intf(rdev, wpan_dev);
  790. }
  791. static int nl802154_set_channel(struct sk_buff *skb, struct genl_info *info)
  792. {
  793. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  794. u8 channel, page;
  795. if (!info->attrs[NL802154_ATTR_PAGE] ||
  796. !info->attrs[NL802154_ATTR_CHANNEL])
  797. return -EINVAL;
  798. page = nla_get_u8(info->attrs[NL802154_ATTR_PAGE]);
  799. channel = nla_get_u8(info->attrs[NL802154_ATTR_CHANNEL]);
  800. /* check 802.15.4 constraints */
  801. if (page > IEEE802154_MAX_PAGE || channel > IEEE802154_MAX_CHANNEL ||
  802. !(rdev->wpan_phy.supported.channels[page] & BIT(channel)))
  803. return -EINVAL;
  804. return rdev_set_channel(rdev, page, channel);
  805. }
  806. static int nl802154_set_cca_mode(struct sk_buff *skb, struct genl_info *info)
  807. {
  808. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  809. struct wpan_phy_cca cca;
  810. if (!(rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE))
  811. return -EOPNOTSUPP;
  812. if (!info->attrs[NL802154_ATTR_CCA_MODE])
  813. return -EINVAL;
  814. cca.mode = nla_get_u32(info->attrs[NL802154_ATTR_CCA_MODE]);
  815. /* checking 802.15.4 constraints */
  816. if (cca.mode < NL802154_CCA_ENERGY ||
  817. cca.mode > NL802154_CCA_ATTR_MAX ||
  818. !(rdev->wpan_phy.supported.cca_modes & BIT(cca.mode)))
  819. return -EINVAL;
  820. if (cca.mode == NL802154_CCA_ENERGY_CARRIER) {
  821. if (!info->attrs[NL802154_ATTR_CCA_OPT])
  822. return -EINVAL;
  823. cca.opt = nla_get_u32(info->attrs[NL802154_ATTR_CCA_OPT]);
  824. if (cca.opt > NL802154_CCA_OPT_ATTR_MAX ||
  825. !(rdev->wpan_phy.supported.cca_opts & BIT(cca.opt)))
  826. return -EINVAL;
  827. }
  828. return rdev_set_cca_mode(rdev, &cca);
  829. }
  830. static int nl802154_set_cca_ed_level(struct sk_buff *skb, struct genl_info *info)
  831. {
  832. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  833. s32 ed_level;
  834. int i;
  835. if (!(rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_ED_LEVEL))
  836. return -EOPNOTSUPP;
  837. if (!info->attrs[NL802154_ATTR_CCA_ED_LEVEL])
  838. return -EINVAL;
  839. ed_level = nla_get_s32(info->attrs[NL802154_ATTR_CCA_ED_LEVEL]);
  840. for (i = 0; i < rdev->wpan_phy.supported.cca_ed_levels_size; i++) {
  841. if (ed_level == rdev->wpan_phy.supported.cca_ed_levels[i])
  842. return rdev_set_cca_ed_level(rdev, ed_level);
  843. }
  844. return -EINVAL;
  845. }
  846. static int nl802154_set_tx_power(struct sk_buff *skb, struct genl_info *info)
  847. {
  848. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  849. s32 power;
  850. int i;
  851. if (!(rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER))
  852. return -EOPNOTSUPP;
  853. if (!info->attrs[NL802154_ATTR_TX_POWER])
  854. return -EINVAL;
  855. power = nla_get_s32(info->attrs[NL802154_ATTR_TX_POWER]);
  856. for (i = 0; i < rdev->wpan_phy.supported.tx_powers_size; i++) {
  857. if (power == rdev->wpan_phy.supported.tx_powers[i])
  858. return rdev_set_tx_power(rdev, power);
  859. }
  860. return -EINVAL;
  861. }
  862. static int nl802154_set_pan_id(struct sk_buff *skb, struct genl_info *info)
  863. {
  864. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  865. struct net_device *dev = info->user_ptr[1];
  866. struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  867. __le16 pan_id;
  868. /* conflict here while tx/rx calls */
  869. if (netif_running(dev))
  870. return -EBUSY;
  871. if (wpan_dev->lowpan_dev) {
  872. if (netif_running(wpan_dev->lowpan_dev))
  873. return -EBUSY;
  874. }
  875. /* don't change address fields on monitor */
  876. if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR ||
  877. !info->attrs[NL802154_ATTR_PAN_ID])
  878. return -EINVAL;
  879. pan_id = nla_get_le16(info->attrs[NL802154_ATTR_PAN_ID]);
  880. /* TODO
  881. * I am not sure about to check here on broadcast pan_id.
  882. * Broadcast is a valid setting, comment from 802.15.4:
  883. * If this value is 0xffff, the device is not associated.
  884. *
  885. * This could useful to simple deassociate an device.
  886. */
  887. if (pan_id == cpu_to_le16(IEEE802154_PAN_ID_BROADCAST))
  888. return -EINVAL;
  889. return rdev_set_pan_id(rdev, wpan_dev, pan_id);
  890. }
  891. static int nl802154_set_short_addr(struct sk_buff *skb, struct genl_info *info)
  892. {
  893. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  894. struct net_device *dev = info->user_ptr[1];
  895. struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  896. __le16 short_addr;
  897. /* conflict here while tx/rx calls */
  898. if (netif_running(dev))
  899. return -EBUSY;
  900. if (wpan_dev->lowpan_dev) {
  901. if (netif_running(wpan_dev->lowpan_dev))
  902. return -EBUSY;
  903. }
  904. /* don't change address fields on monitor */
  905. if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR ||
  906. !info->attrs[NL802154_ATTR_SHORT_ADDR])
  907. return -EINVAL;
  908. short_addr = nla_get_le16(info->attrs[NL802154_ATTR_SHORT_ADDR]);
  909. /* TODO
  910. * I am not sure about to check here on broadcast short_addr.
  911. * Broadcast is a valid setting, comment from 802.15.4:
  912. * A value of 0xfffe indicates that the device has
  913. * associated but has not been allocated an address. A
  914. * value of 0xffff indicates that the device does not
  915. * have a short address.
  916. *
  917. * I think we should allow to set these settings but
  918. * don't allow to allow socket communication with it.
  919. */
  920. if (short_addr == cpu_to_le16(IEEE802154_ADDR_SHORT_UNSPEC) ||
  921. short_addr == cpu_to_le16(IEEE802154_ADDR_SHORT_BROADCAST))
  922. return -EINVAL;
  923. return rdev_set_short_addr(rdev, wpan_dev, short_addr);
  924. }
  925. static int
  926. nl802154_set_backoff_exponent(struct sk_buff *skb, struct genl_info *info)
  927. {
  928. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  929. struct net_device *dev = info->user_ptr[1];
  930. struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  931. u8 min_be, max_be;
  932. /* should be set on netif open inside phy settings */
  933. if (netif_running(dev))
  934. return -EBUSY;
  935. if (!info->attrs[NL802154_ATTR_MIN_BE] ||
  936. !info->attrs[NL802154_ATTR_MAX_BE])
  937. return -EINVAL;
  938. min_be = nla_get_u8(info->attrs[NL802154_ATTR_MIN_BE]);
  939. max_be = nla_get_u8(info->attrs[NL802154_ATTR_MAX_BE]);
  940. /* check 802.15.4 constraints */
  941. if (min_be < rdev->wpan_phy.supported.min_minbe ||
  942. min_be > rdev->wpan_phy.supported.max_minbe ||
  943. max_be < rdev->wpan_phy.supported.min_maxbe ||
  944. max_be > rdev->wpan_phy.supported.max_maxbe ||
  945. min_be > max_be)
  946. return -EINVAL;
  947. return rdev_set_backoff_exponent(rdev, wpan_dev, min_be, max_be);
  948. }
  949. static int
  950. nl802154_set_max_csma_backoffs(struct sk_buff *skb, struct genl_info *info)
  951. {
  952. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  953. struct net_device *dev = info->user_ptr[1];
  954. struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  955. u8 max_csma_backoffs;
  956. /* conflict here while other running iface settings */
  957. if (netif_running(dev))
  958. return -EBUSY;
  959. if (!info->attrs[NL802154_ATTR_MAX_CSMA_BACKOFFS])
  960. return -EINVAL;
  961. max_csma_backoffs = nla_get_u8(
  962. info->attrs[NL802154_ATTR_MAX_CSMA_BACKOFFS]);
  963. /* check 802.15.4 constraints */
  964. if (max_csma_backoffs < rdev->wpan_phy.supported.min_csma_backoffs ||
  965. max_csma_backoffs > rdev->wpan_phy.supported.max_csma_backoffs)
  966. return -EINVAL;
  967. return rdev_set_max_csma_backoffs(rdev, wpan_dev, max_csma_backoffs);
  968. }
  969. static int
  970. nl802154_set_max_frame_retries(struct sk_buff *skb, struct genl_info *info)
  971. {
  972. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  973. struct net_device *dev = info->user_ptr[1];
  974. struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  975. s8 max_frame_retries;
  976. if (netif_running(dev))
  977. return -EBUSY;
  978. if (!info->attrs[NL802154_ATTR_MAX_FRAME_RETRIES])
  979. return -EINVAL;
  980. max_frame_retries = nla_get_s8(
  981. info->attrs[NL802154_ATTR_MAX_FRAME_RETRIES]);
  982. /* check 802.15.4 constraints */
  983. if (max_frame_retries < rdev->wpan_phy.supported.min_frame_retries ||
  984. max_frame_retries > rdev->wpan_phy.supported.max_frame_retries)
  985. return -EINVAL;
  986. return rdev_set_max_frame_retries(rdev, wpan_dev, max_frame_retries);
  987. }
  988. static int nl802154_set_lbt_mode(struct sk_buff *skb, struct genl_info *info)
  989. {
  990. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  991. struct net_device *dev = info->user_ptr[1];
  992. struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  993. int mode;
  994. if (netif_running(dev))
  995. return -EBUSY;
  996. if (!info->attrs[NL802154_ATTR_LBT_MODE])
  997. return -EINVAL;
  998. mode = nla_get_u8(info->attrs[NL802154_ATTR_LBT_MODE]);
  999. if (mode != 0 && mode != 1)
  1000. return -EINVAL;
  1001. if (!wpan_phy_supported_bool(mode, rdev->wpan_phy.supported.lbt))
  1002. return -EINVAL;
  1003. return rdev_set_lbt_mode(rdev, wpan_dev, mode);
  1004. }
  1005. static int
  1006. nl802154_set_ackreq_default(struct sk_buff *skb, struct genl_info *info)
  1007. {
  1008. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  1009. struct net_device *dev = info->user_ptr[1];
  1010. struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  1011. int ackreq;
  1012. if (netif_running(dev))
  1013. return -EBUSY;
  1014. if (!info->attrs[NL802154_ATTR_ACKREQ_DEFAULT])
  1015. return -EINVAL;
  1016. ackreq = nla_get_u8(info->attrs[NL802154_ATTR_ACKREQ_DEFAULT]);
  1017. if (ackreq != 0 && ackreq != 1)
  1018. return -EINVAL;
  1019. return rdev_set_ackreq_default(rdev, wpan_dev, ackreq);
  1020. }
  1021. static int nl802154_wpan_phy_netns(struct sk_buff *skb, struct genl_info *info)
  1022. {
  1023. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  1024. struct net *net;
  1025. int err;
  1026. if (info->attrs[NL802154_ATTR_PID]) {
  1027. u32 pid = nla_get_u32(info->attrs[NL802154_ATTR_PID]);
  1028. net = get_net_ns_by_pid(pid);
  1029. } else if (info->attrs[NL802154_ATTR_NETNS_FD]) {
  1030. u32 fd = nla_get_u32(info->attrs[NL802154_ATTR_NETNS_FD]);
  1031. net = get_net_ns_by_fd(fd);
  1032. } else {
  1033. return -EINVAL;
  1034. }
  1035. if (IS_ERR(net))
  1036. return PTR_ERR(net);
  1037. err = 0;
  1038. /* check if anything to do */
  1039. if (!net_eq(wpan_phy_net(&rdev->wpan_phy), net))
  1040. err = cfg802154_switch_netns(rdev, net);
  1041. put_net(net);
  1042. return err;
  1043. }
  1044. #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
  1045. static const struct nla_policy nl802154_dev_addr_policy[NL802154_DEV_ADDR_ATTR_MAX + 1] = {
  1046. [NL802154_DEV_ADDR_ATTR_PAN_ID] = { .type = NLA_U16 },
  1047. [NL802154_DEV_ADDR_ATTR_MODE] = { .type = NLA_U32 },
  1048. [NL802154_DEV_ADDR_ATTR_SHORT] = { .type = NLA_U16 },
  1049. [NL802154_DEV_ADDR_ATTR_EXTENDED] = { .type = NLA_U64 },
  1050. };
  1051. static int
  1052. ieee802154_llsec_parse_dev_addr(struct nlattr *nla,
  1053. struct ieee802154_addr *addr)
  1054. {
  1055. struct nlattr *attrs[NL802154_DEV_ADDR_ATTR_MAX + 1];
  1056. if (!nla || nla_parse_nested(attrs, NL802154_DEV_ADDR_ATTR_MAX, nla,
  1057. nl802154_dev_addr_policy, NULL))
  1058. return -EINVAL;
  1059. if (!attrs[NL802154_DEV_ADDR_ATTR_PAN_ID] ||
  1060. !attrs[NL802154_DEV_ADDR_ATTR_MODE] ||
  1061. !(attrs[NL802154_DEV_ADDR_ATTR_SHORT] ||
  1062. attrs[NL802154_DEV_ADDR_ATTR_EXTENDED]))
  1063. return -EINVAL;
  1064. addr->pan_id = nla_get_le16(attrs[NL802154_DEV_ADDR_ATTR_PAN_ID]);
  1065. addr->mode = nla_get_u32(attrs[NL802154_DEV_ADDR_ATTR_MODE]);
  1066. switch (addr->mode) {
  1067. case NL802154_DEV_ADDR_SHORT:
  1068. addr->short_addr = nla_get_le16(attrs[NL802154_DEV_ADDR_ATTR_SHORT]);
  1069. break;
  1070. case NL802154_DEV_ADDR_EXTENDED:
  1071. addr->extended_addr = nla_get_le64(attrs[NL802154_DEV_ADDR_ATTR_EXTENDED]);
  1072. break;
  1073. default:
  1074. return -EINVAL;
  1075. }
  1076. return 0;
  1077. }
  1078. static const struct nla_policy nl802154_key_id_policy[NL802154_KEY_ID_ATTR_MAX + 1] = {
  1079. [NL802154_KEY_ID_ATTR_MODE] = { .type = NLA_U32 },
  1080. [NL802154_KEY_ID_ATTR_INDEX] = { .type = NLA_U8 },
  1081. [NL802154_KEY_ID_ATTR_IMPLICIT] = { .type = NLA_NESTED },
  1082. [NL802154_KEY_ID_ATTR_SOURCE_SHORT] = { .type = NLA_U32 },
  1083. [NL802154_KEY_ID_ATTR_SOURCE_EXTENDED] = { .type = NLA_U64 },
  1084. };
  1085. static int
  1086. ieee802154_llsec_parse_key_id(struct nlattr *nla,
  1087. struct ieee802154_llsec_key_id *desc)
  1088. {
  1089. struct nlattr *attrs[NL802154_KEY_ID_ATTR_MAX + 1];
  1090. if (!nla || nla_parse_nested(attrs, NL802154_KEY_ID_ATTR_MAX, nla,
  1091. nl802154_key_id_policy, NULL))
  1092. return -EINVAL;
  1093. if (!attrs[NL802154_KEY_ID_ATTR_MODE])
  1094. return -EINVAL;
  1095. desc->mode = nla_get_u32(attrs[NL802154_KEY_ID_ATTR_MODE]);
  1096. switch (desc->mode) {
  1097. case NL802154_KEY_ID_MODE_IMPLICIT:
  1098. if (!attrs[NL802154_KEY_ID_ATTR_IMPLICIT])
  1099. return -EINVAL;
  1100. if (ieee802154_llsec_parse_dev_addr(attrs[NL802154_KEY_ID_ATTR_IMPLICIT],
  1101. &desc->device_addr) < 0)
  1102. return -EINVAL;
  1103. break;
  1104. case NL802154_KEY_ID_MODE_INDEX:
  1105. break;
  1106. case NL802154_KEY_ID_MODE_INDEX_SHORT:
  1107. if (!attrs[NL802154_KEY_ID_ATTR_SOURCE_SHORT])
  1108. return -EINVAL;
  1109. desc->short_source = nla_get_le32(attrs[NL802154_KEY_ID_ATTR_SOURCE_SHORT]);
  1110. break;
  1111. case NL802154_KEY_ID_MODE_INDEX_EXTENDED:
  1112. if (!attrs[NL802154_KEY_ID_ATTR_SOURCE_EXTENDED])
  1113. return -EINVAL;
  1114. desc->extended_source = nla_get_le64(attrs[NL802154_KEY_ID_ATTR_SOURCE_EXTENDED]);
  1115. break;
  1116. default:
  1117. return -EINVAL;
  1118. }
  1119. if (desc->mode != NL802154_KEY_ID_MODE_IMPLICIT) {
  1120. if (!attrs[NL802154_KEY_ID_ATTR_INDEX])
  1121. return -EINVAL;
  1122. /* TODO change id to idx */
  1123. desc->id = nla_get_u8(attrs[NL802154_KEY_ID_ATTR_INDEX]);
  1124. }
  1125. return 0;
  1126. }
  1127. static int nl802154_set_llsec_params(struct sk_buff *skb,
  1128. struct genl_info *info)
  1129. {
  1130. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  1131. struct net_device *dev = info->user_ptr[1];
  1132. struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  1133. struct ieee802154_llsec_params params;
  1134. u32 changed = 0;
  1135. int ret;
  1136. if (info->attrs[NL802154_ATTR_SEC_ENABLED]) {
  1137. u8 enabled;
  1138. enabled = nla_get_u8(info->attrs[NL802154_ATTR_SEC_ENABLED]);
  1139. if (enabled != 0 && enabled != 1)
  1140. return -EINVAL;
  1141. params.enabled = nla_get_u8(info->attrs[NL802154_ATTR_SEC_ENABLED]);
  1142. changed |= IEEE802154_LLSEC_PARAM_ENABLED;
  1143. }
  1144. if (info->attrs[NL802154_ATTR_SEC_OUT_KEY_ID]) {
  1145. ret = ieee802154_llsec_parse_key_id(info->attrs[NL802154_ATTR_SEC_OUT_KEY_ID],
  1146. &params.out_key);
  1147. if (ret < 0)
  1148. return ret;
  1149. changed |= IEEE802154_LLSEC_PARAM_OUT_KEY;
  1150. }
  1151. if (info->attrs[NL802154_ATTR_SEC_OUT_LEVEL]) {
  1152. params.out_level = nla_get_u32(info->attrs[NL802154_ATTR_SEC_OUT_LEVEL]);
  1153. if (params.out_level > NL802154_SECLEVEL_MAX)
  1154. return -EINVAL;
  1155. changed |= IEEE802154_LLSEC_PARAM_OUT_LEVEL;
  1156. }
  1157. if (info->attrs[NL802154_ATTR_SEC_FRAME_COUNTER]) {
  1158. params.frame_counter = nla_get_be32(info->attrs[NL802154_ATTR_SEC_FRAME_COUNTER]);
  1159. changed |= IEEE802154_LLSEC_PARAM_FRAME_COUNTER;
  1160. }
  1161. return rdev_set_llsec_params(rdev, wpan_dev, &params, changed);
  1162. }
  1163. static int nl802154_send_key(struct sk_buff *msg, u32 cmd, u32 portid,
  1164. u32 seq, int flags,
  1165. struct cfg802154_registered_device *rdev,
  1166. struct net_device *dev,
  1167. const struct ieee802154_llsec_key_entry *key)
  1168. {
  1169. void *hdr;
  1170. u32 commands[NL802154_CMD_FRAME_NR_IDS / 32];
  1171. struct nlattr *nl_key, *nl_key_id;
  1172. hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
  1173. if (!hdr)
  1174. return -1;
  1175. if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
  1176. goto nla_put_failure;
  1177. nl_key = nla_nest_start(msg, NL802154_ATTR_SEC_KEY);
  1178. if (!nl_key)
  1179. goto nla_put_failure;
  1180. nl_key_id = nla_nest_start(msg, NL802154_KEY_ATTR_ID);
  1181. if (!nl_key_id)
  1182. goto nla_put_failure;
  1183. if (ieee802154_llsec_send_key_id(msg, &key->id) < 0)
  1184. goto nla_put_failure;
  1185. nla_nest_end(msg, nl_key_id);
  1186. if (nla_put_u8(msg, NL802154_KEY_ATTR_USAGE_FRAMES,
  1187. key->key->frame_types))
  1188. goto nla_put_failure;
  1189. if (key->key->frame_types & BIT(NL802154_FRAME_CMD)) {
  1190. /* TODO for each nested */
  1191. memset(commands, 0, sizeof(commands));
  1192. commands[7] = key->key->cmd_frame_ids;
  1193. if (nla_put(msg, NL802154_KEY_ATTR_USAGE_CMDS,
  1194. sizeof(commands), commands))
  1195. goto nla_put_failure;
  1196. }
  1197. if (nla_put(msg, NL802154_KEY_ATTR_BYTES, NL802154_KEY_SIZE,
  1198. key->key->key))
  1199. goto nla_put_failure;
  1200. nla_nest_end(msg, nl_key);
  1201. genlmsg_end(msg, hdr);
  1202. return 0;
  1203. nla_put_failure:
  1204. genlmsg_cancel(msg, hdr);
  1205. return -EMSGSIZE;
  1206. }
  1207. static int
  1208. nl802154_dump_llsec_key(struct sk_buff *skb, struct netlink_callback *cb)
  1209. {
  1210. struct cfg802154_registered_device *rdev = NULL;
  1211. struct ieee802154_llsec_key_entry *key;
  1212. struct ieee802154_llsec_table *table;
  1213. struct wpan_dev *wpan_dev;
  1214. int err;
  1215. err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
  1216. if (err)
  1217. return err;
  1218. if (!wpan_dev->netdev) {
  1219. err = -EINVAL;
  1220. goto out_err;
  1221. }
  1222. rdev_lock_llsec_table(rdev, wpan_dev);
  1223. rdev_get_llsec_table(rdev, wpan_dev, &table);
  1224. /* TODO make it like station dump */
  1225. if (cb->args[2])
  1226. goto out;
  1227. list_for_each_entry(key, &table->keys, list) {
  1228. if (nl802154_send_key(skb, NL802154_CMD_NEW_SEC_KEY,
  1229. NETLINK_CB(cb->skb).portid,
  1230. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1231. rdev, wpan_dev->netdev, key) < 0) {
  1232. /* TODO */
  1233. err = -EIO;
  1234. rdev_unlock_llsec_table(rdev, wpan_dev);
  1235. goto out_err;
  1236. }
  1237. }
  1238. cb->args[2] = 1;
  1239. out:
  1240. rdev_unlock_llsec_table(rdev, wpan_dev);
  1241. err = skb->len;
  1242. out_err:
  1243. nl802154_finish_wpan_dev_dump(rdev);
  1244. return err;
  1245. }
  1246. static const struct nla_policy nl802154_key_policy[NL802154_KEY_ATTR_MAX + 1] = {
  1247. [NL802154_KEY_ATTR_ID] = { NLA_NESTED },
  1248. /* TODO handle it as for_each_nested and NLA_FLAG? */
  1249. [NL802154_KEY_ATTR_USAGE_FRAMES] = { NLA_U8 },
  1250. /* TODO handle it as for_each_nested, not static array? */
  1251. [NL802154_KEY_ATTR_USAGE_CMDS] = { .len = NL802154_CMD_FRAME_NR_IDS / 8 },
  1252. [NL802154_KEY_ATTR_BYTES] = { .len = NL802154_KEY_SIZE },
  1253. };
  1254. static int nl802154_add_llsec_key(struct sk_buff *skb, struct genl_info *info)
  1255. {
  1256. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  1257. struct net_device *dev = info->user_ptr[1];
  1258. struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  1259. struct nlattr *attrs[NL802154_KEY_ATTR_MAX + 1];
  1260. struct ieee802154_llsec_key key = { };
  1261. struct ieee802154_llsec_key_id id = { };
  1262. u32 commands[NL802154_CMD_FRAME_NR_IDS / 32] = { };
  1263. if (nla_parse_nested(attrs, NL802154_KEY_ATTR_MAX,
  1264. info->attrs[NL802154_ATTR_SEC_KEY],
  1265. nl802154_key_policy, info->extack))
  1266. return -EINVAL;
  1267. if (!attrs[NL802154_KEY_ATTR_USAGE_FRAMES] ||
  1268. !attrs[NL802154_KEY_ATTR_BYTES])
  1269. return -EINVAL;
  1270. if (ieee802154_llsec_parse_key_id(attrs[NL802154_KEY_ATTR_ID], &id) < 0)
  1271. return -ENOBUFS;
  1272. key.frame_types = nla_get_u8(attrs[NL802154_KEY_ATTR_USAGE_FRAMES]);
  1273. if (key.frame_types > BIT(NL802154_FRAME_MAX) ||
  1274. ((key.frame_types & BIT(NL802154_FRAME_CMD)) &&
  1275. !attrs[NL802154_KEY_ATTR_USAGE_CMDS]))
  1276. return -EINVAL;
  1277. if (attrs[NL802154_KEY_ATTR_USAGE_CMDS]) {
  1278. /* TODO for each nested */
  1279. nla_memcpy(commands, attrs[NL802154_KEY_ATTR_USAGE_CMDS],
  1280. NL802154_CMD_FRAME_NR_IDS / 8);
  1281. /* TODO understand the -EINVAL logic here? last condition */
  1282. if (commands[0] || commands[1] || commands[2] || commands[3] ||
  1283. commands[4] || commands[5] || commands[6] ||
  1284. commands[7] > BIT(NL802154_CMD_FRAME_MAX))
  1285. return -EINVAL;
  1286. key.cmd_frame_ids = commands[7];
  1287. } else {
  1288. key.cmd_frame_ids = 0;
  1289. }
  1290. nla_memcpy(key.key, attrs[NL802154_KEY_ATTR_BYTES], NL802154_KEY_SIZE);
  1291. if (ieee802154_llsec_parse_key_id(attrs[NL802154_KEY_ATTR_ID], &id) < 0)
  1292. return -ENOBUFS;
  1293. return rdev_add_llsec_key(rdev, wpan_dev, &id, &key);
  1294. }
  1295. static int nl802154_del_llsec_key(struct sk_buff *skb, struct genl_info *info)
  1296. {
  1297. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  1298. struct net_device *dev = info->user_ptr[1];
  1299. struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  1300. struct nlattr *attrs[NL802154_KEY_ATTR_MAX + 1];
  1301. struct ieee802154_llsec_key_id id;
  1302. if (nla_parse_nested(attrs, NL802154_KEY_ATTR_MAX,
  1303. info->attrs[NL802154_ATTR_SEC_KEY],
  1304. nl802154_key_policy, info->extack))
  1305. return -EINVAL;
  1306. if (ieee802154_llsec_parse_key_id(attrs[NL802154_KEY_ATTR_ID], &id) < 0)
  1307. return -ENOBUFS;
  1308. return rdev_del_llsec_key(rdev, wpan_dev, &id);
  1309. }
  1310. static int nl802154_send_device(struct sk_buff *msg, u32 cmd, u32 portid,
  1311. u32 seq, int flags,
  1312. struct cfg802154_registered_device *rdev,
  1313. struct net_device *dev,
  1314. const struct ieee802154_llsec_device *dev_desc)
  1315. {
  1316. void *hdr;
  1317. struct nlattr *nl_device;
  1318. hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
  1319. if (!hdr)
  1320. return -1;
  1321. if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
  1322. goto nla_put_failure;
  1323. nl_device = nla_nest_start(msg, NL802154_ATTR_SEC_DEVICE);
  1324. if (!nl_device)
  1325. goto nla_put_failure;
  1326. if (nla_put_u32(msg, NL802154_DEV_ATTR_FRAME_COUNTER,
  1327. dev_desc->frame_counter) ||
  1328. nla_put_le16(msg, NL802154_DEV_ATTR_PAN_ID, dev_desc->pan_id) ||
  1329. nla_put_le16(msg, NL802154_DEV_ATTR_SHORT_ADDR,
  1330. dev_desc->short_addr) ||
  1331. nla_put_le64(msg, NL802154_DEV_ATTR_EXTENDED_ADDR,
  1332. dev_desc->hwaddr, NL802154_DEV_ATTR_PAD) ||
  1333. nla_put_u8(msg, NL802154_DEV_ATTR_SECLEVEL_EXEMPT,
  1334. dev_desc->seclevel_exempt) ||
  1335. nla_put_u32(msg, NL802154_DEV_ATTR_KEY_MODE, dev_desc->key_mode))
  1336. goto nla_put_failure;
  1337. nla_nest_end(msg, nl_device);
  1338. genlmsg_end(msg, hdr);
  1339. return 0;
  1340. nla_put_failure:
  1341. genlmsg_cancel(msg, hdr);
  1342. return -EMSGSIZE;
  1343. }
  1344. static int
  1345. nl802154_dump_llsec_dev(struct sk_buff *skb, struct netlink_callback *cb)
  1346. {
  1347. struct cfg802154_registered_device *rdev = NULL;
  1348. struct ieee802154_llsec_device *dev;
  1349. struct ieee802154_llsec_table *table;
  1350. struct wpan_dev *wpan_dev;
  1351. int err;
  1352. err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
  1353. if (err)
  1354. return err;
  1355. if (!wpan_dev->netdev) {
  1356. err = -EINVAL;
  1357. goto out_err;
  1358. }
  1359. rdev_lock_llsec_table(rdev, wpan_dev);
  1360. rdev_get_llsec_table(rdev, wpan_dev, &table);
  1361. /* TODO make it like station dump */
  1362. if (cb->args[2])
  1363. goto out;
  1364. list_for_each_entry(dev, &table->devices, list) {
  1365. if (nl802154_send_device(skb, NL802154_CMD_NEW_SEC_LEVEL,
  1366. NETLINK_CB(cb->skb).portid,
  1367. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1368. rdev, wpan_dev->netdev, dev) < 0) {
  1369. /* TODO */
  1370. err = -EIO;
  1371. rdev_unlock_llsec_table(rdev, wpan_dev);
  1372. goto out_err;
  1373. }
  1374. }
  1375. cb->args[2] = 1;
  1376. out:
  1377. rdev_unlock_llsec_table(rdev, wpan_dev);
  1378. err = skb->len;
  1379. out_err:
  1380. nl802154_finish_wpan_dev_dump(rdev);
  1381. return err;
  1382. }
  1383. static const struct nla_policy nl802154_dev_policy[NL802154_DEV_ATTR_MAX + 1] = {
  1384. [NL802154_DEV_ATTR_FRAME_COUNTER] = { NLA_U32 },
  1385. [NL802154_DEV_ATTR_PAN_ID] = { .type = NLA_U16 },
  1386. [NL802154_DEV_ATTR_SHORT_ADDR] = { .type = NLA_U16 },
  1387. [NL802154_DEV_ATTR_EXTENDED_ADDR] = { .type = NLA_U64 },
  1388. [NL802154_DEV_ATTR_SECLEVEL_EXEMPT] = { NLA_U8 },
  1389. [NL802154_DEV_ATTR_KEY_MODE] = { NLA_U32 },
  1390. };
  1391. static int
  1392. ieee802154_llsec_parse_device(struct nlattr *nla,
  1393. struct ieee802154_llsec_device *dev)
  1394. {
  1395. struct nlattr *attrs[NL802154_DEV_ATTR_MAX + 1];
  1396. if (!nla || nla_parse_nested(attrs, NL802154_DEV_ATTR_MAX,
  1397. nla, nl802154_dev_policy, NULL))
  1398. return -EINVAL;
  1399. memset(dev, 0, sizeof(*dev));
  1400. if (!attrs[NL802154_DEV_ATTR_FRAME_COUNTER] ||
  1401. !attrs[NL802154_DEV_ATTR_PAN_ID] ||
  1402. !attrs[NL802154_DEV_ATTR_SHORT_ADDR] ||
  1403. !attrs[NL802154_DEV_ATTR_EXTENDED_ADDR] ||
  1404. !attrs[NL802154_DEV_ATTR_SECLEVEL_EXEMPT] ||
  1405. !attrs[NL802154_DEV_ATTR_KEY_MODE])
  1406. return -EINVAL;
  1407. /* TODO be32 */
  1408. dev->frame_counter = nla_get_u32(attrs[NL802154_DEV_ATTR_FRAME_COUNTER]);
  1409. dev->pan_id = nla_get_le16(attrs[NL802154_DEV_ATTR_PAN_ID]);
  1410. dev->short_addr = nla_get_le16(attrs[NL802154_DEV_ATTR_SHORT_ADDR]);
  1411. /* TODO rename hwaddr to extended_addr */
  1412. dev->hwaddr = nla_get_le64(attrs[NL802154_DEV_ATTR_EXTENDED_ADDR]);
  1413. dev->seclevel_exempt = nla_get_u8(attrs[NL802154_DEV_ATTR_SECLEVEL_EXEMPT]);
  1414. dev->key_mode = nla_get_u32(attrs[NL802154_DEV_ATTR_KEY_MODE]);
  1415. if (dev->key_mode > NL802154_DEVKEY_MAX ||
  1416. (dev->seclevel_exempt != 0 && dev->seclevel_exempt != 1))
  1417. return -EINVAL;
  1418. return 0;
  1419. }
  1420. static int nl802154_add_llsec_dev(struct sk_buff *skb, struct genl_info *info)
  1421. {
  1422. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  1423. struct net_device *dev = info->user_ptr[1];
  1424. struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  1425. struct ieee802154_llsec_device dev_desc;
  1426. if (ieee802154_llsec_parse_device(info->attrs[NL802154_ATTR_SEC_DEVICE],
  1427. &dev_desc) < 0)
  1428. return -EINVAL;
  1429. return rdev_add_device(rdev, wpan_dev, &dev_desc);
  1430. }
  1431. static int nl802154_del_llsec_dev(struct sk_buff *skb, struct genl_info *info)
  1432. {
  1433. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  1434. struct net_device *dev = info->user_ptr[1];
  1435. struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  1436. struct nlattr *attrs[NL802154_DEV_ATTR_MAX + 1];
  1437. __le64 extended_addr;
  1438. if (nla_parse_nested(attrs, NL802154_DEV_ATTR_MAX,
  1439. info->attrs[NL802154_ATTR_SEC_DEVICE],
  1440. nl802154_dev_policy, info->extack))
  1441. return -EINVAL;
  1442. if (!attrs[NL802154_DEV_ATTR_EXTENDED_ADDR])
  1443. return -EINVAL;
  1444. extended_addr = nla_get_le64(attrs[NL802154_DEV_ATTR_EXTENDED_ADDR]);
  1445. return rdev_del_device(rdev, wpan_dev, extended_addr);
  1446. }
  1447. static int nl802154_send_devkey(struct sk_buff *msg, u32 cmd, u32 portid,
  1448. u32 seq, int flags,
  1449. struct cfg802154_registered_device *rdev,
  1450. struct net_device *dev, __le64 extended_addr,
  1451. const struct ieee802154_llsec_device_key *devkey)
  1452. {
  1453. void *hdr;
  1454. struct nlattr *nl_devkey, *nl_key_id;
  1455. hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
  1456. if (!hdr)
  1457. return -1;
  1458. if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
  1459. goto nla_put_failure;
  1460. nl_devkey = nla_nest_start(msg, NL802154_ATTR_SEC_DEVKEY);
  1461. if (!nl_devkey)
  1462. goto nla_put_failure;
  1463. if (nla_put_le64(msg, NL802154_DEVKEY_ATTR_EXTENDED_ADDR,
  1464. extended_addr, NL802154_DEVKEY_ATTR_PAD) ||
  1465. nla_put_u32(msg, NL802154_DEVKEY_ATTR_FRAME_COUNTER,
  1466. devkey->frame_counter))
  1467. goto nla_put_failure;
  1468. nl_key_id = nla_nest_start(msg, NL802154_DEVKEY_ATTR_ID);
  1469. if (!nl_key_id)
  1470. goto nla_put_failure;
  1471. if (ieee802154_llsec_send_key_id(msg, &devkey->key_id) < 0)
  1472. goto nla_put_failure;
  1473. nla_nest_end(msg, nl_key_id);
  1474. nla_nest_end(msg, nl_devkey);
  1475. genlmsg_end(msg, hdr);
  1476. return 0;
  1477. nla_put_failure:
  1478. genlmsg_cancel(msg, hdr);
  1479. return -EMSGSIZE;
  1480. }
  1481. static int
  1482. nl802154_dump_llsec_devkey(struct sk_buff *skb, struct netlink_callback *cb)
  1483. {
  1484. struct cfg802154_registered_device *rdev = NULL;
  1485. struct ieee802154_llsec_device_key *kpos;
  1486. struct ieee802154_llsec_device *dpos;
  1487. struct ieee802154_llsec_table *table;
  1488. struct wpan_dev *wpan_dev;
  1489. int err;
  1490. err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
  1491. if (err)
  1492. return err;
  1493. if (!wpan_dev->netdev) {
  1494. err = -EINVAL;
  1495. goto out_err;
  1496. }
  1497. rdev_lock_llsec_table(rdev, wpan_dev);
  1498. rdev_get_llsec_table(rdev, wpan_dev, &table);
  1499. /* TODO make it like station dump */
  1500. if (cb->args[2])
  1501. goto out;
  1502. /* TODO look if remove devkey and do some nested attribute */
  1503. list_for_each_entry(dpos, &table->devices, list) {
  1504. list_for_each_entry(kpos, &dpos->keys, list) {
  1505. if (nl802154_send_devkey(skb,
  1506. NL802154_CMD_NEW_SEC_LEVEL,
  1507. NETLINK_CB(cb->skb).portid,
  1508. cb->nlh->nlmsg_seq,
  1509. NLM_F_MULTI, rdev,
  1510. wpan_dev->netdev,
  1511. dpos->hwaddr,
  1512. kpos) < 0) {
  1513. /* TODO */
  1514. err = -EIO;
  1515. rdev_unlock_llsec_table(rdev, wpan_dev);
  1516. goto out_err;
  1517. }
  1518. }
  1519. }
  1520. cb->args[2] = 1;
  1521. out:
  1522. rdev_unlock_llsec_table(rdev, wpan_dev);
  1523. err = skb->len;
  1524. out_err:
  1525. nl802154_finish_wpan_dev_dump(rdev);
  1526. return err;
  1527. }
  1528. static const struct nla_policy nl802154_devkey_policy[NL802154_DEVKEY_ATTR_MAX + 1] = {
  1529. [NL802154_DEVKEY_ATTR_FRAME_COUNTER] = { NLA_U32 },
  1530. [NL802154_DEVKEY_ATTR_EXTENDED_ADDR] = { NLA_U64 },
  1531. [NL802154_DEVKEY_ATTR_ID] = { NLA_NESTED },
  1532. };
  1533. static int nl802154_add_llsec_devkey(struct sk_buff *skb, struct genl_info *info)
  1534. {
  1535. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  1536. struct net_device *dev = info->user_ptr[1];
  1537. struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  1538. struct nlattr *attrs[NL802154_DEVKEY_ATTR_MAX + 1];
  1539. struct ieee802154_llsec_device_key key;
  1540. __le64 extended_addr;
  1541. if (!info->attrs[NL802154_ATTR_SEC_DEVKEY] ||
  1542. nla_parse_nested(attrs, NL802154_DEVKEY_ATTR_MAX,
  1543. info->attrs[NL802154_ATTR_SEC_DEVKEY],
  1544. nl802154_devkey_policy, info->extack) < 0)
  1545. return -EINVAL;
  1546. if (!attrs[NL802154_DEVKEY_ATTR_FRAME_COUNTER] ||
  1547. !attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR])
  1548. return -EINVAL;
  1549. /* TODO change key.id ? */
  1550. if (ieee802154_llsec_parse_key_id(attrs[NL802154_DEVKEY_ATTR_ID],
  1551. &key.key_id) < 0)
  1552. return -ENOBUFS;
  1553. /* TODO be32 */
  1554. key.frame_counter = nla_get_u32(attrs[NL802154_DEVKEY_ATTR_FRAME_COUNTER]);
  1555. /* TODO change naming hwaddr -> extended_addr
  1556. * check unique identifier short+pan OR extended_addr
  1557. */
  1558. extended_addr = nla_get_le64(attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR]);
  1559. return rdev_add_devkey(rdev, wpan_dev, extended_addr, &key);
  1560. }
  1561. static int nl802154_del_llsec_devkey(struct sk_buff *skb, struct genl_info *info)
  1562. {
  1563. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  1564. struct net_device *dev = info->user_ptr[1];
  1565. struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  1566. struct nlattr *attrs[NL802154_DEVKEY_ATTR_MAX + 1];
  1567. struct ieee802154_llsec_device_key key;
  1568. __le64 extended_addr;
  1569. if (nla_parse_nested(attrs, NL802154_DEVKEY_ATTR_MAX,
  1570. info->attrs[NL802154_ATTR_SEC_DEVKEY],
  1571. nl802154_devkey_policy, info->extack))
  1572. return -EINVAL;
  1573. if (!attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR])
  1574. return -EINVAL;
  1575. /* TODO change key.id ? */
  1576. if (ieee802154_llsec_parse_key_id(attrs[NL802154_DEVKEY_ATTR_ID],
  1577. &key.key_id) < 0)
  1578. return -ENOBUFS;
  1579. /* TODO change naming hwaddr -> extended_addr
  1580. * check unique identifier short+pan OR extended_addr
  1581. */
  1582. extended_addr = nla_get_le64(attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR]);
  1583. return rdev_del_devkey(rdev, wpan_dev, extended_addr, &key);
  1584. }
  1585. static int nl802154_send_seclevel(struct sk_buff *msg, u32 cmd, u32 portid,
  1586. u32 seq, int flags,
  1587. struct cfg802154_registered_device *rdev,
  1588. struct net_device *dev,
  1589. const struct ieee802154_llsec_seclevel *sl)
  1590. {
  1591. void *hdr;
  1592. struct nlattr *nl_seclevel;
  1593. hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
  1594. if (!hdr)
  1595. return -1;
  1596. if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
  1597. goto nla_put_failure;
  1598. nl_seclevel = nla_nest_start(msg, NL802154_ATTR_SEC_LEVEL);
  1599. if (!nl_seclevel)
  1600. goto nla_put_failure;
  1601. if (nla_put_u32(msg, NL802154_SECLEVEL_ATTR_FRAME, sl->frame_type) ||
  1602. nla_put_u32(msg, NL802154_SECLEVEL_ATTR_LEVELS, sl->sec_levels) ||
  1603. nla_put_u8(msg, NL802154_SECLEVEL_ATTR_DEV_OVERRIDE,
  1604. sl->device_override))
  1605. goto nla_put_failure;
  1606. if (sl->frame_type == NL802154_FRAME_CMD) {
  1607. if (nla_put_u32(msg, NL802154_SECLEVEL_ATTR_CMD_FRAME,
  1608. sl->cmd_frame_id))
  1609. goto nla_put_failure;
  1610. }
  1611. nla_nest_end(msg, nl_seclevel);
  1612. genlmsg_end(msg, hdr);
  1613. return 0;
  1614. nla_put_failure:
  1615. genlmsg_cancel(msg, hdr);
  1616. return -EMSGSIZE;
  1617. }
  1618. static int
  1619. nl802154_dump_llsec_seclevel(struct sk_buff *skb, struct netlink_callback *cb)
  1620. {
  1621. struct cfg802154_registered_device *rdev = NULL;
  1622. struct ieee802154_llsec_seclevel *sl;
  1623. struct ieee802154_llsec_table *table;
  1624. struct wpan_dev *wpan_dev;
  1625. int err;
  1626. err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
  1627. if (err)
  1628. return err;
  1629. if (!wpan_dev->netdev) {
  1630. err = -EINVAL;
  1631. goto out_err;
  1632. }
  1633. rdev_lock_llsec_table(rdev, wpan_dev);
  1634. rdev_get_llsec_table(rdev, wpan_dev, &table);
  1635. /* TODO make it like station dump */
  1636. if (cb->args[2])
  1637. goto out;
  1638. list_for_each_entry(sl, &table->security_levels, list) {
  1639. if (nl802154_send_seclevel(skb, NL802154_CMD_NEW_SEC_LEVEL,
  1640. NETLINK_CB(cb->skb).portid,
  1641. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1642. rdev, wpan_dev->netdev, sl) < 0) {
  1643. /* TODO */
  1644. err = -EIO;
  1645. rdev_unlock_llsec_table(rdev, wpan_dev);
  1646. goto out_err;
  1647. }
  1648. }
  1649. cb->args[2] = 1;
  1650. out:
  1651. rdev_unlock_llsec_table(rdev, wpan_dev);
  1652. err = skb->len;
  1653. out_err:
  1654. nl802154_finish_wpan_dev_dump(rdev);
  1655. return err;
  1656. }
  1657. static const struct nla_policy nl802154_seclevel_policy[NL802154_SECLEVEL_ATTR_MAX + 1] = {
  1658. [NL802154_SECLEVEL_ATTR_LEVELS] = { .type = NLA_U8 },
  1659. [NL802154_SECLEVEL_ATTR_FRAME] = { .type = NLA_U32 },
  1660. [NL802154_SECLEVEL_ATTR_CMD_FRAME] = { .type = NLA_U32 },
  1661. [NL802154_SECLEVEL_ATTR_DEV_OVERRIDE] = { .type = NLA_U8 },
  1662. };
  1663. static int
  1664. llsec_parse_seclevel(struct nlattr *nla, struct ieee802154_llsec_seclevel *sl)
  1665. {
  1666. struct nlattr *attrs[NL802154_SECLEVEL_ATTR_MAX + 1];
  1667. if (!nla || nla_parse_nested(attrs, NL802154_SECLEVEL_ATTR_MAX,
  1668. nla, nl802154_seclevel_policy, NULL))
  1669. return -EINVAL;
  1670. memset(sl, 0, sizeof(*sl));
  1671. if (!attrs[NL802154_SECLEVEL_ATTR_LEVELS] ||
  1672. !attrs[NL802154_SECLEVEL_ATTR_FRAME] ||
  1673. !attrs[NL802154_SECLEVEL_ATTR_DEV_OVERRIDE])
  1674. return -EINVAL;
  1675. sl->sec_levels = nla_get_u8(attrs[NL802154_SECLEVEL_ATTR_LEVELS]);
  1676. sl->frame_type = nla_get_u32(attrs[NL802154_SECLEVEL_ATTR_FRAME]);
  1677. sl->device_override = nla_get_u8(attrs[NL802154_SECLEVEL_ATTR_DEV_OVERRIDE]);
  1678. if (sl->frame_type > NL802154_FRAME_MAX ||
  1679. (sl->device_override != 0 && sl->device_override != 1))
  1680. return -EINVAL;
  1681. if (sl->frame_type == NL802154_FRAME_CMD) {
  1682. if (!attrs[NL802154_SECLEVEL_ATTR_CMD_FRAME])
  1683. return -EINVAL;
  1684. sl->cmd_frame_id = nla_get_u32(attrs[NL802154_SECLEVEL_ATTR_CMD_FRAME]);
  1685. if (sl->cmd_frame_id > NL802154_CMD_FRAME_MAX)
  1686. return -EINVAL;
  1687. }
  1688. return 0;
  1689. }
  1690. static int nl802154_add_llsec_seclevel(struct sk_buff *skb,
  1691. struct genl_info *info)
  1692. {
  1693. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  1694. struct net_device *dev = info->user_ptr[1];
  1695. struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  1696. struct ieee802154_llsec_seclevel sl;
  1697. if (llsec_parse_seclevel(info->attrs[NL802154_ATTR_SEC_LEVEL],
  1698. &sl) < 0)
  1699. return -EINVAL;
  1700. return rdev_add_seclevel(rdev, wpan_dev, &sl);
  1701. }
  1702. static int nl802154_del_llsec_seclevel(struct sk_buff *skb,
  1703. struct genl_info *info)
  1704. {
  1705. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  1706. struct net_device *dev = info->user_ptr[1];
  1707. struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  1708. struct ieee802154_llsec_seclevel sl;
  1709. if (!info->attrs[NL802154_ATTR_SEC_LEVEL] ||
  1710. llsec_parse_seclevel(info->attrs[NL802154_ATTR_SEC_LEVEL],
  1711. &sl) < 0)
  1712. return -EINVAL;
  1713. return rdev_del_seclevel(rdev, wpan_dev, &sl);
  1714. }
  1715. #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
  1716. #define NL802154_FLAG_NEED_WPAN_PHY 0x01
  1717. #define NL802154_FLAG_NEED_NETDEV 0x02
  1718. #define NL802154_FLAG_NEED_RTNL 0x04
  1719. #define NL802154_FLAG_CHECK_NETDEV_UP 0x08
  1720. #define NL802154_FLAG_NEED_NETDEV_UP (NL802154_FLAG_NEED_NETDEV |\
  1721. NL802154_FLAG_CHECK_NETDEV_UP)
  1722. #define NL802154_FLAG_NEED_WPAN_DEV 0x10
  1723. #define NL802154_FLAG_NEED_WPAN_DEV_UP (NL802154_FLAG_NEED_WPAN_DEV |\
  1724. NL802154_FLAG_CHECK_NETDEV_UP)
  1725. static int nl802154_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
  1726. struct genl_info *info)
  1727. {
  1728. struct cfg802154_registered_device *rdev;
  1729. struct wpan_dev *wpan_dev;
  1730. struct net_device *dev;
  1731. bool rtnl = ops->internal_flags & NL802154_FLAG_NEED_RTNL;
  1732. if (rtnl)
  1733. rtnl_lock();
  1734. if (ops->internal_flags & NL802154_FLAG_NEED_WPAN_PHY) {
  1735. rdev = cfg802154_get_dev_from_info(genl_info_net(info), info);
  1736. if (IS_ERR(rdev)) {
  1737. if (rtnl)
  1738. rtnl_unlock();
  1739. return PTR_ERR(rdev);
  1740. }
  1741. info->user_ptr[0] = rdev;
  1742. } else if (ops->internal_flags & NL802154_FLAG_NEED_NETDEV ||
  1743. ops->internal_flags & NL802154_FLAG_NEED_WPAN_DEV) {
  1744. ASSERT_RTNL();
  1745. wpan_dev = __cfg802154_wpan_dev_from_attrs(genl_info_net(info),
  1746. info->attrs);
  1747. if (IS_ERR(wpan_dev)) {
  1748. if (rtnl)
  1749. rtnl_unlock();
  1750. return PTR_ERR(wpan_dev);
  1751. }
  1752. dev = wpan_dev->netdev;
  1753. rdev = wpan_phy_to_rdev(wpan_dev->wpan_phy);
  1754. if (ops->internal_flags & NL802154_FLAG_NEED_NETDEV) {
  1755. if (!dev) {
  1756. if (rtnl)
  1757. rtnl_unlock();
  1758. return -EINVAL;
  1759. }
  1760. info->user_ptr[1] = dev;
  1761. } else {
  1762. info->user_ptr[1] = wpan_dev;
  1763. }
  1764. if (dev) {
  1765. if (ops->internal_flags & NL802154_FLAG_CHECK_NETDEV_UP &&
  1766. !netif_running(dev)) {
  1767. if (rtnl)
  1768. rtnl_unlock();
  1769. return -ENETDOWN;
  1770. }
  1771. dev_hold(dev);
  1772. }
  1773. info->user_ptr[0] = rdev;
  1774. }
  1775. return 0;
  1776. }
  1777. static void nl802154_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
  1778. struct genl_info *info)
  1779. {
  1780. if (info->user_ptr[1]) {
  1781. if (ops->internal_flags & NL802154_FLAG_NEED_WPAN_DEV) {
  1782. struct wpan_dev *wpan_dev = info->user_ptr[1];
  1783. if (wpan_dev->netdev)
  1784. dev_put(wpan_dev->netdev);
  1785. } else {
  1786. dev_put(info->user_ptr[1]);
  1787. }
  1788. }
  1789. if (ops->internal_flags & NL802154_FLAG_NEED_RTNL)
  1790. rtnl_unlock();
  1791. }
  1792. static const struct genl_ops nl802154_ops[] = {
  1793. {
  1794. .cmd = NL802154_CMD_GET_WPAN_PHY,
  1795. .doit = nl802154_get_wpan_phy,
  1796. .dumpit = nl802154_dump_wpan_phy,
  1797. .done = nl802154_dump_wpan_phy_done,
  1798. .policy = nl802154_policy,
  1799. /* can be retrieved by unprivileged users */
  1800. .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
  1801. NL802154_FLAG_NEED_RTNL,
  1802. },
  1803. {
  1804. .cmd = NL802154_CMD_GET_INTERFACE,
  1805. .doit = nl802154_get_interface,
  1806. .dumpit = nl802154_dump_interface,
  1807. .policy = nl802154_policy,
  1808. /* can be retrieved by unprivileged users */
  1809. .internal_flags = NL802154_FLAG_NEED_WPAN_DEV |
  1810. NL802154_FLAG_NEED_RTNL,
  1811. },
  1812. {
  1813. .cmd = NL802154_CMD_NEW_INTERFACE,
  1814. .doit = nl802154_new_interface,
  1815. .policy = nl802154_policy,
  1816. .flags = GENL_ADMIN_PERM,
  1817. .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
  1818. NL802154_FLAG_NEED_RTNL,
  1819. },
  1820. {
  1821. .cmd = NL802154_CMD_DEL_INTERFACE,
  1822. .doit = nl802154_del_interface,
  1823. .policy = nl802154_policy,
  1824. .flags = GENL_ADMIN_PERM,
  1825. .internal_flags = NL802154_FLAG_NEED_WPAN_DEV |
  1826. NL802154_FLAG_NEED_RTNL,
  1827. },
  1828. {
  1829. .cmd = NL802154_CMD_SET_CHANNEL,
  1830. .doit = nl802154_set_channel,
  1831. .policy = nl802154_policy,
  1832. .flags = GENL_ADMIN_PERM,
  1833. .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
  1834. NL802154_FLAG_NEED_RTNL,
  1835. },
  1836. {
  1837. .cmd = NL802154_CMD_SET_CCA_MODE,
  1838. .doit = nl802154_set_cca_mode,
  1839. .policy = nl802154_policy,
  1840. .flags = GENL_ADMIN_PERM,
  1841. .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
  1842. NL802154_FLAG_NEED_RTNL,
  1843. },
  1844. {
  1845. .cmd = NL802154_CMD_SET_CCA_ED_LEVEL,
  1846. .doit = nl802154_set_cca_ed_level,
  1847. .policy = nl802154_policy,
  1848. .flags = GENL_ADMIN_PERM,
  1849. .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
  1850. NL802154_FLAG_NEED_RTNL,
  1851. },
  1852. {
  1853. .cmd = NL802154_CMD_SET_TX_POWER,
  1854. .doit = nl802154_set_tx_power,
  1855. .policy = nl802154_policy,
  1856. .flags = GENL_ADMIN_PERM,
  1857. .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
  1858. NL802154_FLAG_NEED_RTNL,
  1859. },
  1860. {
  1861. .cmd = NL802154_CMD_SET_WPAN_PHY_NETNS,
  1862. .doit = nl802154_wpan_phy_netns,
  1863. .policy = nl802154_policy,
  1864. .flags = GENL_ADMIN_PERM,
  1865. .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
  1866. NL802154_FLAG_NEED_RTNL,
  1867. },
  1868. {
  1869. .cmd = NL802154_CMD_SET_PAN_ID,
  1870. .doit = nl802154_set_pan_id,
  1871. .policy = nl802154_policy,
  1872. .flags = GENL_ADMIN_PERM,
  1873. .internal_flags = NL802154_FLAG_NEED_NETDEV |
  1874. NL802154_FLAG_NEED_RTNL,
  1875. },
  1876. {
  1877. .cmd = NL802154_CMD_SET_SHORT_ADDR,
  1878. .doit = nl802154_set_short_addr,
  1879. .policy = nl802154_policy,
  1880. .flags = GENL_ADMIN_PERM,
  1881. .internal_flags = NL802154_FLAG_NEED_NETDEV |
  1882. NL802154_FLAG_NEED_RTNL,
  1883. },
  1884. {
  1885. .cmd = NL802154_CMD_SET_BACKOFF_EXPONENT,
  1886. .doit = nl802154_set_backoff_exponent,
  1887. .policy = nl802154_policy,
  1888. .flags = GENL_ADMIN_PERM,
  1889. .internal_flags = NL802154_FLAG_NEED_NETDEV |
  1890. NL802154_FLAG_NEED_RTNL,
  1891. },
  1892. {
  1893. .cmd = NL802154_CMD_SET_MAX_CSMA_BACKOFFS,
  1894. .doit = nl802154_set_max_csma_backoffs,
  1895. .policy = nl802154_policy,
  1896. .flags = GENL_ADMIN_PERM,
  1897. .internal_flags = NL802154_FLAG_NEED_NETDEV |
  1898. NL802154_FLAG_NEED_RTNL,
  1899. },
  1900. {
  1901. .cmd = NL802154_CMD_SET_MAX_FRAME_RETRIES,
  1902. .doit = nl802154_set_max_frame_retries,
  1903. .policy = nl802154_policy,
  1904. .flags = GENL_ADMIN_PERM,
  1905. .internal_flags = NL802154_FLAG_NEED_NETDEV |
  1906. NL802154_FLAG_NEED_RTNL,
  1907. },
  1908. {
  1909. .cmd = NL802154_CMD_SET_LBT_MODE,
  1910. .doit = nl802154_set_lbt_mode,
  1911. .policy = nl802154_policy,
  1912. .flags = GENL_ADMIN_PERM,
  1913. .internal_flags = NL802154_FLAG_NEED_NETDEV |
  1914. NL802154_FLAG_NEED_RTNL,
  1915. },
  1916. {
  1917. .cmd = NL802154_CMD_SET_ACKREQ_DEFAULT,
  1918. .doit = nl802154_set_ackreq_default,
  1919. .policy = nl802154_policy,
  1920. .flags = GENL_ADMIN_PERM,
  1921. .internal_flags = NL802154_FLAG_NEED_NETDEV |
  1922. NL802154_FLAG_NEED_RTNL,
  1923. },
  1924. #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
  1925. {
  1926. .cmd = NL802154_CMD_SET_SEC_PARAMS,
  1927. .doit = nl802154_set_llsec_params,
  1928. .policy = nl802154_policy,
  1929. .flags = GENL_ADMIN_PERM,
  1930. .internal_flags = NL802154_FLAG_NEED_NETDEV |
  1931. NL802154_FLAG_NEED_RTNL,
  1932. },
  1933. {
  1934. .cmd = NL802154_CMD_GET_SEC_KEY,
  1935. /* TODO .doit by matching key id? */
  1936. .dumpit = nl802154_dump_llsec_key,
  1937. .policy = nl802154_policy,
  1938. .flags = GENL_ADMIN_PERM,
  1939. .internal_flags = NL802154_FLAG_NEED_NETDEV |
  1940. NL802154_FLAG_NEED_RTNL,
  1941. },
  1942. {
  1943. .cmd = NL802154_CMD_NEW_SEC_KEY,
  1944. .doit = nl802154_add_llsec_key,
  1945. .policy = nl802154_policy,
  1946. .flags = GENL_ADMIN_PERM,
  1947. .internal_flags = NL802154_FLAG_NEED_NETDEV |
  1948. NL802154_FLAG_NEED_RTNL,
  1949. },
  1950. {
  1951. .cmd = NL802154_CMD_DEL_SEC_KEY,
  1952. .doit = nl802154_del_llsec_key,
  1953. .policy = nl802154_policy,
  1954. .flags = GENL_ADMIN_PERM,
  1955. .internal_flags = NL802154_FLAG_NEED_NETDEV |
  1956. NL802154_FLAG_NEED_RTNL,
  1957. },
  1958. /* TODO unique identifier must short+pan OR extended_addr */
  1959. {
  1960. .cmd = NL802154_CMD_GET_SEC_DEV,
  1961. /* TODO .doit by matching extended_addr? */
  1962. .dumpit = nl802154_dump_llsec_dev,
  1963. .policy = nl802154_policy,
  1964. .flags = GENL_ADMIN_PERM,
  1965. .internal_flags = NL802154_FLAG_NEED_NETDEV |
  1966. NL802154_FLAG_NEED_RTNL,
  1967. },
  1968. {
  1969. .cmd = NL802154_CMD_NEW_SEC_DEV,
  1970. .doit = nl802154_add_llsec_dev,
  1971. .policy = nl802154_policy,
  1972. .flags = GENL_ADMIN_PERM,
  1973. .internal_flags = NL802154_FLAG_NEED_NETDEV |
  1974. NL802154_FLAG_NEED_RTNL,
  1975. },
  1976. {
  1977. .cmd = NL802154_CMD_DEL_SEC_DEV,
  1978. .doit = nl802154_del_llsec_dev,
  1979. .policy = nl802154_policy,
  1980. .flags = GENL_ADMIN_PERM,
  1981. .internal_flags = NL802154_FLAG_NEED_NETDEV |
  1982. NL802154_FLAG_NEED_RTNL,
  1983. },
  1984. /* TODO remove complete devkey, put it as nested? */
  1985. {
  1986. .cmd = NL802154_CMD_GET_SEC_DEVKEY,
  1987. /* TODO doit by matching ??? */
  1988. .dumpit = nl802154_dump_llsec_devkey,
  1989. .policy = nl802154_policy,
  1990. .flags = GENL_ADMIN_PERM,
  1991. .internal_flags = NL802154_FLAG_NEED_NETDEV |
  1992. NL802154_FLAG_NEED_RTNL,
  1993. },
  1994. {
  1995. .cmd = NL802154_CMD_NEW_SEC_DEVKEY,
  1996. .doit = nl802154_add_llsec_devkey,
  1997. .policy = nl802154_policy,
  1998. .flags = GENL_ADMIN_PERM,
  1999. .internal_flags = NL802154_FLAG_NEED_NETDEV |
  2000. NL802154_FLAG_NEED_RTNL,
  2001. },
  2002. {
  2003. .cmd = NL802154_CMD_DEL_SEC_DEVKEY,
  2004. .doit = nl802154_del_llsec_devkey,
  2005. .policy = nl802154_policy,
  2006. .flags = GENL_ADMIN_PERM,
  2007. .internal_flags = NL802154_FLAG_NEED_NETDEV |
  2008. NL802154_FLAG_NEED_RTNL,
  2009. },
  2010. {
  2011. .cmd = NL802154_CMD_GET_SEC_LEVEL,
  2012. /* TODO .doit by matching frame_type? */
  2013. .dumpit = nl802154_dump_llsec_seclevel,
  2014. .policy = nl802154_policy,
  2015. .flags = GENL_ADMIN_PERM,
  2016. .internal_flags = NL802154_FLAG_NEED_NETDEV |
  2017. NL802154_FLAG_NEED_RTNL,
  2018. },
  2019. {
  2020. .cmd = NL802154_CMD_NEW_SEC_LEVEL,
  2021. .doit = nl802154_add_llsec_seclevel,
  2022. .policy = nl802154_policy,
  2023. .flags = GENL_ADMIN_PERM,
  2024. .internal_flags = NL802154_FLAG_NEED_NETDEV |
  2025. NL802154_FLAG_NEED_RTNL,
  2026. },
  2027. {
  2028. .cmd = NL802154_CMD_DEL_SEC_LEVEL,
  2029. /* TODO match frame_type only? */
  2030. .doit = nl802154_del_llsec_seclevel,
  2031. .policy = nl802154_policy,
  2032. .flags = GENL_ADMIN_PERM,
  2033. .internal_flags = NL802154_FLAG_NEED_NETDEV |
  2034. NL802154_FLAG_NEED_RTNL,
  2035. },
  2036. #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
  2037. };
  2038. static struct genl_family nl802154_fam __ro_after_init = {
  2039. .name = NL802154_GENL_NAME, /* have users key off the name instead */
  2040. .hdrsize = 0, /* no private header */
  2041. .version = 1, /* no particular meaning now */
  2042. .maxattr = NL802154_ATTR_MAX,
  2043. .netnsok = true,
  2044. .pre_doit = nl802154_pre_doit,
  2045. .post_doit = nl802154_post_doit,
  2046. .module = THIS_MODULE,
  2047. .ops = nl802154_ops,
  2048. .n_ops = ARRAY_SIZE(nl802154_ops),
  2049. .mcgrps = nl802154_mcgrps,
  2050. .n_mcgrps = ARRAY_SIZE(nl802154_mcgrps),
  2051. };
  2052. /* initialisation/exit functions */
  2053. int __init nl802154_init(void)
  2054. {
  2055. return genl_register_family(&nl802154_fam);
  2056. }
  2057. void nl802154_exit(void)
  2058. {
  2059. genl_unregister_family(&nl802154_fam);
  2060. }