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