interface: fix memory leak on error in __interface_add()
[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_PEERDNS,
36 IFACE_ATTR_DNS,
37 IFACE_ATTR_DNS_SEARCH,
38 IFACE_ATTR_DNS_METRIC,
39 IFACE_ATTR_METRIC,
40 IFACE_ATTR_INTERFACE,
41 IFACE_ATTR_IP6ASSIGN,
42 IFACE_ATTR_IP6HINT,
43 IFACE_ATTR_IP4TABLE,
44 IFACE_ATTR_IP6TABLE,
45 IFACE_ATTR_IP6CLASS,
46 IFACE_ATTR_DELEGATE,
47 IFACE_ATTR_IP6IFACEID,
48 IFACE_ATTR_FORCE_LINK,
49 IFACE_ATTR_IP6WEIGHT,
50 IFACE_ATTR_MAX
51 };
52
53 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
54 [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
55 [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
56 [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL },
57 [IFACE_ATTR_DEFAULTROUTE] = { .name = "defaultroute", .type = BLOBMSG_TYPE_BOOL },
58 [IFACE_ATTR_PEERDNS] = { .name = "peerdns", .type = BLOBMSG_TYPE_BOOL },
59 [IFACE_ATTR_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
60 [IFACE_ATTR_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
61 [IFACE_ATTR_DNS_SEARCH] = { .name = "dns_search", .type = BLOBMSG_TYPE_ARRAY },
62 [IFACE_ATTR_DNS_METRIC] = { .name = "dns_metric", .type = BLOBMSG_TYPE_INT32 },
63 [IFACE_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
64 [IFACE_ATTR_IP6ASSIGN] = { .name = "ip6assign", .type = BLOBMSG_TYPE_INT32 },
65 [IFACE_ATTR_IP6HINT] = { .name = "ip6hint", .type = BLOBMSG_TYPE_STRING },
66 [IFACE_ATTR_IP4TABLE] = { .name = "ip4table", .type = BLOBMSG_TYPE_STRING },
67 [IFACE_ATTR_IP6TABLE] = { .name = "ip6table", .type = BLOBMSG_TYPE_STRING },
68 [IFACE_ATTR_IP6CLASS] = { .name = "ip6class", .type = BLOBMSG_TYPE_ARRAY },
69 [IFACE_ATTR_DELEGATE] = { .name = "delegate", .type = BLOBMSG_TYPE_BOOL },
70 [IFACE_ATTR_IP6IFACEID] = { .name = "ip6ifaceid", .type = BLOBMSG_TYPE_STRING },
71 [IFACE_ATTR_FORCE_LINK] = { .name = "force_link", .type = BLOBMSG_TYPE_BOOL },
72 [IFACE_ATTR_IP6WEIGHT] = { .name = "ip6weight", .type = BLOBMSG_TYPE_INT32 },
73 };
74
75 const struct uci_blob_param_list interface_attr_list = {
76 .n_params = IFACE_ATTR_MAX,
77 .params = iface_attrs,
78 };
79
80 static void
81 interface_set_main_dev(struct interface *iface, struct device *dev);
82 static void
83 interface_event(struct interface *iface, enum interface_event ev);
84
85 static void
86 interface_error_flush(struct interface *iface)
87 {
88 struct interface_error *error, *tmp;
89
90 list_for_each_entry_safe(error, tmp, &iface->errors, list) {
91 list_del(&error->list);
92 free(error);
93 }
94 }
95
96 static void
97 interface_clear_errors(struct interface *iface)
98 {
99 /* don't flush the errors in case the configured protocol handler matches the
100 running protocol handler and is having the last error capability */
101 if (!(iface->proto &&
102 (iface->proto->handler->flags & PROTO_FLAG_LASTERROR) &&
103 (iface->proto->handler->name == iface->proto_handler->name)))
104 interface_error_flush(iface);
105 }
106
107 void interface_add_error(struct interface *iface, const char *subsystem,
108 const char *code, const char **data, int n_data)
109 {
110 struct interface_error *error;
111 int i, len = 0;
112 int *datalen = NULL;
113 char *dest, *d_subsys, *d_code;
114
115 /* if the configured protocol handler has the last error support capability,
116 errors should only be added if the running protocol handler matches the
117 configured one */
118 if (iface->proto &&
119 (iface->proto->handler->flags & PROTO_FLAG_LASTERROR) &&
120 (iface->proto->handler->name != iface->proto_handler->name))
121 return;
122
123 if (n_data) {
124 len = n_data * sizeof(char *);
125 datalen = alloca(len);
126 for (i = 0; i < n_data; i++) {
127 datalen[i] = strlen(data[i]) + 1;
128 len += datalen[i];
129 }
130 }
131
132 error = calloc_a(sizeof(*error) + sizeof(char *) + len,
133 &d_subsys, subsystem ? strlen(subsystem) + 1 : 0,
134 &d_code, code ? strlen(code) + 1 : 0);
135 if (!error)
136 return;
137
138 /* Only keep the last flagged error, prevent this list grows unlimitted in case the
139 protocol can't be established (e.g auth failure) */
140 if (iface->proto_handler->flags & PROTO_FLAG_LASTERROR)
141 interface_error_flush(iface);
142
143 list_add_tail(&error->list, &iface->errors);
144
145 dest = (char *) &error->data[n_data + 1];
146 for (i = 0; i < n_data; i++) {
147 error->data[i] = dest;
148 memcpy(dest, data[i], datalen[i]);
149 dest += datalen[i];
150 }
151 error->data[n_data] = NULL;
152
153 if (subsystem)
154 error->subsystem = strcpy(d_subsys, subsystem);
155
156 if (code)
157 error->code = strcpy(d_code, code);
158 }
159
160 static void
161 interface_data_del(struct interface *iface, struct interface_data *data)
162 {
163 avl_delete(&iface->data, &data->node);
164 free(data);
165 }
166
167 static void
168 interface_data_flush(struct interface *iface)
169 {
170 struct interface_data *d, *tmp;
171
172 avl_for_each_element_safe(&iface->data, d, node, tmp)
173 interface_data_del(iface, d);
174 }
175
176 int
177 interface_add_data(struct interface *iface, const struct blob_attr *data)
178 {
179 struct interface_data *n, *o;
180
181 if (!blobmsg_check_attr(data, true))
182 return UBUS_STATUS_INVALID_ARGUMENT;
183
184 const char *name = blobmsg_name(data);
185 unsigned len = blob_pad_len(data);
186
187 o = avl_find_element(&iface->data, name, o, node);
188 if (o) {
189 if (blob_pad_len(o->data) == len && !memcmp(o->data, data, len))
190 return 0;
191
192 interface_data_del(iface, o);
193 }
194
195 n = calloc(1, sizeof(*n) + len);
196 if (!n)
197 return UBUS_STATUS_UNKNOWN_ERROR;
198
199 memcpy(n->data, data, len);
200 n->node.key = blobmsg_name(n->data);
201 avl_insert(&iface->data, &n->node);
202
203 iface->updated |= IUF_DATA;
204 return 0;
205 }
206
207 int interface_parse_data(struct interface *iface, const struct blob_attr *attr)
208 {
209 struct blob_attr *cur;
210 int rem, ret;
211
212 iface->updated = 0;
213
214 blob_for_each_attr(cur, attr, rem) {
215 ret = interface_add_data(iface, cur);
216 if (ret)
217 return ret;
218 }
219
220 if (iface->updated && iface->state == IFS_UP)
221 interface_event(iface, IFEV_UPDATE);
222
223 return 0;
224 }
225
226 static void
227 interface_event(struct interface *iface, enum interface_event ev)
228 {
229 struct interface_user *dep, *tmp;
230 struct device *adev = NULL;
231
232 list_for_each_entry_safe(dep, tmp, &iface->users, list)
233 dep->cb(dep, iface, ev);
234
235 list_for_each_entry_safe(dep, tmp, &iface_all_users, list)
236 dep->cb(dep, iface, ev);
237
238 switch (ev) {
239 case IFEV_UP:
240 interface_error_flush(iface);
241 adev = iface->l3_dev.dev;
242 /* fall through */
243 case IFEV_DOWN:
244 case IFEV_UP_FAILED:
245 alias_notify_device(iface->name, adev);
246 break;
247 default:
248 break;
249 }
250 }
251
252 static void
253 interface_flush_state(struct interface *iface)
254 {
255 if (iface->l3_dev.dev)
256 device_release(&iface->l3_dev);
257 interface_data_flush(iface);
258 }
259
260 static void
261 mark_interface_down(struct interface *iface)
262 {
263 enum interface_state state = iface->state;
264
265 if (state == IFS_DOWN)
266 return;
267
268 iface->link_up_event = false;
269 iface->state = IFS_DOWN;
270 switch (state) {
271 case IFS_UP:
272 interface_event(iface, IFEV_DOWN);
273 break;
274 case IFS_SETUP:
275 interface_event(iface, IFEV_UP_FAILED);
276 break;
277 default:
278 break;
279 }
280 interface_ip_set_enabled(&iface->config_ip, false);
281 interface_ip_set_enabled(&iface->proto_ip, false);
282 interface_ip_flush(&iface->proto_ip);
283 interface_flush_state(iface);
284 system_flush_routes();
285 }
286
287 static inline void
288 __set_config_state(struct interface *iface, enum interface_config_state s)
289 {
290 iface->config_state = s;
291 }
292
293 static void
294 __interface_set_down(struct interface *iface, bool force)
295 {
296 enum interface_state state = iface->state;
297 switch (state) {
298 case IFS_UP:
299 case IFS_SETUP:
300 iface->state = IFS_TEARDOWN;
301 if (iface->dynamic)
302 __set_config_state(iface, IFC_REMOVE);
303
304 if (state == IFS_UP)
305 interface_event(iface, IFEV_DOWN);
306
307 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
308 if (force)
309 interface_flush_state(iface);
310 break;
311
312 case IFS_DOWN:
313 if (iface->main_dev.dev)
314 device_release(&iface->main_dev);
315 case IFS_TEARDOWN:
316 default:
317 break;
318 }
319 }
320
321 static int
322 __interface_set_up(struct interface *iface)
323 {
324 int ret;
325
326 netifd_log_message(L_NOTICE, "Interface '%s' is setting up now\n", iface->name);
327
328 iface->state = IFS_SETUP;
329 ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
330 if (ret)
331 mark_interface_down(iface);
332
333 return ret;
334 }
335
336 static void
337 interface_check_state(struct interface *iface)
338 {
339 bool link_state = iface->link_state || iface->force_link;
340
341 switch (iface->state) {
342 case IFS_UP:
343 case IFS_SETUP:
344 if (!iface->enabled || !link_state) {
345 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, false);
346 if (iface->dynamic)
347 __set_config_state(iface, IFC_REMOVE);
348
349 mark_interface_down(iface);
350 }
351 break;
352 case IFS_DOWN:
353 if (!iface->available)
354 return;
355
356 if (iface->autostart && iface->enabled && link_state && !config_init)
357 __interface_set_up(iface);
358 break;
359 default:
360 break;
361 }
362 }
363
364 static void
365 interface_set_enabled(struct interface *iface, bool new_state)
366 {
367 if (iface->enabled == new_state)
368 return;
369
370 netifd_log_message(L_NOTICE, "Interface '%s' is %s\n", iface->name, new_state ? "enabled" : "disabled");
371 iface->enabled = new_state;
372 interface_check_state(iface);
373 }
374
375 static void
376 interface_set_link_state(struct interface *iface, bool new_state)
377 {
378 if (iface->link_state == new_state)
379 return;
380
381 netifd_log_message(L_NOTICE, "Interface '%s' has link connectivity %s\n", iface->name, new_state ? "" : "loss");
382 iface->link_state = new_state;
383 interface_check_state(iface);
384
385 if (new_state && iface->force_link && iface->state == IFS_UP && !iface->link_up_event) {
386 interface_event(iface, IFEV_LINK_UP);
387 iface->link_up_event = true;
388 }
389 }
390
391 static void
392 interface_ext_dev_cb(struct device_user *dep, enum device_event ev)
393 {
394 if (ev == DEV_EVENT_REMOVE)
395 device_remove_user(dep);
396 }
397
398 static void
399 interface_main_dev_cb(struct device_user *dep, enum device_event ev)
400 {
401 struct interface *iface;
402
403 iface = container_of(dep, struct interface, main_dev);
404 switch (ev) {
405 case DEV_EVENT_ADD:
406 interface_set_available(iface, true);
407 break;
408 case DEV_EVENT_REMOVE:
409 interface_set_available(iface, false);
410 if (dep->dev && dep->dev->external)
411 interface_set_main_dev(iface, NULL);
412 break;
413 case DEV_EVENT_UP:
414 interface_set_enabled(iface, true);
415 break;
416 case DEV_EVENT_DOWN:
417 interface_set_enabled(iface, false);
418 break;
419 case DEV_EVENT_LINK_UP:
420 interface_set_link_state(iface, true);
421 break;
422 case DEV_EVENT_LINK_DOWN:
423 interface_set_link_state(iface, false);
424 break;
425 case DEV_EVENT_TOPO_CHANGE:
426 interface_proto_event(iface->proto, PROTO_CMD_RENEW, false);
427 return;
428 default:
429 break;
430 }
431 }
432
433 static void
434 interface_l3_dev_cb(struct device_user *dep, enum device_event ev)
435 {
436 struct interface *iface;
437
438 iface = container_of(dep, struct interface, l3_dev);
439 if (iface->l3_dev.dev == iface->main_dev.dev)
440 return;
441
442 switch (ev) {
443 case DEV_EVENT_LINK_DOWN:
444 if (iface->proto_handler->flags & PROTO_FLAG_TEARDOWN_ON_L3_LINK_DOWN)
445 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, false);
446 break;
447 default:
448 break;
449 }
450 }
451
452 void
453 interface_set_available(struct interface *iface, bool new_state)
454 {
455 if (iface->available == new_state)
456 return;
457
458 D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
459 iface->available = new_state;
460
461 if (new_state) {
462 if (iface->autostart && !config_init)
463 interface_set_up(iface);
464 } else
465 __interface_set_down(iface, true);
466 }
467
468 void
469 interface_add_user(struct interface_user *dep, struct interface *iface)
470 {
471 if (!iface) {
472 list_add(&dep->list, &iface_all_users);
473 return;
474 }
475
476 dep->iface = iface;
477 list_add(&dep->list, &iface->users);
478 if (iface->state == IFS_UP)
479 dep->cb(dep, iface, IFEV_UP);
480 }
481
482 void
483 interface_remove_user(struct interface_user *dep)
484 {
485 list_del_init(&dep->list);
486 dep->iface = NULL;
487 }
488
489 static void
490 interface_add_assignment_classes(struct interface *iface, struct blob_attr *list)
491 {
492 struct blob_attr *cur;
493 int rem;
494
495 blobmsg_for_each_attr(cur, list, rem) {
496 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
497 continue;
498
499 if (!blobmsg_check_attr(cur, NULL))
500 continue;
501
502 struct interface_assignment_class *c = malloc(sizeof(*c) + blobmsg_data_len(cur));
503 memcpy(c->name, blobmsg_data(cur), blobmsg_data_len(cur));
504 list_add(&c->head, &iface->assignment_classes);
505 }
506 }
507
508 static void
509 interface_clear_assignment_classes(struct interface *iface)
510 {
511 while (!list_empty(&iface->assignment_classes)) {
512 struct interface_assignment_class *c = list_first_entry(&iface->assignment_classes,
513 struct interface_assignment_class, head);
514 list_del(&c->head);
515 free(c);
516 }
517 }
518
519 static void
520 interface_merge_assignment_data(struct interface *old, struct interface *new)
521 {
522 bool changed = (old->assignment_hint != new->assignment_hint ||
523 old->assignment_length != new->assignment_length ||
524 old->assignment_iface_id_selection != new->assignment_iface_id_selection ||
525 old->assignment_weight != new->assignment_weight ||
526 (old->assignment_iface_id_selection == IFID_FIXED &&
527 memcmp(&old->assignment_fixed_iface_id, &new->assignment_fixed_iface_id,
528 sizeof(old->assignment_fixed_iface_id))) ||
529 list_empty(&old->assignment_classes) != list_empty(&new->assignment_classes));
530
531 struct interface_assignment_class *c;
532 list_for_each_entry(c, &new->assignment_classes, head) {
533 /* Compare list entries one-by-one to see if there was a change */
534 if (list_empty(&old->assignment_classes)) /* The new list is longer */
535 changed = true;
536
537 if (changed)
538 break;
539
540 struct interface_assignment_class *c_old = list_first_entry(&old->assignment_classes,
541 struct interface_assignment_class, head);
542
543 if (strcmp(c_old->name, c->name)) /* An entry didn't match */
544 break;
545
546 list_del(&c_old->head);
547 free(c_old);
548 }
549
550 /* The old list was longer than the new one or the last entry didn't match */
551 if (!list_empty(&old->assignment_classes)) {
552 interface_clear_assignment_classes(old);
553 changed = true;
554 }
555
556 list_splice_init(&new->assignment_classes, &old->assignment_classes);
557
558 if (changed) {
559 old->assignment_hint = new->assignment_hint;
560 old->assignment_length = new->assignment_length;
561 old->assignment_iface_id_selection = new->assignment_iface_id_selection;
562 old->assignment_fixed_iface_id = new->assignment_fixed_iface_id;
563 old->assignment_weight = new->assignment_weight;
564 interface_refresh_assignments(true);
565 }
566 }
567
568 static void
569 interface_alias_cb(struct interface_user *dep, struct interface *iface, enum interface_event ev)
570 {
571 struct interface *alias = container_of(dep, struct interface, parent_iface);
572 struct device *dev = iface->l3_dev.dev;
573
574 switch (ev) {
575 case IFEV_UP:
576 if (!dev)
577 return;
578
579 interface_set_main_dev(alias, dev);
580 interface_set_available(alias, true);
581 break;
582 case IFEV_DOWN:
583 case IFEV_UP_FAILED:
584 interface_set_available(alias, false);
585 interface_set_main_dev(alias, NULL);
586 break;
587 case IFEV_FREE:
588 interface_remove_user(dep);
589 break;
590 default:
591 break;
592 }
593 }
594
595 static void
596 interface_set_device_config(struct interface *iface, struct device *dev)
597 {
598 if (!dev || !dev->default_config)
599 return;
600
601 if (!iface->device_config &&
602 (!dev->iface_config || dev->config_iface != iface))
603 return;
604
605 dev->config_iface = iface;
606 dev->iface_config = iface->device_config;
607 device_apply_config(dev, dev->type, iface->config);
608 }
609
610 static void
611 interface_claim_device(struct interface *iface)
612 {
613 struct interface *parent;
614 struct device *dev = NULL;
615
616 if (iface->parent_iface.iface)
617 interface_remove_user(&iface->parent_iface);
618
619 device_lock();
620
621 if (iface->parent_ifname) {
622 parent = vlist_find(&interfaces, iface->parent_ifname, parent, node);
623 iface->parent_iface.cb = interface_alias_cb;
624 interface_add_user(&iface->parent_iface, parent);
625 } else if (iface->ifname &&
626 !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
627 dev = device_get(iface->ifname, true);
628 interface_set_device_config(iface, dev);
629 } else {
630 dev = iface->ext_dev.dev;
631 }
632
633 if (dev)
634 interface_set_main_dev(iface, dev);
635
636 device_unlock();
637
638 if (iface->proto_handler->flags & PROTO_FLAG_INIT_AVAILABLE)
639 interface_set_available(iface, true);
640 }
641
642 static void
643 interface_cleanup_state(struct interface *iface)
644 {
645 interface_set_available(iface, false);
646
647 interface_flush_state(iface);
648 interface_clear_errors(iface);
649 interface_set_proto_state(iface, NULL);
650
651 interface_set_main_dev(iface, NULL);
652 interface_set_l3_dev(iface, NULL);
653 }
654
655 static void
656 interface_cleanup(struct interface *iface)
657 {
658 struct interface_user *dep, *tmp;
659
660 uloop_timeout_cancel(&iface->remove_timer);
661 device_remove_user(&iface->ext_dev);
662
663 if (iface->parent_iface.iface)
664 interface_remove_user(&iface->parent_iface);
665
666 list_for_each_entry_safe(dep, tmp, &iface->users, list)
667 interface_remove_user(dep);
668
669 interface_clear_assignment_classes(iface);
670 interface_ip_flush(&iface->config_ip);
671 interface_cleanup_state(iface);
672 }
673
674 static void
675 interface_do_free(struct interface *iface)
676 {
677 interface_event(iface, IFEV_FREE);
678 interface_cleanup(iface);
679 free(iface->config);
680 netifd_ubus_remove_interface(iface);
681 avl_delete(&interfaces.avl, &iface->node.avl);
682 free(iface);
683 }
684
685 static void
686 interface_do_reload(struct interface *iface)
687 {
688 interface_event(iface, IFEV_RELOAD);
689 interface_cleanup_state(iface);
690 proto_init_interface(iface, iface->config);
691 interface_claim_device(iface);
692 }
693
694 static void
695 interface_handle_config_change(struct interface *iface)
696 {
697 enum interface_config_state state = iface->config_state;
698
699 iface->config_state = IFC_NORMAL;
700 switch(state) {
701 case IFC_NORMAL:
702 break;
703 case IFC_RELOAD:
704 interface_do_reload(iface);
705 break;
706 case IFC_REMOVE:
707 interface_do_free(iface);
708 return;
709 }
710 if (iface->autostart)
711 interface_set_up(iface);
712 }
713
714 static void
715 interface_proto_event_cb(struct interface_proto_state *state, enum interface_proto_event ev)
716 {
717 struct interface *iface = state->iface;
718
719 switch (ev) {
720 case IFPEV_UP:
721 if (iface->state != IFS_SETUP) {
722 if (iface->state == IFS_UP && iface->updated)
723 interface_event(iface, IFEV_UPDATE);
724 return;
725 }
726
727 if (!iface->l3_dev.dev)
728 interface_set_l3_dev(iface, iface->main_dev.dev);
729
730 interface_ip_set_enabled(&iface->config_ip, true);
731 interface_ip_set_enabled(&iface->proto_ip, true);
732 system_flush_routes();
733 iface->state = IFS_UP;
734 iface->start_time = system_get_rtime();
735 interface_event(iface, IFEV_UP);
736 netifd_log_message(L_NOTICE, "Interface '%s' is now up\n", iface->name);
737 break;
738 case IFPEV_DOWN:
739 if (iface->state == IFS_DOWN)
740 return;
741
742 netifd_log_message(L_NOTICE, "Interface '%s' is now down\n", iface->name);
743 mark_interface_down(iface);
744 if (iface->main_dev.dev)
745 device_release(&iface->main_dev);
746 if (iface->l3_dev.dev)
747 device_remove_user(&iface->l3_dev);
748 interface_handle_config_change(iface);
749 break;
750 case IFPEV_LINK_LOST:
751 if (iface->state != IFS_UP)
752 return;
753
754 netifd_log_message(L_NOTICE, "Interface '%s' has lost the connection\n", iface->name);
755 mark_interface_down(iface);
756 iface->state = IFS_SETUP;
757 break;
758 default:
759 return;
760 }
761
762 interface_write_resolv_conf();
763 }
764
765 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
766 {
767 if (iface->proto) {
768 iface->proto->free(iface->proto);
769 iface->proto = NULL;
770 }
771 iface->state = IFS_DOWN;
772 iface->proto = state;
773 if (!state)
774 return;
775
776 state->proto_event = interface_proto_event_cb;
777 state->iface = iface;
778 }
779
780 struct interface *
781 interface_alloc(const char *name, struct blob_attr *config, bool dynamic)
782 {
783 struct interface *iface;
784 struct blob_attr *tb[IFACE_ATTR_MAX];
785 struct blob_attr *cur;
786 const char *proto_name = NULL;
787 char *iface_name;
788 bool force_link = false;
789
790 iface = calloc_a(sizeof(*iface), &iface_name, strlen(name) + 1);
791 iface->name = strcpy(iface_name, name);
792 INIT_LIST_HEAD(&iface->errors);
793 INIT_LIST_HEAD(&iface->users);
794 INIT_LIST_HEAD(&iface->hotplug_list);
795 INIT_LIST_HEAD(&iface->assignment_classes);
796 interface_ip_init(iface);
797 avl_init(&iface->data, avl_strcmp, false, NULL);
798 iface->config_ip.enabled = false;
799
800 iface->main_dev.cb = interface_main_dev_cb;
801 iface->l3_dev.cb = interface_l3_dev_cb;
802 iface->ext_dev.cb = interface_ext_dev_cb;
803
804 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
805 blob_data(config), blob_len(config));
806
807 if ((cur = tb[IFACE_ATTR_PROTO]))
808 proto_name = blobmsg_data(cur);
809
810 proto_attach_interface(iface, proto_name);
811 if (iface->proto_handler->flags & PROTO_FLAG_FORCE_LINK_DEFAULT)
812 force_link = true;
813
814 iface->autostart = blobmsg_get_bool_default(tb[IFACE_ATTR_AUTO], true);
815 iface->force_link = blobmsg_get_bool_default(tb[IFACE_ATTR_FORCE_LINK], force_link);
816 iface->dynamic = dynamic;
817 iface->proto_ip.no_defaultroute =
818 !blobmsg_get_bool_default(tb[IFACE_ATTR_DEFAULTROUTE], true);
819 iface->proto_ip.no_dns =
820 !blobmsg_get_bool_default(tb[IFACE_ATTR_PEERDNS], true);
821
822 if ((cur = tb[IFACE_ATTR_DNS]))
823 interface_add_dns_server_list(&iface->config_ip, cur);
824
825 if ((cur = tb[IFACE_ATTR_DNS_SEARCH]))
826 interface_add_dns_search_list(&iface->config_ip, cur);
827
828 if ((cur = tb[IFACE_ATTR_DNS_METRIC]))
829 iface->dns_metric = blobmsg_get_u32(cur);
830
831 if ((cur = tb[IFACE_ATTR_METRIC]))
832 iface->metric = blobmsg_get_u32(cur);
833
834 if ((cur = tb[IFACE_ATTR_IP6ASSIGN]))
835 iface->assignment_length = blobmsg_get_u32(cur);
836
837 /* defaults */
838 iface->assignment_iface_id_selection = IFID_FIXED;
839 iface->assignment_fixed_iface_id = in6addr_any;
840 iface->assignment_fixed_iface_id.s6_addr[15] = 1;
841
842 if ((cur = tb[IFACE_ATTR_IP6IFACEID])) {
843 const char *ifaceid = blobmsg_data(cur);
844 if (!strcmp(ifaceid, "random")) {
845 iface->assignment_iface_id_selection = IFID_RANDOM;
846 }
847 else if (!strcmp(ifaceid, "eui64")) {
848 iface->assignment_iface_id_selection = IFID_EUI64;
849 }
850 else {
851 /* we expect an IPv6 address with network id zero here -> fixed iface id
852 if we cannot parse -> revert to iface id 1 */
853 if (inet_pton(AF_INET6,ifaceid,&iface->assignment_fixed_iface_id) != 1 ||
854 iface->assignment_fixed_iface_id.s6_addr32[0] != 0 ||
855 iface->assignment_fixed_iface_id.s6_addr32[1] != 0) {
856 iface->assignment_fixed_iface_id = in6addr_any;
857 iface->assignment_fixed_iface_id.s6_addr[15] = 1;
858 netifd_log_message(L_WARNING, "Failed to parse ip6ifaceid for interface '%s', \
859 falling back to iface id 1.\n", iface->name);
860 }
861 }
862 }
863
864 iface->assignment_hint = -1;
865 if ((cur = tb[IFACE_ATTR_IP6HINT]))
866 iface->assignment_hint = strtol(blobmsg_get_string(cur), NULL, 16) &
867 ~((1 << (64 - iface->assignment_length)) - 1);
868
869 if ((cur = tb[IFACE_ATTR_IP6CLASS]))
870 interface_add_assignment_classes(iface, cur);
871
872 if ((cur = tb[IFACE_ATTR_IP6WEIGHT]))
873 iface->assignment_weight = blobmsg_get_u32(cur);
874
875 if ((cur = tb[IFACE_ATTR_IP4TABLE])) {
876 if (!system_resolve_rt_table(blobmsg_data(cur), &iface->ip4table))
877 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
878 }
879
880 if ((cur = tb[IFACE_ATTR_IP6TABLE])) {
881 if (!system_resolve_rt_table(blobmsg_data(cur), &iface->ip6table))
882 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
883 }
884
885 iface->proto_ip.no_delegation = !blobmsg_get_bool_default(tb[IFACE_ATTR_DELEGATE], true);
886
887 iface->config_autostart = iface->autostart;
888 return iface;
889 }
890
891 static bool __interface_add(struct interface *iface, struct blob_attr *config, bool alias)
892 {
893 struct blob_attr *tb[IFACE_ATTR_MAX];
894 struct blob_attr *cur;
895 char *name = NULL;
896
897 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
898 blob_data(config), blob_len(config));
899
900 if (alias) {
901 if ((cur = tb[IFACE_ATTR_INTERFACE]))
902 iface->parent_ifname = blobmsg_data(cur);
903
904 if (!iface->parent_ifname)
905 return false;
906 } else {
907 if ((cur = tb[IFACE_ATTR_IFNAME]))
908 iface->ifname = blobmsg_data(cur);
909 }
910
911 if (iface->dynamic) {
912 name = strdup(iface->name);
913
914 if (!name)
915 return false;
916 }
917
918 iface->config = config;
919 vlist_add(&interfaces, &iface->node, iface->name);
920
921 if (name) {
922 iface = vlist_find(&interfaces, name, iface, node);
923 free(name);
924
925 if (!iface)
926 return false;
927
928 /* Don't delete dynamic interface on reload */
929 iface->node.version = -1;
930 }
931
932 return true;
933 }
934
935 bool
936 interface_add(struct interface *iface, struct blob_attr *config)
937 {
938 return __interface_add(iface, config, false);
939 }
940
941 bool
942 interface_add_alias(struct interface *iface, struct blob_attr *config)
943 {
944 if (iface->proto_handler->flags & PROTO_FLAG_NODEV)
945 return false;
946
947 return __interface_add(iface, config, true);
948 }
949
950 void
951 interface_set_l3_dev(struct interface *iface, struct device *dev)
952 {
953 bool enabled = iface->config_ip.enabled;
954 bool claimed = iface->l3_dev.claimed;
955
956 if (iface->l3_dev.dev == dev)
957 return;
958
959 interface_ip_set_enabled(&iface->config_ip, false);
960 interface_ip_set_enabled(&iface->proto_ip, false);
961 interface_ip_flush(&iface->proto_ip);
962 device_add_user(&iface->l3_dev, dev);
963
964 if (dev) {
965 if (claimed) {
966 if (device_claim(&iface->l3_dev) < 0)
967 return;
968 }
969 interface_ip_set_enabled(&iface->config_ip, enabled);
970 interface_ip_set_enabled(&iface->proto_ip, enabled);
971 }
972 }
973
974 static void
975 interface_set_main_dev(struct interface *iface, struct device *dev)
976 {
977 bool claimed = iface->l3_dev.claimed;
978
979 if (iface->main_dev.dev == dev)
980 return;
981
982 interface_set_available(iface, false);
983 device_add_user(&iface->main_dev, dev);
984 if (!dev) {
985 interface_set_link_state(iface, false);
986 return;
987 }
988
989 if (claimed) {
990 if (device_claim(&iface->l3_dev) < 0)
991 return;
992 }
993
994 if (!iface->l3_dev.dev)
995 interface_set_l3_dev(iface, dev);
996 }
997
998 static int
999 interface_remove_link(struct interface *iface, struct device *dev)
1000 {
1001 struct device *mdev = iface->main_dev.dev;
1002
1003 if (mdev && mdev->hotplug_ops)
1004 return mdev->hotplug_ops->del(mdev, dev);
1005
1006 if (dev == iface->ext_dev.dev)
1007 device_remove_user(&iface->ext_dev);
1008
1009 if (!iface->main_dev.hotplug)
1010 return UBUS_STATUS_INVALID_ARGUMENT;
1011
1012 if (dev != iface->main_dev.dev)
1013 return UBUS_STATUS_INVALID_ARGUMENT;
1014
1015 interface_set_main_dev(iface, NULL);
1016 return 0;
1017 }
1018
1019 static int
1020 interface_add_link(struct interface *iface, struct device *dev, bool link_ext)
1021 {
1022 struct device *mdev = iface->main_dev.dev;
1023
1024 if (mdev == dev)
1025 return 0;
1026
1027 if (iface->main_dev.hotplug)
1028 device_remove_user(&iface->main_dev);
1029
1030 if (mdev) {
1031 if (mdev->hotplug_ops)
1032 return mdev->hotplug_ops->add(mdev, dev);
1033 else
1034 return UBUS_STATUS_NOT_SUPPORTED;
1035 }
1036
1037 if (link_ext)
1038 device_add_user(&iface->ext_dev, dev);
1039
1040 interface_set_main_dev(iface, dev);
1041 iface->main_dev.hotplug = true;
1042 return 0;
1043 }
1044
1045 int
1046 interface_handle_link(struct interface *iface, const char *name, bool add, bool link_ext)
1047 {
1048 struct device *dev;
1049 int ret;
1050
1051 device_lock();
1052
1053 dev = device_get(name, add ? (link_ext ? 2 : 1) : 0);
1054 if (!dev) {
1055 ret = UBUS_STATUS_NOT_FOUND;
1056 goto out;
1057 }
1058
1059 if (add) {
1060 interface_set_device_config(iface, dev);
1061 device_set_present(dev, true);
1062
1063 ret = interface_add_link(iface, dev, link_ext);
1064 } else {
1065 ret = interface_remove_link(iface, dev);
1066 }
1067
1068 out:
1069 device_unlock();
1070
1071 return ret;
1072 }
1073
1074 void
1075 interface_set_up(struct interface *iface)
1076 {
1077 int ret;
1078 const char *error = NULL;
1079
1080 iface->autostart = true;
1081
1082 if (iface->state != IFS_DOWN)
1083 return;
1084
1085 interface_clear_errors(iface);
1086 if (iface->available) {
1087 if (iface->main_dev.dev) {
1088 ret = device_claim(&iface->main_dev);
1089 if (!ret)
1090 interface_check_state(iface);
1091 else
1092 error = "DEVICE_CLAIM_FAILED";
1093 } else {
1094 ret = __interface_set_up(iface);
1095 if (ret)
1096 error = "SETUP_FAILED";
1097 }
1098 } else
1099 error = "NO_DEVICE";
1100
1101 if (error)
1102 interface_add_error(iface, "interface", error, NULL, 0);
1103 }
1104
1105 void
1106 interface_set_down(struct interface *iface)
1107 {
1108 if (!iface) {
1109 vlist_for_each_element(&interfaces, iface, node)
1110 __interface_set_down(iface, false);
1111 } else {
1112 iface->autostart = false;
1113 __interface_set_down(iface, false);
1114 }
1115 }
1116
1117 int
1118 interface_renew(struct interface *iface)
1119 {
1120 if (iface->state == IFS_TEARDOWN || iface->state == IFS_DOWN)
1121 return -1;
1122
1123 return interface_proto_event(iface->proto, PROTO_CMD_RENEW, false);
1124 }
1125
1126 void
1127 interface_start_pending(void)
1128 {
1129 struct interface *iface;
1130
1131 vlist_for_each_element(&interfaces, iface, node) {
1132 if (iface->autostart)
1133 interface_set_up(iface);
1134 }
1135 }
1136
1137 static void
1138 set_config_state(struct interface *iface, enum interface_config_state s)
1139 {
1140 __set_config_state(iface, s);
1141 if (iface->state == IFS_DOWN)
1142 interface_handle_config_change(iface);
1143 else
1144 __interface_set_down(iface, false);
1145 }
1146
1147 void
1148 interface_update_start(struct interface *iface, const bool keep_old)
1149 {
1150 iface->updated = 0;
1151
1152 if (!keep_old)
1153 interface_ip_update_start(&iface->proto_ip);
1154 }
1155
1156 void
1157 interface_update_complete(struct interface *iface)
1158 {
1159 interface_ip_update_complete(&iface->proto_ip);
1160 }
1161
1162 static void
1163 interface_replace_dns(struct interface_ip_settings *new, struct interface_ip_settings *old)
1164 {
1165 vlist_simple_replace(&new->dns_servers, &old->dns_servers);
1166 vlist_simple_replace(&new->dns_search, &old->dns_search);
1167 }
1168
1169 static bool
1170 interface_device_config_changed(struct interface *if_old, struct interface *if_new)
1171 {
1172 struct blob_attr *ntb[__DEV_ATTR_MAX];
1173 struct blob_attr *otb[__DEV_ATTR_MAX];
1174 struct device *dev = if_old->main_dev.dev;
1175 unsigned long diff = 0;
1176
1177 BUILD_BUG_ON(sizeof(diff) < __DEV_ATTR_MAX / 8);
1178
1179 if (!dev)
1180 return false;
1181
1182 if (if_old->device_config != if_new->device_config)
1183 return true;
1184
1185 if (!if_new->device_config)
1186 return false;
1187
1188 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb,
1189 blob_data(if_old->config), blob_len(if_old->config));
1190
1191 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, ntb,
1192 blob_data(if_new->config), blob_len(if_new->config));
1193
1194 uci_blob_diff(ntb, otb, &device_attr_list, &diff);
1195 return diff;
1196 }
1197
1198 static void
1199 interface_change_config(struct interface *if_old, struct interface *if_new)
1200 {
1201 struct blob_attr *old_config = if_old->config;
1202 bool reload = false, reload_ip = false;
1203
1204 #define FIELD_CHANGED_STR(field) \
1205 ((!!if_old->field != !!if_new->field) || \
1206 (if_old->field && \
1207 strcmp(if_old->field, if_new->field) != 0))
1208
1209 if (FIELD_CHANGED_STR(parent_ifname)) {
1210 if (if_old->parent_iface.iface)
1211 interface_remove_user(&if_old->parent_iface);
1212 reload = true;
1213 }
1214
1215 if (!reload && interface_device_config_changed(if_old, if_new))
1216 reload = true;
1217
1218 if (FIELD_CHANGED_STR(ifname) ||
1219 if_old->proto_handler != if_new->proto_handler)
1220 reload = true;
1221
1222 if (!if_old->proto_handler->config_params)
1223 D(INTERFACE, "No config parameters for interface '%s'\n",
1224 if_old->name);
1225 else if (!uci_blob_check_equal(if_old->config, if_new->config,
1226 if_old->proto_handler->config_params))
1227 reload = true;
1228
1229 #define UPDATE(field, __var) ({ \
1230 bool __changed = (if_old->field != if_new->field); \
1231 if_old->field = if_new->field; \
1232 __var |= __changed; \
1233 })
1234
1235 if_old->config = if_new->config;
1236 if (if_old->config_autostart != if_new->config_autostart) {
1237 if (if_old->config_autostart)
1238 reload = true;
1239
1240 if_old->autostart = if_new->config_autostart;
1241 }
1242
1243 if_old->device_config = if_new->device_config;
1244 if_old->config_autostart = if_new->config_autostart;
1245 if_old->ifname = if_new->ifname;
1246 if_old->parent_ifname = if_new->parent_ifname;
1247 if_old->dynamic = if_new->dynamic;
1248 if_old->proto_handler = if_new->proto_handler;
1249 if_old->force_link = if_new->force_link;
1250 if_old->dns_metric = if_new->dns_metric;
1251
1252 if_old->proto_ip.no_dns = if_new->proto_ip.no_dns;
1253 interface_replace_dns(&if_old->config_ip, &if_new->config_ip);
1254
1255 UPDATE(metric, reload_ip);
1256 UPDATE(proto_ip.no_defaultroute, reload_ip);
1257 UPDATE(ip4table, reload_ip);
1258 UPDATE(ip6table, reload_ip);
1259 interface_merge_assignment_data(if_old, if_new);
1260
1261 #undef UPDATE
1262
1263 if (reload) {
1264 D(INTERFACE, "Reload interface '%s' because of config changes\n",
1265 if_old->name);
1266 interface_clear_errors(if_old);
1267 set_config_state(if_old, IFC_RELOAD);
1268 goto out;
1269 }
1270
1271 if (reload_ip) {
1272 bool config_ip_enabled = if_old->config_ip.enabled;
1273 bool proto_ip_enabled = if_old->proto_ip.enabled;
1274
1275 interface_ip_set_enabled(&if_old->config_ip, false);
1276 interface_ip_set_enabled(&if_old->proto_ip, false);
1277 interface_ip_set_enabled(&if_old->proto_ip, proto_ip_enabled);
1278 interface_ip_set_enabled(&if_old->config_ip, config_ip_enabled);
1279 }
1280
1281 interface_write_resolv_conf();
1282 if (if_old->main_dev.dev)
1283 interface_check_state(if_old);
1284
1285 out:
1286 if_new->config = NULL;
1287 interface_cleanup(if_new);
1288 free(old_config);
1289 free(if_new);
1290 }
1291
1292 static void
1293 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
1294 struct vlist_node *node_old)
1295 {
1296 struct interface *if_old = container_of(node_old, struct interface, node);
1297 struct interface *if_new = container_of(node_new, struct interface, node);
1298
1299 if (node_old && node_new) {
1300 D(INTERFACE, "Update interface '%s'\n", if_new->name);
1301 interface_change_config(if_old, if_new);
1302 } else if (node_old) {
1303 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
1304 set_config_state(if_old, IFC_REMOVE);
1305 } else if (node_new) {
1306 D(INTERFACE, "Create interface '%s'\n", if_new->name);
1307 interface_event(if_new, IFEV_CREATE);
1308 proto_init_interface(if_new, if_new->config);
1309 interface_claim_device(if_new);
1310 netifd_ubus_add_interface(if_new);
1311 }
1312 }
1313
1314
1315 static void __init
1316 interface_init_list(void)
1317 {
1318 vlist_init(&interfaces, avl_strcmp, interface_update);
1319 interfaces.keep_old = true;
1320 interfaces.no_delete = true;
1321 }