net.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /* SPDX-License-Identifier: LGPL-2.1-or-later */
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2018-2019 Intel Corporation. All rights reserved.
  7. *
  8. *
  9. */
  10. #ifndef __packed
  11. #define __packed __attribute__((packed))
  12. #endif
  13. struct mesh_io;
  14. struct mesh_node;
  15. #define DEV_ID 0
  16. #define UNUSED_KEY_IDX 0xffff
  17. #define APP_AID_DEV 0x00
  18. #define CTL 0x80
  19. #define KEY_CACHE_SIZE 64
  20. #define FRND_CACHE_MAX 32
  21. #define MAX_UNSEG_LEN 15 /* msg_len == 11 + sizeof(MIC) */
  22. #define MAX_SEG_LEN 12 /* UnSeg length - 3 octets overhead */
  23. #define SEG_MAX(seg, len) ((!seg && len <= MAX_UNSEG_LEN) ? 0 : \
  24. (((len) - 1) / MAX_SEG_LEN))
  25. #define SEG_OFF(seg) ((seg) * MAX_SEG_LEN)
  26. #define MAX_SEG_TO_LEN(seg) ((seg) ? SEG_OFF((seg) + 1) : MAX_UNSEG_LEN)
  27. #define SEGMENTED 0x80
  28. #define UNSEGMENTED 0x00
  29. #define SEG_HDR_SHIFT 31
  30. #define IS_SEGMENTED(hdr) (!!((hdr) & (true << SEG_HDR_SHIFT)))
  31. #define KEY_ID_MASK 0x7f
  32. #define KEY_AID_MASK 0x3f
  33. #define KEY_ID_AKF 0x40
  34. #define KEY_AID_SHIFT 0
  35. #define AKF_HDR_SHIFT 30
  36. #define KEY_HDR_SHIFT 24
  37. #define HAS_APP_KEY(hdr) (!!((hdr) & (true << AKF_HDR_SHIFT)))
  38. #define OPCODE_MASK 0x7f
  39. #define OPCODE_HDR_SHIFT 24
  40. #define RELAY 0x80
  41. #define RELAY_HDR_SHIFT 23
  42. #define SZMIC 0x80
  43. #define SZMIC_HDR_SHIFT 23
  44. #define SEQ_ZERO_MASK 0x1fff
  45. #define SEQ_ZERO_HDR_SHIFT 10
  46. #define IS_RELAYED(hdr) (!!((hdr) & (true << RELAY_HDR_SHIFT)))
  47. #define HAS_MIC64(hdr) (!!((hdr) & (true << SZMIC_HDR_SHIFT)))
  48. #define SEG_MASK 0x1f
  49. #define SEGO_HDR_SHIFT 5
  50. #define SEGN_HDR_SHIFT 0
  51. #define SEG_TOTAL(hdr) (((hdr) >> SEGN_HDR_SHIFT) & SEG_MASK)
  52. /* Mask of Hdr bits which must be constant over entire incoming SAR message */
  53. /* (SEG || AKF || AID || SZMIC || SeqZero || SegN) */
  54. #define HDR_KEY_MASK ((true << SEG_HDR_SHIFT) | \
  55. (KEY_ID_MASK << KEY_HDR_SHIFT) | \
  56. (true << SZMIC_HDR_SHIFT) | \
  57. (SEQ_ZERO_MASK << SEQ_ZERO_HDR_SHIFT) | \
  58. (SEG_MASK << SEGN_HDR_SHIFT))
  59. #define HDR_ACK_MASK ((OPCODE_MASK << OPCODE_HDR_SHIFT) | \
  60. (SEQ_ZERO_MASK << SEQ_ZERO_HDR_SHIFT))
  61. #define MSG_CACHE_SIZE 70
  62. #define REPLAY_CACHE_SIZE 10
  63. /* Proxy Configuration Opcodes */
  64. #define PROXY_OP_SET_FILTER_TYPE 0x00
  65. #define PROXY_OP_FILTER_ADD 0x01
  66. #define PROXY_OP_FILTER_DEL 0x02
  67. #define PROXY_OP_FILTER_STATUS 0x03
  68. /* Proxy Filter Defines */
  69. #define PROXY_FILTER_ACCEPT_LIST 0x00
  70. #define PROXY_FILTER_REJECT_LIST 0x01
  71. /* Network Tranport Opcodes */
  72. #define NET_OP_SEG_ACKNOWLEDGE 0x00
  73. #define NET_OP_FRND_POLL 0x01
  74. #define NET_OP_FRND_UPDATE 0x02
  75. #define NET_OP_FRND_REQUEST 0x03
  76. #define NET_OP_FRND_OFFER 0x04
  77. #define NET_OP_FRND_CLEAR 0x05
  78. #define NET_OP_FRND_CLEAR_CONFIRM 0x06
  79. #define NET_OP_PROXY_SUB_ADD 0x07
  80. #define NET_OP_PROXY_SUB_REMOVE 0x08
  81. #define NET_OP_PROXY_SUB_CONFIRM 0x09
  82. #define NET_OP_HEARTBEAT 0x0a
  83. #define FRND_OPCODE(x) \
  84. ((x) >= NET_OP_FRND_POLL && (x) <= NET_OP_FRND_CLEAR_CONFIRM)
  85. #define DEFAULT_MIN_DELAY 0
  86. #define DEFAULT_MAX_DELAY 25
  87. struct mesh_net_prov_caps {
  88. uint8_t num_ele;
  89. uint16_t algorithms;
  90. uint8_t pub_type;
  91. uint8_t static_type;
  92. uint8_t output_size;
  93. uint16_t output_action;
  94. uint8_t input_size;
  95. uint16_t input_action;
  96. } __packed;
  97. struct mesh_net_heartbeat_sub {
  98. struct l_timeout *timer;
  99. uint32_t start;
  100. uint32_t period;
  101. uint16_t features;
  102. uint16_t src;
  103. uint16_t dst;
  104. uint16_t count;
  105. bool enabled;
  106. uint8_t min_hops;
  107. uint8_t max_hops;
  108. };
  109. struct mesh_net_heartbeat_pub {
  110. struct l_timeout *timer;
  111. uint32_t period;
  112. uint16_t dst;
  113. uint16_t count;
  114. uint16_t features;
  115. uint16_t net_idx;
  116. uint8_t ttl;
  117. };
  118. struct mesh_key_set {
  119. bool frnd;
  120. uint8_t nid;
  121. uint8_t enc_key[16];
  122. uint8_t privacy_key[16];
  123. };
  124. struct friend_neg {
  125. int8_t rssi;
  126. bool clearing;
  127. };
  128. struct friend_act {
  129. uint16_t *grp_list;
  130. uint32_t last_hdr;
  131. int16_t grp_cnt;
  132. bool seq;
  133. bool last;
  134. };
  135. struct mesh_friend {
  136. struct mesh_net *net;
  137. struct l_timeout *timeout;
  138. struct l_queue *pkt_cache;
  139. void *pkt;
  140. uint32_t poll_timeout;
  141. uint32_t net_key_cur;
  142. uint32_t net_key_upd;
  143. uint16_t old_friend;
  144. uint16_t net_idx;
  145. uint16_t lp_addr;/* dst; * Primary Element unicast addr */
  146. uint16_t fn_cnt;
  147. uint16_t lp_cnt;
  148. uint8_t receive_delay;
  149. uint8_t ele_cnt;
  150. uint8_t frd;
  151. uint8_t frw;
  152. union {
  153. struct friend_neg negotiate;
  154. struct friend_act active;
  155. } u;
  156. };
  157. struct mesh_frnd_pkt {
  158. uint32_t iv_index;
  159. uint32_t seq;
  160. uint16_t src;
  161. uint16_t dst;
  162. uint16_t size;
  163. uint8_t segN;
  164. uint8_t segO;
  165. uint8_t ttl;
  166. uint8_t tc;
  167. bool szmict;
  168. union {
  169. struct {
  170. uint8_t key_id;
  171. } m;
  172. struct {
  173. uint16_t seq0;
  174. } a;
  175. struct {
  176. uint8_t opcode;
  177. } c;
  178. } u;
  179. uint8_t data[];
  180. };
  181. struct mesh_friend_seg_one {
  182. uint32_t hdr;
  183. uint32_t seq;
  184. bool sent;
  185. bool md;
  186. uint8_t data[15];
  187. };
  188. struct mesh_friend_seg_12 {
  189. uint32_t hdr;
  190. uint32_t seq;
  191. bool sent;
  192. bool md;
  193. uint8_t data[12];
  194. };
  195. struct mesh_friend_msg {
  196. uint32_t iv_index;
  197. uint32_t flags;
  198. uint16_t src;
  199. uint16_t dst;
  200. uint8_t ttl;
  201. uint8_t cnt_in;
  202. uint8_t cnt_out;
  203. uint8_t last_len;
  204. bool done;
  205. bool ctl;
  206. union {
  207. struct mesh_friend_seg_one one[1]; /* Single segment */
  208. struct mesh_friend_seg_12 s12[0]; /* Array of segments */
  209. } u;
  210. };
  211. typedef void (*mesh_status_func_t)(void *user_data, bool result);
  212. struct mesh_net *mesh_net_new(struct mesh_node *node);
  213. void mesh_net_free(void *net);
  214. void mesh_net_cleanup(void);
  215. void mesh_net_set_iv_index(struct mesh_net *net, uint32_t index, bool update);
  216. bool mesh_net_iv_index_update(struct mesh_net *net);
  217. bool mesh_net_set_seq_num(struct mesh_net *net, uint32_t number);
  218. uint32_t mesh_net_get_seq_num(struct mesh_net *net);
  219. uint32_t mesh_net_next_seq_num(struct mesh_net *net);
  220. bool mesh_net_set_default_ttl(struct mesh_net *net, uint8_t ttl);
  221. uint8_t mesh_net_get_default_ttl(struct mesh_net *net);
  222. bool mesh_net_get_frnd_seq(struct mesh_net *net);
  223. void mesh_net_set_frnd_seq(struct mesh_net *net, bool seq);
  224. uint16_t mesh_net_get_address(struct mesh_net *net);
  225. bool mesh_net_register_unicast(struct mesh_net *net,
  226. uint16_t unicast, uint8_t num_ele);
  227. void net_local_beacon(uint32_t key_id, uint8_t *beacon);
  228. bool mesh_net_set_beacon_mode(struct mesh_net *net, bool enable);
  229. bool mesh_net_set_proxy_mode(struct mesh_net *net, bool enable);
  230. bool mesh_net_set_relay_mode(struct mesh_net *net, bool enable, uint8_t cnt,
  231. uint8_t interval);
  232. bool mesh_net_set_friend_mode(struct mesh_net *net, bool enable);
  233. int mesh_net_del_key(struct mesh_net *net, uint16_t net_idx);
  234. int mesh_net_add_key(struct mesh_net *net, uint16_t net_idx,
  235. const uint8_t *key);
  236. int mesh_net_update_key(struct mesh_net *net, uint16_t net_idx,
  237. const uint8_t *key);
  238. bool mesh_net_set_key(struct mesh_net *net, uint16_t idx, const uint8_t *key,
  239. const uint8_t *new_key, uint8_t phase);
  240. uint32_t mesh_net_get_iv_index(struct mesh_net *net);
  241. void mesh_net_get_snb_state(struct mesh_net *net,
  242. uint8_t *flags, uint32_t *iv_index);
  243. bool mesh_net_get_key(struct mesh_net *net, bool new_key, uint16_t idx,
  244. uint32_t *key_id);
  245. bool mesh_net_attach(struct mesh_net *net, struct mesh_io *io);
  246. struct mesh_io *mesh_net_detach(struct mesh_net *net);
  247. struct l_queue *mesh_net_get_app_keys(struct mesh_net *net);
  248. void mesh_net_transport_send(struct mesh_net *net, uint32_t key_id,
  249. uint16_t net_idx, uint32_t iv_index,
  250. uint8_t ttl, uint32_t seq, uint16_t src,
  251. uint16_t dst, const uint8_t *msg,
  252. uint16_t msg_len);
  253. bool mesh_net_app_send(struct mesh_net *net, bool frnd_cred, uint16_t src,
  254. uint16_t dst, uint8_t key_id, uint16_t net_idx,
  255. uint8_t ttl, uint8_t cnt, uint16_t interval,
  256. uint32_t seq, uint32_t iv_index, bool segmented,
  257. bool szmic, const void *msg, uint16_t msg_len);
  258. void mesh_net_ack_send(struct mesh_net *net, uint32_t key_id,
  259. uint32_t iv_index, uint8_t ttl, uint32_t seq,
  260. uint16_t src, uint16_t dst, bool rly,
  261. uint16_t seqZero, uint32_t ack_flags);
  262. int mesh_net_get_identity_mode(struct mesh_net *net, uint16_t idx,
  263. uint8_t *mode);
  264. bool mesh_net_dst_reg(struct mesh_net *net, uint16_t dst);
  265. bool mesh_net_dst_unreg(struct mesh_net *net, uint16_t dst);
  266. struct mesh_friend *mesh_friend_new(struct mesh_net *net, uint16_t dst,
  267. uint8_t ele_cnt, uint8_t frd,
  268. uint8_t frw, uint32_t fpt,
  269. uint16_t fn_cnt, uint16_t lp_cnt);
  270. void mesh_friend_free(void *frnd);
  271. bool mesh_friend_clear(struct mesh_net *net, struct mesh_friend *frnd);
  272. void mesh_friend_sub_add(struct mesh_net *net, uint16_t lpn, uint8_t ele_cnt,
  273. uint8_t grp_cnt,
  274. const uint8_t *list);
  275. void mesh_friend_sub_del(struct mesh_net *net, uint16_t lpn, uint8_t cnt,
  276. const uint8_t *del_list);
  277. int mesh_net_key_refresh_phase_set(struct mesh_net *net, uint16_t net_idx,
  278. uint8_t transition);
  279. int mesh_net_key_refresh_phase_get(struct mesh_net *net, uint16_t net_idx,
  280. uint8_t *phase);
  281. void mesh_net_send_seg(struct mesh_net *net, uint32_t key_id,
  282. uint32_t iv_index, uint8_t ttl, uint32_t seq,
  283. uint16_t src, uint16_t dst, uint32_t hdr,
  284. const void *seg, uint16_t seg_len);
  285. struct mesh_net_heartbeat_sub *mesh_net_get_heartbeat_sub(struct mesh_net *net);
  286. int mesh_net_set_heartbeat_sub(struct mesh_net *net, uint16_t src, uint16_t dst,
  287. uint8_t period_log);
  288. struct mesh_net_heartbeat_pub *mesh_net_get_heartbeat_pub(struct mesh_net *net);
  289. int mesh_net_set_heartbeat_pub(struct mesh_net *net, uint16_t dst,
  290. uint16_t features, uint16_t idx, uint8_t ttl,
  291. uint8_t count_log, uint8_t period_log);
  292. bool mesh_net_key_list_get(struct mesh_net *net, uint8_t *buf, uint16_t *count);
  293. uint16_t mesh_net_get_primary_idx(struct mesh_net *net);
  294. uint32_t mesh_net_friend_timeout(struct mesh_net *net, uint16_t addr);
  295. struct mesh_io *mesh_net_get_io(struct mesh_net *net);
  296. struct mesh_node *mesh_net_node_get(struct mesh_net *net);
  297. bool mesh_net_have_key(struct mesh_net *net, uint16_t net_idx);
  298. bool mesh_net_is_local_address(struct mesh_net *net, uint16_t src,
  299. uint16_t count);
  300. void mesh_net_transmit_params_set(struct mesh_net *net, uint8_t count,
  301. uint16_t interval);
  302. void mesh_net_transmit_params_get(struct mesh_net *net, uint8_t *count,
  303. uint16_t *interval);
  304. struct mesh_prov *mesh_net_get_prov(struct mesh_net *net);
  305. void mesh_net_set_prov(struct mesh_net *net, struct mesh_prov *prov);
  306. uint32_t mesh_net_get_instant(struct mesh_net *net);
  307. struct l_queue *mesh_net_get_friends(struct mesh_net *net);
  308. struct l_queue *mesh_net_get_negotiations(struct mesh_net *net);
  309. bool mesh_net_load_rpl(struct mesh_net *net);