3fb031fa3940b5f900a3e586f7c80bc62f699fc4
[platform/upstream/bluez.git] / profiles / network / server.c
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
6  *
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <errno.h>
32
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>
38
39 #include <glib.h>
40 #include <gdbus/gdbus.h>
41
42 #include "btio/btio.h"
43 #include "lib/uuid.h"
44 #include "src/dbus-common.h"
45 #include "src/adapter.h"
46 #include "src/log.h"
47 #include "src/error.h"
48 #include "src/sdpd.h"
49
50 #include "bnep.h"
51 #include "server.h"
52
53 #define NETWORK_SERVER_INTERFACE "org.bluez.NetworkServer1"
54 #define BNEP_INTERFACE "bnep%d"
55 #define SETUP_TIMEOUT           1
56
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 */
63 };
64
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 */
70 };
71
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 */
82 };
83
84 static GSList *adapters = NULL;
85 static gboolean security = TRUE;
86
87 static struct network_adapter *find_adapter(GSList *list,
88                                         struct btd_adapter *adapter)
89 {
90         for (; list; list = list->next) {
91                 struct network_adapter *na = list->data;
92
93                 if (na->adapter == adapter)
94                         return na;
95         }
96
97         return NULL;
98 }
99
100 static struct network_server *find_server(GSList *list, uint16_t id)
101 {
102         for (; list; list = list->next) {
103                 struct network_server *ns = list->data;
104
105                 if (ns->id == id)
106                         return ns;
107         }
108
109         return NULL;
110 }
111
112 static struct network_server *find_server_by_uuid(GSList *list,
113                                                         const char *uuid)
114 {
115         for (; list; list = list->next) {
116                 struct network_server *ns = list->data;
117
118                 if (strcasecmp(uuid, bnep_uuid(ns->id)) == 0)
119                         return ns;
120
121                 if (strcasecmp(uuid, bnep_name(ns->id)) == 0)
122                         return ns;
123         }
124
125         return NULL;
126 }
127
128 static sdp_record_t *server_record_new(const char *name, uint16_t id)
129 {
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];
134         sdp_data_t *v, *p;
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;
141
142         record = sdp_record_alloc();
143         if (!record)
144                 return NULL;
145
146         record->attrlist = NULL;
147         record->pattern = NULL;
148
149         switch (id) {
150         case BNEP_SVC_NAP:
151                 sdp_uuid16_create(&pan, NAP_SVCLASS_ID);
152                 svclass = sdp_list_append(NULL, &pan);
153                 sdp_set_service_classes(record, svclass);
154
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);
159
160                 sdp_set_info_attr(record, name, NULL, desc);
161
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);
166                 break;
167         case BNEP_SVC_GN:
168                 sdp_uuid16_create(&pan, GN_SVCLASS_ID);
169                 svclass = sdp_list_append(NULL, &pan);
170                 sdp_set_service_classes(record, svclass);
171
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);
176
177                 sdp_set_info_attr(record, name, NULL, desc);
178                 break;
179         case BNEP_SVC_PANU:
180                 sdp_uuid16_create(&pan, PANU_SVCLASS_ID);
181                 svclass = sdp_list_append(NULL, &pan);
182                 sdp_set_service_classes(record, svclass);
183
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);
188
189                 sdp_set_info_attr(record, name, NULL, desc);
190                 break;
191         default:
192                 sdp_record_free(record);
193                 return NULL;
194         }
195
196         sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
197         root = sdp_list_append(NULL, &root_uuid);
198         sdp_set_browse_groups(record, root);
199
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]);
205
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);
210
211         /* Supported protocols */
212         {
213                 uint16_t ptype[] = {
214                         0x0800,  /* IPv4 */
215                         0x0806,  /* ARP */
216                 };
217                 sdp_data_t *head, *pseq;
218                 int p;
219
220                 for (p = 0, head = NULL; p < 2; p++) {
221                         sdp_data_t *data = sdp_data_alloc(SDP_UINT16, &ptype[p]);
222                         if (head)
223                                 sdp_seq_append(head, data);
224                         else
225                                 head = data;
226                 }
227                 pseq = sdp_data_alloc(SDP_SEQ16, head);
228                 proto[1] = sdp_list_append(proto[1], pseq);
229         }
230
231         apseq = sdp_list_append(apseq, proto[1]);
232
233         aproto = sdp_list_append(NULL, apseq);
234         sdp_set_access_protos(record, aproto);
235
236         sdp_add_lang_attr(record);
237
238         sdp_attr_add_new(record, SDP_ATTR_SECURITY_DESC,
239                                 SDP_UINT16, &security_desc);
240
241         sdp_data_free(p);
242         sdp_data_free(v);
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);
250
251         return record;
252 }
253
254 static void session_free(void *data)
255 {
256         struct network_session *session = data;
257
258         if (session->watch)
259                 g_source_remove(session->watch);
260
261         if (session->io)
262                 g_io_channel_unref(session->io);
263
264         g_free(session);
265 }
266
267 static void setup_destroy(void *user_data)
268 {
269         struct network_adapter *na = user_data;
270         struct network_session *setup = na->setup;
271
272         if (!setup)
273                 return;
274
275         na->setup = NULL;
276
277         session_free(setup);
278 }
279
280 static gboolean bnep_setup(GIOChannel *chan,
281                         GIOCondition cond, gpointer user_data)
282 {
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;
288         int n, sk;
289
290         if (cond & G_IO_NVAL)
291                 return FALSE;
292
293         if (cond & (G_IO_ERR | G_IO_HUP)) {
294                 error("Hangup or error on BNEP socket");
295                 return FALSE;
296         }
297
298         sk = g_io_channel_unix_get_fd(chan);
299
300         /* Reading BNEP_SETUP_CONNECTION_REQUEST_MSG */
301         n = read(sk, packet, sizeof(packet));
302         if (n < 0) {
303                 error("read(): %s(%d)", strerror(errno), errno);
304                 return FALSE;
305         }
306
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) {
311                 uint8_t pkt[3];
312
313                 pkt[0] = BNEP_CONTROL;
314                 pkt[1] = BNEP_CMD_NOT_UNDERSTOOD;
315                 pkt[2] = req->ctrl;
316
317                 send(sk, pkt, sizeof(pkt), 0);
318
319                 return FALSE;
320         }
321
322         if (req->type != BNEP_CONTROL || req->ctrl != BNEP_SETUP_CONN_REQ)
323                 return FALSE;
324
325         rsp = bnep_setup_decode(req, &dst_role, &src_role);
326         if (rsp)
327                 goto reply;
328
329         rsp = bnep_setup_chk(dst_role, src_role);
330         if (rsp)
331                 goto reply;
332
333         rsp = BNEP_CONN_NOT_ALLOWED;
334
335         ns = find_server(na->servers, dst_role);
336         if (!ns) {
337                 error("Server unavailable: (0x%x)", dst_role);
338                 goto reply;
339         }
340
341         if (!ns->record_id) {
342                 error("Service record not available");
343                 goto reply;
344         }
345
346         if (!ns->bridge) {
347                 error("Bridge interface not configured");
348                 goto reply;
349         }
350
351         strncpy(na->setup->dev, BNEP_INTERFACE, 16);
352         na->setup->dev[15] = '\0';
353
354         if (bnep_server_add(sk, dst_role, ns->bridge, na->setup->dev,
355                                                         &na->setup->dst) < 0)
356                 goto reply;
357
358         na->setup = NULL;
359
360         rsp = BNEP_SUCCESS;
361
362 reply:
363         bnep_send_ctrl_rsp(sk, BNEP_CONTROL, BNEP_SETUP_CONN_RSP, rsp);
364
365         return FALSE;
366 }
367
368 static void connect_event(GIOChannel *chan, GError *err, gpointer user_data)
369 {
370         struct network_adapter *na = user_data;
371
372         if (err) {
373                 error("%s", err->message);
374                 setup_destroy(na);
375                 return;
376         }
377
378         g_io_channel_set_close_on_unref(chan, TRUE);
379
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);
383 }
384
385 static void auth_cb(DBusError *derr, void *user_data)
386 {
387         struct network_adapter *na = user_data;
388         GError *err = NULL;
389
390         if (derr) {
391                 error("Access denied: %s", derr->message);
392                 goto reject;
393         }
394
395         if (!bt_io_accept(na->setup->io, connect_event, na, NULL,
396                                                         &err)) {
397                 error("bt_io_accept: %s", err->message);
398                 g_error_free(err);
399                 goto reject;
400         }
401
402         return;
403
404 reject:
405         g_io_channel_shutdown(na->setup->io, TRUE, NULL);
406         setup_destroy(na);
407 }
408
409 static void confirm_event(GIOChannel *chan, gpointer user_data)
410 {
411         struct network_adapter *na = user_data;
412         struct network_server *ns;
413         bdaddr_t src, dst;
414         char address[18];
415         GError *err = NULL;
416         guint ret;
417
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,
422                         BT_IO_OPT_INVALID);
423         if (err) {
424                 error("%s", err->message);
425                 g_error_free(err);
426                 goto drop;
427         }
428
429         DBG("BNEP: incoming connect from %s", address);
430
431         if (na->setup) {
432                 error("Refusing connect from %s: setup in progress", address);
433                 goto drop;
434         }
435
436         ns = find_server(na->servers, BNEP_SVC_NAP);
437         if (!ns)
438                 goto drop;
439
440         if (!ns->record_id)
441                 goto drop;
442
443         if (!ns->bridge)
444                 goto drop;
445
446         na->setup = g_new0(struct network_session, 1);
447         bacpy(&na->setup->dst, &dst);
448         na->setup->io = g_io_channel_ref(chan);
449
450         ret = btd_request_authorization(&src, &dst, BNEP_SVC_UUID,
451                                         auth_cb, na);
452         if (ret == 0) {
453                 error("Refusing connect from %s", address);
454                 setup_destroy(na);
455                 goto drop;
456         }
457
458         return;
459
460 drop:
461         g_io_channel_shutdown(chan, TRUE, NULL);
462 }
463
464 int server_init(gboolean secure)
465 {
466         security = secure;
467
468         return 0;
469 }
470
471 static uint32_t register_server_record(struct network_server *ns)
472 {
473         sdp_record_t *record;
474
475         record = server_record_new(ns->name, ns->id);
476         if (!record) {
477                 error("Unable to allocate new service record");
478                 return 0;
479         }
480
481         if (adapter_service_add(ns->na->adapter, record) < 0) {
482                 error("Failed to register service record");
483                 sdp_record_free(record);
484                 return 0;
485         }
486
487         DBG("got record id 0x%x", record->handle);
488
489         return record->handle;
490 }
491
492 static void server_remove_sessions(struct network_server *ns)
493 {
494         GSList *list;
495
496         for (list = ns->sessions; list; list = list->next) {
497                 struct network_session *session = list->data;
498
499                 if (*session->dev == '\0')
500                         continue;
501
502                 bnep_server_delete(ns->bridge, session->dev, &session->dst);
503         }
504
505         g_slist_free_full(ns->sessions, session_free);
506
507         ns->sessions = NULL;
508 }
509
510 static void server_disconnect(DBusConnection *conn, void *user_data)
511 {
512         struct network_server *ns = user_data;
513
514         server_remove_sessions(ns);
515
516         ns->watch_id = 0;
517
518         if (ns->record_id) {
519                 adapter_service_remove(ns->na->adapter, ns->record_id);
520                 ns->record_id = 0;
521         }
522
523         g_free(ns->bridge);
524         ns->bridge = NULL;
525 }
526
527 static DBusMessage *register_server(DBusConnection *conn,
528                                 DBusMessage *msg, void *data)
529 {
530         struct network_adapter *na = data;
531         struct network_server *ns;
532         DBusMessage *reply;
533         const char *uuid, *bridge;
534
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);
538
539         ns = find_server_by_uuid(na->servers, uuid);
540         if (ns == NULL)
541                 return btd_error_failed(msg, "Invalid UUID");
542
543         if (ns->record_id)
544                 return btd_error_already_exists(msg);
545
546         reply = dbus_message_new_method_return(msg);
547         if (!reply)
548                 return NULL;
549
550         ns->record_id = register_server_record(ns);
551         if (!ns->record_id)
552                 return btd_error_failed(msg, "SDP record registration failed");
553
554         g_free(ns->bridge);
555         ns->bridge = g_strdup(bridge);
556
557         ns->watch_id = g_dbus_add_disconnect_watch(conn,
558                                         dbus_message_get_sender(msg),
559                                         server_disconnect, ns, NULL);
560
561         return reply;
562 }
563
564 static DBusMessage *unregister_server(DBusConnection *conn,
565                                         DBusMessage *msg, void *data)
566 {
567         struct network_adapter *na = data;
568         struct network_server *ns;
569         DBusMessage *reply;
570         const char *uuid;
571
572         if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &uuid,
573                                                         DBUS_TYPE_INVALID))
574                 return btd_error_invalid_args(msg);
575
576         ns = find_server_by_uuid(na->servers, uuid);
577         if (!ns)
578                 return btd_error_failed(msg, "Invalid UUID");
579
580         reply = dbus_message_new_method_return(msg);
581         if (!reply)
582                 return NULL;
583
584         g_dbus_remove_watch(conn, ns->watch_id);
585
586         server_disconnect(conn, ns);
587
588         return reply;
589 }
590
591 static void adapter_free(struct network_adapter *na)
592 {
593         if (na->io != NULL) {
594                 g_io_channel_shutdown(na->io, TRUE, NULL);
595                 g_io_channel_unref(na->io);
596         }
597
598         setup_destroy(na);
599         btd_adapter_unref(na->adapter);
600         g_free(na);
601 }
602
603 static void server_free(void *data)
604 {
605         struct network_server *ns = data;
606
607         if (!ns)
608                 return;
609
610         server_remove_sessions(ns);
611
612         if (ns->record_id)
613                 adapter_service_remove(ns->na->adapter, ns->record_id);
614
615         g_dbus_remove_watch(btd_get_dbus_connection(), ns->watch_id);
616         g_free(ns->name);
617         g_free(ns->bridge);
618
619         g_free(ns);
620 }
621
622 static void path_unregister(void *data)
623 {
624         struct network_adapter *na = data;
625
626         DBG("Unregistered interface %s on path %s",
627                 NETWORK_SERVER_INTERFACE, adapter_get_path(na->adapter));
628
629         g_slist_free_full(na->servers, server_free);
630
631         adapters = g_slist_remove(adapters, na);
632         adapter_free(na);
633 }
634
635 static const GDBusMethodTable server_methods[] = {
636         { GDBUS_METHOD("Register",
637                         GDBUS_ARGS({ "uuid", "s" }, { "bridge", "s" }), NULL,
638                         register_server) },
639         { GDBUS_METHOD("Unregister",
640                         GDBUS_ARGS({ "uuid", "s" }), NULL,
641                         unregister_server) },
642         { }
643 };
644
645 static struct network_adapter *create_adapter(struct btd_adapter *adapter)
646 {
647         struct network_adapter *na;
648         GError *err = NULL;
649
650         na = g_new0(struct network_adapter, 1);
651         na->adapter = btd_adapter_ref(adapter);
652
653         na->io = bt_io_listen(NULL, confirm_event, na,
654                                 NULL, &err,
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,
660                                 BT_IO_OPT_SEC_LEVEL,
661                                 security ? BT_IO_SEC_MEDIUM : BT_IO_SEC_LOW,
662                                 BT_IO_OPT_INVALID);
663         if (!na->io) {
664                 error("%s", err->message);
665                 g_error_free(err);
666                 adapter_free(na);
667                 return NULL;
668         }
669
670         return na;
671 }
672
673 int server_register(struct btd_adapter *adapter, uint16_t id)
674 {
675         struct network_adapter *na;
676         struct network_server *ns;
677         const char *path;
678
679         na = find_adapter(adapters, adapter);
680         if (!na) {
681                 na = create_adapter(adapter);
682                 if (!na)
683                         return -EINVAL;
684                 adapters = g_slist_append(adapters, na);
685         }
686
687         ns = find_server(na->servers, id);
688         if (ns)
689                 return 0;
690
691         ns = g_new0(struct network_server, 1);
692
693         ns->name = g_strdup("Network service");
694
695         path = adapter_get_path(adapter);
696
697         if (g_slist_length(na->servers) > 0)
698                 goto done;
699
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);
706                 server_free(ns);
707                 return -1;
708         }
709
710         DBG("Registered interface %s on path %s", NETWORK_SERVER_INTERFACE,
711                                                                         path);
712
713 done:
714         bacpy(&ns->src, btd_adapter_get_address(adapter));
715         ns->id = id;
716         ns->na = na;
717         ns->record_id = 0;
718         na->servers = g_slist_append(na->servers, ns);
719
720         return 0;
721 }
722
723 int server_unregister(struct btd_adapter *adapter, uint16_t id)
724 {
725         struct network_adapter *na;
726         struct network_server *ns;
727
728         na = find_adapter(adapters, adapter);
729         if (!na)
730                 return -EINVAL;
731
732         ns = find_server(na->servers, id);
733         if (!ns)
734                 return -EINVAL;
735
736         na->servers = g_slist_remove(na->servers, ns);
737         server_free(ns);
738
739         if (g_slist_length(na->servers) > 0)
740                 return 0;
741
742         g_dbus_unregister_interface(btd_get_dbus_connection(),
743                                                 adapter_get_path(adapter),
744                                                 NETWORK_SERVER_INTERFACE);
745
746         return 0;
747 }