iphc.c 22 KB

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