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