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 <netinet/in.h>
40 #include <gdbus/gdbus.h>
42 #include "btio/btio.h"
44 #include "src/dbus-common.h"
45 #include "src/adapter.h"
47 #include "src/error.h"
53 #define NETWORK_SERVER_INTERFACE "org.bluez.NetworkServer1"
54 #define BNEP_INTERFACE "bnep%d"
55 #define SETUP_TIMEOUT 1
57 /* Pending Authorization */
58 struct network_session {
59 bdaddr_t dst; /* Remote Bluetooth Address */
60 char dev[16]; /* Interface name */
61 GIOChannel *io; /* Pending connect channel */
62 guint watch; /* BNEP socket watch */
65 struct network_adapter {
66 struct btd_adapter *adapter; /* Adapter pointer */
67 GIOChannel *io; /* Bnep socket */
68 struct network_session *setup; /* Setup in progress */
69 GSList *servers; /* Server register to adapter */
72 /* Main server structure */
73 struct network_server {
74 bdaddr_t src; /* Bluetooth Local Address */
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 GSList *adapters = NULL;
85 static gboolean security = TRUE;
87 static struct network_adapter *find_adapter(GSList *list,
88 struct btd_adapter *adapter)
90 for (; list; list = list->next) {
91 struct network_adapter *na = list->data;
93 if (na->adapter == adapter)
100 static struct network_server *find_server(GSList *list, uint16_t id)
102 for (; list; list = list->next) {
103 struct network_server *ns = list->data;
112 static struct network_server *find_server_by_uuid(GSList *list,
115 for (; list; list = list->next) {
116 struct network_server *ns = list->data;
118 if (strcasecmp(uuid, bnep_uuid(ns->id)) == 0)
121 if (strcasecmp(uuid, bnep_name(ns->id)) == 0)
128 static sdp_record_t *server_record_new(const char *name, uint16_t id)
130 sdp_list_t *svclass, *pfseq, *apseq, *root, *aproto;
131 uuid_t root_uuid, pan, l2cap, bnep;
132 sdp_profile_desc_t profile[1];
133 sdp_list_t *proto[2];
135 uint16_t psm = BNEP_PSM, version = 0x0100;
136 uint16_t security_desc = (security ? 0x0001 : 0x0000);
137 uint16_t net_access_type = 0xfffe;
138 uint32_t max_net_access_rate = 0;
139 const char *desc = "Network service";
140 sdp_record_t *record;
142 record = sdp_record_alloc();
146 record->attrlist = NULL;
147 record->pattern = NULL;
151 sdp_uuid16_create(&pan, NAP_SVCLASS_ID);
152 svclass = sdp_list_append(NULL, &pan);
153 sdp_set_service_classes(record, svclass);
155 sdp_uuid16_create(&profile[0].uuid, NAP_PROFILE_ID);
156 profile[0].version = 0x0100;
157 pfseq = sdp_list_append(NULL, &profile[0]);
158 sdp_set_profile_descs(record, pfseq);
160 sdp_set_info_attr(record, name, NULL, desc);
162 sdp_attr_add_new(record, SDP_ATTR_NET_ACCESS_TYPE,
163 SDP_UINT16, &net_access_type);
164 sdp_attr_add_new(record, SDP_ATTR_MAX_NET_ACCESSRATE,
165 SDP_UINT32, &max_net_access_rate);
168 sdp_uuid16_create(&pan, GN_SVCLASS_ID);
169 svclass = sdp_list_append(NULL, &pan);
170 sdp_set_service_classes(record, svclass);
172 sdp_uuid16_create(&profile[0].uuid, GN_PROFILE_ID);
173 profile[0].version = 0x0100;
174 pfseq = sdp_list_append(NULL, &profile[0]);
175 sdp_set_profile_descs(record, pfseq);
177 sdp_set_info_attr(record, name, NULL, desc);
180 sdp_uuid16_create(&pan, PANU_SVCLASS_ID);
181 svclass = sdp_list_append(NULL, &pan);
182 sdp_set_service_classes(record, svclass);
184 sdp_uuid16_create(&profile[0].uuid, PANU_PROFILE_ID);
185 profile[0].version = 0x0100;
186 pfseq = sdp_list_append(NULL, &profile[0]);
187 sdp_set_profile_descs(record, pfseq);
189 sdp_set_info_attr(record, name, NULL, desc);
192 sdp_record_free(record);
196 sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
197 root = sdp_list_append(NULL, &root_uuid);
198 sdp_set_browse_groups(record, root);
200 sdp_uuid16_create(&l2cap, L2CAP_UUID);
201 proto[0] = sdp_list_append(NULL, &l2cap);
202 p = sdp_data_alloc(SDP_UINT16, &psm);
203 proto[0] = sdp_list_append(proto[0], p);
204 apseq = sdp_list_append(NULL, proto[0]);
206 sdp_uuid16_create(&bnep, BNEP_UUID);
207 proto[1] = sdp_list_append(NULL, &bnep);
208 v = sdp_data_alloc(SDP_UINT16, &version);
209 proto[1] = sdp_list_append(proto[1], v);
211 /* Supported protocols */
217 sdp_data_t *head, *pseq;
220 for (p = 0, head = NULL; p < 2; p++) {
221 sdp_data_t *data = sdp_data_alloc(SDP_UINT16, &ptype[p]);
223 sdp_seq_append(head, data);
227 pseq = sdp_data_alloc(SDP_SEQ16, head);
228 proto[1] = sdp_list_append(proto[1], pseq);
231 apseq = sdp_list_append(apseq, proto[1]);
233 aproto = sdp_list_append(NULL, apseq);
234 sdp_set_access_protos(record, aproto);
236 sdp_add_lang_attr(record);
238 sdp_attr_add_new(record, SDP_ATTR_SECURITY_DESC,
239 SDP_UINT16, &security_desc);
243 sdp_list_free(apseq, NULL);
244 sdp_list_free(root, NULL);
245 sdp_list_free(aproto, NULL);
246 sdp_list_free(proto[0], NULL);
247 sdp_list_free(proto[1], NULL);
248 sdp_list_free(svclass, NULL);
249 sdp_list_free(pfseq, NULL);
254 static void session_free(void *data)
256 struct network_session *session = data;
259 g_source_remove(session->watch);
262 g_io_channel_unref(session->io);
267 static void setup_destroy(void *user_data)
269 struct network_adapter *na = user_data;
270 struct network_session *setup = na->setup;
280 static gboolean bnep_setup(GIOChannel *chan,
281 GIOCondition cond, gpointer user_data)
283 struct network_adapter *na = user_data;
284 struct network_server *ns;
285 uint8_t packet[BNEP_MTU];
286 struct bnep_setup_conn_req *req = (void *) packet;
287 uint16_t src_role, dst_role, rsp = BNEP_CONN_NOT_ALLOWED;
290 if (cond & G_IO_NVAL)
293 if (cond & (G_IO_ERR | G_IO_HUP)) {
294 error("Hangup or error on BNEP socket");
298 sk = g_io_channel_unix_get_fd(chan);
300 /* Reading BNEP_SETUP_CONNECTION_REQUEST_MSG */
301 n = read(sk, packet, sizeof(packet));
303 error("read(): %s(%d)", strerror(errno), errno);
307 /* Highest known Control command ID
308 * is BNEP_FILTER_MULT_ADDR_RSP = 0x06 */
309 if (req->type == BNEP_CONTROL &&
310 req->ctrl > BNEP_FILTER_MULT_ADDR_RSP) {
313 pkt[0] = BNEP_CONTROL;
314 pkt[1] = BNEP_CMD_NOT_UNDERSTOOD;
317 send(sk, pkt, sizeof(pkt), 0);
322 if (req->type != BNEP_CONTROL || req->ctrl != BNEP_SETUP_CONN_REQ)
325 rsp = bnep_setup_decode(req, &dst_role, &src_role);
329 rsp = bnep_setup_chk(dst_role, src_role);
333 rsp = BNEP_CONN_NOT_ALLOWED;
335 ns = find_server(na->servers, dst_role);
337 error("Server unavailable: (0x%x)", dst_role);
341 if (!ns->record_id) {
342 error("Service record not available");
347 error("Bridge interface not configured");
351 strncpy(na->setup->dev, BNEP_INTERFACE, 16);
352 na->setup->dev[15] = '\0';
354 if (bnep_server_add(sk, dst_role, ns->bridge, na->setup->dev,
355 &na->setup->dst) < 0)
363 bnep_send_ctrl_rsp(sk, BNEP_CONTROL, BNEP_SETUP_CONN_RSP, rsp);
368 static void connect_event(GIOChannel *chan, GError *err, gpointer user_data)
370 struct network_adapter *na = user_data;
373 error("%s", err->message);
378 g_io_channel_set_close_on_unref(chan, TRUE);
380 na->setup->watch = g_io_add_watch_full(chan, G_PRIORITY_DEFAULT,
381 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
382 bnep_setup, na, setup_destroy);
385 static void auth_cb(DBusError *derr, void *user_data)
387 struct network_adapter *na = user_data;
391 error("Access denied: %s", derr->message);
395 if (!bt_io_accept(na->setup->io, connect_event, na, NULL,
397 error("bt_io_accept: %s", err->message);
405 g_io_channel_shutdown(na->setup->io, TRUE, NULL);
409 static void confirm_event(GIOChannel *chan, gpointer user_data)
411 struct network_adapter *na = user_data;
412 struct network_server *ns;
418 bt_io_get(chan, &err,
419 BT_IO_OPT_SOURCE_BDADDR, &src,
420 BT_IO_OPT_DEST_BDADDR, &dst,
421 BT_IO_OPT_DEST, address,
424 error("%s", err->message);
429 DBG("BNEP: incoming connect from %s", address);
432 error("Refusing connect from %s: setup in progress", address);
436 ns = find_server(na->servers, BNEP_SVC_NAP);
446 na->setup = g_new0(struct network_session, 1);
447 bacpy(&na->setup->dst, &dst);
448 na->setup->io = g_io_channel_ref(chan);
450 ret = btd_request_authorization(&src, &dst, BNEP_SVC_UUID,
453 error("Refusing connect from %s", address);
461 g_io_channel_shutdown(chan, TRUE, NULL);
464 int server_init(gboolean secure)
471 static uint32_t register_server_record(struct network_server *ns)
473 sdp_record_t *record;
475 record = server_record_new(ns->name, ns->id);
477 error("Unable to allocate new service record");
481 if (adapter_service_add(ns->na->adapter, record) < 0) {
482 error("Failed to register service record");
483 sdp_record_free(record);
487 DBG("got record id 0x%x", record->handle);
489 return record->handle;
492 static void server_remove_sessions(struct network_server *ns)
496 for (list = ns->sessions; list; list = list->next) {
497 struct network_session *session = list->data;
499 if (*session->dev == '\0')
502 bnep_server_delete(ns->bridge, session->dev, &session->dst);
505 g_slist_free_full(ns->sessions, session_free);
510 static void server_disconnect(DBusConnection *conn, void *user_data)
512 struct network_server *ns = user_data;
514 server_remove_sessions(ns);
519 adapter_service_remove(ns->na->adapter, ns->record_id);
527 static DBusMessage *register_server(DBusConnection *conn,
528 DBusMessage *msg, void *data)
530 struct network_adapter *na = data;
531 struct network_server *ns;
533 const char *uuid, *bridge;
535 if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &uuid,
536 DBUS_TYPE_STRING, &bridge, DBUS_TYPE_INVALID))
537 return btd_error_invalid_args(msg);
539 ns = find_server_by_uuid(na->servers, uuid);
541 return btd_error_failed(msg, "Invalid UUID");
544 return btd_error_already_exists(msg);
546 reply = dbus_message_new_method_return(msg);
550 ns->record_id = register_server_record(ns);
552 return btd_error_failed(msg, "SDP record registration failed");
555 ns->bridge = g_strdup(bridge);
557 ns->watch_id = g_dbus_add_disconnect_watch(conn,
558 dbus_message_get_sender(msg),
559 server_disconnect, ns, NULL);
564 static DBusMessage *unregister_server(DBusConnection *conn,
565 DBusMessage *msg, void *data)
567 struct network_adapter *na = data;
568 struct network_server *ns;
572 if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &uuid,
574 return btd_error_invalid_args(msg);
576 ns = find_server_by_uuid(na->servers, uuid);
578 return btd_error_failed(msg, "Invalid UUID");
580 reply = dbus_message_new_method_return(msg);
584 g_dbus_remove_watch(conn, ns->watch_id);
586 server_disconnect(conn, ns);
591 static void adapter_free(struct network_adapter *na)
593 if (na->io != NULL) {
594 g_io_channel_shutdown(na->io, TRUE, NULL);
595 g_io_channel_unref(na->io);
599 btd_adapter_unref(na->adapter);
603 static void server_free(void *data)
605 struct network_server *ns = data;
610 server_remove_sessions(ns);
613 adapter_service_remove(ns->na->adapter, ns->record_id);
615 g_dbus_remove_watch(btd_get_dbus_connection(), ns->watch_id);
622 static void path_unregister(void *data)
624 struct network_adapter *na = data;
626 DBG("Unregistered interface %s on path %s",
627 NETWORK_SERVER_INTERFACE, adapter_get_path(na->adapter));
629 g_slist_free_full(na->servers, server_free);
631 adapters = g_slist_remove(adapters, na);
635 static const GDBusMethodTable server_methods[] = {
636 { GDBUS_METHOD("Register",
637 GDBUS_ARGS({ "uuid", "s" }, { "bridge", "s" }), NULL,
639 { GDBUS_METHOD("Unregister",
640 GDBUS_ARGS({ "uuid", "s" }), NULL,
641 unregister_server) },
645 static struct network_adapter *create_adapter(struct btd_adapter *adapter)
647 struct network_adapter *na;
650 na = g_new0(struct network_adapter, 1);
651 na->adapter = btd_adapter_ref(adapter);
653 na->io = bt_io_listen(NULL, confirm_event, na,
655 BT_IO_OPT_SOURCE_BDADDR,
656 btd_adapter_get_address(adapter),
657 BT_IO_OPT_PSM, BNEP_PSM,
658 BT_IO_OPT_OMTU, BNEP_MTU,
659 BT_IO_OPT_IMTU, BNEP_MTU,
661 security ? BT_IO_SEC_MEDIUM : BT_IO_SEC_LOW,
664 error("%s", err->message);
673 int server_register(struct btd_adapter *adapter, uint16_t id)
675 struct network_adapter *na;
676 struct network_server *ns;
679 na = find_adapter(adapters, adapter);
681 na = create_adapter(adapter);
684 adapters = g_slist_append(adapters, na);
687 ns = find_server(na->servers, id);
691 ns = g_new0(struct network_server, 1);
693 ns->name = g_strdup("Network service");
695 path = adapter_get_path(adapter);
697 if (g_slist_length(na->servers) > 0)
700 if (!g_dbus_register_interface(btd_get_dbus_connection(),
701 path, NETWORK_SERVER_INTERFACE,
702 server_methods, NULL, NULL,
703 na, path_unregister)) {
704 error("D-Bus failed to register %s interface",
705 NETWORK_SERVER_INTERFACE);
710 DBG("Registered interface %s on path %s", NETWORK_SERVER_INTERFACE,
714 bacpy(&ns->src, btd_adapter_get_address(adapter));
718 na->servers = g_slist_append(na->servers, ns);
723 int server_unregister(struct btd_adapter *adapter, uint16_t id)
725 struct network_adapter *na;
726 struct network_server *ns;
728 na = find_adapter(adapters, adapter);
732 ns = find_server(na->servers, id);
736 na->servers = g_slist_remove(na->servers, ns);
739 if (g_slist_length(na->servers) > 0)
742 g_dbus_unregister_interface(btd_get_dbus_connection(),
743 adapter_get_path(adapter),
744 NETWORK_SERVER_INTERFACE);