nl802154.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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. static int nl802154_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
  26. struct genl_info *info);
  27. static void nl802154_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
  28. struct genl_info *info);
  29. /* the netlink family */
  30. static struct genl_family nl802154_fam = {
  31. .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
  32. .name = NL802154_GENL_NAME, /* have users key off the name instead */
  33. .hdrsize = 0, /* no private header */
  34. .version = 1, /* no particular meaning now */
  35. .maxattr = NL802154_ATTR_MAX,
  36. .netnsok = true,
  37. .pre_doit = nl802154_pre_doit,
  38. .post_doit = nl802154_post_doit,
  39. };
  40. /* multicast groups */
  41. enum nl802154_multicast_groups {
  42. NL802154_MCGRP_CONFIG,
  43. };
  44. static const struct genl_multicast_group nl802154_mcgrps[] = {
  45. [NL802154_MCGRP_CONFIG] = { .name = "config", },
  46. };
  47. /* returns ERR_PTR values */
  48. static struct wpan_dev *
  49. __cfg802154_wpan_dev_from_attrs(struct net *netns, struct nlattr **attrs)
  50. {
  51. struct cfg802154_registered_device *rdev;
  52. struct wpan_dev *result = NULL;
  53. bool have_ifidx = attrs[NL802154_ATTR_IFINDEX];
  54. bool have_wpan_dev_id = attrs[NL802154_ATTR_WPAN_DEV];
  55. u64 wpan_dev_id;
  56. int wpan_phy_idx = -1;
  57. int ifidx = -1;
  58. ASSERT_RTNL();
  59. if (!have_ifidx && !have_wpan_dev_id)
  60. return ERR_PTR(-EINVAL);
  61. if (have_ifidx)
  62. ifidx = nla_get_u32(attrs[NL802154_ATTR_IFINDEX]);
  63. if (have_wpan_dev_id) {
  64. wpan_dev_id = nla_get_u64(attrs[NL802154_ATTR_WPAN_DEV]);
  65. wpan_phy_idx = wpan_dev_id >> 32;
  66. }
  67. list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
  68. struct wpan_dev *wpan_dev;
  69. /* TODO netns compare */
  70. if (have_wpan_dev_id && rdev->wpan_phy_idx != wpan_phy_idx)
  71. continue;
  72. list_for_each_entry(wpan_dev, &rdev->wpan_dev_list, list) {
  73. if (have_ifidx && wpan_dev->netdev &&
  74. wpan_dev->netdev->ifindex == ifidx) {
  75. result = wpan_dev;
  76. break;
  77. }
  78. if (have_wpan_dev_id &&
  79. wpan_dev->identifier == (u32)wpan_dev_id) {
  80. result = wpan_dev;
  81. break;
  82. }
  83. }
  84. if (result)
  85. break;
  86. }
  87. if (result)
  88. return result;
  89. return ERR_PTR(-ENODEV);
  90. }
  91. static struct cfg802154_registered_device *
  92. __cfg802154_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
  93. {
  94. struct cfg802154_registered_device *rdev = NULL, *tmp;
  95. struct net_device *netdev;
  96. ASSERT_RTNL();
  97. if (!attrs[NL802154_ATTR_WPAN_PHY] &&
  98. !attrs[NL802154_ATTR_IFINDEX] &&
  99. !attrs[NL802154_ATTR_WPAN_DEV])
  100. return ERR_PTR(-EINVAL);
  101. if (attrs[NL802154_ATTR_WPAN_PHY])
  102. rdev = cfg802154_rdev_by_wpan_phy_idx(
  103. nla_get_u32(attrs[NL802154_ATTR_WPAN_PHY]));
  104. if (attrs[NL802154_ATTR_WPAN_DEV]) {
  105. u64 wpan_dev_id = nla_get_u64(attrs[NL802154_ATTR_WPAN_DEV]);
  106. struct wpan_dev *wpan_dev;
  107. bool found = false;
  108. tmp = cfg802154_rdev_by_wpan_phy_idx(wpan_dev_id >> 32);
  109. if (tmp) {
  110. /* make sure wpan_dev exists */
  111. list_for_each_entry(wpan_dev, &tmp->wpan_dev_list, list) {
  112. if (wpan_dev->identifier != (u32)wpan_dev_id)
  113. continue;
  114. found = true;
  115. break;
  116. }
  117. if (!found)
  118. tmp = NULL;
  119. if (rdev && tmp != rdev)
  120. return ERR_PTR(-EINVAL);
  121. rdev = tmp;
  122. }
  123. }
  124. if (attrs[NL802154_ATTR_IFINDEX]) {
  125. int ifindex = nla_get_u32(attrs[NL802154_ATTR_IFINDEX]);
  126. netdev = __dev_get_by_index(netns, ifindex);
  127. if (netdev) {
  128. if (netdev->ieee802154_ptr)
  129. tmp = wpan_phy_to_rdev(
  130. netdev->ieee802154_ptr->wpan_phy);
  131. else
  132. tmp = NULL;
  133. /* not wireless device -- return error */
  134. if (!tmp)
  135. return ERR_PTR(-EINVAL);
  136. /* mismatch -- return error */
  137. if (rdev && tmp != rdev)
  138. return ERR_PTR(-EINVAL);
  139. rdev = tmp;
  140. }
  141. }
  142. if (!rdev)
  143. return ERR_PTR(-ENODEV);
  144. /* TODO netns compare */
  145. return rdev;
  146. }
  147. /* This function returns a pointer to the driver
  148. * that the genl_info item that is passed refers to.
  149. *
  150. * The result of this can be a PTR_ERR and hence must
  151. * be checked with IS_ERR() for errors.
  152. */
  153. static struct cfg802154_registered_device *
  154. cfg802154_get_dev_from_info(struct net *netns, struct genl_info *info)
  155. {
  156. return __cfg802154_rdev_from_attrs(netns, info->attrs);
  157. }
  158. /* policy for the attributes */
  159. static const struct nla_policy nl802154_policy[NL802154_ATTR_MAX+1] = {
  160. [NL802154_ATTR_WPAN_PHY] = { .type = NLA_U32 },
  161. [NL802154_ATTR_WPAN_PHY_NAME] = { .type = NLA_NUL_STRING,
  162. .len = 20-1 },
  163. [NL802154_ATTR_IFINDEX] = { .type = NLA_U32 },
  164. [NL802154_ATTR_IFTYPE] = { .type = NLA_U32 },
  165. [NL802154_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
  166. [NL802154_ATTR_WPAN_DEV] = { .type = NLA_U64 },
  167. [NL802154_ATTR_PAGE] = { .type = NLA_U8, },
  168. [NL802154_ATTR_CHANNEL] = { .type = NLA_U8, },
  169. [NL802154_ATTR_TX_POWER] = { .type = NLA_S8, },
  170. [NL802154_ATTR_CCA_MODE] = { .type = NLA_U32, },
  171. [NL802154_ATTR_CCA_OPT] = { .type = NLA_U32, },
  172. [NL802154_ATTR_SUPPORTED_CHANNEL] = { .type = NLA_U32, },
  173. [NL802154_ATTR_PAN_ID] = { .type = NLA_U16, },
  174. [NL802154_ATTR_EXTENDED_ADDR] = { .type = NLA_U64 },
  175. [NL802154_ATTR_SHORT_ADDR] = { .type = NLA_U16, },
  176. [NL802154_ATTR_MIN_BE] = { .type = NLA_U8, },
  177. [NL802154_ATTR_MAX_BE] = { .type = NLA_U8, },
  178. [NL802154_ATTR_MAX_CSMA_BACKOFFS] = { .type = NLA_U8, },
  179. [NL802154_ATTR_MAX_FRAME_RETRIES] = { .type = NLA_S8, },
  180. [NL802154_ATTR_LBT_MODE] = { .type = NLA_U8, },
  181. };
  182. /* message building helper */
  183. static inline void *nl802154hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
  184. int flags, u8 cmd)
  185. {
  186. /* since there is no private header just add the generic one */
  187. return genlmsg_put(skb, portid, seq, &nl802154_fam, flags, cmd);
  188. }
  189. static int
  190. nl802154_send_wpan_phy_channels(struct cfg802154_registered_device *rdev,
  191. struct sk_buff *msg)
  192. {
  193. struct nlattr *nl_page;
  194. unsigned long page;
  195. nl_page = nla_nest_start(msg, NL802154_ATTR_CHANNELS_SUPPORTED);
  196. if (!nl_page)
  197. return -ENOBUFS;
  198. for (page = 0; page <= IEEE802154_MAX_PAGE; page++) {
  199. if (nla_put_u32(msg, NL802154_ATTR_SUPPORTED_CHANNEL,
  200. rdev->wpan_phy.channels_supported[page]))
  201. return -ENOBUFS;
  202. }
  203. nla_nest_end(msg, nl_page);
  204. return 0;
  205. }
  206. static int nl802154_send_wpan_phy(struct cfg802154_registered_device *rdev,
  207. enum nl802154_commands cmd,
  208. struct sk_buff *msg, u32 portid, u32 seq,
  209. int flags)
  210. {
  211. void *hdr;
  212. hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
  213. if (!hdr)
  214. return -ENOBUFS;
  215. if (nla_put_u32(msg, NL802154_ATTR_WPAN_PHY, rdev->wpan_phy_idx) ||
  216. nla_put_string(msg, NL802154_ATTR_WPAN_PHY_NAME,
  217. wpan_phy_name(&rdev->wpan_phy)) ||
  218. nla_put_u32(msg, NL802154_ATTR_GENERATION,
  219. cfg802154_rdev_list_generation))
  220. goto nla_put_failure;
  221. if (cmd != NL802154_CMD_NEW_WPAN_PHY)
  222. goto finish;
  223. /* DUMP PHY PIB */
  224. /* current channel settings */
  225. if (nla_put_u8(msg, NL802154_ATTR_PAGE,
  226. rdev->wpan_phy.current_page) ||
  227. nla_put_u8(msg, NL802154_ATTR_CHANNEL,
  228. rdev->wpan_phy.current_channel))
  229. goto nla_put_failure;
  230. /* supported channels array */
  231. if (nl802154_send_wpan_phy_channels(rdev, msg))
  232. goto nla_put_failure;
  233. /* cca mode */
  234. if (nla_put_u32(msg, NL802154_ATTR_CCA_MODE,
  235. rdev->wpan_phy.cca.mode))
  236. goto nla_put_failure;
  237. if (rdev->wpan_phy.cca.mode == NL802154_CCA_ENERGY_CARRIER) {
  238. if (nla_put_u32(msg, NL802154_ATTR_CCA_OPT,
  239. rdev->wpan_phy.cca.opt))
  240. goto nla_put_failure;
  241. }
  242. if (nla_put_s8(msg, NL802154_ATTR_TX_POWER,
  243. rdev->wpan_phy.transmit_power))
  244. goto nla_put_failure;
  245. finish:
  246. genlmsg_end(msg, hdr);
  247. return 0;
  248. nla_put_failure:
  249. genlmsg_cancel(msg, hdr);
  250. return -EMSGSIZE;
  251. }
  252. struct nl802154_dump_wpan_phy_state {
  253. s64 filter_wpan_phy;
  254. long start;
  255. };
  256. static int nl802154_dump_wpan_phy_parse(struct sk_buff *skb,
  257. struct netlink_callback *cb,
  258. struct nl802154_dump_wpan_phy_state *state)
  259. {
  260. struct nlattr **tb = nl802154_fam.attrbuf;
  261. int ret = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl802154_fam.hdrsize,
  262. tb, nl802154_fam.maxattr, nl802154_policy);
  263. /* TODO check if we can handle error here,
  264. * we have no backward compatibility
  265. */
  266. if (ret)
  267. return 0;
  268. if (tb[NL802154_ATTR_WPAN_PHY])
  269. state->filter_wpan_phy = nla_get_u32(tb[NL802154_ATTR_WPAN_PHY]);
  270. if (tb[NL802154_ATTR_WPAN_DEV])
  271. state->filter_wpan_phy = nla_get_u64(tb[NL802154_ATTR_WPAN_DEV]) >> 32;
  272. if (tb[NL802154_ATTR_IFINDEX]) {
  273. struct net_device *netdev;
  274. struct cfg802154_registered_device *rdev;
  275. int ifidx = nla_get_u32(tb[NL802154_ATTR_IFINDEX]);
  276. /* TODO netns */
  277. netdev = __dev_get_by_index(&init_net, ifidx);
  278. if (!netdev)
  279. return -ENODEV;
  280. if (netdev->ieee802154_ptr) {
  281. rdev = wpan_phy_to_rdev(
  282. netdev->ieee802154_ptr->wpan_phy);
  283. state->filter_wpan_phy = rdev->wpan_phy_idx;
  284. }
  285. }
  286. return 0;
  287. }
  288. static int
  289. nl802154_dump_wpan_phy(struct sk_buff *skb, struct netlink_callback *cb)
  290. {
  291. int idx = 0, ret;
  292. struct nl802154_dump_wpan_phy_state *state = (void *)cb->args[0];
  293. struct cfg802154_registered_device *rdev;
  294. rtnl_lock();
  295. if (!state) {
  296. state = kzalloc(sizeof(*state), GFP_KERNEL);
  297. if (!state) {
  298. rtnl_unlock();
  299. return -ENOMEM;
  300. }
  301. state->filter_wpan_phy = -1;
  302. ret = nl802154_dump_wpan_phy_parse(skb, cb, state);
  303. if (ret) {
  304. kfree(state);
  305. rtnl_unlock();
  306. return ret;
  307. }
  308. cb->args[0] = (long)state;
  309. }
  310. list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
  311. /* TODO net ns compare */
  312. if (++idx <= state->start)
  313. continue;
  314. if (state->filter_wpan_phy != -1 &&
  315. state->filter_wpan_phy != rdev->wpan_phy_idx)
  316. continue;
  317. /* attempt to fit multiple wpan_phy data chunks into the skb */
  318. ret = nl802154_send_wpan_phy(rdev,
  319. NL802154_CMD_NEW_WPAN_PHY,
  320. skb,
  321. NETLINK_CB(cb->skb).portid,
  322. cb->nlh->nlmsg_seq, NLM_F_MULTI);
  323. if (ret < 0) {
  324. if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
  325. !skb->len && cb->min_dump_alloc < 4096) {
  326. cb->min_dump_alloc = 4096;
  327. rtnl_unlock();
  328. return 1;
  329. }
  330. idx--;
  331. break;
  332. }
  333. break;
  334. }
  335. rtnl_unlock();
  336. state->start = idx;
  337. return skb->len;
  338. }
  339. static int nl802154_dump_wpan_phy_done(struct netlink_callback *cb)
  340. {
  341. kfree((void *)cb->args[0]);
  342. return 0;
  343. }
  344. static int nl802154_get_wpan_phy(struct sk_buff *skb, struct genl_info *info)
  345. {
  346. struct sk_buff *msg;
  347. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  348. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  349. if (!msg)
  350. return -ENOMEM;
  351. if (nl802154_send_wpan_phy(rdev, NL802154_CMD_NEW_WPAN_PHY, msg,
  352. info->snd_portid, info->snd_seq, 0) < 0) {
  353. nlmsg_free(msg);
  354. return -ENOBUFS;
  355. }
  356. return genlmsg_reply(msg, info);
  357. }
  358. static inline u64 wpan_dev_id(struct wpan_dev *wpan_dev)
  359. {
  360. return (u64)wpan_dev->identifier |
  361. ((u64)wpan_phy_to_rdev(wpan_dev->wpan_phy)->wpan_phy_idx << 32);
  362. }
  363. static int
  364. nl802154_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
  365. struct cfg802154_registered_device *rdev,
  366. struct wpan_dev *wpan_dev)
  367. {
  368. struct net_device *dev = wpan_dev->netdev;
  369. void *hdr;
  370. hdr = nl802154hdr_put(msg, portid, seq, flags,
  371. NL802154_CMD_NEW_INTERFACE);
  372. if (!hdr)
  373. return -1;
  374. if (dev &&
  375. (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex) ||
  376. nla_put_string(msg, NL802154_ATTR_IFNAME, dev->name)))
  377. goto nla_put_failure;
  378. if (nla_put_u32(msg, NL802154_ATTR_WPAN_PHY, rdev->wpan_phy_idx) ||
  379. nla_put_u32(msg, NL802154_ATTR_IFTYPE, wpan_dev->iftype) ||
  380. nla_put_u64(msg, NL802154_ATTR_WPAN_DEV, wpan_dev_id(wpan_dev)) ||
  381. nla_put_u32(msg, NL802154_ATTR_GENERATION,
  382. rdev->devlist_generation ^
  383. (cfg802154_rdev_list_generation << 2)))
  384. goto nla_put_failure;
  385. /* address settings */
  386. if (nla_put_le64(msg, NL802154_ATTR_EXTENDED_ADDR,
  387. wpan_dev->extended_addr) ||
  388. nla_put_le16(msg, NL802154_ATTR_SHORT_ADDR,
  389. wpan_dev->short_addr) ||
  390. nla_put_le16(msg, NL802154_ATTR_PAN_ID, wpan_dev->pan_id))
  391. goto nla_put_failure;
  392. /* ARET handling */
  393. if (nla_put_s8(msg, NL802154_ATTR_MAX_FRAME_RETRIES,
  394. wpan_dev->frame_retries) ||
  395. nla_put_u8(msg, NL802154_ATTR_MAX_BE, wpan_dev->max_be) ||
  396. nla_put_u8(msg, NL802154_ATTR_MAX_CSMA_BACKOFFS,
  397. wpan_dev->csma_retries) ||
  398. nla_put_u8(msg, NL802154_ATTR_MIN_BE, wpan_dev->min_be))
  399. goto nla_put_failure;
  400. /* listen before transmit */
  401. if (nla_put_u8(msg, NL802154_ATTR_LBT_MODE, wpan_dev->lbt))
  402. goto nla_put_failure;
  403. genlmsg_end(msg, hdr);
  404. return 0;
  405. nla_put_failure:
  406. genlmsg_cancel(msg, hdr);
  407. return -EMSGSIZE;
  408. }
  409. static int
  410. nl802154_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
  411. {
  412. int wp_idx = 0;
  413. int if_idx = 0;
  414. int wp_start = cb->args[0];
  415. int if_start = cb->args[1];
  416. struct cfg802154_registered_device *rdev;
  417. struct wpan_dev *wpan_dev;
  418. rtnl_lock();
  419. list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
  420. /* TODO netns compare */
  421. if (wp_idx < wp_start) {
  422. wp_idx++;
  423. continue;
  424. }
  425. if_idx = 0;
  426. list_for_each_entry(wpan_dev, &rdev->wpan_dev_list, list) {
  427. if (if_idx < if_start) {
  428. if_idx++;
  429. continue;
  430. }
  431. if (nl802154_send_iface(skb, NETLINK_CB(cb->skb).portid,
  432. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  433. rdev, wpan_dev) < 0) {
  434. goto out;
  435. }
  436. if_idx++;
  437. }
  438. wp_idx++;
  439. }
  440. out:
  441. rtnl_unlock();
  442. cb->args[0] = wp_idx;
  443. cb->args[1] = if_idx;
  444. return skb->len;
  445. }
  446. static int nl802154_get_interface(struct sk_buff *skb, struct genl_info *info)
  447. {
  448. struct sk_buff *msg;
  449. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  450. struct wpan_dev *wdev = info->user_ptr[1];
  451. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  452. if (!msg)
  453. return -ENOMEM;
  454. if (nl802154_send_iface(msg, info->snd_portid, info->snd_seq, 0,
  455. rdev, wdev) < 0) {
  456. nlmsg_free(msg);
  457. return -ENOBUFS;
  458. }
  459. return genlmsg_reply(msg, info);
  460. }
  461. static int nl802154_new_interface(struct sk_buff *skb, struct genl_info *info)
  462. {
  463. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  464. enum nl802154_iftype type = NL802154_IFTYPE_UNSPEC;
  465. __le64 extended_addr = cpu_to_le64(0x0000000000000000ULL);
  466. /* TODO avoid failing a new interface
  467. * creation due to pending removal?
  468. */
  469. if (!info->attrs[NL802154_ATTR_IFNAME])
  470. return -EINVAL;
  471. if (info->attrs[NL802154_ATTR_IFTYPE]) {
  472. type = nla_get_u32(info->attrs[NL802154_ATTR_IFTYPE]);
  473. if (type > NL802154_IFTYPE_MAX)
  474. return -EINVAL;
  475. }
  476. /* TODO add nla_get_le64 to netlink */
  477. if (info->attrs[NL802154_ATTR_EXTENDED_ADDR])
  478. extended_addr = (__force __le64)nla_get_u64(
  479. info->attrs[NL802154_ATTR_EXTENDED_ADDR]);
  480. if (!rdev->ops->add_virtual_intf)
  481. return -EOPNOTSUPP;
  482. return rdev_add_virtual_intf(rdev,
  483. nla_data(info->attrs[NL802154_ATTR_IFNAME]),
  484. NET_NAME_USER, type, extended_addr);
  485. }
  486. static int nl802154_del_interface(struct sk_buff *skb, struct genl_info *info)
  487. {
  488. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  489. struct wpan_dev *wpan_dev = info->user_ptr[1];
  490. if (!rdev->ops->del_virtual_intf)
  491. return -EOPNOTSUPP;
  492. /* If we remove a wpan device without a netdev then clear
  493. * user_ptr[1] so that nl802154_post_doit won't dereference it
  494. * to check if it needs to do dev_put(). Otherwise it crashes
  495. * since the wpan_dev has been freed, unlike with a netdev where
  496. * we need the dev_put() for the netdev to really be freed.
  497. */
  498. if (!wpan_dev->netdev)
  499. info->user_ptr[1] = NULL;
  500. return rdev_del_virtual_intf(rdev, wpan_dev);
  501. }
  502. static int nl802154_set_channel(struct sk_buff *skb, struct genl_info *info)
  503. {
  504. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  505. u8 channel, page;
  506. if (!info->attrs[NL802154_ATTR_PAGE] ||
  507. !info->attrs[NL802154_ATTR_CHANNEL])
  508. return -EINVAL;
  509. page = nla_get_u8(info->attrs[NL802154_ATTR_PAGE]);
  510. channel = nla_get_u8(info->attrs[NL802154_ATTR_CHANNEL]);
  511. /* check 802.15.4 constraints */
  512. if (page > IEEE802154_MAX_PAGE || channel > IEEE802154_MAX_CHANNEL)
  513. return -EINVAL;
  514. return rdev_set_channel(rdev, page, channel);
  515. }
  516. static int nl802154_set_cca_mode(struct sk_buff *skb, struct genl_info *info)
  517. {
  518. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  519. struct wpan_phy_cca cca;
  520. if (!info->attrs[NL802154_ATTR_CCA_MODE])
  521. return -EINVAL;
  522. cca.mode = nla_get_u32(info->attrs[NL802154_ATTR_CCA_MODE]);
  523. /* checking 802.15.4 constraints */
  524. if (cca.mode < NL802154_CCA_ENERGY || cca.mode > NL802154_CCA_ATTR_MAX)
  525. return -EINVAL;
  526. if (cca.mode == NL802154_CCA_ENERGY_CARRIER) {
  527. if (!info->attrs[NL802154_ATTR_CCA_OPT])
  528. return -EINVAL;
  529. cca.opt = nla_get_u32(info->attrs[NL802154_ATTR_CCA_OPT]);
  530. if (cca.opt > NL802154_CCA_OPT_ATTR_MAX)
  531. return -EINVAL;
  532. }
  533. return rdev_set_cca_mode(rdev, &cca);
  534. }
  535. static int nl802154_set_pan_id(struct sk_buff *skb, struct genl_info *info)
  536. {
  537. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  538. struct net_device *dev = info->user_ptr[1];
  539. struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  540. __le16 pan_id;
  541. /* conflict here while tx/rx calls */
  542. if (netif_running(dev))
  543. return -EBUSY;
  544. /* don't change address fields on monitor */
  545. if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
  546. return -EINVAL;
  547. if (!info->attrs[NL802154_ATTR_PAN_ID])
  548. return -EINVAL;
  549. pan_id = nla_get_le16(info->attrs[NL802154_ATTR_PAN_ID]);
  550. return rdev_set_pan_id(rdev, wpan_dev, pan_id);
  551. }
  552. static int nl802154_set_short_addr(struct sk_buff *skb, struct genl_info *info)
  553. {
  554. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  555. struct net_device *dev = info->user_ptr[1];
  556. struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  557. __le16 short_addr;
  558. /* conflict here while tx/rx calls */
  559. if (netif_running(dev))
  560. return -EBUSY;
  561. /* don't change address fields on monitor */
  562. if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR)
  563. return -EINVAL;
  564. if (!info->attrs[NL802154_ATTR_SHORT_ADDR])
  565. return -EINVAL;
  566. short_addr = nla_get_le16(info->attrs[NL802154_ATTR_SHORT_ADDR]);
  567. return rdev_set_short_addr(rdev, wpan_dev, short_addr);
  568. }
  569. static int
  570. nl802154_set_backoff_exponent(struct sk_buff *skb, struct genl_info *info)
  571. {
  572. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  573. struct net_device *dev = info->user_ptr[1];
  574. struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  575. u8 min_be, max_be;
  576. /* should be set on netif open inside phy settings */
  577. if (netif_running(dev))
  578. return -EBUSY;
  579. if (!info->attrs[NL802154_ATTR_MIN_BE] ||
  580. !info->attrs[NL802154_ATTR_MAX_BE])
  581. return -EINVAL;
  582. min_be = nla_get_u8(info->attrs[NL802154_ATTR_MIN_BE]);
  583. max_be = nla_get_u8(info->attrs[NL802154_ATTR_MAX_BE]);
  584. /* check 802.15.4 constraints */
  585. if (max_be < 3 || max_be > 8 || min_be > max_be)
  586. return -EINVAL;
  587. return rdev_set_backoff_exponent(rdev, wpan_dev, min_be, max_be);
  588. }
  589. static int
  590. nl802154_set_max_csma_backoffs(struct sk_buff *skb, struct genl_info *info)
  591. {
  592. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  593. struct net_device *dev = info->user_ptr[1];
  594. struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  595. u8 max_csma_backoffs;
  596. /* conflict here while other running iface settings */
  597. if (netif_running(dev))
  598. return -EBUSY;
  599. if (!info->attrs[NL802154_ATTR_MAX_CSMA_BACKOFFS])
  600. return -EINVAL;
  601. max_csma_backoffs = nla_get_u8(
  602. info->attrs[NL802154_ATTR_MAX_CSMA_BACKOFFS]);
  603. /* check 802.15.4 constraints */
  604. if (max_csma_backoffs > 5)
  605. return -EINVAL;
  606. return rdev_set_max_csma_backoffs(rdev, wpan_dev, max_csma_backoffs);
  607. }
  608. static int
  609. nl802154_set_max_frame_retries(struct sk_buff *skb, struct genl_info *info)
  610. {
  611. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  612. struct net_device *dev = info->user_ptr[1];
  613. struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  614. s8 max_frame_retries;
  615. if (netif_running(dev))
  616. return -EBUSY;
  617. if (!info->attrs[NL802154_ATTR_MAX_FRAME_RETRIES])
  618. return -EINVAL;
  619. max_frame_retries = nla_get_s8(
  620. info->attrs[NL802154_ATTR_MAX_FRAME_RETRIES]);
  621. /* check 802.15.4 constraints */
  622. if (max_frame_retries < -1 || max_frame_retries > 7)
  623. return -EINVAL;
  624. return rdev_set_max_frame_retries(rdev, wpan_dev, max_frame_retries);
  625. }
  626. static int nl802154_set_lbt_mode(struct sk_buff *skb, struct genl_info *info)
  627. {
  628. struct cfg802154_registered_device *rdev = info->user_ptr[0];
  629. struct net_device *dev = info->user_ptr[1];
  630. struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  631. bool mode;
  632. if (netif_running(dev))
  633. return -EBUSY;
  634. if (!info->attrs[NL802154_ATTR_LBT_MODE])
  635. return -EINVAL;
  636. mode = !!nla_get_u8(info->attrs[NL802154_ATTR_LBT_MODE]);
  637. return rdev_set_lbt_mode(rdev, wpan_dev, mode);
  638. }
  639. #define NL802154_FLAG_NEED_WPAN_PHY 0x01
  640. #define NL802154_FLAG_NEED_NETDEV 0x02
  641. #define NL802154_FLAG_NEED_RTNL 0x04
  642. #define NL802154_FLAG_CHECK_NETDEV_UP 0x08
  643. #define NL802154_FLAG_NEED_NETDEV_UP (NL802154_FLAG_NEED_NETDEV |\
  644. NL802154_FLAG_CHECK_NETDEV_UP)
  645. #define NL802154_FLAG_NEED_WPAN_DEV 0x10
  646. #define NL802154_FLAG_NEED_WPAN_DEV_UP (NL802154_FLAG_NEED_WPAN_DEV |\
  647. NL802154_FLAG_CHECK_NETDEV_UP)
  648. static int nl802154_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
  649. struct genl_info *info)
  650. {
  651. struct cfg802154_registered_device *rdev;
  652. struct wpan_dev *wpan_dev;
  653. struct net_device *dev;
  654. bool rtnl = ops->internal_flags & NL802154_FLAG_NEED_RTNL;
  655. if (rtnl)
  656. rtnl_lock();
  657. if (ops->internal_flags & NL802154_FLAG_NEED_WPAN_PHY) {
  658. rdev = cfg802154_get_dev_from_info(genl_info_net(info), info);
  659. if (IS_ERR(rdev)) {
  660. if (rtnl)
  661. rtnl_unlock();
  662. return PTR_ERR(rdev);
  663. }
  664. info->user_ptr[0] = rdev;
  665. } else if (ops->internal_flags & NL802154_FLAG_NEED_NETDEV ||
  666. ops->internal_flags & NL802154_FLAG_NEED_WPAN_DEV) {
  667. ASSERT_RTNL();
  668. wpan_dev = __cfg802154_wpan_dev_from_attrs(genl_info_net(info),
  669. info->attrs);
  670. if (IS_ERR(wpan_dev)) {
  671. if (rtnl)
  672. rtnl_unlock();
  673. return PTR_ERR(wpan_dev);
  674. }
  675. dev = wpan_dev->netdev;
  676. rdev = wpan_phy_to_rdev(wpan_dev->wpan_phy);
  677. if (ops->internal_flags & NL802154_FLAG_NEED_NETDEV) {
  678. if (!dev) {
  679. if (rtnl)
  680. rtnl_unlock();
  681. return -EINVAL;
  682. }
  683. info->user_ptr[1] = dev;
  684. } else {
  685. info->user_ptr[1] = wpan_dev;
  686. }
  687. if (dev) {
  688. if (ops->internal_flags & NL802154_FLAG_CHECK_NETDEV_UP &&
  689. !netif_running(dev)) {
  690. if (rtnl)
  691. rtnl_unlock();
  692. return -ENETDOWN;
  693. }
  694. dev_hold(dev);
  695. }
  696. info->user_ptr[0] = rdev;
  697. }
  698. return 0;
  699. }
  700. static void nl802154_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
  701. struct genl_info *info)
  702. {
  703. if (info->user_ptr[1]) {
  704. if (ops->internal_flags & NL802154_FLAG_NEED_WPAN_DEV) {
  705. struct wpan_dev *wpan_dev = info->user_ptr[1];
  706. if (wpan_dev->netdev)
  707. dev_put(wpan_dev->netdev);
  708. } else {
  709. dev_put(info->user_ptr[1]);
  710. }
  711. }
  712. if (ops->internal_flags & NL802154_FLAG_NEED_RTNL)
  713. rtnl_unlock();
  714. }
  715. static const struct genl_ops nl802154_ops[] = {
  716. {
  717. .cmd = NL802154_CMD_GET_WPAN_PHY,
  718. .doit = nl802154_get_wpan_phy,
  719. .dumpit = nl802154_dump_wpan_phy,
  720. .done = nl802154_dump_wpan_phy_done,
  721. .policy = nl802154_policy,
  722. /* can be retrieved by unprivileged users */
  723. .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
  724. NL802154_FLAG_NEED_RTNL,
  725. },
  726. {
  727. .cmd = NL802154_CMD_GET_INTERFACE,
  728. .doit = nl802154_get_interface,
  729. .dumpit = nl802154_dump_interface,
  730. .policy = nl802154_policy,
  731. /* can be retrieved by unprivileged users */
  732. .internal_flags = NL802154_FLAG_NEED_WPAN_DEV |
  733. NL802154_FLAG_NEED_RTNL,
  734. },
  735. {
  736. .cmd = NL802154_CMD_NEW_INTERFACE,
  737. .doit = nl802154_new_interface,
  738. .policy = nl802154_policy,
  739. .flags = GENL_ADMIN_PERM,
  740. .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
  741. NL802154_FLAG_NEED_RTNL,
  742. },
  743. {
  744. .cmd = NL802154_CMD_DEL_INTERFACE,
  745. .doit = nl802154_del_interface,
  746. .policy = nl802154_policy,
  747. .flags = GENL_ADMIN_PERM,
  748. .internal_flags = NL802154_FLAG_NEED_WPAN_DEV |
  749. NL802154_FLAG_NEED_RTNL,
  750. },
  751. {
  752. .cmd = NL802154_CMD_SET_CHANNEL,
  753. .doit = nl802154_set_channel,
  754. .policy = nl802154_policy,
  755. .flags = GENL_ADMIN_PERM,
  756. .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
  757. NL802154_FLAG_NEED_RTNL,
  758. },
  759. {
  760. .cmd = NL802154_CMD_SET_CCA_MODE,
  761. .doit = nl802154_set_cca_mode,
  762. .policy = nl802154_policy,
  763. .flags = GENL_ADMIN_PERM,
  764. .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
  765. NL802154_FLAG_NEED_RTNL,
  766. },
  767. {
  768. .cmd = NL802154_CMD_SET_PAN_ID,
  769. .doit = nl802154_set_pan_id,
  770. .policy = nl802154_policy,
  771. .flags = GENL_ADMIN_PERM,
  772. .internal_flags = NL802154_FLAG_NEED_NETDEV |
  773. NL802154_FLAG_NEED_RTNL,
  774. },
  775. {
  776. .cmd = NL802154_CMD_SET_SHORT_ADDR,
  777. .doit = nl802154_set_short_addr,
  778. .policy = nl802154_policy,
  779. .flags = GENL_ADMIN_PERM,
  780. .internal_flags = NL802154_FLAG_NEED_NETDEV |
  781. NL802154_FLAG_NEED_RTNL,
  782. },
  783. {
  784. .cmd = NL802154_CMD_SET_BACKOFF_EXPONENT,
  785. .doit = nl802154_set_backoff_exponent,
  786. .policy = nl802154_policy,
  787. .flags = GENL_ADMIN_PERM,
  788. .internal_flags = NL802154_FLAG_NEED_NETDEV |
  789. NL802154_FLAG_NEED_RTNL,
  790. },
  791. {
  792. .cmd = NL802154_CMD_SET_MAX_CSMA_BACKOFFS,
  793. .doit = nl802154_set_max_csma_backoffs,
  794. .policy = nl802154_policy,
  795. .flags = GENL_ADMIN_PERM,
  796. .internal_flags = NL802154_FLAG_NEED_NETDEV |
  797. NL802154_FLAG_NEED_RTNL,
  798. },
  799. {
  800. .cmd = NL802154_CMD_SET_MAX_FRAME_RETRIES,
  801. .doit = nl802154_set_max_frame_retries,
  802. .policy = nl802154_policy,
  803. .flags = GENL_ADMIN_PERM,
  804. .internal_flags = NL802154_FLAG_NEED_NETDEV |
  805. NL802154_FLAG_NEED_RTNL,
  806. },
  807. {
  808. .cmd = NL802154_CMD_SET_LBT_MODE,
  809. .doit = nl802154_set_lbt_mode,
  810. .policy = nl802154_policy,
  811. .flags = GENL_ADMIN_PERM,
  812. .internal_flags = NL802154_FLAG_NEED_NETDEV |
  813. NL802154_FLAG_NEED_RTNL,
  814. },
  815. };
  816. /* initialisation/exit functions */
  817. int nl802154_init(void)
  818. {
  819. return genl_register_family_with_ops_groups(&nl802154_fam, nl802154_ops,
  820. nl802154_mcgrps);
  821. }
  822. void nl802154_exit(void)
  823. {
  824. genl_unregister_family(&nl802154_fam);
  825. }