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