6lowpan.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  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 <linux/etherdevice.h>
  56. #include <net/af_ieee802154.h>
  57. #include <net/ieee802154.h>
  58. #include <net/ieee802154_netdev.h>
  59. #include <net/ipv6.h>
  60. #include "6lowpan.h"
  61. /* TTL uncompression values */
  62. static const u8 lowpan_ttl_values[] = {0, 1, 64, 255};
  63. static LIST_HEAD(lowpan_devices);
  64. /*
  65. * Uncompression of linklocal:
  66. * 0 -> 16 bytes from packet
  67. * 1 -> 2 bytes from prefix - bunch of zeroes and 8 from packet
  68. * 2 -> 2 bytes from prefix - zeroes + 2 from packet
  69. * 3 -> 2 bytes from prefix - infer 8 bytes from lladdr
  70. *
  71. * NOTE: => the uncompress function does change 0xf to 0x10
  72. * NOTE: 0x00 => no-autoconfig => unspecified
  73. */
  74. static const u8 lowpan_unc_llconf[] = {0x0f, 0x28, 0x22, 0x20};
  75. /*
  76. * Uncompression of ctx-based:
  77. * 0 -> 0 bits from packet [unspecified / reserved]
  78. * 1 -> 8 bytes from prefix - bunch of zeroes and 8 from packet
  79. * 2 -> 8 bytes from prefix - zeroes + 2 from packet
  80. * 3 -> 8 bytes from prefix - infer 8 bytes from lladdr
  81. */
  82. static const u8 lowpan_unc_ctxconf[] = {0x00, 0x88, 0x82, 0x80};
  83. /*
  84. * Uncompression of ctx-base
  85. * 0 -> 0 bits from packet
  86. * 1 -> 2 bytes from prefix - bunch of zeroes 5 from packet
  87. * 2 -> 2 bytes from prefix - zeroes + 3 from packet
  88. * 3 -> 2 bytes from prefix - infer 1 bytes from lladdr
  89. */
  90. static const u8 lowpan_unc_mxconf[] = {0x0f, 0x25, 0x23, 0x21};
  91. /* Link local prefix */
  92. static const u8 lowpan_llprefix[] = {0xfe, 0x80};
  93. /* private device info */
  94. struct lowpan_dev_info {
  95. struct net_device *real_dev; /* real WPAN device ptr */
  96. struct mutex dev_list_mtx; /* mutex for list ops */
  97. };
  98. struct lowpan_dev_record {
  99. struct net_device *ldev;
  100. struct list_head list;
  101. };
  102. struct lowpan_fragment {
  103. struct sk_buff *skb; /* skb to be assembled */
  104. spinlock_t lock; /* concurency lock */
  105. u16 length; /* length to be assemled */
  106. u32 bytes_rcv; /* bytes received */
  107. u16 tag; /* current fragment tag */
  108. struct timer_list timer; /* assembling timer */
  109. struct list_head list; /* fragments list */
  110. };
  111. static unsigned short fragment_tag;
  112. static LIST_HEAD(lowpan_fragments);
  113. spinlock_t flist_lock;
  114. static inline struct
  115. lowpan_dev_info *lowpan_dev_info(const struct net_device *dev)
  116. {
  117. return netdev_priv(dev);
  118. }
  119. static inline void lowpan_address_flip(u8 *src, u8 *dest)
  120. {
  121. int i;
  122. for (i = 0; i < IEEE802154_ADDR_LEN; i++)
  123. (dest)[IEEE802154_ADDR_LEN - i - 1] = (src)[i];
  124. }
  125. /* list of all 6lowpan devices, uses for package delivering */
  126. /* print data in line */
  127. static inline void lowpan_raw_dump_inline(const char *caller, char *msg,
  128. unsigned char *buf, int len)
  129. {
  130. #ifdef DEBUG
  131. if (msg)
  132. pr_debug("(%s) %s: ", caller, msg);
  133. print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_NONE,
  134. 16, 1, buf, len, false);
  135. #endif /* DEBUG */
  136. }
  137. /*
  138. * print data in a table format:
  139. *
  140. * addr: xx xx xx xx xx xx
  141. * addr: xx xx xx xx xx xx
  142. * ...
  143. */
  144. static inline void lowpan_raw_dump_table(const char *caller, char *msg,
  145. unsigned char *buf, int len)
  146. {
  147. #ifdef DEBUG
  148. if (msg)
  149. pr_debug("(%s) %s:\n", caller, msg);
  150. print_hex_dump(KERN_DEBUG, "\t", DUMP_PREFIX_OFFSET,
  151. 16, 1, buf, len, false);
  152. #endif /* DEBUG */
  153. }
  154. static u8
  155. lowpan_compress_addr_64(u8 **hc06_ptr, u8 shift, const struct in6_addr *ipaddr,
  156. const unsigned char *lladdr)
  157. {
  158. u8 val = 0;
  159. if (is_addr_mac_addr_based(ipaddr, lladdr))
  160. val = 3; /* 0-bits */
  161. else if (lowpan_is_iid_16_bit_compressable(ipaddr)) {
  162. /* compress IID to 16 bits xxxx::XXXX */
  163. memcpy(*hc06_ptr, &ipaddr->s6_addr16[7], 2);
  164. *hc06_ptr += 2;
  165. val = 2; /* 16-bits */
  166. } else {
  167. /* do not compress IID => xxxx::IID */
  168. memcpy(*hc06_ptr, &ipaddr->s6_addr16[4], 8);
  169. *hc06_ptr += 8;
  170. val = 1; /* 64-bits */
  171. }
  172. return rol8(val, shift);
  173. }
  174. static void
  175. lowpan_uip_ds6_set_addr_iid(struct in6_addr *ipaddr, unsigned char *lladdr)
  176. {
  177. memcpy(&ipaddr->s6_addr[8], lladdr, IEEE802154_ALEN);
  178. /* second bit-flip (Universe/Local) is done according RFC2464 */
  179. ipaddr->s6_addr[8] ^= 0x02;
  180. }
  181. /*
  182. * Uncompress addresses based on a prefix and a postfix with zeroes in
  183. * between. If the postfix is zero in length it will use the link address
  184. * to configure the IP address (autoconf style).
  185. * pref_post_count takes a byte where the first nibble specify prefix count
  186. * and the second postfix count (NOTE: 15/0xf => 16 bytes copy).
  187. */
  188. static int
  189. lowpan_uncompress_addr(struct sk_buff *skb, struct in6_addr *ipaddr,
  190. u8 const *prefix, u8 pref_post_count, unsigned char *lladdr)
  191. {
  192. u8 prefcount = pref_post_count >> 4;
  193. u8 postcount = pref_post_count & 0x0f;
  194. /* full nibble 15 => 16 */
  195. prefcount = (prefcount == 15 ? 16 : prefcount);
  196. postcount = (postcount == 15 ? 16 : postcount);
  197. if (lladdr)
  198. lowpan_raw_dump_inline(__func__, "linklocal address",
  199. lladdr, IEEE802154_ALEN);
  200. if (prefcount > 0)
  201. memcpy(ipaddr, prefix, prefcount);
  202. if (prefcount + postcount < 16)
  203. memset(&ipaddr->s6_addr[prefcount], 0,
  204. 16 - (prefcount + postcount));
  205. if (postcount > 0) {
  206. memcpy(&ipaddr->s6_addr[16 - postcount], skb->data, postcount);
  207. skb_pull(skb, postcount);
  208. } else if (prefcount > 0) {
  209. if (lladdr == NULL)
  210. return -EINVAL;
  211. /* no IID based configuration if no prefix and no data */
  212. lowpan_uip_ds6_set_addr_iid(ipaddr, lladdr);
  213. }
  214. pr_debug("(%s): uncompressing %d + %d => ", __func__, prefcount,
  215. postcount);
  216. lowpan_raw_dump_inline(NULL, NULL, ipaddr->s6_addr, 16);
  217. return 0;
  218. }
  219. static void
  220. lowpan_compress_udp_header(u8 **hc06_ptr, struct sk_buff *skb)
  221. {
  222. struct udphdr *uh = udp_hdr(skb);
  223. pr_debug("(%s): UDP header compression\n", __func__);
  224. if (((uh->source & LOWPAN_NHC_UDP_4BIT_MASK) ==
  225. LOWPAN_NHC_UDP_4BIT_PORT) &&
  226. ((uh->dest & LOWPAN_NHC_UDP_4BIT_MASK) ==
  227. LOWPAN_NHC_UDP_4BIT_PORT)) {
  228. pr_debug("(%s): both ports compression to 4 bits\n", __func__);
  229. **hc06_ptr = LOWPAN_NHC_UDP_CS_P_11;
  230. **(hc06_ptr + 1) = /* subtraction is faster */
  231. (u8)((uh->dest - LOWPAN_NHC_UDP_4BIT_PORT) +
  232. ((uh->source & LOWPAN_NHC_UDP_4BIT_PORT) << 4));
  233. *hc06_ptr += 2;
  234. } else if ((uh->dest & LOWPAN_NHC_UDP_8BIT_MASK) ==
  235. LOWPAN_NHC_UDP_8BIT_PORT) {
  236. pr_debug("(%s): remove 8 bits of dest\n", __func__);
  237. **hc06_ptr = LOWPAN_NHC_UDP_CS_P_01;
  238. memcpy(*hc06_ptr + 1, &uh->source, 2);
  239. **(hc06_ptr + 3) = (u8)(uh->dest - LOWPAN_NHC_UDP_8BIT_PORT);
  240. *hc06_ptr += 4;
  241. } else if ((uh->source & LOWPAN_NHC_UDP_8BIT_MASK) ==
  242. LOWPAN_NHC_UDP_8BIT_PORT) {
  243. pr_debug("(%s): remove 8 bits of source\n", __func__);
  244. **hc06_ptr = LOWPAN_NHC_UDP_CS_P_10;
  245. memcpy(*hc06_ptr + 1, &uh->dest, 2);
  246. **(hc06_ptr + 3) = (u8)(uh->source - LOWPAN_NHC_UDP_8BIT_PORT);
  247. *hc06_ptr += 4;
  248. } else {
  249. pr_debug("(%s): can't compress header\n", __func__);
  250. **hc06_ptr = LOWPAN_NHC_UDP_CS_P_00;
  251. memcpy(*hc06_ptr + 1, &uh->source, 2);
  252. memcpy(*hc06_ptr + 3, &uh->dest, 2);
  253. *hc06_ptr += 5;
  254. }
  255. /* checksum is always inline */
  256. memcpy(*hc06_ptr, &uh->check, 2);
  257. *hc06_ptr += 2;
  258. }
  259. static u8 lowpan_fetch_skb_u8(struct sk_buff *skb)
  260. {
  261. u8 ret;
  262. ret = skb->data[0];
  263. skb_pull(skb, 1);
  264. return ret;
  265. }
  266. static u16 lowpan_fetch_skb_u16(struct sk_buff *skb)
  267. {
  268. u16 ret;
  269. BUG_ON(!pskb_may_pull(skb, 2));
  270. ret = skb->data[0] | (skb->data[1] << 8);
  271. skb_pull(skb, 2);
  272. return ret;
  273. }
  274. static int
  275. lowpan_uncompress_udp_header(struct sk_buff *skb)
  276. {
  277. struct udphdr *uh = udp_hdr(skb);
  278. u8 tmp;
  279. tmp = lowpan_fetch_skb_u8(skb);
  280. if ((tmp & LOWPAN_NHC_UDP_MASK) == LOWPAN_NHC_UDP_ID) {
  281. pr_debug("(%s): UDP header uncompression\n", __func__);
  282. switch (tmp & LOWPAN_NHC_UDP_CS_P_11) {
  283. case LOWPAN_NHC_UDP_CS_P_00:
  284. memcpy(&uh->source, &skb->data[0], 2);
  285. memcpy(&uh->dest, &skb->data[2], 2);
  286. skb_pull(skb, 4);
  287. break;
  288. case LOWPAN_NHC_UDP_CS_P_01:
  289. memcpy(&uh->source, &skb->data[0], 2);
  290. uh->dest =
  291. skb->data[2] + LOWPAN_NHC_UDP_8BIT_PORT;
  292. skb_pull(skb, 3);
  293. break;
  294. case LOWPAN_NHC_UDP_CS_P_10:
  295. uh->source = skb->data[0] + LOWPAN_NHC_UDP_8BIT_PORT;
  296. memcpy(&uh->dest, &skb->data[1], 2);
  297. skb_pull(skb, 3);
  298. break;
  299. case LOWPAN_NHC_UDP_CS_P_11:
  300. uh->source =
  301. LOWPAN_NHC_UDP_4BIT_PORT + (skb->data[0] >> 4);
  302. uh->dest =
  303. LOWPAN_NHC_UDP_4BIT_PORT + (skb->data[0] & 0x0f);
  304. skb_pull(skb, 1);
  305. break;
  306. default:
  307. pr_debug("(%s) ERROR: unknown UDP format\n", __func__);
  308. goto err;
  309. break;
  310. }
  311. pr_debug("(%s): uncompressed UDP ports: src = %d, dst = %d\n",
  312. __func__, uh->source, uh->dest);
  313. /* copy checksum */
  314. memcpy(&uh->check, &skb->data[0], 2);
  315. skb_pull(skb, 2);
  316. } else {
  317. pr_debug("(%s): ERROR: unsupported NH format\n", __func__);
  318. goto err;
  319. }
  320. return 0;
  321. err:
  322. return -EINVAL;
  323. }
  324. static int lowpan_header_create(struct sk_buff *skb,
  325. struct net_device *dev,
  326. unsigned short type, const void *_daddr,
  327. const void *_saddr, unsigned len)
  328. {
  329. u8 tmp, iphc0, iphc1, *hc06_ptr;
  330. struct ipv6hdr *hdr;
  331. const u8 *saddr = _saddr;
  332. const u8 *daddr = _daddr;
  333. u8 *head;
  334. struct ieee802154_addr sa, da;
  335. if (type != ETH_P_IPV6)
  336. return 0;
  337. /* TODO:
  338. * if this package isn't ipv6 one, where should it be routed?
  339. */
  340. head = kzalloc(100, GFP_KERNEL);
  341. if (head == NULL)
  342. return -ENOMEM;
  343. hdr = ipv6_hdr(skb);
  344. hc06_ptr = head + 2;
  345. pr_debug("(%s): IPv6 header dump:\n\tversion = %d\n\tlength = %d\n"
  346. "\tnexthdr = 0x%02x\n\thop_lim = %d\n", __func__,
  347. hdr->version, ntohs(hdr->payload_len), hdr->nexthdr,
  348. hdr->hop_limit);
  349. lowpan_raw_dump_table(__func__, "raw skb network header dump",
  350. skb_network_header(skb), sizeof(struct ipv6hdr));
  351. if (!saddr)
  352. saddr = dev->dev_addr;
  353. lowpan_raw_dump_inline(__func__, "saddr", (unsigned char *)saddr, 8);
  354. /*
  355. * As we copy some bit-length fields, in the IPHC encoding bytes,
  356. * we sometimes use |=
  357. * If the field is 0, and the current bit value in memory is 1,
  358. * this does not work. We therefore reset the IPHC encoding here
  359. */
  360. iphc0 = LOWPAN_DISPATCH_IPHC;
  361. iphc1 = 0;
  362. /* TODO: context lookup */
  363. lowpan_raw_dump_inline(__func__, "daddr", (unsigned char *)daddr, 8);
  364. /*
  365. * Traffic class, flow label
  366. * If flow label is 0, compress it. If traffic class is 0, compress it
  367. * We have to process both in the same time as the offset of traffic
  368. * class depends on the presence of version and flow label
  369. */
  370. /* hc06 format of TC is ECN | DSCP , original one is DSCP | ECN */
  371. tmp = (hdr->priority << 4) | (hdr->flow_lbl[0] >> 4);
  372. tmp = ((tmp & 0x03) << 6) | (tmp >> 2);
  373. if (((hdr->flow_lbl[0] & 0x0F) == 0) &&
  374. (hdr->flow_lbl[1] == 0) && (hdr->flow_lbl[2] == 0)) {
  375. /* flow label can be compressed */
  376. iphc0 |= LOWPAN_IPHC_FL_C;
  377. if ((hdr->priority == 0) &&
  378. ((hdr->flow_lbl[0] & 0xF0) == 0)) {
  379. /* compress (elide) all */
  380. iphc0 |= LOWPAN_IPHC_TC_C;
  381. } else {
  382. /* compress only the flow label */
  383. *hc06_ptr = tmp;
  384. hc06_ptr += 1;
  385. }
  386. } else {
  387. /* Flow label cannot be compressed */
  388. if ((hdr->priority == 0) &&
  389. ((hdr->flow_lbl[0] & 0xF0) == 0)) {
  390. /* compress only traffic class */
  391. iphc0 |= LOWPAN_IPHC_TC_C;
  392. *hc06_ptr = (tmp & 0xc0) | (hdr->flow_lbl[0] & 0x0F);
  393. memcpy(hc06_ptr + 1, &hdr->flow_lbl[1], 2);
  394. hc06_ptr += 3;
  395. } else {
  396. /* compress nothing */
  397. memcpy(hc06_ptr, &hdr, 4);
  398. /* replace the top byte with new ECN | DSCP format */
  399. *hc06_ptr = tmp;
  400. hc06_ptr += 4;
  401. }
  402. }
  403. /* NOTE: payload length is always compressed */
  404. /* Next Header is compress if UDP */
  405. if (hdr->nexthdr == UIP_PROTO_UDP)
  406. iphc0 |= LOWPAN_IPHC_NH_C;
  407. if ((iphc0 & LOWPAN_IPHC_NH_C) == 0) {
  408. *hc06_ptr = hdr->nexthdr;
  409. hc06_ptr += 1;
  410. }
  411. /*
  412. * Hop limit
  413. * if 1: compress, encoding is 01
  414. * if 64: compress, encoding is 10
  415. * if 255: compress, encoding is 11
  416. * else do not compress
  417. */
  418. switch (hdr->hop_limit) {
  419. case 1:
  420. iphc0 |= LOWPAN_IPHC_TTL_1;
  421. break;
  422. case 64:
  423. iphc0 |= LOWPAN_IPHC_TTL_64;
  424. break;
  425. case 255:
  426. iphc0 |= LOWPAN_IPHC_TTL_255;
  427. break;
  428. default:
  429. *hc06_ptr = hdr->hop_limit;
  430. break;
  431. }
  432. /* source address compression */
  433. if (is_addr_unspecified(&hdr->saddr)) {
  434. pr_debug("(%s): source address is unspecified, setting SAC\n",
  435. __func__);
  436. iphc1 |= LOWPAN_IPHC_SAC;
  437. /* TODO: context lookup */
  438. } else if (is_addr_link_local(&hdr->saddr)) {
  439. pr_debug("(%s): source address is link-local\n", __func__);
  440. iphc1 |= lowpan_compress_addr_64(&hc06_ptr,
  441. LOWPAN_IPHC_SAM_BIT, &hdr->saddr, saddr);
  442. } else {
  443. pr_debug("(%s): send the full source address\n", __func__);
  444. memcpy(hc06_ptr, &hdr->saddr.s6_addr16[0], 16);
  445. hc06_ptr += 16;
  446. }
  447. /* destination address compression */
  448. if (is_addr_mcast(&hdr->daddr)) {
  449. pr_debug("(%s): destination address is multicast", __func__);
  450. iphc1 |= LOWPAN_IPHC_M;
  451. if (lowpan_is_mcast_addr_compressable8(&hdr->daddr)) {
  452. pr_debug("compressed to 1 octet\n");
  453. iphc1 |= LOWPAN_IPHC_DAM_11;
  454. /* use last byte */
  455. *hc06_ptr = hdr->daddr.s6_addr[15];
  456. hc06_ptr += 1;
  457. } else if (lowpan_is_mcast_addr_compressable32(&hdr->daddr)) {
  458. pr_debug("compressed to 4 octets\n");
  459. iphc1 |= LOWPAN_IPHC_DAM_10;
  460. /* second byte + the last three */
  461. *hc06_ptr = hdr->daddr.s6_addr[1];
  462. memcpy(hc06_ptr + 1, &hdr->daddr.s6_addr[13], 3);
  463. hc06_ptr += 4;
  464. } else if (lowpan_is_mcast_addr_compressable48(&hdr->daddr)) {
  465. pr_debug("compressed to 6 octets\n");
  466. iphc1 |= LOWPAN_IPHC_DAM_01;
  467. /* second byte + the last five */
  468. *hc06_ptr = hdr->daddr.s6_addr[1];
  469. memcpy(hc06_ptr + 1, &hdr->daddr.s6_addr[11], 5);
  470. hc06_ptr += 6;
  471. } else {
  472. pr_debug("using full address\n");
  473. iphc1 |= LOWPAN_IPHC_DAM_00;
  474. memcpy(hc06_ptr, &hdr->daddr.s6_addr[0], 16);
  475. hc06_ptr += 16;
  476. }
  477. } else {
  478. pr_debug("(%s): destination address is unicast: ", __func__);
  479. /* TODO: context lookup */
  480. if (is_addr_link_local(&hdr->daddr)) {
  481. pr_debug("destination address is link-local\n");
  482. iphc1 |= lowpan_compress_addr_64(&hc06_ptr,
  483. LOWPAN_IPHC_DAM_BIT, &hdr->daddr, daddr);
  484. } else {
  485. pr_debug("using full address\n");
  486. memcpy(hc06_ptr, &hdr->daddr.s6_addr16[0], 16);
  487. hc06_ptr += 16;
  488. }
  489. }
  490. /* UDP header compression */
  491. if (hdr->nexthdr == UIP_PROTO_UDP)
  492. lowpan_compress_udp_header(&hc06_ptr, skb);
  493. head[0] = iphc0;
  494. head[1] = iphc1;
  495. skb_pull(skb, sizeof(struct ipv6hdr));
  496. memcpy(skb_push(skb, hc06_ptr - head), head, hc06_ptr - head);
  497. kfree(head);
  498. lowpan_raw_dump_table(__func__, "raw skb data dump", skb->data,
  499. skb->len);
  500. /*
  501. * NOTE1: I'm still unsure about the fact that compression and WPAN
  502. * header are created here and not later in the xmit. So wait for
  503. * an opinion of net maintainers.
  504. */
  505. /*
  506. * NOTE2: to be absolutely correct, we must derive PANid information
  507. * from MAC subif of the 'dev' and 'real_dev' network devices, but
  508. * this isn't implemented in mainline yet, so currently we assign 0xff
  509. */
  510. {
  511. /* prepare wpan address data */
  512. sa.addr_type = IEEE802154_ADDR_LONG;
  513. sa.pan_id = 0xff;
  514. da.addr_type = IEEE802154_ADDR_LONG;
  515. da.pan_id = 0xff;
  516. memcpy(&(da.hwaddr), daddr, 8);
  517. memcpy(&(sa.hwaddr), saddr, 8);
  518. mac_cb(skb)->flags = IEEE802154_FC_TYPE_DATA;
  519. return dev_hard_header(skb, lowpan_dev_info(dev)->real_dev,
  520. type, (void *)&da, (void *)&sa, skb->len);
  521. }
  522. }
  523. static int lowpan_skb_deliver(struct sk_buff *skb, struct ipv6hdr *hdr)
  524. {
  525. struct sk_buff *new;
  526. struct lowpan_dev_record *entry;
  527. int stat = NET_RX_SUCCESS;
  528. new = skb_copy_expand(skb, sizeof(struct ipv6hdr), skb_tailroom(skb),
  529. GFP_ATOMIC);
  530. kfree_skb(skb);
  531. if (!new)
  532. return -ENOMEM;
  533. skb_push(new, sizeof(struct ipv6hdr));
  534. skb_reset_network_header(new);
  535. skb_copy_to_linear_data(new, hdr, sizeof(struct ipv6hdr));
  536. new->protocol = htons(ETH_P_IPV6);
  537. new->pkt_type = PACKET_HOST;
  538. rcu_read_lock();
  539. list_for_each_entry_rcu(entry, &lowpan_devices, list)
  540. if (lowpan_dev_info(entry->ldev)->real_dev == new->dev) {
  541. skb = skb_copy(new, GFP_ATOMIC);
  542. if (!skb) {
  543. stat = -ENOMEM;
  544. break;
  545. }
  546. skb->dev = entry->ldev;
  547. stat = netif_rx(skb);
  548. }
  549. rcu_read_unlock();
  550. kfree_skb(new);
  551. return stat;
  552. }
  553. static void lowpan_fragment_timer_expired(unsigned long entry_addr)
  554. {
  555. struct lowpan_fragment *entry = (struct lowpan_fragment *)entry_addr;
  556. pr_debug("%s: timer expired for frame with tag %d\n", __func__,
  557. entry->tag);
  558. spin_lock(&flist_lock);
  559. list_del(&entry->list);
  560. spin_unlock(&flist_lock);
  561. dev_kfree_skb(entry->skb);
  562. kfree(entry);
  563. }
  564. static int
  565. lowpan_process_data(struct sk_buff *skb)
  566. {
  567. struct ipv6hdr hdr;
  568. u8 tmp, iphc0, iphc1, num_context = 0;
  569. u8 *_saddr, *_daddr;
  570. int err;
  571. lowpan_raw_dump_table(__func__, "raw skb data dump", skb->data,
  572. skb->len);
  573. /* at least two bytes will be used for the encoding */
  574. if (skb->len < 2)
  575. goto drop;
  576. iphc0 = lowpan_fetch_skb_u8(skb);
  577. /* fragments assembling */
  578. switch (iphc0 & LOWPAN_DISPATCH_MASK) {
  579. case LOWPAN_DISPATCH_FRAG1:
  580. case LOWPAN_DISPATCH_FRAGN:
  581. {
  582. struct lowpan_fragment *frame;
  583. u8 len, offset;
  584. u16 tag;
  585. bool found = false;
  586. len = lowpan_fetch_skb_u8(skb); /* frame length */
  587. tag = lowpan_fetch_skb_u16(skb);
  588. /*
  589. * check if frame assembling with the same tag is
  590. * already in progress
  591. */
  592. spin_lock(&flist_lock);
  593. list_for_each_entry(frame, &lowpan_fragments, list)
  594. if (frame->tag == tag) {
  595. found = true;
  596. break;
  597. }
  598. /* alloc new frame structure */
  599. if (!found) {
  600. frame = kzalloc(sizeof(struct lowpan_fragment),
  601. GFP_ATOMIC);
  602. if (!frame)
  603. goto unlock_and_drop;
  604. INIT_LIST_HEAD(&frame->list);
  605. frame->length = (iphc0 & 7) | (len << 3);
  606. frame->tag = tag;
  607. /* allocate buffer for frame assembling */
  608. frame->skb = alloc_skb(frame->length +
  609. sizeof(struct ipv6hdr), GFP_ATOMIC);
  610. if (!frame->skb) {
  611. kfree(frame);
  612. goto unlock_and_drop;
  613. }
  614. frame->skb->priority = skb->priority;
  615. frame->skb->dev = skb->dev;
  616. /* reserve headroom for uncompressed ipv6 header */
  617. skb_reserve(frame->skb, sizeof(struct ipv6hdr));
  618. skb_put(frame->skb, frame->length);
  619. init_timer(&frame->timer);
  620. /* time out is the same as for ipv6 - 60 sec */
  621. frame->timer.expires = jiffies + LOWPAN_FRAG_TIMEOUT;
  622. frame->timer.data = (unsigned long)frame;
  623. frame->timer.function = lowpan_fragment_timer_expired;
  624. add_timer(&frame->timer);
  625. list_add_tail(&frame->list, &lowpan_fragments);
  626. }
  627. if ((iphc0 & LOWPAN_DISPATCH_MASK) == LOWPAN_DISPATCH_FRAG1)
  628. goto unlock_and_drop;
  629. offset = lowpan_fetch_skb_u8(skb); /* fetch offset */
  630. /* if payload fits buffer, copy it */
  631. if (likely((offset * 8 + skb->len) <= frame->length))
  632. skb_copy_to_linear_data_offset(frame->skb, offset * 8,
  633. skb->data, skb->len);
  634. else
  635. goto unlock_and_drop;
  636. frame->bytes_rcv += skb->len;
  637. /* frame assembling complete */
  638. if ((frame->bytes_rcv == frame->length) &&
  639. frame->timer.expires > jiffies) {
  640. /* if timer haven't expired - first of all delete it */
  641. del_timer(&frame->timer);
  642. list_del(&frame->list);
  643. spin_unlock(&flist_lock);
  644. dev_kfree_skb(skb);
  645. skb = frame->skb;
  646. kfree(frame);
  647. iphc0 = lowpan_fetch_skb_u8(skb);
  648. break;
  649. }
  650. spin_unlock(&flist_lock);
  651. return kfree_skb(skb), 0;
  652. }
  653. default:
  654. break;
  655. }
  656. iphc1 = lowpan_fetch_skb_u8(skb);
  657. _saddr = mac_cb(skb)->sa.hwaddr;
  658. _daddr = mac_cb(skb)->da.hwaddr;
  659. pr_debug("(%s): iphc0 = %02x, iphc1 = %02x\n", __func__, iphc0, iphc1);
  660. /* another if the CID flag is set */
  661. if (iphc1 & LOWPAN_IPHC_CID) {
  662. pr_debug("(%s): CID flag is set, increase header with one\n",
  663. __func__);
  664. if (!skb->len)
  665. goto drop;
  666. num_context = lowpan_fetch_skb_u8(skb);
  667. }
  668. hdr.version = 6;
  669. /* Traffic Class and Flow Label */
  670. switch ((iphc0 & LOWPAN_IPHC_TF) >> 3) {
  671. /*
  672. * Traffic Class and FLow Label carried in-line
  673. * ECN + DSCP + 4-bit Pad + Flow Label (4 bytes)
  674. */
  675. case 0: /* 00b */
  676. if (!skb->len)
  677. goto drop;
  678. tmp = lowpan_fetch_skb_u8(skb);
  679. memcpy(&hdr.flow_lbl, &skb->data[0], 3);
  680. skb_pull(skb, 3);
  681. hdr.priority = ((tmp >> 2) & 0x0f);
  682. hdr.flow_lbl[0] = ((tmp >> 2) & 0x30) | (tmp << 6) |
  683. (hdr.flow_lbl[0] & 0x0f);
  684. break;
  685. /*
  686. * Traffic class carried in-line
  687. * ECN + DSCP (1 byte), Flow Label is elided
  688. */
  689. case 1: /* 10b */
  690. if (!skb->len)
  691. goto drop;
  692. tmp = lowpan_fetch_skb_u8(skb);
  693. hdr.priority = ((tmp >> 2) & 0x0f);
  694. hdr.flow_lbl[0] = ((tmp << 6) & 0xC0) | ((tmp >> 2) & 0x30);
  695. hdr.flow_lbl[1] = 0;
  696. hdr.flow_lbl[2] = 0;
  697. break;
  698. /*
  699. * Flow Label carried in-line
  700. * ECN + 2-bit Pad + Flow Label (3 bytes), DSCP is elided
  701. */
  702. case 2: /* 01b */
  703. if (!skb->len)
  704. goto drop;
  705. tmp = lowpan_fetch_skb_u8(skb);
  706. hdr.flow_lbl[0] = (skb->data[0] & 0x0F) | ((tmp >> 2) & 0x30);
  707. memcpy(&hdr.flow_lbl[1], &skb->data[0], 2);
  708. skb_pull(skb, 2);
  709. break;
  710. /* Traffic Class and Flow Label are elided */
  711. case 3: /* 11b */
  712. hdr.priority = 0;
  713. hdr.flow_lbl[0] = 0;
  714. hdr.flow_lbl[1] = 0;
  715. hdr.flow_lbl[2] = 0;
  716. break;
  717. default:
  718. break;
  719. }
  720. /* Next Header */
  721. if ((iphc0 & LOWPAN_IPHC_NH_C) == 0) {
  722. /* Next header is carried inline */
  723. if (!skb->len)
  724. goto drop;
  725. hdr.nexthdr = lowpan_fetch_skb_u8(skb);
  726. pr_debug("(%s): NH flag is set, next header is carried "
  727. "inline: %02x\n", __func__, hdr.nexthdr);
  728. }
  729. /* Hop Limit */
  730. if ((iphc0 & 0x03) != LOWPAN_IPHC_TTL_I)
  731. hdr.hop_limit = lowpan_ttl_values[iphc0 & 0x03];
  732. else {
  733. if (!skb->len)
  734. goto drop;
  735. hdr.hop_limit = lowpan_fetch_skb_u8(skb);
  736. }
  737. /* Extract SAM to the tmp variable */
  738. tmp = ((iphc1 & LOWPAN_IPHC_SAM) >> LOWPAN_IPHC_SAM_BIT) & 0x03;
  739. /* Source address uncompression */
  740. pr_debug("(%s): source address stateless compression\n", __func__);
  741. err = lowpan_uncompress_addr(skb, &hdr.saddr, lowpan_llprefix,
  742. lowpan_unc_llconf[tmp], skb->data);
  743. if (err)
  744. goto drop;
  745. /* Extract DAM to the tmp variable */
  746. tmp = ((iphc1 & LOWPAN_IPHC_DAM_11) >> LOWPAN_IPHC_DAM_BIT) & 0x03;
  747. /* check for Multicast Compression */
  748. if (iphc1 & LOWPAN_IPHC_M) {
  749. if (iphc1 & LOWPAN_IPHC_DAC) {
  750. pr_debug("(%s): destination address context-based "
  751. "multicast compression\n", __func__);
  752. /* TODO: implement this */
  753. } else {
  754. u8 prefix[] = {0xff, 0x02};
  755. pr_debug("(%s): destination address non-context-based"
  756. " multicast compression\n", __func__);
  757. if (0 < tmp && tmp < 3) {
  758. if (!skb->len)
  759. goto drop;
  760. else
  761. prefix[1] = lowpan_fetch_skb_u8(skb);
  762. }
  763. err = lowpan_uncompress_addr(skb, &hdr.daddr, prefix,
  764. lowpan_unc_mxconf[tmp], NULL);
  765. if (err)
  766. goto drop;
  767. }
  768. } else {
  769. pr_debug("(%s): destination address stateless compression\n",
  770. __func__);
  771. err = lowpan_uncompress_addr(skb, &hdr.daddr, lowpan_llprefix,
  772. lowpan_unc_llconf[tmp], skb->data);
  773. if (err)
  774. goto drop;
  775. }
  776. /* UDP data uncompression */
  777. if (iphc0 & LOWPAN_IPHC_NH_C)
  778. if (lowpan_uncompress_udp_header(skb))
  779. goto drop;
  780. /* Not fragmented package */
  781. hdr.payload_len = htons(skb->len);
  782. pr_debug("(%s): skb headroom size = %d, data length = %d\n", __func__,
  783. skb_headroom(skb), skb->len);
  784. pr_debug("(%s): IPv6 header dump:\n\tversion = %d\n\tlength = %d\n\t"
  785. "nexthdr = 0x%02x\n\thop_lim = %d\n", __func__, hdr.version,
  786. ntohs(hdr.payload_len), hdr.nexthdr, hdr.hop_limit);
  787. lowpan_raw_dump_table(__func__, "raw header dump", (u8 *)&hdr,
  788. sizeof(hdr));
  789. return lowpan_skb_deliver(skb, &hdr);
  790. unlock_and_drop:
  791. spin_unlock(&flist_lock);
  792. drop:
  793. kfree_skb(skb);
  794. return -EINVAL;
  795. }
  796. static int lowpan_get_mac_header_length(struct sk_buff *skb)
  797. {
  798. /*
  799. * Currently long addressing mode is supported only, so the overall
  800. * header size is 21:
  801. * FC SeqNum DPAN DA SA Sec
  802. * 2 + 1 + 2 + 8 + 8 + 0 = 21
  803. */
  804. return 21;
  805. }
  806. static int
  807. lowpan_fragment_xmit(struct sk_buff *skb, u8 *head,
  808. int mlen, int plen, int offset)
  809. {
  810. struct sk_buff *frag;
  811. int hlen, ret;
  812. /* if payload length is zero, therefore it's a first fragment */
  813. hlen = (plen == 0 ? LOWPAN_FRAG1_HEAD_SIZE : LOWPAN_FRAGN_HEAD_SIZE);
  814. lowpan_raw_dump_inline(__func__, "6lowpan fragment header", head, hlen);
  815. frag = dev_alloc_skb(hlen + mlen + plen + IEEE802154_MFR_SIZE);
  816. if (!frag)
  817. return -ENOMEM;
  818. frag->priority = skb->priority;
  819. frag->dev = skb->dev;
  820. /* copy header, MFR and payload */
  821. memcpy(skb_put(frag, mlen), skb->data, mlen);
  822. memcpy(skb_put(frag, hlen), head, hlen);
  823. if (plen)
  824. skb_copy_from_linear_data_offset(skb, offset + mlen,
  825. skb_put(frag, plen), plen);
  826. lowpan_raw_dump_table(__func__, " raw fragment dump", frag->data,
  827. frag->len);
  828. ret = dev_queue_xmit(frag);
  829. return ret;
  830. }
  831. static int
  832. lowpan_skb_fragmentation(struct sk_buff *skb)
  833. {
  834. int err, header_length, payload_length, tag, offset = 0;
  835. u8 head[5];
  836. header_length = lowpan_get_mac_header_length(skb);
  837. payload_length = skb->len - header_length;
  838. tag = fragment_tag++;
  839. /* first fragment header */
  840. head[0] = LOWPAN_DISPATCH_FRAG1 | (payload_length & 0x7);
  841. head[1] = (payload_length >> 3) & 0xff;
  842. head[2] = tag & 0xff;
  843. head[3] = tag >> 8;
  844. err = lowpan_fragment_xmit(skb, head, header_length, 0, 0);
  845. /* next fragment header */
  846. head[0] &= ~LOWPAN_DISPATCH_FRAG1;
  847. head[0] |= LOWPAN_DISPATCH_FRAGN;
  848. while ((payload_length - offset > 0) && (err >= 0)) {
  849. int len = LOWPAN_FRAG_SIZE;
  850. head[4] = offset / 8;
  851. if (payload_length - offset < len)
  852. len = payload_length - offset;
  853. err = lowpan_fragment_xmit(skb, head, header_length,
  854. len, offset);
  855. offset += len;
  856. }
  857. return err;
  858. }
  859. static netdev_tx_t lowpan_xmit(struct sk_buff *skb, struct net_device *dev)
  860. {
  861. int err = -1;
  862. pr_debug("(%s): package xmit\n", __func__);
  863. skb->dev = lowpan_dev_info(dev)->real_dev;
  864. if (skb->dev == NULL) {
  865. pr_debug("(%s) ERROR: no real wpan device found\n", __func__);
  866. goto error;
  867. }
  868. if (skb->len <= IEEE802154_MTU) {
  869. err = dev_queue_xmit(skb);
  870. goto out;
  871. }
  872. pr_debug("(%s): frame is too big, fragmentation is needed\n",
  873. __func__);
  874. err = lowpan_skb_fragmentation(skb);
  875. error:
  876. dev_kfree_skb(skb);
  877. out:
  878. if (err < 0)
  879. pr_debug("(%s): ERROR: xmit failed\n", __func__);
  880. return (err < 0 ? NETDEV_TX_BUSY : NETDEV_TX_OK);
  881. }
  882. static void lowpan_dev_free(struct net_device *dev)
  883. {
  884. dev_put(lowpan_dev_info(dev)->real_dev);
  885. free_netdev(dev);
  886. }
  887. static struct header_ops lowpan_header_ops = {
  888. .create = lowpan_header_create,
  889. };
  890. static const struct net_device_ops lowpan_netdev_ops = {
  891. .ndo_start_xmit = lowpan_xmit,
  892. .ndo_set_mac_address = eth_mac_addr,
  893. };
  894. static void lowpan_setup(struct net_device *dev)
  895. {
  896. pr_debug("(%s)\n", __func__);
  897. dev->addr_len = IEEE802154_ADDR_LEN;
  898. memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);
  899. dev->type = ARPHRD_IEEE802154;
  900. /* Frame Control + Sequence Number + Address fields + Security Header */
  901. dev->hard_header_len = 2 + 1 + 20 + 14;
  902. dev->needed_tailroom = 2; /* FCS */
  903. dev->mtu = 1281;
  904. dev->tx_queue_len = 0;
  905. dev->flags = IFF_BROADCAST | IFF_MULTICAST;
  906. dev->watchdog_timeo = 0;
  907. dev->netdev_ops = &lowpan_netdev_ops;
  908. dev->header_ops = &lowpan_header_ops;
  909. dev->destructor = lowpan_dev_free;
  910. }
  911. static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[])
  912. {
  913. pr_debug("(%s)\n", __func__);
  914. if (tb[IFLA_ADDRESS]) {
  915. if (nla_len(tb[IFLA_ADDRESS]) != IEEE802154_ADDR_LEN)
  916. return -EINVAL;
  917. }
  918. return 0;
  919. }
  920. static int lowpan_rcv(struct sk_buff *skb, struct net_device *dev,
  921. struct packet_type *pt, struct net_device *orig_dev)
  922. {
  923. if (!netif_running(dev))
  924. goto drop;
  925. if (dev->type != ARPHRD_IEEE802154)
  926. goto drop;
  927. /* check that it's our buffer */
  928. switch (skb->data[0] & 0xe0) {
  929. case LOWPAN_DISPATCH_IPHC: /* ipv6 datagram */
  930. case LOWPAN_DISPATCH_FRAG1: /* first fragment header */
  931. case LOWPAN_DISPATCH_FRAGN: /* next fragments headers */
  932. lowpan_process_data(skb);
  933. break;
  934. default:
  935. break;
  936. }
  937. return NET_RX_SUCCESS;
  938. drop:
  939. kfree_skb(skb);
  940. return NET_RX_DROP;
  941. }
  942. static int lowpan_newlink(struct net *src_net, struct net_device *dev,
  943. struct nlattr *tb[], struct nlattr *data[])
  944. {
  945. struct net_device *real_dev;
  946. struct lowpan_dev_record *entry;
  947. pr_debug("(%s)\n", __func__);
  948. if (!tb[IFLA_LINK])
  949. return -EINVAL;
  950. /* find and hold real wpan device */
  951. real_dev = dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
  952. if (!real_dev)
  953. return -ENODEV;
  954. lowpan_dev_info(dev)->real_dev = real_dev;
  955. mutex_init(&lowpan_dev_info(dev)->dev_list_mtx);
  956. entry = kzalloc(sizeof(struct lowpan_dev_record), GFP_KERNEL);
  957. if (!entry) {
  958. dev_put(real_dev);
  959. lowpan_dev_info(dev)->real_dev = NULL;
  960. return -ENOMEM;
  961. }
  962. entry->ldev = dev;
  963. mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
  964. INIT_LIST_HEAD(&entry->list);
  965. list_add_tail(&entry->list, &lowpan_devices);
  966. mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
  967. register_netdevice(dev);
  968. return 0;
  969. }
  970. static void lowpan_dellink(struct net_device *dev, struct list_head *head)
  971. {
  972. struct lowpan_dev_info *lowpan_dev = lowpan_dev_info(dev);
  973. struct net_device *real_dev = lowpan_dev->real_dev;
  974. struct lowpan_dev_record *entry;
  975. struct lowpan_dev_record *tmp;
  976. ASSERT_RTNL();
  977. mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
  978. list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) {
  979. if (entry->ldev == dev) {
  980. list_del(&entry->list);
  981. kfree(entry);
  982. }
  983. }
  984. mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
  985. mutex_destroy(&lowpan_dev_info(dev)->dev_list_mtx);
  986. unregister_netdevice_queue(dev, head);
  987. dev_put(real_dev);
  988. }
  989. static struct rtnl_link_ops lowpan_link_ops __read_mostly = {
  990. .kind = "lowpan",
  991. .priv_size = sizeof(struct lowpan_dev_info),
  992. .setup = lowpan_setup,
  993. .newlink = lowpan_newlink,
  994. .dellink = lowpan_dellink,
  995. .validate = lowpan_validate,
  996. };
  997. static inline int __init lowpan_netlink_init(void)
  998. {
  999. return rtnl_link_register(&lowpan_link_ops);
  1000. }
  1001. static inline void __init lowpan_netlink_fini(void)
  1002. {
  1003. rtnl_link_unregister(&lowpan_link_ops);
  1004. }
  1005. static struct packet_type lowpan_packet_type = {
  1006. .type = __constant_htons(ETH_P_IEEE802154),
  1007. .func = lowpan_rcv,
  1008. };
  1009. static int __init lowpan_init_module(void)
  1010. {
  1011. int err = 0;
  1012. pr_debug("(%s)\n", __func__);
  1013. err = lowpan_netlink_init();
  1014. if (err < 0)
  1015. goto out;
  1016. dev_add_pack(&lowpan_packet_type);
  1017. out:
  1018. return err;
  1019. }
  1020. static void __exit lowpan_cleanup_module(void)
  1021. {
  1022. pr_debug("(%s)\n", __func__);
  1023. lowpan_netlink_fini();
  1024. dev_remove_pack(&lowpan_packet_type);
  1025. }
  1026. module_init(lowpan_init_module);
  1027. module_exit(lowpan_cleanup_module);
  1028. MODULE_LICENSE("GPL");
  1029. MODULE_ALIAS_RTNL_LINK("lowpan");