add code for versioned lists and use it to manage addresses and routes
[project/netifd.git] / interface.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4
5 #include "netifd.h"
6 #include "device.h"
7 #include "interface.h"
8 #include "interface-ip.h"
9 #include "proto.h"
10 #include "ubus.h"
11 #include "config.h"
12
13 static LIST_HEAD(interfaces);
14
15 enum {
16 IFACE_ATTR_IFNAME,
17 IFACE_ATTR_PROTO,
18 IFACE_ATTR_AUTO,
19 IFACE_ATTR_MAX
20 };
21
22 static const union config_param_info iface_attr_info[IFACE_ATTR_MAX] = {
23 [IFACE_ATTR_IFNAME].type = BLOBMSG_TYPE_STRING,
24 };
25
26 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
27 [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
28 [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
29 [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL },
30 };
31
32 const struct config_param_list interface_attr_list = {
33 .n_params = IFACE_ATTR_MAX,
34 .params = iface_attrs,
35 .info = iface_attr_info,
36 };
37
38 static void
39 clear_interface_errors(struct interface *iface)
40 {
41 struct interface_error *error, *tmp;
42
43 list_for_each_entry_safe(error, tmp, &iface->errors, list) {
44 list_del(&error->list);
45 free(error);
46 }
47 }
48
49 void interface_add_error(struct interface *iface, const char *subsystem,
50 const char *code, const char **data, int n_data)
51 {
52 struct interface_error *error;
53 int i, len = 0;
54 int *datalen = NULL;
55 char *dest;
56
57 if (n_data) {
58 len = n_data * sizeof(char *);
59 datalen = alloca(len);
60 for (i = 0; i < n_data; i++) {
61 datalen[i] = strlen(data[i]) + 1;
62 len += datalen[i];
63 }
64 }
65
66 error = calloc(1, sizeof(*error) + sizeof(char *) + len);
67 if (!error)
68 return;
69
70 list_add_tail(&error->list, &iface->errors);
71 error->subsystem = subsystem;
72 error->code = code;
73
74 dest = (char *) &error->data[n_data + 1];
75 for (i = 0; i < n_data; i++) {
76 error->data[i] = dest;
77 memcpy(dest, data[i], datalen[i]);
78 dest += datalen[i];
79 }
80 error->data[n_data] = NULL;
81 }
82
83 static void
84 interface_event(struct interface *iface, enum interface_event ev)
85 {
86 /* TODO */
87 }
88
89 static void
90 mark_interface_down(struct interface *iface)
91 {
92 vlist_flush_all(&iface->proto_addr);
93 vlist_flush_all(&iface->proto_route);
94 device_release(&iface->main_dev);
95 iface->state = IFS_DOWN;
96 }
97
98 static int
99 __interface_set_up(struct interface *iface)
100 {
101 int ret;
102
103 if (iface->state != IFS_DOWN)
104 return 0;
105
106 ret = device_claim(&iface->main_dev);
107 if (ret)
108 return ret;
109
110 iface->state = IFS_SETUP;
111 ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
112 if (ret) {
113 mark_interface_down(iface);
114 return ret;
115 }
116
117 return 0;
118
119 }
120
121 static void
122 __interface_set_down(struct interface *iface, bool force)
123 {
124 clear_interface_errors(iface);
125
126 if (iface->state == IFS_DOWN ||
127 iface->state == IFS_TEARDOWN)
128 return;
129
130 iface->state = IFS_TEARDOWN;
131 interface_event(iface, IFEV_DOWN);
132 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
133 }
134
135 static void
136 interface_cb(struct device_user *dep, enum device_event ev)
137 {
138 struct interface *iface;
139 bool new_state;
140
141 iface = container_of(dep, struct interface, main_dev);
142 switch (ev) {
143 case DEV_EVENT_ADD:
144 new_state = true;
145 break;
146 case DEV_EVENT_REMOVE:
147 new_state = false;
148 break;
149 default:
150 return;
151 }
152
153 if (iface->active == new_state)
154 return;
155
156 iface->active = new_state;
157
158 if (new_state) {
159 if (iface->autostart && !config_init)
160 interface_set_up(iface);
161 } else
162 __interface_set_down(iface, true);
163 }
164
165 static void
166 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
167 {
168 struct interface *iface = state->iface;
169
170 switch (ev) {
171 case IFPEV_UP:
172 if (iface->state != IFS_SETUP)
173 return;
174
175 iface->state = IFS_UP;
176 interface_event(iface, IFEV_UP);
177 break;
178 case IFPEV_DOWN:
179 if (iface->state == IFS_DOWN)
180 return;
181
182 mark_interface_down(iface);
183 break;
184 }
185 }
186
187 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
188 {
189 if (iface->proto) {
190 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, true);
191 iface->proto->free(iface->proto);
192 iface->proto = NULL;
193 }
194 iface->state = IFS_DOWN;
195 iface->proto = state;
196 if (!state)
197 return;
198
199 state->proto_event = interface_proto_cb;
200 state->iface = iface;
201 }
202
203 struct interface *
204 interface_alloc(const char *name, struct blob_attr *attr)
205 {
206 struct interface *iface;
207 struct blob_attr *tb[IFACE_ATTR_MAX];
208 struct blob_attr *cur;
209 struct device *dev;
210 const char *proto_name = NULL;
211
212 iface = interface_get(name);
213 if (iface)
214 return iface;
215
216 iface = calloc(1, sizeof(*iface));
217 iface->main_dev.cb = interface_cb;
218 iface->l3_iface = &iface->main_dev;
219 strncpy(iface->name, name, sizeof(iface->name) - 1);
220 list_add_tail(&iface->list, &interfaces);
221 INIT_LIST_HEAD(&iface->errors);
222
223 interface_ip_init(iface);
224
225 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
226 blob_data(attr), blob_len(attr));
227
228 if ((cur = tb[IFACE_ATTR_PROTO]))
229 proto_name = blobmsg_data(cur);
230
231 proto_attach_interface(iface, proto_name);
232
233 if ((cur = tb[IFACE_ATTR_IFNAME])) {
234 dev = device_get(blobmsg_data(cur), true);
235 if (dev)
236 device_add_user(&iface->main_dev, dev);
237 }
238
239 if ((cur = tb[IFACE_ATTR_AUTO]))
240 iface->autostart = blobmsg_get_bool(cur);
241 else
242 iface->autostart = true;
243
244 netifd_ubus_add_interface(iface);
245 config_set_state(&iface->config, attr);
246
247 return iface;
248 }
249
250 void
251 interface_free(struct interface *iface)
252 {
253 netifd_ubus_remove_interface(iface);
254 list_del(&iface->list);
255 if (iface->proto->free)
256 iface->proto->free(iface->proto);
257 free(iface);
258 }
259
260 struct interface *
261 interface_get(const char *name)
262 {
263 struct interface *iface;
264
265 list_for_each_entry(iface, &interfaces, list) {
266 if (!strcmp(iface->name, name))
267 return iface;
268 }
269 return NULL;
270 }
271
272 void
273 interface_remove_link(struct interface *iface, struct device *dev)
274 {
275 struct device *mdev = iface->main_dev.dev;
276
277 if (mdev && mdev->hotplug_ops) {
278 mdev->hotplug_ops->del(mdev, dev);
279 return;
280 }
281
282 device_remove_user(&iface->main_dev);
283 }
284
285 int
286 interface_add_link(struct interface *iface, struct device *dev)
287 {
288 struct device *mdev = iface->main_dev.dev;
289
290 if (mdev && mdev->hotplug_ops)
291 return mdev->hotplug_ops->add(mdev, dev);
292
293 if (iface->main_dev.dev)
294 interface_remove_link(iface, NULL);
295
296 device_add_user(&iface->main_dev, dev);
297
298 return 0;
299 }
300
301 int
302 interface_set_up(struct interface *iface)
303 {
304 iface->autostart = true;
305
306 if (!iface->active) {
307 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
308 return -1;
309 }
310
311 if (iface->state != IFS_DOWN)
312 return 0;
313
314 return __interface_set_up(iface);
315 }
316
317 int
318 interface_set_down(struct interface *iface)
319 {
320 iface->autostart = false;
321 __interface_set_down(iface, false);
322
323 return 0;
324 }
325
326 void
327 interface_start_pending(void)
328 {
329 struct interface *iface;
330
331 list_for_each_entry(iface, &interfaces, list) {
332 if (iface->active && iface->autostart)
333 interface_set_up(iface);
334 }
335 }