c861b7d16c506774bdd70accdfc060443c8df75c
[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 mark_interface_down(iface);
276 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, false);
277 }
278 break;
279 case IFS_DOWN:
280 if (iface->autostart && iface->enabled && link_state && !config_init)
281 __interface_set_up(iface);
282 break;
283 default:
284 break;
285 }
286 }
287
288 static void
289 interface_set_enabled(struct interface *iface, bool new_state)
290 {
291 if (iface->enabled == new_state)
292 return;
293
294 netifd_log_message(L_NOTICE, "Interface '%s' is %s\n", iface->name, new_state ? "enabled" : "disabled");
295 iface->enabled = new_state;
296 interface_check_state(iface);
297 }
298
299 static void
300 interface_set_link_state(struct interface *iface, bool new_state)
301 {
302 if (iface->link_state == new_state)
303 return;
304
305 netifd_log_message(L_NOTICE, "Interface '%s' has link connectivity %s\n", iface->name, new_state ? "" : "loss");
306 iface->link_state = new_state;
307 interface_check_state(iface);
308 }
309
310 static void
311 interface_cb(struct device_user *dep, enum device_event ev)
312 {
313 struct interface *iface;
314 bool new_state = false;
315
316 iface = container_of(dep, struct interface, main_dev);
317 switch (ev) {
318 case DEV_EVENT_ADD:
319 new_state = true;
320 case DEV_EVENT_REMOVE:
321 interface_set_available(iface, new_state);
322 if (!new_state && dep->dev->external)
323 interface_set_main_dev(iface, NULL);
324 break;
325 case DEV_EVENT_UP:
326 new_state = true;
327 case DEV_EVENT_DOWN:
328 interface_set_enabled(iface, new_state);
329 break;
330 case DEV_EVENT_LINK_UP:
331 new_state = true;
332 case DEV_EVENT_LINK_DOWN:
333 interface_set_link_state(iface, new_state);
334 break;
335 case DEV_EVENT_TOPO_CHANGE:
336 interface_proto_event(iface->proto, PROTO_CMD_RENEW, false);
337 return;
338 default:
339 break;
340 }
341 }
342
343 void
344 interface_set_available(struct interface *iface, bool new_state)
345 {
346 if (iface->available == new_state)
347 return;
348
349 D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
350 iface->available = new_state;
351
352 if (new_state) {
353 if (iface->autostart && !config_init)
354 interface_set_up(iface);
355 } else
356 __interface_set_down(iface, true);
357 }
358
359 void
360 interface_add_user(struct interface_user *dep, struct interface *iface)
361 {
362 if (!iface) {
363 list_add(&dep->list, &iface_all_users);
364 return;
365 }
366
367 dep->iface = iface;
368 list_add(&dep->list, &iface->users);
369 if (iface->state == IFS_UP)
370 dep->cb(dep, iface, IFEV_UP);
371 }
372
373 void
374 interface_remove_user(struct interface_user *dep)
375 {
376 list_del_init(&dep->list);
377 dep->iface = NULL;
378 }
379
380 static void
381 interface_add_assignment_classes(struct interface *iface, struct blob_attr *list)
382 {
383 struct blob_attr *cur;
384 int rem;
385
386 blobmsg_for_each_attr(cur, list, rem) {
387 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
388 continue;
389
390 if (!blobmsg_check_attr(cur, NULL))
391 continue;
392
393 struct interface_assignment_class *c = malloc(sizeof(*c) + blobmsg_data_len(cur));
394 memcpy(c->name, blobmsg_data(cur), blobmsg_data_len(cur));
395 list_add(&c->head, &iface->assignment_classes);
396 }
397 }
398
399 static void
400 interface_clear_assignment_classes(struct interface *iface)
401 {
402 while (!list_empty(&iface->assignment_classes)) {
403 struct interface_assignment_class *c = list_first_entry(&iface->assignment_classes,
404 struct interface_assignment_class, head);
405 list_del(&c->head);
406 free(c);
407 }
408 }
409
410 static void
411 interface_merge_assignment_data(struct interface *old, struct interface *new)
412 {
413 bool changed = (old->assignment_hint != new->assignment_hint ||
414 old->assignment_length != new->assignment_length ||
415 list_empty(&old->assignment_classes) != list_empty(&new->assignment_classes));
416
417 struct interface_assignment_class *c;
418 list_for_each_entry(c, &new->assignment_classes, head) {
419 // Compare list entries one-by-one to see if there was a change
420 if (list_empty(&old->assignment_classes)) // The new list is longer
421 changed = true;
422
423 if (changed)
424 break;
425
426 struct interface_assignment_class *c_old = list_first_entry(&old->assignment_classes,
427 struct interface_assignment_class, head);
428
429 if (strcmp(c_old->name, c->name)) // An entry didn't match
430 break;
431
432 list_del(&c_old->head);
433 free(c_old);
434 }
435
436 // The old list was longer than the new one or the last entry didn't match
437 if (!list_empty(&old->assignment_classes)) {
438 interface_clear_assignment_classes(old);
439 changed = true;
440 }
441
442 list_splice_init(&new->assignment_classes, &old->assignment_classes);
443
444 if (changed) {
445 old->assignment_hint = new->assignment_hint;
446 old->assignment_length = new->assignment_length;
447 interface_refresh_assignments(true);
448 }
449 }
450
451 static void
452 interface_alias_cb(struct interface_user *dep, struct interface *iface, enum interface_event ev)
453 {
454 struct interface *alias = container_of(dep, struct interface, parent_iface);
455 struct device *dev = iface->l3_dev.dev;
456
457 switch (ev) {
458 case IFEV_UP:
459 if (!dev)
460 return;
461
462 interface_set_main_dev(alias, dev);
463 interface_set_available(alias, true);
464 break;
465 case IFEV_DOWN:
466 interface_set_available(alias, false);
467 interface_set_main_dev(alias, NULL);
468 break;
469 case IFEV_FREE:
470 interface_remove_user(dep);
471 break;
472 case IFEV_RELOAD:
473 case IFEV_UPDATE:
474 break;
475 }
476 }
477
478 static void
479 interface_claim_device(struct interface *iface)
480 {
481 struct interface *parent;
482 struct device *dev = NULL;
483
484 if (iface->parent_iface.iface)
485 interface_remove_user(&iface->parent_iface);
486
487 if (iface->parent_ifname) {
488 parent = vlist_find(&interfaces, iface->parent_ifname, parent, node);
489 iface->parent_iface.cb = interface_alias_cb;
490 interface_add_user(&iface->parent_iface, parent);
491 } else if (iface->ifname &&
492 !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
493 dev = device_get(iface->ifname, true);
494 }
495
496 if (dev)
497 interface_set_main_dev(iface, dev);
498
499 if (iface->proto_handler->flags & PROTO_FLAG_INIT_AVAILABLE)
500 interface_set_available(iface, true);
501 }
502
503 static void
504 interface_cleanup_state(struct interface *iface)
505 {
506 interface_set_available(iface, false);
507
508 interface_flush_state(iface);
509 interface_clear_errors(iface);
510 interface_set_proto_state(iface, NULL);
511
512 if (iface->main_dev.dev)
513 interface_set_main_dev(iface, NULL);
514 }
515
516 static void
517 interface_cleanup(struct interface *iface)
518 {
519 struct interface_user *dep, *tmp;
520
521 if (iface->parent_iface.iface)
522 interface_remove_user(&iface->parent_iface);
523
524 list_for_each_entry_safe(dep, tmp, &iface->users, list)
525 interface_remove_user(dep);
526
527 interface_clear_assignment_classes(iface);
528 interface_ip_flush(&iface->config_ip);
529 interface_cleanup_state(iface);
530 }
531
532 static void
533 interface_do_free(struct interface *iface)
534 {
535 interface_event(iface, IFEV_FREE);
536 interface_cleanup(iface);
537 free(iface->config);
538 netifd_ubus_remove_interface(iface);
539 avl_delete(&interfaces.avl, &iface->node.avl);
540 free(iface);
541 }
542
543 static void
544 interface_do_reload(struct interface *iface)
545 {
546 interface_event(iface, IFEV_RELOAD);
547 interface_cleanup_state(iface);
548 proto_init_interface(iface, iface->config);
549 interface_claim_device(iface);
550 }
551
552 static void
553 interface_handle_config_change(struct interface *iface)
554 {
555 enum interface_config_state state = iface->config_state;
556
557 iface->config_state = IFC_NORMAL;
558 switch(state) {
559 case IFC_NORMAL:
560 break;
561 case IFC_RELOAD:
562 interface_do_reload(iface);
563 break;
564 case IFC_REMOVE:
565 interface_do_free(iface);
566 return;
567 }
568 if (iface->autostart && iface->available)
569 interface_set_up(iface);
570 }
571
572 static void
573 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
574 {
575 struct interface *iface = state->iface;
576
577 switch (ev) {
578 case IFPEV_UP:
579 if (iface->state != IFS_SETUP) {
580 interface_event(iface, IFEV_UPDATE);
581 return;
582 }
583
584 interface_ip_set_enabled(&iface->config_ip, true);
585 system_flush_routes();
586 iface->state = IFS_UP;
587 iface->start_time = system_get_rtime();
588 interface_event(iface, IFEV_UP);
589 netifd_log_message(L_NOTICE, "Interface '%s' is now up\n", iface->name);
590 break;
591 case IFPEV_DOWN:
592 if (iface->state == IFS_DOWN)
593 return;
594
595 netifd_log_message(L_NOTICE, "Interface '%s' is now down\n", iface->name);
596 mark_interface_down(iface);
597 if (iface->main_dev.dev)
598 device_release(&iface->main_dev);
599 if (iface->l3_dev.dev)
600 device_remove_user(&iface->l3_dev);
601 interface_handle_config_change(iface);
602 break;
603 case IFPEV_LINK_LOST:
604 if (iface->state != IFS_UP)
605 return;
606
607 netifd_log_message(L_NOTICE, "Interface '%s' has lost the connection\n", iface->name);
608 mark_interface_down(iface);
609 iface->state = IFS_SETUP;
610 break;
611 default:
612 return;
613 }
614
615 interface_write_resolv_conf();
616 }
617
618 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
619 {
620 if (iface->proto) {
621 iface->proto->free(iface->proto);
622 iface->proto = NULL;
623 }
624 iface->state = IFS_DOWN;
625 iface->proto = state;
626 if (!state)
627 return;
628
629 state->proto_event = interface_proto_cb;
630 state->iface = iface;
631 }
632
633 struct interface *
634 interface_alloc(const char *name, struct blob_attr *config)
635 {
636 struct interface *iface;
637 struct blob_attr *tb[IFACE_ATTR_MAX];
638 struct blob_attr *cur;
639 const char *proto_name = NULL;
640 char *iface_name;
641
642 iface = calloc_a(sizeof(*iface), &iface_name, strlen(name) + 1);
643 iface->name = strcpy(iface_name, name);
644 INIT_LIST_HEAD(&iface->errors);
645 INIT_LIST_HEAD(&iface->users);
646 INIT_LIST_HEAD(&iface->hotplug_list);
647 INIT_LIST_HEAD(&iface->assignment_classes);
648 interface_ip_init(iface);
649 avl_init(&iface->data, avl_strcmp, false, NULL);
650 iface->config_ip.enabled = false;
651
652 iface->main_dev.cb = interface_cb;
653
654 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
655 blob_data(config), blob_len(config));
656
657 if ((cur = tb[IFACE_ATTR_PROTO]))
658 proto_name = blobmsg_data(cur);
659
660 proto_attach_interface(iface, proto_name);
661
662 iface->autostart = blobmsg_get_bool_default(tb[IFACE_ATTR_AUTO], true);
663 iface->force_link = blobmsg_get_bool_default(tb[IFACE_ATTR_FORCE_LINK], false);
664 iface->proto_ip.no_defaultroute =
665 !blobmsg_get_bool_default(tb[IFACE_ATTR_DEFAULTROUTE], true);
666 iface->proto_ip.no_dns =
667 !blobmsg_get_bool_default(tb[IFACE_ATTR_PEERDNS], true);
668
669 if ((cur = tb[IFACE_ATTR_DNS]))
670 interface_add_dns_server_list(&iface->config_ip, cur);
671
672 if ((cur = tb[IFACE_ATTR_DNS_SEARCH]))
673 interface_add_dns_search_list(&iface->config_ip, cur);
674
675 if ((cur = tb[IFACE_ATTR_METRIC]))
676 iface->metric = blobmsg_get_u32(cur);
677
678 if ((cur = tb[IFACE_ATTR_IP6ASSIGN]))
679 iface->assignment_length = blobmsg_get_u32(cur);
680
681 iface->assignment_hint = -1;
682 if ((cur = tb[IFACE_ATTR_IP6HINT]))
683 iface->assignment_hint = strtol(blobmsg_get_string(cur), NULL, 16) &
684 ~((1 << (64 - iface->assignment_length)) - 1);
685
686 if ((cur = tb[IFACE_ATTR_IP6CLASS]))
687 interface_add_assignment_classes(iface, cur);
688
689
690 if ((cur = tb[IFACE_ATTR_IP4TABLE])) {
691 if (!system_resolve_rt_table(blobmsg_data(cur), &iface->ip4table))
692 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
693 }
694
695 if ((cur = tb[IFACE_ATTR_IP6TABLE])) {
696 if (!system_resolve_rt_table(blobmsg_data(cur), &iface->ip6table))
697 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
698 }
699
700 iface->proto_ip.no_delegation = !blobmsg_get_bool_default(tb[IFACE_ATTR_DELEGATE], true);
701
702 iface->config_autostart = iface->autostart;
703 return iface;
704 }
705
706 void interface_set_dynamic(struct interface *iface)
707 {
708 iface->dynamic = true;
709 iface->node.version = -1; // Don't delete on reload
710 }
711
712 static bool __interface_add(struct interface *iface, struct blob_attr *config, bool alias)
713 {
714 struct blob_attr *tb[IFACE_ATTR_MAX];
715 struct blob_attr *cur;
716
717 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
718 blob_data(config), blob_len(config));
719
720 if (alias) {
721 if ((cur = tb[IFACE_ATTR_INTERFACE]))
722 iface->parent_ifname = blobmsg_data(cur);
723
724 if (!iface->parent_ifname)
725 return false;
726 } else {
727 if ((cur = tb[IFACE_ATTR_IFNAME]))
728 iface->ifname = blobmsg_data(cur);
729 }
730
731
732 iface->config = config;
733 vlist_add(&interfaces, &iface->node, iface->name);
734 return true;
735 }
736
737 void
738 interface_add(struct interface *iface, struct blob_attr *config)
739 {
740 __interface_add(iface, config, false);
741 }
742
743 bool
744 interface_add_alias(struct interface *iface, struct blob_attr *config)
745 {
746 if (iface->proto_handler->flags & PROTO_FLAG_NODEV)
747 return false;
748
749 return __interface_add(iface, config, true);
750 }
751
752 void
753 interface_set_l3_dev(struct interface *iface, struct device *dev)
754 {
755 bool enabled = iface->config_ip.enabled;
756 bool claimed = iface->l3_dev.claimed;
757
758 if (iface->l3_dev.dev == dev)
759 return;
760
761 interface_ip_set_enabled(&iface->config_ip, false);
762 interface_ip_flush(&iface->proto_ip);
763 device_add_user(&iface->l3_dev, dev);
764
765 if (dev) {
766 if (claimed)
767 device_claim(&iface->l3_dev);
768 interface_ip_set_enabled(&iface->config_ip, enabled);
769 }
770 }
771
772 void
773 interface_set_main_dev(struct interface *iface, struct device *dev)
774 {
775 bool set_l3 = (!dev || iface->main_dev.dev == iface->l3_dev.dev);
776 bool claimed = iface->l3_dev.claimed;
777
778 if (iface->main_dev.dev == dev)
779 return;
780
781 if (set_l3)
782 interface_set_l3_dev(iface, dev);
783
784 device_add_user(&iface->main_dev, dev);
785 if (!dev) {
786 interface_set_link_state(iface, false);
787 return;
788 }
789
790 if (claimed)
791 device_claim(&iface->l3_dev);
792
793 if (!iface->l3_dev.dev)
794 interface_set_l3_dev(iface, dev);
795 }
796
797 int
798 interface_remove_link(struct interface *iface, struct device *dev)
799 {
800 struct device *mdev = iface->main_dev.dev;
801
802 if (mdev && mdev->hotplug_ops)
803 return mdev->hotplug_ops->del(mdev, dev);
804
805 if (!iface->main_dev.hotplug)
806 return UBUS_STATUS_INVALID_ARGUMENT;
807
808 if (dev != iface->main_dev.dev)
809 return UBUS_STATUS_INVALID_ARGUMENT;
810
811 device_remove_user(&iface->main_dev);
812 return 0;
813 }
814
815 int
816 interface_add_link(struct interface *iface, struct device *dev)
817 {
818 struct device *mdev = iface->main_dev.dev;
819
820 if (mdev == dev)
821 return 0;
822
823 if (iface->main_dev.hotplug)
824 device_remove_user(&iface->main_dev);
825
826 if (mdev) {
827 if (mdev->hotplug_ops)
828 return mdev->hotplug_ops->add(mdev, dev);
829 else
830 return UBUS_STATUS_NOT_SUPPORTED;
831 }
832
833 interface_set_main_dev(iface, dev);
834 iface->main_dev.hotplug = true;
835 return 0;
836 }
837
838 int
839 interface_handle_link(struct interface *iface, const char *name, bool add)
840 {
841 struct device *dev;
842 int ret;
843
844 device_lock();
845
846 dev = device_get(name, add ? 2 : 0);
847 if (!dev) {
848 ret = UBUS_STATUS_NOT_FOUND;
849 goto out;
850 }
851
852 if (add) {
853 device_set_present(dev, true);
854 if (iface->device_config)
855 device_set_config(dev, &simple_device_type, iface->config);
856
857 system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
858 ret = interface_add_link(iface, dev);
859 } else {
860 ret = interface_remove_link(iface, dev);
861 }
862
863 out:
864 device_unlock();
865
866 return ret;
867 }
868
869 int
870 interface_set_up(struct interface *iface)
871 {
872 int ret;
873
874 iface->autostart = true;
875
876 if (iface->state != IFS_DOWN)
877 return 0;
878
879 interface_clear_errors(iface);
880 if (!iface->available) {
881 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
882 return -1;
883 }
884
885 if (iface->main_dev.dev) {
886 ret = device_claim(&iface->main_dev);
887 if (!ret)
888 interface_check_state(iface);
889 }
890 else
891 ret = __interface_set_up(iface);
892
893 return ret;
894 }
895
896 int
897 interface_set_down(struct interface *iface)
898 {
899 if (!iface) {
900 vlist_for_each_element(&interfaces, iface, node)
901 __interface_set_down(iface, false);
902 } else {
903 iface->autostart = false;
904 __interface_set_down(iface, false);
905 }
906
907 return 0;
908 }
909
910 void
911 interface_start_pending(void)
912 {
913 struct interface *iface;
914
915 vlist_for_each_element(&interfaces, iface, node) {
916 if (iface->available && iface->autostart)
917 interface_set_up(iface);
918 }
919 }
920
921 static void
922 set_config_state(struct interface *iface, enum interface_config_state s)
923 {
924 iface->config_state = s;
925 if (iface->state == IFS_DOWN)
926 interface_handle_config_change(iface);
927 else
928 __interface_set_down(iface, false);
929 }
930
931 void
932 interface_update_start(struct interface *iface)
933 {
934 iface->updated = 0;
935 interface_ip_update_start(&iface->proto_ip);
936 }
937
938 void
939 interface_update_complete(struct interface *iface)
940 {
941 interface_ip_update_complete(&iface->proto_ip);
942 }
943
944 static void
945 interface_replace_dns(struct interface_ip_settings *new, struct interface_ip_settings *old)
946 {
947 vlist_simple_replace(&new->dns_servers, &old->dns_servers);
948 vlist_simple_replace(&new->dns_search, &old->dns_search);
949 }
950
951 static void
952 interface_change_config(struct interface *if_old, struct interface *if_new)
953 {
954 struct blob_attr *old_config = if_old->config;
955 bool reload = false, reload_ip = false;
956
957 #define FIELD_CHANGED_STR(field) \
958 ((!!if_old->field != !!if_new->field) || \
959 (if_old->field && \
960 strcmp(if_old->field, if_new->field) != 0))
961
962 if (FIELD_CHANGED_STR(parent_ifname)) {
963 if (if_old->parent_iface.iface)
964 interface_remove_user(&if_old->parent_iface);
965 reload = true;
966 }
967
968 if (FIELD_CHANGED_STR(ifname) ||
969 if_old->proto_handler != if_new->proto_handler)
970 reload = true;
971
972 if (!if_old->proto_handler->config_params)
973 D(INTERFACE, "No config parameters for interface '%s'\n",
974 if_old->name);
975 else if (!uci_blob_check_equal(if_old->config, if_new->config,
976 if_old->proto_handler->config_params))
977 reload = true;
978
979 #define UPDATE(field, __var) ({ \
980 bool __changed = (if_old->field != if_new->field); \
981 if_old->field = if_new->field; \
982 __var |= __changed; \
983 })
984
985 if_old->config = if_new->config;
986 if (!if_old->config_autostart && if_new->config_autostart)
987 if_old->autostart = true;
988
989 if_old->device_config = if_new->device_config;
990 if_old->config_autostart = if_new->config_autostart;
991 if_old->ifname = if_new->ifname;
992 if_old->parent_ifname = if_new->parent_ifname;
993 if_old->proto_handler = if_new->proto_handler;
994 if_old->force_link = if_new->force_link;
995
996 if_old->proto_ip.no_dns = if_new->proto_ip.no_dns;
997 interface_replace_dns(&if_old->config_ip, &if_new->config_ip);
998
999 UPDATE(metric, reload_ip);
1000 UPDATE(proto_ip.no_defaultroute, reload_ip);
1001 UPDATE(ip4table, reload_ip);
1002 UPDATE(ip6table, reload_ip);
1003 interface_merge_assignment_data(if_old, if_new);
1004
1005 #undef UPDATE
1006
1007 if (reload) {
1008 D(INTERFACE, "Reload interface '%s' because of config changes\n",
1009 if_old->name);
1010 interface_clear_errors(if_old);
1011 set_config_state(if_old, IFC_RELOAD);
1012 goto out;
1013 }
1014
1015 if (reload_ip) {
1016 interface_ip_set_enabled(&if_old->config_ip, false);
1017 interface_ip_set_enabled(&if_old->proto_ip, false);
1018 interface_ip_set_enabled(&if_old->proto_ip, if_new->proto_ip.enabled);
1019 interface_ip_set_enabled(&if_old->config_ip, if_new->config_ip.enabled);
1020 }
1021
1022 interface_write_resolv_conf();
1023 interface_check_state(if_old);
1024
1025 out:
1026 if_new->config = NULL;
1027 interface_cleanup(if_new);
1028 free(old_config);
1029 free(if_new);
1030 }
1031
1032 static void
1033 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
1034 struct vlist_node *node_old)
1035 {
1036 struct interface *if_old = container_of(node_old, struct interface, node);
1037 struct interface *if_new = container_of(node_new, struct interface, node);
1038
1039 if (node_old && node_new) {
1040 D(INTERFACE, "Update interface '%s'\n", if_new->name);
1041 interface_change_config(if_old, if_new);
1042 } else if (node_old) {
1043 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
1044 set_config_state(if_old, IFC_REMOVE);
1045 } else if (node_new) {
1046 D(INTERFACE, "Create interface '%s'\n", if_new->name);
1047 proto_init_interface(if_new, if_new->config);
1048 interface_claim_device(if_new);
1049 netifd_ubus_add_interface(if_new);
1050 }
1051 }
1052
1053
1054 static void __init
1055 interface_init_list(void)
1056 {
1057 vlist_init(&interfaces, avl_strcmp, interface_update);
1058 interfaces.keep_old = true;
1059 interfaces.no_delete = true;
1060 }