4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $Id: server.c,v 1.6 2004/01/13 01:54:39 max Exp $
34 #include <sys/param.h>
35 #include <sys/select.h>
37 #include <sys/queue.h>
38 #include <sys/ucred.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
43 #define L2CAP_SOCKET_CHECKED
44 #include <bluetooth.h>
57 static void server_accept_client (server_p srv, int32_t fd);
58 static int32_t server_process_request (server_p srv, int32_t fd);
59 static int32_t server_send_error_response (server_p srv, int32_t fd,
61 static void server_close_fd (server_p srv, int32_t fd);
68 server_init(server_p srv, char const *control)
70 struct sockaddr_un un;
71 struct sockaddr_l2cap l2;
72 int32_t unsock, l2sock;
77 assert(control != NULL);
79 memset(srv, 0, sizeof(*srv));
81 /* Open control socket */
82 if (unlink(control) < 0 && errno != ENOENT) {
83 log_crit("Could not unlink(%s). %s (%d)",
84 control, strerror(errno), errno);
88 unsock = socket(PF_LOCAL, SOCK_STREAM, 0);
90 log_crit("Could not create control socket. %s (%d)",
91 strerror(errno), errno);
95 memset(&un, 0, sizeof(un));
96 un.sun_len = sizeof(un);
97 un.sun_family = AF_LOCAL;
98 strlcpy(un.sun_path, control, sizeof(un.sun_path));
100 if (bind(unsock, (struct sockaddr *) &un, sizeof(un)) < 0) {
101 log_crit("Could not bind control socket. %s (%d)",
102 strerror(errno), errno);
107 if (chmod(control, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) < 0) {
108 log_crit("Could not change permissions on control socket. " \
109 "%s (%d)", strerror(errno), errno);
114 if (listen(unsock, 10) < 0) {
115 log_crit("Could not listen on control socket. %s (%d)",
116 strerror(errno), errno);
121 /* Open L2CAP socket */
122 l2sock = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BLUETOOTH_PROTO_L2CAP);
124 log_crit("Could not create L2CAP socket. %s (%d)",
125 strerror(errno), errno);
131 if (getsockopt(l2sock, SOL_L2CAP, SO_L2CAP_IMTU, &imtu, &size) < 0) {
132 log_crit("Could not get L2CAP IMTU. %s (%d)",
133 strerror(errno), errno);
139 memset(&l2, 0, sizeof(l2));
140 l2.l2cap_len = sizeof(l2);
141 l2.l2cap_family = AF_BLUETOOTH;
142 memcpy(&l2.l2cap_bdaddr, NG_HCI_BDADDR_ANY, sizeof(l2.l2cap_bdaddr));
143 l2.l2cap_psm = htole16(NG_L2CAP_PSM_SDP);
145 if (bind(l2sock, (struct sockaddr *) &l2, sizeof(l2)) < 0) {
146 log_crit("Could not bind L2CAP socket. %s (%d)",
147 strerror(errno), errno);
153 if (listen(l2sock, 10) < 0) {
154 log_crit("Could not listen on L2CAP socket. %s (%d)",
155 strerror(errno), errno);
161 /* Allocate incoming buffer */
162 srv->imtu = (imtu > SDP_LOCAL_MTU)? imtu : SDP_LOCAL_MTU;
163 srv->req = (uint8_t *) calloc(srv->imtu, sizeof(srv->req[0]));
164 if (srv->req == NULL) {
165 log_crit("Could not allocate request buffer");
171 /* Allocate memory for descriptor index */
172 srv->fdidx = (fd_idx_p) calloc(FD_SETSIZE, sizeof(srv->fdidx[0]));
173 if (srv->fdidx == NULL) {
174 log_crit("Could not allocate fd index");
181 /* Register Service Discovery profile (attach it to control socket) */
182 if (provider_register_sd(unsock) < 0) {
183 log_crit("Could not register Service Discovery profile");
192 * If we got here then everything is fine. Add both control sockets
196 FD_ZERO(&srv->fdset);
197 srv->maxfd = (unsock > l2sock)? unsock : l2sock;
199 FD_SET(unsock, &srv->fdset);
200 srv->fdidx[unsock].valid = 1;
201 srv->fdidx[unsock].server = 1;
202 srv->fdidx[unsock].control = 1;
203 srv->fdidx[unsock].priv = 0;
204 srv->fdidx[unsock].rsp_cs = 0;
205 srv->fdidx[unsock].rsp_size = 0;
206 srv->fdidx[unsock].rsp_limit = 0;
207 srv->fdidx[unsock].omtu = SDP_LOCAL_MTU;
208 srv->fdidx[unsock].rsp = NULL;
210 FD_SET(l2sock, &srv->fdset);
211 srv->fdidx[l2sock].valid = 1;
212 srv->fdidx[l2sock].server = 1;
213 srv->fdidx[l2sock].control = 0;
214 srv->fdidx[l2sock].priv = 0;
215 srv->fdidx[l2sock].rsp_cs = 0;
216 srv->fdidx[l2sock].rsp_size = 0;
217 srv->fdidx[l2sock].rsp_limit = 0;
218 srv->fdidx[l2sock].omtu = 0; /* unknown */
219 srv->fdidx[l2sock].rsp = NULL;
229 server_shutdown(server_p srv)
235 for (fd = 0; fd < srv->maxfd + 1; fd ++)
236 if (srv->fdidx[fd].valid)
237 server_close_fd(srv, fd);
242 memset(srv, 0, sizeof(*srv));
246 * Do one server iteration
250 server_do(server_p srv)
257 /* Copy cached version of the fd set and call select */
258 memcpy(&fdset, &srv->fdset, sizeof(fdset));
259 n = select(srv->maxfd + 1, &fdset, NULL, NULL, NULL);
264 log_err("Could not select(%d, %p). %s (%d)",
265 srv->maxfd + 1, &fdset, strerror(errno), errno);
270 /* Process descriptors */
271 for (fd = 0; fd < srv->maxfd + 1 && n > 0; fd ++) {
272 if (!FD_ISSET(fd, &fdset))
275 assert(srv->fdidx[fd].valid);
278 if (srv->fdidx[fd].server)
279 server_accept_client(srv, fd);
280 else if (server_process_request(srv, fd) != 0)
281 server_close_fd(srv, fd);
289 * Accept new client connection and register it with index
293 server_accept_client(server_p srv, int32_t fd)
301 cfd = accept(fd, NULL, NULL);
302 } while (cfd < 0 && errno == EINTR);
305 log_err("Could not accept connection on %s socket. %s (%d)",
306 srv->fdidx[fd].control? "control" : "L2CAP",
307 strerror(errno), errno);
311 assert(!FD_ISSET(cfd, &srv->fdset));
312 assert(!srv->fdidx[cfd].valid);
316 if (!srv->fdidx[fd].control) {
317 /* Get local BD_ADDR */
318 size = sizeof(srv->req_sa);
319 if (getsockname(cfd,(struct sockaddr*)&srv->req_sa,&size) < 0) {
320 log_err("Could not get local BD_ADDR. %s (%d)",
321 strerror(errno), errno);
326 /* Get outgoing MTU */
328 if (getsockopt(cfd,SOL_L2CAP,SO_L2CAP_OMTU,&omtu,&size) < 0) {
329 log_err("Could not get L2CAP OMTU. %s (%d)",
330 strerror(errno), errno);
336 * The maximum size of the L2CAP packet is 65536 bytes.
337 * The minimum L2CAP MTU is 43 bytes. That means we need
338 * 65536 / 43 = ~1524 chunks to transfer maximum packet
339 * size with minimum MTU. The "rsp_cs" field in fd_idx_t
340 * is 11 bits wide, which gives us up to 2048 chunks.
343 if (omtu < NG_L2CAP_MTU_MINIMUM) {
344 log_err("L2CAP OMTU is too small (%d bytes)", omtu);
352 /* Get peer's credentials */
353 memset(&cr, 0, sizeof(cr));
356 if (getsockopt(cfd, 0, LOCAL_PEERCRED, &cr, &size) < 0) {
357 log_err("Could not get peer's credentials. %s (%d)",
358 strerror(errno), errno);
363 /* Check credentials */
364 pw = getpwuid(cr.cr_uid);
366 priv = (strcmp(pw->pw_name, "root") == 0);
368 log_warning("Could not verify credentials for uid %d",
371 memcpy(&srv->req_sa.l2cap_bdaddr, NG_HCI_BDADDR_ANY,
372 sizeof(srv->req_sa.l2cap_bdaddr));
374 omtu = srv->fdidx[fd].omtu;
378 * Allocate buffer. This is an overkill, but we can not know how
379 * big our reply is going to be.
382 rsp = (uint8_t *) calloc(NG_L2CAP_MTU_MAXIMUM, sizeof(rsp[0]));
384 log_crit("Could not allocate response buffer");
389 /* Add client descriptor to the index */
390 FD_SET(cfd, &srv->fdset);
391 if (srv->maxfd < cfd)
393 srv->fdidx[cfd].valid = 1;
394 srv->fdidx[cfd].server = 0;
395 srv->fdidx[cfd].control = srv->fdidx[fd].control;
396 srv->fdidx[cfd].priv = priv;
397 srv->fdidx[cfd].rsp_cs = 0;
398 srv->fdidx[cfd].rsp_size = 0;
399 srv->fdidx[cfd].rsp_limit = 0;
400 srv->fdidx[cfd].omtu = omtu;
401 srv->fdidx[cfd].rsp = rsp;
405 * Process request from the client
409 server_process_request(server_p srv, int32_t fd)
411 sdp_pdu_p pdu = (sdp_pdu_p) srv->req;
414 assert(srv->imtu > 0);
415 assert(srv->req != NULL);
416 assert(FD_ISSET(fd, &srv->fdset));
417 assert(srv->fdidx[fd].valid);
418 assert(!srv->fdidx[fd].server);
419 assert(srv->fdidx[fd].rsp != NULL);
420 assert(srv->fdidx[fd].omtu >= NG_L2CAP_MTU_MINIMUM);
423 len = read(fd, srv->req, srv->imtu);
424 } while (len < 0 && errno == EINTR);
427 log_err("Could not receive SDP request from %s socket. %s (%d)",
428 srv->fdidx[fd].control? "control" : "L2CAP",
429 strerror(errno), errno);
433 log_info("Client on %s socket has disconnected",
434 srv->fdidx[fd].control? "control" : "L2CAP");
438 if (len >= sizeof(*pdu) &&
439 sizeof(*pdu) + (pdu->len = ntohs(pdu->len)) == len) {
441 case SDP_PDU_SERVICE_SEARCH_REQUEST:
442 error = server_prepare_service_search_response(srv, fd);
445 case SDP_PDU_SERVICE_ATTRIBUTE_REQUEST:
446 error = server_prepare_service_attribute_response(srv, fd);
449 case SDP_PDU_SERVICE_SEARCH_ATTRIBUTE_REQUEST:
450 error = server_prepare_service_search_attribute_response(srv, fd);
453 case SDP_PDU_SERVICE_REGISTER_REQUEST:
454 error = server_prepare_service_register_response(srv, fd);
457 case SDP_PDU_SERVICE_UNREGISTER_REQUEST:
458 error = server_prepare_service_unregister_response(srv, fd);
461 case SDP_PDU_SERVICE_CHANGE_REQUEST:
462 error = server_prepare_service_change_response(srv, fd);
466 error = SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX;
470 error = SDP_ERROR_CODE_INVALID_PDU_SIZE;
474 case SDP_PDU_SERVICE_SEARCH_REQUEST:
475 error = server_send_service_search_response(srv, fd);
478 case SDP_PDU_SERVICE_ATTRIBUTE_REQUEST:
479 error = server_send_service_attribute_response(srv, fd);
482 case SDP_PDU_SERVICE_SEARCH_ATTRIBUTE_REQUEST:
483 error = server_send_service_search_attribute_response(srv, fd);
486 case SDP_PDU_SERVICE_REGISTER_REQUEST:
487 error = server_send_service_register_response(srv, fd);
490 case SDP_PDU_SERVICE_UNREGISTER_REQUEST:
491 error = server_send_service_unregister_response(srv, fd);
494 case SDP_PDU_SERVICE_CHANGE_REQUEST:
495 error = server_send_service_change_response(srv, fd);
499 error = SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX;
504 log_err("Could not send SDP response to %s socket, " \
505 "pdu->pid=%d, pdu->tid=%d, error=%d",
506 srv->fdidx[fd].control? "control" : "L2CAP",
507 pdu->pid, ntohs(pdu->tid), error);
509 log_err("Could not process SDP request from %s socket, " \
510 "pdu->pid=%d, pdu->tid=%d, pdu->len=%d, len=%d, " \
512 srv->fdidx[fd].control? "control" : "L2CAP",
513 pdu->pid, ntohs(pdu->tid), pdu->len, len, error);
515 error = server_send_error_response(srv, fd, error);
517 log_err("Could not send SDP error response to %s " \
518 "socket, pdu->pid=%d, pdu->tid=%d, error=%d",
519 srv->fdidx[fd].control? "control" : "L2CAP",
520 pdu->pid, ntohs(pdu->tid), error);
523 /* On error forget response (if any) */
525 srv->fdidx[fd].rsp_cs = 0;
526 srv->fdidx[fd].rsp_size = 0;
527 srv->fdidx[fd].rsp_limit = 0;
534 * Send SDP_Error_Response PDU
538 server_send_error_response(server_p srv, int32_t fd, uint16_t error)
545 } __attribute__ ((packed)) rsp;
547 /* Prepare and send SDP error response */
548 rsp.pdu.pid = SDP_PDU_ERROR_RESPONSE;
549 rsp.pdu.tid = ((sdp_pdu_p)(srv->req))->tid;
550 rsp.pdu.len = htons(sizeof(rsp.error));
551 rsp.error = htons(error);
554 size = write(fd, &rsp, sizeof(rsp));
555 } while (size < 0 && errno == EINTR);
557 return ((size < 0)? errno : 0);
561 * Close descriptor and remove it from index
565 server_close_fd(server_p srv, int32_t fd)
567 provider_p provider = NULL, provider_next = NULL;
569 assert(FD_ISSET(fd, &srv->fdset));
570 assert(srv->fdidx[fd].valid);
574 FD_CLR(fd, &srv->fdset);
575 if (fd == srv->maxfd)
578 if (srv->fdidx[fd].rsp != NULL)
579 free(srv->fdidx[fd].rsp);
581 memset(&srv->fdidx[fd], 0, sizeof(srv->fdidx[fd]));
583 for (provider = provider_get_first();
585 provider = provider_next) {
586 provider_next = provider_get_next(provider);
588 if (provider->fd == fd)
589 provider_unregister(provider);