fix device_lock/device_unlock imbalances
[project/netifd.git] / ubus.c
1 /*
2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14 #define _GNU_SOURCE
15
16 #include <arpa/inet.h>
17 #include <string.h>
18 #include <stdio.h>
19
20 #include "netifd.h"
21 #include "interface.h"
22 #include "proto.h"
23 #include "ubus.h"
24 #include "system.h"
25
26 static struct ubus_context *ctx = NULL;
27 static struct blob_buf b;
28 static struct netifd_fd ubus_fd;
29 static const char *ubus_path;
30
31 /* global object */
32
33 static int
34 netifd_handle_restart(struct ubus_context *ctx, struct ubus_object *obj,
35 struct ubus_request_data *req, const char *method,
36 struct blob_attr *msg)
37 {
38 netifd_restart();
39 return 0;
40 }
41
42 static int
43 netifd_handle_reload(struct ubus_context *ctx, struct ubus_object *obj,
44 struct ubus_request_data *req, const char *method,
45 struct blob_attr *msg)
46 {
47 netifd_reload();
48 return 0;
49 }
50
51 enum {
52 HR_TARGET,
53 HR_V6,
54 __HR_MAX
55 };
56
57 static const struct blobmsg_policy route_policy[__HR_MAX] = {
58 [HR_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
59 [HR_V6] = { .name = "v6", .type = BLOBMSG_TYPE_BOOL },
60 };
61
62 static int
63 netifd_add_host_route(struct ubus_context *ctx, struct ubus_object *obj,
64 struct ubus_request_data *req, const char *method,
65 struct blob_attr *msg)
66 {
67 struct blob_attr *tb[__HR_MAX];
68 struct interface *iface;
69 union if_addr a;
70 bool v6 = false;
71
72 blobmsg_parse(route_policy, __HR_MAX, tb, blob_data(msg), blob_len(msg));
73 if (!tb[HR_TARGET])
74 return UBUS_STATUS_INVALID_ARGUMENT;
75
76 if (tb[HR_V6])
77 v6 = blobmsg_get_bool(tb[HR_V6]);
78
79 memset(&a, 0, sizeof(a));
80 if (!inet_pton(v6 ? AF_INET6 : AF_INET, blobmsg_data(tb[HR_TARGET]), &a))
81 return UBUS_STATUS_INVALID_ARGUMENT;
82
83
84 iface = interface_ip_add_target_route(&a, v6);
85 if (!iface)
86 return UBUS_STATUS_NOT_FOUND;
87
88 blob_buf_init(&b, 0);
89 blobmsg_add_string(&b, "interface", iface->name);
90 ubus_send_reply(ctx, req, b.head);
91
92 return 0;
93 }
94
95 static int
96 netifd_get_proto_handlers(struct ubus_context *ctx, struct ubus_object *obj,
97 struct ubus_request_data *req, const char *method,
98 struct blob_attr *msg)
99 {
100 blob_buf_init(&b, 0);
101 proto_dump_handlers(&b);
102 ubus_send_reply(ctx, req, b.head);
103
104 return 0;
105 }
106
107 static struct ubus_method main_object_methods[] = {
108 { .name = "restart", .handler = netifd_handle_restart },
109 { .name = "reload", .handler = netifd_handle_reload },
110 UBUS_METHOD("add_host_route", netifd_add_host_route, route_policy),
111 { .name = "get_proto_handlers", .handler = netifd_get_proto_handlers },
112 };
113
114 static struct ubus_object_type main_object_type =
115 UBUS_OBJECT_TYPE("netifd", main_object_methods);
116
117 static struct ubus_object main_object = {
118 .name = "network",
119 .type = &main_object_type,
120 .methods = main_object_methods,
121 .n_methods = ARRAY_SIZE(main_object_methods),
122 };
123
124 enum {
125 DEV_NAME,
126 __DEV_MAX,
127 };
128
129 static const struct blobmsg_policy dev_policy[__DEV_MAX] = {
130 [DEV_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
131 };
132
133 static int
134 netifd_dev_status(struct ubus_context *ctx, struct ubus_object *obj,
135 struct ubus_request_data *req, const char *method,
136 struct blob_attr *msg)
137 {
138 struct device *dev = NULL;
139 struct blob_attr *tb[__DEV_MAX];
140
141 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
142
143 if (tb[DEV_NAME]) {
144 dev = device_get(blobmsg_data(tb[DEV_NAME]), false);
145 if (!dev)
146 return UBUS_STATUS_INVALID_ARGUMENT;
147 }
148
149 blob_buf_init(&b, 0);
150 device_dump_status(&b, dev);
151 ubus_send_reply(ctx, req, b.head);
152
153 return 0;
154 }
155
156 enum {
157 ALIAS_ATTR_ALIAS,
158 ALIAS_ATTR_DEV,
159 __ALIAS_ATTR_MAX,
160 };
161
162 static const struct blobmsg_policy alias_attrs[__ALIAS_ATTR_MAX] = {
163 [ALIAS_ATTR_ALIAS] = { "alias", BLOBMSG_TYPE_ARRAY },
164 [ALIAS_ATTR_DEV] = { "device", BLOBMSG_TYPE_STRING },
165 };
166
167 static int
168 netifd_handle_alias(struct ubus_context *ctx, struct ubus_object *obj,
169 struct ubus_request_data *req, const char *method,
170 struct blob_attr *msg)
171 {
172 struct device *dev = NULL;
173 struct blob_attr *tb[__ALIAS_ATTR_MAX];
174 struct blob_attr *cur;
175 int rem;
176
177 blobmsg_parse(alias_attrs, __ALIAS_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
178
179 if (!tb[ALIAS_ATTR_ALIAS])
180 return UBUS_STATUS_INVALID_ARGUMENT;
181
182 if ((cur = tb[ALIAS_ATTR_DEV]) != NULL) {
183 dev = device_get(blobmsg_data(cur), true);
184 if (!dev)
185 return UBUS_STATUS_NOT_FOUND;
186 }
187
188 blobmsg_for_each_attr(cur, tb[ALIAS_ATTR_ALIAS], rem) {
189 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
190 goto error;
191
192 if (!blobmsg_check_attr(cur, NULL))
193 goto error;
194
195 alias_notify_device(blobmsg_data(cur), dev);
196 }
197 return 0;
198
199 error:
200 device_free_unused(dev);
201 return UBUS_STATUS_INVALID_ARGUMENT;
202 }
203
204 enum {
205 DEV_STATE_NAME,
206 DEV_STATE_DEFER,
207 __DEV_STATE_MAX,
208 };
209
210 static const struct blobmsg_policy dev_state_policy[__DEV_STATE_MAX] = {
211 [DEV_STATE_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
212 [DEV_STATE_DEFER] = { .name = "defer", .type = BLOBMSG_TYPE_BOOL },
213 };
214
215 static int
216 netifd_handle_set_state(struct ubus_context *ctx, struct ubus_object *obj,
217 struct ubus_request_data *req, const char *method,
218 struct blob_attr *msg)
219 {
220 struct device *dev = NULL;
221 struct blob_attr *tb[__DEV_STATE_MAX];
222 struct blob_attr *cur;
223
224 blobmsg_parse(dev_state_policy, __DEV_STATE_MAX, tb, blob_data(msg), blob_len(msg));
225
226 cur = tb[DEV_STATE_NAME];
227 if (!cur)
228 return UBUS_STATUS_INVALID_ARGUMENT;
229
230 dev = device_get(blobmsg_data(cur), false);
231 if (!dev)
232 return UBUS_STATUS_NOT_FOUND;
233
234 cur = tb[DEV_STATE_DEFER];
235 if (cur)
236 device_set_deferred(dev, !!blobmsg_get_u8(cur));
237
238 return 0;
239 }
240
241 static struct ubus_method dev_object_methods[] = {
242 UBUS_METHOD("status", netifd_dev_status, dev_policy),
243 UBUS_METHOD("set_alias", netifd_handle_alias, alias_attrs),
244 UBUS_METHOD("set_state", netifd_handle_set_state, dev_state_policy),
245 };
246
247 static struct ubus_object_type dev_object_type =
248 UBUS_OBJECT_TYPE("device", dev_object_methods);
249
250 static struct ubus_object dev_object = {
251 .name = "network.device",
252 .type = &dev_object_type,
253 .methods = dev_object_methods,
254 .n_methods = ARRAY_SIZE(dev_object_methods),
255 };
256
257 static void
258 netifd_ubus_add_fd(void)
259 {
260 ubus_add_uloop(ctx);
261 ubus_fd.fd = ctx->sock.fd;
262 netifd_fd_add(&ubus_fd);
263 }
264
265 static void
266 netifd_ubus_reconnect_timer(struct uloop_timeout *timeout)
267 {
268 static struct uloop_timeout retry = {
269 .cb = netifd_ubus_reconnect_timer,
270 };
271 int t = 2;
272
273 if (ubus_reconnect(ctx, ubus_path) != 0) {
274 DPRINTF("failed to reconnect, trying again in %d seconds\n", t);
275 uloop_timeout_set(&retry, t * 1000);
276 return;
277 }
278
279 DPRINTF("reconnected to ubus, new id: %08x\n", ctx->local_id);
280 netifd_ubus_add_fd();
281 }
282
283 static void
284 netifd_ubus_connection_lost(struct ubus_context *ctx)
285 {
286 netifd_fd_delete(&ubus_fd);
287 netifd_ubus_reconnect_timer(NULL);
288 }
289
290 int
291 netifd_ubus_init(const char *path)
292 {
293 int ret;
294
295 uloop_init();
296 ubus_path = path;
297
298 ctx = ubus_connect(path);
299 if (!ctx)
300 return -EIO;
301
302 DPRINTF("connected as %08x\n", ctx->local_id);
303 ctx->connection_lost = netifd_ubus_connection_lost;
304 netifd_ubus_add_fd();
305
306 ret = ubus_add_object(ctx, &main_object);
307 if (ret)
308 goto out;
309
310 ret = ubus_add_object(ctx, &dev_object);
311
312 out:
313 if (ret != 0)
314 fprintf(stderr, "Failed to publish object: %s\n", ubus_strerror(ret));
315 return ret;
316 }
317
318 void
319 netifd_ubus_done(void)
320 {
321 ubus_free(ctx);
322 }
323
324
325 /* per-interface object */
326
327 static int
328 netifd_handle_up(struct ubus_context *ctx, struct ubus_object *obj,
329 struct ubus_request_data *req, const char *method,
330 struct blob_attr *msg)
331 {
332 struct interface *iface;
333
334 iface = container_of(obj, struct interface, ubus);
335 interface_set_up(iface);
336
337 return 0;
338 }
339
340 static int
341 netifd_handle_down(struct ubus_context *ctx, struct ubus_object *obj,
342 struct ubus_request_data *req, const char *method,
343 struct blob_attr *msg)
344 {
345 struct interface *iface;
346
347 iface = container_of(obj, struct interface, ubus);
348 interface_set_down(iface);
349
350 return 0;
351 }
352
353 static void
354 netifd_add_interface_errors(struct blob_buf *b, struct interface *iface)
355 {
356 struct interface_error *error;
357 void *e, *e2, *e3;
358 int i;
359
360 e = blobmsg_open_array(b, "errors");
361 list_for_each_entry(error, &iface->errors, list) {
362 e2 = blobmsg_open_table(b, NULL);
363
364 blobmsg_add_string(b, "subsystem", error->subsystem);
365 blobmsg_add_string(b, "code", error->code);
366 if (error->data[0]) {
367 e3 = blobmsg_open_array(b, "data");
368 for (i = 0; error->data[i]; i++)
369 blobmsg_add_string(b, NULL, error->data[i]);
370 blobmsg_close_array(b, e3);
371 }
372
373 blobmsg_close_table(b, e2);
374 }
375 blobmsg_close_array(b, e);
376 }
377
378 static void
379 interface_ip_dump_address_list(struct interface_ip_settings *ip, bool v6,
380 bool enabled)
381 {
382 struct device_addr *addr;
383 char *buf;
384 void *a;
385 int buflen = 128;
386 int af;
387
388 vlist_for_each_element(&ip->addr, addr, node) {
389 if (addr->enabled != enabled)
390 continue;
391
392 if ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
393 af = AF_INET;
394 else
395 af = AF_INET6;
396
397 if (af != (v6 ? AF_INET6 : AF_INET))
398 continue;
399
400 a = blobmsg_open_table(&b, NULL);
401
402 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
403 inet_ntop(af, &addr->addr, buf, buflen);
404 blobmsg_add_string_buffer(&b);
405
406 blobmsg_add_u32(&b, "mask", addr->mask);
407
408 blobmsg_close_table(&b, a);
409 }
410 }
411
412 static void
413 interface_ip_dump_route_list(struct interface_ip_settings *ip, bool enabled)
414 {
415 struct device_route *route;
416 int buflen = 128;
417 char *buf;
418 void *r;
419 int af;
420
421 vlist_for_each_element(&ip->route, route, node) {
422 if (route->enabled != enabled)
423 continue;
424
425 if ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
426 af = AF_INET;
427 else
428 af = AF_INET6;
429
430 r = blobmsg_open_table(&b, NULL);
431
432 buf = blobmsg_alloc_string_buffer(&b, "target", buflen);
433 inet_ntop(af, &route->addr, buf, buflen);
434 blobmsg_add_string_buffer(&b);
435
436 blobmsg_add_u32(&b, "mask", route->mask);
437
438 buf = blobmsg_alloc_string_buffer(&b, "nexthop", buflen);
439 inet_ntop(af, &route->nexthop, buf, buflen);
440 blobmsg_add_string_buffer(&b);
441
442 if (route->flags & DEVROUTE_MTU)
443 blobmsg_add_u32(&b, "mtu", route->mtu);
444
445 if (route->flags & DEVROUTE_METRIC)
446 blobmsg_add_u32(&b, "metric", route->metric);
447
448 blobmsg_close_table(&b, r);
449 }
450 }
451
452 static void
453 interface_ip_dump_dns_server_list(struct interface_ip_settings *ip,
454 bool enabled)
455 {
456 struct dns_server *dns;
457 int buflen = 128;
458 char *buf;
459
460 vlist_simple_for_each_element(&ip->dns_servers, dns, node) {
461 if (ip->no_dns == enabled)
462 continue;
463
464 buf = blobmsg_alloc_string_buffer(&b, NULL, buflen);
465 inet_ntop(dns->af, &dns->addr, buf, buflen);
466 blobmsg_add_string_buffer(&b);
467 }
468 }
469
470 static void
471 interface_ip_dump_dns_search_list(struct interface_ip_settings *ip,
472 bool enabled)
473 {
474 struct dns_search_domain *dns;
475
476 vlist_simple_for_each_element(&ip->dns_search, dns, node) {
477 if (ip->no_dns == enabled)
478 continue;
479
480 blobmsg_add_string(&b, NULL, dns->name);
481 }
482 }
483
484 static int
485 netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
486 struct ubus_request_data *req, const char *method,
487 struct blob_attr *msg)
488 {
489 struct interface *iface;
490 struct interface_data *data;
491 struct device *dev;
492 void *a, *inactive;
493
494 iface = container_of(obj, struct interface, ubus);
495
496 blob_buf_init(&b, 0);
497 blobmsg_add_u8(&b, "up", iface->state == IFS_UP);
498 blobmsg_add_u8(&b, "pending", iface->state == IFS_SETUP);
499 blobmsg_add_u8(&b, "available", iface->available);
500 blobmsg_add_u8(&b, "autostart", iface->autostart);
501
502 if (iface->state == IFS_UP) {
503 time_t cur = system_get_rtime();
504 blobmsg_add_u32(&b, "uptime", cur - iface->start_time);
505 blobmsg_add_string(&b, "l3_device", iface->l3_dev.dev->ifname);
506 }
507
508 if (iface->proto_handler)
509 blobmsg_add_string(&b, "proto", iface->proto_handler->name);
510
511 dev = iface->main_dev.dev;
512 if (dev && !dev->hidden &&
513 !(iface->proto_handler->flags & PROTO_FLAG_NODEV))
514 blobmsg_add_string(&b, "device", dev->ifname);
515
516 if (iface->state == IFS_UP) {
517 blobmsg_add_u32(&b, "metric", iface->metric);
518 a = blobmsg_open_array(&b, "ipv4-address");
519 interface_ip_dump_address_list(&iface->config_ip, false, true);
520 interface_ip_dump_address_list(&iface->proto_ip, false, true);
521 blobmsg_close_array(&b, a);
522 a = blobmsg_open_array(&b, "ipv6-address");
523 interface_ip_dump_address_list(&iface->config_ip, true, true);
524 interface_ip_dump_address_list(&iface->proto_ip, true, true);
525 blobmsg_close_array(&b, a);
526 a = blobmsg_open_array(&b, "route");
527 interface_ip_dump_route_list(&iface->config_ip, true);
528 interface_ip_dump_route_list(&iface->proto_ip, true);
529 blobmsg_close_array(&b, a);
530 a = blobmsg_open_array(&b, "dns-server");
531 interface_ip_dump_dns_server_list(&iface->config_ip, true);
532 interface_ip_dump_dns_server_list(&iface->proto_ip, true);
533 blobmsg_close_array(&b, a);
534 a = blobmsg_open_array(&b, "dns-search");
535 interface_ip_dump_dns_search_list(&iface->config_ip, true);
536 interface_ip_dump_dns_search_list(&iface->proto_ip, true);
537 blobmsg_close_array(&b, a);
538
539 inactive = blobmsg_open_table(&b, "inactive");
540 a = blobmsg_open_array(&b, "ipv4-address");
541 interface_ip_dump_address_list(&iface->config_ip, false, false);
542 interface_ip_dump_address_list(&iface->proto_ip, false, false);
543 blobmsg_close_array(&b, a);
544 a = blobmsg_open_array(&b, "ipv6-address");
545 interface_ip_dump_address_list(&iface->config_ip, true, false);
546 interface_ip_dump_address_list(&iface->proto_ip, true, false);
547 blobmsg_close_array(&b, a);
548 a = blobmsg_open_array(&b, "route");
549 interface_ip_dump_route_list(&iface->config_ip, false);
550 interface_ip_dump_route_list(&iface->proto_ip, false);
551 blobmsg_close_array(&b, a);
552 a = blobmsg_open_array(&b, "dns-server");
553 interface_ip_dump_dns_server_list(&iface->config_ip, false);
554 interface_ip_dump_dns_server_list(&iface->proto_ip, false);
555 blobmsg_close_array(&b, a);
556 a = blobmsg_open_array(&b, "dns-search");
557 interface_ip_dump_dns_search_list(&iface->config_ip, false);
558 interface_ip_dump_dns_search_list(&iface->proto_ip, false);
559 blobmsg_close_array(&b, a);
560 blobmsg_close_table(&b, inactive);
561 }
562
563 a = blobmsg_open_table(&b, "data");
564 avl_for_each_element(&iface->data, data, node)
565 blob_put(&b, blob_id(data->data), blob_data(data->data), blob_len(data->data));
566
567 blobmsg_close_table(&b, a);
568
569 if (!list_is_empty(&iface->errors))
570 netifd_add_interface_errors(&b, iface);
571
572 ubus_send_reply(ctx, req, b.head);
573
574 return 0;
575 }
576
577 static int
578 netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
579 struct ubus_request_data *req, const char *method,
580 struct blob_attr *msg)
581 {
582 struct blob_attr *tb[__DEV_MAX];
583 struct interface *iface;
584 struct device *dev;
585 bool add = !strncmp(method, "add", 3);
586 int ret;
587
588 iface = container_of(obj, struct interface, ubus);
589
590 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
591
592 if (!tb[DEV_NAME])
593 return UBUS_STATUS_INVALID_ARGUMENT;
594
595 device_lock();
596
597 dev = device_get(blobmsg_data(tb[DEV_NAME]), add ? 2 : 0);
598 if (add && !dev) {
599 ret = UBUS_STATUS_NOT_FOUND;
600 goto out;
601 }
602
603 if (add) {
604 device_set_present(dev, true);
605 if (iface->device_config)
606 device_set_config(dev, &simple_device_type, iface->config);
607
608 system_if_apply_settings(dev, &dev->settings);
609 ret = interface_add_link(iface, dev);
610 } else {
611 ret = interface_remove_link(iface, dev);
612 }
613
614 out:
615 device_unlock();
616
617 return ret;
618 }
619
620
621 static int
622 netifd_iface_notify_proto(struct ubus_context *ctx, struct ubus_object *obj,
623 struct ubus_request_data *req, const char *method,
624 struct blob_attr *msg)
625 {
626 struct interface *iface;
627
628 iface = container_of(obj, struct interface, ubus);
629
630 if (!iface->proto || !iface->proto->notify)
631 return UBUS_STATUS_NOT_SUPPORTED;
632
633 return iface->proto->notify(iface->proto, msg);
634 }
635
636 static void
637 netifd_iface_do_remove(struct uloop_timeout *timeout)
638 {
639 struct interface *iface;
640
641 iface = container_of(timeout, struct interface, remove_timer);
642 vlist_delete(&interfaces, &iface->node);
643 }
644
645 static int
646 netifd_iface_remove(struct ubus_context *ctx, struct ubus_object *obj,
647 struct ubus_request_data *req, const char *method,
648 struct blob_attr *msg)
649 {
650 struct interface *iface;
651
652 iface = container_of(obj, struct interface, ubus);
653 if (iface->remove_timer.cb)
654 return UBUS_STATUS_INVALID_ARGUMENT;
655
656 iface->remove_timer.cb = netifd_iface_do_remove;
657 uloop_timeout_set(&iface->remove_timer, 100);
658 return 0;
659 }
660
661 static int
662 netifd_handle_iface_prepare(struct ubus_context *ctx, struct ubus_object *obj,
663 struct ubus_request_data *req, const char *method,
664 struct blob_attr *msg)
665 {
666 struct interface *iface;
667 struct device *dev;
668 const struct device_hotplug_ops *ops;
669
670 iface = container_of(obj, struct interface, ubus);
671 dev = iface->main_dev.dev;
672 if (!dev)
673 return 0;
674
675 ops = dev->hotplug_ops;
676 if (!ops)
677 return 0;
678
679 return ops->prepare(dev);
680 }
681
682 static int
683 netifd_handle_set_data(struct ubus_context *ctx, struct ubus_object *obj,
684 struct ubus_request_data *req, const char *method,
685 struct blob_attr *msg)
686 {
687 struct interface *iface;
688 struct blob_attr *cur;
689 int rem, ret;
690
691 iface = container_of(obj, struct interface, ubus);
692
693 blob_for_each_attr(cur, msg, rem) {
694 ret = interface_add_data(iface, cur);
695 if (ret)
696 return ret;
697 }
698
699 return 0;
700 }
701
702 static struct ubus_method iface_object_methods[] = {
703 { .name = "up", .handler = netifd_handle_up },
704 { .name = "down", .handler = netifd_handle_down },
705 { .name = "status", .handler = netifd_handle_status },
706 { .name = "prepare", .handler = netifd_handle_iface_prepare },
707 UBUS_METHOD("add_device", netifd_iface_handle_device, dev_policy ),
708 UBUS_METHOD("remove_device", netifd_iface_handle_device, dev_policy ),
709 { .name = "notify_proto", .handler = netifd_iface_notify_proto },
710 { .name = "remove", .handler = netifd_iface_remove },
711 { .name = "set_data", .handler = netifd_handle_set_data },
712 };
713
714 static struct ubus_object_type iface_object_type =
715 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
716
717
718 void
719 netifd_ubus_interface_event(struct interface *iface, bool up)
720 {
721 blob_buf_init(&b, 0);
722 blobmsg_add_string(&b, "action", up ? "ifup" : "ifdown");
723 blobmsg_add_string(&b, "interface", iface->name);
724 ubus_send_event(ctx, "network.interface", b.head);
725 }
726
727 void
728 netifd_ubus_add_interface(struct interface *iface)
729 {
730 struct ubus_object *obj = &iface->ubus;
731 char *name = NULL;
732
733 asprintf(&name, "%s.interface.%s", main_object.name, iface->name);
734 if (!name)
735 return;
736
737 obj->name = name;
738 obj->type = &iface_object_type;
739 obj->methods = iface_object_methods;
740 obj->n_methods = ARRAY_SIZE(iface_object_methods);
741 if (ubus_add_object(ctx, &iface->ubus)) {
742 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
743 free(name);
744 obj->name = NULL;
745 }
746 }
747
748 void
749 netifd_ubus_remove_interface(struct interface *iface)
750 {
751 if (!iface->ubus.name)
752 return;
753
754 ubus_remove_object(ctx, &iface->ubus);
755 free((void *) iface->ubus.name);
756 }