Bluetooth: Remove unneeded EXPORT_SYMBOL
[platform/adaptation/renesas_rcar/renesas_kernel.git] / net / bluetooth / hci_conn.c
1 /*
2    BlueZ - Bluetooth protocol stack for Linux
3    Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved.
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 /* Bluetooth HCI connection handling. */
26
27 #include <linux/export.h>
28
29 #include <net/bluetooth/bluetooth.h>
30 #include <net/bluetooth/hci_core.h>
31
32 static void hci_le_connect(struct hci_conn *conn)
33 {
34         struct hci_dev *hdev = conn->hdev;
35         struct hci_cp_le_create_conn cp;
36
37         conn->state = BT_CONNECT;
38         conn->out = true;
39         conn->link_mode |= HCI_LM_MASTER;
40         conn->sec_level = BT_SECURITY_LOW;
41
42         memset(&cp, 0, sizeof(cp));
43         cp.scan_interval = cpu_to_le16(0x0060);
44         cp.scan_window = cpu_to_le16(0x0030);
45         bacpy(&cp.peer_addr, &conn->dst);
46         cp.peer_addr_type = conn->dst_type;
47         cp.conn_interval_min = cpu_to_le16(0x0028);
48         cp.conn_interval_max = cpu_to_le16(0x0038);
49         cp.supervision_timeout = cpu_to_le16(0x002a);
50         cp.min_ce_len = cpu_to_le16(0x0000);
51         cp.max_ce_len = cpu_to_le16(0x0000);
52
53         hci_send_cmd(hdev, HCI_OP_LE_CREATE_CONN, sizeof(cp), &cp);
54 }
55
56 static void hci_le_connect_cancel(struct hci_conn *conn)
57 {
58         hci_send_cmd(conn->hdev, HCI_OP_LE_CREATE_CONN_CANCEL, 0, NULL);
59 }
60
61 void hci_acl_connect(struct hci_conn *conn)
62 {
63         struct hci_dev *hdev = conn->hdev;
64         struct inquiry_entry *ie;
65         struct hci_cp_create_conn cp;
66
67         BT_DBG("hcon %p", conn);
68
69         conn->state = BT_CONNECT;
70         conn->out = true;
71
72         conn->link_mode = HCI_LM_MASTER;
73
74         conn->attempt++;
75
76         conn->link_policy = hdev->link_policy;
77
78         memset(&cp, 0, sizeof(cp));
79         bacpy(&cp.bdaddr, &conn->dst);
80         cp.pscan_rep_mode = 0x02;
81
82         ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
83         if (ie) {
84                 if (inquiry_entry_age(ie) <= INQUIRY_ENTRY_AGE_MAX) {
85                         cp.pscan_rep_mode = ie->data.pscan_rep_mode;
86                         cp.pscan_mode     = ie->data.pscan_mode;
87                         cp.clock_offset   = ie->data.clock_offset |
88                                                         cpu_to_le16(0x8000);
89                 }
90
91                 memcpy(conn->dev_class, ie->data.dev_class, 3);
92                 if (ie->data.ssp_mode > 0)
93                         set_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
94         }
95
96         cp.pkt_type = cpu_to_le16(conn->pkt_type);
97         if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER))
98                 cp.role_switch = 0x01;
99         else
100                 cp.role_switch = 0x00;
101
102         hci_send_cmd(hdev, HCI_OP_CREATE_CONN, sizeof(cp), &cp);
103 }
104
105 static void hci_acl_connect_cancel(struct hci_conn *conn)
106 {
107         struct hci_cp_create_conn_cancel cp;
108
109         BT_DBG("%p", conn);
110
111         if (conn->hdev->hci_ver < BLUETOOTH_VER_1_2)
112                 return;
113
114         bacpy(&cp.bdaddr, &conn->dst);
115         hci_send_cmd(conn->hdev, HCI_OP_CREATE_CONN_CANCEL, sizeof(cp), &cp);
116 }
117
118 void hci_acl_disconn(struct hci_conn *conn, __u8 reason)
119 {
120         struct hci_cp_disconnect cp;
121
122         BT_DBG("%p", conn);
123
124         conn->state = BT_DISCONN;
125
126         cp.handle = cpu_to_le16(conn->handle);
127         cp.reason = reason;
128         hci_send_cmd(conn->hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp);
129 }
130
131 void hci_add_sco(struct hci_conn *conn, __u16 handle)
132 {
133         struct hci_dev *hdev = conn->hdev;
134         struct hci_cp_add_sco cp;
135
136         BT_DBG("%p", conn);
137
138         conn->state = BT_CONNECT;
139         conn->out = true;
140
141         conn->attempt++;
142
143         cp.handle   = cpu_to_le16(handle);
144         cp.pkt_type = cpu_to_le16(conn->pkt_type);
145
146         hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp);
147 }
148
149 void hci_setup_sync(struct hci_conn *conn, __u16 handle)
150 {
151         struct hci_dev *hdev = conn->hdev;
152         struct hci_cp_setup_sync_conn cp;
153
154         BT_DBG("%p", conn);
155
156         conn->state = BT_CONNECT;
157         conn->out = true;
158
159         conn->attempt++;
160
161         cp.handle   = cpu_to_le16(handle);
162         cp.pkt_type = cpu_to_le16(conn->pkt_type);
163
164         cp.tx_bandwidth   = cpu_to_le32(0x00001f40);
165         cp.rx_bandwidth   = cpu_to_le32(0x00001f40);
166         cp.max_latency    = cpu_to_le16(0xffff);
167         cp.voice_setting  = cpu_to_le16(hdev->voice_setting);
168         cp.retrans_effort = 0xff;
169
170         hci_send_cmd(hdev, HCI_OP_SETUP_SYNC_CONN, sizeof(cp), &cp);
171 }
172
173 void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max,
174                         u16 latency, u16 to_multiplier)
175 {
176         struct hci_cp_le_conn_update cp;
177         struct hci_dev *hdev = conn->hdev;
178
179         memset(&cp, 0, sizeof(cp));
180
181         cp.handle               = cpu_to_le16(conn->handle);
182         cp.conn_interval_min    = cpu_to_le16(min);
183         cp.conn_interval_max    = cpu_to_le16(max);
184         cp.conn_latency         = cpu_to_le16(latency);
185         cp.supervision_timeout  = cpu_to_le16(to_multiplier);
186         cp.min_ce_len           = cpu_to_le16(0x0001);
187         cp.max_ce_len           = cpu_to_le16(0x0001);
188
189         hci_send_cmd(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(cp), &cp);
190 }
191
192 void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __u8 rand[8],
193                       __u8 ltk[16])
194 {
195         struct hci_dev *hdev = conn->hdev;
196         struct hci_cp_le_start_enc cp;
197
198         BT_DBG("%p", conn);
199
200         memset(&cp, 0, sizeof(cp));
201
202         cp.handle = cpu_to_le16(conn->handle);
203         memcpy(cp.ltk, ltk, sizeof(cp.ltk));
204         cp.ediv = ediv;
205         memcpy(cp.rand, rand, sizeof(cp.rand));
206
207         hci_send_cmd(hdev, HCI_OP_LE_START_ENC, sizeof(cp), &cp);
208 }
209
210 /* Device _must_ be locked */
211 void hci_sco_setup(struct hci_conn *conn, __u8 status)
212 {
213         struct hci_conn *sco = conn->link;
214
215         BT_DBG("%p", conn);
216
217         if (!sco)
218                 return;
219
220         if (!status) {
221                 if (lmp_esco_capable(conn->hdev))
222                         hci_setup_sync(sco, conn->handle);
223                 else
224                         hci_add_sco(sco, conn->handle);
225         } else {
226                 hci_proto_connect_cfm(sco, status);
227                 hci_conn_del(sco);
228         }
229 }
230
231 static void hci_conn_timeout(struct work_struct *work)
232 {
233         struct hci_conn *conn = container_of(work, struct hci_conn,
234                                              disc_work.work);
235         __u8 reason;
236
237         BT_DBG("conn %p state %s", conn, state_to_string(conn->state));
238
239         if (atomic_read(&conn->refcnt))
240                 return;
241
242         switch (conn->state) {
243         case BT_CONNECT:
244         case BT_CONNECT2:
245                 if (conn->out) {
246                         if (conn->type == ACL_LINK)
247                                 hci_acl_connect_cancel(conn);
248                         else if (conn->type == LE_LINK)
249                                 hci_le_connect_cancel(conn);
250                 }
251                 break;
252         case BT_CONFIG:
253         case BT_CONNECTED:
254                 reason = hci_proto_disconn_ind(conn);
255                 hci_acl_disconn(conn, reason);
256                 break;
257         default:
258                 conn->state = BT_CLOSED;
259                 break;
260         }
261 }
262
263 /* Enter sniff mode */
264 static void hci_conn_enter_sniff_mode(struct hci_conn *conn)
265 {
266         struct hci_dev *hdev = conn->hdev;
267
268         BT_DBG("conn %p mode %d", conn, conn->mode);
269
270         if (test_bit(HCI_RAW, &hdev->flags))
271                 return;
272
273         if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn))
274                 return;
275
276         if (conn->mode != HCI_CM_ACTIVE || !(conn->link_policy & HCI_LP_SNIFF))
277                 return;
278
279         if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) {
280                 struct hci_cp_sniff_subrate cp;
281                 cp.handle             = cpu_to_le16(conn->handle);
282                 cp.max_latency        = cpu_to_le16(0);
283                 cp.min_remote_timeout = cpu_to_le16(0);
284                 cp.min_local_timeout  = cpu_to_le16(0);
285                 hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp);
286         }
287
288         if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) {
289                 struct hci_cp_sniff_mode cp;
290                 cp.handle       = cpu_to_le16(conn->handle);
291                 cp.max_interval = cpu_to_le16(hdev->sniff_max_interval);
292                 cp.min_interval = cpu_to_le16(hdev->sniff_min_interval);
293                 cp.attempt      = cpu_to_le16(4);
294                 cp.timeout      = cpu_to_le16(1);
295                 hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp);
296         }
297 }
298
299 static void hci_conn_idle(unsigned long arg)
300 {
301         struct hci_conn *conn = (void *) arg;
302
303         BT_DBG("conn %p mode %d", conn, conn->mode);
304
305         hci_conn_enter_sniff_mode(conn);
306 }
307
308 static void hci_conn_auto_accept(unsigned long arg)
309 {
310         struct hci_conn *conn = (void *) arg;
311         struct hci_dev *hdev = conn->hdev;
312
313         hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_REPLY, sizeof(conn->dst),
314                      &conn->dst);
315 }
316
317 struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst)
318 {
319         struct hci_conn *conn;
320
321         BT_DBG("%s dst %s", hdev->name, batostr(dst));
322
323         conn = kzalloc(sizeof(struct hci_conn), GFP_KERNEL);
324         if (!conn)
325                 return NULL;
326
327         bacpy(&conn->dst, dst);
328         conn->hdev  = hdev;
329         conn->type  = type;
330         conn->mode  = HCI_CM_ACTIVE;
331         conn->state = BT_OPEN;
332         conn->auth_type = HCI_AT_GENERAL_BONDING;
333         conn->io_capability = hdev->io_capability;
334         conn->remote_auth = 0xff;
335         conn->key_type = 0xff;
336
337         set_bit(HCI_CONN_POWER_SAVE, &conn->flags);
338         conn->disc_timeout = HCI_DISCONN_TIMEOUT;
339
340         switch (type) {
341         case ACL_LINK:
342                 conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK;
343                 break;
344         case SCO_LINK:
345                 if (lmp_esco_capable(hdev))
346                         conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
347                                         (hdev->esco_type & EDR_ESCO_MASK);
348                 else
349                         conn->pkt_type = hdev->pkt_type & SCO_PTYPE_MASK;
350                 break;
351         case ESCO_LINK:
352                 conn->pkt_type = hdev->esco_type & ~EDR_ESCO_MASK;
353                 break;
354         }
355
356         skb_queue_head_init(&conn->data_q);
357
358         INIT_LIST_HEAD(&conn->chan_list);
359
360         INIT_DELAYED_WORK(&conn->disc_work, hci_conn_timeout);
361         setup_timer(&conn->idle_timer, hci_conn_idle, (unsigned long)conn);
362         setup_timer(&conn->auto_accept_timer, hci_conn_auto_accept,
363                     (unsigned long) conn);
364
365         atomic_set(&conn->refcnt, 0);
366
367         hci_dev_hold(hdev);
368
369         hci_conn_hash_add(hdev, conn);
370         if (hdev->notify)
371                 hdev->notify(hdev, HCI_NOTIFY_CONN_ADD);
372
373         atomic_set(&conn->devref, 0);
374
375         hci_conn_init_sysfs(conn);
376
377         return conn;
378 }
379
380 int hci_conn_del(struct hci_conn *conn)
381 {
382         struct hci_dev *hdev = conn->hdev;
383
384         BT_DBG("%s conn %p handle %d", hdev->name, conn, conn->handle);
385
386         del_timer(&conn->idle_timer);
387
388         cancel_delayed_work_sync(&conn->disc_work);
389
390         del_timer(&conn->auto_accept_timer);
391
392         if (conn->type == ACL_LINK) {
393                 struct hci_conn *sco = conn->link;
394                 if (sco)
395                         sco->link = NULL;
396
397                 /* Unacked frames */
398                 hdev->acl_cnt += conn->sent;
399         } else if (conn->type == LE_LINK) {
400                 if (hdev->le_pkts)
401                         hdev->le_cnt += conn->sent;
402                 else
403                         hdev->acl_cnt += conn->sent;
404         } else {
405                 struct hci_conn *acl = conn->link;
406                 if (acl) {
407                         acl->link = NULL;
408                         hci_conn_put(acl);
409                 }
410         }
411
412         hci_chan_list_flush(conn);
413
414         hci_conn_hash_del(hdev, conn);
415         if (hdev->notify)
416                 hdev->notify(hdev, HCI_NOTIFY_CONN_DEL);
417
418         skb_queue_purge(&conn->data_q);
419
420         hci_conn_put_device(conn);
421
422         hci_dev_put(hdev);
423
424         if (conn->handle == 0)
425                 kfree(conn);
426
427         return 0;
428 }
429
430 struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src)
431 {
432         int use_src = bacmp(src, BDADDR_ANY);
433         struct hci_dev *hdev = NULL, *d;
434
435         BT_DBG("%s -> %s", batostr(src), batostr(dst));
436
437         read_lock(&hci_dev_list_lock);
438
439         list_for_each_entry(d, &hci_dev_list, list) {
440                 if (!test_bit(HCI_UP, &d->flags) ||
441                     test_bit(HCI_RAW, &d->flags))
442                         continue;
443
444                 /* Simple routing:
445                  *   No source address - find interface with bdaddr != dst
446                  *   Source address    - find interface with bdaddr == src
447                  */
448
449                 if (use_src) {
450                         if (!bacmp(&d->bdaddr, src)) {
451                                 hdev = d; break;
452                         }
453                 } else {
454                         if (bacmp(&d->bdaddr, dst)) {
455                                 hdev = d; break;
456                         }
457                 }
458         }
459
460         if (hdev)
461                 hdev = hci_dev_hold(hdev);
462
463         read_unlock(&hci_dev_list_lock);
464         return hdev;
465 }
466 EXPORT_SYMBOL(hci_get_route);
467
468 /* Create SCO, ACL or LE connection.
469  * Device _must_ be locked */
470 struct hci_conn *hci_connect(struct hci_dev *hdev, int type, bdaddr_t *dst,
471                              __u8 dst_type, __u8 sec_level, __u8 auth_type)
472 {
473         struct hci_conn *acl;
474         struct hci_conn *sco;
475         struct hci_conn *le;
476
477         BT_DBG("%s dst %s", hdev->name, batostr(dst));
478
479         if (type == LE_LINK) {
480                 le = hci_conn_hash_lookup_ba(hdev, LE_LINK, dst);
481                 if (!le) {
482                         le = hci_conn_add(hdev, LE_LINK, dst);
483                         if (!le)
484                                 return ERR_PTR(-ENOMEM);
485
486                         le->dst_type = bdaddr_to_le(dst_type);
487                         hci_le_connect(le);
488                 }
489
490                 le->pending_sec_level = sec_level;
491                 le->auth_type = auth_type;
492
493                 hci_conn_hold(le);
494
495                 return le;
496         }
497
498         acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
499         if (!acl) {
500                 acl = hci_conn_add(hdev, ACL_LINK, dst);
501                 if (!acl)
502                         return ERR_PTR(-ENOMEM);
503         }
504
505         hci_conn_hold(acl);
506
507         if (acl->state == BT_OPEN || acl->state == BT_CLOSED) {
508                 acl->sec_level = BT_SECURITY_LOW;
509                 acl->pending_sec_level = sec_level;
510                 acl->auth_type = auth_type;
511                 hci_acl_connect(acl);
512         }
513
514         if (type == ACL_LINK)
515                 return acl;
516
517         sco = hci_conn_hash_lookup_ba(hdev, type, dst);
518         if (!sco) {
519                 sco = hci_conn_add(hdev, type, dst);
520                 if (!sco) {
521                         hci_conn_put(acl);
522                         return ERR_PTR(-ENOMEM);
523                 }
524         }
525
526         acl->link = sco;
527         sco->link = acl;
528
529         hci_conn_hold(sco);
530
531         if (acl->state == BT_CONNECTED &&
532             (sco->state == BT_OPEN || sco->state == BT_CLOSED)) {
533                 set_bit(HCI_CONN_POWER_SAVE, &acl->flags);
534                 hci_conn_enter_active_mode(acl, BT_POWER_FORCE_ACTIVE_ON);
535
536                 if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->flags)) {
537                         /* defer SCO setup until mode change completed */
538                         set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->flags);
539                         return sco;
540                 }
541
542                 hci_sco_setup(acl, 0x00);
543         }
544
545         return sco;
546 }
547
548 /* Check link security requirement */
549 int hci_conn_check_link_mode(struct hci_conn *conn)
550 {
551         BT_DBG("conn %p", conn);
552
553         if (hci_conn_ssp_enabled(conn) && !(conn->link_mode & HCI_LM_ENCRYPT))
554                 return 0;
555
556         return 1;
557 }
558
559 /* Authenticate remote device */
560 static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
561 {
562         BT_DBG("conn %p", conn);
563
564         if (conn->pending_sec_level > sec_level)
565                 sec_level = conn->pending_sec_level;
566
567         if (sec_level > conn->sec_level)
568                 conn->pending_sec_level = sec_level;
569         else if (conn->link_mode & HCI_LM_AUTH)
570                 return 1;
571
572         /* Make sure we preserve an existing MITM requirement*/
573         auth_type |= (conn->auth_type & 0x01);
574
575         conn->auth_type = auth_type;
576
577         if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
578                 struct hci_cp_auth_requested cp;
579
580                 /* encrypt must be pending if auth is also pending */
581                 set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
582
583                 cp.handle = cpu_to_le16(conn->handle);
584                 hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED,
585                              sizeof(cp), &cp);
586                 if (conn->key_type != 0xff)
587                         set_bit(HCI_CONN_REAUTH_PEND, &conn->flags);
588         }
589
590         return 0;
591 }
592
593 /* Encrypt the the link */
594 static void hci_conn_encrypt(struct hci_conn *conn)
595 {
596         BT_DBG("conn %p", conn);
597
598         if (!test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) {
599                 struct hci_cp_set_conn_encrypt cp;
600                 cp.handle  = cpu_to_le16(conn->handle);
601                 cp.encrypt = 0x01;
602                 hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
603                              &cp);
604         }
605 }
606
607 /* Enable security */
608 int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
609 {
610         BT_DBG("conn %p", conn);
611
612         /* For sdp we don't need the link key. */
613         if (sec_level == BT_SECURITY_SDP)
614                 return 1;
615
616         /* For non 2.1 devices and low security level we don't need the link
617            key. */
618         if (sec_level == BT_SECURITY_LOW && !hci_conn_ssp_enabled(conn))
619                 return 1;
620
621         /* For other security levels we need the link key. */
622         if (!(conn->link_mode & HCI_LM_AUTH))
623                 goto auth;
624
625         /* An authenticated combination key has sufficient security for any
626            security level. */
627         if (conn->key_type == HCI_LK_AUTH_COMBINATION)
628                 goto encrypt;
629
630         /* An unauthenticated combination key has sufficient security for
631            security level 1 and 2. */
632         if (conn->key_type == HCI_LK_UNAUTH_COMBINATION &&
633             (sec_level == BT_SECURITY_MEDIUM || sec_level == BT_SECURITY_LOW))
634                 goto encrypt;
635
636         /* A combination key has always sufficient security for the security
637            levels 1 or 2. High security level requires the combination key
638            is generated using maximum PIN code length (16).
639            For pre 2.1 units. */
640         if (conn->key_type == HCI_LK_COMBINATION &&
641             (sec_level != BT_SECURITY_HIGH || conn->pin_length == 16))
642                 goto encrypt;
643
644 auth:
645         if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags))
646                 return 0;
647
648         if (!hci_conn_auth(conn, sec_level, auth_type))
649                 return 0;
650
651 encrypt:
652         if (conn->link_mode & HCI_LM_ENCRYPT)
653                 return 1;
654
655         hci_conn_encrypt(conn);
656         return 0;
657 }
658 EXPORT_SYMBOL(hci_conn_security);
659
660 /* Check secure link requirement */
661 int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level)
662 {
663         BT_DBG("conn %p", conn);
664
665         if (sec_level != BT_SECURITY_HIGH)
666                 return 1; /* Accept if non-secure is required */
667
668         if (conn->sec_level == BT_SECURITY_HIGH)
669                 return 1;
670
671         return 0; /* Reject not secure link */
672 }
673 EXPORT_SYMBOL(hci_conn_check_secure);
674
675 /* Change link key */
676 int hci_conn_change_link_key(struct hci_conn *conn)
677 {
678         BT_DBG("conn %p", conn);
679
680         if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
681                 struct hci_cp_change_conn_link_key cp;
682                 cp.handle = cpu_to_le16(conn->handle);
683                 hci_send_cmd(conn->hdev, HCI_OP_CHANGE_CONN_LINK_KEY,
684                              sizeof(cp), &cp);
685         }
686
687         return 0;
688 }
689
690 /* Switch role */
691 int hci_conn_switch_role(struct hci_conn *conn, __u8 role)
692 {
693         BT_DBG("conn %p", conn);
694
695         if (!role && conn->link_mode & HCI_LM_MASTER)
696                 return 1;
697
698         if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->flags)) {
699                 struct hci_cp_switch_role cp;
700                 bacpy(&cp.bdaddr, &conn->dst);
701                 cp.role = role;
702                 hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp);
703         }
704
705         return 0;
706 }
707 EXPORT_SYMBOL(hci_conn_switch_role);
708
709 /* Enter active mode */
710 void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active)
711 {
712         struct hci_dev *hdev = conn->hdev;
713
714         BT_DBG("conn %p mode %d", conn, conn->mode);
715
716         if (test_bit(HCI_RAW, &hdev->flags))
717                 return;
718
719         if (conn->mode != HCI_CM_SNIFF)
720                 goto timer;
721
722         if (!test_bit(HCI_CONN_POWER_SAVE, &conn->flags) && !force_active)
723                 goto timer;
724
725         if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) {
726                 struct hci_cp_exit_sniff_mode cp;
727                 cp.handle = cpu_to_le16(conn->handle);
728                 hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp);
729         }
730
731 timer:
732         if (hdev->idle_timeout > 0)
733                 mod_timer(&conn->idle_timer,
734                           jiffies + msecs_to_jiffies(hdev->idle_timeout));
735 }
736
737 /* Drop all connection on the device */
738 void hci_conn_hash_flush(struct hci_dev *hdev)
739 {
740         struct hci_conn_hash *h = &hdev->conn_hash;
741         struct hci_conn *c, *n;
742
743         BT_DBG("hdev %s", hdev->name);
744
745         list_for_each_entry_safe(c, n, &h->list, list) {
746                 c->state = BT_CLOSED;
747
748                 hci_proto_disconn_cfm(c, HCI_ERROR_LOCAL_HOST_TERM);
749                 hci_conn_del(c);
750         }
751 }
752
753 /* Check pending connect attempts */
754 void hci_conn_check_pending(struct hci_dev *hdev)
755 {
756         struct hci_conn *conn;
757
758         BT_DBG("hdev %s", hdev->name);
759
760         hci_dev_lock(hdev);
761
762         conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2);
763         if (conn)
764                 hci_acl_connect(conn);
765
766         hci_dev_unlock(hdev);
767 }
768
769 void hci_conn_hold_device(struct hci_conn *conn)
770 {
771         atomic_inc(&conn->devref);
772 }
773 EXPORT_SYMBOL(hci_conn_hold_device);
774
775 void hci_conn_put_device(struct hci_conn *conn)
776 {
777         if (atomic_dec_and_test(&conn->devref))
778                 hci_conn_del_sysfs(conn);
779 }
780 EXPORT_SYMBOL(hci_conn_put_device);
781
782 int hci_get_conn_list(void __user *arg)
783 {
784         struct hci_conn *c;
785         struct hci_conn_list_req req, *cl;
786         struct hci_conn_info *ci;
787         struct hci_dev *hdev;
788         int n = 0, size, err;
789
790         if (copy_from_user(&req, arg, sizeof(req)))
791                 return -EFAULT;
792
793         if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci))
794                 return -EINVAL;
795
796         size = sizeof(req) + req.conn_num * sizeof(*ci);
797
798         cl = kmalloc(size, GFP_KERNEL);
799         if (!cl)
800                 return -ENOMEM;
801
802         hdev = hci_dev_get(req.dev_id);
803         if (!hdev) {
804                 kfree(cl);
805                 return -ENODEV;
806         }
807
808         ci = cl->conn_info;
809
810         hci_dev_lock(hdev);
811         list_for_each_entry(c, &hdev->conn_hash.list, list) {
812                 bacpy(&(ci + n)->bdaddr, &c->dst);
813                 (ci + n)->handle = c->handle;
814                 (ci + n)->type  = c->type;
815                 (ci + n)->out   = c->out;
816                 (ci + n)->state = c->state;
817                 (ci + n)->link_mode = c->link_mode;
818                 if (++n >= req.conn_num)
819                         break;
820         }
821         hci_dev_unlock(hdev);
822
823         cl->dev_id = hdev->id;
824         cl->conn_num = n;
825         size = sizeof(req) + n * sizeof(*ci);
826
827         hci_dev_put(hdev);
828
829         err = copy_to_user(arg, cl, size);
830         kfree(cl);
831
832         return err ? -EFAULT : 0;
833 }
834
835 int hci_get_conn_info(struct hci_dev *hdev, void __user *arg)
836 {
837         struct hci_conn_info_req req;
838         struct hci_conn_info ci;
839         struct hci_conn *conn;
840         char __user *ptr = arg + sizeof(req);
841
842         if (copy_from_user(&req, arg, sizeof(req)))
843                 return -EFAULT;
844
845         hci_dev_lock(hdev);
846         conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr);
847         if (conn) {
848                 bacpy(&ci.bdaddr, &conn->dst);
849                 ci.handle = conn->handle;
850                 ci.type  = conn->type;
851                 ci.out   = conn->out;
852                 ci.state = conn->state;
853                 ci.link_mode = conn->link_mode;
854         }
855         hci_dev_unlock(hdev);
856
857         if (!conn)
858                 return -ENOENT;
859
860         return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0;
861 }
862
863 int hci_get_auth_info(struct hci_dev *hdev, void __user *arg)
864 {
865         struct hci_auth_info_req req;
866         struct hci_conn *conn;
867
868         if (copy_from_user(&req, arg, sizeof(req)))
869                 return -EFAULT;
870
871         hci_dev_lock(hdev);
872         conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
873         if (conn)
874                 req.type = conn->auth_type;
875         hci_dev_unlock(hdev);
876
877         if (!conn)
878                 return -ENOENT;
879
880         return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
881 }
882
883 struct hci_chan *hci_chan_create(struct hci_conn *conn)
884 {
885         struct hci_dev *hdev = conn->hdev;
886         struct hci_chan *chan;
887
888         BT_DBG("%s conn %p", hdev->name, conn);
889
890         chan = kzalloc(sizeof(struct hci_chan), GFP_KERNEL);
891         if (!chan)
892                 return NULL;
893
894         chan->conn = conn;
895         skb_queue_head_init(&chan->data_q);
896
897         list_add_rcu(&chan->list, &conn->chan_list);
898
899         return chan;
900 }
901
902 int hci_chan_del(struct hci_chan *chan)
903 {
904         struct hci_conn *conn = chan->conn;
905         struct hci_dev *hdev = conn->hdev;
906
907         BT_DBG("%s conn %p chan %p", hdev->name, conn, chan);
908
909         list_del_rcu(&chan->list);
910
911         synchronize_rcu();
912
913         skb_queue_purge(&chan->data_q);
914         kfree(chan);
915
916         return 0;
917 }
918
919 void hci_chan_list_flush(struct hci_conn *conn)
920 {
921         struct hci_chan *chan, *n;
922
923         BT_DBG("conn %p", conn);
924
925         list_for_each_entry_safe(chan, n, &conn->chan_list, list)
926                 hci_chan_del(chan);
927 }