6lowpan_rtnl.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. /* Copyright 2011, Siemens AG
  2. * written by Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
  3. */
  4. /* Based on patches from Jon Smirl <jonsmirl@gmail.com>
  5. * Copyright (c) 2011 Jon Smirl <jonsmirl@gmail.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. /* Jon's code is based on 6lowpan implementation for Contiki which is:
  17. * Copyright (c) 2008, Swedish Institute of Computer Science.
  18. * All rights reserved.
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. * 1. Redistributions of source code must retain the above copyright
  24. * notice, this list of conditions and the following disclaimer.
  25. * 2. Redistributions in binary form must reproduce the above copyright
  26. * notice, this list of conditions and the following disclaimer in the
  27. * documentation and/or other materials provided with the distribution.
  28. * 3. Neither the name of the Institute nor the names of its contributors
  29. * may be used to endorse or promote products derived from this software
  30. * without specific prior written permission.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  33. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  34. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  36. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  37. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  38. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  39. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  40. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  41. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  42. * SUCH DAMAGE.
  43. */
  44. #include <linux/bitops.h>
  45. #include <linux/if_arp.h>
  46. #include <linux/module.h>
  47. #include <linux/moduleparam.h>
  48. #include <linux/netdevice.h>
  49. #include <net/af_ieee802154.h>
  50. #include <net/ieee802154.h>
  51. #include <net/ieee802154_netdev.h>
  52. #include <net/6lowpan.h>
  53. #include <net/ipv6.h>
  54. #include "reassembly.h"
  55. static LIST_HEAD(lowpan_devices);
  56. /* private device info */
  57. struct lowpan_dev_info {
  58. struct net_device *real_dev; /* real WPAN device ptr */
  59. struct mutex dev_list_mtx; /* mutex for list ops */
  60. __be16 fragment_tag;
  61. };
  62. struct lowpan_dev_record {
  63. struct net_device *ldev;
  64. struct list_head list;
  65. };
  66. static inline struct
  67. lowpan_dev_info *lowpan_dev_info(const struct net_device *dev)
  68. {
  69. return netdev_priv(dev);
  70. }
  71. static inline void lowpan_address_flip(u8 *src, u8 *dest)
  72. {
  73. int i;
  74. for (i = 0; i < IEEE802154_ADDR_LEN; i++)
  75. (dest)[IEEE802154_ADDR_LEN - i - 1] = (src)[i];
  76. }
  77. static int lowpan_header_create(struct sk_buff *skb, struct net_device *dev,
  78. unsigned short type, const void *_daddr,
  79. const void *_saddr, unsigned int len)
  80. {
  81. const u8 *saddr = _saddr;
  82. const u8 *daddr = _daddr;
  83. struct ieee802154_addr sa, da;
  84. struct ieee802154_mac_cb *cb = mac_cb_init(skb);
  85. /* TODO:
  86. * if this package isn't ipv6 one, where should it be routed?
  87. */
  88. if (type != ETH_P_IPV6)
  89. return 0;
  90. if (!saddr)
  91. saddr = dev->dev_addr;
  92. raw_dump_inline(__func__, "saddr", (unsigned char *)saddr, 8);
  93. raw_dump_inline(__func__, "daddr", (unsigned char *)daddr, 8);
  94. lowpan_header_compress(skb, dev, type, daddr, saddr, len);
  95. /* NOTE1: I'm still unsure about the fact that compression and WPAN
  96. * header are created here and not later in the xmit. So wait for
  97. * an opinion of net maintainers.
  98. */
  99. /* NOTE2: to be absolutely correct, we must derive PANid information
  100. * from MAC subif of the 'dev' and 'real_dev' network devices, but
  101. * this isn't implemented in mainline yet, so currently we assign 0xff
  102. */
  103. cb->type = IEEE802154_FC_TYPE_DATA;
  104. /* prepare wpan address data */
  105. sa.mode = IEEE802154_ADDR_LONG;
  106. sa.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
  107. sa.extended_addr = ieee802154_devaddr_from_raw(saddr);
  108. /* intra-PAN communications */
  109. da.pan_id = sa.pan_id;
  110. /* if the destination address is the broadcast address, use the
  111. * corresponding short address
  112. */
  113. if (lowpan_is_addr_broadcast(daddr)) {
  114. da.mode = IEEE802154_ADDR_SHORT;
  115. da.short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST);
  116. } else {
  117. da.mode = IEEE802154_ADDR_LONG;
  118. da.extended_addr = ieee802154_devaddr_from_raw(daddr);
  119. }
  120. cb->ackreq = !lowpan_is_addr_broadcast(daddr);
  121. return dev_hard_header(skb, lowpan_dev_info(dev)->real_dev,
  122. type, (void *)&da, (void *)&sa, 0);
  123. }
  124. static int lowpan_give_skb_to_devices(struct sk_buff *skb,
  125. struct net_device *dev)
  126. {
  127. struct lowpan_dev_record *entry;
  128. struct sk_buff *skb_cp;
  129. int stat = NET_RX_SUCCESS;
  130. rcu_read_lock();
  131. list_for_each_entry_rcu(entry, &lowpan_devices, list)
  132. if (lowpan_dev_info(entry->ldev)->real_dev == skb->dev) {
  133. skb_cp = skb_copy(skb, GFP_ATOMIC);
  134. if (!skb_cp) {
  135. stat = -ENOMEM;
  136. break;
  137. }
  138. skb_cp->dev = entry->ldev;
  139. stat = netif_rx(skb_cp);
  140. }
  141. rcu_read_unlock();
  142. return stat;
  143. }
  144. static int process_data(struct sk_buff *skb, const struct ieee802154_hdr *hdr)
  145. {
  146. u8 iphc0, iphc1;
  147. struct ieee802154_addr_sa sa, da;
  148. void *sap, *dap;
  149. raw_dump_table(__func__, "raw skb data dump", skb->data, skb->len);
  150. /* at least two bytes will be used for the encoding */
  151. if (skb->len < 2)
  152. goto drop;
  153. if (lowpan_fetch_skb_u8(skb, &iphc0))
  154. goto drop;
  155. if (lowpan_fetch_skb_u8(skb, &iphc1))
  156. goto drop;
  157. ieee802154_addr_to_sa(&sa, &hdr->source);
  158. ieee802154_addr_to_sa(&da, &hdr->dest);
  159. if (sa.addr_type == IEEE802154_ADDR_SHORT)
  160. sap = &sa.short_addr;
  161. else
  162. sap = &sa.hwaddr;
  163. if (da.addr_type == IEEE802154_ADDR_SHORT)
  164. dap = &da.short_addr;
  165. else
  166. dap = &da.hwaddr;
  167. return lowpan_process_data(skb, skb->dev, sap, sa.addr_type,
  168. IEEE802154_ADDR_LEN, dap, da.addr_type,
  169. IEEE802154_ADDR_LEN, iphc0, iphc1,
  170. lowpan_give_skb_to_devices);
  171. drop:
  172. kfree_skb(skb);
  173. return -EINVAL;
  174. }
  175. static int lowpan_set_address(struct net_device *dev, void *p)
  176. {
  177. struct sockaddr *sa = p;
  178. if (netif_running(dev))
  179. return -EBUSY;
  180. /* TODO: validate addr */
  181. memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
  182. return 0;
  183. }
  184. static struct sk_buff*
  185. lowpan_alloc_frag(struct sk_buff *skb, int size,
  186. const struct ieee802154_hdr *master_hdr)
  187. {
  188. struct net_device *real_dev = lowpan_dev_info(skb->dev)->real_dev;
  189. struct sk_buff *frag;
  190. int rc;
  191. frag = alloc_skb(real_dev->hard_header_len +
  192. real_dev->needed_tailroom + size,
  193. GFP_ATOMIC);
  194. if (likely(frag)) {
  195. frag->dev = real_dev;
  196. frag->priority = skb->priority;
  197. skb_reserve(frag, real_dev->hard_header_len);
  198. skb_reset_network_header(frag);
  199. *mac_cb(frag) = *mac_cb(skb);
  200. rc = dev_hard_header(frag, real_dev, 0, &master_hdr->dest,
  201. &master_hdr->source, size);
  202. if (rc < 0) {
  203. kfree_skb(frag);
  204. return ERR_PTR(-rc);
  205. }
  206. } else {
  207. frag = ERR_PTR(-ENOMEM);
  208. }
  209. return frag;
  210. }
  211. static int
  212. lowpan_xmit_fragment(struct sk_buff *skb, const struct ieee802154_hdr *wpan_hdr,
  213. u8 *frag_hdr, int frag_hdrlen,
  214. int offset, int len)
  215. {
  216. struct sk_buff *frag;
  217. raw_dump_inline(__func__, " fragment header", frag_hdr, frag_hdrlen);
  218. frag = lowpan_alloc_frag(skb, frag_hdrlen + len, wpan_hdr);
  219. if (IS_ERR(frag))
  220. return -PTR_ERR(frag);
  221. memcpy(skb_put(frag, frag_hdrlen), frag_hdr, frag_hdrlen);
  222. memcpy(skb_put(frag, len), skb_network_header(skb) + offset, len);
  223. raw_dump_table(__func__, " fragment dump", frag->data, frag->len);
  224. return dev_queue_xmit(frag);
  225. }
  226. static int
  227. lowpan_xmit_fragmented(struct sk_buff *skb, struct net_device *dev,
  228. const struct ieee802154_hdr *wpan_hdr)
  229. {
  230. u16 dgram_size, dgram_offset;
  231. __be16 frag_tag;
  232. u8 frag_hdr[5];
  233. int frag_cap, frag_len, payload_cap, rc;
  234. int skb_unprocessed, skb_offset;
  235. dgram_size = lowpan_uncompress_size(skb, &dgram_offset) -
  236. skb->mac_len;
  237. frag_tag = lowpan_dev_info(dev)->fragment_tag++;
  238. frag_hdr[0] = LOWPAN_DISPATCH_FRAG1 | ((dgram_size >> 8) & 0x07);
  239. frag_hdr[1] = dgram_size & 0xff;
  240. memcpy(frag_hdr + 2, &frag_tag, sizeof(frag_tag));
  241. payload_cap = ieee802154_max_payload(wpan_hdr);
  242. frag_len = round_down(payload_cap - LOWPAN_FRAG1_HEAD_SIZE -
  243. skb_network_header_len(skb), 8);
  244. skb_offset = skb_network_header_len(skb);
  245. skb_unprocessed = skb->len - skb->mac_len - skb_offset;
  246. rc = lowpan_xmit_fragment(skb, wpan_hdr, frag_hdr,
  247. LOWPAN_FRAG1_HEAD_SIZE, 0,
  248. frag_len + skb_network_header_len(skb));
  249. if (rc) {
  250. pr_debug("%s unable to send FRAG1 packet (tag: %d)",
  251. __func__, frag_tag);
  252. goto err;
  253. }
  254. frag_hdr[0] &= ~LOWPAN_DISPATCH_FRAG1;
  255. frag_hdr[0] |= LOWPAN_DISPATCH_FRAGN;
  256. frag_cap = round_down(payload_cap - LOWPAN_FRAGN_HEAD_SIZE, 8);
  257. do {
  258. dgram_offset += frag_len;
  259. skb_offset += frag_len;
  260. skb_unprocessed -= frag_len;
  261. frag_len = min(frag_cap, skb_unprocessed);
  262. frag_hdr[4] = dgram_offset >> 3;
  263. rc = lowpan_xmit_fragment(skb, wpan_hdr, frag_hdr,
  264. LOWPAN_FRAGN_HEAD_SIZE, skb_offset,
  265. frag_len);
  266. if (rc) {
  267. pr_debug("%s unable to send a FRAGN packet. (tag: %d, offset: %d)\n",
  268. __func__, frag_tag, skb_offset);
  269. goto err;
  270. }
  271. } while (skb_unprocessed > frag_cap);
  272. consume_skb(skb);
  273. return NET_XMIT_SUCCESS;
  274. err:
  275. kfree_skb(skb);
  276. return rc;
  277. }
  278. static netdev_tx_t lowpan_xmit(struct sk_buff *skb, struct net_device *dev)
  279. {
  280. struct ieee802154_hdr wpan_hdr;
  281. int max_single;
  282. pr_debug("package xmit\n");
  283. if (ieee802154_hdr_peek(skb, &wpan_hdr) < 0) {
  284. kfree_skb(skb);
  285. return NET_XMIT_DROP;
  286. }
  287. max_single = ieee802154_max_payload(&wpan_hdr);
  288. if (skb_tail_pointer(skb) - skb_network_header(skb) <= max_single) {
  289. skb->dev = lowpan_dev_info(dev)->real_dev;
  290. return dev_queue_xmit(skb);
  291. } else {
  292. netdev_tx_t rc;
  293. pr_debug("frame is too big, fragmentation is needed\n");
  294. rc = lowpan_xmit_fragmented(skb, dev, &wpan_hdr);
  295. return rc < 0 ? NET_XMIT_DROP : rc;
  296. }
  297. }
  298. static struct wpan_phy *lowpan_get_phy(const struct net_device *dev)
  299. {
  300. struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
  301. return ieee802154_mlme_ops(real_dev)->get_phy(real_dev);
  302. }
  303. static __le16 lowpan_get_pan_id(const struct net_device *dev)
  304. {
  305. struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
  306. return ieee802154_mlme_ops(real_dev)->get_pan_id(real_dev);
  307. }
  308. static __le16 lowpan_get_short_addr(const struct net_device *dev)
  309. {
  310. struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
  311. return ieee802154_mlme_ops(real_dev)->get_short_addr(real_dev);
  312. }
  313. static u8 lowpan_get_dsn(const struct net_device *dev)
  314. {
  315. struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
  316. return ieee802154_mlme_ops(real_dev)->get_dsn(real_dev);
  317. }
  318. static struct header_ops lowpan_header_ops = {
  319. .create = lowpan_header_create,
  320. };
  321. static struct lock_class_key lowpan_tx_busylock;
  322. static struct lock_class_key lowpan_netdev_xmit_lock_key;
  323. static void lowpan_set_lockdep_class_one(struct net_device *dev,
  324. struct netdev_queue *txq,
  325. void *_unused)
  326. {
  327. lockdep_set_class(&txq->_xmit_lock,
  328. &lowpan_netdev_xmit_lock_key);
  329. }
  330. static int lowpan_dev_init(struct net_device *dev)
  331. {
  332. netdev_for_each_tx_queue(dev, lowpan_set_lockdep_class_one, NULL);
  333. dev->qdisc_tx_busylock = &lowpan_tx_busylock;
  334. return 0;
  335. }
  336. static const struct net_device_ops lowpan_netdev_ops = {
  337. .ndo_init = lowpan_dev_init,
  338. .ndo_start_xmit = lowpan_xmit,
  339. .ndo_set_mac_address = lowpan_set_address,
  340. };
  341. static struct ieee802154_mlme_ops lowpan_mlme = {
  342. .get_pan_id = lowpan_get_pan_id,
  343. .get_phy = lowpan_get_phy,
  344. .get_short_addr = lowpan_get_short_addr,
  345. .get_dsn = lowpan_get_dsn,
  346. };
  347. static void lowpan_setup(struct net_device *dev)
  348. {
  349. dev->addr_len = IEEE802154_ADDR_LEN;
  350. memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);
  351. dev->type = ARPHRD_IEEE802154;
  352. /* Frame Control + Sequence Number + Address fields + Security Header */
  353. dev->hard_header_len = 2 + 1 + 20 + 14;
  354. dev->needed_tailroom = 2; /* FCS */
  355. dev->mtu = IPV6_MIN_MTU;
  356. dev->tx_queue_len = 0;
  357. dev->flags = IFF_BROADCAST | IFF_MULTICAST;
  358. dev->watchdog_timeo = 0;
  359. dev->netdev_ops = &lowpan_netdev_ops;
  360. dev->header_ops = &lowpan_header_ops;
  361. dev->ml_priv = &lowpan_mlme;
  362. dev->destructor = free_netdev;
  363. }
  364. static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[])
  365. {
  366. if (tb[IFLA_ADDRESS]) {
  367. if (nla_len(tb[IFLA_ADDRESS]) != IEEE802154_ADDR_LEN)
  368. return -EINVAL;
  369. }
  370. return 0;
  371. }
  372. static int lowpan_rcv(struct sk_buff *skb, struct net_device *dev,
  373. struct packet_type *pt, struct net_device *orig_dev)
  374. {
  375. struct ieee802154_hdr hdr;
  376. int ret;
  377. skb = skb_share_check(skb, GFP_ATOMIC);
  378. if (!skb)
  379. goto drop;
  380. if (!netif_running(dev))
  381. goto drop_skb;
  382. if (dev->type != ARPHRD_IEEE802154)
  383. goto drop_skb;
  384. if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0)
  385. goto drop_skb;
  386. /* check that it's our buffer */
  387. if (skb->data[0] == LOWPAN_DISPATCH_IPV6) {
  388. skb->protocol = htons(ETH_P_IPV6);
  389. skb->pkt_type = PACKET_HOST;
  390. /* Pull off the 1-byte of 6lowpan header. */
  391. skb_pull(skb, 1);
  392. ret = lowpan_give_skb_to_devices(skb, NULL);
  393. if (ret == NET_RX_DROP)
  394. goto drop;
  395. } else {
  396. switch (skb->data[0] & 0xe0) {
  397. case LOWPAN_DISPATCH_IPHC: /* ipv6 datagram */
  398. ret = process_data(skb, &hdr);
  399. if (ret == NET_RX_DROP)
  400. goto drop;
  401. break;
  402. case LOWPAN_DISPATCH_FRAG1: /* first fragment header */
  403. ret = lowpan_frag_rcv(skb, LOWPAN_DISPATCH_FRAG1);
  404. if (ret == 1) {
  405. ret = process_data(skb, &hdr);
  406. if (ret == NET_RX_DROP)
  407. goto drop;
  408. }
  409. break;
  410. case LOWPAN_DISPATCH_FRAGN: /* next fragments headers */
  411. ret = lowpan_frag_rcv(skb, LOWPAN_DISPATCH_FRAGN);
  412. if (ret == 1) {
  413. ret = process_data(skb, &hdr);
  414. if (ret == NET_RX_DROP)
  415. goto drop;
  416. }
  417. break;
  418. default:
  419. break;
  420. }
  421. }
  422. return NET_RX_SUCCESS;
  423. drop_skb:
  424. kfree_skb(skb);
  425. drop:
  426. return NET_RX_DROP;
  427. }
  428. static int lowpan_newlink(struct net *src_net, struct net_device *dev,
  429. struct nlattr *tb[], struct nlattr *data[])
  430. {
  431. struct net_device *real_dev;
  432. struct lowpan_dev_record *entry;
  433. pr_debug("adding new link\n");
  434. if (!tb[IFLA_LINK])
  435. return -EINVAL;
  436. /* find and hold real wpan device */
  437. real_dev = dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
  438. if (!real_dev)
  439. return -ENODEV;
  440. if (real_dev->type != ARPHRD_IEEE802154) {
  441. dev_put(real_dev);
  442. return -EINVAL;
  443. }
  444. lowpan_dev_info(dev)->real_dev = real_dev;
  445. mutex_init(&lowpan_dev_info(dev)->dev_list_mtx);
  446. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  447. if (!entry) {
  448. dev_put(real_dev);
  449. lowpan_dev_info(dev)->real_dev = NULL;
  450. return -ENOMEM;
  451. }
  452. entry->ldev = dev;
  453. /* Set the lowpan harware address to the wpan hardware address. */
  454. memcpy(dev->dev_addr, real_dev->dev_addr, IEEE802154_ADDR_LEN);
  455. mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
  456. INIT_LIST_HEAD(&entry->list);
  457. list_add_tail(&entry->list, &lowpan_devices);
  458. mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
  459. register_netdevice(dev);
  460. return 0;
  461. }
  462. static void lowpan_dellink(struct net_device *dev, struct list_head *head)
  463. {
  464. struct lowpan_dev_info *lowpan_dev = lowpan_dev_info(dev);
  465. struct net_device *real_dev = lowpan_dev->real_dev;
  466. struct lowpan_dev_record *entry, *tmp;
  467. ASSERT_RTNL();
  468. mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
  469. list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) {
  470. if (entry->ldev == dev) {
  471. list_del(&entry->list);
  472. kfree(entry);
  473. }
  474. }
  475. mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
  476. mutex_destroy(&lowpan_dev_info(dev)->dev_list_mtx);
  477. unregister_netdevice_queue(dev, head);
  478. dev_put(real_dev);
  479. }
  480. static struct rtnl_link_ops lowpan_link_ops __read_mostly = {
  481. .kind = "lowpan",
  482. .priv_size = sizeof(struct lowpan_dev_info),
  483. .setup = lowpan_setup,
  484. .newlink = lowpan_newlink,
  485. .dellink = lowpan_dellink,
  486. .validate = lowpan_validate,
  487. };
  488. static inline int __init lowpan_netlink_init(void)
  489. {
  490. return rtnl_link_register(&lowpan_link_ops);
  491. }
  492. static inline void lowpan_netlink_fini(void)
  493. {
  494. rtnl_link_unregister(&lowpan_link_ops);
  495. }
  496. static int lowpan_device_event(struct notifier_block *unused,
  497. unsigned long event, void *ptr)
  498. {
  499. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  500. LIST_HEAD(del_list);
  501. struct lowpan_dev_record *entry, *tmp;
  502. if (dev->type != ARPHRD_IEEE802154)
  503. goto out;
  504. if (event == NETDEV_UNREGISTER) {
  505. list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) {
  506. if (lowpan_dev_info(entry->ldev)->real_dev == dev)
  507. lowpan_dellink(entry->ldev, &del_list);
  508. }
  509. unregister_netdevice_many(&del_list);
  510. }
  511. out:
  512. return NOTIFY_DONE;
  513. }
  514. static struct notifier_block lowpan_dev_notifier = {
  515. .notifier_call = lowpan_device_event,
  516. };
  517. static struct packet_type lowpan_packet_type = {
  518. .type = htons(ETH_P_IEEE802154),
  519. .func = lowpan_rcv,
  520. };
  521. static int __init lowpan_init_module(void)
  522. {
  523. int err = 0;
  524. err = lowpan_net_frag_init();
  525. if (err < 0)
  526. goto out;
  527. err = lowpan_netlink_init();
  528. if (err < 0)
  529. goto out_frag;
  530. dev_add_pack(&lowpan_packet_type);
  531. err = register_netdevice_notifier(&lowpan_dev_notifier);
  532. if (err < 0)
  533. goto out_pack;
  534. return 0;
  535. out_pack:
  536. dev_remove_pack(&lowpan_packet_type);
  537. lowpan_netlink_fini();
  538. out_frag:
  539. lowpan_net_frag_exit();
  540. out:
  541. return err;
  542. }
  543. static void __exit lowpan_cleanup_module(void)
  544. {
  545. lowpan_netlink_fini();
  546. dev_remove_pack(&lowpan_packet_type);
  547. lowpan_net_frag_exit();
  548. unregister_netdevice_notifier(&lowpan_dev_notifier);
  549. }
  550. module_init(lowpan_init_module);
  551. module_exit(lowpan_cleanup_module);
  552. MODULE_LICENSE("GPL");
  553. MODULE_ALIAS_RTNL_LINK("lowpan");