6lowpan_iphc.c 22 KB

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