iphc.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  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. #include "6lowpan_i.h"
  52. #include "nhc.h"
  53. /* Values of fields within the IPHC encoding first byte */
  54. #define LOWPAN_IPHC_TF_MASK 0x18
  55. #define LOWPAN_IPHC_TF_00 0x00
  56. #define LOWPAN_IPHC_TF_01 0x08
  57. #define LOWPAN_IPHC_TF_10 0x10
  58. #define LOWPAN_IPHC_TF_11 0x18
  59. #define LOWPAN_IPHC_NH 0x04
  60. #define LOWPAN_IPHC_HLIM_MASK 0x03
  61. #define LOWPAN_IPHC_HLIM_00 0x00
  62. #define LOWPAN_IPHC_HLIM_01 0x01
  63. #define LOWPAN_IPHC_HLIM_10 0x02
  64. #define LOWPAN_IPHC_HLIM_11 0x03
  65. /* Values of fields within the IPHC encoding second byte */
  66. #define LOWPAN_IPHC_CID 0x80
  67. #define LOWPAN_IPHC_SAC 0x40
  68. #define LOWPAN_IPHC_SAM_MASK 0x30
  69. #define LOWPAN_IPHC_SAM_00 0x00
  70. #define LOWPAN_IPHC_SAM_01 0x10
  71. #define LOWPAN_IPHC_SAM_10 0x20
  72. #define LOWPAN_IPHC_SAM_11 0x30
  73. #define LOWPAN_IPHC_M 0x08
  74. #define LOWPAN_IPHC_DAC 0x04
  75. #define LOWPAN_IPHC_DAM_MASK 0x03
  76. #define LOWPAN_IPHC_DAM_00 0x00
  77. #define LOWPAN_IPHC_DAM_01 0x01
  78. #define LOWPAN_IPHC_DAM_10 0x02
  79. #define LOWPAN_IPHC_DAM_11 0x03
  80. /* ipv6 address based on mac
  81. * second bit-flip (Universe/Local) is done according RFC2464
  82. */
  83. #define is_addr_mac_addr_based(a, m) \
  84. ((((a)->s6_addr[8]) == (((m)[0]) ^ 0x02)) && \
  85. (((a)->s6_addr[9]) == (m)[1]) && \
  86. (((a)->s6_addr[10]) == (m)[2]) && \
  87. (((a)->s6_addr[11]) == (m)[3]) && \
  88. (((a)->s6_addr[12]) == (m)[4]) && \
  89. (((a)->s6_addr[13]) == (m)[5]) && \
  90. (((a)->s6_addr[14]) == (m)[6]) && \
  91. (((a)->s6_addr[15]) == (m)[7]))
  92. /* check whether we can compress the IID to 16 bits,
  93. * it's possible for unicast addresses with first 49 bits are zero only.
  94. */
  95. #define lowpan_is_iid_16_bit_compressable(a) \
  96. ((((a)->s6_addr16[4]) == 0) && \
  97. (((a)->s6_addr[10]) == 0) && \
  98. (((a)->s6_addr[11]) == 0xff) && \
  99. (((a)->s6_addr[12]) == 0xfe) && \
  100. (((a)->s6_addr[13]) == 0))
  101. /* check whether the 112-bit gid of the multicast address is mappable to: */
  102. /* 48 bits, FFXX::00XX:XXXX:XXXX */
  103. #define lowpan_is_mcast_addr_compressable48(a) \
  104. ((((a)->s6_addr16[1]) == 0) && \
  105. (((a)->s6_addr16[2]) == 0) && \
  106. (((a)->s6_addr16[3]) == 0) && \
  107. (((a)->s6_addr16[4]) == 0) && \
  108. (((a)->s6_addr[10]) == 0))
  109. /* 32 bits, FFXX::00XX:XXXX */
  110. #define lowpan_is_mcast_addr_compressable32(a) \
  111. ((((a)->s6_addr16[1]) == 0) && \
  112. (((a)->s6_addr16[2]) == 0) && \
  113. (((a)->s6_addr16[3]) == 0) && \
  114. (((a)->s6_addr16[4]) == 0) && \
  115. (((a)->s6_addr16[5]) == 0) && \
  116. (((a)->s6_addr[12]) == 0))
  117. /* 8 bits, FF02::00XX */
  118. #define lowpan_is_mcast_addr_compressable8(a) \
  119. ((((a)->s6_addr[1]) == 2) && \
  120. (((a)->s6_addr16[1]) == 0) && \
  121. (((a)->s6_addr16[2]) == 0) && \
  122. (((a)->s6_addr16[3]) == 0) && \
  123. (((a)->s6_addr16[4]) == 0) && \
  124. (((a)->s6_addr16[5]) == 0) && \
  125. (((a)->s6_addr16[6]) == 0) && \
  126. (((a)->s6_addr[14]) == 0))
  127. #define lowpan_is_linklocal_zero_padded(a) \
  128. (!(hdr->saddr.s6_addr[1] & 0x3f) && \
  129. !hdr->saddr.s6_addr16[1] && \
  130. !hdr->saddr.s6_addr32[1])
  131. #define LOWPAN_IPHC_CID_DCI(cid) (cid & 0x0f)
  132. #define LOWPAN_IPHC_CID_SCI(cid) ((cid & 0xf0) >> 4)
  133. static inline void
  134. lowpan_iphc_uncompress_802154_lladdr(struct in6_addr *ipaddr,
  135. const void *lladdr)
  136. {
  137. const struct ieee802154_addr *addr = lladdr;
  138. u8 eui64[EUI64_ADDR_LEN];
  139. switch (addr->mode) {
  140. case IEEE802154_ADDR_LONG:
  141. ieee802154_le64_to_be64(eui64, &addr->extended_addr);
  142. lowpan_iphc_uncompress_eui64_lladdr(ipaddr, eui64);
  143. break;
  144. case IEEE802154_ADDR_SHORT:
  145. /* fe:80::ff:fe00:XXXX
  146. * \__/
  147. * short_addr
  148. *
  149. * Universe/Local bit is zero.
  150. */
  151. ipaddr->s6_addr[0] = 0xFE;
  152. ipaddr->s6_addr[1] = 0x80;
  153. ipaddr->s6_addr[11] = 0xFF;
  154. ipaddr->s6_addr[12] = 0xFE;
  155. ieee802154_le16_to_be16(&ipaddr->s6_addr16[7],
  156. &addr->short_addr);
  157. break;
  158. default:
  159. /* should never handled and filtered by 802154 6lowpan */
  160. WARN_ON_ONCE(1);
  161. break;
  162. }
  163. }
  164. static struct lowpan_iphc_ctx *
  165. lowpan_iphc_ctx_get_by_id(const struct net_device *dev, u8 id)
  166. {
  167. struct lowpan_iphc_ctx *ret = &lowpan_dev(dev)->ctx.table[id];
  168. if (!lowpan_iphc_ctx_is_active(ret))
  169. return NULL;
  170. return ret;
  171. }
  172. static struct lowpan_iphc_ctx *
  173. lowpan_iphc_ctx_get_by_addr(const struct net_device *dev,
  174. const struct in6_addr *addr)
  175. {
  176. struct lowpan_iphc_ctx *table = lowpan_dev(dev)->ctx.table;
  177. struct lowpan_iphc_ctx *ret = NULL;
  178. struct in6_addr addr_pfx;
  179. u8 addr_plen;
  180. int i;
  181. for (i = 0; i < LOWPAN_IPHC_CTX_TABLE_SIZE; i++) {
  182. /* Check if context is valid. A context that is not valid
  183. * MUST NOT be used for compression.
  184. */
  185. if (!lowpan_iphc_ctx_is_active(&table[i]) ||
  186. !lowpan_iphc_ctx_is_compression(&table[i]))
  187. continue;
  188. ipv6_addr_prefix(&addr_pfx, addr, table[i].plen);
  189. /* if prefix len < 64, the remaining bits until 64th bit is
  190. * zero. Otherwise we use table[i]->plen.
  191. */
  192. if (table[i].plen < 64)
  193. addr_plen = 64;
  194. else
  195. addr_plen = table[i].plen;
  196. if (ipv6_prefix_equal(&addr_pfx, &table[i].pfx, addr_plen)) {
  197. /* remember first match */
  198. if (!ret) {
  199. ret = &table[i];
  200. continue;
  201. }
  202. /* get the context with longest prefix len */
  203. if (table[i].plen > ret->plen)
  204. ret = &table[i];
  205. }
  206. }
  207. return ret;
  208. }
  209. static struct lowpan_iphc_ctx *
  210. lowpan_iphc_ctx_get_by_mcast_addr(const struct net_device *dev,
  211. const struct in6_addr *addr)
  212. {
  213. struct lowpan_iphc_ctx *table = lowpan_dev(dev)->ctx.table;
  214. struct lowpan_iphc_ctx *ret = NULL;
  215. struct in6_addr addr_mcast, network_pfx = {};
  216. int i;
  217. /* init mcast address with */
  218. memcpy(&addr_mcast, addr, sizeof(*addr));
  219. for (i = 0; i < LOWPAN_IPHC_CTX_TABLE_SIZE; i++) {
  220. /* Check if context is valid. A context that is not valid
  221. * MUST NOT be used for compression.
  222. */
  223. if (!lowpan_iphc_ctx_is_active(&table[i]) ||
  224. !lowpan_iphc_ctx_is_compression(&table[i]))
  225. continue;
  226. /* setting plen */
  227. addr_mcast.s6_addr[3] = table[i].plen;
  228. /* get network prefix to copy into multicast address */
  229. ipv6_addr_prefix(&network_pfx, &table[i].pfx,
  230. table[i].plen);
  231. /* setting network prefix */
  232. memcpy(&addr_mcast.s6_addr[4], &network_pfx, 8);
  233. if (ipv6_addr_equal(addr, &addr_mcast)) {
  234. ret = &table[i];
  235. break;
  236. }
  237. }
  238. return ret;
  239. }
  240. static void lowpan_iphc_uncompress_lladdr(const struct net_device *dev,
  241. struct in6_addr *ipaddr,
  242. const void *lladdr)
  243. {
  244. switch (dev->addr_len) {
  245. case ETH_ALEN:
  246. lowpan_iphc_uncompress_eui48_lladdr(ipaddr, lladdr);
  247. break;
  248. case EUI64_ADDR_LEN:
  249. lowpan_iphc_uncompress_eui64_lladdr(ipaddr, lladdr);
  250. break;
  251. default:
  252. WARN_ON_ONCE(1);
  253. break;
  254. }
  255. }
  256. /* Uncompress address function for source and
  257. * destination address(non-multicast).
  258. *
  259. * address_mode is the masked value for sam or dam value
  260. */
  261. static int lowpan_iphc_uncompress_addr(struct sk_buff *skb,
  262. const struct net_device *dev,
  263. struct in6_addr *ipaddr,
  264. u8 address_mode, const void *lladdr)
  265. {
  266. bool fail;
  267. switch (address_mode) {
  268. /* SAM and DAM are the same here */
  269. case LOWPAN_IPHC_DAM_00:
  270. /* for global link addresses */
  271. fail = lowpan_fetch_skb(skb, ipaddr->s6_addr, 16);
  272. break;
  273. case LOWPAN_IPHC_SAM_01:
  274. case LOWPAN_IPHC_DAM_01:
  275. /* fe:80::XXXX:XXXX:XXXX:XXXX */
  276. ipaddr->s6_addr[0] = 0xFE;
  277. ipaddr->s6_addr[1] = 0x80;
  278. fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[8], 8);
  279. break;
  280. case LOWPAN_IPHC_SAM_10:
  281. case LOWPAN_IPHC_DAM_10:
  282. /* fe:80::ff:fe00:XXXX */
  283. ipaddr->s6_addr[0] = 0xFE;
  284. ipaddr->s6_addr[1] = 0x80;
  285. ipaddr->s6_addr[11] = 0xFF;
  286. ipaddr->s6_addr[12] = 0xFE;
  287. fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[14], 2);
  288. break;
  289. case LOWPAN_IPHC_SAM_11:
  290. case LOWPAN_IPHC_DAM_11:
  291. fail = false;
  292. switch (lowpan_dev(dev)->lltype) {
  293. case LOWPAN_LLTYPE_IEEE802154:
  294. lowpan_iphc_uncompress_802154_lladdr(ipaddr, lladdr);
  295. break;
  296. default:
  297. lowpan_iphc_uncompress_lladdr(dev, ipaddr, lladdr);
  298. break;
  299. }
  300. break;
  301. default:
  302. pr_debug("Invalid address mode value: 0x%x\n", address_mode);
  303. return -EINVAL;
  304. }
  305. if (fail) {
  306. pr_debug("Failed to fetch skb data\n");
  307. return -EIO;
  308. }
  309. raw_dump_inline(NULL, "Reconstructed ipv6 addr is",
  310. ipaddr->s6_addr, 16);
  311. return 0;
  312. }
  313. /* Uncompress address function for source context
  314. * based address(non-multicast).
  315. */
  316. static int lowpan_iphc_uncompress_ctx_addr(struct sk_buff *skb,
  317. const struct net_device *dev,
  318. const struct lowpan_iphc_ctx *ctx,
  319. struct in6_addr *ipaddr,
  320. u8 address_mode, const void *lladdr)
  321. {
  322. bool fail;
  323. switch (address_mode) {
  324. /* SAM and DAM are the same here */
  325. case LOWPAN_IPHC_DAM_00:
  326. fail = false;
  327. /* SAM_00 -> unspec address ::
  328. * Do nothing, address is already ::
  329. *
  330. * DAM 00 -> reserved should never occur.
  331. */
  332. break;
  333. case LOWPAN_IPHC_SAM_01:
  334. case LOWPAN_IPHC_DAM_01:
  335. fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[8], 8);
  336. ipv6_addr_prefix_copy(ipaddr, &ctx->pfx, ctx->plen);
  337. break;
  338. case LOWPAN_IPHC_SAM_10:
  339. case LOWPAN_IPHC_DAM_10:
  340. ipaddr->s6_addr[11] = 0xFF;
  341. ipaddr->s6_addr[12] = 0xFE;
  342. fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[14], 2);
  343. ipv6_addr_prefix_copy(ipaddr, &ctx->pfx, ctx->plen);
  344. break;
  345. case LOWPAN_IPHC_SAM_11:
  346. case LOWPAN_IPHC_DAM_11:
  347. fail = false;
  348. switch (lowpan_dev(dev)->lltype) {
  349. case LOWPAN_LLTYPE_IEEE802154:
  350. lowpan_iphc_uncompress_802154_lladdr(ipaddr, lladdr);
  351. break;
  352. default:
  353. lowpan_iphc_uncompress_lladdr(dev, ipaddr, lladdr);
  354. break;
  355. }
  356. ipv6_addr_prefix_copy(ipaddr, &ctx->pfx, ctx->plen);
  357. break;
  358. default:
  359. pr_debug("Invalid sam value: 0x%x\n", address_mode);
  360. return -EINVAL;
  361. }
  362. if (fail) {
  363. pr_debug("Failed to fetch skb data\n");
  364. return -EIO;
  365. }
  366. raw_dump_inline(NULL,
  367. "Reconstructed context based ipv6 src addr is",
  368. ipaddr->s6_addr, 16);
  369. return 0;
  370. }
  371. /* Uncompress function for multicast destination address,
  372. * when M bit is set.
  373. */
  374. static int lowpan_uncompress_multicast_daddr(struct sk_buff *skb,
  375. struct in6_addr *ipaddr,
  376. u8 address_mode)
  377. {
  378. bool fail;
  379. switch (address_mode) {
  380. case LOWPAN_IPHC_DAM_00:
  381. /* 00: 128 bits. The full address
  382. * is carried in-line.
  383. */
  384. fail = lowpan_fetch_skb(skb, ipaddr->s6_addr, 16);
  385. break;
  386. case LOWPAN_IPHC_DAM_01:
  387. /* 01: 48 bits. The address takes
  388. * the form ffXX::00XX:XXXX:XXXX.
  389. */
  390. ipaddr->s6_addr[0] = 0xFF;
  391. fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[1], 1);
  392. fail |= lowpan_fetch_skb(skb, &ipaddr->s6_addr[11], 5);
  393. break;
  394. case LOWPAN_IPHC_DAM_10:
  395. /* 10: 32 bits. The address takes
  396. * the form ffXX::00XX:XXXX.
  397. */
  398. ipaddr->s6_addr[0] = 0xFF;
  399. fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[1], 1);
  400. fail |= lowpan_fetch_skb(skb, &ipaddr->s6_addr[13], 3);
  401. break;
  402. case LOWPAN_IPHC_DAM_11:
  403. /* 11: 8 bits. The address takes
  404. * the form ff02::00XX.
  405. */
  406. ipaddr->s6_addr[0] = 0xFF;
  407. ipaddr->s6_addr[1] = 0x02;
  408. fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[15], 1);
  409. break;
  410. default:
  411. pr_debug("DAM value has a wrong value: 0x%x\n", address_mode);
  412. return -EINVAL;
  413. }
  414. if (fail) {
  415. pr_debug("Failed to fetch skb data\n");
  416. return -EIO;
  417. }
  418. raw_dump_inline(NULL, "Reconstructed ipv6 multicast addr is",
  419. ipaddr->s6_addr, 16);
  420. return 0;
  421. }
  422. static int lowpan_uncompress_multicast_ctx_daddr(struct sk_buff *skb,
  423. struct lowpan_iphc_ctx *ctx,
  424. struct in6_addr *ipaddr,
  425. u8 address_mode)
  426. {
  427. struct in6_addr network_pfx = {};
  428. bool fail;
  429. ipaddr->s6_addr[0] = 0xFF;
  430. fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[1], 2);
  431. fail |= lowpan_fetch_skb(skb, &ipaddr->s6_addr[12], 4);
  432. if (fail)
  433. return -EIO;
  434. /* take prefix_len and network prefix from the context */
  435. ipaddr->s6_addr[3] = ctx->plen;
  436. /* get network prefix to copy into multicast address */
  437. ipv6_addr_prefix(&network_pfx, &ctx->pfx, ctx->plen);
  438. /* setting network prefix */
  439. memcpy(&ipaddr->s6_addr[4], &network_pfx, 8);
  440. return 0;
  441. }
  442. /* get the ecn values from iphc tf format and set it to ipv6hdr */
  443. static inline void lowpan_iphc_tf_set_ecn(struct ipv6hdr *hdr, const u8 *tf)
  444. {
  445. /* get the two higher bits which is ecn */
  446. u8 ecn = tf[0] & 0xc0;
  447. /* ECN takes 0x30 in hdr->flow_lbl[0] */
  448. hdr->flow_lbl[0] |= (ecn >> 2);
  449. }
  450. /* get the dscp values from iphc tf format and set it to ipv6hdr */
  451. static inline void lowpan_iphc_tf_set_dscp(struct ipv6hdr *hdr, const u8 *tf)
  452. {
  453. /* DSCP is at place after ECN */
  454. u8 dscp = tf[0] & 0x3f;
  455. /* The four highest bits need to be set at hdr->priority */
  456. hdr->priority |= ((dscp & 0x3c) >> 2);
  457. /* The two lower bits is part of hdr->flow_lbl[0] */
  458. hdr->flow_lbl[0] |= ((dscp & 0x03) << 6);
  459. }
  460. /* get the flow label values from iphc tf format and set it to ipv6hdr */
  461. static inline void lowpan_iphc_tf_set_lbl(struct ipv6hdr *hdr, const u8 *lbl)
  462. {
  463. /* flow label is always some array started with lower nibble of
  464. * flow_lbl[0] and followed with two bytes afterwards. Inside inline
  465. * data the flow_lbl position can be different, which will be handled
  466. * by lbl pointer. E.g. case "01" vs "00" the traffic class is 8 bit
  467. * shifted, the different lbl pointer will handle that.
  468. *
  469. * The flow label will started at lower nibble of flow_lbl[0], the
  470. * higher nibbles are part of DSCP + ECN.
  471. */
  472. hdr->flow_lbl[0] |= lbl[0] & 0x0f;
  473. memcpy(&hdr->flow_lbl[1], &lbl[1], 2);
  474. }
  475. /* lowpan_iphc_tf_decompress - decompress the traffic class.
  476. * This function will return zero on success, a value lower than zero if
  477. * failed.
  478. */
  479. static int lowpan_iphc_tf_decompress(struct sk_buff *skb, struct ipv6hdr *hdr,
  480. u8 val)
  481. {
  482. u8 tf[4];
  483. /* Traffic Class and Flow Label */
  484. switch (val) {
  485. case LOWPAN_IPHC_TF_00:
  486. /* ECN + DSCP + 4-bit Pad + Flow Label (4 bytes) */
  487. if (lowpan_fetch_skb(skb, tf, 4))
  488. return -EINVAL;
  489. /* 1 2 3
  490. * 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
  491. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  492. * |ECN| DSCP | rsv | Flow Label |
  493. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  494. */
  495. lowpan_iphc_tf_set_ecn(hdr, tf);
  496. lowpan_iphc_tf_set_dscp(hdr, tf);
  497. lowpan_iphc_tf_set_lbl(hdr, &tf[1]);
  498. break;
  499. case LOWPAN_IPHC_TF_01:
  500. /* ECN + 2-bit Pad + Flow Label (3 bytes), DSCP is elided. */
  501. if (lowpan_fetch_skb(skb, tf, 3))
  502. return -EINVAL;
  503. /* 1 2
  504. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
  505. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  506. * |ECN|rsv| Flow Label |
  507. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  508. */
  509. lowpan_iphc_tf_set_ecn(hdr, tf);
  510. lowpan_iphc_tf_set_lbl(hdr, &tf[0]);
  511. break;
  512. case LOWPAN_IPHC_TF_10:
  513. /* ECN + DSCP (1 byte), Flow Label is elided. */
  514. if (lowpan_fetch_skb(skb, tf, 1))
  515. return -EINVAL;
  516. /* 0 1 2 3 4 5 6 7
  517. * +-+-+-+-+-+-+-+-+
  518. * |ECN| DSCP |
  519. * +-+-+-+-+-+-+-+-+
  520. */
  521. lowpan_iphc_tf_set_ecn(hdr, tf);
  522. lowpan_iphc_tf_set_dscp(hdr, tf);
  523. break;
  524. case LOWPAN_IPHC_TF_11:
  525. /* Traffic Class and Flow Label are elided */
  526. break;
  527. default:
  528. WARN_ON_ONCE(1);
  529. return -EINVAL;
  530. }
  531. return 0;
  532. }
  533. /* TTL uncompression values */
  534. static const u8 lowpan_ttl_values[] = {
  535. [LOWPAN_IPHC_HLIM_01] = 1,
  536. [LOWPAN_IPHC_HLIM_10] = 64,
  537. [LOWPAN_IPHC_HLIM_11] = 255,
  538. };
  539. int lowpan_header_decompress(struct sk_buff *skb, const struct net_device *dev,
  540. const void *daddr, const void *saddr)
  541. {
  542. struct ipv6hdr hdr = {};
  543. struct lowpan_iphc_ctx *ci;
  544. u8 iphc0, iphc1, cid = 0;
  545. int err;
  546. raw_dump_table(__func__, "raw skb data dump uncompressed",
  547. skb->data, skb->len);
  548. if (lowpan_fetch_skb(skb, &iphc0, sizeof(iphc0)) ||
  549. lowpan_fetch_skb(skb, &iphc1, sizeof(iphc1)))
  550. return -EINVAL;
  551. hdr.version = 6;
  552. /* default CID = 0, another if the CID flag is set */
  553. if (iphc1 & LOWPAN_IPHC_CID) {
  554. if (lowpan_fetch_skb(skb, &cid, sizeof(cid)))
  555. return -EINVAL;
  556. }
  557. err = lowpan_iphc_tf_decompress(skb, &hdr,
  558. iphc0 & LOWPAN_IPHC_TF_MASK);
  559. if (err < 0)
  560. return err;
  561. /* Next Header */
  562. if (!(iphc0 & LOWPAN_IPHC_NH)) {
  563. /* Next header is carried inline */
  564. if (lowpan_fetch_skb(skb, &hdr.nexthdr, sizeof(hdr.nexthdr)))
  565. return -EINVAL;
  566. pr_debug("NH flag is set, next header carried inline: %02x\n",
  567. hdr.nexthdr);
  568. }
  569. /* Hop Limit */
  570. if ((iphc0 & LOWPAN_IPHC_HLIM_MASK) != LOWPAN_IPHC_HLIM_00) {
  571. hdr.hop_limit = lowpan_ttl_values[iphc0 & LOWPAN_IPHC_HLIM_MASK];
  572. } else {
  573. if (lowpan_fetch_skb(skb, &hdr.hop_limit,
  574. sizeof(hdr.hop_limit)))
  575. return -EINVAL;
  576. }
  577. if (iphc1 & LOWPAN_IPHC_SAC) {
  578. spin_lock_bh(&lowpan_dev(dev)->ctx.lock);
  579. ci = lowpan_iphc_ctx_get_by_id(dev, LOWPAN_IPHC_CID_SCI(cid));
  580. if (!ci) {
  581. spin_unlock_bh(&lowpan_dev(dev)->ctx.lock);
  582. return -EINVAL;
  583. }
  584. pr_debug("SAC bit is set. Handle context based source address.\n");
  585. err = lowpan_iphc_uncompress_ctx_addr(skb, dev, ci, &hdr.saddr,
  586. iphc1 & LOWPAN_IPHC_SAM_MASK,
  587. saddr);
  588. spin_unlock_bh(&lowpan_dev(dev)->ctx.lock);
  589. } else {
  590. /* Source address uncompression */
  591. pr_debug("source address stateless compression\n");
  592. err = lowpan_iphc_uncompress_addr(skb, dev, &hdr.saddr,
  593. iphc1 & LOWPAN_IPHC_SAM_MASK,
  594. saddr);
  595. }
  596. /* Check on error of previous branch */
  597. if (err)
  598. return -EINVAL;
  599. switch (iphc1 & (LOWPAN_IPHC_M | LOWPAN_IPHC_DAC)) {
  600. case LOWPAN_IPHC_M | LOWPAN_IPHC_DAC:
  601. skb->pkt_type = PACKET_BROADCAST;
  602. spin_lock_bh(&lowpan_dev(dev)->ctx.lock);
  603. ci = lowpan_iphc_ctx_get_by_id(dev, LOWPAN_IPHC_CID_DCI(cid));
  604. if (!ci) {
  605. spin_unlock_bh(&lowpan_dev(dev)->ctx.lock);
  606. return -EINVAL;
  607. }
  608. /* multicast with context */
  609. pr_debug("dest: context-based mcast compression\n");
  610. err = lowpan_uncompress_multicast_ctx_daddr(skb, ci,
  611. &hdr.daddr,
  612. iphc1 & LOWPAN_IPHC_DAM_MASK);
  613. spin_unlock_bh(&lowpan_dev(dev)->ctx.lock);
  614. break;
  615. case LOWPAN_IPHC_M:
  616. skb->pkt_type = PACKET_BROADCAST;
  617. /* multicast */
  618. err = lowpan_uncompress_multicast_daddr(skb, &hdr.daddr,
  619. iphc1 & LOWPAN_IPHC_DAM_MASK);
  620. break;
  621. case LOWPAN_IPHC_DAC:
  622. skb->pkt_type = PACKET_HOST;
  623. spin_lock_bh(&lowpan_dev(dev)->ctx.lock);
  624. ci = lowpan_iphc_ctx_get_by_id(dev, LOWPAN_IPHC_CID_DCI(cid));
  625. if (!ci) {
  626. spin_unlock_bh(&lowpan_dev(dev)->ctx.lock);
  627. return -EINVAL;
  628. }
  629. /* Destination address context based uncompression */
  630. pr_debug("DAC bit is set. Handle context based destination address.\n");
  631. err = lowpan_iphc_uncompress_ctx_addr(skb, dev, ci, &hdr.daddr,
  632. iphc1 & LOWPAN_IPHC_DAM_MASK,
  633. daddr);
  634. spin_unlock_bh(&lowpan_dev(dev)->ctx.lock);
  635. break;
  636. default:
  637. skb->pkt_type = PACKET_HOST;
  638. err = lowpan_iphc_uncompress_addr(skb, dev, &hdr.daddr,
  639. iphc1 & LOWPAN_IPHC_DAM_MASK,
  640. daddr);
  641. pr_debug("dest: stateless compression mode %d dest %pI6c\n",
  642. iphc1 & LOWPAN_IPHC_DAM_MASK, &hdr.daddr);
  643. break;
  644. }
  645. if (err)
  646. return -EINVAL;
  647. /* Next header data uncompression */
  648. if (iphc0 & LOWPAN_IPHC_NH) {
  649. err = lowpan_nhc_do_uncompression(skb, dev, &hdr);
  650. if (err < 0)
  651. return err;
  652. } else {
  653. err = skb_cow(skb, sizeof(hdr));
  654. if (unlikely(err))
  655. return err;
  656. }
  657. switch (lowpan_dev(dev)->lltype) {
  658. case LOWPAN_LLTYPE_IEEE802154:
  659. if (lowpan_802154_cb(skb)->d_size)
  660. hdr.payload_len = htons(lowpan_802154_cb(skb)->d_size -
  661. sizeof(struct ipv6hdr));
  662. else
  663. hdr.payload_len = htons(skb->len);
  664. break;
  665. default:
  666. hdr.payload_len = htons(skb->len);
  667. break;
  668. }
  669. pr_debug("skb headroom size = %d, data length = %d\n",
  670. skb_headroom(skb), skb->len);
  671. pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength = %d\n\t"
  672. "nexthdr = 0x%02x\n\thop_lim = %d\n\tdest = %pI6c\n",
  673. hdr.version, ntohs(hdr.payload_len), hdr.nexthdr,
  674. hdr.hop_limit, &hdr.daddr);
  675. skb_push(skb, sizeof(hdr));
  676. skb_reset_network_header(skb);
  677. skb_copy_to_linear_data(skb, &hdr, sizeof(hdr));
  678. raw_dump_table(__func__, "raw header dump", (u8 *)&hdr, sizeof(hdr));
  679. return 0;
  680. }
  681. EXPORT_SYMBOL_GPL(lowpan_header_decompress);
  682. static const u8 lowpan_iphc_dam_to_sam_value[] = {
  683. [LOWPAN_IPHC_DAM_00] = LOWPAN_IPHC_SAM_00,
  684. [LOWPAN_IPHC_DAM_01] = LOWPAN_IPHC_SAM_01,
  685. [LOWPAN_IPHC_DAM_10] = LOWPAN_IPHC_SAM_10,
  686. [LOWPAN_IPHC_DAM_11] = LOWPAN_IPHC_SAM_11,
  687. };
  688. static inline bool
  689. lowpan_iphc_compress_ctx_802154_lladdr(const struct in6_addr *ipaddr,
  690. const struct lowpan_iphc_ctx *ctx,
  691. const void *lladdr)
  692. {
  693. const struct ieee802154_addr *addr = lladdr;
  694. unsigned char extended_addr[EUI64_ADDR_LEN];
  695. bool lladdr_compress = false;
  696. struct in6_addr tmp = {};
  697. switch (addr->mode) {
  698. case IEEE802154_ADDR_LONG:
  699. ieee802154_le64_to_be64(&extended_addr, &addr->extended_addr);
  700. /* check for SAM/DAM = 11 */
  701. memcpy(&tmp.s6_addr[8], &extended_addr, EUI64_ADDR_LEN);
  702. /* second bit-flip (Universe/Local) is done according RFC2464 */
  703. tmp.s6_addr[8] ^= 0x02;
  704. /* context information are always used */
  705. ipv6_addr_prefix_copy(&tmp, &ctx->pfx, ctx->plen);
  706. if (ipv6_addr_equal(&tmp, ipaddr))
  707. lladdr_compress = true;
  708. break;
  709. case IEEE802154_ADDR_SHORT:
  710. tmp.s6_addr[11] = 0xFF;
  711. tmp.s6_addr[12] = 0xFE;
  712. ieee802154_le16_to_be16(&tmp.s6_addr16[7],
  713. &addr->short_addr);
  714. /* context information are always used */
  715. ipv6_addr_prefix_copy(&tmp, &ctx->pfx, ctx->plen);
  716. if (ipv6_addr_equal(&tmp, ipaddr))
  717. lladdr_compress = true;
  718. break;
  719. default:
  720. /* should never handled and filtered by 802154 6lowpan */
  721. WARN_ON_ONCE(1);
  722. break;
  723. }
  724. return lladdr_compress;
  725. }
  726. static bool lowpan_iphc_addr_equal(const struct net_device *dev,
  727. const struct lowpan_iphc_ctx *ctx,
  728. const struct in6_addr *ipaddr,
  729. const void *lladdr)
  730. {
  731. struct in6_addr tmp = {};
  732. lowpan_iphc_uncompress_lladdr(dev, &tmp, lladdr);
  733. if (ctx)
  734. ipv6_addr_prefix_copy(&tmp, &ctx->pfx, ctx->plen);
  735. return ipv6_addr_equal(&tmp, ipaddr);
  736. }
  737. static u8 lowpan_compress_ctx_addr(u8 **hc_ptr, const struct net_device *dev,
  738. const struct in6_addr *ipaddr,
  739. const struct lowpan_iphc_ctx *ctx,
  740. const unsigned char *lladdr, bool sam)
  741. {
  742. struct in6_addr tmp = {};
  743. u8 dam;
  744. switch (lowpan_dev(dev)->lltype) {
  745. case LOWPAN_LLTYPE_IEEE802154:
  746. if (lowpan_iphc_compress_ctx_802154_lladdr(ipaddr, ctx,
  747. lladdr)) {
  748. dam = LOWPAN_IPHC_DAM_11;
  749. goto out;
  750. }
  751. break;
  752. default:
  753. if (lowpan_iphc_addr_equal(dev, ctx, ipaddr, lladdr)) {
  754. dam = LOWPAN_IPHC_DAM_11;
  755. goto out;
  756. }
  757. break;
  758. }
  759. memset(&tmp, 0, sizeof(tmp));
  760. /* check for SAM/DAM = 10 */
  761. tmp.s6_addr[11] = 0xFF;
  762. tmp.s6_addr[12] = 0xFE;
  763. memcpy(&tmp.s6_addr[14], &ipaddr->s6_addr[14], 2);
  764. /* context information are always used */
  765. ipv6_addr_prefix_copy(&tmp, &ctx->pfx, ctx->plen);
  766. if (ipv6_addr_equal(&tmp, ipaddr)) {
  767. lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr[14], 2);
  768. dam = LOWPAN_IPHC_DAM_10;
  769. goto out;
  770. }
  771. memset(&tmp, 0, sizeof(tmp));
  772. /* check for SAM/DAM = 01, should always match */
  773. memcpy(&tmp.s6_addr[8], &ipaddr->s6_addr[8], 8);
  774. /* context information are always used */
  775. ipv6_addr_prefix_copy(&tmp, &ctx->pfx, ctx->plen);
  776. if (ipv6_addr_equal(&tmp, ipaddr)) {
  777. lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr[8], 8);
  778. dam = LOWPAN_IPHC_DAM_01;
  779. goto out;
  780. }
  781. WARN_ONCE(1, "context found but no address mode matched\n");
  782. return LOWPAN_IPHC_DAM_00;
  783. out:
  784. if (sam)
  785. return lowpan_iphc_dam_to_sam_value[dam];
  786. else
  787. return dam;
  788. }
  789. static inline bool
  790. lowpan_iphc_compress_802154_lladdr(const struct in6_addr *ipaddr,
  791. const void *lladdr)
  792. {
  793. const struct ieee802154_addr *addr = lladdr;
  794. unsigned char extended_addr[EUI64_ADDR_LEN];
  795. bool lladdr_compress = false;
  796. struct in6_addr tmp = {};
  797. switch (addr->mode) {
  798. case IEEE802154_ADDR_LONG:
  799. ieee802154_le64_to_be64(&extended_addr, &addr->extended_addr);
  800. if (is_addr_mac_addr_based(ipaddr, extended_addr))
  801. lladdr_compress = true;
  802. break;
  803. case IEEE802154_ADDR_SHORT:
  804. /* fe:80::ff:fe00:XXXX
  805. * \__/
  806. * short_addr
  807. *
  808. * Universe/Local bit is zero.
  809. */
  810. tmp.s6_addr[0] = 0xFE;
  811. tmp.s6_addr[1] = 0x80;
  812. tmp.s6_addr[11] = 0xFF;
  813. tmp.s6_addr[12] = 0xFE;
  814. ieee802154_le16_to_be16(&tmp.s6_addr16[7],
  815. &addr->short_addr);
  816. if (ipv6_addr_equal(&tmp, ipaddr))
  817. lladdr_compress = true;
  818. break;
  819. default:
  820. /* should never handled and filtered by 802154 6lowpan */
  821. WARN_ON_ONCE(1);
  822. break;
  823. }
  824. return lladdr_compress;
  825. }
  826. static u8 lowpan_compress_addr_64(u8 **hc_ptr, const struct net_device *dev,
  827. const struct in6_addr *ipaddr,
  828. const unsigned char *lladdr, bool sam)
  829. {
  830. u8 dam = LOWPAN_IPHC_DAM_01;
  831. switch (lowpan_dev(dev)->lltype) {
  832. case LOWPAN_LLTYPE_IEEE802154:
  833. if (lowpan_iphc_compress_802154_lladdr(ipaddr, lladdr)) {
  834. dam = LOWPAN_IPHC_DAM_11; /* 0-bits */
  835. pr_debug("address compression 0 bits\n");
  836. goto out;
  837. }
  838. break;
  839. default:
  840. if (lowpan_iphc_addr_equal(dev, NULL, ipaddr, lladdr)) {
  841. dam = LOWPAN_IPHC_DAM_11;
  842. pr_debug("address compression 0 bits\n");
  843. goto out;
  844. }
  845. break;
  846. }
  847. if (lowpan_is_iid_16_bit_compressable(ipaddr)) {
  848. /* compress IID to 16 bits xxxx::XXXX */
  849. lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr16[7], 2);
  850. dam = LOWPAN_IPHC_DAM_10; /* 16-bits */
  851. raw_dump_inline(NULL, "Compressed ipv6 addr is (16 bits)",
  852. *hc_ptr - 2, 2);
  853. goto out;
  854. }
  855. /* do not compress IID => xxxx::IID */
  856. lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr16[4], 8);
  857. raw_dump_inline(NULL, "Compressed ipv6 addr is (64 bits)",
  858. *hc_ptr - 8, 8);
  859. out:
  860. if (sam)
  861. return lowpan_iphc_dam_to_sam_value[dam];
  862. else
  863. return dam;
  864. }
  865. /* lowpan_iphc_get_tc - get the ECN + DCSP fields in hc format */
  866. static inline u8 lowpan_iphc_get_tc(const struct ipv6hdr *hdr)
  867. {
  868. u8 dscp, ecn;
  869. /* hdr->priority contains the higher bits of dscp, lower are part of
  870. * flow_lbl[0]. Note ECN, DCSP is swapped in ipv6 hdr.
  871. */
  872. dscp = (hdr->priority << 2) | ((hdr->flow_lbl[0] & 0xc0) >> 6);
  873. /* ECN is at the two lower bits from first nibble of flow_lbl[0] */
  874. ecn = (hdr->flow_lbl[0] & 0x30);
  875. /* for pretty debug output, also shift ecn to get the ecn value */
  876. pr_debug("ecn 0x%02x dscp 0x%02x\n", ecn >> 4, dscp);
  877. /* ECN is at 0x30 now, shift it to have ECN + DCSP */
  878. return (ecn << 2) | dscp;
  879. }
  880. /* lowpan_iphc_is_flow_lbl_zero - check if flow label is zero */
  881. static inline bool lowpan_iphc_is_flow_lbl_zero(const struct ipv6hdr *hdr)
  882. {
  883. return ((!(hdr->flow_lbl[0] & 0x0f)) &&
  884. !hdr->flow_lbl[1] && !hdr->flow_lbl[2]);
  885. }
  886. /* lowpan_iphc_tf_compress - compress the traffic class which is set by
  887. * ipv6hdr. Return the corresponding format identifier which is used.
  888. */
  889. static u8 lowpan_iphc_tf_compress(u8 **hc_ptr, const struct ipv6hdr *hdr)
  890. {
  891. /* get ecn dscp data in a byteformat as: ECN(hi) + DSCP(lo) */
  892. u8 tc = lowpan_iphc_get_tc(hdr), tf[4], val;
  893. /* printout the traffic class in hc format */
  894. pr_debug("tc 0x%02x\n", tc);
  895. if (lowpan_iphc_is_flow_lbl_zero(hdr)) {
  896. if (!tc) {
  897. /* 11: Traffic Class and Flow Label are elided. */
  898. val = LOWPAN_IPHC_TF_11;
  899. } else {
  900. /* 10: ECN + DSCP (1 byte), Flow Label is elided.
  901. *
  902. * 0 1 2 3 4 5 6 7
  903. * +-+-+-+-+-+-+-+-+
  904. * |ECN| DSCP |
  905. * +-+-+-+-+-+-+-+-+
  906. */
  907. lowpan_push_hc_data(hc_ptr, &tc, sizeof(tc));
  908. val = LOWPAN_IPHC_TF_10;
  909. }
  910. } else {
  911. /* check if dscp is zero, it's after the first two bit */
  912. if (!(tc & 0x3f)) {
  913. /* 01: ECN + 2-bit Pad + Flow Label (3 bytes), DSCP is elided
  914. *
  915. * 1 2
  916. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
  917. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  918. * |ECN|rsv| Flow Label |
  919. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  920. */
  921. memcpy(&tf[0], &hdr->flow_lbl[0], 3);
  922. /* zero the highest 4-bits, contains DCSP + ECN */
  923. tf[0] &= ~0xf0;
  924. /* set ECN */
  925. tf[0] |= (tc & 0xc0);
  926. lowpan_push_hc_data(hc_ptr, tf, 3);
  927. val = LOWPAN_IPHC_TF_01;
  928. } else {
  929. /* 00: ECN + DSCP + 4-bit Pad + Flow Label (4 bytes)
  930. *
  931. * 1 2 3
  932. * 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
  933. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  934. * |ECN| DSCP | rsv | Flow Label |
  935. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  936. */
  937. memcpy(&tf[0], &tc, sizeof(tc));
  938. /* highest nibble of flow_lbl[0] is part of DSCP + ECN
  939. * which will be the 4-bit pad and will be filled with
  940. * zeros afterwards.
  941. */
  942. memcpy(&tf[1], &hdr->flow_lbl[0], 3);
  943. /* zero the 4-bit pad, which is reserved */
  944. tf[1] &= ~0xf0;
  945. lowpan_push_hc_data(hc_ptr, tf, 4);
  946. val = LOWPAN_IPHC_TF_00;
  947. }
  948. }
  949. return val;
  950. }
  951. static u8 lowpan_iphc_mcast_ctx_addr_compress(u8 **hc_ptr,
  952. const struct lowpan_iphc_ctx *ctx,
  953. const struct in6_addr *ipaddr)
  954. {
  955. u8 data[6];
  956. /* flags/scope, reserved (RIID) */
  957. memcpy(data, &ipaddr->s6_addr[1], 2);
  958. /* group ID */
  959. memcpy(&data[1], &ipaddr->s6_addr[11], 4);
  960. lowpan_push_hc_data(hc_ptr, data, 6);
  961. return LOWPAN_IPHC_DAM_00;
  962. }
  963. static u8 lowpan_iphc_mcast_addr_compress(u8 **hc_ptr,
  964. const struct in6_addr *ipaddr)
  965. {
  966. u8 val;
  967. if (lowpan_is_mcast_addr_compressable8(ipaddr)) {
  968. pr_debug("compressed to 1 octet\n");
  969. /* use last byte */
  970. lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr[15], 1);
  971. val = LOWPAN_IPHC_DAM_11;
  972. } else if (lowpan_is_mcast_addr_compressable32(ipaddr)) {
  973. pr_debug("compressed to 4 octets\n");
  974. /* second byte + the last three */
  975. lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr[1], 1);
  976. lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr[13], 3);
  977. val = LOWPAN_IPHC_DAM_10;
  978. } else if (lowpan_is_mcast_addr_compressable48(ipaddr)) {
  979. pr_debug("compressed to 6 octets\n");
  980. /* second byte + the last five */
  981. lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr[1], 1);
  982. lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr[11], 5);
  983. val = LOWPAN_IPHC_DAM_01;
  984. } else {
  985. pr_debug("using full address\n");
  986. lowpan_push_hc_data(hc_ptr, ipaddr->s6_addr, 16);
  987. val = LOWPAN_IPHC_DAM_00;
  988. }
  989. return val;
  990. }
  991. int lowpan_header_compress(struct sk_buff *skb, const struct net_device *dev,
  992. const void *daddr, const void *saddr)
  993. {
  994. u8 iphc0, iphc1, *hc_ptr, cid = 0;
  995. struct ipv6hdr *hdr;
  996. u8 head[LOWPAN_IPHC_MAX_HC_BUF_LEN] = {};
  997. struct lowpan_iphc_ctx *dci, *sci, dci_entry, sci_entry;
  998. int ret, ipv6_daddr_type, ipv6_saddr_type;
  999. if (skb->protocol != htons(ETH_P_IPV6))
  1000. return -EINVAL;
  1001. hdr = ipv6_hdr(skb);
  1002. hc_ptr = head + 2;
  1003. pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength = %d\n"
  1004. "\tnexthdr = 0x%02x\n\thop_lim = %d\n\tdest = %pI6c\n",
  1005. hdr->version, ntohs(hdr->payload_len), hdr->nexthdr,
  1006. hdr->hop_limit, &hdr->daddr);
  1007. raw_dump_table(__func__, "raw skb network header dump",
  1008. skb_network_header(skb), sizeof(struct ipv6hdr));
  1009. /* As we copy some bit-length fields, in the IPHC encoding bytes,
  1010. * we sometimes use |=
  1011. * If the field is 0, and the current bit value in memory is 1,
  1012. * this does not work. We therefore reset the IPHC encoding here
  1013. */
  1014. iphc0 = LOWPAN_DISPATCH_IPHC;
  1015. iphc1 = 0;
  1016. raw_dump_table(__func__, "sending raw skb network uncompressed packet",
  1017. skb->data, skb->len);
  1018. ipv6_daddr_type = ipv6_addr_type(&hdr->daddr);
  1019. spin_lock_bh(&lowpan_dev(dev)->ctx.lock);
  1020. if (ipv6_daddr_type & IPV6_ADDR_MULTICAST)
  1021. dci = lowpan_iphc_ctx_get_by_mcast_addr(dev, &hdr->daddr);
  1022. else
  1023. dci = lowpan_iphc_ctx_get_by_addr(dev, &hdr->daddr);
  1024. if (dci) {
  1025. memcpy(&dci_entry, dci, sizeof(*dci));
  1026. cid |= dci->id;
  1027. }
  1028. spin_unlock_bh(&lowpan_dev(dev)->ctx.lock);
  1029. spin_lock_bh(&lowpan_dev(dev)->ctx.lock);
  1030. sci = lowpan_iphc_ctx_get_by_addr(dev, &hdr->saddr);
  1031. if (sci) {
  1032. memcpy(&sci_entry, sci, sizeof(*sci));
  1033. cid |= (sci->id << 4);
  1034. }
  1035. spin_unlock_bh(&lowpan_dev(dev)->ctx.lock);
  1036. /* if cid is zero it will be compressed */
  1037. if (cid) {
  1038. iphc1 |= LOWPAN_IPHC_CID;
  1039. lowpan_push_hc_data(&hc_ptr, &cid, sizeof(cid));
  1040. }
  1041. /* Traffic Class, Flow Label compression */
  1042. iphc0 |= lowpan_iphc_tf_compress(&hc_ptr, hdr);
  1043. /* NOTE: payload length is always compressed */
  1044. /* Check if we provide the nhc format for nexthdr and compression
  1045. * functionality. If not nexthdr is handled inline and not compressed.
  1046. */
  1047. ret = lowpan_nhc_check_compression(skb, hdr, &hc_ptr);
  1048. if (ret == -ENOENT)
  1049. lowpan_push_hc_data(&hc_ptr, &hdr->nexthdr,
  1050. sizeof(hdr->nexthdr));
  1051. else
  1052. iphc0 |= LOWPAN_IPHC_NH;
  1053. /* Hop limit
  1054. * if 1: compress, encoding is 01
  1055. * if 64: compress, encoding is 10
  1056. * if 255: compress, encoding is 11
  1057. * else do not compress
  1058. */
  1059. switch (hdr->hop_limit) {
  1060. case 1:
  1061. iphc0 |= LOWPAN_IPHC_HLIM_01;
  1062. break;
  1063. case 64:
  1064. iphc0 |= LOWPAN_IPHC_HLIM_10;
  1065. break;
  1066. case 255:
  1067. iphc0 |= LOWPAN_IPHC_HLIM_11;
  1068. break;
  1069. default:
  1070. lowpan_push_hc_data(&hc_ptr, &hdr->hop_limit,
  1071. sizeof(hdr->hop_limit));
  1072. }
  1073. ipv6_saddr_type = ipv6_addr_type(&hdr->saddr);
  1074. /* source address compression */
  1075. if (ipv6_saddr_type == IPV6_ADDR_ANY) {
  1076. pr_debug("source address is unspecified, setting SAC\n");
  1077. iphc1 |= LOWPAN_IPHC_SAC;
  1078. } else {
  1079. if (sci) {
  1080. iphc1 |= lowpan_compress_ctx_addr(&hc_ptr, dev,
  1081. &hdr->saddr,
  1082. &sci_entry, saddr,
  1083. true);
  1084. iphc1 |= LOWPAN_IPHC_SAC;
  1085. } else {
  1086. if (ipv6_saddr_type & IPV6_ADDR_LINKLOCAL &&
  1087. lowpan_is_linklocal_zero_padded(hdr->saddr)) {
  1088. iphc1 |= lowpan_compress_addr_64(&hc_ptr, dev,
  1089. &hdr->saddr,
  1090. saddr, true);
  1091. pr_debug("source address unicast link-local %pI6c iphc1 0x%02x\n",
  1092. &hdr->saddr, iphc1);
  1093. } else {
  1094. pr_debug("send the full source address\n");
  1095. lowpan_push_hc_data(&hc_ptr,
  1096. hdr->saddr.s6_addr, 16);
  1097. }
  1098. }
  1099. }
  1100. /* destination address compression */
  1101. if (ipv6_daddr_type & IPV6_ADDR_MULTICAST) {
  1102. pr_debug("destination address is multicast: ");
  1103. iphc1 |= LOWPAN_IPHC_M;
  1104. if (dci) {
  1105. iphc1 |= lowpan_iphc_mcast_ctx_addr_compress(&hc_ptr,
  1106. &dci_entry,
  1107. &hdr->daddr);
  1108. iphc1 |= LOWPAN_IPHC_DAC;
  1109. } else {
  1110. iphc1 |= lowpan_iphc_mcast_addr_compress(&hc_ptr,
  1111. &hdr->daddr);
  1112. }
  1113. } else {
  1114. if (dci) {
  1115. iphc1 |= lowpan_compress_ctx_addr(&hc_ptr, dev,
  1116. &hdr->daddr,
  1117. &dci_entry, daddr,
  1118. false);
  1119. iphc1 |= LOWPAN_IPHC_DAC;
  1120. } else {
  1121. if (ipv6_daddr_type & IPV6_ADDR_LINKLOCAL &&
  1122. lowpan_is_linklocal_zero_padded(hdr->daddr)) {
  1123. iphc1 |= lowpan_compress_addr_64(&hc_ptr, dev,
  1124. &hdr->daddr,
  1125. daddr, false);
  1126. pr_debug("dest address unicast link-local %pI6c iphc1 0x%02x\n",
  1127. &hdr->daddr, iphc1);
  1128. } else {
  1129. pr_debug("dest address unicast %pI6c\n",
  1130. &hdr->daddr);
  1131. lowpan_push_hc_data(&hc_ptr,
  1132. hdr->daddr.s6_addr, 16);
  1133. }
  1134. }
  1135. }
  1136. /* next header compression */
  1137. if (iphc0 & LOWPAN_IPHC_NH) {
  1138. ret = lowpan_nhc_do_compression(skb, hdr, &hc_ptr);
  1139. if (ret < 0)
  1140. return ret;
  1141. }
  1142. head[0] = iphc0;
  1143. head[1] = iphc1;
  1144. skb_pull(skb, sizeof(struct ipv6hdr));
  1145. skb_reset_transport_header(skb);
  1146. memcpy(skb_push(skb, hc_ptr - head), head, hc_ptr - head);
  1147. skb_reset_network_header(skb);
  1148. pr_debug("header len %d skb %u\n", (int)(hc_ptr - head), skb->len);
  1149. raw_dump_table(__func__, "raw skb data dump compressed",
  1150. skb->data, skb->len);
  1151. return 0;
  1152. }
  1153. EXPORT_SYMBOL_GPL(lowpan_header_compress);