iphc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. /*
  2. * Copyright 2011, Siemens AG
  3. * written by Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
  4. */
  5. /* Based on patches from Jon Smirl <jonsmirl@gmail.com>
  6. * Copyright (c) 2011 Jon Smirl <jonsmirl@gmail.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. */
  18. /* Jon's code is based on 6lowpan implementation for Contiki which is:
  19. * Copyright (c) 2008, Swedish Institute of Computer Science.
  20. * All rights reserved.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the above copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. Neither the name of the Institute nor the names of its contributors
  31. * may be used to endorse or promote products derived from this software
  32. * without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  35. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  36. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  37. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  38. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  39. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  40. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  41. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  42. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  43. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  44. * SUCH DAMAGE.
  45. */
  46. #include <linux/bitops.h>
  47. #include <linux/if_arp.h>
  48. #include <linux/module.h>
  49. #include <linux/netdevice.h>
  50. #include <net/6lowpan.h>
  51. #include <net/ipv6.h>
  52. #include <net/af_ieee802154.h>
  53. /* Uncompress address function for source and
  54. * destination address(non-multicast).
  55. *
  56. * address_mode is sam value or dam value.
  57. */
  58. static int uncompress_addr(struct sk_buff *skb,
  59. struct in6_addr *ipaddr, const u8 address_mode,
  60. const u8 *lladdr, const u8 addr_type,
  61. const u8 addr_len)
  62. {
  63. bool fail;
  64. switch (address_mode) {
  65. case LOWPAN_IPHC_ADDR_00:
  66. /* for global link addresses */
  67. fail = lowpan_fetch_skb(skb, ipaddr->s6_addr, 16);
  68. break;
  69. case LOWPAN_IPHC_ADDR_01:
  70. /* fe:80::XXXX:XXXX:XXXX:XXXX */
  71. ipaddr->s6_addr[0] = 0xFE;
  72. ipaddr->s6_addr[1] = 0x80;
  73. fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[8], 8);
  74. break;
  75. case LOWPAN_IPHC_ADDR_02:
  76. /* fe:80::ff:fe00:XXXX */
  77. ipaddr->s6_addr[0] = 0xFE;
  78. ipaddr->s6_addr[1] = 0x80;
  79. ipaddr->s6_addr[11] = 0xFF;
  80. ipaddr->s6_addr[12] = 0xFE;
  81. fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[14], 2);
  82. break;
  83. case LOWPAN_IPHC_ADDR_03:
  84. fail = false;
  85. switch (addr_type) {
  86. case IEEE802154_ADDR_LONG:
  87. /* fe:80::XXXX:XXXX:XXXX:XXXX
  88. * \_________________/
  89. * hwaddr
  90. */
  91. ipaddr->s6_addr[0] = 0xFE;
  92. ipaddr->s6_addr[1] = 0x80;
  93. memcpy(&ipaddr->s6_addr[8], lladdr, addr_len);
  94. /* second bit-flip (Universe/Local)
  95. * is done according RFC2464
  96. */
  97. ipaddr->s6_addr[8] ^= 0x02;
  98. break;
  99. case IEEE802154_ADDR_SHORT:
  100. /* fe:80::ff:fe00:XXXX
  101. * \__/
  102. * short_addr
  103. *
  104. * Universe/Local bit is zero.
  105. */
  106. ipaddr->s6_addr[0] = 0xFE;
  107. ipaddr->s6_addr[1] = 0x80;
  108. ipaddr->s6_addr[11] = 0xFF;
  109. ipaddr->s6_addr[12] = 0xFE;
  110. ipaddr->s6_addr16[7] = htons(*((u16 *)lladdr));
  111. break;
  112. default:
  113. pr_debug("Invalid addr_type set\n");
  114. return -EINVAL;
  115. }
  116. break;
  117. default:
  118. pr_debug("Invalid address mode value: 0x%x\n", address_mode);
  119. return -EINVAL;
  120. }
  121. if (fail) {
  122. pr_debug("Failed to fetch skb data\n");
  123. return -EIO;
  124. }
  125. raw_dump_inline(NULL, "Reconstructed ipv6 addr is",
  126. ipaddr->s6_addr, 16);
  127. return 0;
  128. }
  129. /* Uncompress address function for source context
  130. * based address(non-multicast).
  131. */
  132. static int uncompress_context_based_src_addr(struct sk_buff *skb,
  133. struct in6_addr *ipaddr,
  134. const u8 sam)
  135. {
  136. switch (sam) {
  137. case LOWPAN_IPHC_ADDR_00:
  138. /* unspec address ::
  139. * Do nothing, address is already ::
  140. */
  141. break;
  142. case LOWPAN_IPHC_ADDR_01:
  143. /* TODO */
  144. case LOWPAN_IPHC_ADDR_02:
  145. /* TODO */
  146. case LOWPAN_IPHC_ADDR_03:
  147. /* TODO */
  148. netdev_warn(skb->dev, "SAM value 0x%x not supported\n", sam);
  149. return -EINVAL;
  150. default:
  151. pr_debug("Invalid sam value: 0x%x\n", sam);
  152. return -EINVAL;
  153. }
  154. raw_dump_inline(NULL,
  155. "Reconstructed context based ipv6 src addr is",
  156. ipaddr->s6_addr, 16);
  157. return 0;
  158. }
  159. /* Uncompress function for multicast destination address,
  160. * when M bit is set.
  161. */
  162. static int lowpan_uncompress_multicast_daddr(struct sk_buff *skb,
  163. struct in6_addr *ipaddr,
  164. const u8 dam)
  165. {
  166. bool fail;
  167. switch (dam) {
  168. case LOWPAN_IPHC_DAM_00:
  169. /* 00: 128 bits. The full address
  170. * is carried in-line.
  171. */
  172. fail = lowpan_fetch_skb(skb, ipaddr->s6_addr, 16);
  173. break;
  174. case LOWPAN_IPHC_DAM_01:
  175. /* 01: 48 bits. The address takes
  176. * the form ffXX::00XX:XXXX:XXXX.
  177. */
  178. ipaddr->s6_addr[0] = 0xFF;
  179. fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[1], 1);
  180. fail |= lowpan_fetch_skb(skb, &ipaddr->s6_addr[11], 5);
  181. break;
  182. case LOWPAN_IPHC_DAM_10:
  183. /* 10: 32 bits. The address takes
  184. * the form ffXX::00XX:XXXX.
  185. */
  186. ipaddr->s6_addr[0] = 0xFF;
  187. fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[1], 1);
  188. fail |= lowpan_fetch_skb(skb, &ipaddr->s6_addr[13], 3);
  189. break;
  190. case LOWPAN_IPHC_DAM_11:
  191. /* 11: 8 bits. The address takes
  192. * the form ff02::00XX.
  193. */
  194. ipaddr->s6_addr[0] = 0xFF;
  195. ipaddr->s6_addr[1] = 0x02;
  196. fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[15], 1);
  197. break;
  198. default:
  199. pr_debug("DAM value has a wrong value: 0x%x\n", dam);
  200. return -EINVAL;
  201. }
  202. if (fail) {
  203. pr_debug("Failed to fetch skb data\n");
  204. return -EIO;
  205. }
  206. raw_dump_inline(NULL, "Reconstructed ipv6 multicast addr is",
  207. ipaddr->s6_addr, 16);
  208. return 0;
  209. }
  210. static int uncompress_udp_header(struct sk_buff *skb, struct udphdr *uh)
  211. {
  212. bool fail;
  213. u8 tmp = 0, val = 0;
  214. fail = lowpan_fetch_skb(skb, &tmp, sizeof(tmp));
  215. if ((tmp & LOWPAN_NHC_UDP_MASK) == LOWPAN_NHC_UDP_ID) {
  216. pr_debug("UDP header uncompression\n");
  217. switch (tmp & LOWPAN_NHC_UDP_CS_P_11) {
  218. case LOWPAN_NHC_UDP_CS_P_00:
  219. fail |= lowpan_fetch_skb(skb, &uh->source,
  220. sizeof(uh->source));
  221. fail |= lowpan_fetch_skb(skb, &uh->dest,
  222. sizeof(uh->dest));
  223. break;
  224. case LOWPAN_NHC_UDP_CS_P_01:
  225. fail |= lowpan_fetch_skb(skb, &uh->source,
  226. sizeof(uh->source));
  227. fail |= lowpan_fetch_skb(skb, &val, sizeof(val));
  228. uh->dest = htons(val + LOWPAN_NHC_UDP_8BIT_PORT);
  229. break;
  230. case LOWPAN_NHC_UDP_CS_P_10:
  231. fail |= lowpan_fetch_skb(skb, &val, sizeof(val));
  232. uh->source = htons(val + LOWPAN_NHC_UDP_8BIT_PORT);
  233. fail |= lowpan_fetch_skb(skb, &uh->dest,
  234. sizeof(uh->dest));
  235. break;
  236. case LOWPAN_NHC_UDP_CS_P_11:
  237. fail |= lowpan_fetch_skb(skb, &val, sizeof(val));
  238. uh->source = htons(LOWPAN_NHC_UDP_4BIT_PORT +
  239. (val >> 4));
  240. uh->dest = htons(LOWPAN_NHC_UDP_4BIT_PORT +
  241. (val & 0x0f));
  242. break;
  243. default:
  244. pr_debug("ERROR: unknown UDP format\n");
  245. goto err;
  246. }
  247. pr_debug("uncompressed UDP ports: src = %d, dst = %d\n",
  248. ntohs(uh->source), ntohs(uh->dest));
  249. /* checksum */
  250. if (tmp & LOWPAN_NHC_UDP_CS_C) {
  251. pr_debug_ratelimited("checksum elided currently not supported\n");
  252. goto err;
  253. } else {
  254. fail |= lowpan_fetch_skb(skb, &uh->check,
  255. sizeof(uh->check));
  256. }
  257. /* UDP length needs to be infered from the lower layers
  258. * here, we obtain the hint from the remaining size of the
  259. * frame
  260. */
  261. uh->len = htons(skb->len + sizeof(struct udphdr));
  262. pr_debug("uncompressed UDP length: src = %d", ntohs(uh->len));
  263. } else {
  264. pr_debug("ERROR: unsupported NH format\n");
  265. goto err;
  266. }
  267. if (fail)
  268. goto err;
  269. return 0;
  270. err:
  271. return -EINVAL;
  272. }
  273. /* TTL uncompression values */
  274. static const u8 lowpan_ttl_values[] = { 0, 1, 64, 255 };
  275. int
  276. lowpan_header_decompress(struct sk_buff *skb, struct net_device *dev,
  277. const u8 *saddr, const u8 saddr_type,
  278. const u8 saddr_len, const u8 *daddr,
  279. const u8 daddr_type, const u8 daddr_len,
  280. u8 iphc0, u8 iphc1)
  281. {
  282. struct ipv6hdr hdr = {};
  283. u8 tmp, num_context = 0;
  284. int err;
  285. raw_dump_table(__func__, "raw skb data dump uncompressed",
  286. skb->data, skb->len);
  287. /* another if the CID flag is set */
  288. if (iphc1 & LOWPAN_IPHC_CID) {
  289. pr_debug("CID flag is set, increase header with one\n");
  290. if (lowpan_fetch_skb(skb, &num_context, sizeof(num_context)))
  291. return -EINVAL;
  292. }
  293. hdr.version = 6;
  294. /* Traffic Class and Flow Label */
  295. switch ((iphc0 & LOWPAN_IPHC_TF) >> 3) {
  296. /* Traffic Class and FLow Label carried in-line
  297. * ECN + DSCP + 4-bit Pad + Flow Label (4 bytes)
  298. */
  299. case 0: /* 00b */
  300. if (lowpan_fetch_skb(skb, &tmp, sizeof(tmp)))
  301. return -EINVAL;
  302. memcpy(&hdr.flow_lbl, &skb->data[0], 3);
  303. skb_pull(skb, 3);
  304. hdr.priority = ((tmp >> 2) & 0x0f);
  305. hdr.flow_lbl[0] = ((tmp >> 2) & 0x30) | (tmp << 6) |
  306. (hdr.flow_lbl[0] & 0x0f);
  307. break;
  308. /* Traffic class carried in-line
  309. * ECN + DSCP (1 byte), Flow Label is elided
  310. */
  311. case 2: /* 10b */
  312. if (lowpan_fetch_skb(skb, &tmp, sizeof(tmp)))
  313. return -EINVAL;
  314. hdr.priority = ((tmp >> 2) & 0x0f);
  315. hdr.flow_lbl[0] = ((tmp << 6) & 0xC0) | ((tmp >> 2) & 0x30);
  316. break;
  317. /* Flow Label carried in-line
  318. * ECN + 2-bit Pad + Flow Label (3 bytes), DSCP is elided
  319. */
  320. case 1: /* 01b */
  321. if (lowpan_fetch_skb(skb, &tmp, sizeof(tmp)))
  322. return -EINVAL;
  323. hdr.flow_lbl[0] = (skb->data[0] & 0x0F) | ((tmp >> 2) & 0x30);
  324. memcpy(&hdr.flow_lbl[1], &skb->data[0], 2);
  325. skb_pull(skb, 2);
  326. break;
  327. /* Traffic Class and Flow Label are elided */
  328. case 3: /* 11b */
  329. break;
  330. default:
  331. break;
  332. }
  333. /* Next Header */
  334. if ((iphc0 & LOWPAN_IPHC_NH_C) == 0) {
  335. /* Next header is carried inline */
  336. if (lowpan_fetch_skb(skb, &hdr.nexthdr, sizeof(hdr.nexthdr)))
  337. return -EINVAL;
  338. pr_debug("NH flag is set, next header carried inline: %02x\n",
  339. hdr.nexthdr);
  340. }
  341. /* Hop Limit */
  342. if ((iphc0 & 0x03) != LOWPAN_IPHC_TTL_I) {
  343. hdr.hop_limit = lowpan_ttl_values[iphc0 & 0x03];
  344. } else {
  345. if (lowpan_fetch_skb(skb, &hdr.hop_limit,
  346. sizeof(hdr.hop_limit)))
  347. return -EINVAL;
  348. }
  349. /* Extract SAM to the tmp variable */
  350. tmp = ((iphc1 & LOWPAN_IPHC_SAM) >> LOWPAN_IPHC_SAM_BIT) & 0x03;
  351. if (iphc1 & LOWPAN_IPHC_SAC) {
  352. /* Source address context based uncompression */
  353. pr_debug("SAC bit is set. Handle context based source address.\n");
  354. err = uncompress_context_based_src_addr(skb, &hdr.saddr, tmp);
  355. } else {
  356. /* Source address uncompression */
  357. pr_debug("source address stateless compression\n");
  358. err = uncompress_addr(skb, &hdr.saddr, tmp, saddr,
  359. saddr_type, saddr_len);
  360. }
  361. /* Check on error of previous branch */
  362. if (err)
  363. return -EINVAL;
  364. /* Extract DAM to the tmp variable */
  365. tmp = ((iphc1 & LOWPAN_IPHC_DAM_11) >> LOWPAN_IPHC_DAM_BIT) & 0x03;
  366. /* check for Multicast Compression */
  367. if (iphc1 & LOWPAN_IPHC_M) {
  368. if (iphc1 & LOWPAN_IPHC_DAC) {
  369. pr_debug("dest: context-based mcast compression\n");
  370. /* TODO: implement this */
  371. } else {
  372. err = lowpan_uncompress_multicast_daddr(skb, &hdr.daddr,
  373. tmp);
  374. if (err)
  375. return -EINVAL;
  376. }
  377. } else {
  378. err = uncompress_addr(skb, &hdr.daddr, tmp, daddr,
  379. daddr_type, daddr_len);
  380. pr_debug("dest: stateless compression mode %d dest %pI6c\n",
  381. tmp, &hdr.daddr);
  382. if (err)
  383. return -EINVAL;
  384. }
  385. /* UDP data uncompression */
  386. if (iphc0 & LOWPAN_IPHC_NH_C) {
  387. struct udphdr uh;
  388. const int needed = sizeof(struct udphdr) + sizeof(hdr);
  389. if (uncompress_udp_header(skb, &uh))
  390. return -EINVAL;
  391. /* replace the compressed UDP head by the uncompressed UDP
  392. * header
  393. */
  394. err = skb_cow(skb, needed);
  395. if (unlikely(err))
  396. return err;
  397. skb_push(skb, sizeof(struct udphdr));
  398. skb_reset_transport_header(skb);
  399. skb_copy_to_linear_data(skb, &uh, sizeof(struct udphdr));
  400. raw_dump_table(__func__, "raw UDP header dump",
  401. (u8 *)&uh, sizeof(uh));
  402. hdr.nexthdr = UIP_PROTO_UDP;
  403. } else {
  404. err = skb_cow(skb, sizeof(hdr));
  405. if (unlikely(err))
  406. return err;
  407. }
  408. hdr.payload_len = htons(skb->len);
  409. pr_debug("skb headroom size = %d, data length = %d\n",
  410. skb_headroom(skb), skb->len);
  411. pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength = %d\n\t"
  412. "nexthdr = 0x%02x\n\thop_lim = %d\n\tdest = %pI6c\n",
  413. hdr.version, ntohs(hdr.payload_len), hdr.nexthdr,
  414. hdr.hop_limit, &hdr.daddr);
  415. skb_push(skb, sizeof(hdr));
  416. skb_reset_network_header(skb);
  417. skb_copy_to_linear_data(skb, &hdr, sizeof(hdr));
  418. raw_dump_table(__func__, "raw header dump", (u8 *)&hdr, sizeof(hdr));
  419. return 0;
  420. }
  421. EXPORT_SYMBOL_GPL(lowpan_header_decompress);
  422. static u8 lowpan_compress_addr_64(u8 **hc_ptr, u8 shift,
  423. const struct in6_addr *ipaddr,
  424. const unsigned char *lladdr)
  425. {
  426. u8 val = 0;
  427. if (is_addr_mac_addr_based(ipaddr, lladdr)) {
  428. val = 3; /* 0-bits */
  429. pr_debug("address compression 0 bits\n");
  430. } else if (lowpan_is_iid_16_bit_compressable(ipaddr)) {
  431. /* compress IID to 16 bits xxxx::XXXX */
  432. lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr16[7], 2);
  433. val = 2; /* 16-bits */
  434. raw_dump_inline(NULL, "Compressed ipv6 addr is (16 bits)",
  435. *hc_ptr - 2, 2);
  436. } else {
  437. /* do not compress IID => xxxx::IID */
  438. lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr16[4], 8);
  439. val = 1; /* 64-bits */
  440. raw_dump_inline(NULL, "Compressed ipv6 addr is (64 bits)",
  441. *hc_ptr - 8, 8);
  442. }
  443. return rol8(val, shift);
  444. }
  445. static void compress_udp_header(u8 **hc_ptr, struct sk_buff *skb)
  446. {
  447. struct udphdr *uh;
  448. u8 tmp;
  449. /* In the case of RAW sockets the transport header is not set by
  450. * the ip6 stack so we must set it ourselves
  451. */
  452. if (skb->transport_header == skb->network_header)
  453. skb_set_transport_header(skb, sizeof(struct ipv6hdr));
  454. uh = udp_hdr(skb);
  455. if (((ntohs(uh->source) & LOWPAN_NHC_UDP_4BIT_MASK) ==
  456. LOWPAN_NHC_UDP_4BIT_PORT) &&
  457. ((ntohs(uh->dest) & LOWPAN_NHC_UDP_4BIT_MASK) ==
  458. LOWPAN_NHC_UDP_4BIT_PORT)) {
  459. pr_debug("UDP header: both ports compression to 4 bits\n");
  460. /* compression value */
  461. tmp = LOWPAN_NHC_UDP_CS_P_11;
  462. lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
  463. /* source and destination port */
  464. tmp = ntohs(uh->dest) - LOWPAN_NHC_UDP_4BIT_PORT +
  465. ((ntohs(uh->source) - LOWPAN_NHC_UDP_4BIT_PORT) << 4);
  466. lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
  467. } else if ((ntohs(uh->dest) & LOWPAN_NHC_UDP_8BIT_MASK) ==
  468. LOWPAN_NHC_UDP_8BIT_PORT) {
  469. pr_debug("UDP header: remove 8 bits of dest\n");
  470. /* compression value */
  471. tmp = LOWPAN_NHC_UDP_CS_P_01;
  472. lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
  473. /* source port */
  474. lowpan_push_hc_data(hc_ptr, &uh->source, sizeof(uh->source));
  475. /* destination port */
  476. tmp = ntohs(uh->dest) - LOWPAN_NHC_UDP_8BIT_PORT;
  477. lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
  478. } else if ((ntohs(uh->source) & LOWPAN_NHC_UDP_8BIT_MASK) ==
  479. LOWPAN_NHC_UDP_8BIT_PORT) {
  480. pr_debug("UDP header: remove 8 bits of source\n");
  481. /* compression value */
  482. tmp = LOWPAN_NHC_UDP_CS_P_10;
  483. lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
  484. /* source port */
  485. tmp = ntohs(uh->source) - LOWPAN_NHC_UDP_8BIT_PORT;
  486. lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
  487. /* destination port */
  488. lowpan_push_hc_data(hc_ptr, &uh->dest, sizeof(uh->dest));
  489. } else {
  490. pr_debug("UDP header: can't compress\n");
  491. /* compression value */
  492. tmp = LOWPAN_NHC_UDP_CS_P_00;
  493. lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
  494. /* source port */
  495. lowpan_push_hc_data(hc_ptr, &uh->source, sizeof(uh->source));
  496. /* destination port */
  497. lowpan_push_hc_data(hc_ptr, &uh->dest, sizeof(uh->dest));
  498. }
  499. /* checksum is always inline */
  500. lowpan_push_hc_data(hc_ptr, &uh->check, sizeof(uh->check));
  501. /* skip the UDP header */
  502. skb_pull(skb, sizeof(struct udphdr));
  503. }
  504. int lowpan_header_compress(struct sk_buff *skb, struct net_device *dev,
  505. unsigned short type, const void *_daddr,
  506. const void *_saddr, unsigned int len)
  507. {
  508. u8 tmp, iphc0, iphc1, *hc_ptr;
  509. struct ipv6hdr *hdr;
  510. u8 head[100] = {};
  511. int addr_type;
  512. if (type != ETH_P_IPV6)
  513. return -EINVAL;
  514. hdr = ipv6_hdr(skb);
  515. hc_ptr = head + 2;
  516. pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength = %d\n"
  517. "\tnexthdr = 0x%02x\n\thop_lim = %d\n\tdest = %pI6c\n",
  518. hdr->version, ntohs(hdr->payload_len), hdr->nexthdr,
  519. hdr->hop_limit, &hdr->daddr);
  520. raw_dump_table(__func__, "raw skb network header dump",
  521. skb_network_header(skb), sizeof(struct ipv6hdr));
  522. /* As we copy some bit-length fields, in the IPHC encoding bytes,
  523. * we sometimes use |=
  524. * If the field is 0, and the current bit value in memory is 1,
  525. * this does not work. We therefore reset the IPHC encoding here
  526. */
  527. iphc0 = LOWPAN_DISPATCH_IPHC;
  528. iphc1 = 0;
  529. /* TODO: context lookup */
  530. raw_dump_inline(__func__, "saddr",
  531. (unsigned char *)_saddr, IEEE802154_ADDR_LEN);
  532. raw_dump_inline(__func__, "daddr",
  533. (unsigned char *)_daddr, IEEE802154_ADDR_LEN);
  534. raw_dump_table(__func__, "sending raw skb network uncompressed packet",
  535. skb->data, skb->len);
  536. /* Traffic class, flow label
  537. * If flow label is 0, compress it. If traffic class is 0, compress it
  538. * We have to process both in the same time as the offset of traffic
  539. * class depends on the presence of version and flow label
  540. */
  541. /* hc format of TC is ECN | DSCP , original one is DSCP | ECN */
  542. tmp = (hdr->priority << 4) | (hdr->flow_lbl[0] >> 4);
  543. tmp = ((tmp & 0x03) << 6) | (tmp >> 2);
  544. if (((hdr->flow_lbl[0] & 0x0F) == 0) &&
  545. (hdr->flow_lbl[1] == 0) && (hdr->flow_lbl[2] == 0)) {
  546. /* flow label can be compressed */
  547. iphc0 |= LOWPAN_IPHC_FL_C;
  548. if ((hdr->priority == 0) &&
  549. ((hdr->flow_lbl[0] & 0xF0) == 0)) {
  550. /* compress (elide) all */
  551. iphc0 |= LOWPAN_IPHC_TC_C;
  552. } else {
  553. /* compress only the flow label */
  554. *hc_ptr = tmp;
  555. hc_ptr += 1;
  556. }
  557. } else {
  558. /* Flow label cannot be compressed */
  559. if ((hdr->priority == 0) &&
  560. ((hdr->flow_lbl[0] & 0xF0) == 0)) {
  561. /* compress only traffic class */
  562. iphc0 |= LOWPAN_IPHC_TC_C;
  563. *hc_ptr = (tmp & 0xc0) | (hdr->flow_lbl[0] & 0x0F);
  564. memcpy(hc_ptr + 1, &hdr->flow_lbl[1], 2);
  565. hc_ptr += 3;
  566. } else {
  567. /* compress nothing */
  568. memcpy(hc_ptr, hdr, 4);
  569. /* replace the top byte with new ECN | DSCP format */
  570. *hc_ptr = tmp;
  571. hc_ptr += 4;
  572. }
  573. }
  574. /* NOTE: payload length is always compressed */
  575. /* Next Header is compress if UDP */
  576. if (hdr->nexthdr == UIP_PROTO_UDP)
  577. iphc0 |= LOWPAN_IPHC_NH_C;
  578. if ((iphc0 & LOWPAN_IPHC_NH_C) == 0)
  579. lowpan_push_hc_data(&hc_ptr, &hdr->nexthdr,
  580. sizeof(hdr->nexthdr));
  581. /* Hop limit
  582. * if 1: compress, encoding is 01
  583. * if 64: compress, encoding is 10
  584. * if 255: compress, encoding is 11
  585. * else do not compress
  586. */
  587. switch (hdr->hop_limit) {
  588. case 1:
  589. iphc0 |= LOWPAN_IPHC_TTL_1;
  590. break;
  591. case 64:
  592. iphc0 |= LOWPAN_IPHC_TTL_64;
  593. break;
  594. case 255:
  595. iphc0 |= LOWPAN_IPHC_TTL_255;
  596. break;
  597. default:
  598. lowpan_push_hc_data(&hc_ptr, &hdr->hop_limit,
  599. sizeof(hdr->hop_limit));
  600. }
  601. addr_type = ipv6_addr_type(&hdr->saddr);
  602. /* source address compression */
  603. if (addr_type == IPV6_ADDR_ANY) {
  604. pr_debug("source address is unspecified, setting SAC\n");
  605. iphc1 |= LOWPAN_IPHC_SAC;
  606. } else {
  607. if (addr_type & IPV6_ADDR_LINKLOCAL) {
  608. iphc1 |= lowpan_compress_addr_64(&hc_ptr,
  609. LOWPAN_IPHC_SAM_BIT,
  610. &hdr->saddr, _saddr);
  611. pr_debug("source address unicast link-local %pI6c iphc1 0x%02x\n",
  612. &hdr->saddr, iphc1);
  613. } else {
  614. pr_debug("send the full source address\n");
  615. lowpan_push_hc_data(&hc_ptr, hdr->saddr.s6_addr, 16);
  616. }
  617. }
  618. addr_type = ipv6_addr_type(&hdr->daddr);
  619. /* destination address compression */
  620. if (addr_type & IPV6_ADDR_MULTICAST) {
  621. pr_debug("destination address is multicast: ");
  622. iphc1 |= LOWPAN_IPHC_M;
  623. if (lowpan_is_mcast_addr_compressable8(&hdr->daddr)) {
  624. pr_debug("compressed to 1 octet\n");
  625. iphc1 |= LOWPAN_IPHC_DAM_11;
  626. /* use last byte */
  627. lowpan_push_hc_data(&hc_ptr,
  628. &hdr->daddr.s6_addr[15], 1);
  629. } else if (lowpan_is_mcast_addr_compressable32(&hdr->daddr)) {
  630. pr_debug("compressed to 4 octets\n");
  631. iphc1 |= LOWPAN_IPHC_DAM_10;
  632. /* second byte + the last three */
  633. lowpan_push_hc_data(&hc_ptr,
  634. &hdr->daddr.s6_addr[1], 1);
  635. lowpan_push_hc_data(&hc_ptr,
  636. &hdr->daddr.s6_addr[13], 3);
  637. } else if (lowpan_is_mcast_addr_compressable48(&hdr->daddr)) {
  638. pr_debug("compressed to 6 octets\n");
  639. iphc1 |= LOWPAN_IPHC_DAM_01;
  640. /* second byte + the last five */
  641. lowpan_push_hc_data(&hc_ptr,
  642. &hdr->daddr.s6_addr[1], 1);
  643. lowpan_push_hc_data(&hc_ptr,
  644. &hdr->daddr.s6_addr[11], 5);
  645. } else {
  646. pr_debug("using full address\n");
  647. iphc1 |= LOWPAN_IPHC_DAM_00;
  648. lowpan_push_hc_data(&hc_ptr, hdr->daddr.s6_addr, 16);
  649. }
  650. } else {
  651. if (addr_type & IPV6_ADDR_LINKLOCAL) {
  652. /* TODO: context lookup */
  653. iphc1 |= lowpan_compress_addr_64(&hc_ptr,
  654. LOWPAN_IPHC_DAM_BIT, &hdr->daddr, _daddr);
  655. pr_debug("dest address unicast link-local %pI6c "
  656. "iphc1 0x%02x\n", &hdr->daddr, iphc1);
  657. } else {
  658. pr_debug("dest address unicast %pI6c\n", &hdr->daddr);
  659. lowpan_push_hc_data(&hc_ptr, hdr->daddr.s6_addr, 16);
  660. }
  661. }
  662. /* UDP header compression */
  663. if (hdr->nexthdr == UIP_PROTO_UDP)
  664. compress_udp_header(&hc_ptr, skb);
  665. head[0] = iphc0;
  666. head[1] = iphc1;
  667. skb_pull(skb, sizeof(struct ipv6hdr));
  668. skb_reset_transport_header(skb);
  669. memcpy(skb_push(skb, hc_ptr - head), head, hc_ptr - head);
  670. skb_reset_network_header(skb);
  671. pr_debug("header len %d skb %u\n", (int)(hc_ptr - head), skb->len);
  672. raw_dump_table(__func__, "raw skb data dump compressed",
  673. skb->data, skb->len);
  674. return 0;
  675. }
  676. EXPORT_SYMBOL_GPL(lowpan_header_compress);
  677. MODULE_LICENSE("GPL");