ng_btsocket.c revision 107120
175115Sfenner/* 275115Sfenner * ng_btsocket.c 375115Sfenner * 475115Sfenner * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com> 575115Sfenner * All rights reserved. 675115Sfenner * 775115Sfenner * Redistribution and use in source and binary forms, with or without 875115Sfenner * modification, are permitted provided that the following conditions 975115Sfenner * are met: 1075115Sfenner * 1. Redistributions of source code must retain the above copyright 1175115Sfenner * notice, this list of conditions and the following disclaimer. 1275115Sfenner * 2. Redistributions in binary form must reproduce the above copyright 1375115Sfenner * notice, this list of conditions and the following disclaimer in the 1475115Sfenner * documentation and/or other materials provided with the distribution. 1575115Sfenner * 1675115Sfenner * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1775115Sfenner * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1875115Sfenner * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1975115Sfenner * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2075115Sfenner * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2175115Sfenner * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2275115Sfenner * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2375115Sfenner * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2475115Sfenner * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2575115Sfenner * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2675115Sfenner * SUCH DAMAGE. 2775115Sfenner * 2875115Sfenner * $Id: ng_btsocket.c,v 1.20 2002/09/13 17:56:58 max Exp $ 2975115Sfenner * $FreeBSD: head/sys/netgraph/bluetooth/socket/ng_btsocket.c 107120 2002-11-20 23:01:59Z julian $ 3075115Sfenner */ 3175115Sfenner 3275115Sfenner#include <sys/param.h> 3375115Sfenner#include <sys/systm.h> 3475115Sfenner#include <sys/errno.h> 3575115Sfenner#include <sys/domain.h> 36127668Sbms#include <sys/kernel.h> 3775115Sfenner#include <sys/lock.h> 3875115Sfenner#include <sys/mutex.h> 3975115Sfenner#include <sys/protosw.h> 4075115Sfenner#include <sys/socket.h> 4175115Sfenner#include <sys/socketvar.h> 4275115Sfenner#include <sys/sysctl.h> 43127668Sbms#include <bitstring.h> 4475115Sfenner#include <netgraph/ng_message.h> 4575115Sfenner#include <netgraph/netgraph.h> 4675115Sfenner#include "ng_bluetooth.h" 4775115Sfenner#include "ng_hci.h" 4875115Sfenner#include "ng_l2cap.h" 4975115Sfenner#include "ng_btsocket.h" 5075115Sfenner#include "ng_btsocket_hci_raw.h" 5175115Sfenner#include "ng_btsocket_l2cap.h" 5275115Sfenner 5375115Sfennerstatic int ng_btsocket_modevent (module_t, int, void *); 5475115Sfennerextern struct domain ng_btsocket_domain; 5575115Sfenner 5675115Sfenner/* 5775115Sfenner * Bluetooth raw HCI sockets 5875115Sfenner */ 5975115Sfenner 6075115Sfennerstatic struct pr_usrreqs ng_btsocket_hci_raw_usrreqs = { 6175115Sfenner ng_btsocket_hci_raw_abort, /* abort */ 6275115Sfenner pru_accept_notsupp, /* accept */ 6375115Sfenner ng_btsocket_hci_raw_attach, /* attach */ 6475115Sfenner ng_btsocket_hci_raw_bind, /* bind */ 6575115Sfenner ng_btsocket_hci_raw_connect, /* connect */ 6675115Sfenner pru_connect2_notsupp, /* connect2 */ 6775115Sfenner ng_btsocket_hci_raw_control, /* control */ 6875115Sfenner ng_btsocket_hci_raw_detach, /* detach */ 6975115Sfenner ng_btsocket_hci_raw_disconnect, /* disconnect */ 7075115Sfenner pru_listen_notsupp, /* listen */ 7175115Sfenner ng_btsocket_hci_raw_peeraddr, /* peeraddr */ 7275115Sfenner pru_rcvd_notsupp, /* rcvd */ 7375115Sfenner pru_rcvoob_notsupp, /* rcvoob */ 7475115Sfenner ng_btsocket_hci_raw_send, /* send */ 7575115Sfenner pru_sense_null, /* send */ 7675115Sfenner NULL, /* shutdown */ 77172683Smlaier ng_btsocket_hci_raw_sockaddr, /* sockaddr */ 78172683Smlaier sosend, 79172683Smlaier soreceive, 80172683Smlaier sopoll 81172683Smlaier}; 82172683Smlaier 83172683Smlaier/* 84172683Smlaier * Bluetooth raw L2CAP sockets 85127668Sbms */ 8675115Sfenner 8775115Sfennerstatic struct pr_usrreqs ng_btsocket_l2cap_raw_usrreqs = { 8875115Sfenner ng_btsocket_l2cap_raw_abort, /* abort */ 8975115Sfenner pru_accept_notsupp, /* accept */ 9075115Sfenner ng_btsocket_l2cap_raw_attach, /* attach */ 9175115Sfenner ng_btsocket_l2cap_raw_bind, /* bind */ 9275115Sfenner ng_btsocket_l2cap_raw_connect, /* connect */ 9375115Sfenner pru_connect2_notsupp, /* connect2 */ 9475115Sfenner ng_btsocket_l2cap_raw_control, /* control */ 9575115Sfenner ng_btsocket_l2cap_raw_detach, /* detach */ 9675115Sfenner ng_btsocket_l2cap_raw_disconnect, /* disconnect */ 9775115Sfenner pru_listen_notsupp, /* listen */ 9875115Sfenner ng_btsocket_l2cap_raw_peeraddr, /* peeraddr */ 9975115Sfenner pru_rcvd_notsupp, /* rcvd */ 10075115Sfenner pru_rcvoob_notsupp, /* rcvoob */ 10175115Sfenner ng_btsocket_l2cap_raw_send, /* send */ 10275115Sfenner pru_sense_null, /* send */ 10375115Sfenner NULL, /* shutdown */ 10475115Sfenner ng_btsocket_l2cap_raw_sockaddr, /* sockaddr */ 10575115Sfenner sosend, 10675115Sfenner soreceive, 10798524Sfenner sopoll 10898524Sfenner}; 10998524Sfenner 11098524Sfenner/* 11198524Sfenner * Bluetooth SEQPACKET L2CAP sockets 11298524Sfenner */ 11398524Sfenner 11498524Sfennerstatic struct pr_usrreqs ng_btsocket_l2cap_usrreqs = { 11598524Sfenner ng_btsocket_l2cap_abort, /* abort */ 11698524Sfenner ng_btsocket_l2cap_accept, /* accept */ 11798524Sfenner ng_btsocket_l2cap_attach, /* attach */ 11898524Sfenner ng_btsocket_l2cap_bind, /* bind */ 11998524Sfenner ng_btsocket_l2cap_connect, /* connect */ 12098524Sfenner pru_connect2_notsupp, /* connect2 */ 12198524Sfenner ng_btsocket_l2cap_control, /* control */ 12298524Sfenner ng_btsocket_l2cap_detach, /* detach */ 12398524Sfenner ng_btsocket_l2cap_disconnect, /* disconnect */ 12475115Sfenner ng_btsocket_l2cap_listen, /* listen */ 12575115Sfenner ng_btsocket_l2cap_peeraddr, /* peeraddr */ 12675115Sfenner pru_rcvd_notsupp, /* rcvd */ 12775115Sfenner pru_rcvoob_notsupp, /* rcvoob */ 12875115Sfenner ng_btsocket_l2cap_send, /* send */ 12975115Sfenner pru_sense_null, /* send */ 13075115Sfenner NULL, /* shutdown */ 13175115Sfenner ng_btsocket_l2cap_sockaddr, /* sockaddr */ 13275115Sfenner sosend, 13375115Sfenner soreceive, 13475115Sfenner sopoll 13575115Sfenner}; 13675115Sfenner 13775115Sfenner/* 13875115Sfenner * Definitions of protocols supported in the BLUETOOTH domain 13975115Sfenner */ 14075115Sfenner 14175115Sfennerstatic struct protosw ng_btsocket_protosw[] = { 14275115Sfenner{ 14375115Sfenner SOCK_RAW, /* protocol type */ 14475115Sfenner &ng_btsocket_domain, /* backpointer to domain */ 14575115Sfenner BLUETOOTH_PROTO_HCI, /* protocol */ 14675115Sfenner PR_ATOMIC | PR_ADDR, /* flags */ 14775115Sfenner NULL, NULL, NULL, /* input, output, ctlinput */ 14875115Sfenner ng_btsocket_hci_raw_ctloutput, /* ctloutput */ 14975115Sfenner NULL, /* ousrreq() */ 15075115Sfenner ng_btsocket_hci_raw_init, /* init */ 15175115Sfenner NULL, NULL, NULL, /* fasttimeo, slowtimo, drain */ 15275115Sfenner &ng_btsocket_hci_raw_usrreqs, /* usrreq table (above) */ 15375115Sfenner /* { NULL } */ /* pfh (protocol filter head?) */ 15475115Sfenner}, 15575115Sfenner{ 15698524Sfenner SOCK_RAW, /* protocol type */ 15798524Sfenner &ng_btsocket_domain, /* backpointer to domain */ 15898524Sfenner BLUETOOTH_PROTO_L2CAP, /* protocol */ 15975115Sfenner PR_ATOMIC | PR_ADDR, /* flags */ 16075115Sfenner NULL, NULL, NULL, /* input, output, ctlinput */ 16198524Sfenner NULL, /* ctloutput */ 16298524Sfenner NULL, /* ousrreq() */ 16398524Sfenner ng_btsocket_l2cap_raw_init, /* init */ 16498524Sfenner NULL, NULL, NULL, /* fasttimeo, slowtimo, drain */ 16598524Sfenner &ng_btsocket_l2cap_raw_usrreqs, /* usrreq table (above) */ 16698524Sfenner /* { NULL } */ /* pfh (protocol filter head?) */ 16798524Sfenner}, 16898524Sfenner{ 16998524Sfenner SOCK_SEQPACKET, /* protocol type */ 17098524Sfenner &ng_btsocket_domain, /* backpointer to domain */ 17198524Sfenner BLUETOOTH_PROTO_L2CAP, /* protocol */ 17298524Sfenner PR_ATOMIC | PR_CONNREQUIRED, /* flags */ 173172683Smlaier NULL, NULL, NULL, /* input, output, ctlinput */ 174172683Smlaier ng_btsocket_l2cap_ctloutput, /* ctloutput */ 17598524Sfenner NULL, /* ousrreq() */ 17698524Sfenner ng_btsocket_l2cap_init, /* init */ 177172683Smlaier NULL, NULL, NULL, /* fasttimeo, slowtimo, drain */ 17898524Sfenner &ng_btsocket_l2cap_usrreqs, /* usrreq table (above) */ 179172683Smlaier /* { NULL } */ /* pfh (protocol filter head?) */ 180172683Smlaier} 181172683Smlaier}; 182172683Smlaier#define ng_btsocket_protosw_size \ 183172683Smlaier (sizeof(ng_btsocket_protosw)/sizeof(ng_btsocket_protosw[0])) 184172683Smlaier#define ng_btsocket_protosw_end \ 185172683Smlaier &ng_btsocket_protosw[ng_btsocket_protosw_size] 18675115Sfenner 187172683Smlaier/* 18875115Sfenner * BLUETOOTH domain 18975115Sfenner */ 19075115Sfenner 19175115Sfennerstruct domain ng_btsocket_domain = { 19298524Sfenner AF_BLUETOOTH, /* family */ 19375115Sfenner "bluetooth", /* domain name */ 19498524Sfenner NULL, /* init() */ 19598524Sfenner NULL, /* externalize() */ 19698524Sfenner NULL, /* dispose() */ 19775115Sfenner ng_btsocket_protosw, /* protosw entry */ 19875115Sfenner ng_btsocket_protosw_end, /* end of protosw entries */ 19975115Sfenner NULL, /* next domain in list */ 20075115Sfenner NULL, /* rtattach() */ 20175115Sfenner 0, /* arg to rtattach in bits */ 20275115Sfenner 0 /* maxrtkey */ 20375115Sfenner}; 20475115Sfenner 20575115Sfenner/* 20675115Sfenner * Socket sysctl tree 20775115Sfenner */ 20875115Sfenner 20975115SfennerSYSCTL_NODE(_net_bluetooth_hci, OID_AUTO, sockets, CTLFLAG_RW, 21075115Sfenner 0, "Bluetooth HCI sockets family"); 211172683SmlaierSYSCTL_NODE(_net_bluetooth_l2cap, OID_AUTO, sockets, CTLFLAG_RW, 212172683Smlaier 0, "Bluetooth L2CAP sockets family"); 21375115Sfenner 21475115Sfenner/* 21575115Sfenner * Module 21675115Sfenner */ 21775115Sfenner 21875115Sfennerstatic moduledata_t ng_btsocket_mod = { 21975115Sfenner "ng_btsocket", 22075115Sfenner ng_btsocket_modevent, 22175115Sfenner NULL 22275115Sfenner}; 22375115Sfenner 22475115SfennerDECLARE_MODULE(ng_btsocket, ng_btsocket_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 22575115SfennerMODULE_VERSION(ng_btsocket, NG_BLUETOOTH_VERSION); 22675115SfennerMODULE_DEPEND(ng_btsocket, ng_bluetooth, NG_BLUETOOTH_VERSION, 227276788Sdelphij NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION); 228276788SdelphijMODULE_DEPEND(ng_btsocket, netgraph, NG_ABI_VERSION, 229276788Sdelphij NG_ABI_VERSION, NG_ABI_VERSION); 230276788Sdelphij 231276788Sdelphij/* 232276788Sdelphij * Handle loading and unloading for this node type. 233276788Sdelphij * This is to handle auxiliary linkages (e.g protocol domain addition). 23475115Sfenner */ 23575115Sfenner 23675115Sfennerstatic int 23775115Sfennerng_btsocket_modevent(module_t mod, int event, void *data) 23875115Sfenner{ 23975115Sfenner int error = 0; 24075115Sfenner 24175115Sfenner switch (event) { 24275115Sfenner case MOD_LOAD: 24375115Sfenner net_add_domain(&ng_btsocket_domain); 24475115Sfenner break; 24575115Sfenner 24675115Sfenner case MOD_UNLOAD: 24775115Sfenner /* XXX can't unload protocol domain yet */ 24875115Sfenner error = EBUSY; 24975115Sfenner break; 25075115Sfenner 25175115Sfenner default: 25275115Sfenner error = EOPNOTSUPP; 25375115Sfenner break; 25475115Sfenner } 255127668Sbms 25675115Sfenner return (error); 25775115Sfenner} /* ng_btsocket_modevent */ 25875115Sfenner 25975115Sfenner