1184610Salfred/*	$NetBSD: server.c,v 1.6 2009/10/25 19:28:45 plunky Exp $	*/
2184610Salfred
3184610Salfred/*-
4184610Salfred * Copyright (c) 2008-2009 Iain Hibbert
5184610Salfred * All rights reserved.
6184610Salfred *
7184610Salfred * Redistribution and use in source and binary forms, with or without
8184610Salfred * modification, are permitted provided that the following conditions
9184610Salfred * are met:
10184610Salfred * 1. Redistributions of source code must retain the above copyright
11184610Salfred *    notice, this list of conditions and the following disclaimer.
12184610Salfred * 2. Redistributions in binary form must reproduce the above copyright
13184610Salfred *    notice, this list of conditions and the following disclaimer in the
14184610Salfred *    documentation and/or other materials provided with the distribution.
15184610Salfred *
16184610Salfred * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17184610Salfred * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18184610Salfred * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19184610Salfred * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20188417Sthompsa * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21188969Sthompsa * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22184610Salfred * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23188417Sthompsa * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24188417Sthompsa * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25188417Sthompsa * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26184610Salfred */
27184610Salfred
28188417Sthompsa#include <sys/cdefs.h>
29188417Sthompsa__RCSID("$NetBSD: server.c,v 1.6 2009/10/25 19:28:45 plunky Exp $");
30188417Sthompsa
31188417Sthompsa#include <sys/ioctl.h>
32188417Sthompsa
33188417Sthompsa#include <net/ethertypes.h>
34192468Ssam
35192468Ssam#include <bluetooth.h>
36188417Sthompsa#include <errno.h>
37184610Salfred#include <sdp.h>
38184610Salfred#include <unistd.h>
39188417Sthompsa
40188417Sthompsa#include "btpand.h"
41188417Sthompsa#include "bnep.h"
42188417Sthompsa
43188417Sthompsastatic struct event	server_ev;
44192468Ssamstatic int		server_count;
45192468Ssam
46184610Salfredstatic sdp_session_t	server_ss;
47188417Sthompsastatic uint32_t		server_handle;
48188417Sthompsastatic sdp_data_t	server_record;
49188417Sthompsa
50188417Sthompsastatic char *		server_ipv4_subnet;
51188417Sthompsastatic char *		server_ipv6_subnet;
52188417Sthompsastatic uint16_t		server_proto[] = { ETHERTYPE_IP, ETHERTYPE_ARP, ETHERTYPE_IPV6 };
53188417Sthompsastatic size_t		server_nproto = __arraycount(server_proto);
54184610Salfred
55184610Salfredstatic void server_open(void);
56188417Sthompsastatic void server_read(int, short, void *);
57188417Sthompsastatic void server_down(channel_t *);
58188417Sthompsastatic void server_update(void);
59188417Sthompsastatic void server_mkrecord(void);
60188417Sthompsa
61184610Salfredvoid
62188417Sthompsaserver_init(void)
63184610Salfred{
64188417Sthompsa
65188417Sthompsa	if (server_limit == 0)
66188417Sthompsa		return;
67188417Sthompsa
68188417Sthompsa	server_open();
69188417Sthompsa	server_update();
70188417Sthompsa}
71184610Salfred
72188417Sthompsa/*
73184610Salfred * Start listening on server socket
74188417Sthompsa */
75188417Sthompsastatic void
76188417Sthompsaserver_open(void)
77184610Salfred{
78188417Sthompsa	struct sockaddr_bt sa;
79184610Salfred	socklen_t len;
80188417Sthompsa	uint16_t mru;
81188417Sthompsa	int fd;
82188417Sthompsa
83188417Sthompsa	fd = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
84192984Sthompsa	if (fd == -1) {
85191746Sthompsa		log_err("Could not open L2CAP socket: %m");
86184610Salfred		exit(EXIT_FAILURE);
87188417Sthompsa	}
88188417Sthompsa
89184610Salfred	memset(&sa, 0, sizeof(sa));
90188417Sthompsa	sa.bt_family = AF_BLUETOOTH;
91184610Salfred	sa.bt_len = sizeof(sa);
92187259Sthompsa	sa.bt_psm = l2cap_psm;
93188417Sthompsa	bdaddr_copy(&sa.bt_bdaddr, &local_bdaddr);
94188417Sthompsa	if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
95188417Sthompsa		log_err("Could not bind server socket: %m");
96187259Sthompsa		exit(EXIT_FAILURE);
97187259Sthompsa	}
98184610Salfred
99188417Sthompsa	if (setsockopt(fd, BTPROTO_L2CAP,
100188417Sthompsa	    SO_L2CAP_LM, &l2cap_mode, sizeof(l2cap_mode)) == -1) {
101192984Sthompsa		log_err("Could not set link mode (0x%4.4x): %m", l2cap_mode);
102184610Salfred		exit(EXIT_FAILURE);
103188417Sthompsa	}
104188417Sthompsa	len = sizeof(l2cap_mode);
105184610Salfred	getsockopt(fd, BTPROTO_L2CAP, SO_L2CAP_LM, &l2cap_mode, &len);
106192984Sthompsa
107184610Salfred	mru = BNEP_MTU_MIN;
108188419Sthompsa	if (setsockopt(fd, BTPROTO_L2CAP,
109188417Sthompsa	    SO_L2CAP_IMTU, &mru, sizeof(mru)) == -1) {
110188417Sthompsa		log_err("Could not set L2CAP IMTU (%d): %m", mru);
111188417Sthompsa		exit(EXIT_FAILURE);
112188417Sthompsa	}
113188417Sthompsa
114188417Sthompsa	if (listen(fd, 0) == -1) {
115188417Sthompsa		log_err("Could not listen on server socket: %m");
116188417Sthompsa		exit(EXIT_FAILURE);
117188417Sthompsa	}
118188417Sthompsa
119188419Sthompsa	event_set(&server_ev, fd, EV_READ | EV_PERSIST, server_read, NULL);
120188417Sthompsa	if (event_add(&server_ev, NULL) == -1) {
121188417Sthompsa		log_err("Could not add server event: %m");
122188417Sthompsa		exit(EXIT_FAILURE);
123188417Sthompsa	}
124188417Sthompsa
125188417Sthompsa	log_info("server socket open");
126188417Sthompsa}
127188417Sthompsa
128188417Sthompsa/*
129188417Sthompsa * handle connection request
130188417Sthompsa */
131188417Sthompsastatic void
132188417Sthompsaserver_read(int s, short ev, void *arg)
133188417Sthompsa{
134188417Sthompsa	struct sockaddr_bt ra, la;
135188417Sthompsa	channel_t *chan;
136188417Sthompsa	socklen_t len;
137184610Salfred	int fd, n, bufsize;
138188417Sthompsa	uint16_t mru, mtu;
139188417Sthompsa
140188417Sthompsa	assert(server_count < server_limit);
141188417Sthompsa
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(bufsize);
167	if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &bufsize, &len) == -1) {
168		log_err("Could not read SO_RCVBUF");
169		close(fd);
170		return;
171	}
172	if (bufsize < 10 * mru) {
173		bufsize = 10 * mru;
174		if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &bufsize,
175		    sizeof(bufsize)) == -1)
176			log_info("Could not increase SO_RCVBUF (from %d)",
177			    bufsize);
178	}
179
180	len = sizeof(mtu);
181	if (getsockopt(fd, BTPROTO_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.bt_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->down = server_down;
233	chan->mru = mru;
234	chan->mtu = mtu;
235	b2eaddr(chan->raddr, &ra.bt_bdaddr);
236	b2eaddr(chan->laddr, &la.bt_bdaddr);
237	chan->state = CHANNEL_WAIT_CONNECT_REQ;
238	channel_timeout(chan, 10);
239	if (!channel_open(chan, fd)) {
240		chan->state = CHANNEL_CLOSED;
241		channel_free(chan);
242		close(fd);
243		return;
244	}
245
246	if (++server_count == server_limit) {
247		log_info("Server limit reached, closing server socket");
248		event_del(&server_ev);
249		close(s);
250	}
251
252	server_update();
253}
254
255/*
256 * Shut down a server channel, we need to update the service record and
257 * may want to restart accepting connections on the server socket
258 */
259static void
260server_down(channel_t *chan)
261{
262
263	assert(server_count > 0);
264
265	channel_close(chan);
266
267	if (server_count-- == server_limit)
268		server_open();
269
270	server_update();
271}
272
273static void
274server_update(void)
275{
276	bool rv;
277
278	if (service_type == NULL)
279		return;
280
281	if (server_ss == NULL) {
282		server_ss = sdp_open_local(control_path);
283		if (server_ss == NULL) {
284			log_err("failed to contact SDP server");
285			return;
286		}
287	}
288
289	server_mkrecord();
290
291	if (server_handle == 0)
292		rv = sdp_record_insert(server_ss, &local_bdaddr,
293		    &server_handle, &server_record);
294	else
295		rv = sdp_record_update(server_ss, server_handle,
296		    &server_record);
297
298	if (!rv) {
299		log_err("%s: %m", service_type);
300		exit(EXIT_FAILURE);
301	}
302}
303
304static void
305server_mkrecord(void)
306{
307	static uint8_t data[256];	/* tis enough */
308	sdp_data_t buf;
309	size_t i;
310
311	buf.next = data;
312	buf.end = data + sizeof(data);
313
314	sdp_put_uint16(&buf, SDP_ATTR_SERVICE_RECORD_HANDLE);
315	sdp_put_uint32(&buf, 0x00000000);
316
317	sdp_put_uint16(&buf, SDP_ATTR_SERVICE_CLASS_ID_LIST);
318	sdp_put_seq(&buf, 3);
319	sdp_put_uuid16(&buf, service_class);
320
321	sdp_put_uint16(&buf, SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST);
322	sdp_put_seq(&buf, 8 + 10 + 3 * server_nproto);
323	sdp_put_seq(&buf, 6);
324	sdp_put_uuid16(&buf, SDP_UUID_PROTOCOL_L2CAP);
325	sdp_put_uint16(&buf, l2cap_psm);
326	sdp_put_seq(&buf, 8 + 3 * server_nproto);
327	sdp_put_uuid16(&buf, SDP_UUID_PROTOCOL_BNEP);
328	sdp_put_uint16(&buf, 0x0100);	/* v1.0 */
329	sdp_put_seq(&buf, 3 * server_nproto);
330	for (i = 0; i < server_nproto; i++)
331		sdp_put_uint16(&buf, server_proto[i]);
332
333	sdp_put_uint16(&buf, SDP_ATTR_BROWSE_GROUP_LIST);
334	sdp_put_seq(&buf, 3);
335	sdp_put_uuid16(&buf, SDP_SERVICE_CLASS_PUBLIC_BROWSE_GROUP);
336
337	sdp_put_uint16(&buf, SDP_ATTR_LANGUAGE_BASE_ATTRIBUTE_ID_LIST);
338	sdp_put_seq(&buf, 9);
339	sdp_put_uint16(&buf, 0x656e);	/* "en" */
340	sdp_put_uint16(&buf, 106);	/* UTF-8 */
341	sdp_put_uint16(&buf, SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID);
342
343	sdp_put_uint16(&buf, SDP_ATTR_SERVICE_AVAILABILITY);
344	sdp_put_uint8(&buf, (UINT8_MAX - server_count * UINT8_MAX / server_limit));
345
346	sdp_put_uint16(&buf, SDP_ATTR_BLUETOOTH_PROFILE_DESCRIPTOR_LIST);
347	sdp_put_seq(&buf, 8);
348	sdp_put_seq(&buf, 6);
349	sdp_put_uuid16(&buf, service_class);
350	sdp_put_uint16(&buf, 0x0100);	/* v1.0 */
351
352	sdp_put_uint16(&buf, SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID
353	    + SDP_ATTR_SERVICE_NAME_OFFSET);
354	sdp_put_str(&buf, service_name, -1);
355
356	sdp_put_uint16(&buf, SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID
357	    + SDP_ATTR_SERVICE_DESCRIPTION_OFFSET);
358	sdp_put_str(&buf, service_desc, -1);
359
360	sdp_put_uint16(&buf, SDP_ATTR_SECURITY_DESCRIPTION);
361	sdp_put_uint16(&buf, (l2cap_mode & L2CAP_LM_AUTH) ?  0x0001 : 0x0000);
362
363	if (service_class == SDP_SERVICE_CLASS_NAP) {
364		sdp_put_uint16(&buf, SDP_ATTR_NET_ACCESS_TYPE);
365		sdp_put_uint16(&buf, 0x0004);	/* 10Mb Ethernet */
366
367		sdp_put_uint16(&buf, SDP_ATTR_MAX_NET_ACCESS_RATE);
368		sdp_put_uint32(&buf, IF_Mbps(10) / 8);	/* octets/second */
369	}
370
371	if (service_class == SDP_SERVICE_CLASS_NAP
372	    || service_class == SDP_SERVICE_CLASS_GN) {
373		if (server_ipv4_subnet) {
374			sdp_put_uint16(&buf, SDP_ATTR_IPV4_SUBNET);
375			sdp_put_str(&buf, server_ipv4_subnet, -1);
376		}
377
378		if (server_ipv6_subnet) {
379			sdp_put_uint16(&buf, SDP_ATTR_IPV6_SUBNET);
380			sdp_put_str(&buf, server_ipv6_subnet, -1);
381		}
382	}
383
384	server_record.next = data;
385	server_record.end = buf.next;
386}
387