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