2fd9e92fb82473541d075dbe1f3833456ba51f6e
[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
26 static struct ubus_context *ctx = NULL;
27 static struct blob_buf b;
28 static const char *ubus_path;
29
30 /* global object */
31
32 static int
33 netifd_handle_restart(struct ubus_context *ctx, struct ubus_object *obj,
34 struct ubus_request_data *req, const char *method,
35 struct blob_attr *msg)
36 {
37 netifd_restart();
38 return 0;
39 }
40
41 static int
42 netifd_handle_reload(struct ubus_context *ctx, struct ubus_object *obj,
43 struct ubus_request_data *req, const char *method,
44 struct blob_attr *msg)
45 {
46 netifd_reload();
47 return 0;
48 }
49
50 enum {
51 HR_TARGET,
52 HR_V6,
53 __HR_MAX
54 };
55
56 static const struct blobmsg_policy route_policy[__HR_MAX] = {
57 [HR_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
58 [HR_V6] = { .name = "v6", .type = BLOBMSG_TYPE_BOOL },
59 };
60
61 static int
62 netifd_add_host_route(struct ubus_context *ctx, struct ubus_object *obj,
63 struct ubus_request_data *req, const char *method,
64 struct blob_attr *msg)
65 {
66 struct blob_attr *tb[__HR_MAX];
67 struct interface *iface;
68 union if_addr a;
69 bool v6 = false;
70
71 blobmsg_parse(route_policy, __HR_MAX, tb, blob_data(msg), blob_len(msg));
72 if (!tb[HR_TARGET])
73 return UBUS_STATUS_INVALID_ARGUMENT;
74
75 if (tb[HR_V6])
76 v6 = blobmsg_get_bool(tb[HR_V6]);
77
78 memset(&a, 0, sizeof(a));
79 if (!inet_pton(v6 ? AF_INET6 : AF_INET, blobmsg_data(tb[HR_TARGET]), &a))
80 return UBUS_STATUS_INVALID_ARGUMENT;
81
82
83 iface = interface_ip_add_target_route(&a, v6);
84 if (!iface)
85 return UBUS_STATUS_NOT_FOUND;
86
87 blob_buf_init(&b, 0);
88 blobmsg_add_string(&b, "interface", iface->name);
89 ubus_send_reply(ctx, req, b.head);
90
91 return 0;
92 }
93
94 static int
95 netifd_get_proto_handlers(struct ubus_context *ctx, struct ubus_object *obj,
96 struct ubus_request_data *req, const char *method,
97 struct blob_attr *msg)
98 {
99 blob_buf_init(&b, 0);
100 proto_dump_handlers(&b);
101 ubus_send_reply(ctx, req, b.head);
102
103 return 0;
104 }
105
106 static struct ubus_method main_object_methods[] = {
107 { .name = "restart", .handler = netifd_handle_restart },
108 { .name = "reload", .handler = netifd_handle_reload },
109 UBUS_METHOD("add_host_route", netifd_add_host_route, route_policy),
110 { .name = "get_proto_handlers", .handler = netifd_get_proto_handlers },
111 };
112
113 static struct ubus_object_type main_object_type =
114 UBUS_OBJECT_TYPE("netifd", main_object_methods);
115
116 static struct ubus_object main_object = {
117 .name = "network",
118 .type = &main_object_type,
119 .methods = main_object_methods,
120 .n_methods = ARRAY_SIZE(main_object_methods),
121 };
122
123 enum {
124 DEV_NAME,
125 __DEV_MAX,
126 };
127
128 static const struct blobmsg_policy dev_policy[__DEV_MAX] = {
129 [DEV_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
130 };
131
132 static int
133 netifd_dev_status(struct ubus_context *ctx, struct ubus_object *obj,
134 struct ubus_request_data *req, const char *method,
135 struct blob_attr *msg)
136 {
137 struct device *dev = NULL;
138 struct blob_attr *tb[__DEV_MAX];
139
140 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
141
142 if (tb[DEV_NAME]) {
143 dev = device_get(blobmsg_data(tb[DEV_NAME]), false);
144 if (!dev)
145 return UBUS_STATUS_INVALID_ARGUMENT;
146 }
147
148 blob_buf_init(&b, 0);
149 device_dump_status(&b, dev);
150 ubus_send_reply(ctx, req, b.head);
151
152 return 0;
153 }
154
155 enum {
156 ALIAS_ATTR_ALIAS,
157 ALIAS_ATTR_DEV,
158 __ALIAS_ATTR_MAX,
159 };
160
161 static const struct blobmsg_policy alias_attrs[__ALIAS_ATTR_MAX] = {
162 [ALIAS_ATTR_ALIAS] = { "alias", BLOBMSG_TYPE_ARRAY },
163 [ALIAS_ATTR_DEV] = { "device", BLOBMSG_TYPE_STRING },
164 };
165
166 static int
167 netifd_handle_alias(struct ubus_context *ctx, struct ubus_object *obj,
168 struct ubus_request_data *req, const char *method,
169 struct blob_attr *msg)
170 {
171 struct device *dev = NULL;
172 struct blob_attr *tb[__ALIAS_ATTR_MAX];
173 struct blob_attr *cur;
174 int rem;
175
176 blobmsg_parse(alias_attrs, __ALIAS_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
177
178 if (!tb[ALIAS_ATTR_ALIAS])
179 return UBUS_STATUS_INVALID_ARGUMENT;
180
181 if ((cur = tb[ALIAS_ATTR_DEV]) != NULL) {
182 dev = device_get(blobmsg_data(cur), true);
183 if (!dev)
184 return UBUS_STATUS_NOT_FOUND;
185 }
186
187 blobmsg_for_each_attr(cur, tb[ALIAS_ATTR_ALIAS], rem) {
188 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
189 goto error;
190
191 if (!blobmsg_check_attr(cur, NULL))
192 goto error;
193
194 alias_notify_device(blobmsg_data(cur), dev);
195 }
196 return 0;
197
198 error:
199 device_free_unused(dev);
200 return UBUS_STATUS_INVALID_ARGUMENT;
201 }
202
203 enum {
204 DEV_STATE_NAME,
205 DEV_STATE_DEFER,
206 __DEV_STATE_MAX,
207 };
208
209 static const struct blobmsg_policy dev_state_policy[__DEV_STATE_MAX] = {
210 [DEV_STATE_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
211 [DEV_STATE_DEFER] = { .name = "defer", .type = BLOBMSG_TYPE_BOOL },
212 };
213
214 static int
215 netifd_handle_set_state(struct ubus_context *ctx, struct ubus_object *obj,
216 struct ubus_request_data *req, const char *method,
217 struct blob_attr *msg)
218 {
219 struct device *dev = NULL;
220 struct blob_attr *tb[__DEV_STATE_MAX];
221 struct blob_attr *cur;
222
223 blobmsg_parse(dev_state_policy, __DEV_STATE_MAX, tb, blob_data(msg), blob_len(msg));
224
225 cur = tb[DEV_STATE_NAME];
226 if (!cur)
227 return UBUS_STATUS_INVALID_ARGUMENT;
228
229 dev = device_get(blobmsg_data(cur), false);
230 if (!dev)
231 return UBUS_STATUS_NOT_FOUND;
232
233 cur = tb[DEV_STATE_DEFER];
234 if (cur)
235 device_set_deferred(dev, !!blobmsg_get_u8(cur));
236
237 return 0;
238 }
239
240 static struct ubus_method dev_object_methods[] = {
241 UBUS_METHOD("status", netifd_dev_status, dev_policy),
242 UBUS_METHOD("set_alias", netifd_handle_alias, alias_attrs),
243 UBUS_METHOD("set_state", netifd_handle_set_state, dev_state_policy),
244 };
245
246 static struct ubus_object_type dev_object_type =
247 UBUS_OBJECT_TYPE("device", dev_object_methods);
248
249 static struct ubus_object dev_object = {
250 .name = "network.device",
251 .type = &dev_object_type,
252 .methods = dev_object_methods,
253 .n_methods = ARRAY_SIZE(dev_object_methods),
254 };
255
256 static void
257 netifd_ubus_add_fd(void)
258 {
259 ubus_add_uloop(ctx);
260 system_fd_set_cloexec(ctx->sock.fd);
261 }
262
263 static void
264 netifd_ubus_reconnect_timer(struct uloop_timeout *timeout)
265 {
266 static struct uloop_timeout retry = {
267 .cb = netifd_ubus_reconnect_timer,
268 };
269 int t = 2;
270
271 if (ubus_reconnect(ctx, ubus_path) != 0) {
272 DPRINTF("failed to reconnect, trying again in %d seconds\n", t);
273 uloop_timeout_set(&retry, t * 1000);
274 return;
275 }
276
277 DPRINTF("reconnected to ubus, new id: %08x\n", ctx->local_id);
278 netifd_ubus_add_fd();
279 }
280
281 static void
282 netifd_ubus_connection_lost(struct ubus_context *ctx)
283 {
284 netifd_ubus_reconnect_timer(NULL);
285 }
286
287 /* per-interface object */
288
289 static int
290 netifd_handle_up(struct ubus_context *ctx, struct ubus_object *obj,
291 struct ubus_request_data *req, const char *method,
292 struct blob_attr *msg)
293 {
294 struct interface *iface;
295
296 iface = container_of(obj, struct interface, ubus);
297 interface_set_up(iface);
298
299 return 0;
300 }
301
302 static int
303 netifd_handle_down(struct ubus_context *ctx, struct ubus_object *obj,
304 struct ubus_request_data *req, const char *method,
305 struct blob_attr *msg)
306 {
307 struct interface *iface;
308
309 iface = container_of(obj, struct interface, ubus);
310 interface_set_down(iface);
311
312 return 0;
313 }
314
315 static void
316 netifd_add_interface_errors(struct blob_buf *b, struct interface *iface)
317 {
318 struct interface_error *error;
319 void *e, *e2, *e3;
320 int i;
321
322 e = blobmsg_open_array(b, "errors");
323 list_for_each_entry(error, &iface->errors, list) {
324 e2 = blobmsg_open_table(b, NULL);
325
326 blobmsg_add_string(b, "subsystem", error->subsystem);
327 blobmsg_add_string(b, "code", error->code);
328 if (error->data[0]) {
329 e3 = blobmsg_open_array(b, "data");
330 for (i = 0; error->data[i]; i++)
331 blobmsg_add_string(b, NULL, error->data[i]);
332 blobmsg_close_array(b, e3);
333 }
334
335 blobmsg_close_table(b, e2);
336 }
337 blobmsg_close_array(b, e);
338 }
339
340 static void
341 interface_ip_dump_address_list(struct interface_ip_settings *ip, bool v6,
342 bool enabled)
343 {
344 struct device_addr *addr;
345 char *buf;
346 void *a;
347 int buflen = 128;
348 int af;
349
350 vlist_for_each_element(&ip->addr, addr, node) {
351 if (addr->enabled != enabled)
352 continue;
353
354 if ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
355 af = AF_INET;
356 else
357 af = AF_INET6;
358
359 if (af != (v6 ? AF_INET6 : AF_INET))
360 continue;
361
362 a = blobmsg_open_table(&b, NULL);
363
364 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
365 inet_ntop(af, &addr->addr, buf, buflen);
366 blobmsg_add_string_buffer(&b);
367
368 blobmsg_add_u32(&b, "mask", addr->mask);
369
370 blobmsg_close_table(&b, a);
371 }
372 }
373
374 static void
375 interface_ip_dump_route_list(struct interface_ip_settings *ip, bool enabled)
376 {
377 struct device_route *route;
378 int buflen = 128;
379 char *buf;
380 void *r;
381 int af;
382
383 vlist_for_each_element(&ip->route, route, node) {
384 if (route->enabled != enabled)
385 continue;
386
387 if ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
388 af = AF_INET;
389 else
390 af = AF_INET6;
391
392 r = blobmsg_open_table(&b, NULL);
393
394 buf = blobmsg_alloc_string_buffer(&b, "target", buflen);
395 inet_ntop(af, &route->addr, buf, buflen);
396 blobmsg_add_string_buffer(&b);
397
398 blobmsg_add_u32(&b, "mask", route->mask);
399
400 buf = blobmsg_alloc_string_buffer(&b, "nexthop", buflen);
401 inet_ntop(af, &route->nexthop, buf, buflen);
402 blobmsg_add_string_buffer(&b);
403
404 if (route->flags & DEVROUTE_MTU)
405 blobmsg_add_u32(&b, "mtu", route->mtu);
406
407 if (route->flags & DEVROUTE_METRIC)
408 blobmsg_add_u32(&b, "metric", route->metric);
409
410 blobmsg_close_table(&b, r);
411 }
412 }
413
414
415 static void
416 interface_ip_dump_prefix_list(struct interface_ip_settings *ip)
417 {
418 struct device_prefix *prefix;
419 char *buf;
420 void *a, *c;
421 const int buflen = INET6_ADDRSTRLEN;
422
423 vlist_for_each_element(&ip->prefix, prefix, node) {
424 a = blobmsg_open_table(&b, NULL);
425
426 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
427 inet_ntop(AF_INET6, &prefix->addr, buf, buflen);
428 blobmsg_add_string_buffer(&b);
429
430 blobmsg_add_u32(&b, "mask", prefix->length);
431
432 time_t now = system_get_rtime();
433 if (prefix->preferred_until) {
434 int preferred = prefix->preferred_until - now;
435 if (preferred < 0)
436 preferred = 0;
437 blobmsg_add_u32(&b, "preferred", preferred);
438 }
439
440 if (prefix->valid_until) {
441 int valid = prefix->valid_until - now;
442 if (valid < 0)
443 valid = 0;
444 blobmsg_add_u32(&b, "valid", valid);
445 }
446
447 c = blobmsg_open_table(&b, "assigned");
448 struct device_prefix_assignment *assign;
449 vlist_for_each_element(prefix->assignments, assign, node) {
450 void *d = blobmsg_open_table(&b, assign->name);
451
452 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
453 inet_ntop(AF_INET6, &assign->addr, buf, buflen);
454 blobmsg_add_string_buffer(&b);
455
456 blobmsg_add_u32(&b, "mask", assign->length);
457
458 blobmsg_close_table(&b, d);
459 }
460 blobmsg_close_table(&b, c);
461
462 blobmsg_close_table(&b, a);
463 }
464 }
465
466
467 static void
468 interface_ip_dump_dns_server_list(struct interface_ip_settings *ip,
469 bool enabled)
470 {
471 struct dns_server *dns;
472 int buflen = 128;
473 char *buf;
474
475 vlist_simple_for_each_element(&ip->dns_servers, dns, node) {
476 if (ip->no_dns == enabled)
477 continue;
478
479 buf = blobmsg_alloc_string_buffer(&b, NULL, buflen);
480 inet_ntop(dns->af, &dns->addr, buf, buflen);
481 blobmsg_add_string_buffer(&b);
482 }
483 }
484
485 static void
486 interface_ip_dump_dns_search_list(struct interface_ip_settings *ip,
487 bool enabled)
488 {
489 struct dns_search_domain *dns;
490
491 vlist_simple_for_each_element(&ip->dns_search, dns, node) {
492 if (ip->no_dns == enabled)
493 continue;
494
495 blobmsg_add_string(&b, NULL, dns->name);
496 }
497 }
498
499 static int
500 netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
501 struct ubus_request_data *req, const char *method,
502 struct blob_attr *msg)
503 {
504 struct interface *iface;
505 struct interface_data *data;
506 struct device *dev;
507 void *a, *inactive;
508
509 iface = container_of(obj, struct interface, ubus);
510
511 blob_buf_init(&b, 0);
512 blobmsg_add_u8(&b, "up", iface->state == IFS_UP);
513 blobmsg_add_u8(&b, "pending", iface->state == IFS_SETUP);
514 blobmsg_add_u8(&b, "available", iface->available);
515 blobmsg_add_u8(&b, "autostart", iface->autostart);
516
517 if (iface->state == IFS_UP) {
518 time_t cur = system_get_rtime();
519 blobmsg_add_u32(&b, "uptime", cur - iface->start_time);
520 blobmsg_add_string(&b, "l3_device", iface->l3_dev.dev->ifname);
521 }
522
523 if (iface->proto_handler)
524 blobmsg_add_string(&b, "proto", iface->proto_handler->name);
525
526 dev = iface->main_dev.dev;
527 if (dev && !dev->hidden &&
528 !(iface->proto_handler->flags & PROTO_FLAG_NODEV))
529 blobmsg_add_string(&b, "device", dev->ifname);
530
531 if (iface->state == IFS_UP) {
532 blobmsg_add_u32(&b, "metric", iface->metric);
533 a = blobmsg_open_array(&b, "ipv4-address");
534 interface_ip_dump_address_list(&iface->config_ip, false, true);
535 interface_ip_dump_address_list(&iface->proto_ip, false, true);
536 blobmsg_close_array(&b, a);
537 a = blobmsg_open_array(&b, "ipv6-address");
538 interface_ip_dump_address_list(&iface->config_ip, true, true);
539 interface_ip_dump_address_list(&iface->proto_ip, true, true);
540 blobmsg_close_array(&b, a);
541 a = blobmsg_open_array(&b, "ipv6-prefix");
542 interface_ip_dump_prefix_list(&iface->config_ip);
543 interface_ip_dump_prefix_list(&iface->proto_ip);
544 blobmsg_close_array(&b, a);
545 a = blobmsg_open_array(&b, "route");
546 interface_ip_dump_route_list(&iface->config_ip, true);
547 interface_ip_dump_route_list(&iface->proto_ip, true);
548 blobmsg_close_array(&b, a);
549 a = blobmsg_open_array(&b, "dns-server");
550 interface_ip_dump_dns_server_list(&iface->config_ip, true);
551 interface_ip_dump_dns_server_list(&iface->proto_ip, true);
552 blobmsg_close_array(&b, a);
553 a = blobmsg_open_array(&b, "dns-search");
554 interface_ip_dump_dns_search_list(&iface->config_ip, true);
555 interface_ip_dump_dns_search_list(&iface->proto_ip, true);
556 blobmsg_close_array(&b, a);
557
558 inactive = blobmsg_open_table(&b, "inactive");
559 a = blobmsg_open_array(&b, "ipv4-address");
560 interface_ip_dump_address_list(&iface->config_ip, false, false);
561 interface_ip_dump_address_list(&iface->proto_ip, false, false);
562 blobmsg_close_array(&b, a);
563 a = blobmsg_open_array(&b, "ipv6-address");
564 interface_ip_dump_address_list(&iface->config_ip, true, false);
565 interface_ip_dump_address_list(&iface->proto_ip, true, false);
566 blobmsg_close_array(&b, a);
567 a = blobmsg_open_array(&b, "route");
568 interface_ip_dump_route_list(&iface->config_ip, false);
569 interface_ip_dump_route_list(&iface->proto_ip, false);
570 blobmsg_close_array(&b, a);
571 a = blobmsg_open_array(&b, "dns-server");
572 interface_ip_dump_dns_server_list(&iface->config_ip, false);
573 interface_ip_dump_dns_server_list(&iface->proto_ip, false);
574 blobmsg_close_array(&b, a);
575 a = blobmsg_open_array(&b, "dns-search");
576 interface_ip_dump_dns_search_list(&iface->config_ip, false);
577 interface_ip_dump_dns_search_list(&iface->proto_ip, false);
578 blobmsg_close_array(&b, a);
579 blobmsg_close_table(&b, inactive);
580 }
581
582 a = blobmsg_open_table(&b, "data");
583 avl_for_each_element(&iface->data, data, node)
584 blob_put(&b, blob_id(data->data), blob_data(data->data), blob_len(data->data));
585
586 blobmsg_close_table(&b, a);
587
588 if (!list_is_empty(&iface->errors))
589 netifd_add_interface_errors(&b, iface);
590
591 ubus_send_reply(ctx, req, b.head);
592
593 return 0;
594 }
595
596 static int
597 netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
598 struct ubus_request_data *req, const char *method,
599 struct blob_attr *msg)
600 {
601 struct blob_attr *tb[__DEV_MAX];
602 struct interface *iface;
603 struct device *dev;
604 bool add = !strncmp(method, "add", 3);
605 int ret;
606
607 iface = container_of(obj, struct interface, ubus);
608
609 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
610
611 if (!tb[DEV_NAME])
612 return UBUS_STATUS_INVALID_ARGUMENT;
613
614 device_lock();
615
616 dev = device_get(blobmsg_data(tb[DEV_NAME]), add ? 2 : 0);
617 if (!dev) {
618 ret = UBUS_STATUS_NOT_FOUND;
619 goto out;
620 }
621
622 if (add) {
623 device_set_present(dev, true);
624 if (iface->device_config)
625 device_set_config(dev, &simple_device_type, iface->config);
626
627 system_if_apply_settings(dev, &dev->settings);
628 ret = interface_add_link(iface, dev);
629 } else {
630 ret = interface_remove_link(iface, dev);
631 }
632
633 out:
634 device_unlock();
635
636 return ret;
637 }
638
639
640 static int
641 netifd_iface_notify_proto(struct ubus_context *ctx, struct ubus_object *obj,
642 struct ubus_request_data *req, const char *method,
643 struct blob_attr *msg)
644 {
645 struct interface *iface;
646
647 iface = container_of(obj, struct interface, ubus);
648
649 if (!iface->proto || !iface->proto->notify)
650 return UBUS_STATUS_NOT_SUPPORTED;
651
652 return iface->proto->notify(iface->proto, msg);
653 }
654
655 static void
656 netifd_iface_do_remove(struct uloop_timeout *timeout)
657 {
658 struct interface *iface;
659
660 iface = container_of(timeout, struct interface, remove_timer);
661 vlist_delete(&interfaces, &iface->node);
662 }
663
664 static int
665 netifd_iface_remove(struct ubus_context *ctx, struct ubus_object *obj,
666 struct ubus_request_data *req, const char *method,
667 struct blob_attr *msg)
668 {
669 struct interface *iface;
670
671 iface = container_of(obj, struct interface, ubus);
672 if (iface->remove_timer.cb)
673 return UBUS_STATUS_INVALID_ARGUMENT;
674
675 iface->remove_timer.cb = netifd_iface_do_remove;
676 uloop_timeout_set(&iface->remove_timer, 100);
677 return 0;
678 }
679
680 static int
681 netifd_handle_iface_prepare(struct ubus_context *ctx, struct ubus_object *obj,
682 struct ubus_request_data *req, const char *method,
683 struct blob_attr *msg)
684 {
685 struct interface *iface;
686 struct device *dev;
687 const struct device_hotplug_ops *ops;
688
689 iface = container_of(obj, struct interface, ubus);
690 dev = iface->main_dev.dev;
691 if (!dev)
692 return 0;
693
694 ops = dev->hotplug_ops;
695 if (!ops)
696 return 0;
697
698 return ops->prepare(dev);
699 }
700
701 static int
702 netifd_handle_set_data(struct ubus_context *ctx, struct ubus_object *obj,
703 struct ubus_request_data *req, const char *method,
704 struct blob_attr *msg)
705 {
706 struct interface *iface;
707 struct blob_attr *cur;
708 int rem, ret;
709
710 iface = container_of(obj, struct interface, ubus);
711
712 blob_for_each_attr(cur, msg, rem) {
713 ret = interface_add_data(iface, cur);
714 if (ret)
715 return ret;
716 }
717
718 return 0;
719 }
720
721 static struct ubus_method iface_object_methods[] = {
722 { .name = "up", .handler = netifd_handle_up },
723 { .name = "down", .handler = netifd_handle_down },
724 { .name = "status", .handler = netifd_handle_status },
725 { .name = "prepare", .handler = netifd_handle_iface_prepare },
726 UBUS_METHOD("add_device", netifd_iface_handle_device, dev_policy ),
727 UBUS_METHOD("remove_device", netifd_iface_handle_device, dev_policy ),
728 { .name = "notify_proto", .handler = netifd_iface_notify_proto },
729 { .name = "remove", .handler = netifd_iface_remove },
730 { .name = "set_data", .handler = netifd_handle_set_data },
731 };
732
733 static struct ubus_object_type iface_object_type =
734 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
735
736
737 static struct ubus_object iface_object = {
738 .name = "network.interface",
739 .type = &iface_object_type,
740 .n_methods = ARRAY_SIZE(iface_object_methods),
741 };
742
743 static void netifd_add_object(struct ubus_object *obj)
744 {
745 int ret = ubus_add_object(ctx, obj);
746
747 if (ret != 0)
748 fprintf(stderr, "Failed to publish object '%s': %s\n", obj->name, ubus_strerror(ret));
749 }
750
751 static const struct blobmsg_policy iface_policy = {
752 .name = "interface",
753 .type = BLOBMSG_TYPE_STRING,
754 };
755
756 static int
757 netifd_handle_iface(struct ubus_context *ctx, struct ubus_object *obj,
758 struct ubus_request_data *req, const char *method,
759 struct blob_attr *msg)
760 {
761 struct interface *iface;
762 struct blob_attr *tb;
763 int i;
764
765 blobmsg_parse(&iface_policy, 1, &tb, blob_data(msg), blob_len(msg));
766 if (!tb)
767 return UBUS_STATUS_INVALID_ARGUMENT;
768
769 iface = vlist_find(&interfaces, blobmsg_data(tb), iface, node);
770 if (!iface)
771 return UBUS_STATUS_NOT_FOUND;
772
773 for (i = 0; i < ARRAY_SIZE(iface_object_methods); i++) {
774 ubus_handler_t cb;
775
776 if (strcmp(method, iface_object_methods[i].name) != 0)
777 continue;
778
779 cb = iface_object_methods[i].handler;
780 return cb(ctx, &iface->ubus, req, method, msg);
781 }
782
783 return UBUS_STATUS_INVALID_ARGUMENT;
784 }
785
786 static void netifd_add_iface_object(void)
787 {
788 struct ubus_method *methods;
789 int i;
790
791 methods = calloc(1, sizeof(iface_object_methods));
792 memcpy(methods, iface_object_methods, sizeof(iface_object_methods));
793 iface_object.methods = methods;
794
795 for (i = 0; i < ARRAY_SIZE(iface_object_methods); i++) {
796 methods[i].handler = netifd_handle_iface;
797 methods[i].policy = &iface_policy;
798 methods[i].n_policy = 1;
799 }
800 netifd_add_object(&iface_object);
801 }
802
803 int
804 netifd_ubus_init(const char *path)
805 {
806 uloop_init();
807 ubus_path = path;
808
809 ctx = ubus_connect(path);
810 if (!ctx)
811 return -EIO;
812
813 DPRINTF("connected as %08x\n", ctx->local_id);
814 ctx->connection_lost = netifd_ubus_connection_lost;
815 netifd_ubus_add_fd();
816
817 netifd_add_object(&main_object);
818 netifd_add_object(&dev_object);
819 netifd_add_iface_object();
820
821 return 0;
822 }
823
824 void
825 netifd_ubus_done(void)
826 {
827 ubus_free(ctx);
828 }
829
830 void
831 netifd_ubus_interface_event(struct interface *iface, bool up)
832 {
833 blob_buf_init(&b, 0);
834 blobmsg_add_string(&b, "action", up ? "ifup" : "ifdown");
835 blobmsg_add_string(&b, "interface", iface->name);
836 ubus_send_event(ctx, "network.interface", b.head);
837 }
838
839 void
840 netifd_ubus_add_interface(struct interface *iface)
841 {
842 struct ubus_object *obj = &iface->ubus;
843 char *name = NULL;
844
845 if (asprintf(&name, "%s.interface.%s", main_object.name, iface->name) == -1)
846 return;
847
848 obj->name = name;
849 obj->type = &iface_object_type;
850 obj->methods = iface_object_methods;
851 obj->n_methods = ARRAY_SIZE(iface_object_methods);
852 if (ubus_add_object(ctx, &iface->ubus)) {
853 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
854 free(name);
855 obj->name = NULL;
856 }
857 }
858
859 void
860 netifd_ubus_remove_interface(struct interface *iface)
861 {
862 if (!iface->ubus.name)
863 return;
864
865 ubus_remove_object(ctx, &iface->ubus);
866 free((void *) iface->ubus.name);
867 }