iphc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  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. static int skb_deliver(struct sk_buff *skb, struct ipv6hdr *hdr,
  163. struct net_device *dev, skb_delivery_cb deliver_skb)
  164. {
  165. struct sk_buff *new;
  166. int stat;
  167. new = skb_copy_expand(skb, sizeof(struct ipv6hdr), skb_tailroom(skb),
  168. GFP_ATOMIC);
  169. kfree_skb(skb);
  170. if (!new)
  171. return -ENOMEM;
  172. skb_push(new, sizeof(struct ipv6hdr));
  173. skb_reset_network_header(new);
  174. skb_copy_to_linear_data(new, hdr, sizeof(struct ipv6hdr));
  175. new->protocol = htons(ETH_P_IPV6);
  176. new->pkt_type = PACKET_HOST;
  177. new->dev = dev;
  178. raw_dump_table(__func__, "raw skb data dump before receiving",
  179. new->data, new->len);
  180. stat = deliver_skb(new, dev);
  181. kfree_skb(new);
  182. return stat;
  183. }
  184. /* Uncompress function for multicast destination address,
  185. * when M bit is set.
  186. */
  187. static int lowpan_uncompress_multicast_daddr(struct sk_buff *skb,
  188. struct in6_addr *ipaddr,
  189. const u8 dam)
  190. {
  191. bool fail;
  192. switch (dam) {
  193. case LOWPAN_IPHC_DAM_00:
  194. /* 00: 128 bits. The full address
  195. * is carried in-line.
  196. */
  197. fail = lowpan_fetch_skb(skb, ipaddr->s6_addr, 16);
  198. break;
  199. case LOWPAN_IPHC_DAM_01:
  200. /* 01: 48 bits. The address takes
  201. * the form ffXX::00XX:XXXX:XXXX.
  202. */
  203. ipaddr->s6_addr[0] = 0xFF;
  204. fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[1], 1);
  205. fail |= lowpan_fetch_skb(skb, &ipaddr->s6_addr[11], 5);
  206. break;
  207. case LOWPAN_IPHC_DAM_10:
  208. /* 10: 32 bits. The address takes
  209. * the form ffXX::00XX:XXXX.
  210. */
  211. ipaddr->s6_addr[0] = 0xFF;
  212. fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[1], 1);
  213. fail |= lowpan_fetch_skb(skb, &ipaddr->s6_addr[13], 3);
  214. break;
  215. case LOWPAN_IPHC_DAM_11:
  216. /* 11: 8 bits. The address takes
  217. * the form ff02::00XX.
  218. */
  219. ipaddr->s6_addr[0] = 0xFF;
  220. ipaddr->s6_addr[1] = 0x02;
  221. fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[15], 1);
  222. break;
  223. default:
  224. pr_debug("DAM value has a wrong value: 0x%x\n", dam);
  225. return -EINVAL;
  226. }
  227. if (fail) {
  228. pr_debug("Failed to fetch skb data\n");
  229. return -EIO;
  230. }
  231. raw_dump_inline(NULL, "Reconstructed ipv6 multicast addr is",
  232. ipaddr->s6_addr, 16);
  233. return 0;
  234. }
  235. static int uncompress_udp_header(struct sk_buff *skb, struct udphdr *uh)
  236. {
  237. bool fail;
  238. u8 tmp = 0, val = 0;
  239. fail = lowpan_fetch_skb(skb, &tmp, sizeof(tmp));
  240. if ((tmp & LOWPAN_NHC_UDP_MASK) == LOWPAN_NHC_UDP_ID) {
  241. pr_debug("UDP header uncompression\n");
  242. switch (tmp & LOWPAN_NHC_UDP_CS_P_11) {
  243. case LOWPAN_NHC_UDP_CS_P_00:
  244. fail |= lowpan_fetch_skb(skb, &uh->source,
  245. sizeof(uh->source));
  246. fail |= lowpan_fetch_skb(skb, &uh->dest,
  247. sizeof(uh->dest));
  248. break;
  249. case LOWPAN_NHC_UDP_CS_P_01:
  250. fail |= lowpan_fetch_skb(skb, &uh->source,
  251. sizeof(uh->source));
  252. fail |= lowpan_fetch_skb(skb, &val, sizeof(val));
  253. uh->dest = htons(val + LOWPAN_NHC_UDP_8BIT_PORT);
  254. break;
  255. case LOWPAN_NHC_UDP_CS_P_10:
  256. fail |= lowpan_fetch_skb(skb, &val, sizeof(val));
  257. uh->source = htons(val + LOWPAN_NHC_UDP_8BIT_PORT);
  258. fail |= lowpan_fetch_skb(skb, &uh->dest,
  259. sizeof(uh->dest));
  260. break;
  261. case LOWPAN_NHC_UDP_CS_P_11:
  262. fail |= lowpan_fetch_skb(skb, &val, sizeof(val));
  263. uh->source = htons(LOWPAN_NHC_UDP_4BIT_PORT +
  264. (val >> 4));
  265. uh->dest = htons(LOWPAN_NHC_UDP_4BIT_PORT +
  266. (val & 0x0f));
  267. break;
  268. default:
  269. pr_debug("ERROR: unknown UDP format\n");
  270. goto err;
  271. }
  272. pr_debug("uncompressed UDP ports: src = %d, dst = %d\n",
  273. ntohs(uh->source), ntohs(uh->dest));
  274. /* checksum */
  275. if (tmp & LOWPAN_NHC_UDP_CS_C) {
  276. pr_debug_ratelimited("checksum elided currently not supported\n");
  277. goto err;
  278. } else {
  279. fail |= lowpan_fetch_skb(skb, &uh->check,
  280. sizeof(uh->check));
  281. }
  282. /* UDP length needs to be infered from the lower layers
  283. * here, we obtain the hint from the remaining size of the
  284. * frame
  285. */
  286. uh->len = htons(skb->len + sizeof(struct udphdr));
  287. pr_debug("uncompressed UDP length: src = %d", ntohs(uh->len));
  288. } else {
  289. pr_debug("ERROR: unsupported NH format\n");
  290. goto err;
  291. }
  292. if (fail)
  293. goto err;
  294. return 0;
  295. err:
  296. return -EINVAL;
  297. }
  298. /* TTL uncompression values */
  299. static const u8 lowpan_ttl_values[] = { 0, 1, 64, 255 };
  300. int lowpan_process_data(struct sk_buff *skb, struct net_device *dev,
  301. const u8 *saddr, const u8 saddr_type, const u8 saddr_len,
  302. const u8 *daddr, const u8 daddr_type, const u8 daddr_len,
  303. u8 iphc0, u8 iphc1, skb_delivery_cb deliver_skb)
  304. {
  305. struct ipv6hdr hdr = {};
  306. u8 tmp, num_context = 0;
  307. int err;
  308. raw_dump_table(__func__, "raw skb data dump uncompressed",
  309. skb->data, skb->len);
  310. /* another if the CID flag is set */
  311. if (iphc1 & LOWPAN_IPHC_CID) {
  312. pr_debug("CID flag is set, increase header with one\n");
  313. if (lowpan_fetch_skb(skb, &num_context, sizeof(num_context)))
  314. goto drop;
  315. }
  316. hdr.version = 6;
  317. /* Traffic Class and Flow Label */
  318. switch ((iphc0 & LOWPAN_IPHC_TF) >> 3) {
  319. /* Traffic Class and FLow Label carried in-line
  320. * ECN + DSCP + 4-bit Pad + Flow Label (4 bytes)
  321. */
  322. case 0: /* 00b */
  323. if (lowpan_fetch_skb(skb, &tmp, sizeof(tmp)))
  324. goto drop;
  325. memcpy(&hdr.flow_lbl, &skb->data[0], 3);
  326. skb_pull(skb, 3);
  327. hdr.priority = ((tmp >> 2) & 0x0f);
  328. hdr.flow_lbl[0] = ((tmp >> 2) & 0x30) | (tmp << 6) |
  329. (hdr.flow_lbl[0] & 0x0f);
  330. break;
  331. /* Traffic class carried in-line
  332. * ECN + DSCP (1 byte), Flow Label is elided
  333. */
  334. case 2: /* 10b */
  335. if (lowpan_fetch_skb(skb, &tmp, sizeof(tmp)))
  336. goto drop;
  337. hdr.priority = ((tmp >> 2) & 0x0f);
  338. hdr.flow_lbl[0] = ((tmp << 6) & 0xC0) | ((tmp >> 2) & 0x30);
  339. break;
  340. /* Flow Label carried in-line
  341. * ECN + 2-bit Pad + Flow Label (3 bytes), DSCP is elided
  342. */
  343. case 1: /* 01b */
  344. if (lowpan_fetch_skb(skb, &tmp, sizeof(tmp)))
  345. goto drop;
  346. hdr.flow_lbl[0] = (skb->data[0] & 0x0F) | ((tmp >> 2) & 0x30);
  347. memcpy(&hdr.flow_lbl[1], &skb->data[0], 2);
  348. skb_pull(skb, 2);
  349. break;
  350. /* Traffic Class and Flow Label are elided */
  351. case 3: /* 11b */
  352. break;
  353. default:
  354. break;
  355. }
  356. /* Next Header */
  357. if ((iphc0 & LOWPAN_IPHC_NH_C) == 0) {
  358. /* Next header is carried inline */
  359. if (lowpan_fetch_skb(skb, &hdr.nexthdr, sizeof(hdr.nexthdr)))
  360. goto drop;
  361. pr_debug("NH flag is set, next header carried inline: %02x\n",
  362. hdr.nexthdr);
  363. }
  364. /* Hop Limit */
  365. if ((iphc0 & 0x03) != LOWPAN_IPHC_TTL_I) {
  366. hdr.hop_limit = lowpan_ttl_values[iphc0 & 0x03];
  367. } else {
  368. if (lowpan_fetch_skb(skb, &hdr.hop_limit,
  369. sizeof(hdr.hop_limit)))
  370. goto drop;
  371. }
  372. /* Extract SAM to the tmp variable */
  373. tmp = ((iphc1 & LOWPAN_IPHC_SAM) >> LOWPAN_IPHC_SAM_BIT) & 0x03;
  374. if (iphc1 & LOWPAN_IPHC_SAC) {
  375. /* Source address context based uncompression */
  376. pr_debug("SAC bit is set. Handle context based source address.\n");
  377. err = uncompress_context_based_src_addr(skb, &hdr.saddr, tmp);
  378. } else {
  379. /* Source address uncompression */
  380. pr_debug("source address stateless compression\n");
  381. err = uncompress_addr(skb, &hdr.saddr, tmp, saddr,
  382. saddr_type, saddr_len);
  383. }
  384. /* Check on error of previous branch */
  385. if (err)
  386. goto drop;
  387. /* Extract DAM to the tmp variable */
  388. tmp = ((iphc1 & LOWPAN_IPHC_DAM_11) >> LOWPAN_IPHC_DAM_BIT) & 0x03;
  389. /* check for Multicast Compression */
  390. if (iphc1 & LOWPAN_IPHC_M) {
  391. if (iphc1 & LOWPAN_IPHC_DAC) {
  392. pr_debug("dest: context-based mcast compression\n");
  393. /* TODO: implement this */
  394. } else {
  395. err = lowpan_uncompress_multicast_daddr(skb, &hdr.daddr,
  396. tmp);
  397. if (err)
  398. goto drop;
  399. }
  400. } else {
  401. err = uncompress_addr(skb, &hdr.daddr, tmp, daddr,
  402. daddr_type, daddr_len);
  403. pr_debug("dest: stateless compression mode %d dest %pI6c\n",
  404. tmp, &hdr.daddr);
  405. if (err)
  406. goto drop;
  407. }
  408. /* UDP data uncompression */
  409. if (iphc0 & LOWPAN_IPHC_NH_C) {
  410. struct udphdr uh;
  411. struct sk_buff *new;
  412. if (uncompress_udp_header(skb, &uh))
  413. goto drop;
  414. /* replace the compressed UDP head by the uncompressed UDP
  415. * header
  416. */
  417. new = skb_copy_expand(skb, sizeof(struct udphdr),
  418. skb_tailroom(skb), GFP_ATOMIC);
  419. kfree_skb(skb);
  420. if (!new)
  421. return -ENOMEM;
  422. skb = new;
  423. skb_push(skb, sizeof(struct udphdr));
  424. skb_reset_transport_header(skb);
  425. skb_copy_to_linear_data(skb, &uh, sizeof(struct udphdr));
  426. raw_dump_table(__func__, "raw UDP header dump",
  427. (u8 *)&uh, sizeof(uh));
  428. hdr.nexthdr = UIP_PROTO_UDP;
  429. }
  430. hdr.payload_len = htons(skb->len);
  431. pr_debug("skb headroom size = %d, data length = %d\n",
  432. skb_headroom(skb), skb->len);
  433. pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength = %d\n\t"
  434. "nexthdr = 0x%02x\n\thop_lim = %d\n\tdest = %pI6c\n",
  435. hdr.version, ntohs(hdr.payload_len), hdr.nexthdr,
  436. hdr.hop_limit, &hdr.daddr);
  437. raw_dump_table(__func__, "raw header dump", (u8 *)&hdr, sizeof(hdr));
  438. return skb_deliver(skb, &hdr, dev, deliver_skb);
  439. drop:
  440. kfree_skb(skb);
  441. return -EINVAL;
  442. }
  443. EXPORT_SYMBOL_GPL(lowpan_process_data);
  444. static u8 lowpan_compress_addr_64(u8 **hc_ptr, u8 shift,
  445. const struct in6_addr *ipaddr,
  446. const unsigned char *lladdr)
  447. {
  448. u8 val = 0;
  449. if (is_addr_mac_addr_based(ipaddr, lladdr)) {
  450. val = 3; /* 0-bits */
  451. pr_debug("address compression 0 bits\n");
  452. } else if (lowpan_is_iid_16_bit_compressable(ipaddr)) {
  453. /* compress IID to 16 bits xxxx::XXXX */
  454. lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr16[7], 2);
  455. val = 2; /* 16-bits */
  456. raw_dump_inline(NULL, "Compressed ipv6 addr is (16 bits)",
  457. *hc_ptr - 2, 2);
  458. } else {
  459. /* do not compress IID => xxxx::IID */
  460. lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr16[4], 8);
  461. val = 1; /* 64-bits */
  462. raw_dump_inline(NULL, "Compressed ipv6 addr is (64 bits)",
  463. *hc_ptr - 8, 8);
  464. }
  465. return rol8(val, shift);
  466. }
  467. static void compress_udp_header(u8 **hc_ptr, struct sk_buff *skb)
  468. {
  469. struct udphdr *uh = udp_hdr(skb);
  470. u8 tmp;
  471. if (((ntohs(uh->source) & LOWPAN_NHC_UDP_4BIT_MASK) ==
  472. LOWPAN_NHC_UDP_4BIT_PORT) &&
  473. ((ntohs(uh->dest) & LOWPAN_NHC_UDP_4BIT_MASK) ==
  474. LOWPAN_NHC_UDP_4BIT_PORT)) {
  475. pr_debug("UDP header: both ports compression to 4 bits\n");
  476. /* compression value */
  477. tmp = LOWPAN_NHC_UDP_CS_P_11;
  478. lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
  479. /* source and destination port */
  480. tmp = ntohs(uh->dest) - LOWPAN_NHC_UDP_4BIT_PORT +
  481. ((ntohs(uh->source) - LOWPAN_NHC_UDP_4BIT_PORT) << 4);
  482. lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
  483. } else if ((ntohs(uh->dest) & LOWPAN_NHC_UDP_8BIT_MASK) ==
  484. LOWPAN_NHC_UDP_8BIT_PORT) {
  485. pr_debug("UDP header: remove 8 bits of dest\n");
  486. /* compression value */
  487. tmp = LOWPAN_NHC_UDP_CS_P_01;
  488. lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
  489. /* source port */
  490. lowpan_push_hc_data(hc_ptr, &uh->source, sizeof(uh->source));
  491. /* destination port */
  492. tmp = ntohs(uh->dest) - LOWPAN_NHC_UDP_8BIT_PORT;
  493. lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
  494. } else if ((ntohs(uh->source) & LOWPAN_NHC_UDP_8BIT_MASK) ==
  495. LOWPAN_NHC_UDP_8BIT_PORT) {
  496. pr_debug("UDP header: remove 8 bits of source\n");
  497. /* compression value */
  498. tmp = LOWPAN_NHC_UDP_CS_P_10;
  499. lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
  500. /* source port */
  501. tmp = ntohs(uh->source) - LOWPAN_NHC_UDP_8BIT_PORT;
  502. lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
  503. /* destination port */
  504. lowpan_push_hc_data(hc_ptr, &uh->dest, sizeof(uh->dest));
  505. } else {
  506. pr_debug("UDP header: can't compress\n");
  507. /* compression value */
  508. tmp = LOWPAN_NHC_UDP_CS_P_00;
  509. lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
  510. /* source port */
  511. lowpan_push_hc_data(hc_ptr, &uh->source, sizeof(uh->source));
  512. /* destination port */
  513. lowpan_push_hc_data(hc_ptr, &uh->dest, sizeof(uh->dest));
  514. }
  515. /* checksum is always inline */
  516. lowpan_push_hc_data(hc_ptr, &uh->check, sizeof(uh->check));
  517. /* skip the UDP header */
  518. skb_pull(skb, sizeof(struct udphdr));
  519. }
  520. int lowpan_header_compress(struct sk_buff *skb, struct net_device *dev,
  521. unsigned short type, const void *_daddr,
  522. const void *_saddr, unsigned int len)
  523. {
  524. u8 tmp, iphc0, iphc1, *hc_ptr;
  525. struct ipv6hdr *hdr;
  526. u8 head[100] = {};
  527. int addr_type;
  528. if (type != ETH_P_IPV6)
  529. return -EINVAL;
  530. hdr = ipv6_hdr(skb);
  531. hc_ptr = head + 2;
  532. pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength = %d\n"
  533. "\tnexthdr = 0x%02x\n\thop_lim = %d\n\tdest = %pI6c\n",
  534. hdr->version, ntohs(hdr->payload_len), hdr->nexthdr,
  535. hdr->hop_limit, &hdr->daddr);
  536. raw_dump_table(__func__, "raw skb network header dump",
  537. skb_network_header(skb), sizeof(struct ipv6hdr));
  538. /* As we copy some bit-length fields, in the IPHC encoding bytes,
  539. * we sometimes use |=
  540. * If the field is 0, and the current bit value in memory is 1,
  541. * this does not work. We therefore reset the IPHC encoding here
  542. */
  543. iphc0 = LOWPAN_DISPATCH_IPHC;
  544. iphc1 = 0;
  545. /* TODO: context lookup */
  546. raw_dump_inline(__func__, "saddr",
  547. (unsigned char *)_saddr, IEEE802154_ADDR_LEN);
  548. raw_dump_inline(__func__, "daddr",
  549. (unsigned char *)_daddr, IEEE802154_ADDR_LEN);
  550. raw_dump_table(__func__, "sending raw skb network uncompressed packet",
  551. skb->data, skb->len);
  552. /* Traffic class, flow label
  553. * If flow label is 0, compress it. If traffic class is 0, compress it
  554. * We have to process both in the same time as the offset of traffic
  555. * class depends on the presence of version and flow label
  556. */
  557. /* hc format of TC is ECN | DSCP , original one is DSCP | ECN */
  558. tmp = (hdr->priority << 4) | (hdr->flow_lbl[0] >> 4);
  559. tmp = ((tmp & 0x03) << 6) | (tmp >> 2);
  560. if (((hdr->flow_lbl[0] & 0x0F) == 0) &&
  561. (hdr->flow_lbl[1] == 0) && (hdr->flow_lbl[2] == 0)) {
  562. /* flow label can be compressed */
  563. iphc0 |= LOWPAN_IPHC_FL_C;
  564. if ((hdr->priority == 0) &&
  565. ((hdr->flow_lbl[0] & 0xF0) == 0)) {
  566. /* compress (elide) all */
  567. iphc0 |= LOWPAN_IPHC_TC_C;
  568. } else {
  569. /* compress only the flow label */
  570. *hc_ptr = tmp;
  571. hc_ptr += 1;
  572. }
  573. } else {
  574. /* Flow label cannot be compressed */
  575. if ((hdr->priority == 0) &&
  576. ((hdr->flow_lbl[0] & 0xF0) == 0)) {
  577. /* compress only traffic class */
  578. iphc0 |= LOWPAN_IPHC_TC_C;
  579. *hc_ptr = (tmp & 0xc0) | (hdr->flow_lbl[0] & 0x0F);
  580. memcpy(hc_ptr + 1, &hdr->flow_lbl[1], 2);
  581. hc_ptr += 3;
  582. } else {
  583. /* compress nothing */
  584. memcpy(hc_ptr, hdr, 4);
  585. /* replace the top byte with new ECN | DSCP format */
  586. *hc_ptr = tmp;
  587. hc_ptr += 4;
  588. }
  589. }
  590. /* NOTE: payload length is always compressed */
  591. /* Next Header is compress if UDP */
  592. if (hdr->nexthdr == UIP_PROTO_UDP)
  593. iphc0 |= LOWPAN_IPHC_NH_C;
  594. if ((iphc0 & LOWPAN_IPHC_NH_C) == 0)
  595. lowpan_push_hc_data(&hc_ptr, &hdr->nexthdr,
  596. sizeof(hdr->nexthdr));
  597. /* Hop limit
  598. * if 1: compress, encoding is 01
  599. * if 64: compress, encoding is 10
  600. * if 255: compress, encoding is 11
  601. * else do not compress
  602. */
  603. switch (hdr->hop_limit) {
  604. case 1:
  605. iphc0 |= LOWPAN_IPHC_TTL_1;
  606. break;
  607. case 64:
  608. iphc0 |= LOWPAN_IPHC_TTL_64;
  609. break;
  610. case 255:
  611. iphc0 |= LOWPAN_IPHC_TTL_255;
  612. break;
  613. default:
  614. lowpan_push_hc_data(&hc_ptr, &hdr->hop_limit,
  615. sizeof(hdr->hop_limit));
  616. }
  617. addr_type = ipv6_addr_type(&hdr->saddr);
  618. /* source address compression */
  619. if (addr_type == IPV6_ADDR_ANY) {
  620. pr_debug("source address is unspecified, setting SAC\n");
  621. iphc1 |= LOWPAN_IPHC_SAC;
  622. } else {
  623. if (addr_type & IPV6_ADDR_LINKLOCAL) {
  624. iphc1 |= lowpan_compress_addr_64(&hc_ptr,
  625. LOWPAN_IPHC_SAM_BIT,
  626. &hdr->saddr, _saddr);
  627. pr_debug("source address unicast link-local %pI6c iphc1 0x%02x\n",
  628. &hdr->saddr, iphc1);
  629. } else {
  630. pr_debug("send the full source address\n");
  631. lowpan_push_hc_data(&hc_ptr, hdr->saddr.s6_addr, 16);
  632. }
  633. }
  634. addr_type = ipv6_addr_type(&hdr->daddr);
  635. /* destination address compression */
  636. if (addr_type & IPV6_ADDR_MULTICAST) {
  637. pr_debug("destination address is multicast: ");
  638. iphc1 |= LOWPAN_IPHC_M;
  639. if (lowpan_is_mcast_addr_compressable8(&hdr->daddr)) {
  640. pr_debug("compressed to 1 octet\n");
  641. iphc1 |= LOWPAN_IPHC_DAM_11;
  642. /* use last byte */
  643. lowpan_push_hc_data(&hc_ptr,
  644. &hdr->daddr.s6_addr[15], 1);
  645. } else if (lowpan_is_mcast_addr_compressable32(&hdr->daddr)) {
  646. pr_debug("compressed to 4 octets\n");
  647. iphc1 |= LOWPAN_IPHC_DAM_10;
  648. /* second byte + the last three */
  649. lowpan_push_hc_data(&hc_ptr,
  650. &hdr->daddr.s6_addr[1], 1);
  651. lowpan_push_hc_data(&hc_ptr,
  652. &hdr->daddr.s6_addr[13], 3);
  653. } else if (lowpan_is_mcast_addr_compressable48(&hdr->daddr)) {
  654. pr_debug("compressed to 6 octets\n");
  655. iphc1 |= LOWPAN_IPHC_DAM_01;
  656. /* second byte + the last five */
  657. lowpan_push_hc_data(&hc_ptr,
  658. &hdr->daddr.s6_addr[1], 1);
  659. lowpan_push_hc_data(&hc_ptr,
  660. &hdr->daddr.s6_addr[11], 5);
  661. } else {
  662. pr_debug("using full address\n");
  663. iphc1 |= LOWPAN_IPHC_DAM_00;
  664. lowpan_push_hc_data(&hc_ptr, hdr->daddr.s6_addr, 16);
  665. }
  666. } else {
  667. if (addr_type & IPV6_ADDR_LINKLOCAL) {
  668. /* TODO: context lookup */
  669. iphc1 |= lowpan_compress_addr_64(&hc_ptr,
  670. LOWPAN_IPHC_DAM_BIT, &hdr->daddr, _daddr);
  671. pr_debug("dest address unicast link-local %pI6c "
  672. "iphc1 0x%02x\n", &hdr->daddr, iphc1);
  673. } else {
  674. pr_debug("dest address unicast %pI6c\n", &hdr->daddr);
  675. lowpan_push_hc_data(&hc_ptr, hdr->daddr.s6_addr, 16);
  676. }
  677. }
  678. /* UDP header compression */
  679. if (hdr->nexthdr == UIP_PROTO_UDP)
  680. compress_udp_header(&hc_ptr, skb);
  681. head[0] = iphc0;
  682. head[1] = iphc1;
  683. skb_pull(skb, sizeof(struct ipv6hdr));
  684. skb_reset_transport_header(skb);
  685. memcpy(skb_push(skb, hc_ptr - head), head, hc_ptr - head);
  686. skb_reset_network_header(skb);
  687. pr_debug("header len %d skb %u\n", (int)(hc_ptr - head), skb->len);
  688. raw_dump_table(__func__, "raw skb data dump compressed",
  689. skb->data, skb->len);
  690. return 0;
  691. }
  692. EXPORT_SYMBOL_GPL(lowpan_header_compress);
  693. MODULE_LICENSE("GPL");