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>
22 #include "interface.h"
28 struct ubus_context
*ubus_ctx
= NULL
;
29 static struct blob_buf b
;
30 static const char *ubus_path
;
31 static struct udebug_ubus udebug
;
36 netifd_handle_restart(struct ubus_context
*ctx
, struct ubus_object
*obj
,
37 struct ubus_request_data
*req
, const char *method
,
38 struct blob_attr
*msg
)
45 netifd_handle_reload(struct ubus_context
*ctx
, struct ubus_object
*obj
,
46 struct ubus_request_data
*req
, const char *method
,
47 struct blob_attr
*msg
)
50 return UBUS_STATUS_NOT_FOUND
;
52 return UBUS_STATUS_OK
;
63 static const struct blobmsg_policy route_policy
[__HR_MAX
] = {
64 [HR_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
65 [HR_V6
] = { .name
= "v6", .type
= BLOBMSG_TYPE_BOOL
},
66 [HR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
67 [HR_EXCLUDE
] = { .name
= "exclude", .type
= BLOBMSG_TYPE_BOOL
},
71 netifd_add_host_route(struct ubus_context
*ctx
, struct ubus_object
*obj
,
72 struct ubus_request_data
*req
, const char *method
,
73 struct blob_attr
*msg
)
75 struct blob_attr
*tb
[__HR_MAX
];
76 struct interface
*iface
= NULL
;
81 blobmsg_parse(route_policy
, __HR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
83 return UBUS_STATUS_INVALID_ARGUMENT
;
86 v6
= blobmsg_get_bool(tb
[HR_V6
]);
89 exclude
= blobmsg_get_bool(tb
[HR_EXCLUDE
]);
92 iface
= vlist_find(&interfaces
, blobmsg_data(tb
[HR_INTERFACE
]), iface
, node
);
94 memset(&a
, 0, sizeof(a
));
95 if (!inet_pton(v6
? AF_INET6
: AF_INET
, blobmsg_data(tb
[HR_TARGET
]), &a
))
96 return UBUS_STATUS_INVALID_ARGUMENT
;
98 iface
= interface_ip_add_target_route(&a
, v6
, iface
, exclude
);
100 return UBUS_STATUS_NOT_FOUND
;
102 blob_buf_init(&b
, 0);
103 blobmsg_add_string(&b
, "interface", iface
->name
);
104 ubus_send_reply(ctx
, req
, b
.head
);
110 netifd_get_proto_handlers(struct ubus_context
*ctx
, struct ubus_object
*obj
,
111 struct ubus_request_data
*req
, const char *method
,
112 struct blob_attr
*msg
)
114 blob_buf_init(&b
, 0);
115 proto_dump_handlers(&b
);
116 ubus_send_reply(ctx
, req
, b
.head
);
127 static const struct blobmsg_policy dynamic_policy
[__DI_MAX
] = {
128 [DI_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
132 netifd_add_dynamic(struct ubus_context
*ctx
, struct ubus_object
*obj
,
133 struct ubus_request_data
*req
, const char *method
,
134 struct blob_attr
*msg
)
136 struct blob_attr
*tb
[__DI_MAX
];
137 struct interface
*iface
;
138 struct blob_attr
*config
;
140 blobmsg_parse(dynamic_policy
, __DI_MAX
, tb
, blob_data(msg
), blob_len(msg
));
143 return UBUS_STATUS_INVALID_ARGUMENT
;
145 const char *name
= blobmsg_get_string(tb
[DI_NAME
]);
147 iface
= interface_alloc(name
, msg
, true);
149 return UBUS_STATUS_UNKNOWN_ERROR
;
151 config
= blob_memdup(msg
);
155 if (!interface_add(iface
, config
))
156 goto error_free_config
;
158 return UBUS_STATUS_OK
;
164 return UBUS_STATUS_UNKNOWN_ERROR
;
173 static const struct blobmsg_policy netns_updown_policy
[__NETNS_UPDOWN_MAX
] = {
174 [NETNS_UPDOWN_JAIL
] = { .name
= "jail", .type
= BLOBMSG_TYPE_STRING
},
175 [NETNS_UPDOWN_START
] = { .name
= "start", .type
= BLOBMSG_TYPE_BOOL
},
179 netifd_netns_updown(struct ubus_context
*ctx
, struct ubus_object
*obj
,
180 struct ubus_request_data
*req
, const char *method
,
181 struct blob_attr
*msg
)
183 int target_netns_fd
= ubus_request_get_caller_fd(req
);
184 struct blob_attr
*tb
[__NETNS_UPDOWN_MAX
];
187 if (target_netns_fd
< 0)
188 return UBUS_STATUS_INVALID_ARGUMENT
;
190 blobmsg_parse(netns_updown_policy
, __NETNS_UPDOWN_MAX
, tb
, blob_data(msg
), blob_len(msg
));
192 start
= tb
[NETNS_UPDOWN_START
] && blobmsg_get_bool(tb
[NETNS_UPDOWN_START
]);
195 if (!tb
[NETNS_UPDOWN_JAIL
])
196 return UBUS_STATUS_INVALID_ARGUMENT
;
198 interface_start_jail(target_netns_fd
, blobmsg_get_string(tb
[NETNS_UPDOWN_JAIL
]));
200 interface_stop_jail(target_netns_fd
);
202 close(target_netns_fd
);
204 return UBUS_STATUS_OK
;
207 static struct ubus_method main_object_methods
[] = {
208 { .name
= "restart", .handler
= netifd_handle_restart
},
209 { .name
= "reload", .handler
= netifd_handle_reload
},
210 UBUS_METHOD("add_host_route", netifd_add_host_route
, route_policy
),
211 { .name
= "get_proto_handlers", .handler
= netifd_get_proto_handlers
},
212 UBUS_METHOD("add_dynamic", netifd_add_dynamic
, dynamic_policy
),
213 UBUS_METHOD("netns_updown", netifd_netns_updown
, netns_updown_policy
),
216 static struct ubus_object_type main_object_type
=
217 UBUS_OBJECT_TYPE("netifd", main_object_methods
);
219 static struct ubus_object main_object
= {
221 .type
= &main_object_type
,
222 .methods
= main_object_methods
,
223 .n_methods
= ARRAY_SIZE(main_object_methods
),
231 static const struct blobmsg_policy dev_policy
[__DEV_MAX
] = {
232 [DEV_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
236 netifd_dev_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
237 struct ubus_request_data
*req
, const char *method
,
238 struct blob_attr
*msg
)
240 struct device
*dev
= NULL
;
241 struct blob_attr
*tb
[__DEV_MAX
];
243 blobmsg_parse(dev_policy
, __DEV_MAX
, tb
, blob_data(msg
), blob_len(msg
));
246 dev
= device_find(blobmsg_data(tb
[DEV_NAME
]));
248 return UBUS_STATUS_INVALID_ARGUMENT
;
251 blob_buf_init(&b
, 0);
252 device_dump_status(&b
, dev
);
253 ubus_send_reply(ctx
, req
, b
.head
);
264 static const struct blobmsg_policy alias_attrs
[__ALIAS_ATTR_MAX
] = {
265 [ALIAS_ATTR_ALIAS
] = { "alias", BLOBMSG_TYPE_ARRAY
},
266 [ALIAS_ATTR_DEV
] = { "device", BLOBMSG_TYPE_STRING
},
270 netifd_handle_alias(struct ubus_context
*ctx
, struct ubus_object
*obj
,
271 struct ubus_request_data
*req
, const char *method
,
272 struct blob_attr
*msg
)
274 struct device
*dev
= NULL
;
275 struct blob_attr
*tb
[__ALIAS_ATTR_MAX
];
276 struct blob_attr
*cur
;
279 blobmsg_parse(alias_attrs
, __ALIAS_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
281 if (!tb
[ALIAS_ATTR_ALIAS
])
282 return UBUS_STATUS_INVALID_ARGUMENT
;
284 if ((cur
= tb
[ALIAS_ATTR_DEV
]) != NULL
) {
285 dev
= device_get(blobmsg_data(cur
), true);
287 return UBUS_STATUS_NOT_FOUND
;
290 blobmsg_for_each_attr(cur
, tb
[ALIAS_ATTR_ALIAS
], rem
) {
291 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
294 if (!blobmsg_check_attr(cur
, false))
297 alias_notify_device(blobmsg_data(cur
), dev
);
302 device_free_unused();
303 return UBUS_STATUS_INVALID_ARGUMENT
;
309 DEV_STATE_AUTH_STATUS
,
310 DEV_STATE_AUTH_VLANS
,
314 static const struct blobmsg_policy dev_state_policy
[__DEV_STATE_MAX
] = {
315 [DEV_STATE_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
316 [DEV_STATE_DEFER
] = { .name
= "defer", .type
= BLOBMSG_TYPE_BOOL
},
317 [DEV_STATE_AUTH_STATUS
] = { .name
= "auth_status", .type
= BLOBMSG_TYPE_BOOL
},
318 [DEV_STATE_AUTH_VLANS
] = { .name
= "auth_vlans", BLOBMSG_TYPE_ARRAY
},
322 netifd_handle_set_state(struct ubus_context
*ctx
, struct ubus_object
*obj
,
323 struct ubus_request_data
*req
, const char *method
,
324 struct blob_attr
*msg
)
326 struct device
*dev
= NULL
;
327 struct blob_attr
*tb
[__DEV_STATE_MAX
];
328 struct blob_attr
*cur
;
331 blobmsg_parse(dev_state_policy
, __DEV_STATE_MAX
, tb
, blob_data(msg
), blob_len(msg
));
333 cur
= tb
[DEV_STATE_NAME
];
335 return UBUS_STATUS_INVALID_ARGUMENT
;
337 dev
= device_find(blobmsg_data(cur
));
339 return UBUS_STATUS_NOT_FOUND
;
341 cur
= tb
[DEV_STATE_DEFER
];
343 device_set_deferred(dev
, !!blobmsg_get_u8(cur
));
345 if ((cur
= tb
[DEV_STATE_AUTH_STATUS
]) != NULL
)
346 auth_status
= blobmsg_get_bool(cur
);
348 auth_status
= dev
->auth_status
;
349 if (tb
[DEV_STATE_AUTH_STATUS
] || tb
[DEV_STATE_AUTH_VLANS
])
350 device_set_auth_status(dev
, auth_status
, tb
[DEV_STATE_AUTH_VLANS
]);
357 DEV_HOTPLUG_ATTR_NAME
,
358 DEV_HOTPLUG_ATTR_ADD
,
359 __DEV_HOTPLUG_ATTR_MAX
,
362 static const struct blobmsg_policy dev_hotplug_policy
[__DEV_HOTPLUG_ATTR_MAX
] = {
363 [DEV_HOTPLUG_ATTR_NAME
] = { "name", BLOBMSG_TYPE_STRING
},
364 [DEV_HOTPLUG_ATTR_ADD
] = { "add", BLOBMSG_TYPE_BOOL
},
368 netifd_handle_dev_hotplug(struct ubus_context
*ctx
, struct ubus_object
*obj
,
369 struct ubus_request_data
*req
, const char *method
,
370 struct blob_attr
*msg
)
372 struct blob_attr
*tb
[__DEV_HOTPLUG_ATTR_MAX
];
375 blobmsg_parse(dev_hotplug_policy
, __DEV_HOTPLUG_ATTR_MAX
, tb
,
376 blob_data(msg
), blob_len(msg
));
378 if (!tb
[DEV_HOTPLUG_ATTR_NAME
] || !tb
[DEV_HOTPLUG_ATTR_ADD
])
379 return UBUS_STATUS_INVALID_ARGUMENT
;
381 name
= blobmsg_get_string(tb
[DEV_HOTPLUG_ATTR_NAME
]);
382 device_hotplug_event(name
, blobmsg_get_bool(tb
[DEV_HOTPLUG_ATTR_ADD
]));
389 netifd_handle_stp_init(struct ubus_context
*ctx
, struct ubus_object
*obj
,
390 struct ubus_request_data
*req
, const char *method
,
391 struct blob_attr
*msg
)
398 static struct ubus_method dev_object_methods
[] = {
399 UBUS_METHOD("status", netifd_dev_status
, dev_policy
),
400 UBUS_METHOD("set_alias", netifd_handle_alias
, alias_attrs
),
401 UBUS_METHOD("set_state", netifd_handle_set_state
, dev_state_policy
),
403 UBUS_METHOD("hotplug_event", netifd_handle_dev_hotplug
, dev_hotplug_policy
),
405 UBUS_METHOD_NOARG("stp_init", netifd_handle_stp_init
)
408 static struct ubus_object_type dev_object_type
=
409 UBUS_OBJECT_TYPE("device", dev_object_methods
);
411 static struct ubus_object dev_object
= {
412 .name
= "network.device",
413 .type
= &dev_object_type
,
414 .methods
= dev_object_methods
,
415 .n_methods
= ARRAY_SIZE(dev_object_methods
),
419 netifd_ubus_add_fd(void)
421 ubus_add_uloop(ubus_ctx
);
422 system_fd_set_cloexec(ubus_ctx
->sock
.fd
);
425 void netifd_ubus_device_notify(const char *event
, struct blob_attr
*data
, int timeout
)
427 ubus_notify(ubus_ctx
, &dev_object
, event
, data
, timeout
);
431 netifd_ubus_reconnect_timer(struct uloop_timeout
*timeout
)
433 static struct uloop_timeout retry
= {
434 .cb
= netifd_ubus_reconnect_timer
,
438 if (ubus_reconnect(ubus_ctx
, ubus_path
) != 0) {
439 D(SYSTEM
, "failed to reconnect, trying again in %d seconds\n", t
);
440 uloop_timeout_set(&retry
, t
* 1000);
444 D(SYSTEM
, "reconnected to ubus, new id: %08x\n", ubus_ctx
->local_id
);
445 netifd_ubus_add_fd();
449 netifd_ubus_connection_lost(struct ubus_context
*ctx
)
451 netifd_ubus_reconnect_timer(NULL
);
454 /* per-interface object */
457 netifd_handle_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
458 struct ubus_request_data
*req
, const char *method
,
459 struct blob_attr
*msg
)
461 struct interface
*iface
;
463 iface
= container_of(obj
, struct interface
, ubus
);
464 interface_set_up(iface
);
470 netifd_handle_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
471 struct ubus_request_data
*req
, const char *method
,
472 struct blob_attr
*msg
)
474 struct interface
*iface
;
476 iface
= container_of(obj
, struct interface
, ubus
);
477 interface_set_down(iface
);
483 netifd_handle_renew(struct ubus_context
*ctx
, struct ubus_object
*obj
,
484 struct ubus_request_data
*req
, const char *method
,
485 struct blob_attr
*msg
)
487 struct interface
*iface
;
489 iface
= container_of(obj
, struct interface
, ubus
);
490 interface_renew(iface
);
496 netifd_add_interface_errors(struct blob_buf
*b
, struct interface
*iface
)
498 struct interface_error
*error
;
502 e
= blobmsg_open_array(b
, "errors");
503 list_for_each_entry(error
, &iface
->errors
, list
) {
504 e2
= blobmsg_open_table(b
, NULL
);
506 blobmsg_add_string(b
, "subsystem", error
->subsystem
);
507 blobmsg_add_string(b
, "code", error
->code
);
508 if (error
->data
[0]) {
509 e3
= blobmsg_open_array(b
, "data");
510 for (i
= 0; error
->data
[i
]; i
++)
511 blobmsg_add_string(b
, NULL
, error
->data
[i
]);
512 blobmsg_close_array(b
, e3
);
515 blobmsg_close_table(b
, e2
);
517 blobmsg_close_array(b
, e
);
521 interface_ip_dump_address_list(struct interface_ip_settings
*ip
, bool v6
, bool enabled
)
523 struct device_addr
*addr
;
529 time_t now
= system_get_rtime();
530 vlist_for_each_element(&ip
->addr
, addr
, node
) {
531 if (addr
->enabled
!= enabled
)
534 if ((addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
539 if (af
!= (v6
? AF_INET6
: AF_INET
))
542 a
= blobmsg_open_table(&b
, NULL
);
544 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
545 inet_ntop(af
, &addr
->addr
, buf
, buflen
);
546 blobmsg_add_string_buffer(&b
);
548 blobmsg_add_u32(&b
, "mask", addr
->mask
);
550 if (addr
->point_to_point
) {
551 buf
= blobmsg_alloc_string_buffer(&b
, "ptpaddress", buflen
);
552 inet_ntop(af
, &addr
->point_to_point
, buf
, buflen
);
553 blobmsg_add_string_buffer(&b
);
556 if (addr
->preferred_until
) {
557 int preferred
= addr
->preferred_until
- now
;
560 blobmsg_add_u32(&b
, "preferred", preferred
);
563 if (addr
->valid_until
)
564 blobmsg_add_u32(&b
, "valid", addr
->valid_until
- now
);
567 blobmsg_add_string(&b
, "class", addr
->pclass
);
569 blobmsg_close_table(&b
, a
);
574 interface_ip_dump_neighbor_list(struct interface_ip_settings
*ip
, bool enabled
)
576 struct device_neighbor
*neighbor
;
582 vlist_for_each_element(&ip
->neighbor
, neighbor
, node
) {
583 if (neighbor
->enabled
!= enabled
)
586 if ((neighbor
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
591 r
= blobmsg_open_table(&b
, NULL
);
593 if (neighbor
->flags
& DEVNEIGH_MAC
)
594 blobmsg_add_string(&b
, "mac", format_macaddr(neighbor
->macaddr
));
596 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
597 inet_ntop(af
, &neighbor
->addr
, buf
, buflen
);
598 blobmsg_add_string_buffer(&b
);
601 blobmsg_add_u32(&b
, "proxy", neighbor
->proxy
);
603 if (neighbor
->router
)
604 blobmsg_add_u32(&b
, "router", neighbor
->router
);
606 blobmsg_close_table(&b
, r
);
611 interface_ip_dump_route_list(struct interface_ip_settings
*ip
, bool enabled
)
613 struct device_route
*route
;
619 time_t now
= system_get_rtime();
620 vlist_for_each_element(&ip
->route
, route
, node
) {
621 if (route
->enabled
!= enabled
)
624 if ((ip
->no_defaultroute
== enabled
) && !route
->mask
)
627 if ((route
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
632 r
= blobmsg_open_table(&b
, NULL
);
634 buf
= blobmsg_alloc_string_buffer(&b
, "target", buflen
);
635 inet_ntop(af
, &route
->addr
, buf
, buflen
);
636 blobmsg_add_string_buffer(&b
);
638 blobmsg_add_u32(&b
, "mask", route
->mask
);
640 buf
= blobmsg_alloc_string_buffer(&b
, "nexthop", buflen
);
641 inet_ntop(af
, &route
->nexthop
, buf
, buflen
);
642 blobmsg_add_string_buffer(&b
);
644 if (route
->flags
& DEVROUTE_TYPE
)
645 blobmsg_add_u32(&b
, "type", route
->type
);
647 if (route
->flags
& DEVROUTE_PROTO
)
648 blobmsg_add_u32(&b
, "proto", route
->proto
);
650 if (route
->flags
& DEVROUTE_MTU
)
651 blobmsg_add_u32(&b
, "mtu", route
->mtu
);
653 if (route
->flags
& DEVROUTE_METRIC
)
654 blobmsg_add_u32(&b
, "metric", route
->metric
);
656 if (route
->flags
& DEVROUTE_TABLE
)
657 blobmsg_add_u32(&b
, "table", route
->table
);
659 if (route
->valid_until
)
660 blobmsg_add_u32(&b
, "valid", route
->valid_until
- now
);
662 buf
= blobmsg_alloc_string_buffer(&b
, "source", buflen
);
663 inet_ntop(af
, &route
->source
, buf
, buflen
);
664 snprintf(buf
+ strlen(buf
), buflen
- strlen(buf
), "/%u", route
->sourcemask
);
665 blobmsg_add_string_buffer(&b
);
667 blobmsg_close_table(&b
, r
);
673 interface_ip_dump_prefix_list(struct interface_ip_settings
*ip
)
675 struct device_prefix
*prefix
;
678 const int buflen
= INET6_ADDRSTRLEN
;
680 time_t now
= system_get_rtime();
681 vlist_for_each_element(&ip
->prefix
, prefix
, node
) {
682 a
= blobmsg_open_table(&b
, NULL
);
684 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
685 inet_ntop(AF_INET6
, &prefix
->addr
, buf
, buflen
);
686 blobmsg_add_string_buffer(&b
);
688 blobmsg_add_u32(&b
, "mask", prefix
->length
);
690 if (prefix
->preferred_until
) {
691 int preferred
= prefix
->preferred_until
- now
;
694 blobmsg_add_u32(&b
, "preferred", preferred
);
697 if (prefix
->valid_until
)
698 blobmsg_add_u32(&b
, "valid", prefix
->valid_until
- now
);
700 blobmsg_add_string(&b
, "class", prefix
->pclass
);
702 c
= blobmsg_open_table(&b
, "assigned");
703 struct device_prefix_assignment
*assign
;
704 list_for_each_entry(assign
, &prefix
->assignments
, head
) {
705 if (!assign
->name
[0])
708 struct in6_addr addr
= prefix
->addr
;
709 addr
.s6_addr32
[1] |= htonl(assign
->assigned
);
711 void *d
= blobmsg_open_table(&b
, assign
->name
);
713 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
714 inet_ntop(AF_INET6
, &addr
, buf
, buflen
);
715 blobmsg_add_string_buffer(&b
);
717 blobmsg_add_u32(&b
, "mask", assign
->length
);
719 blobmsg_close_table(&b
, d
);
721 blobmsg_close_table(&b
, c
);
723 blobmsg_close_table(&b
, a
);
729 interface_ip_dump_prefix_assignment_list(struct interface
*iface
)
733 const int buflen
= INET6_ADDRSTRLEN
;
734 time_t now
= system_get_rtime();
736 struct device_prefix
*prefix
;
737 list_for_each_entry(prefix
, &prefixes
, head
) {
738 struct device_prefix_assignment
*assign
;
739 list_for_each_entry(assign
, &prefix
->assignments
, head
) {
740 if (strcmp(assign
->name
, iface
->name
))
743 struct in6_addr addr
= prefix
->addr
;
744 addr
.s6_addr32
[1] |= htonl(assign
->assigned
);
746 a
= blobmsg_open_table(&b
, NULL
);
748 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
749 inet_ntop(AF_INET6
, &addr
, buf
, buflen
);
750 blobmsg_add_string_buffer(&b
);
752 blobmsg_add_u32(&b
, "mask", assign
->length
);
754 if (prefix
->preferred_until
) {
755 int preferred
= prefix
->preferred_until
- now
;
758 blobmsg_add_u32(&b
, "preferred", preferred
);
761 if (prefix
->valid_until
)
762 blobmsg_add_u32(&b
, "valid", prefix
->valid_until
- now
);
764 void *c
= blobmsg_open_table(&b
, "local-address");
765 if (assign
->enabled
) {
766 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
767 inet_ntop(AF_INET6
, &assign
->addr
, buf
, buflen
);
768 blobmsg_add_string_buffer(&b
);
770 blobmsg_add_u32(&b
, "mask", assign
->length
);
772 blobmsg_close_table(&b
, c
);
774 blobmsg_close_table(&b
, a
);
780 interface_ip_dump_dns_server_list(struct interface_ip_settings
*ip
, bool enabled
)
782 struct dns_server
*dns
;
786 vlist_simple_for_each_element(&ip
->dns_servers
, dns
, node
) {
787 if (ip
->no_dns
== enabled
)
790 buf
= blobmsg_alloc_string_buffer(&b
, NULL
, buflen
);
791 inet_ntop(dns
->af
, &dns
->addr
, buf
, buflen
);
792 blobmsg_add_string_buffer(&b
);
797 interface_ip_dump_dns_search_list(struct interface_ip_settings
*ip
, bool enabled
)
799 struct dns_search_domain
*dns
;
801 vlist_simple_for_each_element(&ip
->dns_search
, dns
, node
) {
802 if (ip
->no_dns
== enabled
)
805 blobmsg_add_string(&b
, NULL
, dns
->name
);
810 netifd_dump_status(struct interface
*iface
)
812 struct interface_data
*data
;
816 blobmsg_add_u8(&b
, "up", iface
->state
== IFS_UP
);
817 blobmsg_add_u8(&b
, "pending", iface
->state
== IFS_SETUP
);
818 blobmsg_add_u8(&b
, "available", iface
->available
);
819 blobmsg_add_u8(&b
, "autostart", iface
->autostart
);
820 blobmsg_add_u8(&b
, "dynamic", iface
->dynamic
);
822 if (iface
->state
== IFS_UP
) {
823 time_t cur
= system_get_rtime();
824 blobmsg_add_u32(&b
, "uptime", cur
- iface
->start_time
);
825 if (iface
->l3_dev
.dev
)
826 blobmsg_add_string(&b
, "l3_device", iface
->l3_dev
.dev
->ifname
);
829 if (iface
->proto_handler
)
830 blobmsg_add_string(&b
, "proto", iface
->proto_handler
->name
);
832 dev
= iface
->main_dev
.dev
;
833 if (dev
&& !dev
->hidden
&& iface
->proto_handler
&&
834 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
))
835 blobmsg_add_string(&b
, "device", dev
->ifname
);
838 blobmsg_add_string(&b
, "jail", iface
->jail
);
840 if (iface
->jail_device
)
841 blobmsg_add_string(&b
, "jail_device", iface
->jail_device
);
843 if (iface
->state
== IFS_UP
) {
844 if (iface
->updated
) {
845 a
= blobmsg_open_array(&b
, "updated");
847 if (iface
->updated
& IUF_ADDRESS
)
848 blobmsg_add_string(&b
, NULL
, "addresses");
849 if (iface
->updated
& IUF_ROUTE
)
850 blobmsg_add_string(&b
, NULL
, "routes");
851 if (iface
->updated
& IUF_PREFIX
)
852 blobmsg_add_string(&b
, NULL
, "prefixes");
853 if (iface
->updated
& IUF_DATA
)
854 blobmsg_add_string(&b
, NULL
, "data");
856 blobmsg_close_array(&b
, a
);
860 blobmsg_add_u32(&b
, "ip4table", iface
->ip4table
);
862 blobmsg_add_u32(&b
, "ip6table", iface
->ip6table
);
863 blobmsg_add_u32(&b
, "metric", iface
->metric
);
864 blobmsg_add_u32(&b
, "dns_metric", iface
->dns_metric
);
865 blobmsg_add_u8(&b
, "delegation", !iface
->proto_ip
.no_delegation
);
866 if (iface
->assignment_weight
)
867 blobmsg_add_u32(&b
, "ip6weight", iface
->assignment_weight
);
868 a
= blobmsg_open_array(&b
, "ipv4-address");
869 interface_ip_dump_address_list(&iface
->config_ip
, false, true);
870 interface_ip_dump_address_list(&iface
->proto_ip
, false, true);
871 blobmsg_close_array(&b
, a
);
872 a
= blobmsg_open_array(&b
, "ipv6-address");
873 interface_ip_dump_address_list(&iface
->config_ip
, true, true);
874 interface_ip_dump_address_list(&iface
->proto_ip
, true, true);
875 blobmsg_close_array(&b
, a
);
876 a
= blobmsg_open_array(&b
, "ipv6-prefix");
877 interface_ip_dump_prefix_list(&iface
->config_ip
);
878 interface_ip_dump_prefix_list(&iface
->proto_ip
);
879 blobmsg_close_array(&b
, a
);
880 a
= blobmsg_open_array(&b
, "ipv6-prefix-assignment");
881 interface_ip_dump_prefix_assignment_list(iface
);
882 blobmsg_close_array(&b
, a
);
883 a
= blobmsg_open_array(&b
, "route");
884 interface_ip_dump_route_list(&iface
->config_ip
, true);
885 interface_ip_dump_route_list(&iface
->proto_ip
, true);
886 blobmsg_close_array(&b
, a
);
887 a
= blobmsg_open_array(&b
, "dns-server");
888 interface_ip_dump_dns_server_list(&iface
->config_ip
, true);
889 interface_ip_dump_dns_server_list(&iface
->proto_ip
, true);
890 blobmsg_close_array(&b
, a
);
891 a
= blobmsg_open_array(&b
, "dns-search");
892 interface_ip_dump_dns_search_list(&iface
->config_ip
, true);
893 interface_ip_dump_dns_search_list(&iface
->proto_ip
, true);
894 blobmsg_close_array(&b
, a
);
895 a
= blobmsg_open_array(&b
, "neighbors");
896 interface_ip_dump_neighbor_list(&iface
->config_ip
, true);
897 interface_ip_dump_neighbor_list(&iface
->proto_ip
, true);
898 blobmsg_close_array(&b
, a
);
900 inactive
= blobmsg_open_table(&b
, "inactive");
901 a
= blobmsg_open_array(&b
, "ipv4-address");
902 interface_ip_dump_address_list(&iface
->config_ip
, false, false);
903 interface_ip_dump_address_list(&iface
->proto_ip
, false, false);
904 blobmsg_close_array(&b
, a
);
905 a
= blobmsg_open_array(&b
, "ipv6-address");
906 interface_ip_dump_address_list(&iface
->config_ip
, true, false);
907 interface_ip_dump_address_list(&iface
->proto_ip
, true, false);
908 blobmsg_close_array(&b
, a
);
909 a
= blobmsg_open_array(&b
, "route");
910 interface_ip_dump_route_list(&iface
->config_ip
, false);
911 interface_ip_dump_route_list(&iface
->proto_ip
, false);
912 blobmsg_close_array(&b
, a
);
913 a
= blobmsg_open_array(&b
, "dns-server");
914 interface_ip_dump_dns_server_list(&iface
->config_ip
, false);
915 interface_ip_dump_dns_server_list(&iface
->proto_ip
, false);
916 blobmsg_close_array(&b
, a
);
917 a
= blobmsg_open_array(&b
, "dns-search");
918 interface_ip_dump_dns_search_list(&iface
->config_ip
, false);
919 interface_ip_dump_dns_search_list(&iface
->proto_ip
, false);
920 blobmsg_close_array(&b
, a
);
921 a
= blobmsg_open_array(&b
, "neighbors");
922 interface_ip_dump_neighbor_list(&iface
->config_ip
, false);
923 interface_ip_dump_neighbor_list(&iface
->proto_ip
, false);
924 blobmsg_close_array(&b
, a
);
925 blobmsg_close_table(&b
, inactive
);
928 a
= blobmsg_open_table(&b
, "data");
931 blobmsg_add_string(&b
, "zone", iface
->zone
);
932 avl_for_each_element(&iface
->data
, data
, node
)
933 blobmsg_add_blob(&b
, data
->data
);
935 blobmsg_close_table(&b
, a
);
937 if (!list_empty(&iface
->errors
))
938 netifd_add_interface_errors(&b
, iface
);
942 netifd_handle_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
943 struct ubus_request_data
*req
, const char *method
,
944 struct blob_attr
*msg
)
946 struct interface
*iface
= container_of(obj
, struct interface
, ubus
);
948 blob_buf_init(&b
, 0);
949 netifd_dump_status(iface
);
950 ubus_send_reply(ctx
, req
, b
.head
);
957 netifd_handle_dump(struct ubus_context
*ctx
, struct ubus_object
*obj
,
958 struct ubus_request_data
*req
, const char *method
,
959 struct blob_attr
*msg
)
961 blob_buf_init(&b
, 0);
962 void *a
= blobmsg_open_array(&b
, "interface");
964 struct interface
*iface
;
965 vlist_for_each_element(&interfaces
, iface
, node
) {
966 void *i
= blobmsg_open_table(&b
, NULL
);
967 blobmsg_add_string(&b
, "interface", iface
->name
);
968 netifd_dump_status(iface
);
969 blobmsg_close_table(&b
, i
);
972 blobmsg_close_array(&b
, a
);
973 ubus_send_reply(ctx
, req
, b
.head
);
985 static const struct blobmsg_policy dev_link_policy
[__DEV_LINK_MAX
] = {
986 [DEV_LINK_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
987 [DEV_LINK_EXT
] = { .name
= "link-ext", .type
= BLOBMSG_TYPE_BOOL
},
988 [DEV_LINK_VLAN
] = { .name
= "vlan", .type
= BLOBMSG_TYPE_ARRAY
},
992 netifd_iface_handle_device(struct ubus_context
*ctx
, struct ubus_object
*obj
,
993 struct ubus_request_data
*req
, const char *method
,
994 struct blob_attr
*msg
)
996 struct blob_attr
*tb
[__DEV_LINK_MAX
];
997 struct blob_attr
*cur
;
998 struct interface
*iface
;
999 bool add
= !strncmp(method
, "add", 3);
1000 bool link_ext
= true;
1002 iface
= container_of(obj
, struct interface
, ubus
);
1004 blobmsg_parse(dev_link_policy
, __DEV_LINK_MAX
, tb
, blob_data(msg
), blob_len(msg
));
1006 if (!tb
[DEV_LINK_NAME
])
1007 return UBUS_STATUS_INVALID_ARGUMENT
;
1009 cur
= tb
[DEV_LINK_EXT
];
1011 link_ext
= blobmsg_get_bool(cur
);
1013 return interface_handle_link(iface
, blobmsg_data(tb
[DEV_LINK_NAME
]),
1014 tb
[DEV_LINK_VLAN
], add
, link_ext
);
1019 netifd_iface_notify_proto(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 if (!iface
->proto
|| !iface
->proto
->notify
)
1028 return UBUS_STATUS_NOT_SUPPORTED
;
1030 return iface
->proto
->notify(iface
->proto
, msg
);
1034 netifd_iface_do_remove(struct uloop_timeout
*timeout
)
1036 struct interface
*iface
;
1038 iface
= container_of(timeout
, struct interface
, remove_timer
);
1039 vlist_delete(&interfaces
, &iface
->node
);
1043 netifd_iface_remove(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1044 struct ubus_request_data
*req
, const char *method
,
1045 struct blob_attr
*msg
)
1047 struct interface
*iface
;
1049 iface
= container_of(obj
, struct interface
, ubus
);
1050 if (iface
->remove_timer
.cb
)
1051 return UBUS_STATUS_INVALID_ARGUMENT
;
1053 iface
->remove_timer
.cb
= netifd_iface_do_remove
;
1054 uloop_timeout_set(&iface
->remove_timer
, 100);
1059 netifd_handle_iface_prepare(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1060 struct ubus_request_data
*req
, const char *method
,
1061 struct blob_attr
*msg
)
1063 struct interface
*iface
;
1064 struct device
*dev
, *bridge_dev
= NULL
;
1065 const struct device_hotplug_ops
*ops
;
1067 iface
= container_of(obj
, struct interface
, ubus
);
1068 dev
= iface
->main_dev
.dev
;
1072 ops
= dev
->hotplug_ops
;
1076 ops
->prepare(dev
, &bridge_dev
);
1079 blob_buf_init(&b
, 0);
1081 blobmsg_add_string(&b
, "bridge", bridge_dev
->ifname
);
1082 ubus_send_reply(ctx
, req
, b
.head
);
1088 netifd_handle_set_data(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1089 struct ubus_request_data
*req
, const char *method
,
1090 struct blob_attr
*msg
)
1092 struct interface
*iface
;
1094 iface
= container_of(obj
, struct interface
, ubus
);
1096 return interface_parse_data(iface
, msg
);
1099 static struct ubus_method iface_object_methods
[] = {
1100 { .name
= "up", .handler
= netifd_handle_up
},
1101 { .name
= "down", .handler
= netifd_handle_down
},
1102 { .name
= "renew", .handler
= netifd_handle_renew
},
1103 { .name
= "status", .handler
= netifd_handle_status
},
1104 { .name
= "prepare", .handler
= netifd_handle_iface_prepare
},
1105 { .name
= "dump", .handler
= netifd_handle_dump
},
1106 UBUS_METHOD("add_device", netifd_iface_handle_device
, dev_link_policy
),
1107 UBUS_METHOD("remove_device", netifd_iface_handle_device
, dev_link_policy
),
1108 { .name
= "notify_proto", .handler
= netifd_iface_notify_proto
},
1109 { .name
= "remove", .handler
= netifd_iface_remove
},
1110 { .name
= "set_data", .handler
= netifd_handle_set_data
},
1113 static struct ubus_object_type iface_object_type
=
1114 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods
);
1117 static struct ubus_object iface_object
= {
1118 .name
= "network.interface",
1119 .type
= &iface_object_type
,
1120 .n_methods
= ARRAY_SIZE(iface_object_methods
),
1123 static void netifd_add_object(struct ubus_object
*obj
)
1125 int ret
= ubus_add_object(ubus_ctx
, obj
);
1128 fprintf(stderr
, "Failed to publish object '%s': %s\n", obj
->name
, ubus_strerror(ret
));
1131 static const struct blobmsg_policy iface_policy
= {
1132 .name
= "interface",
1133 .type
= BLOBMSG_TYPE_STRING
,
1137 netifd_handle_iface(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1138 struct ubus_request_data
*req
, const char *method
,
1139 struct blob_attr
*msg
)
1141 struct interface
*iface
;
1142 struct blob_attr
*tb
;
1145 blobmsg_parse(&iface_policy
, 1, &tb
, blob_data(msg
), blob_len(msg
));
1147 return UBUS_STATUS_INVALID_ARGUMENT
;
1149 iface
= vlist_find(&interfaces
, blobmsg_data(tb
), iface
, node
);
1151 return UBUS_STATUS_NOT_FOUND
;
1153 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
1156 if (strcmp(method
, iface_object_methods
[i
].name
) != 0)
1159 cb
= iface_object_methods
[i
].handler
;
1160 return cb(ctx
, &iface
->ubus
, req
, method
, msg
);
1163 return UBUS_STATUS_INVALID_ARGUMENT
;
1166 static void netifd_add_iface_object(void)
1168 struct ubus_method
*methods
;
1171 methods
= calloc(1, sizeof(iface_object_methods
));
1175 memcpy(methods
, iface_object_methods
, sizeof(iface_object_methods
));
1176 iface_object
.methods
= methods
;
1178 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
1179 if (methods
[i
].handler
== netifd_handle_dump
)
1182 methods
[i
].handler
= netifd_handle_iface
;
1183 methods
[i
].policy
= &iface_policy
;
1184 methods
[i
].n_policy
= 1;
1186 netifd_add_object(&iface_object
);
1189 static struct wireless_device
*
1190 get_wdev(struct blob_attr
*msg
, int *ret
)
1192 struct blobmsg_policy wdev_policy
= {
1194 .type
= BLOBMSG_TYPE_STRING
,
1196 struct blob_attr
*dev_attr
;
1197 struct wireless_device
*wdev
= NULL
;
1200 blobmsg_parse(&wdev_policy
, 1, &dev_attr
, blob_data(msg
), blob_len(msg
));
1202 *ret
= UBUS_STATUS_INVALID_ARGUMENT
;
1206 wdev
= vlist_find(&wireless_devices
, blobmsg_data(dev_attr
), wdev
, node
);
1208 *ret
= UBUS_STATUS_NOT_FOUND
;
1217 netifd_handle_wdev_reconf(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1218 struct ubus_request_data
*req
, const char *method
,
1219 struct blob_attr
*msg
)
1221 struct wireless_device
*wdev
;
1224 wdev
= get_wdev(msg
, &ret
);
1225 if (ret
== UBUS_STATUS_NOT_FOUND
)
1229 wireless_device_reconf(wdev
);
1231 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1232 wireless_device_reconf(wdev
);
1239 netifd_handle_wdev_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1240 struct ubus_request_data
*req
, const char *method
,
1241 struct blob_attr
*msg
)
1243 struct wireless_device
*wdev
;
1246 wdev
= get_wdev(msg
, &ret
);
1247 if (ret
== UBUS_STATUS_NOT_FOUND
)
1251 wireless_device_set_up(wdev
);
1253 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1254 wireless_device_set_up(wdev
);
1261 netifd_handle_wdev_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1262 struct ubus_request_data
*req
, const char *method
,
1263 struct blob_attr
*msg
)
1265 struct wireless_device
*wdev
;
1268 wdev
= get_wdev(msg
, &ret
);
1269 if (ret
== UBUS_STATUS_NOT_FOUND
)
1273 wireless_device_set_down(wdev
);
1275 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1276 wireless_device_set_down(wdev
);
1283 netifd_handle_wdev_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1284 struct ubus_request_data
*req
, const char *method
,
1285 struct blob_attr
*msg
)
1287 struct wireless_device
*wdev
;
1290 wdev
= get_wdev(msg
, &ret
);
1291 if (ret
== UBUS_STATUS_NOT_FOUND
)
1294 blob_buf_init(&b
, 0);
1296 wireless_device_status(wdev
, &b
);
1298 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1299 wireless_device_status(wdev
, &b
);
1301 ubus_send_reply(ctx
, req
, b
.head
);
1306 netifd_handle_wdev_get_validate(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1307 struct ubus_request_data
*req
, const char *method
,
1308 struct blob_attr
*msg
)
1310 struct wireless_device
*wdev
;
1313 wdev
= get_wdev(msg
, &ret
);
1314 if (ret
== UBUS_STATUS_NOT_FOUND
)
1317 blob_buf_init(&b
, 0);
1319 wireless_device_get_validate(wdev
, &b
);
1321 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1322 wireless_device_get_validate(wdev
, &b
);
1324 ubus_send_reply(ctx
, req
, b
.head
);
1329 netifd_handle_wdev_notify(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1330 struct ubus_request_data
*req
, const char *method
,
1331 struct blob_attr
*msg
)
1333 struct wireless_device
*wdev
;
1336 wdev
= get_wdev(msg
, &ret
);
1340 return wireless_device_notify(wdev
, msg
, req
);
1343 static struct ubus_method wireless_object_methods
[] = {
1344 { .name
= "up", .handler
= netifd_handle_wdev_up
},
1345 { .name
= "down", .handler
= netifd_handle_wdev_down
},
1346 { .name
= "reconf", .handler
= netifd_handle_wdev_reconf
},
1347 { .name
= "status", .handler
= netifd_handle_wdev_status
},
1348 { .name
= "notify", .handler
= netifd_handle_wdev_notify
},
1349 { .name
= "get_validate", .handler
= netifd_handle_wdev_get_validate
},
1352 static struct ubus_object_type wireless_object_type
=
1353 UBUS_OBJECT_TYPE("netifd_iface", wireless_object_methods
);
1356 static struct ubus_object wireless_object
= {
1357 .name
= "network.wireless",
1358 .type
= &wireless_object_type
,
1359 .methods
= wireless_object_methods
,
1360 .n_methods
= ARRAY_SIZE(wireless_object_methods
),
1364 netifd_extdev_invoke(uint32_t id
, const char *method
, struct blob_attr
*msg
,
1365 ubus_data_handler_t data_cb
, void *data
)
1367 return ubus_invoke(ubus_ctx
, id
, method
, msg
, data_cb
, data
, 3000);
1371 netifd_udebug_cb(struct udebug_ubus
*ctx
, struct blob_attr
*data
, bool enabled
)
1373 netifd_udebug_set_enabled(enabled
);
1377 netifd_ubus_init(const char *path
)
1382 ubus_ctx
= ubus_connect(path
);
1386 D(SYSTEM
, "connected as %08x\n", ubus_ctx
->local_id
);
1387 ubus_ctx
->connection_lost
= netifd_ubus_connection_lost
;
1388 netifd_ubus_add_fd();
1390 netifd_add_object(&main_object
);
1391 netifd_add_object(&dev_object
);
1392 netifd_add_object(&wireless_object
);
1393 netifd_add_iface_object();
1395 udebug_ubus_init(&udebug
, ubus_ctx
, "netifd", netifd_udebug_cb
);
1401 netifd_ubus_done(void)
1403 udebug_ubus_free(&udebug
);
1404 ubus_free(ubus_ctx
);
1408 netifd_ubus_interface_event(struct interface
*iface
, bool up
)
1410 blob_buf_init(&b
, 0);
1411 blobmsg_add_string(&b
, "action", up
? "ifup" : "ifdown");
1412 blobmsg_add_string(&b
, "interface", iface
->name
);
1413 ubus_send_event(ubus_ctx
, "network.interface", b
.head
);
1417 netifd_ubus_interface_notify(struct interface
*iface
, bool up
)
1419 const char *event
= (up
) ? "interface.update" : "interface.down";
1420 blob_buf_init(&b
, 0);
1421 blobmsg_add_string(&b
, "interface", iface
->name
);
1422 netifd_dump_status(iface
);
1423 ubus_notify(ubus_ctx
, &iface_object
, event
, b
.head
, -1);
1424 ubus_notify(ubus_ctx
, &iface
->ubus
, event
, b
.head
, -1);
1428 netifd_ubus_add_interface(struct interface
*iface
)
1430 struct ubus_object
*obj
= &iface
->ubus
;
1433 if (asprintf(&name
, "%s.interface.%s", main_object
.name
, iface
->name
) == -1)
1437 obj
->type
= &iface_object_type
;
1438 obj
->methods
= iface_object_methods
;
1439 obj
->n_methods
= ARRAY_SIZE(iface_object_methods
);
1440 if (ubus_add_object(ubus_ctx
, &iface
->ubus
)) {
1441 D(SYSTEM
, "failed to publish ubus object for interface '%s'\n", iface
->name
);
1448 netifd_ubus_remove_interface(struct interface
*iface
)
1450 if (!iface
->ubus
.name
)
1453 ubus_remove_object(ubus_ctx
, &iface
->ubus
);
1454 free((void *) iface
->ubus
.name
);