s/remove_device_user/device_remove_user/g
[project/netifd.git] / vlan.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4
5 #include "netifd.h"
6 #include "system.h"
7
8 struct vlan_device {
9 struct device dev;
10 struct device_user dep;
11
12 device_state_cb set_state;
13 int id;
14 };
15
16 static void free_vlan_if(struct device *iface)
17 {
18 struct vlan_device *vldev;
19
20 vldev = container_of(iface, struct vlan_device, dev);
21 device_remove_user(&vldev->dep);
22 cleanup_device(&vldev->dev);
23 free(vldev);
24 }
25
26 static int vlan_set_device_state(struct device *dev, bool up)
27 {
28 struct vlan_device *vldev;
29 int ret = 0;
30
31 vldev = container_of(dev, struct vlan_device, dev);
32 if (!up) {
33 vldev->set_state(dev, false);
34 system_vlan_del(dev);
35 release_device(vldev->dep.dev);
36 return 0;
37 }
38
39 ret = claim_device(vldev->dep.dev);
40 if (ret)
41 return ret;
42
43 system_vlan_add(vldev->dep.dev, vldev->id);
44 ret = vldev->set_state(dev, true);
45 if (ret)
46 release_device(vldev->dep.dev);
47
48 return ret;
49 }
50
51 static void vlan_dev_cb(struct device_user *dep, enum device_event ev)
52 {
53 struct vlan_device *vldev;
54
55 vldev = container_of(dep, struct vlan_device, dep);
56 switch(ev) {
57 case DEV_EVENT_ADD:
58 set_device_present(&vldev->dev, true);
59 break;
60 case DEV_EVENT_REMOVE:
61 set_device_present(&vldev->dev, false);
62 break;
63 default:
64 break;
65 }
66 }
67
68 static struct device *get_vlan_device(struct device *dev, int id, bool create)
69 {
70 static const struct device_type vlan_type = {
71 .name = "VLAN",
72 .free = free_vlan_if,
73 };
74 struct vlan_device *vldev;
75 struct device_user *dep;
76
77 /* look for an existing interface before creating a new one */
78 list_for_each_entry(dep, &dev->users, list) {
79 if (dep->cb != vlan_dev_cb)
80 continue;
81
82 vldev = container_of(dep, struct vlan_device, dep);
83 if (vldev->id != id)
84 continue;
85
86 return &vldev->dev;
87 }
88
89 if (!create)
90 return NULL;
91
92 vldev = calloc(1, sizeof(*vldev));
93 snprintf(vldev->dev.ifname, IFNAMSIZ, "%s.%d", dev->ifname, id);
94
95 init_device(&vldev->dev, &vlan_type, NULL);
96
97 vldev->set_state = vldev->dev.set_state;
98 vldev->dev.set_state = vlan_set_device_state;
99
100 vldev->id = id;
101
102 vldev->dep.cb = vlan_dev_cb;
103 add_device_user(&vldev->dep, dev);
104
105 return &vldev->dev;
106 }
107
108 static char *split_vlan(char *s)
109 {
110 s = strchr(s, '.');
111 if (!s)
112 goto out;
113
114 *s = 0;
115 s++;
116
117 out:
118 return s;
119 }
120
121 struct device *get_vlan_device_chain(const char *ifname, bool create)
122 {
123 struct device *iface = NULL;
124 char *buf, *s, *next, *err = NULL;
125 int id;
126
127 buf = strdup(ifname);
128 if (!buf)
129 return NULL;
130
131 s = split_vlan(buf);
132 iface = get_device(buf, create);
133 if (!iface && !create)
134 goto error;
135
136 do {
137 next = split_vlan(s);
138 id = strtoul(s, &err, 10);
139 if (err && *err)
140 goto error;
141
142 iface = get_vlan_device(iface, id, create);
143 if (!iface)
144 goto error;
145
146 s = next;
147 if (!s)
148 goto out;
149 } while (1);
150
151 error:
152 iface = NULL;
153 out:
154 free(buf);
155 return iface;
156 }