iphc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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. hdr.payload_len = htons(skb->len);
  333. pr_debug("skb headroom size = %d, data length = %d\n",
  334. skb_headroom(skb), skb->len);
  335. pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength = %d\n\t"
  336. "nexthdr = 0x%02x\n\thop_lim = %d\n\tdest = %pI6c\n",
  337. hdr.version, ntohs(hdr.payload_len), hdr.nexthdr,
  338. hdr.hop_limit, &hdr.daddr);
  339. skb_push(skb, sizeof(hdr));
  340. skb_reset_network_header(skb);
  341. skb_copy_to_linear_data(skb, &hdr, sizeof(hdr));
  342. raw_dump_table(__func__, "raw header dump", (u8 *)&hdr, sizeof(hdr));
  343. return 0;
  344. }
  345. EXPORT_SYMBOL_GPL(lowpan_header_decompress);
  346. static u8 lowpan_compress_addr_64(u8 **hc_ptr, u8 shift,
  347. const struct in6_addr *ipaddr,
  348. const unsigned char *lladdr)
  349. {
  350. u8 val = 0;
  351. if (is_addr_mac_addr_based(ipaddr, lladdr)) {
  352. val = 3; /* 0-bits */
  353. pr_debug("address compression 0 bits\n");
  354. } else if (lowpan_is_iid_16_bit_compressable(ipaddr)) {
  355. /* compress IID to 16 bits xxxx::XXXX */
  356. lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr16[7], 2);
  357. val = 2; /* 16-bits */
  358. raw_dump_inline(NULL, "Compressed ipv6 addr is (16 bits)",
  359. *hc_ptr - 2, 2);
  360. } else {
  361. /* do not compress IID => xxxx::IID */
  362. lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr16[4], 8);
  363. val = 1; /* 64-bits */
  364. raw_dump_inline(NULL, "Compressed ipv6 addr is (64 bits)",
  365. *hc_ptr - 8, 8);
  366. }
  367. return rol8(val, shift);
  368. }
  369. int lowpan_header_compress(struct sk_buff *skb, struct net_device *dev,
  370. unsigned short type, const void *_daddr,
  371. const void *_saddr, unsigned int len)
  372. {
  373. u8 tmp, iphc0, iphc1, *hc_ptr;
  374. struct ipv6hdr *hdr;
  375. u8 head[100] = {};
  376. int ret, addr_type;
  377. if (type != ETH_P_IPV6)
  378. return -EINVAL;
  379. hdr = ipv6_hdr(skb);
  380. hc_ptr = head + 2;
  381. pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength = %d\n"
  382. "\tnexthdr = 0x%02x\n\thop_lim = %d\n\tdest = %pI6c\n",
  383. hdr->version, ntohs(hdr->payload_len), hdr->nexthdr,
  384. hdr->hop_limit, &hdr->daddr);
  385. raw_dump_table(__func__, "raw skb network header dump",
  386. skb_network_header(skb), sizeof(struct ipv6hdr));
  387. /* As we copy some bit-length fields, in the IPHC encoding bytes,
  388. * we sometimes use |=
  389. * If the field is 0, and the current bit value in memory is 1,
  390. * this does not work. We therefore reset the IPHC encoding here
  391. */
  392. iphc0 = LOWPAN_DISPATCH_IPHC;
  393. iphc1 = 0;
  394. /* TODO: context lookup */
  395. raw_dump_inline(__func__, "saddr",
  396. (unsigned char *)_saddr, IEEE802154_ADDR_LEN);
  397. raw_dump_inline(__func__, "daddr",
  398. (unsigned char *)_daddr, IEEE802154_ADDR_LEN);
  399. raw_dump_table(__func__, "sending raw skb network uncompressed packet",
  400. skb->data, skb->len);
  401. /* Traffic class, flow label
  402. * If flow label is 0, compress it. If traffic class is 0, compress it
  403. * We have to process both in the same time as the offset of traffic
  404. * class depends on the presence of version and flow label
  405. */
  406. /* hc format of TC is ECN | DSCP , original one is DSCP | ECN */
  407. tmp = (hdr->priority << 4) | (hdr->flow_lbl[0] >> 4);
  408. tmp = ((tmp & 0x03) << 6) | (tmp >> 2);
  409. if (((hdr->flow_lbl[0] & 0x0F) == 0) &&
  410. (hdr->flow_lbl[1] == 0) && (hdr->flow_lbl[2] == 0)) {
  411. /* flow label can be compressed */
  412. iphc0 |= LOWPAN_IPHC_FL_C;
  413. if ((hdr->priority == 0) &&
  414. ((hdr->flow_lbl[0] & 0xF0) == 0)) {
  415. /* compress (elide) all */
  416. iphc0 |= LOWPAN_IPHC_TC_C;
  417. } else {
  418. /* compress only the flow label */
  419. *hc_ptr = tmp;
  420. hc_ptr += 1;
  421. }
  422. } else {
  423. /* Flow label cannot be compressed */
  424. if ((hdr->priority == 0) &&
  425. ((hdr->flow_lbl[0] & 0xF0) == 0)) {
  426. /* compress only traffic class */
  427. iphc0 |= LOWPAN_IPHC_TC_C;
  428. *hc_ptr = (tmp & 0xc0) | (hdr->flow_lbl[0] & 0x0F);
  429. memcpy(hc_ptr + 1, &hdr->flow_lbl[1], 2);
  430. hc_ptr += 3;
  431. } else {
  432. /* compress nothing */
  433. memcpy(hc_ptr, hdr, 4);
  434. /* replace the top byte with new ECN | DSCP format */
  435. *hc_ptr = tmp;
  436. hc_ptr += 4;
  437. }
  438. }
  439. /* NOTE: payload length is always compressed */
  440. /* Check if we provide the nhc format for nexthdr and compression
  441. * functionality. If not nexthdr is handled inline and not compressed.
  442. */
  443. ret = lowpan_nhc_check_compression(skb, hdr, &hc_ptr, &iphc0);
  444. if (ret < 0)
  445. return ret;
  446. /* Hop limit
  447. * if 1: compress, encoding is 01
  448. * if 64: compress, encoding is 10
  449. * if 255: compress, encoding is 11
  450. * else do not compress
  451. */
  452. switch (hdr->hop_limit) {
  453. case 1:
  454. iphc0 |= LOWPAN_IPHC_TTL_1;
  455. break;
  456. case 64:
  457. iphc0 |= LOWPAN_IPHC_TTL_64;
  458. break;
  459. case 255:
  460. iphc0 |= LOWPAN_IPHC_TTL_255;
  461. break;
  462. default:
  463. lowpan_push_hc_data(&hc_ptr, &hdr->hop_limit,
  464. sizeof(hdr->hop_limit));
  465. }
  466. addr_type = ipv6_addr_type(&hdr->saddr);
  467. /* source address compression */
  468. if (addr_type == IPV6_ADDR_ANY) {
  469. pr_debug("source address is unspecified, setting SAC\n");
  470. iphc1 |= LOWPAN_IPHC_SAC;
  471. } else {
  472. if (addr_type & IPV6_ADDR_LINKLOCAL) {
  473. iphc1 |= lowpan_compress_addr_64(&hc_ptr,
  474. LOWPAN_IPHC_SAM_BIT,
  475. &hdr->saddr, _saddr);
  476. pr_debug("source address unicast link-local %pI6c iphc1 0x%02x\n",
  477. &hdr->saddr, iphc1);
  478. } else {
  479. pr_debug("send the full source address\n");
  480. lowpan_push_hc_data(&hc_ptr, hdr->saddr.s6_addr, 16);
  481. }
  482. }
  483. addr_type = ipv6_addr_type(&hdr->daddr);
  484. /* destination address compression */
  485. if (addr_type & IPV6_ADDR_MULTICAST) {
  486. pr_debug("destination address is multicast: ");
  487. iphc1 |= LOWPAN_IPHC_M;
  488. if (lowpan_is_mcast_addr_compressable8(&hdr->daddr)) {
  489. pr_debug("compressed to 1 octet\n");
  490. iphc1 |= LOWPAN_IPHC_DAM_11;
  491. /* use last byte */
  492. lowpan_push_hc_data(&hc_ptr,
  493. &hdr->daddr.s6_addr[15], 1);
  494. } else if (lowpan_is_mcast_addr_compressable32(&hdr->daddr)) {
  495. pr_debug("compressed to 4 octets\n");
  496. iphc1 |= LOWPAN_IPHC_DAM_10;
  497. /* second byte + the last three */
  498. lowpan_push_hc_data(&hc_ptr,
  499. &hdr->daddr.s6_addr[1], 1);
  500. lowpan_push_hc_data(&hc_ptr,
  501. &hdr->daddr.s6_addr[13], 3);
  502. } else if (lowpan_is_mcast_addr_compressable48(&hdr->daddr)) {
  503. pr_debug("compressed to 6 octets\n");
  504. iphc1 |= LOWPAN_IPHC_DAM_01;
  505. /* second byte + the last five */
  506. lowpan_push_hc_data(&hc_ptr,
  507. &hdr->daddr.s6_addr[1], 1);
  508. lowpan_push_hc_data(&hc_ptr,
  509. &hdr->daddr.s6_addr[11], 5);
  510. } else {
  511. pr_debug("using full address\n");
  512. iphc1 |= LOWPAN_IPHC_DAM_00;
  513. lowpan_push_hc_data(&hc_ptr, hdr->daddr.s6_addr, 16);
  514. }
  515. } else {
  516. if (addr_type & IPV6_ADDR_LINKLOCAL) {
  517. /* TODO: context lookup */
  518. iphc1 |= lowpan_compress_addr_64(&hc_ptr,
  519. LOWPAN_IPHC_DAM_BIT, &hdr->daddr, _daddr);
  520. pr_debug("dest address unicast link-local %pI6c "
  521. "iphc1 0x%02x\n", &hdr->daddr, iphc1);
  522. } else {
  523. pr_debug("dest address unicast %pI6c\n", &hdr->daddr);
  524. lowpan_push_hc_data(&hc_ptr, hdr->daddr.s6_addr, 16);
  525. }
  526. }
  527. /* next header compression */
  528. if (iphc0 & LOWPAN_IPHC_NH_C) {
  529. ret = lowpan_nhc_do_compression(skb, hdr, &hc_ptr);
  530. if (ret < 0)
  531. return ret;
  532. }
  533. head[0] = iphc0;
  534. head[1] = iphc1;
  535. skb_pull(skb, sizeof(struct ipv6hdr));
  536. skb_reset_transport_header(skb);
  537. memcpy(skb_push(skb, hc_ptr - head), head, hc_ptr - head);
  538. skb_reset_network_header(skb);
  539. pr_debug("header len %d skb %u\n", (int)(hc_ptr - head), skb->len);
  540. raw_dump_table(__func__, "raw skb data dump compressed",
  541. skb->data, skb->len);
  542. return 0;
  543. }
  544. EXPORT_SYMBOL_GPL(lowpan_header_compress);