Skip to contentSkip to conversation

6LoWPAN userspace support

RFC New Luiz Augusto von Dentz 1 participant 8 messages Oct 20, 2017

Subsystems (4)
lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

cover 6LoWPAN userspace support

From: Luiz Augusto von Dentz <[email protected]>

This introduces support for 6LoWPAN userspace interface creation along with IPSP plugin which takes care of L2CAP channel creation.

The bt_6lo instance handles all the details to create the network interface and also attach channels to it so any io is automatically forward to the right path. The plugin make use of bt_6lo and create interfaces for each adapter available using "bt" as prefix followed by adapter index.

At the current stage only router/central role is supported since peripheral role may actually need to be enabled via advertisement which perhaps will require a D-Bus to be triggered on demand.

Since the IPSP plugins does actually register a proper profile driver Device.Connect can now attempt to connect the IPSP channel and then proceed to attach the channel to interface, so it is well integrated with existing D-Bus APIs.

Note that this all depends on TUN/TAP changes proposed here:
http://www.spinics.net/lists/linux-bluetooth/msg72471.html

Luiz Augusto von Dentz (7):
shared/io: Add io_recv
shared: Add initial code for 6LoWPAN
unit: Add initial test for bt_6lo
uuid: Add IPSP UUID
build: Add IPSP plugin
ipsp: Connect to IPSP PSM
ipsp: Add support for bt_6lo

 Makefile.am             |   8 +-
 Makefile.plugins        |   5 +
 configure.ac            |   4 +
 lib/uuid.h              |   2 +
 profiles/network/ipsp.c | 318 +++++++++++++++++++++++++++++++
 src/shared/6lo.c        | 493 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/shared/6lo.h        |  40 ++++
 src/shared/io-glib.c    |  20 ++
 src/shared/io.h         |   1 +
 unit/test-6lo.c         | 271 ++++++++++++++++++++++++++
 10 files changed, 1161 insertions(+), 1 deletion(-)
 create mode 100644 profiles/network/ipsp.c
 create mode 100644 src/shared/6lo.c
 create mode 100644 src/shared/6lo.h
 create mode 100644 unit/test-6lo.c
Signature · 1 line
2.13.6
author maintainer
#1 lore raw

RFC 1/7 shared/io: Add io_recv

From: Luiz Augusto von Dentz <[email protected]>

This adds io_recv for reading using different buffer pointed by iovec.

---
 src/shared/io-glib.c | 20 ++++++++++++++++++++
 src/shared/io.h      |  1 +
 2 files changed, 21 insertions(+)
src/shared/io-glib.c +20 -0
index 6687a6b28..3ea68ac4e 100644
@@ -280,6 +280,26 @@ ssize_t io_send(struct io *io, const struct iovec *iov, int iovcnt)
 	return ret;
 }
 
+ssize_t io_recv(struct io *io, const struct iovec *iov, int iovcnt)
+{
+	int fd;
+	ssize_t ret;
+
+	if (!io || !io->channel)
+		return -ENOTCONN;
+
+	fd = io_get_fd(io);
+
+	do {
+		ret = readv(fd, iov, iovcnt);
+	} while (ret < 0 && errno == EINTR);
+
+	if (ret < 0)
+		return -errno;
+
+	return ret;
+}
+
 bool io_shutdown(struct io *io)
 {
 	if (!io || !io->channel)
src/shared/io.h +1 -0
index 8bc1111d0..14f0034cf 100644
@@ -35,6 +35,7 @@ int io_get_fd(struct io *io);
 bool io_set_close_on_destroy(struct io *io, bool do_close);
 
 ssize_t io_send(struct io *io, const struct iovec *iov, int iovcnt);
+ssize_t io_recv(struct io *io, const struct iovec *iov, int iovcnt);
 bool io_shutdown(struct io *io);
 
 typedef bool (*io_callback_func_t)(struct io *io, void *user_data);
Signature · 1 line
2.13.6
author maintainer
#2 lore raw

RFC 2/7 shared: Add initial code for 6LoWPAN

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This introduces struct bt_6lo to interface with 6LoWPAN kernel driver.

---
 Makefile.am      |   2 +-
 src/shared/6lo.c | 493 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/shared/6lo.h |  40 +++++
 3 files changed, 534 insertions(+), 1 deletion(-)
 create mode 100644 src/shared/6lo.c
 create mode 100644 src/shared/6lo.h
Makefile.am +1 -1
index 8faabf44b..bb5a77408 100644
@@ -119,7 +119,7 @@ shared_sources = src/shared/io.h src/shared/timeout.h \
 			src/shared/gatt-server.h src/shared/gatt-server.c \
 			src/shared/gatt-db.h src/shared/gatt-db.c \
 			src/shared/gap.h src/shared/gap.c \
-			src/shared/tty.h
+			src/shared/tty.h src/shared/6lo.h src/shared/6lo.c
 
 src_libshared_glib_la_SOURCES = $(shared_sources) \
 				src/shared/io-glib.c \
src/shared/6lo.c new file +493 -0

493 lines not on this page · raw message

src/shared/6lo.h new file +40 -0
new file mode 100644
index 000000000..4dbe780b2
@@ -0,0 +1,40 @@
+/*
+ *
+ *  BlueZ - Bluetooth protocol stack for Linux
+ *
+ *  Copyright (C) 2017  Intel Corporation.
+ *
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2.1 of the License, or (at your option) any later version.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+struct bt_6lo;
+
+struct bt_6lo *bt_6lo_new_default(void);
+struct bt_6lo *bt_6lo_new(int fd);
+
+struct bt_6lo *bt_6lo_ref(struct bt_6lo *lo);
+void bt_6lo_unref(struct bt_6lo *lo);
+
+typedef void (*bt_6lo_destroy_func_t)(void *user_data);
+typedef void (*bt_6lo_debug_func_t)(const char *str, void *user_data);
+bool bt_6lo_set_debug(struct bt_6lo *lo, bt_6lo_debug_func_t callback,
+			void *user_data, bt_6lo_destroy_func_t destroy);
+
+int bt_6lo_add(struct bt_6lo *lo, const char *name, const uint8_t *addr);
+int bt_6lo_remove(struct bt_6lo *lo, const uint8_t *addr);
+
+int bt_6lo_attach(struct bt_6lo *lo, const uint8_t *src, int fd,
+						const uint8_t *dst);
Signature · 1 line
2.13.6
author maintainer
#3 lore raw

RFC 3/7 unit: Add initial test for bt_6lo

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

---
 Makefile.am     |   6 ++
 unit/test-6lo.c | 271 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 277 insertions(+)
 create mode 100644 unit/test-6lo.c
Makefile.am +6 -0
index bb5a77408..3ba1a8dab 100644
@@ -430,6 +430,12 @@ unit_test_midi_LDADD = src/libshared-glib.la \
 			@GLIB_LIBS@ @ALSA_LIBS@
 endif
 
+unit_tests += unit/test-6lo
+
+unit_test_6lo_SOURCES = unit/test-6lo.c
+unit_test_6lo_LDADD = src/libshared-glib.la \
+				lib/libbluetooth-internal.la @GLIB_LIBS@
+
 if MAINTAINER_MODE
 noinst_PROGRAMS += $(unit_tests)
 endif
unit/test-6lo.c new file +271 -0

271 lines not on this page · raw message

Signature · 1 line
2.13.6
author maintainer
#4 lore raw

RFC 4/7 uuid: Add IPSP UUID

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This adds IPSP UUID in string format.

---
 lib/uuid.h | 2 ++
 1 file changed, 2 insertions(+)
lib/uuid.h +2 -0
index 2c57a3378..71ed3df3b 100644
@@ -81,6 +81,8 @@ extern "C" {
 #define SENSOR_LOCATION_UUID	"00002a5d-0000-1000-8000-00805f9b34fb"
 #define SC_CONTROL_POINT_UUID	"00002a55-0000-1000-8000-00805f9b34fb"
 
+#define IPSP_UUID		"00001820-0000-1000-8000-00805f9b34fb"
+
 #define RFCOMM_UUID_STR		"00000003-0000-1000-8000-00805f9b34fb"
 
 #define HDP_UUID		"00001400-0000-1000-8000-00805f9b34fb"
Signature · 1 line
2.13.6
author maintainer
#5 lore raw

RFC 5/7 build: Add IPSP plugin

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This adds the initial plugin structure to handle IPSP profile.

---
 Makefile.plugins        |   5 ++
 configure.ac            |   4 ++
 profiles/network/ipsp.c | 132 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 141 insertions(+)
 create mode 100644 profiles/network/ipsp.c
Makefile.plugins +5 -0
index 73377e532..b5127b697 100644
@@ -53,6 +53,11 @@ builtin_sources += profiles/network/manager.c \
 			profiles/network/connection.c
 endif
 
+if IPSP
+builtin_modules += ipsp
+builtin_sources += profiles/network/ipsp.c
+endif
+
 if HID
 builtin_modules += input
 builtin_sources += profiles/input/manager.c \
configure.ac +4 -0
index 964101412..24489f7d2 100644
@@ -160,6 +160,10 @@ AC_ARG_ENABLE(network, AC_HELP_STRING([--disable-network],
 		[disable network profiles]), [enable_network=${enableval}])
 AM_CONDITIONAL(NETWORK, test "${enable_network}" != "no")
 
+AC_ARG_ENABLE(ipsp, AC_HELP_STRING([--disable-ipsp],
+		[disable ipsp profile]), [enable_ipsp=${enableval}])
+AM_CONDITIONAL(IPSP, test "${enable_ipsp}" != "no")
+
 AC_ARG_ENABLE(hid, AC_HELP_STRING([--disable-hid],
 		[disable HID profile]), [enable_hid=${enableval}])
 AM_CONDITIONAL(HID, test "${enable_hid}" != "no")
profiles/network/ipsp.c new file +132 -0

132 lines not on this page · raw message

Signature · 1 line
2.13.6
author maintainer
#6 lore raw

RFC 6/7 ipsp: Connect to IPSP PSM

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

In case the remote device has IPSP service connect attempt to the IPSP L2CAP PSM.

---
 profiles/network/ipsp.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 98 insertions(+), 3 deletions(-)
profiles/network/ipsp.c +98 -3
index 2645b0598..28683f4b6 100644
@@ -34,6 +34,7 @@
 #include "lib/sdp.h"
 #include "lib/uuid.h"
 
+#include "btio/btio.h"
 #include "src/shared/util.h"
 #include "src/shared/att.h"
 #include "src/shared/queue.h"
@@ -47,6 +48,28 @@
 #include "src/log.h"
 
 #define IPSP_UUID16 0x1820
+#define IPSP_PSM 0x0023
+#define IPSP_MTU 1280
+
+struct ipsp_session {
+	GIOChannel *io;
+	unsigned int id;
+};
+
+static void ipsp_cleanup(struct btd_service *service)
+{
+	struct ipsp_session *session = btd_service_get_user_data(service);
+
+	if (!session)
+		return;
+
+	btd_service_set_user_data(service, NULL);
+
+	if (session->id > 0)
+		g_source_remove(session->id);
+
+	g_io_channel_unref(session->io);
+}
 
 static int ipsp_probe(struct btd_service *service)
 {
@@ -64,6 +87,49 @@ static void ipsp_remove(struct btd_service *service)
 	const char *path = device_get_path(device);
 
 	DBG("path %s", path);
+
+	ipsp_cleanup(service);
+}
+
+static gboolean ipsp_session(GIOChannel *chan, GIOCondition cond,
+				gpointer data)
+{
+	struct btd_service *service = data;
+
+	if (cond & G_IO_NVAL)
+		return FALSE;
+
+	if (cond & (G_IO_HUP | G_IO_ERR)) {
+		DBG("IPSP session disconnected");
+		ipsp_cleanup(service);
+		return FALSE;
+	}
+
+	return TRUE;
+}
+
+static void ipsp_connect(GIOChannel *io, GError *err, gpointer user_data)
+{
+	struct btd_service *service = user_data;
+	struct ipsp_session *session = btd_service_get_user_data(service);
+
+	if (err) {
+		DBG("%s", err->message);
+
+		ipsp_cleanup(service);
+
+		btd_service_connecting_complete(service, -EIO);
+		return;
+	}
+
+	session->id = g_io_add_watch(io,
+				G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
+				(GIOFunc) ipsp_session, service);
+
+	g_io_channel_unref(session->io);
+	session->io = NULL;
+
+	btd_service_connecting_complete(service, 0);
 }
 
 static void foreach_ipsp_service(struct gatt_db_attribute *attr,
@@ -77,12 +143,13 @@ static void foreach_ipsp_service(struct gatt_db_attribute *attr,
 static int ipsp_accept(struct btd_service *service)
 {
 	struct btd_device *device = btd_service_get_device(service);
+	struct btd_adapter *adapter = device_get_adapter(device);
 	struct gatt_db *db = btd_device_get_gatt_db(device);
-	const char *path = device_get_path(device);
 	bool found = false;
 	bt_uuid_t ipsp_uuid;
+	struct ipsp_session *session;
 
-	DBG("path %s", path);
+	DBG("path %s", device_get_path(device));
 
 	/* Handle the IPSP services */
 	bt_uuid16_create(&ipsp_uuid, IPSP_UUID16);
@@ -92,7 +159,33 @@ static int ipsp_accept(struct btd_service *service)
 		return -EINVAL;
 	}
 
-	btd_service_connecting_complete(service, 0);
+	session = g_new0(struct ipsp_session, 1);
+	if (!session) {
+		error("Unable to allocate IPSP session");
+		return -ENOMEM;
+	}
+
+	/* Connect L2CAP channel */
+	session->io = bt_io_connect(ipsp_connect, service, NULL, NULL,
+				BT_IO_OPT_SOURCE_BDADDR,
+				btd_adapter_get_address(adapter),
+				BT_IO_OPT_SOURCE_TYPE,
+				btd_adapter_get_address_type(adapter),
+				BT_IO_OPT_DEST_BDADDR,
+				device_get_address(device),
+				BT_IO_OPT_DEST_TYPE,
+				btd_device_get_bdaddr_type(device),
+				BT_IO_OPT_PSM, IPSP_PSM,
+				BT_IO_OPT_IMTU, IPSP_MTU,
+				BT_IO_OPT_INVALID);
+	if (!session->io) {
+		error("Unable create IPSP connection");
+		g_free(session);
+		return -EIO;
+	}
+
+	/* Attach session to the service */
+	btd_service_set_user_data(service, session);
 
 	return 0;
 }
@@ -104,6 +197,8 @@ static int ipsp_disconnect(struct btd_service *service)
 
 	DBG("path %s", path);
 
+	ipsp_cleanup(service);
+
 	btd_service_disconnecting_complete(service, 0);
 
 	return 0;
Signature · 1 line
2.13.6
author maintainer
#7 lore raw

RFC 7/7 ipsp: Add support for bt_6lo

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This makes use of bt_6lo instance to create interfaces and attach channels to them.

---
 profiles/network/ipsp.c | 131 ++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 111 insertions(+), 20 deletions(-)
profiles/network/ipsp.c +111 -20
index 28683f4b6..f6bace956 100644
@@ -40,6 +40,7 @@
 #include "src/shared/queue.h"
 #include "src/shared/gatt-db.h"
 #include "src/shared/gatt-client.h"
+#include "src/shared/6lo.h"
 #include "src/plugin.h"
 #include "src/adapter.h"
 #include "src/device.h"
@@ -50,8 +51,12 @@
 #define IPSP_UUID16 0x1820
 #define IPSP_PSM 0x0023
 #define IPSP_MTU 1280
+#define IF_PREFIX "bt"
+
+static struct bt_6lo *lo;
 
 struct ipsp_session {
+	int ifindex;
 	GIOChannel *io;
 	unsigned int id;
 };
@@ -63,36 +68,49 @@ static void ipsp_cleanup(struct btd_service *service)
 	if (!session)
 		return;
 
-	btd_service_set_user_data(service, NULL);
-
-	if (session->id > 0)
+	if (session->id > 0) {
 		g_source_remove(session->id);
+		session->id = 0;
+	}
 
-	g_io_channel_unref(session->io);
+	if (session->io) {
+		g_io_channel_unref(session->io);
+		session->io = NULL;
+	}
 }
 
 static int ipsp_probe(struct btd_service *service)
 {
 	struct btd_device *device = btd_service_get_device(service);
 	const char *path = device_get_path(device);
+	struct ipsp_session *session;
 
 	DBG("path %s", path);
 
+	session = new0(struct ipsp_session, 1);
+
+	/* Attach session to the service */
+	btd_service_set_user_data(service, session);
+
 	return 0;
 }
 
 static void ipsp_remove(struct btd_service *service)
 {
 	struct btd_device *device = btd_service_get_device(service);
+	struct ipsp_session *session = btd_service_get_user_data(service);
 	const char *path = device_get_path(device);
 
 	DBG("path %s", path);
 
 	ipsp_cleanup(service);
+
+	btd_service_set_user_data(service, NULL);
+
+	g_free(session);
 }
 
-static gboolean ipsp_session(GIOChannel *chan, GIOCondition cond,
-				gpointer data)
+static gboolean ipsp_hup(GIOChannel *chan, GIOCondition cond, gpointer data)
 {
 	struct btd_service *service = data;
 
@@ -112,19 +130,38 @@ static void ipsp_connect(GIOChannel *io, GError *err, gpointer user_data)
 {
 	struct btd_service *service = user_data;
 	struct ipsp_session *session = btd_service_get_user_data(service);
+	GError *gerr = NULL;
+	bdaddr_t src, dst;
+	int fd;
 
 	if (err) {
 		DBG("%s", err->message);
-
 		ipsp_cleanup(service);
+		btd_service_connecting_complete(service, -EIO);
+		return;
+	}
+
+	bt_io_get(io, &gerr, BT_IO_OPT_SOURCE_BDADDR, &src,
+					BT_IO_OPT_DEST_BDADDR, &dst,
+					BT_IO_OPT_INVALID);
+	if (gerr) {
+		error("bt_io_get: %s", gerr->message);
+		g_error_free(gerr);
+		g_io_channel_unref(io);
+		return;
+	}
+
+	fd = g_io_channel_unix_get_fd(io);
 
+	if (bt_6lo_attach(lo, src.b, fd, dst.b) < 0) {
+		DBG("Unable to attach channel");
+		ipsp_cleanup(service);
 		btd_service_connecting_complete(service, -EIO);
 		return;
 	}
 
-	session->id = g_io_add_watch(io,
-				G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
-				(GIOFunc) ipsp_session, service);
+	session->id = g_io_add_watch(io, G_IO_ERR | G_IO_HUP | G_IO_NVAL,
+					(GIOFunc) ipsp_hup, service);
 
 	g_io_channel_unref(session->io);
 	session->io = NULL;
@@ -144,10 +181,10 @@ static int ipsp_accept(struct btd_service *service)
 {
 	struct btd_device *device = btd_service_get_device(service);
 	struct btd_adapter *adapter = device_get_adapter(device);
+	struct ipsp_session *session = btd_service_get_user_data(service);
 	struct gatt_db *db = btd_device_get_gatt_db(device);
 	bool found = false;
 	bt_uuid_t ipsp_uuid;
-	struct ipsp_session *session;
 
 	DBG("path %s", device_get_path(device));
 
@@ -159,11 +196,8 @@ static int ipsp_accept(struct btd_service *service)
 		return -EINVAL;
 	}
 
-	session = g_new0(struct ipsp_session, 1);
-	if (!session) {
-		error("Unable to allocate IPSP session");
-		return -ENOMEM;
-	}
+	if (session->io)
+		return -EINPROGRESS;
 
 	/* Connect L2CAP channel */
 	session->io = bt_io_connect(ipsp_connect, service, NULL, NULL,
@@ -184,9 +218,6 @@ static int ipsp_accept(struct btd_service *service)
 		return -EIO;
 	}
 
-	/* Attach session to the service */
-	btd_service_set_user_data(service, session);
-
 	return 0;
 }
 
@@ -213,14 +244,74 @@ static struct btd_profile ipsp_profile = {
 	.disconnect	= ipsp_disconnect,
 };
 
+static int net_6lo_probe(struct btd_adapter *adapter)
+{
+	uint16_t index = btd_adapter_get_index(adapter);
+	const bdaddr_t *addr = btd_adapter_get_address(adapter);
+	char *ifname;
+	int err;
+
+	DBG("");
+
+	ifname = g_strdup_printf("%s%u", IF_PREFIX, index);
+
+	err = bt_6lo_add(lo, ifname, addr->b);
+
+	g_free(ifname);
+
+	return err;
+}
+
+static void net_6lo_remove(struct btd_adapter *adapter)
+{
+	const bdaddr_t *addr = btd_adapter_get_address(adapter);
+
+	DBG("");
+
+	bt_6lo_remove(lo, addr->b);
+}
+
+static struct btd_adapter_driver net_6lo_driver = {
+	.name	= "network-6lo",
+	.probe	= net_6lo_probe,
+	.remove	= net_6lo_remove,
+};
+
+static void net_6lo_debug(const char *str, void *user_data)
+{
+	DBG("%s", str);
+}
+
 static int ipsp_init(void)
 {
-	return btd_profile_register(&ipsp_profile);
+	int err;
+
+	lo = bt_6lo_new_default();
+	if (!lo)
+		return -ENOTSUP;
+
+	bt_6lo_set_debug(lo, net_6lo_debug, NULL, NULL);
+
+	err = btd_register_adapter_driver(&net_6lo_driver);
+	if (err < 0) {
+		bt_6lo_unref(lo);
+		return err;
+	}
+
+	err = btd_profile_register(&ipsp_profile);
+	if (err < 0) {
+		btd_unregister_adapter_driver(&net_6lo_driver);
+		bt_6lo_unref(lo);
+	}
+
+	return err;
 }
 
 static void ipsp_exit(void)
 {
 	btd_profile_unregister(&ipsp_profile);
+	btd_unregister_adapter_driver(&net_6lo_driver);
+	bt_6lo_unref(lo);
 }
 
 BLUETOOTH_PLUGIN_DEFINE(ipsp, VERSION, BLUETOOTH_PLUGIN_PRIORITY_DEFAULT,
Signature · 1 line
2.13.6