2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include <arpa/inet.h>
21 #include "interface.h"
27 struct ubus_context
*ubus_ctx
= NULL
;
28 static struct blob_buf b
;
29 static const char *ubus_path
;
34 netifd_handle_restart(struct ubus_context
*ctx
, struct ubus_object
*obj
,
35 struct ubus_request_data
*req
, const char *method
,
36 struct blob_attr
*msg
)
43 netifd_handle_reload(struct ubus_context
*ctx
, struct ubus_object
*obj
,
44 struct ubus_request_data
*req
, const char *method
,
45 struct blob_attr
*msg
)
48 return UBUS_STATUS_NOT_FOUND
;
50 return UBUS_STATUS_OK
;
60 static const struct blobmsg_policy route_policy
[__HR_MAX
] = {
61 [HR_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
62 [HR_V6
] = { .name
= "v6", .type
= BLOBMSG_TYPE_BOOL
},
63 [HR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
67 netifd_add_host_route(struct ubus_context
*ctx
, struct ubus_object
*obj
,
68 struct ubus_request_data
*req
, const char *method
,
69 struct blob_attr
*msg
)
71 struct blob_attr
*tb
[__HR_MAX
];
72 struct interface
*iface
= NULL
;
76 blobmsg_parse(route_policy
, __HR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
78 return UBUS_STATUS_INVALID_ARGUMENT
;
81 v6
= blobmsg_get_bool(tb
[HR_V6
]);
84 iface
= vlist_find(&interfaces
, blobmsg_data(tb
[HR_INTERFACE
]), iface
, node
);
86 memset(&a
, 0, sizeof(a
));
87 if (!inet_pton(v6
? AF_INET6
: AF_INET
, blobmsg_data(tb
[HR_TARGET
]), &a
))
88 return UBUS_STATUS_INVALID_ARGUMENT
;
91 iface
= interface_ip_add_target_route(&a
, v6
, iface
);
93 return UBUS_STATUS_NOT_FOUND
;
96 blobmsg_add_string(&b
, "interface", iface
->name
);
97 ubus_send_reply(ctx
, req
, b
.head
);
103 netifd_get_proto_handlers(struct ubus_context
*ctx
, struct ubus_object
*obj
,
104 struct ubus_request_data
*req
, const char *method
,
105 struct blob_attr
*msg
)
107 blob_buf_init(&b
, 0);
108 proto_dump_handlers(&b
);
109 ubus_send_reply(ctx
, req
, b
.head
);
120 static const struct blobmsg_policy dynamic_policy
[__DI_MAX
] = {
121 [DI_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
125 netifd_add_dynamic(struct ubus_context
*ctx
, struct ubus_object
*obj
,
126 struct ubus_request_data
*req
, const char *method
,
127 struct blob_attr
*msg
)
129 struct blob_attr
*tb
[__DI_MAX
];
130 struct interface
*iface
;
131 struct blob_attr
*config
;
133 blobmsg_parse(dynamic_policy
, __DI_MAX
, tb
, blob_data(msg
), blob_len(msg
));
136 return UBUS_STATUS_INVALID_ARGUMENT
;
138 const char *name
= blobmsg_get_string(tb
[DI_NAME
]);
140 iface
= interface_alloc(name
, msg
, true);
142 return UBUS_STATUS_UNKNOWN_ERROR
;
144 config
= blob_memdup(msg
);
148 if (!interface_add(iface
, config
))
151 return UBUS_STATUS_OK
;
156 return UBUS_STATUS_UNKNOWN_ERROR
;
159 static struct ubus_method main_object_methods
[] = {
160 { .name
= "restart", .handler
= netifd_handle_restart
},
161 { .name
= "reload", .handler
= netifd_handle_reload
},
162 UBUS_METHOD("add_host_route", netifd_add_host_route
, route_policy
),
163 { .name
= "get_proto_handlers", .handler
= netifd_get_proto_handlers
},
164 UBUS_METHOD("add_dynamic", netifd_add_dynamic
, dynamic_policy
),
167 static struct ubus_object_type main_object_type
=
168 UBUS_OBJECT_TYPE("netifd", main_object_methods
);
170 static struct ubus_object main_object
= {
172 .type
= &main_object_type
,
173 .methods
= main_object_methods
,
174 .n_methods
= ARRAY_SIZE(main_object_methods
),
182 static const struct blobmsg_policy dev_policy
[__DEV_MAX
] = {
183 [DEV_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
187 netifd_dev_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
188 struct ubus_request_data
*req
, const char *method
,
189 struct blob_attr
*msg
)
191 struct device
*dev
= NULL
;
192 struct blob_attr
*tb
[__DEV_MAX
];
194 blobmsg_parse(dev_policy
, __DEV_MAX
, tb
, blob_data(msg
), blob_len(msg
));
197 dev
= device_find(blobmsg_data(tb
[DEV_NAME
]));
199 return UBUS_STATUS_INVALID_ARGUMENT
;
202 blob_buf_init(&b
, 0);
203 device_dump_status(&b
, dev
);
204 ubus_send_reply(ctx
, req
, b
.head
);
215 static const struct blobmsg_policy alias_attrs
[__ALIAS_ATTR_MAX
] = {
216 [ALIAS_ATTR_ALIAS
] = { "alias", BLOBMSG_TYPE_ARRAY
},
217 [ALIAS_ATTR_DEV
] = { "device", BLOBMSG_TYPE_STRING
},
221 netifd_handle_alias(struct ubus_context
*ctx
, struct ubus_object
*obj
,
222 struct ubus_request_data
*req
, const char *method
,
223 struct blob_attr
*msg
)
225 struct device
*dev
= NULL
;
226 struct blob_attr
*tb
[__ALIAS_ATTR_MAX
];
227 struct blob_attr
*cur
;
230 blobmsg_parse(alias_attrs
, __ALIAS_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
232 if (!tb
[ALIAS_ATTR_ALIAS
])
233 return UBUS_STATUS_INVALID_ARGUMENT
;
235 if ((cur
= tb
[ALIAS_ATTR_DEV
]) != NULL
) {
236 dev
= device_get(blobmsg_data(cur
), true);
238 return UBUS_STATUS_NOT_FOUND
;
241 blobmsg_for_each_attr(cur
, tb
[ALIAS_ATTR_ALIAS
], rem
) {
242 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
245 if (!blobmsg_check_attr(cur
, NULL
))
248 alias_notify_device(blobmsg_data(cur
), dev
);
253 device_free_unused(dev
);
254 return UBUS_STATUS_INVALID_ARGUMENT
;
263 static const struct blobmsg_policy dev_state_policy
[__DEV_STATE_MAX
] = {
264 [DEV_STATE_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
265 [DEV_STATE_DEFER
] = { .name
= "defer", .type
= BLOBMSG_TYPE_BOOL
},
269 netifd_handle_set_state(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
[__DEV_STATE_MAX
];
275 struct blob_attr
*cur
;
277 blobmsg_parse(dev_state_policy
, __DEV_STATE_MAX
, tb
, blob_data(msg
), blob_len(msg
));
279 cur
= tb
[DEV_STATE_NAME
];
281 return UBUS_STATUS_INVALID_ARGUMENT
;
283 dev
= device_find(blobmsg_data(cur
));
285 return UBUS_STATUS_NOT_FOUND
;
287 cur
= tb
[DEV_STATE_DEFER
];
289 device_set_deferred(dev
, !!blobmsg_get_u8(cur
));
294 static struct ubus_method dev_object_methods
[] = {
295 UBUS_METHOD("status", netifd_dev_status
, dev_policy
),
296 UBUS_METHOD("set_alias", netifd_handle_alias
, alias_attrs
),
297 UBUS_METHOD("set_state", netifd_handle_set_state
, dev_state_policy
),
300 static struct ubus_object_type dev_object_type
=
301 UBUS_OBJECT_TYPE("device", dev_object_methods
);
303 static struct ubus_object dev_object
= {
304 .name
= "network.device",
305 .type
= &dev_object_type
,
306 .methods
= dev_object_methods
,
307 .n_methods
= ARRAY_SIZE(dev_object_methods
),
311 netifd_ubus_add_fd(void)
313 ubus_add_uloop(ubus_ctx
);
314 system_fd_set_cloexec(ubus_ctx
->sock
.fd
);
318 netifd_ubus_reconnect_timer(struct uloop_timeout
*timeout
)
320 static struct uloop_timeout retry
= {
321 .cb
= netifd_ubus_reconnect_timer
,
325 if (ubus_reconnect(ubus_ctx
, ubus_path
) != 0) {
326 DPRINTF("failed to reconnect, trying again in %d seconds\n", t
);
327 uloop_timeout_set(&retry
, t
* 1000);
331 DPRINTF("reconnected to ubus, new id: %08x\n", ubus_ctx
->local_id
);
332 netifd_ubus_add_fd();
336 netifd_ubus_connection_lost(struct ubus_context
*ctx
)
338 netifd_ubus_reconnect_timer(NULL
);
341 /* per-interface object */
344 netifd_handle_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
345 struct ubus_request_data
*req
, const char *method
,
346 struct blob_attr
*msg
)
348 struct interface
*iface
;
350 iface
= container_of(obj
, struct interface
, ubus
);
351 interface_set_up(iface
);
357 netifd_handle_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
358 struct ubus_request_data
*req
, const char *method
,
359 struct blob_attr
*msg
)
361 struct interface
*iface
;
363 iface
= container_of(obj
, struct interface
, ubus
);
364 interface_set_down(iface
);
370 netifd_handle_renew(struct ubus_context
*ctx
, struct ubus_object
*obj
,
371 struct ubus_request_data
*req
, const char *method
,
372 struct blob_attr
*msg
)
374 struct interface
*iface
;
376 iface
= container_of(obj
, struct interface
, ubus
);
377 interface_renew(iface
);
383 netifd_add_interface_errors(struct blob_buf
*b
, struct interface
*iface
)
385 struct interface_error
*error
;
389 e
= blobmsg_open_array(b
, "errors");
390 list_for_each_entry(error
, &iface
->errors
, list
) {
391 e2
= blobmsg_open_table(b
, NULL
);
393 blobmsg_add_string(b
, "subsystem", error
->subsystem
);
394 blobmsg_add_string(b
, "code", error
->code
);
395 if (error
->data
[0]) {
396 e3
= blobmsg_open_array(b
, "data");
397 for (i
= 0; error
->data
[i
]; i
++)
398 blobmsg_add_string(b
, NULL
, error
->data
[i
]);
399 blobmsg_close_array(b
, e3
);
402 blobmsg_close_table(b
, e2
);
404 blobmsg_close_array(b
, e
);
408 interface_ip_dump_address_list(struct interface_ip_settings
*ip
, bool v6
, bool enabled
)
410 struct device_addr
*addr
;
416 time_t now
= system_get_rtime();
417 vlist_for_each_element(&ip
->addr
, addr
, node
) {
418 if (addr
->enabled
!= enabled
)
421 if ((addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
426 if (af
!= (v6
? AF_INET6
: AF_INET
))
429 a
= blobmsg_open_table(&b
, NULL
);
431 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
432 inet_ntop(af
, &addr
->addr
, buf
, buflen
);
433 blobmsg_add_string_buffer(&b
);
435 blobmsg_add_u32(&b
, "mask", addr
->mask
);
437 if (addr
->point_to_point
) {
438 buf
= blobmsg_alloc_string_buffer(&b
, "ptpaddress", buflen
);
439 inet_ntop(af
, &addr
->point_to_point
, buf
, buflen
);
440 blobmsg_add_string_buffer(&b
);
443 if (addr
->preferred_until
) {
444 int preferred
= addr
->preferred_until
- now
;
447 blobmsg_add_u32(&b
, "preferred", preferred
);
450 if (addr
->valid_until
)
451 blobmsg_add_u32(&b
, "valid", addr
->valid_until
- now
);
454 blobmsg_add_string(&b
, "class", addr
->pclass
);
456 blobmsg_close_table(&b
, a
);
461 interface_ip_dump_route_list(struct interface_ip_settings
*ip
, bool enabled
)
463 struct device_route
*route
;
469 time_t now
= system_get_rtime();
470 vlist_for_each_element(&ip
->route
, route
, node
) {
471 if (route
->enabled
!= enabled
)
474 if ((ip
->no_defaultroute
== enabled
) && !route
->mask
)
477 if ((route
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
482 r
= blobmsg_open_table(&b
, NULL
);
484 buf
= blobmsg_alloc_string_buffer(&b
, "target", buflen
);
485 inet_ntop(af
, &route
->addr
, buf
, buflen
);
486 blobmsg_add_string_buffer(&b
);
488 blobmsg_add_u32(&b
, "mask", route
->mask
);
490 buf
= blobmsg_alloc_string_buffer(&b
, "nexthop", buflen
);
491 inet_ntop(af
, &route
->nexthop
, buf
, buflen
);
492 blobmsg_add_string_buffer(&b
);
494 if (route
->flags
& DEVROUTE_TYPE
)
495 blobmsg_add_u32(&b
, "type", route
->type
);
497 if (route
->flags
& DEVROUTE_PROTO
)
498 blobmsg_add_u32(&b
, "proto", route
->proto
);
500 if (route
->flags
& DEVROUTE_MTU
)
501 blobmsg_add_u32(&b
, "mtu", route
->mtu
);
503 if (route
->flags
& DEVROUTE_METRIC
)
504 blobmsg_add_u32(&b
, "metric", route
->metric
);
506 if (route
->flags
& DEVROUTE_TABLE
)
507 blobmsg_add_u32(&b
, "table", route
->table
);
509 if (route
->valid_until
)
510 blobmsg_add_u32(&b
, "valid", route
->valid_until
- now
);
512 buf
= blobmsg_alloc_string_buffer(&b
, "source", buflen
);
513 inet_ntop(af
, &route
->source
, buf
, buflen
);
514 snprintf(buf
+ strlen(buf
), buflen
- strlen(buf
), "/%u", route
->sourcemask
);
515 blobmsg_add_string_buffer(&b
);
517 blobmsg_close_table(&b
, r
);
523 interface_ip_dump_prefix_list(struct interface_ip_settings
*ip
)
525 struct device_prefix
*prefix
;
528 const int buflen
= INET6_ADDRSTRLEN
;
530 time_t now
= system_get_rtime();
531 vlist_for_each_element(&ip
->prefix
, prefix
, node
) {
532 a
= blobmsg_open_table(&b
, NULL
);
534 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
535 inet_ntop(AF_INET6
, &prefix
->addr
, buf
, buflen
);
536 blobmsg_add_string_buffer(&b
);
538 blobmsg_add_u32(&b
, "mask", prefix
->length
);
540 if (prefix
->preferred_until
) {
541 int preferred
= prefix
->preferred_until
- now
;
544 blobmsg_add_u32(&b
, "preferred", preferred
);
547 if (prefix
->valid_until
)
548 blobmsg_add_u32(&b
, "valid", prefix
->valid_until
- now
);
550 blobmsg_add_string(&b
, "class", prefix
->pclass
);
552 c
= blobmsg_open_table(&b
, "assigned");
553 struct device_prefix_assignment
*assign
;
554 list_for_each_entry(assign
, &prefix
->assignments
, head
) {
555 if (!assign
->name
[0])
558 struct in6_addr addr
= prefix
->addr
;
559 addr
.s6_addr32
[1] |= htonl(assign
->assigned
);
561 void *d
= blobmsg_open_table(&b
, assign
->name
);
563 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
564 inet_ntop(AF_INET6
, &addr
, buf
, buflen
);
565 blobmsg_add_string_buffer(&b
);
567 blobmsg_add_u32(&b
, "mask", assign
->length
);
569 blobmsg_close_table(&b
, d
);
571 blobmsg_close_table(&b
, c
);
573 blobmsg_close_table(&b
, a
);
579 interface_ip_dump_prefix_assignment_list(struct interface
*iface
)
583 const int buflen
= INET6_ADDRSTRLEN
;
584 time_t now
= system_get_rtime();
586 struct device_prefix
*prefix
;
587 list_for_each_entry(prefix
, &prefixes
, head
) {
588 struct device_prefix_assignment
*assign
;
589 list_for_each_entry(assign
, &prefix
->assignments
, head
) {
590 if (strcmp(assign
->name
, iface
->name
))
593 struct in6_addr addr
= prefix
->addr
;
594 addr
.s6_addr32
[1] |= htonl(assign
->assigned
);
596 a
= blobmsg_open_table(&b
, NULL
);
598 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
599 inet_ntop(AF_INET6
, &addr
, buf
, buflen
);
600 blobmsg_add_string_buffer(&b
);
602 blobmsg_add_u32(&b
, "mask", assign
->length
);
604 if (prefix
->preferred_until
) {
605 int preferred
= prefix
->preferred_until
- now
;
608 blobmsg_add_u32(&b
, "preferred", preferred
);
611 if (prefix
->valid_until
)
612 blobmsg_add_u32(&b
, "valid", prefix
->valid_until
- now
);
614 void *c
= blobmsg_open_table(&b
, "local-address");
615 if (assign
->enabled
) {
616 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
617 inet_ntop(AF_INET6
, &assign
->addr
, buf
, buflen
);
618 blobmsg_add_string_buffer(&b
);
620 blobmsg_add_u32(&b
, "mask", assign
->length
);
622 blobmsg_close_table(&b
, c
);
624 blobmsg_close_table(&b
, a
);
630 interface_ip_dump_dns_server_list(struct interface_ip_settings
*ip
, bool enabled
)
632 struct dns_server
*dns
;
636 vlist_simple_for_each_element(&ip
->dns_servers
, dns
, node
) {
637 if (ip
->no_dns
== enabled
)
640 buf
= blobmsg_alloc_string_buffer(&b
, NULL
, buflen
);
641 inet_ntop(dns
->af
, &dns
->addr
, buf
, buflen
);
642 blobmsg_add_string_buffer(&b
);
647 interface_ip_dump_dns_search_list(struct interface_ip_settings
*ip
, bool enabled
)
649 struct dns_search_domain
*dns
;
651 vlist_simple_for_each_element(&ip
->dns_search
, dns
, node
) {
652 if (ip
->no_dns
== enabled
)
655 blobmsg_add_string(&b
, NULL
, dns
->name
);
660 netifd_dump_status(struct interface
*iface
)
662 struct interface_data
*data
;
666 blobmsg_add_u8(&b
, "up", iface
->state
== IFS_UP
);
667 blobmsg_add_u8(&b
, "pending", iface
->state
== IFS_SETUP
);
668 blobmsg_add_u8(&b
, "available", iface
->available
);
669 blobmsg_add_u8(&b
, "autostart", iface
->autostart
);
670 blobmsg_add_u8(&b
, "dynamic", iface
->dynamic
);
672 if (iface
->state
== IFS_UP
) {
673 time_t cur
= system_get_rtime();
674 blobmsg_add_u32(&b
, "uptime", cur
- iface
->start_time
);
675 if (iface
->l3_dev
.dev
)
676 blobmsg_add_string(&b
, "l3_device", iface
->l3_dev
.dev
->ifname
);
679 if (iface
->proto_handler
)
680 blobmsg_add_string(&b
, "proto", iface
->proto_handler
->name
);
682 dev
= iface
->main_dev
.dev
;
683 if (dev
&& !dev
->hidden
&& iface
->proto_handler
&&
684 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
))
685 blobmsg_add_string(&b
, "device", dev
->ifname
);
687 if (iface
->state
== IFS_UP
) {
688 if (iface
->updated
) {
689 a
= blobmsg_open_array(&b
, "updated");
691 if (iface
->updated
& IUF_ADDRESS
)
692 blobmsg_add_string(&b
, NULL
, "addresses");
693 if (iface
->updated
& IUF_ROUTE
)
694 blobmsg_add_string(&b
, NULL
, "routes");
695 if (iface
->updated
& IUF_PREFIX
)
696 blobmsg_add_string(&b
, NULL
, "prefixes");
697 if (iface
->updated
& IUF_DATA
)
698 blobmsg_add_string(&b
, NULL
, "data");
700 blobmsg_close_array(&b
, a
);
704 blobmsg_add_u32(&b
, "ip4table", iface
->ip4table
);
706 blobmsg_add_u32(&b
, "ip6table", iface
->ip6table
);
707 blobmsg_add_u32(&b
, "metric", iface
->metric
);
708 blobmsg_add_u32(&b
, "dns_metric", iface
->dns_metric
);
709 blobmsg_add_u8(&b
, "delegation", !iface
->proto_ip
.no_delegation
);
710 if (iface
->assignment_weight
)
711 blobmsg_add_u32(&b
, "ip6weight", iface
->assignment_weight
);
712 a
= blobmsg_open_array(&b
, "ipv4-address");
713 interface_ip_dump_address_list(&iface
->config_ip
, false, true);
714 interface_ip_dump_address_list(&iface
->proto_ip
, false, true);
715 blobmsg_close_array(&b
, a
);
716 a
= blobmsg_open_array(&b
, "ipv6-address");
717 interface_ip_dump_address_list(&iface
->config_ip
, true, true);
718 interface_ip_dump_address_list(&iface
->proto_ip
, true, true);
719 blobmsg_close_array(&b
, a
);
720 a
= blobmsg_open_array(&b
, "ipv6-prefix");
721 interface_ip_dump_prefix_list(&iface
->config_ip
);
722 interface_ip_dump_prefix_list(&iface
->proto_ip
);
723 blobmsg_close_array(&b
, a
);
724 a
= blobmsg_open_array(&b
, "ipv6-prefix-assignment");
725 interface_ip_dump_prefix_assignment_list(iface
);
726 blobmsg_close_array(&b
, a
);
727 a
= blobmsg_open_array(&b
, "route");
728 interface_ip_dump_route_list(&iface
->config_ip
, true);
729 interface_ip_dump_route_list(&iface
->proto_ip
, true);
730 blobmsg_close_array(&b
, a
);
731 a
= blobmsg_open_array(&b
, "dns-server");
732 interface_ip_dump_dns_server_list(&iface
->config_ip
, true);
733 interface_ip_dump_dns_server_list(&iface
->proto_ip
, true);
734 blobmsg_close_array(&b
, a
);
735 a
= blobmsg_open_array(&b
, "dns-search");
736 interface_ip_dump_dns_search_list(&iface
->config_ip
, true);
737 interface_ip_dump_dns_search_list(&iface
->proto_ip
, true);
738 blobmsg_close_array(&b
, a
);
740 inactive
= blobmsg_open_table(&b
, "inactive");
741 a
= blobmsg_open_array(&b
, "ipv4-address");
742 interface_ip_dump_address_list(&iface
->config_ip
, false, false);
743 interface_ip_dump_address_list(&iface
->proto_ip
, false, false);
744 blobmsg_close_array(&b
, a
);
745 a
= blobmsg_open_array(&b
, "ipv6-address");
746 interface_ip_dump_address_list(&iface
->config_ip
, true, false);
747 interface_ip_dump_address_list(&iface
->proto_ip
, true, false);
748 blobmsg_close_array(&b
, a
);
749 a
= blobmsg_open_array(&b
, "route");
750 interface_ip_dump_route_list(&iface
->config_ip
, false);
751 interface_ip_dump_route_list(&iface
->proto_ip
, false);
752 blobmsg_close_array(&b
, a
);
753 a
= blobmsg_open_array(&b
, "dns-server");
754 interface_ip_dump_dns_server_list(&iface
->config_ip
, false);
755 interface_ip_dump_dns_server_list(&iface
->proto_ip
, false);
756 blobmsg_close_array(&b
, a
);
757 a
= blobmsg_open_array(&b
, "dns-search");
758 interface_ip_dump_dns_search_list(&iface
->config_ip
, false);
759 interface_ip_dump_dns_search_list(&iface
->proto_ip
, false);
760 blobmsg_close_array(&b
, a
);
761 blobmsg_close_table(&b
, inactive
);
764 a
= blobmsg_open_table(&b
, "data");
765 avl_for_each_element(&iface
->data
, data
, node
)
766 blobmsg_add_blob(&b
, data
->data
);
768 blobmsg_close_table(&b
, a
);
770 if (!list_empty(&iface
->errors
))
771 netifd_add_interface_errors(&b
, iface
);
775 netifd_handle_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
776 struct ubus_request_data
*req
, const char *method
,
777 struct blob_attr
*msg
)
779 struct interface
*iface
= container_of(obj
, struct interface
, ubus
);
781 blob_buf_init(&b
, 0);
782 netifd_dump_status(iface
);
783 ubus_send_reply(ctx
, req
, b
.head
);
790 netifd_handle_dump(struct ubus_context
*ctx
, struct ubus_object
*obj
,
791 struct ubus_request_data
*req
, const char *method
,
792 struct blob_attr
*msg
)
794 blob_buf_init(&b
, 0);
795 void *a
= blobmsg_open_array(&b
, "interface");
797 struct interface
*iface
;
798 vlist_for_each_element(&interfaces
, iface
, node
) {
799 void *i
= blobmsg_open_table(&b
, NULL
);
800 blobmsg_add_string(&b
, "interface", iface
->name
);
801 netifd_dump_status(iface
);
802 blobmsg_close_table(&b
, i
);
805 blobmsg_close_array(&b
, a
);
806 ubus_send_reply(ctx
, req
, b
.head
);
817 static const struct blobmsg_policy dev_link_policy
[__DEV_LINK_MAX
] = {
818 [DEV_LINK_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
819 [DEV_LINK_EXT
] = { .name
= "link-ext", .type
= BLOBMSG_TYPE_BOOL
},
823 netifd_iface_handle_device(struct ubus_context
*ctx
, struct ubus_object
*obj
,
824 struct ubus_request_data
*req
, const char *method
,
825 struct blob_attr
*msg
)
827 struct blob_attr
*tb
[__DEV_LINK_MAX
];
828 struct blob_attr
*cur
;
829 struct interface
*iface
;
830 bool add
= !strncmp(method
, "add", 3);
831 bool link_ext
= true;
833 iface
= container_of(obj
, struct interface
, ubus
);
835 blobmsg_parse(dev_link_policy
, __DEV_LINK_MAX
, tb
, blob_data(msg
), blob_len(msg
));
837 if (!tb
[DEV_LINK_NAME
])
838 return UBUS_STATUS_INVALID_ARGUMENT
;
840 cur
= tb
[DEV_LINK_EXT
];
842 link_ext
= blobmsg_get_bool(cur
);
844 return interface_handle_link(iface
, blobmsg_data(tb
[DEV_LINK_NAME
]), add
, link_ext
);
849 netifd_iface_notify_proto(struct ubus_context
*ctx
, struct ubus_object
*obj
,
850 struct ubus_request_data
*req
, const char *method
,
851 struct blob_attr
*msg
)
853 struct interface
*iface
;
855 iface
= container_of(obj
, struct interface
, ubus
);
857 if (!iface
->proto
|| !iface
->proto
->notify
)
858 return UBUS_STATUS_NOT_SUPPORTED
;
860 return iface
->proto
->notify(iface
->proto
, msg
);
864 netifd_iface_do_remove(struct uloop_timeout
*timeout
)
866 struct interface
*iface
;
868 iface
= container_of(timeout
, struct interface
, remove_timer
);
869 vlist_delete(&interfaces
, &iface
->node
);
873 netifd_iface_remove(struct ubus_context
*ctx
, struct ubus_object
*obj
,
874 struct ubus_request_data
*req
, const char *method
,
875 struct blob_attr
*msg
)
877 struct interface
*iface
;
879 iface
= container_of(obj
, struct interface
, ubus
);
880 if (iface
->remove_timer
.cb
)
881 return UBUS_STATUS_INVALID_ARGUMENT
;
883 iface
->remove_timer
.cb
= netifd_iface_do_remove
;
884 uloop_timeout_set(&iface
->remove_timer
, 100);
889 netifd_handle_iface_prepare(struct ubus_context
*ctx
, struct ubus_object
*obj
,
890 struct ubus_request_data
*req
, const char *method
,
891 struct blob_attr
*msg
)
893 struct interface
*iface
;
895 const struct device_hotplug_ops
*ops
;
897 iface
= container_of(obj
, struct interface
, ubus
);
898 dev
= iface
->main_dev
.dev
;
902 ops
= dev
->hotplug_ops
;
906 return ops
->prepare(dev
);
910 netifd_handle_set_data(struct ubus_context
*ctx
, struct ubus_object
*obj
,
911 struct ubus_request_data
*req
, const char *method
,
912 struct blob_attr
*msg
)
914 struct interface
*iface
;
916 iface
= container_of(obj
, struct interface
, ubus
);
918 return interface_parse_data(iface
, msg
);
921 static struct ubus_method iface_object_methods
[] = {
922 { .name
= "up", .handler
= netifd_handle_up
},
923 { .name
= "down", .handler
= netifd_handle_down
},
924 { .name
= "renew", .handler
= netifd_handle_renew
},
925 { .name
= "status", .handler
= netifd_handle_status
},
926 { .name
= "prepare", .handler
= netifd_handle_iface_prepare
},
927 { .name
= "dump", .handler
= netifd_handle_dump
},
928 UBUS_METHOD("add_device", netifd_iface_handle_device
, dev_link_policy
),
929 UBUS_METHOD("remove_device", netifd_iface_handle_device
, dev_link_policy
),
930 { .name
= "notify_proto", .handler
= netifd_iface_notify_proto
},
931 { .name
= "remove", .handler
= netifd_iface_remove
},
932 { .name
= "set_data", .handler
= netifd_handle_set_data
},
935 static struct ubus_object_type iface_object_type
=
936 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods
);
939 static struct ubus_object iface_object
= {
940 .name
= "network.interface",
941 .type
= &iface_object_type
,
942 .n_methods
= ARRAY_SIZE(iface_object_methods
),
945 static void netifd_add_object(struct ubus_object
*obj
)
947 int ret
= ubus_add_object(ubus_ctx
, obj
);
950 fprintf(stderr
, "Failed to publish object '%s': %s\n", obj
->name
, ubus_strerror(ret
));
953 static const struct blobmsg_policy iface_policy
= {
955 .type
= BLOBMSG_TYPE_STRING
,
959 netifd_handle_iface(struct ubus_context
*ctx
, struct ubus_object
*obj
,
960 struct ubus_request_data
*req
, const char *method
,
961 struct blob_attr
*msg
)
963 struct interface
*iface
;
964 struct blob_attr
*tb
;
967 blobmsg_parse(&iface_policy
, 1, &tb
, blob_data(msg
), blob_len(msg
));
969 return UBUS_STATUS_INVALID_ARGUMENT
;
971 iface
= vlist_find(&interfaces
, blobmsg_data(tb
), iface
, node
);
973 return UBUS_STATUS_NOT_FOUND
;
975 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
978 if (strcmp(method
, iface_object_methods
[i
].name
) != 0)
981 cb
= iface_object_methods
[i
].handler
;
982 return cb(ctx
, &iface
->ubus
, req
, method
, msg
);
985 return UBUS_STATUS_INVALID_ARGUMENT
;
988 static void netifd_add_iface_object(void)
990 struct ubus_method
*methods
;
993 methods
= calloc(1, sizeof(iface_object_methods
));
997 memcpy(methods
, iface_object_methods
, sizeof(iface_object_methods
));
998 iface_object
.methods
= methods
;
1000 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
1001 if (methods
[i
].handler
== netifd_handle_dump
)
1004 methods
[i
].handler
= netifd_handle_iface
;
1005 methods
[i
].policy
= &iface_policy
;
1006 methods
[i
].n_policy
= 1;
1008 netifd_add_object(&iface_object
);
1011 static struct wireless_device
*
1012 get_wdev(struct blob_attr
*msg
, int *ret
)
1014 struct blobmsg_policy wdev_policy
= {
1016 .type
= BLOBMSG_TYPE_STRING
,
1018 struct blob_attr
*dev_attr
;
1019 struct wireless_device
*wdev
= NULL
;
1022 blobmsg_parse(&wdev_policy
, 1, &dev_attr
, blob_data(msg
), blob_len(msg
));
1024 *ret
= UBUS_STATUS_INVALID_ARGUMENT
;
1028 wdev
= vlist_find(&wireless_devices
, blobmsg_data(dev_attr
), wdev
, node
);
1030 *ret
= UBUS_STATUS_NOT_FOUND
;
1039 netifd_handle_wdev_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1040 struct ubus_request_data
*req
, const char *method
,
1041 struct blob_attr
*msg
)
1043 struct wireless_device
*wdev
;
1046 wdev
= get_wdev(msg
, &ret
);
1047 if (ret
== UBUS_STATUS_NOT_FOUND
)
1051 wireless_device_set_up(wdev
);
1053 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1054 wireless_device_set_up(wdev
);
1061 netifd_handle_wdev_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1062 struct ubus_request_data
*req
, const char *method
,
1063 struct blob_attr
*msg
)
1065 struct wireless_device
*wdev
;
1068 wdev
= get_wdev(msg
, &ret
);
1069 if (ret
== UBUS_STATUS_NOT_FOUND
)
1073 wireless_device_set_down(wdev
);
1075 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1076 wireless_device_set_down(wdev
);
1083 netifd_handle_wdev_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1084 struct ubus_request_data
*req
, const char *method
,
1085 struct blob_attr
*msg
)
1087 struct wireless_device
*wdev
;
1090 wdev
= get_wdev(msg
, &ret
);
1091 if (ret
== UBUS_STATUS_NOT_FOUND
)
1094 blob_buf_init(&b
, 0);
1096 wireless_device_status(wdev
, &b
);
1098 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1099 wireless_device_status(wdev
, &b
);
1101 ubus_send_reply(ctx
, req
, b
.head
);
1106 netifd_handle_wdev_get_validate(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1107 struct ubus_request_data
*req
, const char *method
,
1108 struct blob_attr
*msg
)
1110 struct wireless_device
*wdev
;
1113 wdev
= get_wdev(msg
, &ret
);
1114 if (ret
== UBUS_STATUS_NOT_FOUND
)
1117 blob_buf_init(&b
, 0);
1119 wireless_device_get_validate(wdev
, &b
);
1121 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1122 wireless_device_get_validate(wdev
, &b
);
1124 ubus_send_reply(ctx
, req
, b
.head
);
1129 netifd_handle_wdev_notify(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1130 struct ubus_request_data
*req
, const char *method
,
1131 struct blob_attr
*msg
)
1133 struct wireless_device
*wdev
;
1136 wdev
= get_wdev(msg
, &ret
);
1140 return wireless_device_notify(wdev
, msg
, req
);
1143 static struct ubus_method wireless_object_methods
[] = {
1144 { .name
= "up", .handler
= netifd_handle_wdev_up
},
1145 { .name
= "down", .handler
= netifd_handle_wdev_down
},
1146 { .name
= "status", .handler
= netifd_handle_wdev_status
},
1147 { .name
= "notify", .handler
= netifd_handle_wdev_notify
},
1148 { .name
= "get_validate", .handler
= netifd_handle_wdev_get_validate
},
1151 static struct ubus_object_type wireless_object_type
=
1152 UBUS_OBJECT_TYPE("netifd_iface", wireless_object_methods
);
1155 static struct ubus_object wireless_object
= {
1156 .name
= "network.wireless",
1157 .type
= &wireless_object_type
,
1158 .methods
= wireless_object_methods
,
1159 .n_methods
= ARRAY_SIZE(wireless_object_methods
),
1163 netifd_ubus_init(const char *path
)
1168 ubus_ctx
= ubus_connect(path
);
1172 DPRINTF("connected as %08x\n", ubus_ctx
->local_id
);
1173 ubus_ctx
->connection_lost
= netifd_ubus_connection_lost
;
1174 netifd_ubus_add_fd();
1176 netifd_add_object(&main_object
);
1177 netifd_add_object(&dev_object
);
1178 netifd_add_object(&wireless_object
);
1179 netifd_add_iface_object();
1185 netifd_ubus_done(void)
1187 ubus_free(ubus_ctx
);
1191 netifd_ubus_interface_event(struct interface
*iface
, bool up
)
1193 blob_buf_init(&b
, 0);
1194 blobmsg_add_string(&b
, "action", up
? "ifup" : "ifdown");
1195 blobmsg_add_string(&b
, "interface", iface
->name
);
1196 ubus_send_event(ubus_ctx
, "network.interface", b
.head
);
1200 netifd_ubus_interface_notify(struct interface
*iface
, bool up
)
1202 const char *event
= (up
) ? "interface.update" : "interface.down";
1203 blob_buf_init(&b
, 0);
1204 blobmsg_add_string(&b
, "interface", iface
->name
);
1205 netifd_dump_status(iface
);
1206 ubus_notify(ubus_ctx
, &iface_object
, event
, b
.head
, -1);
1207 ubus_notify(ubus_ctx
, &iface
->ubus
, event
, b
.head
, -1);
1211 netifd_ubus_add_interface(struct interface
*iface
)
1213 struct ubus_object
*obj
= &iface
->ubus
;
1216 if (asprintf(&name
, "%s.interface.%s", main_object
.name
, iface
->name
) == -1)
1220 obj
->type
= &iface_object_type
;
1221 obj
->methods
= iface_object_methods
;
1222 obj
->n_methods
= ARRAY_SIZE(iface_object_methods
);
1223 if (ubus_add_object(ubus_ctx
, &iface
->ubus
)) {
1224 DPRINTF("failed to publish ubus object for interface '%s'\n", iface
->name
);
1231 netifd_ubus_remove_interface(struct interface
*iface
)
1233 if (!iface
->ubus
.name
)
1236 ubus_remove_object(ubus_ctx
, &iface
->ubus
);
1237 free((void *) iface
->ubus
.name
);