6lowpan.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  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. #define DEBUG
  51. #include <linux/bitops.h>
  52. #include <linux/if_arp.h>
  53. #include <linux/module.h>
  54. #include <linux/moduleparam.h>
  55. #include <linux/netdevice.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 u8 lowpan_fetch_skb_u8(struct sk_buff *skb)
  220. {
  221. u8 ret;
  222. ret = skb->data[0];
  223. skb_pull(skb, 1);
  224. return ret;
  225. }
  226. static u16 lowpan_fetch_skb_u16(struct sk_buff *skb)
  227. {
  228. u16 ret;
  229. BUG_ON(!pskb_may_pull(skb, 2));
  230. ret = skb->data[0] | (skb->data[1] << 8);
  231. skb_pull(skb, 2);
  232. return ret;
  233. }
  234. static int lowpan_header_create(struct sk_buff *skb,
  235. struct net_device *dev,
  236. unsigned short type, const void *_daddr,
  237. const void *_saddr, unsigned len)
  238. {
  239. u8 tmp, iphc0, iphc1, *hc06_ptr;
  240. struct ipv6hdr *hdr;
  241. const u8 *saddr = _saddr;
  242. const u8 *daddr = _daddr;
  243. u8 *head;
  244. struct ieee802154_addr sa, da;
  245. if (type != ETH_P_IPV6)
  246. return 0;
  247. /* TODO:
  248. * if this package isn't ipv6 one, where should it be routed?
  249. */
  250. head = kzalloc(100, GFP_KERNEL);
  251. if (head == NULL)
  252. return -ENOMEM;
  253. hdr = ipv6_hdr(skb);
  254. hc06_ptr = head + 2;
  255. pr_debug("(%s): IPv6 header dump:\n\tversion = %d\n\tlength = %d\n"
  256. "\tnexthdr = 0x%02x\n\thop_lim = %d\n", __func__,
  257. hdr->version, ntohs(hdr->payload_len), hdr->nexthdr,
  258. hdr->hop_limit);
  259. lowpan_raw_dump_table(__func__, "raw skb network header dump",
  260. skb_network_header(skb), sizeof(struct ipv6hdr));
  261. if (!saddr)
  262. saddr = dev->dev_addr;
  263. lowpan_raw_dump_inline(__func__, "saddr", (unsigned char *)saddr, 8);
  264. /*
  265. * As we copy some bit-length fields, in the IPHC encoding bytes,
  266. * we sometimes use |=
  267. * If the field is 0, and the current bit value in memory is 1,
  268. * this does not work. We therefore reset the IPHC encoding here
  269. */
  270. iphc0 = LOWPAN_DISPATCH_IPHC;
  271. iphc1 = 0;
  272. /* TODO: context lookup */
  273. lowpan_raw_dump_inline(__func__, "daddr", (unsigned char *)daddr, 8);
  274. /*
  275. * Traffic class, flow label
  276. * If flow label is 0, compress it. If traffic class is 0, compress it
  277. * We have to process both in the same time as the offset of traffic
  278. * class depends on the presence of version and flow label
  279. */
  280. /* hc06 format of TC is ECN | DSCP , original one is DSCP | ECN */
  281. tmp = (hdr->priority << 4) | (hdr->flow_lbl[0] >> 4);
  282. tmp = ((tmp & 0x03) << 6) | (tmp >> 2);
  283. if (((hdr->flow_lbl[0] & 0x0F) == 0) &&
  284. (hdr->flow_lbl[1] == 0) && (hdr->flow_lbl[2] == 0)) {
  285. /* flow label can be compressed */
  286. iphc0 |= LOWPAN_IPHC_FL_C;
  287. if ((hdr->priority == 0) &&
  288. ((hdr->flow_lbl[0] & 0xF0) == 0)) {
  289. /* compress (elide) all */
  290. iphc0 |= LOWPAN_IPHC_TC_C;
  291. } else {
  292. /* compress only the flow label */
  293. *hc06_ptr = tmp;
  294. hc06_ptr += 1;
  295. }
  296. } else {
  297. /* Flow label cannot be compressed */
  298. if ((hdr->priority == 0) &&
  299. ((hdr->flow_lbl[0] & 0xF0) == 0)) {
  300. /* compress only traffic class */
  301. iphc0 |= LOWPAN_IPHC_TC_C;
  302. *hc06_ptr = (tmp & 0xc0) | (hdr->flow_lbl[0] & 0x0F);
  303. memcpy(hc06_ptr + 1, &hdr->flow_lbl[1], 2);
  304. hc06_ptr += 3;
  305. } else {
  306. /* compress nothing */
  307. memcpy(hc06_ptr, &hdr, 4);
  308. /* replace the top byte with new ECN | DSCP format */
  309. *hc06_ptr = tmp;
  310. hc06_ptr += 4;
  311. }
  312. }
  313. /* NOTE: payload length is always compressed */
  314. /* Next Header is compress if UDP */
  315. if (hdr->nexthdr == UIP_PROTO_UDP)
  316. iphc0 |= LOWPAN_IPHC_NH_C;
  317. /* TODO: next header compression */
  318. if ((iphc0 & LOWPAN_IPHC_NH_C) == 0) {
  319. *hc06_ptr = hdr->nexthdr;
  320. hc06_ptr += 1;
  321. }
  322. /*
  323. * Hop limit
  324. * if 1: compress, encoding is 01
  325. * if 64: compress, encoding is 10
  326. * if 255: compress, encoding is 11
  327. * else do not compress
  328. */
  329. switch (hdr->hop_limit) {
  330. case 1:
  331. iphc0 |= LOWPAN_IPHC_TTL_1;
  332. break;
  333. case 64:
  334. iphc0 |= LOWPAN_IPHC_TTL_64;
  335. break;
  336. case 255:
  337. iphc0 |= LOWPAN_IPHC_TTL_255;
  338. break;
  339. default:
  340. *hc06_ptr = hdr->hop_limit;
  341. break;
  342. }
  343. /* source address compression */
  344. if (is_addr_unspecified(&hdr->saddr)) {
  345. pr_debug("(%s): source address is unspecified, setting SAC\n",
  346. __func__);
  347. iphc1 |= LOWPAN_IPHC_SAC;
  348. /* TODO: context lookup */
  349. } else if (is_addr_link_local(&hdr->saddr)) {
  350. pr_debug("(%s): source address is link-local\n", __func__);
  351. iphc1 |= lowpan_compress_addr_64(&hc06_ptr,
  352. LOWPAN_IPHC_SAM_BIT, &hdr->saddr, saddr);
  353. } else {
  354. pr_debug("(%s): send the full source address\n", __func__);
  355. memcpy(hc06_ptr, &hdr->saddr.s6_addr16[0], 16);
  356. hc06_ptr += 16;
  357. }
  358. /* destination address compression */
  359. if (is_addr_mcast(&hdr->daddr)) {
  360. pr_debug("(%s): destination address is multicast", __func__);
  361. iphc1 |= LOWPAN_IPHC_M;
  362. if (lowpan_is_mcast_addr_compressable8(&hdr->daddr)) {
  363. pr_debug("compressed to 1 octet\n");
  364. iphc1 |= LOWPAN_IPHC_DAM_11;
  365. /* use last byte */
  366. *hc06_ptr = hdr->daddr.s6_addr[15];
  367. hc06_ptr += 1;
  368. } else if (lowpan_is_mcast_addr_compressable32(&hdr->daddr)) {
  369. pr_debug("compressed to 4 octets\n");
  370. iphc1 |= LOWPAN_IPHC_DAM_10;
  371. /* second byte + the last three */
  372. *hc06_ptr = hdr->daddr.s6_addr[1];
  373. memcpy(hc06_ptr + 1, &hdr->daddr.s6_addr[13], 3);
  374. hc06_ptr += 4;
  375. } else if (lowpan_is_mcast_addr_compressable48(&hdr->daddr)) {
  376. pr_debug("compressed to 6 octets\n");
  377. iphc1 |= LOWPAN_IPHC_DAM_01;
  378. /* second byte + the last five */
  379. *hc06_ptr = hdr->daddr.s6_addr[1];
  380. memcpy(hc06_ptr + 1, &hdr->daddr.s6_addr[11], 5);
  381. hc06_ptr += 6;
  382. } else {
  383. pr_debug("using full address\n");
  384. iphc1 |= LOWPAN_IPHC_DAM_00;
  385. memcpy(hc06_ptr, &hdr->daddr.s6_addr[0], 16);
  386. hc06_ptr += 16;
  387. }
  388. } else {
  389. pr_debug("(%s): destination address is unicast: ", __func__);
  390. /* TODO: context lookup */
  391. if (is_addr_link_local(&hdr->daddr)) {
  392. pr_debug("destination address is link-local\n");
  393. iphc1 |= lowpan_compress_addr_64(&hc06_ptr,
  394. LOWPAN_IPHC_DAM_BIT, &hdr->daddr, daddr);
  395. } else {
  396. pr_debug("using full address\n");
  397. memcpy(hc06_ptr, &hdr->daddr.s6_addr16[0], 16);
  398. hc06_ptr += 16;
  399. }
  400. }
  401. /* TODO: UDP header compression */
  402. /* TODO: Next Header compression */
  403. head[0] = iphc0;
  404. head[1] = iphc1;
  405. skb_pull(skb, sizeof(struct ipv6hdr));
  406. memcpy(skb_push(skb, hc06_ptr - head), head, hc06_ptr - head);
  407. kfree(head);
  408. lowpan_raw_dump_table(__func__, "raw skb data dump", skb->data,
  409. skb->len);
  410. /*
  411. * NOTE1: I'm still unsure about the fact that compression and WPAN
  412. * header are created here and not later in the xmit. So wait for
  413. * an opinion of net maintainers.
  414. */
  415. /*
  416. * NOTE2: to be absolutely correct, we must derive PANid information
  417. * from MAC subif of the 'dev' and 'real_dev' network devices, but
  418. * this isn't implemented in mainline yet, so currently we assign 0xff
  419. */
  420. {
  421. /* prepare wpan address data */
  422. sa.addr_type = IEEE802154_ADDR_LONG;
  423. sa.pan_id = 0xff;
  424. da.addr_type = IEEE802154_ADDR_LONG;
  425. da.pan_id = 0xff;
  426. memcpy(&(da.hwaddr), daddr, 8);
  427. memcpy(&(sa.hwaddr), saddr, 8);
  428. mac_cb(skb)->flags = IEEE802154_FC_TYPE_DATA;
  429. return dev_hard_header(skb, lowpan_dev_info(dev)->real_dev,
  430. type, (void *)&da, (void *)&sa, skb->len);
  431. }
  432. }
  433. static int lowpan_skb_deliver(struct sk_buff *skb, struct ipv6hdr *hdr)
  434. {
  435. struct sk_buff *new;
  436. struct lowpan_dev_record *entry;
  437. int stat = NET_RX_SUCCESS;
  438. new = skb_copy_expand(skb, sizeof(struct ipv6hdr), skb_tailroom(skb),
  439. GFP_ATOMIC);
  440. kfree_skb(skb);
  441. if (!new)
  442. return -ENOMEM;
  443. skb_push(new, sizeof(struct ipv6hdr));
  444. skb_reset_network_header(new);
  445. skb_copy_to_linear_data(new, hdr, sizeof(struct ipv6hdr));
  446. new->protocol = htons(ETH_P_IPV6);
  447. new->pkt_type = PACKET_HOST;
  448. rcu_read_lock();
  449. list_for_each_entry_rcu(entry, &lowpan_devices, list)
  450. if (lowpan_dev_info(entry->ldev)->real_dev == new->dev) {
  451. skb = skb_copy(new, GFP_ATOMIC);
  452. if (!skb) {
  453. stat = -ENOMEM;
  454. break;
  455. }
  456. skb->dev = entry->ldev;
  457. stat = netif_rx(skb);
  458. }
  459. rcu_read_unlock();
  460. kfree_skb(new);
  461. return stat;
  462. }
  463. static void lowpan_fragment_timer_expired(unsigned long entry_addr)
  464. {
  465. struct lowpan_fragment *entry = (struct lowpan_fragment *)entry_addr;
  466. pr_debug("%s: timer expired for frame with tag %d\n", __func__,
  467. entry->tag);
  468. spin_lock(&flist_lock);
  469. list_del(&entry->list);
  470. spin_unlock(&flist_lock);
  471. dev_kfree_skb(entry->skb);
  472. kfree(entry);
  473. }
  474. static int
  475. lowpan_process_data(struct sk_buff *skb)
  476. {
  477. struct ipv6hdr hdr;
  478. u8 tmp, iphc0, iphc1, num_context = 0;
  479. u8 *_saddr, *_daddr;
  480. int err;
  481. lowpan_raw_dump_table(__func__, "raw skb data dump", skb->data,
  482. skb->len);
  483. /* at least two bytes will be used for the encoding */
  484. if (skb->len < 2)
  485. goto drop;
  486. iphc0 = lowpan_fetch_skb_u8(skb);
  487. /* fragments assembling */
  488. switch (iphc0 & LOWPAN_DISPATCH_MASK) {
  489. case LOWPAN_DISPATCH_FRAG1:
  490. case LOWPAN_DISPATCH_FRAGN:
  491. {
  492. struct lowpan_fragment *frame;
  493. u8 len, offset;
  494. u16 tag;
  495. bool found = false;
  496. len = lowpan_fetch_skb_u8(skb); /* frame length */
  497. tag = lowpan_fetch_skb_u16(skb);
  498. /*
  499. * check if frame assembling with the same tag is
  500. * already in progress
  501. */
  502. spin_lock(&flist_lock);
  503. list_for_each_entry(frame, &lowpan_fragments, list)
  504. if (frame->tag == tag) {
  505. found = true;
  506. break;
  507. }
  508. /* alloc new frame structure */
  509. if (!found) {
  510. frame = kzalloc(sizeof(struct lowpan_fragment),
  511. GFP_ATOMIC);
  512. if (!frame)
  513. goto unlock_and_drop;
  514. INIT_LIST_HEAD(&frame->list);
  515. frame->length = (iphc0 & 7) | (len << 3);
  516. frame->tag = tag;
  517. /* allocate buffer for frame assembling */
  518. frame->skb = alloc_skb(frame->length +
  519. sizeof(struct ipv6hdr), GFP_ATOMIC);
  520. if (!frame->skb) {
  521. kfree(frame);
  522. goto unlock_and_drop;
  523. }
  524. frame->skb->priority = skb->priority;
  525. frame->skb->dev = skb->dev;
  526. /* reserve headroom for uncompressed ipv6 header */
  527. skb_reserve(frame->skb, sizeof(struct ipv6hdr));
  528. skb_put(frame->skb, frame->length);
  529. init_timer(&frame->timer);
  530. /* time out is the same as for ipv6 - 60 sec */
  531. frame->timer.expires = jiffies + LOWPAN_FRAG_TIMEOUT;
  532. frame->timer.data = (unsigned long)frame;
  533. frame->timer.function = lowpan_fragment_timer_expired;
  534. add_timer(&frame->timer);
  535. list_add_tail(&frame->list, &lowpan_fragments);
  536. }
  537. if ((iphc0 & LOWPAN_DISPATCH_MASK) == LOWPAN_DISPATCH_FRAG1)
  538. goto unlock_and_drop;
  539. offset = lowpan_fetch_skb_u8(skb); /* fetch offset */
  540. /* if payload fits buffer, copy it */
  541. if (likely((offset * 8 + skb->len) <= frame->length))
  542. skb_copy_to_linear_data_offset(frame->skb, offset * 8,
  543. skb->data, skb->len);
  544. else
  545. goto unlock_and_drop;
  546. frame->bytes_rcv += skb->len;
  547. /* frame assembling complete */
  548. if ((frame->bytes_rcv == frame->length) &&
  549. frame->timer.expires > jiffies) {
  550. /* if timer haven't expired - first of all delete it */
  551. del_timer(&frame->timer);
  552. list_del(&frame->list);
  553. spin_unlock(&flist_lock);
  554. dev_kfree_skb(skb);
  555. skb = frame->skb;
  556. kfree(frame);
  557. iphc0 = lowpan_fetch_skb_u8(skb);
  558. break;
  559. }
  560. spin_unlock(&flist_lock);
  561. return kfree_skb(skb), 0;
  562. }
  563. default:
  564. break;
  565. }
  566. iphc1 = lowpan_fetch_skb_u8(skb);
  567. _saddr = mac_cb(skb)->sa.hwaddr;
  568. _daddr = mac_cb(skb)->da.hwaddr;
  569. pr_debug("(%s): iphc0 = %02x, iphc1 = %02x\n", __func__, iphc0, iphc1);
  570. /* another if the CID flag is set */
  571. if (iphc1 & LOWPAN_IPHC_CID) {
  572. pr_debug("(%s): CID flag is set, increase header with one\n",
  573. __func__);
  574. if (!skb->len)
  575. goto drop;
  576. num_context = lowpan_fetch_skb_u8(skb);
  577. }
  578. hdr.version = 6;
  579. /* Traffic Class and Flow Label */
  580. switch ((iphc0 & LOWPAN_IPHC_TF) >> 3) {
  581. /*
  582. * Traffic Class and FLow Label carried in-line
  583. * ECN + DSCP + 4-bit Pad + Flow Label (4 bytes)
  584. */
  585. case 0: /* 00b */
  586. if (!skb->len)
  587. goto drop;
  588. tmp = lowpan_fetch_skb_u8(skb);
  589. memcpy(&hdr.flow_lbl, &skb->data[0], 3);
  590. skb_pull(skb, 3);
  591. hdr.priority = ((tmp >> 2) & 0x0f);
  592. hdr.flow_lbl[0] = ((tmp >> 2) & 0x30) | (tmp << 6) |
  593. (hdr.flow_lbl[0] & 0x0f);
  594. break;
  595. /*
  596. * Traffic class carried in-line
  597. * ECN + DSCP (1 byte), Flow Label is elided
  598. */
  599. case 1: /* 10b */
  600. if (!skb->len)
  601. goto drop;
  602. tmp = lowpan_fetch_skb_u8(skb);
  603. hdr.priority = ((tmp >> 2) & 0x0f);
  604. hdr.flow_lbl[0] = ((tmp << 6) & 0xC0) | ((tmp >> 2) & 0x30);
  605. hdr.flow_lbl[1] = 0;
  606. hdr.flow_lbl[2] = 0;
  607. break;
  608. /*
  609. * Flow Label carried in-line
  610. * ECN + 2-bit Pad + Flow Label (3 bytes), DSCP is elided
  611. */
  612. case 2: /* 01b */
  613. if (!skb->len)
  614. goto drop;
  615. tmp = lowpan_fetch_skb_u8(skb);
  616. hdr.flow_lbl[0] = (skb->data[0] & 0x0F) | ((tmp >> 2) & 0x30);
  617. memcpy(&hdr.flow_lbl[1], &skb->data[0], 2);
  618. skb_pull(skb, 2);
  619. break;
  620. /* Traffic Class and Flow Label are elided */
  621. case 3: /* 11b */
  622. hdr.priority = 0;
  623. hdr.flow_lbl[0] = 0;
  624. hdr.flow_lbl[1] = 0;
  625. hdr.flow_lbl[2] = 0;
  626. break;
  627. default:
  628. break;
  629. }
  630. /* Next Header */
  631. if ((iphc0 & LOWPAN_IPHC_NH_C) == 0) {
  632. /* Next header is carried inline */
  633. if (!skb->len)
  634. goto drop;
  635. hdr.nexthdr = lowpan_fetch_skb_u8(skb);
  636. pr_debug("(%s): NH flag is set, next header is carried "
  637. "inline: %02x\n", __func__, hdr.nexthdr);
  638. }
  639. /* Hop Limit */
  640. if ((iphc0 & 0x03) != LOWPAN_IPHC_TTL_I)
  641. hdr.hop_limit = lowpan_ttl_values[iphc0 & 0x03];
  642. else {
  643. if (!skb->len)
  644. goto drop;
  645. hdr.hop_limit = lowpan_fetch_skb_u8(skb);
  646. }
  647. /* Extract SAM to the tmp variable */
  648. tmp = ((iphc1 & LOWPAN_IPHC_SAM) >> LOWPAN_IPHC_SAM_BIT) & 0x03;
  649. /* Source address uncompression */
  650. pr_debug("(%s): source address stateless compression\n", __func__);
  651. err = lowpan_uncompress_addr(skb, &hdr.saddr, lowpan_llprefix,
  652. lowpan_unc_llconf[tmp], skb->data);
  653. if (err)
  654. goto drop;
  655. /* Extract DAM to the tmp variable */
  656. tmp = ((iphc1 & LOWPAN_IPHC_DAM_11) >> LOWPAN_IPHC_DAM_BIT) & 0x03;
  657. /* check for Multicast Compression */
  658. if (iphc1 & LOWPAN_IPHC_M) {
  659. if (iphc1 & LOWPAN_IPHC_DAC) {
  660. pr_debug("(%s): destination address context-based "
  661. "multicast compression\n", __func__);
  662. /* TODO: implement this */
  663. } else {
  664. u8 prefix[] = {0xff, 0x02};
  665. pr_debug("(%s): destination address non-context-based"
  666. " multicast compression\n", __func__);
  667. if (0 < tmp && tmp < 3) {
  668. if (!skb->len)
  669. goto drop;
  670. else
  671. prefix[1] = lowpan_fetch_skb_u8(skb);
  672. }
  673. err = lowpan_uncompress_addr(skb, &hdr.daddr, prefix,
  674. lowpan_unc_mxconf[tmp], NULL);
  675. if (err)
  676. goto drop;
  677. }
  678. } else {
  679. pr_debug("(%s): destination address stateless compression\n",
  680. __func__);
  681. err = lowpan_uncompress_addr(skb, &hdr.daddr, lowpan_llprefix,
  682. lowpan_unc_llconf[tmp], skb->data);
  683. if (err)
  684. goto drop;
  685. }
  686. /* TODO: UDP header parse */
  687. /* Not fragmented package */
  688. hdr.payload_len = htons(skb->len);
  689. pr_debug("(%s): skb headroom size = %d, data length = %d\n", __func__,
  690. skb_headroom(skb), skb->len);
  691. pr_debug("(%s): IPv6 header dump:\n\tversion = %d\n\tlength = %d\n\t"
  692. "nexthdr = 0x%02x\n\thop_lim = %d\n", __func__, hdr.version,
  693. ntohs(hdr.payload_len), hdr.nexthdr, hdr.hop_limit);
  694. lowpan_raw_dump_table(__func__, "raw header dump", (u8 *)&hdr,
  695. sizeof(hdr));
  696. return lowpan_skb_deliver(skb, &hdr);
  697. unlock_and_drop:
  698. spin_unlock(&flist_lock);
  699. drop:
  700. kfree_skb(skb);
  701. return -EINVAL;
  702. }
  703. static int lowpan_set_address(struct net_device *dev, void *p)
  704. {
  705. struct sockaddr *sa = p;
  706. if (netif_running(dev))
  707. return -EBUSY;
  708. /* TODO: validate addr */
  709. memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
  710. return 0;
  711. }
  712. static int lowpan_get_mac_header_length(struct sk_buff *skb)
  713. {
  714. /*
  715. * Currently long addressing mode is supported only, so the overall
  716. * header size is 21:
  717. * FC SeqNum DPAN DA SA Sec
  718. * 2 + 1 + 2 + 8 + 8 + 0 = 21
  719. */
  720. return 21;
  721. }
  722. static int
  723. lowpan_fragment_xmit(struct sk_buff *skb, u8 *head,
  724. int mlen, int plen, int offset)
  725. {
  726. struct sk_buff *frag;
  727. int hlen, ret;
  728. /* if payload length is zero, therefore it's a first fragment */
  729. hlen = (plen == 0 ? LOWPAN_FRAG1_HEAD_SIZE : LOWPAN_FRAGN_HEAD_SIZE);
  730. lowpan_raw_dump_inline(__func__, "6lowpan fragment header", head, hlen);
  731. frag = dev_alloc_skb(hlen + mlen + plen + IEEE802154_MFR_SIZE);
  732. if (!frag)
  733. return -ENOMEM;
  734. frag->priority = skb->priority;
  735. frag->dev = skb->dev;
  736. /* copy header, MFR and payload */
  737. memcpy(skb_put(frag, mlen), skb->data, mlen);
  738. memcpy(skb_put(frag, hlen), head, hlen);
  739. if (plen)
  740. skb_copy_from_linear_data_offset(skb, offset + mlen,
  741. skb_put(frag, plen), plen);
  742. lowpan_raw_dump_table(__func__, " raw fragment dump", frag->data,
  743. frag->len);
  744. ret = dev_queue_xmit(frag);
  745. if (ret < 0)
  746. dev_kfree_skb(frag);
  747. return ret;
  748. }
  749. static int
  750. lowpan_skb_fragmentation(struct sk_buff *skb)
  751. {
  752. int err, header_length, payload_length, tag, offset = 0;
  753. u8 head[5];
  754. header_length = lowpan_get_mac_header_length(skb);
  755. payload_length = skb->len - header_length;
  756. tag = fragment_tag++;
  757. /* first fragment header */
  758. head[0] = LOWPAN_DISPATCH_FRAG1 | (payload_length & 0x7);
  759. head[1] = (payload_length >> 3) & 0xff;
  760. head[2] = tag & 0xff;
  761. head[3] = tag >> 8;
  762. err = lowpan_fragment_xmit(skb, head, header_length, 0, 0);
  763. /* next fragment header */
  764. head[0] &= ~LOWPAN_DISPATCH_FRAG1;
  765. head[0] |= LOWPAN_DISPATCH_FRAGN;
  766. while ((payload_length - offset > 0) && (err >= 0)) {
  767. int len = LOWPAN_FRAG_SIZE;
  768. head[4] = offset / 8;
  769. if (payload_length - offset < len)
  770. len = payload_length - offset;
  771. err = lowpan_fragment_xmit(skb, head, header_length,
  772. len, offset);
  773. offset += len;
  774. }
  775. return err;
  776. }
  777. static netdev_tx_t lowpan_xmit(struct sk_buff *skb, struct net_device *dev)
  778. {
  779. int err = -1;
  780. pr_debug("(%s): package xmit\n", __func__);
  781. skb->dev = lowpan_dev_info(dev)->real_dev;
  782. if (skb->dev == NULL) {
  783. pr_debug("(%s) ERROR: no real wpan device found\n", __func__);
  784. goto error;
  785. }
  786. if (skb->len <= IEEE802154_MTU) {
  787. err = dev_queue_xmit(skb);
  788. goto out;
  789. }
  790. pr_debug("(%s): frame is too big, fragmentation is needed\n",
  791. __func__);
  792. err = lowpan_skb_fragmentation(skb);
  793. error:
  794. dev_kfree_skb(skb);
  795. out:
  796. if (err < 0)
  797. pr_debug("(%s): ERROR: xmit failed\n", __func__);
  798. return (err < 0 ? NETDEV_TX_BUSY : NETDEV_TX_OK);
  799. }
  800. static void lowpan_dev_free(struct net_device *dev)
  801. {
  802. dev_put(lowpan_dev_info(dev)->real_dev);
  803. free_netdev(dev);
  804. }
  805. static struct header_ops lowpan_header_ops = {
  806. .create = lowpan_header_create,
  807. };
  808. static const struct net_device_ops lowpan_netdev_ops = {
  809. .ndo_start_xmit = lowpan_xmit,
  810. .ndo_set_mac_address = lowpan_set_address,
  811. };
  812. static void lowpan_setup(struct net_device *dev)
  813. {
  814. pr_debug("(%s)\n", __func__);
  815. dev->addr_len = IEEE802154_ADDR_LEN;
  816. memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);
  817. dev->type = ARPHRD_IEEE802154;
  818. dev->features = NETIF_F_NO_CSUM;
  819. /* Frame Control + Sequence Number + Address fields + Security Header */
  820. dev->hard_header_len = 2 + 1 + 20 + 14;
  821. dev->needed_tailroom = 2; /* FCS */
  822. dev->mtu = 1281;
  823. dev->tx_queue_len = 0;
  824. dev->flags = IFF_NOARP | IFF_BROADCAST;
  825. dev->watchdog_timeo = 0;
  826. dev->netdev_ops = &lowpan_netdev_ops;
  827. dev->header_ops = &lowpan_header_ops;
  828. dev->destructor = lowpan_dev_free;
  829. }
  830. static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[])
  831. {
  832. pr_debug("(%s)\n", __func__);
  833. if (tb[IFLA_ADDRESS]) {
  834. if (nla_len(tb[IFLA_ADDRESS]) != IEEE802154_ADDR_LEN)
  835. return -EINVAL;
  836. }
  837. return 0;
  838. }
  839. static int lowpan_rcv(struct sk_buff *skb, struct net_device *dev,
  840. struct packet_type *pt, struct net_device *orig_dev)
  841. {
  842. if (!netif_running(dev))
  843. goto drop;
  844. if (dev->type != ARPHRD_IEEE802154)
  845. goto drop;
  846. /* check that it's our buffer */
  847. switch (skb->data[0] & 0xe0) {
  848. case LOWPAN_DISPATCH_IPHC: /* ipv6 datagram */
  849. case LOWPAN_DISPATCH_FRAG1: /* first fragment header */
  850. case LOWPAN_DISPATCH_FRAGN: /* next fragments headers */
  851. lowpan_process_data(skb);
  852. break;
  853. default:
  854. break;
  855. }
  856. return NET_RX_SUCCESS;
  857. drop:
  858. kfree_skb(skb);
  859. return NET_RX_DROP;
  860. }
  861. static int lowpan_newlink(struct net *src_net, struct net_device *dev,
  862. struct nlattr *tb[], struct nlattr *data[])
  863. {
  864. struct net_device *real_dev;
  865. struct lowpan_dev_record *entry;
  866. pr_debug("(%s)\n", __func__);
  867. if (!tb[IFLA_LINK])
  868. return -EINVAL;
  869. /* find and hold real wpan device */
  870. real_dev = dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
  871. if (!real_dev)
  872. return -ENODEV;
  873. lowpan_dev_info(dev)->real_dev = real_dev;
  874. mutex_init(&lowpan_dev_info(dev)->dev_list_mtx);
  875. entry = kzalloc(sizeof(struct lowpan_dev_record), GFP_KERNEL);
  876. if (!entry) {
  877. dev_put(real_dev);
  878. lowpan_dev_info(dev)->real_dev = NULL;
  879. return -ENOMEM;
  880. }
  881. entry->ldev = dev;
  882. mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
  883. INIT_LIST_HEAD(&entry->list);
  884. list_add_tail(&entry->list, &lowpan_devices);
  885. mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
  886. register_netdevice(dev);
  887. return 0;
  888. }
  889. static void lowpan_dellink(struct net_device *dev, struct list_head *head)
  890. {
  891. struct lowpan_dev_info *lowpan_dev = lowpan_dev_info(dev);
  892. struct net_device *real_dev = lowpan_dev->real_dev;
  893. struct lowpan_dev_record *entry;
  894. struct lowpan_dev_record *tmp;
  895. ASSERT_RTNL();
  896. mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
  897. list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) {
  898. if (entry->ldev == dev) {
  899. list_del(&entry->list);
  900. kfree(entry);
  901. }
  902. }
  903. mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
  904. mutex_destroy(&lowpan_dev_info(dev)->dev_list_mtx);
  905. unregister_netdevice_queue(dev, head);
  906. dev_put(real_dev);
  907. }
  908. static struct rtnl_link_ops lowpan_link_ops __read_mostly = {
  909. .kind = "lowpan",
  910. .priv_size = sizeof(struct lowpan_dev_info),
  911. .setup = lowpan_setup,
  912. .newlink = lowpan_newlink,
  913. .dellink = lowpan_dellink,
  914. .validate = lowpan_validate,
  915. };
  916. static inline int __init lowpan_netlink_init(void)
  917. {
  918. return rtnl_link_register(&lowpan_link_ops);
  919. }
  920. static inline void __init lowpan_netlink_fini(void)
  921. {
  922. rtnl_link_unregister(&lowpan_link_ops);
  923. }
  924. static struct packet_type lowpan_packet_type = {
  925. .type = __constant_htons(ETH_P_IEEE802154),
  926. .func = lowpan_rcv,
  927. };
  928. static int __init lowpan_init_module(void)
  929. {
  930. int err = 0;
  931. pr_debug("(%s)\n", __func__);
  932. err = lowpan_netlink_init();
  933. if (err < 0)
  934. goto out;
  935. dev_add_pack(&lowpan_packet_type);
  936. out:
  937. return err;
  938. }
  939. static void __exit lowpan_cleanup_module(void)
  940. {
  941. pr_debug("(%s)\n", __func__);
  942. lowpan_netlink_fini();
  943. dev_remove_pack(&lowpan_packet_type);
  944. }
  945. module_init(lowpan_init_module);
  946. module_exit(lowpan_cleanup_module);
  947. MODULE_LICENSE("GPL");
  948. MODULE_ALIAS_RTNL_LINK("lowpan");