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"
26 static struct ubus_context
*ctx
= NULL
;
27 static struct blob_buf b
;
28 static struct netifd_fd ubus_fd
;
33 netifd_handle_restart(struct ubus_context
*ctx
, struct ubus_object
*obj
,
34 struct ubus_request_data
*req
, const char *method
,
35 struct blob_attr
*msg
)
42 netifd_handle_reload(struct ubus_context
*ctx
, struct ubus_object
*obj
,
43 struct ubus_request_data
*req
, const char *method
,
44 struct blob_attr
*msg
)
56 static const struct blobmsg_policy route_policy
[__HR_MAX
] = {
57 [HR_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
58 [HR_V6
] = { .name
= "v6", .type
= BLOBMSG_TYPE_BOOL
},
62 netifd_add_host_route(struct ubus_context
*ctx
, struct ubus_object
*obj
,
63 struct ubus_request_data
*req
, const char *method
,
64 struct blob_attr
*msg
)
66 struct blob_attr
*tb
[__HR_MAX
];
67 struct interface
*iface
;
71 blobmsg_parse(route_policy
, __HR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
73 return UBUS_STATUS_INVALID_ARGUMENT
;
76 v6
= blobmsg_get_bool(tb
[HR_V6
]);
78 memset(&a
, 0, sizeof(a
));
79 if (!inet_pton(v6
? AF_INET6
: AF_INET
, blobmsg_data(tb
[HR_TARGET
]), &a
))
80 return UBUS_STATUS_INVALID_ARGUMENT
;
83 iface
= interface_ip_add_target_route(&a
, v6
);
85 return UBUS_STATUS_NOT_FOUND
;
88 blobmsg_add_string(&b
, "interface", iface
->name
);
89 ubus_send_reply(ctx
, req
, b
.head
);
94 static struct ubus_method main_object_methods
[] = {
95 { .name
= "restart", .handler
= netifd_handle_restart
},
96 { .name
= "reload", .handler
= netifd_handle_reload
},
97 UBUS_METHOD("add_host_route", netifd_add_host_route
, route_policy
),
100 static struct ubus_object_type main_object_type
=
101 UBUS_OBJECT_TYPE("netifd", main_object_methods
);
103 static struct ubus_object main_object
= {
105 .type
= &main_object_type
,
106 .methods
= main_object_methods
,
107 .n_methods
= ARRAY_SIZE(main_object_methods
),
115 static const struct blobmsg_policy dev_policy
[__DEV_MAX
] = {
116 [DEV_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
120 netifd_dev_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
121 struct ubus_request_data
*req
, const char *method
,
122 struct blob_attr
*msg
)
124 struct device
*dev
= NULL
;
125 struct blob_attr
*tb
[__DEV_MAX
];
127 blobmsg_parse(dev_policy
, __DEV_MAX
, tb
, blob_data(msg
), blob_len(msg
));
130 dev
= device_get(blobmsg_data(tb
[DEV_NAME
]), false);
132 return UBUS_STATUS_INVALID_ARGUMENT
;
135 blob_buf_init(&b
, 0);
136 device_dump_status(&b
, dev
);
137 ubus_send_reply(ctx
, req
, b
.head
);
148 static const struct blobmsg_policy alias_attrs
[__ALIAS_ATTR_MAX
] = {
149 [ALIAS_ATTR_ALIAS
] = { "alias", BLOBMSG_TYPE_ARRAY
},
150 [ALIAS_ATTR_DEV
] = { "device", BLOBMSG_TYPE_STRING
},
154 netifd_handle_alias(struct ubus_context
*ctx
, struct ubus_object
*obj
,
155 struct ubus_request_data
*req
, const char *method
,
156 struct blob_attr
*msg
)
158 struct device
*dev
= NULL
;
159 struct blob_attr
*tb
[__ALIAS_ATTR_MAX
];
160 struct blob_attr
*cur
;
163 blobmsg_parse(alias_attrs
, __ALIAS_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
165 if (!tb
[ALIAS_ATTR_ALIAS
])
166 return UBUS_STATUS_INVALID_ARGUMENT
;
168 if ((cur
= tb
[ALIAS_ATTR_DEV
]) != NULL
) {
169 dev
= device_get(blobmsg_data(cur
), true);
171 return UBUS_STATUS_NOT_FOUND
;
174 blobmsg_for_each_attr(cur
, tb
[ALIAS_ATTR_ALIAS
], rem
) {
175 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
178 if (!blobmsg_check_attr(cur
, NULL
))
181 alias_notify_device(blobmsg_data(cur
), dev
);
186 device_free_unused(dev
);
187 return UBUS_STATUS_INVALID_ARGUMENT
;
190 static struct ubus_method dev_object_methods
[] = {
191 UBUS_METHOD("status", netifd_dev_status
, dev_policy
),
192 UBUS_METHOD("set_alias", netifd_handle_alias
, alias_attrs
),
195 static struct ubus_object_type dev_object_type
=
196 UBUS_OBJECT_TYPE("device", dev_object_methods
);
198 static struct ubus_object dev_object
= {
199 .name
= "network.device",
200 .type
= &dev_object_type
,
201 .methods
= dev_object_methods
,
202 .n_methods
= ARRAY_SIZE(dev_object_methods
),
206 netifd_ubus_init(const char *path
)
210 ctx
= ubus_connect(path
);
214 DPRINTF("connected as %08x\n", ctx
->local_id
);
217 ubus_fd
.fd
= ctx
->sock
.fd
;
218 netifd_fd_add(&ubus_fd
);
220 ret
= ubus_add_object(ctx
, &main_object
);
224 ret
= ubus_add_object(ctx
, &dev_object
);
228 fprintf(stderr
, "Failed to publish object: %s\n", ubus_strerror(ret
));
233 netifd_ubus_done(void)
239 /* per-interface object */
242 netifd_handle_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
243 struct ubus_request_data
*req
, const char *method
,
244 struct blob_attr
*msg
)
246 struct interface
*iface
;
248 iface
= container_of(obj
, struct interface
, ubus
);
249 interface_set_up(iface
);
255 netifd_handle_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
256 struct ubus_request_data
*req
, const char *method
,
257 struct blob_attr
*msg
)
259 struct interface
*iface
;
261 iface
= container_of(obj
, struct interface
, ubus
);
262 interface_set_down(iface
);
268 netifd_add_interface_errors(struct blob_buf
*b
, struct interface
*iface
)
270 struct interface_error
*error
;
274 e
= blobmsg_open_array(b
, "errors");
275 list_for_each_entry(error
, &iface
->errors
, list
) {
276 e2
= blobmsg_open_table(b
, NULL
);
278 blobmsg_add_string(b
, "subsystem", error
->subsystem
);
279 blobmsg_add_string(b
, "code", error
->code
);
280 if (error
->data
[0]) {
281 e3
= blobmsg_open_array(b
, "data");
282 for (i
= 0; error
->data
[i
]; i
++)
283 blobmsg_add_string(b
, NULL
, error
->data
[i
]);
284 blobmsg_close_array(b
, e3
);
287 blobmsg_close_table(b
, e2
);
289 blobmsg_close_array(b
, e
);
293 interface_ip_dump_address_list(struct interface_ip_settings
*ip
)
295 struct device_addr
*addr
;
301 vlist_for_each_element(&ip
->addr
, addr
, node
) {
302 if ((addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
307 a
= blobmsg_open_table(&b
, NULL
);
309 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
310 inet_ntop(af
, &addr
->addr
, buf
, buflen
);
311 blobmsg_add_string_buffer(&b
);
313 blobmsg_add_u32(&b
, "mask", addr
->mask
);
315 blobmsg_close_table(&b
, a
);
320 interface_ip_dump_route_list(struct interface_ip_settings
*ip
)
322 struct device_route
*route
;
328 vlist_for_each_element(&ip
->route
, route
, node
) {
329 if ((route
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
334 r
= blobmsg_open_table(&b
, NULL
);
336 buf
= blobmsg_alloc_string_buffer(&b
, "target", buflen
);
337 inet_ntop(af
, &route
->addr
, buf
, buflen
);
338 blobmsg_add_string_buffer(&b
);
340 blobmsg_add_u32(&b
, "mask", route
->mask
);
342 buf
= blobmsg_alloc_string_buffer(&b
, "nexthop", buflen
);
343 inet_ntop(af
, &route
->nexthop
, buf
, buflen
);
344 blobmsg_add_string_buffer(&b
);
346 blobmsg_close_table(&b
, r
);
351 netifd_handle_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
352 struct ubus_request_data
*req
, const char *method
,
353 struct blob_attr
*msg
)
355 struct interface
*iface
;
356 struct interface_data
*data
;
360 iface
= container_of(obj
, struct interface
, ubus
);
362 blob_buf_init(&b
, 0);
363 blobmsg_add_u8(&b
, "up", iface
->state
== IFS_UP
);
364 blobmsg_add_u8(&b
, "pending", iface
->state
== IFS_SETUP
);
365 blobmsg_add_u8(&b
, "available", iface
->available
);
366 blobmsg_add_u8(&b
, "autostart", iface
->autostart
);
368 if (iface
->state
== IFS_UP
) {
369 time_t cur
= system_get_rtime();
370 blobmsg_add_u32(&b
, "uptime", cur
- iface
->start_time
);
371 blobmsg_add_string(&b
, "l3_device", iface
->l3_dev
.dev
->ifname
);
374 dev
= iface
->main_dev
.dev
;
375 if (dev
&& !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
))
376 blobmsg_add_string(&b
, "device", dev
->ifname
);
378 if (iface
->state
== IFS_UP
) {
379 a
= blobmsg_open_array(&b
, "address");
380 interface_ip_dump_address_list(&iface
->config_ip
);
381 interface_ip_dump_address_list(&iface
->proto_ip
);
382 blobmsg_close_array(&b
, a
);
383 a
= blobmsg_open_array(&b
, "route");
384 interface_ip_dump_route_list(&iface
->config_ip
);
385 interface_ip_dump_route_list(&iface
->proto_ip
);
386 blobmsg_close_array(&b
, a
);
389 a
= blobmsg_open_table(&b
, "data");
390 avl_for_each_element(&iface
->data
, data
, node
)
391 blob_put(&b
, blob_id(data
->data
), blob_data(data
->data
), blob_len(data
->data
));
393 blobmsg_close_table(&b
, a
);
395 if (!list_is_empty(&iface
->errors
))
396 netifd_add_interface_errors(&b
, iface
);
398 ubus_send_reply(ctx
, req
, b
.head
);
404 netifd_iface_handle_device(struct ubus_context
*ctx
, struct ubus_object
*obj
,
405 struct ubus_request_data
*req
, const char *method
,
406 struct blob_attr
*msg
)
408 struct blob_attr
*tb
[__DEV_MAX
];
409 struct interface
*iface
;
411 bool add
= !strncmp(method
, "add", 3);
414 iface
= container_of(obj
, struct interface
, ubus
);
416 blobmsg_parse(dev_policy
, __DEV_MAX
, tb
, blob_data(msg
), blob_len(msg
));
419 return UBUS_STATUS_INVALID_ARGUMENT
;
423 dev
= device_get(blobmsg_data(tb
[DEV_NAME
]), add
? 2 : 0);
425 return UBUS_STATUS_NOT_FOUND
;
428 return interface_add_link(iface
, dev
);
430 return interface_remove_link(iface
, dev
);
439 netifd_iface_notify_proto(struct ubus_context
*ctx
, struct ubus_object
*obj
,
440 struct ubus_request_data
*req
, const char *method
,
441 struct blob_attr
*msg
)
443 struct interface
*iface
;
445 iface
= container_of(obj
, struct interface
, ubus
);
447 if (!iface
->proto
|| !iface
->proto
->notify
)
448 return UBUS_STATUS_NOT_SUPPORTED
;
450 return iface
->proto
->notify(iface
->proto
, msg
);
454 netifd_iface_do_remove(struct uloop_timeout
*timeout
)
456 struct interface
*iface
;
458 iface
= container_of(timeout
, struct interface
, remove_timer
);
459 vlist_delete(&interfaces
, &iface
->node
);
463 netifd_iface_remove(struct ubus_context
*ctx
, struct ubus_object
*obj
,
464 struct ubus_request_data
*req
, const char *method
,
465 struct blob_attr
*msg
)
467 struct interface
*iface
;
469 iface
= container_of(obj
, struct interface
, ubus
);
470 if (iface
->remove_timer
.cb
)
471 return UBUS_STATUS_INVALID_ARGUMENT
;
473 iface
->remove_timer
.cb
= netifd_iface_do_remove
;
474 uloop_timeout_set(&iface
->remove_timer
, 100);
479 netifd_handle_iface_prepare(struct ubus_context
*ctx
, struct ubus_object
*obj
,
480 struct ubus_request_data
*req
, const char *method
,
481 struct blob_attr
*msg
)
483 struct interface
*iface
;
485 const struct device_hotplug_ops
*ops
;
487 iface
= container_of(obj
, struct interface
, ubus
);
488 dev
= iface
->main_dev
.dev
;
492 ops
= dev
->hotplug_ops
;
496 return ops
->prepare(dev
);
500 netifd_handle_set_data(struct ubus_context
*ctx
, struct ubus_object
*obj
,
501 struct ubus_request_data
*req
, const char *method
,
502 struct blob_attr
*msg
)
504 struct interface
*iface
;
505 struct blob_attr
*cur
;
508 iface
= container_of(obj
, struct interface
, ubus
);
510 blob_for_each_attr(cur
, msg
, rem
) {
511 ret
= interface_add_data(iface
, cur
);
519 static struct ubus_method iface_object_methods
[] = {
520 { .name
= "up", .handler
= netifd_handle_up
},
521 { .name
= "down", .handler
= netifd_handle_down
},
522 { .name
= "status", .handler
= netifd_handle_status
},
523 { .name
= "prepare", .handler
= netifd_handle_iface_prepare
},
524 UBUS_METHOD("add_device", netifd_iface_handle_device
, dev_policy
),
525 UBUS_METHOD("remove_device", netifd_iface_handle_device
, dev_policy
),
526 { .name
= "notify_proto", .handler
= netifd_iface_notify_proto
},
527 { .name
= "remove", .handler
= netifd_iface_remove
},
528 { .name
= "set_data", .handler
= netifd_handle_set_data
},
531 static struct ubus_object_type iface_object_type
=
532 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods
);
536 netifd_ubus_interface_event(struct interface
*iface
, bool up
)
538 blob_buf_init(&b
, 0);
539 blobmsg_add_string(&b
, "action", up
? "ifup" : "ifdown");
540 blobmsg_add_string(&b
, "interface", iface
->name
);
541 ubus_send_event(ctx
, "network.interface", b
.head
);
545 netifd_ubus_add_interface(struct interface
*iface
)
547 struct ubus_object
*obj
= &iface
->ubus
;
550 asprintf(&name
, "%s.interface.%s", main_object
.name
, iface
->name
);
555 obj
->type
= &iface_object_type
;
556 obj
->methods
= iface_object_methods
;
557 obj
->n_methods
= ARRAY_SIZE(iface_object_methods
);
558 if (ubus_add_object(ctx
, &iface
->ubus
)) {
559 DPRINTF("failed to publish ubus object for interface '%s'\n", iface
->name
);
566 netifd_ubus_remove_interface(struct interface
*iface
)
568 if (!iface
->ubus
.name
)
571 ubus_remove_object(ctx
, &iface
->ubus
);
572 free((void *) iface
->ubus
.name
);