1 /*
2  * Copyright (c) 2016 Intel Corporation.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * @brief IEEE 802.15.4 L2 stack public header
10  */
11 
12 #ifndef ZEPHYR_INCLUDE_NET_IEEE802154_H_
13 #define ZEPHYR_INCLUDE_NET_IEEE802154_H_
14 
15 #include <limits.h>
16 #include <zephyr/net/net_l2.h>
17 #include <zephyr/net/net_mgmt.h>
18 #include <zephyr/crypto/cipher.h>
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 /**
25  * @brief IEEE 802.15.4 library
26  * @defgroup ieee802154 IEEE 802.15.4 Library
27  * @ingroup networking
28  * @{
29  */
30 
31 /* References are to the IEEE 802.15.4-2020 standard */
32 #define IEEE802154_MAX_PHY_PACKET_SIZE	127 /* see section 11.3, aMaxPhyPacketSize */
33 #define IEEE802154_FCS_LENGTH		2   /* see section 7.2.1.1 */
34 #define IEEE802154_MTU			(IEEE802154_MAX_PHY_PACKET_SIZE - IEEE802154_FCS_LENGTH)
35 /* TODO: Support flexible MTU and FCS lengths for IEEE 802.15.4-2015ff */
36 
37 #define IEEE802154_SHORT_ADDR_LENGTH	2
38 #define IEEE802154_EXT_ADDR_LENGTH	8
39 #define IEEE802154_MAX_ADDR_LENGTH	IEEE802154_EXT_ADDR_LENGTH
40 
41 #define IEEE802154_NO_CHANNEL		USHRT_MAX
42 
43 /* See IEEE 802.15.4-2020, sections 6.1 and 7.3.5 */
44 #define IEEE802154_BROADCAST_ADDRESS	     0xffff
45 #define IEEE802154_NO_SHORT_ADDRESS_ASSIGNED 0xfffe
46 
47 /* See IEEE 802.15.4-2020, section 6.1 */
48 #define IEEE802154_BROADCAST_PAN_ID 0xffff
49 
50 /* See IEEE 802.15.4-2020, section 7.3.5 */
51 #define IEEE802154_SHORT_ADDRESS_NOT_ASSOCIATED IEEE802154_BROADCAST_ADDRESS
52 
53 struct ieee802154_security_ctx {
54 	uint32_t frame_counter;
55 	struct cipher_ctx enc;
56 	struct cipher_ctx dec;
57 	uint8_t key[16];
58 	uint8_t key_len;
59 	uint8_t level	: 3;
60 	uint8_t key_mode	: 2;
61 	uint8_t _unused	: 3;
62 };
63 
64 /* This not meant to be used by any code but 802.15.4 L2 stack */
65 struct ieee802154_context {
66 	uint16_t pan_id; /* in CPU byte order */
67 	uint16_t channel; /* in CPU byte order */
68 	/* short address:
69 	 *   0 == not associated,
70 	 *   0xfffe == associated but no short address assigned
71 	 * see section 7.4.2
72 	 */
73 	uint16_t short_addr; /* in CPU byte order */
74 	uint8_t ext_addr[IEEE802154_MAX_ADDR_LENGTH]; /* in little endian */
75 	struct net_linkaddr_storage linkaddr; /* in big endian */
76 #ifdef CONFIG_NET_L2_IEEE802154_SECURITY
77 	struct ieee802154_security_ctx sec_ctx;
78 #endif
79 #ifdef CONFIG_NET_L2_IEEE802154_MGMT
80 	struct ieee802154_req_params *scan_ctx; /* guarded by scan_ctx_lock */
81 	struct k_sem scan_ctx_lock;
82 
83 	uint8_t coord_ext_addr[IEEE802154_MAX_ADDR_LENGTH]; /* in little endian */
84 	uint16_t coord_short_addr; /* in CPU byte order */
85 #endif
86 	int16_t tx_power;
87 	enum net_l2_flags flags;
88 
89 	uint8_t sequence;
90 
91 	uint8_t _unused : 6;
92 
93 	uint8_t ack_received : 1;  /* guarded by ack_lock */
94 	uint8_t ack_requested : 1; /* guarded by ack_lock */
95 	uint8_t ack_seq;	   /* guarded by ack_lock */
96 	struct k_sem ack_lock;
97 
98 	struct k_sem ctx_lock; /* guards all mutable context attributes unless
99 				* otherwise mentioned on attribute level
100 				*/
101 };
102 
103 #define IEEE802154_L2_CTX_TYPE	struct ieee802154_context
104 
105 #ifdef __cplusplus
106 }
107 #endif
108 
109 /**
110  * @}
111  */
112 
113 #endif /* ZEPHYR_INCLUDE_NET_IEEE802154_H_ */
114