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