clean up interface state change handling
[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 #include "system.h"
13
14 struct vlist_tree interfaces;
15
16 enum {
17 IFACE_ATTR_IFNAME,
18 IFACE_ATTR_PROTO,
19 IFACE_ATTR_AUTO,
20 IFACE_ATTR_MAX
21 };
22
23 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
24 [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
25 [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
26 [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL },
27 };
28
29 const struct config_param_list interface_attr_list = {
30 .n_params = IFACE_ATTR_MAX,
31 .params = iface_attrs,
32 };
33
34 static void
35 interface_clear_errors(struct interface *iface)
36 {
37 struct interface_error *error, *tmp;
38
39 list_for_each_entry_safe(error, tmp, &iface->errors, list) {
40 list_del(&error->list);
41 free(error);
42 }
43 }
44
45 void interface_add_error(struct interface *iface, const char *subsystem,
46 const char *code, const char **data, int n_data)
47 {
48 struct interface_error *error;
49 int i, len = 0;
50 int *datalen = NULL;
51 char *dest;
52
53 if (n_data) {
54 len = n_data * sizeof(char *);
55 datalen = alloca(len);
56 for (i = 0; i < n_data; i++) {
57 datalen[i] = strlen(data[i]) + 1;
58 len += datalen[i];
59 }
60 }
61
62 error = calloc(1, sizeof(*error) + sizeof(char *) + len);
63 if (!error)
64 return;
65
66 list_add_tail(&error->list, &iface->errors);
67 error->subsystem = subsystem;
68 error->code = code;
69
70 dest = (char *) &error->data[n_data + 1];
71 for (i = 0; i < n_data; i++) {
72 error->data[i] = dest;
73 memcpy(dest, data[i], datalen[i]);
74 dest += datalen[i];
75 }
76 error->data[n_data] = NULL;
77 }
78
79 static void
80 interface_event(struct interface *iface, enum interface_event ev)
81 {
82 struct interface_user *dep, *tmp;
83
84 list_for_each_entry_safe(dep, tmp, &iface->users, list)
85 dep->cb(dep, IFEV_UP);
86
87 interface_queue_event(iface, ev);
88 }
89
90 static void
91 interface_flush_state(struct interface *iface)
92 {
93 interface_ip_flush(&iface->proto_ip);
94 if (iface->main_dev.dev)
95 device_release(&iface->main_dev);
96 if (iface->l3_dev != &iface->main_dev && iface->l3_dev->dev)
97 device_release(iface->l3_dev);
98 }
99
100 static void
101 mark_interface_down(struct interface *iface)
102 {
103 interface_flush_state(iface);
104 iface->state = IFS_DOWN;
105 }
106
107 static int
108 __interface_set_up(struct interface *iface)
109 {
110 int ret;
111
112 if (iface->state != IFS_DOWN)
113 return 0;
114
115 if (iface->main_dev.dev) {
116 ret = device_claim(&iface->main_dev);
117 if (ret)
118 return ret;
119 }
120
121 iface->state = IFS_SETUP;
122 ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
123 if (ret) {
124 mark_interface_down(iface);
125 return ret;
126 }
127
128 return 0;
129 }
130
131 void
132 __interface_set_down(struct interface *iface, bool force)
133 {
134 interface_clear_errors(iface);
135
136 if (iface->state == IFS_DOWN ||
137 iface->state == IFS_TEARDOWN)
138 return;
139
140 iface->state = IFS_TEARDOWN;
141 interface_event(iface, IFEV_DOWN);
142 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
143 if (force)
144 interface_flush_state(iface);
145 }
146
147 static void
148 interface_cb(struct device_user *dep, enum device_event ev)
149 {
150 struct interface *iface;
151 bool new_state;
152
153 iface = container_of(dep, struct interface, main_dev);
154 switch (ev) {
155 case DEV_EVENT_ADD:
156 new_state = true;
157 break;
158 case DEV_EVENT_REMOVE:
159 new_state = false;
160 break;
161 default:
162 return;
163 }
164
165 interface_set_available(iface, new_state);
166 }
167
168 void
169 interface_set_available(struct interface *iface, bool new_state)
170 {
171 if (iface->available == new_state)
172 return;
173
174 D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
175 iface->available = new_state;
176
177 if (new_state) {
178 if (iface->autostart && !config_init)
179 interface_set_up(iface);
180 } else
181 __interface_set_down(iface, true);
182 }
183
184 void
185 interface_add_user(struct interface_user *dep, struct interface *iface)
186 {
187 dep->iface = iface;
188 list_add(&dep->list, &iface->users);
189 if (iface->state == IFS_UP)
190 dep->cb(dep, IFEV_UP);
191 }
192
193 void
194 interface_remove_user(struct interface_user *dep)
195 {
196 list_del_init(&dep->list);
197 dep->iface = NULL;
198 }
199
200 static void
201 interface_claim_device(struct interface *iface)
202 {
203 struct device *dev;
204
205 if (iface->ifname && iface->proto_handler &&
206 !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
207 dev = device_get(iface->ifname, true);
208 if (dev)
209 device_add_user(&iface->main_dev, dev);
210 }
211 }
212
213
214 static void
215 interface_cleanup(struct interface *iface)
216 {
217 struct interface_user *dep, *tmp;
218
219 list_for_each_entry_safe(dep, tmp, &iface->users, list)
220 interface_remove_user(dep);
221
222 interface_ip_flush(&iface->config_ip);
223 interface_flush_state(iface);
224 interface_clear_errors(iface);
225 if (iface->main_dev.dev)
226 device_remove_user(&iface->main_dev);
227 iface->l3_dev = &iface->main_dev;
228 interface_set_proto_state(iface, NULL);
229 }
230
231 static void
232 interface_do_free(struct interface *iface)
233 {
234 interface_cleanup(iface);
235 free(iface->config);
236 netifd_ubus_remove_interface(iface);
237 avl_delete(&interfaces.avl, &iface->node.avl);
238 free(iface);
239 }
240
241 static void
242 interface_do_reload(struct interface *iface)
243 {
244 interface_cleanup(iface);
245
246 interface_claim_device(iface);
247 proto_init_interface(iface, iface->config);
248 }
249
250 static void
251 interface_handle_config_change(struct interface *iface)
252 {
253 switch(iface->config_state) {
254 case IFC_NORMAL:
255 break;
256 case IFC_RELOAD:
257 interface_do_reload(iface);
258 break;
259 case IFC_REMOVE:
260 interface_do_free(iface);
261 return;
262 }
263 if (iface->autostart && iface->available)
264 interface_set_up(iface);
265 }
266
267 static void
268 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
269 {
270 struct interface *iface = state->iface;
271
272 switch (ev) {
273 case IFPEV_UP:
274 if (iface->state != IFS_SETUP)
275 return;
276
277 interface_ip_set_enabled(&iface->config_ip, true);
278 system_flush_routes();
279 iface->state = IFS_UP;
280 iface->start_time = system_get_rtime();
281 interface_event(iface, IFEV_UP);
282 interface_write_resolv_conf();
283 netifd_log_message(L_NOTICE, "Interface '%s' is now up\n", iface->name);
284 break;
285 case IFPEV_DOWN:
286 if (iface->state == IFS_DOWN)
287 return;
288
289 netifd_log_message(L_NOTICE, "Interface '%s' is now down\n", iface->name);
290 interface_ip_set_enabled(&iface->config_ip, false);
291 system_flush_routes();
292 mark_interface_down(iface);
293 interface_handle_config_change(iface);
294 break;
295 case IFPEV_LINK_LOST:
296 if (iface->state != IFS_UP)
297 return;
298
299 netifd_log_message(L_NOTICE, "Interface '%s' has lost the connection\n", iface->name);
300 iface->state = IFS_SETUP;
301 interface_event(iface, IFEV_DOWN);
302 break;
303 }
304 }
305
306 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
307 {
308 if (iface->proto) {
309 iface->proto->free(iface->proto);
310 iface->proto = NULL;
311 }
312 iface->state = IFS_DOWN;
313 iface->proto = state;
314 if (!state)
315 return;
316
317 state->proto_event = interface_proto_cb;
318 state->iface = iface;
319 }
320
321 void
322 interface_init(struct interface *iface, const char *name,
323 struct blob_attr *config)
324 {
325 struct blob_attr *tb[IFACE_ATTR_MAX];
326 struct blob_attr *cur;
327 const char *proto_name = NULL;
328
329 strncpy(iface->name, name, sizeof(iface->name) - 1);
330 INIT_LIST_HEAD(&iface->errors);
331 INIT_LIST_HEAD(&iface->users);
332 INIT_LIST_HEAD(&iface->hotplug_list);
333 interface_ip_init(&iface->proto_ip, iface);
334 interface_ip_init(&iface->config_ip, iface);
335 iface->config_ip.enabled = false;
336
337 iface->main_dev.cb = interface_cb;
338 iface->l3_dev = &iface->main_dev;
339
340 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
341 blob_data(config), blob_len(config));
342
343 if ((cur = tb[IFACE_ATTR_PROTO]))
344 proto_name = blobmsg_data(cur);
345
346 proto_attach_interface(iface, proto_name);
347
348 if ((cur = tb[IFACE_ATTR_AUTO]))
349 iface->autostart = blobmsg_get_bool(cur);
350 else
351 iface->autostart = true;
352 iface->config_autostart = iface->autostart;
353 }
354
355 void
356 interface_add(struct interface *iface, struct blob_attr *config)
357 {
358 struct blob_attr *tb[IFACE_ATTR_MAX];
359 struct blob_attr *cur;
360
361 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
362 blob_data(config), blob_len(config));
363
364 if ((cur = tb[IFACE_ATTR_IFNAME]))
365 iface->ifname = blobmsg_data(cur);
366
367 iface->config = config;
368 vlist_add(&interfaces, &iface->node);
369 }
370
371 void
372 interface_remove_link(struct interface *iface, struct device *dev)
373 {
374 struct device *mdev = iface->main_dev.dev;
375
376 if (mdev && mdev->hotplug_ops) {
377 mdev->hotplug_ops->del(mdev, dev);
378 return;
379 }
380
381 device_remove_user(&iface->main_dev);
382 }
383
384 int
385 interface_add_link(struct interface *iface, struct device *dev)
386 {
387 struct device *mdev = iface->main_dev.dev;
388
389 if (mdev && mdev->hotplug_ops)
390 return mdev->hotplug_ops->add(mdev, dev);
391
392 if (iface->main_dev.dev)
393 interface_remove_link(iface, NULL);
394
395 device_add_user(&iface->main_dev, dev);
396
397 return 0;
398 }
399
400 int
401 interface_set_up(struct interface *iface)
402 {
403 iface->autostart = true;
404
405 if (iface->state != IFS_DOWN)
406 return 0;
407
408 interface_clear_errors(iface);
409 if (!iface->available) {
410 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
411 return -1;
412 }
413
414 return __interface_set_up(iface);
415 }
416
417 int
418 interface_set_down(struct interface *iface)
419 {
420 if (!iface) {
421 vlist_for_each_element(&interfaces, iface, node)
422 __interface_set_down(iface, false);
423 } else {
424 iface->autostart = false;
425 __interface_set_down(iface, false);
426 }
427
428 return 0;
429 }
430
431 void
432 interface_start_pending(void)
433 {
434 struct interface *iface;
435
436 vlist_for_each_element(&interfaces, iface, node) {
437 if (iface->available && iface->autostart)
438 interface_set_up(iface);
439 }
440 }
441
442 static void
443 set_config_state(struct interface *iface, enum interface_config_state s)
444 {
445 iface->config_state = s;
446 if (iface->state == IFS_DOWN)
447 interface_handle_config_change(iface);
448 else
449 __interface_set_down(iface, false);
450 }
451
452 void
453 interface_update_start(struct interface *iface)
454 {
455 interface_ip_update_start(&iface->proto_ip);
456 }
457
458 void
459 interface_update_complete(struct interface *iface)
460 {
461 struct device_route *route;
462
463 interface_ip_update_complete(&iface->proto_ip);
464 vlist_for_each_element(&iface->config_ip.route, route, node) {
465 if (iface->l3_dev->dev) {
466 system_add_route(iface->l3_dev->dev, route);
467 route->enabled = true;
468 }
469 }
470 }
471
472 static void
473 interface_change_config(struct interface *if_old, struct interface *if_new)
474 {
475 struct blob_attr *old_config = if_old->config;
476 const char *old_ifname = if_old->ifname;
477 const struct proto_handler *proto = if_old->proto_handler;
478
479 interface_clear_errors(if_old);
480 if_old->config = if_new->config;
481 if (!if_old->config_autostart && if_new->config_autostart)
482 if_old->autostart = true;
483
484 if_old->config_autostart = if_new->config_autostart;
485 if_old->ifname = if_new->ifname;
486 if_old->proto_handler = if_new->proto_handler;
487
488 if (strcmp(old_ifname, if_new->ifname) != 0 ||
489 proto != if_new->proto_handler) {
490 D(INTERFACE, "Reload interface '%s' because of ifname/proto change\n",
491 if_old->name);
492 goto reload;
493 }
494
495 if (!proto->config_params)
496 D(INTERFACE, "No config parameters for interface '%s'\n",
497 if_old->name);
498 else if (!config_check_equal(old_config, if_new->config,
499 proto->config_params)) {
500 D(INTERFACE, "Reload interface '%s because of config changes\n",
501 if_old->name);
502 goto reload;
503 }
504
505 goto out;
506
507 reload:
508 set_config_state(if_old, IFC_RELOAD);
509 out:
510 free(old_config);
511 free(if_new);
512 }
513
514 static void
515 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
516 struct vlist_node *node_old)
517 {
518 struct interface *if_old = container_of(node_old, struct interface, node);
519 struct interface *if_new = container_of(node_new, struct interface, node);
520
521 if (node_old && node_new) {
522 D(INTERFACE, "Update interface '%s'\n", if_new->name);
523 interface_change_config(if_old, if_new);
524 } else if (node_old) {
525 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
526 set_config_state(if_old, IFC_REMOVE);
527 } else if (node_new) {
528 D(INTERFACE, "Create interface '%s'\n", if_new->name);
529 interface_claim_device(if_new);
530 proto_init_interface(if_new, if_new->config);
531 netifd_ubus_add_interface(if_new);
532 }
533 }
534
535
536 static void __init
537 interface_init_list(void)
538 {
539 vlist_init(&interfaces, avl_strcmp, interface_update,
540 struct interface, node, name);
541 interfaces.keep_old = true;
542 interfaces.no_delete = true;
543 }