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