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

Sources/netifd/interface.c

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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt