2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
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
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.
16 #include <arpa/inet.h>
21 #include "interface.h"
27 struct ubus_context
*ubus_ctx
= NULL
;
28 static struct blob_buf b
;
29 static const char *ubus_path
;
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
)
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
)
48 return UBUS_STATUS_NOT_FOUND
;
50 return UBUS_STATUS_OK
;
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
},
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
)
71 struct blob_attr
*tb
[__HR_MAX
];
72 struct interface
*iface
= NULL
;
76 blobmsg_parse(route_policy
, __HR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
78 return UBUS_STATUS_INVALID_ARGUMENT
;
81 v6
= blobmsg_get_bool(tb
[HR_V6
]);
84 iface
= vlist_find(&interfaces
, blobmsg_data(tb
[HR_INTERFACE
]), iface
, node
);
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
;
91 iface
= interface_ip_add_target_route(&a
, v6
, iface
);
93 return UBUS_STATUS_NOT_FOUND
;
96 blobmsg_add_string(&b
, "interface", iface
->name
);
97 ubus_send_reply(ctx
, req
, b
.head
);
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
)
107 blob_buf_init(&b
, 0);
108 proto_dump_handlers(&b
);
109 ubus_send_reply(ctx
, req
, b
.head
);
120 static const struct blobmsg_policy dynamic_policy
[__DI_MAX
] = {
121 [DI_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
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
)
129 struct blob_attr
*tb
[__DI_MAX
];
130 struct interface
*iface
;
131 struct blob_attr
*config
;
133 blobmsg_parse(dynamic_policy
, __DI_MAX
, tb
, blob_data(msg
), blob_len(msg
));
136 return UBUS_STATUS_INVALID_ARGUMENT
;
138 const char *name
= blobmsg_get_string(tb
[DI_NAME
]);
140 iface
= interface_alloc(name
, msg
, true);
142 return UBUS_STATUS_UNKNOWN_ERROR
;
144 config
= blob_memdup(msg
);
148 if (!interface_add(iface
, config
))
149 goto error_free_config
;
151 return UBUS_STATUS_OK
;
157 return UBUS_STATUS_UNKNOWN_ERROR
;
167 static const struct blobmsg_policy netns_updown_policy
[__NETNS_UPDOWN_MAX
] = {
168 [NETNS_UPDOWN_JAIL
] = { .name
= "jail", .type
= BLOBMSG_TYPE_STRING
},
169 [NETNS_UPDOWN_PID
] = { .name
= "pid", .type
= BLOBMSG_TYPE_INT32
},
170 [NETNS_UPDOWN_START
] = { .name
= "start", .type
= BLOBMSG_TYPE_BOOL
},
174 netifd_netns_updown(struct ubus_context
*ctx
, struct ubus_object
*obj
,
175 struct ubus_request_data
*req
, const char *method
,
176 struct blob_attr
*msg
)
178 struct blob_attr
*tb
[__NETNS_UPDOWN_MAX
];
183 blobmsg_parse(netns_updown_policy
, __NETNS_UPDOWN_MAX
, tb
, blob_data(msg
), blob_len(msg
));
184 if (!tb
[NETNS_UPDOWN_JAIL
] || !tb
[NETNS_UPDOWN_PID
])
185 return UBUS_STATUS_INVALID_ARGUMENT
;
187 start
= tb
[NETNS_UPDOWN_START
] && blobmsg_get_bool(tb
[NETNS_UPDOWN_START
]);
188 jail
= blobmsg_get_string(tb
[NETNS_UPDOWN_JAIL
]);
189 netns_pid
= blobmsg_get_u32(tb
[NETNS_UPDOWN_PID
]);
192 interface_start_jail(jail
, netns_pid
);
194 interface_stop_jail(jail
, netns_pid
);
196 return UBUS_STATUS_OK
;
199 static struct ubus_method main_object_methods
[] = {
200 { .name
= "restart", .handler
= netifd_handle_restart
},
201 { .name
= "reload", .handler
= netifd_handle_reload
},
202 UBUS_METHOD("add_host_route", netifd_add_host_route
, route_policy
),
203 { .name
= "get_proto_handlers", .handler
= netifd_get_proto_handlers
},
204 UBUS_METHOD("add_dynamic", netifd_add_dynamic
, dynamic_policy
),
205 UBUS_METHOD("netns_updown", netifd_netns_updown
, netns_updown_policy
),
208 static struct ubus_object_type main_object_type
=
209 UBUS_OBJECT_TYPE("netifd", main_object_methods
);
211 static struct ubus_object main_object
= {
213 .type
= &main_object_type
,
214 .methods
= main_object_methods
,
215 .n_methods
= ARRAY_SIZE(main_object_methods
),
223 static const struct blobmsg_policy dev_policy
[__DEV_MAX
] = {
224 [DEV_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
228 netifd_dev_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
229 struct ubus_request_data
*req
, const char *method
,
230 struct blob_attr
*msg
)
232 struct device
*dev
= NULL
;
233 struct blob_attr
*tb
[__DEV_MAX
];
235 blobmsg_parse(dev_policy
, __DEV_MAX
, tb
, blob_data(msg
), blob_len(msg
));
238 dev
= device_find(blobmsg_data(tb
[DEV_NAME
]));
240 return UBUS_STATUS_INVALID_ARGUMENT
;
243 blob_buf_init(&b
, 0);
244 device_dump_status(&b
, dev
);
245 ubus_send_reply(ctx
, req
, b
.head
);
256 static const struct blobmsg_policy alias_attrs
[__ALIAS_ATTR_MAX
] = {
257 [ALIAS_ATTR_ALIAS
] = { "alias", BLOBMSG_TYPE_ARRAY
},
258 [ALIAS_ATTR_DEV
] = { "device", BLOBMSG_TYPE_STRING
},
262 netifd_handle_alias(struct ubus_context
*ctx
, struct ubus_object
*obj
,
263 struct ubus_request_data
*req
, const char *method
,
264 struct blob_attr
*msg
)
266 struct device
*dev
= NULL
;
267 struct blob_attr
*tb
[__ALIAS_ATTR_MAX
];
268 struct blob_attr
*cur
;
271 blobmsg_parse(alias_attrs
, __ALIAS_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
273 if (!tb
[ALIAS_ATTR_ALIAS
])
274 return UBUS_STATUS_INVALID_ARGUMENT
;
276 if ((cur
= tb
[ALIAS_ATTR_DEV
]) != NULL
) {
277 dev
= device_get(blobmsg_data(cur
), true);
279 return UBUS_STATUS_NOT_FOUND
;
282 blobmsg_for_each_attr(cur
, tb
[ALIAS_ATTR_ALIAS
], rem
) {
283 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
286 if (!blobmsg_check_attr(cur
, false))
289 alias_notify_device(blobmsg_data(cur
), dev
);
294 device_free_unused(dev
);
295 return UBUS_STATUS_INVALID_ARGUMENT
;
304 static const struct blobmsg_policy dev_state_policy
[__DEV_STATE_MAX
] = {
305 [DEV_STATE_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
306 [DEV_STATE_DEFER
] = { .name
= "defer", .type
= BLOBMSG_TYPE_BOOL
},
310 netifd_handle_set_state(struct ubus_context
*ctx
, struct ubus_object
*obj
,
311 struct ubus_request_data
*req
, const char *method
,
312 struct blob_attr
*msg
)
314 struct device
*dev
= NULL
;
315 struct blob_attr
*tb
[__DEV_STATE_MAX
];
316 struct blob_attr
*cur
;
318 blobmsg_parse(dev_state_policy
, __DEV_STATE_MAX
, tb
, blob_data(msg
), blob_len(msg
));
320 cur
= tb
[DEV_STATE_NAME
];
322 return UBUS_STATUS_INVALID_ARGUMENT
;
324 dev
= device_find(blobmsg_data(cur
));
326 return UBUS_STATUS_NOT_FOUND
;
328 cur
= tb
[DEV_STATE_DEFER
];
330 device_set_deferred(dev
, !!blobmsg_get_u8(cur
));
335 static struct ubus_method dev_object_methods
[] = {
336 UBUS_METHOD("status", netifd_dev_status
, dev_policy
),
337 UBUS_METHOD("set_alias", netifd_handle_alias
, alias_attrs
),
338 UBUS_METHOD("set_state", netifd_handle_set_state
, dev_state_policy
),
341 static struct ubus_object_type dev_object_type
=
342 UBUS_OBJECT_TYPE("device", dev_object_methods
);
344 static struct ubus_object dev_object
= {
345 .name
= "network.device",
346 .type
= &dev_object_type
,
347 .methods
= dev_object_methods
,
348 .n_methods
= ARRAY_SIZE(dev_object_methods
),
352 netifd_ubus_add_fd(void)
354 ubus_add_uloop(ubus_ctx
);
355 system_fd_set_cloexec(ubus_ctx
->sock
.fd
);
359 netifd_ubus_reconnect_timer(struct uloop_timeout
*timeout
)
361 static struct uloop_timeout retry
= {
362 .cb
= netifd_ubus_reconnect_timer
,
366 if (ubus_reconnect(ubus_ctx
, ubus_path
) != 0) {
367 DPRINTF("failed to reconnect, trying again in %d seconds\n", t
);
368 uloop_timeout_set(&retry
, t
* 1000);
372 DPRINTF("reconnected to ubus, new id: %08x\n", ubus_ctx
->local_id
);
373 netifd_ubus_add_fd();
377 netifd_ubus_connection_lost(struct ubus_context
*ctx
)
379 netifd_ubus_reconnect_timer(NULL
);
382 /* per-interface object */
385 netifd_handle_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
386 struct ubus_request_data
*req
, const char *method
,
387 struct blob_attr
*msg
)
389 struct interface
*iface
;
391 iface
= container_of(obj
, struct interface
, ubus
);
392 interface_set_up(iface
);
398 netifd_handle_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
399 struct ubus_request_data
*req
, const char *method
,
400 struct blob_attr
*msg
)
402 struct interface
*iface
;
404 iface
= container_of(obj
, struct interface
, ubus
);
405 interface_set_down(iface
);
411 netifd_handle_renew(struct ubus_context
*ctx
, struct ubus_object
*obj
,
412 struct ubus_request_data
*req
, const char *method
,
413 struct blob_attr
*msg
)
415 struct interface
*iface
;
417 iface
= container_of(obj
, struct interface
, ubus
);
418 interface_renew(iface
);
424 netifd_add_interface_errors(struct blob_buf
*b
, struct interface
*iface
)
426 struct interface_error
*error
;
430 e
= blobmsg_open_array(b
, "errors");
431 list_for_each_entry(error
, &iface
->errors
, list
) {
432 e2
= blobmsg_open_table(b
, NULL
);
434 blobmsg_add_string(b
, "subsystem", error
->subsystem
);
435 blobmsg_add_string(b
, "code", error
->code
);
436 if (error
->data
[0]) {
437 e3
= blobmsg_open_array(b
, "data");
438 for (i
= 0; error
->data
[i
]; i
++)
439 blobmsg_add_string(b
, NULL
, error
->data
[i
]);
440 blobmsg_close_array(b
, e3
);
443 blobmsg_close_table(b
, e2
);
445 blobmsg_close_array(b
, e
);
449 interface_ip_dump_address_list(struct interface_ip_settings
*ip
, bool v6
, bool enabled
)
451 struct device_addr
*addr
;
457 time_t now
= system_get_rtime();
458 vlist_for_each_element(&ip
->addr
, addr
, node
) {
459 if (addr
->enabled
!= enabled
)
462 if ((addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
467 if (af
!= (v6
? AF_INET6
: AF_INET
))
470 a
= blobmsg_open_table(&b
, NULL
);
472 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
473 inet_ntop(af
, &addr
->addr
, buf
, buflen
);
474 blobmsg_add_string_buffer(&b
);
476 blobmsg_add_u32(&b
, "mask", addr
->mask
);
478 if (addr
->point_to_point
) {
479 buf
= blobmsg_alloc_string_buffer(&b
, "ptpaddress", buflen
);
480 inet_ntop(af
, &addr
->point_to_point
, buf
, buflen
);
481 blobmsg_add_string_buffer(&b
);
484 if (addr
->preferred_until
) {
485 int preferred
= addr
->preferred_until
- now
;
488 blobmsg_add_u32(&b
, "preferred", preferred
);
491 if (addr
->valid_until
)
492 blobmsg_add_u32(&b
, "valid", addr
->valid_until
- now
);
495 blobmsg_add_string(&b
, "class", addr
->pclass
);
497 blobmsg_close_table(&b
, a
);
502 interface_ip_dump_neighbor_list(struct interface_ip_settings
*ip
, bool enabled
)
504 struct device_neighbor
*neighbor
;
510 vlist_for_each_element(&ip
->neighbor
, neighbor
, node
) {
511 if (neighbor
->enabled
!= enabled
)
514 if ((neighbor
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
519 r
= blobmsg_open_table(&b
, NULL
);
521 if (neighbor
->flags
& DEVNEIGH_MAC
)
522 blobmsg_add_string(&b
, "mac", format_macaddr(neighbor
->macaddr
));
524 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
525 inet_ntop(af
, &neighbor
->addr
, buf
, buflen
);
526 blobmsg_add_string_buffer(&b
);
529 blobmsg_add_u32(&b
, "proxy", neighbor
->proxy
);
531 if (neighbor
->router
)
532 blobmsg_add_u32(&b
, "router", neighbor
->router
);
534 blobmsg_close_table(&b
, r
);
539 interface_ip_dump_route_list(struct interface_ip_settings
*ip
, bool enabled
)
541 struct device_route
*route
;
547 time_t now
= system_get_rtime();
548 vlist_for_each_element(&ip
->route
, route
, node
) {
549 if (route
->enabled
!= enabled
)
552 if ((ip
->no_defaultroute
== enabled
) && !route
->mask
)
555 if ((route
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
560 r
= blobmsg_open_table(&b
, NULL
);
562 buf
= blobmsg_alloc_string_buffer(&b
, "target", buflen
);
563 inet_ntop(af
, &route
->addr
, buf
, buflen
);
564 blobmsg_add_string_buffer(&b
);
566 blobmsg_add_u32(&b
, "mask", route
->mask
);
568 buf
= blobmsg_alloc_string_buffer(&b
, "nexthop", buflen
);
569 inet_ntop(af
, &route
->nexthop
, buf
, buflen
);
570 blobmsg_add_string_buffer(&b
);
572 if (route
->flags
& DEVROUTE_TYPE
)
573 blobmsg_add_u32(&b
, "type", route
->type
);
575 if (route
->flags
& DEVROUTE_PROTO
)
576 blobmsg_add_u32(&b
, "proto", route
->proto
);
578 if (route
->flags
& DEVROUTE_MTU
)
579 blobmsg_add_u32(&b
, "mtu", route
->mtu
);
581 if (route
->flags
& DEVROUTE_METRIC
)
582 blobmsg_add_u32(&b
, "metric", route
->metric
);
584 if (route
->flags
& DEVROUTE_TABLE
)
585 blobmsg_add_u32(&b
, "table", route
->table
);
587 if (route
->valid_until
)
588 blobmsg_add_u32(&b
, "valid", route
->valid_until
- now
);
590 buf
= blobmsg_alloc_string_buffer(&b
, "source", buflen
);
591 inet_ntop(af
, &route
->source
, buf
, buflen
);
592 snprintf(buf
+ strlen(buf
), buflen
- strlen(buf
), "/%u", route
->sourcemask
);
593 blobmsg_add_string_buffer(&b
);
595 blobmsg_close_table(&b
, r
);
601 interface_ip_dump_prefix_list(struct interface_ip_settings
*ip
)
603 struct device_prefix
*prefix
;
606 const int buflen
= INET6_ADDRSTRLEN
;
608 time_t now
= system_get_rtime();
609 vlist_for_each_element(&ip
->prefix
, prefix
, node
) {
610 a
= blobmsg_open_table(&b
, NULL
);
612 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
613 inet_ntop(AF_INET6
, &prefix
->addr
, buf
, buflen
);
614 blobmsg_add_string_buffer(&b
);
616 blobmsg_add_u32(&b
, "mask", prefix
->length
);
618 if (prefix
->preferred_until
) {
619 int preferred
= prefix
->preferred_until
- now
;
622 blobmsg_add_u32(&b
, "preferred", preferred
);
625 if (prefix
->valid_until
)
626 blobmsg_add_u32(&b
, "valid", prefix
->valid_until
- now
);
628 blobmsg_add_string(&b
, "class", prefix
->pclass
);
630 c
= blobmsg_open_table(&b
, "assigned");
631 struct device_prefix_assignment
*assign
;
632 list_for_each_entry(assign
, &prefix
->assignments
, head
) {
633 if (!assign
->name
[0])
636 struct in6_addr addr
= prefix
->addr
;
637 addr
.s6_addr32
[1] |= htonl(assign
->assigned
);
639 void *d
= blobmsg_open_table(&b
, assign
->name
);
641 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
642 inet_ntop(AF_INET6
, &addr
, buf
, buflen
);
643 blobmsg_add_string_buffer(&b
);
645 blobmsg_add_u32(&b
, "mask", assign
->length
);
647 blobmsg_close_table(&b
, d
);
649 blobmsg_close_table(&b
, c
);
651 blobmsg_close_table(&b
, a
);
657 interface_ip_dump_prefix_assignment_list(struct interface
*iface
)
661 const int buflen
= INET6_ADDRSTRLEN
;
662 time_t now
= system_get_rtime();
664 struct device_prefix
*prefix
;
665 list_for_each_entry(prefix
, &prefixes
, head
) {
666 struct device_prefix_assignment
*assign
;
667 list_for_each_entry(assign
, &prefix
->assignments
, head
) {
668 if (strcmp(assign
->name
, iface
->name
))
671 struct in6_addr addr
= prefix
->addr
;
672 addr
.s6_addr32
[1] |= htonl(assign
->assigned
);
674 a
= blobmsg_open_table(&b
, NULL
);
676 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
677 inet_ntop(AF_INET6
, &addr
, buf
, buflen
);
678 blobmsg_add_string_buffer(&b
);
680 blobmsg_add_u32(&b
, "mask", assign
->length
);
682 if (prefix
->preferred_until
) {
683 int preferred
= prefix
->preferred_until
- now
;
686 blobmsg_add_u32(&b
, "preferred", preferred
);
689 if (prefix
->valid_until
)
690 blobmsg_add_u32(&b
, "valid", prefix
->valid_until
- now
);
692 void *c
= blobmsg_open_table(&b
, "local-address");
693 if (assign
->enabled
) {
694 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
695 inet_ntop(AF_INET6
, &assign
->addr
, buf
, buflen
);
696 blobmsg_add_string_buffer(&b
);
698 blobmsg_add_u32(&b
, "mask", assign
->length
);
700 blobmsg_close_table(&b
, c
);
702 blobmsg_close_table(&b
, a
);
708 interface_ip_dump_dns_server_list(struct interface_ip_settings
*ip
, bool enabled
)
710 struct dns_server
*dns
;
714 vlist_simple_for_each_element(&ip
->dns_servers
, dns
, node
) {
715 if (ip
->no_dns
== enabled
)
718 buf
= blobmsg_alloc_string_buffer(&b
, NULL
, buflen
);
719 inet_ntop(dns
->af
, &dns
->addr
, buf
, buflen
);
720 blobmsg_add_string_buffer(&b
);
725 interface_ip_dump_dns_search_list(struct interface_ip_settings
*ip
, bool enabled
)
727 struct dns_search_domain
*dns
;
729 vlist_simple_for_each_element(&ip
->dns_search
, dns
, node
) {
730 if (ip
->no_dns
== enabled
)
733 blobmsg_add_string(&b
, NULL
, dns
->name
);
738 netifd_dump_status(struct interface
*iface
)
740 struct interface_data
*data
;
744 blobmsg_add_u8(&b
, "up", iface
->state
== IFS_UP
);
745 blobmsg_add_u8(&b
, "pending", iface
->state
== IFS_SETUP
);
746 blobmsg_add_u8(&b
, "available", iface
->available
);
747 blobmsg_add_u8(&b
, "autostart", iface
->autostart
);
748 blobmsg_add_u8(&b
, "dynamic", iface
->dynamic
);
750 if (iface
->state
== IFS_UP
) {
751 time_t cur
= system_get_rtime();
752 blobmsg_add_u32(&b
, "uptime", cur
- iface
->start_time
);
753 if (iface
->l3_dev
.dev
)
754 blobmsg_add_string(&b
, "l3_device", iface
->l3_dev
.dev
->ifname
);
757 if (iface
->proto_handler
)
758 blobmsg_add_string(&b
, "proto", iface
->proto_handler
->name
);
760 dev
= iface
->main_dev
.dev
;
761 if (dev
&& !dev
->hidden
&& iface
->proto_handler
&&
762 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
))
763 blobmsg_add_string(&b
, "device", dev
->ifname
);
766 blobmsg_add_string(&b
, "jail", iface
->jail
);
768 if (iface
->jail_ifname
)
769 blobmsg_add_string(&b
, "jail_ifname", iface
->jail_ifname
);
771 if (iface
->state
== IFS_UP
) {
772 if (iface
->updated
) {
773 a
= blobmsg_open_array(&b
, "updated");
775 if (iface
->updated
& IUF_ADDRESS
)
776 blobmsg_add_string(&b
, NULL
, "addresses");
777 if (iface
->updated
& IUF_ROUTE
)
778 blobmsg_add_string(&b
, NULL
, "routes");
779 if (iface
->updated
& IUF_PREFIX
)
780 blobmsg_add_string(&b
, NULL
, "prefixes");
781 if (iface
->updated
& IUF_DATA
)
782 blobmsg_add_string(&b
, NULL
, "data");
784 blobmsg_close_array(&b
, a
);
788 blobmsg_add_u32(&b
, "ip4table", iface
->ip4table
);
790 blobmsg_add_u32(&b
, "ip6table", iface
->ip6table
);
791 blobmsg_add_u32(&b
, "metric", iface
->metric
);
792 blobmsg_add_u32(&b
, "dns_metric", iface
->dns_metric
);
793 blobmsg_add_u8(&b
, "delegation", !iface
->proto_ip
.no_delegation
);
794 if (iface
->assignment_weight
)
795 blobmsg_add_u32(&b
, "ip6weight", iface
->assignment_weight
);
796 a
= blobmsg_open_array(&b
, "ipv4-address");
797 interface_ip_dump_address_list(&iface
->config_ip
, false, true);
798 interface_ip_dump_address_list(&iface
->proto_ip
, false, true);
799 blobmsg_close_array(&b
, a
);
800 a
= blobmsg_open_array(&b
, "ipv6-address");
801 interface_ip_dump_address_list(&iface
->config_ip
, true, true);
802 interface_ip_dump_address_list(&iface
->proto_ip
, true, true);
803 blobmsg_close_array(&b
, a
);
804 a
= blobmsg_open_array(&b
, "ipv6-prefix");
805 interface_ip_dump_prefix_list(&iface
->config_ip
);
806 interface_ip_dump_prefix_list(&iface
->proto_ip
);
807 blobmsg_close_array(&b
, a
);
808 a
= blobmsg_open_array(&b
, "ipv6-prefix-assignment");
809 interface_ip_dump_prefix_assignment_list(iface
);
810 blobmsg_close_array(&b
, a
);
811 a
= blobmsg_open_array(&b
, "route");
812 interface_ip_dump_route_list(&iface
->config_ip
, true);
813 interface_ip_dump_route_list(&iface
->proto_ip
, true);
814 blobmsg_close_array(&b
, a
);
815 a
= blobmsg_open_array(&b
, "dns-server");
816 interface_ip_dump_dns_server_list(&iface
->config_ip
, true);
817 interface_ip_dump_dns_server_list(&iface
->proto_ip
, true);
818 blobmsg_close_array(&b
, a
);
819 a
= blobmsg_open_array(&b
, "dns-search");
820 interface_ip_dump_dns_search_list(&iface
->config_ip
, true);
821 interface_ip_dump_dns_search_list(&iface
->proto_ip
, true);
822 blobmsg_close_array(&b
, a
);
823 a
= blobmsg_open_array(&b
, "neighbors");
824 interface_ip_dump_neighbor_list(&iface
->config_ip
, true);
825 interface_ip_dump_neighbor_list(&iface
->proto_ip
, true);
826 blobmsg_close_array(&b
, a
);
828 inactive
= blobmsg_open_table(&b
, "inactive");
829 a
= blobmsg_open_array(&b
, "ipv4-address");
830 interface_ip_dump_address_list(&iface
->config_ip
, false, false);
831 interface_ip_dump_address_list(&iface
->proto_ip
, false, false);
832 blobmsg_close_array(&b
, a
);
833 a
= blobmsg_open_array(&b
, "ipv6-address");
834 interface_ip_dump_address_list(&iface
->config_ip
, true, false);
835 interface_ip_dump_address_list(&iface
->proto_ip
, true, false);
836 blobmsg_close_array(&b
, a
);
837 a
= blobmsg_open_array(&b
, "route");
838 interface_ip_dump_route_list(&iface
->config_ip
, false);
839 interface_ip_dump_route_list(&iface
->proto_ip
, false);
840 blobmsg_close_array(&b
, a
);
841 a
= blobmsg_open_array(&b
, "dns-server");
842 interface_ip_dump_dns_server_list(&iface
->config_ip
, false);
843 interface_ip_dump_dns_server_list(&iface
->proto_ip
, false);
844 blobmsg_close_array(&b
, a
);
845 a
= blobmsg_open_array(&b
, "dns-search");
846 interface_ip_dump_dns_search_list(&iface
->config_ip
, false);
847 interface_ip_dump_dns_search_list(&iface
->proto_ip
, false);
848 blobmsg_close_array(&b
, a
);
849 a
= blobmsg_open_array(&b
, "neighbors");
850 interface_ip_dump_neighbor_list(&iface
->config_ip
, false);
851 interface_ip_dump_neighbor_list(&iface
->proto_ip
, false);
852 blobmsg_close_array(&b
, a
);
853 blobmsg_close_table(&b
, inactive
);
856 a
= blobmsg_open_table(&b
, "data");
857 avl_for_each_element(&iface
->data
, data
, node
)
858 blobmsg_add_blob(&b
, data
->data
);
860 blobmsg_close_table(&b
, a
);
862 if (!list_empty(&iface
->errors
))
863 netifd_add_interface_errors(&b
, iface
);
867 netifd_handle_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
868 struct ubus_request_data
*req
, const char *method
,
869 struct blob_attr
*msg
)
871 struct interface
*iface
= container_of(obj
, struct interface
, ubus
);
873 blob_buf_init(&b
, 0);
874 netifd_dump_status(iface
);
875 ubus_send_reply(ctx
, req
, b
.head
);
882 netifd_handle_dump(struct ubus_context
*ctx
, struct ubus_object
*obj
,
883 struct ubus_request_data
*req
, const char *method
,
884 struct blob_attr
*msg
)
886 blob_buf_init(&b
, 0);
887 void *a
= blobmsg_open_array(&b
, "interface");
889 struct interface
*iface
;
890 vlist_for_each_element(&interfaces
, iface
, node
) {
891 void *i
= blobmsg_open_table(&b
, NULL
);
892 blobmsg_add_string(&b
, "interface", iface
->name
);
893 netifd_dump_status(iface
);
894 blobmsg_close_table(&b
, i
);
897 blobmsg_close_array(&b
, a
);
898 ubus_send_reply(ctx
, req
, b
.head
);
910 static const struct blobmsg_policy dev_link_policy
[__DEV_LINK_MAX
] = {
911 [DEV_LINK_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
912 [DEV_LINK_EXT
] = { .name
= "link-ext", .type
= BLOBMSG_TYPE_BOOL
},
913 [DEV_LINK_VLAN
] = { .name
= "vlan", .type
= BLOBMSG_TYPE_ARRAY
},
917 netifd_iface_handle_device(struct ubus_context
*ctx
, struct ubus_object
*obj
,
918 struct ubus_request_data
*req
, const char *method
,
919 struct blob_attr
*msg
)
921 struct blob_attr
*tb
[__DEV_LINK_MAX
];
922 struct blob_attr
*cur
;
923 struct interface
*iface
;
924 bool add
= !strncmp(method
, "add", 3);
925 bool link_ext
= true;
927 iface
= container_of(obj
, struct interface
, ubus
);
929 blobmsg_parse(dev_link_policy
, __DEV_LINK_MAX
, tb
, blob_data(msg
), blob_len(msg
));
931 if (!tb
[DEV_LINK_NAME
])
932 return UBUS_STATUS_INVALID_ARGUMENT
;
934 cur
= tb
[DEV_LINK_EXT
];
936 link_ext
= blobmsg_get_bool(cur
);
938 return interface_handle_link(iface
, blobmsg_data(tb
[DEV_LINK_NAME
]),
939 tb
[DEV_LINK_VLAN
], add
, link_ext
);
944 netifd_iface_notify_proto(struct ubus_context
*ctx
, struct ubus_object
*obj
,
945 struct ubus_request_data
*req
, const char *method
,
946 struct blob_attr
*msg
)
948 struct interface
*iface
;
950 iface
= container_of(obj
, struct interface
, ubus
);
952 if (!iface
->proto
|| !iface
->proto
->notify
)
953 return UBUS_STATUS_NOT_SUPPORTED
;
955 return iface
->proto
->notify(iface
->proto
, msg
);
959 netifd_iface_do_remove(struct uloop_timeout
*timeout
)
961 struct interface
*iface
;
963 iface
= container_of(timeout
, struct interface
, remove_timer
);
964 vlist_delete(&interfaces
, &iface
->node
);
968 netifd_iface_remove(struct ubus_context
*ctx
, struct ubus_object
*obj
,
969 struct ubus_request_data
*req
, const char *method
,
970 struct blob_attr
*msg
)
972 struct interface
*iface
;
974 iface
= container_of(obj
, struct interface
, ubus
);
975 if (iface
->remove_timer
.cb
)
976 return UBUS_STATUS_INVALID_ARGUMENT
;
978 iface
->remove_timer
.cb
= netifd_iface_do_remove
;
979 uloop_timeout_set(&iface
->remove_timer
, 100);
984 netifd_handle_iface_prepare(struct ubus_context
*ctx
, struct ubus_object
*obj
,
985 struct ubus_request_data
*req
, const char *method
,
986 struct blob_attr
*msg
)
988 struct interface
*iface
;
990 const struct device_hotplug_ops
*ops
;
992 iface
= container_of(obj
, struct interface
, ubus
);
993 dev
= iface
->main_dev
.dev
;
997 ops
= dev
->hotplug_ops
;
1001 return ops
->prepare(dev
);
1005 netifd_handle_set_data(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1006 struct ubus_request_data
*req
, const char *method
,
1007 struct blob_attr
*msg
)
1009 struct interface
*iface
;
1011 iface
= container_of(obj
, struct interface
, ubus
);
1013 return interface_parse_data(iface
, msg
);
1016 static struct ubus_method iface_object_methods
[] = {
1017 { .name
= "up", .handler
= netifd_handle_up
},
1018 { .name
= "down", .handler
= netifd_handle_down
},
1019 { .name
= "renew", .handler
= netifd_handle_renew
},
1020 { .name
= "status", .handler
= netifd_handle_status
},
1021 { .name
= "prepare", .handler
= netifd_handle_iface_prepare
},
1022 { .name
= "dump", .handler
= netifd_handle_dump
},
1023 UBUS_METHOD("add_device", netifd_iface_handle_device
, dev_link_policy
),
1024 UBUS_METHOD("remove_device", netifd_iface_handle_device
, dev_link_policy
),
1025 { .name
= "notify_proto", .handler
= netifd_iface_notify_proto
},
1026 { .name
= "remove", .handler
= netifd_iface_remove
},
1027 { .name
= "set_data", .handler
= netifd_handle_set_data
},
1030 static struct ubus_object_type iface_object_type
=
1031 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods
);
1034 static struct ubus_object iface_object
= {
1035 .name
= "network.interface",
1036 .type
= &iface_object_type
,
1037 .n_methods
= ARRAY_SIZE(iface_object_methods
),
1040 static void netifd_add_object(struct ubus_object
*obj
)
1042 int ret
= ubus_add_object(ubus_ctx
, obj
);
1045 fprintf(stderr
, "Failed to publish object '%s': %s\n", obj
->name
, ubus_strerror(ret
));
1048 static const struct blobmsg_policy iface_policy
= {
1049 .name
= "interface",
1050 .type
= BLOBMSG_TYPE_STRING
,
1054 netifd_handle_iface(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1055 struct ubus_request_data
*req
, const char *method
,
1056 struct blob_attr
*msg
)
1058 struct interface
*iface
;
1059 struct blob_attr
*tb
;
1062 blobmsg_parse(&iface_policy
, 1, &tb
, blob_data(msg
), blob_len(msg
));
1064 return UBUS_STATUS_INVALID_ARGUMENT
;
1066 iface
= vlist_find(&interfaces
, blobmsg_data(tb
), iface
, node
);
1068 return UBUS_STATUS_NOT_FOUND
;
1070 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
1073 if (strcmp(method
, iface_object_methods
[i
].name
) != 0)
1076 cb
= iface_object_methods
[i
].handler
;
1077 return cb(ctx
, &iface
->ubus
, req
, method
, msg
);
1080 return UBUS_STATUS_INVALID_ARGUMENT
;
1083 static void netifd_add_iface_object(void)
1085 struct ubus_method
*methods
;
1088 methods
= calloc(1, sizeof(iface_object_methods
));
1092 memcpy(methods
, iface_object_methods
, sizeof(iface_object_methods
));
1093 iface_object
.methods
= methods
;
1095 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
1096 if (methods
[i
].handler
== netifd_handle_dump
)
1099 methods
[i
].handler
= netifd_handle_iface
;
1100 methods
[i
].policy
= &iface_policy
;
1101 methods
[i
].n_policy
= 1;
1103 netifd_add_object(&iface_object
);
1106 static struct wireless_device
*
1107 get_wdev(struct blob_attr
*msg
, int *ret
)
1109 struct blobmsg_policy wdev_policy
= {
1111 .type
= BLOBMSG_TYPE_STRING
,
1113 struct blob_attr
*dev_attr
;
1114 struct wireless_device
*wdev
= NULL
;
1117 blobmsg_parse(&wdev_policy
, 1, &dev_attr
, blob_data(msg
), blob_len(msg
));
1119 *ret
= UBUS_STATUS_INVALID_ARGUMENT
;
1123 wdev
= vlist_find(&wireless_devices
, blobmsg_data(dev_attr
), wdev
, node
);
1125 *ret
= UBUS_STATUS_NOT_FOUND
;
1134 netifd_handle_wdev_reconf(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1135 struct ubus_request_data
*req
, const char *method
,
1136 struct blob_attr
*msg
)
1138 struct wireless_device
*wdev
;
1141 wdev
= get_wdev(msg
, &ret
);
1142 if (ret
== UBUS_STATUS_NOT_FOUND
)
1146 wireless_device_reconf(wdev
);
1148 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1149 wireless_device_reconf(wdev
);
1156 netifd_handle_wdev_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1157 struct ubus_request_data
*req
, const char *method
,
1158 struct blob_attr
*msg
)
1160 struct wireless_device
*wdev
;
1163 wdev
= get_wdev(msg
, &ret
);
1164 if (ret
== UBUS_STATUS_NOT_FOUND
)
1168 wireless_device_set_up(wdev
);
1170 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1171 wireless_device_set_up(wdev
);
1178 netifd_handle_wdev_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1179 struct ubus_request_data
*req
, const char *method
,
1180 struct blob_attr
*msg
)
1182 struct wireless_device
*wdev
;
1185 wdev
= get_wdev(msg
, &ret
);
1186 if (ret
== UBUS_STATUS_NOT_FOUND
)
1190 wireless_device_set_down(wdev
);
1192 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1193 wireless_device_set_down(wdev
);
1200 netifd_handle_wdev_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1201 struct ubus_request_data
*req
, const char *method
,
1202 struct blob_attr
*msg
)
1204 struct wireless_device
*wdev
;
1207 wdev
= get_wdev(msg
, &ret
);
1208 if (ret
== UBUS_STATUS_NOT_FOUND
)
1211 blob_buf_init(&b
, 0);
1213 wireless_device_status(wdev
, &b
);
1215 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1216 wireless_device_status(wdev
, &b
);
1218 ubus_send_reply(ctx
, req
, b
.head
);
1223 netifd_handle_wdev_get_validate(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1224 struct ubus_request_data
*req
, const char *method
,
1225 struct blob_attr
*msg
)
1227 struct wireless_device
*wdev
;
1230 wdev
= get_wdev(msg
, &ret
);
1231 if (ret
== UBUS_STATUS_NOT_FOUND
)
1234 blob_buf_init(&b
, 0);
1236 wireless_device_get_validate(wdev
, &b
);
1238 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1239 wireless_device_get_validate(wdev
, &b
);
1241 ubus_send_reply(ctx
, req
, b
.head
);
1246 netifd_handle_wdev_notify(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1247 struct ubus_request_data
*req
, const char *method
,
1248 struct blob_attr
*msg
)
1250 struct wireless_device
*wdev
;
1253 wdev
= get_wdev(msg
, &ret
);
1257 return wireless_device_notify(wdev
, msg
, req
);
1260 static struct ubus_method wireless_object_methods
[] = {
1261 { .name
= "up", .handler
= netifd_handle_wdev_up
},
1262 { .name
= "down", .handler
= netifd_handle_wdev_down
},
1263 { .name
= "reconf", .handler
= netifd_handle_wdev_reconf
},
1264 { .name
= "status", .handler
= netifd_handle_wdev_status
},
1265 { .name
= "notify", .handler
= netifd_handle_wdev_notify
},
1266 { .name
= "get_validate", .handler
= netifd_handle_wdev_get_validate
},
1269 static struct ubus_object_type wireless_object_type
=
1270 UBUS_OBJECT_TYPE("netifd_iface", wireless_object_methods
);
1273 static struct ubus_object wireless_object
= {
1274 .name
= "network.wireless",
1275 .type
= &wireless_object_type
,
1276 .methods
= wireless_object_methods
,
1277 .n_methods
= ARRAY_SIZE(wireless_object_methods
),
1281 netifd_ubus_init(const char *path
)
1286 ubus_ctx
= ubus_connect(path
);
1290 DPRINTF("connected as %08x\n", ubus_ctx
->local_id
);
1291 ubus_ctx
->connection_lost
= netifd_ubus_connection_lost
;
1292 netifd_ubus_add_fd();
1294 netifd_add_object(&main_object
);
1295 netifd_add_object(&dev_object
);
1296 netifd_add_object(&wireless_object
);
1297 netifd_add_iface_object();
1303 netifd_ubus_done(void)
1305 ubus_free(ubus_ctx
);
1309 netifd_ubus_interface_event(struct interface
*iface
, bool up
)
1311 blob_buf_init(&b
, 0);
1312 blobmsg_add_string(&b
, "action", up
? "ifup" : "ifdown");
1313 blobmsg_add_string(&b
, "interface", iface
->name
);
1314 ubus_send_event(ubus_ctx
, "network.interface", b
.head
);
1318 netifd_ubus_interface_notify(struct interface
*iface
, bool up
)
1320 const char *event
= (up
) ? "interface.update" : "interface.down";
1321 blob_buf_init(&b
, 0);
1322 blobmsg_add_string(&b
, "interface", iface
->name
);
1323 netifd_dump_status(iface
);
1324 ubus_notify(ubus_ctx
, &iface_object
, event
, b
.head
, -1);
1325 ubus_notify(ubus_ctx
, &iface
->ubus
, event
, b
.head
, -1);
1329 netifd_ubus_add_interface(struct interface
*iface
)
1331 struct ubus_object
*obj
= &iface
->ubus
;
1334 if (asprintf(&name
, "%s.interface.%s", main_object
.name
, iface
->name
) == -1)
1338 obj
->type
= &iface_object_type
;
1339 obj
->methods
= iface_object_methods
;
1340 obj
->n_methods
= ARRAY_SIZE(iface_object_methods
);
1341 if (ubus_add_object(ubus_ctx
, &iface
->ubus
)) {
1342 DPRINTF("failed to publish ubus object for interface '%s'\n", iface
->name
);
1349 netifd_ubus_remove_interface(struct interface
*iface
)
1351 if (!iface
->ubus
.name
)
1354 ubus_remove_object(ubus_ctx
, &iface
->ubus
);
1355 free((void *) iface
->ubus
.name
);