summaryrefslogtreecommitdiff
path: root/ubus.c
blob: 2c4e4e6823ad6d8c009cf9c8c79dfb8bc9d9b40d (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#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_MAX,
	__DEV_MAX_NOFORCE = __DEV_MAX - 1,
};

static const struct blobmsg_policy dev_policy[__DEV_MAX] = {
	[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_MAX];
	bool add = !strncmp(method, "add", 3);

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

	if (!tb[DEV_NAME])
		return UBUS_STATUS_INVALID_ARGUMENT;

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

	if (!add || (tb[DEV_FORCE] && blobmsg_get_u8(tb[DEV_FORCE])))
		device_set_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("remove_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);
	interface_set_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);
	interface_set_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 int
netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
			   struct ubus_request_data *req, const char *method,
			   struct blob_attr *msg)
{
	struct interface *iface;
	struct device *dev, *main_dev;
	struct blob_attr *tb[__DEV_MAX];
	bool add = !strncmp(method, "add", 3);
	int ret;

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

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

	if (!tb[DEV_NAME])
		return UBUS_STATUS_INVALID_ARGUMENT;

	main_dev = iface->main_dev.dev;
	if (!main_dev)
		return UBUS_STATUS_NOT_FOUND;

	if (!main_dev->hotplug_ops)
		return UBUS_STATUS_NOT_SUPPORTED;

	dev = device_get(blobmsg_data(tb[DEV_NAME]), add);
	if (!dev)
		return UBUS_STATUS_NOT_FOUND;

	if (main_dev != dev) {
		if (add)
			ret = main_dev->hotplug_ops->add(main_dev, dev);
		else
			ret = main_dev->hotplug_ops->del(main_dev, dev);
		if (ret)
			ret = UBUS_STATUS_UNKNOWN_ERROR;
	} else {
		ret = UBUS_STATUS_INVALID_ARGUMENT;
	}

	if (add)
		device_free_unused(dev);

	return ret;
}


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 },
	{ .name = "add_device", .handler = netifd_iface_handle_device,
	  .policy = dev_policy, .n_policy = __DEV_MAX_NOFORCE },
	{ .name = "remove_device", .handler = netifd_iface_handle_device,
	  .policy = dev_policy, .n_policy = __DEV_MAX_NOFORCE },
};

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);
}