1 /* 2 * ng_btsocket.c 3 */ 4 5 /*- 6 * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com> 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * $Id: ng_btsocket.c,v 1.4 2003/09/14 23:29:06 max Exp $ 31 * $FreeBSD$ 32 */ 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/bitstring.h> 37 #include <sys/errno.h> 38 #include <sys/domain.h> 39 #include <sys/kernel.h> 40 #include <sys/lock.h> 41 #include <sys/mbuf.h> 42 #include <sys/mutex.h> 43 #include <sys/protosw.h> 44 #include <sys/socket.h> 45 #include <sys/socketvar.h> 46 #include <sys/sysctl.h> 47 #include <sys/taskqueue.h> 48 #include <netgraph/ng_message.h> 49 #include <netgraph/netgraph.h> 50 #include <netgraph/bluetooth/include/ng_bluetooth.h> 51 #include <netgraph/bluetooth/include/ng_hci.h> 52 #include <netgraph/bluetooth/include/ng_l2cap.h> 53 #include <netgraph/bluetooth/include/ng_btsocket.h> 54 #include <netgraph/bluetooth/include/ng_btsocket_hci_raw.h> 55 #include <netgraph/bluetooth/include/ng_btsocket_l2cap.h> 56 #include <netgraph/bluetooth/include/ng_btsocket_rfcomm.h> 57 58 static int ng_btsocket_modevent (module_t, int, void *); 59 extern struct domain ng_btsocket_domain; 60 61 /* 62 * Bluetooth raw HCI sockets 63 */ 64 65 static struct pr_usrreqs ng_btsocket_hci_raw_usrreqs = { 66 .pru_abort = ng_btsocket_hci_raw_abort, 67 .pru_attach = ng_btsocket_hci_raw_attach, 68 .pru_bind = ng_btsocket_hci_raw_bind, 69 .pru_connect = ng_btsocket_hci_raw_connect, 70 .pru_control = ng_btsocket_hci_raw_control, 71 .pru_detach = ng_btsocket_hci_raw_detach, 72 .pru_disconnect = ng_btsocket_hci_raw_disconnect, 73 .pru_peeraddr = ng_btsocket_hci_raw_peeraddr, 74 .pru_send = ng_btsocket_hci_raw_send, 75 .pru_shutdown = NULL, 76 .pru_sockaddr = ng_btsocket_hci_raw_sockaddr, 77 .pru_close = ng_btsocket_hci_raw_close, 78 }; 79 80 /* 81 * Bluetooth raw L2CAP sockets 82 */ 83 84 static struct pr_usrreqs ng_btsocket_l2cap_raw_usrreqs = { 85 .pru_abort = ng_btsocket_l2cap_raw_abort, 86 .pru_attach = ng_btsocket_l2cap_raw_attach, 87 .pru_bind = ng_btsocket_l2cap_raw_bind, 88 .pru_connect = ng_btsocket_l2cap_raw_connect, 89 .pru_control = ng_btsocket_l2cap_raw_control, 90 .pru_detach = ng_btsocket_l2cap_raw_detach, 91 .pru_disconnect = ng_btsocket_l2cap_raw_disconnect, 92 .pru_peeraddr = ng_btsocket_l2cap_raw_peeraddr, 93 .pru_send = ng_btsocket_l2cap_raw_send, 94 .pru_shutdown = NULL, 95 .pru_sockaddr = ng_btsocket_l2cap_raw_sockaddr, 96 .pru_close = ng_btsocket_l2cap_raw_close, 97 }; 98 99 /* 100 * Bluetooth SEQPACKET L2CAP sockets 101 */ 102 103 static struct pr_usrreqs ng_btsocket_l2cap_usrreqs = { 104 .pru_abort = ng_btsocket_l2cap_abort, 105 .pru_accept = ng_btsocket_l2cap_accept, 106 .pru_attach = ng_btsocket_l2cap_attach, 107 .pru_bind = ng_btsocket_l2cap_bind, 108 .pru_connect = ng_btsocket_l2cap_connect, 109 .pru_control = ng_btsocket_l2cap_control, 110 .pru_detach = ng_btsocket_l2cap_detach, 111 .pru_disconnect = ng_btsocket_l2cap_disconnect, 112 .pru_listen = ng_btsocket_l2cap_listen, 113 .pru_peeraddr = ng_btsocket_l2cap_peeraddr, 114 .pru_send = ng_btsocket_l2cap_send, 115 .pru_shutdown = NULL, 116 .pru_sockaddr = ng_btsocket_l2cap_sockaddr, 117 .pru_close = ng_btsocket_l2cap_close, 118 }; 119 120 /* 121 * Bluetooth STREAM RFCOMM sockets 122 */ 123 124 static struct pr_usrreqs ng_btsocket_rfcomm_usrreqs = { 125 .pru_abort = ng_btsocket_rfcomm_abort, 126 .pru_accept = ng_btsocket_rfcomm_accept, 127 .pru_attach = ng_btsocket_rfcomm_attach, 128 .pru_bind = ng_btsocket_rfcomm_bind, 129 .pru_connect = ng_btsocket_rfcomm_connect, 130 .pru_control = ng_btsocket_rfcomm_control, 131 .pru_detach = ng_btsocket_rfcomm_detach, 132 .pru_disconnect = ng_btsocket_rfcomm_disconnect, 133 .pru_listen = ng_btsocket_rfcomm_listen, 134 .pru_peeraddr = ng_btsocket_rfcomm_peeraddr, 135 .pru_send = ng_btsocket_rfcomm_send, 136 .pru_shutdown = NULL, 137 .pru_sockaddr = ng_btsocket_rfcomm_sockaddr, 138 .pru_close = ng_btsocket_rfcomm_close, 139 }; 140 141 /* 142 * Definitions of protocols supported in the BLUETOOTH domain 143 */ 144 145 static struct protosw ng_btsocket_protosw[] = { 146 { 147 .pr_type = SOCK_RAW, 148 .pr_domain = &ng_btsocket_domain, 149 .pr_protocol = BLUETOOTH_PROTO_HCI, 150 .pr_flags = PR_ATOMIC|PR_ADDR, 151 .pr_ctloutput = ng_btsocket_hci_raw_ctloutput, 152 .pr_init = ng_btsocket_hci_raw_init, 153 .pr_usrreqs = &ng_btsocket_hci_raw_usrreqs, 154 }, 155 { 156 .pr_type = SOCK_RAW, 157 .pr_domain = &ng_btsocket_domain, 158 .pr_protocol = BLUETOOTH_PROTO_L2CAP, 159 .pr_flags = PR_ATOMIC|PR_ADDR, 160 .pr_init = ng_btsocket_l2cap_raw_init, 161 .pr_usrreqs = &ng_btsocket_l2cap_raw_usrreqs, 162 }, 163 { 164 .pr_type = SOCK_SEQPACKET, 165 .pr_domain = &ng_btsocket_domain, 166 .pr_protocol = BLUETOOTH_PROTO_L2CAP, 167 .pr_flags = PR_ATOMIC|PR_CONNREQUIRED, 168 .pr_ctloutput = ng_btsocket_l2cap_ctloutput, 169 .pr_init = ng_btsocket_l2cap_init, 170 .pr_usrreqs = &ng_btsocket_l2cap_usrreqs, 171 }, 172 { 173 .pr_type = SOCK_STREAM, 174 .pr_domain = &ng_btsocket_domain, 175 .pr_protocol = BLUETOOTH_PROTO_RFCOMM, 176 .pr_flags = PR_CONNREQUIRED, 177 .pr_ctloutput = ng_btsocket_rfcomm_ctloutput, 178 .pr_init = ng_btsocket_rfcomm_init, 179 .pr_usrreqs = &ng_btsocket_rfcomm_usrreqs, 180 } 181 }; 182 #define ng_btsocket_protosw_size \ 183 (sizeof(ng_btsocket_protosw)/sizeof(ng_btsocket_protosw[0])) 184 #define ng_btsocket_protosw_end \ 185 &ng_btsocket_protosw[ng_btsocket_protosw_size] 186 187 /* 188 * BLUETOOTH domain 189 */ 190 191 struct domain ng_btsocket_domain = { 192 .dom_family = AF_BLUETOOTH, 193 .dom_name = "bluetooth", 194 .dom_protosw = ng_btsocket_protosw, 195 .dom_protoswNPROTOSW = ng_btsocket_protosw_end 196 }; 197 198 /* 199 * Socket sysctl tree 200 */ 201 202 SYSCTL_NODE(_net_bluetooth_hci, OID_AUTO, sockets, CTLFLAG_RW, 203 0, "Bluetooth HCI sockets family"); 204 SYSCTL_NODE(_net_bluetooth_l2cap, OID_AUTO, sockets, CTLFLAG_RW, 205 0, "Bluetooth L2CAP sockets family"); 206 SYSCTL_NODE(_net_bluetooth_rfcomm, OID_AUTO, sockets, CTLFLAG_RW, 207 0, "Bluetooth RFCOMM sockets family"); 208 209 /* 210 * Module 211 */ 212 213 static moduledata_t ng_btsocket_mod = { 214 "ng_btsocket", 215 ng_btsocket_modevent, 216 NULL 217 }; 218 219 DECLARE_MODULE(ng_btsocket, ng_btsocket_mod, SI_SUB_PROTO_DOMAIN, 220 SI_ORDER_ANY); 221 MODULE_VERSION(ng_btsocket, NG_BLUETOOTH_VERSION); 222 MODULE_DEPEND(ng_btsocket, ng_bluetooth, NG_BLUETOOTH_VERSION, 223 NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION); 224 MODULE_DEPEND(ng_btsocket, netgraph, NG_ABI_VERSION, 225 NG_ABI_VERSION, NG_ABI_VERSION); 226 227 /* 228 * Handle loading and unloading for this node type. 229 * This is to handle auxiliary linkages (e.g protocol domain addition). 230 */ 231 232 static int 233 ng_btsocket_modevent(module_t mod, int event, void *data) 234 { 235 int error = 0; 236 237 switch (event) { 238 case MOD_LOAD: 239 net_add_domain(&ng_btsocket_domain); 240 break; 241 242 case MOD_UNLOAD: 243 /* XXX can't unload protocol domain yet */ 244 error = EBUSY; 245 break; 246 247 default: 248 error = EOPNOTSUPP; 249 break; 250 } 251 252 return (error); 253 } /* ng_btsocket_modevent */ 254 255