1 /** @file
2  *  @brief Internal APIs for Bluetooth connection handling.
3  */
4 
5 /*
6  * Copyright (c) 2015 Intel Corporation
7  *
8  * SPDX-License-Identifier: Apache-2.0
9  */
10 typedef enum __packed {
11 	BT_CONN_DISCONNECTED,
12 	BT_CONN_CONNECT_SCAN,
13 	BT_CONN_CONNECT_AUTO,
14 	BT_CONN_CONNECT_ADV,
15 	BT_CONN_CONNECT_DIR_ADV,
16 	BT_CONN_CONNECT,
17 	BT_CONN_CONNECTED,
18 	BT_CONN_DISCONNECT,
19 } bt_conn_state_t;
20 
21 /* bt_conn flags: the flags defined here represent connection parameters */
22 enum {
23 	BT_CONN_AUTO_CONNECT,
24 	BT_CONN_BR_LEGACY_SECURE,	/* 16 digits legacy PIN tracker */
25 	BT_CONN_USER,			/* user I/O when pairing */
26 	BT_CONN_BR_PAIRING,		/* BR connection in pairing context */
27 	BT_CONN_BR_NOBOND,		/* SSP no bond pairing tracker */
28 	BT_CONN_BR_PAIRING_INITIATOR,	/* local host starts authentication */
29 	BT_CONN_CLEANUP,                /* Disconnected, pending cleanup */
30 	BT_CONN_AUTO_PHY_UPDATE,        /* Auto-update PHY */
31 	BT_CONN_SLAVE_PARAM_UPDATE,	/* If slave param update timer fired */
32 	BT_CONN_SLAVE_PARAM_SET,	/* If slave param were set from app */
33 	BT_CONN_SLAVE_PARAM_L2CAP,	/* If should force L2CAP for CPUP */
34 	BT_CONN_FORCE_PAIR,             /* Pairing even with existing keys. */
35 
36 	BT_CONN_AUTO_PHY_COMPLETE,      /* Auto-initiated PHY procedure done */
37 	BT_CONN_AUTO_FEATURE_EXCH,	/* Auto-initiated LE Feat done */
38 	BT_CONN_AUTO_VERSION_INFO,      /* Auto-initiated LE version done */
39 	BT_CONN_AUTO_DATA_LEN_COMPLETE, /* Auto-initiated Data Length done */
40 
41 	/* Total number of flags - must be at the end of the enum */
42 	BT_CONN_NUM_FLAGS,
43 };
44 
45 struct bt_conn_le {
46 	bt_addr_le_t		dst;
47 
48 	bt_addr_le_t		init_addr;
49 	bt_addr_le_t		resp_addr;
50 
51 	u16_t			interval;
52 	u16_t			interval_min;
53 	u16_t			interval_max;
54 
55 	u16_t			latency;
56 	u16_t			timeout;
57 	u16_t			pending_latency;
58 	u16_t			pending_timeout;
59 
60 	u8_t			features[8];
61 
62 	struct bt_keys		*keys;
63 
64 #if defined(CONFIG_BT_USER_PHY_UPDATE)
65 	struct bt_conn_le_phy_info      phy;
66 #endif
67 
68 #if defined(CONFIG_BT_USER_DATA_LEN_UPDATE)
69 	struct bt_conn_le_data_len_info data_len;
70 #endif
71 };
72 
73 #if defined(CONFIG_BT_BREDR)
74 /* For now reserve space for 2 pages of LMP remote features */
75 #define LMP_MAX_PAGES 2
76 
77 struct bt_conn_br {
78 	bt_addr_t		dst;
79 	u8_t			remote_io_capa;
80 	u8_t			remote_auth;
81 	u8_t			pairing_method;
82 	/* remote LMP features pages per 8 bytes each */
83 	u8_t			features[LMP_MAX_PAGES][8];
84 
85 	struct bt_keys_link_key	*link_key;
86 };
87 
88 struct bt_conn_sco {
89 	/* Reference to ACL Connection */
90 	struct bt_conn          *acl;
91 	u16_t                pkt_type;
92 };
93 #endif
94 
95 typedef void (*bt_conn_tx_cb_t)(struct bt_conn *conn, void *user_data);
96 
97 struct bt_conn_tx {
98 	sys_snode_t node;
99 
100 	bt_conn_tx_cb_t cb;
101 	void *user_data;
102 
103 	/* Number of pending packets without a callback after this one */
104 	bt_u32_t pending_no_cb;
105 };
106 
107 struct bt_conn {
108 	u16_t			handle;
109 	u8_t			type;
110 	u8_t			role;
111 
112 	ATOMIC_DEFINE(flags, BT_CONN_NUM_FLAGS);
113 
114 	/* Which local identity address this connection uses */
115 	u8_t                    id;
116 
117 #if defined(CONFIG_BT_SMP) || defined(CONFIG_BT_BREDR)
118 	bt_security_t		sec_level;
119 	bt_security_t		required_sec_level;
120 	u8_t			encrypt;
121 #endif /* CONFIG_BT_SMP || CONFIG_BT_BREDR */
122 
123 	/* Connection error or reason for disconnect */
124 	u8_t			err;
125 
126 	bt_conn_state_t		state;
127 
128 	u16_t		        rx_len;
129 	struct net_buf		*rx;
130 
131 	/* Sent but not acknowledged TX packets with a callback */
132 	sys_slist_t		tx_pending;
133 	/* Sent but not acknowledged TX packets without a callback before
134 	 * the next packet (if any) in tx_pending.
135 	 */
136 	bt_u32_t                   pending_no_cb;
137 
138 	/* Completed TX for which we need to call the callback */
139 	sys_slist_t		tx_complete;
140 	struct k_work           tx_complete_work;
141 
142 
143 	/* Queue for outgoing ACL data */
144 	struct kfifo		tx_queue;
145 
146 	/* Active L2CAP channels */
147 	sys_slist_t		channels;
148 
149 	atomic_t		ref;
150 
151 	/* Delayed work for connection update and other deferred tasks */
152 	struct k_delayed_work	update_work;
153 
154 	union {
155 		struct bt_conn_le	le;
156 #if defined(CONFIG_BT_BREDR)
157 		struct bt_conn_br	br;
158 		struct bt_conn_sco	sco;
159 #endif
160 	};
161 
162 #if defined(CONFIG_BT_REMOTE_VERSION)
163 	struct bt_conn_rv {
164 		u8_t  version;
165 		u16_t manufacturer;
166 		u16_t subversion;
167 	} rv;
168 #endif
169 };
170 
171 /* Process incoming data for a connection */
172 void bt_conn_recv(struct bt_conn *conn, struct net_buf *buf, u8_t flags);
173 
174 /* Send data over a connection */
175 int bt_conn_send_cb(struct bt_conn *conn, struct net_buf *buf,
176 		    bt_conn_tx_cb_t cb, void *user_data);
177 
bt_conn_send(struct bt_conn * conn,struct net_buf * buf)178 static inline int bt_conn_send(struct bt_conn *conn, struct net_buf *buf)
179 {
180 	return bt_conn_send_cb(conn, buf, NULL, NULL);
181 }
182 
183 /* Check if a connection object with the peer already exists */
184 bool bt_conn_exists_le(u8_t id, const bt_addr_le_t *peer);
185 
186 /* Add a new LE connection */
187 struct bt_conn *bt_conn_add_le(u8_t id, const bt_addr_le_t *peer);
188 
189 /* Add a new BR/EDR connection */
190 struct bt_conn *bt_conn_add_br(const bt_addr_t *peer);
191 
192 /* Add a new SCO connection */
193 struct bt_conn *bt_conn_add_sco(const bt_addr_t *peer, int link_type);
194 
195 /* Cleanup SCO references */
196 void bt_sco_cleanup(struct bt_conn *sco_conn);
197 
198 /* Look up an existing sco connection by BT address */
199 struct bt_conn *bt_conn_lookup_addr_sco(const bt_addr_t *peer);
200 
201 /* Look up an existing connection by BT address */
202 struct bt_conn *bt_conn_lookup_addr_br(const bt_addr_t *peer);
203 
204 void bt_conn_pin_code_req(struct bt_conn *conn);
205 u8_t bt_conn_get_io_capa(void);
206 u8_t bt_conn_ssp_get_auth(const struct bt_conn *conn);
207 void bt_conn_ssp_auth(struct bt_conn *conn, bt_u32_t passkey);
208 void bt_conn_ssp_auth_complete(struct bt_conn *conn, u8_t status);
209 
210 void bt_conn_disconnect_all(u8_t id);
211 
212 /* Look up an existing connection */
213 struct bt_conn *bt_conn_lookup_handle(u16_t handle);
214 
215 /* Check if the connection is with the given peer. */
216 bool bt_conn_is_peer_addr_le(const struct bt_conn *conn, u8_t id,
217 			     const bt_addr_le_t *peer);
218 
219 /* Helpers for identifying & looking up connections based on the the index to
220  * the connection list. This is useful for O(1) lookups, but can't be used
221  * e.g. as the handle since that's assigned to us by the controller.
222  */
223 #define BT_CONN_INDEX_INVALID 0xff
224 struct bt_conn *bt_conn_lookup_index(u8_t index);
225 
226 /* Look up a connection state. For BT_ADDR_LE_ANY, returns the first connection
227  * with the specific state
228  */
229 struct bt_conn *bt_conn_lookup_state_le(u8_t id, const bt_addr_le_t *peer,
230 					const bt_conn_state_t state);
231 
232 /* Set connection object in certain state and perform action related to state */
233 void bt_conn_set_state(struct bt_conn *conn, bt_conn_state_t state);
234 
235 int bt_conn_le_conn_update(struct bt_conn *conn,
236 			   const struct bt_le_conn_param *param);
237 
238 void notify_remote_info(struct bt_conn *conn);
239 
240 void notify_le_param_updated(struct bt_conn *conn);
241 
242 void notify_le_data_len_updated(struct bt_conn *conn);
243 
244 void notify_le_phy_updated(struct bt_conn *conn);
245 
246 bool le_param_req(struct bt_conn *conn, struct bt_le_conn_param *param);
247 
248 #if defined(CONFIG_BT_SMP)
249 /* rand and ediv should be in BT order */
250 int bt_conn_le_start_encryption(struct bt_conn *conn, u8_t rand[8],
251 				u8_t ediv[2], const u8_t *ltk, size_t len);
252 
253 /* Notify higher layers that RPA was resolved */
254 void bt_conn_identity_resolved(struct bt_conn *conn);
255 #endif /* CONFIG_BT_SMP */
256 
257 #if defined(CONFIG_BT_SMP) || defined(CONFIG_BT_BREDR)
258 /* Notify higher layers that connection security changed */
259 void bt_conn_security_changed(struct bt_conn *conn, enum bt_security_err err);
260 #endif /* CONFIG_BT_SMP || CONFIG_BT_BREDR */
261 
262 /* Prepare a PDU to be sent over a connection */
263 #if defined(CONFIG_NET_BUF_LOG)
264 struct net_buf *bt_conn_create_pdu_timeout_debug(struct net_buf_pool *pool,
265 						 size_t reserve,
266 						 k_timeout_t timeout,
267 						 const char *func, int line);
268 #define bt_conn_create_pdu_timeout(_pool, _reserve, _timeout) \
269 	bt_conn_create_pdu_timeout_debug(_pool, _reserve, _timeout, \
270 					 __func__, __LINE__)
271 
272 #define bt_conn_create_pdu(_pool, _reserve) \
273 	bt_conn_create_pdu_timeout_debug(_pool, _reserve, K_FOREVER, \
274 					 __func__, __line__)
275 #else
276 struct net_buf *bt_conn_create_pdu_timeout(struct net_buf_pool *pool,
277 					   size_t reserve, k_timeout_t timeout);
278 
279 #define bt_conn_create_pdu(_pool, _reserve) \
280 	bt_conn_create_pdu_timeout(_pool, _reserve, K_FOREVER)
281 #endif
282 
283 /* Prepare a PDU to be sent over a connection */
284 #if defined(CONFIG_NET_BUF_LOG)
285 struct net_buf *bt_conn_create_frag_timeout_debug(size_t reserve,
286 						  k_timeout_t timeout,
287 						  const char *func, int line);
288 
289 #define bt_conn_create_frag_timeout(_reserve, _timeout) \
290 	bt_conn_create_frag_timeout_debug(_reserve, _timeout, \
291 					  __func__, __LINE__)
292 
293 #define bt_conn_create_frag(_reserve) \
294 	bt_conn_create_frag_timeout_debug(_reserve, K_FOREVER, \
295 					  __func__, __LINE__)
296 #else
297 struct net_buf *bt_conn_create_frag_timeout(size_t reserve,
298 					    k_timeout_t timeout);
299 
300 #define bt_conn_create_frag(_reserve) \
301 	bt_conn_create_frag_timeout(_reserve, K_FOREVER)
302 #endif
303 
304 /* Initialize connection management */
305 int bt_conn_init(void);
306 
307 /* Selects based on connecton type right semaphore for ACL packets */
308 struct k_sem *bt_conn_get_pkts(struct bt_conn *conn);
309 
310 /* k_poll related helpers for the TX thread */
311 int bt_conn_prepare_events(struct k_poll_event events[]);
312 void bt_conn_process_tx(struct bt_conn *conn);
313