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 | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 61 | /* Values of fields within the IPHC encoding first byte */ |
| 62 | #define LOWPAN_IPHC_TF_MASK 0x18 |
| 63 | #define LOWPAN_IPHC_TF_00 0x00 |
| 64 | #define LOWPAN_IPHC_TF_01 0x08 |
| 65 | #define LOWPAN_IPHC_TF_10 0x10 |
| 66 | #define LOWPAN_IPHC_TF_11 0x18 |
Alexander Aring | 6350047 | 2015-10-20 08:31:22 +0200 | [diff] [blame] | 67 | |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 68 | #define LOWPAN_IPHC_NH 0x04 |
| 69 | |
| 70 | #define LOWPAN_IPHC_HLIM_MASK 0x03 |
| 71 | #define LOWPAN_IPHC_HLIM_00 0x00 |
| 72 | #define LOWPAN_IPHC_HLIM_01 0x01 |
| 73 | #define LOWPAN_IPHC_HLIM_10 0x02 |
| 74 | #define LOWPAN_IPHC_HLIM_11 0x03 |
Alexander Aring | 6350047 | 2015-10-20 08:31:22 +0200 | [diff] [blame] | 75 | |
| 76 | /* Values of fields within the IPHC encoding second byte */ |
| 77 | #define LOWPAN_IPHC_CID 0x80 |
| 78 | |
Alexander Aring | 6350047 | 2015-10-20 08:31:22 +0200 | [diff] [blame] | 79 | #define LOWPAN_IPHC_SAC 0x40 |
Alexander Aring | 6350047 | 2015-10-20 08:31:22 +0200 | [diff] [blame] | 80 | |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 81 | #define LOWPAN_IPHC_SAM_MASK 0x30 |
| 82 | #define LOWPAN_IPHC_SAM_00 0x00 |
| 83 | #define LOWPAN_IPHC_SAM_01 0x10 |
| 84 | #define LOWPAN_IPHC_SAM_10 0x20 |
| 85 | #define LOWPAN_IPHC_SAM_11 0x30 |
Alexander Aring | 6350047 | 2015-10-20 08:31:22 +0200 | [diff] [blame] | 86 | |
| 87 | #define LOWPAN_IPHC_M 0x08 |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 88 | |
Alexander Aring | 6350047 | 2015-10-20 08:31:22 +0200 | [diff] [blame] | 89 | #define LOWPAN_IPHC_DAC 0x04 |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 90 | |
| 91 | #define LOWPAN_IPHC_DAM_MASK 0x03 |
Alexander Aring | 6350047 | 2015-10-20 08:31:22 +0200 | [diff] [blame] | 92 | #define LOWPAN_IPHC_DAM_00 0x00 |
| 93 | #define LOWPAN_IPHC_DAM_01 0x01 |
| 94 | #define LOWPAN_IPHC_DAM_10 0x02 |
| 95 | #define LOWPAN_IPHC_DAM_11 0x03 |
| 96 | |
Alexander Aring | 6350047 | 2015-10-20 08:31:22 +0200 | [diff] [blame] | 97 | /* ipv6 address based on mac |
| 98 | * second bit-flip (Universe/Local) is done according RFC2464 |
| 99 | */ |
| 100 | #define is_addr_mac_addr_based(a, m) \ |
| 101 | ((((a)->s6_addr[8]) == (((m)[0]) ^ 0x02)) && \ |
| 102 | (((a)->s6_addr[9]) == (m)[1]) && \ |
| 103 | (((a)->s6_addr[10]) == (m)[2]) && \ |
| 104 | (((a)->s6_addr[11]) == (m)[3]) && \ |
| 105 | (((a)->s6_addr[12]) == (m)[4]) && \ |
| 106 | (((a)->s6_addr[13]) == (m)[5]) && \ |
| 107 | (((a)->s6_addr[14]) == (m)[6]) && \ |
| 108 | (((a)->s6_addr[15]) == (m)[7])) |
| 109 | |
| 110 | /* check whether we can compress the IID to 16 bits, |
| 111 | * it's possible for unicast addresses with first 49 bits are zero only. |
| 112 | */ |
| 113 | #define lowpan_is_iid_16_bit_compressable(a) \ |
| 114 | ((((a)->s6_addr16[4]) == 0) && \ |
| 115 | (((a)->s6_addr[10]) == 0) && \ |
| 116 | (((a)->s6_addr[11]) == 0xff) && \ |
| 117 | (((a)->s6_addr[12]) == 0xfe) && \ |
| 118 | (((a)->s6_addr[13]) == 0)) |
| 119 | |
| 120 | /* check whether the 112-bit gid of the multicast address is mappable to: */ |
| 121 | |
| 122 | /* 48 bits, FFXX::00XX:XXXX:XXXX */ |
| 123 | #define lowpan_is_mcast_addr_compressable48(a) \ |
| 124 | ((((a)->s6_addr16[1]) == 0) && \ |
| 125 | (((a)->s6_addr16[2]) == 0) && \ |
| 126 | (((a)->s6_addr16[3]) == 0) && \ |
| 127 | (((a)->s6_addr16[4]) == 0) && \ |
| 128 | (((a)->s6_addr[10]) == 0)) |
| 129 | |
| 130 | /* 32 bits, FFXX::00XX:XXXX */ |
| 131 | #define lowpan_is_mcast_addr_compressable32(a) \ |
| 132 | ((((a)->s6_addr16[1]) == 0) && \ |
| 133 | (((a)->s6_addr16[2]) == 0) && \ |
| 134 | (((a)->s6_addr16[3]) == 0) && \ |
| 135 | (((a)->s6_addr16[4]) == 0) && \ |
| 136 | (((a)->s6_addr16[5]) == 0) && \ |
| 137 | (((a)->s6_addr[12]) == 0)) |
| 138 | |
| 139 | /* 8 bits, FF02::00XX */ |
| 140 | #define lowpan_is_mcast_addr_compressable8(a) \ |
| 141 | ((((a)->s6_addr[1]) == 2) && \ |
| 142 | (((a)->s6_addr16[1]) == 0) && \ |
| 143 | (((a)->s6_addr16[2]) == 0) && \ |
| 144 | (((a)->s6_addr16[3]) == 0) && \ |
| 145 | (((a)->s6_addr16[4]) == 0) && \ |
| 146 | (((a)->s6_addr16[5]) == 0) && \ |
| 147 | (((a)->s6_addr16[6]) == 0) && \ |
| 148 | (((a)->s6_addr[14]) == 0)) |
| 149 | |
Alexander Aring | 8911d77 | 2015-10-13 13:42:58 +0200 | [diff] [blame] | 150 | static inline void iphc_uncompress_eui64_lladdr(struct in6_addr *ipaddr, |
| 151 | const void *lladdr) |
| 152 | { |
| 153 | /* fe:80::XXXX:XXXX:XXXX:XXXX |
| 154 | * \_________________/ |
| 155 | * hwaddr |
| 156 | */ |
| 157 | ipaddr->s6_addr[0] = 0xFE; |
| 158 | ipaddr->s6_addr[1] = 0x80; |
| 159 | memcpy(&ipaddr->s6_addr[8], lladdr, EUI64_ADDR_LEN); |
| 160 | /* second bit-flip (Universe/Local) |
| 161 | * is done according RFC2464 |
| 162 | */ |
| 163 | ipaddr->s6_addr[8] ^= 0x02; |
| 164 | } |
| 165 | |
| 166 | static inline void iphc_uncompress_802154_lladdr(struct in6_addr *ipaddr, |
| 167 | const void *lladdr) |
| 168 | { |
| 169 | const struct ieee802154_addr *addr = lladdr; |
| 170 | u8 eui64[EUI64_ADDR_LEN] = { }; |
| 171 | |
| 172 | switch (addr->mode) { |
| 173 | case IEEE802154_ADDR_LONG: |
| 174 | ieee802154_le64_to_be64(eui64, &addr->extended_addr); |
| 175 | iphc_uncompress_eui64_lladdr(ipaddr, eui64); |
| 176 | break; |
| 177 | case IEEE802154_ADDR_SHORT: |
| 178 | /* fe:80::ff:fe00:XXXX |
| 179 | * \__/ |
| 180 | * short_addr |
| 181 | * |
| 182 | * Universe/Local bit is zero. |
| 183 | */ |
| 184 | ipaddr->s6_addr[0] = 0xFE; |
| 185 | ipaddr->s6_addr[1] = 0x80; |
| 186 | ipaddr->s6_addr[11] = 0xFF; |
| 187 | ipaddr->s6_addr[12] = 0xFE; |
| 188 | ieee802154_le16_to_be16(&ipaddr->s6_addr16[7], |
| 189 | &addr->short_addr); |
| 190 | break; |
| 191 | default: |
| 192 | /* should never handled and filtered by 802154 6lowpan */ |
| 193 | WARN_ON_ONCE(1); |
| 194 | break; |
| 195 | } |
| 196 | } |
| 197 | |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 198 | /* Uncompress address function for source and |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 199 | * destination address(non-multicast). |
| 200 | * |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 201 | * address_mode is the masked value for sam or dam value |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 202 | */ |
Alexander Aring | 8911d77 | 2015-10-13 13:42:58 +0200 | [diff] [blame] | 203 | static int uncompress_addr(struct sk_buff *skb, const struct net_device *dev, |
| 204 | struct in6_addr *ipaddr, u8 address_mode, |
| 205 | const void *lladdr) |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 206 | { |
| 207 | bool fail; |
| 208 | |
| 209 | switch (address_mode) { |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 210 | /* SAM and DAM are the same here */ |
| 211 | case LOWPAN_IPHC_DAM_00: |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 212 | /* for global link addresses */ |
| 213 | fail = lowpan_fetch_skb(skb, ipaddr->s6_addr, 16); |
| 214 | break; |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 215 | case LOWPAN_IPHC_SAM_01: |
| 216 | case LOWPAN_IPHC_DAM_01: |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 217 | /* fe:80::XXXX:XXXX:XXXX:XXXX */ |
| 218 | ipaddr->s6_addr[0] = 0xFE; |
| 219 | ipaddr->s6_addr[1] = 0x80; |
| 220 | fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[8], 8); |
| 221 | break; |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 222 | case LOWPAN_IPHC_SAM_10: |
| 223 | case LOWPAN_IPHC_DAM_10: |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 224 | /* fe:80::ff:fe00:XXXX */ |
| 225 | ipaddr->s6_addr[0] = 0xFE; |
| 226 | ipaddr->s6_addr[1] = 0x80; |
| 227 | ipaddr->s6_addr[11] = 0xFF; |
| 228 | ipaddr->s6_addr[12] = 0xFE; |
| 229 | fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[14], 2); |
| 230 | break; |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 231 | case LOWPAN_IPHC_SAM_11: |
| 232 | case LOWPAN_IPHC_DAM_11: |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 233 | fail = false; |
Alexander Aring | 8911d77 | 2015-10-13 13:42:58 +0200 | [diff] [blame] | 234 | switch (lowpan_priv(dev)->lltype) { |
| 235 | case LOWPAN_LLTYPE_IEEE802154: |
| 236 | iphc_uncompress_802154_lladdr(ipaddr, lladdr); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 237 | break; |
| 238 | default: |
Alexander Aring | 8911d77 | 2015-10-13 13:42:58 +0200 | [diff] [blame] | 239 | iphc_uncompress_eui64_lladdr(ipaddr, lladdr); |
| 240 | break; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 241 | } |
| 242 | break; |
| 243 | default: |
| 244 | pr_debug("Invalid address mode value: 0x%x\n", address_mode); |
| 245 | return -EINVAL; |
| 246 | } |
| 247 | |
| 248 | if (fail) { |
| 249 | pr_debug("Failed to fetch skb data\n"); |
| 250 | return -EIO; |
| 251 | } |
| 252 | |
| 253 | raw_dump_inline(NULL, "Reconstructed ipv6 addr is", |
| 254 | ipaddr->s6_addr, 16); |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 259 | /* Uncompress address function for source context |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 260 | * based address(non-multicast). |
| 261 | */ |
| 262 | static int uncompress_context_based_src_addr(struct sk_buff *skb, |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 263 | struct in6_addr *ipaddr, |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 264 | u8 address_mode) |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 265 | { |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 266 | switch (address_mode) { |
| 267 | case LOWPAN_IPHC_SAM_00: |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 268 | /* unspec address :: |
| 269 | * Do nothing, address is already :: |
| 270 | */ |
| 271 | break; |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 272 | case LOWPAN_IPHC_SAM_01: |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 273 | /* TODO */ |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 274 | case LOWPAN_IPHC_SAM_10: |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 275 | /* TODO */ |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 276 | case LOWPAN_IPHC_SAM_11: |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 277 | /* TODO */ |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 278 | netdev_warn(skb->dev, "SAM value 0x%x not supported\n", |
| 279 | address_mode); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 280 | return -EINVAL; |
| 281 | default: |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 282 | pr_debug("Invalid sam value: 0x%x\n", address_mode); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 283 | return -EINVAL; |
| 284 | } |
| 285 | |
| 286 | raw_dump_inline(NULL, |
| 287 | "Reconstructed context based ipv6 src addr is", |
| 288 | ipaddr->s6_addr, 16); |
| 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 293 | /* Uncompress function for multicast destination address, |
| 294 | * when M bit is set. |
| 295 | */ |
Marcel Holtmann | 7fc4cfd | 2014-07-30 03:48:41 +0200 | [diff] [blame] | 296 | static int lowpan_uncompress_multicast_daddr(struct sk_buff *skb, |
| 297 | struct in6_addr *ipaddr, |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 298 | u8 address_mode) |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 299 | { |
| 300 | bool fail; |
| 301 | |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 302 | switch (address_mode) { |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 303 | case LOWPAN_IPHC_DAM_00: |
| 304 | /* 00: 128 bits. The full address |
| 305 | * is carried in-line. |
| 306 | */ |
| 307 | fail = lowpan_fetch_skb(skb, ipaddr->s6_addr, 16); |
| 308 | break; |
| 309 | case LOWPAN_IPHC_DAM_01: |
| 310 | /* 01: 48 bits. The address takes |
| 311 | * the form ffXX::00XX:XXXX:XXXX. |
| 312 | */ |
| 313 | ipaddr->s6_addr[0] = 0xFF; |
| 314 | fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[1], 1); |
| 315 | fail |= lowpan_fetch_skb(skb, &ipaddr->s6_addr[11], 5); |
| 316 | break; |
| 317 | case LOWPAN_IPHC_DAM_10: |
| 318 | /* 10: 32 bits. The address takes |
| 319 | * the form ffXX::00XX:XXXX. |
| 320 | */ |
| 321 | ipaddr->s6_addr[0] = 0xFF; |
| 322 | fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[1], 1); |
| 323 | fail |= lowpan_fetch_skb(skb, &ipaddr->s6_addr[13], 3); |
| 324 | break; |
| 325 | case LOWPAN_IPHC_DAM_11: |
| 326 | /* 11: 8 bits. The address takes |
| 327 | * the form ff02::00XX. |
| 328 | */ |
| 329 | ipaddr->s6_addr[0] = 0xFF; |
| 330 | ipaddr->s6_addr[1] = 0x02; |
| 331 | fail = lowpan_fetch_skb(skb, &ipaddr->s6_addr[15], 1); |
| 332 | break; |
| 333 | default: |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 334 | pr_debug("DAM value has a wrong value: 0x%x\n", address_mode); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 335 | return -EINVAL; |
| 336 | } |
| 337 | |
| 338 | if (fail) { |
| 339 | pr_debug("Failed to fetch skb data\n"); |
| 340 | return -EIO; |
| 341 | } |
| 342 | |
| 343 | raw_dump_inline(NULL, "Reconstructed ipv6 multicast addr is", |
Marcel Holtmann | 7fc4cfd | 2014-07-30 03:48:41 +0200 | [diff] [blame] | 344 | ipaddr->s6_addr, 16); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 345 | |
| 346 | return 0; |
| 347 | } |
| 348 | |
Alexander Aring | b5af9bd | 2015-10-20 08:31:25 +0200 | [diff] [blame] | 349 | /* get the ecn values from iphc tf format and set it to ipv6hdr */ |
| 350 | static inline void lowpan_iphc_tf_set_ecn(struct ipv6hdr *hdr, const u8 *tf) |
| 351 | { |
| 352 | /* get the two higher bits which is ecn */ |
| 353 | u8 ecn = tf[0] & 0xc0; |
| 354 | |
| 355 | /* ECN takes 0x30 in hdr->flow_lbl[0] */ |
| 356 | hdr->flow_lbl[0] |= (ecn >> 2); |
| 357 | } |
| 358 | |
| 359 | /* get the dscp values from iphc tf format and set it to ipv6hdr */ |
| 360 | static inline void lowpan_iphc_tf_set_dscp(struct ipv6hdr *hdr, const u8 *tf) |
| 361 | { |
| 362 | /* DSCP is at place after ECN */ |
| 363 | u8 dscp = tf[0] & 0x3f; |
| 364 | |
| 365 | /* The four highest bits need to be set at hdr->priority */ |
| 366 | hdr->priority |= ((dscp & 0x3c) >> 2); |
| 367 | /* The two lower bits is part of hdr->flow_lbl[0] */ |
| 368 | hdr->flow_lbl[0] |= ((dscp & 0x03) << 6); |
| 369 | } |
| 370 | |
| 371 | /* get the flow label values from iphc tf format and set it to ipv6hdr */ |
| 372 | static inline void lowpan_iphc_tf_set_lbl(struct ipv6hdr *hdr, const u8 *lbl) |
| 373 | { |
| 374 | /* flow label is always some array started with lower nibble of |
| 375 | * flow_lbl[0] and followed with two bytes afterwards. Inside inline |
| 376 | * data the flow_lbl position can be different, which will be handled |
| 377 | * by lbl pointer. E.g. case "01" vs "00" the traffic class is 8 bit |
| 378 | * shifted, the different lbl pointer will handle that. |
| 379 | * |
| 380 | * The flow label will started at lower nibble of flow_lbl[0], the |
| 381 | * higher nibbles are part of DSCP + ECN. |
| 382 | */ |
| 383 | hdr->flow_lbl[0] |= lbl[0] & 0x0f; |
| 384 | memcpy(&hdr->flow_lbl[1], &lbl[1], 2); |
| 385 | } |
| 386 | |
| 387 | /* lowpan_iphc_tf_decompress - decompress the traffic class. |
| 388 | * This function will return zero on success, a value lower than zero if |
| 389 | * failed. |
| 390 | */ |
| 391 | static int lowpan_iphc_tf_decompress(struct sk_buff *skb, struct ipv6hdr *hdr, |
| 392 | u8 val) |
| 393 | { |
| 394 | u8 tf[4]; |
| 395 | |
| 396 | /* Traffic Class and Flow Label */ |
| 397 | switch (val) { |
| 398 | case LOWPAN_IPHC_TF_00: |
| 399 | /* ECN + DSCP + 4-bit Pad + Flow Label (4 bytes) */ |
| 400 | if (lowpan_fetch_skb(skb, tf, 4)) |
| 401 | return -EINVAL; |
| 402 | |
| 403 | /* 1 2 3 |
| 404 | * 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 |
| 405 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 406 | * |ECN| DSCP | rsv | Flow Label | |
| 407 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 408 | */ |
| 409 | lowpan_iphc_tf_set_ecn(hdr, tf); |
| 410 | lowpan_iphc_tf_set_dscp(hdr, tf); |
| 411 | lowpan_iphc_tf_set_lbl(hdr, &tf[1]); |
| 412 | break; |
| 413 | case LOWPAN_IPHC_TF_01: |
| 414 | /* ECN + 2-bit Pad + Flow Label (3 bytes), DSCP is elided. */ |
| 415 | if (lowpan_fetch_skb(skb, tf, 3)) |
| 416 | return -EINVAL; |
| 417 | |
| 418 | /* 1 2 |
| 419 | * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 |
| 420 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 421 | * |ECN|rsv| Flow Label | |
| 422 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 423 | */ |
| 424 | lowpan_iphc_tf_set_ecn(hdr, tf); |
| 425 | lowpan_iphc_tf_set_lbl(hdr, &tf[0]); |
| 426 | break; |
| 427 | case LOWPAN_IPHC_TF_10: |
| 428 | /* ECN + DSCP (1 byte), Flow Label is elided. */ |
| 429 | if (lowpan_fetch_skb(skb, tf, 1)) |
| 430 | return -EINVAL; |
| 431 | |
| 432 | /* 0 1 2 3 4 5 6 7 |
| 433 | * +-+-+-+-+-+-+-+-+ |
| 434 | * |ECN| DSCP | |
| 435 | * +-+-+-+-+-+-+-+-+ |
| 436 | */ |
| 437 | lowpan_iphc_tf_set_ecn(hdr, tf); |
| 438 | lowpan_iphc_tf_set_dscp(hdr, tf); |
| 439 | break; |
| 440 | case LOWPAN_IPHC_TF_11: |
| 441 | /* Traffic Class and Flow Label are elided */ |
| 442 | break; |
| 443 | default: |
| 444 | WARN_ON_ONCE(1); |
| 445 | return -EINVAL; |
| 446 | } |
| 447 | |
| 448 | return 0; |
| 449 | } |
| 450 | |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 451 | /* TTL uncompression values */ |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 452 | static const u8 lowpan_ttl_values[] = { |
| 453 | [LOWPAN_IPHC_HLIM_01] = 1, |
| 454 | [LOWPAN_IPHC_HLIM_10] = 64, |
| 455 | [LOWPAN_IPHC_HLIM_11] = 255, |
| 456 | }; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 457 | |
Alexander Aring | 8911d77 | 2015-10-13 13:42:58 +0200 | [diff] [blame] | 458 | int lowpan_header_decompress(struct sk_buff *skb, const struct net_device *dev, |
| 459 | const void *daddr, const void *saddr) |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 460 | { |
| 461 | struct ipv6hdr hdr = {}; |
Alexander Aring | b5af9bd | 2015-10-20 08:31:25 +0200 | [diff] [blame] | 462 | u8 iphc0, iphc1; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 463 | int err; |
| 464 | |
| 465 | raw_dump_table(__func__, "raw skb data dump uncompressed", |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 466 | skb->data, skb->len); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 467 | |
Alexander Aring | 478208e | 2015-10-13 13:42:59 +0200 | [diff] [blame] | 468 | if (lowpan_fetch_skb(skb, &iphc0, sizeof(iphc0)) || |
| 469 | lowpan_fetch_skb(skb, &iphc1, sizeof(iphc1))) |
Alexander Aring | 8911d77 | 2015-10-13 13:42:58 +0200 | [diff] [blame] | 470 | return -EINVAL; |
| 471 | |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 472 | /* another if the CID flag is set */ |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 473 | if (iphc1 & LOWPAN_IPHC_CID) |
| 474 | return -ENOTSUPP; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 475 | |
| 476 | hdr.version = 6; |
| 477 | |
Alexander Aring | b5af9bd | 2015-10-20 08:31:25 +0200 | [diff] [blame] | 478 | err = lowpan_iphc_tf_decompress(skb, &hdr, |
| 479 | iphc0 & LOWPAN_IPHC_TF_MASK); |
| 480 | if (err < 0) |
| 481 | return err; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 482 | |
| 483 | /* Next Header */ |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 484 | if (!(iphc0 & LOWPAN_IPHC_NH)) { |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 485 | /* Next header is carried inline */ |
Alexander Aring | 4ebc960 | 2014-07-29 23:47:00 +0200 | [diff] [blame] | 486 | if (lowpan_fetch_skb(skb, &hdr.nexthdr, sizeof(hdr.nexthdr))) |
Martin Townsend | 56b2c3e | 2014-11-06 19:15:13 +0000 | [diff] [blame] | 487 | return -EINVAL; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 488 | |
| 489 | pr_debug("NH flag is set, next header carried inline: %02x\n", |
| 490 | hdr.nexthdr); |
| 491 | } |
| 492 | |
| 493 | /* Hop Limit */ |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 494 | if ((iphc0 & LOWPAN_IPHC_HLIM_MASK) != LOWPAN_IPHC_HLIM_00) { |
| 495 | hdr.hop_limit = lowpan_ttl_values[iphc0 & LOWPAN_IPHC_HLIM_MASK]; |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 496 | } else { |
Alexander Aring | 4ebc960 | 2014-07-29 23:47:00 +0200 | [diff] [blame] | 497 | if (lowpan_fetch_skb(skb, &hdr.hop_limit, |
| 498 | sizeof(hdr.hop_limit))) |
Martin Townsend | 56b2c3e | 2014-11-06 19:15:13 +0000 | [diff] [blame] | 499 | return -EINVAL; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 500 | } |
| 501 | |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 502 | if (iphc1 & LOWPAN_IPHC_SAC) { |
| 503 | /* Source address context based uncompression */ |
| 504 | pr_debug("SAC bit is set. Handle context based source address.\n"); |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 505 | err = uncompress_context_based_src_addr(skb, &hdr.saddr, |
| 506 | iphc1 & LOWPAN_IPHC_SAM_MASK); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 507 | } else { |
| 508 | /* Source address uncompression */ |
| 509 | pr_debug("source address stateless compression\n"); |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 510 | err = uncompress_addr(skb, dev, &hdr.saddr, |
| 511 | iphc1 & LOWPAN_IPHC_SAM_MASK, saddr); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | /* Check on error of previous branch */ |
| 515 | if (err) |
Martin Townsend | 56b2c3e | 2014-11-06 19:15:13 +0000 | [diff] [blame] | 516 | return -EINVAL; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 517 | |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 518 | /* check for Multicast Compression */ |
| 519 | if (iphc1 & LOWPAN_IPHC_M) { |
| 520 | if (iphc1 & LOWPAN_IPHC_DAC) { |
| 521 | pr_debug("dest: context-based mcast compression\n"); |
| 522 | /* TODO: implement this */ |
| 523 | } else { |
Marcel Holtmann | 7fc4cfd | 2014-07-30 03:48:41 +0200 | [diff] [blame] | 524 | err = lowpan_uncompress_multicast_daddr(skb, &hdr.daddr, |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 525 | iphc1 & LOWPAN_IPHC_DAM_MASK); |
Marcel Holtmann | 7fc4cfd | 2014-07-30 03:48:41 +0200 | [diff] [blame] | 526 | |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 527 | if (err) |
Martin Townsend | 56b2c3e | 2014-11-06 19:15:13 +0000 | [diff] [blame] | 528 | return -EINVAL; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 529 | } |
| 530 | } else { |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 531 | err = uncompress_addr(skb, dev, &hdr.daddr, |
| 532 | iphc1 & LOWPAN_IPHC_DAM_MASK, daddr); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 533 | pr_debug("dest: stateless compression mode %d dest %pI6c\n", |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 534 | iphc1 & LOWPAN_IPHC_DAM_MASK, &hdr.daddr); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 535 | if (err) |
Martin Townsend | 56b2c3e | 2014-11-06 19:15:13 +0000 | [diff] [blame] | 536 | return -EINVAL; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 537 | } |
| 538 | |
Alexander Aring | cc6ed26 | 2015-01-09 16:42:58 +0100 | [diff] [blame] | 539 | /* Next header data uncompression */ |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 540 | if (iphc0 & LOWPAN_IPHC_NH) { |
Alexander Aring | cc6ed26 | 2015-01-09 16:42:58 +0100 | [diff] [blame] | 541 | err = lowpan_nhc_do_uncompression(skb, dev, &hdr); |
| 542 | if (err < 0) |
Martin Townsend | 11e3ff7 | 2014-10-13 11:00:56 +0100 | [diff] [blame] | 543 | return err; |
Martin Townsend | 11e3ff7 | 2014-10-13 11:00:56 +0100 | [diff] [blame] | 544 | } else { |
| 545 | err = skb_cow(skb, sizeof(hdr)); |
Martin Townsend | 56b2c3e | 2014-11-06 19:15:13 +0000 | [diff] [blame] | 546 | if (unlikely(err)) |
Martin Townsend | 11e3ff7 | 2014-10-13 11:00:56 +0100 | [diff] [blame] | 547 | return err; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 548 | } |
| 549 | |
Alexander Aring | 72a5e6b | 2015-09-02 14:21:25 +0200 | [diff] [blame] | 550 | switch (lowpan_priv(dev)->lltype) { |
| 551 | case LOWPAN_LLTYPE_IEEE802154: |
| 552 | if (lowpan_802154_cb(skb)->d_size) |
| 553 | hdr.payload_len = htons(lowpan_802154_cb(skb)->d_size - |
| 554 | sizeof(struct ipv6hdr)); |
| 555 | else |
| 556 | hdr.payload_len = htons(skb->len); |
| 557 | break; |
| 558 | default: |
| 559 | hdr.payload_len = htons(skb->len); |
| 560 | break; |
| 561 | } |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 562 | |
| 563 | pr_debug("skb headroom size = %d, data length = %d\n", |
| 564 | skb_headroom(skb), skb->len); |
| 565 | |
| 566 | pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength = %d\n\t" |
| 567 | "nexthdr = 0x%02x\n\thop_lim = %d\n\tdest = %pI6c\n", |
| 568 | hdr.version, ntohs(hdr.payload_len), hdr.nexthdr, |
| 569 | hdr.hop_limit, &hdr.daddr); |
| 570 | |
Martin Townsend | f8b3617 | 2014-10-23 15:40:53 +0100 | [diff] [blame] | 571 | skb_push(skb, sizeof(hdr)); |
| 572 | skb_reset_network_header(skb); |
| 573 | skb_copy_to_linear_data(skb, &hdr, sizeof(hdr)); |
| 574 | |
Marcel Holtmann | 7fc4cfd | 2014-07-30 03:48:41 +0200 | [diff] [blame] | 575 | raw_dump_table(__func__, "raw header dump", (u8 *)&hdr, sizeof(hdr)); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 576 | |
Martin Townsend | f8b3617 | 2014-10-23 15:40:53 +0100 | [diff] [blame] | 577 | return 0; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 578 | } |
Martin Townsend | 0114123 | 2014-10-23 15:40:56 +0100 | [diff] [blame] | 579 | EXPORT_SYMBOL_GPL(lowpan_header_decompress); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 580 | |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 581 | static const u8 lowpan_iphc_dam_to_sam_value[] = { |
| 582 | [LOWPAN_IPHC_DAM_00] = LOWPAN_IPHC_SAM_00, |
| 583 | [LOWPAN_IPHC_DAM_01] = LOWPAN_IPHC_SAM_01, |
| 584 | [LOWPAN_IPHC_DAM_10] = LOWPAN_IPHC_SAM_10, |
| 585 | [LOWPAN_IPHC_DAM_11] = LOWPAN_IPHC_SAM_11, |
| 586 | }; |
| 587 | |
| 588 | static u8 lowpan_compress_addr_64(u8 **hc_ptr, const struct in6_addr *ipaddr, |
| 589 | const unsigned char *lladdr, bool sam) |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 590 | { |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 591 | u8 dam = LOWPAN_IPHC_DAM_00; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 592 | |
| 593 | if (is_addr_mac_addr_based(ipaddr, lladdr)) { |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 594 | dam = LOWPAN_IPHC_DAM_11; /* 0-bits */ |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 595 | pr_debug("address compression 0 bits\n"); |
| 596 | } else if (lowpan_is_iid_16_bit_compressable(ipaddr)) { |
| 597 | /* compress IID to 16 bits xxxx::XXXX */ |
Alexander Aring | 85c7124 | 2014-07-29 23:47:01 +0200 | [diff] [blame] | 598 | lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr16[7], 2); |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 599 | dam = LOWPAN_IPHC_DAM_10; /* 16-bits */ |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 600 | raw_dump_inline(NULL, "Compressed ipv6 addr is (16 bits)", |
Alexander Aring | 84ca5e0 | 2014-07-29 23:46:58 +0200 | [diff] [blame] | 601 | *hc_ptr - 2, 2); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 602 | } else { |
| 603 | /* do not compress IID => xxxx::IID */ |
Alexander Aring | 85c7124 | 2014-07-29 23:47:01 +0200 | [diff] [blame] | 604 | lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr16[4], 8); |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 605 | dam = LOWPAN_IPHC_DAM_01; /* 64-bits */ |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 606 | raw_dump_inline(NULL, "Compressed ipv6 addr is (64 bits)", |
Alexander Aring | 84ca5e0 | 2014-07-29 23:46:58 +0200 | [diff] [blame] | 607 | *hc_ptr - 8, 8); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 608 | } |
| 609 | |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 610 | if (sam) |
| 611 | return lowpan_iphc_dam_to_sam_value[dam]; |
| 612 | else |
| 613 | return dam; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 614 | } |
| 615 | |
Alexander Aring | b5af9bd | 2015-10-20 08:31:25 +0200 | [diff] [blame] | 616 | /* lowpan_iphc_get_tc - get the ECN + DCSP fields in hc format */ |
| 617 | static inline u8 lowpan_iphc_get_tc(const struct ipv6hdr *hdr) |
| 618 | { |
| 619 | u8 dscp, ecn; |
| 620 | |
| 621 | /* hdr->priority contains the higher bits of dscp, lower are part of |
| 622 | * flow_lbl[0]. Note ECN, DCSP is swapped in ipv6 hdr. |
| 623 | */ |
| 624 | dscp = (hdr->priority << 2) | ((hdr->flow_lbl[0] & 0xc0) >> 6); |
| 625 | /* ECN is at the two lower bits from first nibble of flow_lbl[0] */ |
| 626 | ecn = (hdr->flow_lbl[0] & 0x30); |
| 627 | /* for pretty debug output, also shift ecn to get the ecn value */ |
| 628 | pr_debug("ecn 0x%02x dscp 0x%02x\n", ecn >> 4, dscp); |
| 629 | /* ECN is at 0x30 now, shift it to have ECN + DCSP */ |
| 630 | return (ecn << 2) | dscp; |
| 631 | } |
| 632 | |
| 633 | /* lowpan_iphc_is_flow_lbl_zero - check if flow label is zero */ |
| 634 | static inline bool lowpan_iphc_is_flow_lbl_zero(const struct ipv6hdr *hdr) |
| 635 | { |
| 636 | return ((!(hdr->flow_lbl[0] & 0x0f)) && |
| 637 | !hdr->flow_lbl[1] && !hdr->flow_lbl[2]); |
| 638 | } |
| 639 | |
| 640 | /* lowpan_iphc_tf_compress - compress the traffic class which is set by |
| 641 | * ipv6hdr. Return the corresponding format identifier which is used. |
| 642 | */ |
| 643 | static u8 lowpan_iphc_tf_compress(u8 **hc_ptr, const struct ipv6hdr *hdr) |
| 644 | { |
| 645 | /* get ecn dscp data in a byteformat as: ECN(hi) + DSCP(lo) */ |
| 646 | u8 tc = lowpan_iphc_get_tc(hdr), tf[4], val; |
| 647 | |
| 648 | /* printout the traffic class in hc format */ |
| 649 | pr_debug("tc 0x%02x\n", tc); |
| 650 | |
| 651 | if (lowpan_iphc_is_flow_lbl_zero(hdr)) { |
| 652 | if (!tc) { |
| 653 | /* 11: Traffic Class and Flow Label are elided. */ |
| 654 | val = LOWPAN_IPHC_TF_11; |
| 655 | } else { |
| 656 | /* 10: ECN + DSCP (1 byte), Flow Label is elided. |
| 657 | * |
| 658 | * 0 1 2 3 4 5 6 7 |
| 659 | * +-+-+-+-+-+-+-+-+ |
| 660 | * |ECN| DSCP | |
| 661 | * +-+-+-+-+-+-+-+-+ |
| 662 | */ |
| 663 | lowpan_push_hc_data(hc_ptr, &tc, sizeof(tc)); |
| 664 | val = LOWPAN_IPHC_TF_10; |
| 665 | } |
| 666 | } else { |
| 667 | /* check if dscp is zero, it's after the first two bit */ |
| 668 | if (!(tc & 0x3f)) { |
| 669 | /* 01: ECN + 2-bit Pad + Flow Label (3 bytes), DSCP is elided |
| 670 | * |
| 671 | * 1 2 |
| 672 | * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 |
| 673 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 674 | * |ECN|rsv| Flow Label | |
| 675 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 676 | */ |
| 677 | memcpy(&tf[0], &hdr->flow_lbl[0], 3); |
| 678 | /* zero the highest 4-bits, contains DCSP + ECN */ |
| 679 | tf[0] &= ~0xf0; |
| 680 | /* set ECN */ |
| 681 | tf[0] |= (tc & 0xc0); |
| 682 | |
| 683 | lowpan_push_hc_data(hc_ptr, tf, 3); |
| 684 | val = LOWPAN_IPHC_TF_01; |
| 685 | } else { |
| 686 | /* 00: ECN + DSCP + 4-bit Pad + Flow Label (4 bytes) |
| 687 | * |
| 688 | * 1 2 3 |
| 689 | * 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 |
| 690 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 691 | * |ECN| DSCP | rsv | Flow Label | |
| 692 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 693 | */ |
| 694 | memcpy(&tf[0], &tc, sizeof(tc)); |
| 695 | /* highest nibble of flow_lbl[0] is part of DSCP + ECN |
| 696 | * which will be the 4-bit pad and will be filled with |
| 697 | * zeros afterwards. |
| 698 | */ |
| 699 | memcpy(&tf[1], &hdr->flow_lbl[0], 3); |
| 700 | /* zero the 4-bit pad, which is reserved */ |
| 701 | tf[1] &= ~0xf0; |
| 702 | |
| 703 | lowpan_push_hc_data(hc_ptr, tf, 4); |
| 704 | val = LOWPAN_IPHC_TF_00; |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | return val; |
| 709 | } |
| 710 | |
Alexander Aring | 09bf420 | 2015-10-20 08:31:26 +0200 | [diff] [blame] | 711 | static u8 lowpan_iphc_mcast_addr_compress(u8 **hc_ptr, |
| 712 | const struct in6_addr *ipaddr) |
| 713 | { |
| 714 | u8 val; |
| 715 | |
| 716 | if (lowpan_is_mcast_addr_compressable8(ipaddr)) { |
| 717 | pr_debug("compressed to 1 octet\n"); |
| 718 | /* use last byte */ |
| 719 | lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr[15], 1); |
| 720 | val = LOWPAN_IPHC_DAM_11; |
| 721 | } else if (lowpan_is_mcast_addr_compressable32(ipaddr)) { |
| 722 | pr_debug("compressed to 4 octets\n"); |
| 723 | /* second byte + the last three */ |
| 724 | lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr[1], 1); |
| 725 | lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr[13], 3); |
| 726 | val = LOWPAN_IPHC_DAM_10; |
| 727 | } else if (lowpan_is_mcast_addr_compressable48(ipaddr)) { |
| 728 | pr_debug("compressed to 6 octets\n"); |
| 729 | /* second byte + the last five */ |
| 730 | lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr[1], 1); |
| 731 | lowpan_push_hc_data(hc_ptr, &ipaddr->s6_addr[11], 5); |
| 732 | val = LOWPAN_IPHC_DAM_01; |
| 733 | } else { |
| 734 | pr_debug("using full address\n"); |
| 735 | lowpan_push_hc_data(hc_ptr, ipaddr->s6_addr, 16); |
| 736 | val = LOWPAN_IPHC_DAM_00; |
| 737 | } |
| 738 | |
| 739 | return val; |
| 740 | } |
| 741 | |
Alexander Aring | a6f7738 | 2015-10-13 13:42:57 +0200 | [diff] [blame] | 742 | int lowpan_header_compress(struct sk_buff *skb, const struct net_device *dev, |
| 743 | const void *daddr, const void *saddr) |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 744 | { |
Alexander Aring | b5af9bd | 2015-10-20 08:31:25 +0200 | [diff] [blame] | 745 | u8 iphc0, iphc1, *hc_ptr; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 746 | struct ipv6hdr *hdr; |
Alexander Aring | bf513fd | 2015-10-13 13:42:56 +0200 | [diff] [blame] | 747 | u8 head[LOWPAN_IPHC_MAX_HC_BUF_LEN] = {}; |
Alexander Aring | cc6ed26 | 2015-01-09 16:42:58 +0100 | [diff] [blame] | 748 | int ret, addr_type; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 749 | |
Alexander Aring | a6f7738 | 2015-10-13 13:42:57 +0200 | [diff] [blame] | 750 | if (skb->protocol != htons(ETH_P_IPV6)) |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 751 | return -EINVAL; |
| 752 | |
| 753 | hdr = ipv6_hdr(skb); |
Alexander Aring | 84ca5e0 | 2014-07-29 23:46:58 +0200 | [diff] [blame] | 754 | hc_ptr = head + 2; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 755 | |
| 756 | pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength = %d\n" |
| 757 | "\tnexthdr = 0x%02x\n\thop_lim = %d\n\tdest = %pI6c\n", |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 758 | hdr->version, ntohs(hdr->payload_len), hdr->nexthdr, |
| 759 | hdr->hop_limit, &hdr->daddr); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 760 | |
| 761 | raw_dump_table(__func__, "raw skb network header dump", |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 762 | skb_network_header(skb), sizeof(struct ipv6hdr)); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 763 | |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 764 | /* As we copy some bit-length fields, in the IPHC encoding bytes, |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 765 | * we sometimes use |= |
| 766 | * If the field is 0, and the current bit value in memory is 1, |
| 767 | * this does not work. We therefore reset the IPHC encoding here |
| 768 | */ |
| 769 | iphc0 = LOWPAN_DISPATCH_IPHC; |
| 770 | iphc1 = 0; |
| 771 | |
| 772 | /* TODO: context lookup */ |
| 773 | |
Alexander Aring | a6f7738 | 2015-10-13 13:42:57 +0200 | [diff] [blame] | 774 | raw_dump_inline(__func__, "saddr", saddr, EUI64_ADDR_LEN); |
| 775 | raw_dump_inline(__func__, "daddr", daddr, EUI64_ADDR_LEN); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 776 | |
Marcel Holtmann | 7fc4cfd | 2014-07-30 03:48:41 +0200 | [diff] [blame] | 777 | raw_dump_table(__func__, "sending raw skb network uncompressed packet", |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 778 | skb->data, skb->len); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 779 | |
Alexander Aring | b5af9bd | 2015-10-20 08:31:25 +0200 | [diff] [blame] | 780 | /* Traffic Class, Flow Label compression */ |
| 781 | iphc0 |= lowpan_iphc_tf_compress(&hc_ptr, hdr); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 782 | |
| 783 | /* NOTE: payload length is always compressed */ |
| 784 | |
Alexander Aring | cc6ed26 | 2015-01-09 16:42:58 +0100 | [diff] [blame] | 785 | /* Check if we provide the nhc format for nexthdr and compression |
| 786 | * functionality. If not nexthdr is handled inline and not compressed. |
| 787 | */ |
Alexander Aring | 607b0bd | 2015-10-20 08:31:21 +0200 | [diff] [blame] | 788 | ret = lowpan_nhc_check_compression(skb, hdr, &hc_ptr); |
| 789 | if (ret == -ENOENT) |
| 790 | lowpan_push_hc_data(&hc_ptr, &hdr->nexthdr, |
| 791 | sizeof(hdr->nexthdr)); |
| 792 | else |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 793 | iphc0 |= LOWPAN_IPHC_NH; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 794 | |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 795 | /* Hop limit |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 796 | * if 1: compress, encoding is 01 |
| 797 | * if 64: compress, encoding is 10 |
| 798 | * if 255: compress, encoding is 11 |
| 799 | * else do not compress |
| 800 | */ |
| 801 | switch (hdr->hop_limit) { |
| 802 | case 1: |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 803 | iphc0 |= LOWPAN_IPHC_HLIM_01; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 804 | break; |
| 805 | case 64: |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 806 | iphc0 |= LOWPAN_IPHC_HLIM_10; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 807 | break; |
| 808 | case 255: |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 809 | iphc0 |= LOWPAN_IPHC_HLIM_11; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 810 | break; |
| 811 | default: |
Alexander Aring | 85c7124 | 2014-07-29 23:47:01 +0200 | [diff] [blame] | 812 | lowpan_push_hc_data(&hc_ptr, &hdr->hop_limit, |
| 813 | sizeof(hdr->hop_limit)); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 814 | } |
| 815 | |
Alexander Aring | 556a5bf | 2014-07-29 23:47:02 +0200 | [diff] [blame] | 816 | addr_type = ipv6_addr_type(&hdr->saddr); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 817 | /* source address compression */ |
Alexander Aring | 556a5bf | 2014-07-29 23:47:02 +0200 | [diff] [blame] | 818 | if (addr_type == IPV6_ADDR_ANY) { |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 819 | pr_debug("source address is unspecified, setting SAC\n"); |
| 820 | iphc1 |= LOWPAN_IPHC_SAC; |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 821 | } else { |
Alexander Aring | 556a5bf | 2014-07-29 23:47:02 +0200 | [diff] [blame] | 822 | if (addr_type & IPV6_ADDR_LINKLOCAL) { |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 823 | iphc1 |= lowpan_compress_addr_64(&hc_ptr, &hdr->saddr, |
| 824 | saddr, true); |
Alexander Aring | 556a5bf | 2014-07-29 23:47:02 +0200 | [diff] [blame] | 825 | pr_debug("source address unicast link-local %pI6c iphc1 0x%02x\n", |
| 826 | &hdr->saddr, iphc1); |
| 827 | } else { |
| 828 | pr_debug("send the full source address\n"); |
| 829 | lowpan_push_hc_data(&hc_ptr, hdr->saddr.s6_addr, 16); |
| 830 | } |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 831 | } |
| 832 | |
Alexander Aring | 556a5bf | 2014-07-29 23:47:02 +0200 | [diff] [blame] | 833 | addr_type = ipv6_addr_type(&hdr->daddr); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 834 | /* destination address compression */ |
Alexander Aring | 556a5bf | 2014-07-29 23:47:02 +0200 | [diff] [blame] | 835 | if (addr_type & IPV6_ADDR_MULTICAST) { |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 836 | pr_debug("destination address is multicast: "); |
| 837 | iphc1 |= LOWPAN_IPHC_M; |
Alexander Aring | 09bf420 | 2015-10-20 08:31:26 +0200 | [diff] [blame] | 838 | iphc1 |= lowpan_iphc_mcast_addr_compress(&hc_ptr, &hdr->daddr); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 839 | } else { |
Alexander Aring | 556a5bf | 2014-07-29 23:47:02 +0200 | [diff] [blame] | 840 | if (addr_type & IPV6_ADDR_LINKLOCAL) { |
| 841 | /* TODO: context lookup */ |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 842 | iphc1 |= lowpan_compress_addr_64(&hc_ptr, &hdr->daddr, |
| 843 | daddr, false); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 844 | pr_debug("dest address unicast link-local %pI6c " |
Marcel Holtmann | 7fc4cfd | 2014-07-30 03:48:41 +0200 | [diff] [blame] | 845 | "iphc1 0x%02x\n", &hdr->daddr, iphc1); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 846 | } else { |
| 847 | pr_debug("dest address unicast %pI6c\n", &hdr->daddr); |
Alexander Aring | 85c7124 | 2014-07-29 23:47:01 +0200 | [diff] [blame] | 848 | lowpan_push_hc_data(&hc_ptr, hdr->daddr.s6_addr, 16); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 849 | } |
| 850 | } |
| 851 | |
Alexander Aring | cc6ed26 | 2015-01-09 16:42:58 +0100 | [diff] [blame] | 852 | /* next header compression */ |
Alexander Aring | c8a3e7e | 2015-10-20 08:31:24 +0200 | [diff] [blame] | 853 | if (iphc0 & LOWPAN_IPHC_NH) { |
Alexander Aring | cc6ed26 | 2015-01-09 16:42:58 +0100 | [diff] [blame] | 854 | ret = lowpan_nhc_do_compression(skb, hdr, &hc_ptr); |
| 855 | if (ret < 0) |
| 856 | return ret; |
| 857 | } |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 858 | |
| 859 | head[0] = iphc0; |
| 860 | head[1] = iphc1; |
| 861 | |
| 862 | skb_pull(skb, sizeof(struct ipv6hdr)); |
| 863 | skb_reset_transport_header(skb); |
Alexander Aring | 84ca5e0 | 2014-07-29 23:46:58 +0200 | [diff] [blame] | 864 | memcpy(skb_push(skb, hc_ptr - head), head, hc_ptr - head); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 865 | skb_reset_network_header(skb); |
| 866 | |
Alexander Aring | 84ca5e0 | 2014-07-29 23:46:58 +0200 | [diff] [blame] | 867 | 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] | 868 | |
| 869 | raw_dump_table(__func__, "raw skb data dump compressed", |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 870 | skb->data, skb->len); |
Jukka Rissanen | 8df8c56 | 2013-12-11 17:05:34 +0200 | [diff] [blame] | 871 | return 0; |
| 872 | } |
| 873 | EXPORT_SYMBOL_GPL(lowpan_header_compress); |