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