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
;
61 static const struct blobmsg_policy route_policy
[__HR_MAX
] = {
62 [HR_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
63 [HR_V6
] = { .name
= "v6", .type
= BLOBMSG_TYPE_BOOL
},
64 [HR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
65 [HR_EXCLUDE
] = { .name
= "exclude", .type
= BLOBMSG_TYPE_BOOL
},
69 netifd_add_host_route(struct ubus_context
*ctx
, struct ubus_object
*obj
,
70 struct ubus_request_data
*req
, const char *method
,
71 struct blob_attr
*msg
)
73 struct blob_attr
*tb
[__HR_MAX
];
74 struct interface
*iface
= NULL
;
79 blobmsg_parse(route_policy
, __HR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
81 return UBUS_STATUS_INVALID_ARGUMENT
;
84 v6
= blobmsg_get_bool(tb
[HR_V6
]);
87 exclude
= blobmsg_get_bool(tb
[HR_EXCLUDE
]);
90 iface
= vlist_find(&interfaces
, blobmsg_data(tb
[HR_INTERFACE
]), iface
, node
);
92 memset(&a
, 0, sizeof(a
));
93 if (!inet_pton(v6
? AF_INET6
: AF_INET
, blobmsg_data(tb
[HR_TARGET
]), &a
))
94 return UBUS_STATUS_INVALID_ARGUMENT
;
96 iface
= interface_ip_add_target_route(&a
, v6
, iface
, exclude
);
98 return UBUS_STATUS_NOT_FOUND
;
100 blob_buf_init(&b
, 0);
101 blobmsg_add_string(&b
, "interface", iface
->name
);
102 ubus_send_reply(ctx
, req
, b
.head
);
108 netifd_get_proto_handlers(struct ubus_context
*ctx
, struct ubus_object
*obj
,
109 struct ubus_request_data
*req
, const char *method
,
110 struct blob_attr
*msg
)
112 blob_buf_init(&b
, 0);
113 proto_dump_handlers(&b
);
114 ubus_send_reply(ctx
, req
, b
.head
);
125 static const struct blobmsg_policy dynamic_policy
[__DI_MAX
] = {
126 [DI_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
130 netifd_add_dynamic(struct ubus_context
*ctx
, struct ubus_object
*obj
,
131 struct ubus_request_data
*req
, const char *method
,
132 struct blob_attr
*msg
)
134 struct blob_attr
*tb
[__DI_MAX
];
135 struct interface
*iface
;
136 struct blob_attr
*config
;
138 blobmsg_parse(dynamic_policy
, __DI_MAX
, tb
, blob_data(msg
), blob_len(msg
));
141 return UBUS_STATUS_INVALID_ARGUMENT
;
143 const char *name
= blobmsg_get_string(tb
[DI_NAME
]);
145 iface
= interface_alloc(name
, msg
, true);
147 return UBUS_STATUS_UNKNOWN_ERROR
;
149 config
= blob_memdup(msg
);
153 if (!interface_add(iface
, config
))
154 goto error_free_config
;
156 return UBUS_STATUS_OK
;
162 return UBUS_STATUS_UNKNOWN_ERROR
;
171 static const struct blobmsg_policy netns_updown_policy
[__NETNS_UPDOWN_MAX
] = {
172 [NETNS_UPDOWN_JAIL
] = { .name
= "jail", .type
= BLOBMSG_TYPE_STRING
},
173 [NETNS_UPDOWN_START
] = { .name
= "start", .type
= BLOBMSG_TYPE_BOOL
},
177 netifd_netns_updown(struct ubus_context
*ctx
, struct ubus_object
*obj
,
178 struct ubus_request_data
*req
, const char *method
,
179 struct blob_attr
*msg
)
181 int target_netns_fd
= ubus_request_get_caller_fd(req
);
182 struct blob_attr
*tb
[__NETNS_UPDOWN_MAX
];
185 if (target_netns_fd
< 0)
186 return UBUS_STATUS_INVALID_ARGUMENT
;
188 blobmsg_parse(netns_updown_policy
, __NETNS_UPDOWN_MAX
, tb
, blob_data(msg
), blob_len(msg
));
190 start
= tb
[NETNS_UPDOWN_START
] && blobmsg_get_bool(tb
[NETNS_UPDOWN_START
]);
193 if (!tb
[NETNS_UPDOWN_JAIL
])
194 return UBUS_STATUS_INVALID_ARGUMENT
;
196 interface_start_jail(target_netns_fd
, blobmsg_get_string(tb
[NETNS_UPDOWN_JAIL
]));
198 interface_stop_jail(target_netns_fd
);
200 close(target_netns_fd
);
202 return UBUS_STATUS_OK
;
205 static struct ubus_method main_object_methods
[] = {
206 { .name
= "restart", .handler
= netifd_handle_restart
},
207 { .name
= "reload", .handler
= netifd_handle_reload
},
208 UBUS_METHOD("add_host_route", netifd_add_host_route
, route_policy
),
209 { .name
= "get_proto_handlers", .handler
= netifd_get_proto_handlers
},
210 UBUS_METHOD("add_dynamic", netifd_add_dynamic
, dynamic_policy
),
211 UBUS_METHOD("netns_updown", netifd_netns_updown
, netns_updown_policy
),
214 static struct ubus_object_type main_object_type
=
215 UBUS_OBJECT_TYPE("netifd", main_object_methods
);
217 static struct ubus_object main_object
= {
219 .type
= &main_object_type
,
220 .methods
= main_object_methods
,
221 .n_methods
= ARRAY_SIZE(main_object_methods
),
229 static const struct blobmsg_policy dev_policy
[__DEV_MAX
] = {
230 [DEV_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
234 netifd_dev_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
235 struct ubus_request_data
*req
, const char *method
,
236 struct blob_attr
*msg
)
238 struct device
*dev
= NULL
;
239 struct blob_attr
*tb
[__DEV_MAX
];
241 blobmsg_parse(dev_policy
, __DEV_MAX
, tb
, blob_data(msg
), blob_len(msg
));
244 dev
= device_find(blobmsg_data(tb
[DEV_NAME
]));
246 return UBUS_STATUS_INVALID_ARGUMENT
;
249 blob_buf_init(&b
, 0);
250 device_dump_status(&b
, dev
);
251 ubus_send_reply(ctx
, req
, b
.head
);
262 static const struct blobmsg_policy alias_attrs
[__ALIAS_ATTR_MAX
] = {
263 [ALIAS_ATTR_ALIAS
] = { "alias", BLOBMSG_TYPE_ARRAY
},
264 [ALIAS_ATTR_DEV
] = { "device", BLOBMSG_TYPE_STRING
},
268 netifd_handle_alias(struct ubus_context
*ctx
, struct ubus_object
*obj
,
269 struct ubus_request_data
*req
, const char *method
,
270 struct blob_attr
*msg
)
272 struct device
*dev
= NULL
;
273 struct blob_attr
*tb
[__ALIAS_ATTR_MAX
];
274 struct blob_attr
*cur
;
277 blobmsg_parse(alias_attrs
, __ALIAS_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
279 if (!tb
[ALIAS_ATTR_ALIAS
])
280 return UBUS_STATUS_INVALID_ARGUMENT
;
282 if ((cur
= tb
[ALIAS_ATTR_DEV
]) != NULL
) {
283 dev
= device_get(blobmsg_data(cur
), true);
285 return UBUS_STATUS_NOT_FOUND
;
288 blobmsg_for_each_attr(cur
, tb
[ALIAS_ATTR_ALIAS
], rem
) {
289 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
292 if (!blobmsg_check_attr(cur
, false))
295 alias_notify_device(blobmsg_data(cur
), dev
);
300 device_free_unused();
301 return UBUS_STATUS_INVALID_ARGUMENT
;
307 DEV_STATE_AUTH_STATUS
,
308 DEV_STATE_AUTH_VLANS
,
312 static const struct blobmsg_policy dev_state_policy
[__DEV_STATE_MAX
] = {
313 [DEV_STATE_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
314 [DEV_STATE_DEFER
] = { .name
= "defer", .type
= BLOBMSG_TYPE_BOOL
},
315 [DEV_STATE_AUTH_STATUS
] = { .name
= "auth_status", .type
= BLOBMSG_TYPE_BOOL
},
316 [DEV_STATE_AUTH_VLANS
] = { .name
= "auth_vlans", BLOBMSG_TYPE_ARRAY
},
320 netifd_handle_set_state(struct ubus_context
*ctx
, struct ubus_object
*obj
,
321 struct ubus_request_data
*req
, const char *method
,
322 struct blob_attr
*msg
)
324 struct device
*dev
= NULL
;
325 struct blob_attr
*tb
[__DEV_STATE_MAX
];
326 struct blob_attr
*cur
;
329 blobmsg_parse(dev_state_policy
, __DEV_STATE_MAX
, tb
, blob_data(msg
), blob_len(msg
));
331 cur
= tb
[DEV_STATE_NAME
];
333 return UBUS_STATUS_INVALID_ARGUMENT
;
335 dev
= device_find(blobmsg_data(cur
));
337 return UBUS_STATUS_NOT_FOUND
;
339 cur
= tb
[DEV_STATE_DEFER
];
341 device_set_deferred(dev
, !!blobmsg_get_u8(cur
));
343 if ((cur
= tb
[DEV_STATE_AUTH_STATUS
]) != NULL
)
344 auth_status
= blobmsg_get_bool(cur
);
346 auth_status
= dev
->auth_status
;
347 if (tb
[DEV_STATE_AUTH_STATUS
] || tb
[DEV_STATE_AUTH_VLANS
])
348 device_set_auth_status(dev
, auth_status
, tb
[DEV_STATE_AUTH_VLANS
]);
355 DEV_HOTPLUG_ATTR_NAME
,
356 DEV_HOTPLUG_ATTR_ADD
,
357 __DEV_HOTPLUG_ATTR_MAX
,
360 static const struct blobmsg_policy dev_hotplug_policy
[__DEV_HOTPLUG_ATTR_MAX
] = {
361 [DEV_HOTPLUG_ATTR_NAME
] = { "name", BLOBMSG_TYPE_STRING
},
362 [DEV_HOTPLUG_ATTR_ADD
] = { "add", BLOBMSG_TYPE_BOOL
},
366 netifd_handle_dev_hotplug(struct ubus_context
*ctx
, struct ubus_object
*obj
,
367 struct ubus_request_data
*req
, const char *method
,
368 struct blob_attr
*msg
)
370 struct blob_attr
*tb
[__DEV_HOTPLUG_ATTR_MAX
];
373 blobmsg_parse(dev_hotplug_policy
, __DEV_HOTPLUG_ATTR_MAX
, tb
,
374 blob_data(msg
), blob_len(msg
));
376 if (!tb
[DEV_HOTPLUG_ATTR_NAME
] || !tb
[DEV_HOTPLUG_ATTR_ADD
])
377 return UBUS_STATUS_INVALID_ARGUMENT
;
379 name
= blobmsg_get_string(tb
[DEV_HOTPLUG_ATTR_NAME
]);
380 device_hotplug_event(name
, blobmsg_get_bool(tb
[DEV_HOTPLUG_ATTR_ADD
]));
387 netifd_handle_stp_init(struct ubus_context
*ctx
, struct ubus_object
*obj
,
388 struct ubus_request_data
*req
, const char *method
,
389 struct blob_attr
*msg
)
396 static struct ubus_method dev_object_methods
[] = {
397 UBUS_METHOD("status", netifd_dev_status
, dev_policy
),
398 UBUS_METHOD("set_alias", netifd_handle_alias
, alias_attrs
),
399 UBUS_METHOD("set_state", netifd_handle_set_state
, dev_state_policy
),
401 UBUS_METHOD("hotplug_event", netifd_handle_dev_hotplug
, dev_hotplug_policy
),
403 UBUS_METHOD_NOARG("stp_init", netifd_handle_stp_init
)
406 static struct ubus_object_type dev_object_type
=
407 UBUS_OBJECT_TYPE("device", dev_object_methods
);
409 static struct ubus_object dev_object
= {
410 .name
= "network.device",
411 .type
= &dev_object_type
,
412 .methods
= dev_object_methods
,
413 .n_methods
= ARRAY_SIZE(dev_object_methods
),
417 netifd_ubus_add_fd(void)
419 ubus_add_uloop(ubus_ctx
);
420 system_fd_set_cloexec(ubus_ctx
->sock
.fd
);
423 void netifd_ubus_device_notify(const char *event
, struct blob_attr
*data
, int timeout
)
425 ubus_notify(ubus_ctx
, &dev_object
, event
, data
, timeout
);
429 netifd_ubus_reconnect_timer(struct uloop_timeout
*timeout
)
431 static struct uloop_timeout retry
= {
432 .cb
= netifd_ubus_reconnect_timer
,
436 if (ubus_reconnect(ubus_ctx
, ubus_path
) != 0) {
437 DPRINTF("failed to reconnect, trying again in %d seconds\n", t
);
438 uloop_timeout_set(&retry
, t
* 1000);
442 DPRINTF("reconnected to ubus, new id: %08x\n", ubus_ctx
->local_id
);
443 netifd_ubus_add_fd();
447 netifd_ubus_connection_lost(struct ubus_context
*ctx
)
449 netifd_ubus_reconnect_timer(NULL
);
452 /* per-interface object */
455 netifd_handle_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
456 struct ubus_request_data
*req
, const char *method
,
457 struct blob_attr
*msg
)
459 struct interface
*iface
;
461 iface
= container_of(obj
, struct interface
, ubus
);
462 interface_set_up(iface
);
468 netifd_handle_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
469 struct ubus_request_data
*req
, const char *method
,
470 struct blob_attr
*msg
)
472 struct interface
*iface
;
474 iface
= container_of(obj
, struct interface
, ubus
);
475 interface_set_down(iface
);
481 netifd_handle_renew(struct ubus_context
*ctx
, struct ubus_object
*obj
,
482 struct ubus_request_data
*req
, const char *method
,
483 struct blob_attr
*msg
)
485 struct interface
*iface
;
487 iface
= container_of(obj
, struct interface
, ubus
);
488 interface_renew(iface
);
494 netifd_add_interface_errors(struct blob_buf
*b
, struct interface
*iface
)
496 struct interface_error
*error
;
500 e
= blobmsg_open_array(b
, "errors");
501 list_for_each_entry(error
, &iface
->errors
, list
) {
502 e2
= blobmsg_open_table(b
, NULL
);
504 blobmsg_add_string(b
, "subsystem", error
->subsystem
);
505 blobmsg_add_string(b
, "code", error
->code
);
506 if (error
->data
[0]) {
507 e3
= blobmsg_open_array(b
, "data");
508 for (i
= 0; error
->data
[i
]; i
++)
509 blobmsg_add_string(b
, NULL
, error
->data
[i
]);
510 blobmsg_close_array(b
, e3
);
513 blobmsg_close_table(b
, e2
);
515 blobmsg_close_array(b
, e
);
519 interface_ip_dump_address_list(struct interface_ip_settings
*ip
, bool v6
, bool enabled
)
521 struct device_addr
*addr
;
527 time_t now
= system_get_rtime();
528 vlist_for_each_element(&ip
->addr
, addr
, node
) {
529 if (addr
->enabled
!= enabled
)
532 if ((addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
537 if (af
!= (v6
? AF_INET6
: AF_INET
))
540 a
= blobmsg_open_table(&b
, NULL
);
542 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
543 inet_ntop(af
, &addr
->addr
, buf
, buflen
);
544 blobmsg_add_string_buffer(&b
);
546 blobmsg_add_u32(&b
, "mask", addr
->mask
);
548 if (addr
->point_to_point
) {
549 buf
= blobmsg_alloc_string_buffer(&b
, "ptpaddress", buflen
);
550 inet_ntop(af
, &addr
->point_to_point
, buf
, buflen
);
551 blobmsg_add_string_buffer(&b
);
554 if (addr
->preferred_until
) {
555 int preferred
= addr
->preferred_until
- now
;
558 blobmsg_add_u32(&b
, "preferred", preferred
);
561 if (addr
->valid_until
)
562 blobmsg_add_u32(&b
, "valid", addr
->valid_until
- now
);
565 blobmsg_add_string(&b
, "class", addr
->pclass
);
567 blobmsg_close_table(&b
, a
);
572 interface_ip_dump_neighbor_list(struct interface_ip_settings
*ip
, bool enabled
)
574 struct device_neighbor
*neighbor
;
580 vlist_for_each_element(&ip
->neighbor
, neighbor
, node
) {
581 if (neighbor
->enabled
!= enabled
)
584 if ((neighbor
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
589 r
= blobmsg_open_table(&b
, NULL
);
591 if (neighbor
->flags
& DEVNEIGH_MAC
)
592 blobmsg_add_string(&b
, "mac", format_macaddr(neighbor
->macaddr
));
594 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
595 inet_ntop(af
, &neighbor
->addr
, buf
, buflen
);
596 blobmsg_add_string_buffer(&b
);
599 blobmsg_add_u32(&b
, "proxy", neighbor
->proxy
);
601 if (neighbor
->router
)
602 blobmsg_add_u32(&b
, "router", neighbor
->router
);
604 blobmsg_close_table(&b
, r
);
609 interface_ip_dump_route_list(struct interface_ip_settings
*ip
, bool enabled
)
611 struct device_route
*route
;
617 time_t now
= system_get_rtime();
618 vlist_for_each_element(&ip
->route
, route
, node
) {
619 if (route
->enabled
!= enabled
)
622 if ((ip
->no_defaultroute
== enabled
) && !route
->mask
)
625 if ((route
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
630 r
= blobmsg_open_table(&b
, NULL
);
632 buf
= blobmsg_alloc_string_buffer(&b
, "target", buflen
);
633 inet_ntop(af
, &route
->addr
, buf
, buflen
);
634 blobmsg_add_string_buffer(&b
);
636 blobmsg_add_u32(&b
, "mask", route
->mask
);
638 buf
= blobmsg_alloc_string_buffer(&b
, "nexthop", buflen
);
639 inet_ntop(af
, &route
->nexthop
, buf
, buflen
);
640 blobmsg_add_string_buffer(&b
);
642 if (route
->flags
& DEVROUTE_TYPE
)
643 blobmsg_add_u32(&b
, "type", route
->type
);
645 if (route
->flags
& DEVROUTE_PROTO
)
646 blobmsg_add_u32(&b
, "proto", route
->proto
);
648 if (route
->flags
& DEVROUTE_MTU
)
649 blobmsg_add_u32(&b
, "mtu", route
->mtu
);
651 if (route
->flags
& DEVROUTE_METRIC
)
652 blobmsg_add_u32(&b
, "metric", route
->metric
);
654 if (route
->flags
& DEVROUTE_TABLE
)
655 blobmsg_add_u32(&b
, "table", route
->table
);
657 if (route
->valid_until
)
658 blobmsg_add_u32(&b
, "valid", route
->valid_until
- now
);
660 buf
= blobmsg_alloc_string_buffer(&b
, "source", buflen
);
661 inet_ntop(af
, &route
->source
, buf
, buflen
);
662 snprintf(buf
+ strlen(buf
), buflen
- strlen(buf
), "/%u", route
->sourcemask
);
663 blobmsg_add_string_buffer(&b
);
665 blobmsg_close_table(&b
, r
);
671 interface_ip_dump_prefix_list(struct interface_ip_settings
*ip
)
673 struct device_prefix
*prefix
;
676 const int buflen
= INET6_ADDRSTRLEN
;
678 time_t now
= system_get_rtime();
679 vlist_for_each_element(&ip
->prefix
, prefix
, node
) {
680 a
= blobmsg_open_table(&b
, NULL
);
682 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
683 inet_ntop(AF_INET6
, &prefix
->addr
, buf
, buflen
);
684 blobmsg_add_string_buffer(&b
);
686 blobmsg_add_u32(&b
, "mask", prefix
->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 blobmsg_add_string(&b
, "class", prefix
->pclass
);
700 c
= blobmsg_open_table(&b
, "assigned");
701 struct device_prefix_assignment
*assign
;
702 list_for_each_entry(assign
, &prefix
->assignments
, head
) {
703 if (!assign
->name
[0])
706 struct in6_addr addr
= prefix
->addr
;
707 addr
.s6_addr32
[1] |= htonl(assign
->assigned
);
709 void *d
= blobmsg_open_table(&b
, assign
->name
);
711 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
712 inet_ntop(AF_INET6
, &addr
, buf
, buflen
);
713 blobmsg_add_string_buffer(&b
);
715 blobmsg_add_u32(&b
, "mask", assign
->length
);
717 blobmsg_close_table(&b
, d
);
719 blobmsg_close_table(&b
, c
);
721 blobmsg_close_table(&b
, a
);
727 interface_ip_dump_prefix_assignment_list(struct interface
*iface
)
731 const int buflen
= INET6_ADDRSTRLEN
;
732 time_t now
= system_get_rtime();
734 struct device_prefix
*prefix
;
735 list_for_each_entry(prefix
, &prefixes
, head
) {
736 struct device_prefix_assignment
*assign
;
737 list_for_each_entry(assign
, &prefix
->assignments
, head
) {
738 if (strcmp(assign
->name
, iface
->name
))
741 struct in6_addr addr
= prefix
->addr
;
742 addr
.s6_addr32
[1] |= htonl(assign
->assigned
);
744 a
= blobmsg_open_table(&b
, NULL
);
746 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
747 inet_ntop(AF_INET6
, &addr
, buf
, buflen
);
748 blobmsg_add_string_buffer(&b
);
750 blobmsg_add_u32(&b
, "mask", assign
->length
);
752 if (prefix
->preferred_until
) {
753 int preferred
= prefix
->preferred_until
- now
;
756 blobmsg_add_u32(&b
, "preferred", preferred
);
759 if (prefix
->valid_until
)
760 blobmsg_add_u32(&b
, "valid", prefix
->valid_until
- now
);
762 void *c
= blobmsg_open_table(&b
, "local-address");
763 if (assign
->enabled
) {
764 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
765 inet_ntop(AF_INET6
, &assign
->addr
, buf
, buflen
);
766 blobmsg_add_string_buffer(&b
);
768 blobmsg_add_u32(&b
, "mask", assign
->length
);
770 blobmsg_close_table(&b
, c
);
772 blobmsg_close_table(&b
, a
);
778 interface_ip_dump_dns_server_list(struct interface_ip_settings
*ip
, bool enabled
)
780 struct dns_server
*dns
;
784 vlist_simple_for_each_element(&ip
->dns_servers
, dns
, node
) {
785 if (ip
->no_dns
== enabled
)
788 buf
= blobmsg_alloc_string_buffer(&b
, NULL
, buflen
);
789 inet_ntop(dns
->af
, &dns
->addr
, buf
, buflen
);
790 blobmsg_add_string_buffer(&b
);
795 interface_ip_dump_dns_search_list(struct interface_ip_settings
*ip
, bool enabled
)
797 struct dns_search_domain
*dns
;
799 vlist_simple_for_each_element(&ip
->dns_search
, dns
, node
) {
800 if (ip
->no_dns
== enabled
)
803 blobmsg_add_string(&b
, NULL
, dns
->name
);
808 netifd_dump_status(struct interface
*iface
)
810 struct interface_data
*data
;
814 blobmsg_add_u8(&b
, "up", iface
->state
== IFS_UP
);
815 blobmsg_add_u8(&b
, "pending", iface
->state
== IFS_SETUP
);
816 blobmsg_add_u8(&b
, "available", iface
->available
);
817 blobmsg_add_u8(&b
, "autostart", iface
->autostart
);
818 blobmsg_add_u8(&b
, "dynamic", iface
->dynamic
);
820 if (iface
->state
== IFS_UP
) {
821 time_t cur
= system_get_rtime();
822 blobmsg_add_u32(&b
, "uptime", cur
- iface
->start_time
);
823 if (iface
->l3_dev
.dev
)
824 blobmsg_add_string(&b
, "l3_device", iface
->l3_dev
.dev
->ifname
);
827 if (iface
->proto_handler
)
828 blobmsg_add_string(&b
, "proto", iface
->proto_handler
->name
);
830 dev
= iface
->main_dev
.dev
;
831 if (dev
&& !dev
->hidden
&& iface
->proto_handler
&&
832 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
))
833 blobmsg_add_string(&b
, "device", dev
->ifname
);
836 blobmsg_add_string(&b
, "jail", iface
->jail
);
838 if (iface
->jail_device
)
839 blobmsg_add_string(&b
, "jail_device", iface
->jail_device
);
841 if (iface
->state
== IFS_UP
) {
842 if (iface
->updated
) {
843 a
= blobmsg_open_array(&b
, "updated");
845 if (iface
->updated
& IUF_ADDRESS
)
846 blobmsg_add_string(&b
, NULL
, "addresses");
847 if (iface
->updated
& IUF_ROUTE
)
848 blobmsg_add_string(&b
, NULL
, "routes");
849 if (iface
->updated
& IUF_PREFIX
)
850 blobmsg_add_string(&b
, NULL
, "prefixes");
851 if (iface
->updated
& IUF_DATA
)
852 blobmsg_add_string(&b
, NULL
, "data");
854 blobmsg_close_array(&b
, a
);
858 blobmsg_add_u32(&b
, "ip4table", iface
->ip4table
);
860 blobmsg_add_u32(&b
, "ip6table", iface
->ip6table
);
861 blobmsg_add_u32(&b
, "metric", iface
->metric
);
862 blobmsg_add_u32(&b
, "dns_metric", iface
->dns_metric
);
863 blobmsg_add_u8(&b
, "delegation", !iface
->proto_ip
.no_delegation
);
864 if (iface
->assignment_weight
)
865 blobmsg_add_u32(&b
, "ip6weight", iface
->assignment_weight
);
866 a
= blobmsg_open_array(&b
, "ipv4-address");
867 interface_ip_dump_address_list(&iface
->config_ip
, false, true);
868 interface_ip_dump_address_list(&iface
->proto_ip
, false, true);
869 blobmsg_close_array(&b
, a
);
870 a
= blobmsg_open_array(&b
, "ipv6-address");
871 interface_ip_dump_address_list(&iface
->config_ip
, true, true);
872 interface_ip_dump_address_list(&iface
->proto_ip
, true, true);
873 blobmsg_close_array(&b
, a
);
874 a
= blobmsg_open_array(&b
, "ipv6-prefix");
875 interface_ip_dump_prefix_list(&iface
->config_ip
);
876 interface_ip_dump_prefix_list(&iface
->proto_ip
);
877 blobmsg_close_array(&b
, a
);
878 a
= blobmsg_open_array(&b
, "ipv6-prefix-assignment");
879 interface_ip_dump_prefix_assignment_list(iface
);
880 blobmsg_close_array(&b
, a
);
881 a
= blobmsg_open_array(&b
, "route");
882 interface_ip_dump_route_list(&iface
->config_ip
, true);
883 interface_ip_dump_route_list(&iface
->proto_ip
, true);
884 blobmsg_close_array(&b
, a
);
885 a
= blobmsg_open_array(&b
, "dns-server");
886 interface_ip_dump_dns_server_list(&iface
->config_ip
, true);
887 interface_ip_dump_dns_server_list(&iface
->proto_ip
, true);
888 blobmsg_close_array(&b
, a
);
889 a
= blobmsg_open_array(&b
, "dns-search");
890 interface_ip_dump_dns_search_list(&iface
->config_ip
, true);
891 interface_ip_dump_dns_search_list(&iface
->proto_ip
, true);
892 blobmsg_close_array(&b
, a
);
893 a
= blobmsg_open_array(&b
, "neighbors");
894 interface_ip_dump_neighbor_list(&iface
->config_ip
, true);
895 interface_ip_dump_neighbor_list(&iface
->proto_ip
, true);
896 blobmsg_close_array(&b
, a
);
898 inactive
= blobmsg_open_table(&b
, "inactive");
899 a
= blobmsg_open_array(&b
, "ipv4-address");
900 interface_ip_dump_address_list(&iface
->config_ip
, false, false);
901 interface_ip_dump_address_list(&iface
->proto_ip
, false, false);
902 blobmsg_close_array(&b
, a
);
903 a
= blobmsg_open_array(&b
, "ipv6-address");
904 interface_ip_dump_address_list(&iface
->config_ip
, true, false);
905 interface_ip_dump_address_list(&iface
->proto_ip
, true, false);
906 blobmsg_close_array(&b
, a
);
907 a
= blobmsg_open_array(&b
, "route");
908 interface_ip_dump_route_list(&iface
->config_ip
, false);
909 interface_ip_dump_route_list(&iface
->proto_ip
, false);
910 blobmsg_close_array(&b
, a
);
911 a
= blobmsg_open_array(&b
, "dns-server");
912 interface_ip_dump_dns_server_list(&iface
->config_ip
, false);
913 interface_ip_dump_dns_server_list(&iface
->proto_ip
, false);
914 blobmsg_close_array(&b
, a
);
915 a
= blobmsg_open_array(&b
, "dns-search");
916 interface_ip_dump_dns_search_list(&iface
->config_ip
, false);
917 interface_ip_dump_dns_search_list(&iface
->proto_ip
, false);
918 blobmsg_close_array(&b
, a
);
919 a
= blobmsg_open_array(&b
, "neighbors");
920 interface_ip_dump_neighbor_list(&iface
->config_ip
, false);
921 interface_ip_dump_neighbor_list(&iface
->proto_ip
, false);
922 blobmsg_close_array(&b
, a
);
923 blobmsg_close_table(&b
, inactive
);
926 a
= blobmsg_open_table(&b
, "data");
929 blobmsg_add_string(&b
, "zone", iface
->zone
);
930 avl_for_each_element(&iface
->data
, data
, node
)
931 blobmsg_add_blob(&b
, data
->data
);
933 blobmsg_close_table(&b
, a
);
935 if (!list_empty(&iface
->errors
))
936 netifd_add_interface_errors(&b
, iface
);
940 netifd_handle_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
941 struct ubus_request_data
*req
, const char *method
,
942 struct blob_attr
*msg
)
944 struct interface
*iface
= container_of(obj
, struct interface
, ubus
);
946 blob_buf_init(&b
, 0);
947 netifd_dump_status(iface
);
948 ubus_send_reply(ctx
, req
, b
.head
);
955 netifd_handle_dump(struct ubus_context
*ctx
, struct ubus_object
*obj
,
956 struct ubus_request_data
*req
, const char *method
,
957 struct blob_attr
*msg
)
959 blob_buf_init(&b
, 0);
960 void *a
= blobmsg_open_array(&b
, "interface");
962 struct interface
*iface
;
963 vlist_for_each_element(&interfaces
, iface
, node
) {
964 void *i
= blobmsg_open_table(&b
, NULL
);
965 blobmsg_add_string(&b
, "interface", iface
->name
);
966 netifd_dump_status(iface
);
967 blobmsg_close_table(&b
, i
);
970 blobmsg_close_array(&b
, a
);
971 ubus_send_reply(ctx
, req
, b
.head
);
983 static const struct blobmsg_policy dev_link_policy
[__DEV_LINK_MAX
] = {
984 [DEV_LINK_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
985 [DEV_LINK_EXT
] = { .name
= "link-ext", .type
= BLOBMSG_TYPE_BOOL
},
986 [DEV_LINK_VLAN
] = { .name
= "vlan", .type
= BLOBMSG_TYPE_ARRAY
},
990 netifd_iface_handle_device(struct ubus_context
*ctx
, struct ubus_object
*obj
,
991 struct ubus_request_data
*req
, const char *method
,
992 struct blob_attr
*msg
)
994 struct blob_attr
*tb
[__DEV_LINK_MAX
];
995 struct blob_attr
*cur
;
996 struct interface
*iface
;
997 bool add
= !strncmp(method
, "add", 3);
998 bool link_ext
= true;
1000 iface
= container_of(obj
, struct interface
, ubus
);
1002 blobmsg_parse(dev_link_policy
, __DEV_LINK_MAX
, tb
, blob_data(msg
), blob_len(msg
));
1004 if (!tb
[DEV_LINK_NAME
])
1005 return UBUS_STATUS_INVALID_ARGUMENT
;
1007 cur
= tb
[DEV_LINK_EXT
];
1009 link_ext
= blobmsg_get_bool(cur
);
1011 return interface_handle_link(iface
, blobmsg_data(tb
[DEV_LINK_NAME
]),
1012 tb
[DEV_LINK_VLAN
], add
, link_ext
);
1017 netifd_iface_notify_proto(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1018 struct ubus_request_data
*req
, const char *method
,
1019 struct blob_attr
*msg
)
1021 struct interface
*iface
;
1023 iface
= container_of(obj
, struct interface
, ubus
);
1025 if (!iface
->proto
|| !iface
->proto
->notify
)
1026 return UBUS_STATUS_NOT_SUPPORTED
;
1028 return iface
->proto
->notify(iface
->proto
, msg
);
1032 netifd_iface_do_remove(struct uloop_timeout
*timeout
)
1034 struct interface
*iface
;
1036 iface
= container_of(timeout
, struct interface
, remove_timer
);
1037 vlist_delete(&interfaces
, &iface
->node
);
1041 netifd_iface_remove(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1042 struct ubus_request_data
*req
, const char *method
,
1043 struct blob_attr
*msg
)
1045 struct interface
*iface
;
1047 iface
= container_of(obj
, struct interface
, ubus
);
1048 if (iface
->remove_timer
.cb
)
1049 return UBUS_STATUS_INVALID_ARGUMENT
;
1051 iface
->remove_timer
.cb
= netifd_iface_do_remove
;
1052 uloop_timeout_set(&iface
->remove_timer
, 100);
1057 netifd_handle_iface_prepare(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1058 struct ubus_request_data
*req
, const char *method
,
1059 struct blob_attr
*msg
)
1061 struct interface
*iface
;
1062 struct device
*dev
, *bridge_dev
= NULL
;
1063 const struct device_hotplug_ops
*ops
;
1065 iface
= container_of(obj
, struct interface
, ubus
);
1066 dev
= iface
->main_dev
.dev
;
1070 ops
= dev
->hotplug_ops
;
1074 ops
->prepare(dev
, &bridge_dev
);
1077 blob_buf_init(&b
, 0);
1079 blobmsg_add_string(&b
, "bridge", bridge_dev
->ifname
);
1080 ubus_send_reply(ctx
, req
, b
.head
);
1086 netifd_handle_set_data(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1087 struct ubus_request_data
*req
, const char *method
,
1088 struct blob_attr
*msg
)
1090 struct interface
*iface
;
1092 iface
= container_of(obj
, struct interface
, ubus
);
1094 return interface_parse_data(iface
, msg
);
1097 static struct ubus_method iface_object_methods
[] = {
1098 { .name
= "up", .handler
= netifd_handle_up
},
1099 { .name
= "down", .handler
= netifd_handle_down
},
1100 { .name
= "renew", .handler
= netifd_handle_renew
},
1101 { .name
= "status", .handler
= netifd_handle_status
},
1102 { .name
= "prepare", .handler
= netifd_handle_iface_prepare
},
1103 { .name
= "dump", .handler
= netifd_handle_dump
},
1104 UBUS_METHOD("add_device", netifd_iface_handle_device
, dev_link_policy
),
1105 UBUS_METHOD("remove_device", netifd_iface_handle_device
, dev_link_policy
),
1106 { .name
= "notify_proto", .handler
= netifd_iface_notify_proto
},
1107 { .name
= "remove", .handler
= netifd_iface_remove
},
1108 { .name
= "set_data", .handler
= netifd_handle_set_data
},
1111 static struct ubus_object_type iface_object_type
=
1112 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods
);
1115 static struct ubus_object iface_object
= {
1116 .name
= "network.interface",
1117 .type
= &iface_object_type
,
1118 .n_methods
= ARRAY_SIZE(iface_object_methods
),
1121 static void netifd_add_object(struct ubus_object
*obj
)
1123 int ret
= ubus_add_object(ubus_ctx
, obj
);
1126 fprintf(stderr
, "Failed to publish object '%s': %s\n", obj
->name
, ubus_strerror(ret
));
1129 static const struct blobmsg_policy iface_policy
= {
1130 .name
= "interface",
1131 .type
= BLOBMSG_TYPE_STRING
,
1135 netifd_handle_iface(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1136 struct ubus_request_data
*req
, const char *method
,
1137 struct blob_attr
*msg
)
1139 struct interface
*iface
;
1140 struct blob_attr
*tb
;
1143 blobmsg_parse(&iface_policy
, 1, &tb
, blob_data(msg
), blob_len(msg
));
1145 return UBUS_STATUS_INVALID_ARGUMENT
;
1147 iface
= vlist_find(&interfaces
, blobmsg_data(tb
), iface
, node
);
1149 return UBUS_STATUS_NOT_FOUND
;
1151 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
1154 if (strcmp(method
, iface_object_methods
[i
].name
) != 0)
1157 cb
= iface_object_methods
[i
].handler
;
1158 return cb(ctx
, &iface
->ubus
, req
, method
, msg
);
1161 return UBUS_STATUS_INVALID_ARGUMENT
;
1164 static void netifd_add_iface_object(void)
1166 struct ubus_method
*methods
;
1169 methods
= calloc(1, sizeof(iface_object_methods
));
1173 memcpy(methods
, iface_object_methods
, sizeof(iface_object_methods
));
1174 iface_object
.methods
= methods
;
1176 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
1177 if (methods
[i
].handler
== netifd_handle_dump
)
1180 methods
[i
].handler
= netifd_handle_iface
;
1181 methods
[i
].policy
= &iface_policy
;
1182 methods
[i
].n_policy
= 1;
1184 netifd_add_object(&iface_object
);
1187 static struct wireless_device
*
1188 get_wdev(struct blob_attr
*msg
, int *ret
)
1190 struct blobmsg_policy wdev_policy
= {
1192 .type
= BLOBMSG_TYPE_STRING
,
1194 struct blob_attr
*dev_attr
;
1195 struct wireless_device
*wdev
= NULL
;
1198 blobmsg_parse(&wdev_policy
, 1, &dev_attr
, blob_data(msg
), blob_len(msg
));
1200 *ret
= UBUS_STATUS_INVALID_ARGUMENT
;
1204 wdev
= vlist_find(&wireless_devices
, blobmsg_data(dev_attr
), wdev
, node
);
1206 *ret
= UBUS_STATUS_NOT_FOUND
;
1215 netifd_handle_wdev_reconf(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1216 struct ubus_request_data
*req
, const char *method
,
1217 struct blob_attr
*msg
)
1219 struct wireless_device
*wdev
;
1222 wdev
= get_wdev(msg
, &ret
);
1223 if (ret
== UBUS_STATUS_NOT_FOUND
)
1227 wireless_device_reconf(wdev
);
1229 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1230 wireless_device_reconf(wdev
);
1237 netifd_handle_wdev_up(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
)
1249 wireless_device_set_up(wdev
);
1251 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1252 wireless_device_set_up(wdev
);
1259 netifd_handle_wdev_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1260 struct ubus_request_data
*req
, const char *method
,
1261 struct blob_attr
*msg
)
1263 struct wireless_device
*wdev
;
1266 wdev
= get_wdev(msg
, &ret
);
1267 if (ret
== UBUS_STATUS_NOT_FOUND
)
1271 wireless_device_set_down(wdev
);
1273 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1274 wireless_device_set_down(wdev
);
1281 netifd_handle_wdev_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1282 struct ubus_request_data
*req
, const char *method
,
1283 struct blob_attr
*msg
)
1285 struct wireless_device
*wdev
;
1288 wdev
= get_wdev(msg
, &ret
);
1289 if (ret
== UBUS_STATUS_NOT_FOUND
)
1292 blob_buf_init(&b
, 0);
1294 wireless_device_status(wdev
, &b
);
1296 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1297 wireless_device_status(wdev
, &b
);
1299 ubus_send_reply(ctx
, req
, b
.head
);
1304 netifd_handle_wdev_get_validate(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1305 struct ubus_request_data
*req
, const char *method
,
1306 struct blob_attr
*msg
)
1308 struct wireless_device
*wdev
;
1311 wdev
= get_wdev(msg
, &ret
);
1312 if (ret
== UBUS_STATUS_NOT_FOUND
)
1315 blob_buf_init(&b
, 0);
1317 wireless_device_get_validate(wdev
, &b
);
1319 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1320 wireless_device_get_validate(wdev
, &b
);
1322 ubus_send_reply(ctx
, req
, b
.head
);
1327 netifd_handle_wdev_notify(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1328 struct ubus_request_data
*req
, const char *method
,
1329 struct blob_attr
*msg
)
1331 struct wireless_device
*wdev
;
1334 wdev
= get_wdev(msg
, &ret
);
1338 return wireless_device_notify(wdev
, msg
, req
);
1341 static struct ubus_method wireless_object_methods
[] = {
1342 { .name
= "up", .handler
= netifd_handle_wdev_up
},
1343 { .name
= "down", .handler
= netifd_handle_wdev_down
},
1344 { .name
= "reconf", .handler
= netifd_handle_wdev_reconf
},
1345 { .name
= "status", .handler
= netifd_handle_wdev_status
},
1346 { .name
= "notify", .handler
= netifd_handle_wdev_notify
},
1347 { .name
= "get_validate", .handler
= netifd_handle_wdev_get_validate
},
1350 static struct ubus_object_type wireless_object_type
=
1351 UBUS_OBJECT_TYPE("netifd_iface", wireless_object_methods
);
1354 static struct ubus_object wireless_object
= {
1355 .name
= "network.wireless",
1356 .type
= &wireless_object_type
,
1357 .methods
= wireless_object_methods
,
1358 .n_methods
= ARRAY_SIZE(wireless_object_methods
),
1362 netifd_extdev_invoke(uint32_t id
, const char *method
, struct blob_attr
*msg
,
1363 ubus_data_handler_t data_cb
, void *data
)
1365 return ubus_invoke(ubus_ctx
, id
, method
, msg
, data_cb
, data
, 3000);
1369 netifd_ubus_init(const char *path
)
1374 ubus_ctx
= ubus_connect(path
);
1378 DPRINTF("connected as %08x\n", ubus_ctx
->local_id
);
1379 ubus_ctx
->connection_lost
= netifd_ubus_connection_lost
;
1380 netifd_ubus_add_fd();
1382 netifd_add_object(&main_object
);
1383 netifd_add_object(&dev_object
);
1384 netifd_add_object(&wireless_object
);
1385 netifd_add_iface_object();
1391 netifd_ubus_done(void)
1393 ubus_free(ubus_ctx
);
1397 netifd_ubus_interface_event(struct interface
*iface
, bool up
)
1399 blob_buf_init(&b
, 0);
1400 blobmsg_add_string(&b
, "action", up
? "ifup" : "ifdown");
1401 blobmsg_add_string(&b
, "interface", iface
->name
);
1402 ubus_send_event(ubus_ctx
, "network.interface", b
.head
);
1406 netifd_ubus_interface_notify(struct interface
*iface
, bool up
)
1408 const char *event
= (up
) ? "interface.update" : "interface.down";
1409 blob_buf_init(&b
, 0);
1410 blobmsg_add_string(&b
, "interface", iface
->name
);
1411 netifd_dump_status(iface
);
1412 ubus_notify(ubus_ctx
, &iface_object
, event
, b
.head
, -1);
1413 ubus_notify(ubus_ctx
, &iface
->ubus
, event
, b
.head
, -1);
1417 netifd_ubus_add_interface(struct interface
*iface
)
1419 struct ubus_object
*obj
= &iface
->ubus
;
1422 if (asprintf(&name
, "%s.interface.%s", main_object
.name
, iface
->name
) == -1)
1426 obj
->type
= &iface_object_type
;
1427 obj
->methods
= iface_object_methods
;
1428 obj
->n_methods
= ARRAY_SIZE(iface_object_methods
);
1429 if (ubus_add_object(ubus_ctx
, &iface
->ubus
)) {
1430 DPRINTF("failed to publish ubus object for interface '%s'\n", iface
->name
);
1437 netifd_ubus_remove_interface(struct interface
*iface
)
1439 if (!iface
->ubus
.name
)
1442 ubus_remove_object(ubus_ctx
, &iface
->ubus
);
1443 free((void *) iface
->ubus
.name
);