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
33 #include <bluetooth/bluetooth.h>
34 #include <bluetooth/bnep.h>
35 #include <bluetooth/sdp.h>
36 #include <bluetooth/sdp_lib.h>
37 #include <bluetooth/uuid.h>
38 #include <netinet/in.h>
43 #include "../src/dbus-common.h"
44 #include "../src/adapter.h"
54 #define NETWORK_SERVER_INTERFACE "org.bluez.NetworkServer"
55 #define SETUP_TIMEOUT 1
57 /* Pending Authorization */
58 struct network_session {
59 bdaddr_t dst; /* Remote Bluetooth Address */
60 GIOChannel *io; /* Pending connect channel */
61 guint watch; /* BNEP socket watch */
64 struct network_adapter {
65 struct btd_adapter *adapter; /* Adapter pointer */
66 GIOChannel *io; /* Bnep socket */
67 struct network_session *setup; /* Setup in progress */
68 GSList *servers; /* Server register to adapter */
71 /* Main server structure */
72 struct network_server {
73 bdaddr_t src; /* Bluetooth Local Address */
74 char *iface; /* DBus interface */
75 char *name; /* Server service name */
76 char *bridge; /* Bridge name */
77 uint32_t record_id; /* Service record id */
78 uint16_t id; /* Service class identifier */
79 GSList *sessions; /* Active connections */
80 struct network_adapter *na; /* Adapter reference */
81 guint watch_id; /* Client service watch */
84 static DBusConnection *connection = NULL;
85 static GSList *adapters = NULL;
86 static gboolean security = TRUE;
88 static struct network_adapter *find_adapter(GSList *list,
89 struct btd_adapter *adapter)
91 for (; list; list = list->next) {
92 struct network_adapter *na = list->data;
94 if (na->adapter == adapter)
101 static struct network_server *find_server(GSList *list, uint16_t id)
103 for (; list; list = list->next) {
104 struct network_server *ns = list->data;
113 static sdp_record_t *server_record_new(const char *name, uint16_t id)
115 sdp_list_t *svclass, *pfseq, *apseq, *root, *aproto;
116 uuid_t root_uuid, pan, l2cap, bnep;
117 sdp_profile_desc_t profile[1];
118 sdp_list_t *proto[2];
120 uint16_t psm = BNEP_PSM, version = 0x0100;
121 uint16_t security_desc = (security ? 0x0001 : 0x0000);
122 uint16_t net_access_type = 0xfffe;
123 uint32_t max_net_access_rate = 0;
124 const char *desc = "Network service";
125 sdp_record_t *record;
127 record = sdp_record_alloc();
131 record->attrlist = NULL;
132 record->pattern = NULL;
136 sdp_uuid16_create(&pan, NAP_SVCLASS_ID);
137 svclass = sdp_list_append(NULL, &pan);
138 sdp_set_service_classes(record, svclass);
140 sdp_uuid16_create(&profile[0].uuid, NAP_PROFILE_ID);
141 profile[0].version = 0x0100;
142 pfseq = sdp_list_append(NULL, &profile[0]);
143 sdp_set_profile_descs(record, pfseq);
145 sdp_set_info_attr(record, name, NULL, desc);
147 sdp_attr_add_new(record, SDP_ATTR_NET_ACCESS_TYPE,
148 SDP_UINT16, &net_access_type);
149 sdp_attr_add_new(record, SDP_ATTR_MAX_NET_ACCESSRATE,
150 SDP_UINT32, &max_net_access_rate);
153 sdp_uuid16_create(&pan, GN_SVCLASS_ID);
154 svclass = sdp_list_append(NULL, &pan);
155 sdp_set_service_classes(record, svclass);
157 sdp_uuid16_create(&profile[0].uuid, GN_PROFILE_ID);
158 profile[0].version = 0x0100;
159 pfseq = sdp_list_append(NULL, &profile[0]);
160 sdp_set_profile_descs(record, pfseq);
162 sdp_set_info_attr(record, name, NULL, desc);
165 sdp_uuid16_create(&pan, PANU_SVCLASS_ID);
166 svclass = sdp_list_append(NULL, &pan);
167 sdp_set_service_classes(record, svclass);
169 sdp_uuid16_create(&profile[0].uuid, PANU_PROFILE_ID);
170 profile[0].version = 0x0100;
171 pfseq = sdp_list_append(NULL, &profile[0]);
172 sdp_set_profile_descs(record, pfseq);
174 sdp_set_info_attr(record, name, NULL, desc);
177 sdp_record_free(record);
181 sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
182 root = sdp_list_append(NULL, &root_uuid);
183 sdp_set_browse_groups(record, root);
185 sdp_uuid16_create(&l2cap, L2CAP_UUID);
186 proto[0] = sdp_list_append(NULL, &l2cap);
187 p = sdp_data_alloc(SDP_UINT16, &psm);
188 proto[0] = sdp_list_append(proto[0], p);
189 apseq = sdp_list_append(NULL, proto[0]);
191 sdp_uuid16_create(&bnep, BNEP_UUID);
192 proto[1] = sdp_list_append(NULL, &bnep);
193 v = sdp_data_alloc(SDP_UINT16, &version);
194 proto[1] = sdp_list_append(proto[1], v);
196 /* Supported protocols */
202 sdp_data_t *head, *pseq;
205 for (p = 0, head = NULL; p < 2; p++) {
206 sdp_data_t *data = sdp_data_alloc(SDP_UINT16, &ptype[p]);
208 sdp_seq_append(head, data);
212 pseq = sdp_data_alloc(SDP_SEQ16, head);
213 proto[1] = sdp_list_append(proto[1], pseq);
216 apseq = sdp_list_append(apseq, proto[1]);
218 aproto = sdp_list_append(NULL, apseq);
219 sdp_set_access_protos(record, aproto);
221 sdp_add_lang_attr(record);
223 sdp_attr_add_new(record, SDP_ATTR_SECURITY_DESC,
224 SDP_UINT16, &security_desc);
228 sdp_list_free(apseq, NULL);
229 sdp_list_free(root, NULL);
230 sdp_list_free(aproto, NULL);
231 sdp_list_free(proto[0], NULL);
232 sdp_list_free(proto[1], NULL);
233 sdp_list_free(svclass, NULL);
234 sdp_list_free(pfseq, NULL);
239 static ssize_t send_bnep_ctrl_rsp(int sk, uint16_t val)
241 struct bnep_control_rsp rsp;
243 rsp.type = BNEP_CONTROL;
244 rsp.ctrl = BNEP_SETUP_CONN_RSP;
245 rsp.resp = htons(val);
247 return send(sk, &rsp, sizeof(rsp), 0);
250 static int server_connadd(struct network_server *ns,
251 struct network_session *session,
257 memset(devname, 0, sizeof(devname));
258 strcpy(devname, "bnep%d");
260 nsk = g_io_channel_unix_get_fd(session->io);
261 err = bnep_connadd(nsk, dst_role, devname);
265 info("Added new connection: %s", devname);
267 if (bnep_add_to_bridge(devname, ns->bridge) < 0) {
268 error("Can't add %s to the bridge %s: %s(%d)",
269 devname, ns->bridge, strerror(errno), errno);
275 ns->sessions = g_slist_append(ns->sessions, session);
280 static uint16_t bnep_setup_chk(uint16_t dst_role, uint16_t src_role)
282 /* Allowed PAN Profile scenarios */
286 if (src_role == BNEP_SVC_PANU)
288 return BNEP_CONN_INVALID_SRC;
290 if (src_role == BNEP_SVC_PANU ||
291 src_role == BNEP_SVC_GN ||
292 src_role == BNEP_SVC_NAP)
295 return BNEP_CONN_INVALID_SRC;
298 return BNEP_CONN_INVALID_DST;
301 static uint16_t bnep_setup_decode(struct bnep_setup_conn_req *req,
302 uint16_t *dst_role, uint16_t *src_role)
304 uint8_t *dest, *source;
307 source = req->service + req->uuid_size;
309 switch (req->uuid_size) {
311 *dst_role = bt_get_be16(dest);
312 *src_role = bt_get_be16(source);
315 case 16: /* UUID128 */
316 *dst_role = bt_get_be32(dest);
317 *src_role = bt_get_be32(source);
320 return BNEP_CONN_INVALID_SVC;
326 static void session_free(void *data)
328 struct network_session *session = data;
331 g_source_remove(session->watch);
334 g_io_channel_unref(session->io);
339 static void setup_destroy(void *user_data)
341 struct network_adapter *na = user_data;
342 struct network_session *setup = na->setup;
352 static gboolean bnep_setup(GIOChannel *chan,
353 GIOCondition cond, gpointer user_data)
355 struct network_adapter *na = user_data;
356 struct network_server *ns;
357 uint8_t packet[BNEP_MTU];
358 struct bnep_setup_conn_req *req = (void *) packet;
359 uint16_t src_role, dst_role, rsp = BNEP_CONN_NOT_ALLOWED;
362 if (cond & G_IO_NVAL)
365 if (cond & (G_IO_ERR | G_IO_HUP)) {
366 error("Hangup or error on BNEP socket");
370 sk = g_io_channel_unix_get_fd(chan);
372 /* Reading BNEP_SETUP_CONNECTION_REQUEST_MSG */
373 n = read(sk, packet, sizeof(packet));
375 error("read(): %s(%d)", strerror(errno), errno);
379 /* Highest known Control command ID
380 * is BNEP_FILTER_MULT_ADDR_RSP = 0x06 */
381 if (req->type == BNEP_CONTROL &&
382 req->ctrl > BNEP_FILTER_MULT_ADDR_RSP) {
385 pkt[0] = BNEP_CONTROL;
386 pkt[1] = BNEP_CMD_NOT_UNDERSTOOD;
389 send(sk, pkt, sizeof(pkt), 0);
394 if (req->type != BNEP_CONTROL || req->ctrl != BNEP_SETUP_CONN_REQ)
397 rsp = bnep_setup_decode(req, &dst_role, &src_role);
401 rsp = bnep_setup_chk(dst_role, src_role);
405 ns = find_server(na->servers, dst_role);
407 error("Server unavailable: (0x%x)", dst_role);
411 if (!ns->record_id) {
412 error("Service record not available");
417 error("Bridge interface not configured");
421 if (server_connadd(ns, na->setup, dst_role) < 0)
429 send_bnep_ctrl_rsp(sk, rsp);
434 static void connect_event(GIOChannel *chan, GError *err, gpointer user_data)
436 struct network_adapter *na = user_data;
439 error("%s", err->message);
444 g_io_channel_set_close_on_unref(chan, TRUE);
446 na->setup->watch = g_io_add_watch_full(chan, G_PRIORITY_DEFAULT,
447 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
448 bnep_setup, na, setup_destroy);
451 static void auth_cb(DBusError *derr, void *user_data)
453 struct network_adapter *na = user_data;
457 error("Access denied: %s", derr->message);
461 if (!bt_io_accept(na->setup->io, connect_event, na, NULL,
463 error("bt_io_accept: %s", err->message);
471 g_io_channel_shutdown(na->setup->io, TRUE, NULL);
475 static void confirm_event(GIOChannel *chan, gpointer user_data)
477 struct network_adapter *na = user_data;
478 struct network_server *ns;
484 bt_io_get(chan, BT_IO_L2CAP, &err,
485 BT_IO_OPT_SOURCE_BDADDR, &src,
486 BT_IO_OPT_DEST_BDADDR, &dst,
487 BT_IO_OPT_DEST, address,
490 error("%s", err->message);
495 DBG("BNEP: incoming connect from %s", address);
498 error("Refusing connect from %s: setup in progress", address);
502 ns = find_server(na->servers, BNEP_SVC_NAP);
512 na->setup = g_new0(struct network_session, 1);
513 bacpy(&na->setup->dst, &dst);
514 na->setup->io = g_io_channel_ref(chan);
516 perr = btd_request_authorization(&src, &dst, BNEP_SVC_UUID,
519 error("Refusing connect from %s: %s (%d)", address,
520 strerror(-perr), -perr);
528 g_io_channel_shutdown(chan, TRUE, NULL);
531 int server_init(DBusConnection *conn, gboolean secure)
534 connection = dbus_connection_ref(conn);
539 void server_exit(void)
541 dbus_connection_unref(connection);
545 static uint32_t register_server_record(struct network_server *ns)
547 sdp_record_t *record;
549 record = server_record_new(ns->name, ns->id);
551 error("Unable to allocate new service record");
555 if (add_record_to_server(&ns->src, record) < 0) {
556 error("Failed to register service record");
557 sdp_record_free(record);
561 DBG("got record id 0x%x", record->handle);
563 return record->handle;
566 static void server_disconnect(DBusConnection *conn, void *user_data)
568 struct network_server *ns = user_data;
573 remove_record_from_server(ns->record_id);
581 static DBusMessage *register_server(DBusConnection *conn,
582 DBusMessage *msg, void *data)
584 struct network_server *ns = data;
586 const char *uuid, *bridge;
588 if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &uuid,
589 DBUS_TYPE_STRING, &bridge, DBUS_TYPE_INVALID))
592 if (g_strcmp0(uuid, "nap"))
593 return btd_error_failed(msg, "Invalid UUID");
596 return btd_error_already_exists(msg);
598 reply = dbus_message_new_method_return(msg);
602 ns->record_id = register_server_record(ns);
604 return btd_error_failed(msg, "SDP record registration failed");
607 ns->bridge = g_strdup(bridge);
609 ns->watch_id = g_dbus_add_disconnect_watch(conn,
610 dbus_message_get_sender(msg),
611 server_disconnect, ns, NULL);
616 static DBusMessage *unregister_server(DBusConnection *conn,
617 DBusMessage *msg, void *data)
619 struct network_server *ns = data;
623 if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &uuid,
627 if (g_strcmp0(uuid, "nap"))
628 return btd_error_failed(msg, "Invalid UUID");
630 reply = dbus_message_new_method_return(msg);
634 g_dbus_remove_watch(conn, ns->watch_id);
636 server_disconnect(conn, ns);
641 static void adapter_free(struct network_adapter *na)
643 if (na->io != NULL) {
644 g_io_channel_shutdown(na->io, TRUE, NULL);
645 g_io_channel_unref(na->io);
649 btd_adapter_unref(na->adapter);
653 static void server_free(struct network_server *ns)
658 /* FIXME: Missing release/free all bnepX interfaces */
660 remove_record_from_server(ns->record_id);
666 g_slist_free_full(ns->sessions, session_free);
671 static void path_unregister(void *data)
673 struct network_server *ns = data;
674 struct network_adapter *na = ns->na;
676 DBG("Unregistered interface %s on path %s",
677 ns->iface, adapter_get_path(na->adapter));
679 na->servers = g_slist_remove(na->servers, ns);
685 adapters = g_slist_remove(adapters, na);
689 static const GDBusMethodTable server_methods[] = {
690 { GDBUS_METHOD("Register",
691 GDBUS_ARGS({ "uuid", "s" }, { "bridge", "s" }), NULL,
693 { GDBUS_METHOD("Unregister",
694 GDBUS_ARGS({ "uuid", "s" }), NULL,
695 unregister_server) },
699 static struct network_adapter *create_adapter(struct btd_adapter *adapter)
701 struct network_adapter *na;
705 na = g_new0(struct network_adapter, 1);
706 na->adapter = btd_adapter_ref(adapter);
708 adapter_get_address(adapter, &src);
710 na->io = bt_io_listen(BT_IO_L2CAP, NULL, confirm_event, na,
712 BT_IO_OPT_SOURCE_BDADDR, &src,
713 BT_IO_OPT_PSM, BNEP_PSM,
714 BT_IO_OPT_OMTU, BNEP_MTU,
715 BT_IO_OPT_IMTU, BNEP_MTU,
717 security ? BT_IO_SEC_MEDIUM : BT_IO_SEC_LOW,
720 error("%s", err->message);
729 int server_register(struct btd_adapter *adapter)
731 struct network_adapter *na;
732 struct network_server *ns;
735 na = find_adapter(adapters, adapter);
737 na = create_adapter(adapter);
740 adapters = g_slist_append(adapters, na);
743 ns = find_server(na->servers, BNEP_SVC_NAP);
747 ns = g_new0(struct network_server, 1);
749 ns->iface = g_strdup(NETWORK_SERVER_INTERFACE);
750 ns->name = g_strdup("Network service");
752 path = adapter_get_path(adapter);
754 if (!g_dbus_register_interface(connection, path, ns->iface,
755 server_methods, NULL, NULL,
756 ns, path_unregister)) {
757 error("D-Bus failed to register %s interface",
763 adapter_get_address(adapter, &ns->src);
764 ns->id = BNEP_SVC_NAP;
767 na->servers = g_slist_append(na->servers, ns);
769 DBG("Registered interface %s on path %s", ns->iface, path);
774 int server_unregister(struct btd_adapter *adapter)
776 struct network_adapter *na;
777 struct network_server *ns;
778 uint16_t id = BNEP_SVC_NAP;
780 na = find_adapter(adapters, adapter);
784 ns = find_server(na->servers, id);
788 g_dbus_unregister_interface(connection, adapter_get_path(adapter),