wireless: add some comments to functions
[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 #include "wireless.h"
26
27 struct ubus_context *ubus_ctx = NULL;
28 static struct blob_buf b;
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 if (netifd_reload())
48 return UBUS_STATUS_NOT_FOUND;
49
50 return UBUS_STATUS_OK;
51 }
52
53 enum {
54 HR_TARGET,
55 HR_V6,
56 HR_INTERFACE,
57 __HR_MAX
58 };
59
60 static const struct blobmsg_policy route_policy[__HR_MAX] = {
61 [HR_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
62 [HR_V6] = { .name = "v6", .type = BLOBMSG_TYPE_BOOL },
63 [HR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
64 };
65
66 static int
67 netifd_add_host_route(struct ubus_context *ctx, struct ubus_object *obj,
68 struct ubus_request_data *req, const char *method,
69 struct blob_attr *msg)
70 {
71 struct blob_attr *tb[__HR_MAX];
72 struct interface *iface = NULL;
73 union if_addr a;
74 bool v6 = false;
75
76 blobmsg_parse(route_policy, __HR_MAX, tb, blob_data(msg), blob_len(msg));
77 if (!tb[HR_TARGET])
78 return UBUS_STATUS_INVALID_ARGUMENT;
79
80 if (tb[HR_V6])
81 v6 = blobmsg_get_bool(tb[HR_V6]);
82
83 if (tb[HR_INTERFACE])
84 iface = vlist_find(&interfaces, blobmsg_data(tb[HR_INTERFACE]), iface, node);
85
86 memset(&a, 0, sizeof(a));
87 if (!inet_pton(v6 ? AF_INET6 : AF_INET, blobmsg_data(tb[HR_TARGET]), &a))
88 return UBUS_STATUS_INVALID_ARGUMENT;
89
90
91 iface = interface_ip_add_target_route(&a, v6, iface);
92 if (!iface)
93 return UBUS_STATUS_NOT_FOUND;
94
95 blob_buf_init(&b, 0);
96 blobmsg_add_string(&b, "interface", iface->name);
97 ubus_send_reply(ctx, req, b.head);
98
99 return 0;
100 }
101
102 static int
103 netifd_get_proto_handlers(struct ubus_context *ctx, struct ubus_object *obj,
104 struct ubus_request_data *req, const char *method,
105 struct blob_attr *msg)
106 {
107 blob_buf_init(&b, 0);
108 proto_dump_handlers(&b);
109 ubus_send_reply(ctx, req, b.head);
110
111 return 0;
112 }
113
114
115 enum {
116 DI_NAME,
117 __DI_MAX
118 };
119
120 static const struct blobmsg_policy dynamic_policy[__DI_MAX] = {
121 [DI_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
122 };
123
124 static int
125 netifd_add_dynamic(struct ubus_context *ctx, struct ubus_object *obj,
126 struct ubus_request_data *req, const char *method,
127 struct blob_attr *msg)
128 {
129 struct blob_attr *tb[__DI_MAX];
130 struct interface *iface;
131 struct blob_attr *config;
132
133 blobmsg_parse(dynamic_policy, __DI_MAX, tb, blob_data(msg), blob_len(msg));
134
135 if (!tb[DI_NAME])
136 return UBUS_STATUS_INVALID_ARGUMENT;
137
138 const char *name = blobmsg_get_string(tb[DI_NAME]);
139
140 iface = interface_alloc(name, msg, true);
141 if (!iface)
142 return UBUS_STATUS_UNKNOWN_ERROR;
143
144 config = blob_memdup(msg);
145 if (!config)
146 goto error;
147
148 if (!interface_add(iface, config))
149 goto error_free_config;
150
151 return UBUS_STATUS_OK;
152
153 error_free_config:
154 free(config);
155 error:
156 free(iface);
157 return UBUS_STATUS_UNKNOWN_ERROR;
158 }
159
160 enum {
161 NETNS_UPDOWN_JAIL,
162 NETNS_UPDOWN_PID,
163 NETNS_UPDOWN_START,
164 __NETNS_UPDOWN_MAX
165 };
166
167 static const struct blobmsg_policy netns_updown_policy[__NETNS_UPDOWN_MAX] = {
168 [NETNS_UPDOWN_JAIL] = { .name = "jail", .type = BLOBMSG_TYPE_STRING },
169 [NETNS_UPDOWN_PID] = { .name = "pid", .type = BLOBMSG_TYPE_INT32 },
170 [NETNS_UPDOWN_START] = { .name = "start", .type = BLOBMSG_TYPE_BOOL },
171 };
172
173 static int
174 netifd_netns_updown(struct ubus_context *ctx, struct ubus_object *obj,
175 struct ubus_request_data *req, const char *method,
176 struct blob_attr *msg)
177 {
178 struct blob_attr *tb[__NETNS_UPDOWN_MAX];
179 char *jail;
180 pid_t netns_pid;
181 bool start;
182
183 blobmsg_parse(netns_updown_policy, __NETNS_UPDOWN_MAX, tb, blob_data(msg), blob_len(msg));
184 if (!tb[NETNS_UPDOWN_JAIL] || !tb[NETNS_UPDOWN_PID])
185 return UBUS_STATUS_INVALID_ARGUMENT;
186
187 start = tb[NETNS_UPDOWN_START] && blobmsg_get_bool(tb[NETNS_UPDOWN_START]);
188 jail = blobmsg_get_string(tb[NETNS_UPDOWN_JAIL]);
189 netns_pid = blobmsg_get_u32(tb[NETNS_UPDOWN_PID]);
190
191 if (start)
192 interface_start_jail(jail, netns_pid);
193 else
194 interface_stop_jail(jail, netns_pid);
195
196 return UBUS_STATUS_OK;
197 }
198
199 static struct ubus_method main_object_methods[] = {
200 { .name = "restart", .handler = netifd_handle_restart },
201 { .name = "reload", .handler = netifd_handle_reload },
202 UBUS_METHOD("add_host_route", netifd_add_host_route, route_policy),
203 { .name = "get_proto_handlers", .handler = netifd_get_proto_handlers },
204 UBUS_METHOD("add_dynamic", netifd_add_dynamic, dynamic_policy),
205 UBUS_METHOD("netns_updown", netifd_netns_updown, netns_updown_policy),
206 };
207
208 static struct ubus_object_type main_object_type =
209 UBUS_OBJECT_TYPE("netifd", main_object_methods);
210
211 static struct ubus_object main_object = {
212 .name = "network",
213 .type = &main_object_type,
214 .methods = main_object_methods,
215 .n_methods = ARRAY_SIZE(main_object_methods),
216 };
217
218 enum {
219 DEV_NAME,
220 __DEV_MAX,
221 };
222
223 static const struct blobmsg_policy dev_policy[__DEV_MAX] = {
224 [DEV_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
225 };
226
227 static int
228 netifd_dev_status(struct ubus_context *ctx, struct ubus_object *obj,
229 struct ubus_request_data *req, const char *method,
230 struct blob_attr *msg)
231 {
232 struct device *dev = NULL;
233 struct blob_attr *tb[__DEV_MAX];
234
235 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
236
237 if (tb[DEV_NAME]) {
238 dev = device_find(blobmsg_data(tb[DEV_NAME]));
239 if (!dev)
240 return UBUS_STATUS_INVALID_ARGUMENT;
241 }
242
243 blob_buf_init(&b, 0);
244 device_dump_status(&b, dev);
245 ubus_send_reply(ctx, req, b.head);
246
247 return 0;
248 }
249
250 enum {
251 ALIAS_ATTR_ALIAS,
252 ALIAS_ATTR_DEV,
253 __ALIAS_ATTR_MAX,
254 };
255
256 static const struct blobmsg_policy alias_attrs[__ALIAS_ATTR_MAX] = {
257 [ALIAS_ATTR_ALIAS] = { "alias", BLOBMSG_TYPE_ARRAY },
258 [ALIAS_ATTR_DEV] = { "device", BLOBMSG_TYPE_STRING },
259 };
260
261 static int
262 netifd_handle_alias(struct ubus_context *ctx, struct ubus_object *obj,
263 struct ubus_request_data *req, const char *method,
264 struct blob_attr *msg)
265 {
266 struct device *dev = NULL;
267 struct blob_attr *tb[__ALIAS_ATTR_MAX];
268 struct blob_attr *cur;
269 int rem;
270
271 blobmsg_parse(alias_attrs, __ALIAS_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
272
273 if (!tb[ALIAS_ATTR_ALIAS])
274 return UBUS_STATUS_INVALID_ARGUMENT;
275
276 if ((cur = tb[ALIAS_ATTR_DEV]) != NULL) {
277 dev = device_get(blobmsg_data(cur), true);
278 if (!dev)
279 return UBUS_STATUS_NOT_FOUND;
280 }
281
282 blobmsg_for_each_attr(cur, tb[ALIAS_ATTR_ALIAS], rem) {
283 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
284 goto error;
285
286 if (!blobmsg_check_attr(cur, false))
287 goto error;
288
289 alias_notify_device(blobmsg_data(cur), dev);
290 }
291 return 0;
292
293 error:
294 device_free_unused(dev);
295 return UBUS_STATUS_INVALID_ARGUMENT;
296 }
297
298 enum {
299 DEV_STATE_NAME,
300 DEV_STATE_DEFER,
301 DEV_STATE_AUTH_STATUS,
302 __DEV_STATE_MAX,
303 };
304
305 static const struct blobmsg_policy dev_state_policy[__DEV_STATE_MAX] = {
306 [DEV_STATE_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
307 [DEV_STATE_DEFER] = { .name = "defer", .type = BLOBMSG_TYPE_BOOL },
308 [DEV_STATE_AUTH_STATUS] = { .name = "auth_status", .type = BLOBMSG_TYPE_BOOL },
309 };
310
311 static int
312 netifd_handle_set_state(struct ubus_context *ctx, struct ubus_object *obj,
313 struct ubus_request_data *req, const char *method,
314 struct blob_attr *msg)
315 {
316 struct device *dev = NULL;
317 struct blob_attr *tb[__DEV_STATE_MAX];
318 struct blob_attr *cur;
319
320 blobmsg_parse(dev_state_policy, __DEV_STATE_MAX, tb, blob_data(msg), blob_len(msg));
321
322 cur = tb[DEV_STATE_NAME];
323 if (!cur)
324 return UBUS_STATUS_INVALID_ARGUMENT;
325
326 dev = device_find(blobmsg_data(cur));
327 if (!dev)
328 return UBUS_STATUS_NOT_FOUND;
329
330 cur = tb[DEV_STATE_DEFER];
331 if (cur)
332 device_set_deferred(dev, !!blobmsg_get_u8(cur));
333
334 cur = tb[DEV_STATE_AUTH_STATUS];
335 if (cur)
336 device_set_auth_status(dev, !!blobmsg_get_u8(cur));
337
338 return 0;
339 }
340
341 static struct ubus_method dev_object_methods[] = {
342 UBUS_METHOD("status", netifd_dev_status, dev_policy),
343 UBUS_METHOD("set_alias", netifd_handle_alias, alias_attrs),
344 UBUS_METHOD("set_state", netifd_handle_set_state, dev_state_policy),
345 };
346
347 static struct ubus_object_type dev_object_type =
348 UBUS_OBJECT_TYPE("device", dev_object_methods);
349
350 static struct ubus_object dev_object = {
351 .name = "network.device",
352 .type = &dev_object_type,
353 .methods = dev_object_methods,
354 .n_methods = ARRAY_SIZE(dev_object_methods),
355 };
356
357 static void
358 netifd_ubus_add_fd(void)
359 {
360 ubus_add_uloop(ubus_ctx);
361 system_fd_set_cloexec(ubus_ctx->sock.fd);
362 }
363
364 static void
365 netifd_ubus_reconnect_timer(struct uloop_timeout *timeout)
366 {
367 static struct uloop_timeout retry = {
368 .cb = netifd_ubus_reconnect_timer,
369 };
370 int t = 2;
371
372 if (ubus_reconnect(ubus_ctx, ubus_path) != 0) {
373 DPRINTF("failed to reconnect, trying again in %d seconds\n", t);
374 uloop_timeout_set(&retry, t * 1000);
375 return;
376 }
377
378 DPRINTF("reconnected to ubus, new id: %08x\n", ubus_ctx->local_id);
379 netifd_ubus_add_fd();
380 }
381
382 static void
383 netifd_ubus_connection_lost(struct ubus_context *ctx)
384 {
385 netifd_ubus_reconnect_timer(NULL);
386 }
387
388 /* per-interface object */
389
390 static int
391 netifd_handle_up(struct ubus_context *ctx, struct ubus_object *obj,
392 struct ubus_request_data *req, const char *method,
393 struct blob_attr *msg)
394 {
395 struct interface *iface;
396
397 iface = container_of(obj, struct interface, ubus);
398 interface_set_up(iface);
399
400 return 0;
401 }
402
403 static int
404 netifd_handle_down(struct ubus_context *ctx, struct ubus_object *obj,
405 struct ubus_request_data *req, const char *method,
406 struct blob_attr *msg)
407 {
408 struct interface *iface;
409
410 iface = container_of(obj, struct interface, ubus);
411 interface_set_down(iface);
412
413 return 0;
414 }
415
416 static int
417 netifd_handle_renew(struct ubus_context *ctx, struct ubus_object *obj,
418 struct ubus_request_data *req, const char *method,
419 struct blob_attr *msg)
420 {
421 struct interface *iface;
422
423 iface = container_of(obj, struct interface, ubus);
424 interface_renew(iface);
425
426 return 0;
427 }
428
429 static void
430 netifd_add_interface_errors(struct blob_buf *b, struct interface *iface)
431 {
432 struct interface_error *error;
433 void *e, *e2, *e3;
434 int i;
435
436 e = blobmsg_open_array(b, "errors");
437 list_for_each_entry(error, &iface->errors, list) {
438 e2 = blobmsg_open_table(b, NULL);
439
440 blobmsg_add_string(b, "subsystem", error->subsystem);
441 blobmsg_add_string(b, "code", error->code);
442 if (error->data[0]) {
443 e3 = blobmsg_open_array(b, "data");
444 for (i = 0; error->data[i]; i++)
445 blobmsg_add_string(b, NULL, error->data[i]);
446 blobmsg_close_array(b, e3);
447 }
448
449 blobmsg_close_table(b, e2);
450 }
451 blobmsg_close_array(b, e);
452 }
453
454 static void
455 interface_ip_dump_address_list(struct interface_ip_settings *ip, bool v6, bool enabled)
456 {
457 struct device_addr *addr;
458 char *buf;
459 void *a;
460 int buflen = 128;
461 int af;
462
463 time_t now = system_get_rtime();
464 vlist_for_each_element(&ip->addr, addr, node) {
465 if (addr->enabled != enabled)
466 continue;
467
468 if ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
469 af = AF_INET;
470 else
471 af = AF_INET6;
472
473 if (af != (v6 ? AF_INET6 : AF_INET))
474 continue;
475
476 a = blobmsg_open_table(&b, NULL);
477
478 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
479 inet_ntop(af, &addr->addr, buf, buflen);
480 blobmsg_add_string_buffer(&b);
481
482 blobmsg_add_u32(&b, "mask", addr->mask);
483
484 if (addr->point_to_point) {
485 buf = blobmsg_alloc_string_buffer(&b, "ptpaddress", buflen);
486 inet_ntop(af, &addr->point_to_point, buf, buflen);
487 blobmsg_add_string_buffer(&b);
488 }
489
490 if (addr->preferred_until) {
491 int preferred = addr->preferred_until - now;
492 if (preferred < 0)
493 preferred = 0;
494 blobmsg_add_u32(&b, "preferred", preferred);
495 }
496
497 if (addr->valid_until)
498 blobmsg_add_u32(&b, "valid", addr->valid_until - now);
499
500 if (addr->pclass)
501 blobmsg_add_string(&b, "class", addr->pclass);
502
503 blobmsg_close_table(&b, a);
504 }
505 }
506
507 static void
508 interface_ip_dump_neighbor_list(struct interface_ip_settings *ip, bool enabled)
509 {
510 struct device_neighbor *neighbor;
511 int buflen = 128;
512 char *buf;
513 void *r;
514 int af;
515
516 vlist_for_each_element(&ip->neighbor, neighbor, node) {
517 if (neighbor->enabled != enabled)
518 continue;
519
520 if ((neighbor->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
521 af = AF_INET;
522 else
523 af = AF_INET6;
524
525 r = blobmsg_open_table(&b, NULL);
526
527 if (neighbor->flags & DEVNEIGH_MAC)
528 blobmsg_add_string(&b, "mac", format_macaddr(neighbor->macaddr));
529
530 buf = blobmsg_alloc_string_buffer(&b , "address", buflen);
531 inet_ntop(af, &neighbor->addr, buf, buflen);
532 blobmsg_add_string_buffer(&b);
533
534 if (neighbor->proxy)
535 blobmsg_add_u32(&b, "proxy", neighbor->proxy);
536
537 if (neighbor->router)
538 blobmsg_add_u32(&b, "router", neighbor->router);
539
540 blobmsg_close_table(&b, r);
541 }
542 }
543
544 static void
545 interface_ip_dump_route_list(struct interface_ip_settings *ip, bool enabled)
546 {
547 struct device_route *route;
548 int buflen = 128;
549 char *buf;
550 void *r;
551 int af;
552
553 time_t now = system_get_rtime();
554 vlist_for_each_element(&ip->route, route, node) {
555 if (route->enabled != enabled)
556 continue;
557
558 if ((ip->no_defaultroute == enabled) && !route->mask)
559 continue;
560
561 if ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
562 af = AF_INET;
563 else
564 af = AF_INET6;
565
566 r = blobmsg_open_table(&b, NULL);
567
568 buf = blobmsg_alloc_string_buffer(&b, "target", buflen);
569 inet_ntop(af, &route->addr, buf, buflen);
570 blobmsg_add_string_buffer(&b);
571
572 blobmsg_add_u32(&b, "mask", route->mask);
573
574 buf = blobmsg_alloc_string_buffer(&b, "nexthop", buflen);
575 inet_ntop(af, &route->nexthop, buf, buflen);
576 blobmsg_add_string_buffer(&b);
577
578 if (route->flags & DEVROUTE_TYPE)
579 blobmsg_add_u32(&b, "type", route->type);
580
581 if (route->flags & DEVROUTE_PROTO)
582 blobmsg_add_u32(&b, "proto", route->proto);
583
584 if (route->flags & DEVROUTE_MTU)
585 blobmsg_add_u32(&b, "mtu", route->mtu);
586
587 if (route->flags & DEVROUTE_METRIC)
588 blobmsg_add_u32(&b, "metric", route->metric);
589
590 if (route->flags & DEVROUTE_TABLE)
591 blobmsg_add_u32(&b, "table", route->table);
592
593 if (route->valid_until)
594 blobmsg_add_u32(&b, "valid", route->valid_until - now);
595
596 buf = blobmsg_alloc_string_buffer(&b, "source", buflen);
597 inet_ntop(af, &route->source, buf, buflen);
598 snprintf(buf + strlen(buf), buflen - strlen(buf), "/%u", route->sourcemask);
599 blobmsg_add_string_buffer(&b);
600
601 blobmsg_close_table(&b, r);
602 }
603 }
604
605
606 static void
607 interface_ip_dump_prefix_list(struct interface_ip_settings *ip)
608 {
609 struct device_prefix *prefix;
610 char *buf;
611 void *a, *c;
612 const int buflen = INET6_ADDRSTRLEN;
613
614 time_t now = system_get_rtime();
615 vlist_for_each_element(&ip->prefix, prefix, node) {
616 a = blobmsg_open_table(&b, NULL);
617
618 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
619 inet_ntop(AF_INET6, &prefix->addr, buf, buflen);
620 blobmsg_add_string_buffer(&b);
621
622 blobmsg_add_u32(&b, "mask", prefix->length);
623
624 if (prefix->preferred_until) {
625 int preferred = prefix->preferred_until - now;
626 if (preferred < 0)
627 preferred = 0;
628 blobmsg_add_u32(&b, "preferred", preferred);
629 }
630
631 if (prefix->valid_until)
632 blobmsg_add_u32(&b, "valid", prefix->valid_until - now);
633
634 blobmsg_add_string(&b, "class", prefix->pclass);
635
636 c = blobmsg_open_table(&b, "assigned");
637 struct device_prefix_assignment *assign;
638 list_for_each_entry(assign, &prefix->assignments, head) {
639 if (!assign->name[0])
640 continue;
641
642 struct in6_addr addr = prefix->addr;
643 addr.s6_addr32[1] |= htonl(assign->assigned);
644
645 void *d = blobmsg_open_table(&b, assign->name);
646
647 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
648 inet_ntop(AF_INET6, &addr, buf, buflen);
649 blobmsg_add_string_buffer(&b);
650
651 blobmsg_add_u32(&b, "mask", assign->length);
652
653 blobmsg_close_table(&b, d);
654 }
655 blobmsg_close_table(&b, c);
656
657 blobmsg_close_table(&b, a);
658 }
659 }
660
661
662 static void
663 interface_ip_dump_prefix_assignment_list(struct interface *iface)
664 {
665 void *a;
666 char *buf;
667 const int buflen = INET6_ADDRSTRLEN;
668 time_t now = system_get_rtime();
669
670 struct device_prefix *prefix;
671 list_for_each_entry(prefix, &prefixes, head) {
672 struct device_prefix_assignment *assign;
673 list_for_each_entry(assign, &prefix->assignments, head) {
674 if (strcmp(assign->name, iface->name))
675 continue;
676
677 struct in6_addr addr = prefix->addr;
678 addr.s6_addr32[1] |= htonl(assign->assigned);
679
680 a = blobmsg_open_table(&b, NULL);
681
682 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
683 inet_ntop(AF_INET6, &addr, buf, buflen);
684 blobmsg_add_string_buffer(&b);
685
686 blobmsg_add_u32(&b, "mask", assign->length);
687
688 if (prefix->preferred_until) {
689 int preferred = prefix->preferred_until - now;
690 if (preferred < 0)
691 preferred = 0;
692 blobmsg_add_u32(&b, "preferred", preferred);
693 }
694
695 if (prefix->valid_until)
696 blobmsg_add_u32(&b, "valid", prefix->valid_until - now);
697
698 void *c = blobmsg_open_table(&b, "local-address");
699 if (assign->enabled) {
700 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
701 inet_ntop(AF_INET6, &assign->addr, buf, buflen);
702 blobmsg_add_string_buffer(&b);
703
704 blobmsg_add_u32(&b, "mask", assign->length);
705 }
706 blobmsg_close_table(&b, c);
707
708 blobmsg_close_table(&b, a);
709 }
710 }
711 }
712
713 static void
714 interface_ip_dump_dns_server_list(struct interface_ip_settings *ip, bool enabled)
715 {
716 struct dns_server *dns;
717 int buflen = 128;
718 char *buf;
719
720 vlist_simple_for_each_element(&ip->dns_servers, dns, node) {
721 if (ip->no_dns == enabled)
722 continue;
723
724 buf = blobmsg_alloc_string_buffer(&b, NULL, buflen);
725 inet_ntop(dns->af, &dns->addr, buf, buflen);
726 blobmsg_add_string_buffer(&b);
727 }
728 }
729
730 static void
731 interface_ip_dump_dns_search_list(struct interface_ip_settings *ip, bool enabled)
732 {
733 struct dns_search_domain *dns;
734
735 vlist_simple_for_each_element(&ip->dns_search, dns, node) {
736 if (ip->no_dns == enabled)
737 continue;
738
739 blobmsg_add_string(&b, NULL, dns->name);
740 }
741 }
742
743 static void
744 netifd_dump_status(struct interface *iface)
745 {
746 struct interface_data *data;
747 struct device *dev;
748 void *a, *inactive;
749
750 blobmsg_add_u8(&b, "up", iface->state == IFS_UP);
751 blobmsg_add_u8(&b, "pending", iface->state == IFS_SETUP);
752 blobmsg_add_u8(&b, "available", iface->available);
753 blobmsg_add_u8(&b, "autostart", iface->autostart);
754 blobmsg_add_u8(&b, "dynamic", iface->dynamic);
755
756 if (iface->state == IFS_UP) {
757 time_t cur = system_get_rtime();
758 blobmsg_add_u32(&b, "uptime", cur - iface->start_time);
759 if (iface->l3_dev.dev)
760 blobmsg_add_string(&b, "l3_device", iface->l3_dev.dev->ifname);
761 }
762
763 if (iface->proto_handler)
764 blobmsg_add_string(&b, "proto", iface->proto_handler->name);
765
766 dev = iface->main_dev.dev;
767 if (dev && !dev->hidden && iface->proto_handler &&
768 !(iface->proto_handler->flags & PROTO_FLAG_NODEV))
769 blobmsg_add_string(&b, "device", dev->ifname);
770
771 if (iface->jail)
772 blobmsg_add_string(&b, "jail", iface->jail);
773
774 if (iface->jail_ifname)
775 blobmsg_add_string(&b, "jail_ifname", iface->jail_ifname);
776
777 if (iface->state == IFS_UP) {
778 if (iface->updated) {
779 a = blobmsg_open_array(&b, "updated");
780
781 if (iface->updated & IUF_ADDRESS)
782 blobmsg_add_string(&b, NULL, "addresses");
783 if (iface->updated & IUF_ROUTE)
784 blobmsg_add_string(&b, NULL, "routes");
785 if (iface->updated & IUF_PREFIX)
786 blobmsg_add_string(&b, NULL, "prefixes");
787 if (iface->updated & IUF_DATA)
788 blobmsg_add_string(&b, NULL, "data");
789
790 blobmsg_close_array(&b, a);
791 }
792
793 if (iface->ip4table)
794 blobmsg_add_u32(&b, "ip4table", iface->ip4table);
795 if (iface->ip6table)
796 blobmsg_add_u32(&b, "ip6table", iface->ip6table);
797 blobmsg_add_u32(&b, "metric", iface->metric);
798 blobmsg_add_u32(&b, "dns_metric", iface->dns_metric);
799 blobmsg_add_u8(&b, "delegation", !iface->proto_ip.no_delegation);
800 if (iface->assignment_weight)
801 blobmsg_add_u32(&b, "ip6weight", iface->assignment_weight);
802 a = blobmsg_open_array(&b, "ipv4-address");
803 interface_ip_dump_address_list(&iface->config_ip, false, true);
804 interface_ip_dump_address_list(&iface->proto_ip, false, true);
805 blobmsg_close_array(&b, a);
806 a = blobmsg_open_array(&b, "ipv6-address");
807 interface_ip_dump_address_list(&iface->config_ip, true, true);
808 interface_ip_dump_address_list(&iface->proto_ip, true, true);
809 blobmsg_close_array(&b, a);
810 a = blobmsg_open_array(&b, "ipv6-prefix");
811 interface_ip_dump_prefix_list(&iface->config_ip);
812 interface_ip_dump_prefix_list(&iface->proto_ip);
813 blobmsg_close_array(&b, a);
814 a = blobmsg_open_array(&b, "ipv6-prefix-assignment");
815 interface_ip_dump_prefix_assignment_list(iface);
816 blobmsg_close_array(&b, a);
817 a = blobmsg_open_array(&b, "route");
818 interface_ip_dump_route_list(&iface->config_ip, true);
819 interface_ip_dump_route_list(&iface->proto_ip, true);
820 blobmsg_close_array(&b, a);
821 a = blobmsg_open_array(&b, "dns-server");
822 interface_ip_dump_dns_server_list(&iface->config_ip, true);
823 interface_ip_dump_dns_server_list(&iface->proto_ip, true);
824 blobmsg_close_array(&b, a);
825 a = blobmsg_open_array(&b, "dns-search");
826 interface_ip_dump_dns_search_list(&iface->config_ip, true);
827 interface_ip_dump_dns_search_list(&iface->proto_ip, true);
828 blobmsg_close_array(&b, a);
829 a = blobmsg_open_array(&b, "neighbors");
830 interface_ip_dump_neighbor_list(&iface->config_ip, true);
831 interface_ip_dump_neighbor_list(&iface->proto_ip, true);
832 blobmsg_close_array(&b, a);
833
834 inactive = blobmsg_open_table(&b, "inactive");
835 a = blobmsg_open_array(&b, "ipv4-address");
836 interface_ip_dump_address_list(&iface->config_ip, false, false);
837 interface_ip_dump_address_list(&iface->proto_ip, false, false);
838 blobmsg_close_array(&b, a);
839 a = blobmsg_open_array(&b, "ipv6-address");
840 interface_ip_dump_address_list(&iface->config_ip, true, false);
841 interface_ip_dump_address_list(&iface->proto_ip, true, false);
842 blobmsg_close_array(&b, a);
843 a = blobmsg_open_array(&b, "route");
844 interface_ip_dump_route_list(&iface->config_ip, false);
845 interface_ip_dump_route_list(&iface->proto_ip, false);
846 blobmsg_close_array(&b, a);
847 a = blobmsg_open_array(&b, "dns-server");
848 interface_ip_dump_dns_server_list(&iface->config_ip, false);
849 interface_ip_dump_dns_server_list(&iface->proto_ip, false);
850 blobmsg_close_array(&b, a);
851 a = blobmsg_open_array(&b, "dns-search");
852 interface_ip_dump_dns_search_list(&iface->config_ip, false);
853 interface_ip_dump_dns_search_list(&iface->proto_ip, false);
854 blobmsg_close_array(&b, a);
855 a = blobmsg_open_array(&b, "neighbors");
856 interface_ip_dump_neighbor_list(&iface->config_ip, false);
857 interface_ip_dump_neighbor_list(&iface->proto_ip, false);
858 blobmsg_close_array(&b, a);
859 blobmsg_close_table(&b, inactive);
860 }
861
862 a = blobmsg_open_table(&b, "data");
863 avl_for_each_element(&iface->data, data, node)
864 blobmsg_add_blob(&b, data->data);
865
866 blobmsg_close_table(&b, a);
867
868 if (!list_empty(&iface->errors))
869 netifd_add_interface_errors(&b, iface);
870 }
871
872 static int
873 netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
874 struct ubus_request_data *req, const char *method,
875 struct blob_attr *msg)
876 {
877 struct interface *iface = container_of(obj, struct interface, ubus);
878
879 blob_buf_init(&b, 0);
880 netifd_dump_status(iface);
881 ubus_send_reply(ctx, req, b.head);
882
883 return 0;
884 }
885
886
887 static int
888 netifd_handle_dump(struct ubus_context *ctx, struct ubus_object *obj,
889 struct ubus_request_data *req, const char *method,
890 struct blob_attr *msg)
891 {
892 blob_buf_init(&b, 0);
893 void *a = blobmsg_open_array(&b, "interface");
894
895 struct interface *iface;
896 vlist_for_each_element(&interfaces, iface, node) {
897 void *i = blobmsg_open_table(&b, NULL);
898 blobmsg_add_string(&b, "interface", iface->name);
899 netifd_dump_status(iface);
900 blobmsg_close_table(&b, i);
901 }
902
903 blobmsg_close_array(&b, a);
904 ubus_send_reply(ctx, req, b.head);
905
906 return 0;
907 }
908
909 enum {
910 DEV_LINK_NAME,
911 DEV_LINK_EXT,
912 DEV_LINK_VLAN,
913 __DEV_LINK_MAX,
914 };
915
916 static const struct blobmsg_policy dev_link_policy[__DEV_LINK_MAX] = {
917 [DEV_LINK_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
918 [DEV_LINK_EXT] = { .name = "link-ext", .type = BLOBMSG_TYPE_BOOL },
919 [DEV_LINK_VLAN] = { .name = "vlan", .type = BLOBMSG_TYPE_ARRAY },
920 };
921
922 static int
923 netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
924 struct ubus_request_data *req, const char *method,
925 struct blob_attr *msg)
926 {
927 struct blob_attr *tb[__DEV_LINK_MAX];
928 struct blob_attr *cur;
929 struct interface *iface;
930 bool add = !strncmp(method, "add", 3);
931 bool link_ext = true;
932
933 iface = container_of(obj, struct interface, ubus);
934
935 blobmsg_parse(dev_link_policy, __DEV_LINK_MAX, tb, blob_data(msg), blob_len(msg));
936
937 if (!tb[DEV_LINK_NAME])
938 return UBUS_STATUS_INVALID_ARGUMENT;
939
940 cur = tb[DEV_LINK_EXT];
941 if (cur)
942 link_ext = blobmsg_get_bool(cur);
943
944 return interface_handle_link(iface, blobmsg_data(tb[DEV_LINK_NAME]),
945 tb[DEV_LINK_VLAN], add, link_ext);
946 }
947
948
949 static int
950 netifd_iface_notify_proto(struct ubus_context *ctx, struct ubus_object *obj,
951 struct ubus_request_data *req, const char *method,
952 struct blob_attr *msg)
953 {
954 struct interface *iface;
955
956 iface = container_of(obj, struct interface, ubus);
957
958 if (!iface->proto || !iface->proto->notify)
959 return UBUS_STATUS_NOT_SUPPORTED;
960
961 return iface->proto->notify(iface->proto, msg);
962 }
963
964 static void
965 netifd_iface_do_remove(struct uloop_timeout *timeout)
966 {
967 struct interface *iface;
968
969 iface = container_of(timeout, struct interface, remove_timer);
970 vlist_delete(&interfaces, &iface->node);
971 }
972
973 static int
974 netifd_iface_remove(struct ubus_context *ctx, struct ubus_object *obj,
975 struct ubus_request_data *req, const char *method,
976 struct blob_attr *msg)
977 {
978 struct interface *iface;
979
980 iface = container_of(obj, struct interface, ubus);
981 if (iface->remove_timer.cb)
982 return UBUS_STATUS_INVALID_ARGUMENT;
983
984 iface->remove_timer.cb = netifd_iface_do_remove;
985 uloop_timeout_set(&iface->remove_timer, 100);
986 return 0;
987 }
988
989 static int
990 netifd_handle_iface_prepare(struct ubus_context *ctx, struct ubus_object *obj,
991 struct ubus_request_data *req, const char *method,
992 struct blob_attr *msg)
993 {
994 struct interface *iface;
995 struct device *dev, *bridge_dev = NULL;
996 const struct device_hotplug_ops *ops;
997
998 iface = container_of(obj, struct interface, ubus);
999 dev = iface->main_dev.dev;
1000 if (!dev)
1001 goto out;
1002
1003 ops = dev->hotplug_ops;
1004 if (!ops)
1005 goto out;
1006
1007 ops->prepare(dev, &bridge_dev);
1008
1009 out:
1010 blob_buf_init(&b, 0);
1011 if (bridge_dev)
1012 blobmsg_add_string(&b, "bridge", bridge_dev->ifname);
1013 ubus_send_reply(ctx, req, b.head);
1014
1015 return 0;
1016 }
1017
1018 static int
1019 netifd_handle_set_data(struct ubus_context *ctx, struct ubus_object *obj,
1020 struct ubus_request_data *req, const char *method,
1021 struct blob_attr *msg)
1022 {
1023 struct interface *iface;
1024
1025 iface = container_of(obj, struct interface, ubus);
1026
1027 return interface_parse_data(iface, msg);
1028 }
1029
1030 static struct ubus_method iface_object_methods[] = {
1031 { .name = "up", .handler = netifd_handle_up },
1032 { .name = "down", .handler = netifd_handle_down },
1033 { .name = "renew", .handler = netifd_handle_renew },
1034 { .name = "status", .handler = netifd_handle_status },
1035 { .name = "prepare", .handler = netifd_handle_iface_prepare },
1036 { .name = "dump", .handler = netifd_handle_dump },
1037 UBUS_METHOD("add_device", netifd_iface_handle_device, dev_link_policy ),
1038 UBUS_METHOD("remove_device", netifd_iface_handle_device, dev_link_policy ),
1039 { .name = "notify_proto", .handler = netifd_iface_notify_proto },
1040 { .name = "remove", .handler = netifd_iface_remove },
1041 { .name = "set_data", .handler = netifd_handle_set_data },
1042 };
1043
1044 static struct ubus_object_type iface_object_type =
1045 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
1046
1047
1048 static struct ubus_object iface_object = {
1049 .name = "network.interface",
1050 .type = &iface_object_type,
1051 .n_methods = ARRAY_SIZE(iface_object_methods),
1052 };
1053
1054 static void netifd_add_object(struct ubus_object *obj)
1055 {
1056 int ret = ubus_add_object(ubus_ctx, obj);
1057
1058 if (ret != 0)
1059 fprintf(stderr, "Failed to publish object '%s': %s\n", obj->name, ubus_strerror(ret));
1060 }
1061
1062 static const struct blobmsg_policy iface_policy = {
1063 .name = "interface",
1064 .type = BLOBMSG_TYPE_STRING,
1065 };
1066
1067 static int
1068 netifd_handle_iface(struct ubus_context *ctx, struct ubus_object *obj,
1069 struct ubus_request_data *req, const char *method,
1070 struct blob_attr *msg)
1071 {
1072 struct interface *iface;
1073 struct blob_attr *tb;
1074 int i;
1075
1076 blobmsg_parse(&iface_policy, 1, &tb, blob_data(msg), blob_len(msg));
1077 if (!tb)
1078 return UBUS_STATUS_INVALID_ARGUMENT;
1079
1080 iface = vlist_find(&interfaces, blobmsg_data(tb), iface, node);
1081 if (!iface)
1082 return UBUS_STATUS_NOT_FOUND;
1083
1084 for (i = 0; i < ARRAY_SIZE(iface_object_methods); i++) {
1085 ubus_handler_t cb;
1086
1087 if (strcmp(method, iface_object_methods[i].name) != 0)
1088 continue;
1089
1090 cb = iface_object_methods[i].handler;
1091 return cb(ctx, &iface->ubus, req, method, msg);
1092 }
1093
1094 return UBUS_STATUS_INVALID_ARGUMENT;
1095 }
1096
1097 static void netifd_add_iface_object(void)
1098 {
1099 struct ubus_method *methods;
1100 int i;
1101
1102 methods = calloc(1, sizeof(iface_object_methods));
1103 if (!methods)
1104 return;
1105
1106 memcpy(methods, iface_object_methods, sizeof(iface_object_methods));
1107 iface_object.methods = methods;
1108
1109 for (i = 0; i < ARRAY_SIZE(iface_object_methods); i++) {
1110 if (methods[i].handler == netifd_handle_dump)
1111 continue;
1112
1113 methods[i].handler = netifd_handle_iface;
1114 methods[i].policy = &iface_policy;
1115 methods[i].n_policy = 1;
1116 }
1117 netifd_add_object(&iface_object);
1118 }
1119
1120 static struct wireless_device *
1121 get_wdev(struct blob_attr *msg, int *ret)
1122 {
1123 struct blobmsg_policy wdev_policy = {
1124 .name = "device",
1125 .type = BLOBMSG_TYPE_STRING,
1126 };
1127 struct blob_attr *dev_attr;
1128 struct wireless_device *wdev = NULL;
1129
1130
1131 blobmsg_parse(&wdev_policy, 1, &dev_attr, blob_data(msg), blob_len(msg));
1132 if (!dev_attr) {
1133 *ret = UBUS_STATUS_INVALID_ARGUMENT;
1134 return NULL;
1135 }
1136
1137 wdev = vlist_find(&wireless_devices, blobmsg_data(dev_attr), wdev, node);
1138 if (!wdev) {
1139 *ret = UBUS_STATUS_NOT_FOUND;
1140 return NULL;
1141 }
1142
1143 *ret = 0;
1144 return wdev;
1145 }
1146
1147 static int
1148 netifd_handle_wdev_reconf(struct ubus_context *ctx, struct ubus_object *obj,
1149 struct ubus_request_data *req, const char *method,
1150 struct blob_attr *msg)
1151 {
1152 struct wireless_device *wdev;
1153 int ret;
1154
1155 wdev = get_wdev(msg, &ret);
1156 if (ret == UBUS_STATUS_NOT_FOUND)
1157 return ret;
1158
1159 if (wdev) {
1160 wireless_device_reconf(wdev);
1161 } else {
1162 vlist_for_each_element(&wireless_devices, wdev, node)
1163 wireless_device_reconf(wdev);
1164 }
1165
1166 return 0;
1167 }
1168
1169 static int
1170 netifd_handle_wdev_up(struct ubus_context *ctx, struct ubus_object *obj,
1171 struct ubus_request_data *req, const char *method,
1172 struct blob_attr *msg)
1173 {
1174 struct wireless_device *wdev;
1175 int ret;
1176
1177 wdev = get_wdev(msg, &ret);
1178 if (ret == UBUS_STATUS_NOT_FOUND)
1179 return ret;
1180
1181 if (wdev) {
1182 wireless_device_set_up(wdev);
1183 } else {
1184 vlist_for_each_element(&wireless_devices, wdev, node)
1185 wireless_device_set_up(wdev);
1186 }
1187
1188 return 0;
1189 }
1190
1191 static int
1192 netifd_handle_wdev_down(struct ubus_context *ctx, struct ubus_object *obj,
1193 struct ubus_request_data *req, const char *method,
1194 struct blob_attr *msg)
1195 {
1196 struct wireless_device *wdev;
1197 int ret;
1198
1199 wdev = get_wdev(msg, &ret);
1200 if (ret == UBUS_STATUS_NOT_FOUND)
1201 return ret;
1202
1203 if (wdev) {
1204 wireless_device_set_down(wdev);
1205 } else {
1206 vlist_for_each_element(&wireless_devices, wdev, node)
1207 wireless_device_set_down(wdev);
1208 }
1209
1210 return 0;
1211 }
1212
1213 static int
1214 netifd_handle_wdev_status(struct ubus_context *ctx, struct ubus_object *obj,
1215 struct ubus_request_data *req, const char *method,
1216 struct blob_attr *msg)
1217 {
1218 struct wireless_device *wdev;
1219 int ret;
1220
1221 wdev = get_wdev(msg, &ret);
1222 if (ret == UBUS_STATUS_NOT_FOUND)
1223 return ret;
1224
1225 blob_buf_init(&b, 0);
1226 if (wdev) {
1227 wireless_device_status(wdev, &b);
1228 } else {
1229 vlist_for_each_element(&wireless_devices, wdev, node)
1230 wireless_device_status(wdev, &b);
1231 }
1232 ubus_send_reply(ctx, req, b.head);
1233 return 0;
1234 }
1235
1236 static int
1237 netifd_handle_wdev_get_validate(struct ubus_context *ctx, struct ubus_object *obj,
1238 struct ubus_request_data *req, const char *method,
1239 struct blob_attr *msg)
1240 {
1241 struct wireless_device *wdev;
1242 int ret;
1243
1244 wdev = get_wdev(msg, &ret);
1245 if (ret == UBUS_STATUS_NOT_FOUND)
1246 return ret;
1247
1248 blob_buf_init(&b, 0);
1249 if (wdev) {
1250 wireless_device_get_validate(wdev, &b);
1251 } else {
1252 vlist_for_each_element(&wireless_devices, wdev, node)
1253 wireless_device_get_validate(wdev, &b);
1254 }
1255 ubus_send_reply(ctx, req, b.head);
1256 return 0;
1257 }
1258
1259 static int
1260 netifd_handle_wdev_notify(struct ubus_context *ctx, struct ubus_object *obj,
1261 struct ubus_request_data *req, const char *method,
1262 struct blob_attr *msg)
1263 {
1264 struct wireless_device *wdev;
1265 int ret;
1266
1267 wdev = get_wdev(msg, &ret);
1268 if (!wdev)
1269 return ret;
1270
1271 return wireless_device_notify(wdev, msg, req);
1272 }
1273
1274 static struct ubus_method wireless_object_methods[] = {
1275 { .name = "up", .handler = netifd_handle_wdev_up },
1276 { .name = "down", .handler = netifd_handle_wdev_down },
1277 { .name = "reconf", .handler = netifd_handle_wdev_reconf },
1278 { .name = "status", .handler = netifd_handle_wdev_status },
1279 { .name = "notify", .handler = netifd_handle_wdev_notify },
1280 { .name = "get_validate", .handler = netifd_handle_wdev_get_validate },
1281 };
1282
1283 static struct ubus_object_type wireless_object_type =
1284 UBUS_OBJECT_TYPE("netifd_iface", wireless_object_methods);
1285
1286
1287 static struct ubus_object wireless_object = {
1288 .name = "network.wireless",
1289 .type = &wireless_object_type,
1290 .methods = wireless_object_methods,
1291 .n_methods = ARRAY_SIZE(wireless_object_methods),
1292 };
1293
1294 int
1295 netifd_extdev_invoke(uint32_t id, const char *method, struct blob_attr *msg,
1296 ubus_data_handler_t data_cb, void *data)
1297 {
1298 return ubus_invoke(ubus_ctx, id, method, msg, data_cb, data, 3000);
1299 }
1300
1301 int
1302 netifd_ubus_init(const char *path)
1303 {
1304 uloop_init();
1305 ubus_path = path;
1306
1307 ubus_ctx = ubus_connect(path);
1308 if (!ubus_ctx)
1309 return -EIO;
1310
1311 DPRINTF("connected as %08x\n", ubus_ctx->local_id);
1312 ubus_ctx->connection_lost = netifd_ubus_connection_lost;
1313 netifd_ubus_add_fd();
1314
1315 netifd_add_object(&main_object);
1316 netifd_add_object(&dev_object);
1317 netifd_add_object(&wireless_object);
1318 netifd_add_iface_object();
1319
1320 return 0;
1321 }
1322
1323 void
1324 netifd_ubus_done(void)
1325 {
1326 ubus_free(ubus_ctx);
1327 }
1328
1329 void
1330 netifd_ubus_interface_event(struct interface *iface, bool up)
1331 {
1332 blob_buf_init(&b, 0);
1333 blobmsg_add_string(&b, "action", up ? "ifup" : "ifdown");
1334 blobmsg_add_string(&b, "interface", iface->name);
1335 ubus_send_event(ubus_ctx, "network.interface", b.head);
1336 }
1337
1338 void
1339 netifd_ubus_interface_notify(struct interface *iface, bool up)
1340 {
1341 const char *event = (up) ? "interface.update" : "interface.down";
1342 blob_buf_init(&b, 0);
1343 blobmsg_add_string(&b, "interface", iface->name);
1344 netifd_dump_status(iface);
1345 ubus_notify(ubus_ctx, &iface_object, event, b.head, -1);
1346 ubus_notify(ubus_ctx, &iface->ubus, event, b.head, -1);
1347 }
1348
1349 void
1350 netifd_ubus_add_interface(struct interface *iface)
1351 {
1352 struct ubus_object *obj = &iface->ubus;
1353 char *name = NULL;
1354
1355 if (asprintf(&name, "%s.interface.%s", main_object.name, iface->name) == -1)
1356 return;
1357
1358 obj->name = name;
1359 obj->type = &iface_object_type;
1360 obj->methods = iface_object_methods;
1361 obj->n_methods = ARRAY_SIZE(iface_object_methods);
1362 if (ubus_add_object(ubus_ctx, &iface->ubus)) {
1363 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
1364 free(name);
1365 obj->name = NULL;
1366 }
1367 }
1368
1369 void
1370 netifd_ubus_remove_interface(struct interface *iface)
1371 {
1372 if (!iface->ubus.name)
1373 return;
1374
1375 ubus_remove_object(ubus_ctx, &iface->ubus);
1376 free((void *) iface->ubus.name);
1377 }