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.
23 struct device_user dep;
25 device_state_cb set_state;
29 static void free_vlan_if(struct device *iface)
31 struct vlan_device *vldev;
33 vldev = container_of(iface, struct vlan_device, dev);
34 device_remove_user(&vldev->dep);
35 device_cleanup(&vldev->dev);
39 static int vlan_set_device_state(struct device *dev, bool up)
41 struct vlan_device *vldev;
44 vldev = container_of(dev, struct vlan_device, dev);
46 vldev->set_state(dev, false);
48 device_release(&vldev->dep);
52 ret = device_claim(&vldev->dep);
56 system_vlan_add(vldev->dep.dev, vldev->id);
57 ret = vldev->set_state(dev, true);
59 device_release(&vldev->dep);
64 static void vlan_dev_set_name(struct vlan_device *vldev, struct device *dev)
66 vldev->dev.hidden = dev->hidden;
67 snprintf(vldev->dev.ifname, IFNAMSIZ, "%s.%d", dev->ifname, vldev->id);
70 static void vlan_dev_cb(struct device_user *dep, enum device_event ev)
72 struct vlan_device *vldev;
73 bool new_state = false;
75 vldev = container_of(dep, struct vlan_device, dep);
79 case DEV_EVENT_REMOVE:
80 device_set_present(&vldev->dev, new_state);
82 case DEV_EVENT_LINK_UP:
84 case DEV_EVENT_LINK_DOWN:
85 device_set_link(&vldev->dev, new_state);
87 case DEV_EVENT_UPDATE_IFNAME:
88 vlan_dev_set_name(vldev, dep->dev);
89 device_broadcast_event(&vldev->dev, ev);
91 case DEV_EVENT_TOPO_CHANGE:
92 /* Propagate topo changes */
93 device_broadcast_event(&vldev->dev, DEV_EVENT_TOPO_CHANGE);
100 static struct device *get_vlan_device(struct device *dev, int id, bool create)
102 static const struct device_type vlan_type = {
104 .config_params = &device_attr_list,
105 .keep_link_status = true,
106 .free = free_vlan_if,
108 struct vlan_device *vldev;
109 struct device_user *dep;
111 /* look for an existing interface before creating a new one */
112 list_for_each_entry(dep, &dev->users.list, list.list) {
113 if (dep->cb != vlan_dev_cb)
116 vldev = container_of(dep, struct vlan_device, dep);
126 D(DEVICE, "Create vlan device '%s.%d'\n", dev->ifname, id);
128 vldev = calloc(1, sizeof(*vldev));
131 vlan_dev_set_name(vldev, dev);
133 device_init_virtual(&vldev->dev, &vlan_type, NULL);
134 vldev->dev.default_config = true;
136 vldev->set_state = vldev->dev.set_state;
137 vldev->dev.set_state = vlan_set_device_state;
139 vldev->dep.cb = vlan_dev_cb;
140 device_add_user(&vldev->dep, dev);
145 static char *split_vlan(char *s)
158 struct device *get_vlan_device_chain(const char *ifname, bool create)
160 struct device *dev = NULL;
161 char *buf, *s, *next, *err = NULL;
164 buf = strdup(ifname);
169 dev = device_get(buf, create);
174 next = split_vlan(s);
175 id = strtoul(s, &err, 10);
179 dev = get_vlan_device(dev, id, create);