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_UNKNOWN_ERROR
;
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
;
134 blobmsg_parse(dynamic_policy
, __DI_MAX
, tb
, blob_data(msg
), blob_len(msg
));
137 return UBUS_STATUS_INVALID_ARGUMENT
;
139 const char *name
= blobmsg_get_string(tb
[DI_NAME
]);
141 iface
= interface_alloc(name
, msg
);
143 return UBUS_STATUS_UNKNOWN_ERROR
;
145 config
= blob_memdup(msg
);
149 interface_add(iface
, config
);
151 // need to look up the interface name again, in case of config update
152 // the pointer will have changed
153 iface
= vlist_find(&interfaces
, name
, iface
, node
);
155 return UBUS_STATUS_UNKNOWN_ERROR
;
157 // Set interface as dynamic
158 interface_set_dynamic(iface
);
160 dev
= iface
->main_dev
.dev
;
161 if (!dev
|| !dev
->default_config
)
162 return UBUS_STATUS_UNKNOWN_ERROR
;
164 return UBUS_STATUS_OK
;
168 return UBUS_STATUS_UNKNOWN_ERROR
;
171 static struct ubus_method main_object_methods
[] = {
172 { .name
= "restart", .handler
= netifd_handle_restart
},
173 { .name
= "reload", .handler
= netifd_handle_reload
},
174 UBUS_METHOD("add_host_route", netifd_add_host_route
, route_policy
),
175 { .name
= "get_proto_handlers", .handler
= netifd_get_proto_handlers
},
176 UBUS_METHOD("add_dynamic", netifd_add_dynamic
, dynamic_policy
),
179 static struct ubus_object_type main_object_type
=
180 UBUS_OBJECT_TYPE("netifd", main_object_methods
);
182 static struct ubus_object main_object
= {
184 .type
= &main_object_type
,
185 .methods
= main_object_methods
,
186 .n_methods
= ARRAY_SIZE(main_object_methods
),
194 static const struct blobmsg_policy dev_policy
[__DEV_MAX
] = {
195 [DEV_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
199 netifd_dev_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
200 struct ubus_request_data
*req
, const char *method
,
201 struct blob_attr
*msg
)
203 struct device
*dev
= NULL
;
204 struct blob_attr
*tb
[__DEV_MAX
];
206 blobmsg_parse(dev_policy
, __DEV_MAX
, tb
, blob_data(msg
), blob_len(msg
));
209 dev
= device_find(blobmsg_data(tb
[DEV_NAME
]));
211 return UBUS_STATUS_INVALID_ARGUMENT
;
214 blob_buf_init(&b
, 0);
215 device_dump_status(&b
, dev
);
216 ubus_send_reply(ctx
, req
, b
.head
);
227 static const struct blobmsg_policy alias_attrs
[__ALIAS_ATTR_MAX
] = {
228 [ALIAS_ATTR_ALIAS
] = { "alias", BLOBMSG_TYPE_ARRAY
},
229 [ALIAS_ATTR_DEV
] = { "device", BLOBMSG_TYPE_STRING
},
233 netifd_handle_alias(struct ubus_context
*ctx
, struct ubus_object
*obj
,
234 struct ubus_request_data
*req
, const char *method
,
235 struct blob_attr
*msg
)
237 struct device
*dev
= NULL
;
238 struct blob_attr
*tb
[__ALIAS_ATTR_MAX
];
239 struct blob_attr
*cur
;
242 blobmsg_parse(alias_attrs
, __ALIAS_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
244 if (!tb
[ALIAS_ATTR_ALIAS
])
245 return UBUS_STATUS_INVALID_ARGUMENT
;
247 if ((cur
= tb
[ALIAS_ATTR_DEV
]) != NULL
) {
248 dev
= device_get(blobmsg_data(cur
), true);
250 return UBUS_STATUS_NOT_FOUND
;
253 blobmsg_for_each_attr(cur
, tb
[ALIAS_ATTR_ALIAS
], rem
) {
254 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
257 if (!blobmsg_check_attr(cur
, NULL
))
260 alias_notify_device(blobmsg_data(cur
), dev
);
265 device_free_unused(dev
);
266 return UBUS_STATUS_INVALID_ARGUMENT
;
275 static const struct blobmsg_policy dev_state_policy
[__DEV_STATE_MAX
] = {
276 [DEV_STATE_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
277 [DEV_STATE_DEFER
] = { .name
= "defer", .type
= BLOBMSG_TYPE_BOOL
},
281 netifd_handle_set_state(struct ubus_context
*ctx
, struct ubus_object
*obj
,
282 struct ubus_request_data
*req
, const char *method
,
283 struct blob_attr
*msg
)
285 struct device
*dev
= NULL
;
286 struct blob_attr
*tb
[__DEV_STATE_MAX
];
287 struct blob_attr
*cur
;
289 blobmsg_parse(dev_state_policy
, __DEV_STATE_MAX
, tb
, blob_data(msg
), blob_len(msg
));
291 cur
= tb
[DEV_STATE_NAME
];
293 return UBUS_STATUS_INVALID_ARGUMENT
;
295 dev
= device_find(blobmsg_data(cur
));
297 return UBUS_STATUS_NOT_FOUND
;
299 cur
= tb
[DEV_STATE_DEFER
];
301 device_set_deferred(dev
, !!blobmsg_get_u8(cur
));
306 static struct ubus_method dev_object_methods
[] = {
307 UBUS_METHOD("status", netifd_dev_status
, dev_policy
),
308 UBUS_METHOD("set_alias", netifd_handle_alias
, alias_attrs
),
309 UBUS_METHOD("set_state", netifd_handle_set_state
, dev_state_policy
),
312 static struct ubus_object_type dev_object_type
=
313 UBUS_OBJECT_TYPE("device", dev_object_methods
);
315 static struct ubus_object dev_object
= {
316 .name
= "network.device",
317 .type
= &dev_object_type
,
318 .methods
= dev_object_methods
,
319 .n_methods
= ARRAY_SIZE(dev_object_methods
),
323 netifd_ubus_add_fd(void)
325 ubus_add_uloop(ubus_ctx
);
326 system_fd_set_cloexec(ubus_ctx
->sock
.fd
);
330 netifd_ubus_reconnect_timer(struct uloop_timeout
*timeout
)
332 static struct uloop_timeout retry
= {
333 .cb
= netifd_ubus_reconnect_timer
,
337 if (ubus_reconnect(ubus_ctx
, ubus_path
) != 0) {
338 DPRINTF("failed to reconnect, trying again in %d seconds\n", t
);
339 uloop_timeout_set(&retry
, t
* 1000);
343 DPRINTF("reconnected to ubus, new id: %08x\n", ubus_ctx
->local_id
);
344 netifd_ubus_add_fd();
348 netifd_ubus_connection_lost(struct ubus_context
*ctx
)
350 netifd_ubus_reconnect_timer(NULL
);
353 /* per-interface object */
356 netifd_handle_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
357 struct ubus_request_data
*req
, const char *method
,
358 struct blob_attr
*msg
)
360 struct interface
*iface
;
362 iface
= container_of(obj
, struct interface
, ubus
);
363 interface_set_up(iface
);
369 netifd_handle_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
370 struct ubus_request_data
*req
, const char *method
,
371 struct blob_attr
*msg
)
373 struct interface
*iface
;
375 iface
= container_of(obj
, struct interface
, ubus
);
376 interface_set_down(iface
);
382 netifd_add_interface_errors(struct blob_buf
*b
, struct interface
*iface
)
384 struct interface_error
*error
;
388 e
= blobmsg_open_array(b
, "errors");
389 list_for_each_entry(error
, &iface
->errors
, list
) {
390 e2
= blobmsg_open_table(b
, NULL
);
392 blobmsg_add_string(b
, "subsystem", error
->subsystem
);
393 blobmsg_add_string(b
, "code", error
->code
);
394 if (error
->data
[0]) {
395 e3
= blobmsg_open_array(b
, "data");
396 for (i
= 0; error
->data
[i
]; i
++)
397 blobmsg_add_string(b
, NULL
, error
->data
[i
]);
398 blobmsg_close_array(b
, e3
);
401 blobmsg_close_table(b
, e2
);
403 blobmsg_close_array(b
, e
);
407 interface_ip_dump_address_list(struct interface_ip_settings
*ip
, bool v6
, bool enabled
)
409 struct device_addr
*addr
;
415 time_t now
= system_get_rtime();
416 vlist_for_each_element(&ip
->addr
, addr
, node
) {
417 if (addr
->enabled
!= enabled
)
420 if ((addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
425 if (af
!= (v6
? AF_INET6
: AF_INET
))
428 a
= blobmsg_open_table(&b
, NULL
);
430 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
431 inet_ntop(af
, &addr
->addr
, buf
, buflen
);
432 blobmsg_add_string_buffer(&b
);
434 blobmsg_add_u32(&b
, "mask", addr
->mask
);
436 if (addr
->preferred_until
) {
437 int preferred
= addr
->preferred_until
- now
;
440 blobmsg_add_u32(&b
, "preferred", preferred
);
443 if (addr
->valid_until
)
444 blobmsg_add_u32(&b
, "valid", addr
->valid_until
- now
);
447 blobmsg_add_string(&b
, "class", addr
->pclass
);
449 blobmsg_close_table(&b
, a
);
454 interface_ip_dump_route_list(struct interface_ip_settings
*ip
, bool enabled
)
456 struct device_route
*route
;
462 time_t now
= system_get_rtime();
463 vlist_for_each_element(&ip
->route
, route
, node
) {
464 if (route
->enabled
!= enabled
)
467 if ((ip
->no_defaultroute
== enabled
) && !route
->mask
)
470 if ((route
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
475 r
= blobmsg_open_table(&b
, NULL
);
477 buf
= blobmsg_alloc_string_buffer(&b
, "target", buflen
);
478 inet_ntop(af
, &route
->addr
, buf
, buflen
);
479 blobmsg_add_string_buffer(&b
);
481 blobmsg_add_u32(&b
, "mask", route
->mask
);
483 buf
= blobmsg_alloc_string_buffer(&b
, "nexthop", buflen
);
484 inet_ntop(af
, &route
->nexthop
, buf
, buflen
);
485 blobmsg_add_string_buffer(&b
);
487 if (route
->flags
& DEVROUTE_TYPE
)
488 blobmsg_add_u32(&b
, "type", route
->type
);
490 if (route
->flags
& DEVROUTE_PROTO
)
491 blobmsg_add_u32(&b
, "proto", route
->proto
);
493 if (route
->flags
& DEVROUTE_MTU
)
494 blobmsg_add_u32(&b
, "mtu", route
->mtu
);
496 if (route
->flags
& DEVROUTE_METRIC
)
497 blobmsg_add_u32(&b
, "metric", route
->metric
);
499 if (route
->flags
& DEVROUTE_TABLE
)
500 blobmsg_add_u32(&b
, "table", route
->table
);
502 if (route
->valid_until
)
503 blobmsg_add_u32(&b
, "valid", route
->valid_until
- now
);
505 buf
= blobmsg_alloc_string_buffer(&b
, "source", buflen
);
506 inet_ntop(af
, &route
->source
, buf
, buflen
);
507 snprintf(buf
+ strlen(buf
), buflen
- strlen(buf
), "/%u", route
->sourcemask
);
508 blobmsg_add_string_buffer(&b
);
510 blobmsg_close_table(&b
, r
);
516 interface_ip_dump_prefix_list(struct interface_ip_settings
*ip
)
518 struct device_prefix
*prefix
;
521 const int buflen
= INET6_ADDRSTRLEN
;
523 time_t now
= system_get_rtime();
524 vlist_for_each_element(&ip
->prefix
, prefix
, node
) {
525 a
= blobmsg_open_table(&b
, NULL
);
527 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
528 inet_ntop(AF_INET6
, &prefix
->addr
, buf
, buflen
);
529 blobmsg_add_string_buffer(&b
);
531 blobmsg_add_u32(&b
, "mask", prefix
->length
);
533 if (prefix
->preferred_until
) {
534 int preferred
= prefix
->preferred_until
- now
;
537 blobmsg_add_u32(&b
, "preferred", preferred
);
540 if (prefix
->valid_until
)
541 blobmsg_add_u32(&b
, "valid", prefix
->valid_until
- now
);
543 blobmsg_add_string(&b
, "class", prefix
->pclass
);
545 c
= blobmsg_open_table(&b
, "assigned");
546 struct device_prefix_assignment
*assign
;
547 list_for_each_entry(assign
, &prefix
->assignments
, head
) {
548 if (!assign
->name
[0])
551 struct in6_addr addr
= prefix
->addr
;
552 addr
.s6_addr32
[1] |= htonl(assign
->assigned
);
554 void *d
= blobmsg_open_table(&b
, assign
->name
);
556 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
557 inet_ntop(AF_INET6
, &addr
, buf
, buflen
);
558 blobmsg_add_string_buffer(&b
);
560 blobmsg_add_u32(&b
, "mask", assign
->length
);
562 blobmsg_close_table(&b
, d
);
564 blobmsg_close_table(&b
, c
);
566 blobmsg_close_table(&b
, a
);
572 interface_ip_dump_prefix_assignment_list(struct interface
*iface
)
576 const int buflen
= INET6_ADDRSTRLEN
;
577 time_t now
= system_get_rtime();
579 struct device_prefix
*prefix
;
580 list_for_each_entry(prefix
, &prefixes
, head
) {
581 struct device_prefix_assignment
*assign
;
582 list_for_each_entry(assign
, &prefix
->assignments
, head
) {
583 if (strcmp(assign
->name
, iface
->name
))
586 struct in6_addr addr
= prefix
->addr
;
587 addr
.s6_addr32
[1] |= htonl(assign
->assigned
);
589 a
= blobmsg_open_table(&b
, NULL
);
591 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
592 inet_ntop(AF_INET6
, &addr
, buf
, buflen
);
593 blobmsg_add_string_buffer(&b
);
595 blobmsg_add_u32(&b
, "mask", assign
->length
);
597 if (prefix
->preferred_until
) {
598 int preferred
= prefix
->preferred_until
- now
;
601 blobmsg_add_u32(&b
, "preferred", preferred
);
604 if (prefix
->valid_until
)
605 blobmsg_add_u32(&b
, "valid", prefix
->valid_until
- now
);
607 void *c
= blobmsg_open_table(&b
, "local-address");
608 if (assign
->enabled
) {
609 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
610 inet_ntop(AF_INET6
, &assign
->addr
, buf
, buflen
);
611 blobmsg_add_string_buffer(&b
);
613 blobmsg_add_u32(&b
, "mask", assign
->length
< 64 ? 64 : assign
->length
);
615 blobmsg_close_table(&b
, c
);
617 blobmsg_close_table(&b
, a
);
623 interface_ip_dump_dns_server_list(struct interface_ip_settings
*ip
, bool enabled
)
625 struct dns_server
*dns
;
629 vlist_simple_for_each_element(&ip
->dns_servers
, dns
, node
) {
630 if (ip
->no_dns
== enabled
)
633 buf
= blobmsg_alloc_string_buffer(&b
, NULL
, buflen
);
634 inet_ntop(dns
->af
, &dns
->addr
, buf
, buflen
);
635 blobmsg_add_string_buffer(&b
);
640 interface_ip_dump_dns_search_list(struct interface_ip_settings
*ip
, bool enabled
)
642 struct dns_search_domain
*dns
;
644 vlist_simple_for_each_element(&ip
->dns_search
, dns
, node
) {
645 if (ip
->no_dns
== enabled
)
648 blobmsg_add_string(&b
, NULL
, dns
->name
);
653 netifd_dump_status(struct interface
*iface
)
655 struct interface_data
*data
;
659 blobmsg_add_u8(&b
, "up", iface
->state
== IFS_UP
);
660 blobmsg_add_u8(&b
, "pending", iface
->state
== IFS_SETUP
);
661 blobmsg_add_u8(&b
, "available", iface
->available
);
662 blobmsg_add_u8(&b
, "autostart", iface
->autostart
);
663 blobmsg_add_u8(&b
, "dynamic", iface
->dynamic
);
665 if (iface
->state
== IFS_UP
) {
666 time_t cur
= system_get_rtime();
667 blobmsg_add_u32(&b
, "uptime", cur
- iface
->start_time
);
668 if (iface
->l3_dev
.dev
)
669 blobmsg_add_string(&b
, "l3_device", iface
->l3_dev
.dev
->ifname
);
672 if (iface
->proto_handler
)
673 blobmsg_add_string(&b
, "proto", iface
->proto_handler
->name
);
675 dev
= iface
->main_dev
.dev
;
676 if (dev
&& !dev
->hidden
&& iface
->proto_handler
&&
677 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
))
678 blobmsg_add_string(&b
, "device", dev
->ifname
);
680 if (iface
->state
== IFS_UP
) {
681 if (iface
->updated
) {
682 a
= blobmsg_open_array(&b
, "updated");
684 if (iface
->updated
& IUF_ADDRESS
)
685 blobmsg_add_string(&b
, NULL
, "addresses");
686 if (iface
->updated
& IUF_ROUTE
)
687 blobmsg_add_string(&b
, NULL
, "routes");
688 if (iface
->updated
& IUF_PREFIX
)
689 blobmsg_add_string(&b
, NULL
, "prefixes");
690 if (iface
->updated
& IUF_DATA
)
691 blobmsg_add_string(&b
, NULL
, "data");
693 blobmsg_close_array(&b
, a
);
697 blobmsg_add_u32(&b
, "ip4table", iface
->ip4table
);
699 blobmsg_add_u32(&b
, "ip6table", iface
->ip6table
);
700 blobmsg_add_u32(&b
, "metric", iface
->metric
);
701 blobmsg_add_u32(&b
, "dns_metric", iface
->dns_metric
);
702 blobmsg_add_u8(&b
, "delegation", !iface
->proto_ip
.no_delegation
);
703 if (iface
->assignment_weight
)
704 blobmsg_add_u32(&b
, "ip6weight", iface
->assignment_weight
);
705 a
= blobmsg_open_array(&b
, "ipv4-address");
706 interface_ip_dump_address_list(&iface
->config_ip
, false, true);
707 interface_ip_dump_address_list(&iface
->proto_ip
, false, true);
708 blobmsg_close_array(&b
, a
);
709 a
= blobmsg_open_array(&b
, "ipv6-address");
710 interface_ip_dump_address_list(&iface
->config_ip
, true, true);
711 interface_ip_dump_address_list(&iface
->proto_ip
, true, true);
712 blobmsg_close_array(&b
, a
);
713 a
= blobmsg_open_array(&b
, "ipv6-prefix");
714 interface_ip_dump_prefix_list(&iface
->config_ip
);
715 interface_ip_dump_prefix_list(&iface
->proto_ip
);
716 blobmsg_close_array(&b
, a
);
717 a
= blobmsg_open_array(&b
, "ipv6-prefix-assignment");
718 interface_ip_dump_prefix_assignment_list(iface
);
719 blobmsg_close_array(&b
, a
);
720 a
= blobmsg_open_array(&b
, "route");
721 interface_ip_dump_route_list(&iface
->config_ip
, true);
722 interface_ip_dump_route_list(&iface
->proto_ip
, true);
723 blobmsg_close_array(&b
, a
);
724 a
= blobmsg_open_array(&b
, "dns-server");
725 interface_ip_dump_dns_server_list(&iface
->config_ip
, true);
726 interface_ip_dump_dns_server_list(&iface
->proto_ip
, true);
727 blobmsg_close_array(&b
, a
);
728 a
= blobmsg_open_array(&b
, "dns-search");
729 interface_ip_dump_dns_search_list(&iface
->config_ip
, true);
730 interface_ip_dump_dns_search_list(&iface
->proto_ip
, true);
731 blobmsg_close_array(&b
, a
);
733 inactive
= blobmsg_open_table(&b
, "inactive");
734 a
= blobmsg_open_array(&b
, "ipv4-address");
735 interface_ip_dump_address_list(&iface
->config_ip
, false, false);
736 interface_ip_dump_address_list(&iface
->proto_ip
, false, false);
737 blobmsg_close_array(&b
, a
);
738 a
= blobmsg_open_array(&b
, "ipv6-address");
739 interface_ip_dump_address_list(&iface
->config_ip
, true, false);
740 interface_ip_dump_address_list(&iface
->proto_ip
, true, false);
741 blobmsg_close_array(&b
, a
);
742 a
= blobmsg_open_array(&b
, "route");
743 interface_ip_dump_route_list(&iface
->config_ip
, false);
744 interface_ip_dump_route_list(&iface
->proto_ip
, false);
745 blobmsg_close_array(&b
, a
);
746 a
= blobmsg_open_array(&b
, "dns-server");
747 interface_ip_dump_dns_server_list(&iface
->config_ip
, false);
748 interface_ip_dump_dns_server_list(&iface
->proto_ip
, false);
749 blobmsg_close_array(&b
, a
);
750 a
= blobmsg_open_array(&b
, "dns-search");
751 interface_ip_dump_dns_search_list(&iface
->config_ip
, false);
752 interface_ip_dump_dns_search_list(&iface
->proto_ip
, false);
753 blobmsg_close_array(&b
, a
);
754 blobmsg_close_table(&b
, inactive
);
757 a
= blobmsg_open_table(&b
, "data");
758 avl_for_each_element(&iface
->data
, data
, node
)
759 blobmsg_add_blob(&b
, data
->data
);
761 blobmsg_close_table(&b
, a
);
763 if (!list_empty(&iface
->errors
))
764 netifd_add_interface_errors(&b
, iface
);
768 netifd_handle_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
769 struct ubus_request_data
*req
, const char *method
,
770 struct blob_attr
*msg
)
772 struct interface
*iface
= container_of(obj
, struct interface
, ubus
);
774 blob_buf_init(&b
, 0);
775 netifd_dump_status(iface
);
776 ubus_send_reply(ctx
, req
, b
.head
);
783 netifd_handle_dump(struct ubus_context
*ctx
, struct ubus_object
*obj
,
784 struct ubus_request_data
*req
, const char *method
,
785 struct blob_attr
*msg
)
787 blob_buf_init(&b
, 0);
788 void *a
= blobmsg_open_array(&b
, "interface");
790 struct interface
*iface
;
791 vlist_for_each_element(&interfaces
, iface
, node
) {
792 void *i
= blobmsg_open_table(&b
, NULL
);
793 blobmsg_add_string(&b
, "interface", iface
->name
);
794 netifd_dump_status(iface
);
795 blobmsg_close_table(&b
, i
);
798 blobmsg_close_array(&b
, a
);
799 ubus_send_reply(ctx
, req
, b
.head
);
810 static const struct blobmsg_policy dev_link_policy
[__DEV_LINK_MAX
] = {
811 [DEV_LINK_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
812 [DEV_LINK_EXT
] = { .name
= "link-ext", .type
= BLOBMSG_TYPE_BOOL
},
816 netifd_iface_handle_device(struct ubus_context
*ctx
, struct ubus_object
*obj
,
817 struct ubus_request_data
*req
, const char *method
,
818 struct blob_attr
*msg
)
820 struct blob_attr
*tb
[__DEV_LINK_MAX
];
821 struct blob_attr
*cur
;
822 struct interface
*iface
;
823 bool add
= !strncmp(method
, "add", 3);
824 bool link_ext
= true;
826 iface
= container_of(obj
, struct interface
, ubus
);
828 blobmsg_parse(dev_link_policy
, __DEV_LINK_MAX
, tb
, blob_data(msg
), blob_len(msg
));
830 if (!tb
[DEV_LINK_NAME
])
831 return UBUS_STATUS_INVALID_ARGUMENT
;
833 cur
= tb
[DEV_LINK_EXT
];
835 link_ext
= blobmsg_get_bool(cur
);
837 return interface_handle_link(iface
, blobmsg_data(tb
[DEV_LINK_NAME
]), add
, link_ext
);
842 netifd_iface_notify_proto(struct ubus_context
*ctx
, struct ubus_object
*obj
,
843 struct ubus_request_data
*req
, const char *method
,
844 struct blob_attr
*msg
)
846 struct interface
*iface
;
848 iface
= container_of(obj
, struct interface
, ubus
);
850 if (!iface
->proto
|| !iface
->proto
->notify
)
851 return UBUS_STATUS_NOT_SUPPORTED
;
853 return iface
->proto
->notify(iface
->proto
, msg
);
857 netifd_iface_do_remove(struct uloop_timeout
*timeout
)
859 struct interface
*iface
;
861 iface
= container_of(timeout
, struct interface
, remove_timer
);
862 vlist_delete(&interfaces
, &iface
->node
);
866 netifd_iface_remove(struct ubus_context
*ctx
, struct ubus_object
*obj
,
867 struct ubus_request_data
*req
, const char *method
,
868 struct blob_attr
*msg
)
870 struct interface
*iface
;
872 iface
= container_of(obj
, struct interface
, ubus
);
873 if (iface
->remove_timer
.cb
)
874 return UBUS_STATUS_INVALID_ARGUMENT
;
876 iface
->remove_timer
.cb
= netifd_iface_do_remove
;
877 uloop_timeout_set(&iface
->remove_timer
, 100);
882 netifd_handle_iface_prepare(struct ubus_context
*ctx
, struct ubus_object
*obj
,
883 struct ubus_request_data
*req
, const char *method
,
884 struct blob_attr
*msg
)
886 struct interface
*iface
;
888 const struct device_hotplug_ops
*ops
;
890 iface
= container_of(obj
, struct interface
, ubus
);
891 dev
= iface
->main_dev
.dev
;
895 ops
= dev
->hotplug_ops
;
899 return ops
->prepare(dev
);
903 netifd_handle_set_data(struct ubus_context
*ctx
, struct ubus_object
*obj
,
904 struct ubus_request_data
*req
, const char *method
,
905 struct blob_attr
*msg
)
907 struct interface
*iface
;
909 iface
= container_of(obj
, struct interface
, ubus
);
911 return interface_parse_data(iface
, msg
);
914 static struct ubus_method iface_object_methods
[] = {
915 { .name
= "up", .handler
= netifd_handle_up
},
916 { .name
= "down", .handler
= netifd_handle_down
},
917 { .name
= "status", .handler
= netifd_handle_status
},
918 { .name
= "prepare", .handler
= netifd_handle_iface_prepare
},
919 { .name
= "dump", .handler
= netifd_handle_dump
},
920 UBUS_METHOD("add_device", netifd_iface_handle_device
, dev_link_policy
),
921 UBUS_METHOD("remove_device", netifd_iface_handle_device
, dev_link_policy
),
922 { .name
= "notify_proto", .handler
= netifd_iface_notify_proto
},
923 { .name
= "remove", .handler
= netifd_iface_remove
},
924 { .name
= "set_data", .handler
= netifd_handle_set_data
},
927 static struct ubus_object_type iface_object_type
=
928 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods
);
931 static struct ubus_object iface_object
= {
932 .name
= "network.interface",
933 .type
= &iface_object_type
,
934 .n_methods
= ARRAY_SIZE(iface_object_methods
),
937 static void netifd_add_object(struct ubus_object
*obj
)
939 int ret
= ubus_add_object(ubus_ctx
, obj
);
942 fprintf(stderr
, "Failed to publish object '%s': %s\n", obj
->name
, ubus_strerror(ret
));
945 static const struct blobmsg_policy iface_policy
= {
947 .type
= BLOBMSG_TYPE_STRING
,
951 netifd_handle_iface(struct ubus_context
*ctx
, struct ubus_object
*obj
,
952 struct ubus_request_data
*req
, const char *method
,
953 struct blob_attr
*msg
)
955 struct interface
*iface
;
956 struct blob_attr
*tb
;
959 blobmsg_parse(&iface_policy
, 1, &tb
, blob_data(msg
), blob_len(msg
));
961 return UBUS_STATUS_INVALID_ARGUMENT
;
963 iface
= vlist_find(&interfaces
, blobmsg_data(tb
), iface
, node
);
965 return UBUS_STATUS_NOT_FOUND
;
967 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
970 if (strcmp(method
, iface_object_methods
[i
].name
) != 0)
973 cb
= iface_object_methods
[i
].handler
;
974 return cb(ctx
, &iface
->ubus
, req
, method
, msg
);
977 return UBUS_STATUS_INVALID_ARGUMENT
;
980 static void netifd_add_iface_object(void)
982 struct ubus_method
*methods
;
985 methods
= calloc(1, sizeof(iface_object_methods
));
989 memcpy(methods
, iface_object_methods
, sizeof(iface_object_methods
));
990 iface_object
.methods
= methods
;
992 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
993 if (methods
[i
].handler
== netifd_handle_dump
)
996 methods
[i
].handler
= netifd_handle_iface
;
997 methods
[i
].policy
= &iface_policy
;
998 methods
[i
].n_policy
= 1;
1000 netifd_add_object(&iface_object
);
1003 static struct wireless_device
*
1004 get_wdev(struct blob_attr
*msg
, int *ret
)
1006 struct blobmsg_policy wdev_policy
= {
1008 .type
= BLOBMSG_TYPE_STRING
,
1010 struct blob_attr
*dev_attr
;
1011 struct wireless_device
*wdev
= NULL
;
1014 blobmsg_parse(&wdev_policy
, 1, &dev_attr
, blob_data(msg
), blob_len(msg
));
1016 *ret
= UBUS_STATUS_INVALID_ARGUMENT
;
1020 wdev
= vlist_find(&wireless_devices
, blobmsg_data(dev_attr
), wdev
, node
);
1022 *ret
= UBUS_STATUS_NOT_FOUND
;
1031 netifd_handle_wdev_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1032 struct ubus_request_data
*req
, const char *method
,
1033 struct blob_attr
*msg
)
1035 struct wireless_device
*wdev
;
1038 wdev
= get_wdev(msg
, &ret
);
1039 if (ret
== UBUS_STATUS_NOT_FOUND
)
1043 wireless_device_set_up(wdev
);
1045 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1046 wireless_device_set_up(wdev
);
1053 netifd_handle_wdev_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1054 struct ubus_request_data
*req
, const char *method
,
1055 struct blob_attr
*msg
)
1057 struct wireless_device
*wdev
;
1060 wdev
= get_wdev(msg
, &ret
);
1061 if (ret
== UBUS_STATUS_NOT_FOUND
)
1065 wireless_device_set_down(wdev
);
1067 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1068 wireless_device_set_down(wdev
);
1075 netifd_handle_wdev_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1076 struct ubus_request_data
*req
, const char *method
,
1077 struct blob_attr
*msg
)
1079 struct wireless_device
*wdev
;
1082 wdev
= get_wdev(msg
, &ret
);
1083 if (ret
== UBUS_STATUS_NOT_FOUND
)
1086 blob_buf_init(&b
, 0);
1088 wireless_device_status(wdev
, &b
);
1090 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1091 wireless_device_status(wdev
, &b
);
1093 ubus_send_reply(ctx
, req
, b
.head
);
1098 netifd_handle_wdev_get_validate(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1099 struct ubus_request_data
*req
, const char *method
,
1100 struct blob_attr
*msg
)
1102 struct wireless_device
*wdev
;
1105 wdev
= get_wdev(msg
, &ret
);
1106 if (ret
== UBUS_STATUS_NOT_FOUND
)
1109 blob_buf_init(&b
, 0);
1111 wireless_device_get_validate(wdev
, &b
);
1113 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1114 wireless_device_get_validate(wdev
, &b
);
1116 ubus_send_reply(ctx
, req
, b
.head
);
1121 netifd_handle_wdev_notify(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1122 struct ubus_request_data
*req
, const char *method
,
1123 struct blob_attr
*msg
)
1125 struct wireless_device
*wdev
;
1128 wdev
= get_wdev(msg
, &ret
);
1132 return wireless_device_notify(wdev
, msg
, req
);
1135 static struct ubus_method wireless_object_methods
[] = {
1136 { .name
= "up", .handler
= netifd_handle_wdev_up
},
1137 { .name
= "down", .handler
= netifd_handle_wdev_down
},
1138 { .name
= "status", .handler
= netifd_handle_wdev_status
},
1139 { .name
= "notify", .handler
= netifd_handle_wdev_notify
},
1140 { .name
= "get_validate", .handler
= netifd_handle_wdev_get_validate
},
1143 static struct ubus_object_type wireless_object_type
=
1144 UBUS_OBJECT_TYPE("netifd_iface", wireless_object_methods
);
1147 static struct ubus_object wireless_object
= {
1148 .name
= "network.wireless",
1149 .type
= &wireless_object_type
,
1150 .methods
= wireless_object_methods
,
1151 .n_methods
= ARRAY_SIZE(wireless_object_methods
),
1155 netifd_ubus_init(const char *path
)
1160 ubus_ctx
= ubus_connect(path
);
1164 DPRINTF("connected as %08x\n", ubus_ctx
->local_id
);
1165 ubus_ctx
->connection_lost
= netifd_ubus_connection_lost
;
1166 netifd_ubus_add_fd();
1168 netifd_add_object(&main_object
);
1169 netifd_add_object(&dev_object
);
1170 netifd_add_object(&wireless_object
);
1171 netifd_add_iface_object();
1177 netifd_ubus_done(void)
1179 ubus_free(ubus_ctx
);
1183 netifd_ubus_interface_event(struct interface
*iface
, bool up
)
1185 blob_buf_init(&b
, 0);
1186 blobmsg_add_string(&b
, "action", up
? "ifup" : "ifdown");
1187 blobmsg_add_string(&b
, "interface", iface
->name
);
1188 ubus_send_event(ubus_ctx
, "network.interface", b
.head
);
1192 netifd_ubus_interface_notify(struct interface
*iface
, bool up
)
1194 const char *event
= (up
) ? "interface.update" : "interface.down";
1195 blob_buf_init(&b
, 0);
1196 blobmsg_add_string(&b
, "interface", iface
->name
);
1197 netifd_dump_status(iface
);
1198 ubus_notify(ubus_ctx
, &iface_object
, event
, b
.head
, -1);
1199 ubus_notify(ubus_ctx
, &iface
->ubus
, event
, b
.head
, -1);
1203 netifd_ubus_add_interface(struct interface
*iface
)
1205 struct ubus_object
*obj
= &iface
->ubus
;
1208 if (asprintf(&name
, "%s.interface.%s", main_object
.name
, iface
->name
) == -1)
1212 obj
->type
= &iface_object_type
;
1213 obj
->methods
= iface_object_methods
;
1214 obj
->n_methods
= ARRAY_SIZE(iface_object_methods
);
1215 if (ubus_add_object(ubus_ctx
, &iface
->ubus
)) {
1216 DPRINTF("failed to publish ubus object for interface '%s'\n", iface
->name
);
1223 netifd_ubus_remove_interface(struct interface
*iface
)
1225 if (!iface
->ubus
.name
)
1228 ubus_remove_object(ubus_ctx
, &iface
->ubus
);
1229 free((void *) iface
->ubus
.name
);