iphc.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277
  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. /* 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_network_header(skb);
  657. skb_copy_to_linear_data(skb, &hdr, sizeof(hdr));
  658. raw_dump_table(__func__, "raw header dump", (u8 *)&hdr, sizeof(hdr));
  659. return 0;
  660. }
  661. EXPORT_SYMBOL_GPL(lowpan_header_decompress);
  662. static const u8 lowpan_iphc_dam_to_sam_value[] = {
  663. [LOWPAN_IPHC_DAM_00] = LOWPAN_IPHC_SAM_00,
  664. [LOWPAN_IPHC_DAM_01] = LOWPAN_IPHC_SAM_01,
  665. [LOWPAN_IPHC_DAM_10] = LOWPAN_IPHC_SAM_10,
  666. [LOWPAN_IPHC_DAM_11] = LOWPAN_IPHC_SAM_11,
  667. };
  668. static inline bool
  669. lowpan_iphc_compress_ctx_802154_lladdr(const struct in6_addr *ipaddr,
  670. const struct lowpan_iphc_ctx *ctx,
  671. const void *lladdr)
  672. {
  673. const struct ieee802154_addr *addr = lladdr;
  674. unsigned char extended_addr[EUI64_ADDR_LEN];
  675. bool lladdr_compress = false;
  676. struct in6_addr tmp = {};
  677. switch (addr->mode) {
  678. case IEEE802154_ADDR_LONG:
  679. ieee802154_le64_to_be64(&extended_addr, &addr->extended_addr);
  680. /* check for SAM/DAM = 11 */
  681. memcpy(&tmp.s6_addr[8], &extended_addr, EUI64_ADDR_LEN);
  682. /* second bit-flip (Universe/Local) is done according RFC2464 */
  683. tmp.s6_addr[8] ^= 0x02;
  684. /* context information are always used */
  685. ipv6_addr_prefix_copy(&tmp, &ctx->pfx, ctx->plen);
  686. if (ipv6_addr_equal(&tmp, ipaddr))
  687. lladdr_compress = true;
  688. break;
  689. case IEEE802154_ADDR_SHORT:
  690. tmp.s6_addr[11] = 0xFF;
  691. tmp.s6_addr[12] = 0xFE;
  692. ieee802154_le16_to_be16(&tmp.s6_addr16[7],
  693. &addr->short_addr);
  694. /* context information are always used */
  695. ipv6_addr_prefix_copy(&tmp, &ctx->pfx, ctx->plen);
  696. if (ipv6_addr_equal(&tmp, ipaddr))
  697. lladdr_compress = true;
  698. break;
  699. default:
  700. /* should never handled and filtered by 802154 6lowpan */
  701. WARN_ON_ONCE(1);
  702. break;
  703. }
  704. return lladdr_compress;
  705. }
  706. static u8 lowpan_compress_ctx_addr(u8 **hc_ptr, const struct net_device *dev,
  707. const struct in6_addr *ipaddr,
  708. const struct lowpan_iphc_ctx *ctx,
  709. const unsigned char *lladdr, bool sam)
  710. {
  711. struct in6_addr tmp = {};
  712. u8 dam;
  713. switch (lowpan_dev(dev)->lltype) {
  714. case LOWPAN_LLTYPE_IEEE802154:
  715. if (lowpan_iphc_compress_ctx_802154_lladdr(ipaddr, ctx,
  716. lladdr)) {
  717. dam = LOWPAN_IPHC_DAM_11;
  718. goto out;
  719. }
  720. break;
  721. default:
  722. /* check for SAM/DAM = 11 */
  723. memcpy(&tmp.s6_addr[8], lladdr, EUI64_ADDR_LEN);
  724. /* second bit-flip (Universe/Local) is done according RFC2464 */
  725. tmp.s6_addr[8] ^= 0x02;
  726. /* context information are always used */
  727. ipv6_addr_prefix_copy(&tmp, &ctx->pfx, ctx->plen);
  728. if (ipv6_addr_equal(&tmp, ipaddr)) {
  729. dam = LOWPAN_IPHC_DAM_11;
  730. goto out;
  731. }
  732. break;
  733. }
  734. memset(&tmp, 0, sizeof(tmp));
  735. /* check for SAM/DAM = 10 */
  736. tmp.s6_addr[11] = 0xFF;
  737. tmp.s6_addr[12] = 0xFE;
  738. memcpy(&tmp.s6_addr[14], &ipaddr->s6_addr[14], 2);
  739. /* context information are always used */
  740. ipv6_addr_prefix_copy(&tmp, &ctx->pfx, ctx->plen);
  741. if (ipv6_addr_equal(&tmp, ipaddr)) {
  742. lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr[14], 2);
  743. dam = LOWPAN_IPHC_DAM_10;
  744. goto out;
  745. }
  746. memset(&tmp, 0, sizeof(tmp));
  747. /* check for SAM/DAM = 01, should always match */
  748. memcpy(&tmp.s6_addr[8], &ipaddr->s6_addr[8], 8);
  749. /* context information are always used */
  750. ipv6_addr_prefix_copy(&tmp, &ctx->pfx, ctx->plen);
  751. if (ipv6_addr_equal(&tmp, ipaddr)) {
  752. lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr[8], 8);
  753. dam = LOWPAN_IPHC_DAM_01;
  754. goto out;
  755. }
  756. WARN_ONCE(1, "context found but no address mode matched\n");
  757. return LOWPAN_IPHC_DAM_00;
  758. out:
  759. if (sam)
  760. return lowpan_iphc_dam_to_sam_value[dam];
  761. else
  762. return dam;
  763. }
  764. static inline bool
  765. lowpan_iphc_compress_802154_lladdr(const struct in6_addr *ipaddr,
  766. const void *lladdr)
  767. {
  768. const struct ieee802154_addr *addr = lladdr;
  769. unsigned char extended_addr[EUI64_ADDR_LEN];
  770. bool lladdr_compress = false;
  771. struct in6_addr tmp = {};
  772. switch (addr->mode) {
  773. case IEEE802154_ADDR_LONG:
  774. ieee802154_le64_to_be64(&extended_addr, &addr->extended_addr);
  775. if (is_addr_mac_addr_based(ipaddr, extended_addr))
  776. lladdr_compress = true;
  777. break;
  778. case IEEE802154_ADDR_SHORT:
  779. /* fe:80::ff:fe00:XXXX
  780. * \__/
  781. * short_addr
  782. *
  783. * Universe/Local bit is zero.
  784. */
  785. tmp.s6_addr[0] = 0xFE;
  786. tmp.s6_addr[1] = 0x80;
  787. tmp.s6_addr[11] = 0xFF;
  788. tmp.s6_addr[12] = 0xFE;
  789. ieee802154_le16_to_be16(&tmp.s6_addr16[7],
  790. &addr->short_addr);
  791. if (ipv6_addr_equal(&tmp, ipaddr))
  792. lladdr_compress = true;
  793. break;
  794. default:
  795. /* should never handled and filtered by 802154 6lowpan */
  796. WARN_ON_ONCE(1);
  797. break;
  798. }
  799. return lladdr_compress;
  800. }
  801. static u8 lowpan_compress_addr_64(u8 **hc_ptr, const struct net_device *dev,
  802. const struct in6_addr *ipaddr,
  803. const unsigned char *lladdr, bool sam)
  804. {
  805. u8 dam = LOWPAN_IPHC_DAM_01;
  806. switch (lowpan_dev(dev)->lltype) {
  807. case LOWPAN_LLTYPE_IEEE802154:
  808. if (lowpan_iphc_compress_802154_lladdr(ipaddr, lladdr)) {
  809. dam = LOWPAN_IPHC_DAM_11; /* 0-bits */
  810. pr_debug("address compression 0 bits\n");
  811. goto out;
  812. }
  813. break;
  814. default:
  815. if (is_addr_mac_addr_based(ipaddr, lladdr)) {
  816. dam = LOWPAN_IPHC_DAM_11; /* 0-bits */
  817. pr_debug("address compression 0 bits\n");
  818. goto out;
  819. }
  820. break;
  821. }
  822. if (lowpan_is_iid_16_bit_compressable(ipaddr)) {
  823. /* compress IID to 16 bits xxxx::XXXX */
  824. lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr16[7], 2);
  825. dam = LOWPAN_IPHC_DAM_10; /* 16-bits */
  826. raw_dump_inline(NULL, "Compressed ipv6 addr is (16 bits)",
  827. *hc_ptr - 2, 2);
  828. goto out;
  829. }
  830. /* do not compress IID => xxxx::IID */
  831. lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr16[4], 8);
  832. raw_dump_inline(NULL, "Compressed ipv6 addr is (64 bits)",
  833. *hc_ptr - 8, 8);
  834. out:
  835. if (sam)
  836. return lowpan_iphc_dam_to_sam_value[dam];
  837. else
  838. return dam;
  839. }
  840. /* lowpan_iphc_get_tc - get the ECN + DCSP fields in hc format */
  841. static inline u8 lowpan_iphc_get_tc(const struct ipv6hdr *hdr)
  842. {
  843. u8 dscp, ecn;
  844. /* hdr->priority contains the higher bits of dscp, lower are part of
  845. * flow_lbl[0]. Note ECN, DCSP is swapped in ipv6 hdr.
  846. */
  847. dscp = (hdr->priority << 2) | ((hdr->flow_lbl[0] & 0xc0) >> 6);
  848. /* ECN is at the two lower bits from first nibble of flow_lbl[0] */
  849. ecn = (hdr->flow_lbl[0] & 0x30);
  850. /* for pretty debug output, also shift ecn to get the ecn value */
  851. pr_debug("ecn 0x%02x dscp 0x%02x\n", ecn >> 4, dscp);
  852. /* ECN is at 0x30 now, shift it to have ECN + DCSP */
  853. return (ecn << 2) | dscp;
  854. }
  855. /* lowpan_iphc_is_flow_lbl_zero - check if flow label is zero */
  856. static inline bool lowpan_iphc_is_flow_lbl_zero(const struct ipv6hdr *hdr)
  857. {
  858. return ((!(hdr->flow_lbl[0] & 0x0f)) &&
  859. !hdr->flow_lbl[1] && !hdr->flow_lbl[2]);
  860. }
  861. /* lowpan_iphc_tf_compress - compress the traffic class which is set by
  862. * ipv6hdr. Return the corresponding format identifier which is used.
  863. */
  864. static u8 lowpan_iphc_tf_compress(u8 **hc_ptr, const struct ipv6hdr *hdr)
  865. {
  866. /* get ecn dscp data in a byteformat as: ECN(hi) + DSCP(lo) */
  867. u8 tc = lowpan_iphc_get_tc(hdr), tf[4], val;
  868. /* printout the traffic class in hc format */
  869. pr_debug("tc 0x%02x\n", tc);
  870. if (lowpan_iphc_is_flow_lbl_zero(hdr)) {
  871. if (!tc) {
  872. /* 11: Traffic Class and Flow Label are elided. */
  873. val = LOWPAN_IPHC_TF_11;
  874. } else {
  875. /* 10: ECN + DSCP (1 byte), Flow Label is elided.
  876. *
  877. * 0 1 2 3 4 5 6 7
  878. * +-+-+-+-+-+-+-+-+
  879. * |ECN| DSCP |
  880. * +-+-+-+-+-+-+-+-+
  881. */
  882. lowpan_push_hc_data(hc_ptr, &tc, sizeof(tc));
  883. val = LOWPAN_IPHC_TF_10;
  884. }
  885. } else {
  886. /* check if dscp is zero, it's after the first two bit */
  887. if (!(tc & 0x3f)) {
  888. /* 01: ECN + 2-bit Pad + Flow Label (3 bytes), DSCP is elided
  889. *
  890. * 1 2
  891. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
  892. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  893. * |ECN|rsv| Flow Label |
  894. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  895. */
  896. memcpy(&tf[0], &hdr->flow_lbl[0], 3);
  897. /* zero the highest 4-bits, contains DCSP + ECN */
  898. tf[0] &= ~0xf0;
  899. /* set ECN */
  900. tf[0] |= (tc & 0xc0);
  901. lowpan_push_hc_data(hc_ptr, tf, 3);
  902. val = LOWPAN_IPHC_TF_01;
  903. } else {
  904. /* 00: ECN + DSCP + 4-bit Pad + Flow Label (4 bytes)
  905. *
  906. * 1 2 3
  907. * 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
  908. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  909. * |ECN| DSCP | rsv | Flow Label |
  910. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  911. */
  912. memcpy(&tf[0], &tc, sizeof(tc));
  913. /* highest nibble of flow_lbl[0] is part of DSCP + ECN
  914. * which will be the 4-bit pad and will be filled with
  915. * zeros afterwards.
  916. */
  917. memcpy(&tf[1], &hdr->flow_lbl[0], 3);
  918. /* zero the 4-bit pad, which is reserved */
  919. tf[1] &= ~0xf0;
  920. lowpan_push_hc_data(hc_ptr, tf, 4);
  921. val = LOWPAN_IPHC_TF_00;
  922. }
  923. }
  924. return val;
  925. }
  926. static u8 lowpan_iphc_mcast_ctx_addr_compress(u8 **hc_ptr,
  927. const struct lowpan_iphc_ctx *ctx,
  928. const struct in6_addr *ipaddr)
  929. {
  930. u8 data[6];
  931. /* flags/scope, reserved (RIID) */
  932. memcpy(data, &ipaddr->s6_addr[1], 2);
  933. /* group ID */
  934. memcpy(&data[1], &ipaddr->s6_addr[11], 4);
  935. lowpan_push_hc_data(hc_ptr, data, 6);
  936. return LOWPAN_IPHC_DAM_00;
  937. }
  938. static u8 lowpan_iphc_mcast_addr_compress(u8 **hc_ptr,
  939. const struct in6_addr *ipaddr)
  940. {
  941. u8 val;
  942. if (lowpan_is_mcast_addr_compressable8(ipaddr)) {
  943. pr_debug("compressed to 1 octet\n");
  944. /* use last byte */
  945. lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr[15], 1);
  946. val = LOWPAN_IPHC_DAM_11;
  947. } else if (lowpan_is_mcast_addr_compressable32(ipaddr)) {
  948. pr_debug("compressed to 4 octets\n");
  949. /* second byte + the last three */
  950. lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr[1], 1);
  951. lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr[13], 3);
  952. val = LOWPAN_IPHC_DAM_10;
  953. } else if (lowpan_is_mcast_addr_compressable48(ipaddr)) {
  954. pr_debug("compressed to 6 octets\n");
  955. /* second byte + the last five */
  956. lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr[1], 1);
  957. lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr[11], 5);
  958. val = LOWPAN_IPHC_DAM_01;
  959. } else {
  960. pr_debug("using full address\n");
  961. lowpan_push_hc_data(hc_ptr, ipaddr->s6_addr, 16);
  962. val = LOWPAN_IPHC_DAM_00;
  963. }
  964. return val;
  965. }
  966. int lowpan_header_compress(struct sk_buff *skb, const struct net_device *dev,
  967. const void *daddr, const void *saddr)
  968. {
  969. u8 iphc0, iphc1, *hc_ptr, cid = 0;
  970. struct ipv6hdr *hdr;
  971. u8 head[LOWPAN_IPHC_MAX_HC_BUF_LEN] = {};
  972. struct lowpan_iphc_ctx *dci, *sci, dci_entry, sci_entry;
  973. int ret, ipv6_daddr_type, ipv6_saddr_type;
  974. if (skb->protocol != htons(ETH_P_IPV6))
  975. return -EINVAL;
  976. hdr = ipv6_hdr(skb);
  977. hc_ptr = head + 2;
  978. pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength = %d\n"
  979. "\tnexthdr = 0x%02x\n\thop_lim = %d\n\tdest = %pI6c\n",
  980. hdr->version, ntohs(hdr->payload_len), hdr->nexthdr,
  981. hdr->hop_limit, &hdr->daddr);
  982. raw_dump_table(__func__, "raw skb network header dump",
  983. skb_network_header(skb), sizeof(struct ipv6hdr));
  984. /* As we copy some bit-length fields, in the IPHC encoding bytes,
  985. * we sometimes use |=
  986. * If the field is 0, and the current bit value in memory is 1,
  987. * this does not work. We therefore reset the IPHC encoding here
  988. */
  989. iphc0 = LOWPAN_DISPATCH_IPHC;
  990. iphc1 = 0;
  991. raw_dump_table(__func__, "sending raw skb network uncompressed packet",
  992. skb->data, skb->len);
  993. ipv6_daddr_type = ipv6_addr_type(&hdr->daddr);
  994. spin_lock_bh(&lowpan_dev(dev)->ctx.lock);
  995. if (ipv6_daddr_type & IPV6_ADDR_MULTICAST)
  996. dci = lowpan_iphc_ctx_get_by_mcast_addr(dev, &hdr->daddr);
  997. else
  998. dci = lowpan_iphc_ctx_get_by_addr(dev, &hdr->daddr);
  999. if (dci) {
  1000. memcpy(&dci_entry, dci, sizeof(*dci));
  1001. cid |= dci->id;
  1002. }
  1003. spin_unlock_bh(&lowpan_dev(dev)->ctx.lock);
  1004. spin_lock_bh(&lowpan_dev(dev)->ctx.lock);
  1005. sci = lowpan_iphc_ctx_get_by_addr(dev, &hdr->saddr);
  1006. if (sci) {
  1007. memcpy(&sci_entry, sci, sizeof(*sci));
  1008. cid |= (sci->id << 4);
  1009. }
  1010. spin_unlock_bh(&lowpan_dev(dev)->ctx.lock);
  1011. /* if cid is zero it will be compressed */
  1012. if (cid) {
  1013. iphc1 |= LOWPAN_IPHC_CID;
  1014. lowpan_push_hc_data(&hc_ptr, &cid, sizeof(cid));
  1015. }
  1016. /* Traffic Class, Flow Label compression */
  1017. iphc0 |= lowpan_iphc_tf_compress(&hc_ptr, hdr);
  1018. /* NOTE: payload length is always compressed */
  1019. /* Check if we provide the nhc format for nexthdr and compression
  1020. * functionality. If not nexthdr is handled inline and not compressed.
  1021. */
  1022. ret = lowpan_nhc_check_compression(skb, hdr, &hc_ptr);
  1023. if (ret == -ENOENT)
  1024. lowpan_push_hc_data(&hc_ptr, &hdr->nexthdr,
  1025. sizeof(hdr->nexthdr));
  1026. else
  1027. iphc0 |= LOWPAN_IPHC_NH;
  1028. /* Hop limit
  1029. * if 1: compress, encoding is 01
  1030. * if 64: compress, encoding is 10
  1031. * if 255: compress, encoding is 11
  1032. * else do not compress
  1033. */
  1034. switch (hdr->hop_limit) {
  1035. case 1:
  1036. iphc0 |= LOWPAN_IPHC_HLIM_01;
  1037. break;
  1038. case 64:
  1039. iphc0 |= LOWPAN_IPHC_HLIM_10;
  1040. break;
  1041. case 255:
  1042. iphc0 |= LOWPAN_IPHC_HLIM_11;
  1043. break;
  1044. default:
  1045. lowpan_push_hc_data(&hc_ptr, &hdr->hop_limit,
  1046. sizeof(hdr->hop_limit));
  1047. }
  1048. ipv6_saddr_type = ipv6_addr_type(&hdr->saddr);
  1049. /* source address compression */
  1050. if (ipv6_saddr_type == IPV6_ADDR_ANY) {
  1051. pr_debug("source address is unspecified, setting SAC\n");
  1052. iphc1 |= LOWPAN_IPHC_SAC;
  1053. } else {
  1054. if (sci) {
  1055. iphc1 |= lowpan_compress_ctx_addr(&hc_ptr, dev,
  1056. &hdr->saddr,
  1057. &sci_entry, saddr,
  1058. true);
  1059. iphc1 |= LOWPAN_IPHC_SAC;
  1060. } else {
  1061. if (ipv6_saddr_type & IPV6_ADDR_LINKLOCAL &&
  1062. lowpan_is_linklocal_zero_padded(hdr->saddr)) {
  1063. iphc1 |= lowpan_compress_addr_64(&hc_ptr, dev,
  1064. &hdr->saddr,
  1065. saddr, true);
  1066. pr_debug("source address unicast link-local %pI6c iphc1 0x%02x\n",
  1067. &hdr->saddr, iphc1);
  1068. } else {
  1069. pr_debug("send the full source address\n");
  1070. lowpan_push_hc_data(&hc_ptr,
  1071. hdr->saddr.s6_addr, 16);
  1072. }
  1073. }
  1074. }
  1075. /* destination address compression */
  1076. if (ipv6_daddr_type & IPV6_ADDR_MULTICAST) {
  1077. pr_debug("destination address is multicast: ");
  1078. iphc1 |= LOWPAN_IPHC_M;
  1079. if (dci) {
  1080. iphc1 |= lowpan_iphc_mcast_ctx_addr_compress(&hc_ptr,
  1081. &dci_entry,
  1082. &hdr->daddr);
  1083. iphc1 |= LOWPAN_IPHC_DAC;
  1084. } else {
  1085. iphc1 |= lowpan_iphc_mcast_addr_compress(&hc_ptr,
  1086. &hdr->daddr);
  1087. }
  1088. } else {
  1089. if (dci) {
  1090. iphc1 |= lowpan_compress_ctx_addr(&hc_ptr, dev,
  1091. &hdr->daddr,
  1092. &dci_entry, daddr,
  1093. false);
  1094. iphc1 |= LOWPAN_IPHC_DAC;
  1095. } else {
  1096. if (ipv6_daddr_type & IPV6_ADDR_LINKLOCAL &&
  1097. lowpan_is_linklocal_zero_padded(hdr->daddr)) {
  1098. iphc1 |= lowpan_compress_addr_64(&hc_ptr, dev,
  1099. &hdr->daddr,
  1100. daddr, false);
  1101. pr_debug("dest address unicast link-local %pI6c iphc1 0x%02x\n",
  1102. &hdr->daddr, iphc1);
  1103. } else {
  1104. pr_debug("dest address unicast %pI6c\n",
  1105. &hdr->daddr);
  1106. lowpan_push_hc_data(&hc_ptr,
  1107. hdr->daddr.s6_addr, 16);
  1108. }
  1109. }
  1110. }
  1111. /* next header compression */
  1112. if (iphc0 & LOWPAN_IPHC_NH) {
  1113. ret = lowpan_nhc_do_compression(skb, hdr, &hc_ptr);
  1114. if (ret < 0)
  1115. return ret;
  1116. }
  1117. head[0] = iphc0;
  1118. head[1] = iphc1;
  1119. skb_pull(skb, sizeof(struct ipv6hdr));
  1120. skb_reset_transport_header(skb);
  1121. memcpy(skb_push(skb, hc_ptr - head), head, hc_ptr - head);
  1122. skb_reset_network_header(skb);
  1123. pr_debug("header len %d skb %u\n", (int)(hc_ptr - head), skb->len);
  1124. raw_dump_table(__func__, "raw skb data dump compressed",
  1125. skb->data, skb->len);
  1126. return 0;
  1127. }
  1128. EXPORT_SYMBOL_GPL(lowpan_header_compress);