6lowpan.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. /*
  2. * Copyright 2011, Siemens AG
  3. * written by Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
  4. */
  5. /*
  6. * Based on patches from Jon Smirl <jonsmirl@gmail.com>
  7. * Copyright (c) 2011 Jon Smirl <jonsmirl@gmail.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. /* Jon's code is based on 6lowpan implementation for Contiki which is:
  23. * Copyright (c) 2008, Swedish Institute of Computer Science.
  24. * All rights reserved.
  25. *
  26. * Redistribution and use in source and binary forms, with or without
  27. * modification, are permitted provided that the following conditions
  28. * are met:
  29. * 1. Redistributions of source code must retain the above copyright
  30. * notice, this list of conditions and the following disclaimer.
  31. * 2. Redistributions in binary form must reproduce the above copyright
  32. * notice, this list of conditions and the following disclaimer in the
  33. * documentation and/or other materials provided with the distribution.
  34. * 3. Neither the name of the Institute nor the names of its contributors
  35. * may be used to endorse or promote products derived from this software
  36. * without specific prior written permission.
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  39. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  41. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  42. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  43. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  44. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  45. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  46. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  47. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  48. * SUCH DAMAGE.
  49. */
  50. #include <linux/bitops.h>
  51. #include <linux/if_arp.h>
  52. #include <linux/module.h>
  53. #include <linux/moduleparam.h>
  54. #include <linux/netdevice.h>
  55. #include <net/af_ieee802154.h>
  56. #include <net/ieee802154.h>
  57. #include <net/ieee802154_netdev.h>
  58. #include <net/ipv6.h>
  59. #include "6lowpan.h"
  60. static LIST_HEAD(lowpan_devices);
  61. /* private device info */
  62. struct lowpan_dev_info {
  63. struct net_device *real_dev; /* real WPAN device ptr */
  64. struct mutex dev_list_mtx; /* mutex for list ops */
  65. unsigned short fragment_tag;
  66. };
  67. struct lowpan_dev_record {
  68. struct net_device *ldev;
  69. struct list_head list;
  70. };
  71. struct lowpan_fragment {
  72. struct sk_buff *skb; /* skb to be assembled */
  73. u16 length; /* length to be assemled */
  74. u32 bytes_rcv; /* bytes received */
  75. u16 tag; /* current fragment tag */
  76. struct timer_list timer; /* assembling timer */
  77. struct list_head list; /* fragments list */
  78. };
  79. static LIST_HEAD(lowpan_fragments);
  80. static DEFINE_SPINLOCK(flist_lock);
  81. static inline struct
  82. lowpan_dev_info *lowpan_dev_info(const struct net_device *dev)
  83. {
  84. return netdev_priv(dev);
  85. }
  86. static inline void lowpan_address_flip(u8 *src, u8 *dest)
  87. {
  88. int i;
  89. for (i = 0; i < IEEE802154_ADDR_LEN; i++)
  90. (dest)[IEEE802154_ADDR_LEN - i - 1] = (src)[i];
  91. }
  92. static int lowpan_header_create(struct sk_buff *skb,
  93. struct net_device *dev,
  94. unsigned short type, const void *_daddr,
  95. const void *_saddr, unsigned int len)
  96. {
  97. struct ipv6hdr *hdr;
  98. const u8 *saddr = _saddr;
  99. const u8 *daddr = _daddr;
  100. struct ieee802154_addr sa, da;
  101. /* TODO:
  102. * if this package isn't ipv6 one, where should it be routed?
  103. */
  104. if (type != ETH_P_IPV6)
  105. return 0;
  106. hdr = ipv6_hdr(skb);
  107. if (!saddr)
  108. saddr = dev->dev_addr;
  109. raw_dump_inline(__func__, "saddr", (unsigned char *)saddr, 8);
  110. raw_dump_inline(__func__, "daddr", (unsigned char *)daddr, 8);
  111. lowpan_header_compress(skb, dev, type, daddr, saddr, len);
  112. /*
  113. * NOTE1: I'm still unsure about the fact that compression and WPAN
  114. * header are created here and not later in the xmit. So wait for
  115. * an opinion of net maintainers.
  116. */
  117. /*
  118. * NOTE2: to be absolutely correct, we must derive PANid information
  119. * from MAC subif of the 'dev' and 'real_dev' network devices, but
  120. * this isn't implemented in mainline yet, so currently we assign 0xff
  121. */
  122. mac_cb(skb)->flags = IEEE802154_FC_TYPE_DATA;
  123. mac_cb(skb)->seq = ieee802154_mlme_ops(dev)->get_dsn(dev);
  124. /* prepare wpan address data */
  125. sa.addr_type = IEEE802154_ADDR_LONG;
  126. sa.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
  127. memcpy(&(sa.hwaddr), saddr, 8);
  128. /* intra-PAN communications */
  129. da.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
  130. /*
  131. * if the destination address is the broadcast address, use the
  132. * corresponding short address
  133. */
  134. if (lowpan_is_addr_broadcast(daddr)) {
  135. da.addr_type = IEEE802154_ADDR_SHORT;
  136. da.short_addr = IEEE802154_ADDR_BROADCAST;
  137. } else {
  138. da.addr_type = IEEE802154_ADDR_LONG;
  139. memcpy(&(da.hwaddr), daddr, IEEE802154_ADDR_LEN);
  140. /* request acknowledgment */
  141. mac_cb(skb)->flags |= MAC_CB_FLAG_ACKREQ;
  142. }
  143. return dev_hard_header(skb, lowpan_dev_info(dev)->real_dev,
  144. type, (void *)&da, (void *)&sa, skb->len);
  145. }
  146. static int lowpan_give_skb_to_devices(struct sk_buff *skb,
  147. struct net_device *dev)
  148. {
  149. struct lowpan_dev_record *entry;
  150. struct sk_buff *skb_cp;
  151. int stat = NET_RX_SUCCESS;
  152. rcu_read_lock();
  153. list_for_each_entry_rcu(entry, &lowpan_devices, list)
  154. if (lowpan_dev_info(entry->ldev)->real_dev == skb->dev) {
  155. skb_cp = skb_copy(skb, GFP_ATOMIC);
  156. if (!skb_cp) {
  157. stat = -ENOMEM;
  158. break;
  159. }
  160. skb_cp->dev = entry->ldev;
  161. stat = netif_rx(skb_cp);
  162. }
  163. rcu_read_unlock();
  164. return stat;
  165. }
  166. static void lowpan_fragment_timer_expired(unsigned long entry_addr)
  167. {
  168. struct lowpan_fragment *entry = (struct lowpan_fragment *)entry_addr;
  169. pr_debug("timer expired for frame with tag %d\n", entry->tag);
  170. list_del(&entry->list);
  171. dev_kfree_skb(entry->skb);
  172. kfree(entry);
  173. }
  174. static struct lowpan_fragment *
  175. lowpan_alloc_new_frame(struct sk_buff *skb, u16 len, u16 tag)
  176. {
  177. struct lowpan_fragment *frame;
  178. frame = kzalloc(sizeof(struct lowpan_fragment),
  179. GFP_ATOMIC);
  180. if (!frame)
  181. goto frame_err;
  182. INIT_LIST_HEAD(&frame->list);
  183. frame->length = len;
  184. frame->tag = tag;
  185. /* allocate buffer for frame assembling */
  186. frame->skb = netdev_alloc_skb_ip_align(skb->dev, frame->length +
  187. sizeof(struct ipv6hdr));
  188. if (!frame->skb)
  189. goto skb_err;
  190. frame->skb->priority = skb->priority;
  191. /* reserve headroom for uncompressed ipv6 header */
  192. skb_reserve(frame->skb, sizeof(struct ipv6hdr));
  193. skb_put(frame->skb, frame->length);
  194. /* copy the first control block to keep a
  195. * trace of the link-layer addresses in case
  196. * of a link-local compressed address
  197. */
  198. memcpy(frame->skb->cb, skb->cb, sizeof(skb->cb));
  199. init_timer(&frame->timer);
  200. /* time out is the same as for ipv6 - 60 sec */
  201. frame->timer.expires = jiffies + LOWPAN_FRAG_TIMEOUT;
  202. frame->timer.data = (unsigned long)frame;
  203. frame->timer.function = lowpan_fragment_timer_expired;
  204. add_timer(&frame->timer);
  205. list_add_tail(&frame->list, &lowpan_fragments);
  206. return frame;
  207. skb_err:
  208. kfree(frame);
  209. frame_err:
  210. return NULL;
  211. }
  212. static int process_data(struct sk_buff *skb)
  213. {
  214. u8 iphc0, iphc1;
  215. const struct ieee802154_addr *_saddr, *_daddr;
  216. raw_dump_table(__func__, "raw skb data dump", skb->data, skb->len);
  217. /* at least two bytes will be used for the encoding */
  218. if (skb->len < 2)
  219. goto drop;
  220. if (lowpan_fetch_skb_u8(skb, &iphc0))
  221. goto drop;
  222. /* fragments assembling */
  223. switch (iphc0 & LOWPAN_DISPATCH_MASK) {
  224. case LOWPAN_DISPATCH_FRAG1:
  225. case LOWPAN_DISPATCH_FRAGN:
  226. {
  227. struct lowpan_fragment *frame;
  228. /* slen stores the rightmost 8 bits of the 11 bits length */
  229. u8 slen, offset = 0;
  230. u16 len, tag;
  231. bool found = false;
  232. if (lowpan_fetch_skb_u8(skb, &slen) || /* frame length */
  233. lowpan_fetch_skb_u16(skb, &tag)) /* fragment tag */
  234. goto drop;
  235. /* adds the 3 MSB to the 8 LSB to retrieve the 11 bits length */
  236. len = ((iphc0 & 7) << 8) | slen;
  237. if ((iphc0 & LOWPAN_DISPATCH_MASK) == LOWPAN_DISPATCH_FRAG1) {
  238. pr_debug("%s received a FRAG1 packet (tag: %d, "
  239. "size of the entire IP packet: %d)",
  240. __func__, tag, len);
  241. } else { /* FRAGN */
  242. if (lowpan_fetch_skb_u8(skb, &offset))
  243. goto unlock_and_drop;
  244. pr_debug("%s received a FRAGN packet (tag: %d, "
  245. "size of the entire IP packet: %d, "
  246. "offset: %d)", __func__, tag, len, offset * 8);
  247. }
  248. /*
  249. * check if frame assembling with the same tag is
  250. * already in progress
  251. */
  252. spin_lock_bh(&flist_lock);
  253. list_for_each_entry(frame, &lowpan_fragments, list)
  254. if (frame->tag == tag) {
  255. found = true;
  256. break;
  257. }
  258. /* alloc new frame structure */
  259. if (!found) {
  260. pr_debug("%s first fragment received for tag %d, "
  261. "begin packet reassembly", __func__, tag);
  262. frame = lowpan_alloc_new_frame(skb, len, tag);
  263. if (!frame)
  264. goto unlock_and_drop;
  265. }
  266. /* if payload fits buffer, copy it */
  267. if (likely((offset * 8 + skb->len) <= frame->length))
  268. skb_copy_to_linear_data_offset(frame->skb, offset * 8,
  269. skb->data, skb->len);
  270. else
  271. goto unlock_and_drop;
  272. frame->bytes_rcv += skb->len;
  273. /* frame assembling complete */
  274. if ((frame->bytes_rcv == frame->length) &&
  275. frame->timer.expires > jiffies) {
  276. /* if timer haven't expired - first of all delete it */
  277. del_timer_sync(&frame->timer);
  278. list_del(&frame->list);
  279. spin_unlock_bh(&flist_lock);
  280. pr_debug("%s successfully reassembled fragment "
  281. "(tag %d)", __func__, tag);
  282. dev_kfree_skb(skb);
  283. skb = frame->skb;
  284. kfree(frame);
  285. if (lowpan_fetch_skb_u8(skb, &iphc0))
  286. goto drop;
  287. break;
  288. }
  289. spin_unlock_bh(&flist_lock);
  290. return kfree_skb(skb), 0;
  291. }
  292. default:
  293. break;
  294. }
  295. if (lowpan_fetch_skb_u8(skb, &iphc1))
  296. goto drop;
  297. _saddr = &mac_cb(skb)->sa;
  298. _daddr = &mac_cb(skb)->da;
  299. return lowpan_process_data(skb, skb->dev, (u8 *)_saddr->hwaddr,
  300. _saddr->addr_type, IEEE802154_ADDR_LEN,
  301. (u8 *)_daddr->hwaddr, _daddr->addr_type,
  302. IEEE802154_ADDR_LEN, iphc0, iphc1,
  303. lowpan_give_skb_to_devices);
  304. unlock_and_drop:
  305. spin_unlock_bh(&flist_lock);
  306. drop:
  307. kfree_skb(skb);
  308. return -EINVAL;
  309. }
  310. static int lowpan_set_address(struct net_device *dev, void *p)
  311. {
  312. struct sockaddr *sa = p;
  313. if (netif_running(dev))
  314. return -EBUSY;
  315. /* TODO: validate addr */
  316. memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
  317. return 0;
  318. }
  319. static int
  320. lowpan_fragment_xmit(struct sk_buff *skb, u8 *head,
  321. int mlen, int plen, int offset, int type)
  322. {
  323. struct sk_buff *frag;
  324. int hlen;
  325. hlen = (type == LOWPAN_DISPATCH_FRAG1) ?
  326. LOWPAN_FRAG1_HEAD_SIZE : LOWPAN_FRAGN_HEAD_SIZE;
  327. raw_dump_inline(__func__, "6lowpan fragment header", head, hlen);
  328. frag = netdev_alloc_skb(skb->dev,
  329. hlen + mlen + plen + IEEE802154_MFR_SIZE);
  330. if (!frag)
  331. return -ENOMEM;
  332. frag->priority = skb->priority;
  333. /* copy header, MFR and payload */
  334. skb_put(frag, mlen);
  335. skb_copy_to_linear_data(frag, skb_mac_header(skb), mlen);
  336. skb_put(frag, hlen);
  337. skb_copy_to_linear_data_offset(frag, mlen, head, hlen);
  338. skb_put(frag, plen);
  339. skb_copy_to_linear_data_offset(frag, mlen + hlen,
  340. skb_network_header(skb) + offset, plen);
  341. raw_dump_table(__func__, " raw fragment dump", frag->data, frag->len);
  342. return dev_queue_xmit(frag);
  343. }
  344. static int
  345. lowpan_skb_fragmentation(struct sk_buff *skb, struct net_device *dev)
  346. {
  347. int err, header_length, payload_length, tag, offset = 0;
  348. u8 head[5];
  349. header_length = skb->mac_len;
  350. payload_length = skb->len - header_length;
  351. tag = lowpan_dev_info(dev)->fragment_tag++;
  352. /* first fragment header */
  353. head[0] = LOWPAN_DISPATCH_FRAG1 | ((payload_length >> 8) & 0x7);
  354. head[1] = payload_length & 0xff;
  355. head[2] = tag >> 8;
  356. head[3] = tag & 0xff;
  357. err = lowpan_fragment_xmit(skb, head, header_length, LOWPAN_FRAG_SIZE,
  358. 0, LOWPAN_DISPATCH_FRAG1);
  359. if (err) {
  360. pr_debug("%s unable to send FRAG1 packet (tag: %d)",
  361. __func__, tag);
  362. goto exit;
  363. }
  364. offset = LOWPAN_FRAG_SIZE;
  365. /* next fragment header */
  366. head[0] &= ~LOWPAN_DISPATCH_FRAG1;
  367. head[0] |= LOWPAN_DISPATCH_FRAGN;
  368. while (payload_length - offset > 0) {
  369. int len = LOWPAN_FRAG_SIZE;
  370. head[4] = offset / 8;
  371. if (payload_length - offset < len)
  372. len = payload_length - offset;
  373. err = lowpan_fragment_xmit(skb, head, header_length,
  374. len, offset, LOWPAN_DISPATCH_FRAGN);
  375. if (err) {
  376. pr_debug("%s unable to send a subsequent FRAGN packet "
  377. "(tag: %d, offset: %d", __func__, tag, offset);
  378. goto exit;
  379. }
  380. offset += len;
  381. }
  382. exit:
  383. return err;
  384. }
  385. static netdev_tx_t lowpan_xmit(struct sk_buff *skb, struct net_device *dev)
  386. {
  387. int err = -1;
  388. pr_debug("package xmit\n");
  389. skb->dev = lowpan_dev_info(dev)->real_dev;
  390. if (skb->dev == NULL) {
  391. pr_debug("ERROR: no real wpan device found\n");
  392. goto error;
  393. }
  394. /* Send directly if less than the MTU minus the 2 checksum bytes. */
  395. if (skb->len <= IEEE802154_MTU - IEEE802154_MFR_SIZE) {
  396. err = dev_queue_xmit(skb);
  397. goto out;
  398. }
  399. pr_debug("frame is too big, fragmentation is needed\n");
  400. err = lowpan_skb_fragmentation(skb, dev);
  401. error:
  402. dev_kfree_skb(skb);
  403. out:
  404. if (err)
  405. pr_debug("ERROR: xmit failed\n");
  406. return (err < 0) ? NET_XMIT_DROP : err;
  407. }
  408. static struct wpan_phy *lowpan_get_phy(const struct net_device *dev)
  409. {
  410. struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
  411. return ieee802154_mlme_ops(real_dev)->get_phy(real_dev);
  412. }
  413. static u16 lowpan_get_pan_id(const struct net_device *dev)
  414. {
  415. struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
  416. return ieee802154_mlme_ops(real_dev)->get_pan_id(real_dev);
  417. }
  418. static u16 lowpan_get_short_addr(const struct net_device *dev)
  419. {
  420. struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
  421. return ieee802154_mlme_ops(real_dev)->get_short_addr(real_dev);
  422. }
  423. static u8 lowpan_get_dsn(const struct net_device *dev)
  424. {
  425. struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
  426. return ieee802154_mlme_ops(real_dev)->get_dsn(real_dev);
  427. }
  428. static struct header_ops lowpan_header_ops = {
  429. .create = lowpan_header_create,
  430. };
  431. static const struct net_device_ops lowpan_netdev_ops = {
  432. .ndo_start_xmit = lowpan_xmit,
  433. .ndo_set_mac_address = lowpan_set_address,
  434. };
  435. static struct ieee802154_mlme_ops lowpan_mlme = {
  436. .get_pan_id = lowpan_get_pan_id,
  437. .get_phy = lowpan_get_phy,
  438. .get_short_addr = lowpan_get_short_addr,
  439. .get_dsn = lowpan_get_dsn,
  440. };
  441. static void lowpan_setup(struct net_device *dev)
  442. {
  443. dev->addr_len = IEEE802154_ADDR_LEN;
  444. memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);
  445. dev->type = ARPHRD_IEEE802154;
  446. /* Frame Control + Sequence Number + Address fields + Security Header */
  447. dev->hard_header_len = 2 + 1 + 20 + 14;
  448. dev->needed_tailroom = 2; /* FCS */
  449. dev->mtu = 1281;
  450. dev->tx_queue_len = 0;
  451. dev->flags = IFF_BROADCAST | IFF_MULTICAST;
  452. dev->watchdog_timeo = 0;
  453. dev->netdev_ops = &lowpan_netdev_ops;
  454. dev->header_ops = &lowpan_header_ops;
  455. dev->ml_priv = &lowpan_mlme;
  456. dev->destructor = free_netdev;
  457. }
  458. static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[])
  459. {
  460. if (tb[IFLA_ADDRESS]) {
  461. if (nla_len(tb[IFLA_ADDRESS]) != IEEE802154_ADDR_LEN)
  462. return -EINVAL;
  463. }
  464. return 0;
  465. }
  466. static int lowpan_rcv(struct sk_buff *skb, struct net_device *dev,
  467. struct packet_type *pt, struct net_device *orig_dev)
  468. {
  469. struct sk_buff *local_skb;
  470. if (!netif_running(dev))
  471. goto drop;
  472. if (dev->type != ARPHRD_IEEE802154)
  473. goto drop;
  474. /* check that it's our buffer */
  475. if (skb->data[0] == LOWPAN_DISPATCH_IPV6) {
  476. /* Copy the packet so that the IPv6 header is
  477. * properly aligned.
  478. */
  479. local_skb = skb_copy_expand(skb, NET_SKB_PAD - 1,
  480. skb_tailroom(skb), GFP_ATOMIC);
  481. if (!local_skb)
  482. goto drop;
  483. local_skb->protocol = htons(ETH_P_IPV6);
  484. local_skb->pkt_type = PACKET_HOST;
  485. /* Pull off the 1-byte of 6lowpan header. */
  486. skb_pull(local_skb, 1);
  487. lowpan_give_skb_to_devices(local_skb, NULL);
  488. kfree_skb(local_skb);
  489. kfree_skb(skb);
  490. } else {
  491. switch (skb->data[0] & 0xe0) {
  492. case LOWPAN_DISPATCH_IPHC: /* ipv6 datagram */
  493. case LOWPAN_DISPATCH_FRAG1: /* first fragment header */
  494. case LOWPAN_DISPATCH_FRAGN: /* next fragments headers */
  495. local_skb = skb_clone(skb, GFP_ATOMIC);
  496. if (!local_skb)
  497. goto drop;
  498. process_data(local_skb);
  499. kfree_skb(skb);
  500. break;
  501. default:
  502. break;
  503. }
  504. }
  505. return NET_RX_SUCCESS;
  506. drop:
  507. kfree_skb(skb);
  508. return NET_RX_DROP;
  509. }
  510. static int lowpan_newlink(struct net *src_net, struct net_device *dev,
  511. struct nlattr *tb[], struct nlattr *data[])
  512. {
  513. struct net_device *real_dev;
  514. struct lowpan_dev_record *entry;
  515. pr_debug("adding new link\n");
  516. if (!tb[IFLA_LINK])
  517. return -EINVAL;
  518. /* find and hold real wpan device */
  519. real_dev = dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
  520. if (!real_dev)
  521. return -ENODEV;
  522. if (real_dev->type != ARPHRD_IEEE802154) {
  523. dev_put(real_dev);
  524. return -EINVAL;
  525. }
  526. lowpan_dev_info(dev)->real_dev = real_dev;
  527. lowpan_dev_info(dev)->fragment_tag = 0;
  528. mutex_init(&lowpan_dev_info(dev)->dev_list_mtx);
  529. entry = kzalloc(sizeof(struct lowpan_dev_record), GFP_KERNEL);
  530. if (!entry) {
  531. dev_put(real_dev);
  532. lowpan_dev_info(dev)->real_dev = NULL;
  533. return -ENOMEM;
  534. }
  535. entry->ldev = dev;
  536. /* Set the lowpan harware address to the wpan hardware address. */
  537. memcpy(dev->dev_addr, real_dev->dev_addr, IEEE802154_ADDR_LEN);
  538. mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
  539. INIT_LIST_HEAD(&entry->list);
  540. list_add_tail(&entry->list, &lowpan_devices);
  541. mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
  542. register_netdevice(dev);
  543. return 0;
  544. }
  545. static void lowpan_dellink(struct net_device *dev, struct list_head *head)
  546. {
  547. struct lowpan_dev_info *lowpan_dev = lowpan_dev_info(dev);
  548. struct net_device *real_dev = lowpan_dev->real_dev;
  549. struct lowpan_dev_record *entry, *tmp;
  550. ASSERT_RTNL();
  551. mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
  552. list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) {
  553. if (entry->ldev == dev) {
  554. list_del(&entry->list);
  555. kfree(entry);
  556. }
  557. }
  558. mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
  559. mutex_destroy(&lowpan_dev_info(dev)->dev_list_mtx);
  560. unregister_netdevice_queue(dev, head);
  561. dev_put(real_dev);
  562. }
  563. static struct rtnl_link_ops lowpan_link_ops __read_mostly = {
  564. .kind = "lowpan",
  565. .priv_size = sizeof(struct lowpan_dev_info),
  566. .setup = lowpan_setup,
  567. .newlink = lowpan_newlink,
  568. .dellink = lowpan_dellink,
  569. .validate = lowpan_validate,
  570. };
  571. static inline int __init lowpan_netlink_init(void)
  572. {
  573. return rtnl_link_register(&lowpan_link_ops);
  574. }
  575. static inline void lowpan_netlink_fini(void)
  576. {
  577. rtnl_link_unregister(&lowpan_link_ops);
  578. }
  579. static int lowpan_device_event(struct notifier_block *unused,
  580. unsigned long event, void *ptr)
  581. {
  582. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  583. LIST_HEAD(del_list);
  584. struct lowpan_dev_record *entry, *tmp;
  585. if (dev->type != ARPHRD_IEEE802154)
  586. goto out;
  587. if (event == NETDEV_UNREGISTER) {
  588. list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) {
  589. if (lowpan_dev_info(entry->ldev)->real_dev == dev)
  590. lowpan_dellink(entry->ldev, &del_list);
  591. }
  592. unregister_netdevice_many(&del_list);
  593. }
  594. out:
  595. return NOTIFY_DONE;
  596. }
  597. static struct notifier_block lowpan_dev_notifier = {
  598. .notifier_call = lowpan_device_event,
  599. };
  600. static struct packet_type lowpan_packet_type = {
  601. .type = __constant_htons(ETH_P_IEEE802154),
  602. .func = lowpan_rcv,
  603. };
  604. static int __init lowpan_init_module(void)
  605. {
  606. int err = 0;
  607. err = lowpan_netlink_init();
  608. if (err < 0)
  609. goto out;
  610. dev_add_pack(&lowpan_packet_type);
  611. err = register_netdevice_notifier(&lowpan_dev_notifier);
  612. if (err < 0) {
  613. dev_remove_pack(&lowpan_packet_type);
  614. lowpan_netlink_fini();
  615. }
  616. out:
  617. return err;
  618. }
  619. static void __exit lowpan_cleanup_module(void)
  620. {
  621. struct lowpan_fragment *frame, *tframe;
  622. lowpan_netlink_fini();
  623. dev_remove_pack(&lowpan_packet_type);
  624. unregister_netdevice_notifier(&lowpan_dev_notifier);
  625. /* Now 6lowpan packet_type is removed, so no new fragments are
  626. * expected on RX, therefore that's the time to clean incomplete
  627. * fragments.
  628. */
  629. spin_lock_bh(&flist_lock);
  630. list_for_each_entry_safe(frame, tframe, &lowpan_fragments, list) {
  631. del_timer_sync(&frame->timer);
  632. list_del(&frame->list);
  633. dev_kfree_skb(frame->skb);
  634. kfree(frame);
  635. }
  636. spin_unlock_bh(&flist_lock);
  637. }
  638. module_init(lowpan_init_module);
  639. module_exit(lowpan_cleanup_module);
  640. MODULE_LICENSE("GPL");
  641. MODULE_ALIAS_RTNL_LINK("lowpan");