CMake: bump the minimum required CMake version to 3.5
[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 int vlan_dev_set_name(struct vlan_device *vldev, struct device *dev)
65 {
66 char *name;
67
68 name = alloca(strlen(dev->ifname) + sizeof(".2147483647\0"));
69 vldev->dev.hidden = dev->hidden;
70 sprintf(name, "%s.%d", dev->ifname, vldev->id);
71
72 return device_set_ifname(&vldev->dev, name);
73 }
74
75 static void vlan_dev_cb(struct device_user *dep, enum device_event ev)
76 {
77 struct vlan_device *vldev;
78
79 vldev = container_of(dep, struct vlan_device, dep);
80 switch(ev) {
81 case DEV_EVENT_ADD:
82 device_set_present(&vldev->dev, true);
83 break;
84 case DEV_EVENT_REMOVE:
85 device_set_present(&vldev->dev, false);
86 break;
87 case DEV_EVENT_UPDATE_IFNAME:
88 if (vlan_dev_set_name(vldev, dep->dev) < 0)
89 free_vlan_if(&vldev->dev);
90 break;
91 case DEV_EVENT_TOPO_CHANGE:
92 /* Propagate topo changes */
93 device_broadcast_event(&vldev->dev, DEV_EVENT_TOPO_CHANGE);
94 break;
95 default:
96 break;
97 }
98 }
99
100 static struct device *get_vlan_device(struct device *dev, int id, bool create)
101 {
102 static struct device_type vlan_type = {
103 .name = "VLAN",
104 .config_params = &device_attr_list,
105 .free = free_vlan_if,
106 };
107 struct vlan_device *vldev;
108 struct device_user *dep;
109
110 /* look for an existing interface before creating a new one */
111 list_for_each_entry(dep, &dev->users.list, list.list) {
112 if (dep->cb != vlan_dev_cb)
113 continue;
114
115 vldev = container_of(dep, struct vlan_device, dep);
116 if (vldev->id != id)
117 continue;
118
119 return &vldev->dev;
120 }
121
122 if (!create)
123 return NULL;
124
125 D(DEVICE, "Create vlan device '%s.%d'\n", dev->ifname, id);
126
127 vldev = calloc(1, sizeof(*vldev));
128 if (!vldev)
129 return NULL;
130
131 vldev->id = id;
132
133 if (device_init(&vldev->dev, &vlan_type, NULL) < 0)
134 goto error;
135
136 if (vlan_dev_set_name(vldev, dev) < 0)
137 goto error;
138
139 vldev->dev.default_config = true;
140
141 vldev->set_state = vldev->dev.set_state;
142 vldev->dev.set_state = vlan_set_device_state;
143
144 vldev->dep.cb = vlan_dev_cb;
145 device_add_user(&vldev->dep, dev);
146
147 return &vldev->dev;
148
149 error:
150 device_cleanup(&vldev->dev);
151 free(vldev);
152 return NULL;
153 }
154
155 static char *split_vlan(char *s)
156 {
157 s = strchr(s, '.');
158 if (!s)
159 goto out;
160
161 *s = 0;
162 s++;
163
164 out:
165 return s;
166 }
167
168 struct device *get_vlan_device_chain(const char *ifname, bool create)
169 {
170 struct device *dev = NULL;
171 char *buf, *s, *next, *err = NULL;
172 int id;
173
174 buf = strdup(ifname);
175 if (!buf)
176 return NULL;
177
178 s = split_vlan(buf);
179 dev = device_get(buf, create);
180 if (!dev)
181 goto error;
182
183 do {
184 next = split_vlan(s);
185 id = strtoul(s, &err, 10);
186 if (err && *err)
187 goto error;
188
189 dev = get_vlan_device(dev, id, create);
190 if (!dev)
191 goto error;
192
193 s = next;
194 if (!s)
195 goto out;
196 } while (1);
197
198 error:
199 dev = NULL;
200 out:
201 free(buf);
202 return dev;
203 }