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
;
30 static struct udebug_ubus udebug
;
35 netifd_handle_restart(struct ubus_context
*ctx
, struct ubus_object
*obj
,
36 struct ubus_request_data
*req
, const char *method
,
37 struct blob_attr
*msg
)
44 netifd_handle_reload(struct ubus_context
*ctx
, struct ubus_object
*obj
,
45 struct ubus_request_data
*req
, const char *method
,
46 struct blob_attr
*msg
)
49 return UBUS_STATUS_NOT_FOUND
;
51 return UBUS_STATUS_OK
;
62 static const struct blobmsg_policy route_policy
[__HR_MAX
] = {
63 [HR_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
64 [HR_V6
] = { .name
= "v6", .type
= BLOBMSG_TYPE_BOOL
},
65 [HR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
66 [HR_EXCLUDE
] = { .name
= "exclude", .type
= BLOBMSG_TYPE_BOOL
},
70 netifd_add_host_route(struct ubus_context
*ctx
, struct ubus_object
*obj
,
71 struct ubus_request_data
*req
, const char *method
,
72 struct blob_attr
*msg
)
74 struct blob_attr
*tb
[__HR_MAX
];
75 struct interface
*iface
= NULL
;
80 blobmsg_parse(route_policy
, __HR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
82 return UBUS_STATUS_INVALID_ARGUMENT
;
85 v6
= blobmsg_get_bool(tb
[HR_V6
]);
88 exclude
= blobmsg_get_bool(tb
[HR_EXCLUDE
]);
91 iface
= vlist_find(&interfaces
, blobmsg_data(tb
[HR_INTERFACE
]), iface
, node
);
93 memset(&a
, 0, sizeof(a
));
94 if (!inet_pton(v6
? AF_INET6
: AF_INET
, blobmsg_data(tb
[HR_TARGET
]), &a
))
95 return UBUS_STATUS_INVALID_ARGUMENT
;
97 iface
= interface_ip_add_target_route(&a
, v6
, iface
, exclude
);
99 return UBUS_STATUS_NOT_FOUND
;
101 blob_buf_init(&b
, 0);
102 blobmsg_add_string(&b
, "interface", iface
->name
);
103 ubus_send_reply(ctx
, req
, b
.head
);
109 netifd_get_proto_handlers(struct ubus_context
*ctx
, struct ubus_object
*obj
,
110 struct ubus_request_data
*req
, const char *method
,
111 struct blob_attr
*msg
)
113 blob_buf_init(&b
, 0);
114 proto_dump_handlers(&b
);
115 ubus_send_reply(ctx
, req
, b
.head
);
126 static const struct blobmsg_policy dynamic_policy
[__DI_MAX
] = {
127 [DI_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
131 netifd_add_dynamic(struct ubus_context
*ctx
, struct ubus_object
*obj
,
132 struct ubus_request_data
*req
, const char *method
,
133 struct blob_attr
*msg
)
135 struct blob_attr
*tb
[__DI_MAX
];
136 struct interface
*iface
;
137 struct blob_attr
*config
;
139 blobmsg_parse(dynamic_policy
, __DI_MAX
, tb
, blob_data(msg
), blob_len(msg
));
142 return UBUS_STATUS_INVALID_ARGUMENT
;
144 const char *name
= blobmsg_get_string(tb
[DI_NAME
]);
146 iface
= interface_alloc(name
, msg
, true);
148 return UBUS_STATUS_UNKNOWN_ERROR
;
150 config
= blob_memdup(msg
);
154 if (!interface_add(iface
, config
))
155 goto error_free_config
;
157 return UBUS_STATUS_OK
;
163 return UBUS_STATUS_UNKNOWN_ERROR
;
172 static const struct blobmsg_policy netns_updown_policy
[__NETNS_UPDOWN_MAX
] = {
173 [NETNS_UPDOWN_JAIL
] = { .name
= "jail", .type
= BLOBMSG_TYPE_STRING
},
174 [NETNS_UPDOWN_START
] = { .name
= "start", .type
= BLOBMSG_TYPE_BOOL
},
178 netifd_netns_updown(struct ubus_context
*ctx
, struct ubus_object
*obj
,
179 struct ubus_request_data
*req
, const char *method
,
180 struct blob_attr
*msg
)
182 int target_netns_fd
= ubus_request_get_caller_fd(req
);
183 struct blob_attr
*tb
[__NETNS_UPDOWN_MAX
];
186 if (target_netns_fd
< 0)
187 return UBUS_STATUS_INVALID_ARGUMENT
;
189 blobmsg_parse(netns_updown_policy
, __NETNS_UPDOWN_MAX
, tb
, blob_data(msg
), blob_len(msg
));
191 start
= tb
[NETNS_UPDOWN_START
] && blobmsg_get_bool(tb
[NETNS_UPDOWN_START
]);
194 if (!tb
[NETNS_UPDOWN_JAIL
])
195 return UBUS_STATUS_INVALID_ARGUMENT
;
197 interface_start_jail(target_netns_fd
, blobmsg_get_string(tb
[NETNS_UPDOWN_JAIL
]));
199 interface_stop_jail(target_netns_fd
);
201 close(target_netns_fd
);
203 return UBUS_STATUS_OK
;
206 static struct ubus_method main_object_methods
[] = {
207 { .name
= "restart", .handler
= netifd_handle_restart
},
208 { .name
= "reload", .handler
= netifd_handle_reload
},
209 UBUS_METHOD("add_host_route", netifd_add_host_route
, route_policy
),
210 { .name
= "get_proto_handlers", .handler
= netifd_get_proto_handlers
},
211 UBUS_METHOD("add_dynamic", netifd_add_dynamic
, dynamic_policy
),
212 UBUS_METHOD("netns_updown", netifd_netns_updown
, netns_updown_policy
),
215 static struct ubus_object_type main_object_type
=
216 UBUS_OBJECT_TYPE("netifd", main_object_methods
);
218 static struct ubus_object main_object
= {
220 .type
= &main_object_type
,
221 .methods
= main_object_methods
,
222 .n_methods
= ARRAY_SIZE(main_object_methods
),
230 static const struct blobmsg_policy dev_policy
[__DEV_MAX
] = {
231 [DEV_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
235 netifd_dev_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
236 struct ubus_request_data
*req
, const char *method
,
237 struct blob_attr
*msg
)
239 struct device
*dev
= NULL
;
240 struct blob_attr
*tb
[__DEV_MAX
];
242 blobmsg_parse(dev_policy
, __DEV_MAX
, tb
, blob_data(msg
), blob_len(msg
));
245 dev
= device_find(blobmsg_data(tb
[DEV_NAME
]));
247 return UBUS_STATUS_INVALID_ARGUMENT
;
250 blob_buf_init(&b
, 0);
251 device_dump_status(&b
, dev
);
252 ubus_send_reply(ctx
, req
, b
.head
);
263 static const struct blobmsg_policy alias_attrs
[__ALIAS_ATTR_MAX
] = {
264 [ALIAS_ATTR_ALIAS
] = { "alias", BLOBMSG_TYPE_ARRAY
},
265 [ALIAS_ATTR_DEV
] = { "device", BLOBMSG_TYPE_STRING
},
269 netifd_handle_alias(struct ubus_context
*ctx
, struct ubus_object
*obj
,
270 struct ubus_request_data
*req
, const char *method
,
271 struct blob_attr
*msg
)
273 struct device
*dev
= NULL
;
274 struct blob_attr
*tb
[__ALIAS_ATTR_MAX
];
275 struct blob_attr
*cur
;
278 blobmsg_parse(alias_attrs
, __ALIAS_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
280 if (!tb
[ALIAS_ATTR_ALIAS
])
281 return UBUS_STATUS_INVALID_ARGUMENT
;
283 if ((cur
= tb
[ALIAS_ATTR_DEV
]) != NULL
) {
284 dev
= device_get(blobmsg_data(cur
), true);
286 return UBUS_STATUS_NOT_FOUND
;
289 blobmsg_for_each_attr(cur
, tb
[ALIAS_ATTR_ALIAS
], rem
) {
290 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
293 if (!blobmsg_check_attr(cur
, false))
296 alias_notify_device(blobmsg_data(cur
), dev
);
301 device_free_unused();
302 return UBUS_STATUS_INVALID_ARGUMENT
;
308 DEV_STATE_AUTH_STATUS
,
309 DEV_STATE_AUTH_VLANS
,
313 static const struct blobmsg_policy dev_state_policy
[__DEV_STATE_MAX
] = {
314 [DEV_STATE_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
315 [DEV_STATE_DEFER
] = { .name
= "defer", .type
= BLOBMSG_TYPE_BOOL
},
316 [DEV_STATE_AUTH_STATUS
] = { .name
= "auth_status", .type
= BLOBMSG_TYPE_BOOL
},
317 [DEV_STATE_AUTH_VLANS
] = { .name
= "auth_vlans", BLOBMSG_TYPE_ARRAY
},
321 netifd_handle_set_state(struct ubus_context
*ctx
, struct ubus_object
*obj
,
322 struct ubus_request_data
*req
, const char *method
,
323 struct blob_attr
*msg
)
325 struct device
*dev
= NULL
;
326 struct blob_attr
*tb
[__DEV_STATE_MAX
];
327 struct blob_attr
*cur
;
330 blobmsg_parse(dev_state_policy
, __DEV_STATE_MAX
, tb
, blob_data(msg
), blob_len(msg
));
332 cur
= tb
[DEV_STATE_NAME
];
334 return UBUS_STATUS_INVALID_ARGUMENT
;
336 dev
= device_find(blobmsg_data(cur
));
338 return UBUS_STATUS_NOT_FOUND
;
340 cur
= tb
[DEV_STATE_DEFER
];
342 device_set_deferred(dev
, !!blobmsg_get_u8(cur
));
344 if ((cur
= tb
[DEV_STATE_AUTH_STATUS
]) != NULL
)
345 auth_status
= blobmsg_get_bool(cur
);
347 auth_status
= dev
->auth_status
;
348 if (tb
[DEV_STATE_AUTH_STATUS
] || tb
[DEV_STATE_AUTH_VLANS
])
349 device_set_auth_status(dev
, auth_status
, tb
[DEV_STATE_AUTH_VLANS
]);
356 DEV_HOTPLUG_ATTR_NAME
,
357 DEV_HOTPLUG_ATTR_ADD
,
358 __DEV_HOTPLUG_ATTR_MAX
,
361 static const struct blobmsg_policy dev_hotplug_policy
[__DEV_HOTPLUG_ATTR_MAX
] = {
362 [DEV_HOTPLUG_ATTR_NAME
] = { "name", BLOBMSG_TYPE_STRING
},
363 [DEV_HOTPLUG_ATTR_ADD
] = { "add", BLOBMSG_TYPE_BOOL
},
367 netifd_handle_dev_hotplug(struct ubus_context
*ctx
, struct ubus_object
*obj
,
368 struct ubus_request_data
*req
, const char *method
,
369 struct blob_attr
*msg
)
371 struct blob_attr
*tb
[__DEV_HOTPLUG_ATTR_MAX
];
374 blobmsg_parse(dev_hotplug_policy
, __DEV_HOTPLUG_ATTR_MAX
, tb
,
375 blob_data(msg
), blob_len(msg
));
377 if (!tb
[DEV_HOTPLUG_ATTR_NAME
] || !tb
[DEV_HOTPLUG_ATTR_ADD
])
378 return UBUS_STATUS_INVALID_ARGUMENT
;
380 name
= blobmsg_get_string(tb
[DEV_HOTPLUG_ATTR_NAME
]);
381 device_hotplug_event(name
, blobmsg_get_bool(tb
[DEV_HOTPLUG_ATTR_ADD
]));
388 netifd_handle_stp_init(struct ubus_context
*ctx
, struct ubus_object
*obj
,
389 struct ubus_request_data
*req
, const char *method
,
390 struct blob_attr
*msg
)
397 static struct ubus_method dev_object_methods
[] = {
398 UBUS_METHOD("status", netifd_dev_status
, dev_policy
),
399 UBUS_METHOD("set_alias", netifd_handle_alias
, alias_attrs
),
400 UBUS_METHOD("set_state", netifd_handle_set_state
, dev_state_policy
),
402 UBUS_METHOD("hotplug_event", netifd_handle_dev_hotplug
, dev_hotplug_policy
),
404 UBUS_METHOD_NOARG("stp_init", netifd_handle_stp_init
)
407 static struct ubus_object_type dev_object_type
=
408 UBUS_OBJECT_TYPE("device", dev_object_methods
);
410 static struct ubus_object dev_object
= {
411 .name
= "network.device",
412 .type
= &dev_object_type
,
413 .methods
= dev_object_methods
,
414 .n_methods
= ARRAY_SIZE(dev_object_methods
),
418 netifd_ubus_add_fd(void)
420 ubus_add_uloop(ubus_ctx
);
421 system_fd_set_cloexec(ubus_ctx
->sock
.fd
);
424 void netifd_ubus_device_notify(const char *event
, struct blob_attr
*data
, int timeout
)
426 ubus_notify(ubus_ctx
, &dev_object
, event
, data
, timeout
);
430 netifd_ubus_reconnect_timer(struct uloop_timeout
*timeout
)
432 static struct uloop_timeout retry
= {
433 .cb
= netifd_ubus_reconnect_timer
,
437 if (ubus_reconnect(ubus_ctx
, ubus_path
) != 0) {
438 D(SYSTEM
, "failed to reconnect, trying again in %d seconds", t
);
439 uloop_timeout_set(&retry
, t
* 1000);
443 D(SYSTEM
, "reconnected to ubus, new id: %08x", ubus_ctx
->local_id
);
444 netifd_ubus_add_fd();
448 netifd_ubus_connection_lost(struct ubus_context
*ctx
)
450 netifd_ubus_reconnect_timer(NULL
);
453 /* per-interface object */
456 netifd_handle_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
457 struct ubus_request_data
*req
, const char *method
,
458 struct blob_attr
*msg
)
460 struct interface
*iface
;
462 iface
= container_of(obj
, struct interface
, ubus
);
463 interface_set_up(iface
);
469 netifd_handle_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
470 struct ubus_request_data
*req
, const char *method
,
471 struct blob_attr
*msg
)
473 struct interface
*iface
;
475 iface
= container_of(obj
, struct interface
, ubus
);
476 interface_set_down(iface
);
482 netifd_handle_renew(struct ubus_context
*ctx
, struct ubus_object
*obj
,
483 struct ubus_request_data
*req
, const char *method
,
484 struct blob_attr
*msg
)
486 struct interface
*iface
;
488 iface
= container_of(obj
, struct interface
, ubus
);
489 interface_renew(iface
);
495 netifd_add_interface_errors(struct blob_buf
*b
, struct interface
*iface
)
497 struct interface_error
*error
;
501 e
= blobmsg_open_array(b
, "errors");
502 list_for_each_entry(error
, &iface
->errors
, list
) {
503 e2
= blobmsg_open_table(b
, NULL
);
505 blobmsg_add_string(b
, "subsystem", error
->subsystem
);
506 blobmsg_add_string(b
, "code", error
->code
);
507 if (error
->data
[0]) {
508 e3
= blobmsg_open_array(b
, "data");
509 for (i
= 0; error
->data
[i
]; i
++)
510 blobmsg_add_string(b
, NULL
, error
->data
[i
]);
511 blobmsg_close_array(b
, e3
);
514 blobmsg_close_table(b
, e2
);
516 blobmsg_close_array(b
, e
);
520 interface_ip_dump_address_list(struct interface_ip_settings
*ip
, bool v6
, bool enabled
)
522 struct device_addr
*addr
;
528 time_t now
= system_get_rtime();
529 vlist_for_each_element(&ip
->addr
, addr
, node
) {
530 if (addr
->enabled
!= enabled
)
533 if ((addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
538 if (af
!= (v6
? AF_INET6
: AF_INET
))
541 a
= blobmsg_open_table(&b
, NULL
);
543 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
544 inet_ntop(af
, &addr
->addr
, buf
, buflen
);
545 blobmsg_add_string_buffer(&b
);
547 blobmsg_add_u32(&b
, "mask", addr
->mask
);
549 if (addr
->point_to_point
) {
550 buf
= blobmsg_alloc_string_buffer(&b
, "ptpaddress", buflen
);
551 inet_ntop(af
, &addr
->point_to_point
, buf
, buflen
);
552 blobmsg_add_string_buffer(&b
);
555 if (addr
->preferred_until
) {
556 int preferred
= addr
->preferred_until
- now
;
559 blobmsg_add_u32(&b
, "preferred", preferred
);
562 if (addr
->valid_until
)
563 blobmsg_add_u32(&b
, "valid", addr
->valid_until
- now
);
566 blobmsg_add_string(&b
, "class", addr
->pclass
);
568 blobmsg_close_table(&b
, a
);
573 interface_ip_dump_neighbor_list(struct interface_ip_settings
*ip
, bool enabled
)
575 struct device_neighbor
*neighbor
;
581 vlist_for_each_element(&ip
->neighbor
, neighbor
, node
) {
582 if (neighbor
->enabled
!= enabled
)
585 if ((neighbor
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
590 r
= blobmsg_open_table(&b
, NULL
);
592 if (neighbor
->flags
& DEVNEIGH_MAC
)
593 blobmsg_add_string(&b
, "mac", format_macaddr(neighbor
->macaddr
));
595 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
596 inet_ntop(af
, &neighbor
->addr
, buf
, buflen
);
597 blobmsg_add_string_buffer(&b
);
600 blobmsg_add_u32(&b
, "proxy", neighbor
->proxy
);
602 if (neighbor
->router
)
603 blobmsg_add_u32(&b
, "router", neighbor
->router
);
605 blobmsg_close_table(&b
, r
);
610 interface_ip_dump_route_list(struct interface_ip_settings
*ip
, bool enabled
)
612 struct device_route
*route
;
618 time_t now
= system_get_rtime();
619 vlist_for_each_element(&ip
->route
, route
, node
) {
620 if (route
->enabled
!= enabled
)
623 if ((ip
->no_defaultroute
== enabled
) && !route
->mask
)
626 if ((route
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
631 r
= blobmsg_open_table(&b
, NULL
);
633 buf
= blobmsg_alloc_string_buffer(&b
, "target", buflen
);
634 inet_ntop(af
, &route
->addr
, buf
, buflen
);
635 blobmsg_add_string_buffer(&b
);
637 blobmsg_add_u32(&b
, "mask", route
->mask
);
639 buf
= blobmsg_alloc_string_buffer(&b
, "nexthop", buflen
);
640 inet_ntop(af
, &route
->nexthop
, buf
, buflen
);
641 blobmsg_add_string_buffer(&b
);
643 if (route
->flags
& DEVROUTE_TYPE
)
644 blobmsg_add_u32(&b
, "type", route
->type
);
646 if (route
->flags
& DEVROUTE_PROTO
)
647 blobmsg_add_u32(&b
, "proto", route
->proto
);
649 if (route
->flags
& DEVROUTE_MTU
)
650 blobmsg_add_u32(&b
, "mtu", route
->mtu
);
652 if (route
->flags
& DEVROUTE_METRIC
)
653 blobmsg_add_u32(&b
, "metric", route
->metric
);
655 if (route
->flags
& DEVROUTE_TABLE
)
656 blobmsg_add_u32(&b
, "table", route
->table
);
658 if (route
->valid_until
)
659 blobmsg_add_u32(&b
, "valid", route
->valid_until
- now
);
661 buf
= blobmsg_alloc_string_buffer(&b
, "source", buflen
);
662 inet_ntop(af
, &route
->source
, buf
, buflen
);
663 snprintf(buf
+ strlen(buf
), buflen
- strlen(buf
), "/%u", route
->sourcemask
);
664 blobmsg_add_string_buffer(&b
);
666 blobmsg_close_table(&b
, r
);
672 interface_ip_dump_prefix_list(struct interface_ip_settings
*ip
)
674 struct device_prefix
*prefix
;
677 const int buflen
= INET6_ADDRSTRLEN
;
679 time_t now
= system_get_rtime();
680 vlist_for_each_element(&ip
->prefix
, prefix
, node
) {
681 a
= blobmsg_open_table(&b
, NULL
);
683 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
684 inet_ntop(AF_INET6
, &prefix
->addr
, buf
, buflen
);
685 blobmsg_add_string_buffer(&b
);
687 blobmsg_add_u32(&b
, "mask", prefix
->length
);
689 if (prefix
->preferred_until
) {
690 int preferred
= prefix
->preferred_until
- now
;
693 blobmsg_add_u32(&b
, "preferred", preferred
);
696 if (prefix
->valid_until
)
697 blobmsg_add_u32(&b
, "valid", prefix
->valid_until
- now
);
699 blobmsg_add_string(&b
, "class", prefix
->pclass
);
701 c
= blobmsg_open_table(&b
, "assigned");
702 struct device_prefix_assignment
*assign
;
703 list_for_each_entry(assign
, &prefix
->assignments
, head
) {
704 if (!assign
->name
[0])
707 struct in6_addr addr
= prefix
->addr
;
708 addr
.s6_addr32
[1] |= htonl(assign
->assigned
);
710 void *d
= blobmsg_open_table(&b
, assign
->name
);
712 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
713 inet_ntop(AF_INET6
, &addr
, buf
, buflen
);
714 blobmsg_add_string_buffer(&b
);
716 blobmsg_add_u32(&b
, "mask", assign
->length
);
718 blobmsg_close_table(&b
, d
);
720 blobmsg_close_table(&b
, c
);
722 blobmsg_close_table(&b
, a
);
728 interface_ip_dump_prefix_assignment_list(struct interface
*iface
)
732 const int buflen
= INET6_ADDRSTRLEN
;
733 time_t now
= system_get_rtime();
735 struct device_prefix
*prefix
;
736 list_for_each_entry(prefix
, &prefixes
, head
) {
737 struct device_prefix_assignment
*assign
;
738 list_for_each_entry(assign
, &prefix
->assignments
, head
) {
739 if (strcmp(assign
->name
, iface
->name
))
742 struct in6_addr addr
= prefix
->addr
;
743 addr
.s6_addr32
[1] |= htonl(assign
->assigned
);
745 a
= blobmsg_open_table(&b
, NULL
);
747 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
748 inet_ntop(AF_INET6
, &addr
, buf
, buflen
);
749 blobmsg_add_string_buffer(&b
);
751 blobmsg_add_u32(&b
, "mask", assign
->length
);
753 if (prefix
->preferred_until
) {
754 int preferred
= prefix
->preferred_until
- now
;
757 blobmsg_add_u32(&b
, "preferred", preferred
);
760 if (prefix
->valid_until
)
761 blobmsg_add_u32(&b
, "valid", prefix
->valid_until
- now
);
763 void *c
= blobmsg_open_table(&b
, "local-address");
764 if (assign
->enabled
) {
765 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
766 inet_ntop(AF_INET6
, &assign
->addr
, buf
, buflen
);
767 blobmsg_add_string_buffer(&b
);
769 blobmsg_add_u32(&b
, "mask", assign
->length
);
771 blobmsg_close_table(&b
, c
);
773 blobmsg_close_table(&b
, a
);
779 interface_ip_dump_dns_server_list(struct interface_ip_settings
*ip
, bool enabled
)
781 struct dns_server
*dns
;
785 vlist_simple_for_each_element(&ip
->dns_servers
, dns
, node
) {
786 if (ip
->no_dns
== enabled
)
789 buf
= blobmsg_alloc_string_buffer(&b
, NULL
, buflen
);
790 inet_ntop(dns
->af
, &dns
->addr
, buf
, buflen
);
791 blobmsg_add_string_buffer(&b
);
796 interface_ip_dump_dns_search_list(struct interface_ip_settings
*ip
, bool enabled
)
798 struct dns_search_domain
*dns
;
800 vlist_simple_for_each_element(&ip
->dns_search
, dns
, node
) {
801 if (ip
->no_dns
== enabled
)
804 blobmsg_add_string(&b
, NULL
, dns
->name
);
809 netifd_dump_status(struct interface
*iface
)
811 struct interface_data
*data
;
815 blobmsg_add_u8(&b
, "up", iface
->state
== IFS_UP
);
816 blobmsg_add_u8(&b
, "pending", iface
->state
== IFS_SETUP
);
817 blobmsg_add_u8(&b
, "available", iface
->available
);
818 blobmsg_add_u8(&b
, "autostart", iface
->autostart
);
819 blobmsg_add_u8(&b
, "dynamic", iface
->dynamic
);
821 if (iface
->state
== IFS_UP
) {
822 time_t cur
= system_get_rtime();
823 blobmsg_add_u32(&b
, "uptime", cur
- iface
->start_time
);
824 if (iface
->l3_dev
.dev
)
825 blobmsg_add_string(&b
, "l3_device", iface
->l3_dev
.dev
->ifname
);
828 if (iface
->proto_handler
)
829 blobmsg_add_string(&b
, "proto", iface
->proto_handler
->name
);
831 dev
= iface
->main_dev
.dev
;
832 if (dev
&& !dev
->hidden
&& iface
->proto_handler
&&
833 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
))
834 blobmsg_add_string(&b
, "device", dev
->ifname
);
837 blobmsg_add_string(&b
, "jail", iface
->jail
);
839 if (iface
->jail_device
)
840 blobmsg_add_string(&b
, "jail_device", iface
->jail_device
);
842 if (iface
->state
== IFS_UP
) {
843 if (iface
->updated
) {
844 a
= blobmsg_open_array(&b
, "updated");
846 if (iface
->updated
& IUF_ADDRESS
)
847 blobmsg_add_string(&b
, NULL
, "addresses");
848 if (iface
->updated
& IUF_ROUTE
)
849 blobmsg_add_string(&b
, NULL
, "routes");
850 if (iface
->updated
& IUF_PREFIX
)
851 blobmsg_add_string(&b
, NULL
, "prefixes");
852 if (iface
->updated
& IUF_DATA
)
853 blobmsg_add_string(&b
, NULL
, "data");
855 blobmsg_close_array(&b
, a
);
859 blobmsg_add_u32(&b
, "ip4table", iface
->ip4table
);
861 blobmsg_add_u32(&b
, "ip6table", iface
->ip6table
);
862 blobmsg_add_u32(&b
, "metric", iface
->metric
);
863 blobmsg_add_u32(&b
, "dns_metric", iface
->dns_metric
);
864 blobmsg_add_u8(&b
, "delegation", !iface
->proto_ip
.no_delegation
);
865 if (iface
->assignment_weight
)
866 blobmsg_add_u32(&b
, "ip6weight", iface
->assignment_weight
);
867 a
= blobmsg_open_array(&b
, "ipv4-address");
868 interface_ip_dump_address_list(&iface
->config_ip
, false, true);
869 interface_ip_dump_address_list(&iface
->proto_ip
, false, true);
870 blobmsg_close_array(&b
, a
);
871 a
= blobmsg_open_array(&b
, "ipv6-address");
872 interface_ip_dump_address_list(&iface
->config_ip
, true, true);
873 interface_ip_dump_address_list(&iface
->proto_ip
, true, true);
874 blobmsg_close_array(&b
, a
);
875 a
= blobmsg_open_array(&b
, "ipv6-prefix");
876 interface_ip_dump_prefix_list(&iface
->config_ip
);
877 interface_ip_dump_prefix_list(&iface
->proto_ip
);
878 blobmsg_close_array(&b
, a
);
879 a
= blobmsg_open_array(&b
, "ipv6-prefix-assignment");
880 interface_ip_dump_prefix_assignment_list(iface
);
881 blobmsg_close_array(&b
, a
);
882 a
= blobmsg_open_array(&b
, "route");
883 interface_ip_dump_route_list(&iface
->config_ip
, true);
884 interface_ip_dump_route_list(&iface
->proto_ip
, true);
885 blobmsg_close_array(&b
, a
);
886 a
= blobmsg_open_array(&b
, "dns-server");
887 interface_ip_dump_dns_server_list(&iface
->config_ip
, true);
888 interface_ip_dump_dns_server_list(&iface
->proto_ip
, true);
889 blobmsg_close_array(&b
, a
);
890 a
= blobmsg_open_array(&b
, "dns-search");
891 interface_ip_dump_dns_search_list(&iface
->config_ip
, true);
892 interface_ip_dump_dns_search_list(&iface
->proto_ip
, true);
893 blobmsg_close_array(&b
, a
);
894 a
= blobmsg_open_array(&b
, "neighbors");
895 interface_ip_dump_neighbor_list(&iface
->config_ip
, true);
896 interface_ip_dump_neighbor_list(&iface
->proto_ip
, true);
897 blobmsg_close_array(&b
, a
);
899 inactive
= blobmsg_open_table(&b
, "inactive");
900 a
= blobmsg_open_array(&b
, "ipv4-address");
901 interface_ip_dump_address_list(&iface
->config_ip
, false, false);
902 interface_ip_dump_address_list(&iface
->proto_ip
, false, false);
903 blobmsg_close_array(&b
, a
);
904 a
= blobmsg_open_array(&b
, "ipv6-address");
905 interface_ip_dump_address_list(&iface
->config_ip
, true, false);
906 interface_ip_dump_address_list(&iface
->proto_ip
, true, false);
907 blobmsg_close_array(&b
, a
);
908 a
= blobmsg_open_array(&b
, "route");
909 interface_ip_dump_route_list(&iface
->config_ip
, false);
910 interface_ip_dump_route_list(&iface
->proto_ip
, false);
911 blobmsg_close_array(&b
, a
);
912 a
= blobmsg_open_array(&b
, "dns-server");
913 interface_ip_dump_dns_server_list(&iface
->config_ip
, false);
914 interface_ip_dump_dns_server_list(&iface
->proto_ip
, false);
915 blobmsg_close_array(&b
, a
);
916 a
= blobmsg_open_array(&b
, "dns-search");
917 interface_ip_dump_dns_search_list(&iface
->config_ip
, false);
918 interface_ip_dump_dns_search_list(&iface
->proto_ip
, false);
919 blobmsg_close_array(&b
, a
);
920 a
= blobmsg_open_array(&b
, "neighbors");
921 interface_ip_dump_neighbor_list(&iface
->config_ip
, false);
922 interface_ip_dump_neighbor_list(&iface
->proto_ip
, false);
923 blobmsg_close_array(&b
, a
);
924 blobmsg_close_table(&b
, inactive
);
927 a
= blobmsg_open_table(&b
, "data");
930 blobmsg_add_string(&b
, "zone", iface
->zone
);
931 avl_for_each_element(&iface
->data
, data
, node
)
932 blobmsg_add_blob(&b
, data
->data
);
934 blobmsg_close_table(&b
, a
);
936 if (!list_empty(&iface
->errors
))
937 netifd_add_interface_errors(&b
, iface
);
941 netifd_handle_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
942 struct ubus_request_data
*req
, const char *method
,
943 struct blob_attr
*msg
)
945 struct interface
*iface
= container_of(obj
, struct interface
, ubus
);
947 blob_buf_init(&b
, 0);
948 netifd_dump_status(iface
);
949 ubus_send_reply(ctx
, req
, b
.head
);
956 netifd_handle_dump(struct ubus_context
*ctx
, struct ubus_object
*obj
,
957 struct ubus_request_data
*req
, const char *method
,
958 struct blob_attr
*msg
)
960 blob_buf_init(&b
, 0);
961 void *a
= blobmsg_open_array(&b
, "interface");
963 struct interface
*iface
;
964 vlist_for_each_element(&interfaces
, iface
, node
) {
965 void *i
= blobmsg_open_table(&b
, NULL
);
966 blobmsg_add_string(&b
, "interface", iface
->name
);
967 netifd_dump_status(iface
);
968 blobmsg_close_table(&b
, i
);
971 blobmsg_close_array(&b
, a
);
972 ubus_send_reply(ctx
, req
, b
.head
);
984 static const struct blobmsg_policy dev_link_policy
[__DEV_LINK_MAX
] = {
985 [DEV_LINK_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
986 [DEV_LINK_EXT
] = { .name
= "link-ext", .type
= BLOBMSG_TYPE_BOOL
},
987 [DEV_LINK_VLAN
] = { .name
= "vlan", .type
= BLOBMSG_TYPE_ARRAY
},
991 netifd_iface_handle_device(struct ubus_context
*ctx
, struct ubus_object
*obj
,
992 struct ubus_request_data
*req
, const char *method
,
993 struct blob_attr
*msg
)
995 struct blob_attr
*tb
[__DEV_LINK_MAX
];
996 struct blob_attr
*cur
;
997 struct interface
*iface
;
998 bool add
= !strncmp(method
, "add", 3);
999 bool link_ext
= true;
1001 iface
= container_of(obj
, struct interface
, ubus
);
1003 blobmsg_parse(dev_link_policy
, __DEV_LINK_MAX
, tb
, blob_data(msg
), blob_len(msg
));
1005 if (!tb
[DEV_LINK_NAME
])
1006 return UBUS_STATUS_INVALID_ARGUMENT
;
1008 cur
= tb
[DEV_LINK_EXT
];
1010 link_ext
= blobmsg_get_bool(cur
);
1012 return interface_handle_link(iface
, blobmsg_data(tb
[DEV_LINK_NAME
]),
1013 tb
[DEV_LINK_VLAN
], add
, link_ext
);
1018 netifd_iface_notify_proto(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1019 struct ubus_request_data
*req
, const char *method
,
1020 struct blob_attr
*msg
)
1022 struct interface
*iface
;
1024 iface
= container_of(obj
, struct interface
, ubus
);
1026 if (!iface
->proto
|| !iface
->proto
->notify
)
1027 return UBUS_STATUS_NOT_SUPPORTED
;
1029 return iface
->proto
->notify(iface
->proto
, msg
);
1033 netifd_iface_do_remove(struct uloop_timeout
*timeout
)
1035 struct interface
*iface
;
1037 iface
= container_of(timeout
, struct interface
, remove_timer
);
1038 vlist_delete(&interfaces
, &iface
->node
);
1042 netifd_iface_remove(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1043 struct ubus_request_data
*req
, const char *method
,
1044 struct blob_attr
*msg
)
1046 struct interface
*iface
;
1048 iface
= container_of(obj
, struct interface
, ubus
);
1049 if (iface
->remove_timer
.cb
)
1050 return UBUS_STATUS_INVALID_ARGUMENT
;
1052 iface
->remove_timer
.cb
= netifd_iface_do_remove
;
1053 uloop_timeout_set(&iface
->remove_timer
, 100);
1058 netifd_handle_iface_prepare(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1059 struct ubus_request_data
*req
, const char *method
,
1060 struct blob_attr
*msg
)
1062 struct interface
*iface
;
1063 struct device
*dev
, *bridge_dev
= NULL
;
1064 const struct device_hotplug_ops
*ops
;
1066 iface
= container_of(obj
, struct interface
, ubus
);
1067 dev
= iface
->main_dev
.dev
;
1071 ops
= dev
->hotplug_ops
;
1075 ops
->prepare(dev
, &bridge_dev
);
1078 blob_buf_init(&b
, 0);
1080 blobmsg_add_string(&b
, "bridge", bridge_dev
->ifname
);
1081 ubus_send_reply(ctx
, req
, b
.head
);
1087 netifd_handle_set_data(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1088 struct ubus_request_data
*req
, const char *method
,
1089 struct blob_attr
*msg
)
1091 struct interface
*iface
;
1093 iface
= container_of(obj
, struct interface
, ubus
);
1095 return interface_parse_data(iface
, msg
);
1098 static struct ubus_method iface_object_methods
[] = {
1099 { .name
= "up", .handler
= netifd_handle_up
},
1100 { .name
= "down", .handler
= netifd_handle_down
},
1101 { .name
= "renew", .handler
= netifd_handle_renew
},
1102 { .name
= "status", .handler
= netifd_handle_status
},
1103 { .name
= "prepare", .handler
= netifd_handle_iface_prepare
},
1104 { .name
= "dump", .handler
= netifd_handle_dump
},
1105 UBUS_METHOD("add_device", netifd_iface_handle_device
, dev_link_policy
),
1106 UBUS_METHOD("remove_device", netifd_iface_handle_device
, dev_link_policy
),
1107 { .name
= "notify_proto", .handler
= netifd_iface_notify_proto
},
1108 { .name
= "remove", .handler
= netifd_iface_remove
},
1109 { .name
= "set_data", .handler
= netifd_handle_set_data
},
1112 static struct ubus_object_type iface_object_type
=
1113 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods
);
1116 static struct ubus_object iface_object
= {
1117 .name
= "network.interface",
1118 .type
= &iface_object_type
,
1119 .n_methods
= ARRAY_SIZE(iface_object_methods
),
1122 static void netifd_add_object(struct ubus_object
*obj
)
1124 int ret
= ubus_add_object(ubus_ctx
, obj
);
1127 fprintf(stderr
, "Failed to publish object '%s': %s\n", obj
->name
, ubus_strerror(ret
));
1130 static const struct blobmsg_policy iface_policy
= {
1131 .name
= "interface",
1132 .type
= BLOBMSG_TYPE_STRING
,
1136 netifd_handle_iface(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1137 struct ubus_request_data
*req
, const char *method
,
1138 struct blob_attr
*msg
)
1140 struct interface
*iface
;
1141 struct blob_attr
*tb
;
1144 blobmsg_parse(&iface_policy
, 1, &tb
, blob_data(msg
), blob_len(msg
));
1146 return UBUS_STATUS_INVALID_ARGUMENT
;
1148 iface
= vlist_find(&interfaces
, blobmsg_data(tb
), iface
, node
);
1150 return UBUS_STATUS_NOT_FOUND
;
1152 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
1155 if (strcmp(method
, iface_object_methods
[i
].name
) != 0)
1158 cb
= iface_object_methods
[i
].handler
;
1159 return cb(ctx
, &iface
->ubus
, req
, method
, msg
);
1162 return UBUS_STATUS_INVALID_ARGUMENT
;
1165 static void netifd_add_iface_object(void)
1167 struct ubus_method
*methods
;
1170 methods
= calloc(1, sizeof(iface_object_methods
));
1174 memcpy(methods
, iface_object_methods
, sizeof(iface_object_methods
));
1175 iface_object
.methods
= methods
;
1177 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
1178 if (methods
[i
].handler
== netifd_handle_dump
)
1181 methods
[i
].handler
= netifd_handle_iface
;
1182 methods
[i
].policy
= &iface_policy
;
1183 methods
[i
].n_policy
= 1;
1185 netifd_add_object(&iface_object
);
1188 static struct wireless_device
*
1189 get_wdev(struct blob_attr
*msg
, int *ret
)
1191 struct blobmsg_policy wdev_policy
= {
1193 .type
= BLOBMSG_TYPE_STRING
,
1195 struct blob_attr
*dev_attr
;
1196 struct wireless_device
*wdev
= NULL
;
1199 blobmsg_parse(&wdev_policy
, 1, &dev_attr
, blob_data(msg
), blob_len(msg
));
1201 *ret
= UBUS_STATUS_INVALID_ARGUMENT
;
1205 wdev
= vlist_find(&wireless_devices
, blobmsg_data(dev_attr
), wdev
, node
);
1207 *ret
= UBUS_STATUS_NOT_FOUND
;
1216 netifd_handle_wdev_reconf(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1217 struct ubus_request_data
*req
, const char *method
,
1218 struct blob_attr
*msg
)
1220 struct wireless_device
*wdev
;
1223 wdev
= get_wdev(msg
, &ret
);
1224 if (ret
== UBUS_STATUS_NOT_FOUND
)
1228 wireless_device_reconf(wdev
);
1230 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1231 wireless_device_reconf(wdev
);
1238 netifd_handle_wdev_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1239 struct ubus_request_data
*req
, const char *method
,
1240 struct blob_attr
*msg
)
1242 struct wireless_device
*wdev
;
1245 wdev
= get_wdev(msg
, &ret
);
1246 if (ret
== UBUS_STATUS_NOT_FOUND
)
1250 wireless_device_set_up(wdev
);
1252 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1253 wireless_device_set_up(wdev
);
1260 netifd_handle_wdev_down(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
);
1268 if (ret
== UBUS_STATUS_NOT_FOUND
)
1272 wireless_device_set_down(wdev
);
1274 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1275 wireless_device_set_down(wdev
);
1282 netifd_handle_wdev_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1283 struct ubus_request_data
*req
, const char *method
,
1284 struct blob_attr
*msg
)
1286 struct wireless_device
*wdev
;
1289 wdev
= get_wdev(msg
, &ret
);
1290 if (ret
== UBUS_STATUS_NOT_FOUND
)
1293 blob_buf_init(&b
, 0);
1295 wireless_device_status(wdev
, &b
);
1297 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1298 wireless_device_status(wdev
, &b
);
1300 ubus_send_reply(ctx
, req
, b
.head
);
1305 netifd_handle_wdev_get_validate(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1306 struct ubus_request_data
*req
, const char *method
,
1307 struct blob_attr
*msg
)
1309 struct wireless_device
*wdev
;
1312 wdev
= get_wdev(msg
, &ret
);
1313 if (ret
== UBUS_STATUS_NOT_FOUND
)
1316 blob_buf_init(&b
, 0);
1318 wireless_device_get_validate(wdev
, &b
);
1320 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1321 wireless_device_get_validate(wdev
, &b
);
1323 ubus_send_reply(ctx
, req
, b
.head
);
1328 netifd_handle_wdev_notify(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1329 struct ubus_request_data
*req
, const char *method
,
1330 struct blob_attr
*msg
)
1332 struct wireless_device
*wdev
;
1335 wdev
= get_wdev(msg
, &ret
);
1339 return wireless_device_notify(wdev
, msg
, req
);
1342 static struct ubus_method wireless_object_methods
[] = {
1343 { .name
= "up", .handler
= netifd_handle_wdev_up
},
1344 { .name
= "down", .handler
= netifd_handle_wdev_down
},
1345 { .name
= "reconf", .handler
= netifd_handle_wdev_reconf
},
1346 { .name
= "status", .handler
= netifd_handle_wdev_status
},
1347 { .name
= "notify", .handler
= netifd_handle_wdev_notify
},
1348 { .name
= "get_validate", .handler
= netifd_handle_wdev_get_validate
},
1351 static struct ubus_object_type wireless_object_type
=
1352 UBUS_OBJECT_TYPE("netifd_iface", wireless_object_methods
);
1355 static struct ubus_object wireless_object
= {
1356 .name
= "network.wireless",
1357 .type
= &wireless_object_type
,
1358 .methods
= wireless_object_methods
,
1359 .n_methods
= ARRAY_SIZE(wireless_object_methods
),
1363 netifd_extdev_invoke(uint32_t id
, const char *method
, struct blob_attr
*msg
,
1364 ubus_data_handler_t data_cb
, void *data
)
1366 return ubus_invoke(ubus_ctx
, id
, method
, msg
, data_cb
, data
, 3000);
1370 netifd_ubus_init(const char *path
)
1374 ubus_ctx
= ubus_connect(path
);
1378 D(SYSTEM
, "connected as %08x", 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();
1387 udebug_ubus_init(&udebug
, ubus_ctx
, "netifd", netifd_udebug_config
);
1393 netifd_ubus_done(void)
1395 udebug_ubus_free(&udebug
);
1396 ubus_free(ubus_ctx
);
1400 netifd_ubus_interface_event(struct interface
*iface
, bool up
)
1402 blob_buf_init(&b
, 0);
1403 blobmsg_add_string(&b
, "action", up
? "ifup" : "ifdown");
1404 blobmsg_add_string(&b
, "interface", iface
->name
);
1405 ubus_send_event(ubus_ctx
, "network.interface", b
.head
);
1409 netifd_ubus_interface_notify(struct interface
*iface
, bool up
)
1411 const char *event
= (up
) ? "interface.update" : "interface.down";
1412 blob_buf_init(&b
, 0);
1413 blobmsg_add_string(&b
, "interface", iface
->name
);
1414 netifd_dump_status(iface
);
1415 ubus_notify(ubus_ctx
, &iface_object
, event
, b
.head
, -1);
1416 ubus_notify(ubus_ctx
, &iface
->ubus
, event
, b
.head
, -1);
1420 netifd_ubus_add_interface(struct interface
*iface
)
1422 struct ubus_object
*obj
= &iface
->ubus
;
1425 if (asprintf(&name
, "%s.interface.%s", main_object
.name
, iface
->name
) == -1)
1429 obj
->type
= &iface_object_type
;
1430 obj
->methods
= iface_object_methods
;
1431 obj
->n_methods
= ARRAY_SIZE(iface_object_methods
);
1432 if (ubus_add_object(ubus_ctx
, &iface
->ubus
)) {
1433 D(SYSTEM
, "failed to publish ubus object for interface '%s'", iface
->name
);
1440 netifd_ubus_remove_interface(struct interface
*iface
)
1442 if (!iface
->ubus
.name
)
1445 ubus_remove_object(ubus_ctx
, &iface
->ubus
);
1446 free((void *) iface
->ubus
.name
);