iphc.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. /*
  2. * Copyright 2011, Siemens AG
  3. * written by Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
  4. */
  5. /* Based on patches from Jon Smirl <jonsmirl@gmail.com>
  6. * Copyright (c) 2011 Jon Smirl <jonsmirl@gmail.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. */
  18. /* Jon's code is based on 6lowpan implementation for Contiki which is:
  19. * Copyright (c) 2008, Swedish Institute of Computer Science.
  20. * All rights reserved.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the above copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. Neither the name of the Institute nor the names of its contributors
  31. * may be used to endorse or promote products derived from this software
  32. * without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  35. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  36. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  37. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  38. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  39. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  40. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  41. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  42. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  43. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  44. * SUCH DAMAGE.
  45. */
  46. #include <linux/bitops.h>
  47. #include <linux/if_arp.h>
  48. #include <linux/netdevice.h>
  49. #include <net/6lowpan.h>
  50. #include <net/ipv6.h>
  51. /* special link-layer handling */
  52. #include <net/mac802154.h>
  53. #include "nhc.h"
  54. /* Values of fields within the IPHC encoding first byte */
  55. #define LOWPAN_IPHC_TF_MASK 0x18
  56. #define LOWPAN_IPHC_TF_00 0x00
  57. #define LOWPAN_IPHC_TF_01 0x08
  58. #define LOWPAN_IPHC_TF_10 0x10
  59. #define LOWPAN_IPHC_TF_11 0x18
  60. #define LOWPAN_IPHC_NH 0x04
  61. #define LOWPAN_IPHC_HLIM_MASK 0x03
  62. #define LOWPAN_IPHC_HLIM_00 0x00
  63. #define LOWPAN_IPHC_HLIM_01 0x01
  64. #define LOWPAN_IPHC_HLIM_10 0x02
  65. #define LOWPAN_IPHC_HLIM_11 0x03
  66. /* Values of fields within the IPHC encoding second byte */
  67. #define LOWPAN_IPHC_CID 0x80
  68. #define LOWPAN_IPHC_SAC 0x40
  69. #define LOWPAN_IPHC_SAM_MASK 0x30
  70. #define LOWPAN_IPHC_SAM_00 0x00
  71. #define LOWPAN_IPHC_SAM_01 0x10
  72. #define LOWPAN_IPHC_SAM_10 0x20
  73. #define LOWPAN_IPHC_SAM_11 0x30
  74. #define LOWPAN_IPHC_M 0x08
  75. #define LOWPAN_IPHC_DAC 0x04
  76. #define LOWPAN_IPHC_DAM_MASK 0x03
  77. #define LOWPAN_IPHC_DAM_00 0x00
  78. #define LOWPAN_IPHC_DAM_01 0x01
  79. #define LOWPAN_IPHC_DAM_10 0x02
  80. #define LOWPAN_IPHC_DAM_11 0x03
  81. /* ipv6 address based on mac
  82. * second bit-flip (Universe/Local) is done according RFC2464
  83. */
  84. #define is_addr_mac_addr_based(a, m) \
  85. ((((a)->s6_addr[8]) == (((m)[0]) ^ 0x02)) && \
  86. (((a)->s6_addr[9]) == (m)[1]) && \
  87. (((a)->s6_addr[10]) == (m)[2]) && \
  88. (((a)->s6_addr[11]) == (m)[3]) && \
  89. (((a)->s6_addr[12]) == (m)[4]) && \
  90. (((a)->s6_addr[13]) == (m)[5]) && \
  91. (((a)->s6_addr[14]) == (m)[6]) && \
  92. (((a)->s6_addr[15]) == (m)[7]))
  93. /* check whether we can compress the IID to 16 bits,
  94. * it's possible for unicast addresses with first 49 bits are zero only.
  95. */
  96. #define lowpan_is_iid_16_bit_compressable(a) \
  97. ((((a)->s6_addr16[4]) == 0) && \
  98. (((a)->s6_addr[10]) == 0) && \
  99. (((a)->s6_addr[11]) == 0xff) && \
  100. (((a)->s6_addr[12]) == 0xfe) && \
  101. (((a)->s6_addr[13]) == 0))
  102. /* check whether the 112-bit gid of the multicast address is mappable to: */
  103. /* 48 bits, FFXX::00XX:XXXX:XXXX */
  104. #define lowpan_is_mcast_addr_compressable48(a) \
  105. ((((a)->s6_addr16[1]) == 0) && \
  106. (((a)->s6_addr16[2]) == 0) && \
  107. (((a)->s6_addr16[3]) == 0) && \
  108. (((a)->s6_addr16[4]) == 0) && \
  109. (((a)->s6_addr[10]) == 0))
  110. /* 32 bits, FFXX::00XX:XXXX */
  111. #define lowpan_is_mcast_addr_compressable32(a) \
  112. ((((a)->s6_addr16[1]) == 0) && \
  113. (((a)->s6_addr16[2]) == 0) && \
  114. (((a)->s6_addr16[3]) == 0) && \
  115. (((a)->s6_addr16[4]) == 0) && \
  116. (((a)->s6_addr16[5]) == 0) && \
  117. (((a)->s6_addr[12]) == 0))
  118. /* 8 bits, FF02::00XX */
  119. #define lowpan_is_mcast_addr_compressable8(a) \
  120. ((((a)->s6_addr[1]) == 2) && \
  121. (((a)->s6_addr16[1]) == 0) && \
  122. (((a)->s6_addr16[2]) == 0) && \
  123. (((a)->s6_addr16[3]) == 0) && \
  124. (((a)->s6_addr16[4]) == 0) && \
  125. (((a)->s6_addr16[5]) == 0) && \
  126. (((a)->s6_addr16[6]) == 0) && \
  127. (((a)->s6_addr[14]) == 0))
  128. static inline void iphc_uncompress_eui64_lladdr(struct in6_addr *ipaddr,
  129. const void *lladdr)
  130. {
  131. /* fe:80::XXXX:XXXX:XXXX:XXXX
  132. * \_________________/
  133. * hwaddr
  134. */
  135. ipaddr->s6_addr[0] = 0xFE;
  136. ipaddr->s6_addr[1] = 0x80;
  137. memcpy(&ipaddr->s6_addr[8], lladdr, EUI64_ADDR_LEN);
  138. /* second bit-flip (Universe/Local)
  139. * is done according RFC2464
  140. */
  141. ipaddr->s6_addr[8] ^= 0x02;
  142. }
  143. static inline void iphc_uncompress_802154_lladdr(struct in6_addr *ipaddr,
  144. const void *lladdr)
  145. {
  146. const struct ieee802154_addr *addr = lladdr;
  147. u8 eui64[EUI64_ADDR_LEN] = { };
  148. switch (addr->mode) {
  149. case IEEE802154_ADDR_LONG:
  150. ieee802154_le64_to_be64(eui64, &addr->extended_addr);
  151. iphc_uncompress_eui64_lladdr(ipaddr, eui64);
  152. break;
  153. case IEEE802154_ADDR_SHORT:
  154. /* fe:80::ff:fe00:XXXX
  155. * \__/
  156. * short_addr
  157. *
  158. * Universe/Local bit is zero.
  159. */
  160. ipaddr->s6_addr[0] = 0xFE;
  161. ipaddr->s6_addr[1] = 0x80;
  162. ipaddr->s6_addr[11] = 0xFF;
  163. ipaddr->s6_addr[12] = 0xFE;
  164. ieee802154_le16_to_be16(&ipaddr->s6_addr16[7],
  165. &addr->short_addr);
  166. break;
  167. default:
  168. /* should never handled and filtered by 802154 6lowpan */
  169. WARN_ON_ONCE(1);
  170. break;
  171. }
  172. }
  173. /* Uncompress address function for source and
  174. * destination address(non-multicast).
  175. *
  176. * address_mode is the masked value for sam or dam value
  177. */
  178. static int uncompress_addr(struct sk_buff *skb, const struct net_device *dev,
  179. struct in6_addr *ipaddr, u8 address_mode,
  180. const void *lladdr)
  181. {
  182. bool fail;
  183. switch (address_mode) {
  184. /* SAM and DAM are the same here */
  185. case LOWPAN_IPHC_DAM_00:
  186. /* for global link addresses */
  187. fail = lowpan_fetch_skb(skb, ipaddr->s6_addr, 16);
  188. break;
  189. case LOWPAN_IPHC_SAM_01:
  190. case LOWPAN_IPHC_DAM_01:
  191. /* fe:80::XXXX:XXXX:XXXX:XXXX */
  192. ipaddr->s6_addr[0] = 0xFE;
  193. ipaddr->s6_addr[1] = 0x80;
  194. fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[8], 8);
  195. break;
  196. case LOWPAN_IPHC_SAM_10:
  197. case LOWPAN_IPHC_DAM_10:
  198. /* fe:80::ff:fe00:XXXX */
  199. ipaddr->s6_addr[0] = 0xFE;
  200. ipaddr->s6_addr[1] = 0x80;
  201. ipaddr->s6_addr[11] = 0xFF;
  202. ipaddr->s6_addr[12] = 0xFE;
  203. fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[14], 2);
  204. break;
  205. case LOWPAN_IPHC_SAM_11:
  206. case LOWPAN_IPHC_DAM_11:
  207. fail = false;
  208. switch (lowpan_priv(dev)->lltype) {
  209. case LOWPAN_LLTYPE_IEEE802154:
  210. iphc_uncompress_802154_lladdr(ipaddr, lladdr);
  211. break;
  212. default:
  213. iphc_uncompress_eui64_lladdr(ipaddr, lladdr);
  214. break;
  215. }
  216. break;
  217. default:
  218. pr_debug("Invalid address mode value: 0x%x\n", address_mode);
  219. return -EINVAL;
  220. }
  221. if (fail) {
  222. pr_debug("Failed to fetch skb data\n");
  223. return -EIO;
  224. }
  225. raw_dump_inline(NULL, "Reconstructed ipv6 addr is",
  226. ipaddr->s6_addr, 16);
  227. return 0;
  228. }
  229. /* Uncompress address function for source context
  230. * based address(non-multicast).
  231. */
  232. static int uncompress_context_based_src_addr(struct sk_buff *skb,
  233. struct in6_addr *ipaddr,
  234. u8 address_mode)
  235. {
  236. switch (address_mode) {
  237. case LOWPAN_IPHC_SAM_00:
  238. /* unspec address ::
  239. * Do nothing, address is already ::
  240. */
  241. break;
  242. case LOWPAN_IPHC_SAM_01:
  243. /* TODO */
  244. case LOWPAN_IPHC_SAM_10:
  245. /* TODO */
  246. case LOWPAN_IPHC_SAM_11:
  247. /* TODO */
  248. netdev_warn(skb->dev, "SAM value 0x%x not supported\n",
  249. address_mode);
  250. return -EINVAL;
  251. default:
  252. pr_debug("Invalid sam value: 0x%x\n", address_mode);
  253. return -EINVAL;
  254. }
  255. raw_dump_inline(NULL,
  256. "Reconstructed context based ipv6 src addr is",
  257. ipaddr->s6_addr, 16);
  258. return 0;
  259. }
  260. /* Uncompress function for multicast destination address,
  261. * when M bit is set.
  262. */
  263. static int lowpan_uncompress_multicast_daddr(struct sk_buff *skb,
  264. struct in6_addr *ipaddr,
  265. u8 address_mode)
  266. {
  267. bool fail;
  268. switch (address_mode) {
  269. case LOWPAN_IPHC_DAM_00:
  270. /* 00: 128 bits. The full address
  271. * is carried in-line.
  272. */
  273. fail = lowpan_fetch_skb(skb, ipaddr->s6_addr, 16);
  274. break;
  275. case LOWPAN_IPHC_DAM_01:
  276. /* 01: 48 bits. The address takes
  277. * the form ffXX::00XX:XXXX:XXXX.
  278. */
  279. ipaddr->s6_addr[0] = 0xFF;
  280. fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[1], 1);
  281. fail |= lowpan_fetch_skb(skb, &ipaddr->s6_addr[11], 5);
  282. break;
  283. case LOWPAN_IPHC_DAM_10:
  284. /* 10: 32 bits. The address takes
  285. * the form ffXX::00XX:XXXX.
  286. */
  287. ipaddr->s6_addr[0] = 0xFF;
  288. fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[1], 1);
  289. fail |= lowpan_fetch_skb(skb, &ipaddr->s6_addr[13], 3);
  290. break;
  291. case LOWPAN_IPHC_DAM_11:
  292. /* 11: 8 bits. The address takes
  293. * the form ff02::00XX.
  294. */
  295. ipaddr->s6_addr[0] = 0xFF;
  296. ipaddr->s6_addr[1] = 0x02;
  297. fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[15], 1);
  298. break;
  299. default:
  300. pr_debug("DAM value has a wrong value: 0x%x\n", address_mode);
  301. return -EINVAL;
  302. }
  303. if (fail) {
  304. pr_debug("Failed to fetch skb data\n");
  305. return -EIO;
  306. }
  307. raw_dump_inline(NULL, "Reconstructed ipv6 multicast addr is",
  308. ipaddr->s6_addr, 16);
  309. return 0;
  310. }
  311. /* get the ecn values from iphc tf format and set it to ipv6hdr */
  312. static inline void lowpan_iphc_tf_set_ecn(struct ipv6hdr *hdr, const u8 *tf)
  313. {
  314. /* get the two higher bits which is ecn */
  315. u8 ecn = tf[0] & 0xc0;
  316. /* ECN takes 0x30 in hdr->flow_lbl[0] */
  317. hdr->flow_lbl[0] |= (ecn >> 2);
  318. }
  319. /* get the dscp values from iphc tf format and set it to ipv6hdr */
  320. static inline void lowpan_iphc_tf_set_dscp(struct ipv6hdr *hdr, const u8 *tf)
  321. {
  322. /* DSCP is at place after ECN */
  323. u8 dscp = tf[0] & 0x3f;
  324. /* The four highest bits need to be set at hdr->priority */
  325. hdr->priority |= ((dscp & 0x3c) >> 2);
  326. /* The two lower bits is part of hdr->flow_lbl[0] */
  327. hdr->flow_lbl[0] |= ((dscp & 0x03) << 6);
  328. }
  329. /* get the flow label values from iphc tf format and set it to ipv6hdr */
  330. static inline void lowpan_iphc_tf_set_lbl(struct ipv6hdr *hdr, const u8 *lbl)
  331. {
  332. /* flow label is always some array started with lower nibble of
  333. * flow_lbl[0] and followed with two bytes afterwards. Inside inline
  334. * data the flow_lbl position can be different, which will be handled
  335. * by lbl pointer. E.g. case "01" vs "00" the traffic class is 8 bit
  336. * shifted, the different lbl pointer will handle that.
  337. *
  338. * The flow label will started at lower nibble of flow_lbl[0], the
  339. * higher nibbles are part of DSCP + ECN.
  340. */
  341. hdr->flow_lbl[0] |= lbl[0] & 0x0f;
  342. memcpy(&hdr->flow_lbl[1], &lbl[1], 2);
  343. }
  344. /* lowpan_iphc_tf_decompress - decompress the traffic class.
  345. * This function will return zero on success, a value lower than zero if
  346. * failed.
  347. */
  348. static int lowpan_iphc_tf_decompress(struct sk_buff *skb, struct ipv6hdr *hdr,
  349. u8 val)
  350. {
  351. u8 tf[4];
  352. /* Traffic Class and Flow Label */
  353. switch (val) {
  354. case LOWPAN_IPHC_TF_00:
  355. /* ECN + DSCP + 4-bit Pad + Flow Label (4 bytes) */
  356. if (lowpan_fetch_skb(skb, tf, 4))
  357. return -EINVAL;
  358. /* 1 2 3
  359. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  360. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  361. * |ECN| DSCP | rsv | Flow Label |
  362. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  363. */
  364. lowpan_iphc_tf_set_ecn(hdr, tf);
  365. lowpan_iphc_tf_set_dscp(hdr, tf);
  366. lowpan_iphc_tf_set_lbl(hdr, &tf[1]);
  367. break;
  368. case LOWPAN_IPHC_TF_01:
  369. /* ECN + 2-bit Pad + Flow Label (3 bytes), DSCP is elided. */
  370. if (lowpan_fetch_skb(skb, tf, 3))
  371. return -EINVAL;
  372. /* 1 2
  373. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
  374. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  375. * |ECN|rsv| Flow Label |
  376. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  377. */
  378. lowpan_iphc_tf_set_ecn(hdr, tf);
  379. lowpan_iphc_tf_set_lbl(hdr, &tf[0]);
  380. break;
  381. case LOWPAN_IPHC_TF_10:
  382. /* ECN + DSCP (1 byte), Flow Label is elided. */
  383. if (lowpan_fetch_skb(skb, tf, 1))
  384. return -EINVAL;
  385. /* 0 1 2 3 4 5 6 7
  386. * +-+-+-+-+-+-+-+-+
  387. * |ECN| DSCP |
  388. * +-+-+-+-+-+-+-+-+
  389. */
  390. lowpan_iphc_tf_set_ecn(hdr, tf);
  391. lowpan_iphc_tf_set_dscp(hdr, tf);
  392. break;
  393. case LOWPAN_IPHC_TF_11:
  394. /* Traffic Class and Flow Label are elided */
  395. break;
  396. default:
  397. WARN_ON_ONCE(1);
  398. return -EINVAL;
  399. }
  400. return 0;
  401. }
  402. /* TTL uncompression values */
  403. static const u8 lowpan_ttl_values[] = {
  404. [LOWPAN_IPHC_HLIM_01] = 1,
  405. [LOWPAN_IPHC_HLIM_10] = 64,
  406. [LOWPAN_IPHC_HLIM_11] = 255,
  407. };
  408. int lowpan_header_decompress(struct sk_buff *skb, const struct net_device *dev,
  409. const void *daddr, const void *saddr)
  410. {
  411. struct ipv6hdr hdr = {};
  412. u8 iphc0, iphc1;
  413. int err;
  414. raw_dump_table(__func__, "raw skb data dump uncompressed",
  415. skb->data, skb->len);
  416. if (lowpan_fetch_skb(skb, &iphc0, sizeof(iphc0)) ||
  417. lowpan_fetch_skb(skb, &iphc1, sizeof(iphc1)))
  418. return -EINVAL;
  419. /* another if the CID flag is set */
  420. if (iphc1 & LOWPAN_IPHC_CID)
  421. return -ENOTSUPP;
  422. hdr.version = 6;
  423. err = lowpan_iphc_tf_decompress(skb, &hdr,
  424. iphc0 & LOWPAN_IPHC_TF_MASK);
  425. if (err < 0)
  426. return err;
  427. /* Next Header */
  428. if (!(iphc0 & LOWPAN_IPHC_NH)) {
  429. /* Next header is carried inline */
  430. if (lowpan_fetch_skb(skb, &hdr.nexthdr, sizeof(hdr.nexthdr)))
  431. return -EINVAL;
  432. pr_debug("NH flag is set, next header carried inline: %02x\n",
  433. hdr.nexthdr);
  434. }
  435. /* Hop Limit */
  436. if ((iphc0 & LOWPAN_IPHC_HLIM_MASK) != LOWPAN_IPHC_HLIM_00) {
  437. hdr.hop_limit = lowpan_ttl_values[iphc0 & LOWPAN_IPHC_HLIM_MASK];
  438. } else {
  439. if (lowpan_fetch_skb(skb, &hdr.hop_limit,
  440. sizeof(hdr.hop_limit)))
  441. return -EINVAL;
  442. }
  443. if (iphc1 & LOWPAN_IPHC_SAC) {
  444. /* Source address context based uncompression */
  445. pr_debug("SAC bit is set. Handle context based source address.\n");
  446. err = uncompress_context_based_src_addr(skb, &hdr.saddr,
  447. iphc1 & LOWPAN_IPHC_SAM_MASK);
  448. } else {
  449. /* Source address uncompression */
  450. pr_debug("source address stateless compression\n");
  451. err = uncompress_addr(skb, dev, &hdr.saddr,
  452. iphc1 & LOWPAN_IPHC_SAM_MASK, saddr);
  453. }
  454. /* Check on error of previous branch */
  455. if (err)
  456. return -EINVAL;
  457. /* check for Multicast Compression */
  458. if (iphc1 & LOWPAN_IPHC_M) {
  459. if (iphc1 & LOWPAN_IPHC_DAC) {
  460. pr_debug("dest: context-based mcast compression\n");
  461. /* TODO: implement this */
  462. } else {
  463. err = lowpan_uncompress_multicast_daddr(skb, &hdr.daddr,
  464. iphc1 & LOWPAN_IPHC_DAM_MASK);
  465. if (err)
  466. return -EINVAL;
  467. }
  468. } else {
  469. err = uncompress_addr(skb, dev, &hdr.daddr,
  470. iphc1 & LOWPAN_IPHC_DAM_MASK, daddr);
  471. pr_debug("dest: stateless compression mode %d dest %pI6c\n",
  472. iphc1 & LOWPAN_IPHC_DAM_MASK, &hdr.daddr);
  473. if (err)
  474. return -EINVAL;
  475. }
  476. /* Next header data uncompression */
  477. if (iphc0 & LOWPAN_IPHC_NH) {
  478. err = lowpan_nhc_do_uncompression(skb, dev, &hdr);
  479. if (err < 0)
  480. return err;
  481. } else {
  482. err = skb_cow(skb, sizeof(hdr));
  483. if (unlikely(err))
  484. return err;
  485. }
  486. switch (lowpan_priv(dev)->lltype) {
  487. case LOWPAN_LLTYPE_IEEE802154:
  488. if (lowpan_802154_cb(skb)->d_size)
  489. hdr.payload_len = htons(lowpan_802154_cb(skb)->d_size -
  490. sizeof(struct ipv6hdr));
  491. else
  492. hdr.payload_len = htons(skb->len);
  493. break;
  494. default:
  495. hdr.payload_len = htons(skb->len);
  496. break;
  497. }
  498. pr_debug("skb headroom size = %d, data length = %d\n",
  499. skb_headroom(skb), skb->len);
  500. pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength = %d\n\t"
  501. "nexthdr = 0x%02x\n\thop_lim = %d\n\tdest = %pI6c\n",
  502. hdr.version, ntohs(hdr.payload_len), hdr.nexthdr,
  503. hdr.hop_limit, &hdr.daddr);
  504. skb_push(skb, sizeof(hdr));
  505. skb_reset_mac_header(skb);
  506. skb_reset_network_header(skb);
  507. skb_copy_to_linear_data(skb, &hdr, sizeof(hdr));
  508. raw_dump_table(__func__, "raw header dump", (u8 *)&hdr, sizeof(hdr));
  509. return 0;
  510. }
  511. EXPORT_SYMBOL_GPL(lowpan_header_decompress);
  512. static const u8 lowpan_iphc_dam_to_sam_value[] = {
  513. [LOWPAN_IPHC_DAM_00] = LOWPAN_IPHC_SAM_00,
  514. [LOWPAN_IPHC_DAM_01] = LOWPAN_IPHC_SAM_01,
  515. [LOWPAN_IPHC_DAM_10] = LOWPAN_IPHC_SAM_10,
  516. [LOWPAN_IPHC_DAM_11] = LOWPAN_IPHC_SAM_11,
  517. };
  518. static u8 lowpan_compress_addr_64(u8 **hc_ptr, const struct in6_addr *ipaddr,
  519. const unsigned char *lladdr, bool sam)
  520. {
  521. u8 dam = LOWPAN_IPHC_DAM_00;
  522. if (is_addr_mac_addr_based(ipaddr, lladdr)) {
  523. dam = LOWPAN_IPHC_DAM_11; /* 0-bits */
  524. pr_debug("address compression 0 bits\n");
  525. } else if (lowpan_is_iid_16_bit_compressable(ipaddr)) {
  526. /* compress IID to 16 bits xxxx::XXXX */
  527. lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr16[7], 2);
  528. dam = LOWPAN_IPHC_DAM_10; /* 16-bits */
  529. raw_dump_inline(NULL, "Compressed ipv6 addr is (16 bits)",
  530. *hc_ptr - 2, 2);
  531. } else {
  532. /* do not compress IID => xxxx::IID */
  533. lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr16[4], 8);
  534. dam = LOWPAN_IPHC_DAM_01; /* 64-bits */
  535. raw_dump_inline(NULL, "Compressed ipv6 addr is (64 bits)",
  536. *hc_ptr - 8, 8);
  537. }
  538. if (sam)
  539. return lowpan_iphc_dam_to_sam_value[dam];
  540. else
  541. return dam;
  542. }
  543. /* lowpan_iphc_get_tc - get the ECN + DCSP fields in hc format */
  544. static inline u8 lowpan_iphc_get_tc(const struct ipv6hdr *hdr)
  545. {
  546. u8 dscp, ecn;
  547. /* hdr->priority contains the higher bits of dscp, lower are part of
  548. * flow_lbl[0]. Note ECN, DCSP is swapped in ipv6 hdr.
  549. */
  550. dscp = (hdr->priority << 2) | ((hdr->flow_lbl[0] & 0xc0) >> 6);
  551. /* ECN is at the two lower bits from first nibble of flow_lbl[0] */
  552. ecn = (hdr->flow_lbl[0] & 0x30);
  553. /* for pretty debug output, also shift ecn to get the ecn value */
  554. pr_debug("ecn 0x%02x dscp 0x%02x\n", ecn >> 4, dscp);
  555. /* ECN is at 0x30 now, shift it to have ECN + DCSP */
  556. return (ecn << 2) | dscp;
  557. }
  558. /* lowpan_iphc_is_flow_lbl_zero - check if flow label is zero */
  559. static inline bool lowpan_iphc_is_flow_lbl_zero(const struct ipv6hdr *hdr)
  560. {
  561. return ((!(hdr->flow_lbl[0] & 0x0f)) &&
  562. !hdr->flow_lbl[1] && !hdr->flow_lbl[2]);
  563. }
  564. /* lowpan_iphc_tf_compress - compress the traffic class which is set by
  565. * ipv6hdr. Return the corresponding format identifier which is used.
  566. */
  567. static u8 lowpan_iphc_tf_compress(u8 **hc_ptr, const struct ipv6hdr *hdr)
  568. {
  569. /* get ecn dscp data in a byteformat as: ECN(hi) + DSCP(lo) */
  570. u8 tc = lowpan_iphc_get_tc(hdr), tf[4], val;
  571. /* printout the traffic class in hc format */
  572. pr_debug("tc 0x%02x\n", tc);
  573. if (lowpan_iphc_is_flow_lbl_zero(hdr)) {
  574. if (!tc) {
  575. /* 11: Traffic Class and Flow Label are elided. */
  576. val = LOWPAN_IPHC_TF_11;
  577. } else {
  578. /* 10: ECN + DSCP (1 byte), Flow Label is elided.
  579. *
  580. * 0 1 2 3 4 5 6 7
  581. * +-+-+-+-+-+-+-+-+
  582. * |ECN| DSCP |
  583. * +-+-+-+-+-+-+-+-+
  584. */
  585. lowpan_push_hc_data(hc_ptr, &tc, sizeof(tc));
  586. val = LOWPAN_IPHC_TF_10;
  587. }
  588. } else {
  589. /* check if dscp is zero, it's after the first two bit */
  590. if (!(tc & 0x3f)) {
  591. /* 01: ECN + 2-bit Pad + Flow Label (3 bytes), DSCP is elided
  592. *
  593. * 1 2
  594. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
  595. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  596. * |ECN|rsv| Flow Label |
  597. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  598. */
  599. memcpy(&tf[0], &hdr->flow_lbl[0], 3);
  600. /* zero the highest 4-bits, contains DCSP + ECN */
  601. tf[0] &= ~0xf0;
  602. /* set ECN */
  603. tf[0] |= (tc & 0xc0);
  604. lowpan_push_hc_data(hc_ptr, tf, 3);
  605. val = LOWPAN_IPHC_TF_01;
  606. } else {
  607. /* 00: ECN + DSCP + 4-bit Pad + Flow Label (4 bytes)
  608. *
  609. * 1 2 3
  610. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  611. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  612. * |ECN| DSCP | rsv | Flow Label |
  613. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  614. */
  615. memcpy(&tf[0], &tc, sizeof(tc));
  616. /* highest nibble of flow_lbl[0] is part of DSCP + ECN
  617. * which will be the 4-bit pad and will be filled with
  618. * zeros afterwards.
  619. */
  620. memcpy(&tf[1], &hdr->flow_lbl[0], 3);
  621. /* zero the 4-bit pad, which is reserved */
  622. tf[1] &= ~0xf0;
  623. lowpan_push_hc_data(hc_ptr, tf, 4);
  624. val = LOWPAN_IPHC_TF_00;
  625. }
  626. }
  627. return val;
  628. }
  629. static u8 lowpan_iphc_mcast_addr_compress(u8 **hc_ptr,
  630. const struct in6_addr *ipaddr)
  631. {
  632. u8 val;
  633. if (lowpan_is_mcast_addr_compressable8(ipaddr)) {
  634. pr_debug("compressed to 1 octet\n");
  635. /* use last byte */
  636. lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr[15], 1);
  637. val = LOWPAN_IPHC_DAM_11;
  638. } else if (lowpan_is_mcast_addr_compressable32(ipaddr)) {
  639. pr_debug("compressed to 4 octets\n");
  640. /* second byte + the last three */
  641. lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr[1], 1);
  642. lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr[13], 3);
  643. val = LOWPAN_IPHC_DAM_10;
  644. } else if (lowpan_is_mcast_addr_compressable48(ipaddr)) {
  645. pr_debug("compressed to 6 octets\n");
  646. /* second byte + the last five */
  647. lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr[1], 1);
  648. lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr[11], 5);
  649. val = LOWPAN_IPHC_DAM_01;
  650. } else {
  651. pr_debug("using full address\n");
  652. lowpan_push_hc_data(hc_ptr, ipaddr->s6_addr, 16);
  653. val = LOWPAN_IPHC_DAM_00;
  654. }
  655. return val;
  656. }
  657. int lowpan_header_compress(struct sk_buff *skb, const struct net_device *dev,
  658. const void *daddr, const void *saddr)
  659. {
  660. u8 iphc0, iphc1, *hc_ptr;
  661. struct ipv6hdr *hdr;
  662. u8 head[LOWPAN_IPHC_MAX_HC_BUF_LEN] = {};
  663. int ret, addr_type;
  664. if (skb->protocol != htons(ETH_P_IPV6))
  665. return -EINVAL;
  666. hdr = ipv6_hdr(skb);
  667. hc_ptr = head + 2;
  668. pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength = %d\n"
  669. "\tnexthdr = 0x%02x\n\thop_lim = %d\n\tdest = %pI6c\n",
  670. hdr->version, ntohs(hdr->payload_len), hdr->nexthdr,
  671. hdr->hop_limit, &hdr->daddr);
  672. raw_dump_table(__func__, "raw skb network header dump",
  673. skb_network_header(skb), sizeof(struct ipv6hdr));
  674. /* As we copy some bit-length fields, in the IPHC encoding bytes,
  675. * we sometimes use |=
  676. * If the field is 0, and the current bit value in memory is 1,
  677. * this does not work. We therefore reset the IPHC encoding here
  678. */
  679. iphc0 = LOWPAN_DISPATCH_IPHC;
  680. iphc1 = 0;
  681. /* TODO: context lookup */
  682. raw_dump_inline(__func__, "saddr", saddr, EUI64_ADDR_LEN);
  683. raw_dump_inline(__func__, "daddr", daddr, EUI64_ADDR_LEN);
  684. raw_dump_table(__func__, "sending raw skb network uncompressed packet",
  685. skb->data, skb->len);
  686. /* Traffic Class, Flow Label compression */
  687. iphc0 |= lowpan_iphc_tf_compress(&hc_ptr, hdr);
  688. /* NOTE: payload length is always compressed */
  689. /* Check if we provide the nhc format for nexthdr and compression
  690. * functionality. If not nexthdr is handled inline and not compressed.
  691. */
  692. ret = lowpan_nhc_check_compression(skb, hdr, &hc_ptr);
  693. if (ret == -ENOENT)
  694. lowpan_push_hc_data(&hc_ptr, &hdr->nexthdr,
  695. sizeof(hdr->nexthdr));
  696. else
  697. iphc0 |= LOWPAN_IPHC_NH;
  698. /* Hop limit
  699. * if 1: compress, encoding is 01
  700. * if 64: compress, encoding is 10
  701. * if 255: compress, encoding is 11
  702. * else do not compress
  703. */
  704. switch (hdr->hop_limit) {
  705. case 1:
  706. iphc0 |= LOWPAN_IPHC_HLIM_01;
  707. break;
  708. case 64:
  709. iphc0 |= LOWPAN_IPHC_HLIM_10;
  710. break;
  711. case 255:
  712. iphc0 |= LOWPAN_IPHC_HLIM_11;
  713. break;
  714. default:
  715. lowpan_push_hc_data(&hc_ptr, &hdr->hop_limit,
  716. sizeof(hdr->hop_limit));
  717. }
  718. addr_type = ipv6_addr_type(&hdr->saddr);
  719. /* source address compression */
  720. if (addr_type == IPV6_ADDR_ANY) {
  721. pr_debug("source address is unspecified, setting SAC\n");
  722. iphc1 |= LOWPAN_IPHC_SAC;
  723. } else {
  724. if (addr_type & IPV6_ADDR_LINKLOCAL) {
  725. iphc1 |= lowpan_compress_addr_64(&hc_ptr, &hdr->saddr,
  726. saddr, true);
  727. pr_debug("source address unicast link-local %pI6c iphc1 0x%02x\n",
  728. &hdr->saddr, iphc1);
  729. } else {
  730. pr_debug("send the full source address\n");
  731. lowpan_push_hc_data(&hc_ptr, hdr->saddr.s6_addr, 16);
  732. }
  733. }
  734. addr_type = ipv6_addr_type(&hdr->daddr);
  735. /* destination address compression */
  736. if (addr_type & IPV6_ADDR_MULTICAST) {
  737. pr_debug("destination address is multicast: ");
  738. iphc1 |= LOWPAN_IPHC_M;
  739. iphc1 |= lowpan_iphc_mcast_addr_compress(&hc_ptr, &hdr->daddr);
  740. } else {
  741. if (addr_type & IPV6_ADDR_LINKLOCAL) {
  742. /* TODO: context lookup */
  743. iphc1 |= lowpan_compress_addr_64(&hc_ptr, &hdr->daddr,
  744. daddr, false);
  745. pr_debug("dest address unicast link-local %pI6c "
  746. "iphc1 0x%02x\n", &hdr->daddr, iphc1);
  747. } else {
  748. pr_debug("dest address unicast %pI6c\n", &hdr->daddr);
  749. lowpan_push_hc_data(&hc_ptr, hdr->daddr.s6_addr, 16);
  750. }
  751. }
  752. /* next header compression */
  753. if (iphc0 & LOWPAN_IPHC_NH) {
  754. ret = lowpan_nhc_do_compression(skb, hdr, &hc_ptr);
  755. if (ret < 0)
  756. return ret;
  757. }
  758. head[0] = iphc0;
  759. head[1] = iphc1;
  760. skb_pull(skb, sizeof(struct ipv6hdr));
  761. skb_reset_transport_header(skb);
  762. memcpy(skb_push(skb, hc_ptr - head), head, hc_ptr - head);
  763. skb_reset_network_header(skb);
  764. pr_debug("header len %d skb %u\n", (int)(hc_ptr - head), skb->len);
  765. raw_dump_table(__func__, "raw skb data dump compressed",
  766. skb->data, skb->len);
  767. return 0;
  768. }
  769. EXPORT_SYMBOL_GPL(lowpan_header_compress);