• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/netifd/ubus.c

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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt