Skip to content

Commit

Permalink
babeld: add remove_interface function
Browse files Browse the repository at this point in the history
Add the ubus function to remove an interface from babeld:
    ubus call babeld remove_interface '{"ifname":"eth0"}'

Signed-off-by: Nick Hainke <[email protected]>
  • Loading branch information
PolynomialDivision committed Feb 1, 2022
1 parent 879725c commit 31d3611
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
From 2f75f3d7073bad9b4b0c031aa792013fd6b61c19 Mon Sep 17 00:00:00 2001
From: Polynomialdivision <[email protected]>
Date: Tue, 1 Feb 2022 22:37:29 +0100
Subject: [PATCH] interface/local: add remove_interface function

Babeld can add interfaces dynamically. However, babeld is missing a
function to remove interfaces and stop meshing on them. Add a function
that can do it.

Signed-off-by: Nick Hainke <[email protected]>
---
interface.c | 33 +++++++++++++++++++++++++++++++++
interface.h | 1 +
local.h | 1 +
3 files changed, 35 insertions(+)

--- a/interface.c
+++ b/interface.c
@@ -94,6 +94,39 @@ add_interface(char *ifname, struct inter
return ifp;
}

+void
+remove_interface(char *ifname)
+{
+ struct interface *tmp_ifp, *ifp = NULL;
+
+ if(interfaces == NULL)
+ return;
+
+ if(strcmp(interfaces->name, ifname) == 0) {
+ tmp_ifp = interfaces;
+ interface_updown(tmp_ifp, 0);
+ interfaces = tmp_ifp->next;
+ free(tmp_ifp);
+ return;
+ }
+
+ FOR_ALL_INTERFACES(ifp) {
+ if(ifp->next == NULL)
+ break;
+ if(strcmp(ifp->next->name, ifname) == 0) {
+ break;
+ }
+ }
+
+ if(ifp->next == NULL)
+ return;
+
+ tmp_ifp = ifp->next;
+ interface_updown(tmp_ifp, 0);
+ ifp->next = tmp_ifp->next;
+ free(tmp_ifp);
+}
+
int
flush_interface(char *ifname)
{
--- a/interface.h
+++ b/interface.h
@@ -147,6 +147,7 @@ if_up(struct interface *ifp)
}

struct interface *add_interface(char *ifname, struct interface_conf *if_conf);
+void remove_interface(char *ifname);
int flush_interface(char *ifname);
unsigned jitter(struct buffered *buf, int urgent);
unsigned update_jitter(struct interface *ifp, int urgent);
--- a/local.h
+++ b/local.h
@@ -27,6 +27,7 @@ struct xroute;
#define LOCAL_FLUSH 0
#define LOCAL_ADD 1
#define LOCAL_CHANGE 2
+#define LOCAL_REMOVE 3

#ifndef MAX_LOCAL_SOCKETS
#define MAX_LOCAL_SOCKETS 4
26 changes: 26 additions & 0 deletions babeld/src/ubus.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,30 @@ static int babeld_ubus_add_interface(struct ubus_context *ctx_local,
return UBUS_STATUS_OK;
}

// Remove an inteface (ubus equivalent to "interface"-function).
static int babeld_ubus_remove_interface(struct ubus_context *ctx_local,
struct ubus_object *obj,
struct ubus_request_data *req,
const char *method,
struct blob_attr *msg) {
struct blob_attr *tb[__INTERFACE_MAX];
struct blob_buf b = {0};
struct interface *ifp = NULL;
char *ifname;

blobmsg_parse(interface_policy, __INTERFACE_MAX, tb, blob_data(msg),
blob_len(msg));

if (!tb[INTERFACE_IFNAME])
return UBUS_STATUS_INVALID_ARGUMENT;

ifname = blobmsg_get_string(tb[INTERFACE_IFNAME]);

remove_interface(ifname);

return UBUS_STATUS_OK;
}

// Sends a babel info message on ubus socket.
static int babeld_ubus_babeld_info(struct ubus_context *ctx_local,
struct ubus_object *obj,
Expand Down Expand Up @@ -364,6 +388,8 @@ static int babeld_ubus_get_neighbours(struct ubus_context *ctx_local,
// List of functions we expose via the ubus bus.
static const struct ubus_method babeld_methods[] = {
UBUS_METHOD("add_interface", babeld_ubus_add_interface, interface_policy),
UBUS_METHOD("remove_interface", babeld_ubus_remove_interface,
interface_policy),
UBUS_METHOD_NOARG("get_info", babeld_ubus_babeld_info),
UBUS_METHOD_NOARG("get_xroutes", babeld_ubus_get_xroutes),
UBUS_METHOD_NOARG("get_routes", babeld_ubus_get_routes),
Expand Down

0 comments on commit 31d3611

Please sign in to comment.