X-Git-Url: http://git.archive.openwrt.org/?a=blobdiff_plain;f=interface.c;h=576d2a3a42848e7b8b8ca3ef9ddd408131ca489b;hb=fd50535d7d4060d3cb80f1c317a087507ecfdf2b;hp=1f73130db4f8b764fc4eb86769eaf85223ab35c9;hpb=170304603e7d905e98301c703ad2ec107b9a774c;p=project%2Fnetifd.git diff --git a/interface.c b/interface.c index 1f73130..576d2a3 100644 --- a/interface.c +++ b/interface.c @@ -5,35 +5,29 @@ #include "netifd.h" #include "device.h" #include "interface.h" +#include "interface-ip.h" #include "proto.h" #include "ubus.h" #include "config.h" -static LIST_HEAD(interfaces); +struct vlist_tree interfaces; enum { - IFACE_ATTR_TYPE, IFACE_ATTR_IFNAME, IFACE_ATTR_PROTO, IFACE_ATTR_AUTO, IFACE_ATTR_MAX }; -static const union config_param_info iface_attr_info[IFACE_ATTR_MAX] = { - [IFACE_ATTR_IFNAME].type = BLOBMSG_TYPE_STRING, -}; - static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = { - [IFACE_ATTR_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING }, [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING }, - [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_ARRAY }, + [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING }, [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL }, }; const struct config_param_list interface_attr_list = { .n_params = IFACE_ATTR_MAX, .params = iface_attrs, - .info = iface_attr_info, }; static void @@ -90,22 +84,26 @@ interface_event(struct interface *iface, enum interface_event ev) static void mark_interface_down(struct interface *iface) { - interface_del_ctx_addr(iface, NULL); - release_device(iface->main_dev.dev); + vlist_flush_all(&iface->proto_addr); + vlist_flush_all(&iface->proto_route); + if (iface->main_dev.dev) + device_release(&iface->main_dev); iface->state = IFS_DOWN; } static int -__set_interface_up(struct interface *iface) +__interface_set_up(struct interface *iface) { int ret; if (iface->state != IFS_DOWN) return 0; - ret = device_claim(iface->main_dev.dev); - if (ret) - return ret; + if (iface->main_dev.dev) { + ret = device_claim(&iface->main_dev); + if (ret) + return ret; + } iface->state = IFS_SETUP; ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false); @@ -119,7 +117,7 @@ __set_interface_up(struct interface *iface) } static void -__set_interface_down(struct interface *iface, bool force) +__interface_set_down(struct interface *iface, bool force) { clear_interface_errors(iface); @@ -129,8 +127,6 @@ __set_interface_down(struct interface *iface, bool force) iface->state = IFS_TEARDOWN; interface_event(iface, IFEV_DOWN); - - interface_del_all_routes(iface); interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force); } @@ -152,16 +148,22 @@ interface_cb(struct device_user *dep, enum device_event ev) return; } - if (iface->active == new_state) + interface_set_available(iface, new_state); +} + +void +interface_set_available(struct interface *iface, bool new_state) +{ + if (iface->available == new_state) return; - iface->active = new_state; + iface->available = new_state; if (new_state) { if (iface->autostart && !config_init) - set_interface_up(iface); + interface_set_up(iface); } else - __set_interface_down(iface, true); + __interface_set_down(iface, true); } static void @@ -183,6 +185,13 @@ interface_proto_cb(struct interface_proto_state *state, enum interface_proto_eve mark_interface_down(iface); break; + case IFPEV_LINK_LOST: + if (iface->state != IFS_UP) + return; + + iface->state = IFS_SETUP; + interface_event(iface, IFEV_DOWN); + break; } } @@ -202,68 +211,48 @@ void interface_set_proto_state(struct interface *iface, struct interface_proto_s state->iface = iface; } -struct interface * -alloc_interface(const char *name, struct uci_section *s, struct blob_attr *attr) +void +interface_init(struct interface *iface, const char *name, + struct blob_attr *config) { - struct interface *iface; struct blob_attr *tb[IFACE_ATTR_MAX]; struct blob_attr *cur; struct device *dev; + const char *proto_name = NULL; - iface = get_interface(name); - if (iface) - return iface; - - iface = calloc(1, sizeof(*iface)); - iface->main_dev.cb = interface_cb; - iface->l3_iface = &iface->main_dev; strncpy(iface->name, name, sizeof(iface->name) - 1); - list_add(&iface->list, &interfaces); INIT_LIST_HEAD(&iface->errors); - INIT_LIST_HEAD(&iface->address); - INIT_LIST_HEAD(&iface->routes); - - proto_attach_interface(iface, s); - netifd_ubus_add_interface(iface); + iface->main_dev.cb = interface_cb; + iface->l3_dev = &iface->main_dev; blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, - blob_data(attr), blob_len(attr)); + blob_data(config), blob_len(config)); - if ((cur = tb[IFACE_ATTR_TYPE])) { - if (!strcmp(blobmsg_data(cur), "bridge")) - interface_attach_bridge(iface, s); - } + if ((cur = tb[IFACE_ATTR_PROTO])) + proto_name = blobmsg_data(cur); - if ((cur = tb[IFACE_ATTR_IFNAME])) { + proto_attach_interface(iface, proto_name); + + if (iface->proto_handler && + !(iface->proto_handler->flags & PROTO_FLAG_NODEV) && + (cur = tb[IFACE_ATTR_IFNAME])) { dev = device_get(blobmsg_data(cur), true); if (dev) device_add_user(&iface->main_dev, dev); } - return iface; + if ((cur = tb[IFACE_ATTR_AUTO])) + iface->autostart = blobmsg_get_bool(cur); + else + iface->autostart = true; } void -free_interface(struct interface *iface) -{ - netifd_ubus_remove_interface(iface); - list_del(&iface->list); - if (iface->proto->free) - iface->proto->free(iface->proto); - free(iface); -} - -struct interface * -get_interface(const char *name) +interface_add(struct interface *iface, struct blob_attr *config) { - struct interface *iface; - - list_for_each_entry(iface, &interfaces, list) { - if (!strcmp(iface->name, name)) - return iface; - } - return NULL; + iface->config = config; + vlist_add(&interfaces, &iface->node); } void @@ -296,11 +285,11 @@ interface_add_link(struct interface *iface, struct device *dev) } int -set_interface_up(struct interface *iface) +interface_set_up(struct interface *iface) { iface->autostart = true; - if (!iface->active) { + if (!iface->available) { interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0); return -1; } @@ -308,25 +297,60 @@ set_interface_up(struct interface *iface) if (iface->state != IFS_DOWN) return 0; - return __set_interface_up(iface); + return __interface_set_up(iface); } int -set_interface_down(struct interface *iface) +interface_set_down(struct interface *iface) { - iface->autostart = false; - __set_interface_down(iface, false); + if (!iface) { + vlist_for_each_element(&interfaces, iface, node) + __interface_set_down(iface, false); + } else { + iface->autostart = false; + __interface_set_down(iface, false); + } return 0; } void -start_pending_interfaces(void) +interface_start_pending(void) { struct interface *iface; - list_for_each_entry(iface, &interfaces, list) { - if (iface->active && iface->autostart) - set_interface_up(iface); + vlist_for_each_element(&interfaces, iface, node) { + if (iface->available && iface->autostart) + interface_set_up(iface); + } +} + +static void +interface_update(struct vlist_tree *tree, struct vlist_node *node_new, + struct vlist_node *node_old) +{ + struct interface *if_old = container_of(node_old, struct interface, node); + struct interface *if_new = container_of(node_new, struct interface, node); + + if (node_old) { + free(if_old->config); + netifd_ubus_remove_interface(if_old); + if (if_old->proto->free) + if_old->proto->free(if_old->proto); + free(if_old); + } + + if (node_new) { + proto_init_interface(if_new, if_new->config); + interface_ip_init(if_new); + netifd_ubus_add_interface(if_new); } } + + +static void __init +interface_init_list(void) +{ + vlist_init(&interfaces, avl_strcmp, interface_update, + struct interface, node, name); +}