1/*	$NetBSD: server.c,v 1.7 2011/02/08 21:59:50 plunky Exp $	*/
2
3/*-
4 * Copyright (c) 2008-2009 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#include <sys/cdefs.h>
29__RCSID("$NetBSD: server.c,v 1.7 2011/02/08 21:59:50 plunky Exp $");
30
31#include <sys/ioctl.h>
32
33#include <net/ethertypes.h>
34
35#include <bluetooth.h>
36#include <errno.h>
37#include <sdp.h>
38#include <unistd.h>
39
40#include "btpand.h"
41#include "bnep.h"
42
43static struct event	server_ev;
44static int		server_count;
45
46static sdp_session_t	server_ss;
47static uint32_t		server_handle;
48static sdp_data_t	server_record;
49
50static char *		server_ipv4_subnet;
51static char *		server_ipv6_subnet;
52static uint16_t		server_proto[] = { ETHERTYPE_IP, ETHERTYPE_ARP, ETHERTYPE_IPV6 };
53static size_t		server_nproto = __arraycount(server_proto);
54
55static void server_open(void);
56static void server_read(int, short, void *);
57static void server_down(channel_t *);
58static void server_update(void);
59static void server_mkrecord(void);
60
61void
62server_init(void)
63{
64
65	if (server_limit == 0)
66		return;
67
68	server_open();
69	server_update();
70}
71
72/*
73 * Start listening on server socket
74 */
75static void
76server_open(void)
77{
78	struct sockaddr_bt sa;
79	socklen_t len;
80	uint16_t mru;
81	int fd;
82
83	fd = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
84	if (fd == -1) {
85		log_err("Could not open L2CAP socket: %m");
86		exit(EXIT_FAILURE);
87	}
88
89	memset(&sa, 0, sizeof(sa));
90	sa.bt_family = AF_BLUETOOTH;
91	sa.bt_len = sizeof(sa);
92	sa.bt_psm = l2cap_psm;
93	bdaddr_copy(&sa.bt_bdaddr, &local_bdaddr);
94	if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
95		log_err("Could not bind server socket: %m");
96		exit(EXIT_FAILURE);
97	}
98
99	if (setsockopt(fd, BTPROTO_L2CAP,
100	    SO_L2CAP_LM, &l2cap_mode, sizeof(l2cap_mode)) == -1) {
101		log_err("Could not set link mode (0x%4.4x): %m", l2cap_mode);
102		exit(EXIT_FAILURE);
103	}
104	len = sizeof(l2cap_mode);
105	getsockopt(fd, BTPROTO_L2CAP, SO_L2CAP_LM, &l2cap_mode, &len);
106
107	mru = BNEP_MTU_MIN;
108	if (setsockopt(fd, BTPROTO_L2CAP,
109	    SO_L2CAP_IMTU, &mru, sizeof(mru)) == -1) {
110		log_err("Could not set L2CAP IMTU (%d): %m", mru);
111		exit(EXIT_FAILURE);
112	}
113
114	if (listen(fd, 0) == -1) {
115		log_err("Could not listen on server socket: %m");
116		exit(EXIT_FAILURE);
117	}
118
119	event_set(&server_ev, fd, EV_READ | EV_PERSIST, server_read, NULL);
120	if (event_add(&server_ev, NULL) == -1) {
121		log_err("Could not add server event: %m");
122		exit(EXIT_FAILURE);
123	}
124
125	log_info("server socket open");
126}
127
128/*
129 * handle connection request
130 */
131static void
132server_read(int s, short ev, void *arg)
133{
134	struct sockaddr_bt ra, la;
135	channel_t *chan;
136	socklen_t len;
137	int fd, n;
138	uint16_t mru, mtu;
139
140	assert(server_count < server_limit);
141
142	len = sizeof(ra);
143	fd = accept(s, (struct sockaddr *)&ra, &len);
144	if (fd == -1)
145		return;
146
147	n = 1;
148	if (ioctl(fd, FIONBIO, &n) == -1) {
149		log_err("Could not set NonBlocking IO: %m");
150		close(fd);
151		return;
152	}
153
154	len = sizeof(mru);
155	if (getsockopt(fd, BTPROTO_L2CAP, SO_L2CAP_IMTU, &mru, &len) == -1) {
156		log_err("Could not get L2CAP IMTU: %m");
157		close(fd);
158		return;
159	}
160	if(mru < BNEP_MTU_MIN) {
161		log_err("L2CAP IMTU too small (%d)", mru);
162		close(fd);
163		return;
164	}
165
166	len = sizeof(n);
167	if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &n, &len) == -1) {
168		log_err("Could not read SO_RCVBUF");
169		close(fd);
170		return;
171	}
172	if (n < 10 * mru) {
173		n = 10 * mru;
174		if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n)) == -1)
175			log_info("Could not increase SO_RCVBUF (to %d)", n);
176	}
177
178	len = sizeof(mtu);
179	if (getsockopt(fd, BTPROTO_L2CAP, SO_L2CAP_OMTU, &mtu, &len) == -1) {
180		log_err("Could not get L2CAP OMTU: %m");
181		close(fd);
182		return;
183	}
184	if (mtu < BNEP_MTU_MIN) {
185		log_err("L2CAP OMTU too small (%d)", mtu);
186		close(fd);
187		return;
188	}
189
190	len = sizeof(n);
191	if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &n, &len) == -1) {
192		log_err("Could not get socket send buffer size: %m");
193		close(fd);
194		return;
195	}
196	if (n < (mtu * 2)) {
197		n = mtu * 2;
198		if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &n, sizeof(n)) == -1) {
199			log_err("Could not set socket send buffer size (%d): %m", n);
200			close(fd);
201			return;
202		}
203	}
204	n = mtu;
205	if (setsockopt(fd, SOL_SOCKET, SO_SNDLOWAT, &n, sizeof(n)) == -1) {
206		log_err("Could not set socket low water mark (%d): %m", n);
207		close(fd);
208		return;
209	}
210
211	len = sizeof(la);
212	if (getsockname(fd, (struct sockaddr *)&la, &len) == -1) {
213		log_err("Could not get socket address: %m");
214		close(fd);
215		return;
216	}
217
218	log_info("Accepted connection from %s", bt_ntoa(&ra.bt_bdaddr, NULL));
219
220	chan = channel_alloc();
221	if (chan == NULL) {
222		close(fd);
223		return;
224	}
225
226	chan->send = bnep_send;
227	chan->recv = bnep_recv;
228	chan->down = server_down;
229	chan->mru = mru;
230	chan->mtu = mtu;
231	b2eaddr(chan->raddr, &ra.bt_bdaddr);
232	b2eaddr(chan->laddr, &la.bt_bdaddr);
233	chan->state = CHANNEL_WAIT_CONNECT_REQ;
234	channel_timeout(chan, 10);
235	if (!channel_open(chan, fd)) {
236		chan->state = CHANNEL_CLOSED;
237		channel_free(chan);
238		close(fd);
239		return;
240	}
241
242	if (++server_count == server_limit) {
243		log_info("Server limit reached, closing server socket");
244		event_del(&server_ev);
245		close(s);
246	}
247
248	server_update();
249}
250
251/*
252 * Shut down a server channel, we need to update the service record and
253 * may want to restart accepting connections on the server socket
254 */
255static void
256server_down(channel_t *chan)
257{
258
259	assert(server_count > 0);
260
261	channel_close(chan);
262
263	if (server_count-- == server_limit)
264		server_open();
265
266	server_update();
267}
268
269static void
270server_update(void)
271{
272	bool rv;
273
274	if (service_type == NULL)
275		return;
276
277	if (server_ss == NULL) {
278		server_ss = sdp_open_local(control_path);
279		if (server_ss == NULL) {
280			log_err("failed to contact SDP server");
281			return;
282		}
283	}
284
285	server_mkrecord();
286
287	if (server_handle == 0)
288		rv = sdp_record_insert(server_ss, &local_bdaddr,
289		    &server_handle, &server_record);
290	else
291		rv = sdp_record_update(server_ss, server_handle,
292		    &server_record);
293
294	if (!rv) {
295		log_err("%s: %m", service_type);
296		exit(EXIT_FAILURE);
297	}
298}
299
300static void
301server_mkrecord(void)
302{
303	static uint8_t data[256];	/* tis enough */
304	sdp_data_t buf;
305	size_t i;
306
307	buf.next = data;
308	buf.end = data + sizeof(data);
309
310	sdp_put_uint16(&buf, SDP_ATTR_SERVICE_RECORD_HANDLE);
311	sdp_put_uint32(&buf, 0x00000000);
312
313	sdp_put_uint16(&buf, SDP_ATTR_SERVICE_CLASS_ID_LIST);
314	sdp_put_seq(&buf, 3);
315	sdp_put_uuid16(&buf, service_class);
316
317	sdp_put_uint16(&buf, SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST);
318	sdp_put_seq(&buf, 8 + 10 + 3 * server_nproto);
319	sdp_put_seq(&buf, 6);
320	sdp_put_uuid16(&buf, SDP_UUID_PROTOCOL_L2CAP);
321	sdp_put_uint16(&buf, l2cap_psm);
322	sdp_put_seq(&buf, 8 + 3 * server_nproto);
323	sdp_put_uuid16(&buf, SDP_UUID_PROTOCOL_BNEP);
324	sdp_put_uint16(&buf, 0x0100);	/* v1.0 */
325	sdp_put_seq(&buf, 3 * server_nproto);
326	for (i = 0; i < server_nproto; i++)
327		sdp_put_uint16(&buf, server_proto[i]);
328
329	sdp_put_uint16(&buf, SDP_ATTR_BROWSE_GROUP_LIST);
330	sdp_put_seq(&buf, 3);
331	sdp_put_uuid16(&buf, SDP_SERVICE_CLASS_PUBLIC_BROWSE_GROUP);
332
333	sdp_put_uint16(&buf, SDP_ATTR_LANGUAGE_BASE_ATTRIBUTE_ID_LIST);
334	sdp_put_seq(&buf, 9);
335	sdp_put_uint16(&buf, 0x656e);	/* "en" */
336	sdp_put_uint16(&buf, 106);	/* UTF-8 */
337	sdp_put_uint16(&buf, SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID);
338
339	sdp_put_uint16(&buf, SDP_ATTR_SERVICE_AVAILABILITY);
340	sdp_put_uint8(&buf, (UINT8_MAX - server_count * UINT8_MAX / server_limit));
341
342	sdp_put_uint16(&buf, SDP_ATTR_BLUETOOTH_PROFILE_DESCRIPTOR_LIST);
343	sdp_put_seq(&buf, 8);
344	sdp_put_seq(&buf, 6);
345	sdp_put_uuid16(&buf, service_class);
346	sdp_put_uint16(&buf, 0x0100);	/* v1.0 */
347
348	sdp_put_uint16(&buf, SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID
349	    + SDP_ATTR_SERVICE_NAME_OFFSET);
350	sdp_put_str(&buf, service_name, -1);
351
352	sdp_put_uint16(&buf, SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID
353	    + SDP_ATTR_SERVICE_DESCRIPTION_OFFSET);
354	sdp_put_str(&buf, service_desc, -1);
355
356	sdp_put_uint16(&buf, SDP_ATTR_SECURITY_DESCRIPTION);
357	sdp_put_uint16(&buf, (l2cap_mode & L2CAP_LM_AUTH) ?  0x0001 : 0x0000);
358
359	if (service_class == SDP_SERVICE_CLASS_NAP) {
360		sdp_put_uint16(&buf, SDP_ATTR_NET_ACCESS_TYPE);
361		sdp_put_uint16(&buf, 0x0004);	/* 10Mb Ethernet */
362
363		sdp_put_uint16(&buf, SDP_ATTR_MAX_NET_ACCESS_RATE);
364		sdp_put_uint32(&buf, IF_Mbps(10) / 8);	/* octets/second */
365	}
366
367	if (service_class == SDP_SERVICE_CLASS_NAP
368	    || service_class == SDP_SERVICE_CLASS_GN) {
369		if (server_ipv4_subnet) {
370			sdp_put_uint16(&buf, SDP_ATTR_IPV4_SUBNET);
371			sdp_put_str(&buf, server_ipv4_subnet, -1);
372		}
373
374		if (server_ipv6_subnet) {
375			sdp_put_uint16(&buf, SDP_ATTR_IPV6_SUBNET);
376			sdp_put_str(&buf, server_ipv6_subnet, -1);
377		}
378	}
379
380	server_record.next = data;
381	server_record.end = buf.next;
382}
383