iphc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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. goto drop;
  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. goto drop;
  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. goto drop;
  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. goto drop;
  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. goto drop;
  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. goto drop;
  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. goto drop;
  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. goto drop;
  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. goto drop;
  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. goto drop;
  394. /* replace the compressed UDP head by the uncompressed UDP
  395. * header
  396. */
  397. err = skb_cow(skb, needed);
  398. if (unlikely(err)) {
  399. kfree_skb(skb);
  400. return err;
  401. }
  402. skb_push(skb, sizeof(struct udphdr));
  403. skb_reset_transport_header(skb);
  404. skb_copy_to_linear_data(skb, &uh, sizeof(struct udphdr));
  405. raw_dump_table(__func__, "raw UDP header dump",
  406. (u8 *)&uh, sizeof(uh));
  407. hdr.nexthdr = UIP_PROTO_UDP;
  408. } else {
  409. err = skb_cow(skb, sizeof(hdr));
  410. if (unlikely(err)) {
  411. kfree_skb(skb);
  412. return err;
  413. }
  414. }
  415. hdr.payload_len = htons(skb->len);
  416. pr_debug("skb headroom size = %d, data length = %d\n",
  417. skb_headroom(skb), skb->len);
  418. pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength = %d\n\t"
  419. "nexthdr = 0x%02x\n\thop_lim = %d\n\tdest = %pI6c\n",
  420. hdr.version, ntohs(hdr.payload_len), hdr.nexthdr,
  421. hdr.hop_limit, &hdr.daddr);
  422. skb_push(skb, sizeof(hdr));
  423. skb_reset_network_header(skb);
  424. skb_copy_to_linear_data(skb, &hdr, sizeof(hdr));
  425. raw_dump_table(__func__, "raw header dump", (u8 *)&hdr, sizeof(hdr));
  426. return 0;
  427. drop:
  428. kfree_skb(skb);
  429. return -EINVAL;
  430. }
  431. EXPORT_SYMBOL_GPL(lowpan_header_decompress);
  432. static u8 lowpan_compress_addr_64(u8 **hc_ptr, u8 shift,
  433. const struct in6_addr *ipaddr,
  434. const unsigned char *lladdr)
  435. {
  436. u8 val = 0;
  437. if (is_addr_mac_addr_based(ipaddr, lladdr)) {
  438. val = 3; /* 0-bits */
  439. pr_debug("address compression 0 bits\n");
  440. } else if (lowpan_is_iid_16_bit_compressable(ipaddr)) {
  441. /* compress IID to 16 bits xxxx::XXXX */
  442. lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr16[7], 2);
  443. val = 2; /* 16-bits */
  444. raw_dump_inline(NULL, "Compressed ipv6 addr is (16 bits)",
  445. *hc_ptr - 2, 2);
  446. } else {
  447. /* do not compress IID => xxxx::IID */
  448. lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr16[4], 8);
  449. val = 1; /* 64-bits */
  450. raw_dump_inline(NULL, "Compressed ipv6 addr is (64 bits)",
  451. *hc_ptr - 8, 8);
  452. }
  453. return rol8(val, shift);
  454. }
  455. static void compress_udp_header(u8 **hc_ptr, struct sk_buff *skb)
  456. {
  457. struct udphdr *uh = udp_hdr(skb);
  458. u8 tmp;
  459. if (((ntohs(uh->source) & LOWPAN_NHC_UDP_4BIT_MASK) ==
  460. LOWPAN_NHC_UDP_4BIT_PORT) &&
  461. ((ntohs(uh->dest) & LOWPAN_NHC_UDP_4BIT_MASK) ==
  462. LOWPAN_NHC_UDP_4BIT_PORT)) {
  463. pr_debug("UDP header: both ports compression to 4 bits\n");
  464. /* compression value */
  465. tmp = LOWPAN_NHC_UDP_CS_P_11;
  466. lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
  467. /* source and destination port */
  468. tmp = ntohs(uh->dest) - LOWPAN_NHC_UDP_4BIT_PORT +
  469. ((ntohs(uh->source) - LOWPAN_NHC_UDP_4BIT_PORT) << 4);
  470. lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
  471. } else if ((ntohs(uh->dest) & LOWPAN_NHC_UDP_8BIT_MASK) ==
  472. LOWPAN_NHC_UDP_8BIT_PORT) {
  473. pr_debug("UDP header: remove 8 bits of dest\n");
  474. /* compression value */
  475. tmp = LOWPAN_NHC_UDP_CS_P_01;
  476. lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
  477. /* source port */
  478. lowpan_push_hc_data(hc_ptr, &uh->source, sizeof(uh->source));
  479. /* destination port */
  480. tmp = ntohs(uh->dest) - LOWPAN_NHC_UDP_8BIT_PORT;
  481. lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
  482. } else if ((ntohs(uh->source) & LOWPAN_NHC_UDP_8BIT_MASK) ==
  483. LOWPAN_NHC_UDP_8BIT_PORT) {
  484. pr_debug("UDP header: remove 8 bits of source\n");
  485. /* compression value */
  486. tmp = LOWPAN_NHC_UDP_CS_P_10;
  487. lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
  488. /* source port */
  489. tmp = ntohs(uh->source) - LOWPAN_NHC_UDP_8BIT_PORT;
  490. lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
  491. /* destination port */
  492. lowpan_push_hc_data(hc_ptr, &uh->dest, sizeof(uh->dest));
  493. } else {
  494. pr_debug("UDP header: can't compress\n");
  495. /* compression value */
  496. tmp = LOWPAN_NHC_UDP_CS_P_00;
  497. lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
  498. /* source port */
  499. lowpan_push_hc_data(hc_ptr, &uh->source, sizeof(uh->source));
  500. /* destination port */
  501. lowpan_push_hc_data(hc_ptr, &uh->dest, sizeof(uh->dest));
  502. }
  503. /* checksum is always inline */
  504. lowpan_push_hc_data(hc_ptr, &uh->check, sizeof(uh->check));
  505. /* skip the UDP header */
  506. skb_pull(skb, sizeof(struct udphdr));
  507. }
  508. int lowpan_header_compress(struct sk_buff *skb, struct net_device *dev,
  509. unsigned short type, const void *_daddr,
  510. const void *_saddr, unsigned int len)
  511. {
  512. u8 tmp, iphc0, iphc1, *hc_ptr;
  513. struct ipv6hdr *hdr;
  514. u8 head[100] = {};
  515. int addr_type;
  516. if (type != ETH_P_IPV6)
  517. return -EINVAL;
  518. hdr = ipv6_hdr(skb);
  519. hc_ptr = head + 2;
  520. pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength = %d\n"
  521. "\tnexthdr = 0x%02x\n\thop_lim = %d\n\tdest = %pI6c\n",
  522. hdr->version, ntohs(hdr->payload_len), hdr->nexthdr,
  523. hdr->hop_limit, &hdr->daddr);
  524. raw_dump_table(__func__, "raw skb network header dump",
  525. skb_network_header(skb), sizeof(struct ipv6hdr));
  526. /* As we copy some bit-length fields, in the IPHC encoding bytes,
  527. * we sometimes use |=
  528. * If the field is 0, and the current bit value in memory is 1,
  529. * this does not work. We therefore reset the IPHC encoding here
  530. */
  531. iphc0 = LOWPAN_DISPATCH_IPHC;
  532. iphc1 = 0;
  533. /* TODO: context lookup */
  534. raw_dump_inline(__func__, "saddr",
  535. (unsigned char *)_saddr, IEEE802154_ADDR_LEN);
  536. raw_dump_inline(__func__, "daddr",
  537. (unsigned char *)_daddr, IEEE802154_ADDR_LEN);
  538. raw_dump_table(__func__, "sending raw skb network uncompressed packet",
  539. skb->data, skb->len);
  540. /* Traffic class, flow label
  541. * If flow label is 0, compress it. If traffic class is 0, compress it
  542. * We have to process both in the same time as the offset of traffic
  543. * class depends on the presence of version and flow label
  544. */
  545. /* hc format of TC is ECN | DSCP , original one is DSCP | ECN */
  546. tmp = (hdr->priority << 4) | (hdr->flow_lbl[0] >> 4);
  547. tmp = ((tmp & 0x03) << 6) | (tmp >> 2);
  548. if (((hdr->flow_lbl[0] & 0x0F) == 0) &&
  549. (hdr->flow_lbl[1] == 0) && (hdr->flow_lbl[2] == 0)) {
  550. /* flow label can be compressed */
  551. iphc0 |= LOWPAN_IPHC_FL_C;
  552. if ((hdr->priority == 0) &&
  553. ((hdr->flow_lbl[0] & 0xF0) == 0)) {
  554. /* compress (elide) all */
  555. iphc0 |= LOWPAN_IPHC_TC_C;
  556. } else {
  557. /* compress only the flow label */
  558. *hc_ptr = tmp;
  559. hc_ptr += 1;
  560. }
  561. } else {
  562. /* Flow label cannot be compressed */
  563. if ((hdr->priority == 0) &&
  564. ((hdr->flow_lbl[0] & 0xF0) == 0)) {
  565. /* compress only traffic class */
  566. iphc0 |= LOWPAN_IPHC_TC_C;
  567. *hc_ptr = (tmp & 0xc0) | (hdr->flow_lbl[0] & 0x0F);
  568. memcpy(hc_ptr + 1, &hdr->flow_lbl[1], 2);
  569. hc_ptr += 3;
  570. } else {
  571. /* compress nothing */
  572. memcpy(hc_ptr, hdr, 4);
  573. /* replace the top byte with new ECN | DSCP format */
  574. *hc_ptr = tmp;
  575. hc_ptr += 4;
  576. }
  577. }
  578. /* NOTE: payload length is always compressed */
  579. /* Next Header is compress if UDP */
  580. if (hdr->nexthdr == UIP_PROTO_UDP)
  581. iphc0 |= LOWPAN_IPHC_NH_C;
  582. if ((iphc0 & LOWPAN_IPHC_NH_C) == 0)
  583. lowpan_push_hc_data(&hc_ptr, &hdr->nexthdr,
  584. sizeof(hdr->nexthdr));
  585. /* Hop limit
  586. * if 1: compress, encoding is 01
  587. * if 64: compress, encoding is 10
  588. * if 255: compress, encoding is 11
  589. * else do not compress
  590. */
  591. switch (hdr->hop_limit) {
  592. case 1:
  593. iphc0 |= LOWPAN_IPHC_TTL_1;
  594. break;
  595. case 64:
  596. iphc0 |= LOWPAN_IPHC_TTL_64;
  597. break;
  598. case 255:
  599. iphc0 |= LOWPAN_IPHC_TTL_255;
  600. break;
  601. default:
  602. lowpan_push_hc_data(&hc_ptr, &hdr->hop_limit,
  603. sizeof(hdr->hop_limit));
  604. }
  605. addr_type = ipv6_addr_type(&hdr->saddr);
  606. /* source address compression */
  607. if (addr_type == IPV6_ADDR_ANY) {
  608. pr_debug("source address is unspecified, setting SAC\n");
  609. iphc1 |= LOWPAN_IPHC_SAC;
  610. } else {
  611. if (addr_type & IPV6_ADDR_LINKLOCAL) {
  612. iphc1 |= lowpan_compress_addr_64(&hc_ptr,
  613. LOWPAN_IPHC_SAM_BIT,
  614. &hdr->saddr, _saddr);
  615. pr_debug("source address unicast link-local %pI6c iphc1 0x%02x\n",
  616. &hdr->saddr, iphc1);
  617. } else {
  618. pr_debug("send the full source address\n");
  619. lowpan_push_hc_data(&hc_ptr, hdr->saddr.s6_addr, 16);
  620. }
  621. }
  622. addr_type = ipv6_addr_type(&hdr->daddr);
  623. /* destination address compression */
  624. if (addr_type & IPV6_ADDR_MULTICAST) {
  625. pr_debug("destination address is multicast: ");
  626. iphc1 |= LOWPAN_IPHC_M;
  627. if (lowpan_is_mcast_addr_compressable8(&hdr->daddr)) {
  628. pr_debug("compressed to 1 octet\n");
  629. iphc1 |= LOWPAN_IPHC_DAM_11;
  630. /* use last byte */
  631. lowpan_push_hc_data(&hc_ptr,
  632. &hdr->daddr.s6_addr[15], 1);
  633. } else if (lowpan_is_mcast_addr_compressable32(&hdr->daddr)) {
  634. pr_debug("compressed to 4 octets\n");
  635. iphc1 |= LOWPAN_IPHC_DAM_10;
  636. /* second byte + the last three */
  637. lowpan_push_hc_data(&hc_ptr,
  638. &hdr->daddr.s6_addr[1], 1);
  639. lowpan_push_hc_data(&hc_ptr,
  640. &hdr->daddr.s6_addr[13], 3);
  641. } else if (lowpan_is_mcast_addr_compressable48(&hdr->daddr)) {
  642. pr_debug("compressed to 6 octets\n");
  643. iphc1 |= LOWPAN_IPHC_DAM_01;
  644. /* second byte + the last five */
  645. lowpan_push_hc_data(&hc_ptr,
  646. &hdr->daddr.s6_addr[1], 1);
  647. lowpan_push_hc_data(&hc_ptr,
  648. &hdr->daddr.s6_addr[11], 5);
  649. } else {
  650. pr_debug("using full address\n");
  651. iphc1 |= LOWPAN_IPHC_DAM_00;
  652. lowpan_push_hc_data(&hc_ptr, hdr->daddr.s6_addr, 16);
  653. }
  654. } else {
  655. if (addr_type & IPV6_ADDR_LINKLOCAL) {
  656. /* TODO: context lookup */
  657. iphc1 |= lowpan_compress_addr_64(&hc_ptr,
  658. LOWPAN_IPHC_DAM_BIT, &hdr->daddr, _daddr);
  659. pr_debug("dest address unicast link-local %pI6c "
  660. "iphc1 0x%02x\n", &hdr->daddr, iphc1);
  661. } else {
  662. pr_debug("dest address unicast %pI6c\n", &hdr->daddr);
  663. lowpan_push_hc_data(&hc_ptr, hdr->daddr.s6_addr, 16);
  664. }
  665. }
  666. /* UDP header compression */
  667. if (hdr->nexthdr == UIP_PROTO_UDP)
  668. compress_udp_header(&hc_ptr, skb);
  669. head[0] = iphc0;
  670. head[1] = iphc1;
  671. skb_pull(skb, sizeof(struct ipv6hdr));
  672. skb_reset_transport_header(skb);
  673. memcpy(skb_push(skb, hc_ptr - head), head, hc_ptr - head);
  674. skb_reset_network_header(skb);
  675. pr_debug("header len %d skb %u\n", (int)(hc_ptr - head), skb->len);
  676. raw_dump_table(__func__, "raw skb data dump compressed",
  677. skb->data, skb->len);
  678. return 0;
  679. }
  680. EXPORT_SYMBOL_GPL(lowpan_header_compress);
  681. MODULE_LICENSE("GPL");