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