xref: /freebsd/usr.sbin/bluetooth/btpand/server.c (revision 7750ad47a9a7dbc83f87158464170c8640723293)
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 #include <bluetooth.h>
36 #include <inttypes.h>
37 #include <errno.h>
38 #include <sdp.h>
39 #include <unistd.h>
40 
41 #include "btpand.h"
42 #include "bnep.h"
43 
44 static struct event	server_ev;
45 static int		server_fd;
46 static int		server_avail;
47 
48 static void *		server_ss;
49 static uint32_t		server_handle;
50 
51 static void server_open(void);
52 static void server_close(void);
53 static void server_read(int, short, void *);
54 static void server_register(void);
55 
56 void
57 server_init(void)
58 {
59 
60 	server_fd = -1;
61 }
62 
63 /*
64  * The server_update() function is called whenever the channel count is
65  * changed. We maintain the SDP record and open or close the server socket
66  * as required.
67  */
68 void
69 server_update(int count)
70 {
71 
72 	if (server_limit == 0)
73 		return;
74 
75 	log_debug("count %d", count);
76 
77 	server_avail = UINT8_MAX - (count - 1) * UINT8_MAX / server_limit;
78 	log_info("Service Availability: %d/%d", server_avail, UINT8_MAX);
79 
80 	if (server_avail == 0 && server_fd != -1)
81 		server_close();
82 
83 	if (server_avail > 0 && server_fd == -1)
84 		server_open();
85 
86 	if (service_name)
87 		server_register();
88 }
89 
90 static void
91 server_open(void)
92 {
93 	struct sockaddr_l2cap sa;
94 	uint16_t mru;
95 
96 	server_fd = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BLUETOOTH_PROTO_L2CAP);
97 	if (server_fd == -1) {
98 		log_err("Could not open L2CAP socket: %m");
99 		exit(EXIT_FAILURE);
100 	}
101 
102 	memset(&sa, 0, sizeof(sa));
103 	sa.l2cap_family = AF_BLUETOOTH;
104 	sa.l2cap_len = sizeof(sa);
105 	sa.l2cap_psm = htole16(l2cap_psm);
106 	bdaddr_copy(&sa.l2cap_bdaddr, &local_bdaddr);
107 	if (bind(server_fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
108 		log_err("Could not bind server socket: %m");
109 		exit(EXIT_FAILURE);
110 	}
111 
112 	mru = BNEP_MTU_MIN;
113 	if (setsockopt(server_fd, SOL_L2CAP,
114 	    SO_L2CAP_IMTU, &mru, sizeof(mru)) == -1) {
115 		log_err("Could not set L2CAP IMTU (%d): %m", mru);
116 		exit(EXIT_FAILURE);
117 	}
118 
119 	if (listen(server_fd, 0) == -1) {
120 		log_err("Could not listen on server socket: %m");
121 		exit(EXIT_FAILURE);
122 	}
123 
124 	event_set(&server_ev, server_fd, EV_READ | EV_PERSIST, server_read, NULL);
125 	if (event_add(&server_ev, NULL) == -1) {
126 		log_err("Could not add server event: %m");
127 		exit(EXIT_FAILURE);
128 	}
129 
130 	log_info("server socket open");
131 }
132 
133 static void
134 server_close(void)
135 {
136 
137 	event_del(&server_ev);
138 	close(server_fd);
139 	server_fd = -1;
140 
141 	log_info("server socket closed");
142 }
143 
144 /*
145  * handle connection request
146  */
147 static void
148 server_read(int s, short ev, void *arg)
149 {
150 	struct sockaddr_l2cap ra, la;
151 	channel_t *chan;
152 	socklen_t len;
153 	int fd, n;
154 	uint16_t mru, mtu;
155 
156 	len = sizeof(ra);
157 	fd = accept(s, (struct sockaddr *)&ra, &len);
158 	if (fd == -1)
159 		return;
160 
161 	n = 1;
162 	if (ioctl(fd, FIONBIO, &n) == -1) {
163 		log_err("Could not set NonBlocking IO: %m");
164 		close(fd);
165 		return;
166 	}
167 
168 	len = sizeof(mru);
169 	if (getsockopt(fd, SOL_L2CAP, SO_L2CAP_IMTU, &mru, &len) == -1) {
170 		log_err("Could not get L2CAP IMTU: %m");
171 		close(fd);
172 		return;
173 	}
174 	if(mru < BNEP_MTU_MIN) {
175 		log_err("L2CAP IMTU too small (%d)", mru);
176 		close(fd);
177 		return;
178 	}
179 
180 	len = sizeof(mtu);
181 	if (getsockopt(fd, SOL_L2CAP, SO_L2CAP_OMTU, &mtu, &len) == -1) {
182 		log_err("Could not get L2CAP OMTU: %m");
183 		close(fd);
184 		return;
185 	}
186 	if (mtu < BNEP_MTU_MIN) {
187 		log_err("L2CAP OMTU too small (%d)", mtu);
188 		close(fd);
189 		return;
190 	}
191 
192 	len = sizeof(n);
193 	if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &n, &len) == -1) {
194 		log_err("Could not get socket send buffer size: %m");
195 		close(fd);
196 		return;
197 	}
198 
199 	if (n < (mtu * 2)) {
200 		n = mtu * 2;
201 		if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &n, sizeof(n)) == -1) {
202 			log_err("Could not set socket send buffer size (%d): %m", n);
203 			close(fd);
204 			return;
205 		}
206 	}
207 
208 	n = mtu;
209 	if (setsockopt(fd, SOL_SOCKET, SO_SNDLOWAT, &n, sizeof(n)) == -1) {
210 		log_err("Could not set socket low water mark (%d): %m", n);
211 		close(fd);
212 		return;
213 	}
214 
215 	len = sizeof(la);
216 	if (getsockname(fd, (struct sockaddr *)&la, &len) == -1) {
217 		log_err("Could not get socket address: %m");
218 		close(fd);
219 		return;
220 	}
221 
222 	log_info("Accepted connection from %s", bt_ntoa(&ra.l2cap_bdaddr, NULL));
223 
224 	chan = channel_alloc();
225 	if (chan == NULL) {
226 		close(fd);
227 		return;
228 	}
229 
230 	chan->send = bnep_send;
231 	chan->recv = bnep_recv;
232 	chan->mru = mru;
233 	chan->mtu = mtu;
234 	b2eaddr(chan->raddr, &ra.l2cap_bdaddr);
235 	b2eaddr(chan->laddr, &la.l2cap_bdaddr);
236 	chan->state = CHANNEL_WAIT_CONNECT_REQ;
237 	channel_timeout(chan, 10);
238 	if (!channel_open(chan, fd)) {
239 		chan->state = CHANNEL_CLOSED;
240 		channel_free(chan);
241 		close(fd);
242 		return;
243 	}
244 }
245 
246 static void
247 server_register(void)
248 {
249 	sdp_nap_profile_t p;
250 	int rv;
251 
252 	if (server_ss == NULL) {
253 		server_ss = sdp_open_local(control_path);
254 		if (server_ss == NULL || sdp_error(server_ss) != 0) {
255 			log_err("failed to contact SDP server");
256 			return;
257 		}
258 	}
259 
260 	memset(&p, 0, sizeof(p));
261 	p.psm = l2cap_psm;
262 	p.load_factor = server_avail;
263 	p.security_description = (l2cap_mode == 0 ? 0x0000 : 0x0001);
264 
265 	if (server_handle)
266 		rv = sdp_change_service(server_ss, server_handle,
267 		    (uint8_t *)&p, sizeof(p));
268 	else
269 		rv = sdp_register_service(server_ss, service_class,
270 		    &local_bdaddr, (uint8_t *)&p, sizeof(p), &server_handle);
271 
272 	if (rv != 0) {
273 		errno = sdp_error(server_ss);
274 		log_err("%s: %m", service_name);
275 		exit(EXIT_FAILURE);
276 	}
277 }
278