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 adaptation layer implementation
10  */
11 
12 #include <zephyr/logging/log.h>
13 LOG_MODULE_REGISTER(net_ieee802154_6lo, CONFIG_NET_L2_IEEE802154_LOG_LEVEL);
14 
15 #include "ieee802154_6lo.h"
16 #include "ieee802154_frame.h"
17 #include "ieee802154_security.h"
18 
19 #include <6lo.h>
20 #include <ipv6.h>
21 #ifdef CONFIG_NET_L2_IEEE802154_FRAGMENT
22 #include "ieee802154_6lo_fragment.h"
23 #endif /* CONFIG_NET_L2_IEEE802154_FRAGMENT */
24 
ieee802154_6lo_decode_pkt(struct net_if * iface,struct net_pkt * pkt)25 enum net_verdict ieee802154_6lo_decode_pkt(struct net_if *iface, struct net_pkt *pkt)
26 {
27 #ifdef CONFIG_NET_L2_IEEE802154_FRAGMENT
28 	return ieee802154_6lo_reassemble(pkt);
29 #else
30 	if (!net_6lo_uncompress(pkt)) {
31 		NET_DBG("Packet decompression failed");
32 		return NET_DROP;
33 	}
34 	return NET_CONTINUE;
35 #endif /* CONFIG_NET_L2_IEEE802154_FRAGMENT */
36 }
37 
ieee802154_6lo_encode_pkt(struct net_if * iface,struct net_pkt * pkt,struct ieee802154_6lo_fragment_ctx * f_ctx,uint8_t ll_hdr_len,uint8_t authtag_len)38 int ieee802154_6lo_encode_pkt(struct net_if *iface, struct net_pkt *pkt,
39 			      struct ieee802154_6lo_fragment_ctx *f_ctx, uint8_t ll_hdr_len,
40 			      uint8_t authtag_len)
41 {
42 	if (net_pkt_family(pkt) != AF_INET6) {
43 		return 0;
44 	}
45 
46 	/* hdr_diff will hold the hdr size difference on success */
47 	int hdr_diff = net_6lo_compress(pkt, true);
48 
49 	if (hdr_diff < 0) {
50 		return hdr_diff;
51 	}
52 
53 #ifdef CONFIG_NET_L2_IEEE802154_FRAGMENT
54 	bool requires_fragmentation =
55 		ieee802154_6lo_requires_fragmentation(pkt, ll_hdr_len, authtag_len);
56 
57 	if (requires_fragmentation) {
58 		ieee802154_6lo_fragment_ctx_init(f_ctx, pkt, hdr_diff, true);
59 	}
60 	return requires_fragmentation ? 1 : 0;
61 #else
62 	return 0;
63 #endif /* CONFIG_NET_L2_IEEE802154_FRAGMENT */
64 }
65