6lowpan_rtnl.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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,
  78. struct net_device *dev,
  79. unsigned short type, const void *_daddr,
  80. const void *_saddr, unsigned int len)
  81. {
  82. const u8 *saddr = _saddr;
  83. const u8 *daddr = _daddr;
  84. struct ieee802154_addr sa, da;
  85. struct ieee802154_mac_cb *cb = mac_cb_init(skb);
  86. /* TODO:
  87. * if this package isn't ipv6 one, where should it be routed?
  88. */
  89. if (type != ETH_P_IPV6)
  90. return 0;
  91. if (!saddr)
  92. saddr = dev->dev_addr;
  93. raw_dump_inline(__func__, "saddr", (unsigned char *)saddr, 8);
  94. raw_dump_inline(__func__, "daddr", (unsigned char *)daddr, 8);
  95. lowpan_header_compress(skb, dev, type, daddr, saddr, len);
  96. /* NOTE1: I'm still unsure about the fact that compression and WPAN
  97. * header are created here and not later in the xmit. So wait for
  98. * an opinion of net maintainers.
  99. */
  100. /* NOTE2: to be absolutely correct, we must derive PANid information
  101. * from MAC subif of the 'dev' and 'real_dev' network devices, but
  102. * this isn't implemented in mainline yet, so currently we assign 0xff
  103. */
  104. cb->type = IEEE802154_FC_TYPE_DATA;
  105. /* prepare wpan address data */
  106. sa.mode = IEEE802154_ADDR_LONG;
  107. sa.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
  108. sa.extended_addr = ieee802154_devaddr_from_raw(saddr);
  109. /* intra-PAN communications */
  110. da.pan_id = sa.pan_id;
  111. /* if the destination address is the broadcast address, use the
  112. * corresponding short address
  113. */
  114. if (lowpan_is_addr_broadcast(daddr)) {
  115. da.mode = IEEE802154_ADDR_SHORT;
  116. da.short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST);
  117. } else {
  118. da.mode = IEEE802154_ADDR_LONG;
  119. da.extended_addr = ieee802154_devaddr_from_raw(daddr);
  120. }
  121. cb->ackreq = !lowpan_is_addr_broadcast(daddr);
  122. return dev_hard_header(skb, lowpan_dev_info(dev)->real_dev,
  123. type, (void *)&da, (void *)&sa, 0);
  124. }
  125. static int lowpan_give_skb_to_devices(struct sk_buff *skb,
  126. struct net_device *dev)
  127. {
  128. struct lowpan_dev_record *entry;
  129. struct sk_buff *skb_cp;
  130. int stat = NET_RX_SUCCESS;
  131. rcu_read_lock();
  132. list_for_each_entry_rcu(entry, &lowpan_devices, list)
  133. if (lowpan_dev_info(entry->ldev)->real_dev == skb->dev) {
  134. skb_cp = skb_copy(skb, GFP_ATOMIC);
  135. if (!skb_cp) {
  136. stat = -ENOMEM;
  137. break;
  138. }
  139. skb_cp->dev = entry->ldev;
  140. stat = netif_rx(skb_cp);
  141. }
  142. rcu_read_unlock();
  143. return stat;
  144. }
  145. static int process_data(struct sk_buff *skb, const struct ieee802154_hdr *hdr)
  146. {
  147. u8 iphc0, iphc1;
  148. struct ieee802154_addr_sa sa, da;
  149. void *sap, *dap;
  150. raw_dump_table(__func__, "raw skb data dump", skb->data, skb->len);
  151. /* at least two bytes will be used for the encoding */
  152. if (skb->len < 2)
  153. goto drop;
  154. if (lowpan_fetch_skb_u8(skb, &iphc0))
  155. goto drop;
  156. if (lowpan_fetch_skb_u8(skb, &iphc1))
  157. goto drop;
  158. ieee802154_addr_to_sa(&sa, &hdr->source);
  159. ieee802154_addr_to_sa(&da, &hdr->dest);
  160. if (sa.addr_type == IEEE802154_ADDR_SHORT)
  161. sap = &sa.short_addr;
  162. else
  163. sap = &sa.hwaddr;
  164. if (da.addr_type == IEEE802154_ADDR_SHORT)
  165. dap = &da.short_addr;
  166. else
  167. dap = &da.hwaddr;
  168. return lowpan_process_data(skb, skb->dev, sap, sa.addr_type,
  169. IEEE802154_ADDR_LEN, dap, da.addr_type,
  170. IEEE802154_ADDR_LEN, iphc0, iphc1,
  171. lowpan_give_skb_to_devices);
  172. drop:
  173. kfree_skb(skb);
  174. return -EINVAL;
  175. }
  176. static int lowpan_set_address(struct net_device *dev, void *p)
  177. {
  178. struct sockaddr *sa = p;
  179. if (netif_running(dev))
  180. return -EBUSY;
  181. /* TODO: validate addr */
  182. memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
  183. return 0;
  184. }
  185. static struct sk_buff*
  186. lowpan_alloc_frag(struct sk_buff *skb, int size,
  187. const struct ieee802154_hdr *master_hdr)
  188. {
  189. struct net_device *real_dev = lowpan_dev_info(skb->dev)->real_dev;
  190. struct sk_buff *frag;
  191. int rc;
  192. frag = alloc_skb(real_dev->hard_header_len +
  193. real_dev->needed_tailroom + size,
  194. GFP_ATOMIC);
  195. if (likely(frag)) {
  196. frag->dev = real_dev;
  197. frag->priority = skb->priority;
  198. skb_reserve(frag, real_dev->hard_header_len);
  199. skb_reset_network_header(frag);
  200. *mac_cb(frag) = *mac_cb(skb);
  201. rc = dev_hard_header(frag, real_dev, 0, &master_hdr->dest,
  202. &master_hdr->source, size);
  203. if (rc < 0) {
  204. kfree_skb(frag);
  205. return ERR_PTR(-rc);
  206. }
  207. } else {
  208. frag = ERR_PTR(ENOMEM);
  209. }
  210. return frag;
  211. }
  212. static int
  213. lowpan_xmit_fragment(struct sk_buff *skb, const struct ieee802154_hdr *wpan_hdr,
  214. u8 *frag_hdr, int frag_hdrlen,
  215. int offset, int len)
  216. {
  217. struct sk_buff *frag;
  218. raw_dump_inline(__func__, " fragment header", frag_hdr, frag_hdrlen);
  219. frag = lowpan_alloc_frag(skb, frag_hdrlen + len, wpan_hdr);
  220. if (IS_ERR(frag))
  221. return -PTR_ERR(frag);
  222. memcpy(skb_put(frag, frag_hdrlen), frag_hdr, frag_hdrlen);
  223. memcpy(skb_put(frag, len), skb_network_header(skb) + offset, len);
  224. raw_dump_table(__func__, " fragment dump", frag->data, frag->len);
  225. return dev_queue_xmit(frag);
  226. }
  227. static int
  228. lowpan_xmit_fragmented(struct sk_buff *skb, struct net_device *dev,
  229. const struct ieee802154_hdr *wpan_hdr)
  230. {
  231. u16 dgram_size, dgram_offset;
  232. __be16 frag_tag;
  233. u8 frag_hdr[5];
  234. int frag_cap, frag_len, payload_cap, rc;
  235. int skb_unprocessed, skb_offset;
  236. dgram_size = lowpan_uncompress_size(skb, &dgram_offset) -
  237. skb->mac_len;
  238. frag_tag = lowpan_dev_info(dev)->fragment_tag++;
  239. frag_hdr[0] = LOWPAN_DISPATCH_FRAG1 | ((dgram_size >> 8) & 0x07);
  240. frag_hdr[1] = dgram_size & 0xff;
  241. memcpy(frag_hdr + 2, &frag_tag, sizeof(frag_tag));
  242. payload_cap = ieee802154_max_payload(wpan_hdr);
  243. frag_len = round_down(payload_cap - LOWPAN_FRAG1_HEAD_SIZE -
  244. skb_network_header_len(skb), 8);
  245. skb_offset = skb_network_header_len(skb);
  246. skb_unprocessed = skb->len - skb->mac_len - skb_offset;
  247. rc = lowpan_xmit_fragment(skb, wpan_hdr, frag_hdr,
  248. LOWPAN_FRAG1_HEAD_SIZE, 0,
  249. frag_len + skb_network_header_len(skb));
  250. if (rc) {
  251. pr_debug("%s unable to send FRAG1 packet (tag: %d)",
  252. __func__, frag_tag);
  253. goto err;
  254. }
  255. frag_hdr[0] &= ~LOWPAN_DISPATCH_FRAG1;
  256. frag_hdr[0] |= LOWPAN_DISPATCH_FRAGN;
  257. frag_cap = round_down(payload_cap - LOWPAN_FRAGN_HEAD_SIZE, 8);
  258. do {
  259. dgram_offset += frag_len;
  260. skb_offset += frag_len;
  261. skb_unprocessed -= frag_len;
  262. frag_len = min(frag_cap, skb_unprocessed);
  263. frag_hdr[4] = dgram_offset >> 3;
  264. rc = lowpan_xmit_fragment(skb, wpan_hdr, frag_hdr,
  265. LOWPAN_FRAGN_HEAD_SIZE, skb_offset,
  266. frag_len);
  267. if (rc) {
  268. pr_debug("%s unable to send a FRAGN packet. (tag: %d, offset: %d)\n",
  269. __func__, frag_tag, skb_offset);
  270. goto err;
  271. }
  272. } while (skb_unprocessed > frag_cap);
  273. consume_skb(skb);
  274. return NET_XMIT_SUCCESS;
  275. err:
  276. kfree_skb(skb);
  277. return rc;
  278. }
  279. static netdev_tx_t lowpan_xmit(struct sk_buff *skb, struct net_device *dev)
  280. {
  281. struct ieee802154_hdr wpan_hdr;
  282. int max_single;
  283. pr_debug("package xmit\n");
  284. if (ieee802154_hdr_peek(skb, &wpan_hdr) < 0) {
  285. kfree_skb(skb);
  286. return NET_XMIT_DROP;
  287. }
  288. max_single = ieee802154_max_payload(&wpan_hdr);
  289. if (skb_tail_pointer(skb) - skb_network_header(skb) <= max_single) {
  290. skb->dev = lowpan_dev_info(dev)->real_dev;
  291. return dev_queue_xmit(skb);
  292. } else {
  293. netdev_tx_t rc;
  294. pr_debug("frame is too big, fragmentation is needed\n");
  295. rc = lowpan_xmit_fragmented(skb, dev, &wpan_hdr);
  296. return rc < 0 ? NET_XMIT_DROP : rc;
  297. }
  298. }
  299. static struct wpan_phy *lowpan_get_phy(const struct net_device *dev)
  300. {
  301. struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
  302. return ieee802154_mlme_ops(real_dev)->get_phy(real_dev);
  303. }
  304. static __le16 lowpan_get_pan_id(const struct net_device *dev)
  305. {
  306. struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
  307. return ieee802154_mlme_ops(real_dev)->get_pan_id(real_dev);
  308. }
  309. static __le16 lowpan_get_short_addr(const struct net_device *dev)
  310. {
  311. struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
  312. return ieee802154_mlme_ops(real_dev)->get_short_addr(real_dev);
  313. }
  314. static u8 lowpan_get_dsn(const struct net_device *dev)
  315. {
  316. struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
  317. return ieee802154_mlme_ops(real_dev)->get_dsn(real_dev);
  318. }
  319. static struct header_ops lowpan_header_ops = {
  320. .create = lowpan_header_create,
  321. };
  322. static struct lock_class_key lowpan_tx_busylock;
  323. static struct lock_class_key lowpan_netdev_xmit_lock_key;
  324. static void lowpan_set_lockdep_class_one(struct net_device *dev,
  325. struct netdev_queue *txq,
  326. void *_unused)
  327. {
  328. lockdep_set_class(&txq->_xmit_lock,
  329. &lowpan_netdev_xmit_lock_key);
  330. }
  331. static int lowpan_dev_init(struct net_device *dev)
  332. {
  333. netdev_for_each_tx_queue(dev, lowpan_set_lockdep_class_one, NULL);
  334. dev->qdisc_tx_busylock = &lowpan_tx_busylock;
  335. return 0;
  336. }
  337. static const struct net_device_ops lowpan_netdev_ops = {
  338. .ndo_init = lowpan_dev_init,
  339. .ndo_start_xmit = lowpan_xmit,
  340. .ndo_set_mac_address = lowpan_set_address,
  341. };
  342. static struct ieee802154_mlme_ops lowpan_mlme = {
  343. .get_pan_id = lowpan_get_pan_id,
  344. .get_phy = lowpan_get_phy,
  345. .get_short_addr = lowpan_get_short_addr,
  346. .get_dsn = lowpan_get_dsn,
  347. };
  348. static void lowpan_setup(struct net_device *dev)
  349. {
  350. dev->addr_len = IEEE802154_ADDR_LEN;
  351. memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);
  352. dev->type = ARPHRD_IEEE802154;
  353. /* Frame Control + Sequence Number + Address fields + Security Header */
  354. dev->hard_header_len = 2 + 1 + 20 + 14;
  355. dev->needed_tailroom = 2; /* FCS */
  356. dev->mtu = 1281;
  357. dev->tx_queue_len = 0;
  358. dev->flags = IFF_BROADCAST | IFF_MULTICAST;
  359. dev->watchdog_timeo = 0;
  360. dev->netdev_ops = &lowpan_netdev_ops;
  361. dev->header_ops = &lowpan_header_ops;
  362. dev->ml_priv = &lowpan_mlme;
  363. dev->destructor = free_netdev;
  364. }
  365. static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[])
  366. {
  367. if (tb[IFLA_ADDRESS]) {
  368. if (nla_len(tb[IFLA_ADDRESS]) != IEEE802154_ADDR_LEN)
  369. return -EINVAL;
  370. }
  371. return 0;
  372. }
  373. static int lowpan_rcv(struct sk_buff *skb, struct net_device *dev,
  374. struct packet_type *pt, struct net_device *orig_dev)
  375. {
  376. struct ieee802154_hdr hdr;
  377. int ret;
  378. skb = skb_share_check(skb, GFP_ATOMIC);
  379. if (!skb)
  380. goto drop;
  381. if (!netif_running(dev))
  382. goto drop_skb;
  383. if (dev->type != ARPHRD_IEEE802154)
  384. goto drop_skb;
  385. if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0)
  386. goto drop_skb;
  387. /* check that it's our buffer */
  388. if (skb->data[0] == LOWPAN_DISPATCH_IPV6) {
  389. skb->protocol = htons(ETH_P_IPV6);
  390. skb->pkt_type = PACKET_HOST;
  391. /* Pull off the 1-byte of 6lowpan header. */
  392. skb_pull(skb, 1);
  393. ret = lowpan_give_skb_to_devices(skb, NULL);
  394. if (ret == NET_RX_DROP)
  395. goto drop;
  396. } else {
  397. switch (skb->data[0] & 0xe0) {
  398. case LOWPAN_DISPATCH_IPHC: /* ipv6 datagram */
  399. ret = process_data(skb, &hdr);
  400. if (ret == NET_RX_DROP)
  401. goto drop;
  402. break;
  403. case LOWPAN_DISPATCH_FRAG1: /* first fragment header */
  404. ret = lowpan_frag_rcv(skb, LOWPAN_DISPATCH_FRAG1);
  405. if (ret == 1) {
  406. ret = process_data(skb, &hdr);
  407. if (ret == NET_RX_DROP)
  408. goto drop;
  409. }
  410. break;
  411. case LOWPAN_DISPATCH_FRAGN: /* next fragments headers */
  412. ret = lowpan_frag_rcv(skb, LOWPAN_DISPATCH_FRAGN);
  413. if (ret == 1) {
  414. ret = process_data(skb, &hdr);
  415. if (ret == NET_RX_DROP)
  416. goto drop;
  417. }
  418. break;
  419. default:
  420. break;
  421. }
  422. }
  423. return NET_RX_SUCCESS;
  424. drop_skb:
  425. kfree_skb(skb);
  426. drop:
  427. return NET_RX_DROP;
  428. }
  429. static int lowpan_newlink(struct net *src_net, struct net_device *dev,
  430. struct nlattr *tb[], struct nlattr *data[])
  431. {
  432. struct net_device *real_dev;
  433. struct lowpan_dev_record *entry;
  434. pr_debug("adding new link\n");
  435. if (!tb[IFLA_LINK])
  436. return -EINVAL;
  437. /* find and hold real wpan device */
  438. real_dev = dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
  439. if (!real_dev)
  440. return -ENODEV;
  441. if (real_dev->type != ARPHRD_IEEE802154) {
  442. dev_put(real_dev);
  443. return -EINVAL;
  444. }
  445. lowpan_dev_info(dev)->real_dev = real_dev;
  446. mutex_init(&lowpan_dev_info(dev)->dev_list_mtx);
  447. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  448. if (!entry) {
  449. dev_put(real_dev);
  450. lowpan_dev_info(dev)->real_dev = NULL;
  451. return -ENOMEM;
  452. }
  453. entry->ldev = dev;
  454. /* Set the lowpan harware address to the wpan hardware address. */
  455. memcpy(dev->dev_addr, real_dev->dev_addr, IEEE802154_ADDR_LEN);
  456. mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
  457. INIT_LIST_HEAD(&entry->list);
  458. list_add_tail(&entry->list, &lowpan_devices);
  459. mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
  460. register_netdevice(dev);
  461. return 0;
  462. }
  463. static void lowpan_dellink(struct net_device *dev, struct list_head *head)
  464. {
  465. struct lowpan_dev_info *lowpan_dev = lowpan_dev_info(dev);
  466. struct net_device *real_dev = lowpan_dev->real_dev;
  467. struct lowpan_dev_record *entry, *tmp;
  468. ASSERT_RTNL();
  469. mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
  470. list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) {
  471. if (entry->ldev == dev) {
  472. list_del(&entry->list);
  473. kfree(entry);
  474. }
  475. }
  476. mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
  477. mutex_destroy(&lowpan_dev_info(dev)->dev_list_mtx);
  478. unregister_netdevice_queue(dev, head);
  479. dev_put(real_dev);
  480. }
  481. static struct rtnl_link_ops lowpan_link_ops __read_mostly = {
  482. .kind = "lowpan",
  483. .priv_size = sizeof(struct lowpan_dev_info),
  484. .setup = lowpan_setup,
  485. .newlink = lowpan_newlink,
  486. .dellink = lowpan_dellink,
  487. .validate = lowpan_validate,
  488. };
  489. static inline int __init lowpan_netlink_init(void)
  490. {
  491. return rtnl_link_register(&lowpan_link_ops);
  492. }
  493. static inline void lowpan_netlink_fini(void)
  494. {
  495. rtnl_link_unregister(&lowpan_link_ops);
  496. }
  497. static int lowpan_device_event(struct notifier_block *unused,
  498. unsigned long event, void *ptr)
  499. {
  500. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  501. LIST_HEAD(del_list);
  502. struct lowpan_dev_record *entry, *tmp;
  503. if (dev->type != ARPHRD_IEEE802154)
  504. goto out;
  505. if (event == NETDEV_UNREGISTER) {
  506. list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) {
  507. if (lowpan_dev_info(entry->ldev)->real_dev == dev)
  508. lowpan_dellink(entry->ldev, &del_list);
  509. }
  510. unregister_netdevice_many(&del_list);
  511. }
  512. out:
  513. return NOTIFY_DONE;
  514. }
  515. static struct notifier_block lowpan_dev_notifier = {
  516. .notifier_call = lowpan_device_event,
  517. };
  518. static struct packet_type lowpan_packet_type = {
  519. .type = htons(ETH_P_IEEE802154),
  520. .func = lowpan_rcv,
  521. };
  522. static int __init lowpan_init_module(void)
  523. {
  524. int err = 0;
  525. err = lowpan_net_frag_init();
  526. if (err < 0)
  527. goto out;
  528. err = lowpan_netlink_init();
  529. if (err < 0)
  530. goto out_frag;
  531. dev_add_pack(&lowpan_packet_type);
  532. err = register_netdevice_notifier(&lowpan_dev_notifier);
  533. if (err < 0)
  534. goto out_pack;
  535. return 0;
  536. out_pack:
  537. dev_remove_pack(&lowpan_packet_type);
  538. lowpan_netlink_fini();
  539. out_frag:
  540. lowpan_net_frag_exit();
  541. out:
  542. return err;
  543. }
  544. static void __exit lowpan_cleanup_module(void)
  545. {
  546. lowpan_netlink_fini();
  547. dev_remove_pack(&lowpan_packet_type);
  548. lowpan_net_frag_exit();
  549. unregister_netdevice_notifier(&lowpan_dev_notifier);
  550. }
  551. module_init(lowpan_init_module);
  552. module_exit(lowpan_cleanup_module);
  553. MODULE_LICENSE("GPL");
  554. MODULE_ALIAS_RTNL_LINK("lowpan");