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