2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2000-2001 Qualcomm Incorporated
5 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
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;
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.
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.
26 * BlueZ HCI socket layer.
28 * $Id: hci_sock.c,v 1.1.1.1 2005/04/11 02:51:12 jack Exp $
31 #include <linux/config.h>
32 #include <linux/module.h>
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/tqueue.h>
45 #include <linux/interrupt.h>
46 #include <linux/socket.h>
47 #include <linux/ioctl.h>
50 #include <asm/system.h>
51 #include <asm/uaccess.h>
53 #include <net/bluetooth/bluetooth.h>
54 #include <net/bluetooth/hci_core.h>
56 #ifndef HCI_SOCK_DEBUG
58 #define BT_DBG( A... )
61 /* ----- HCI socket interface ----- */
64 static struct hci_sec_filter hci_sec_filter = {
72 { 0x2a000002, 0x0, 0x0, 0x0 },
74 { 0x1200, 0x0, 0x0, 0x0 },
76 { 0x80100000, 0x2a, 0x0, 0x0 },
78 { 0x22a, 0x0, 0x0, 0x0 }
82 static struct bluez_sock_list hci_sk_list = {
83 lock: RW_LOCK_UNLOCKED
86 /* Send frame to RAW socket */
87 void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb)
91 BT_DBG("hdev %p len %d", hdev, skb->len);
93 read_lock(&hci_sk_list.lock);
94 for (sk = hci_sk_list.head; sk; sk = sk->next) {
95 struct hci_filter *flt;
98 if (sk->state != BT_BOUND || hci_pi(sk)->hdev != hdev)
101 /* Don't send frame to the socket it came from */
106 flt = &hci_pi(sk)->filter;
108 if (!hci_test_bit((skb->pkt_type & HCI_FLT_TYPE_BITS), &flt->type_mask))
111 if (skb->pkt_type == HCI_EVENT_PKT) {
112 register int evt = (*(__u8 *)skb->data & HCI_FLT_EVENT_BITS);
114 if (!hci_test_bit(evt, &flt->event_mask))
117 if (flt->opcode && ((evt == EVT_CMD_COMPLETE &&
118 flt->opcode != *(__u16 *)(skb->data + 3)) ||
119 (evt == EVT_CMD_STATUS &&
120 flt->opcode != *(__u16 *)(skb->data + 4))))
124 if (!(nskb = skb_clone(skb, GFP_ATOMIC)))
127 /* Put type byte before the data */
128 memcpy(skb_push(nskb, 1), &nskb->pkt_type, 1);
130 if (sock_queue_rcv_skb(sk, nskb))
133 read_unlock(&hci_sk_list.lock);
136 static int hci_sock_release(struct socket *sock)
138 struct sock *sk = sock->sk;
139 struct hci_dev *hdev = hci_pi(sk)->hdev;
141 BT_DBG("sock %p sk %p", sock, sk);
146 bluez_sock_unlink(&hci_sk_list, sk);
149 atomic_dec(&hdev->promisc);
155 skb_queue_purge(&sk->receive_queue);
156 skb_queue_purge(&sk->write_queue);
164 /* Ioctls that require bound socket */
165 static inline int hci_sock_bound_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg)
167 struct hci_dev *hdev = hci_pi(sk)->hdev;
174 if (!capable(CAP_NET_ADMIN))
178 set_bit(HCI_RAW, &hdev->flags);
180 clear_bit(HCI_RAW, &hdev->flags);
185 return hci_get_conn_info(hdev, arg);
189 return hdev->ioctl(hdev, cmd, arg);
194 static int hci_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
196 struct sock *sk = sock->sk;
199 BT_DBG("cmd %x arg %lx", cmd, arg);
203 return hci_get_dev_list(arg);
206 return hci_get_dev_info(arg);
209 return hci_get_conn_list(arg);
212 if (!capable(CAP_NET_ADMIN))
214 return hci_dev_open(arg);
217 if (!capable(CAP_NET_ADMIN))
219 return hci_dev_close(arg);
222 if (!capable(CAP_NET_ADMIN))
224 return hci_dev_reset(arg);
227 if (!capable(CAP_NET_ADMIN))
229 return hci_dev_reset_stat(arg);
239 if (!capable(CAP_NET_ADMIN))
241 return hci_dev_cmd(cmd, arg);
244 return hci_inquiry(arg);
248 err = hci_sock_bound_ioctl(sk, cmd, arg);
254 static int hci_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
256 struct sockaddr_hci *haddr = (struct sockaddr_hci *) addr;
257 struct sock *sk = sock->sk;
258 struct hci_dev *hdev = NULL;
261 BT_DBG("sock %p sk %p", sock, sk);
263 if (!haddr || haddr->hci_family != AF_BLUETOOTH)
268 if (hci_pi(sk)->hdev) {
273 if (haddr->hci_dev != HCI_DEV_NONE) {
274 if (!(hdev = hci_dev_get(haddr->hci_dev))) {
279 atomic_inc(&hdev->promisc);
282 hci_pi(sk)->hdev = hdev;
283 sk->state = BT_BOUND;
290 static int hci_sock_getname(struct socket *sock, struct sockaddr *addr, int *addr_len, int peer)
292 struct sockaddr_hci *haddr = (struct sockaddr_hci *) addr;
293 struct sock *sk = sock->sk;
295 BT_DBG("sock %p sk %p", sock, sk);
299 *addr_len = sizeof(*haddr);
300 haddr->hci_family = AF_BLUETOOTH;
301 haddr->hci_dev = hci_pi(sk)->hdev->id;
307 static inline void hci_sock_cmsg(struct sock *sk, struct msghdr *msg, struct sk_buff *skb)
309 __u32 mask = hci_pi(sk)->cmsg_mask;
311 if (mask & HCI_CMSG_DIR)
312 put_cmsg(msg, SOL_HCI, HCI_CMSG_DIR, sizeof(int), &bluez_cb(skb)->incomming);
314 if (mask & HCI_CMSG_TSTAMP)
315 put_cmsg(msg, SOL_HCI, HCI_CMSG_TSTAMP, sizeof(skb->stamp), &skb->stamp);
318 static int hci_sock_recvmsg(struct socket *sock, struct msghdr *msg, int len, int flags, struct scm_cookie *scm)
320 int noblock = flags & MSG_DONTWAIT;
321 struct sock *sk = sock->sk;
325 BT_DBG("sock %p, sk %p", sock, sk);
327 if (flags & (MSG_OOB))
330 if (sk->state == BT_CLOSED)
333 if (!(skb = skb_recv_datagram(sk, flags, noblock, &err)))
336 msg->msg_namelen = 0;
340 msg->msg_flags |= MSG_TRUNC;
344 skb->h.raw = skb->data;
345 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
347 hci_sock_cmsg(sk, msg, skb);
349 skb_free_datagram(sk, skb);
351 return err ? : copied;
354 static int hci_sock_sendmsg(struct socket *sock, struct msghdr *msg, int len,
355 struct scm_cookie *scm)
357 struct sock *sk = sock->sk;
358 struct hci_dev *hdev;
362 BT_DBG("sock %p sk %p", sock, sk);
364 if (msg->msg_flags & MSG_OOB)
367 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_NOSIGNAL|MSG_ERRQUEUE))
375 if (!(hdev = hci_pi(sk)->hdev)) {
380 if (!(skb = bluez_skb_send_alloc(sk, len, msg->msg_flags & MSG_DONTWAIT, &err)))
383 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
388 skb->pkt_type = *((unsigned char *) skb->data);
391 if (!capable(CAP_NET_RAW)) {
394 if (skb->pkt_type == HCI_COMMAND_PKT) {
395 __u16 opcode = __le16_to_cpu(*(__u16 *)skb->data);
396 __u16 ogf = cmd_opcode_ogf(opcode) - 1;
397 __u16 ocf = cmd_opcode_ocf(opcode) & HCI_FLT_OCF_BITS;
399 if (ogf > HCI_SFLT_MAX_OGF ||
400 !hci_test_bit(ocf, &hci_sec_filter.ocf_mask[ogf]))
406 /* Send frame to HCI core */
407 skb->dev = (void *) hdev;
420 int hci_sock_setsockopt(struct socket *sock, int level, int optname, char *optval, int len)
422 struct sock *sk = sock->sk;
423 struct hci_filter flt = { opcode: 0 };
424 int err = 0, opt = 0;
426 BT_DBG("sk %p, opt %d", sk, optname);
432 if (get_user(opt, (int *)optval)) {
438 hci_pi(sk)->cmsg_mask |= HCI_CMSG_DIR;
440 hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_DIR;
444 if (get_user(opt, (int *)optval)) {
450 hci_pi(sk)->cmsg_mask |= HCI_CMSG_TSTAMP;
452 hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_TSTAMP;
456 len = MIN(len, sizeof(struct hci_filter));
457 if (copy_from_user(&flt, optval, len)) {
462 if (!capable(CAP_NET_RAW)) {
463 flt.type_mask &= hci_sec_filter.type_mask;
464 flt.event_mask[0] &= hci_sec_filter.event_mask[0];
465 flt.event_mask[1] &= hci_sec_filter.event_mask[1];
468 memcpy(&hci_pi(sk)->filter, &flt, len);
480 int hci_sock_getsockopt(struct socket *sock, int level, int optname, char *optval, int *optlen)
482 struct sock *sk = sock->sk;
485 if (get_user(len, optlen))
490 if (hci_pi(sk)->cmsg_mask & HCI_CMSG_DIR)
495 if (put_user(opt, optval))
500 if (hci_pi(sk)->cmsg_mask & HCI_CMSG_TSTAMP)
505 if (put_user(opt, optval))
510 len = MIN(len, sizeof(struct hci_filter));
511 if (copy_to_user(optval, &hci_pi(sk)->filter, len))
523 struct proto_ops hci_sock_ops = {
524 family: PF_BLUETOOTH,
525 release: hci_sock_release,
527 getname: hci_sock_getname,
528 sendmsg: hci_sock_sendmsg,
529 recvmsg: hci_sock_recvmsg,
530 ioctl: hci_sock_ioctl,
532 listen: sock_no_listen,
533 shutdown: sock_no_shutdown,
534 setsockopt: hci_sock_setsockopt,
535 getsockopt: hci_sock_getsockopt,
536 connect: sock_no_connect,
537 socketpair: sock_no_socketpair,
538 accept: sock_no_accept,
542 static int hci_sock_create(struct socket *sock, int protocol)
546 BT_DBG("sock %p", sock);
548 if (sock->type != SOCK_RAW)
549 return -ESOCKTNOSUPPORT;
551 sock->ops = &hci_sock_ops;
553 if (!(sk = sk_alloc(PF_BLUETOOTH, GFP_KERNEL, 1)))
556 sock->state = SS_UNCONNECTED;
557 sock_init_data(sock, sk);
559 memset(&sk->protinfo, 0, sizeof(struct hci_pinfo));
561 sk->protocol = protocol;
564 bluez_sock_link(&hci_sk_list, sk);
570 static int hci_sock_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
572 struct hci_dev *hdev = (struct hci_dev *) ptr;
575 BT_DBG("hdev %s event %ld", hdev->name, event);
577 /* Send event to sockets */
579 sd.dev_id = hdev->id;
580 hci_si_event(NULL, EVT_SI_DEVICE, EVT_SI_DEVICE_SIZE, &sd);
582 if (event == HCI_DEV_UNREG) {
585 /* Detach sockets from device */
586 read_lock(&hci_sk_list.lock);
587 for (sk = hci_sk_list.head; sk; sk = sk->next) {
589 if (hci_pi(sk)->hdev == hdev) {
590 hci_pi(sk)->hdev = NULL;
593 sk->state_change(sk);
599 read_unlock(&hci_sk_list.lock);
605 struct net_proto_family hci_sock_family_ops = {
606 family: PF_BLUETOOTH,
607 create: hci_sock_create
610 struct notifier_block hci_sock_nblock = {
611 notifier_call: hci_sock_dev_event
614 int hci_sock_init(void)
616 if (bluez_sock_register(BTPROTO_HCI, &hci_sock_family_ops)) {
617 BT_ERR("Can't register HCI socket");
621 hci_register_notifier(&hci_sock_nblock);
625 int hci_sock_cleanup(void)
627 if (bluez_sock_unregister(BTPROTO_HCI))
628 BT_ERR("Can't unregister HCI socket");
630 hci_unregister_notifier(&hci_sock_nblock);