• source navigation  • diff markup  • identifier search  • freetext search  • 

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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt