Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 | /** @file * @brief 802.15.4 fragment related functions */ /* * Copyright (c) 2016 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ #if defined(CONFIG_NET_DEBUG_L2_IEEE802154_FRAGMENT) #define SYS_LOG_DOMAIN "net/ieee802154" #define NET_LOG_ENABLED 1 #endif #include <errno.h> #include <net/net_core.h> #include <net/net_pkt.h> #include <net/net_if.h> #include <net/net_stats.h> #include <net/udp.h> #include "ieee802154_fragment.h" #include "net_private.h" #include "6lo.h" #include "6lo_private.h" #define FRAG_REASSEMBLY_TIMEOUT (MSEC_PER_SEC * \ CONFIG_NET_L2_IEEE802154_REASSEMBLY_TIMEOUT) #define REASS_CACHE_SIZE CONFIG_NET_L2_IEEE802154_FRAGMENT_REASS_CACHE_SIZE static u16_t datagram_tag; /** * Reassemble cache : Depends on cache size it used for reassemble * IPv6 packets simultaneously. */ struct frag_cache { struct k_delayed_work timer; /* Reassemble timer */ struct net_pkt *pkt; /* Reassemble packet */ u16_t size; /* Datagram size */ u16_t tag; /* Datagram tag */ bool used; }; static struct frag_cache cache[REASS_CACHE_SIZE]; /** * RFC 4944, section 5.3 * If an entire payload (e.g., IPv6) datagram fits within a single 802.15.4 * frame, it is unfragmented and the LoWPAN encapsulation should not contain * a fragmentation header. If the datagram does not fit within a single * IEEE 802.15.4 frame, it SHALL be broken into link fragments. As the * fragment offset can only express multiples of eight bytes, all link * fragments for a datagram except the last one MUST be multiples of eight * bytes in length. * * RFC 7668, section 3 (IPv6 over Bluetooth Low Energy) * Functionality is comprised of link-local IPv6 addresses and stateless * IPv6 address autoconfiguration, Neighbor Discovery, and header compression * Fragmentation features from 6LoWPAN standards are not used due to Bluetooth * LE's link-layer fragmentation support. */ /** * 1 2 3 * 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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * |1 1 0 0 0| datagram_size | datagram_tag | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * * 1 2 3 * 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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * |1 1 0 0 0| datagram_size | datagram_tag | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * |datagram_offset| * +-+-+-+-+-+-+-+-+ */ static inline struct net_buf *prepare_new_fragment(struct net_pkt *pkt, u8_t offset) { struct net_buf *frag; frag = net_pkt_get_frag(pkt, K_FOREVER); if (!frag) { return NULL; } /* Reserve space for fragmentation header */ if (!offset) { net_buf_add(frag, NET_6LO_FRAG1_HDR_LEN); } else { net_buf_add(frag, NET_6LO_FRAGN_HDR_LEN); } net_pkt_frag_add(pkt, frag); return frag; } static inline void set_datagram_size(u8_t *ptr, u16_t size) { ptr[0] |= ((size & 0x7FF) >> 8); ptr[1] = (u8_t) size; } static inline void set_datagram_tag(u8_t *ptr, u16_t tag) { ptr[0] = tag >> 8; ptr[1] = (u8_t) tag; } static inline void set_up_frag_hdr(struct net_buf *frag, u16_t size, u8_t offset) { u8_t pos = 0; if (offset) { frag->data[pos] = NET_6LO_DISPATCH_FRAGN; } else { frag->data[pos] = NET_6LO_DISPATCH_FRAG1; } set_datagram_size(frag->data, size); pos += NET_6LO_FRAG_DATAGRAM_SIZE_LEN; set_datagram_tag(frag->data + pos, datagram_tag); pos += NET_6LO_FRAG_DATAGRAM_OFFSET_LEN; if (offset) { frag->data[pos] = offset; } } static inline u8_t calc_max_payload(struct net_pkt *pkt, struct net_buf *frag, u8_t offset) { u8_t max; max = frag->size - net_pkt_ll_reserve(pkt); max -= offset ? NET_6LO_FRAGN_HDR_LEN : NET_6LO_FRAG1_HDR_LEN; return (max & 0xF8); } static inline u8_t move_frag_data(struct net_buf *frag, struct net_buf *next, u8_t max, bool first, int hdr_diff, u8_t *room_left) { u8_t room; u8_t move; u8_t occupied; /* First fragment */ if (first) { occupied = frag->len - NET_6LO_FRAG1_HDR_LEN; } else { occupied = frag->len - NET_6LO_FRAGN_HDR_LEN; } /* Remaining room for data */ room = max - occupied; if (first) { room -= hdr_diff; } /* Calculate remaining room space for data to move */ move = next->len > room ? room : next->len; memmove(frag->data + frag->len, next->data, move); net_buf_add(frag, move); /* Room left in current fragment */ *room_left = room - move; return move; } static inline void compact_frag(struct net_buf *frag, u8_t moved) { u8_t remaining = frag->len - moved; /* Move remaining data next to fragmentation header, * (leave space for header). */ if (remaining) { memmove(frag->data, frag->data + moved, remaining); } frag->len = remaining; } /** * ch : compressed (IPv6) header(s) * fh : fragment header (dispatch + size + tag + [offset]) * p : payload (first fragment holds IPv6 hdr as payload) * e : empty space * * Input to ieee802154_fragment() buf chain looks like below * * | ch + p | p | p | p | p | p + e | * * After complete fragmentation buf chain looks like below * * |fh + p + e | fh + p + e | fh + p + e | fh + p + e | fh + p + e | * * Space in every fragment is because fragment payload should be multiple * of 8 octets (we have predefined packets at compile time, data packet mtu * is set already). * * Create the first fragment, add fragmentation header and insert * fragment at beginning of pkt, move data from next fragments to * previous one, from here on insert fragmentation header and adjust * data on subsequent packets. */ bool ieee802154_fragment(struct net_pkt *pkt, int hdr_diff) { struct net_buf *frag; struct net_buf *next; u16_t processed; u16_t offset; u16_t size; u8_t room; u8_t move; u8_t max; bool first; if (!pkt || !pkt->frags) { return false; } /* If it is a single fragment do not add fragmentation header */ if (!pkt->frags->frags) { return true; } /* Datagram_size: total length before compression */ size = net_pkt_get_len(pkt) + hdr_diff; room = 0; offset = 0; processed = 0; first = true; datagram_tag++; next = pkt->frags; pkt->frags = NULL; /* First fragment has compressed header, but SIZE and OFFSET * values in fragmentation header are based on uncompressed * IP packet. */ while (1) { if (!room) { /* Prepare new fragment based on offset */ frag = prepare_new_fragment(pkt, offset); if (!frag) { return false; } /* Set fragmentation header in the beginning */ set_up_frag_hdr(frag, size, offset); /* Calculate max payload in multiples of 8 bytes */ max = calc_max_payload(pkt, frag, offset); /* Calculate how much data is processed */ processed += max; offset = processed >> 3; } /* Move data from next fragment to current fragment */ move = move_frag_data(frag, next, max, first, hdr_diff, &room); first = false; /* Compact the next fragment */ compact_frag(next, move); if (!next->len) { next = net_pkt_frag_del(pkt, NULL, next); if (!next) { break; } } } return true; } static inline u16_t get_datagram_size(u8_t *ptr) { return ((ptr[0] & 0x1F) << 8) | ptr[1]; } static inline u16_t get_datagram_tag(u8_t *ptr) { return (ptr[0] << 8) | ptr[1]; } static inline void remove_frag_header(struct net_buf *frag, u8_t hdr_len) { memmove(frag->data, frag->data + hdr_len, frag->len - hdr_len); frag->len -= hdr_len; } static void update_protocol_header_lengths(struct net_pkt *pkt, u16_t size) { net_pkt_set_ip_hdr_len(pkt, NET_IPV6H_LEN); NET_IPV6_HDR(pkt)->len[0] = (size - NET_IPV6H_LEN) >> 8; NET_IPV6_HDR(pkt)->len[1] = (u8_t) (size - NET_IPV6H_LEN); if (NET_IPV6_HDR(pkt)->nexthdr == IPPROTO_UDP) { struct net_udp_hdr hdr, *udp_hdr; udp_hdr = net_udp_get_hdr(pkt, &hdr); NET_ASSERT(udp_hdr); udp_hdr->len = htons(size - NET_IPV6H_LEN); net_udp_set_hdr(pkt, udp_hdr); } } static inline void clear_reass_cache(u16_t size, u16_t tag) { u8_t i; for (i = 0; i < REASS_CACHE_SIZE; i++) { if (!(cache[i].size == size && cache[i].tag == tag)) { continue; } if (cache[i].pkt) { net_pkt_unref(cache[i].pkt); } cache[i].pkt = NULL; cache[i].size = 0; cache[i].tag = 0; cache[i].used = false; k_delayed_work_cancel(&cache[i].timer); } } /** * If the reassembly not completed within reassembly timeout discard * the whole packet. */ static void reass_timeout(struct k_work *work) { struct frag_cache *cache = CONTAINER_OF(work, struct frag_cache, timer); if (cache->pkt) { net_pkt_unref(cache->pkt); } cache->pkt = NULL; cache->size = 0; cache->tag = 0; cache->used = false; } /** * Upon reception of first fragment with respective of size and tag * create a new cache. If number of unused cache are out then * discard the fragments. */ static inline struct frag_cache *set_reass_cache(struct net_pkt *pkt, u16_t size, u16_t tag) { int i; for (i = 0; i < REASS_CACHE_SIZE; i++) { if (cache[i].used) { continue; } cache[i].pkt = pkt; cache[i].size = size; cache[i].tag = tag; cache[i].used = true; k_delayed_work_init(&cache[i].timer, reass_timeout); k_delayed_work_submit(&cache[i].timer, FRAG_REASSEMBLY_TIMEOUT); return &cache[i]; } return NULL; } /** * Return cache if it matches with size and tag of stored caches, * otherwise return NULL. */ static inline struct frag_cache *get_reass_cache(u16_t size, u16_t tag) { u8_t i; for (i = 0; i < REASS_CACHE_SIZE; i++) { if (cache[i].used) { if (cache[i].size == size && cache[i].tag == tag) { return &cache[i]; } } } return NULL; } /* Helper function to write fragment data to Rx packet based on offset. */ static inline bool copy_frag(struct net_pkt *pkt, struct net_buf *frag, u16_t offset) { struct net_buf *input = frag; struct net_buf *write; u16_t pos = offset; write = pkt->frags; while (input) { write = net_pkt_write(pkt, write, pos, &pos, input->len, input->data, NET_6LO_RX_PKT_TIMEOUT); if (!write && pos == 0xffff) { /* Free the new bufs we tried to get, we need to discard * the whole fragment chain. */ net_pkt_frag_unref(pkt->frags); pkt->frags = NULL; return false; } input = input->frags; } net_pkt_frag_unref(frag); return true; } /** * Parse size and tag from the fragment, check if we have any cache * related to it. If not create a new cache. * Remove the fragmentation header and uncompress IPv6 and related headers. * Cache Rx part of fragment along with data buf for the first fragment * in the cache, remaining fragments just cache data fragment, unref * RX pkt. So in both the cases caller can assume packet is consumed. */ static inline enum net_verdict add_frag_to_cache(struct net_pkt *pkt, bool first) { struct frag_cache *cache; struct net_buf *frag; u16_t size; u16_t tag; u16_t offset = 0; u8_t pos = 0; /* Parse total size of packet */ size = get_datagram_size(pkt->frags->data); pos += NET_6LO_FRAG_DATAGRAM_SIZE_LEN; /* Parse the datagram tag */ tag = get_datagram_tag(pkt->frags->data + pos); pos += NET_6LO_FRAG_DATAGRAM_OFFSET_LEN; if (!first) { offset = ((u16_t)pkt->frags->data[pos]) << 3; pos++; } /* Remove frag header and update data */ remove_frag_header(pkt->frags, pos); /* Uncompress the IP headers */ if (first && !net_6lo_uncompress(pkt)) { NET_ERR("Could not uncompress first frag's 6lo hdr"); clear_reass_cache(size, tag); return NET_DROP; } /* If there are no fragments in the cache means this frag * is the first one. So cache Rx pkt otherwise not. * Write data fragment data to cached Rx based on offset parameter. * (Detach data fragment from incoming Rx and copy that data). */ frag = pkt->frags; pkt->frags = NULL; cache = get_reass_cache(size, tag); if (!cache) { cache = set_reass_cache(pkt, size, tag); if (!cache) { NET_ERR("Could not get a cache entry"); pkt->frags = frag; return NET_DROP; } /* If write failed, then attach frag back to incoming packet * and return NET_DROP, caller will take care of freeing it. */ if (!copy_frag(cache->pkt, frag, offset)) { pkt->frags = frag; /* Initialize to NULL to prevent duble free. It's only * needed here because this is the first fragment. */ cache->pkt = NULL; clear_reass_cache(size, tag); NET_ERR("Copying frag failed"); return NET_DROP; } NET_DBG("packet inserted into cache"); return NET_OK; } /* Add data packet to reassembly packet */ if (!copy_frag(cache->pkt, frag, offset)) { pkt->frags = frag; clear_reass_cache(size, tag); return NET_DROP; } /* Check if all the fragments are received or not */ if (net_pkt_get_len(cache->pkt) == size) { /* Assign frags back to input packet. */ pkt->frags = cache->pkt->frags; cache->pkt->frags = NULL; /* Lengths are elided in compression, so calculate it. */ update_protocol_header_lengths(pkt, cache->size); /* Once reassemble is done, cache is no longer needed. */ clear_reass_cache(size, tag); NET_DBG("All fragments received and reassembled"); return NET_CONTINUE; } /* Unref Rx part of original packet */ net_pkt_unref(pkt); return NET_OK; } enum net_verdict ieee802154_reassemble(struct net_pkt *pkt) { if (!pkt || !pkt->frags) { NET_ERR("Nothing to reassemble"); return NET_DROP; } switch (pkt->frags->data[0] & 0xF0) { case NET_6LO_DISPATCH_FRAG1: /* First fragment with IP headers */ return add_frag_to_cache(pkt, true); case NET_6LO_DISPATCH_FRAGN: /* Further fragments */ return add_frag_to_cache(pkt, false); default: NET_DBG("No frag dispatch (%02x)", pkt->frags->data[0]); /* Received unfragmented packet, uncompress */ if (net_6lo_uncompress(pkt)) { return NET_CONTINUE; } NET_ERR("Could not uncompress. Bogus packet?"); } return NET_DROP; } |