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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt