summaryrefslogtreecommitdiffhomepage
path: root/ubus.c
blob: 72cf0e54737f8ff972cc9bf4f1448da63ee6192e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <string.h>

#include "netifd.h"
#include "interface.h"
#include "ubus.h"

static struct ubus_context *ctx = NULL;
static struct blob_buf b;

/* global object */

enum {
	DEV_NAME,
	DEV_FORCE,
	DEV_LAST,
};

static const struct blobmsg_policy dev_policy[] = {
	[DEV_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
	[DEV_FORCE] = { .name = "force", .type = BLOBMSG_TYPE_INT8 },
};

static int netifd_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
				struct ubus_request_data *req, const char *method,
				struct blob_attr *msg)
{
	struct device *dev;
	struct blob_attr *tb[DEV_LAST];
	bool add = !strncmp(method, "add", 3);

	blobmsg_parse(dev_policy, ARRAY_SIZE(dev_policy), tb, blob_data(msg), blob_len(msg));

	if (!tb[DEV_NAME])
		return UBUS_STATUS_INVALID_ARGUMENT;

	dev = get_device(blobmsg_data(tb[DEV_NAME]), false);
	if (!dev)
		return UBUS_STATUS_NOT_FOUND;

	if (!add || (tb[DEV_FORCE] && blobmsg_get_u8(tb[DEV_FORCE])))
		set_device_present(dev, add);
	else
		check_device_state(dev);

	return 0;
}

static struct ubus_method main_object_methods[] = {
	UBUS_METHOD("add_device", netifd_handle_device, dev_policy),
	UBUS_METHOD("del_device", netifd_handle_device, dev_policy),
};

static struct ubus_object_type main_object_type =
	UBUS_OBJECT_TYPE("netifd", main_object_methods);

static struct ubus_object main_object = {
	.name = "network.interface",
	.type = &main_object_type,
	.methods = main_object_methods,
	.n_methods = ARRAY_SIZE(main_object_methods),
};

int netifd_ubus_init(const char *path)
{
	int ret;

	ctx = ubus_connect(path);
	if (!ctx)
		return -EIO;

	DPRINTF("connected as %08x\n", ctx->local_id);
	uloop_init();
	ubus_add_uloop(ctx);

	ret = ubus_add_object(ctx, &main_object);
	if (ret != 0)
		fprintf(stderr, "Failed to publish object: %s\n", ubus_strerror(ret));

	return 0;
}

void netifd_ubus_done(void)
{
	ubus_free(ctx);
}


/* per-interface object */

static int netifd_handle_up(struct ubus_context *ctx, struct ubus_object *obj,
			    struct ubus_request_data *req, const char *method,
			    struct blob_attr *msg)
{
	struct interface *iface;

	iface = container_of(obj, struct interface, ubus);
	set_interface_up(iface);

	return 0;
}

static int netifd_handle_down(struct ubus_context *ctx, struct ubus_object *obj,
			      struct ubus_request_data *req, const char *method,
			      struct blob_attr *msg)
{
	struct interface *iface;

	iface = container_of(obj, struct interface, ubus);
	set_interface_down(iface);

	return 0;
}

static void netifd_add_interface_errors(struct blob_buf *b, struct interface *iface)
{
	struct interface_error *error;
	void *e, *e2, *e3;
	int i;

	e = blobmsg_open_array(b, "errors");
	list_for_each_entry(error, &iface->errors, list) {
		e2 = blobmsg_open_table(b, NULL);

		blobmsg_add_string(b, "subsystem", error->subsystem);
		blobmsg_add_string(b, "code", error->code);
		if (error->data[0]) {
			e3 = blobmsg_open_array(b, "data");
			for (i = 0; error->data[i]; i++)
				blobmsg_add_string(b, NULL, error->data[i]);
			blobmsg_close_array(b, e3);
		}

		blobmsg_close_table(b, e2);
	}
	blobmsg_close_array(b, e);
}

static int netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
				struct ubus_request_data *req, const char *method,
				struct blob_attr *msg)
{
	static const char *iface_state[] = {
		[IFS_SETUP] = "setup",
		[IFS_UP] = "up",
		[IFS_TEARDOWN] = "teardown",
		[IFS_DOWN] = "down",
	};
	struct interface *iface;

	iface = container_of(obj, struct interface, ubus);

	blob_buf_init(&b, 0);
	blobmsg_add_string(&b, "state", iface_state[iface->state]);
	blobmsg_add_u8(&b, "active", iface->active);
	blobmsg_add_u8(&b, "autostart", iface->autostart);
	if (iface->main_dev.dev) {
		struct device *dev = iface->main_dev.dev;
		const char *field;
		void *devinfo;

		/* use a different field for virtual devices */
		if (dev->avl.key)
			field = "device";
		else
			field = "link";

		devinfo = blobmsg_open_table(&b, field);
		blobmsg_add_string(&b, "name", dev->ifname);

		if (dev->type->dump_status)
			dev->type->dump_status(dev, &b);

		blobmsg_close_table(&b, devinfo);
	}

	if (!list_is_empty(&iface->errors))
		netifd_add_interface_errors(&b, iface);

	ubus_send_reply(ctx, req, b.head);

	return 0;
}


static struct ubus_method iface_object_methods[] = {
	{ .name = "up", .handler = netifd_handle_up },
	{ .name = "down", .handler = netifd_handle_down },
	{ .name = "status", .handler = netifd_handle_status },
};

static struct ubus_object_type iface_object_type =
	UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);


void netifd_ubus_add_interface(struct interface *iface)
{
	struct ubus_object *obj = &iface->ubus;
	char *name;

	name = malloc(strlen(main_object.name) + strlen(iface->name) + 2);
	if (!name)
		return;

	sprintf(name, "%s.%s", main_object.name, iface->name);
	obj->name = name;
	obj->type = &iface_object_type;
	obj->methods = iface_object_methods;
	obj->n_methods = ARRAY_SIZE(iface_object_methods);
	if (ubus_add_object(ctx, &iface->ubus)) {
		DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
		free(name);
		obj->name = NULL;
	}
}

void netifd_ubus_remove_interface(struct interface *iface)
{
	if (!iface->ubus.name)
		return;

	ubus_remove_object(ctx, &iface->ubus);
	free((void *) iface->ubus.name);
}