1 /*
2  * Copyright (c) 2016 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * @brief 802.15.4 6LoWPAN fragment handler
10  *
11  * This is not to be included by the application.
12  */
13 
14 #ifndef __NET_IEEE802154_6LO_FRAGMENT_H__
15 #define __NET_IEEE802154_6LO_FRAGMENT_H__
16 
17 #include "ieee802154_6lo.h"
18 #include "ieee802154_frame.h"
19 
20 #include <zephyr/net/net_pkt.h>
21 #include <zephyr/sys/slist.h>
22 #include <zephyr/types.h>
23 
ieee802154_6lo_requires_fragmentation(struct net_pkt * pkt,uint8_t ll_hdr_len,uint8_t authtag_len)24 static inline bool ieee802154_6lo_requires_fragmentation(struct net_pkt *pkt, uint8_t ll_hdr_len,
25 							 uint8_t authtag_len)
26 {
27 	return (ll_hdr_len + net_pkt_get_len(pkt) + authtag_len > IEEE802154_MTU);
28 }
29 
ieee802154_6lo_fragment_ctx_init(struct ieee802154_6lo_fragment_ctx * ctx,struct net_pkt * pkt,uint16_t hdr_diff,bool iphc)30 static inline void ieee802154_6lo_fragment_ctx_init(struct ieee802154_6lo_fragment_ctx *ctx,
31 						    struct net_pkt *pkt, uint16_t hdr_diff,
32 						    bool iphc)
33 {
34 	ctx->buf = pkt->buffer;
35 	ctx->pos = ctx->buf->data;
36 	ctx->hdr_diff = hdr_diff;
37 	ctx->pkt_size = net_pkt_get_len(pkt) + (iphc ? hdr_diff : -1);
38 	ctx->offset = 0U;
39 	ctx->processed = 0U;
40 }
41 
42 /**
43  *  @brief Fragment IPv6 packet as per RFC 6282
44  *
45  *  @details After IPv6 compression, transmission of IPv6 over 802.15.4
46  *  needs to be fragmented. Every fragment will have fragmentation header
47  *  data size, data offset, data tag and payload.
48  *
49  *  @param ctx Pointer to valid fragmentation context
50  *  @param frame_buf Pointer to valid buffer where to write the fragment
51  *  @param ipch bool true for IPHC compression, false for IPv6 dispatch header
52  *
53  *  @return pointer to the next buffer to be processed or NULL if no more
54  *          buffers need processing
55  */
56 struct net_buf *ieee802154_6lo_fragment(struct ieee802154_6lo_fragment_ctx *ctx,
57 					struct net_buf *frame_buf, bool iphc);
58 
59 /**
60  *  @brief Reassemble 802.15.4 fragments as per RFC 6282
61  *
62  *  @details If the data does not fit into single fragment whole IPv6 packet
63  *  comes in number of fragments. This function will reassemble them all as
64  *  per data tag, data offset and data size. First packet is uncompressed
65  *  immediately after reception.
66  *
67  *  @param pkt Pointer to network fragment, which gets updated to full reassembled
68  *         packet when verdict is NET_CONTINUE
69  *
70  *  @return NET_CONTINUE reassembly done, pkt is complete
71  *          NET_OK waiting for other fragments,
72  *          NET_DROP invalid fragment.
73  */
74 enum net_verdict ieee802154_6lo_reassemble(struct net_pkt *pkt);
75 
76 #endif /* __NET_IEEE802154_6LO_FRAGMENT_H__ */
77