• 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 "config.h"
 27 #include "system.h"
 28 #include "wireless.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 static void
706 interface_do_free(struct interface *iface)
707 {
708         interface_event(iface, IFEV_FREE);
709         interface_cleanup(iface);
710         free(iface->config);
711         netifd_ubus_remove_interface(iface);
712         avl_delete(&interfaces.avl, &iface->node.avl);
713         free(iface->zone);
714         free(iface->jail);
715         free(iface->jail_device);
716         free(iface->host_device);
717         free(iface);
718 }
719 
720 static void
721 interface_do_reload(struct interface *iface)
722 {
723         interface_event(iface, IFEV_RELOAD);
724         interface_cleanup_state(iface);
725         proto_init_interface(iface, iface->config);
726         interface_claim_device(iface);
727 }
728 
729 static void
730 interface_handle_config_change(struct interface *iface)
731 {
732         enum interface_config_state state = iface->config_state;
733 
734         iface->config_state = IFC_NORMAL;
735         switch(state) {
736         case IFC_NORMAL:
737                 break;
738         case IFC_RELOAD:
739                 interface_do_reload(iface);
740                 break;
741         case IFC_REMOVE:
742                 interface_do_free(iface);
743                 return;
744         }
745         if (iface->autostart)
746                 interface_set_up(iface);
747 }
748 
749 static void
750 interface_proto_event_cb(struct interface_proto_state *state, enum interface_proto_event ev)
751 {
752         struct interface *iface = state->iface;
753 
754         switch (ev) {
755         case IFPEV_UP:
756                 if (iface->state != IFS_SETUP) {
757                         if (iface->state == IFS_UP && iface->updated)
758                                 interface_event(iface, IFEV_UPDATE);
759                         return;
760                 }
761 
762                 if (!iface->l3_dev.dev)
763                         interface_set_l3_dev(iface, iface->main_dev.dev);
764 
765                 interface_ip_set_enabled(&iface->config_ip, true);
766                 interface_ip_set_enabled(&iface->proto_ip, true);
767                 system_flush_routes();
768                 iface->state = IFS_UP;
769                 iface->start_time = system_get_rtime();
770                 interface_event(iface, IFEV_UP);
771                 netifd_log_message(L_NOTICE, "Interface '%s' is now up\n", iface->name);
772                 break;
773         case IFPEV_DOWN:
774                 if (iface->state == IFS_DOWN)
775                         return;
776 
777                 netifd_log_message(L_NOTICE, "Interface '%s' is now down\n", iface->name);
778                 mark_interface_down(iface);
779                 interface_write_resolv_conf(iface->jail);
780                 if (iface->main_dev.dev && !(iface->config_state == IFC_NORMAL && iface->autostart && iface->available))
781                         device_release(&iface->main_dev);
782                 if (iface->l3_dev.dev)
783                         device_remove_user(&iface->l3_dev);
784                 interface_handle_config_change(iface);
785                 return;
786         case IFPEV_LINK_LOST:
787                 if (iface->state != IFS_UP)
788                         return;
789 
790                 netifd_log_message(L_NOTICE, "Interface '%s' has lost the connection\n", iface->name);
791                 mark_interface_down(iface);
792                 iface->state = IFS_SETUP;
793                 break;
794         default:
795                 return;
796         }
797 
798         interface_write_resolv_conf(iface->jail);
799 }
800 
801 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
802 {
803         if (iface->proto) {
804                 iface->proto->free(iface->proto);
805                 iface->proto = NULL;
806         }
807         iface->state = IFS_DOWN;
808         iface->proto = state;
809         if (!state)
810                 return;
811 
812         state->proto_event = interface_proto_event_cb;
813         state->iface = iface;
814 }
815 
816 struct interface *
817 interface_alloc(const char *name, struct blob_attr *config, bool dynamic)
818 {
819         struct interface *iface;
820         struct blob_attr *tb[IFACE_ATTR_MAX];
821         struct blob_attr *cur;
822         const char *proto_name = NULL;
823         char *iface_name;
824         bool force_link = false;
825 
826         iface = calloc_a(sizeof(*iface), &iface_name, strlen(name) + 1);
827         iface->name = strcpy(iface_name, name);
828         INIT_LIST_HEAD(&iface->errors);
829         INIT_LIST_HEAD(&iface->users);
830         INIT_LIST_HEAD(&iface->hotplug_list);
831         INIT_LIST_HEAD(&iface->assignment_classes);
832         interface_ip_init(iface);
833         avl_init(&iface->data, avl_strcmp, false, NULL);
834         iface->config_ip.enabled = false;
835 
836         iface->main_dev.cb = interface_main_dev_cb;
837         iface->l3_dev.cb = interface_l3_dev_cb;
838         iface->ext_dev.cb = interface_ext_dev_cb;
839 
840         blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
841                       blob_data(config), blob_len(config));
842 
843         iface->zone = NULL;
844         if ((cur = tb[IFACE_ATTR_ZONE]))
845                 iface->zone = strdup(blobmsg_get_string(cur));
846 
847         if ((cur = tb[IFACE_ATTR_PROTO]))
848                 proto_name = blobmsg_data(cur);
849 
850         proto_attach_interface(iface, proto_name);
851         if (iface->proto_handler->flags & PROTO_FLAG_FORCE_LINK_DEFAULT)
852                 force_link = true;
853 
854         iface->autostart = blobmsg_get_bool_default(tb[IFACE_ATTR_AUTO], true);
855         iface->renew = blobmsg_get_bool_default(tb[IFACE_ATTR_RENEW], true);
856         iface->force_link = blobmsg_get_bool_default(tb[IFACE_ATTR_FORCE_LINK], force_link);
857         iface->dynamic = dynamic;
858         iface->proto_ip.no_defaultroute =
859                 !blobmsg_get_bool_default(tb[IFACE_ATTR_DEFAULTROUTE], true);
860         iface->proto_ip.no_dns =
861                 !blobmsg_get_bool_default(tb[IFACE_ATTR_PEERDNS], true);
862 
863         if ((cur = tb[IFACE_ATTR_DNS]))
864                 interface_add_dns_server_list(&iface->config_ip, cur);
865 
866         if ((cur = tb[IFACE_ATTR_DNS_SEARCH]))
867                 interface_add_dns_search_list(&iface->config_ip, cur);
868 
869         if ((cur = tb[IFACE_ATTR_DNS_METRIC]))
870                 iface->dns_metric = blobmsg_get_u32(cur);
871 
872         if ((cur = tb[IFACE_ATTR_METRIC]))
873                 iface->metric = blobmsg_get_u32(cur);
874 
875         if ((cur = tb[IFACE_ATTR_IP6ASSIGN]))
876                 iface->assignment_length = blobmsg_get_u32(cur);
877 
878         /* defaults */
879         iface->assignment_iface_id_selection = IFID_FIXED;
880         iface->assignment_fixed_iface_id = in6addr_any;
881         iface->assignment_fixed_iface_id.s6_addr[15] = 1;
882 
883         if ((cur = tb[IFACE_ATTR_IP6IFACEID])) {
884                 const char *ifaceid = blobmsg_data(cur);
885                 if (!strcmp(ifaceid, "random")) {
886                         iface->assignment_iface_id_selection = IFID_RANDOM;
887                 }
888                 else if (!strcmp(ifaceid, "eui64")) {
889                         iface->assignment_iface_id_selection = IFID_EUI64;
890                 }
891                 else {
892                         /* we expect an IPv6 address with network id zero here -> fixed iface id
893                            if we cannot parse -> revert to iface id 1 */
894                         if (inet_pton(AF_INET6,ifaceid,&iface->assignment_fixed_iface_id) != 1 ||
895                                         iface->assignment_fixed_iface_id.s6_addr32[0] != 0 ||
896                                         iface->assignment_fixed_iface_id.s6_addr32[1] != 0) {
897                                 iface->assignment_fixed_iface_id = in6addr_any;
898                                 iface->assignment_fixed_iface_id.s6_addr[15] = 1;
899                                 netifd_log_message(L_WARNING, "Failed to parse ip6ifaceid for interface '%s', \
900                                                         falling back to iface id 1.\n", iface->name);
901                         }
902                 }
903         }
904 
905         iface->assignment_hint = -1;
906         if ((cur = tb[IFACE_ATTR_IP6HINT]))
907                 iface->assignment_hint = strtol(blobmsg_get_string(cur), NULL, 16) &
908                                 ~((1 << (64 - iface->assignment_length)) - 1);
909 
910         if ((cur = tb[IFACE_ATTR_IP6CLASS]))
911                 interface_add_assignment_classes(iface, cur);
912 
913         if ((cur = tb[IFACE_ATTR_IP6WEIGHT]))
914                 iface->assignment_weight = blobmsg_get_u32(cur);
915 
916         if ((cur = tb[IFACE_ATTR_IP4TABLE])) {
917                 if (!system_resolve_rt_table(blobmsg_data(cur), &iface->ip4table))
918                         D(INTERFACE, "Failed to resolve routing table: %s", (char *) blobmsg_data(cur));
919         }
920 
921         if ((cur = tb[IFACE_ATTR_IP6TABLE])) {
922                 if (!system_resolve_rt_table(blobmsg_data(cur), &iface->ip6table))
923                         D(INTERFACE, "Failed to resolve routing table: %s", (char *) blobmsg_data(cur));
924         }
925 
926         iface->proto_ip.no_delegation = !blobmsg_get_bool_default(tb[IFACE_ATTR_DELEGATE], true);
927 
928         iface->config_autostart = iface->autostart;
929         iface->jail = NULL;
930 
931         if ((cur = tb[IFACE_ATTR_JAIL])) {
932                 iface->jail = strdup(blobmsg_get_string(cur));
933                 iface->autostart = false;
934         }
935 
936         iface->jail_device = NULL;
937         if ((cur = tb[IFACE_ATTR_JAIL_DEVICE]))
938                 iface->jail_device = strdup(blobmsg_get_string(cur));
939         else if ((cur = tb[IFACE_ATTR_JAIL_IFNAME]))
940                 iface->jail_device = strdup(blobmsg_get_string(cur));
941 
942         iface->host_device = NULL;
943         if ((cur = tb[IFACE_ATTR_HOST_DEVICE]))
944                 iface->host_device = strdup(blobmsg_get_string(cur));
945 
946         return iface;
947 }
948 
949 static bool __interface_add(struct interface *iface, struct blob_attr *config, bool alias)
950 {
951         struct blob_attr *tb[IFACE_ATTR_MAX];
952         struct blob_attr *cur;
953         char *name = NULL;
954 
955         blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
956                       blob_data(config), blob_len(config));
957 
958         if (alias) {
959                 if ((cur = tb[IFACE_ATTR_INTERFACE]))
960                         iface->parent_ifname = blobmsg_data(cur);
961 
962                 if (!iface->parent_ifname)
963                         return false;
964         } else {
965                 cur = tb[IFACE_ATTR_DEVICE];
966                 if (!cur)
967                         cur = tb[IFACE_ATTR_IFNAME];
968                 if (cur)
969                         iface->device = blobmsg_data(cur);
970         }
971 
972         if (iface->dynamic) {
973                 name = strdup(iface->name);
974 
975                 if (!name)
976                         return false;
977         }
978 
979         iface->config = config;
980         iface->tags = tb[IFACE_ATTR_TAGS];
981         vlist_add(&interfaces, &iface->node, iface->name);
982 
983         if (name) {
984                 iface = vlist_find(&interfaces, name, iface, node);
985                 free(name);
986 
987                 /* Don't delete dynamic interface on reload */
988                 if (iface)
989                         iface->node.version = -1;
990         }
991 
992         return true;
993 }
994 
995 bool
996 interface_add(struct interface *iface, struct blob_attr *config)
997 {
998         return __interface_add(iface, config, false);
999 }
1000 
1001 bool
1002 interface_add_alias(struct interface *iface, struct blob_attr *config)
1003 {
1004         if (iface->proto_handler->flags & PROTO_FLAG_NODEV)
1005                 return false;
1006 
1007         return __interface_add(iface, config, true);
1008 }
1009 
1010 void
1011 interface_set_l3_dev(struct interface *iface, struct device *dev)
1012 {
1013         bool enabled = iface->config_ip.enabled;
1014         bool claimed = iface->l3_dev.claimed;
1015 
1016         if (iface->l3_dev.dev == dev)
1017                 return;
1018 
1019         interface_ip_set_enabled(&iface->config_ip, false);
1020         interface_ip_set_enabled(&iface->proto_ip, false);
1021         interface_ip_flush(&iface->proto_ip);
1022         device_add_user(&iface->l3_dev, dev);
1023 
1024         if (dev) {
1025                 if (claimed) {
1026                         if (device_claim(&iface->l3_dev) < 0)
1027                                 return;
1028                 }
1029                 interface_ip_set_enabled(&iface->config_ip, enabled);
1030                 interface_ip_set_enabled(&iface->proto_ip, enabled);
1031         }
1032 }
1033 
1034 static void
1035 interface_set_main_dev(struct interface *iface, struct device *dev)
1036 {
1037         bool claimed = iface->l3_dev.claimed;
1038 
1039         if (iface->main_dev.dev == dev)
1040                 return;
1041 
1042         interface_set_available(iface, false);
1043         device_add_user(&iface->main_dev, dev);
1044         if (!dev) {
1045                 interface_set_link_state(iface, false);
1046                 return;
1047         }
1048 
1049         if (claimed) {
1050                 if (device_claim(&iface->l3_dev) < 0)
1051                         return;
1052         }
1053 
1054         if (!iface->l3_dev.dev)
1055                 interface_set_l3_dev(iface, dev);
1056 }
1057 
1058 static int
1059 interface_remove_link(struct interface *iface, struct device *dev,
1060                       struct blob_attr *vlan)
1061 {
1062         struct device *mdev = iface->main_dev.dev;
1063 
1064         if (mdev && mdev->hotplug_ops)
1065                 return mdev->hotplug_ops->del(mdev, dev, vlan);
1066 
1067         if (dev == iface->ext_dev.dev)
1068                 device_remove_user(&iface->ext_dev);
1069 
1070         if (!iface->main_dev.hotplug)
1071                 return UBUS_STATUS_INVALID_ARGUMENT;
1072 
1073         if (dev != iface->main_dev.dev)
1074                 return UBUS_STATUS_INVALID_ARGUMENT;
1075 
1076         interface_set_main_dev(iface, NULL);
1077         return 0;
1078 }
1079 
1080 static int
1081 interface_add_link(struct interface *iface, struct device *dev,
1082                    struct blob_attr *vlan, bool link_ext)
1083 {
1084         struct device *mdev = iface->main_dev.dev;
1085 
1086         if (mdev == dev) {
1087                 if (iface->state != IFS_UP) {
1088                         interface_set_available(iface, false);
1089                         if (dev->present)
1090                                 interface_set_available(iface, true);
1091                 }
1092                 return 0;
1093         }
1094 
1095         if (iface->main_dev.hotplug)
1096                 interface_set_main_dev(iface, NULL);
1097 
1098         if (mdev) {
1099                 if (mdev->hotplug_ops)
1100                         return mdev->hotplug_ops->add(mdev, dev, vlan);
1101                 else
1102                         return UBUS_STATUS_NOT_SUPPORTED;
1103         }
1104 
1105         if (link_ext)
1106                 device_add_user(&iface->ext_dev, dev);
1107 
1108         interface_set_main_dev(iface, dev);
1109         iface->main_dev.hotplug = true;
1110         return 0;
1111 }
1112 
1113 int
1114 interface_handle_link(struct interface *iface, const char *name,
1115                       struct blob_attr *vlan, bool add, bool link_ext)
1116 {
1117         struct device *dev;
1118 
1119         dev = device_get(name, add ? (link_ext ? 2 : 1) : 0);
1120         if (!dev)
1121                 return UBUS_STATUS_NOT_FOUND;
1122 
1123         if (!add)
1124                 return interface_remove_link(iface, dev, vlan);
1125 
1126         interface_set_device_config(iface, dev);
1127         if (!link_ext)
1128                 device_set_present(dev, true);
1129 
1130         return interface_add_link(iface, dev, vlan, link_ext);
1131 }
1132 
1133 void
1134 interface_set_up(struct interface *iface)
1135 {
1136         int ret;
1137         const char *error = NULL;
1138 
1139         iface->autostart = true;
1140         wireless_check_network_enabled();
1141 
1142         if (iface->state != IFS_DOWN)
1143                 return;
1144 
1145         interface_clear_errors(iface);
1146         if (iface->available) {
1147                 if (iface->main_dev.dev) {
1148                         ret = device_claim(&iface->main_dev);
1149                         if (!ret)
1150                                 interface_check_state(iface);
1151                         else
1152                                 error = "DEVICE_CLAIM_FAILED";
1153                 } else {
1154                         ret = __interface_set_up(iface);
1155                         if (ret)
1156                                 error = "SETUP_FAILED";
1157                 }
1158         } else
1159                 error = "NO_DEVICE";
1160 
1161         if (error)
1162                 interface_add_error(iface, "interface", error, NULL, 0);
1163 }
1164 
1165 void
1166 interface_set_down(struct interface *iface)
1167 {
1168         if (!iface) {
1169                 vlist_for_each_element(&interfaces, iface, node)
1170                         __interface_set_down(iface, false);
1171         } else {
1172                 iface->autostart = false;
1173                 wireless_check_network_enabled();
1174                 __interface_set_down(iface, false);
1175         }
1176 }
1177 
1178 int
1179 interface_renew(struct interface *iface)
1180 {
1181         if (iface->state == IFS_TEARDOWN || iface->state == IFS_DOWN)
1182                 return -1;
1183 
1184         return interface_proto_event(iface->proto, PROTO_CMD_RENEW, false);
1185 }
1186 
1187 void
1188 interface_start_pending(void)
1189 {
1190         struct interface *iface;
1191 
1192         vlist_for_each_element(&interfaces, iface, node) {
1193                 if (iface->autostart)
1194                         interface_set_up(iface);
1195         }
1196 }
1197 
1198 void
1199 interface_start_jail(int netns_fd, const char *jail)
1200 {
1201         struct interface *iface;
1202 
1203         vlist_for_each_element(&interfaces, iface, node) {
1204                 if (!iface->jail || strcmp(iface->jail, jail))
1205                         continue;
1206 
1207                 system_link_netns_move(iface->main_dev.dev, netns_fd, iface->jail_device);
1208         }
1209 }
1210 
1211 void
1212 interface_stop_jail(int netns_fd)
1213 {
1214         struct interface *iface;
1215         char *orig_ifname;
1216 
1217         vlist_for_each_element(&interfaces, iface, node) {
1218                 orig_ifname = iface->host_device;
1219                 interface_set_down(iface);
1220                 system_link_netns_move(iface->main_dev.dev, netns_fd, orig_ifname);
1221         }
1222 }
1223 
1224 static void
1225 set_config_state(struct interface *iface, enum interface_config_state s)
1226 {
1227         __set_config_state(iface, s);
1228         if (iface->state == IFS_DOWN)
1229                 interface_handle_config_change(iface);
1230         else
1231                 __interface_set_down(iface, false);
1232 }
1233 
1234 void
1235 interface_update_start(struct interface *iface, const bool keep_old)
1236 {
1237         iface->updated = 0;
1238 
1239         if (!keep_old)
1240                 interface_ip_update_start(&iface->proto_ip);
1241 }
1242 
1243 void
1244 interface_update_complete(struct interface *iface)
1245 {
1246         interface_ip_update_complete(&iface->proto_ip);
1247 }
1248 
1249 static void
1250 interface_replace_dns(struct interface_ip_settings *new, struct interface_ip_settings *old)
1251 {
1252         vlist_simple_replace(&new->dns_servers, &old->dns_servers);
1253         vlist_simple_replace(&new->dns_search, &old->dns_search);
1254 }
1255 
1256 static bool
1257 interface_device_config_changed(struct interface *if_old, struct interface *if_new)
1258 {
1259         struct blob_attr *ntb[__DEV_ATTR_MAX];
1260         struct blob_attr *otb[__DEV_ATTR_MAX];
1261         struct device *dev = if_old->main_dev.dev;
1262         unsigned long diff[2] = {};
1263 
1264         BUILD_BUG_ON(sizeof(diff) < __DEV_ATTR_MAX / 8);
1265 
1266         if (!dev)
1267                 return false;
1268 
1269         if (if_old->device_config != if_new->device_config)
1270                 return true;
1271 
1272         if (!if_new->device_config)
1273                 return false;
1274 
1275         blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb,
1276                 blob_data(if_old->config), blob_len(if_old->config));
1277 
1278         blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, ntb,
1279                 blob_data(if_new->config), blob_len(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                 D(INTERFACE, "Reload interface '%s' because of config changes",
1377                   if_old->name);
1378                 interface_clear_errors(if_old);
1379                 set_config_state(if_old, IFC_RELOAD);
1380                 goto out;
1381         }
1382 
1383         if (reload_ip) {
1384                 bool config_ip_enabled = if_old->config_ip.enabled;
1385                 bool proto_ip_enabled = if_old->proto_ip.enabled;
1386 
1387                 interface_ip_set_enabled(&if_old->config_ip, false);
1388                 interface_ip_set_enabled(&if_old->proto_ip, false);
1389                 interface_ip_set_enabled(&if_old->proto_ip, proto_ip_enabled);
1390                 interface_ip_set_enabled(&if_old->config_ip, config_ip_enabled);
1391         }
1392 
1393         if (update_prefix_delegation)
1394                 interface_update_prefix_delegation(&if_old->proto_ip);
1395 
1396         interface_write_resolv_conf(if_old->jail);
1397         if (if_old->main_dev.dev)
1398                 interface_check_state(if_old);
1399 
1400 out:
1401         if_new->config = NULL;
1402         interface_cleanup(if_new);
1403         free(old_config);
1404         free(if_new);
1405 }
1406 
1407 static void
1408 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
1409                  struct vlist_node *node_old)
1410 {
1411         struct interface *if_old = container_of(node_old, struct interface, node);
1412         struct interface *if_new = container_of(node_new, struct interface, node);
1413 
1414         if (node_old && node_new) {
1415                 D(INTERFACE, "Update interface '%s'", if_new->name);
1416                 interface_change_config(if_old, if_new);
1417         } else if (node_old) {
1418                 D(INTERFACE, "Remove interface '%s'", if_old->name);
1419                 set_config_state(if_old, IFC_REMOVE);
1420         } else if (node_new) {
1421                 D(INTERFACE, "Create interface '%s'", if_new->name);
1422                 interface_event(if_new, IFEV_CREATE);
1423                 proto_init_interface(if_new, if_new->config);
1424                 interface_claim_device(if_new);
1425                 netifd_ubus_add_interface(if_new);
1426         }
1427 }
1428 
1429 
1430 static void __init
1431 interface_init_list(void)
1432 {
1433         vlist_init(&interfaces, avl_strcmp, interface_update);
1434         interfaces.keep_old = true;
1435         interfaces.no_delete = true;
1436 }
1437 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt