iphc.c 36 KB

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