netifd : Reinsert ip parameters (addresses/routes) based on old interface config...
[project/netifd.git] / interface.c
1 /*
2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14 #include <string.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17
18 #include "netifd.h"
19 #include "device.h"
20 #include "interface.h"
21 #include "interface-ip.h"
22 #include "proto.h"
23 #include "ubus.h"
24 #include "config.h"
25 #include "system.h"
26
27 struct vlist_tree interfaces;
28 static LIST_HEAD(iface_all_users);
29
30 enum {
31 IFACE_ATTR_IFNAME,
32 IFACE_ATTR_PROTO,
33 IFACE_ATTR_AUTO,
34 IFACE_ATTR_DEFAULTROUTE,
35 IFACE_ATTR_PEERDNS,
36 IFACE_ATTR_DNS,
37 IFACE_ATTR_DNS_SEARCH,
38 IFACE_ATTR_METRIC,
39 IFACE_ATTR_INTERFACE,
40 IFACE_ATTR_IP6ASSIGN,
41 IFACE_ATTR_IP6HINT,
42 IFACE_ATTR_IP4TABLE,
43 IFACE_ATTR_IP6TABLE,
44 IFACE_ATTR_IP6CLASS,
45 IFACE_ATTR_DELEGATE,
46 IFACE_ATTR_FORCE_LINK,
47 IFACE_ATTR_MAX
48 };
49
50 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
51 [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
52 [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
53 [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL },
54 [IFACE_ATTR_DEFAULTROUTE] = { .name = "defaultroute", .type = BLOBMSG_TYPE_BOOL },
55 [IFACE_ATTR_PEERDNS] = { .name = "peerdns", .type = BLOBMSG_TYPE_BOOL },
56 [IFACE_ATTR_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
57 [IFACE_ATTR_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
58 [IFACE_ATTR_DNS_SEARCH] = { .name = "dns_search", .type = BLOBMSG_TYPE_ARRAY },
59 [IFACE_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
60 [IFACE_ATTR_IP6ASSIGN] = { .name = "ip6assign", .type = BLOBMSG_TYPE_INT32 },
61 [IFACE_ATTR_IP6HINT] = { .name = "ip6hint", .type = BLOBMSG_TYPE_STRING },
62 [IFACE_ATTR_IP4TABLE] = { .name = "ip4table", .type = BLOBMSG_TYPE_STRING },
63 [IFACE_ATTR_IP6TABLE] = { .name = "ip6table", .type = BLOBMSG_TYPE_STRING },
64 [IFACE_ATTR_IP6CLASS] = { .name = "ip6class", .type = BLOBMSG_TYPE_ARRAY },
65 [IFACE_ATTR_DELEGATE] = { .name = "delegate", .type = BLOBMSG_TYPE_BOOL },
66 [IFACE_ATTR_FORCE_LINK] = { .name = "force_link", .type = BLOBMSG_TYPE_BOOL },
67 };
68
69 static const struct uci_blob_param_info iface_attr_info[IFACE_ATTR_MAX] = {
70 [IFACE_ATTR_DNS] = { .type = BLOBMSG_TYPE_STRING },
71 [IFACE_ATTR_IP6CLASS] = { .type = BLOBMSG_TYPE_STRING },
72 };
73
74 const struct uci_blob_param_list interface_attr_list = {
75 .n_params = IFACE_ATTR_MAX,
76 .params = iface_attrs,
77 .info = iface_attr_info,
78 };
79
80 static void
81 interface_clear_errors(struct interface *iface)
82 {
83 struct interface_error *error, *tmp;
84
85 list_for_each_entry_safe(error, tmp, &iface->errors, list) {
86 list_del(&error->list);
87 free(error);
88 }
89 }
90
91 void interface_add_error(struct interface *iface, const char *subsystem,
92 const char *code, const char **data, int n_data)
93 {
94 struct interface_error *error;
95 int i, len = 0;
96 int *datalen = NULL;
97 char *dest, *d_subsys, *d_code;
98
99 if (n_data) {
100 len = n_data * sizeof(char *);
101 datalen = alloca(len);
102 for (i = 0; i < n_data; i++) {
103 datalen[i] = strlen(data[i]) + 1;
104 len += datalen[i];
105 }
106 }
107
108 error = calloc_a(sizeof(*error) + sizeof(char *) + len,
109 &d_subsys, subsystem ? strlen(subsystem) + 1 : 0,
110 &d_code, code ? strlen(code) + 1 : 0);
111 if (!error)
112 return;
113
114 list_add_tail(&error->list, &iface->errors);
115
116 dest = (char *) &error->data[n_data + 1];
117 for (i = 0; i < n_data; i++) {
118 error->data[i] = dest;
119 memcpy(dest, data[i], datalen[i]);
120 dest += datalen[i];
121 }
122 error->data[n_data++] = NULL;
123
124 if (subsystem)
125 error->subsystem = strcpy(d_subsys, subsystem);
126
127 if (code)
128 error->code = strcpy(d_code, code);
129 }
130
131 static void
132 interface_data_del(struct interface *iface, struct interface_data *data)
133 {
134 avl_delete(&iface->data, &data->node);
135 free(data);
136 }
137
138 static void
139 interface_data_flush(struct interface *iface)
140 {
141 struct interface_data *d, *tmp;
142
143 avl_for_each_element_safe(&iface->data, d, node, tmp)
144 interface_data_del(iface, d);
145 }
146
147 int
148 interface_add_data(struct interface *iface, const struct blob_attr *data)
149 {
150 struct interface_data *n, *o;
151
152 if (!blobmsg_check_attr(data, true))
153 return UBUS_STATUS_INVALID_ARGUMENT;
154
155 const char *name = blobmsg_name(data);
156 unsigned len = blob_pad_len(data);
157
158 o = avl_find_element(&iface->data, name, o, node);
159 if (o) {
160 if (blob_pad_len(o->data) == len && !memcmp(o->data, data, len))
161 return 0;
162
163 interface_data_del(iface, o);
164 }
165
166 n = calloc(1, sizeof(*n) + len);
167 memcpy(n->data, data, len);
168 n->node.key = blobmsg_name(n->data);
169 avl_insert(&iface->data, &n->node);
170
171 iface->updated |= IUF_DATA;
172 return 0;
173 }
174
175 static void
176 interface_event(struct interface *iface, enum interface_event ev)
177 {
178 struct interface_user *dep, *tmp;
179 struct device *adev = NULL;
180
181 list_for_each_entry_safe(dep, tmp, &iface->users, list)
182 dep->cb(dep, iface, ev);
183
184 list_for_each_entry_safe(dep, tmp, &iface_all_users, list)
185 dep->cb(dep, iface, ev);
186
187 switch (ev) {
188 case IFEV_UP:
189 adev = iface->l3_dev.dev;
190 /* fall through */
191 case IFEV_DOWN:
192 alias_notify_device(iface->name, adev);
193 break;
194 default:
195 break;
196 }
197 }
198
199 static void
200 interface_flush_state(struct interface *iface)
201 {
202 if (iface->l3_dev.dev)
203 device_release(&iface->l3_dev);
204 interface_data_flush(iface);
205 }
206
207 static void
208 mark_interface_down(struct interface *iface)
209 {
210 enum interface_state state = iface->state;
211
212 if (state == IFS_DOWN)
213 return;
214
215 iface->state = IFS_DOWN;
216 if (state == IFS_UP)
217 interface_event(iface, IFEV_DOWN);
218 interface_ip_set_enabled(&iface->config_ip, false);
219 interface_ip_flush(&iface->proto_ip);
220 interface_flush_state(iface);
221 system_flush_routes();
222 }
223
224 void
225 __interface_set_down(struct interface *iface, bool force)
226 {
227 enum interface_state state = iface->state;
228 switch (state) {
229 case IFS_UP:
230 case IFS_SETUP:
231 iface->state = IFS_TEARDOWN;
232 if (state == IFS_UP)
233 interface_event(iface, IFEV_DOWN);
234
235 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
236 if (force)
237 interface_flush_state(iface);
238
239 if (iface->dynamic)
240 vlist_delete(&interfaces, &iface->node);
241 break;
242
243 case IFS_DOWN:
244 if (iface->main_dev.dev)
245 device_release(&iface->main_dev);
246 case IFS_TEARDOWN:
247 default:
248 break;
249 }
250 }
251
252 static int
253 __interface_set_up(struct interface *iface)
254 {
255 int ret;
256
257 netifd_log_message(L_NOTICE, "Interface '%s' is setting up now\n", iface->name);
258
259 iface->state = IFS_SETUP;
260 ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
261 if (ret)
262 mark_interface_down(iface);
263
264 return ret;
265 }
266
267 static void
268 interface_check_state(struct interface *iface)
269 {
270 bool link_state = iface->link_state || iface->force_link;
271
272 switch (iface->state) {
273 case IFS_UP:
274 if (!iface->enabled || !link_state) {
275 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, false);
276 mark_interface_down(iface);
277 }
278 break;
279 case IFS_DOWN:
280 if (!iface->available)
281 return;
282
283 if (iface->autostart && iface->enabled && link_state && !config_init)
284 __interface_set_up(iface);
285 break;
286 default:
287 break;
288 }
289 }
290
291 static void
292 interface_set_enabled(struct interface *iface, bool new_state)
293 {
294 if (iface->enabled == new_state)
295 return;
296
297 netifd_log_message(L_NOTICE, "Interface '%s' is %s\n", iface->name, new_state ? "enabled" : "disabled");
298 iface->enabled = new_state;
299 interface_check_state(iface);
300 }
301
302 static void
303 interface_set_link_state(struct interface *iface, bool new_state)
304 {
305 if (iface->link_state == new_state)
306 return;
307
308 netifd_log_message(L_NOTICE, "Interface '%s' has link connectivity %s\n", iface->name, new_state ? "" : "loss");
309 iface->link_state = new_state;
310 interface_check_state(iface);
311 }
312
313 static void
314 interface_ext_cb(struct device_user *dep, enum device_event ev)
315 {
316 if (ev == DEV_EVENT_REMOVE)
317 device_remove_user(dep);
318 }
319
320 static void
321 interface_cb(struct device_user *dep, enum device_event ev)
322 {
323 struct interface *iface;
324 bool new_state = false;
325
326 iface = container_of(dep, struct interface, main_dev);
327 switch (ev) {
328 case DEV_EVENT_ADD:
329 new_state = true;
330 case DEV_EVENT_REMOVE:
331 interface_set_available(iface, new_state);
332 if (!new_state && dep->dev && dep->dev->external)
333 interface_set_main_dev(iface, NULL);
334 break;
335 case DEV_EVENT_UP:
336 new_state = true;
337 case DEV_EVENT_DOWN:
338 interface_set_enabled(iface, new_state);
339 break;
340 case DEV_EVENT_LINK_UP:
341 new_state = true;
342 case DEV_EVENT_LINK_DOWN:
343 interface_set_link_state(iface, new_state);
344 break;
345 case DEV_EVENT_TOPO_CHANGE:
346 interface_proto_event(iface->proto, PROTO_CMD_RENEW, false);
347 return;
348 default:
349 break;
350 }
351 }
352
353 void
354 interface_set_available(struct interface *iface, bool new_state)
355 {
356 if (iface->available == new_state)
357 return;
358
359 D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
360 iface->available = new_state;
361
362 if (new_state) {
363 if (iface->autostart && !config_init)
364 interface_set_up(iface);
365 } else
366 __interface_set_down(iface, true);
367 }
368
369 void
370 interface_add_user(struct interface_user *dep, struct interface *iface)
371 {
372 if (!iface) {
373 list_add(&dep->list, &iface_all_users);
374 return;
375 }
376
377 dep->iface = iface;
378 list_add(&dep->list, &iface->users);
379 if (iface->state == IFS_UP)
380 dep->cb(dep, iface, IFEV_UP);
381 }
382
383 void
384 interface_remove_user(struct interface_user *dep)
385 {
386 list_del_init(&dep->list);
387 dep->iface = NULL;
388 }
389
390 static void
391 interface_add_assignment_classes(struct interface *iface, struct blob_attr *list)
392 {
393 struct blob_attr *cur;
394 int rem;
395
396 blobmsg_for_each_attr(cur, list, rem) {
397 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
398 continue;
399
400 if (!blobmsg_check_attr(cur, NULL))
401 continue;
402
403 struct interface_assignment_class *c = malloc(sizeof(*c) + blobmsg_data_len(cur));
404 memcpy(c->name, blobmsg_data(cur), blobmsg_data_len(cur));
405 list_add(&c->head, &iface->assignment_classes);
406 }
407 }
408
409 static void
410 interface_clear_assignment_classes(struct interface *iface)
411 {
412 while (!list_empty(&iface->assignment_classes)) {
413 struct interface_assignment_class *c = list_first_entry(&iface->assignment_classes,
414 struct interface_assignment_class, head);
415 list_del(&c->head);
416 free(c);
417 }
418 }
419
420 static void
421 interface_merge_assignment_data(struct interface *old, struct interface *new)
422 {
423 bool changed = (old->assignment_hint != new->assignment_hint ||
424 old->assignment_length != new->assignment_length ||
425 list_empty(&old->assignment_classes) != list_empty(&new->assignment_classes));
426
427 struct interface_assignment_class *c;
428 list_for_each_entry(c, &new->assignment_classes, head) {
429 // Compare list entries one-by-one to see if there was a change
430 if (list_empty(&old->assignment_classes)) // The new list is longer
431 changed = true;
432
433 if (changed)
434 break;
435
436 struct interface_assignment_class *c_old = list_first_entry(&old->assignment_classes,
437 struct interface_assignment_class, head);
438
439 if (strcmp(c_old->name, c->name)) // An entry didn't match
440 break;
441
442 list_del(&c_old->head);
443 free(c_old);
444 }
445
446 // The old list was longer than the new one or the last entry didn't match
447 if (!list_empty(&old->assignment_classes)) {
448 interface_clear_assignment_classes(old);
449 changed = true;
450 }
451
452 list_splice_init(&new->assignment_classes, &old->assignment_classes);
453
454 if (changed) {
455 old->assignment_hint = new->assignment_hint;
456 old->assignment_length = new->assignment_length;
457 interface_refresh_assignments(true);
458 }
459 }
460
461 static void
462 interface_alias_cb(struct interface_user *dep, struct interface *iface, enum interface_event ev)
463 {
464 struct interface *alias = container_of(dep, struct interface, parent_iface);
465 struct device *dev = iface->l3_dev.dev;
466
467 switch (ev) {
468 case IFEV_UP:
469 if (!dev)
470 return;
471
472 interface_set_main_dev(alias, dev);
473 interface_set_available(alias, true);
474 break;
475 case IFEV_DOWN:
476 interface_set_available(alias, false);
477 interface_set_main_dev(alias, NULL);
478 break;
479 case IFEV_FREE:
480 interface_remove_user(dep);
481 break;
482 case IFEV_RELOAD:
483 case IFEV_UPDATE:
484 break;
485 }
486 }
487
488 static void
489 interface_claim_device(struct interface *iface)
490 {
491 struct interface *parent;
492 struct device *dev = NULL;
493
494 if (iface->parent_iface.iface)
495 interface_remove_user(&iface->parent_iface);
496
497 if (iface->parent_ifname) {
498 parent = vlist_find(&interfaces, iface->parent_ifname, parent, node);
499 iface->parent_iface.cb = interface_alias_cb;
500 interface_add_user(&iface->parent_iface, parent);
501 } else if (iface->ifname &&
502 !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
503 dev = device_get(iface->ifname, true);
504 if (dev && dev->default_config && iface->device_config)
505 device_set_config(dev, dev->type, iface->config);
506 } else {
507 dev = iface->ext_dev.dev;
508 }
509
510 if (dev)
511 interface_set_main_dev(iface, dev);
512
513 if (iface->proto_handler->flags & PROTO_FLAG_INIT_AVAILABLE)
514 interface_set_available(iface, true);
515 }
516
517 static void
518 interface_cleanup_state(struct interface *iface)
519 {
520 interface_set_available(iface, false);
521
522 interface_flush_state(iface);
523 interface_clear_errors(iface);
524 interface_set_proto_state(iface, NULL);
525
526 interface_set_main_dev(iface, NULL);
527 interface_set_l3_dev(iface, NULL);
528 }
529
530 static void
531 interface_cleanup(struct interface *iface)
532 {
533 struct interface_user *dep, *tmp;
534
535 uloop_timeout_cancel(&iface->remove_timer);
536 device_remove_user(&iface->ext_dev);
537
538 if (iface->parent_iface.iface)
539 interface_remove_user(&iface->parent_iface);
540
541 list_for_each_entry_safe(dep, tmp, &iface->users, list)
542 interface_remove_user(dep);
543
544 interface_clear_assignment_classes(iface);
545 interface_ip_flush(&iface->config_ip);
546 interface_cleanup_state(iface);
547 }
548
549 static void
550 interface_do_free(struct interface *iface)
551 {
552 interface_event(iface, IFEV_FREE);
553 interface_cleanup(iface);
554 free(iface->config);
555 netifd_ubus_remove_interface(iface);
556 avl_delete(&interfaces.avl, &iface->node.avl);
557 free(iface);
558 }
559
560 static void
561 interface_do_reload(struct interface *iface)
562 {
563 interface_event(iface, IFEV_RELOAD);
564 interface_cleanup_state(iface);
565 proto_init_interface(iface, iface->config);
566 interface_claim_device(iface);
567 }
568
569 static void
570 interface_handle_config_change(struct interface *iface)
571 {
572 enum interface_config_state state = iface->config_state;
573
574 iface->config_state = IFC_NORMAL;
575 switch(state) {
576 case IFC_NORMAL:
577 break;
578 case IFC_RELOAD:
579 interface_do_reload(iface);
580 break;
581 case IFC_REMOVE:
582 interface_do_free(iface);
583 return;
584 }
585 if (iface->autostart && iface->available)
586 interface_set_up(iface);
587 }
588
589 static void
590 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
591 {
592 struct interface *iface = state->iface;
593
594 switch (ev) {
595 case IFPEV_UP:
596 if (iface->state != IFS_SETUP) {
597 interface_event(iface, IFEV_UPDATE);
598 return;
599 }
600
601 if (!iface->l3_dev.dev)
602 interface_set_l3_dev(iface, iface->main_dev.dev);
603
604 interface_ip_set_enabled(&iface->config_ip, true);
605 system_flush_routes();
606 iface->state = IFS_UP;
607 iface->start_time = system_get_rtime();
608 interface_event(iface, IFEV_UP);
609 netifd_log_message(L_NOTICE, "Interface '%s' is now up\n", iface->name);
610 break;
611 case IFPEV_DOWN:
612 if (iface->state == IFS_DOWN)
613 return;
614
615 netifd_log_message(L_NOTICE, "Interface '%s' is now down\n", iface->name);
616 mark_interface_down(iface);
617 if (iface->main_dev.dev)
618 device_release(&iface->main_dev);
619 if (iface->l3_dev.dev)
620 device_remove_user(&iface->l3_dev);
621 interface_handle_config_change(iface);
622 break;
623 case IFPEV_LINK_LOST:
624 if (iface->state != IFS_UP)
625 return;
626
627 netifd_log_message(L_NOTICE, "Interface '%s' has lost the connection\n", iface->name);
628 mark_interface_down(iface);
629 iface->state = IFS_SETUP;
630 break;
631 default:
632 return;
633 }
634
635 interface_write_resolv_conf();
636 }
637
638 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
639 {
640 if (iface->proto) {
641 iface->proto->free(iface->proto);
642 iface->proto = NULL;
643 }
644 iface->state = IFS_DOWN;
645 iface->proto = state;
646 if (!state)
647 return;
648
649 state->proto_event = interface_proto_cb;
650 state->iface = iface;
651 }
652
653 struct interface *
654 interface_alloc(const char *name, struct blob_attr *config)
655 {
656 struct interface *iface;
657 struct blob_attr *tb[IFACE_ATTR_MAX];
658 struct blob_attr *cur;
659 const char *proto_name = NULL;
660 char *iface_name;
661 bool force_link = false;
662
663 iface = calloc_a(sizeof(*iface), &iface_name, strlen(name) + 1);
664 iface->name = strcpy(iface_name, name);
665 INIT_LIST_HEAD(&iface->errors);
666 INIT_LIST_HEAD(&iface->users);
667 INIT_LIST_HEAD(&iface->hotplug_list);
668 INIT_LIST_HEAD(&iface->assignment_classes);
669 interface_ip_init(iface);
670 avl_init(&iface->data, avl_strcmp, false, NULL);
671 iface->config_ip.enabled = false;
672
673 iface->main_dev.cb = interface_cb;
674 iface->ext_dev.cb = interface_ext_cb;
675
676 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
677 blob_data(config), blob_len(config));
678
679 if ((cur = tb[IFACE_ATTR_PROTO]))
680 proto_name = blobmsg_data(cur);
681
682 proto_attach_interface(iface, proto_name);
683 if (iface->proto_handler->flags & PROTO_FLAG_FORCE_LINK_DEFAULT)
684 force_link = true;
685
686 iface->autostart = blobmsg_get_bool_default(tb[IFACE_ATTR_AUTO], true);
687 iface->force_link = blobmsg_get_bool_default(tb[IFACE_ATTR_FORCE_LINK], force_link);
688 iface->proto_ip.no_defaultroute =
689 !blobmsg_get_bool_default(tb[IFACE_ATTR_DEFAULTROUTE], true);
690 iface->proto_ip.no_dns =
691 !blobmsg_get_bool_default(tb[IFACE_ATTR_PEERDNS], true);
692
693 if ((cur = tb[IFACE_ATTR_DNS]))
694 interface_add_dns_server_list(&iface->config_ip, cur);
695
696 if ((cur = tb[IFACE_ATTR_DNS_SEARCH]))
697 interface_add_dns_search_list(&iface->config_ip, cur);
698
699 if ((cur = tb[IFACE_ATTR_METRIC]))
700 iface->metric = blobmsg_get_u32(cur);
701
702 if ((cur = tb[IFACE_ATTR_IP6ASSIGN]))
703 iface->assignment_length = blobmsg_get_u32(cur);
704
705 iface->assignment_hint = -1;
706 if ((cur = tb[IFACE_ATTR_IP6HINT]))
707 iface->assignment_hint = strtol(blobmsg_get_string(cur), NULL, 16) &
708 ~((1 << (64 - iface->assignment_length)) - 1);
709
710 if ((cur = tb[IFACE_ATTR_IP6CLASS]))
711 interface_add_assignment_classes(iface, cur);
712
713
714 if ((cur = tb[IFACE_ATTR_IP4TABLE])) {
715 if (!system_resolve_rt_table(blobmsg_data(cur), &iface->ip4table))
716 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
717 }
718
719 if ((cur = tb[IFACE_ATTR_IP6TABLE])) {
720 if (!system_resolve_rt_table(blobmsg_data(cur), &iface->ip6table))
721 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
722 }
723
724 iface->proto_ip.no_delegation = !blobmsg_get_bool_default(tb[IFACE_ATTR_DELEGATE], true);
725
726 iface->config_autostart = iface->autostart;
727 return iface;
728 }
729
730 void interface_set_dynamic(struct interface *iface)
731 {
732 iface->dynamic = true;
733 iface->autostart = true;
734 iface->node.version = -1; // Don't delete on reload
735 }
736
737 static bool __interface_add(struct interface *iface, struct blob_attr *config, bool alias)
738 {
739 struct blob_attr *tb[IFACE_ATTR_MAX];
740 struct blob_attr *cur;
741
742 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
743 blob_data(config), blob_len(config));
744
745 if (alias) {
746 if ((cur = tb[IFACE_ATTR_INTERFACE]))
747 iface->parent_ifname = blobmsg_data(cur);
748
749 if (!iface->parent_ifname)
750 return false;
751 } else {
752 if ((cur = tb[IFACE_ATTR_IFNAME]))
753 iface->ifname = blobmsg_data(cur);
754 }
755
756
757 iface->config = config;
758 vlist_add(&interfaces, &iface->node, iface->name);
759 return true;
760 }
761
762 void
763 interface_add(struct interface *iface, struct blob_attr *config)
764 {
765 __interface_add(iface, config, false);
766 }
767
768 bool
769 interface_add_alias(struct interface *iface, struct blob_attr *config)
770 {
771 if (iface->proto_handler->flags & PROTO_FLAG_NODEV)
772 return false;
773
774 return __interface_add(iface, config, true);
775 }
776
777 void
778 interface_set_l3_dev(struct interface *iface, struct device *dev)
779 {
780 bool enabled = iface->config_ip.enabled;
781 bool claimed = iface->l3_dev.claimed;
782
783 if (iface->l3_dev.dev == dev)
784 return;
785
786 interface_ip_set_enabled(&iface->config_ip, false);
787 interface_ip_flush(&iface->proto_ip);
788 device_add_user(&iface->l3_dev, dev);
789
790 if (dev) {
791 if (claimed) {
792 if (device_claim(&iface->l3_dev) < 0)
793 return;
794 }
795 interface_ip_set_enabled(&iface->config_ip, enabled);
796 }
797 }
798
799 void
800 interface_set_main_dev(struct interface *iface, struct device *dev)
801 {
802 bool claimed = iface->l3_dev.claimed;
803
804 if (iface->main_dev.dev == dev)
805 return;
806
807 interface_set_available(iface, false);
808 device_add_user(&iface->main_dev, dev);
809 if (!dev) {
810 interface_set_link_state(iface, false);
811 return;
812 }
813
814 if (claimed) {
815 if (device_claim(&iface->l3_dev) < 0)
816 return;
817 }
818
819 if (!iface->l3_dev.dev)
820 interface_set_l3_dev(iface, dev);
821 }
822
823 int
824 interface_remove_link(struct interface *iface, struct device *dev)
825 {
826 struct device *mdev = iface->main_dev.dev;
827
828 if (mdev && mdev->hotplug_ops)
829 return mdev->hotplug_ops->del(mdev, dev);
830
831 if (dev == iface->ext_dev.dev)
832 device_remove_user(&iface->ext_dev);
833
834 if (!iface->main_dev.hotplug)
835 return UBUS_STATUS_INVALID_ARGUMENT;
836
837 if (dev != iface->main_dev.dev)
838 return UBUS_STATUS_INVALID_ARGUMENT;
839
840 interface_set_main_dev(iface, NULL);
841 return 0;
842 }
843
844 static int
845 interface_add_link(struct interface *iface, struct device *dev, bool link_ext)
846 {
847 struct device *mdev = iface->main_dev.dev;
848
849 if (mdev == dev)
850 return 0;
851
852 if (iface->main_dev.hotplug)
853 device_remove_user(&iface->main_dev);
854
855 if (mdev) {
856 if (mdev->hotplug_ops)
857 return mdev->hotplug_ops->add(mdev, dev);
858 else
859 return UBUS_STATUS_NOT_SUPPORTED;
860 }
861
862 if (link_ext)
863 device_add_user(&iface->ext_dev, dev);
864
865 interface_set_main_dev(iface, dev);
866 iface->main_dev.hotplug = true;
867 return 0;
868 }
869
870 int
871 interface_handle_link(struct interface *iface, const char *name, bool add, bool link_ext)
872 {
873 struct device *dev;
874 int ret;
875
876 device_lock();
877
878 dev = device_get(name, add ? (link_ext ? 2 : 1) : 0);
879 if (!dev) {
880 ret = UBUS_STATUS_NOT_FOUND;
881 goto out;
882 }
883
884 if (add) {
885 device_set_present(dev, true);
886 if (iface->device_config && dev->default_config)
887 device_set_config(dev, dev->type, iface->config);
888
889 system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
890 ret = interface_add_link(iface, dev, link_ext);
891 } else {
892 ret = interface_remove_link(iface, dev);
893 }
894
895 out:
896 device_unlock();
897
898 return ret;
899 }
900
901 int
902 interface_set_up(struct interface *iface)
903 {
904 int ret;
905
906 iface->autostart = true;
907
908 if (iface->state != IFS_DOWN)
909 return 0;
910
911 interface_clear_errors(iface);
912 if (!iface->available) {
913 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
914 return -1;
915 }
916
917 if (iface->main_dev.dev) {
918 ret = device_claim(&iface->main_dev);
919 if (!ret)
920 interface_check_state(iface);
921 }
922 else
923 ret = __interface_set_up(iface);
924
925 return ret;
926 }
927
928 int
929 interface_set_down(struct interface *iface)
930 {
931 if (!iface) {
932 vlist_for_each_element(&interfaces, iface, node)
933 __interface_set_down(iface, false);
934 } else {
935 iface->autostart = false;
936 __interface_set_down(iface, false);
937 }
938
939 return 0;
940 }
941
942 void
943 interface_start_pending(void)
944 {
945 struct interface *iface;
946
947 vlist_for_each_element(&interfaces, iface, node) {
948 if (iface->available && iface->autostart)
949 interface_set_up(iface);
950 }
951 }
952
953 static void
954 set_config_state(struct interface *iface, enum interface_config_state s)
955 {
956 iface->config_state = s;
957 if (iface->state == IFS_DOWN)
958 interface_handle_config_change(iface);
959 else
960 __interface_set_down(iface, false);
961 }
962
963 void
964 interface_update_start(struct interface *iface)
965 {
966 iface->updated = 0;
967 interface_ip_update_start(&iface->proto_ip);
968 }
969
970 void
971 interface_update_complete(struct interface *iface)
972 {
973 interface_ip_update_complete(&iface->proto_ip);
974 }
975
976 static void
977 interface_replace_dns(struct interface_ip_settings *new, struct interface_ip_settings *old)
978 {
979 vlist_simple_replace(&new->dns_servers, &old->dns_servers);
980 vlist_simple_replace(&new->dns_search, &old->dns_search);
981 }
982
983 static void
984 interface_change_config(struct interface *if_old, struct interface *if_new)
985 {
986 struct blob_attr *old_config = if_old->config;
987 bool reload = false, reload_ip = false;
988
989 #define FIELD_CHANGED_STR(field) \
990 ((!!if_old->field != !!if_new->field) || \
991 (if_old->field && \
992 strcmp(if_old->field, if_new->field) != 0))
993
994 if (FIELD_CHANGED_STR(parent_ifname)) {
995 if (if_old->parent_iface.iface)
996 interface_remove_user(&if_old->parent_iface);
997 reload = true;
998 }
999
1000 if (FIELD_CHANGED_STR(ifname) ||
1001 if_old->proto_handler != if_new->proto_handler)
1002 reload = true;
1003
1004 if (!if_old->proto_handler->config_params)
1005 D(INTERFACE, "No config parameters for interface '%s'\n",
1006 if_old->name);
1007 else if (!uci_blob_check_equal(if_old->config, if_new->config,
1008 if_old->proto_handler->config_params))
1009 reload = true;
1010
1011 #define UPDATE(field, __var) ({ \
1012 bool __changed = (if_old->field != if_new->field); \
1013 if_old->field = if_new->field; \
1014 __var |= __changed; \
1015 })
1016
1017 if_old->config = if_new->config;
1018 if (!if_old->config_autostart && if_new->config_autostart)
1019 if_old->autostart = true;
1020
1021 if_old->device_config = if_new->device_config;
1022 if_old->config_autostart = if_new->config_autostart;
1023 if_old->ifname = if_new->ifname;
1024 if_old->parent_ifname = if_new->parent_ifname;
1025 if_old->proto_handler = if_new->proto_handler;
1026 if_old->force_link = if_new->force_link;
1027
1028 if_old->proto_ip.no_dns = if_new->proto_ip.no_dns;
1029 interface_replace_dns(&if_old->config_ip, &if_new->config_ip);
1030
1031 UPDATE(metric, reload_ip);
1032 UPDATE(proto_ip.no_defaultroute, reload_ip);
1033 UPDATE(ip4table, reload_ip);
1034 UPDATE(ip6table, reload_ip);
1035 interface_merge_assignment_data(if_old, if_new);
1036
1037 #undef UPDATE
1038
1039 if (reload) {
1040 D(INTERFACE, "Reload interface '%s' because of config changes\n",
1041 if_old->name);
1042 interface_clear_errors(if_old);
1043 set_config_state(if_old, IFC_RELOAD);
1044 goto out;
1045 }
1046
1047 if (reload_ip) {
1048 bool config_ip_enabled = if_old->config_ip.enabled;
1049 bool proto_ip_enabled = if_old->proto_ip.enabled;
1050
1051 interface_ip_set_enabled(&if_old->config_ip, false);
1052 interface_ip_set_enabled(&if_old->proto_ip, false);
1053 interface_ip_set_enabled(&if_old->proto_ip, proto_ip_enabled);
1054 interface_ip_set_enabled(&if_old->config_ip, config_ip_enabled);
1055 }
1056
1057 interface_write_resolv_conf();
1058 if (if_old->main_dev.dev)
1059 interface_check_state(if_old);
1060
1061 out:
1062 if_new->config = NULL;
1063 interface_cleanup(if_new);
1064 free(old_config);
1065 free(if_new);
1066 }
1067
1068 static void
1069 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
1070 struct vlist_node *node_old)
1071 {
1072 struct interface *if_old = container_of(node_old, struct interface, node);
1073 struct interface *if_new = container_of(node_new, struct interface, node);
1074
1075 if (node_old && node_new) {
1076 D(INTERFACE, "Update interface '%s'\n", if_new->name);
1077 interface_change_config(if_old, if_new);
1078 } else if (node_old) {
1079 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
1080 set_config_state(if_old, IFC_REMOVE);
1081 } else if (node_new) {
1082 D(INTERFACE, "Create interface '%s'\n", if_new->name);
1083 proto_init_interface(if_new, if_new->config);
1084 interface_claim_device(if_new);
1085 netifd_ubus_add_interface(if_new);
1086 }
1087 }
1088
1089
1090 static void __init
1091 interface_init_list(void)
1092 {
1093 vlist_init(&interfaces, avl_strcmp, interface_update);
1094 interfaces.keep_old = true;
1095 interfaces.no_delete = true;
1096 }