6lowpan.c 32 KB

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