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