iface.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. /*
  2. * Copyright 2007-2012 Siemens AG
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * Written by:
  14. * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
  15. * Sergey Lapin <slapin@ossfans.org>
  16. * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
  17. * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
  18. */
  19. #include <linux/netdevice.h>
  20. #include <linux/module.h>
  21. #include <linux/if_arp.h>
  22. #include <linux/ieee802154.h>
  23. #include <net/nl802154.h>
  24. #include <net/mac802154.h>
  25. #include <net/ieee802154_netdev.h>
  26. #include <net/cfg802154.h>
  27. #include "ieee802154_i.h"
  28. #include "driver-ops.h"
  29. int mac802154_wpan_update_llsec(struct net_device *dev)
  30. {
  31. struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
  32. struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
  33. struct wpan_dev *wpan_dev = &sdata->wpan_dev;
  34. int rc = 0;
  35. if (ops->llsec) {
  36. struct ieee802154_llsec_params params;
  37. int changed = 0;
  38. params.pan_id = wpan_dev->pan_id;
  39. changed |= IEEE802154_LLSEC_PARAM_PAN_ID;
  40. params.hwaddr = wpan_dev->extended_addr;
  41. changed |= IEEE802154_LLSEC_PARAM_HWADDR;
  42. rc = ops->llsec->set_params(dev, &params, changed);
  43. }
  44. return rc;
  45. }
  46. static int
  47. mac802154_wpan_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  48. {
  49. struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
  50. struct wpan_dev *wpan_dev = &sdata->wpan_dev;
  51. struct sockaddr_ieee802154 *sa =
  52. (struct sockaddr_ieee802154 *)&ifr->ifr_addr;
  53. int err = -ENOIOCTLCMD;
  54. if (cmd != SIOCGIFADDR && cmd != SIOCSIFADDR)
  55. return err;
  56. rtnl_lock();
  57. switch (cmd) {
  58. case SIOCGIFADDR:
  59. {
  60. u16 pan_id, short_addr;
  61. pan_id = le16_to_cpu(wpan_dev->pan_id);
  62. short_addr = le16_to_cpu(wpan_dev->short_addr);
  63. if (pan_id == IEEE802154_PANID_BROADCAST ||
  64. short_addr == IEEE802154_ADDR_BROADCAST) {
  65. err = -EADDRNOTAVAIL;
  66. break;
  67. }
  68. sa->family = AF_IEEE802154;
  69. sa->addr.addr_type = IEEE802154_ADDR_SHORT;
  70. sa->addr.pan_id = pan_id;
  71. sa->addr.short_addr = short_addr;
  72. err = 0;
  73. break;
  74. }
  75. case SIOCSIFADDR:
  76. if (netif_running(dev)) {
  77. rtnl_unlock();
  78. return -EBUSY;
  79. }
  80. dev_warn(&dev->dev,
  81. "Using DEBUGing ioctl SIOCSIFADDR isn't recommended!\n");
  82. if (sa->family != AF_IEEE802154 ||
  83. sa->addr.addr_type != IEEE802154_ADDR_SHORT ||
  84. sa->addr.pan_id == IEEE802154_PANID_BROADCAST ||
  85. sa->addr.short_addr == IEEE802154_ADDR_BROADCAST ||
  86. sa->addr.short_addr == IEEE802154_ADDR_UNDEF) {
  87. err = -EINVAL;
  88. break;
  89. }
  90. wpan_dev->pan_id = cpu_to_le16(sa->addr.pan_id);
  91. wpan_dev->short_addr = cpu_to_le16(sa->addr.short_addr);
  92. err = mac802154_wpan_update_llsec(dev);
  93. break;
  94. }
  95. rtnl_unlock();
  96. return err;
  97. }
  98. static int mac802154_wpan_mac_addr(struct net_device *dev, void *p)
  99. {
  100. struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
  101. struct sockaddr *addr = p;
  102. __le64 extended_addr;
  103. if (netif_running(dev))
  104. return -EBUSY;
  105. /* lowpan need to be down for update
  106. * SLAAC address after ifup
  107. */
  108. if (sdata->wpan_dev.lowpan_dev) {
  109. if (netif_running(sdata->wpan_dev.lowpan_dev))
  110. return -EBUSY;
  111. }
  112. ieee802154_be64_to_le64(&extended_addr, addr->sa_data);
  113. if (!ieee802154_is_valid_extended_unicast_addr(extended_addr))
  114. return -EINVAL;
  115. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  116. sdata->wpan_dev.extended_addr = extended_addr;
  117. /* update lowpan interface mac address when
  118. * wpan mac has been changed
  119. */
  120. if (sdata->wpan_dev.lowpan_dev)
  121. memcpy(sdata->wpan_dev.lowpan_dev->dev_addr, dev->dev_addr,
  122. dev->addr_len);
  123. return mac802154_wpan_update_llsec(dev);
  124. }
  125. static int ieee802154_setup_hw(struct ieee802154_sub_if_data *sdata)
  126. {
  127. struct ieee802154_local *local = sdata->local;
  128. struct wpan_dev *wpan_dev = &sdata->wpan_dev;
  129. int ret;
  130. if (local->hw.flags & IEEE802154_HW_PROMISCUOUS) {
  131. ret = drv_set_promiscuous_mode(local,
  132. wpan_dev->promiscuous_mode);
  133. if (ret < 0)
  134. return ret;
  135. }
  136. if (local->hw.flags & IEEE802154_HW_AFILT) {
  137. ret = drv_set_pan_id(local, wpan_dev->pan_id);
  138. if (ret < 0)
  139. return ret;
  140. ret = drv_set_extended_addr(local, wpan_dev->extended_addr);
  141. if (ret < 0)
  142. return ret;
  143. ret = drv_set_short_addr(local, wpan_dev->short_addr);
  144. if (ret < 0)
  145. return ret;
  146. }
  147. if (local->hw.flags & IEEE802154_HW_LBT) {
  148. ret = drv_set_lbt_mode(local, wpan_dev->lbt);
  149. if (ret < 0)
  150. return ret;
  151. }
  152. if (local->hw.flags & IEEE802154_HW_CSMA_PARAMS) {
  153. ret = drv_set_csma_params(local, wpan_dev->min_be,
  154. wpan_dev->max_be,
  155. wpan_dev->csma_retries);
  156. if (ret < 0)
  157. return ret;
  158. }
  159. if (local->hw.flags & IEEE802154_HW_FRAME_RETRIES) {
  160. ret = drv_set_max_frame_retries(local, wpan_dev->frame_retries);
  161. if (ret < 0)
  162. return ret;
  163. }
  164. return 0;
  165. }
  166. static int mac802154_slave_open(struct net_device *dev)
  167. {
  168. struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
  169. struct ieee802154_local *local = sdata->local;
  170. int res;
  171. ASSERT_RTNL();
  172. set_bit(SDATA_STATE_RUNNING, &sdata->state);
  173. if (!local->open_count) {
  174. res = ieee802154_setup_hw(sdata);
  175. if (res)
  176. goto err;
  177. res = drv_start(local);
  178. if (res)
  179. goto err;
  180. }
  181. local->open_count++;
  182. netif_start_queue(dev);
  183. return 0;
  184. err:
  185. /* might already be clear but that doesn't matter */
  186. clear_bit(SDATA_STATE_RUNNING, &sdata->state);
  187. return res;
  188. }
  189. static int
  190. ieee802154_check_mac_settings(struct ieee802154_local *local,
  191. struct wpan_dev *wpan_dev,
  192. struct wpan_dev *nwpan_dev)
  193. {
  194. ASSERT_RTNL();
  195. if (local->hw.flags & IEEE802154_HW_PROMISCUOUS) {
  196. if (wpan_dev->promiscuous_mode != nwpan_dev->promiscuous_mode)
  197. return -EBUSY;
  198. }
  199. if (local->hw.flags & IEEE802154_HW_AFILT) {
  200. if (wpan_dev->pan_id != nwpan_dev->pan_id ||
  201. wpan_dev->short_addr != nwpan_dev->short_addr ||
  202. wpan_dev->extended_addr != nwpan_dev->extended_addr)
  203. return -EBUSY;
  204. }
  205. if (local->hw.flags & IEEE802154_HW_CSMA_PARAMS) {
  206. if (wpan_dev->min_be != nwpan_dev->min_be ||
  207. wpan_dev->max_be != nwpan_dev->max_be ||
  208. wpan_dev->csma_retries != nwpan_dev->csma_retries)
  209. return -EBUSY;
  210. }
  211. if (local->hw.flags & IEEE802154_HW_FRAME_RETRIES) {
  212. if (wpan_dev->frame_retries != nwpan_dev->frame_retries)
  213. return -EBUSY;
  214. }
  215. if (local->hw.flags & IEEE802154_HW_LBT) {
  216. if (wpan_dev->lbt != nwpan_dev->lbt)
  217. return -EBUSY;
  218. }
  219. return 0;
  220. }
  221. static int
  222. ieee802154_check_concurrent_iface(struct ieee802154_sub_if_data *sdata,
  223. enum nl802154_iftype iftype)
  224. {
  225. struct ieee802154_local *local = sdata->local;
  226. struct wpan_dev *wpan_dev = &sdata->wpan_dev;
  227. struct ieee802154_sub_if_data *nsdata;
  228. /* we hold the RTNL here so can safely walk the list */
  229. list_for_each_entry(nsdata, &local->interfaces, list) {
  230. if (nsdata != sdata && ieee802154_sdata_running(nsdata)) {
  231. int ret;
  232. /* TODO currently we don't support multiple node types
  233. * we need to run skb_clone at rx path. Check if there
  234. * exist really an use case if we need to support
  235. * multiple node types at the same time.
  236. */
  237. if (wpan_dev->iftype == NL802154_IFTYPE_NODE &&
  238. nsdata->wpan_dev.iftype == NL802154_IFTYPE_NODE)
  239. return -EBUSY;
  240. /* check all phy mac sublayer settings are the same.
  241. * We have only one phy, different values makes trouble.
  242. */
  243. ret = ieee802154_check_mac_settings(local, wpan_dev,
  244. &nsdata->wpan_dev);
  245. if (ret < 0)
  246. return ret;
  247. }
  248. }
  249. return 0;
  250. }
  251. static int mac802154_wpan_open(struct net_device *dev)
  252. {
  253. int rc;
  254. struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
  255. struct wpan_dev *wpan_dev = &sdata->wpan_dev;
  256. rc = ieee802154_check_concurrent_iface(sdata, wpan_dev->iftype);
  257. if (rc < 0)
  258. return rc;
  259. return mac802154_slave_open(dev);
  260. }
  261. static int mac802154_slave_close(struct net_device *dev)
  262. {
  263. struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
  264. struct ieee802154_local *local = sdata->local;
  265. ASSERT_RTNL();
  266. netif_stop_queue(dev);
  267. local->open_count--;
  268. clear_bit(SDATA_STATE_RUNNING, &sdata->state);
  269. if (!local->open_count)
  270. ieee802154_stop_device(local);
  271. return 0;
  272. }
  273. static int mac802154_set_header_security(struct ieee802154_sub_if_data *sdata,
  274. struct ieee802154_hdr *hdr,
  275. const struct ieee802154_mac_cb *cb)
  276. {
  277. struct ieee802154_llsec_params params;
  278. u8 level;
  279. mac802154_llsec_get_params(&sdata->sec, &params);
  280. if (!params.enabled && cb->secen_override && cb->secen)
  281. return -EINVAL;
  282. if (!params.enabled ||
  283. (cb->secen_override && !cb->secen) ||
  284. !params.out_level)
  285. return 0;
  286. if (cb->seclevel_override && !cb->seclevel)
  287. return -EINVAL;
  288. level = cb->seclevel_override ? cb->seclevel : params.out_level;
  289. hdr->fc.security_enabled = 1;
  290. hdr->sec.level = level;
  291. hdr->sec.key_id_mode = params.out_key.mode;
  292. if (params.out_key.mode == IEEE802154_SCF_KEY_SHORT_INDEX)
  293. hdr->sec.short_src = params.out_key.short_source;
  294. else if (params.out_key.mode == IEEE802154_SCF_KEY_HW_INDEX)
  295. hdr->sec.extended_src = params.out_key.extended_source;
  296. hdr->sec.key_id = params.out_key.id;
  297. return 0;
  298. }
  299. static int ieee802154_header_create(struct sk_buff *skb,
  300. struct net_device *dev,
  301. const struct ieee802154_addr *daddr,
  302. const struct ieee802154_addr *saddr,
  303. unsigned len)
  304. {
  305. struct ieee802154_hdr hdr;
  306. struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
  307. struct wpan_dev *wpan_dev = &sdata->wpan_dev;
  308. struct ieee802154_mac_cb *cb = mac_cb(skb);
  309. int hlen;
  310. if (!daddr)
  311. return -EINVAL;
  312. memset(&hdr.fc, 0, sizeof(hdr.fc));
  313. hdr.fc.type = cb->type;
  314. hdr.fc.security_enabled = cb->secen;
  315. hdr.fc.ack_request = cb->ackreq;
  316. hdr.seq = atomic_inc_return(&dev->ieee802154_ptr->dsn) & 0xFF;
  317. if (mac802154_set_header_security(sdata, &hdr, cb) < 0)
  318. return -EINVAL;
  319. if (!saddr) {
  320. if (wpan_dev->short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST) ||
  321. wpan_dev->short_addr == cpu_to_le16(IEEE802154_ADDR_UNDEF) ||
  322. wpan_dev->pan_id == cpu_to_le16(IEEE802154_PANID_BROADCAST)) {
  323. hdr.source.mode = IEEE802154_ADDR_LONG;
  324. hdr.source.extended_addr = wpan_dev->extended_addr;
  325. } else {
  326. hdr.source.mode = IEEE802154_ADDR_SHORT;
  327. hdr.source.short_addr = wpan_dev->short_addr;
  328. }
  329. hdr.source.pan_id = wpan_dev->pan_id;
  330. } else {
  331. hdr.source = *(const struct ieee802154_addr *)saddr;
  332. }
  333. hdr.dest = *(const struct ieee802154_addr *)daddr;
  334. hlen = ieee802154_hdr_push(skb, &hdr);
  335. if (hlen < 0)
  336. return -EINVAL;
  337. skb_reset_mac_header(skb);
  338. skb->mac_len = hlen;
  339. if (len > ieee802154_max_payload(&hdr))
  340. return -EMSGSIZE;
  341. return hlen;
  342. }
  343. static const struct wpan_dev_header_ops ieee802154_header_ops = {
  344. .create = ieee802154_header_create,
  345. };
  346. /* This header create functionality assumes a 8 byte array for
  347. * source and destination pointer at maximum. To adapt this for
  348. * the 802.15.4 dataframe header we use extended address handling
  349. * here only and intra pan connection. fc fields are mostly fallback
  350. * handling. For provide dev_hard_header for dgram sockets.
  351. */
  352. static int mac802154_header_create(struct sk_buff *skb,
  353. struct net_device *dev,
  354. unsigned short type,
  355. const void *daddr,
  356. const void *saddr,
  357. unsigned len)
  358. {
  359. struct ieee802154_hdr hdr;
  360. struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
  361. struct wpan_dev *wpan_dev = &sdata->wpan_dev;
  362. struct ieee802154_mac_cb cb = { };
  363. int hlen;
  364. if (!daddr)
  365. return -EINVAL;
  366. memset(&hdr.fc, 0, sizeof(hdr.fc));
  367. hdr.fc.type = IEEE802154_FC_TYPE_DATA;
  368. hdr.fc.ack_request = wpan_dev->ackreq;
  369. hdr.seq = atomic_inc_return(&dev->ieee802154_ptr->dsn) & 0xFF;
  370. /* TODO currently a workaround to give zero cb block to set
  371. * security parameters defaults according MIB.
  372. */
  373. if (mac802154_set_header_security(sdata, &hdr, &cb) < 0)
  374. return -EINVAL;
  375. hdr.dest.pan_id = wpan_dev->pan_id;
  376. hdr.dest.mode = IEEE802154_ADDR_LONG;
  377. ieee802154_be64_to_le64(&hdr.dest.extended_addr, daddr);
  378. hdr.source.pan_id = hdr.dest.pan_id;
  379. hdr.source.mode = IEEE802154_ADDR_LONG;
  380. if (!saddr)
  381. hdr.source.extended_addr = wpan_dev->extended_addr;
  382. else
  383. ieee802154_be64_to_le64(&hdr.source.extended_addr, saddr);
  384. hlen = ieee802154_hdr_push(skb, &hdr);
  385. if (hlen < 0)
  386. return -EINVAL;
  387. skb_reset_mac_header(skb);
  388. skb->mac_len = hlen;
  389. if (len > ieee802154_max_payload(&hdr))
  390. return -EMSGSIZE;
  391. return hlen;
  392. }
  393. static int
  394. mac802154_header_parse(const struct sk_buff *skb, unsigned char *haddr)
  395. {
  396. struct ieee802154_hdr hdr;
  397. if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0) {
  398. pr_debug("malformed packet\n");
  399. return 0;
  400. }
  401. if (hdr.source.mode == IEEE802154_ADDR_LONG) {
  402. ieee802154_le64_to_be64(haddr, &hdr.source.extended_addr);
  403. return IEEE802154_EXTENDED_ADDR_LEN;
  404. }
  405. return 0;
  406. }
  407. static const struct header_ops mac802154_header_ops = {
  408. .create = mac802154_header_create,
  409. .parse = mac802154_header_parse,
  410. };
  411. static const struct net_device_ops mac802154_wpan_ops = {
  412. .ndo_open = mac802154_wpan_open,
  413. .ndo_stop = mac802154_slave_close,
  414. .ndo_start_xmit = ieee802154_subif_start_xmit,
  415. .ndo_do_ioctl = mac802154_wpan_ioctl,
  416. .ndo_set_mac_address = mac802154_wpan_mac_addr,
  417. };
  418. static const struct net_device_ops mac802154_monitor_ops = {
  419. .ndo_open = mac802154_wpan_open,
  420. .ndo_stop = mac802154_slave_close,
  421. .ndo_start_xmit = ieee802154_monitor_start_xmit,
  422. };
  423. static void mac802154_wpan_free(struct net_device *dev)
  424. {
  425. struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
  426. mac802154_llsec_destroy(&sdata->sec);
  427. free_netdev(dev);
  428. }
  429. static void ieee802154_if_setup(struct net_device *dev)
  430. {
  431. dev->addr_len = IEEE802154_EXTENDED_ADDR_LEN;
  432. memset(dev->broadcast, 0xff, IEEE802154_EXTENDED_ADDR_LEN);
  433. /* Let hard_header_len set to IEEE802154_MIN_HEADER_LEN. AF_PACKET
  434. * will not send frames without any payload, but ack frames
  435. * has no payload, so substract one that we can send a 3 bytes
  436. * frame. The xmit callback assumes at least a hard header where two
  437. * bytes fc and sequence field are set.
  438. */
  439. dev->hard_header_len = IEEE802154_MIN_HEADER_LEN - 1;
  440. /* The auth_tag header is for security and places in private payload
  441. * room of mac frame which stucks between payload and FCS field.
  442. */
  443. dev->needed_tailroom = IEEE802154_MAX_AUTH_TAG_LEN +
  444. IEEE802154_FCS_LEN;
  445. /* The mtu size is the payload without mac header in this case.
  446. * We have a dynamic length header with a minimum header length
  447. * which is hard_header_len. In this case we let mtu to the size
  448. * of maximum payload which is IEEE802154_MTU - IEEE802154_FCS_LEN -
  449. * hard_header_len. The FCS which is set by hardware or ndo_start_xmit
  450. * and the minimum mac header which can be evaluated inside driver
  451. * layer. The rest of mac header will be part of payload if greater
  452. * than hard_header_len.
  453. */
  454. dev->mtu = IEEE802154_MTU - IEEE802154_FCS_LEN -
  455. dev->hard_header_len;
  456. dev->tx_queue_len = 300;
  457. dev->flags = IFF_NOARP | IFF_BROADCAST;
  458. }
  459. static int
  460. ieee802154_setup_sdata(struct ieee802154_sub_if_data *sdata,
  461. enum nl802154_iftype type)
  462. {
  463. struct wpan_dev *wpan_dev = &sdata->wpan_dev;
  464. int ret;
  465. u8 tmp;
  466. /* set some type-dependent values */
  467. sdata->wpan_dev.iftype = type;
  468. get_random_bytes(&tmp, sizeof(tmp));
  469. atomic_set(&wpan_dev->bsn, tmp);
  470. get_random_bytes(&tmp, sizeof(tmp));
  471. atomic_set(&wpan_dev->dsn, tmp);
  472. /* defaults per 802.15.4-2011 */
  473. wpan_dev->min_be = 3;
  474. wpan_dev->max_be = 5;
  475. wpan_dev->csma_retries = 4;
  476. wpan_dev->frame_retries = 3;
  477. wpan_dev->pan_id = cpu_to_le16(IEEE802154_PANID_BROADCAST);
  478. wpan_dev->short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST);
  479. switch (type) {
  480. case NL802154_IFTYPE_NODE:
  481. ieee802154_be64_to_le64(&wpan_dev->extended_addr,
  482. sdata->dev->dev_addr);
  483. sdata->dev->header_ops = &mac802154_header_ops;
  484. sdata->dev->destructor = mac802154_wpan_free;
  485. sdata->dev->netdev_ops = &mac802154_wpan_ops;
  486. sdata->dev->ml_priv = &mac802154_mlme_wpan;
  487. wpan_dev->promiscuous_mode = false;
  488. wpan_dev->header_ops = &ieee802154_header_ops;
  489. mutex_init(&sdata->sec_mtx);
  490. mac802154_llsec_init(&sdata->sec);
  491. ret = mac802154_wpan_update_llsec(sdata->dev);
  492. if (ret < 0)
  493. return ret;
  494. break;
  495. case NL802154_IFTYPE_MONITOR:
  496. sdata->dev->destructor = free_netdev;
  497. sdata->dev->netdev_ops = &mac802154_monitor_ops;
  498. wpan_dev->promiscuous_mode = true;
  499. break;
  500. default:
  501. BUG();
  502. }
  503. return 0;
  504. }
  505. struct net_device *
  506. ieee802154_if_add(struct ieee802154_local *local, const char *name,
  507. unsigned char name_assign_type, enum nl802154_iftype type,
  508. __le64 extended_addr)
  509. {
  510. struct net_device *ndev = NULL;
  511. struct ieee802154_sub_if_data *sdata = NULL;
  512. int ret = -ENOMEM;
  513. ASSERT_RTNL();
  514. ndev = alloc_netdev(sizeof(*sdata), name,
  515. name_assign_type, ieee802154_if_setup);
  516. if (!ndev)
  517. return ERR_PTR(-ENOMEM);
  518. ndev->needed_headroom = local->hw.extra_tx_headroom +
  519. IEEE802154_MAX_HEADER_LEN;
  520. ret = dev_alloc_name(ndev, ndev->name);
  521. if (ret < 0)
  522. goto err;
  523. ieee802154_le64_to_be64(ndev->perm_addr,
  524. &local->hw.phy->perm_extended_addr);
  525. switch (type) {
  526. case NL802154_IFTYPE_NODE:
  527. ndev->type = ARPHRD_IEEE802154;
  528. if (ieee802154_is_valid_extended_unicast_addr(extended_addr))
  529. ieee802154_le64_to_be64(ndev->dev_addr, &extended_addr);
  530. else
  531. memcpy(ndev->dev_addr, ndev->perm_addr,
  532. IEEE802154_EXTENDED_ADDR_LEN);
  533. break;
  534. case NL802154_IFTYPE_MONITOR:
  535. ndev->type = ARPHRD_IEEE802154_MONITOR;
  536. break;
  537. default:
  538. ret = -EINVAL;
  539. goto err;
  540. }
  541. /* TODO check this */
  542. SET_NETDEV_DEV(ndev, &local->phy->dev);
  543. sdata = netdev_priv(ndev);
  544. ndev->ieee802154_ptr = &sdata->wpan_dev;
  545. memcpy(sdata->name, ndev->name, IFNAMSIZ);
  546. sdata->dev = ndev;
  547. sdata->wpan_dev.wpan_phy = local->hw.phy;
  548. sdata->local = local;
  549. /* setup type-dependent data */
  550. ret = ieee802154_setup_sdata(sdata, type);
  551. if (ret)
  552. goto err;
  553. ret = register_netdevice(ndev);
  554. if (ret < 0)
  555. goto err;
  556. mutex_lock(&local->iflist_mtx);
  557. list_add_tail_rcu(&sdata->list, &local->interfaces);
  558. mutex_unlock(&local->iflist_mtx);
  559. return ndev;
  560. err:
  561. free_netdev(ndev);
  562. return ERR_PTR(ret);
  563. }
  564. void ieee802154_if_remove(struct ieee802154_sub_if_data *sdata)
  565. {
  566. ASSERT_RTNL();
  567. mutex_lock(&sdata->local->iflist_mtx);
  568. list_del_rcu(&sdata->list);
  569. mutex_unlock(&sdata->local->iflist_mtx);
  570. synchronize_rcu();
  571. unregister_netdevice(sdata->dev);
  572. }
  573. void ieee802154_remove_interfaces(struct ieee802154_local *local)
  574. {
  575. struct ieee802154_sub_if_data *sdata, *tmp;
  576. mutex_lock(&local->iflist_mtx);
  577. list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
  578. list_del(&sdata->list);
  579. unregister_netdevice(sdata->dev);
  580. }
  581. mutex_unlock(&local->iflist_mtx);
  582. }
  583. static int netdev_notify(struct notifier_block *nb,
  584. unsigned long state, void *ptr)
  585. {
  586. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  587. struct ieee802154_sub_if_data *sdata;
  588. if (state != NETDEV_CHANGENAME)
  589. return NOTIFY_DONE;
  590. if (!dev->ieee802154_ptr || !dev->ieee802154_ptr->wpan_phy)
  591. return NOTIFY_DONE;
  592. if (dev->ieee802154_ptr->wpan_phy->privid != mac802154_wpan_phy_privid)
  593. return NOTIFY_DONE;
  594. sdata = IEEE802154_DEV_TO_SUB_IF(dev);
  595. memcpy(sdata->name, dev->name, IFNAMSIZ);
  596. return NOTIFY_OK;
  597. }
  598. static struct notifier_block mac802154_netdev_notifier = {
  599. .notifier_call = netdev_notify,
  600. };
  601. int ieee802154_iface_init(void)
  602. {
  603. return register_netdevice_notifier(&mac802154_netdev_notifier);
  604. }
  605. void ieee802154_iface_exit(void)
  606. {
  607. unregister_netdevice_notifier(&mac802154_netdev_notifier);
  608. }