iphc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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/netdevice.h>
  49. #include <net/6lowpan.h>
  50. #include <net/ipv6.h>
  51. #include <net/af_ieee802154.h>
  52. #include "nhc.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. /* TTL uncompression values */
  211. static const u8 lowpan_ttl_values[] = { 0, 1, 64, 255 };
  212. int
  213. lowpan_header_decompress(struct sk_buff *skb, struct net_device *dev,
  214. const u8 *saddr, const u8 saddr_type,
  215. const u8 saddr_len, const u8 *daddr,
  216. const u8 daddr_type, const u8 daddr_len,
  217. u8 iphc0, u8 iphc1)
  218. {
  219. struct ipv6hdr hdr = {};
  220. u8 tmp, num_context = 0;
  221. int err;
  222. raw_dump_table(__func__, "raw skb data dump uncompressed",
  223. skb->data, skb->len);
  224. /* another if the CID flag is set */
  225. if (iphc1 & LOWPAN_IPHC_CID) {
  226. pr_debug("CID flag is set, increase header with one\n");
  227. if (lowpan_fetch_skb(skb, &num_context, sizeof(num_context)))
  228. return -EINVAL;
  229. }
  230. hdr.version = 6;
  231. /* Traffic Class and Flow Label */
  232. switch ((iphc0 & LOWPAN_IPHC_TF) >> 3) {
  233. /* Traffic Class and FLow Label carried in-line
  234. * ECN + DSCP + 4-bit Pad + Flow Label (4 bytes)
  235. */
  236. case 0: /* 00b */
  237. if (lowpan_fetch_skb(skb, &tmp, sizeof(tmp)))
  238. return -EINVAL;
  239. memcpy(&hdr.flow_lbl, &skb->data[0], 3);
  240. skb_pull(skb, 3);
  241. hdr.priority = ((tmp >> 2) & 0x0f);
  242. hdr.flow_lbl[0] = ((tmp >> 2) & 0x30) | (tmp << 6) |
  243. (hdr.flow_lbl[0] & 0x0f);
  244. break;
  245. /* Traffic class carried in-line
  246. * ECN + DSCP (1 byte), Flow Label is elided
  247. */
  248. case 2: /* 10b */
  249. if (lowpan_fetch_skb(skb, &tmp, sizeof(tmp)))
  250. return -EINVAL;
  251. hdr.priority = ((tmp >> 2) & 0x0f);
  252. hdr.flow_lbl[0] = ((tmp << 6) & 0xC0) | ((tmp >> 2) & 0x30);
  253. break;
  254. /* Flow Label carried in-line
  255. * ECN + 2-bit Pad + Flow Label (3 bytes), DSCP is elided
  256. */
  257. case 1: /* 01b */
  258. if (lowpan_fetch_skb(skb, &tmp, sizeof(tmp)))
  259. return -EINVAL;
  260. hdr.flow_lbl[0] = (tmp & 0x0F) | ((tmp >> 2) & 0x30);
  261. memcpy(&hdr.flow_lbl[1], &skb->data[0], 2);
  262. skb_pull(skb, 2);
  263. break;
  264. /* Traffic Class and Flow Label are elided */
  265. case 3: /* 11b */
  266. break;
  267. default:
  268. break;
  269. }
  270. /* Next Header */
  271. if ((iphc0 & LOWPAN_IPHC_NH_C) == 0) {
  272. /* Next header is carried inline */
  273. if (lowpan_fetch_skb(skb, &hdr.nexthdr, sizeof(hdr.nexthdr)))
  274. return -EINVAL;
  275. pr_debug("NH flag is set, next header carried inline: %02x\n",
  276. hdr.nexthdr);
  277. }
  278. /* Hop Limit */
  279. if ((iphc0 & 0x03) != LOWPAN_IPHC_TTL_I) {
  280. hdr.hop_limit = lowpan_ttl_values[iphc0 & 0x03];
  281. } else {
  282. if (lowpan_fetch_skb(skb, &hdr.hop_limit,
  283. sizeof(hdr.hop_limit)))
  284. return -EINVAL;
  285. }
  286. /* Extract SAM to the tmp variable */
  287. tmp = ((iphc1 & LOWPAN_IPHC_SAM) >> LOWPAN_IPHC_SAM_BIT) & 0x03;
  288. if (iphc1 & LOWPAN_IPHC_SAC) {
  289. /* Source address context based uncompression */
  290. pr_debug("SAC bit is set. Handle context based source address.\n");
  291. err = uncompress_context_based_src_addr(skb, &hdr.saddr, tmp);
  292. } else {
  293. /* Source address uncompression */
  294. pr_debug("source address stateless compression\n");
  295. err = uncompress_addr(skb, &hdr.saddr, tmp, saddr,
  296. saddr_type, saddr_len);
  297. }
  298. /* Check on error of previous branch */
  299. if (err)
  300. return -EINVAL;
  301. /* Extract DAM to the tmp variable */
  302. tmp = ((iphc1 & LOWPAN_IPHC_DAM_11) >> LOWPAN_IPHC_DAM_BIT) & 0x03;
  303. /* check for Multicast Compression */
  304. if (iphc1 & LOWPAN_IPHC_M) {
  305. if (iphc1 & LOWPAN_IPHC_DAC) {
  306. pr_debug("dest: context-based mcast compression\n");
  307. /* TODO: implement this */
  308. } else {
  309. err = lowpan_uncompress_multicast_daddr(skb, &hdr.daddr,
  310. tmp);
  311. if (err)
  312. return -EINVAL;
  313. }
  314. } else {
  315. err = uncompress_addr(skb, &hdr.daddr, tmp, daddr,
  316. daddr_type, daddr_len);
  317. pr_debug("dest: stateless compression mode %d dest %pI6c\n",
  318. tmp, &hdr.daddr);
  319. if (err)
  320. return -EINVAL;
  321. }
  322. /* Next header data uncompression */
  323. if (iphc0 & LOWPAN_IPHC_NH_C) {
  324. err = lowpan_nhc_do_uncompression(skb, dev, &hdr);
  325. if (err < 0)
  326. return err;
  327. } else {
  328. err = skb_cow(skb, sizeof(hdr));
  329. if (unlikely(err))
  330. return err;
  331. }
  332. switch (lowpan_priv(dev)->lltype) {
  333. case LOWPAN_LLTYPE_IEEE802154:
  334. if (lowpan_802154_cb(skb)->d_size)
  335. hdr.payload_len = htons(lowpan_802154_cb(skb)->d_size -
  336. sizeof(struct ipv6hdr));
  337. else
  338. hdr.payload_len = htons(skb->len);
  339. break;
  340. default:
  341. hdr.payload_len = htons(skb->len);
  342. break;
  343. }
  344. pr_debug("skb headroom size = %d, data length = %d\n",
  345. skb_headroom(skb), skb->len);
  346. pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength = %d\n\t"
  347. "nexthdr = 0x%02x\n\thop_lim = %d\n\tdest = %pI6c\n",
  348. hdr.version, ntohs(hdr.payload_len), hdr.nexthdr,
  349. hdr.hop_limit, &hdr.daddr);
  350. skb_push(skb, sizeof(hdr));
  351. skb_reset_network_header(skb);
  352. skb_copy_to_linear_data(skb, &hdr, sizeof(hdr));
  353. raw_dump_table(__func__, "raw header dump", (u8 *)&hdr, sizeof(hdr));
  354. return 0;
  355. }
  356. EXPORT_SYMBOL_GPL(lowpan_header_decompress);
  357. static u8 lowpan_compress_addr_64(u8 **hc_ptr, u8 shift,
  358. const struct in6_addr *ipaddr,
  359. const unsigned char *lladdr)
  360. {
  361. u8 val = 0;
  362. if (is_addr_mac_addr_based(ipaddr, lladdr)) {
  363. val = 3; /* 0-bits */
  364. pr_debug("address compression 0 bits\n");
  365. } else if (lowpan_is_iid_16_bit_compressable(ipaddr)) {
  366. /* compress IID to 16 bits xxxx::XXXX */
  367. lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr16[7], 2);
  368. val = 2; /* 16-bits */
  369. raw_dump_inline(NULL, "Compressed ipv6 addr is (16 bits)",
  370. *hc_ptr - 2, 2);
  371. } else {
  372. /* do not compress IID => xxxx::IID */
  373. lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr16[4], 8);
  374. val = 1; /* 64-bits */
  375. raw_dump_inline(NULL, "Compressed ipv6 addr is (64 bits)",
  376. *hc_ptr - 8, 8);
  377. }
  378. return rol8(val, shift);
  379. }
  380. int lowpan_header_compress(struct sk_buff *skb, struct net_device *dev,
  381. unsigned short type, const void *_daddr,
  382. const void *_saddr, unsigned int len)
  383. {
  384. u8 tmp, iphc0, iphc1, *hc_ptr;
  385. struct ipv6hdr *hdr;
  386. u8 head[100] = {};
  387. int ret, addr_type;
  388. if (type != ETH_P_IPV6)
  389. return -EINVAL;
  390. hdr = ipv6_hdr(skb);
  391. hc_ptr = head + 2;
  392. pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength = %d\n"
  393. "\tnexthdr = 0x%02x\n\thop_lim = %d\n\tdest = %pI6c\n",
  394. hdr->version, ntohs(hdr->payload_len), hdr->nexthdr,
  395. hdr->hop_limit, &hdr->daddr);
  396. raw_dump_table(__func__, "raw skb network header dump",
  397. skb_network_header(skb), sizeof(struct ipv6hdr));
  398. /* As we copy some bit-length fields, in the IPHC encoding bytes,
  399. * we sometimes use |=
  400. * If the field is 0, and the current bit value in memory is 1,
  401. * this does not work. We therefore reset the IPHC encoding here
  402. */
  403. iphc0 = LOWPAN_DISPATCH_IPHC;
  404. iphc1 = 0;
  405. /* TODO: context lookup */
  406. raw_dump_inline(__func__, "saddr",
  407. (unsigned char *)_saddr, IEEE802154_ADDR_LEN);
  408. raw_dump_inline(__func__, "daddr",
  409. (unsigned char *)_daddr, IEEE802154_ADDR_LEN);
  410. raw_dump_table(__func__, "sending raw skb network uncompressed packet",
  411. skb->data, skb->len);
  412. /* Traffic class, flow label
  413. * If flow label is 0, compress it. If traffic class is 0, compress it
  414. * We have to process both in the same time as the offset of traffic
  415. * class depends on the presence of version and flow label
  416. */
  417. /* hc format of TC is ECN | DSCP , original one is DSCP | ECN */
  418. tmp = (hdr->priority << 4) | (hdr->flow_lbl[0] >> 4);
  419. tmp = ((tmp & 0x03) << 6) | (tmp >> 2);
  420. if (((hdr->flow_lbl[0] & 0x0F) == 0) &&
  421. (hdr->flow_lbl[1] == 0) && (hdr->flow_lbl[2] == 0)) {
  422. /* flow label can be compressed */
  423. iphc0 |= LOWPAN_IPHC_FL_C;
  424. if ((hdr->priority == 0) &&
  425. ((hdr->flow_lbl[0] & 0xF0) == 0)) {
  426. /* compress (elide) all */
  427. iphc0 |= LOWPAN_IPHC_TC_C;
  428. } else {
  429. /* compress only the flow label */
  430. *hc_ptr = tmp;
  431. hc_ptr += 1;
  432. }
  433. } else {
  434. /* Flow label cannot be compressed */
  435. if ((hdr->priority == 0) &&
  436. ((hdr->flow_lbl[0] & 0xF0) == 0)) {
  437. /* compress only traffic class */
  438. iphc0 |= LOWPAN_IPHC_TC_C;
  439. *hc_ptr = (tmp & 0xc0) | (hdr->flow_lbl[0] & 0x0F);
  440. memcpy(hc_ptr + 1, &hdr->flow_lbl[1], 2);
  441. hc_ptr += 3;
  442. } else {
  443. /* compress nothing */
  444. memcpy(hc_ptr, hdr, 4);
  445. /* replace the top byte with new ECN | DSCP format */
  446. *hc_ptr = tmp;
  447. hc_ptr += 4;
  448. }
  449. }
  450. /* NOTE: payload length is always compressed */
  451. /* Check if we provide the nhc format for nexthdr and compression
  452. * functionality. If not nexthdr is handled inline and not compressed.
  453. */
  454. ret = lowpan_nhc_check_compression(skb, hdr, &hc_ptr, &iphc0);
  455. if (ret < 0)
  456. return ret;
  457. /* Hop limit
  458. * if 1: compress, encoding is 01
  459. * if 64: compress, encoding is 10
  460. * if 255: compress, encoding is 11
  461. * else do not compress
  462. */
  463. switch (hdr->hop_limit) {
  464. case 1:
  465. iphc0 |= LOWPAN_IPHC_TTL_1;
  466. break;
  467. case 64:
  468. iphc0 |= LOWPAN_IPHC_TTL_64;
  469. break;
  470. case 255:
  471. iphc0 |= LOWPAN_IPHC_TTL_255;
  472. break;
  473. default:
  474. lowpan_push_hc_data(&hc_ptr, &hdr->hop_limit,
  475. sizeof(hdr->hop_limit));
  476. }
  477. addr_type = ipv6_addr_type(&hdr->saddr);
  478. /* source address compression */
  479. if (addr_type == IPV6_ADDR_ANY) {
  480. pr_debug("source address is unspecified, setting SAC\n");
  481. iphc1 |= LOWPAN_IPHC_SAC;
  482. } else {
  483. if (addr_type & IPV6_ADDR_LINKLOCAL) {
  484. iphc1 |= lowpan_compress_addr_64(&hc_ptr,
  485. LOWPAN_IPHC_SAM_BIT,
  486. &hdr->saddr, _saddr);
  487. pr_debug("source address unicast link-local %pI6c iphc1 0x%02x\n",
  488. &hdr->saddr, iphc1);
  489. } else {
  490. pr_debug("send the full source address\n");
  491. lowpan_push_hc_data(&hc_ptr, hdr->saddr.s6_addr, 16);
  492. }
  493. }
  494. addr_type = ipv6_addr_type(&hdr->daddr);
  495. /* destination address compression */
  496. if (addr_type & IPV6_ADDR_MULTICAST) {
  497. pr_debug("destination address is multicast: ");
  498. iphc1 |= LOWPAN_IPHC_M;
  499. if (lowpan_is_mcast_addr_compressable8(&hdr->daddr)) {
  500. pr_debug("compressed to 1 octet\n");
  501. iphc1 |= LOWPAN_IPHC_DAM_11;
  502. /* use last byte */
  503. lowpan_push_hc_data(&hc_ptr,
  504. &hdr->daddr.s6_addr[15], 1);
  505. } else if (lowpan_is_mcast_addr_compressable32(&hdr->daddr)) {
  506. pr_debug("compressed to 4 octets\n");
  507. iphc1 |= LOWPAN_IPHC_DAM_10;
  508. /* second byte + the last three */
  509. lowpan_push_hc_data(&hc_ptr,
  510. &hdr->daddr.s6_addr[1], 1);
  511. lowpan_push_hc_data(&hc_ptr,
  512. &hdr->daddr.s6_addr[13], 3);
  513. } else if (lowpan_is_mcast_addr_compressable48(&hdr->daddr)) {
  514. pr_debug("compressed to 6 octets\n");
  515. iphc1 |= LOWPAN_IPHC_DAM_01;
  516. /* second byte + the last five */
  517. lowpan_push_hc_data(&hc_ptr,
  518. &hdr->daddr.s6_addr[1], 1);
  519. lowpan_push_hc_data(&hc_ptr,
  520. &hdr->daddr.s6_addr[11], 5);
  521. } else {
  522. pr_debug("using full address\n");
  523. iphc1 |= LOWPAN_IPHC_DAM_00;
  524. lowpan_push_hc_data(&hc_ptr, hdr->daddr.s6_addr, 16);
  525. }
  526. } else {
  527. if (addr_type & IPV6_ADDR_LINKLOCAL) {
  528. /* TODO: context lookup */
  529. iphc1 |= lowpan_compress_addr_64(&hc_ptr,
  530. LOWPAN_IPHC_DAM_BIT, &hdr->daddr, _daddr);
  531. pr_debug("dest address unicast link-local %pI6c "
  532. "iphc1 0x%02x\n", &hdr->daddr, iphc1);
  533. } else {
  534. pr_debug("dest address unicast %pI6c\n", &hdr->daddr);
  535. lowpan_push_hc_data(&hc_ptr, hdr->daddr.s6_addr, 16);
  536. }
  537. }
  538. /* next header compression */
  539. if (iphc0 & LOWPAN_IPHC_NH_C) {
  540. ret = lowpan_nhc_do_compression(skb, hdr, &hc_ptr);
  541. if (ret < 0)
  542. return ret;
  543. }
  544. head[0] = iphc0;
  545. head[1] = iphc1;
  546. skb_pull(skb, sizeof(struct ipv6hdr));
  547. skb_reset_transport_header(skb);
  548. memcpy(skb_push(skb, hc_ptr - head), head, hc_ptr - head);
  549. skb_reset_network_header(skb);
  550. pr_debug("header len %d skb %u\n", (int)(hc_ptr - head), skb->len);
  551. raw_dump_table(__func__, "raw skb data dump compressed",
  552. skb->data, skb->len);
  553. return 0;
  554. }
  555. EXPORT_SYMBOL_GPL(lowpan_header_compress);