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