6lowpan.c 35 KB

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