1/*
2   BlueZ - Bluetooth protocol stack for Linux
3   Copyright (C) 2000-2001 Qualcomm Incorporated
4
5   Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
6
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License version 2 as
9   published by the Free Software Foundation;
10
11   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
14   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
15   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
16   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
20   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
22   SOFTWARE IS DISCLAIMED.
23*/
24
25/*
26 * HCI Connection handling.
27 *
28 * $Id: hci_conn.c,v 1.1.1.1 2008/10/15 03:27:33 james26_jang Exp $
29 */
30
31#include <linux/config.h>
32#include <linux/module.h>
33
34#include <linux/types.h>
35#include <linux/errno.h>
36#include <linux/kernel.h>
37#include <linux/major.h>
38#include <linux/sched.h>
39#include <linux/slab.h>
40#include <linux/poll.h>
41#include <linux/fcntl.h>
42#include <linux/init.h>
43#include <linux/skbuff.h>
44#include <linux/interrupt.h>
45#include <linux/notifier.h>
46#include <net/sock.h>
47
48#include <asm/system.h>
49#include <asm/uaccess.h>
50#include <asm/unaligned.h>
51
52#include <net/bluetooth/bluetooth.h>
53#include <net/bluetooth/hci_core.h>
54
55#ifndef HCI_CORE_DEBUG
56#undef  BT_DBG
57#define BT_DBG( A... )
58#endif
59
60void hci_acl_connect(struct hci_conn *conn)
61{
62	struct hci_dev *hdev = conn->hdev;
63	struct inquiry_entry *ie;
64	create_conn_cp cp;
65
66	BT_DBG("%p", conn);
67
68	conn->state = BT_CONNECT;
69	conn->out   = 1;
70	conn->link_mode = HCI_LM_MASTER;
71
72	memset(&cp, 0, sizeof(cp));
73	bacpy(&cp.bdaddr, &conn->dst);
74
75	if ((ie = inquiry_cache_lookup(hdev, &conn->dst)) &&
76			inquiry_entry_age(ie) <= INQUIRY_ENTRY_AGE_MAX) {
77		cp.pscan_rep_mode = ie->info.pscan_rep_mode;
78		cp.pscan_mode     = ie->info.pscan_mode;
79		cp.clock_offset   = ie->info.clock_offset | __cpu_to_le16(0x8000);
80	}
81
82	cp.pkt_type = __cpu_to_le16(hdev->pkt_type & ACL_PTYPE_MASK);
83	if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER))
84		cp.role_switch	= 0x01;
85	else
86		cp.role_switch	= 0x00;
87
88	hci_send_cmd(hdev, OGF_LINK_CTL, OCF_CREATE_CONN,
89				CREATE_CONN_CP_SIZE, &cp);
90}
91
92void hci_acl_disconn(struct hci_conn *conn, __u8 reason)
93{
94	disconnect_cp cp;
95
96	BT_DBG("%p", conn);
97
98	conn->state = BT_DISCONN;
99
100	cp.handle = __cpu_to_le16(conn->handle);
101	cp.reason = reason;
102	hci_send_cmd(conn->hdev, OGF_LINK_CTL, OCF_DISCONNECT,
103				DISCONNECT_CP_SIZE, &cp);
104}
105
106void hci_add_sco(struct hci_conn *conn, __u16 handle)
107{
108	struct hci_dev *hdev = conn->hdev;
109	add_sco_cp cp;
110
111	BT_DBG("%p", conn);
112
113	conn->state = BT_CONNECT;
114	conn->out = 1;
115
116	cp.pkt_type = __cpu_to_le16(hdev->pkt_type & SCO_PTYPE_MASK);
117	cp.handle   = __cpu_to_le16(handle);
118
119	hci_send_cmd(hdev, OGF_LINK_CTL, OCF_ADD_SCO, ADD_SCO_CP_SIZE, &cp);
120}
121
122static void hci_conn_timeout(unsigned long arg)
123{
124	struct hci_conn *conn = (void *)arg;
125	struct hci_dev  *hdev = conn->hdev;
126
127	BT_DBG("conn %p state %d", conn, conn->state);
128
129	if (atomic_read(&conn->refcnt))
130		return;
131
132	hci_dev_lock(hdev);
133 	if (conn->state == BT_CONNECTED)
134		hci_acl_disconn(conn, 0x13);
135	else
136		conn->state = BT_CLOSED;
137	hci_dev_unlock(hdev);
138	return;
139}
140
141static void hci_conn_init_timer(struct hci_conn *conn)
142{
143	init_timer(&conn->timer);
144	conn->timer.function = hci_conn_timeout;
145	conn->timer.data = (unsigned long)conn;
146}
147
148struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst)
149{
150	struct hci_conn *conn;
151
152	BT_DBG("%s dst %s", hdev->name, batostr(dst));
153
154	if (!(conn = kmalloc(sizeof(struct hci_conn), GFP_ATOMIC)))
155		return NULL;
156	memset(conn, 0, sizeof(struct hci_conn));
157
158	bacpy(&conn->dst, dst);
159	conn->type   = type;
160	conn->hdev   = hdev;
161	conn->state  = BT_OPEN;
162
163	skb_queue_head_init(&conn->data_q);
164	hci_conn_init_timer(conn);
165
166	atomic_set(&conn->refcnt, 0);
167
168	hci_dev_hold(hdev);
169
170	tasklet_disable(&hdev->tx_task);
171	conn_hash_add(hdev, conn);
172	tasklet_enable(&hdev->tx_task);
173
174	return conn;
175}
176
177int hci_conn_del(struct hci_conn *conn)
178{
179	struct hci_dev  *hdev = conn->hdev;
180
181	BT_DBG("%s conn %p handle %d", hdev->name, conn, conn->handle);
182
183	hci_conn_del_timer(conn);
184
185	if (conn->type == SCO_LINK) {
186		struct hci_conn *acl = conn->link;
187		if (acl) {
188			acl->link = NULL;
189			hci_conn_put(acl);
190		}
191	} else {
192		struct hci_conn *sco = conn->link;
193		if (sco)
194			sco->link = NULL;
195
196		/* Unacked frames */
197		hdev->acl_cnt += conn->sent;
198	}
199
200	tasklet_disable(&hdev->tx_task);
201	conn_hash_del(hdev, conn);
202	tasklet_enable(&hdev->tx_task);
203
204	skb_queue_purge(&conn->data_q);
205
206	hci_dev_put(hdev);
207
208	kfree(conn);
209	return 0;
210}
211
212struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src)
213{
214	int use_src = bacmp(src, BDADDR_ANY);
215	struct hci_dev *hdev = NULL;
216	struct list_head *p;
217
218	BT_DBG("%s -> %s", batostr(src), batostr(dst));
219
220	read_lock_bh(&hdev_list_lock);
221
222	list_for_each(p, &hdev_list) {
223		struct hci_dev *d;
224		d = list_entry(p, struct hci_dev, list);
225
226		if (!test_bit(HCI_UP, &d->flags))
227			continue;
228
229		/* Simple routing:
230	 	 * 	No source address - find interface with bdaddr != dst
231	 	 *	Source address 	  - find interface with bdaddr == src
232	 	 */
233
234		if (use_src) {
235			if (!bacmp(&d->bdaddr, src)) {
236				hdev = d; break;
237			}
238		} else {
239			if (bacmp(&d->bdaddr, dst)) {
240				hdev = d; break;
241			}
242		}
243	}
244
245	if (hdev)
246		hci_dev_hold(hdev);
247
248	read_unlock_bh(&hdev_list_lock);
249	return hdev;
250}
251
252/* Create SCO or ACL connection.
253 * Device _must_ be locked */
254struct hci_conn * hci_connect(struct hci_dev *hdev, int type, bdaddr_t *dst)
255{
256	struct hci_conn *acl;
257
258	BT_DBG("%s dst %s", hdev->name, batostr(dst));
259
260	if (!(acl = conn_hash_lookup_ba(hdev, ACL_LINK, dst))) {
261		if (!(acl = hci_conn_add(hdev, ACL_LINK, dst)))
262			return NULL;
263	}
264
265	hci_conn_hold(acl);
266
267	if (acl->state == BT_OPEN || acl->state == BT_CLOSED)
268		hci_acl_connect(acl);
269
270	if (type == SCO_LINK) {
271		struct hci_conn *sco;
272
273		if (!(sco = conn_hash_lookup_ba(hdev, SCO_LINK, dst))) {
274			if (!(sco = hci_conn_add(hdev, SCO_LINK, dst))) {
275				hci_conn_put(acl);
276				return NULL;
277			}
278		}
279		acl->link = sco;
280		sco->link = acl;
281
282		hci_conn_hold(sco);
283
284		if (acl->state == BT_CONNECTED &&
285				(sco->state == BT_OPEN || sco->state == BT_CLOSED))
286			hci_add_sco(sco, acl->handle);
287
288		return sco;
289	} else {
290		return acl;
291	}
292}
293
294/* Authenticate remote device */
295int hci_conn_auth(struct hci_conn *conn)
296{
297	BT_DBG("conn %p", conn);
298
299	if (conn->link_mode & HCI_LM_AUTH)
300		return 1;
301
302	if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
303		auth_requested_cp ar;
304		ar.handle = __cpu_to_le16(conn->handle);
305		hci_send_cmd(conn->hdev, OGF_LINK_CTL, OCF_AUTH_REQUESTED,
306				AUTH_REQUESTED_CP_SIZE, &ar);
307	}
308	return 0;
309}
310
311/* Enable encryption */
312int hci_conn_encrypt(struct hci_conn *conn)
313{
314	BT_DBG("conn %p", conn);
315
316	if (conn->link_mode & HCI_LM_ENCRYPT)
317		return 1;
318
319	if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend))
320		return 0;
321
322	if (hci_conn_auth(conn)) {
323		set_conn_encrypt_cp ce;
324		ce.handle  = __cpu_to_le16(conn->handle);
325		ce.encrypt = 1;
326		hci_send_cmd(conn->hdev, OGF_LINK_CTL, OCF_SET_CONN_ENCRYPT,
327				SET_CONN_ENCRYPT_CP_SIZE, &ce);
328	}
329	return 0;
330}
331
332/* Drop all connection on the device */
333void hci_conn_hash_flush(struct hci_dev *hdev)
334{
335	struct conn_hash *h = &hdev->conn_hash;
336        struct list_head *p;
337
338	BT_DBG("hdev %s", hdev->name);
339
340	p = h->list.next;
341	while (p != &h->list) {
342		struct hci_conn *c;
343
344		c = list_entry(p, struct hci_conn, list);
345		p = p->next;
346
347		c->state = BT_CLOSED;
348
349		hci_proto_disconn_ind(c, 0x16);
350		hci_conn_del(c);
351	}
352}
353
354int hci_get_conn_list(unsigned long arg)
355{
356	struct hci_conn_list_req req, *cl;
357	struct hci_conn_info *ci;
358	struct hci_dev *hdev;
359	struct list_head *p;
360	int n = 0, size;
361
362	if (copy_from_user(&req, (void *) arg, sizeof(req)))
363		return -EFAULT;
364
365	if (!(hdev = hci_dev_get(req.dev_id)))
366		return -ENODEV;
367
368	size = req.conn_num * sizeof(struct hci_conn_info) + sizeof(req);
369
370	if (verify_area(VERIFY_WRITE, (void *)arg, size))
371		return -EFAULT;
372
373	if (!(cl = (void *) kmalloc(size, GFP_KERNEL)))
374		return -ENOMEM;
375	ci = cl->conn_info;
376
377	hci_dev_lock_bh(hdev);
378	list_for_each(p, &hdev->conn_hash.list) {
379		register struct hci_conn *c;
380		c = list_entry(p, struct hci_conn, list);
381
382		bacpy(&(ci + n)->bdaddr, &c->dst);
383		(ci + n)->handle = c->handle;
384		(ci + n)->type  = c->type;
385		(ci + n)->out   = c->out;
386		(ci + n)->state = c->state;
387		(ci + n)->link_mode = c->link_mode;
388		n++;
389	}
390	hci_dev_unlock_bh(hdev);
391
392	cl->dev_id = hdev->id;
393	cl->conn_num = n;
394	size = n * sizeof(struct hci_conn_info) + sizeof(req);
395
396	hci_dev_put(hdev);
397
398	copy_to_user((void *) arg, cl, size);
399	kfree(cl);
400
401	return 0;
402}
403
404int hci_get_conn_info(struct hci_dev *hdev, unsigned long arg)
405{
406	struct hci_conn_info_req req;
407	struct hci_conn_info ci;
408	struct hci_conn *conn;
409	char *ptr = (void *) arg + sizeof(req);
410
411	if (copy_from_user(&req, (void *) arg, sizeof(req)))
412		return -EFAULT;
413
414	if (verify_area(VERIFY_WRITE, ptr, sizeof(ci)))
415		return -EFAULT;
416
417	hci_dev_lock_bh(hdev);
418	conn = conn_hash_lookup_ba(hdev, req.type, &req.bdaddr);
419	if (conn) {
420		bacpy(&ci.bdaddr, &conn->dst);
421		ci.handle = conn->handle;
422		ci.type  = conn->type;
423		ci.out   = conn->out;
424		ci.state = conn->state;
425		ci.link_mode = conn->link_mode;
426	}
427	hci_dev_unlock_bh(hdev);
428
429	if (!conn)
430		return -ENOENT;
431
432	copy_to_user(ptr, &ci, sizeof(ci));
433	return 0;
434}
435