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