xref: /freebsd/usr.sbin/bluetooth/btpand/server.c (revision 193d9e768ba63fcfb187cfd17f461f7d41345048)
1 /*	$NetBSD: server.c,v 1.2 2009/01/24 17:29:28 plunky Exp $	*/
2 
3 /*-
4  * Copyright (c) 2008 Iain Hibbert
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 /* $FreeBSD$ */
29 
30 #include <sys/cdefs.h>
31 __RCSID("$NetBSD: server.c,v 1.2 2009/01/24 17:29:28 plunky Exp $");
32 
33 #include <sys/ioctl.h>
34 
35 #define L2CAP_SOCKET_CHECKED
36 #include <bluetooth.h>
37 #include <inttypes.h>
38 #include <errno.h>
39 #include <sdp.h>
40 #include <unistd.h>
41 
42 #include "btpand.h"
43 #include "bnep.h"
44 
45 static struct event	server_ev;
46 static int		server_fd;
47 static int		server_avail;
48 
49 static void *		server_ss;
50 static uint32_t		server_handle;
51 
52 static void server_open(void);
53 static void server_close(void);
54 static void server_read(int, short, void *);
55 static void server_register(void);
56 
57 void
58 server_init(void)
59 {
60 
61 	server_fd = -1;
62 }
63 
64 /*
65  * The server_update() function is called whenever the channel count is
66  * changed. We maintain the SDP record and open or close the server socket
67  * as required.
68  */
69 void
70 server_update(int count)
71 {
72 
73 	if (server_limit == 0)
74 		return;
75 
76 	log_debug("count %d", count);
77 
78 	server_avail = UINT8_MAX - (count - 1) * UINT8_MAX / server_limit;
79 	log_info("Service Availability: %d/%d", server_avail, UINT8_MAX);
80 
81 	if (server_avail == 0 && server_fd != -1)
82 		server_close();
83 
84 	if (server_avail > 0 && server_fd == -1)
85 		server_open();
86 
87 	if (service_name)
88 		server_register();
89 }
90 
91 static void
92 server_open(void)
93 {
94 	struct sockaddr_l2cap sa;
95 	uint16_t mru;
96 
97 	server_fd = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BLUETOOTH_PROTO_L2CAP);
98 	if (server_fd == -1) {
99 		log_err("Could not open L2CAP socket: %m");
100 		exit(EXIT_FAILURE);
101 	}
102 
103 	memset(&sa, 0, sizeof(sa));
104 	sa.l2cap_family = AF_BLUETOOTH;
105 	sa.l2cap_len = sizeof(sa);
106 	sa.l2cap_psm = htole16(l2cap_psm);
107 	sa.l2cap_bdaddr_type = BDADDR_BREDR;
108 	sa.l2cap_cid = 0;
109 
110 	bdaddr_copy(&sa.l2cap_bdaddr, &local_bdaddr);
111 	if (bind(server_fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
112 		log_err("Could not bind server socket: %m");
113 		exit(EXIT_FAILURE);
114 	}
115 
116 	mru = BNEP_MTU_MIN;
117 	if (setsockopt(server_fd, SOL_L2CAP,
118 	    SO_L2CAP_IMTU, &mru, sizeof(mru)) == -1) {
119 		log_err("Could not set L2CAP IMTU (%d): %m", mru);
120 		exit(EXIT_FAILURE);
121 	}
122 
123 	if (listen(server_fd, 0) == -1) {
124 		log_err("Could not listen on server socket: %m");
125 		exit(EXIT_FAILURE);
126 	}
127 
128 	event_set(&server_ev, server_fd, EV_READ | EV_PERSIST, server_read, NULL);
129 	if (event_add(&server_ev, NULL) == -1) {
130 		log_err("Could not add server event: %m");
131 		exit(EXIT_FAILURE);
132 	}
133 
134 	log_info("server socket open");
135 }
136 
137 static void
138 server_close(void)
139 {
140 
141 	event_del(&server_ev);
142 	close(server_fd);
143 	server_fd = -1;
144 
145 	log_info("server socket closed");
146 }
147 
148 /*
149  * handle connection request
150  */
151 static void
152 server_read(int s, short ev, void *arg)
153 {
154 	struct sockaddr_l2cap ra, la;
155 	channel_t *chan;
156 	socklen_t len;
157 	int fd, n;
158 	uint16_t mru, mtu;
159 
160 	len = sizeof(ra);
161 	fd = accept(s, (struct sockaddr *)&ra, &len);
162 	if (fd == -1)
163 		return;
164 
165 	n = 1;
166 	if (ioctl(fd, FIONBIO, &n) == -1) {
167 		log_err("Could not set NonBlocking IO: %m");
168 		close(fd);
169 		return;
170 	}
171 
172 	len = sizeof(mru);
173 	if (getsockopt(fd, SOL_L2CAP, SO_L2CAP_IMTU, &mru, &len) == -1) {
174 		log_err("Could not get L2CAP IMTU: %m");
175 		close(fd);
176 		return;
177 	}
178 	if(mru < BNEP_MTU_MIN) {
179 		log_err("L2CAP IMTU too small (%d)", mru);
180 		close(fd);
181 		return;
182 	}
183 
184 	len = sizeof(n);
185 	if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &n, &len) == -1) {
186 		log_err("Could not read SO_RCVBUF");
187 		close(fd);
188 		return;
189 	}
190 	if (n < (mru * 10)) {
191 		n = mru * 10;
192 		if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n)) == -1)
193 			log_info("Could not increase SO_RCVBUF (from %d)", n);
194 	}
195 
196 	len = sizeof(mtu);
197 	if (getsockopt(fd, SOL_L2CAP, SO_L2CAP_OMTU, &mtu, &len) == -1) {
198 		log_err("Could not get L2CAP OMTU: %m");
199 		close(fd);
200 		return;
201 	}
202 	if (mtu < BNEP_MTU_MIN) {
203 		log_err("L2CAP OMTU too small (%d)", mtu);
204 		close(fd);
205 		return;
206 	}
207 
208 	len = sizeof(n);
209 	if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &n, &len) == -1) {
210 		log_err("Could not get socket send buffer size: %m");
211 		close(fd);
212 		return;
213 	}
214 
215 	if (n < (mtu * 2)) {
216 		n = mtu * 2;
217 		if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &n, sizeof(n)) == -1) {
218 			log_err("Could not set socket send buffer size (%d): %m", n);
219 			close(fd);
220 			return;
221 		}
222 	}
223 
224 	n = mtu;
225 	if (setsockopt(fd, SOL_SOCKET, SO_SNDLOWAT, &n, sizeof(n)) == -1) {
226 		log_err("Could not set socket low water mark (%d): %m", n);
227 		close(fd);
228 		return;
229 	}
230 
231 	len = sizeof(la);
232 	if (getsockname(fd, (struct sockaddr *)&la, &len) == -1) {
233 		log_err("Could not get socket address: %m");
234 		close(fd);
235 		return;
236 	}
237 
238 	log_info("Accepted connection from %s", bt_ntoa(&ra.l2cap_bdaddr, NULL));
239 
240 	chan = channel_alloc();
241 	if (chan == NULL) {
242 		close(fd);
243 		return;
244 	}
245 
246 	chan->send = bnep_send;
247 	chan->recv = bnep_recv;
248 	chan->mru = mru;
249 	chan->mtu = mtu;
250 	b2eaddr(chan->raddr, &ra.l2cap_bdaddr);
251 	b2eaddr(chan->laddr, &la.l2cap_bdaddr);
252 	chan->state = CHANNEL_WAIT_CONNECT_REQ;
253 	channel_timeout(chan, 10);
254 	if (!channel_open(chan, fd)) {
255 		chan->state = CHANNEL_CLOSED;
256 		channel_free(chan);
257 		close(fd);
258 		return;
259 	}
260 }
261 
262 static void
263 server_register(void)
264 {
265 	sdp_nap_profile_t p;
266 	int rv;
267 
268 	if (server_ss == NULL) {
269 		server_ss = sdp_open_local(control_path);
270 		if (server_ss == NULL || sdp_error(server_ss) != 0) {
271 			log_err("failed to contact SDP server");
272 			return;
273 		}
274 	}
275 
276 	memset(&p, 0, sizeof(p));
277 	p.psm = l2cap_psm;
278 	p.load_factor = server_avail;
279 	p.security_description = (l2cap_mode == 0 ? 0x0000 : 0x0001);
280 
281 	if (server_handle)
282 		rv = sdp_change_service(server_ss, server_handle,
283 		    (uint8_t *)&p, sizeof(p));
284 	else
285 		rv = sdp_register_service(server_ss, service_class,
286 		    &local_bdaddr, (uint8_t *)&p, sizeof(p), &server_handle);
287 
288 	if (rv != 0) {
289 		errno = sdp_error(server_ss);
290 		log_err("%s: %m", service_name);
291 		exit(EXIT_FAILURE);
292 	}
293 }
294