3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <sys/param.h>
33 #include <sys/ioctl.h>
34 #include <sys/socket.h>
37 #include <linux/sockios.h>
41 #include "lib/bluetooth.h"
42 #include "lib/l2cap.h"
47 #include "src/shared/util.h"
48 #include "btio/btio.h"
52 #define CON_SETUP_RETRIES 3
53 #define CON_SETUP_TO 9
57 #ifdef __TIZEN_PATCH__
58 /* Compatibility with old ioctls */
59 #define OLD_BNEPCONADD 1
60 #define OLD_BNEPCONDEL 2
61 #define OLD_BNEPGETCONLIST 3
62 #define OLD_BNEPGETCONINFO 4
64 static unsigned long bnepconnadd;
65 static unsigned long bnepconndel;
66 static unsigned long bnepgetconnlist;
67 static unsigned long bnepgetconninfo;
73 } __attribute__ ((packed));
84 bnep_connect_cb conn_cb;
86 bnep_disconnect_cb disconn_cb;
92 ctl = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_BNEP);
96 if (err == -EPROTONOSUPPORT)
97 warn("kernel lacks bnep-protocol support");
99 error("bnep: Failed to open control socket: %s (%d)",
100 strerror(-err), -err);
104 #ifdef __TIZEN_PATCH__
105 /* Temporary ioctl compatibility hack */
107 struct bnep_connlist_req req;
108 struct bnep_conninfo ci[1];
113 if (!ioctl(ctl, BNEPGETCONNLIST, &req)) {
115 bnepconnadd = BNEPCONNADD;
116 bnepconndel = BNEPCONNDEL;
117 bnepgetconnlist = BNEPGETCONNLIST;
118 bnepgetconninfo = BNEPGETCONNINFO;
121 bnepconnadd = OLD_BNEPCONADD;
122 bnepconndel = OLD_BNEPCONDEL;
123 bnepgetconnlist = OLD_BNEPGETCONLIST;
124 bnepgetconninfo = OLD_BNEPGETCONINFO;
131 int bnep_cleanup(void)
137 static int bnep_conndel(const bdaddr_t *dst)
139 struct bnep_conndel_req req;
141 memset(&req, 0, sizeof(req));
142 baswap((bdaddr_t *)&req.dst, dst);
144 if (ioctl(ctl, BNEPCONNDEL, &req) < 0) {
146 error("bnep: Failed to kill connection: %s (%d)",
147 strerror(-err), -err);
153 static int bnep_connadd(int sk, uint16_t role, char *dev)
155 struct bnep_connadd_req req;
157 memset(&req, 0, sizeof(req));
158 strncpy(req.device, dev, 16);
159 req.device[15] = '\0';
163 req.flags = (1 << BNEP_SETUP_RESPONSE);
164 if (ioctl(ctl, BNEPCONNADD, &req) < 0) {
166 error("bnep: Failed to add device %s: %s(%d)",
167 dev, strerror(-err), -err);
171 strncpy(dev, req.device, 16);
175 static uint32_t bnep_getsuppfeat(void)
179 if (ioctl(ctl, BNEPGETSUPPFEAT, &feat) < 0)
182 DBG("supported features: 0x%x", feat);
187 static int bnep_if_up(const char *devname)
192 sk = socket(AF_INET, SOCK_DGRAM, 0);
194 memset(&ifr, 0, sizeof(ifr));
195 strncpy(ifr.ifr_name, devname, IF_NAMESIZE - 1);
197 ifr.ifr_flags |= IFF_UP;
198 ifr.ifr_flags |= IFF_MULTICAST;
200 if (ioctl(sk, SIOCSIFFLAGS, (void *) &ifr) < 0) {
202 error("bnep: Could not bring up %s: %s(%d)",
203 devname, strerror(-err), -err);
211 static int bnep_if_down(const char *devname)
216 sk = socket(AF_INET, SOCK_DGRAM, 0);
218 #ifdef __TIZEN_PATCH__
223 memset(&ifr, 0, sizeof(ifr));
224 strncpy(ifr.ifr_name, devname, IF_NAMESIZE - 1);
226 ifr.ifr_flags &= ~IFF_UP;
228 /* Bring down the interface */
229 if (ioctl(sk, SIOCSIFFLAGS, (void *) &ifr) < 0) {
231 error("bnep: Could not bring down %s: %s(%d)",
232 devname, strerror(-err), -err);
240 static gboolean bnep_watchdog_cb(GIOChannel *chan, GIOCondition cond,
243 struct bnep *session = data;
245 if (session->disconn_cb)
246 session->disconn_cb(session->disconn_data);
251 static gboolean bnep_setup_cb(GIOChannel *chan, GIOCondition cond,
254 struct bnep *session = data;
255 struct bnep_control_rsp *rsp;
256 struct timeval timeo;
261 if (cond & G_IO_NVAL)
264 if (session->setup_to > 0) {
265 g_source_remove(session->setup_to);
266 session->setup_to = 0;
269 if (cond & (G_IO_HUP | G_IO_ERR)) {
270 error("bnep: Hangup or error on l2cap server socket");
274 sk = g_io_channel_unix_get_fd(chan);
275 memset(pkt, 0, BNEP_MTU);
276 r = read(sk, pkt, sizeof(pkt) - 1);
278 error("bnep: IO Channel read error");
283 error("bnep: No packet received on l2cap socket");
289 if ((size_t) r < sizeof(*rsp)) {
290 error("bnep: Packet received is not bnep type");
295 if (rsp->type != BNEP_CONTROL) {
296 error("bnep: Packet received is not bnep type");
300 if (rsp->ctrl != BNEP_SETUP_CONN_RSP)
303 r = ntohs(rsp->resp);
304 if (r != BNEP_SUCCESS) {
305 error("bnep: failed");
309 memset(&timeo, 0, sizeof(timeo));
311 setsockopt(sk, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo));
313 sk = g_io_channel_unix_get_fd(session->io);
314 if (bnep_connadd(sk, session->src, session->iface) < 0)
317 if (bnep_if_up(session->iface) < 0) {
318 bnep_conndel(&session->dst_addr);
322 session->watch = g_io_add_watch(session->io,
323 G_IO_ERR | G_IO_HUP | G_IO_NVAL,
324 (GIOFunc) bnep_watchdog_cb, session);
325 g_io_channel_unref(session->io);
328 session->conn_cb(session->iface, 0, session->conn_data);
333 session->conn_cb(NULL, -EIO, session->conn_data);
338 static int bnep_setup_conn_req(struct bnep *session)
340 struct bnep_setup_conn_req *req;
341 struct __service_16 *s;
342 unsigned char pkt[BNEP_MTU];
347 req->type = BNEP_CONTROL;
348 req->ctrl = BNEP_SETUP_CONN_REQ;
349 req->uuid_size = 2; /* 16bit UUID */
350 s = (void *) req->service;
351 s->src = htons(session->src);
352 s->dst = htons(session->dst);
354 fd = g_io_channel_unix_get_fd(session->io);
355 if (write(fd, pkt, sizeof(*req) + sizeof(*s)) < 0) {
356 error("bnep: connection req send failed: %s", strerror(errno));
365 static gboolean bnep_conn_req_to(gpointer user_data)
367 struct bnep *session = user_data;
369 if (session->attempts == CON_SETUP_RETRIES) {
370 error("bnep: Too many bnep connection attempts");
372 error("bnep: connection setup TO, retrying...");
373 if (bnep_setup_conn_req(session) == 0)
377 session->conn_cb(NULL, -ETIMEDOUT, session->conn_data);
382 struct bnep *bnep_new(int sk, uint16_t local_role, uint16_t remote_role,
385 struct bnep *session;
392 session = g_new0(struct bnep, 1);
393 session->io = g_io_channel_unix_new(dup_fd);
394 session->src = local_role;
395 session->dst = remote_role;
396 strncpy(session->iface, iface, 16);
397 session->iface[15] = '\0';
399 g_io_channel_set_close_on_unref(session->io, TRUE);
400 session->watch = g_io_add_watch(session->io,
401 G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
402 (GIOFunc) bnep_setup_cb, session);
407 void bnep_free(struct bnep *session)
413 g_io_channel_shutdown(session->io, FALSE, NULL);
414 g_io_channel_unref(session->io);
418 if (session->watch > 0) {
419 g_source_remove(session->watch);
426 int bnep_connect(struct bnep *session, bnep_connect_cb conn_cb,
427 bnep_disconnect_cb disconn_cb,
428 void *conn_data, void *disconn_data)
433 if (!session || !conn_cb || !disconn_cb)
436 session->attempts = 0;
437 session->conn_cb = conn_cb;
438 session->disconn_cb = disconn_cb;
439 session->conn_data = conn_data;
440 session->disconn_data = disconn_data;
442 bt_io_get(session->io, &gerr, BT_IO_OPT_DEST_BDADDR, &session->dst_addr,
445 error("bnep: connect failed: %s", gerr->message);
450 err = bnep_setup_conn_req(session);
454 session->setup_to = g_timeout_add_seconds(CON_SETUP_TO,
455 bnep_conn_req_to, session);
459 void bnep_disconnect(struct bnep *session)
464 #ifndef __TIZEN_PATCH__
465 if (session->watch > 0) {
466 g_source_remove(session->watch);
471 g_io_channel_unref(session->io);
475 bnep_if_down(session->iface);
476 bnep_conndel(&session->dst_addr);
479 #ifndef __TIZEN_PATCH__
480 static int bnep_add_to_bridge(const char *devname, const char *bridge)
486 if (!devname || !bridge)
489 ifindex = if_nametoindex(devname);
491 sk = socket(AF_INET, SOCK_STREAM, 0);
494 #ifdef __TIZEN_PATCH__
495 err = ioctl(sk, SIOCBRADDBR, bridge);
498 info("bridge create err: %d", err);
503 memset(&ifr, 0, sizeof(ifr));
504 strncpy(ifr.ifr_name, bridge, IFNAMSIZ - 1);
505 ifr.ifr_ifindex = ifindex;
507 if (ioctl(sk, SIOCBRADDIF, &ifr) < 0) {
509 error("bnep: Can't add %s to the bridge %s: %s(%d)",
510 devname, bridge, strerror(-err), -err);
512 info("bnep: bridge %s: interface %s added", bridge, devname);
521 static int bnep_del_from_bridge(const char *devname, const char *bridge)
527 if (!devname || !bridge)
530 ifindex = if_nametoindex(devname);
532 sk = socket(AF_INET, SOCK_STREAM, 0);
536 memset(&ifr, 0, sizeof(ifr));
537 strncpy(ifr.ifr_name, bridge, IFNAMSIZ - 1);
538 ifr.ifr_ifindex = ifindex;
540 if (ioctl(sk, SIOCBRDELIF, &ifr) < 0) {
542 error("bnep: Can't delete %s from the bridge %s: %s(%d)",
543 devname, bridge, strerror(-err), -err);
545 info("bnep: bridge %s: interface %s removed", bridge, devname);
553 static ssize_t bnep_send_ctrl_rsp(int sk, uint8_t ctrl, uint16_t resp)
558 case BNEP_CMD_NOT_UNDERSTOOD: {
559 struct bnep_ctrl_cmd_not_understood_cmd rsp;
561 rsp.type = BNEP_CONTROL;
563 rsp.unkn_ctrl = (uint8_t) resp;
565 sent = send(sk, &rsp, sizeof(rsp), 0);
568 case BNEP_FILTER_MULT_ADDR_RSP:
569 case BNEP_FILTER_NET_TYPE_RSP:
570 case BNEP_SETUP_CONN_RSP: {
571 struct bnep_control_rsp rsp;
573 rsp.type = BNEP_CONTROL;
575 rsp.resp = htons(resp);
577 sent = send(sk, &rsp, sizeof(rsp), 0);
581 error("bnep: wrong response type");
589 static uint16_t bnep_setup_decode(int sk, struct bnep_setup_conn_req *req,
592 const uint8_t bt_base[] = { 0x00, 0x00, 0x10, 0x00, 0x80, 0x00,
593 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB };
595 uint8_t *dest, *source;
598 if (((req->type != BNEP_CONTROL) &&
599 (req->type != (BNEP_CONTROL | BNEP_EXT_HEADER))) ||
600 req->ctrl != BNEP_SETUP_CONN_REQ)
601 return BNEP_CONN_NOT_ALLOWED;
604 source = req->service + req->uuid_size;
606 switch (req->uuid_size) {
608 *dst = get_be16(dest);
609 src = get_be16(source);
611 case 16: /* UUID128 */
612 /* Check that the bytes in the UUID, except the service ID
613 * itself, are correct. The service ID is checked in
614 * bnep_setup_chk(). */
615 if (memcmp(&dest[4], bt_base, sizeof(bt_base)) != 0)
616 return BNEP_CONN_INVALID_DST;
617 if (memcmp(&source[4], bt_base, sizeof(bt_base)) != 0)
618 return BNEP_CONN_INVALID_SRC;
620 /* Intentional no-break */
623 val = get_be32(dest);
625 return BNEP_CONN_INVALID_DST;
629 val = get_be32(source);
631 return BNEP_CONN_INVALID_SRC;
636 return BNEP_CONN_INVALID_SVC;
639 /* Allowed PAN Profile scenarios */
643 if (src == BNEP_SVC_PANU)
645 return BNEP_CONN_INVALID_SRC;
647 if (src == BNEP_SVC_PANU || src == BNEP_SVC_GN ||
651 return BNEP_CONN_INVALID_SRC;
654 return BNEP_CONN_INVALID_DST;
657 #ifdef __TIZEN_PATCH__
658 int bnep_if_down_wrapper(const char *devname)
660 bnep_if_down(devname);
664 int bnep_conndel_wrapper(const bdaddr_t *dst)
671 static int bnep_server_add_legacy(int sk, uint16_t dst, char *bridge,
672 char *iface, const bdaddr_t *addr,
673 uint8_t *setup_data, int len)
678 n = read(sk, setup_data, len);
681 rsp = BNEP_CONN_NOT_ALLOWED;
685 err = bnep_connadd(sk, dst, iface);
687 rsp = BNEP_CONN_NOT_ALLOWED;
691 #ifndef __TIZEN_PATCH__
692 err = bnep_add_to_bridge(iface, bridge);
695 rsp = BNEP_CONN_NOT_ALLOWED;
700 err = bnep_if_up(iface);
702 bnep_del_from_bridge(iface, bridge);
704 rsp = BNEP_CONN_NOT_ALLOWED;
711 if (bnep_send_ctrl_rsp(sk, BNEP_SETUP_CONN_RSP, rsp) < 0) {
713 error("bnep: send ctrl rsp error: %s (%d)", strerror(-err),
720 int bnep_server_add(int sk, char *bridge, char *iface, const bdaddr_t *addr,
721 uint8_t *setup_data, int len)
726 struct bnep_setup_conn_req *req = (void *) setup_data;
728 /* Highest known Control command ID
729 * is BNEP_FILTER_MULT_ADDR_RSP = 0x06 */
730 if (req->type == BNEP_CONTROL &&
731 req->ctrl > BNEP_FILTER_MULT_ADDR_RSP) {
732 error("bnep: cmd not understood");
733 err = bnep_send_ctrl_rsp(sk, BNEP_CMD_NOT_UNDERSTOOD,
736 error("send not understood ctrl rsp error: %s (%d)",
737 strerror(errno), errno);
742 /* Processing BNEP_SETUP_CONNECTION_REQUEST_MSG */
743 rsp = bnep_setup_decode(sk, req, &dst);
744 if (rsp != BNEP_SUCCESS) {
746 error("bnep: error while decoding setup connection request: %d",
751 feat = bnep_getsuppfeat();
754 * Take out setup data if kernel doesn't support handling it, especially
755 * setup request. If kernel would have set session flags, they should
756 * be checked and handled respectively.
758 if (!feat || !(feat & (1 << BNEP_SETUP_RESPONSE)))
759 return bnep_server_add_legacy(sk, dst, bridge, iface, addr,
762 err = bnep_connadd(sk, dst, iface);
764 rsp = BNEP_CONN_NOT_ALLOWED;
768 #ifndef __TIZEN_PATCH__
769 err = bnep_add_to_bridge(iface, bridge);
774 err = bnep_if_up(iface);
781 bnep_del_from_bridge(iface, bridge);
789 if (bnep_send_ctrl_rsp(sk, BNEP_SETUP_CONN_RSP, rsp) < 0) {
791 error("bnep: send ctrl rsp error: %s (%d)", strerror(-err),
798 void bnep_server_delete(char *bridge, char *iface, const bdaddr_t *addr)
800 if (!bridge || !iface || !addr)
803 bnep_del_from_bridge(iface, bridge);