Index: head/usr.sbin/bluetooth/bthidd/client.c =================================================================== --- head/usr.sbin/bluetooth/bthidd/client.c (revision 162493) +++ head/usr.sbin/bluetooth/bthidd/client.c (revision 162494) @@ -1,255 +1,256 @@ /* * client.c */ /*- * Copyright (c) 2006 Maksim Yevmenkin * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $Id: client.c,v 1.7 2006/09/07 21:06:53 max Exp $ * $FreeBSD$ */ #include #include #include #include #include #include #include #include #include #include #include #include "bthid_config.h" #include "bthidd.h" static int32_t client_socket(bdaddr_p bdaddr, int32_t psm); /* * Get next config entry and create outbound connection (if required) * * XXX Do only one device at a time. At least one of my devices (3COM * Bluetooth PCCARD) rejects Create_Connection command if another * Create_Connection command is still pending. Weird... */ static int32_t connect_in_progress = 0; int32_t client_rescan(bthid_server_p srv) { static hid_device_p d; bthid_session_p s; assert(srv != NULL); if (connect_in_progress) return (0); /* another connect is still pending */ d = get_next_hid_device(d); if (d == NULL) return (0); /* XXX should not happen? empty config? */ if ((s = session_by_bdaddr(srv, &d->bdaddr)) != NULL) return (0); /* session already active */ if (!d->new_device) { if (d->reconnect_initiate) return (0); /* device will initiate reconnect */ } syslog(LOG_NOTICE, "Opening outbound session for %s " \ "(new_device=%d, reconnect_initiate=%d)", bt_ntoa(&d->bdaddr, NULL), d->new_device, d->reconnect_initiate); if ((s = session_open(srv, d)) == NULL) { syslog(LOG_CRIT, "Could not create outbound session for %s", bt_ntoa(&d->bdaddr, NULL)); return (-1); } /* Open control channel */ s->ctrl = client_socket(&s->bdaddr, d->control_psm); if (s->ctrl < 0) { syslog(LOG_ERR, "Could not open control channel to %s. %s (%d)", bt_ntoa(&s->bdaddr, NULL), strerror(errno), errno); session_close(s); return (-1); } s->state = W4CTRL; FD_SET(s->ctrl, &srv->wfdset); if (s->ctrl > srv->maxfd) srv->maxfd = s->ctrl; connect_in_progress = 1; return (0); } /* * Process connect on the socket */ int32_t client_connect(bthid_server_p srv, int32_t fd) { bthid_session_p s; hid_device_p d; - int32_t error, len; + int32_t error; + socklen_t len; assert(srv != NULL); assert(fd >= 0); s = session_by_fd(srv, fd); assert(s != NULL); d = get_hid_device(&s->bdaddr); assert(d != NULL); error = 0; len = sizeof(error); if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { syslog(LOG_ERR, "Could not get socket error for %s. %s (%d)", bt_ntoa(&s->bdaddr, NULL), strerror(errno), errno); session_close(s); connect_in_progress = 0; return (-1); } if (error != 0) { syslog(LOG_ERR, "Could not connect to %s. %s (%d)", bt_ntoa(&s->bdaddr, NULL), strerror(error), error); session_close(s); connect_in_progress = 0; return (0); } switch (s->state) { case W4CTRL: /* Control channel is open */ assert(s->ctrl == fd); assert(s->intr == -1); /* Open interrupt channel */ s->intr = client_socket(&s->bdaddr, d->interrupt_psm); if (s->intr < 0) { syslog(LOG_ERR, "Could not open interrupt channel " \ "to %s. %s (%d)", bt_ntoa(&s->bdaddr, NULL), strerror(errno), errno); session_close(s); connect_in_progress = 0; return (-1); } s->state = W4INTR; FD_SET(s->intr, &srv->wfdset); if (s->intr > srv->maxfd) srv->maxfd = s->intr; d->new_device = 0; /* reset new device flag */ write_hids_file(); break; case W4INTR: /* Interrupt channel is open */ assert(s->ctrl != -1); assert(s->intr == fd); s->state = OPEN; connect_in_progress = 0; /* Register session's vkbd descriptor (if any) for read */ if (s->state == OPEN && d->keyboard) { assert(s->vkbd != -1); FD_SET(s->vkbd, &srv->rfdset); if (s->vkbd > srv->maxfd) srv->maxfd = s->vkbd; } break; default: assert(0); break; } /* Move fd to from the write fd set into read fd set */ FD_CLR(fd, &srv->wfdset); FD_SET(fd, &srv->rfdset); return (0); } /* * Create bound non-blocking socket and initiate connect */ static int client_socket(bdaddr_p bdaddr, int32_t psm) { struct sockaddr_l2cap l2addr; int32_t s, m; s = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BLUETOOTH_PROTO_L2CAP); if (s < 0) return (-1); m = fcntl(s, F_GETFL); if (m < 0) { close(s); return (-1); } if (fcntl(s, F_SETFL, (m|O_NONBLOCK)) < 0) { close(s); return (-1); } l2addr.l2cap_len = sizeof(l2addr); l2addr.l2cap_family = AF_BLUETOOTH; memset(&l2addr.l2cap_bdaddr, 0, sizeof(l2addr.l2cap_bdaddr)); l2addr.l2cap_psm = 0; if (bind(s, (struct sockaddr *) &l2addr, sizeof(l2addr)) < 0) { close(s); return (-1); } memcpy(&l2addr.l2cap_bdaddr, bdaddr, sizeof(l2addr.l2cap_bdaddr)); l2addr.l2cap_psm = psm; if (connect(s, (struct sockaddr *) &l2addr, sizeof(l2addr)) < 0 && errno != EINPROGRESS) { close(s); return (-1); } return (s); } Index: head/usr.sbin/bluetooth/bthidd/server.c =================================================================== --- head/usr.sbin/bluetooth/bthidd/server.c (revision 162493) +++ head/usr.sbin/bluetooth/bthidd/server.c (revision 162494) @@ -1,348 +1,349 @@ /* * server.c */ /*- * Copyright (c) 2006 Maksim Yevmenkin * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $Id: server.c,v 1.9 2006/09/07 21:06:53 max Exp $ * $FreeBSD$ */ #include #include #include #include #include #include #include #include #include #include #include #include #include "bthid_config.h" #include "bthidd.h" #include "kbd.h" #undef max #define max(x, y) (((x) > (y))? (x) : (y)) static int32_t server_accept (bthid_server_p srv, int32_t fd); static int32_t server_process(bthid_server_p srv, int32_t fd); /* * Initialize server */ int32_t server_init(bthid_server_p srv) { struct sockaddr_l2cap l2addr; assert(srv != NULL); srv->ctrl = srv->intr = -1; FD_ZERO(&srv->rfdset); FD_ZERO(&srv->wfdset); LIST_INIT(&srv->sessions); /* Open /dev/consolectl */ srv->cons = open("/dev/consolectl", O_RDWR); if (srv->cons < 0) { syslog(LOG_ERR, "Could not open /dev/consolectl. %s (%d)", strerror(errno), errno); return (-1); } /* Create control socket */ srv->ctrl = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BLUETOOTH_PROTO_L2CAP); if (srv->ctrl < 0) { syslog(LOG_ERR, "Could not create control L2CAP socket. " \ "%s (%d)", strerror(errno), errno); close(srv->cons); return (-1); } l2addr.l2cap_len = sizeof(l2addr); l2addr.l2cap_family = AF_BLUETOOTH; memcpy(&l2addr.l2cap_bdaddr, &srv->bdaddr, sizeof(l2addr.l2cap_bdaddr)); l2addr.l2cap_psm = htole16(0x11); if (bind(srv->ctrl, (struct sockaddr *) &l2addr, sizeof(l2addr)) < 0) { syslog(LOG_ERR, "Could not bind control L2CAP socket. " \ "%s (%d)", strerror(errno), errno); close(srv->ctrl); close(srv->cons); return (-1); } if (listen(srv->ctrl, 10) < 0) { syslog(LOG_ERR, "Could not listen on control L2CAP socket. " \ "%s (%d)", strerror(errno), errno); close(srv->ctrl); close(srv->cons); return (-1); } /* Create intrrupt socket */ srv->intr = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BLUETOOTH_PROTO_L2CAP); if (srv->intr < 0) { syslog(LOG_ERR, "Could not create interrupt L2CAP socket. " \ "%s (%d)", strerror(errno), errno); close(srv->ctrl); close(srv->cons); return (-1); } l2addr.l2cap_psm = htole16(0x13); if (bind(srv->intr, (struct sockaddr *) &l2addr, sizeof(l2addr)) < 0) { syslog(LOG_ERR, "Could not bind interrupt L2CAP socket. " \ "%s (%d)", strerror(errno), errno); close(srv->intr); close(srv->ctrl); close(srv->cons); return (-1); } if (listen(srv->intr, 10) < 0) { syslog(LOG_ERR, "Could not listen on interrupt L2CAP socket. "\ "%s (%d)", strerror(errno), errno); close(srv->intr); close(srv->ctrl); close(srv->cons); return (-1); } FD_SET(srv->ctrl, &srv->rfdset); FD_SET(srv->intr, &srv->rfdset); srv->maxfd = max(srv->ctrl, srv->intr); return (0); } /* * Shutdown server */ void server_shutdown(bthid_server_p srv) { assert(srv != NULL); close(srv->cons); close(srv->ctrl); close(srv->intr); while (!LIST_EMPTY(&srv->sessions)) session_close(LIST_FIRST(&srv->sessions)); memset(srv, 0, sizeof(*srv)); } /* * Do one server iteration */ int32_t server_do(bthid_server_p srv) { struct timeval tv; fd_set rfdset, wfdset; int32_t n, fd; assert(srv != NULL); tv.tv_sec = 1; tv.tv_usec = 0; /* Copy cached version of the fd sets and call select */ memcpy(&rfdset, &srv->rfdset, sizeof(rfdset)); memcpy(&wfdset, &srv->wfdset, sizeof(wfdset)); n = select(srv->maxfd + 1, &rfdset, &wfdset, NULL, &tv); if (n < 0) { if (errno == EINTR) return (0); syslog(LOG_ERR, "Could not select(%d, %p, %p). %s (%d)", srv->maxfd + 1, &rfdset, &wfdset, strerror(errno), errno); return (-1); } /* Process descriptors (if any) */ for (fd = 0; fd < srv->maxfd + 1 && n > 0; fd ++) { if (FD_ISSET(fd, &rfdset)) { n --; if (fd == srv->ctrl || fd == srv->intr) server_accept(srv, fd); else server_process(srv, fd); } else if (FD_ISSET(fd, &wfdset)) { n --; client_connect(srv, fd); } } return (0); } /* * Accept new connection */ static int32_t server_accept(bthid_server_p srv, int32_t fd) { bthid_session_p s; hid_device_p d; struct sockaddr_l2cap l2addr; - int32_t len, new_fd; + int32_t new_fd; + socklen_t len; len = sizeof(l2addr); if ((new_fd = accept(fd, (struct sockaddr *) &l2addr, &len)) < 0) { syslog(LOG_ERR, "Could not accept %s connection. %s (%d)", (fd == srv->ctrl)? "control" : "interrupt", strerror(errno), errno); return (-1); } /* Is device configured? */ if ((d = get_hid_device(&l2addr.l2cap_bdaddr)) == NULL) { syslog(LOG_ERR, "Rejecting %s connection from %s. " \ "Device not configured", (fd == srv->ctrl)? "control" : "interrupt", bt_ntoa(&l2addr.l2cap_bdaddr, NULL)); close(new_fd); return (-1); } /* Check if we have session for the device */ if ((s = session_by_bdaddr(srv, &l2addr.l2cap_bdaddr)) == NULL) { d->new_device = 0; /* reset new device flag */ write_hids_file(); /* Create new inbound session */ if ((s = session_open(srv, d)) == NULL) { syslog(LOG_CRIT, "Could not open inbound session " "for %s", bt_ntoa(&l2addr.l2cap_bdaddr, NULL)); close(new_fd); return (-1); } } /* Update descriptors */ if (fd == srv->ctrl) { assert(s->ctrl == -1); s->ctrl = new_fd; s->state = (s->intr == -1)? W4INTR : OPEN; } else { assert(s->intr == -1); s->intr = new_fd; s->state = (s->ctrl == -1)? W4CTRL : OPEN; } FD_SET(new_fd, &srv->rfdset); if (new_fd > srv->maxfd) srv->maxfd = new_fd; syslog(LOG_NOTICE, "Accepted %s connection from %s", (fd == srv->ctrl)? "control" : "interrupt", bt_ntoa(&l2addr.l2cap_bdaddr, NULL)); /* Register session's vkbd descriptor (if needed) for read */ if (s->state == OPEN && d->keyboard) { assert(s->vkbd != -1); FD_SET(s->vkbd, &srv->rfdset); if (s->vkbd > srv->maxfd) srv->maxfd = s->vkbd; } return (0); } /* * Process data on the connection */ static int32_t server_process(bthid_server_p srv, int32_t fd) { bthid_session_p s = session_by_fd(srv, fd); int32_t len, to_read; int32_t (*cb)(bthid_session_p, uint8_t *, int32_t); union { uint8_t b[1024]; vkbd_status_t s; } data; if (s == NULL) return (0); /* can happen on device disconnect */ if (fd == s->ctrl) { cb = hid_control; to_read = sizeof(data.b); } else if (fd == s->intr) { cb = hid_interrupt; to_read = sizeof(data.b); } else { assert(fd == s->vkbd); cb = kbd_status_changed; to_read = sizeof(data.s); } do { len = read(fd, &data, to_read); } while (len < 0 && errno == EINTR); if (len < 0) { syslog(LOG_ERR, "Could not read data from %s (%s). %s (%d)", bt_ntoa(&s->bdaddr, NULL), (fd == s->ctrl)? "control" : "interrupt", strerror(errno), errno); session_close(s); return (0); } if (len == 0) { syslog(LOG_NOTICE, "Remote device %s has closed %s connection", bt_ntoa(&s->bdaddr, NULL), (fd == s->ctrl)? "control" : "interrupt"); session_close(s); return (0); } (*cb)(s, (uint8_t *) &data, len); return (0); } Index: head/usr.sbin/bluetooth/hcsecd/hcsecd.c =================================================================== --- head/usr.sbin/bluetooth/hcsecd/hcsecd.c (revision 162493) +++ head/usr.sbin/bluetooth/hcsecd/hcsecd.c (revision 162494) @@ -1,446 +1,447 @@ /* * hcsecd.c * * Copyright (c) 2001-2002 Maksim Yevmenkin * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $Id: hcsecd.c,v 1.6 2003/08/18 19:19:55 max Exp $ * $FreeBSD$ */ #include #include #include #include #include #include #include #include #include #include #include #include "hcsecd.h" static int done = 0; static int process_pin_code_request_event (int sock, struct sockaddr_hci *addr, bdaddr_p bdaddr); static int process_link_key_request_event (int sock, struct sockaddr_hci *addr, bdaddr_p bdaddr); static int send_pin_code_reply (int sock, struct sockaddr_hci *addr, bdaddr_p bdaddr, char const *pin); static int send_link_key_reply (int sock, struct sockaddr_hci *addr, bdaddr_p bdaddr, uint8_t *key); static int process_link_key_notification_event (int sock, struct sockaddr_hci *addr, ng_hci_link_key_notification_ep *ep); static void sighup (int s); static void sigint (int s); static void usage (void); /* Main */ int main(int argc, char *argv[]) { - int n, detach, sock, size; + int n, detach, sock; + socklen_t size; struct sigaction sa; struct sockaddr_hci addr; struct ng_btsocket_hci_raw_filter filter; char buffer[HCSECD_BUFFER_SIZE]; ng_hci_event_pkt_t *event = NULL; detach = 1; while ((n = getopt(argc, argv, "df:h")) != -1) { switch (n) { case 'd': detach = 0; break; case 'f': config_file = optarg; break; case 'h': default: usage(); /* NOT REACHED */ } } if (config_file == NULL) usage(); /* NOT REACHED */ if (getuid() != 0) errx(1, "** ERROR: You should run %s as privileged user!", HCSECD_IDENT); /* Set signal handlers */ memset(&sa, 0, sizeof(sa)); sa.sa_handler = sigint; sa.sa_flags = SA_NOCLDWAIT; if (sigaction(SIGINT, &sa, NULL) < 0) err(1, "Could not sigaction(SIGINT)"); if (sigaction(SIGTERM, &sa, NULL) < 0) err(1, "Could not sigaction(SIGINT)"); memset(&sa, 0, sizeof(sa)); sa.sa_handler = sighup; if (sigaction(SIGHUP, &sa, NULL) < 0) err(1, "Could not sigaction(SIGHUP)"); /* Open socket and set filter */ sock = socket(PF_BLUETOOTH, SOCK_RAW, BLUETOOTH_PROTO_HCI); if (sock < 0) err(1, "Could not create HCI socket"); memset(&filter, 0, sizeof(filter)); bit_set(filter.event_mask, NG_HCI_EVENT_PIN_CODE_REQ - 1); bit_set(filter.event_mask, NG_HCI_EVENT_LINK_KEY_REQ - 1); bit_set(filter.event_mask, NG_HCI_EVENT_LINK_KEY_NOTIFICATION - 1); if (setsockopt(sock, SOL_HCI_RAW, SO_HCI_RAW_FILTER, (void * const) &filter, sizeof(filter)) < 0) err(1, "Could not set HCI socket filter"); if (detach) if (daemon(0, 0) < 0) err(1, "Could not daemon()ize"); openlog(HCSECD_IDENT, LOG_NDELAY|LOG_PERROR|LOG_PID, LOG_DAEMON); read_config_file(); read_keys_file(); if (detach) { FILE *pid = NULL; if ((pid = fopen(HCSECD_PIDFILE, "w")) == NULL) { syslog(LOG_ERR, "Could not create PID file %s. %s (%d)", HCSECD_PIDFILE, strerror(errno), errno); exit(1); } fprintf(pid, "%d", getpid()); fclose(pid); } event = (ng_hci_event_pkt_t *) buffer; while (!done) { size = sizeof(addr); n = recvfrom(sock, buffer, sizeof(buffer), 0, (struct sockaddr *) &addr, &size); if (n < 0) { if (errno == EINTR) continue; syslog(LOG_ERR, "Could not receive from HCI socket. " \ "%s (%d)", strerror(errno), errno); exit(1); } if (event->type != NG_HCI_EVENT_PKT) { syslog(LOG_ERR, "Received unexpected HCI packet, " \ "type=%#x", event->type); continue; } switch (event->event) { case NG_HCI_EVENT_PIN_CODE_REQ: process_pin_code_request_event(sock, &addr, (bdaddr_p)(event + 1)); break; case NG_HCI_EVENT_LINK_KEY_REQ: process_link_key_request_event(sock, &addr, (bdaddr_p)(event + 1)); break; case NG_HCI_EVENT_LINK_KEY_NOTIFICATION: process_link_key_notification_event(sock, &addr, (ng_hci_link_key_notification_ep *)(event + 1)); break; default: syslog(LOG_ERR, "Received unexpected HCI event, " \ "event=%#x", event->event); break; } } if (detach) if (remove(HCSECD_PIDFILE) < 0) syslog(LOG_ERR, "Could not remove PID file %s. %s (%d)", HCSECD_PIDFILE, strerror(errno), errno); dump_keys_file(); clean_config(); closelog(); close(sock); return (0); } /* Process PIN_Code_Request event */ static int process_pin_code_request_event(int sock, struct sockaddr_hci *addr, bdaddr_p bdaddr) { link_key_p key = NULL; syslog(LOG_DEBUG, "Got PIN_Code_Request event from '%s', " \ "remote bdaddr %s", addr->hci_node, bt_ntoa(bdaddr, NULL)); if ((key = get_key(bdaddr, 0)) != NULL) { syslog(LOG_DEBUG, "Found matching entry, " \ "remote bdaddr %s, name '%s', PIN code %s", bt_ntoa(&key->bdaddr, NULL), (key->name != NULL)? key->name : "No name", (key->pin != NULL)? "exists" : "doesn't exist"); return (send_pin_code_reply(sock, addr, bdaddr, key->pin)); } syslog(LOG_DEBUG, "Could not PIN code for remote bdaddr %s", bt_ntoa(bdaddr, NULL)); return (send_pin_code_reply(sock, addr, bdaddr, NULL)); } /* Process Link_Key_Request event */ static int process_link_key_request_event(int sock, struct sockaddr_hci *addr, bdaddr_p bdaddr) { link_key_p key = NULL; syslog(LOG_DEBUG, "Got Link_Key_Request event from '%s', " \ "remote bdaddr %s", addr->hci_node, bt_ntoa(bdaddr, NULL)); if ((key = get_key(bdaddr, 0)) != NULL) { syslog(LOG_DEBUG, "Found matching entry, " \ "remote bdaddr %s, name '%s', link key %s", bt_ntoa(&key->bdaddr, NULL), (key->name != NULL)? key->name : "No name", (key->key != NULL)? "exists" : "doesn't exist"); return (send_link_key_reply(sock, addr, bdaddr, key->key)); } syslog(LOG_DEBUG, "Could not find link key for remote bdaddr %s", bt_ntoa(bdaddr, NULL)); return (send_link_key_reply(sock, addr, bdaddr, NULL)); } /* Send PIN_Code_[Negative]_Reply */ static int send_pin_code_reply(int sock, struct sockaddr_hci *addr, bdaddr_p bdaddr, char const *pin) { uint8_t buffer[HCSECD_BUFFER_SIZE]; ng_hci_cmd_pkt_t *cmd = NULL; memset(buffer, 0, sizeof(buffer)); cmd = (ng_hci_cmd_pkt_t *) buffer; cmd->type = NG_HCI_CMD_PKT; if (pin != NULL) { ng_hci_pin_code_rep_cp *cp = NULL; cmd->opcode = htole16(NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL, NG_HCI_OCF_PIN_CODE_REP)); cmd->length = sizeof(*cp); cp = (ng_hci_pin_code_rep_cp *)(cmd + 1); memcpy(&cp->bdaddr, bdaddr, sizeof(cp->bdaddr)); strncpy(cp->pin, pin, sizeof(cp->pin)); cp->pin_size = strlen(cp->pin); syslog(LOG_DEBUG, "Sending PIN_Code_Reply to '%s' " \ "for remote bdaddr %s", addr->hci_node, bt_ntoa(bdaddr, NULL)); } else { ng_hci_pin_code_neg_rep_cp *cp = NULL; cmd->opcode = htole16(NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL, NG_HCI_OCF_PIN_CODE_NEG_REP)); cmd->length = sizeof(*cp); cp = (ng_hci_pin_code_neg_rep_cp *)(cmd + 1); memcpy(&cp->bdaddr, bdaddr, sizeof(cp->bdaddr)); syslog(LOG_DEBUG, "Sending PIN_Code_Negative_Reply to '%s' " \ "for remote bdaddr %s", addr->hci_node, bt_ntoa(bdaddr, NULL)); } again: if (sendto(sock, buffer, sizeof(*cmd) + cmd->length, 0, (struct sockaddr *) addr, sizeof(*addr)) < 0) { if (errno == EINTR) goto again; syslog(LOG_ERR, "Could not send PIN code reply to '%s' " \ "for remote bdaddr %s. %s (%d)", addr->hci_node, bt_ntoa(bdaddr, NULL), strerror(errno), errno); return (-1); } return (0); } /* Send Link_Key_[Negative]_Reply */ static int send_link_key_reply(int sock, struct sockaddr_hci *addr, bdaddr_p bdaddr, uint8_t *key) { uint8_t buffer[HCSECD_BUFFER_SIZE]; ng_hci_cmd_pkt_t *cmd = NULL; memset(buffer, 0, sizeof(buffer)); cmd = (ng_hci_cmd_pkt_t *) buffer; cmd->type = NG_HCI_CMD_PKT; if (key != NULL) { ng_hci_link_key_rep_cp *cp = NULL; cmd->opcode = htole16(NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL, NG_HCI_OCF_LINK_KEY_REP)); cmd->length = sizeof(*cp); cp = (ng_hci_link_key_rep_cp *)(cmd + 1); memcpy(&cp->bdaddr, bdaddr, sizeof(cp->bdaddr)); memcpy(&cp->key, key, sizeof(cp->key)); syslog(LOG_DEBUG, "Sending Link_Key_Reply to '%s' " \ "for remote bdaddr %s", addr->hci_node, bt_ntoa(bdaddr, NULL)); } else { ng_hci_link_key_neg_rep_cp *cp = NULL; cmd->opcode = htole16(NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL, NG_HCI_OCF_LINK_KEY_NEG_REP)); cmd->length = sizeof(*cp); cp = (ng_hci_link_key_neg_rep_cp *)(cmd + 1); memcpy(&cp->bdaddr, bdaddr, sizeof(cp->bdaddr)); syslog(LOG_DEBUG, "Sending Link_Key_Negative_Reply to '%s' " \ "for remote bdaddr %s", addr->hci_node, bt_ntoa(bdaddr, NULL)); } again: if (sendto(sock, buffer, sizeof(*cmd) + cmd->length, 0, (struct sockaddr *) addr, sizeof(*addr)) < 0) { if (errno == EINTR) goto again; syslog(LOG_ERR, "Could not send link key reply to '%s' " \ "for remote bdaddr %s. %s (%d)", addr->hci_node, bt_ntoa(bdaddr, NULL), strerror(errno), errno); return (-1); } return (0); } /* Process Link_Key_Notification event */ static int process_link_key_notification_event(int sock, struct sockaddr_hci *addr, ng_hci_link_key_notification_ep *ep) { link_key_p key = NULL; syslog(LOG_DEBUG, "Got Link_Key_Notification event from '%s', " \ "remote bdaddr %s", addr->hci_node, bt_ntoa(&ep->bdaddr, NULL)); if ((key = get_key(&ep->bdaddr, 1)) == NULL) { syslog(LOG_ERR, "Could not find entry for remote bdaddr %s", bt_ntoa(&ep->bdaddr, NULL)); return (-1); } syslog(LOG_DEBUG, "Updating link key for the entry, " \ "remote bdaddr %s, name '%s', link key %s", bt_ntoa(&key->bdaddr, NULL), (key->name != NULL)? key->name : "No name", (key->key != NULL)? "exists" : "doesn't exist"); if (key->key == NULL) { key->key = (uint8_t *) malloc(NG_HCI_KEY_SIZE); if (key->key == NULL) { syslog(LOG_ERR, "Could not allocate link key"); exit(1); } } memcpy(key->key, &ep->key, NG_HCI_KEY_SIZE); return (0); } /* Signal handlers */ static void sighup(int s) { syslog(LOG_DEBUG, "Got SIGHUP (%d)", s); dump_keys_file(); read_config_file(); read_keys_file(); } static void sigint(int s) { syslog(LOG_DEBUG, "Got signal %d, total number of signals %d", s, ++ done); } /* Display usage and exit */ static void usage(void) { fprintf(stderr, "Usage: %s [-d] -f config_file [-h]\n" \ "Where:\n" \ "\t-d do not detach from terminal\n" \ "\t-f config_file use \n" \ "\t-h display this message\n", HCSECD_IDENT); exit(255); } Index: head/usr.sbin/bluetooth/rfcomm_pppd/rfcomm_pppd.c =================================================================== --- head/usr.sbin/bluetooth/rfcomm_pppd/rfcomm_pppd.c (revision 162493) +++ head/usr.sbin/bluetooth/rfcomm_pppd/rfcomm_pppd.c (revision 162494) @@ -1,449 +1,449 @@ /* * rfcomm_pppd.c * * Copyright (c) 2001-2003 Maksim Yevmenkin * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $Id: rfcomm_pppd.c,v 1.5 2003/09/07 18:32:11 max Exp $ * $FreeBSD$ */ #include #include #include #include #include #include #include #include #include #include #include #include #include #define RFCOMM_PPPD "rfcomm_pppd" int rfcomm_channel_lookup (bdaddr_t const *local, bdaddr_t const *remote, int service, int *channel, int *error); static void exec_ppp (int s, char *unit, char *label); static void sighandler (int s); static void usage (void); static int done; /* Main */ int main(int argc, char *argv[]) { struct sockaddr_rfcomm sock_addr; char *label = NULL, *unit = NULL, *ep = NULL; bdaddr_t addr; int s, channel, detach, server, service, regsp; pid_t pid; memcpy(&addr, NG_HCI_BDADDR_ANY, sizeof(addr)); channel = 0; detach = 1; server = 0; service = 0; regsp = 0; /* Parse command line arguments */ while ((s = getopt(argc, argv, "a:cC:dhl:sSu:")) != -1) { switch (s) { case 'a': /* BDADDR */ if (!bt_aton(optarg, &addr)) { struct hostent *he = NULL; if ((he = bt_gethostbyname(optarg)) == NULL) errx(1, "%s: %s", optarg, hstrerror(h_errno)); memcpy(&addr, he->h_addr, sizeof(addr)); } break; case 'c': /* client */ server = 0; break; case 'C': /* RFCOMM channel */ channel = strtoul(optarg, &ep, 10); if (*ep != '\0') { channel = 0; switch (tolower(optarg[0])) { case 'd': /* DialUp Networking */ service = SDP_SERVICE_CLASS_DIALUP_NETWORKING; break; case 'l': /* LAN Access Using PPP */ service = SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP; break; } } break; case 'd': /* do not detach */ detach = 0; break; case 'l': /* PPP label */ label = optarg; break; case 's': /* server */ server = 1; break; case 'S': /* Register SP service as well as LAN service */ regsp = 1; break; case 'u': /* PPP -unit option */ strtoul(optarg, &ep, 10); if (*ep != '\0') usage(); /* NOT REACHED */ unit = optarg; break; case 'h': default: usage(); /* NOT REACHED */ } } /* Check if we got everything we wanted */ if (label == NULL) errx(1, "Must specify PPP label"); if (!server) { if (memcmp(&addr, NG_HCI_BDADDR_ANY, sizeof(addr)) == 0) errx(1, "Must specify server BD_ADDR"); /* Check channel, if was not set then obtain it via SDP */ if (channel == 0 && service != 0) if (rfcomm_channel_lookup(NULL, &addr, service, &channel, &s) != 0) errc(1, s, "Could not obtain RFCOMM channel"); } if (channel <= 0 || channel > 30) errx(1, "Invalid RFCOMM channel number %d", channel); openlog(RFCOMM_PPPD, LOG_PID | LOG_PERROR | LOG_NDELAY, LOG_USER); if (detach) { pid = fork(); if (pid == (pid_t) -1) { syslog(LOG_ERR, "Could not fork(). %s (%d)", strerror(errno), errno); exit(1); } if (pid != 0) exit(0); if (daemon(0, 0) < 0) { syslog(LOG_ERR, "Could not daemon(0, 0). %s (%d)", strerror(errno), errno); exit(1); } } s = socket(PF_BLUETOOTH, SOCK_STREAM, BLUETOOTH_PROTO_RFCOMM); if (s < 0) { syslog(LOG_ERR, "Could not create socket. %s (%d)", strerror(errno), errno); exit(1); } if (server) { struct sigaction sa; void *ss = NULL; sdp_lan_profile_t lan; /* Install signal handler */ memset(&sa, 0, sizeof(sa)); sa.sa_handler = sighandler; if (sigaction(SIGTERM, &sa, NULL) < 0) { syslog(LOG_ERR, "Could not sigaction(SIGTERM). %s (%d)", strerror(errno), errno); exit(1); } if (sigaction(SIGHUP, &sa, NULL) < 0) { syslog(LOG_ERR, "Could not sigaction(SIGHUP). %s (%d)", strerror(errno), errno); exit(1); } if (sigaction(SIGINT, &sa, NULL) < 0) { syslog(LOG_ERR, "Could not sigaction(SIGINT). %s (%d)", strerror(errno), errno); exit(1); } sa.sa_handler = SIG_IGN; sa.sa_flags = SA_NOCLDWAIT; if (sigaction(SIGCHLD, &sa, NULL) < 0) { syslog(LOG_ERR, "Could not sigaction(SIGCHLD). %s (%d)", strerror(errno), errno); exit(1); } /* bind socket and listen for incoming connections */ sock_addr.rfcomm_len = sizeof(sock_addr); sock_addr.rfcomm_family = AF_BLUETOOTH; memcpy(&sock_addr.rfcomm_bdaddr, &addr, sizeof(sock_addr.rfcomm_bdaddr)); sock_addr.rfcomm_channel = channel; if (bind(s, (struct sockaddr *) &sock_addr, sizeof(sock_addr)) < 0) { syslog(LOG_ERR, "Could not bind socket. %s (%d)", strerror(errno), errno); exit(1); } if (listen(s, 10) < 0) { syslog(LOG_ERR, "Could not listen on socket. %s (%d)", strerror(errno), errno); exit(1); } ss = sdp_open_local(NULL); if (ss == NULL) { syslog(LOG_ERR, "Unable to create local SDP session"); exit(1); } if (sdp_error(ss) != 0) { syslog(LOG_ERR, "Unable to open local SDP session. " \ "%s (%d)", strerror(sdp_error(ss)), sdp_error(ss)); exit(1); } memset(&lan, 0, sizeof(lan)); lan.server_channel = channel; if (sdp_register_service(ss, SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP, &addr, (void *) &lan, sizeof(lan), NULL) != 0) { syslog(LOG_ERR, "Unable to register LAN service with " \ "local SDP daemon. %s (%d)", strerror(sdp_error(ss)), sdp_error(ss)); exit(1); } /* * Register SP (Serial Port) service on the same RFCOMM channel * if requested. It appears that some cell phones are using so * called "callback mechanism". In this scenario user is trying * to connect his cell phone to the Internet, and, user's host * computer is acting as the gateway server. It seems that it * is not possible to tell the phone to just connect and start * using the LAN service. Instead the user's host computer must * "jump start" the phone by connecting to the phone's SP * service. What happens next is the phone kills the existing * connection and opens another connection back to the user's * host computer. The phone really wants to use LAN service, * but for whatever reason it looks for SP service on the * user's host computer. This brain damaged behavior was * reported for Nokia 6600 and Sony/Ericsson P900. Both phones * are Symbian-based phones. Perhaps this is a Symbian problem? */ if (regsp) { sdp_sp_profile_t sp; memset(&sp, 0, sizeof(sp)); sp.server_channel = channel; if (sdp_register_service(ss, SDP_SERVICE_CLASS_SERIAL_PORT, &addr, (void *) &sp, sizeof(sp), NULL) != 0) { syslog(LOG_ERR, "Unable to register SP " \ "service with local SDP daemon. " \ "%s (%d)", strerror(sdp_error(ss)), sdp_error(ss)); exit(1); } } for (done = 0; !done; ) { - int len = sizeof(sock_addr); - int s1 = accept(s, (struct sockaddr *) &sock_addr, &len); + socklen_t len = sizeof(sock_addr); + int s1 = accept(s, (struct sockaddr *) &sock_addr, &len); if (s1 < 0) { syslog(LOG_ERR, "Could not accept connection " \ "on socket. %s (%d)", strerror(errno), errno); exit(1); } pid = fork(); if (pid == (pid_t) -1) { syslog(LOG_ERR, "Could not fork(). %s (%d)", strerror(errno), errno); exit(1); } if (pid == 0) { sdp_close(ss); close(s); /* Reset signal handler */ memset(&sa, 0, sizeof(sa)); sa.sa_handler = SIG_DFL; sigaction(SIGTERM, &sa, NULL); sigaction(SIGHUP, &sa, NULL); sigaction(SIGINT, &sa, NULL); sigaction(SIGCHLD, &sa, NULL); /* Become daemon */ daemon(0, 0); /* * XXX Make sure user does not shoot himself * in the foot. Do not pass unit option to the * PPP when operating in the server mode. */ exec_ppp(s1, NULL, label); } else close(s1); } } else { sock_addr.rfcomm_len = sizeof(sock_addr); sock_addr.rfcomm_family = AF_BLUETOOTH; memcpy(&sock_addr.rfcomm_bdaddr, NG_HCI_BDADDR_ANY, sizeof(sock_addr.rfcomm_bdaddr)); sock_addr.rfcomm_channel = 0; if (bind(s, (struct sockaddr *) &sock_addr, sizeof(sock_addr)) < 0) { syslog(LOG_ERR, "Could not bind socket. %s (%d)", strerror(errno), errno); exit(1); } memcpy(&sock_addr.rfcomm_bdaddr, &addr, sizeof(sock_addr.rfcomm_bdaddr)); sock_addr.rfcomm_channel = channel; if (connect(s, (struct sockaddr *) &sock_addr, sizeof(sock_addr)) < 0) { syslog(LOG_ERR, "Could not connect socket. %s (%d)", strerror(errno), errno); exit(1); } exec_ppp(s, unit, label); } exit(0); } /* main */ /* * Redirects stdin/stdout to s, stderr to /dev/null and exec * 'ppp -direct -quiet [-unit N] label'. Never returns. */ static void exec_ppp(int s, char *unit, char *label) { char ppp[] = "/usr/sbin/ppp"; char *ppp_args[] = { ppp, "-direct", "-quiet", NULL, NULL, NULL, NULL }; close(0); if (dup(s) < 0) { syslog(LOG_ERR, "Could not dup(0). %s (%d)", strerror(errno), errno); exit(1); } close(1); if (dup(s) < 0) { syslog(LOG_ERR, "Could not dup(1). %s (%d)", strerror(errno), errno); exit(1); } close(2); open("/dev/null", O_RDWR); if (unit != NULL) { ppp_args[3] = "-unit"; ppp_args[4] = unit; ppp_args[5] = label; } else ppp_args[3] = label; if (execv(ppp, ppp_args) < 0) { syslog(LOG_ERR, "Could not exec(%s -direct -quiet%s%s %s). " \ "%s (%d)", ppp, (unit != NULL)? " -unit " : "", (unit != NULL)? unit : "", label, strerror(errno), errno); exit(1); } } /* run_ppp */ /* Signal handler */ static void sighandler(int s) { done = 1; } /* sighandler */ /* Display usage and exit */ static void usage(void) { fprintf(stdout, "Usage: %s options\n" \ "Where options are:\n" \ "\t-a address Address to listen on or connect to (required for client)\n" \ "\t-c Act as a clinet (default)\n" \ "\t-C channel RFCOMM channel to listen on or connect to (required)\n" \ "\t-d Run in foreground\n" \ "\t-l label Use PPP label (required)\n" \ "\t-s Act as a server\n" \ "\t-S Register Serial Port service (server mode only)\n" \ "\t-u N Tell PPP to operate on /dev/tunN (client mode only)\n" \ "\t-h Display this message\n", RFCOMM_PPPD); exit(255); } /* usage */ Index: head/usr.sbin/bluetooth/sdpd/server.c =================================================================== --- head/usr.sbin/bluetooth/sdpd/server.c (revision 162493) +++ head/usr.sbin/bluetooth/sdpd/server.c (revision 162494) @@ -1,586 +1,588 @@ /* * server.c * * Copyright (c) 2004 Maksim Yevmenkin * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $Id: server.c,v 1.6 2004/01/13 01:54:39 max Exp $ * $FreeBSD$ */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "log.h" #include "profile.h" #include "provider.h" #include "server.h" static void server_accept_client (server_p srv, int32_t fd); static int32_t server_process_request (server_p srv, int32_t fd); static int32_t server_send_error_response (server_p srv, int32_t fd, uint16_t error); static void server_close_fd (server_p srv, int32_t fd); /* * Initialize server */ int32_t server_init(server_p srv, char const *control) { struct sockaddr_un un; struct sockaddr_l2cap l2; - int32_t unsock, l2sock, size; + int32_t unsock, l2sock; + socklen_t size; uint16_t imtu; assert(srv != NULL); assert(control != NULL); memset(srv, 0, sizeof(srv)); /* Open control socket */ if (unlink(control) < 0 && errno != ENOENT) { log_crit("Could not unlink(%s). %s (%d)", control, strerror(errno), errno); return (-1); } unsock = socket(PF_LOCAL, SOCK_STREAM, 0); if (unsock < 0) { log_crit("Could not create control socket. %s (%d)", strerror(errno), errno); return (-1); } memset(&un, 0, sizeof(un)); un.sun_len = sizeof(un); un.sun_family = AF_LOCAL; strlcpy(un.sun_path, control, sizeof(un.sun_path)); if (bind(unsock, (struct sockaddr *) &un, sizeof(un)) < 0) { log_crit("Could not bind control socket. %s (%d)", strerror(errno), errno); close(unsock); return (-1); } if (chmod(control, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) < 0) { log_crit("Could not change permissions on control socket. " \ "%s (%d)", strerror(errno), errno); close(unsock); return (-1); } if (listen(unsock, 10) < 0) { log_crit("Could not listen on control socket. %s (%d)", strerror(errno), errno); close(unsock); return (-1); } /* Open L2CAP socket */ l2sock = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BLUETOOTH_PROTO_L2CAP); if (l2sock < 0) { log_crit("Could not create L2CAP socket. %s (%d)", strerror(errno), errno); close(unsock); return (-1); } size = sizeof(imtu); if (getsockopt(l2sock, SOL_L2CAP, SO_L2CAP_IMTU, &imtu, &size) < 0) { log_crit("Could not get L2CAP IMTU. %s (%d)", strerror(errno), errno); close(unsock); close(l2sock); return (-1); } memset(&l2, 0, sizeof(l2)); l2.l2cap_len = sizeof(l2); l2.l2cap_family = AF_BLUETOOTH; memcpy(&l2.l2cap_bdaddr, NG_HCI_BDADDR_ANY, sizeof(l2.l2cap_bdaddr)); l2.l2cap_psm = htole16(NG_L2CAP_PSM_SDP); if (bind(l2sock, (struct sockaddr *) &l2, sizeof(l2)) < 0) { log_crit("Could not bind L2CAP socket. %s (%d)", strerror(errno), errno); close(unsock); close(l2sock); return (-1); } if (listen(l2sock, 10) < 0) { log_crit("Could not listen on L2CAP socket. %s (%d)", strerror(errno), errno); close(unsock); close(l2sock); return (-1); } /* Allocate incoming buffer */ srv->imtu = (imtu > SDP_LOCAL_MTU)? imtu : SDP_LOCAL_MTU; srv->req = (uint8_t *) calloc(srv->imtu, sizeof(srv->req[0])); if (srv->req == NULL) { log_crit("Could not allocate request buffer"); close(unsock); close(l2sock); return (-1); } /* Allocate memory for descriptor index */ srv->fdidx = (fd_idx_p) calloc(FD_SETSIZE, sizeof(srv->fdidx[0])); if (srv->fdidx == NULL) { log_crit("Could not allocate fd index"); free(srv->req); close(unsock); close(l2sock); return (-1); } /* Register Service Discovery profile (attach it to control socket) */ if (provider_register_sd(unsock) < 0) { log_crit("Could not register Service Discovery profile"); free(srv->fdidx); free(srv->req); close(unsock); close(l2sock); return (-1); } /* * If we got here then everything is fine. Add both control sockets * to the index. */ FD_ZERO(&srv->fdset); srv->maxfd = (unsock > l2sock)? unsock : l2sock; FD_SET(unsock, &srv->fdset); srv->fdidx[unsock].valid = 1; srv->fdidx[unsock].server = 1; srv->fdidx[unsock].control = 1; srv->fdidx[unsock].priv = 0; srv->fdidx[unsock].rsp_cs = 0; srv->fdidx[unsock].rsp_size = 0; srv->fdidx[unsock].rsp_limit = 0; srv->fdidx[unsock].omtu = SDP_LOCAL_MTU; srv->fdidx[unsock].rsp = NULL; FD_SET(l2sock, &srv->fdset); srv->fdidx[l2sock].valid = 1; srv->fdidx[l2sock].server = 1; srv->fdidx[l2sock].control = 0; srv->fdidx[l2sock].priv = 0; srv->fdidx[l2sock].rsp_cs = 0; srv->fdidx[l2sock].rsp_size = 0; srv->fdidx[l2sock].rsp_limit = 0; srv->fdidx[l2sock].omtu = 0; /* unknown */ srv->fdidx[l2sock].rsp = NULL; return (0); } /* * Shutdown server */ void server_shutdown(server_p srv) { int fd; assert(srv != NULL); for (fd = 0; fd < srv->maxfd + 1; fd ++) if (srv->fdidx[fd].valid) server_close_fd(srv, fd); free(srv->req); free(srv->fdidx); memset(srv, 0, sizeof(*srv)); } /* * Do one server iteration */ int32_t server_do(server_p srv) { fd_set fdset; int32_t n, fd; assert(srv != NULL); /* Copy cached version of the fd set and call select */ memcpy(&fdset, &srv->fdset, sizeof(fdset)); n = select(srv->maxfd + 1, &fdset, NULL, NULL, NULL); if (n < 0) { if (errno == EINTR) return (0); log_err("Could not select(%d, %p). %s (%d)", srv->maxfd + 1, &fdset, strerror(errno), errno); return (-1); } /* Process descriptors */ for (fd = 0; fd < srv->maxfd + 1 && n > 0; fd ++) { if (!FD_ISSET(fd, &fdset)) continue; assert(srv->fdidx[fd].valid); n --; if (srv->fdidx[fd].server) server_accept_client(srv, fd); else if (server_process_request(srv, fd) != 0) server_close_fd(srv, fd); } return (0); } /* * Accept new client connection and register it with index */ static void server_accept_client(server_p srv, int32_t fd) { uint8_t *rsp = NULL; - int32_t cfd, size, priv; + int32_t cfd, priv; uint16_t omtu; + socklen_t size; do { cfd = accept(fd, NULL, NULL); } while (cfd < 0 && errno == EINTR); if (cfd < 0) { log_err("Could not accept connection on %s socket. %s (%d)", srv->fdidx[fd].control? "control" : "L2CAP", strerror(errno), errno); return; } assert(!FD_ISSET(cfd, &srv->fdset)); assert(!srv->fdidx[cfd].valid); priv = 0; if (!srv->fdidx[fd].control) { /* Get local BD_ADDR */ size = sizeof(srv->req_sa); if (getsockname(cfd,(struct sockaddr*)&srv->req_sa,&size) < 0) { log_err("Could not get local BD_ADDR. %s (%d)", strerror(errno), errno); close(cfd); return; } /* Get outgoing MTU */ size = sizeof(omtu); if (getsockopt(cfd,SOL_L2CAP,SO_L2CAP_OMTU,&omtu,&size) < 0) { log_err("Could not get L2CAP OMTU. %s (%d)", strerror(errno), errno); close(cfd); return; } /* * The maximum size of the L2CAP packet is 65536 bytes. * The minimum L2CAP MTU is 43 bytes. That means we need * 65536 / 43 = ~1524 chunks to transfer maximum packet * size with minimum MTU. The "rsp_cs" field in fd_idx_t * is 11 bit wide that gives us upto 2048 chunks. */ if (omtu < NG_L2CAP_MTU_MINIMUM) { log_err("L2CAP OMTU is too small (%d bytes)", omtu); close(cfd); return; } } else { struct xucred cr; struct passwd *pw; /* Get peer's credentials */ memset(&cr, 0, sizeof(cr)); size = sizeof(cr); if (getsockopt(cfd, 0, LOCAL_PEERCRED, &cr, &size) < 0) { log_err("Could not get peer's credentials. %s (%d)", strerror(errno), errno); close(cfd); return; } /* Check credentials */ pw = getpwuid(cr.cr_uid); if (pw != NULL) priv = (strcmp(pw->pw_name, "root") == 0); else log_warning("Could not verify credentials for uid %d", cr.cr_uid); memcpy(&srv->req_sa.l2cap_bdaddr, NG_HCI_BDADDR_ANY, sizeof(srv->req_sa.l2cap_bdaddr)); omtu = srv->fdidx[fd].omtu; } /* * Allocate buffer. This is an overkill, but we can not know how * big our reply is going to be. */ rsp = (uint8_t *) calloc(NG_L2CAP_MTU_MAXIMUM, sizeof(rsp[0])); if (rsp == NULL) { log_crit("Could not allocate response buffer"); close(cfd); return; } /* Add client descriptor to the index */ FD_SET(cfd, &srv->fdset); if (srv->maxfd < cfd) srv->maxfd = cfd; srv->fdidx[cfd].valid = 1; srv->fdidx[cfd].server = 0; srv->fdidx[cfd].control = srv->fdidx[fd].control; srv->fdidx[cfd].priv = priv; srv->fdidx[cfd].rsp_cs = 0; srv->fdidx[cfd].rsp_size = 0; srv->fdidx[cfd].rsp_limit = 0; srv->fdidx[cfd].omtu = omtu; srv->fdidx[cfd].rsp = rsp; } /* * Process request from the client */ static int32_t server_process_request(server_p srv, int32_t fd) { sdp_pdu_p pdu = (sdp_pdu_p) srv->req; int32_t len, error; assert(srv->imtu > 0); assert(srv->req != NULL); assert(FD_ISSET(fd, &srv->fdset)); assert(srv->fdidx[fd].valid); assert(!srv->fdidx[fd].server); assert(srv->fdidx[fd].rsp != NULL); assert(srv->fdidx[fd].omtu >= NG_L2CAP_MTU_MINIMUM); do { len = read(fd, srv->req, srv->imtu); } while (len < 0 && errno == EINTR); if (len < 0) { log_err("Could not receive SDP request from %s socket. %s (%d)", srv->fdidx[fd].control? "control" : "L2CAP", strerror(errno), errno); return (-1); } if (len == 0) { log_info("Client on %s socket has disconnected", srv->fdidx[fd].control? "control" : "L2CAP"); return (-1); } if (sizeof(*pdu) + (pdu->len = ntohs(pdu->len)) == len) { switch (pdu->pid) { case SDP_PDU_SERVICE_SEARCH_REQUEST: error = server_prepare_service_search_response(srv, fd); break; case SDP_PDU_SERVICE_ATTRIBUTE_REQUEST: error = server_prepare_service_attribute_response(srv, fd); break; case SDP_PDU_SERVICE_SEARCH_ATTRIBUTE_REQUEST: error = server_prepare_service_search_attribute_response(srv, fd); break; case SDP_PDU_SERVICE_REGISTER_REQUEST: error = server_prepare_service_register_response(srv, fd); break; case SDP_PDU_SERVICE_UNREGISTER_REQUEST: error = server_prepare_service_unregister_response(srv, fd); break; case SDP_PDU_SERVICE_CHANGE_REQUEST: error = server_prepare_service_change_response(srv, fd); break; default: error = SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX; break; } } else error = SDP_ERROR_CODE_INVALID_PDU_SIZE; if (error == 0) { switch (pdu->pid) { case SDP_PDU_SERVICE_SEARCH_REQUEST: error = server_send_service_search_response(srv, fd); break; case SDP_PDU_SERVICE_ATTRIBUTE_REQUEST: error = server_send_service_attribute_response(srv, fd); break; case SDP_PDU_SERVICE_SEARCH_ATTRIBUTE_REQUEST: error = server_send_service_search_attribute_response(srv, fd); break; case SDP_PDU_SERVICE_REGISTER_REQUEST: error = server_send_service_register_response(srv, fd); break; case SDP_PDU_SERVICE_UNREGISTER_REQUEST: error = server_send_service_unregister_response(srv, fd); break; case SDP_PDU_SERVICE_CHANGE_REQUEST: error = server_send_service_change_response(srv, fd); break; default: error = SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX; break; } if (error != 0) log_err("Could not send SDP response to %s socket, " \ "pdu->pid=%d, pdu->tid=%d, error=%d", srv->fdidx[fd].control? "control" : "L2CAP", pdu->pid, ntohs(pdu->tid), error); } else { log_err("Could not process SDP request from %s socket, " \ "pdu->pid=%d, pdu->tid=%d, pdu->len=%d, len=%d, " \ "error=%d", srv->fdidx[fd].control? "control" : "L2CAP", pdu->pid, ntohs(pdu->tid), pdu->len, len, error); error = server_send_error_response(srv, fd, error); if (error != 0) log_err("Could not send SDP error response to %s " \ "socket, pdu->pid=%d, pdu->tid=%d, error=%d", srv->fdidx[fd].control? "control" : "L2CAP", pdu->pid, ntohs(pdu->tid), error); } /* On error forget response (if any) */ if (error != 0) { srv->fdidx[fd].rsp_cs = 0; srv->fdidx[fd].rsp_size = 0; srv->fdidx[fd].rsp_limit = 0; } return (error); } /* * Send SDP_Error_Response PDU */ static int32_t server_send_error_response(server_p srv, int32_t fd, uint16_t error) { int32_t size; struct { sdp_pdu_t pdu; uint16_t error; } __attribute__ ((packed)) rsp; /* Prepare and send SDP error response */ rsp.pdu.pid = SDP_PDU_ERROR_RESPONSE; rsp.pdu.tid = ((sdp_pdu_p)(srv->req))->tid; rsp.pdu.len = htons(sizeof(rsp.error)); rsp.error = htons(error); do { size = write(fd, &rsp, sizeof(rsp)); } while (size < 0 && errno == EINTR); return ((size < 0)? errno : 0); } /* * Close descriptor and remove it from index */ static void server_close_fd(server_p srv, int32_t fd) { provider_p provider = NULL, provider_next = NULL; assert(FD_ISSET(fd, &srv->fdset)); assert(srv->fdidx[fd].valid); close(fd); FD_CLR(fd, &srv->fdset); if (fd == srv->maxfd) srv->maxfd --; if (srv->fdidx[fd].rsp != NULL) free(srv->fdidx[fd].rsp); memset(&srv->fdidx[fd], 0, sizeof(srv->fdidx[fd])); for (provider = provider_get_first(); provider != NULL; provider = provider_next) { provider_next = provider_get_next(provider); if (provider->fd == fd) provider_unregister(provider); } }