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