1 /* Bluetooth Mesh */ 2 3 /* 4 * Copyright (c) 2017 Intel Corporation 5 * 6 * SPDX-License-Identifier: Apache-2.0 7 */ 8 9 #ifndef __NET_H__ 10 #define __NET_H__ 11 12 #define BT_MESH_NET_FLAG_KR BIT(0) 13 #define BT_MESH_NET_FLAG_IVU BIT(1) 14 15 #define BT_MESH_KR_NORMAL 0x00 16 #define BT_MESH_KR_PHASE_1 0x01 17 #define BT_MESH_KR_PHASE_2 0x02 18 #define BT_MESH_KR_PHASE_3 0x03 19 20 #define BT_MESH_IV_UPDATE(flags) ((flags >> 1) & 0x01) 21 #define BT_MESH_KEY_REFRESH(flags) (flags & 0x01) 22 23 #include <stdbool.h> 24 #include "atomic.h" 25 #include "mesh/mesh.h" 26 #include "mesh/glue.h" 27 28 /* How many hours in between updating IVU duration */ 29 #define BT_MESH_IVU_MIN_HOURS 96 30 #define BT_MESH_IVU_HOURS (BT_MESH_IVU_MIN_HOURS / \ 31 CONFIG_BT_MESH_IVU_DIVIDER) 32 #define BT_MESH_IVU_TIMEOUT K_HOURS(BT_MESH_IVU_HOURS) 33 34 struct bt_mesh_app_key { 35 u16_t net_idx; 36 u16_t app_idx; 37 bool updated; 38 struct bt_mesh_app_keys { 39 u8_t id; 40 u8_t val[16]; 41 } keys[2]; 42 }; 43 44 struct bt_mesh_subnet { 45 u32_t beacon_sent; /* Timestamp of last sent beacon */ 46 u8_t beacons_last; /* Number of beacons during last 47 * observation window 48 */ 49 u8_t beacons_cur; /* Number of beaconds observed during 50 * currently ongoing window. 51 */ 52 53 u8_t beacon_cache[21]; /* Cached last authenticated beacon */ 54 55 u16_t net_idx; /* NetKeyIndex */ 56 57 bool kr_flag; /* Key Refresh Flag */ 58 u8_t kr_phase; /* Key Refresh Phase */ 59 60 u8_t node_id; /* Node Identity State */ 61 u32_t node_id_start; /* Node Identity started timestamp */ 62 63 u8_t auth[8]; /* Beacon Authentication Value */ 64 65 struct bt_mesh_subnet_keys { 66 u8_t net[16]; /* NetKey */ 67 u8_t nid; /* NID */ 68 u8_t enc[16]; /* EncKey */ 69 u8_t net_id[8]; /* Network ID */ 70 #if (MYNEWT_VAL(BLE_MESH_GATT_PROXY)) 71 u8_t identity[16]; /* IdentityKey */ 72 #endif 73 u8_t privacy[16]; /* PrivacyKey */ 74 u8_t beacon[16]; /* BeaconKey */ 75 } keys[2]; 76 }; 77 78 struct bt_mesh_rpl { 79 u16_t src; 80 bool old_iv; 81 #if defined(CONFIG_BT_SETTINGS) 82 bool store; 83 #endif 84 u32_t seq; 85 }; 86 87 #if MYNEWT_VAL(BLE_MESH_FRIEND) 88 #define FRIEND_SEG_RX MYNEWT_VAL(BLE_MESH_FRIEND_SEG_RX) 89 #define FRIEND_SUB_LIST_SIZE MYNEWT_VAL(BLE_MESH_FRIEND_SUB_LIST_SIZE) 90 #else 91 #define FRIEND_SEG_RX 0 92 #define FRIEND_SUB_LIST_SIZE 0 93 #endif 94 95 struct bt_mesh_friend { 96 u16_t lpn; 97 u8_t recv_delay; 98 u8_t fsn:1, 99 send_last:1, 100 pending_req:1, 101 sec_update:1, 102 pending_buf:1, 103 valid:1, 104 established:1; 105 s32_t poll_to; 106 u16_t lpn_counter; 107 u16_t counter; 108 109 u16_t net_idx; 110 111 u16_t sub_list[FRIEND_SUB_LIST_SIZE]; 112 113 struct k_delayed_work timer; 114 115 struct bt_mesh_friend_seg { 116 struct net_buf_slist_t queue; 117 } seg[FRIEND_SEG_RX]; 118 119 struct os_mbuf *last; 120 121 struct net_buf_slist_t queue; 122 u32_t queue_size; 123 124 /* Friend Clear Procedure */ 125 struct { 126 u32_t start; /* Clear Procedure start */ 127 u16_t frnd; /* Previous Friend's address */ 128 u16_t repeat_sec; /* Repeat timeout in seconds */ 129 struct k_delayed_work timer; /* Repeat timer */ 130 } clear; 131 }; 132 133 #if (MYNEWT_VAL(BLE_MESH_LOW_POWER)) 134 #define LPN_GROUPS CONFIG_BT_MESH_LPN_GROUPS 135 #else 136 #define LPN_GROUPS 0 137 #endif 138 139 /* Low Power Node state */ 140 struct bt_mesh_lpn { 141 enum __packed { 142 BT_MESH_LPN_DISABLED, /* LPN feature is disabled */ 143 BT_MESH_LPN_CLEAR, /* Clear in progress */ 144 BT_MESH_LPN_TIMER, /* Waiting for auto timer expiry */ 145 BT_MESH_LPN_ENABLED, /* LPN enabled, but no Friend */ 146 BT_MESH_LPN_REQ_WAIT, /* Wait before scanning for offers */ 147 BT_MESH_LPN_WAIT_OFFER, /* Friend Req sent */ 148 BT_MESH_LPN_ESTABLISHED, /* Friendship established */ 149 BT_MESH_LPN_RECV_DELAY, /* Poll sent, waiting ReceiveDelay */ 150 BT_MESH_LPN_WAIT_UPDATE, /* Waiting for Update or message */ 151 } state; 152 153 /* Transaction Number (used for subscription list) */ 154 u8_t xact_next; 155 u8_t xact_pending; 156 u8_t sent_req; 157 158 /* Address of our Friend when we're a LPN. Unassigned if we don't 159 * have a friend yet. 160 */ 161 u16_t frnd; 162 163 /* Value from the friend offer */ 164 u8_t recv_win; 165 166 u8_t req_attempts; /* Number of Request attempts */ 167 168 s32_t poll_timeout; 169 170 u8_t groups_changed:1, /* Friend Subscription List needs updating */ 171 pending_poll:1, /* Poll to be sent after subscription */ 172 disable:1, /* Disable LPN after clearing */ 173 fsn:1, /* Friend Sequence Number */ 174 established:1, /* Friendship established */ 175 clear_success:1; /* Friend Clear Confirm received */ 176 177 /* Friend Queue Size */ 178 u8_t queue_size; 179 180 /* LPNCounter */ 181 u16_t counter; 182 183 /* Previous Friend of this LPN */ 184 u16_t old_friend; 185 186 /* Duration reported for last advertising packet */ 187 u16_t adv_duration; 188 189 /* Next LPN related action timer */ 190 struct k_delayed_work timer; 191 192 /* Subscribed groups */ 193 u16_t groups[LPN_GROUPS]; 194 195 /* Bit fields for tracking which groups the Friend knows about */ 196 ATOMIC_DEFINE(added, LPN_GROUPS); 197 ATOMIC_DEFINE(pending, LPN_GROUPS); 198 ATOMIC_DEFINE(to_remove, LPN_GROUPS); 199 }; 200 201 /* bt_mesh_net.flags, mainly used for pending storage actions */ 202 enum { 203 BT_MESH_RPL_PENDING, 204 BT_MESH_KEYS_PENDING, 205 BT_MESH_NET_PENDING, 206 BT_MESH_IV_PENDING, 207 BT_MESH_SEQ_PENDING, 208 BT_MESH_HB_PUB_PENDING, 209 BT_MESH_CFG_PENDING, 210 BT_MESH_MOD_PENDING, 211 212 /* Don't touch - intentionally last */ 213 BT_MESH_FLAG_COUNT, 214 }; 215 216 struct bt_mesh_net { 217 u32_t iv_index; /* Current IV Index */ 218 u32_t seq:24, /* Next outgoing sequence number */ 219 iv_update:1, /* 1 if IV Update in Progress */ 220 ivu_initiator:1, /* IV Update initiated by us */ 221 ivu_test:1, /* IV Update test mode */ 222 pending_update:1, /* Update blocked by SDU in progress */ 223 valid:1; /* 0 if unused */ 224 225 ATOMIC_DEFINE(flags, BT_MESH_FLAG_COUNT); 226 227 /* Local network interface */ 228 struct ble_npl_callout local_work; 229 struct net_buf_slist_t local_queue; 230 231 #if MYNEWT_VAL(BLE_MESH_FRIEND) 232 /* Friend state, unique for each LPN that we're Friends for */ 233 struct bt_mesh_friend frnd[MYNEWT_VAL(BLE_MESH_FRIEND_LPN_COUNT)]; 234 #endif 235 236 #if (MYNEWT_VAL(BLE_MESH_LOW_POWER)) 237 struct bt_mesh_lpn lpn; /* Low Power Node state */ 238 #endif 239 240 /* Number of hours in current IV Update state */ 241 u8_t ivu_duration; 242 243 /* Timer to track duration in current IV Update state */ 244 struct k_delayed_work ivu_timer; 245 246 u8_t dev_key[16]; 247 248 struct bt_mesh_app_key app_keys[MYNEWT_VAL(BLE_MESH_APP_KEY_COUNT)]; 249 250 struct bt_mesh_subnet sub[MYNEWT_VAL(BLE_MESH_SUBNET_COUNT)]; 251 252 struct bt_mesh_rpl rpl[MYNEWT_VAL(BLE_MESH_CRPL)]; 253 }; 254 255 /* Network interface */ 256 enum bt_mesh_net_if { 257 BT_MESH_NET_IF_ADV, 258 BT_MESH_NET_IF_LOCAL, 259 BT_MESH_NET_IF_PROXY, 260 BT_MESH_NET_IF_PROXY_CFG, 261 }; 262 263 /* Decoding context for Network/Transport data */ 264 struct bt_mesh_net_rx { 265 struct bt_mesh_subnet *sub; 266 struct bt_mesh_msg_ctx ctx; 267 u32_t seq; /* Sequence Number */ 268 u8_t old_iv:1, /* iv_index - 1 was used */ 269 new_key:1, /* Data was encrypted with updated key */ 270 friend_cred:1, /* Data was encrypted with friend cred */ 271 ctl:1, /* Network Control */ 272 net_if:2, /* Network interface */ 273 local_match:1, /* Matched a local element */ 274 friend_match:1; /* Matched an LPN we're friends for */ 275 s8_t rssi; 276 }; 277 278 /* Encoding context for Network/Transport data */ 279 struct bt_mesh_net_tx { 280 struct bt_mesh_subnet *sub; 281 struct bt_mesh_msg_ctx *ctx; 282 u16_t src; 283 u8_t xmit; 284 u8_t friend_cred:1, 285 aszmic:1, 286 aid:6; 287 }; 288 289 extern struct bt_mesh_net bt_mesh; 290 291 #define BT_MESH_NET_IVI_TX (bt_mesh.iv_index - bt_mesh.iv_update) 292 #define BT_MESH_NET_IVI_RX(rx) (bt_mesh.iv_index - (rx)->old_iv) 293 294 #define BT_MESH_NET_HDR_LEN 9 295 296 int bt_mesh_net_keys_create(struct bt_mesh_subnet_keys *keys, 297 const u8_t key[16]); 298 299 int bt_mesh_net_create(u16_t idx, u8_t flags, const u8_t key[16], 300 u32_t iv_index); 301 302 u8_t bt_mesh_net_flags(struct bt_mesh_subnet *sub); 303 304 bool bt_mesh_kr_update(struct bt_mesh_subnet *sub, u8_t new_kr, bool new_key); 305 306 void bt_mesh_net_revoke_keys(struct bt_mesh_subnet *sub); 307 308 int bt_mesh_net_beacon_update(struct bt_mesh_subnet *sub); 309 310 void bt_mesh_rpl_reset(void); 311 312 bool bt_mesh_net_iv_update(u32_t iv_index, bool iv_update); 313 314 void bt_mesh_net_sec_update(struct bt_mesh_subnet *sub); 315 316 struct bt_mesh_subnet *bt_mesh_subnet_get(u16_t net_idx); 317 318 struct bt_mesh_subnet *bt_mesh_subnet_find(const u8_t net_id[8], u8_t flags, 319 u32_t iv_index, const u8_t auth[8], 320 bool *new_key); 321 322 int bt_mesh_net_encode(struct bt_mesh_net_tx *tx, struct os_mbuf *buf, 323 bool proxy); 324 325 int bt_mesh_net_send(struct bt_mesh_net_tx *tx, struct os_mbuf *buf, 326 const struct bt_mesh_send_cb *cb, void *cb_data); 327 328 int bt_mesh_net_resend(struct bt_mesh_subnet *sub, struct os_mbuf *buf, 329 bool new_key, const struct bt_mesh_send_cb *cb, 330 void *cb_data); 331 332 int bt_mesh_net_decode(struct os_mbuf *data, enum bt_mesh_net_if net_if, 333 struct bt_mesh_net_rx *rx, struct os_mbuf *buf); 334 335 void bt_mesh_net_recv(struct os_mbuf *data, s8_t rssi, 336 enum bt_mesh_net_if net_if); 337 338 u32_t bt_mesh_next_seq(void); 339 340 void bt_mesh_net_start(void); 341 342 void bt_mesh_net_init(void); 343 344 /* Friendship Credential Management */ 345 struct friend_cred { 346 u16_t net_idx; 347 u16_t addr; 348 349 u16_t lpn_counter; 350 u16_t frnd_counter; 351 352 struct { 353 u8_t nid; /* NID */ 354 u8_t enc[16]; /* EncKey */ 355 u8_t privacy[16]; /* PrivacyKey */ 356 } cred[2]; 357 }; 358 359 int friend_cred_get(struct bt_mesh_subnet *sub, u16_t addr, u8_t *nid, 360 const u8_t **enc, const u8_t **priv); 361 int friend_cred_set(struct friend_cred *cred, u8_t idx, const u8_t net_key[16]); 362 void friend_cred_refresh(u16_t net_idx); 363 int friend_cred_update(struct bt_mesh_subnet *sub); 364 struct friend_cred *friend_cred_create(struct bt_mesh_subnet *sub, u16_t addr, 365 u16_t lpn_counter, u16_t frnd_counter); 366 void friend_cred_clear(struct friend_cred *cred); 367 int friend_cred_del(u16_t net_idx, u16_t addr); 368 369 #endif 370