mbed-os/source/Common_Protocols/ipv6_fragmentation.c

741 lines
28 KiB
C
Raw Normal View History

/*
Squashed 'features/nanostack/sal-stack-nanostack/' changes from 82bbdcc..4a188ea 4a188ea Merge branch 'release_internal' into release_external 27ba18e removed ws resources from nanostack. b693eae Merge pull request #1778 from ARMmbed/ws_bbr_update 543dd1e Renamed symbol to micro seconds. ab5d98a Added ND proxy support to ws border router f7c71c0 Remove new unittests from release external. ac55c15 Merge branch 'release_internal' into release_external f9b9e47 Fix GCC_ARM/ARM compiler warnings (#1777) 40efd39 Coverity tool reported error fix (#1776) b063457 Merge pull request #1775 from ARMmbed/mac_header_trace_fix b1fd538 Removed Debug trace support from common mac header class 90a36a6 coverity reported error fixes. (#1770) 9644114 Merge pull request #1774 from ARMmbed/debug_trace_fix 1e490f0 Removed debug trace which could be called at interrupt routine 1373a5f Clarify parameter name in phy_csma_params_t (#1773) 4a32a6c fixes for coverity tool reported issues. (#1772) 545d262 Update copyright year (#1771) 5ae07da address error handling fix. (#1769) 13c1ceb Clean multicast address when interface is shutdown 88bb7cc correct interface passed to address compare. (#1768) 53949a4 Thread device registers new address upon receipt of error notification. (#1767) 1653b06 replace ETX calculation for Wi-SUN 745c1dd refactor RPL dis sending 6e1cc58 Merge pull request #1764 from ARMmbed/mle_purge_fix 0b920d8 Fixed broken neighbor purge from list a6f3a0d Thread network data handling refactored (#1762) 87cc2c9 Merge pull request #1763 from ARMmbed/mle_fixs 944f9ca Fix broken MLE neigbor list write operation. 833e740 updated address error payload (#1761) 2565170 Add CBOR parsing to simpleenrolment message e38c70f fix duplicate address detection (#1760) 7648c1c Scope updated to three and prefix added to destination address. (#1758) 5612e08 DUA.ntf updates (#1752) 708bb3b pbbr stop removed to fix failing tests. (#1757) 1444c2f Add POC code for storing multicast address 684efef Fix MLR message handling b691b78 context id update (#1753) 5635089 fix key sequence issues when keys are rotated 74db027 fix for sleepy node poll. (#1750) 49adf2e Link entry for neighbour (#1749) 5bad8ee Correct MAC neighbour table sizes for Thread (#1748) 3858eb0 valgrind reported error: fixed uninitialised variables. (#1747) 24ef520 Update licenses in new WS files (#1745) 77dccdf valgrind tool reported error fix: (#1746) 9ac47a8 Adjust WS build configurations (#1744) c593e2b DUA.ntf to MTD child implemented (#1735) 3f5c6c1 Update missing SPDX identifiers to test files (#1743) 130fec4 Fix GCC_ARM linker error with some Thread builds (#1742) 65574a4 Merge pull request #1741 from ARMmbed/merge_koli_to_master 2dc41c0 Merge remote-tracking branch 'origin/koli' 2fc10b5 Put smarter dag max rank increment 2048. 0 affect a lot of troubles. d311040 RPL print update, nud and aneighbour update f9b23d4 Fix compiler warnings (#1740) 5a1f295 Merge pull request #1738 from ARMmbed/master_merge_to_koli abee481 Fix merge compile problems 2c77c37 Merge branch 'master' into koli 32fe4b8 Increase too small maximum root path length (#1737) 03bc696 Fix LLC unit test's 3702f57 LLC data indication and fhss channel set update 45905fa Update DHCP service memory allocation (#1734) 9c9b9e3 Fix crash which will happen if address registration fail and operation is triggered again by NULL pointer. cb83216 Enabled proper wi-sun security level 6. 7990fa9 Fix data for 6lowpan when mle is disabled. 501a612 Wi-sun trigle timer update: 47619c4 Function name code conventions fix. 1350bc5 mac_neighbor_info() macro defned for simplify code 41b84e4 Added check when weighting rule update must do network scan. 0215058 Fix ns_dyn_mem_init stub 33398ef Added Neighbor connected and trusted state update for wisun 24aa802 Integrate Mac neighbour table to Thread and 6Lowpan code 37c6342 Mac neighbour table update 7bb978e Thread Neighbor class update 26dd252 Removed almost duplicate mle entry discover for ll64 7a0aaa5 Rename some parameters which will help integration to new neighbor table. 3f905a1 Update nsdynmemLIB unit test stub (#1730) 2bd3e91 Return 0 instead of -1 on null pointer (#1728) fde5104 Function parameter and name refactor. e0f7dcf Updates to thread extension bootstrap (#1714) 13b3b05 Thread Neighbor and MLE API update 49ccb9d Removed threadNeighbour flag from mle_table_entry_t. e142eec Removed unused holdTime parameter from mle_table_entry. c8a99cb MLE service Update and integrate new features 726b08b Adjust NULL string tracing (#1726) 66636b8 Merge pull request #1724 from ARMmbed/IOTTHD-2537 2710836 Enable security to wi-sun with test security key. 8994bb2 MAC Security update c56886f MAC: Fixed frame length when calling FHSS TX conditions 4eb5567 CCM & AES update for support multiple user and context. 7544ef3 added response to confirmable unicast. (#1722) a8a5a90 duplicate address detection fix (#1720) 721c0b0 MLE service releases CCM library in error case (#1719) d0467a0 Fix stack overflow (#1718) 7ea9001 Added support for multiple registrations in one message 0a36af3 WS IE library update and LLC data confirmation update 4352709 MAC enhanced ACK RX and TX support 0b7f9fe Update Thread child id request timeout handling (#1715) 3e8df0a added neighbor advertisement sending after dad complete. (#1713) a46a363 updated multicast address registration timer (#1711) 7c6c482 Added target EID tlv to Dua response 2d3aff9 Delete parent mle-entry in case of parent lost or updated (#1707) b43db5f duplicate address handling (#1709) cb54705 Address generation changed to slaac based generation (#1708) ee0306b changed coap message type to non-confirmable. (#1706) 776e0e5 Merge pull request #1705 from ARMmbed/IOTTHD-2531 d92b2f9 Thread advertisement adjustment (#1703) f8d3d67 WS: Learn parent unicast dwell time bd88fa0 WS Lib: Fixed byte order of fixed channel 1f9162d Merge pull request #1704 from ARMmbed/IOTTHD-2484 2e15944 FHSS unit tests: Datarate storage removed e64bd19 FHSS: Implemented checking TX time 0ba4b9a Merge pull request #1700 from ARMmbed/IOTTHD-2483 1adb52b coap message type decided based on destination (#1702) d1cf42d multicast handling updated(#1701) 44110a1 Thread bootstrap improvements (#1699) 34c0df9 FHSS unit tests: fixed tests after TX slot update ddd7e92 FHSS: Calculate number of TX slots using defined slot length eaf35d2 Lowered Thread advertisement speedup threshold values (#1691) b67f2d8 Reset children when no response from parent (#1698) 3569c8a Mleid bbr fix (#1697) 1fc81fc Thread device learns weighting from advertisement (#1696) ba98835 Merge pull request #1689 from ARMmbed/IOTTHD-2475 28307de Randomize channel used in fixed channel 3412f4a WS bootstrap: Moved calculating and setting FHSS hop to own function 51a498b reset Dodag if global address is lost. c0e8cae Merge branch 'koli' into IOTTHD-2475 0ec37a6 Count the amount of configuration solicits c8f5c8d FHSS unit tests: Updated FHSS config tests. 490384f FHSS unit tests: Test TX/RX slots 4fdbc09 FHSS: Calculate hop count using RPL rank 8f194f8 FHSS: Implemented TX slot calculation b06b58a FHSS: Implemented setting hop count API d73d210 FHSS: Moved own hop to common FHSS structure caafff0 FHSS: Check TX slot for unicast frames 7406149 Merge pull request #1684 from ARMmbed/IOTTHD-2476 cc5f6d8 FHSS: validate received synch info. c48f0ef Merge pull request #1687 from ARMmbed/disable_channel_traces d5d1a13 Fixed trickle configuration for Discovery eef3bd7 FHSS: Disable FHSS channel debugs for WS fc74a31 Fixed unit test for support extensions. e803974 FHSS and Mac extension enable update. 4859f16 MAC RF extension enable e73e9b2 Merge pull request #1682 from ARMmbed/IOTTHD-2460 03f7105 fixed EAPOL parent selection cbdd2a9 Fixed set channel unit test ce1ab34 FHSS: Do not change channel during asynch transmissions c0456a3 Defined own structure to give CSMA backoff symbol time and cca mode. dff1e7d Merge pull request #1677 from ARMmbed/merge_MtoK 34dcbb6 Merge pull request #1674 from ARMmbed/IOTTHD-2443 3f9b34d Merge branch 'master' into koli f578c26 Updated border router configuration 1ce2385 RF PHY extension update 32db0cd FHSS: Learn FHSS configuration from parent PA config 5974344 Merge pull request #1670 from ARMmbed/IOTTHD-2449 82a8bca FHSS: support FHSS created by application 67f578a Calculated RSL from neighbours 4aa6890 Merge pull request #1664 from ARMmbed/IOTTHD-2442 65e2fad Fixed uninitialized parameter use at rpl instance allocate. 4767dd5 Merge pull request #1666 from ARMmbed/fhss_is_bc_flag_fix 6ffc81a FHSS unit tests: Fixed tests 14e1597 Fixed missing TX ime calculation for pre-builded MAC packet send. 03f447a FHSS: Do not use broadcast queue when fixed channel 40f3685 FHSS: do not push asynch messages in broadcast queue 45daec9 FHSS: FHSS is on BC channel by default 2dbc92c FHSS: Remove failed handle after successful transmission a152f45 Merge branch 'koli' into IOTTHD-2442 ea583c6 FHSS: Fixed updating broadcast synch c3aa7d1 Select correct parent based on network size and route cost 7bf3994 Fixed Code style warning. 2e1f32f Fixed WS_NESTED_IE information discover return length check. 445745a LLC Neighbor, Mac packet rebuild, mac timer update and phy Extension. 6cb78ff Distribute and learn network size bc4f46f Merge pull request #1656 from ARMmbed/IOTTHD-2441 7f94971 FHSS: Added random timeout before polling TX queue after channel change b6e40af Merge pull request #1653 from ARMmbed/IOTTHD-2366 b28fcaa FHSS unit tests: Test failed TX allocation fbe7795 Modified neighbour processing b2cff91 FHSS: Use channel retries with WS 9b11201 FHSS: Moved TX fail handler in FHSS common 9fff108 Merge pull request #1650 from ARMmbed/merge_MtoK2 04797c2 Detect border router connection lost 5227398 Merge branch 'master' into koli c0e8673 Merge pull request #1647 from ARMmbed/IOTTHD-2405 b7428e0 Merge pull request #1646 from ARMmbed/IOTTHD-2404 04449e8 FHSS unit tests: Test tx conditions and tx done callbacks baaec35 Merge pull request #1644 from ARMmbed/ws_start_bc 69c1483 FHSS: Added excluded channels in neighbor table 92acbd2 WS: fixed merge conflicts 4e01969 Merge branch 'koli' into ws_start_bc 97200b3 Added version number increase in border router 27c8b65 FHSS: unit test update 6eb1255 FHSS: WS bootstrap to start broadcast schedule 659a1c4 FHSS: Write BT-IE 64f31b1 Merge pull request #1635 from ARMmbed/IOTTHD-2217 7706f2e FHSS: Do not return to RX channel when configured fixed channel 908eb17 Change RPL dis destination address to RPL address 666dbbf FHSS: Start broadcast schedule only if device is border router 98f9991 FHSS: Set neighbor info get function 126275b Merge pull request #1632 from ARMmbed/IOTTHD-2402 7fa2c2b WS: Learn channel function from parent 3980870 WS: Enable FHSS in bootstrap ff72d95 Merge pull request #1629 from ARMmbed/cppcheck_fixes 85f1345 MAC: Check length pointer in ie vector length validate 672c4ef FHSS: Removed unnecessary fhss_api NULL e5637fd FHSS: Initialize next_channel in broadcast channel calculation cab9849 Send address registration to all PC parents (#1618) 5417936 Merge pull request #1623 from ARMmbed/mergeMtoK3 ad41972 Wait RPL to be ready before bootstrap is ready a314db0 Merge branch 'master' into koli f6ac06f Fhss ut improvements (#1622) 60a3b0f Fixed ETX update at LLC layer. bb152ce Merge pull request #1617 from ARMmbed/IOTTHD-2321 49c213a FHSS unit tests: Added more tx handle tests d5cc85a FHSS unit tests: Test removing parent info and get neighbor info 2d01008 FHSS unit tests: test setting parent BC info 681dedd Merge pull request #1613 from ARMmbed/IOTTHD-2320 7e59035 Fix broken unit test build. 6be455f WS ETX update 0efc062 ETX service update e5aba03 WS PAN advert validation update 802ce3e WS bootstrap update be296cd WS IE lib update cfa5fd9 FHSS: Store parent broadcast info pointer d0171be FHSS: Implemented get neighbor info and set/remove parent info callbacks 686ad9c Fix acynch --> asynch b4059b8 WS neighbor class integrated to use new FHSS structures c1b0d96 Merge pull request #1610 from ARMmbed/IOTTHD-2313 3d98860 FHSS: Implemented WS timing/schedule info structure f96db0c Merge pull request #1607 from ARMmbed/merge_MtoK 72bb71f Fixed missing params after merging master to koli d119df9 Merge branch 'master' into koli 8166585 Merge pull request #1602 from ARMmbed/IOTTHD-2311 90fbb45 Fixed LLC data unit test and add new functions to test 94e516f Integrated new WS IE header and payload API's c553e8c WS ie library read update 7db0938 fixed mac_mlme valgrind errors. 4e2a743 Fixed thread test some of. 2680dd6 Fixed unstable unit test verified by valgrind. 18eda56 Merge pull request #1605 from ARMmbed/fhss_ut_valgrind_fix eb96eae FHSS unit tests: fixed uninitialized varnings 27b7864 Fixed mac helper stub issues. a88590f FHSS unit tests: Test tr51cf with excluded channels 3c9356a FHSS unit tests: Updated channel function tests 3a562d3 FHSS: Added excluded channels in channel function interface b9606c9 FHSS: Static channel functions added 695e64c Merge pull request #1601 from ARMmbed/IOTTHD-2214 f973986 FHSS unit tests: Implemented FHSS common unit tests 2188af4 FHSS unit tests: Removed FHSS common functions from FHSS e6bac6a Rename and refactor structures and function names. b037628 Fixed unit test and stubs 8153fe3 Fixed Asynch request missing msdu_handle set at LLC. 1af46d8 WS bootstrap update c771df0 LLC update UTT / BT information to ws neighbor and refresh mac neighbor a43f6ad Mac helper API update: 14792db Ws neighbor hoping storage init integrated to interface init. ae7945d Wi-sun neighbor hopping infor storage API definition 8f5c0bf FHSS: Removed fhss_beacon, fhss_beacon_tasklet and fhss_mac_interface 1e94358 Fixed unitest build. 3b9a4b0 Base Integration for Mac neighbor table to WS 7729e20 Mac neighbor table integrate to interface structure 2c6f30a Addede NULL check some of mac neighbor table check. ed45fda Mac neighbor table unit test's. 0f85841 Added mac_neighbor_table stub's fc4c97b Generic mesh neighbor table class. 4b81978 Process advertisement solicit and process response (#1592) 73c6bc1 Merge pull request #1589 from ARMmbed/IOTTHD-2218 057b6ec Omit NA sending in ARO success cases. Use ACK instead. (#1587) 4686668 ws_management_api unit tests (#1591) 9ae6511 FHSS unit tests: Implemented FHSS WS tests 8147217 Added trickle for Advertisement and processing of response (#1586) c52b039 Merge pull request #1584 from ARMmbed/IOTTHD-2055 ac58048 FHSS: WS FHSS to use pre-set TX time feeee0f FHSS: Synch callback to write ufsi 3e8112b Merge pull request #1578 from ARMmbed/IOTTHD-2286 4cd6c49 Merge pull request #1579 from ARMmbed/fhss_bug_fix 296a2f1 FHSS: Fixed scramble table bug 80e4270 FHSS: Added vendor defined CF support 24f418c Merge pull request #1575 from ARMmbed/IOTTHD-2288 a849c8d FHSS unit tests: Update FHSS tests 83bd90e FHSS: fhss_struct defined static 04ae3c3 FHSS: separated is_bc_callback to FHSS and WS 59fe586 FHSS: removed allocation of scramble table 1ca1d0b FHSS: Continued fhss_structure cleaning 036e19c Api Updates to LLC, ws_ie_lib and ws_info 192007b Fixed MAC Asynch message send and header build bug. e0494b7 WS address registration callback for SLAAC (#1568) 93b7e32 FHSS: superframe handlers separated ff66ae9 FHSS: Separate FHSS and WS parameters - Configuration structs moved 6ace69b Merge pull request #1573 from ARMmbed/fhss_cleaning1 07d4089 FHSS: Cleaning. Static functions. Separated tx fail and retry callbacks 8b9d39b Merge pull request #1571 from ARMmbed/fhss_callback_update 316b007 FHSS: removed unnecessary state set handler db1ded0 WS LLC data service enabled to WS bootstrap. c326e4a Fixed MPX header size calculation to MPX data request. fa2d7d0 Removed Adaptation interface depency from LLC away. 4356ae8 separated BBR to own file (#1567) 75d93f8 Merge pull request #1564 from ARMmbed/IOTTHD-2213 19c4be6 FHSS: Separated tx done cb for FHSS and WS 3e326d1 FHSS: separated synch info write for FHSS and WS 15d4465 Function and parameter rename. 5af7992 Unit test for llc data service. ffb3639 Fixed stubs. 3f66f6a LLC delete added for test purpose b48cda0 Impelement LLC reset 47458c8 WS IE Lib unit test and fixed mac ie lib stub 6b6e509 MPX header module unit test. 55309e8 ws_llc_asynch_request() API update 5d9a379 Wisun spesific IE elemts write operation library. c718985 FHSS: Calculate destination slot and ufsi b4e1616 Merge pull request #1561 from ARMmbed/IOTTHD-2172 096aeef ws create interface and start rpl for poc (#1562) 77c851a FHSS: Added BSI in ws configuration 7d4a2f8 FHSS: Start broadcast schedule when bc intervals are known e55ec8a FHSS: Prevent changing unicast channel when broadcast is active b60b113 FHSS: Broadcast handler implemented 9bf1d1f WS Bootstrap skeleton (#1559) 3fb6390 Merge pull request #1560 from ARMmbed/mergeMtoK2 b78a370 Merge branch 'master' into koli 8b43c6e Merge pull request #1555 from ARMmbed/IOTTHD-2215 78314e8 FHSS: WS FHSS enable implemented d04b818 FHSS: Compensation callback to return timeout value 472becb FHSS: FHSS configuration to union b5f9e01 FHSS unit tests: Fixed after FHSS enable/down updates 2cbe6dc FHSS: Moved FHSS reset and down to FHSS module 4719187 FHSS: Separated FHSS enable functions for FHSS and WS FHSS f4548d8 FHSS: Implemented WS configuration structure 7ace04b Initial commit for ws_bootstrap (#1556) 9e46cf9 Added malformed message check to nested IE discovery. 7f4c184 Added support for write and discover Nested IE elements. e3d8150 SW MAC Asynch data request support 9a98177 Merge pull request #1552 from ARMmbed/cf_update 30075dd FHSS: TR51 channel function updated (bug in the spec?) 6b04d5a Merge pull request #1548 from ARMmbed/IOTTHD-2093 4249fad Integrated ws llc and mpx features. a49bf11 Fix Const pointer wrong use. 3e1972f WS LLC module implementation base 957f270 MPX frame support 93f0ed7 MAC ie library update: b2198b3 MPX support Integrate 2531c49 WS LLC and MAC MPX API definition and IE types definition. a5493d2 FHSS: Do not send TX failure if TX in progress in channel change 958991a FHSS: Few FHSS functions to static 7237796 Merge pull request #1545 from ARMmbed/IOTTHD-2097 f794990 FHSS: Implemented destination channel change in TX handle callback d2957a1 FHSS: fhss tx conditions callbacks added c1e7300 FHSS: hard coded channel function 1d54c7d FHSS: fhss tx handle callbacks added 0cc1889 FHSS: Moved external API set to fhss and fhss_ws c6cfb6c Merge pull request #1544 from ARMmbed/fhss_headers_refactored 1b4e203 FHSS: refactoring FHSS header files e047723 Merge pull request #1541 from ARMmbed/IOTTHD-2171 5d98e3e FHSS: Added support for TR51 channel function c8a9c7c FHSS: function to calculate nearest (larger or equal) prime number 5e70ec1 FHSS: ws struct to hold ws specific parameters f2d7558 FHSS unit tests: Fixed tests 149f17b FHSS: Own create api for WS e19de33 FHSS: WS to use Direct Hash channel function fb99776 FHSS: Some cleaning 7a21ca1 FHSS: Added common superframe handler a881580 FHSS: Added ws handler for FHSS state set f7ab36c FHSS: Created FHSS state set handler f6c61fc Added Copyrights c445590 MAC IE element library 6e94b08 MCPS-Data-REQ API update 38aedc5 Remove commented code away. 663fd3e Fixed missing doxygen tags. b9f7bf4 MAC functional updates and fix's: 2cef8ac Merge pull request #1538 from ARMmbed/mergeMtoK 321f700 Merge pull request #1534 from ARMmbed/IOTTHD-2047 146d7c4 Merge branch 'master' into koli 100db21 FHSS unit test: Added FHSS common to unit test makefile 37425ca FHSS: Separated common FHSS functions to own module 59b7b76 FHSS: Removed massive trace flags 60dfccb FHSS: Removed unnecessary channel functions e456662 FHSS: Removed scramble table generation from fhss enable b643622 Merge pull request #1532 from ARMmbed/IOTTHD-2049 b741f98 MCPS Data request API update. c329f61 MAC API update and internal update 55861f3 FHSS unit tests: Fixed tests after API change f9a31e9 FHSS API: Added callback to read synch info f94b2ab FHSS: FHSS to use synch info callback 6830fd3 FHSS API: Added synch info write callback ad5a1e1 Merge pull request #1527 from ARMmbed/IOTTHD-2095 3ed145f FHSS API: Added new configurations 0f9f0bb FHSS: Platform API to support multiple simultaneous timeouts bf65e9c Unit tests: Added PHY extension stub 322167e MAC: RX time get function implemented 23d3a58 MAC: Created a function to set TX time 2384c33 PHY API: extension to support timestamp 209e49a Merge pull request #1528 from ARMmbed/IOTTHD-2046 89a85a8 FHSS: Added copyright headers, comments etc. 5d0f44e Unit tests: DH1CF channel function tests implemented fbd5568 FHSS: DH1CF channel functions implemented cf03db2 FHSS: Implemented tr51 channel functions 00af7f1 Unit tests: Created channel function unit tests git-subtree-dir: features/nanostack/sal-stack-nanostack git-subtree-split: 4a188ea8231c0539eaf2b2859d89f2bcdcb47d8b
2018-08-08 13:51:52 +00:00
* Copyright (c) 2015-2018, Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* IPv6 fragmentation and defragmentation
*
* (Could fairly easily be modified to also do IPv4)
*
* References:
*
* RFC 815 IP Datagram Reassembly Algorithms
* RFC 3168 The Addition of Explicit Congestion Notification (ECN) to IP
* RFC 6040 Tunnelling of Explicit Congestion Notification
* RFC 6660 Encoding Three Pre-Congestion Notification (PCN) States in the
* IP Header Using a Single Diffserv Codepoint (DSCP)
Squashed 'features/nanostack/sal-stack-nanostack/' changes from 43c7ec2..82bbdcc 82bbdcc Merge branch 'release_internal' into release_external 7c7568d address error sent when matching entry found. (#1695) 0860ae7 Keep parent mle_entry in child_update_req timeout (#1693) 92044d6 Changed BBR router upgrade status (#1661) e4a9657 Compiler warning clean-up (#1690) a573bc4 Detection of duplicate address entries updated (#1686) f28cce8 pbbr route handling updated (#1681) 389fab0 MLE parent request timeout fixes (#1676) feb1784 Remove fragment/PMTU handling dropped by RFC 8200 073f7e8 entry keep alive flag set to true if entry already exists. (#1675) 1584759 Extra Thread-specific address check handling 934687b Tighten core "for us" check for unicast addresses 58a60e7 address query logic implemented(#1667) c25df78 Fix for Router advertisement process in REED (#1658) f8b227f Thread DHCP address allocation fix (#1657) 41ca387 removed the dependence of AE message relay on DUA prefix (#1655) 9e7e9e0 REED parser refactoring for MLE advertisement (#1654) 4fda90e Clear data request flag in child_id_req timeout (#1649) 5af4a39 updated mleid rloc sync in leader. (#1645) 564d3fe Added filter callback for MLE messages (#1638) 9f85599 Security bits handled correctly (#1642) 9e6c3a1 Update Thread MLE neighbor list cleanup, attempt #2 (#1640) 1060c34 file version checking handled after other errors. (#1641) 9c6b876 added nvm support for leader router id allocation. (#1626) 4f5e97d Revert "Update Thread MLE neighbor list cleanup (#1637)" (#1639) a20a0ba Update Thread MLE neighbor list cleanup (#1637) 32cfb6b Fix to partition selection rules (#1633) 0d655ed Update MLE receiver in Child ID req timeout error (#1634) 2378dad Discard late Child ID responses (#1630) 3e8b4ce Multicast registration time fix (#1631) f5530b1 Thread key ID change during attach (#1627) 1b7fdc0 Skip discovery request if MLE table is full (#1625) 4022ad9 Add new test API for clearing Thread stack values (#1624) ffb47a2 additional reed advertisement (#1609) cfb1e81 Thread tx failure handling update (#1620) 15846ed refactored network data sync as leader (#1615) a5c67fd data request resent if leader after resync does not receive data response.(#1614) f8a39da documentation fix: (#1608) b69ba04 Allow MLE entry forced saving to MAC (#1600) 0e85099 Update router id sequence number in router sync (#1594) f2cd4cf Read the short addr as 16-bit value before calling mac api (#1585) 912cf02 purge old partition data when child id response is received (#1588) 648ef89 Remove unnecessary call to mac (#1583) 09cff0e Fix uninitialised memory defect found by valgrind (#1582) a1acffc Update Thread publishing during address change (#1581) d2ddb1a Send new a/sd immediately after address change (#1576) c18d1fd added check for parent info before processing child update (#1577) 96e86b6 implemented Sleepy End Device child update requests. (#1570) 33cb7cb Flag check updated. (#1572) 9f8e7f2 Merge pull request #1566 from ARMmbed/ut_fix 565da92 Update socket_api_test.cpp 59545f9 Update socket_api_test.cpp 09414e1 ML-EID is now added to address registration tlv. (#1565) beddb01 unittest fix for newer release of cpputest git-subtree-dir: features/nanostack/sal-stack-nanostack git-subtree-split: 82bbdcc7ec63355fc19bf2c6054716584742b99b
2018-05-11 06:52:09 +00:00
* RFC 8200 Internet Protocol, Version 6 (IPv6) Specification
* RFC 8201 Path MTU Discovery for IP version 6
*/
#include "nsconfig.h"
#include "ns_types.h"
#include "ns_list.h"
#include "ns_trace.h"
#include "common_functions.h"
#include "nsdynmemLIB.h"
#include <string.h>
#include "ns_trace.h"
#include "Core/include/socket.h"
#include "NWK_INTERFACE/Include/protocol.h"
#include "Common_Protocols/ip.h"
#include "Common_Protocols/ipv6.h"
#include "Common_Protocols/icmpv6.h"
#include "Common_Protocols/ipv6_fragmentation.h"
#include "NWK_INTERFACE/Include/protocol_stats.h"
#define TRACE_GROUP "Ifrg"
/* FRAGMENT REASSEMBLY
*
* Allow fragment RX to be disabled for really constrained systems.
* This would violate RFC 2460 and RFC 6434 - all IPv6 nodes must be able to
* process fragment headers and reassemble 1500-octet datagrams.
*/
#ifndef NO_IP_FRAGMENT_RX
static uint16_t ipv6_frag_mru = IPV6_FRAG_MRU;
typedef struct ip_fragmented_datagram {
uint8_t age;
bool discard; /* Set to ignore all future fragments (and not send Time Exceeded) */
bool had_last;
int8_t ecn;
uint32_t id;
uint16_t fragmentable; /* Offset in buf->buf[] of fragmentable part */
uint16_t first_hole; /* Offset of first hole (relative to fragmentable part) */
buffer_t *buf;
ns_list_link_t link;
} ip_fragmented_datagram_t;
/* We reassemble into the datagram buffer in basically the style of RFC 815 */
/* An 6-byte hole descriptor is placed directly in buffer holes */
/* We link them them by buffer offset (relative to start of fragmentable section) */
/* Note the possible need to align means we can't use more than 7 bytes */
typedef struct hole {
uint16_t first;
uint16_t last;
uint16_t next;
} hole_t;
/* Given the offset of a hole in the datagram buffer, return an aligned pointer
* to put a hole_t in it. We assume a "normal" platform requiring 2-byte
* alignment for hole_t, and letting us manipulate uintptr_t in the conventional
* fashion.
*/
static hole_t *hole_pointer(const ip_fragmented_datagram_t *dgram, uint16_t offset)
{
uintptr_t ptr = (uintptr_t)(dgram->buf->buf + dgram->fragmentable + offset);
return (hole_t *)((ptr + 1) & ~(uintptr_t) 1);
}
static NS_LIST_DEFINE(frag_list, ip_fragmented_datagram_t, link);
/* Maximum time to hold fragments in seconds */
#define FRAG_TTL 60
/* How many partially-assembled datagrams we will hold */
#define MAX_FRAG_DATAGRAMS 4
/* Dummy negative ECN value used during assembly */
#define IP_ECN__ILLEGAL (-1)
/* RFC 5722 - discard already-received *and future* fragments */
static void invalidate_datagram(ip_fragmented_datagram_t *dgram)
{
// Would like to free the buffer here, but it contains the
// source and destination address we need to match the datagram entry.
dgram->discard = true;
}
static void free_datagram(ip_fragmented_datagram_t *dgram)
{
ns_list_remove(&frag_list, dgram);
if (dgram->buf) {
buffer_free(dgram->buf);
}
ns_dyn_mem_free(dgram);
}
/* We would be in trouble if last fragment is < 8 bytes, and we didn't have
* room for the hole descriptor. Avoid a problem by ensuring that we always
* allocate a multiple-of-8 reassembly buffer.
*/
uint16_t ipv6_frag_set_mru(uint16_t frag_mru)
{
frag_mru = (frag_mru + 7) & ~ UINT16_C(7);
if (frag_mru < IPV6_MIN_FRAG_MRU) {
frag_mru = (IPV6_MIN_FRAG_MRU + 7) & ~ UINT16_C(7);
}
if (ipv6_frag_mru != frag_mru) {
/* I don't want to worry about the complications of changing MRU while
* we've got ongoing reassembly. Simplest just to drop any pending.
*/
ns_list_foreach_safe(ip_fragmented_datagram_t, dgram, &frag_list) {
free_datagram(dgram);
}
ipv6_frag_mru = frag_mru;
}
return ipv6_frag_mru;
}
void ipv6_frag_timer(uint8_t secs)
{
ns_list_foreach_safe(ip_fragmented_datagram_t, dgram, &frag_list) {
if ((dgram->age += secs) > FRAG_TTL) {
uint16_t first_hole = dgram->first_hole;
/* If we've received the first fragment, can send "time exceeded" */
if (first_hole != 0 && !dgram->discard) {
/* Take as much as we've got, up to first hole; icmpv6_error will limit to min MTU */
dgram->buf->buf_end = dgram->fragmentable + first_hole;
/* Fill in IP header length */
common_write_16_bit(buffer_data_length(dgram->buf) - 40, buffer_data_pointer(dgram->buf) + 4);
buffer_t *err = icmpv6_error(dgram->buf, NULL, ICMPV6_TYPE_ERROR_TIME_EXCEEDED, ICMPV6_CODE_TME_EXCD_FRG_REASS_TME_EXCD, 0);
protocol_push(err);
dgram->buf = NULL;
}
free_datagram(dgram);
}
}
}
static void delete_hole(ip_fragmented_datagram_t *dgram, uint16_t hole, uint16_t *prev_ptr)
{
hole_t *hole_ptr = hole_pointer(dgram, hole);
*prev_ptr = hole_ptr->next;
}
static hole_t *create_hole(ip_fragmented_datagram_t *dgram, uint16_t first, uint16_t last, uint16_t *prev_ptr)
{
hole_t *hole_ptr = hole_pointer(dgram, first);
hole_ptr->first = first;
hole_ptr->last = last;
hole_ptr->next = *prev_ptr;
*prev_ptr = first;
return hole_ptr;
}
static ip_fragmented_datagram_t *ip_frag_dgram_lookup(buffer_t *buf, uint32_t id, uint16_t unfrag_len)
{
int_fast8_t count = 0;
ns_list_foreach(ip_fragmented_datagram_t, dgram, &frag_list) {
if (id == dgram->id &&
addr_ipv6_equal(buf->src_sa.address, dgram->buf->src_sa.address) &&
addr_ipv6_equal(buf->dst_sa.address, dgram->buf->dst_sa.address)) {
return dgram;
}
count++;
}
/* Not found - create one */
if (count >= MAX_FRAG_DATAGRAMS) {
free_datagram(ns_list_get_last(&frag_list));
}
ip_fragmented_datagram_t *new_dgram = ns_dyn_mem_temporary_alloc(sizeof(ip_fragmented_datagram_t));
if (!new_dgram) {
return NULL;
}
/* We track payload holes as per RFC 815, roughly, and reserve header
* room in front, based on the unfragmentable size of the first-received
* fragment.
*
* So initial state is:
*
* buf_ptr -> default buffer headroom + first-received-fragment header size
* fragmentable = buf_end = buf_ptr = offset of where fragments are assembled.
*
* When we receive the first (0-offset) fragment, we move down buf_ptr to
* put in its header, and when we receive the final (M=0) fragment, we
* set buf_end accordingly.
*
* Two odd cases to worry about:
*
* 1) First fragment is not received first, and has a larger
* header than our first-received fragment. In this case, we
* shuffle data if required when we get that first fragment.
* (Actual shuffle will normally be avoided by buffer headroom slack).
* 2) First fragment is not received first, and has a smaller
* header than our first-received fragment, meaning an IPV6_MRU-sized
* datagram may have more fragmented payload than we expected. Avoid
* a problem in this case by allocating a bigger-than-IPV6_MRU buffer
* if first-received fragment has extension headers.
*/
new_dgram->buf = buffer_get(unfrag_len + ipv6_frag_mru - 40);
if (!new_dgram->buf) {
ns_dyn_mem_free(new_dgram);
return NULL;
}
new_dgram->fragmentable = new_dgram->buf->buf_end = new_dgram->buf->buf_ptr += unfrag_len;
new_dgram->first_hole = 0xffff;
create_hole(new_dgram, 0, 0xffff, &new_dgram->first_hole);
new_dgram->buf->src_sa = buf->src_sa;
new_dgram->buf->dst_sa = buf->dst_sa;
new_dgram->id = id;
new_dgram->age = 0;
new_dgram->discard = false;
new_dgram->had_last = false;
new_dgram->ecn = buf->options.traffic_class & IP_TCLASS_ECN_MASK;
ns_list_add_to_start(&frag_list, new_dgram);
return new_dgram;
}
/*
* 4x4 combination array implementing the ECN combination rules from RFC 3168.
*
* Summary visualisation: N10C
* +----
* N|NNN-
* 1|N11C
* 0|N10C
* C|-CCC
*
* Each of the 16 entries, with justification:
*/
static const int8_t frag_ecn_combination[4][4] = {
// We MUST preserve the ECN codepoint when all fragments match.
[IP_ECN_NOT_ECT][IP_ECN_NOT_ECT] = IP_ECN_NOT_ECT,
[IP_ECN_ECT_0 ][IP_ECN_ECT_0 ] = IP_ECN_ECT_0,
[IP_ECN_ECT_1 ][IP_ECN_ECT_1 ] = IP_ECN_ECT_1,
[IP_ECN_CE ][IP_ECN_CE ] = IP_ECN_CE,
// We MUST set CE if any fragment has CE...
[IP_ECN_CE ][IP_ECN_ECT_0 ] = IP_ECN_CE,
[IP_ECN_CE ][IP_ECN_ECT_1 ] = IP_ECN_CE,
[IP_ECN_ECT_0 ][IP_ECN_CE ] = IP_ECN_CE,
[IP_ECN_ECT_1 ][IP_ECN_CE ] = IP_ECN_CE,
// ...except we MUST drop the packet if we see CE + Not-ECT.
[IP_ECN_CE ][IP_ECN_NOT_ECT] = IP_ECN__ILLEGAL,
[IP_ECN_NOT_ECT][IP_ECN_CE ] = IP_ECN__ILLEGAL,
// For the remaining cases, RFC 3168 leaves us free to do anything.
// To make the above CE+Not-ECT rule work in all delivery orders, with
// intervening ECT fragments, Not-ECT overrides ECT.
[IP_ECN_NOT_ECT][IP_ECN_ECT_0 ] = IP_ECN_NOT_ECT,
[IP_ECN_NOT_ECT][IP_ECN_ECT_1 ] = IP_ECN_NOT_ECT,
[IP_ECN_ECT_0 ][IP_ECN_NOT_ECT] = IP_ECN_NOT_ECT,
[IP_ECN_ECT_1 ][IP_ECN_NOT_ECT] = IP_ECN_NOT_ECT,
// Last two cases - RFC 3168 doesn't specify, but we follow the
// model of RFC 6040 and RFC 6660 which for tunnelling make ECT(1)
// take priority, as it can be used as a mild congestion indication.
[IP_ECN_ECT_0 ][IP_ECN_ECT_1 ] = IP_ECN_ECT_1,
[IP_ECN_ECT_1 ][IP_ECN_ECT_0 ] = IP_ECN_ECT_1
};
/*
* RFC 2460 notes:
*
* fragment packets:
*
* +------------------+--------+--------------+
* | Unfragmentable |Fragment| first |
* | Part | Header | fragment |
* +------------------+--------+--------------+
*
* +------------------+--------+--------------+
* | Unfragmentable |Fragment| second |
* | Part | Header | fragment |
* +------------------+--------+--------------+
* o
* o
* o
* +------------------+--------+----------+
* | Unfragmentable |Fragment| last |
* | Part | Header | fragment |
* +------------------+--------+----------+
*
* reassembled original packet:
*
* +------------------+----------------------//------------------------+
* | Unfragmentable | Fragmentable |
* | Part | Part |
* +------------------+----------------------//------------------------+
*
* The following rules govern reassembly:
*
* An original packet is reassembled only from fragment packets that
* have the same Source Address, Destination Address, and Fragment
* Identification.
*
* The Unfragmentable Part of the reassembled packet consists of all
* headers up to, but not including, the Fragment header of the first
* fragment packet (that is, the packet whose Fragment Offset is
* zero), with the following two changes:
*
* The Next Header field of the last header of the Unfragmentable
* Part is obtained from the Next Header field of the first
* fragment's Fragment header.
*
* The Payload Length of the reassembled packet is computed from
* the length of the Unfragmentable Part and the length and offset
* of the last fragment.
*
* Fragment Header
*
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Next Header | Reserved | Fragment Offset |Res|M|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Identification |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*
* Fragment Offset 13-bit unsigned integer. The offset, in 8-octet
* units, of the data following this header,
* relative to the start of the Fragmentable Part
* of the original packet.
*
* M flag 1 = more fragments; 0 = last fragment.
*/
/* On entry: frag_hdr -> fragment header
* nh_ptr -> Next Header octet in previous header
* payload_length = length of remaining data, including this header
* buffer data pointers describe entire IP fragment packet
* buffer src/dst filled in
* Returns: Either reassembled packet (B_DIR_UP | B_TO_IPV6_FWD)
* or ICMP error response (B_DIR_DOWN | B_TO_ICMP)
* or NULL (fragment accepted, reassembly in progress)
*/
buffer_t *ipv6_frag_up(buffer_t *frag_buf, const uint8_t *frag_hdr, uint8_t *nh_ptr, uint16_t payload_length)
{
if (payload_length <= 8) {
return icmpv6_error(frag_buf, NULL, ICMPV6_TYPE_ERROR_PARAMETER_PROBLEM, ICMPV6_CODE_PARAM_PRB_HDR_ERR, 4);
}
payload_length -= 8;
uint8_t *ip_hdr = buffer_data_pointer(frag_buf);
uint16_t unfrag_len = frag_hdr - ip_hdr;
uint16_t fragment_first = common_read_16_bit(frag_hdr + 2) & 0xFFF8;
uint16_t fragment_last = fragment_first + payload_length - 1;
bool more = frag_hdr[3] & 1;
/* All fragments apart from last must be multiples of 8 */
if (more && (payload_length & 7)) {
return icmpv6_error(frag_buf, NULL, ICMPV6_TYPE_ERROR_PARAMETER_PROBLEM, ICMPV6_CODE_PARAM_PRB_HDR_ERR, 4);
}
/* Check we don't overflow 16-bit size */
if (fragment_last < fragment_first) {
return icmpv6_error(frag_buf, NULL, ICMPV6_TYPE_ERROR_PARAMETER_PROBLEM, ICMPV6_CODE_PARAM_PRB_HDR_ERR, frag_hdr + 2 - ip_hdr);
}
if (fragment_first == 0) {
/* Replace "Next Header" byte in previous header */
*nh_ptr = frag_hdr[0];
if (!more) {
/* Atomic fragment handling - strip out the fragment header.
* See RFC 6946, which says that we require a special case for atomic
* fragments:
*
* A host that receives an IPv6 packet that includes a Fragment
* Header with the "Fragment Offset" equal to 0 and the "M" flag
* equal to 0 MUST process that packet in isolation from any other
* packets/fragments, even if such packets/fragments contain the same
* set {IPv6 Source Address, IPv6 Destination Address, Fragment
* Identification}.
*
* (Conceivably, we could just skip the header and keep parsing,
* but this keeps it consistent with real fragments).
*/
/* Move unfragmentable part up, eliminating fragment header */
memmove(ip_hdr + 8, ip_hdr, unfrag_len);
ip_hdr = buffer_data_strip_header(frag_buf, 8);
/* Reduce Payload Length in IP header */
uint16_t len = common_read_16_bit(ip_hdr + 4);
common_write_16_bit(len - 8, ip_hdr + 4);
frag_buf->offset = unfrag_len;
frag_buf->options.ip_extflags |= IPEXT_FRAGMENT;
frag_buf->info = (buffer_info_t)(B_DIR_UP | B_TO_IPV6_FWD | B_FROM_IPV6_FWD);
return frag_buf;
}
}
/* Adjust buffer pointer to point to fragment data. ip_ptr remains
* pointing at IP header, which we need for first fragment. */
buffer_data_pointer_set(frag_buf, frag_hdr + 8);
/* Locate or create datagram assembly buffer */
uint32_t id = common_read_32_bit(frag_hdr + 4);
ip_fragmented_datagram_t *dgram = ip_frag_dgram_lookup(frag_buf, id, unfrag_len);
if (!dgram || dgram->discard) {
protocol_stats_update(STATS_IP_RX_DROP, 1);
return buffer_free(frag_buf);
}
buffer_t *dgram_buf = dgram->buf;
/* Length checks. For predictability, best to ensure we always try to
* respect IPV6_MRU as a hard limit, which means a bit of care. */
uint16_t limit;
if (dgram_buf->buf_ptr == dgram->fragmentable) {
/* Haven't yet got final header size - good enough to do rough check;
* we have enough buffer to fit MRU - min IP header size */
limit = ipv6_frag_mru - 40;
} else {
/* We do know final header size, so can do precise MRU check */
limit = ipv6_frag_mru - (dgram->fragmentable - dgram_buf->buf_ptr);
}
/* Make sure we have room for following data, and hence a hole descriptor */
if (more) {
limit -= 8;
}
if (fragment_last >= limit) {
/* Fragment would make datagram exceed MRU */
tr_warn("Datagram size %u too big", fragment_last + 1);
fail:
invalidate_datagram(dgram);
protocol_stats_update(STATS_IP_RX_DROP, 1);
return buffer_free(frag_buf);
}
/* Hole-filling algorithm, basically as per RFC815, but with added
* checks for overlap (RFC 5722). We keep the hole list sorted to aid this,
* (and Time Exceeded messages) - something RFC 815 doesn't strictly require.
*/
uint16_t hole_off = dgram->first_hole;
uint16_t *prev_ptr = &dgram->first_hole;
bool okay = false;
do {
hole_t *hole = hole_pointer(dgram, hole_off);
uint_fast16_t hole_first = hole->first;
uint_fast16_t hole_last = hole->last;
/* Fragment is beyond this hole - move to next (RFC 815 step 2) */
if (fragment_first > hole_last) {
prev_ptr = &hole->next;
hole_off = hole->next;
continue;
}
/* RFC 815 step 3 would have us check for fragment_last < hole_first,
* and skipping, but we don't need/want to do that - it's covered by
* the next check.
*/
/* Unlike RFC 815, we now check for any overlap (RFC 5722) */
if (fragment_first < hole_first || fragment_last > hole_last) {
break;
}
/* Unhook this hole from the hole list (RFC 815 step 4) */
delete_hole(dgram, hole_off, prev_ptr);
hole = NULL;
/* Create a new hole in front if necessary (RFC 815 step 5) */
if (fragment_first > hole_first) {
prev_ptr = &create_hole(dgram, hole_first, fragment_first - 1, prev_ptr)->next;
}
if (more) {
/* Create a following hole if necessary (RFC 815 step 6) */
if (fragment_last < hole_last) {
create_hole(dgram, fragment_last + 1, hole_last, prev_ptr);
}
} else {
/* If we already have some later data, it's broken. */
if (hole_last != 0xffff) {
break;
}
dgram->had_last = true;
}
/* Update end of buffer, if this is the last-placed fragment so far */
if (hole_last == 0xffff) {
dgram_buf->buf_end = dgram->fragmentable + fragment_last + 1;
}
/* Unlike RFC 815, we're now done. We don't allow overlaps, so we finish
* as soon as we identify one hole that it entirely or partially fills */
okay = true;
break;
} while (hole_off != 0xffff);
/* If /any/ reassembly problems - overlaps etc - abandon the datagram */
if (!okay) {
tr_warn("Reassembly error");
goto fail;
}
/* Hole list updated, can now copy in the fragment data */
memcpy(dgram_buf->buf + dgram->fragmentable + fragment_first, buffer_data_pointer(frag_buf), fragment_last + 1 - fragment_first);
/* Combine the "improper security" flags, so reassembled buffer's flag is set if any fragment wasn't secure */
/* XXX should have some sort of overall "merge buffer metadata" routine handling this and whatever else */
dgram_buf->options.ll_security_bypass_rx |= frag_buf->options.ll_security_bypass_rx;
/* Combine the ECN field */
dgram->ecn = frag_ecn_combination[dgram->ecn][frag_buf->options.traffic_class & IP_TCLASS_ECN_MASK];
if (dgram->ecn == IP_ECN__ILLEGAL) {
tr_warn("Illegal ECN");
goto fail;
}
/* Overlap checks above ensure first-packet processing only happens once */
if (fragment_first == 0) {
/* Now know final header size, so repeat MRU check */
uint16_t frag_so_far = dgram_buf->buf_end - dgram->fragmentable;
if (!dgram->had_last) {
/* This fudge factor represents our expectation of more data, and
* also makes sure we memmove the trailing hole descriptor. */
frag_so_far += 8;
}
if (unfrag_len + frag_so_far > ipv6_frag_mru) {
tr_warn("Datagram size %u too big", unfrag_len + frag_so_far);
goto fail;
}
if (dgram_buf->buf_ptr < unfrag_len) {
/* Didn't reserve enough space for header. Shuffle data up into what will be final position */
/* We know we have buffer room, thanks to previous checks against IPV6_MRU */
uint16_t new_frag_offset = dgram_buf->size - ipv6_frag_mru + unfrag_len;
memmove(dgram_buf->buf + new_frag_offset, dgram_buf->buf + dgram->fragmentable, frag_so_far);
dgram->buf->buf_ptr = dgram->fragmentable = new_frag_offset;
}
/* Move the start pointer, and copy the header */
memcpy(buffer_data_reserve_header(dgram_buf, unfrag_len), ip_hdr, unfrag_len);
/* Clone the buffer header from this first fragment, preserving only size + pointers */
/* Also the security flag, already merged above */
bool buf_security = dgram_buf->options.ll_security_bypass_rx;
buffer_copy_metadata(dgram_buf, frag_buf, true);
dgram_buf->options.ll_security_bypass_rx = buf_security;
/* Mark position of fragment header - allows skipping previous headers */
dgram_buf->offset = unfrag_len;
dgram_buf->options.ip_extflags |= IPEXT_FRAGMENT;
}
/* Free the original fragment buffer - we've extracted its juice */
buffer_free(frag_buf);
/* Thanks to David Clark, completion check is now simple */
if (dgram->first_hole != 0xffff) {
/* Not yet complete - processing finished on this fragment */
return NULL;
}
/* First 8 bytes of the IP header, currently from the first fragment,
* that we need to patch:
* . . . . .
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* |Version| DSCP |ECN| Flow Label |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Payload Length | Next Header | Hop Limit |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
/* Fill in the combined ECN - 2 bits in the middle of the second byte */
buffer_data_pointer(dgram_buf)[1] &= ~(3 << 4);
buffer_data_pointer(dgram_buf)[1] |= (dgram->ecn << 4);
/* Fill in final IP header length */
common_write_16_bit(buffer_data_length(dgram_buf) - 40, buffer_data_pointer(dgram_buf) + 4);
/* We've completed the datagram. Free the assembly structures (but not the buffer!) */
dgram->buf = NULL;
free_datagram(dgram);
/* Send on the completed datagram */
dgram_buf->info = (buffer_info_t)(B_DIR_UP | B_TO_IPV6_FWD | B_FROM_IPV6_FWD);
return dgram_buf;
}
#endif /* NO_IP_FRAGMENT_RX */
/* FRAGMENT CREATION
*
* Allow fragment TX to be disabled for constrained systems.
* This would violate RFC 6434, which says all IPv6 nodes must be able to
* generate fragment headers. (Even if our only link has the minimum 1280-byte
* MTU, we may still need to insert a fragment header).
*/
#ifndef NO_IP_FRAGMENT_TX
buffer_t *ipv6_frag_down(buffer_t *dgram_buf)
{
uint8_t *ip_ptr = buffer_data_pointer(dgram_buf);
uint16_t pmtu = ipv6_mtu(dgram_buf);
uint8_t *frag_hdr;
buffer_list_t frags_list = NS_LIST_INIT(frags_list);
ipv6_destination_t *dest = ipv6_destination_lookup_or_create(dgram_buf->dst_sa.address, dgram_buf->interface->id);
if (!dest) {
return buffer_free(dgram_buf);
}
/* Skip over HbH and Routing headers to reach fragmentable part. Assume
* packet well-formed (we created it...).
*/
uint8_t *nh_ptr = &ip_ptr[6];
uint8_t nh = *nh_ptr;
uint8_t *fragmentable = ip_ptr + 40;
while (nh == IPV6_NH_HOP_BY_HOP || nh == IPV6_NH_ROUTING) {
nh_ptr = &fragmentable[0];
nh = *nh_ptr;
fragmentable += (fragmentable[1] + 1) * 8;
}
uint16_t unfrag_len = fragmentable - ip_ptr;
uint16_t fragmentable_len = buffer_data_end(dgram_buf) - fragmentable;
*nh_ptr = IPV6_NH_FRAGMENT;
/* Check for silly situation - can't fit any fragment data (8 for fragment
* header, 8 for minimum fragment payload) */
if (unfrag_len + 8 + 8 > pmtu) {
goto failed;
}
++dest->fragment_id;
/* RFC 7112 requires the entire header chain to be in the first fragment. */
/* We don't explicitly check for this, but it would be spectacularly unlikely. */
/* I think it would require a super-sized routing header */
/* This is much simpler (more simplistic?) than the 6LoWPAN fragmentation,
* which relies on co-operation with lower layers to ensure it works one
* fragment at a time. We make all the fragments in one go, meaning higher
* overhead, but IP fragmentation should be pretty rare - we don't need
* to optimise this.
*/
for (uint16_t frag_offset = 0; fragmentable_len;) {
/* How much going in this packet? */
uint16_t frag_len = (pmtu - unfrag_len - 8);
if (fragmentable_len > frag_len) {
frag_len &= ~7;
} else {
frag_len = fragmentable_len;
}
buffer_t *frag_buf = buffer_get(unfrag_len + 8 + frag_len);
if (!frag_buf) {
goto failed;
}
/* Clone the buffer header, apart from size+ptr */
buffer_copy_metadata(frag_buf, dgram_buf, false);
/* We splat the socket, so no upper-layer callbacks from the fragments */
buffer_socket_set(frag_buf, NULL);
/* Construct the new packet contents */
buffer_data_length_set(frag_buf, unfrag_len + 8 + frag_len);
uint8_t *ptr = buffer_data_pointer(frag_buf);
/* Unfragmentable part */
memcpy(ptr, ip_ptr, unfrag_len);
/* Adjust length in IP header */
common_write_16_bit(unfrag_len - 40 + 8 + frag_len, ptr + 4);
/* Fragment header */
frag_hdr = ptr + unfrag_len;
frag_hdr[0] = nh;
frag_hdr[1] = 0;
common_write_16_bit(frag_offset | (frag_len != fragmentable_len), frag_hdr + 2);
common_write_32_bit(dest->fragment_id, frag_hdr + 4);
/* Fragment data */
memcpy(frag_hdr + 8, fragmentable + frag_offset, frag_len);
fragmentable_len -= frag_len;
frag_offset += frag_len;
/* Add to our fragment list */
ns_list_add_to_start(&frags_list, frag_buf);
}
/* Now have a list of fragment buffers - report "success" to the socket */
/* (TCP may save the dgram payload here? It strips off headers, so okay...) */
socket_tx_buffer_event_and_free(dgram_buf, SOCKET_TX_DONE);
/* Push the fragments. Backwards, as it happens, but who cares? */
ns_list_foreach_safe(buffer_t, f, &frags_list) {
ns_list_remove(&frags_list, f);
protocol_push(f);
}
return NULL;
failed:
/* Failed to allocate a buffer - no point sending any fragments if we
* can't send all.
*/
ns_list_foreach_safe(buffer_t, f, &frags_list) {
ns_list_remove(&frags_list, f);
buffer_free(f);
}
socket_tx_buffer_event_and_free(dgram_buf, SOCKET_NO_RAM);
return NULL;
}
#endif /* NO_IP_FRAGMENT_TX */