interface: proto_ip: order by address index first
[project/netifd.git] / vlan.c
1 /*
2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
4 *
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
8 *
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.
13 */
14 #include <string.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17
18 #include "netifd.h"
19 #include "system.h"
20
21 struct vlan_device {
22 struct device dev;
23 struct device_user dep;
24
25 device_state_cb set_state;
26 int id;
27 };
28
29 static void free_vlan_if(struct device *iface)
30 {
31 struct vlan_device *vldev;
32
33 vldev = container_of(iface, struct vlan_device, dev);
34 device_remove_user(&vldev->dep);
35 device_cleanup(&vldev->dev);
36 free(vldev);
37 }
38
39 static int vlan_set_device_state(struct device *dev, bool up)
40 {
41 struct vlan_device *vldev;
42 int ret = 0;
43
44 vldev = container_of(dev, struct vlan_device, dev);
45 if (!up) {
46 vldev->set_state(dev, false);
47 system_vlan_del(dev);
48 device_release(&vldev->dep);
49 return 0;
50 }
51
52 ret = device_claim(&vldev->dep);
53 if (ret < 0)
54 return ret;
55
56 system_vlan_add(vldev->dep.dev, vldev->id);
57 ret = vldev->set_state(dev, true);
58 if (ret)
59 device_release(&vldev->dep);
60
61 return ret;
62 }
63
64 static void vlan_dev_cb(struct device_user *dep, enum device_event ev)
65 {
66 char name[IFNAMSIZ + 1];
67 struct vlan_device *vldev;
68
69 vldev = container_of(dep, struct vlan_device, dep);
70 switch(ev) {
71 case DEV_EVENT_ADD:
72 device_set_present(&vldev->dev, true);
73 break;
74 case DEV_EVENT_REMOVE:
75 device_set_present(&vldev->dev, false);
76 break;
77 case DEV_EVENT_UPDATE_IFNAME:
78 vldev->dev.hidden = dep->dev->hidden;
79 if (snprintf(name, sizeof(name), "%s.%d", dep->dev->ifname,
80 vldev->id) >= sizeof(name) - 1 ||
81 device_set_ifname(&vldev->dev, name))
82 free_vlan_if(&vldev->dev);
83 break;
84 case DEV_EVENT_TOPO_CHANGE:
85 /* Propagate topo changes */
86 device_broadcast_event(&vldev->dev, DEV_EVENT_TOPO_CHANGE);
87 break;
88 default:
89 break;
90 }
91 }
92
93 static struct device *get_vlan_device(struct device *dev, int id, bool create)
94 {
95 static struct device_type vlan_type = {
96 .name = "VLAN",
97 .config_params = &device_attr_list,
98 .free = free_vlan_if,
99 };
100 struct vlan_device *vldev;
101 struct device_user *dep;
102 char name[IFNAMSIZ + 1];
103
104 /* look for an existing interface before creating a new one */
105 list_for_each_entry(dep, &dev->users.list, list.list) {
106 if (dep->cb != vlan_dev_cb)
107 continue;
108
109 vldev = container_of(dep, struct vlan_device, dep);
110 if (vldev->id != id)
111 continue;
112
113 return &vldev->dev;
114 }
115
116 if (!create)
117 return NULL;
118
119 if (snprintf(name, sizeof(name), "%s.%d", dev->ifname, id) >= sizeof(name) - 1)
120 return NULL;
121
122 D(DEVICE, "Create vlan device '%s'\n", name);
123
124 vldev = calloc(1, sizeof(*vldev));
125 if (!vldev)
126 return NULL;
127
128 vldev->id = id;
129 vldev->dev.hidden = dev->hidden;
130 strcpy(vldev->dev.ifname, name);
131
132 if (device_init(&vldev->dev, &vlan_type, NULL) < 0)
133 goto error;
134
135 vldev->dev.default_config = true;
136
137 vldev->set_state = vldev->dev.set_state;
138 vldev->dev.set_state = vlan_set_device_state;
139
140 vldev->dep.cb = vlan_dev_cb;
141 device_add_user(&vldev->dep, dev);
142
143 return &vldev->dev;
144
145 error:
146 device_cleanup(&vldev->dev);
147 free(vldev);
148 return NULL;
149 }
150
151 static char *split_vlan(char *s)
152 {
153 s = strchr(s, '.');
154 if (!s)
155 goto out;
156
157 *s = 0;
158 s++;
159
160 out:
161 return s;
162 }
163
164 struct device *get_vlan_device_chain(const char *ifname, bool create)
165 {
166 struct device *dev = NULL;
167 char *buf, *s, *next, *err = NULL;
168 int id;
169
170 buf = strdup(ifname);
171 if (!buf)
172 return NULL;
173
174 s = split_vlan(buf);
175 dev = device_get(buf, create);
176 if (!dev)
177 goto error;
178
179 do {
180 next = split_vlan(s);
181 id = strtoul(s, &err, 10);
182 if (err && *err)
183 goto error;
184
185 dev = get_vlan_device(dev, id, create);
186 if (!dev)
187 goto error;
188
189 s = next;
190 if (!s)
191 goto out;
192 } while (1);
193
194 error:
195 dev = NULL;
196 out:
197 free(buf);
198 return dev;
199 }