1 /*
2  * Copyright (c) 2020 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_SUBSYS_BLUETOOTH_MESH_SUBNET_H_
8 #define ZEPHYR_SUBSYS_BLUETOOTH_MESH_SUBNET_H_
9 
10 #include <stdint.h>
11 #include <sys/types.h>
12 #include <zephyr/net/buf.h>
13 #include <zephyr/kernel.h>
14 #include <zephyr/sys/iterable_sections.h>
15 
16 #define BT_MESH_NET_FLAG_KR       BIT(0)
17 #define BT_MESH_NET_FLAG_IVU      BIT(1)
18 
19 #define BT_MESH_KR_NORMAL         0x00
20 #define BT_MESH_KR_PHASE_1        0x01
21 #define BT_MESH_KR_PHASE_2        0x02
22 #define BT_MESH_KR_PHASE_3        0x03
23 
24 /** Which of the two subnet.keys should be used for sending. */
25 #define SUBNET_KEY_TX_IDX(sub) ((sub)->kr_phase == BT_MESH_KR_PHASE_2)
26 
27 struct bt_mesh_net_rx;
28 enum bt_mesh_key_evt;
29 
30 /** Network message encryption credentials */
31 struct bt_mesh_net_cred {
32 	uint8_t nid;         /* NID */
33 	uint8_t enc[16];     /* EncKey */
34 	uint8_t privacy[16]; /* PrivacyKey */
35 };
36 
37 struct bt_mesh_beacon {
38 	uint32_t sent;        /* Timestamp of last sent beacon */
39 	uint32_t recv;        /* Timestamp of last received beacon */
40 	uint8_t  last;       /* Number of beacons during last
41 				      * observation window
42 				      */
43 	uint8_t  cur;        /* Number of beacons observed during
44 				      * currently ongoing window.
45 				      */
46 	uint8_t  cache[8];   /* Cached last beacon auth value */
47 	uint8_t  auth[8];    /* Beacon Authentication Value */
48 };
49 
50 /** Subnet instance. */
51 struct bt_mesh_subnet {
52 	uint16_t net_idx;            /* NetKeyIndex */
53 
54 	uint8_t  kr_phase;           /* Key Refresh Phase */
55 
56 	uint8_t  node_id;            /* Node Identity State */
57 	uint32_t node_id_start;      /* Node Identity started timestamp */
58 
59 	struct bt_mesh_beacon secure_beacon;
60 
61 #if defined(CONFIG_BT_MESH_PRIV_BEACONS)
62 	struct bt_mesh_beacon priv_beacon;
63 	struct {
64 		uint16_t idx;        /* Private beacon random index */
65 		bool node_id;        /* Private Node Identity enabled */
66 		uint8_t data[5];     /* Private Beacon data */
67 	} priv_beacon_ctx;
68 #endif
69 
70 	struct bt_mesh_subnet_keys {
71 		bool valid;
72 		uint8_t net[16];       /* NetKey */
73 		struct bt_mesh_net_cred msg;
74 		uint8_t net_id[8];     /* Network ID */
75 	#if defined(CONFIG_BT_MESH_GATT_PROXY)
76 		uint8_t identity[16];  /* IdentityKey */
77 	#endif
78 		uint8_t beacon[16];      /* BeaconKey */
79 #if defined(CONFIG_BT_MESH_V1d1)
80 		uint8_t priv_beacon[16]; /* PrivateBeaconKey */
81 #endif
82 	} keys[2];
83 #if defined(CONFIG_BT_MESH_PROXY_SOLICITATION)
84 	bool sol_tx;
85 #endif
86 #if defined(CONFIG_BT_MESH_OD_PRIV_PROXY_SRV)
87 	uint32_t priv_net_id_sent; /* Timestamp for Private Network ID advertising
88 				    * started via Proxy Solicitation
89 				    */
90 	bool solicited;      /* Subnet received valid Solicitation PDU */
91 #endif
92 };
93 
94 /** Subnet callback structure. Instantiate with @ref BT_MESH_SUBNET_CB */
95 struct bt_mesh_subnet_cb {
96 	void (*evt_handler)(struct bt_mesh_subnet *subnet,
97 			    enum bt_mesh_key_evt evt);
98 };
99 
100 /**
101  *  @brief Register a subnet event callback.
102  *
103  *  @param _name Handler name.
104  */
105 #define BT_MESH_SUBNET_CB_DEFINE(_name)                                    \
106 	static const STRUCT_SECTION_ITERABLE(                               \
107 		bt_mesh_subnet_cb, _CONCAT(bt_mesh_subnet_cb_, _name))
108 
109 /** @brief Reset all Network keys. */
110 void bt_mesh_net_keys_reset(void);
111 
112 /** @brief Call cb on every valid Subnet until it returns a non-zero value.
113  *
114  *  @param cb Callback to call, or NULL to return first valid subnet. If the callback returns true,
115  *            iteration stops, and the passed subnet is returned.
116  *  @param cb_data Callback data to pass to callback.
117  *
118  *  @return Subnet that returned non-zero value.
119  */
120 struct bt_mesh_subnet *bt_mesh_subnet_find(bool (*cb)(struct bt_mesh_subnet *sub, void *cb_data),
121 					   void *cb_data);
122 
123 /** @brief Iterate through all valid Subnets.
124  *
125  *  @param cb Callback to call on every Subnet.
126  *
127  *  @returns The number of valid subnets.
128  */
129 size_t bt_mesh_subnet_foreach(void (*cb)(struct bt_mesh_subnet *sub));
130 
131 /** @brief Get the next valid Subnet.
132  *
133  *  If there's only one valid Subnet, this will be returned on every call.
134  *
135  *  @param sub Previous Subnet, or NULL to get the first valid.
136  *
137  *  @returns Gets the next valid Subnet after @c sub, or NULL if there are no
138  *           valid Subnets.
139  */
140 struct bt_mesh_subnet *bt_mesh_subnet_next(struct bt_mesh_subnet *sub);
141 
142 /** @brief Get a pointer to the Subnet with the given index.
143  *
144  *  @param net_idx Network index to look for.
145  *
146  *  @returns Subnet with index @c net_idx, or NULL if no such Subnet is known.
147  */
148 struct bt_mesh_subnet *bt_mesh_subnet_get(uint16_t net_idx);
149 
150 /** @brief Initialize a new Subnet.
151  *
152  *  @param net_idx Network index of the Subnet.
153  *  @param kr_phase Key refresh phase the Subnet should be in.
154  *  @param key The current network key for the Subnet.
155  *  @param new_key New network key, if available.
156  *
157  *  @returns 0 on success, or (negative) error code on failure.
158  */
159 int bt_mesh_subnet_set(uint16_t net_idx, uint8_t kr_phase,
160 		       const uint8_t key[16], const uint8_t new_key[16]);
161 
162 /** @brief Create Friendship credentials.
163  *
164  *  @param cred Credential object to create.
165  *  @param lpn_addr Address of the LPN node in the friendship.
166  *  @param frnd_addr Address of the Friend node in the friendship.
167  *  @param lpn_counter The LPN's counter parameter.
168  *  @param frnd_counter The Friend node's counter parameter.
169  *  @param key Network key to create the Friendship credentials for.
170  *
171  *  @returns 0 on success, or (negative) error code on failure.
172  */
173 int bt_mesh_friend_cred_create(struct bt_mesh_net_cred *cred,
174 			       uint16_t lpn_addr, uint16_t frnd_addr,
175 			       uint16_t lpn_counter, uint16_t frnd_counter,
176 			       const uint8_t key[16]);
177 
178 /** @brief Iterate through all valid network credentials to decrypt a message.
179  *
180  *  @param rx Network RX parameters, passed to the callback.
181  *  @param in Input message buffer, passed to the callback.
182  *  @param out Output message buffer, passed to the callback.
183  *  @param cb Callback to call for each known network credential. Iteration
184  *            stops when this callback returns @c true.
185  *
186  *  @returns Whether any of the credentials got a @c true return from the
187  *           callback.
188  */
189 bool bt_mesh_net_cred_find(struct bt_mesh_net_rx *rx, struct net_buf_simple *in,
190 			   struct net_buf_simple *out,
191 			   bool (*cb)(struct bt_mesh_net_rx *rx,
192 				      struct net_buf_simple *in,
193 				      struct net_buf_simple *out,
194 				      const struct bt_mesh_net_cred *cred));
195 
196 /** @brief Get the network flags of the given Subnet.
197  *
198  *  @param sub Subnet to get the network flags of.
199  *
200  *  @returns A bitmap of @ref BT_MESH_NET_FLAG_KR and @ref BT_MESH_NET_FLAG_IVU.
201  */
202 uint8_t bt_mesh_net_flags(struct bt_mesh_subnet *sub);
203 
204 /** @brief Process a Key Refresh event from a beacon.
205  *
206  *  @param sub Subnet the Key Refresh was received on.
207  *  @param kr_flag Key Refresh flag.
208  *  @param new_key Whether the Key Refresh event was received on the new key
209  *                 set.
210  *
211  *  @returns Whether the Key Refresh event caused a change.
212  */
213 void bt_mesh_kr_update(struct bt_mesh_subnet *sub, bool kr_flag, bool new_key);
214 
215 /** @brief Check whether the Subnet has the refreshed keys.
216  *
217  *  @param sub Subnet.
218  *
219  *  @returns Whether the Subnet's second key is valid.
220  */
221 static inline bool
bt_mesh_subnet_has_new_key(const struct bt_mesh_subnet * sub)222 bt_mesh_subnet_has_new_key(const struct bt_mesh_subnet *sub)
223 {
224 	return sub->kr_phase != BT_MESH_KR_NORMAL;
225 }
226 
227 /** @brief Store the Subnet information in persistent storage.
228  *
229  * @param net_idx Network index to store.
230  */
231 void bt_mesh_subnet_store(uint16_t net_idx);
232 
233 /** @brief Store the pending Subnets in persistent storage. */
234 void bt_mesh_subnet_pending_store(void);
235 
236 #endif /* ZEPHYR_SUBSYS_BLUETOOTH_MESH_SUBNET_H_ */
237