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
;
301 DEV_STATE_AUTH_STATUS
,
305 static const struct blobmsg_policy dev_state_policy
[__DEV_STATE_MAX
] = {
306 [DEV_STATE_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
307 [DEV_STATE_DEFER
] = { .name
= "defer", .type
= BLOBMSG_TYPE_BOOL
},
308 [DEV_STATE_AUTH_STATUS
] = { .name
= "auth_status", .type
= BLOBMSG_TYPE_BOOL
},
312 netifd_handle_set_state(struct ubus_context
*ctx
, struct ubus_object
*obj
,
313 struct ubus_request_data
*req
, const char *method
,
314 struct blob_attr
*msg
)
316 struct device
*dev
= NULL
;
317 struct blob_attr
*tb
[__DEV_STATE_MAX
];
318 struct blob_attr
*cur
;
320 blobmsg_parse(dev_state_policy
, __DEV_STATE_MAX
, tb
, blob_data(msg
), blob_len(msg
));
322 cur
= tb
[DEV_STATE_NAME
];
324 return UBUS_STATUS_INVALID_ARGUMENT
;
326 dev
= device_find(blobmsg_data(cur
));
328 return UBUS_STATUS_NOT_FOUND
;
330 cur
= tb
[DEV_STATE_DEFER
];
332 device_set_deferred(dev
, !!blobmsg_get_u8(cur
));
334 cur
= tb
[DEV_STATE_AUTH_STATUS
];
336 device_set_auth_status(dev
, !!blobmsg_get_u8(cur
));
341 static struct ubus_method dev_object_methods
[] = {
342 UBUS_METHOD("status", netifd_dev_status
, dev_policy
),
343 UBUS_METHOD("set_alias", netifd_handle_alias
, alias_attrs
),
344 UBUS_METHOD("set_state", netifd_handle_set_state
, dev_state_policy
),
347 static struct ubus_object_type dev_object_type
=
348 UBUS_OBJECT_TYPE("device", dev_object_methods
);
350 static struct ubus_object dev_object
= {
351 .name
= "network.device",
352 .type
= &dev_object_type
,
353 .methods
= dev_object_methods
,
354 .n_methods
= ARRAY_SIZE(dev_object_methods
),
358 netifd_ubus_add_fd(void)
360 ubus_add_uloop(ubus_ctx
);
361 system_fd_set_cloexec(ubus_ctx
->sock
.fd
);
365 netifd_ubus_reconnect_timer(struct uloop_timeout
*timeout
)
367 static struct uloop_timeout retry
= {
368 .cb
= netifd_ubus_reconnect_timer
,
372 if (ubus_reconnect(ubus_ctx
, ubus_path
) != 0) {
373 DPRINTF("failed to reconnect, trying again in %d seconds\n", t
);
374 uloop_timeout_set(&retry
, t
* 1000);
378 DPRINTF("reconnected to ubus, new id: %08x\n", ubus_ctx
->local_id
);
379 netifd_ubus_add_fd();
383 netifd_ubus_connection_lost(struct ubus_context
*ctx
)
385 netifd_ubus_reconnect_timer(NULL
);
388 /* per-interface object */
391 netifd_handle_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
392 struct ubus_request_data
*req
, const char *method
,
393 struct blob_attr
*msg
)
395 struct interface
*iface
;
397 iface
= container_of(obj
, struct interface
, ubus
);
398 interface_set_up(iface
);
404 netifd_handle_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
405 struct ubus_request_data
*req
, const char *method
,
406 struct blob_attr
*msg
)
408 struct interface
*iface
;
410 iface
= container_of(obj
, struct interface
, ubus
);
411 interface_set_down(iface
);
417 netifd_handle_renew(struct ubus_context
*ctx
, struct ubus_object
*obj
,
418 struct ubus_request_data
*req
, const char *method
,
419 struct blob_attr
*msg
)
421 struct interface
*iface
;
423 iface
= container_of(obj
, struct interface
, ubus
);
424 interface_renew(iface
);
430 netifd_add_interface_errors(struct blob_buf
*b
, struct interface
*iface
)
432 struct interface_error
*error
;
436 e
= blobmsg_open_array(b
, "errors");
437 list_for_each_entry(error
, &iface
->errors
, list
) {
438 e2
= blobmsg_open_table(b
, NULL
);
440 blobmsg_add_string(b
, "subsystem", error
->subsystem
);
441 blobmsg_add_string(b
, "code", error
->code
);
442 if (error
->data
[0]) {
443 e3
= blobmsg_open_array(b
, "data");
444 for (i
= 0; error
->data
[i
]; i
++)
445 blobmsg_add_string(b
, NULL
, error
->data
[i
]);
446 blobmsg_close_array(b
, e3
);
449 blobmsg_close_table(b
, e2
);
451 blobmsg_close_array(b
, e
);
455 interface_ip_dump_address_list(struct interface_ip_settings
*ip
, bool v6
, bool enabled
)
457 struct device_addr
*addr
;
463 time_t now
= system_get_rtime();
464 vlist_for_each_element(&ip
->addr
, addr
, node
) {
465 if (addr
->enabled
!= enabled
)
468 if ((addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
473 if (af
!= (v6
? AF_INET6
: AF_INET
))
476 a
= blobmsg_open_table(&b
, NULL
);
478 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
479 inet_ntop(af
, &addr
->addr
, buf
, buflen
);
480 blobmsg_add_string_buffer(&b
);
482 blobmsg_add_u32(&b
, "mask", addr
->mask
);
484 if (addr
->point_to_point
) {
485 buf
= blobmsg_alloc_string_buffer(&b
, "ptpaddress", buflen
);
486 inet_ntop(af
, &addr
->point_to_point
, buf
, buflen
);
487 blobmsg_add_string_buffer(&b
);
490 if (addr
->preferred_until
) {
491 int preferred
= addr
->preferred_until
- now
;
494 blobmsg_add_u32(&b
, "preferred", preferred
);
497 if (addr
->valid_until
)
498 blobmsg_add_u32(&b
, "valid", addr
->valid_until
- now
);
501 blobmsg_add_string(&b
, "class", addr
->pclass
);
503 blobmsg_close_table(&b
, a
);
508 interface_ip_dump_neighbor_list(struct interface_ip_settings
*ip
, bool enabled
)
510 struct device_neighbor
*neighbor
;
516 vlist_for_each_element(&ip
->neighbor
, neighbor
, node
) {
517 if (neighbor
->enabled
!= enabled
)
520 if ((neighbor
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
525 r
= blobmsg_open_table(&b
, NULL
);
527 if (neighbor
->flags
& DEVNEIGH_MAC
)
528 blobmsg_add_string(&b
, "mac", format_macaddr(neighbor
->macaddr
));
530 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
531 inet_ntop(af
, &neighbor
->addr
, buf
, buflen
);
532 blobmsg_add_string_buffer(&b
);
535 blobmsg_add_u32(&b
, "proxy", neighbor
->proxy
);
537 if (neighbor
->router
)
538 blobmsg_add_u32(&b
, "router", neighbor
->router
);
540 blobmsg_close_table(&b
, r
);
545 interface_ip_dump_route_list(struct interface_ip_settings
*ip
, bool enabled
)
547 struct device_route
*route
;
553 time_t now
= system_get_rtime();
554 vlist_for_each_element(&ip
->route
, route
, node
) {
555 if (route
->enabled
!= enabled
)
558 if ((ip
->no_defaultroute
== enabled
) && !route
->mask
)
561 if ((route
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
566 r
= blobmsg_open_table(&b
, NULL
);
568 buf
= blobmsg_alloc_string_buffer(&b
, "target", buflen
);
569 inet_ntop(af
, &route
->addr
, buf
, buflen
);
570 blobmsg_add_string_buffer(&b
);
572 blobmsg_add_u32(&b
, "mask", route
->mask
);
574 buf
= blobmsg_alloc_string_buffer(&b
, "nexthop", buflen
);
575 inet_ntop(af
, &route
->nexthop
, buf
, buflen
);
576 blobmsg_add_string_buffer(&b
);
578 if (route
->flags
& DEVROUTE_TYPE
)
579 blobmsg_add_u32(&b
, "type", route
->type
);
581 if (route
->flags
& DEVROUTE_PROTO
)
582 blobmsg_add_u32(&b
, "proto", route
->proto
);
584 if (route
->flags
& DEVROUTE_MTU
)
585 blobmsg_add_u32(&b
, "mtu", route
->mtu
);
587 if (route
->flags
& DEVROUTE_METRIC
)
588 blobmsg_add_u32(&b
, "metric", route
->metric
);
590 if (route
->flags
& DEVROUTE_TABLE
)
591 blobmsg_add_u32(&b
, "table", route
->table
);
593 if (route
->valid_until
)
594 blobmsg_add_u32(&b
, "valid", route
->valid_until
- now
);
596 buf
= blobmsg_alloc_string_buffer(&b
, "source", buflen
);
597 inet_ntop(af
, &route
->source
, buf
, buflen
);
598 snprintf(buf
+ strlen(buf
), buflen
- strlen(buf
), "/%u", route
->sourcemask
);
599 blobmsg_add_string_buffer(&b
);
601 blobmsg_close_table(&b
, r
);
607 interface_ip_dump_prefix_list(struct interface_ip_settings
*ip
)
609 struct device_prefix
*prefix
;
612 const int buflen
= INET6_ADDRSTRLEN
;
614 time_t now
= system_get_rtime();
615 vlist_for_each_element(&ip
->prefix
, prefix
, node
) {
616 a
= blobmsg_open_table(&b
, NULL
);
618 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
619 inet_ntop(AF_INET6
, &prefix
->addr
, buf
, buflen
);
620 blobmsg_add_string_buffer(&b
);
622 blobmsg_add_u32(&b
, "mask", prefix
->length
);
624 if (prefix
->preferred_until
) {
625 int preferred
= prefix
->preferred_until
- now
;
628 blobmsg_add_u32(&b
, "preferred", preferred
);
631 if (prefix
->valid_until
)
632 blobmsg_add_u32(&b
, "valid", prefix
->valid_until
- now
);
634 blobmsg_add_string(&b
, "class", prefix
->pclass
);
636 c
= blobmsg_open_table(&b
, "assigned");
637 struct device_prefix_assignment
*assign
;
638 list_for_each_entry(assign
, &prefix
->assignments
, head
) {
639 if (!assign
->name
[0])
642 struct in6_addr addr
= prefix
->addr
;
643 addr
.s6_addr32
[1] |= htonl(assign
->assigned
);
645 void *d
= blobmsg_open_table(&b
, assign
->name
);
647 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
648 inet_ntop(AF_INET6
, &addr
, buf
, buflen
);
649 blobmsg_add_string_buffer(&b
);
651 blobmsg_add_u32(&b
, "mask", assign
->length
);
653 blobmsg_close_table(&b
, d
);
655 blobmsg_close_table(&b
, c
);
657 blobmsg_close_table(&b
, a
);
663 interface_ip_dump_prefix_assignment_list(struct interface
*iface
)
667 const int buflen
= INET6_ADDRSTRLEN
;
668 time_t now
= system_get_rtime();
670 struct device_prefix
*prefix
;
671 list_for_each_entry(prefix
, &prefixes
, head
) {
672 struct device_prefix_assignment
*assign
;
673 list_for_each_entry(assign
, &prefix
->assignments
, head
) {
674 if (strcmp(assign
->name
, iface
->name
))
677 struct in6_addr addr
= prefix
->addr
;
678 addr
.s6_addr32
[1] |= htonl(assign
->assigned
);
680 a
= blobmsg_open_table(&b
, NULL
);
682 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
683 inet_ntop(AF_INET6
, &addr
, buf
, buflen
);
684 blobmsg_add_string_buffer(&b
);
686 blobmsg_add_u32(&b
, "mask", assign
->length
);
688 if (prefix
->preferred_until
) {
689 int preferred
= prefix
->preferred_until
- now
;
692 blobmsg_add_u32(&b
, "preferred", preferred
);
695 if (prefix
->valid_until
)
696 blobmsg_add_u32(&b
, "valid", prefix
->valid_until
- now
);
698 void *c
= blobmsg_open_table(&b
, "local-address");
699 if (assign
->enabled
) {
700 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
701 inet_ntop(AF_INET6
, &assign
->addr
, buf
, buflen
);
702 blobmsg_add_string_buffer(&b
);
704 blobmsg_add_u32(&b
, "mask", assign
->length
);
706 blobmsg_close_table(&b
, c
);
708 blobmsg_close_table(&b
, a
);
714 interface_ip_dump_dns_server_list(struct interface_ip_settings
*ip
, bool enabled
)
716 struct dns_server
*dns
;
720 vlist_simple_for_each_element(&ip
->dns_servers
, dns
, node
) {
721 if (ip
->no_dns
== enabled
)
724 buf
= blobmsg_alloc_string_buffer(&b
, NULL
, buflen
);
725 inet_ntop(dns
->af
, &dns
->addr
, buf
, buflen
);
726 blobmsg_add_string_buffer(&b
);
731 interface_ip_dump_dns_search_list(struct interface_ip_settings
*ip
, bool enabled
)
733 struct dns_search_domain
*dns
;
735 vlist_simple_for_each_element(&ip
->dns_search
, dns
, node
) {
736 if (ip
->no_dns
== enabled
)
739 blobmsg_add_string(&b
, NULL
, dns
->name
);
744 netifd_dump_status(struct interface
*iface
)
746 struct interface_data
*data
;
750 blobmsg_add_u8(&b
, "up", iface
->state
== IFS_UP
);
751 blobmsg_add_u8(&b
, "pending", iface
->state
== IFS_SETUP
);
752 blobmsg_add_u8(&b
, "available", iface
->available
);
753 blobmsg_add_u8(&b
, "autostart", iface
->autostart
);
754 blobmsg_add_u8(&b
, "dynamic", iface
->dynamic
);
756 if (iface
->state
== IFS_UP
) {
757 time_t cur
= system_get_rtime();
758 blobmsg_add_u32(&b
, "uptime", cur
- iface
->start_time
);
759 if (iface
->l3_dev
.dev
)
760 blobmsg_add_string(&b
, "l3_device", iface
->l3_dev
.dev
->ifname
);
763 if (iface
->proto_handler
)
764 blobmsg_add_string(&b
, "proto", iface
->proto_handler
->name
);
766 dev
= iface
->main_dev
.dev
;
767 if (dev
&& !dev
->hidden
&& iface
->proto_handler
&&
768 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
))
769 blobmsg_add_string(&b
, "device", dev
->ifname
);
772 blobmsg_add_string(&b
, "jail", iface
->jail
);
774 if (iface
->jail_ifname
)
775 blobmsg_add_string(&b
, "jail_ifname", iface
->jail_ifname
);
777 if (iface
->state
== IFS_UP
) {
778 if (iface
->updated
) {
779 a
= blobmsg_open_array(&b
, "updated");
781 if (iface
->updated
& IUF_ADDRESS
)
782 blobmsg_add_string(&b
, NULL
, "addresses");
783 if (iface
->updated
& IUF_ROUTE
)
784 blobmsg_add_string(&b
, NULL
, "routes");
785 if (iface
->updated
& IUF_PREFIX
)
786 blobmsg_add_string(&b
, NULL
, "prefixes");
787 if (iface
->updated
& IUF_DATA
)
788 blobmsg_add_string(&b
, NULL
, "data");
790 blobmsg_close_array(&b
, a
);
794 blobmsg_add_u32(&b
, "ip4table", iface
->ip4table
);
796 blobmsg_add_u32(&b
, "ip6table", iface
->ip6table
);
797 blobmsg_add_u32(&b
, "metric", iface
->metric
);
798 blobmsg_add_u32(&b
, "dns_metric", iface
->dns_metric
);
799 blobmsg_add_u8(&b
, "delegation", !iface
->proto_ip
.no_delegation
);
800 if (iface
->assignment_weight
)
801 blobmsg_add_u32(&b
, "ip6weight", iface
->assignment_weight
);
802 a
= blobmsg_open_array(&b
, "ipv4-address");
803 interface_ip_dump_address_list(&iface
->config_ip
, false, true);
804 interface_ip_dump_address_list(&iface
->proto_ip
, false, true);
805 blobmsg_close_array(&b
, a
);
806 a
= blobmsg_open_array(&b
, "ipv6-address");
807 interface_ip_dump_address_list(&iface
->config_ip
, true, true);
808 interface_ip_dump_address_list(&iface
->proto_ip
, true, true);
809 blobmsg_close_array(&b
, a
);
810 a
= blobmsg_open_array(&b
, "ipv6-prefix");
811 interface_ip_dump_prefix_list(&iface
->config_ip
);
812 interface_ip_dump_prefix_list(&iface
->proto_ip
);
813 blobmsg_close_array(&b
, a
);
814 a
= blobmsg_open_array(&b
, "ipv6-prefix-assignment");
815 interface_ip_dump_prefix_assignment_list(iface
);
816 blobmsg_close_array(&b
, a
);
817 a
= blobmsg_open_array(&b
, "route");
818 interface_ip_dump_route_list(&iface
->config_ip
, true);
819 interface_ip_dump_route_list(&iface
->proto_ip
, true);
820 blobmsg_close_array(&b
, a
);
821 a
= blobmsg_open_array(&b
, "dns-server");
822 interface_ip_dump_dns_server_list(&iface
->config_ip
, true);
823 interface_ip_dump_dns_server_list(&iface
->proto_ip
, true);
824 blobmsg_close_array(&b
, a
);
825 a
= blobmsg_open_array(&b
, "dns-search");
826 interface_ip_dump_dns_search_list(&iface
->config_ip
, true);
827 interface_ip_dump_dns_search_list(&iface
->proto_ip
, true);
828 blobmsg_close_array(&b
, a
);
829 a
= blobmsg_open_array(&b
, "neighbors");
830 interface_ip_dump_neighbor_list(&iface
->config_ip
, true);
831 interface_ip_dump_neighbor_list(&iface
->proto_ip
, true);
832 blobmsg_close_array(&b
, a
);
834 inactive
= blobmsg_open_table(&b
, "inactive");
835 a
= blobmsg_open_array(&b
, "ipv4-address");
836 interface_ip_dump_address_list(&iface
->config_ip
, false, false);
837 interface_ip_dump_address_list(&iface
->proto_ip
, false, false);
838 blobmsg_close_array(&b
, a
);
839 a
= blobmsg_open_array(&b
, "ipv6-address");
840 interface_ip_dump_address_list(&iface
->config_ip
, true, false);
841 interface_ip_dump_address_list(&iface
->proto_ip
, true, false);
842 blobmsg_close_array(&b
, a
);
843 a
= blobmsg_open_array(&b
, "route");
844 interface_ip_dump_route_list(&iface
->config_ip
, false);
845 interface_ip_dump_route_list(&iface
->proto_ip
, false);
846 blobmsg_close_array(&b
, a
);
847 a
= blobmsg_open_array(&b
, "dns-server");
848 interface_ip_dump_dns_server_list(&iface
->config_ip
, false);
849 interface_ip_dump_dns_server_list(&iface
->proto_ip
, false);
850 blobmsg_close_array(&b
, a
);
851 a
= blobmsg_open_array(&b
, "dns-search");
852 interface_ip_dump_dns_search_list(&iface
->config_ip
, false);
853 interface_ip_dump_dns_search_list(&iface
->proto_ip
, false);
854 blobmsg_close_array(&b
, a
);
855 a
= blobmsg_open_array(&b
, "neighbors");
856 interface_ip_dump_neighbor_list(&iface
->config_ip
, false);
857 interface_ip_dump_neighbor_list(&iface
->proto_ip
, false);
858 blobmsg_close_array(&b
, a
);
859 blobmsg_close_table(&b
, inactive
);
862 a
= blobmsg_open_table(&b
, "data");
863 avl_for_each_element(&iface
->data
, data
, node
)
864 blobmsg_add_blob(&b
, data
->data
);
866 blobmsg_close_table(&b
, a
);
868 if (!list_empty(&iface
->errors
))
869 netifd_add_interface_errors(&b
, iface
);
873 netifd_handle_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
874 struct ubus_request_data
*req
, const char *method
,
875 struct blob_attr
*msg
)
877 struct interface
*iface
= container_of(obj
, struct interface
, ubus
);
879 blob_buf_init(&b
, 0);
880 netifd_dump_status(iface
);
881 ubus_send_reply(ctx
, req
, b
.head
);
888 netifd_handle_dump(struct ubus_context
*ctx
, struct ubus_object
*obj
,
889 struct ubus_request_data
*req
, const char *method
,
890 struct blob_attr
*msg
)
892 blob_buf_init(&b
, 0);
893 void *a
= blobmsg_open_array(&b
, "interface");
895 struct interface
*iface
;
896 vlist_for_each_element(&interfaces
, iface
, node
) {
897 void *i
= blobmsg_open_table(&b
, NULL
);
898 blobmsg_add_string(&b
, "interface", iface
->name
);
899 netifd_dump_status(iface
);
900 blobmsg_close_table(&b
, i
);
903 blobmsg_close_array(&b
, a
);
904 ubus_send_reply(ctx
, req
, b
.head
);
916 static const struct blobmsg_policy dev_link_policy
[__DEV_LINK_MAX
] = {
917 [DEV_LINK_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
918 [DEV_LINK_EXT
] = { .name
= "link-ext", .type
= BLOBMSG_TYPE_BOOL
},
919 [DEV_LINK_VLAN
] = { .name
= "vlan", .type
= BLOBMSG_TYPE_ARRAY
},
923 netifd_iface_handle_device(struct ubus_context
*ctx
, struct ubus_object
*obj
,
924 struct ubus_request_data
*req
, const char *method
,
925 struct blob_attr
*msg
)
927 struct blob_attr
*tb
[__DEV_LINK_MAX
];
928 struct blob_attr
*cur
;
929 struct interface
*iface
;
930 bool add
= !strncmp(method
, "add", 3);
931 bool link_ext
= true;
933 iface
= container_of(obj
, struct interface
, ubus
);
935 blobmsg_parse(dev_link_policy
, __DEV_LINK_MAX
, tb
, blob_data(msg
), blob_len(msg
));
937 if (!tb
[DEV_LINK_NAME
])
938 return UBUS_STATUS_INVALID_ARGUMENT
;
940 cur
= tb
[DEV_LINK_EXT
];
942 link_ext
= blobmsg_get_bool(cur
);
944 return interface_handle_link(iface
, blobmsg_data(tb
[DEV_LINK_NAME
]),
945 tb
[DEV_LINK_VLAN
], add
, link_ext
);
950 netifd_iface_notify_proto(struct ubus_context
*ctx
, struct ubus_object
*obj
,
951 struct ubus_request_data
*req
, const char *method
,
952 struct blob_attr
*msg
)
954 struct interface
*iface
;
956 iface
= container_of(obj
, struct interface
, ubus
);
958 if (!iface
->proto
|| !iface
->proto
->notify
)
959 return UBUS_STATUS_NOT_SUPPORTED
;
961 return iface
->proto
->notify(iface
->proto
, msg
);
965 netifd_iface_do_remove(struct uloop_timeout
*timeout
)
967 struct interface
*iface
;
969 iface
= container_of(timeout
, struct interface
, remove_timer
);
970 vlist_delete(&interfaces
, &iface
->node
);
974 netifd_iface_remove(struct ubus_context
*ctx
, struct ubus_object
*obj
,
975 struct ubus_request_data
*req
, const char *method
,
976 struct blob_attr
*msg
)
978 struct interface
*iface
;
980 iface
= container_of(obj
, struct interface
, ubus
);
981 if (iface
->remove_timer
.cb
)
982 return UBUS_STATUS_INVALID_ARGUMENT
;
984 iface
->remove_timer
.cb
= netifd_iface_do_remove
;
985 uloop_timeout_set(&iface
->remove_timer
, 100);
990 netifd_handle_iface_prepare(struct ubus_context
*ctx
, struct ubus_object
*obj
,
991 struct ubus_request_data
*req
, const char *method
,
992 struct blob_attr
*msg
)
994 struct interface
*iface
;
995 struct device
*dev
, *bridge_dev
= NULL
;
996 const struct device_hotplug_ops
*ops
;
998 iface
= container_of(obj
, struct interface
, ubus
);
999 dev
= iface
->main_dev
.dev
;
1003 ops
= dev
->hotplug_ops
;
1007 ops
->prepare(dev
, &bridge_dev
);
1010 blob_buf_init(&b
, 0);
1012 blobmsg_add_string(&b
, "bridge", bridge_dev
->ifname
);
1013 ubus_send_reply(ctx
, req
, b
.head
);
1019 netifd_handle_set_data(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1020 struct ubus_request_data
*req
, const char *method
,
1021 struct blob_attr
*msg
)
1023 struct interface
*iface
;
1025 iface
= container_of(obj
, struct interface
, ubus
);
1027 return interface_parse_data(iface
, msg
);
1030 static struct ubus_method iface_object_methods
[] = {
1031 { .name
= "up", .handler
= netifd_handle_up
},
1032 { .name
= "down", .handler
= netifd_handle_down
},
1033 { .name
= "renew", .handler
= netifd_handle_renew
},
1034 { .name
= "status", .handler
= netifd_handle_status
},
1035 { .name
= "prepare", .handler
= netifd_handle_iface_prepare
},
1036 { .name
= "dump", .handler
= netifd_handle_dump
},
1037 UBUS_METHOD("add_device", netifd_iface_handle_device
, dev_link_policy
),
1038 UBUS_METHOD("remove_device", netifd_iface_handle_device
, dev_link_policy
),
1039 { .name
= "notify_proto", .handler
= netifd_iface_notify_proto
},
1040 { .name
= "remove", .handler
= netifd_iface_remove
},
1041 { .name
= "set_data", .handler
= netifd_handle_set_data
},
1044 static struct ubus_object_type iface_object_type
=
1045 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods
);
1048 static struct ubus_object iface_object
= {
1049 .name
= "network.interface",
1050 .type
= &iface_object_type
,
1051 .n_methods
= ARRAY_SIZE(iface_object_methods
),
1054 static void netifd_add_object(struct ubus_object
*obj
)
1056 int ret
= ubus_add_object(ubus_ctx
, obj
);
1059 fprintf(stderr
, "Failed to publish object '%s': %s\n", obj
->name
, ubus_strerror(ret
));
1062 static const struct blobmsg_policy iface_policy
= {
1063 .name
= "interface",
1064 .type
= BLOBMSG_TYPE_STRING
,
1068 netifd_handle_iface(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1069 struct ubus_request_data
*req
, const char *method
,
1070 struct blob_attr
*msg
)
1072 struct interface
*iface
;
1073 struct blob_attr
*tb
;
1076 blobmsg_parse(&iface_policy
, 1, &tb
, blob_data(msg
), blob_len(msg
));
1078 return UBUS_STATUS_INVALID_ARGUMENT
;
1080 iface
= vlist_find(&interfaces
, blobmsg_data(tb
), iface
, node
);
1082 return UBUS_STATUS_NOT_FOUND
;
1084 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
1087 if (strcmp(method
, iface_object_methods
[i
].name
) != 0)
1090 cb
= iface_object_methods
[i
].handler
;
1091 return cb(ctx
, &iface
->ubus
, req
, method
, msg
);
1094 return UBUS_STATUS_INVALID_ARGUMENT
;
1097 static void netifd_add_iface_object(void)
1099 struct ubus_method
*methods
;
1102 methods
= calloc(1, sizeof(iface_object_methods
));
1106 memcpy(methods
, iface_object_methods
, sizeof(iface_object_methods
));
1107 iface_object
.methods
= methods
;
1109 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
1110 if (methods
[i
].handler
== netifd_handle_dump
)
1113 methods
[i
].handler
= netifd_handle_iface
;
1114 methods
[i
].policy
= &iface_policy
;
1115 methods
[i
].n_policy
= 1;
1117 netifd_add_object(&iface_object
);
1120 static struct wireless_device
*
1121 get_wdev(struct blob_attr
*msg
, int *ret
)
1123 struct blobmsg_policy wdev_policy
= {
1125 .type
= BLOBMSG_TYPE_STRING
,
1127 struct blob_attr
*dev_attr
;
1128 struct wireless_device
*wdev
= NULL
;
1131 blobmsg_parse(&wdev_policy
, 1, &dev_attr
, blob_data(msg
), blob_len(msg
));
1133 *ret
= UBUS_STATUS_INVALID_ARGUMENT
;
1137 wdev
= vlist_find(&wireless_devices
, blobmsg_data(dev_attr
), wdev
, node
);
1139 *ret
= UBUS_STATUS_NOT_FOUND
;
1148 netifd_handle_wdev_reconf(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1149 struct ubus_request_data
*req
, const char *method
,
1150 struct blob_attr
*msg
)
1152 struct wireless_device
*wdev
;
1155 wdev
= get_wdev(msg
, &ret
);
1156 if (ret
== UBUS_STATUS_NOT_FOUND
)
1160 wireless_device_reconf(wdev
);
1162 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1163 wireless_device_reconf(wdev
);
1170 netifd_handle_wdev_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1171 struct ubus_request_data
*req
, const char *method
,
1172 struct blob_attr
*msg
)
1174 struct wireless_device
*wdev
;
1177 wdev
= get_wdev(msg
, &ret
);
1178 if (ret
== UBUS_STATUS_NOT_FOUND
)
1182 wireless_device_set_up(wdev
);
1184 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1185 wireless_device_set_up(wdev
);
1192 netifd_handle_wdev_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1193 struct ubus_request_data
*req
, const char *method
,
1194 struct blob_attr
*msg
)
1196 struct wireless_device
*wdev
;
1199 wdev
= get_wdev(msg
, &ret
);
1200 if (ret
== UBUS_STATUS_NOT_FOUND
)
1204 wireless_device_set_down(wdev
);
1206 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1207 wireless_device_set_down(wdev
);
1214 netifd_handle_wdev_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1215 struct ubus_request_data
*req
, const char *method
,
1216 struct blob_attr
*msg
)
1218 struct wireless_device
*wdev
;
1221 wdev
= get_wdev(msg
, &ret
);
1222 if (ret
== UBUS_STATUS_NOT_FOUND
)
1225 blob_buf_init(&b
, 0);
1227 wireless_device_status(wdev
, &b
);
1229 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1230 wireless_device_status(wdev
, &b
);
1232 ubus_send_reply(ctx
, req
, b
.head
);
1237 netifd_handle_wdev_get_validate(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1238 struct ubus_request_data
*req
, const char *method
,
1239 struct blob_attr
*msg
)
1241 struct wireless_device
*wdev
;
1244 wdev
= get_wdev(msg
, &ret
);
1245 if (ret
== UBUS_STATUS_NOT_FOUND
)
1248 blob_buf_init(&b
, 0);
1250 wireless_device_get_validate(wdev
, &b
);
1252 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1253 wireless_device_get_validate(wdev
, &b
);
1255 ubus_send_reply(ctx
, req
, b
.head
);
1260 netifd_handle_wdev_notify(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1261 struct ubus_request_data
*req
, const char *method
,
1262 struct blob_attr
*msg
)
1264 struct wireless_device
*wdev
;
1267 wdev
= get_wdev(msg
, &ret
);
1271 return wireless_device_notify(wdev
, msg
, req
);
1274 static struct ubus_method wireless_object_methods
[] = {
1275 { .name
= "up", .handler
= netifd_handle_wdev_up
},
1276 { .name
= "down", .handler
= netifd_handle_wdev_down
},
1277 { .name
= "reconf", .handler
= netifd_handle_wdev_reconf
},
1278 { .name
= "status", .handler
= netifd_handle_wdev_status
},
1279 { .name
= "notify", .handler
= netifd_handle_wdev_notify
},
1280 { .name
= "get_validate", .handler
= netifd_handle_wdev_get_validate
},
1283 static struct ubus_object_type wireless_object_type
=
1284 UBUS_OBJECT_TYPE("netifd_iface", wireless_object_methods
);
1287 static struct ubus_object wireless_object
= {
1288 .name
= "network.wireless",
1289 .type
= &wireless_object_type
,
1290 .methods
= wireless_object_methods
,
1291 .n_methods
= ARRAY_SIZE(wireless_object_methods
),
1295 netifd_extdev_invoke(uint32_t id
, const char *method
, struct blob_attr
*msg
,
1296 ubus_data_handler_t data_cb
, void *data
)
1298 return ubus_invoke(ubus_ctx
, id
, method
, msg
, data_cb
, data
, 3000);
1302 netifd_ubus_init(const char *path
)
1307 ubus_ctx
= ubus_connect(path
);
1311 DPRINTF("connected as %08x\n", ubus_ctx
->local_id
);
1312 ubus_ctx
->connection_lost
= netifd_ubus_connection_lost
;
1313 netifd_ubus_add_fd();
1315 netifd_add_object(&main_object
);
1316 netifd_add_object(&dev_object
);
1317 netifd_add_object(&wireless_object
);
1318 netifd_add_iface_object();
1324 netifd_ubus_done(void)
1326 ubus_free(ubus_ctx
);
1330 netifd_ubus_interface_event(struct interface
*iface
, bool up
)
1332 blob_buf_init(&b
, 0);
1333 blobmsg_add_string(&b
, "action", up
? "ifup" : "ifdown");
1334 blobmsg_add_string(&b
, "interface", iface
->name
);
1335 ubus_send_event(ubus_ctx
, "network.interface", b
.head
);
1339 netifd_ubus_interface_notify(struct interface
*iface
, bool up
)
1341 const char *event
= (up
) ? "interface.update" : "interface.down";
1342 blob_buf_init(&b
, 0);
1343 blobmsg_add_string(&b
, "interface", iface
->name
);
1344 netifd_dump_status(iface
);
1345 ubus_notify(ubus_ctx
, &iface_object
, event
, b
.head
, -1);
1346 ubus_notify(ubus_ctx
, &iface
->ubus
, event
, b
.head
, -1);
1350 netifd_ubus_add_interface(struct interface
*iface
)
1352 struct ubus_object
*obj
= &iface
->ubus
;
1355 if (asprintf(&name
, "%s.interface.%s", main_object
.name
, iface
->name
) == -1)
1359 obj
->type
= &iface_object_type
;
1360 obj
->methods
= iface_object_methods
;
1361 obj
->n_methods
= ARRAY_SIZE(iface_object_methods
);
1362 if (ubus_add_object(ubus_ctx
, &iface
->ubus
)) {
1363 DPRINTF("failed to publish ubus object for interface '%s'\n", iface
->name
);
1370 netifd_ubus_remove_interface(struct interface
*iface
)
1372 if (!iface
->ubus
.name
)
1375 ubus_remove_object(ubus_ctx
, &iface
->ubus
);
1376 free((void *) iface
->ubus
.name
);