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