6lowpan.c 29 KB

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