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 | 8911d77 | 2015-10-13 13:42:58 +0200 | [diff] [blame^] | 61 | static inline void iphc_uncompress_eui64_lladdr(struct in6_addr *ipaddr, |
| 62 | const void *lladdr) |
| 63 | { |
| 64 | /* fe:80::XXXX:XXXX:XXXX:XXXX |
| 65 | * \_________________/ |
| 66 | * hwaddr |
| 67 | */ |
| 68 | ipaddr->s6_addr[0] = 0xFE; |
| 69 | ipaddr->s6_addr[1] = 0x80; |
| 70 | memcpy(&ipaddr->s6_addr[8], lladdr, EUI64_ADDR_LEN); |
| 71 | /* second bit-flip (Universe/Local) |
| 72 | * is done according RFC2464 |
| 73 | */ |
| 74 | ipaddr->s6_addr[8] ^= 0x02; |
| 75 | } |
| 76 | |
| 77 | static inline void iphc_uncompress_802154_lladdr(struct in6_addr *ipaddr, |
| 78 | const void *lladdr) |
| 79 | { |
| 80 | const struct ieee802154_addr *addr = lladdr; |
| 81 | u8 eui64[EUI64_ADDR_LEN] = { }; |
| 82 | |
| 83 | switch (addr->mode) { |
| 84 | case IEEE802154_ADDR_LONG: |
| 85 | ieee802154_le64_to_be64(eui64, &addr->extended_addr); |
| 86 | iphc_uncompress_eui64_lladdr(ipaddr, eui64); |
| 87 | break; |
| 88 | case IEEE802154_ADDR_SHORT: |
| 89 | /* fe:80::ff:fe00:XXXX |
| 90 | * \__/ |
| 91 | * short_addr |
| 92 | * |
| 93 | * Universe/Local bit is zero. |
| 94 | */ |
| 95 | ipaddr->s6_addr[0] = 0xFE; |
| 96 | ipaddr->s6_addr[1] = 0x80; |
| 97 | ipaddr->s6_addr[11] = 0xFF; |
| 98 | ipaddr->s6_addr[12] = 0xFE; |
| 99 | ieee802154_le16_to_be16(&ipaddr->s6_addr16[7], |
| 100 | &addr->short_addr); |
| 101 | break; |
| 102 | default: |
| 103 | /* should never handled and filtered by 802154 6lowpan */ |
| 104 | WARN_ON_ONCE(1); |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 109 | /* Uncompress address function for source and |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 110 | * destination address(non-multicast). |
| 111 | * |
| 112 | * address_mode is sam value or dam value. |
| 113 | */ |
Alexander Aring | 8911d77 | 2015-10-13 13:42:58 +0200 | [diff] [blame^] | 114 | static int uncompress_addr(struct sk_buff *skb, const struct net_device *dev, |
| 115 | struct in6_addr *ipaddr, u8 address_mode, |
| 116 | const void *lladdr) |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 117 | { |
| 118 | bool fail; |
| 119 | |
| 120 | switch (address_mode) { |
| 121 | case LOWPAN_IPHC_ADDR_00: |
| 122 | /* for global link addresses */ |
| 123 | fail = lowpan_fetch_skb(skb, ipaddr->s6_addr, 16); |
| 124 | break; |
| 125 | case LOWPAN_IPHC_ADDR_01: |
| 126 | /* fe:80::XXXX:XXXX:XXXX:XXXX */ |
| 127 | ipaddr->s6_addr[0] = 0xFE; |
| 128 | ipaddr->s6_addr[1] = 0x80; |
| 129 | fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[8], 8); |
| 130 | break; |
| 131 | case LOWPAN_IPHC_ADDR_02: |
| 132 | /* fe:80::ff:fe00:XXXX */ |
| 133 | ipaddr->s6_addr[0] = 0xFE; |
| 134 | ipaddr->s6_addr[1] = 0x80; |
| 135 | ipaddr->s6_addr[11] = 0xFF; |
| 136 | ipaddr->s6_addr[12] = 0xFE; |
| 137 | fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[14], 2); |
| 138 | break; |
| 139 | case LOWPAN_IPHC_ADDR_03: |
| 140 | fail = false; |
Alexander Aring | 8911d77 | 2015-10-13 13:42:58 +0200 | [diff] [blame^] | 141 | switch (lowpan_priv(dev)->lltype) { |
| 142 | case LOWPAN_LLTYPE_IEEE802154: |
| 143 | iphc_uncompress_802154_lladdr(ipaddr, lladdr); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 144 | break; |
| 145 | default: |
Alexander Aring | 8911d77 | 2015-10-13 13:42:58 +0200 | [diff] [blame^] | 146 | iphc_uncompress_eui64_lladdr(ipaddr, lladdr); |
| 147 | break; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 148 | } |
| 149 | break; |
| 150 | default: |
| 151 | pr_debug("Invalid address mode value: 0x%x\n", address_mode); |
| 152 | return -EINVAL; |
| 153 | } |
| 154 | |
| 155 | if (fail) { |
| 156 | pr_debug("Failed to fetch skb data\n"); |
| 157 | return -EIO; |
| 158 | } |
| 159 | |
| 160 | raw_dump_inline(NULL, "Reconstructed ipv6 addr is", |
| 161 | ipaddr->s6_addr, 16); |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 166 | /* Uncompress address function for source context |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 167 | * based address(non-multicast). |
| 168 | */ |
| 169 | static int uncompress_context_based_src_addr(struct sk_buff *skb, |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 170 | struct in6_addr *ipaddr, |
| 171 | const u8 sam) |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 172 | { |
| 173 | switch (sam) { |
| 174 | case LOWPAN_IPHC_ADDR_00: |
| 175 | /* unspec address :: |
| 176 | * Do nothing, address is already :: |
| 177 | */ |
| 178 | break; |
| 179 | case LOWPAN_IPHC_ADDR_01: |
| 180 | /* TODO */ |
| 181 | case LOWPAN_IPHC_ADDR_02: |
| 182 | /* TODO */ |
| 183 | case LOWPAN_IPHC_ADDR_03: |
| 184 | /* TODO */ |
| 185 | netdev_warn(skb->dev, "SAM value 0x%x not supported\n", sam); |
| 186 | return -EINVAL; |
| 187 | default: |
| 188 | pr_debug("Invalid sam value: 0x%x\n", sam); |
| 189 | return -EINVAL; |
| 190 | } |
| 191 | |
| 192 | raw_dump_inline(NULL, |
| 193 | "Reconstructed context based ipv6 src addr is", |
| 194 | ipaddr->s6_addr, 16); |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 199 | /* Uncompress function for multicast destination address, |
| 200 | * when M bit is set. |
| 201 | */ |
Marcel Holtmann | 7fc4cfd | 2014-07-30 03:48:41 +0200 | [diff] [blame] | 202 | static int lowpan_uncompress_multicast_daddr(struct sk_buff *skb, |
| 203 | struct in6_addr *ipaddr, |
| 204 | const u8 dam) |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 205 | { |
| 206 | bool fail; |
| 207 | |
| 208 | switch (dam) { |
| 209 | case LOWPAN_IPHC_DAM_00: |
| 210 | /* 00: 128 bits. The full address |
| 211 | * is carried in-line. |
| 212 | */ |
| 213 | fail = lowpan_fetch_skb(skb, ipaddr->s6_addr, 16); |
| 214 | break; |
| 215 | case LOWPAN_IPHC_DAM_01: |
| 216 | /* 01: 48 bits. The address takes |
| 217 | * the form ffXX::00XX:XXXX:XXXX. |
| 218 | */ |
| 219 | ipaddr->s6_addr[0] = 0xFF; |
| 220 | fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[1], 1); |
| 221 | fail |= lowpan_fetch_skb(skb, &ipaddr->s6_addr[11], 5); |
| 222 | break; |
| 223 | case LOWPAN_IPHC_DAM_10: |
| 224 | /* 10: 32 bits. The address takes |
| 225 | * the form ffXX::00XX:XXXX. |
| 226 | */ |
| 227 | ipaddr->s6_addr[0] = 0xFF; |
| 228 | fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[1], 1); |
| 229 | fail |= lowpan_fetch_skb(skb, &ipaddr->s6_addr[13], 3); |
| 230 | break; |
| 231 | case LOWPAN_IPHC_DAM_11: |
| 232 | /* 11: 8 bits. The address takes |
| 233 | * the form ff02::00XX. |
| 234 | */ |
| 235 | ipaddr->s6_addr[0] = 0xFF; |
| 236 | ipaddr->s6_addr[1] = 0x02; |
| 237 | fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[15], 1); |
| 238 | break; |
| 239 | default: |
| 240 | pr_debug("DAM value has a wrong value: 0x%x\n", dam); |
| 241 | return -EINVAL; |
| 242 | } |
| 243 | |
| 244 | if (fail) { |
| 245 | pr_debug("Failed to fetch skb data\n"); |
| 246 | return -EIO; |
| 247 | } |
| 248 | |
| 249 | raw_dump_inline(NULL, "Reconstructed ipv6 multicast addr is", |
Marcel Holtmann | 7fc4cfd | 2014-07-30 03:48:41 +0200 | [diff] [blame] | 250 | ipaddr->s6_addr, 16); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 255 | /* TTL uncompression values */ |
| 256 | static const u8 lowpan_ttl_values[] = { 0, 1, 64, 255 }; |
| 257 | |
Alexander Aring | 8911d77 | 2015-10-13 13:42:58 +0200 | [diff] [blame^] | 258 | int lowpan_header_decompress(struct sk_buff *skb, const struct net_device *dev, |
| 259 | const void *daddr, const void *saddr) |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 260 | { |
| 261 | struct ipv6hdr hdr = {}; |
Alexander Aring | 8911d77 | 2015-10-13 13:42:58 +0200 | [diff] [blame^] | 262 | u8 iphc0, iphc1, tmp, num_context = 0; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 263 | int err; |
| 264 | |
| 265 | raw_dump_table(__func__, "raw skb data dump uncompressed", |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 266 | skb->data, skb->len); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 267 | |
Alexander Aring | 8911d77 | 2015-10-13 13:42:58 +0200 | [diff] [blame^] | 268 | if (lowpan_fetch_skb_u8(skb, &iphc0) || |
| 269 | lowpan_fetch_skb_u8(skb, &iphc1)) |
| 270 | return -EINVAL; |
| 271 | |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 272 | /* another if the CID flag is set */ |
| 273 | if (iphc1 & LOWPAN_IPHC_CID) { |
| 274 | pr_debug("CID flag is set, increase header with one\n"); |
Alexander Aring | 4ebc960 | 2014-07-29 23:47:00 +0200 | [diff] [blame] | 275 | if (lowpan_fetch_skb(skb, &num_context, sizeof(num_context))) |
Martin Townsend | 56b2c3e | 2014-11-06 19:15:13 +0000 | [diff] [blame] | 276 | return -EINVAL; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | hdr.version = 6; |
| 280 | |
| 281 | /* Traffic Class and Flow Label */ |
| 282 | switch ((iphc0 & LOWPAN_IPHC_TF) >> 3) { |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 283 | /* Traffic Class and FLow Label carried in-line |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 284 | * ECN + DSCP + 4-bit Pad + Flow Label (4 bytes) |
| 285 | */ |
| 286 | case 0: /* 00b */ |
Alexander Aring | 4ebc960 | 2014-07-29 23:47:00 +0200 | [diff] [blame] | 287 | if (lowpan_fetch_skb(skb, &tmp, sizeof(tmp))) |
Martin Townsend | 56b2c3e | 2014-11-06 19:15:13 +0000 | [diff] [blame] | 288 | return -EINVAL; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 289 | |
| 290 | memcpy(&hdr.flow_lbl, &skb->data[0], 3); |
| 291 | skb_pull(skb, 3); |
| 292 | hdr.priority = ((tmp >> 2) & 0x0f); |
| 293 | hdr.flow_lbl[0] = ((tmp >> 2) & 0x30) | (tmp << 6) | |
| 294 | (hdr.flow_lbl[0] & 0x0f); |
| 295 | break; |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 296 | /* Traffic class carried in-line |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 297 | * ECN + DSCP (1 byte), Flow Label is elided |
| 298 | */ |
| 299 | case 2: /* 10b */ |
Alexander Aring | 4ebc960 | 2014-07-29 23:47:00 +0200 | [diff] [blame] | 300 | if (lowpan_fetch_skb(skb, &tmp, sizeof(tmp))) |
Martin Townsend | 56b2c3e | 2014-11-06 19:15:13 +0000 | [diff] [blame] | 301 | return -EINVAL; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 302 | |
| 303 | hdr.priority = ((tmp >> 2) & 0x0f); |
| 304 | hdr.flow_lbl[0] = ((tmp << 6) & 0xC0) | ((tmp >> 2) & 0x30); |
| 305 | break; |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 306 | /* Flow Label carried in-line |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 307 | * ECN + 2-bit Pad + Flow Label (3 bytes), DSCP is elided |
| 308 | */ |
| 309 | case 1: /* 01b */ |
Alexander Aring | 4ebc960 | 2014-07-29 23:47:00 +0200 | [diff] [blame] | 310 | if (lowpan_fetch_skb(skb, &tmp, sizeof(tmp))) |
Martin Townsend | 56b2c3e | 2014-11-06 19:15:13 +0000 | [diff] [blame] | 311 | return -EINVAL; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 312 | |
Lukasz Duda | 77e867b | 2015-08-10 21:15:52 +0200 | [diff] [blame] | 313 | hdr.flow_lbl[0] = (tmp & 0x0F) | ((tmp >> 2) & 0x30); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 314 | memcpy(&hdr.flow_lbl[1], &skb->data[0], 2); |
| 315 | skb_pull(skb, 2); |
| 316 | break; |
| 317 | /* Traffic Class and Flow Label are elided */ |
| 318 | case 3: /* 11b */ |
| 319 | break; |
| 320 | default: |
| 321 | break; |
| 322 | } |
| 323 | |
| 324 | /* Next Header */ |
| 325 | if ((iphc0 & LOWPAN_IPHC_NH_C) == 0) { |
| 326 | /* Next header is carried inline */ |
Alexander Aring | 4ebc960 | 2014-07-29 23:47:00 +0200 | [diff] [blame] | 327 | if (lowpan_fetch_skb(skb, &hdr.nexthdr, sizeof(hdr.nexthdr))) |
Martin Townsend | 56b2c3e | 2014-11-06 19:15:13 +0000 | [diff] [blame] | 328 | return -EINVAL; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 329 | |
| 330 | pr_debug("NH flag is set, next header carried inline: %02x\n", |
| 331 | hdr.nexthdr); |
| 332 | } |
| 333 | |
| 334 | /* Hop Limit */ |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 335 | if ((iphc0 & 0x03) != LOWPAN_IPHC_TTL_I) { |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 336 | hdr.hop_limit = lowpan_ttl_values[iphc0 & 0x03]; |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 337 | } else { |
Alexander Aring | 4ebc960 | 2014-07-29 23:47:00 +0200 | [diff] [blame] | 338 | if (lowpan_fetch_skb(skb, &hdr.hop_limit, |
| 339 | sizeof(hdr.hop_limit))) |
Martin Townsend | 56b2c3e | 2014-11-06 19:15:13 +0000 | [diff] [blame] | 340 | return -EINVAL; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /* Extract SAM to the tmp variable */ |
| 344 | tmp = ((iphc1 & LOWPAN_IPHC_SAM) >> LOWPAN_IPHC_SAM_BIT) & 0x03; |
| 345 | |
| 346 | if (iphc1 & LOWPAN_IPHC_SAC) { |
| 347 | /* Source address context based uncompression */ |
| 348 | pr_debug("SAC bit is set. Handle context based source address.\n"); |
Marcel Holtmann | 7fc4cfd | 2014-07-30 03:48:41 +0200 | [diff] [blame] | 349 | err = uncompress_context_based_src_addr(skb, &hdr.saddr, tmp); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 350 | } else { |
| 351 | /* Source address uncompression */ |
| 352 | pr_debug("source address stateless compression\n"); |
Alexander Aring | 8911d77 | 2015-10-13 13:42:58 +0200 | [diff] [blame^] | 353 | err = uncompress_addr(skb, dev, &hdr.saddr, tmp, saddr); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /* Check on error of previous branch */ |
| 357 | if (err) |
Martin Townsend | 56b2c3e | 2014-11-06 19:15:13 +0000 | [diff] [blame] | 358 | return -EINVAL; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 359 | |
| 360 | /* Extract DAM to the tmp variable */ |
| 361 | tmp = ((iphc1 & LOWPAN_IPHC_DAM_11) >> LOWPAN_IPHC_DAM_BIT) & 0x03; |
| 362 | |
| 363 | /* check for Multicast Compression */ |
| 364 | if (iphc1 & LOWPAN_IPHC_M) { |
| 365 | if (iphc1 & LOWPAN_IPHC_DAC) { |
| 366 | pr_debug("dest: context-based mcast compression\n"); |
| 367 | /* TODO: implement this */ |
| 368 | } else { |
Marcel Holtmann | 7fc4cfd | 2014-07-30 03:48:41 +0200 | [diff] [blame] | 369 | err = lowpan_uncompress_multicast_daddr(skb, &hdr.daddr, |
| 370 | tmp); |
| 371 | |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 372 | if (err) |
Martin Townsend | 56b2c3e | 2014-11-06 19:15:13 +0000 | [diff] [blame] | 373 | return -EINVAL; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 374 | } |
| 375 | } else { |
Alexander Aring | 8911d77 | 2015-10-13 13:42:58 +0200 | [diff] [blame^] | 376 | err = uncompress_addr(skb, dev, &hdr.daddr, tmp, daddr); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 377 | pr_debug("dest: stateless compression mode %d dest %pI6c\n", |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 378 | tmp, &hdr.daddr); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 379 | if (err) |
Martin Townsend | 56b2c3e | 2014-11-06 19:15:13 +0000 | [diff] [blame] | 380 | return -EINVAL; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 381 | } |
| 382 | |
Alexander Aring | cc6ed26 | 2015-01-09 16:42:58 +0100 | [diff] [blame] | 383 | /* Next header data uncompression */ |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 384 | if (iphc0 & LOWPAN_IPHC_NH_C) { |
Alexander Aring | cc6ed26 | 2015-01-09 16:42:58 +0100 | [diff] [blame] | 385 | err = lowpan_nhc_do_uncompression(skb, dev, &hdr); |
| 386 | if (err < 0) |
Martin Townsend | 11e3ff70 | 2014-10-13 11:00:56 +0100 | [diff] [blame] | 387 | return err; |
Martin Townsend | 11e3ff70 | 2014-10-13 11:00:56 +0100 | [diff] [blame] | 388 | } else { |
| 389 | err = skb_cow(skb, sizeof(hdr)); |
Martin Townsend | 56b2c3e | 2014-11-06 19:15:13 +0000 | [diff] [blame] | 390 | if (unlikely(err)) |
Martin Townsend | 11e3ff70 | 2014-10-13 11:00:56 +0100 | [diff] [blame] | 391 | return err; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 392 | } |
| 393 | |
Alexander Aring | 72a5e6b | 2015-09-02 14:21:25 +0200 | [diff] [blame] | 394 | switch (lowpan_priv(dev)->lltype) { |
| 395 | case LOWPAN_LLTYPE_IEEE802154: |
| 396 | if (lowpan_802154_cb(skb)->d_size) |
| 397 | hdr.payload_len = htons(lowpan_802154_cb(skb)->d_size - |
| 398 | sizeof(struct ipv6hdr)); |
| 399 | else |
| 400 | hdr.payload_len = htons(skb->len); |
| 401 | break; |
| 402 | default: |
| 403 | hdr.payload_len = htons(skb->len); |
| 404 | break; |
| 405 | } |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 406 | |
| 407 | pr_debug("skb headroom size = %d, data length = %d\n", |
| 408 | skb_headroom(skb), skb->len); |
| 409 | |
| 410 | pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength = %d\n\t" |
| 411 | "nexthdr = 0x%02x\n\thop_lim = %d\n\tdest = %pI6c\n", |
| 412 | hdr.version, ntohs(hdr.payload_len), hdr.nexthdr, |
| 413 | hdr.hop_limit, &hdr.daddr); |
| 414 | |
Martin Townsend | f8b3617 | 2014-10-23 15:40:53 +0100 | [diff] [blame] | 415 | skb_push(skb, sizeof(hdr)); |
| 416 | skb_reset_network_header(skb); |
| 417 | skb_copy_to_linear_data(skb, &hdr, sizeof(hdr)); |
| 418 | |
Marcel Holtmann | 7fc4cfd | 2014-07-30 03:48:41 +0200 | [diff] [blame] | 419 | raw_dump_table(__func__, "raw header dump", (u8 *)&hdr, sizeof(hdr)); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 420 | |
Martin Townsend | f8b3617 | 2014-10-23 15:40:53 +0100 | [diff] [blame] | 421 | return 0; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 422 | } |
Martin Townsend | 0114123 | 2014-10-23 15:40:56 +0100 | [diff] [blame] | 423 | EXPORT_SYMBOL_GPL(lowpan_header_decompress); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 424 | |
Alexander Aring | 84ca5e0 | 2014-07-29 23:46:58 +0200 | [diff] [blame] | 425 | static u8 lowpan_compress_addr_64(u8 **hc_ptr, u8 shift, |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 426 | const struct in6_addr *ipaddr, |
| 427 | const unsigned char *lladdr) |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 428 | { |
| 429 | u8 val = 0; |
| 430 | |
| 431 | if (is_addr_mac_addr_based(ipaddr, lladdr)) { |
| 432 | val = 3; /* 0-bits */ |
| 433 | pr_debug("address compression 0 bits\n"); |
| 434 | } else if (lowpan_is_iid_16_bit_compressable(ipaddr)) { |
| 435 | /* compress IID to 16 bits xxxx::XXXX */ |
Alexander Aring | 85c7124 | 2014-07-29 23:47:01 +0200 | [diff] [blame] | 436 | lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr16[7], 2); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 437 | val = 2; /* 16-bits */ |
| 438 | raw_dump_inline(NULL, "Compressed ipv6 addr is (16 bits)", |
Alexander Aring | 84ca5e0 | 2014-07-29 23:46:58 +0200 | [diff] [blame] | 439 | *hc_ptr - 2, 2); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 440 | } else { |
| 441 | /* do not compress IID => xxxx::IID */ |
Alexander Aring | 85c7124 | 2014-07-29 23:47:01 +0200 | [diff] [blame] | 442 | lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr16[4], 8); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 443 | val = 1; /* 64-bits */ |
| 444 | raw_dump_inline(NULL, "Compressed ipv6 addr is (64 bits)", |
Alexander Aring | 84ca5e0 | 2014-07-29 23:46:58 +0200 | [diff] [blame] | 445 | *hc_ptr - 8, 8); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | return rol8(val, shift); |
| 449 | } |
| 450 | |
Alexander Aring | a6f7738 | 2015-10-13 13:42:57 +0200 | [diff] [blame] | 451 | int lowpan_header_compress(struct sk_buff *skb, const struct net_device *dev, |
| 452 | const void *daddr, const void *saddr) |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 453 | { |
Alexander Aring | 84ca5e0 | 2014-07-29 23:46:58 +0200 | [diff] [blame] | 454 | u8 tmp, iphc0, iphc1, *hc_ptr; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 455 | struct ipv6hdr *hdr; |
Alexander Aring | bf513fd | 2015-10-13 13:42:56 +0200 | [diff] [blame] | 456 | u8 head[LOWPAN_IPHC_MAX_HC_BUF_LEN] = {}; |
Alexander Aring | cc6ed26 | 2015-01-09 16:42:58 +0100 | [diff] [blame] | 457 | int ret, addr_type; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 458 | |
Alexander Aring | a6f7738 | 2015-10-13 13:42:57 +0200 | [diff] [blame] | 459 | if (skb->protocol != htons(ETH_P_IPV6)) |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 460 | return -EINVAL; |
| 461 | |
| 462 | hdr = ipv6_hdr(skb); |
Alexander Aring | 84ca5e0 | 2014-07-29 23:46:58 +0200 | [diff] [blame] | 463 | hc_ptr = head + 2; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 464 | |
| 465 | pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength = %d\n" |
| 466 | "\tnexthdr = 0x%02x\n\thop_lim = %d\n\tdest = %pI6c\n", |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 467 | hdr->version, ntohs(hdr->payload_len), hdr->nexthdr, |
| 468 | hdr->hop_limit, &hdr->daddr); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 469 | |
| 470 | raw_dump_table(__func__, "raw skb network header dump", |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 471 | skb_network_header(skb), sizeof(struct ipv6hdr)); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 472 | |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 473 | /* As we copy some bit-length fields, in the IPHC encoding bytes, |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 474 | * we sometimes use |= |
| 475 | * If the field is 0, and the current bit value in memory is 1, |
| 476 | * this does not work. We therefore reset the IPHC encoding here |
| 477 | */ |
| 478 | iphc0 = LOWPAN_DISPATCH_IPHC; |
| 479 | iphc1 = 0; |
| 480 | |
| 481 | /* TODO: context lookup */ |
| 482 | |
Alexander Aring | a6f7738 | 2015-10-13 13:42:57 +0200 | [diff] [blame] | 483 | raw_dump_inline(__func__, "saddr", saddr, EUI64_ADDR_LEN); |
| 484 | raw_dump_inline(__func__, "daddr", daddr, EUI64_ADDR_LEN); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 485 | |
Marcel Holtmann | 7fc4cfd | 2014-07-30 03:48:41 +0200 | [diff] [blame] | 486 | raw_dump_table(__func__, "sending raw skb network uncompressed packet", |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 487 | skb->data, skb->len); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 488 | |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 489 | /* Traffic class, flow label |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 490 | * If flow label is 0, compress it. If traffic class is 0, compress it |
| 491 | * We have to process both in the same time as the offset of traffic |
| 492 | * class depends on the presence of version and flow label |
| 493 | */ |
| 494 | |
Alexander Aring | 84ca5e0 | 2014-07-29 23:46:58 +0200 | [diff] [blame] | 495 | /* hc format of TC is ECN | DSCP , original one is DSCP | ECN */ |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 496 | tmp = (hdr->priority << 4) | (hdr->flow_lbl[0] >> 4); |
| 497 | tmp = ((tmp & 0x03) << 6) | (tmp >> 2); |
| 498 | |
| 499 | if (((hdr->flow_lbl[0] & 0x0F) == 0) && |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 500 | (hdr->flow_lbl[1] == 0) && (hdr->flow_lbl[2] == 0)) { |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 501 | /* flow label can be compressed */ |
| 502 | iphc0 |= LOWPAN_IPHC_FL_C; |
| 503 | if ((hdr->priority == 0) && |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 504 | ((hdr->flow_lbl[0] & 0xF0) == 0)) { |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 505 | /* compress (elide) all */ |
| 506 | iphc0 |= LOWPAN_IPHC_TC_C; |
| 507 | } else { |
| 508 | /* compress only the flow label */ |
Alexander Aring | 84ca5e0 | 2014-07-29 23:46:58 +0200 | [diff] [blame] | 509 | *hc_ptr = tmp; |
| 510 | hc_ptr += 1; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 511 | } |
| 512 | } else { |
| 513 | /* Flow label cannot be compressed */ |
| 514 | if ((hdr->priority == 0) && |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 515 | ((hdr->flow_lbl[0] & 0xF0) == 0)) { |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 516 | /* compress only traffic class */ |
| 517 | iphc0 |= LOWPAN_IPHC_TC_C; |
Alexander Aring | 84ca5e0 | 2014-07-29 23:46:58 +0200 | [diff] [blame] | 518 | *hc_ptr = (tmp & 0xc0) | (hdr->flow_lbl[0] & 0x0F); |
| 519 | memcpy(hc_ptr + 1, &hdr->flow_lbl[1], 2); |
| 520 | hc_ptr += 3; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 521 | } else { |
| 522 | /* compress nothing */ |
Alexander Aring | 84ca5e0 | 2014-07-29 23:46:58 +0200 | [diff] [blame] | 523 | memcpy(hc_ptr, hdr, 4); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 524 | /* replace the top byte with new ECN | DSCP format */ |
Alexander Aring | 84ca5e0 | 2014-07-29 23:46:58 +0200 | [diff] [blame] | 525 | *hc_ptr = tmp; |
| 526 | hc_ptr += 4; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | |
| 530 | /* NOTE: payload length is always compressed */ |
| 531 | |
Alexander Aring | cc6ed26 | 2015-01-09 16:42:58 +0100 | [diff] [blame] | 532 | /* Check if we provide the nhc format for nexthdr and compression |
| 533 | * functionality. If not nexthdr is handled inline and not compressed. |
| 534 | */ |
| 535 | ret = lowpan_nhc_check_compression(skb, hdr, &hc_ptr, &iphc0); |
| 536 | if (ret < 0) |
| 537 | return ret; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 538 | |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 539 | /* Hop limit |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 540 | * if 1: compress, encoding is 01 |
| 541 | * if 64: compress, encoding is 10 |
| 542 | * if 255: compress, encoding is 11 |
| 543 | * else do not compress |
| 544 | */ |
| 545 | switch (hdr->hop_limit) { |
| 546 | case 1: |
| 547 | iphc0 |= LOWPAN_IPHC_TTL_1; |
| 548 | break; |
| 549 | case 64: |
| 550 | iphc0 |= LOWPAN_IPHC_TTL_64; |
| 551 | break; |
| 552 | case 255: |
| 553 | iphc0 |= LOWPAN_IPHC_TTL_255; |
| 554 | break; |
| 555 | default: |
Alexander Aring | 85c7124 | 2014-07-29 23:47:01 +0200 | [diff] [blame] | 556 | lowpan_push_hc_data(&hc_ptr, &hdr->hop_limit, |
| 557 | sizeof(hdr->hop_limit)); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 558 | } |
| 559 | |
Alexander Aring | 556a5bf | 2014-07-29 23:47:02 +0200 | [diff] [blame] | 560 | addr_type = ipv6_addr_type(&hdr->saddr); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 561 | /* source address compression */ |
Alexander Aring | 556a5bf | 2014-07-29 23:47:02 +0200 | [diff] [blame] | 562 | if (addr_type == IPV6_ADDR_ANY) { |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 563 | pr_debug("source address is unspecified, setting SAC\n"); |
| 564 | iphc1 |= LOWPAN_IPHC_SAC; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 565 | } else { |
Alexander Aring | 556a5bf | 2014-07-29 23:47:02 +0200 | [diff] [blame] | 566 | if (addr_type & IPV6_ADDR_LINKLOCAL) { |
| 567 | iphc1 |= lowpan_compress_addr_64(&hc_ptr, |
| 568 | LOWPAN_IPHC_SAM_BIT, |
Alexander Aring | a6f7738 | 2015-10-13 13:42:57 +0200 | [diff] [blame] | 569 | &hdr->saddr, saddr); |
Alexander Aring | 556a5bf | 2014-07-29 23:47:02 +0200 | [diff] [blame] | 570 | pr_debug("source address unicast link-local %pI6c iphc1 0x%02x\n", |
| 571 | &hdr->saddr, iphc1); |
| 572 | } else { |
| 573 | pr_debug("send the full source address\n"); |
| 574 | lowpan_push_hc_data(&hc_ptr, hdr->saddr.s6_addr, 16); |
| 575 | } |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 576 | } |
| 577 | |
Alexander Aring | 556a5bf | 2014-07-29 23:47:02 +0200 | [diff] [blame] | 578 | addr_type = ipv6_addr_type(&hdr->daddr); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 579 | /* destination address compression */ |
Alexander Aring | 556a5bf | 2014-07-29 23:47:02 +0200 | [diff] [blame] | 580 | if (addr_type & IPV6_ADDR_MULTICAST) { |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 581 | pr_debug("destination address is multicast: "); |
| 582 | iphc1 |= LOWPAN_IPHC_M; |
| 583 | if (lowpan_is_mcast_addr_compressable8(&hdr->daddr)) { |
| 584 | pr_debug("compressed to 1 octet\n"); |
| 585 | iphc1 |= LOWPAN_IPHC_DAM_11; |
| 586 | /* use last byte */ |
Alexander Aring | 85c7124 | 2014-07-29 23:47:01 +0200 | [diff] [blame] | 587 | lowpan_push_hc_data(&hc_ptr, |
| 588 | &hdr->daddr.s6_addr[15], 1); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 589 | } else if (lowpan_is_mcast_addr_compressable32(&hdr->daddr)) { |
| 590 | pr_debug("compressed to 4 octets\n"); |
| 591 | iphc1 |= LOWPAN_IPHC_DAM_10; |
| 592 | /* second byte + the last three */ |
Alexander Aring | 85c7124 | 2014-07-29 23:47:01 +0200 | [diff] [blame] | 593 | lowpan_push_hc_data(&hc_ptr, |
| 594 | &hdr->daddr.s6_addr[1], 1); |
| 595 | lowpan_push_hc_data(&hc_ptr, |
| 596 | &hdr->daddr.s6_addr[13], 3); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 597 | } else if (lowpan_is_mcast_addr_compressable48(&hdr->daddr)) { |
| 598 | pr_debug("compressed to 6 octets\n"); |
| 599 | iphc1 |= LOWPAN_IPHC_DAM_01; |
| 600 | /* second byte + the last five */ |
Alexander Aring | 85c7124 | 2014-07-29 23:47:01 +0200 | [diff] [blame] | 601 | lowpan_push_hc_data(&hc_ptr, |
| 602 | &hdr->daddr.s6_addr[1], 1); |
| 603 | lowpan_push_hc_data(&hc_ptr, |
| 604 | &hdr->daddr.s6_addr[11], 5); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 605 | } else { |
| 606 | pr_debug("using full address\n"); |
| 607 | iphc1 |= LOWPAN_IPHC_DAM_00; |
Alexander Aring | 85c7124 | 2014-07-29 23:47:01 +0200 | [diff] [blame] | 608 | lowpan_push_hc_data(&hc_ptr, hdr->daddr.s6_addr, 16); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 609 | } |
| 610 | } else { |
Alexander Aring | 556a5bf | 2014-07-29 23:47:02 +0200 | [diff] [blame] | 611 | if (addr_type & IPV6_ADDR_LINKLOCAL) { |
| 612 | /* TODO: context lookup */ |
Alexander Aring | 84ca5e0 | 2014-07-29 23:46:58 +0200 | [diff] [blame] | 613 | iphc1 |= lowpan_compress_addr_64(&hc_ptr, |
Alexander Aring | a6f7738 | 2015-10-13 13:42:57 +0200 | [diff] [blame] | 614 | LOWPAN_IPHC_DAM_BIT, &hdr->daddr, daddr); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 615 | pr_debug("dest address unicast link-local %pI6c " |
Marcel Holtmann | 7fc4cfd | 2014-07-30 03:48:41 +0200 | [diff] [blame] | 616 | "iphc1 0x%02x\n", &hdr->daddr, iphc1); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 617 | } else { |
| 618 | pr_debug("dest address unicast %pI6c\n", &hdr->daddr); |
Alexander Aring | 85c7124 | 2014-07-29 23:47:01 +0200 | [diff] [blame] | 619 | lowpan_push_hc_data(&hc_ptr, hdr->daddr.s6_addr, 16); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | |
Alexander Aring | cc6ed26 | 2015-01-09 16:42:58 +0100 | [diff] [blame] | 623 | /* next header compression */ |
| 624 | if (iphc0 & LOWPAN_IPHC_NH_C) { |
| 625 | ret = lowpan_nhc_do_compression(skb, hdr, &hc_ptr); |
| 626 | if (ret < 0) |
| 627 | return ret; |
| 628 | } |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 629 | |
| 630 | head[0] = iphc0; |
| 631 | head[1] = iphc1; |
| 632 | |
| 633 | skb_pull(skb, sizeof(struct ipv6hdr)); |
| 634 | skb_reset_transport_header(skb); |
Alexander Aring | 84ca5e0 | 2014-07-29 23:46:58 +0200 | [diff] [blame] | 635 | memcpy(skb_push(skb, hc_ptr - head), head, hc_ptr - head); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 636 | skb_reset_network_header(skb); |
| 637 | |
Alexander Aring | 84ca5e0 | 2014-07-29 23:46:58 +0200 | [diff] [blame] | 638 | 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] | 639 | |
| 640 | raw_dump_table(__func__, "raw skb data dump compressed", |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 641 | skb->data, skb->len); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 642 | return 0; |
| 643 | } |
| 644 | EXPORT_SYMBOL_GPL(lowpan_header_compress); |