6lowpan.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  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_ATOMIC);
  420. kfree_skb(skb);
  421. if (!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_ATOMIC);
  432. if (!skb) {
  433. stat = -ENOMEM;
  434. break;
  435. }
  436. skb->dev = entry->ldev;
  437. stat = netif_rx(skb);
  438. }
  439. rcu_read_unlock();
  440. kfree_skb(new);
  441. return stat;
  442. }
  443. static int
  444. lowpan_process_data(struct sk_buff *skb)
  445. {
  446. struct ipv6hdr hdr;
  447. u8 tmp, iphc0, iphc1, num_context = 0;
  448. u8 *_saddr, *_daddr;
  449. int err;
  450. lowpan_raw_dump_table(__func__, "raw skb data dump", skb->data,
  451. skb->len);
  452. /* at least two bytes will be used for the encoding */
  453. if (skb->len < 2)
  454. goto drop;
  455. iphc0 = lowpan_fetch_skb_u8(skb);
  456. iphc1 = lowpan_fetch_skb_u8(skb);
  457. _saddr = mac_cb(skb)->sa.hwaddr;
  458. _daddr = mac_cb(skb)->da.hwaddr;
  459. pr_debug("(%s): iphc0 = %02x, iphc1 = %02x\n", __func__, iphc0, iphc1);
  460. /* another if the CID flag is set */
  461. if (iphc1 & LOWPAN_IPHC_CID) {
  462. pr_debug("(%s): CID flag is set, increase header with one\n",
  463. __func__);
  464. if (!skb->len)
  465. goto drop;
  466. num_context = lowpan_fetch_skb_u8(skb);
  467. }
  468. hdr.version = 6;
  469. /* Traffic Class and Flow Label */
  470. switch ((iphc0 & LOWPAN_IPHC_TF) >> 3) {
  471. /*
  472. * Traffic Class and FLow Label carried in-line
  473. * ECN + DSCP + 4-bit Pad + Flow Label (4 bytes)
  474. */
  475. case 0: /* 00b */
  476. if (!skb->len)
  477. goto drop;
  478. tmp = lowpan_fetch_skb_u8(skb);
  479. memcpy(&hdr.flow_lbl, &skb->data[0], 3);
  480. skb_pull(skb, 3);
  481. hdr.priority = ((tmp >> 2) & 0x0f);
  482. hdr.flow_lbl[0] = ((tmp >> 2) & 0x30) | (tmp << 6) |
  483. (hdr.flow_lbl[0] & 0x0f);
  484. break;
  485. /*
  486. * Traffic class carried in-line
  487. * ECN + DSCP (1 byte), Flow Label is elided
  488. */
  489. case 1: /* 10b */
  490. if (!skb->len)
  491. goto drop;
  492. tmp = lowpan_fetch_skb_u8(skb);
  493. hdr.priority = ((tmp >> 2) & 0x0f);
  494. hdr.flow_lbl[0] = ((tmp << 6) & 0xC0) | ((tmp >> 2) & 0x30);
  495. hdr.flow_lbl[1] = 0;
  496. hdr.flow_lbl[2] = 0;
  497. break;
  498. /*
  499. * Flow Label carried in-line
  500. * ECN + 2-bit Pad + Flow Label (3 bytes), DSCP is elided
  501. */
  502. case 2: /* 01b */
  503. if (!skb->len)
  504. goto drop;
  505. tmp = lowpan_fetch_skb_u8(skb);
  506. hdr.flow_lbl[0] = (skb->data[0] & 0x0F) | ((tmp >> 2) & 0x30);
  507. memcpy(&hdr.flow_lbl[1], &skb->data[0], 2);
  508. skb_pull(skb, 2);
  509. break;
  510. /* Traffic Class and Flow Label are elided */
  511. case 3: /* 11b */
  512. hdr.priority = 0;
  513. hdr.flow_lbl[0] = 0;
  514. hdr.flow_lbl[1] = 0;
  515. hdr.flow_lbl[2] = 0;
  516. break;
  517. default:
  518. break;
  519. }
  520. /* Next Header */
  521. if ((iphc0 & LOWPAN_IPHC_NH_C) == 0) {
  522. /* Next header is carried inline */
  523. if (!skb->len)
  524. goto drop;
  525. hdr.nexthdr = lowpan_fetch_skb_u8(skb);
  526. pr_debug("(%s): NH flag is set, next header is carried "
  527. "inline: %02x\n", __func__, hdr.nexthdr);
  528. }
  529. /* Hop Limit */
  530. if ((iphc0 & 0x03) != LOWPAN_IPHC_TTL_I)
  531. hdr.hop_limit = lowpan_ttl_values[iphc0 & 0x03];
  532. else {
  533. if (!skb->len)
  534. goto drop;
  535. hdr.hop_limit = lowpan_fetch_skb_u8(skb);
  536. }
  537. /* Extract SAM to the tmp variable */
  538. tmp = ((iphc1 & LOWPAN_IPHC_SAM) >> LOWPAN_IPHC_SAM_BIT) & 0x03;
  539. /* Source address uncompression */
  540. pr_debug("(%s): source address stateless compression\n", __func__);
  541. err = lowpan_uncompress_addr(skb, &hdr.saddr, lowpan_llprefix,
  542. lowpan_unc_llconf[tmp], skb->data);
  543. if (err)
  544. goto drop;
  545. /* Extract DAM to the tmp variable */
  546. tmp = ((iphc1 & LOWPAN_IPHC_DAM_11) >> LOWPAN_IPHC_DAM_BIT) & 0x03;
  547. /* check for Multicast Compression */
  548. if (iphc1 & LOWPAN_IPHC_M) {
  549. if (iphc1 & LOWPAN_IPHC_DAC) {
  550. pr_debug("(%s): destination address context-based "
  551. "multicast compression\n", __func__);
  552. /* TODO: implement this */
  553. } else {
  554. u8 prefix[] = {0xff, 0x02};
  555. pr_debug("(%s): destination address non-context-based"
  556. " multicast compression\n", __func__);
  557. if (0 < tmp && tmp < 3) {
  558. if (!skb->len)
  559. goto drop;
  560. else
  561. prefix[1] = lowpan_fetch_skb_u8(skb);
  562. }
  563. err = lowpan_uncompress_addr(skb, &hdr.daddr, prefix,
  564. lowpan_unc_mxconf[tmp], NULL);
  565. if (err)
  566. goto drop;
  567. }
  568. } else {
  569. pr_debug("(%s): destination address stateless compression\n",
  570. __func__);
  571. err = lowpan_uncompress_addr(skb, &hdr.daddr, lowpan_llprefix,
  572. lowpan_unc_llconf[tmp], skb->data);
  573. if (err)
  574. goto drop;
  575. }
  576. /* TODO: UDP header parse */
  577. /* Not fragmented package */
  578. hdr.payload_len = htons(skb->len);
  579. pr_debug("(%s): skb headroom size = %d, data length = %d\n", __func__,
  580. skb_headroom(skb), skb->len);
  581. pr_debug("(%s): IPv6 header dump:\n\tversion = %d\n\tlength = %d\n\t"
  582. "nexthdr = 0x%02x\n\thop_lim = %d\n", __func__, hdr.version,
  583. ntohs(hdr.payload_len), hdr.nexthdr, hdr.hop_limit);
  584. lowpan_raw_dump_table(__func__, "raw header dump", (u8 *)&hdr,
  585. sizeof(hdr));
  586. return lowpan_skb_deliver(skb, &hdr);
  587. drop:
  588. kfree_skb(skb);
  589. return -EINVAL;
  590. }
  591. static int lowpan_set_address(struct net_device *dev, void *p)
  592. {
  593. struct sockaddr *sa = p;
  594. if (netif_running(dev))
  595. return -EBUSY;
  596. /* TODO: validate addr */
  597. memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
  598. return 0;
  599. }
  600. static netdev_tx_t lowpan_xmit(struct sk_buff *skb, struct net_device *dev)
  601. {
  602. int err = 0;
  603. pr_debug("(%s): package xmit\n", __func__);
  604. skb->dev = lowpan_dev_info(dev)->real_dev;
  605. if (skb->dev == NULL) {
  606. pr_debug("(%s) ERROR: no real wpan device found\n", __func__);
  607. dev_kfree_skb(skb);
  608. } else
  609. err = dev_queue_xmit(skb);
  610. return (err < 0 ? NETDEV_TX_BUSY : NETDEV_TX_OK);
  611. }
  612. static void lowpan_dev_free(struct net_device *dev)
  613. {
  614. dev_put(lowpan_dev_info(dev)->real_dev);
  615. free_netdev(dev);
  616. }
  617. static struct header_ops lowpan_header_ops = {
  618. .create = lowpan_header_create,
  619. };
  620. static const struct net_device_ops lowpan_netdev_ops = {
  621. .ndo_start_xmit = lowpan_xmit,
  622. .ndo_set_mac_address = lowpan_set_address,
  623. };
  624. static void lowpan_setup(struct net_device *dev)
  625. {
  626. pr_debug("(%s)\n", __func__);
  627. dev->addr_len = IEEE802154_ADDR_LEN;
  628. memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);
  629. dev->type = ARPHRD_IEEE802154;
  630. dev->features = NETIF_F_NO_CSUM;
  631. /* Frame Control + Sequence Number + Address fields + Security Header */
  632. dev->hard_header_len = 2 + 1 + 20 + 14;
  633. dev->needed_tailroom = 2; /* FCS */
  634. dev->mtu = 1281;
  635. dev->tx_queue_len = 0;
  636. dev->flags = IFF_NOARP | IFF_BROADCAST;
  637. dev->watchdog_timeo = 0;
  638. dev->netdev_ops = &lowpan_netdev_ops;
  639. dev->header_ops = &lowpan_header_ops;
  640. dev->destructor = lowpan_dev_free;
  641. }
  642. static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[])
  643. {
  644. pr_debug("(%s)\n", __func__);
  645. if (tb[IFLA_ADDRESS]) {
  646. if (nla_len(tb[IFLA_ADDRESS]) != IEEE802154_ADDR_LEN)
  647. return -EINVAL;
  648. }
  649. return 0;
  650. }
  651. static int lowpan_rcv(struct sk_buff *skb, struct net_device *dev,
  652. struct packet_type *pt, struct net_device *orig_dev)
  653. {
  654. if (!netif_running(dev))
  655. goto drop;
  656. if (dev->type != ARPHRD_IEEE802154)
  657. goto drop;
  658. /* check that it's our buffer */
  659. if ((skb->data[0] & 0xe0) == 0x60)
  660. lowpan_process_data(skb);
  661. return NET_RX_SUCCESS;
  662. drop:
  663. kfree_skb(skb);
  664. return NET_RX_DROP;
  665. }
  666. static int lowpan_newlink(struct net *src_net, struct net_device *dev,
  667. struct nlattr *tb[], struct nlattr *data[])
  668. {
  669. struct net_device *real_dev;
  670. struct lowpan_dev_record *entry;
  671. pr_debug("(%s)\n", __func__);
  672. if (!tb[IFLA_LINK])
  673. return -EINVAL;
  674. /* find and hold real wpan device */
  675. real_dev = dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
  676. if (!real_dev)
  677. return -ENODEV;
  678. lowpan_dev_info(dev)->real_dev = real_dev;
  679. mutex_init(&lowpan_dev_info(dev)->dev_list_mtx);
  680. entry = kzalloc(sizeof(struct lowpan_dev_record), GFP_KERNEL);
  681. if (!entry) {
  682. dev_put(real_dev);
  683. lowpan_dev_info(dev)->real_dev = NULL;
  684. return -ENOMEM;
  685. }
  686. entry->ldev = dev;
  687. mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
  688. INIT_LIST_HEAD(&entry->list);
  689. list_add_tail(&entry->list, &lowpan_devices);
  690. mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
  691. register_netdevice(dev);
  692. return 0;
  693. }
  694. static void lowpan_dellink(struct net_device *dev, struct list_head *head)
  695. {
  696. struct lowpan_dev_info *lowpan_dev = lowpan_dev_info(dev);
  697. struct net_device *real_dev = lowpan_dev->real_dev;
  698. struct lowpan_dev_record *entry;
  699. struct lowpan_dev_record *tmp;
  700. ASSERT_RTNL();
  701. mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
  702. list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) {
  703. if (entry->ldev == dev) {
  704. list_del(&entry->list);
  705. kfree(entry);
  706. }
  707. }
  708. mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
  709. mutex_destroy(&lowpan_dev_info(dev)->dev_list_mtx);
  710. unregister_netdevice_queue(dev, head);
  711. dev_put(real_dev);
  712. }
  713. static struct rtnl_link_ops lowpan_link_ops __read_mostly = {
  714. .kind = "lowpan",
  715. .priv_size = sizeof(struct lowpan_dev_info),
  716. .setup = lowpan_setup,
  717. .newlink = lowpan_newlink,
  718. .dellink = lowpan_dellink,
  719. .validate = lowpan_validate,
  720. };
  721. static inline int __init lowpan_netlink_init(void)
  722. {
  723. return rtnl_link_register(&lowpan_link_ops);
  724. }
  725. static inline void __init lowpan_netlink_fini(void)
  726. {
  727. rtnl_link_unregister(&lowpan_link_ops);
  728. }
  729. static struct packet_type lowpan_packet_type = {
  730. .type = __constant_htons(ETH_P_IEEE802154),
  731. .func = lowpan_rcv,
  732. };
  733. static int __init lowpan_init_module(void)
  734. {
  735. int err = 0;
  736. pr_debug("(%s)\n", __func__);
  737. err = lowpan_netlink_init();
  738. if (err < 0)
  739. goto out;
  740. dev_add_pack(&lowpan_packet_type);
  741. out:
  742. return err;
  743. }
  744. static void __exit lowpan_cleanup_module(void)
  745. {
  746. pr_debug("(%s)\n", __func__);
  747. lowpan_netlink_fini();
  748. dev_remove_pack(&lowpan_packet_type);
  749. }
  750. module_init(lowpan_init_module);
  751. module_exit(lowpan_cleanup_module);
  752. MODULE_LICENSE("GPL");
  753. MODULE_ALIAS_RTNL_LINK("lowpan");