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