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