6lowpan.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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. static inline struct
  103. lowpan_dev_info *lowpan_dev_info(const struct net_device *dev)
  104. {
  105. return netdev_priv(dev);
  106. }
  107. static inline void lowpan_address_flip(u8 *src, u8 *dest)
  108. {
  109. int i;
  110. for (i = 0; i < IEEE802154_ADDR_LEN; i++)
  111. (dest)[IEEE802154_ADDR_LEN - i - 1] = (src)[i];
  112. }
  113. /* list of all 6lowpan devices, uses for package delivering */
  114. /* print data in line */
  115. static inline void lowpan_raw_dump_inline(const char *caller, char *msg,
  116. unsigned char *buf, int len)
  117. {
  118. #ifdef DEBUG
  119. if (msg)
  120. pr_debug("(%s) %s: ", caller, msg);
  121. print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_NONE,
  122. 16, 1, buf, len, false);
  123. #endif /* DEBUG */
  124. }
  125. /*
  126. * print data in a table format:
  127. *
  128. * addr: xx xx xx xx xx xx
  129. * addr: xx xx xx xx xx xx
  130. * ...
  131. */
  132. static inline void lowpan_raw_dump_table(const char *caller, char *msg,
  133. unsigned char *buf, int len)
  134. {
  135. #ifdef DEBUG
  136. if (msg)
  137. pr_debug("(%s) %s:\n", caller, msg);
  138. print_hex_dump(KERN_DEBUG, "\t", DUMP_PREFIX_OFFSET,
  139. 16, 1, buf, len, false);
  140. #endif /* DEBUG */
  141. }
  142. static u8
  143. lowpan_compress_addr_64(u8 **hc06_ptr, u8 shift, const struct in6_addr *ipaddr,
  144. const unsigned char *lladdr)
  145. {
  146. u8 val = 0;
  147. if (is_addr_mac_addr_based(ipaddr, lladdr))
  148. val = 3; /* 0-bits */
  149. else if (lowpan_is_iid_16_bit_compressable(ipaddr)) {
  150. /* compress IID to 16 bits xxxx::XXXX */
  151. memcpy(*hc06_ptr, &ipaddr->s6_addr16[7], 2);
  152. *hc06_ptr += 2;
  153. val = 2; /* 16-bits */
  154. } else {
  155. /* do not compress IID => xxxx::IID */
  156. memcpy(*hc06_ptr, &ipaddr->s6_addr16[4], 8);
  157. *hc06_ptr += 8;
  158. val = 1; /* 64-bits */
  159. }
  160. return rol8(val, shift);
  161. }
  162. static void
  163. lowpan_uip_ds6_set_addr_iid(struct in6_addr *ipaddr, unsigned char *lladdr)
  164. {
  165. memcpy(&ipaddr->s6_addr[8], lladdr, IEEE802154_ALEN);
  166. /* second bit-flip (Universe/Local) is done according RFC2464 */
  167. ipaddr->s6_addr[8] ^= 0x02;
  168. }
  169. /*
  170. * Uncompress addresses based on a prefix and a postfix with zeroes in
  171. * between. If the postfix is zero in length it will use the link address
  172. * to configure the IP address (autoconf style).
  173. * pref_post_count takes a byte where the first nibble specify prefix count
  174. * and the second postfix count (NOTE: 15/0xf => 16 bytes copy).
  175. */
  176. static int
  177. lowpan_uncompress_addr(struct sk_buff *skb, struct in6_addr *ipaddr,
  178. u8 const *prefix, u8 pref_post_count, unsigned char *lladdr)
  179. {
  180. u8 prefcount = pref_post_count >> 4;
  181. u8 postcount = pref_post_count & 0x0f;
  182. /* full nibble 15 => 16 */
  183. prefcount = (prefcount == 15 ? 16 : prefcount);
  184. postcount = (postcount == 15 ? 16 : postcount);
  185. if (lladdr)
  186. lowpan_raw_dump_inline(__func__, "linklocal address",
  187. lladdr, IEEE802154_ALEN);
  188. if (prefcount > 0)
  189. memcpy(ipaddr, prefix, prefcount);
  190. if (prefcount + postcount < 16)
  191. memset(&ipaddr->s6_addr[prefcount], 0,
  192. 16 - (prefcount + postcount));
  193. if (postcount > 0) {
  194. memcpy(&ipaddr->s6_addr[16 - postcount], skb->data, postcount);
  195. skb_pull(skb, postcount);
  196. } else if (prefcount > 0) {
  197. if (lladdr == NULL)
  198. return -EINVAL;
  199. /* no IID based configuration if no prefix and no data */
  200. lowpan_uip_ds6_set_addr_iid(ipaddr, lladdr);
  201. }
  202. pr_debug("(%s): uncompressing %d + %d => ", __func__, prefcount,
  203. postcount);
  204. lowpan_raw_dump_inline(NULL, NULL, ipaddr->s6_addr, 16);
  205. return 0;
  206. }
  207. static u8 lowpan_fetch_skb_u8(struct sk_buff *skb)
  208. {
  209. u8 ret;
  210. ret = skb->data[0];
  211. skb_pull(skb, 1);
  212. return ret;
  213. }
  214. static int lowpan_header_create(struct sk_buff *skb,
  215. struct net_device *dev,
  216. unsigned short type, const void *_daddr,
  217. const void *_saddr, unsigned len)
  218. {
  219. u8 tmp, iphc0, iphc1, *hc06_ptr;
  220. struct ipv6hdr *hdr;
  221. const u8 *saddr = _saddr;
  222. const u8 *daddr = _daddr;
  223. u8 *head;
  224. struct ieee802154_addr sa, da;
  225. if (type != ETH_P_IPV6)
  226. return 0;
  227. /* TODO:
  228. * if this package isn't ipv6 one, where should it be routed?
  229. */
  230. head = kzalloc(100, GFP_KERNEL);
  231. if (head == NULL)
  232. return -ENOMEM;
  233. hdr = ipv6_hdr(skb);
  234. hc06_ptr = head + 2;
  235. pr_debug("(%s): IPv6 header dump:\n\tversion = %d\n\tlength = %d\n"
  236. "\tnexthdr = 0x%02x\n\thop_lim = %d\n", __func__,
  237. hdr->version, ntohs(hdr->payload_len), hdr->nexthdr,
  238. hdr->hop_limit);
  239. lowpan_raw_dump_table(__func__, "raw skb network header dump",
  240. skb_network_header(skb), sizeof(struct ipv6hdr));
  241. if (!saddr)
  242. saddr = dev->dev_addr;
  243. lowpan_raw_dump_inline(__func__, "saddr", (unsigned char *)saddr, 8);
  244. /*
  245. * As we copy some bit-length fields, in the IPHC encoding bytes,
  246. * we sometimes use |=
  247. * If the field is 0, and the current bit value in memory is 1,
  248. * this does not work. We therefore reset the IPHC encoding here
  249. */
  250. iphc0 = LOWPAN_DISPATCH_IPHC;
  251. iphc1 = 0;
  252. /* TODO: context lookup */
  253. lowpan_raw_dump_inline(__func__, "daddr", (unsigned char *)daddr, 8);
  254. /*
  255. * Traffic class, flow label
  256. * If flow label is 0, compress it. If traffic class is 0, compress it
  257. * We have to process both in the same time as the offset of traffic
  258. * class depends on the presence of version and flow label
  259. */
  260. /* hc06 format of TC is ECN | DSCP , original one is DSCP | ECN */
  261. tmp = (hdr->priority << 4) | (hdr->flow_lbl[0] >> 4);
  262. tmp = ((tmp & 0x03) << 6) | (tmp >> 2);
  263. if (((hdr->flow_lbl[0] & 0x0F) == 0) &&
  264. (hdr->flow_lbl[1] == 0) && (hdr->flow_lbl[2] == 0)) {
  265. /* flow label can be compressed */
  266. iphc0 |= LOWPAN_IPHC_FL_C;
  267. if ((hdr->priority == 0) &&
  268. ((hdr->flow_lbl[0] & 0xF0) == 0)) {
  269. /* compress (elide) all */
  270. iphc0 |= LOWPAN_IPHC_TC_C;
  271. } else {
  272. /* compress only the flow label */
  273. *hc06_ptr = tmp;
  274. hc06_ptr += 1;
  275. }
  276. } else {
  277. /* Flow label cannot be compressed */
  278. if ((hdr->priority == 0) &&
  279. ((hdr->flow_lbl[0] & 0xF0) == 0)) {
  280. /* compress only traffic class */
  281. iphc0 |= LOWPAN_IPHC_TC_C;
  282. *hc06_ptr = (tmp & 0xc0) | (hdr->flow_lbl[0] & 0x0F);
  283. memcpy(hc06_ptr + 1, &hdr->flow_lbl[1], 2);
  284. hc06_ptr += 3;
  285. } else {
  286. /* compress nothing */
  287. memcpy(hc06_ptr, &hdr, 4);
  288. /* replace the top byte with new ECN | DSCP format */
  289. *hc06_ptr = tmp;
  290. hc06_ptr += 4;
  291. }
  292. }
  293. /* NOTE: payload length is always compressed */
  294. /* Next Header is compress if UDP */
  295. if (hdr->nexthdr == UIP_PROTO_UDP)
  296. iphc0 |= LOWPAN_IPHC_NH_C;
  297. /* TODO: next header compression */
  298. if ((iphc0 & LOWPAN_IPHC_NH_C) == 0) {
  299. *hc06_ptr = hdr->nexthdr;
  300. hc06_ptr += 1;
  301. }
  302. /*
  303. * Hop limit
  304. * if 1: compress, encoding is 01
  305. * if 64: compress, encoding is 10
  306. * if 255: compress, encoding is 11
  307. * else do not compress
  308. */
  309. switch (hdr->hop_limit) {
  310. case 1:
  311. iphc0 |= LOWPAN_IPHC_TTL_1;
  312. break;
  313. case 64:
  314. iphc0 |= LOWPAN_IPHC_TTL_64;
  315. break;
  316. case 255:
  317. iphc0 |= LOWPAN_IPHC_TTL_255;
  318. break;
  319. default:
  320. *hc06_ptr = hdr->hop_limit;
  321. break;
  322. }
  323. /* source address compression */
  324. if (is_addr_unspecified(&hdr->saddr)) {
  325. pr_debug("(%s): source address is unspecified, setting SAC\n",
  326. __func__);
  327. iphc1 |= LOWPAN_IPHC_SAC;
  328. /* TODO: context lookup */
  329. } else if (is_addr_link_local(&hdr->saddr)) {
  330. pr_debug("(%s): source address is link-local\n", __func__);
  331. iphc1 |= lowpan_compress_addr_64(&hc06_ptr,
  332. LOWPAN_IPHC_SAM_BIT, &hdr->saddr, saddr);
  333. } else {
  334. pr_debug("(%s): send the full source address\n", __func__);
  335. memcpy(hc06_ptr, &hdr->saddr.s6_addr16[0], 16);
  336. hc06_ptr += 16;
  337. }
  338. /* destination address compression */
  339. if (is_addr_mcast(&hdr->daddr)) {
  340. pr_debug("(%s): destination address is multicast", __func__);
  341. iphc1 |= LOWPAN_IPHC_M;
  342. if (lowpan_is_mcast_addr_compressable8(&hdr->daddr)) {
  343. pr_debug("compressed to 1 octet\n");
  344. iphc1 |= LOWPAN_IPHC_DAM_11;
  345. /* use last byte */
  346. *hc06_ptr = hdr->daddr.s6_addr[15];
  347. hc06_ptr += 1;
  348. } else if (lowpan_is_mcast_addr_compressable32(&hdr->daddr)) {
  349. pr_debug("compressed to 4 octets\n");
  350. iphc1 |= LOWPAN_IPHC_DAM_10;
  351. /* second byte + the last three */
  352. *hc06_ptr = hdr->daddr.s6_addr[1];
  353. memcpy(hc06_ptr + 1, &hdr->daddr.s6_addr[13], 3);
  354. hc06_ptr += 4;
  355. } else if (lowpan_is_mcast_addr_compressable48(&hdr->daddr)) {
  356. pr_debug("compressed to 6 octets\n");
  357. iphc1 |= LOWPAN_IPHC_DAM_01;
  358. /* second byte + the last five */
  359. *hc06_ptr = hdr->daddr.s6_addr[1];
  360. memcpy(hc06_ptr + 1, &hdr->daddr.s6_addr[11], 5);
  361. hc06_ptr += 6;
  362. } else {
  363. pr_debug("using full address\n");
  364. iphc1 |= LOWPAN_IPHC_DAM_00;
  365. memcpy(hc06_ptr, &hdr->daddr.s6_addr[0], 16);
  366. hc06_ptr += 16;
  367. }
  368. } else {
  369. pr_debug("(%s): destination address is unicast: ", __func__);
  370. /* TODO: context lookup */
  371. if (is_addr_link_local(&hdr->daddr)) {
  372. pr_debug("destination address is link-local\n");
  373. iphc1 |= lowpan_compress_addr_64(&hc06_ptr,
  374. LOWPAN_IPHC_DAM_BIT, &hdr->daddr, daddr);
  375. } else {
  376. pr_debug("using full address\n");
  377. memcpy(hc06_ptr, &hdr->daddr.s6_addr16[0], 16);
  378. hc06_ptr += 16;
  379. }
  380. }
  381. /* TODO: UDP header compression */
  382. /* TODO: Next Header compression */
  383. head[0] = iphc0;
  384. head[1] = iphc1;
  385. skb_pull(skb, sizeof(struct ipv6hdr));
  386. memcpy(skb_push(skb, hc06_ptr - head), head, hc06_ptr - head);
  387. kfree(head);
  388. lowpan_raw_dump_table(__func__, "raw skb data dump", skb->data,
  389. skb->len);
  390. /*
  391. * NOTE1: I'm still unsure about the fact that compression and WPAN
  392. * header are created here and not later in the xmit. So wait for
  393. * an opinion of net maintainers.
  394. */
  395. /*
  396. * NOTE2: to be absolutely correct, we must derive PANid information
  397. * from MAC subif of the 'dev' and 'real_dev' network devices, but
  398. * this isn't implemented in mainline yet, so currently we assign 0xff
  399. */
  400. {
  401. /* prepare wpan address data */
  402. sa.addr_type = IEEE802154_ADDR_LONG;
  403. sa.pan_id = 0xff;
  404. da.addr_type = IEEE802154_ADDR_LONG;
  405. da.pan_id = 0xff;
  406. memcpy(&(da.hwaddr), daddr, 8);
  407. memcpy(&(sa.hwaddr), saddr, 8);
  408. mac_cb(skb)->flags = IEEE802154_FC_TYPE_DATA;
  409. return dev_hard_header(skb, lowpan_dev_info(dev)->real_dev,
  410. type, (void *)&da, (void *)&sa, skb->len);
  411. }
  412. }
  413. static int lowpan_skb_deliver(struct sk_buff *skb, struct ipv6hdr *hdr)
  414. {
  415. struct sk_buff *new;
  416. struct lowpan_dev_record *entry;
  417. int stat = NET_RX_SUCCESS;
  418. new = skb_copy_expand(skb, sizeof(struct ipv6hdr), skb_tailroom(skb),
  419. GFP_KERNEL);
  420. kfree_skb(skb);
  421. if (NULL == new)
  422. return -ENOMEM;
  423. skb_push(new, sizeof(struct ipv6hdr));
  424. skb_reset_network_header(new);
  425. skb_copy_to_linear_data(new, hdr, sizeof(struct ipv6hdr));
  426. new->protocol = htons(ETH_P_IPV6);
  427. new->pkt_type = PACKET_HOST;
  428. rcu_read_lock();
  429. list_for_each_entry_rcu(entry, &lowpan_devices, list)
  430. if (lowpan_dev_info(entry->ldev)->real_dev == new->dev) {
  431. skb = skb_copy(new, GFP_KERNEL);
  432. skb->dev = entry->ldev;
  433. if (in_interrupt())
  434. stat = netif_rx(skb);
  435. else
  436. stat = netif_rx_ni(skb);
  437. }
  438. rcu_read_unlock();
  439. kfree_skb(new);
  440. return stat;
  441. }
  442. static int
  443. lowpan_process_data(struct sk_buff *skb)
  444. {
  445. struct ipv6hdr hdr;
  446. u8 tmp, iphc0, iphc1, num_context = 0;
  447. u8 *_saddr, *_daddr;
  448. int err;
  449. lowpan_raw_dump_table(__func__, "raw skb data dump", skb->data,
  450. skb->len);
  451. /* at least two bytes will be used for the encoding */
  452. if (skb->len < 2)
  453. goto drop;
  454. iphc0 = lowpan_fetch_skb_u8(skb);
  455. iphc1 = lowpan_fetch_skb_u8(skb);
  456. _saddr = mac_cb(skb)->sa.hwaddr;
  457. _daddr = mac_cb(skb)->da.hwaddr;
  458. pr_debug("(%s): iphc0 = %02x, iphc1 = %02x\n", __func__, iphc0, iphc1);
  459. /* another if the CID flag is set */
  460. if (iphc1 & LOWPAN_IPHC_CID) {
  461. pr_debug("(%s): CID flag is set, increase header with one\n",
  462. __func__);
  463. if (!skb->len)
  464. goto drop;
  465. num_context = lowpan_fetch_skb_u8(skb);
  466. }
  467. hdr.version = 6;
  468. /* Traffic Class and Flow Label */
  469. switch ((iphc0 & LOWPAN_IPHC_TF) >> 3) {
  470. /*
  471. * Traffic Class and FLow Label carried in-line
  472. * ECN + DSCP + 4-bit Pad + Flow Label (4 bytes)
  473. */
  474. case 0: /* 00b */
  475. if (!skb->len)
  476. goto drop;
  477. tmp = lowpan_fetch_skb_u8(skb);
  478. memcpy(&hdr.flow_lbl, &skb->data[0], 3);
  479. skb_pull(skb, 3);
  480. hdr.priority = ((tmp >> 2) & 0x0f);
  481. hdr.flow_lbl[0] = ((tmp >> 2) & 0x30) | (tmp << 6) |
  482. (hdr.flow_lbl[0] & 0x0f);
  483. break;
  484. /*
  485. * Traffic class carried in-line
  486. * ECN + DSCP (1 byte), Flow Label is elided
  487. */
  488. case 1: /* 10b */
  489. if (!skb->len)
  490. goto drop;
  491. tmp = lowpan_fetch_skb_u8(skb);
  492. hdr.priority = ((tmp >> 2) & 0x0f);
  493. hdr.flow_lbl[0] = ((tmp << 6) & 0xC0) | ((tmp >> 2) & 0x30);
  494. hdr.flow_lbl[1] = 0;
  495. hdr.flow_lbl[2] = 0;
  496. break;
  497. /*
  498. * Flow Label carried in-line
  499. * ECN + 2-bit Pad + Flow Label (3 bytes), DSCP is elided
  500. */
  501. case 2: /* 01b */
  502. if (!skb->len)
  503. goto drop;
  504. tmp = lowpan_fetch_skb_u8(skb);
  505. hdr.flow_lbl[0] = (skb->data[0] & 0x0F) | ((tmp >> 2) & 0x30);
  506. memcpy(&hdr.flow_lbl[1], &skb->data[0], 2);
  507. skb_pull(skb, 2);
  508. break;
  509. /* Traffic Class and Flow Label are elided */
  510. case 3: /* 11b */
  511. hdr.priority = 0;
  512. hdr.flow_lbl[0] = 0;
  513. hdr.flow_lbl[1] = 0;
  514. hdr.flow_lbl[2] = 0;
  515. break;
  516. default:
  517. break;
  518. }
  519. /* Next Header */
  520. if ((iphc0 & LOWPAN_IPHC_NH_C) == 0) {
  521. /* Next header is carried inline */
  522. if (!skb->len)
  523. goto drop;
  524. hdr.nexthdr = lowpan_fetch_skb_u8(skb);
  525. pr_debug("(%s): NH flag is set, next header is carried "
  526. "inline: %02x\n", __func__, hdr.nexthdr);
  527. }
  528. /* Hop Limit */
  529. if ((iphc0 & 0x03) != LOWPAN_IPHC_TTL_I)
  530. hdr.hop_limit = lowpan_ttl_values[iphc0 & 0x03];
  531. else {
  532. if (!skb->len)
  533. goto drop;
  534. hdr.hop_limit = lowpan_fetch_skb_u8(skb);
  535. }
  536. /* Extract SAM to the tmp variable */
  537. tmp = ((iphc1 & LOWPAN_IPHC_SAM) >> LOWPAN_IPHC_SAM_BIT) & 0x03;
  538. /* Source address uncompression */
  539. pr_debug("(%s): source address stateless compression\n", __func__);
  540. err = lowpan_uncompress_addr(skb, &hdr.saddr, lowpan_llprefix,
  541. lowpan_unc_llconf[tmp], skb->data);
  542. if (err)
  543. goto drop;
  544. /* Extract DAM to the tmp variable */
  545. tmp = ((iphc1 & LOWPAN_IPHC_DAM_11) >> LOWPAN_IPHC_DAM_BIT) & 0x03;
  546. /* check for Multicast Compression */
  547. if (iphc1 & LOWPAN_IPHC_M) {
  548. if (iphc1 & LOWPAN_IPHC_DAC) {
  549. pr_debug("(%s): destination address context-based "
  550. "multicast compression\n", __func__);
  551. /* TODO: implement this */
  552. } else {
  553. u8 prefix[] = {0xff, 0x02};
  554. pr_debug("(%s): destination address non-context-based"
  555. " multicast compression\n", __func__);
  556. if (0 < tmp && tmp < 3) {
  557. if (!skb->len)
  558. goto drop;
  559. else
  560. prefix[1] = lowpan_fetch_skb_u8(skb);
  561. }
  562. err = lowpan_uncompress_addr(skb, &hdr.daddr, prefix,
  563. lowpan_unc_mxconf[tmp], NULL);
  564. if (err)
  565. goto drop;
  566. }
  567. } else {
  568. pr_debug("(%s): destination address stateless compression\n",
  569. __func__);
  570. err = lowpan_uncompress_addr(skb, &hdr.daddr, lowpan_llprefix,
  571. lowpan_unc_llconf[tmp], skb->data);
  572. if (err)
  573. goto drop;
  574. }
  575. /* TODO: UDP header parse */
  576. /* Not fragmented package */
  577. hdr.payload_len = htons(skb->len);
  578. pr_debug("(%s): skb headroom size = %d, data length = %d\n", __func__,
  579. skb_headroom(skb), skb->len);
  580. pr_debug("(%s): IPv6 header dump:\n\tversion = %d\n\tlength = %d\n\t"
  581. "nexthdr = 0x%02x\n\thop_lim = %d\n", __func__, hdr.version,
  582. ntohs(hdr.payload_len), hdr.nexthdr, hdr.hop_limit);
  583. lowpan_raw_dump_table(__func__, "raw header dump", (u8 *)&hdr,
  584. sizeof(hdr));
  585. return lowpan_skb_deliver(skb, &hdr);
  586. drop:
  587. kfree(skb);
  588. return -EINVAL;
  589. }
  590. static int lowpan_set_address(struct net_device *dev, void *p)
  591. {
  592. struct sockaddr *sa = p;
  593. if (netif_running(dev))
  594. return -EBUSY;
  595. /* TODO: validate addr */
  596. memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
  597. return 0;
  598. }
  599. static netdev_tx_t lowpan_xmit(struct sk_buff *skb, struct net_device *dev)
  600. {
  601. int err = 0;
  602. pr_debug("(%s): package xmit\n", __func__);
  603. skb->dev = lowpan_dev_info(dev)->real_dev;
  604. if (skb->dev == NULL) {
  605. pr_debug("(%s) ERROR: no real wpan device found\n", __func__);
  606. dev_kfree_skb(skb);
  607. } else
  608. err = dev_queue_xmit(skb);
  609. return (err < 0 ? NETDEV_TX_BUSY : NETDEV_TX_OK);
  610. }
  611. static void lowpan_dev_free(struct net_device *dev)
  612. {
  613. dev_put(lowpan_dev_info(dev)->real_dev);
  614. free_netdev(dev);
  615. }
  616. static struct header_ops lowpan_header_ops = {
  617. .create = lowpan_header_create,
  618. };
  619. static const struct net_device_ops lowpan_netdev_ops = {
  620. .ndo_start_xmit = lowpan_xmit,
  621. .ndo_set_mac_address = lowpan_set_address,
  622. };
  623. static void lowpan_setup(struct net_device *dev)
  624. {
  625. pr_debug("(%s)\n", __func__);
  626. dev->addr_len = IEEE802154_ADDR_LEN;
  627. memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);
  628. dev->type = ARPHRD_IEEE802154;
  629. dev->features = NETIF_F_NO_CSUM;
  630. /* Frame Control + Sequence Number + Address fields + Security Header */
  631. dev->hard_header_len = 2 + 1 + 20 + 14;
  632. dev->needed_tailroom = 2; /* FCS */
  633. dev->mtu = 1281;
  634. dev->tx_queue_len = 0;
  635. dev->flags = IFF_NOARP | IFF_BROADCAST;
  636. dev->watchdog_timeo = 0;
  637. dev->netdev_ops = &lowpan_netdev_ops;
  638. dev->header_ops = &lowpan_header_ops;
  639. dev->destructor = lowpan_dev_free;
  640. }
  641. static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[])
  642. {
  643. pr_debug("(%s)\n", __func__);
  644. if (tb[IFLA_ADDRESS]) {
  645. if (nla_len(tb[IFLA_ADDRESS]) != IEEE802154_ADDR_LEN)
  646. return -EINVAL;
  647. }
  648. return 0;
  649. }
  650. static int lowpan_rcv(struct sk_buff *skb, struct net_device *dev,
  651. struct packet_type *pt, struct net_device *orig_dev)
  652. {
  653. if (!netif_running(dev))
  654. goto drop;
  655. if (dev->type != ARPHRD_IEEE802154)
  656. goto drop;
  657. /* check that it's our buffer */
  658. if ((skb->data[0] & 0xe0) == 0x60)
  659. lowpan_process_data(skb);
  660. return NET_RX_SUCCESS;
  661. drop:
  662. kfree_skb(skb);
  663. return NET_RX_DROP;
  664. }
  665. static int lowpan_newlink(struct net *src_net, struct net_device *dev,
  666. struct nlattr *tb[], struct nlattr *data[])
  667. {
  668. struct net_device *real_dev;
  669. struct lowpan_dev_record *entry;
  670. pr_debug("(%s)\n", __func__);
  671. if (!tb[IFLA_LINK])
  672. return -EINVAL;
  673. /* find and hold real wpan device */
  674. real_dev = dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
  675. if (!real_dev)
  676. return -ENODEV;
  677. lowpan_dev_info(dev)->real_dev = real_dev;
  678. mutex_init(&lowpan_dev_info(dev)->dev_list_mtx);
  679. entry = kzalloc(sizeof(struct lowpan_dev_record), GFP_KERNEL);
  680. if (!entry)
  681. return -ENOMEM;
  682. entry->ldev = dev;
  683. mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
  684. INIT_LIST_HEAD(&entry->list);
  685. list_add_tail(&entry->list, &lowpan_devices);
  686. mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
  687. register_netdevice(dev);
  688. return 0;
  689. }
  690. static void lowpan_dellink(struct net_device *dev, struct list_head *head)
  691. {
  692. struct lowpan_dev_info *lowpan_dev = lowpan_dev_info(dev);
  693. struct net_device *real_dev = lowpan_dev->real_dev;
  694. struct lowpan_dev_record *entry;
  695. ASSERT_RTNL();
  696. mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
  697. list_for_each_entry(entry, &lowpan_devices, list)
  698. if (entry->ldev == dev) {
  699. list_del(&entry->list);
  700. kfree(entry);
  701. }
  702. mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
  703. mutex_destroy(&lowpan_dev_info(dev)->dev_list_mtx);
  704. unregister_netdevice_queue(dev, head);
  705. dev_put(real_dev);
  706. }
  707. static struct rtnl_link_ops lowpan_link_ops __read_mostly = {
  708. .kind = "lowpan",
  709. .priv_size = sizeof(struct lowpan_dev_info),
  710. .setup = lowpan_setup,
  711. .newlink = lowpan_newlink,
  712. .dellink = lowpan_dellink,
  713. .validate = lowpan_validate,
  714. };
  715. static inline int __init lowpan_netlink_init(void)
  716. {
  717. return rtnl_link_register(&lowpan_link_ops);
  718. }
  719. static inline void __init lowpan_netlink_fini(void)
  720. {
  721. rtnl_link_unregister(&lowpan_link_ops);
  722. }
  723. static struct packet_type lowpan_packet_type = {
  724. .type = __constant_htons(ETH_P_IEEE802154),
  725. .func = lowpan_rcv,
  726. };
  727. static int __init lowpan_init_module(void)
  728. {
  729. int err = 0;
  730. pr_debug("(%s)\n", __func__);
  731. err = lowpan_netlink_init();
  732. if (err < 0)
  733. goto out;
  734. dev_add_pack(&lowpan_packet_type);
  735. out:
  736. return err;
  737. }
  738. static void __exit lowpan_cleanup_module(void)
  739. {
  740. pr_debug("(%s)\n", __func__);
  741. lowpan_netlink_fini();
  742. dev_remove_pack(&lowpan_packet_type);
  743. }
  744. module_init(lowpan_init_module);
  745. module_exit(lowpan_cleanup_module);
  746. MODULE_LICENSE("GPL");
  747. MODULE_ALIAS_RTNL_LINK("lowpan");