10a91742d1ee0a073aca3ef16c08af573e55b699
[project/netifd.git] / interface.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4
5 #include "netifd.h"
6 #include "device.h"
7 #include "interface.h"
8 #include "interface-ip.h"
9 #include "proto.h"
10 #include "ubus.h"
11 #include "config.h"
12 #include "system.h"
13
14 struct vlist_tree interfaces;
15
16 enum {
17 IFACE_ATTR_IFNAME,
18 IFACE_ATTR_PROTO,
19 IFACE_ATTR_AUTO,
20 IFACE_ATTR_DEFAULTROUTE,
21 IFACE_ATTR_METRIC,
22 IFACE_ATTR_MAX
23 };
24
25 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
26 [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
27 [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
28 [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL },
29 [IFACE_ATTR_DEFAULTROUTE] = { .name = "defaultroute", .type = BLOBMSG_TYPE_BOOL },
30 [IFACE_ATTR_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
31 };
32
33 const struct config_param_list interface_attr_list = {
34 .n_params = IFACE_ATTR_MAX,
35 .params = iface_attrs,
36 };
37
38 static void
39 interface_clear_errors(struct interface *iface)
40 {
41 struct interface_error *error, *tmp;
42
43 list_for_each_entry_safe(error, tmp, &iface->errors, list) {
44 list_del(&error->list);
45 free(error);
46 }
47 }
48
49 void interface_add_error(struct interface *iface, const char *subsystem,
50 const char *code, const char **data, int n_data)
51 {
52 struct interface_error *error;
53 int i, len = 0;
54 int *datalen = NULL;
55 char *dest;
56
57 if (n_data) {
58 len = n_data * sizeof(char *);
59 datalen = alloca(len);
60 for (i = 0; i < n_data; i++) {
61 datalen[i] = strlen(data[i]) + 1;
62 len += datalen[i];
63 }
64 }
65
66 error = calloc(1, sizeof(*error) + sizeof(char *) + len);
67 if (!error)
68 return;
69
70 list_add_tail(&error->list, &iface->errors);
71 error->subsystem = subsystem;
72 error->code = code;
73
74 dest = (char *) &error->data[n_data + 1];
75 for (i = 0; i < n_data; i++) {
76 error->data[i] = dest;
77 memcpy(dest, data[i], datalen[i]);
78 dest += datalen[i];
79 }
80 error->data[n_data] = NULL;
81 }
82
83 static void
84 interface_data_del(struct interface *iface, struct interface_data *data)
85 {
86 avl_delete(&iface->data, &data->node);
87 free(data);
88 }
89
90 static void
91 interface_data_flush(struct interface *iface)
92 {
93 struct interface_data *d, *tmp;
94
95 avl_for_each_element_safe(&iface->data, d, node, tmp)
96 interface_data_del(iface, d);
97 }
98
99 int
100 interface_add_data(struct interface *iface, const struct blob_attr *data)
101 {
102 struct interface_data *n, *o;
103
104 if (!blobmsg_check_attr(data, true))
105 return UBUS_STATUS_INVALID_ARGUMENT;
106
107 n = calloc(1, sizeof(*data) + blob_pad_len(data));
108 memcpy(n->data, data, blob_pad_len(data));
109 n->node.key = blobmsg_name(data);
110
111 o = avl_find_element(&iface->data, n->node.key, o, node);
112 if (o)
113 interface_data_del(iface, o);
114
115 avl_insert(&iface->data, &n->node);
116 return 0;
117 }
118
119 static void
120 interface_event(struct interface *iface, enum interface_event ev)
121 {
122 struct interface_user *dep, *tmp;
123
124 list_for_each_entry_safe(dep, tmp, &iface->users, list)
125 dep->cb(dep, IFEV_UP);
126
127 interface_queue_event(iface, ev);
128 }
129
130 static void
131 interface_flush_state(struct interface *iface)
132 {
133 if (iface->main_dev.dev)
134 device_release(&iface->main_dev);
135 if (iface->l3_dev.dev)
136 device_release(&iface->l3_dev);
137 interface_data_flush(iface);
138 }
139
140 static void
141 mark_interface_down(struct interface *iface)
142 {
143 if (iface->state == IFS_UP)
144 interface_event(iface, IFEV_DOWN);
145 interface_ip_set_enabled(&iface->config_ip, false);
146 interface_ip_flush(&iface->proto_ip);
147 interface_flush_state(iface);
148 system_flush_routes();
149 iface->state = IFS_DOWN;
150 }
151
152 void
153 __interface_set_down(struct interface *iface, bool force)
154 {
155 interface_clear_errors(iface);
156
157 if (iface->state == IFS_DOWN ||
158 iface->state == IFS_TEARDOWN)
159 return;
160
161 if (iface->state == IFS_UP)
162 interface_event(iface, IFEV_DOWN);
163 iface->state = IFS_TEARDOWN;
164 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
165 if (force)
166 interface_flush_state(iface);
167 }
168
169 static void
170 interface_cb(struct device_user *dep, enum device_event ev)
171 {
172 struct interface *iface;
173 bool new_state;
174
175 iface = container_of(dep, struct interface, main_dev);
176 switch (ev) {
177 case DEV_EVENT_ADD:
178 new_state = true;
179 break;
180 case DEV_EVENT_REMOVE:
181 new_state = false;
182 break;
183 default:
184 return;
185 }
186
187 interface_set_available(iface, new_state);
188 }
189
190 void
191 interface_set_available(struct interface *iface, bool new_state)
192 {
193 if (iface->available == new_state)
194 return;
195
196 D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
197 iface->available = new_state;
198
199 if (new_state) {
200 if (iface->autostart && !config_init)
201 interface_set_up(iface);
202 } else
203 __interface_set_down(iface, true);
204 }
205
206 void
207 interface_add_user(struct interface_user *dep, struct interface *iface)
208 {
209 dep->iface = iface;
210 list_add(&dep->list, &iface->users);
211 if (iface->state == IFS_UP)
212 dep->cb(dep, IFEV_UP);
213 }
214
215 void
216 interface_remove_user(struct interface_user *dep)
217 {
218 list_del_init(&dep->list);
219 dep->iface = NULL;
220 }
221
222 static void
223 interface_claim_device(struct interface *iface)
224 {
225 struct device *dev;
226
227 if (iface->ifname &&
228 !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
229 dev = device_get(iface->ifname, true);
230 if (dev)
231 interface_set_main_dev(iface, dev);
232 }
233 if (iface->proto_handler->flags & PROTO_FLAG_INIT_AVAILABLE)
234 interface_set_available(iface, true);
235 }
236
237
238 static void
239 interface_cleanup(struct interface *iface, bool reload)
240 {
241 struct interface_user *dep, *tmp;
242
243 list_for_each_entry_safe(dep, tmp, &iface->users, list)
244 interface_remove_user(dep);
245
246 interface_dequeue_event(iface);
247 interface_ip_flush(&iface->config_ip);
248 interface_flush_state(iface);
249 interface_clear_errors(iface);
250 if (iface->main_dev.dev &&
251 (!reload || !iface->main_dev.hotplug))
252 interface_set_main_dev(iface, NULL);
253 interface_set_proto_state(iface, NULL);
254 }
255
256 static void
257 interface_do_free(struct interface *iface)
258 {
259 interface_cleanup(iface, false);
260 free(iface->config);
261 netifd_ubus_remove_interface(iface);
262 avl_delete(&interfaces.avl, &iface->node.avl);
263 free(iface);
264 }
265
266 static void
267 interface_do_reload(struct interface *iface)
268 {
269 interface_cleanup(iface, true);
270 proto_init_interface(iface, iface->config);
271 interface_claim_device(iface);
272 }
273
274 static void
275 interface_handle_config_change(struct interface *iface)
276 {
277 switch(iface->config_state) {
278 case IFC_NORMAL:
279 break;
280 case IFC_RELOAD:
281 interface_do_reload(iface);
282 break;
283 case IFC_REMOVE:
284 interface_do_free(iface);
285 return;
286 }
287 if (iface->autostart && iface->available)
288 interface_set_up(iface);
289 }
290
291 static void
292 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
293 {
294 struct interface *iface = state->iface;
295
296 switch (ev) {
297 case IFPEV_UP:
298 if (iface->state != IFS_SETUP)
299 return;
300
301 interface_ip_set_enabled(&iface->config_ip, true);
302 system_flush_routes();
303 iface->state = IFS_UP;
304 iface->start_time = system_get_rtime();
305 interface_event(iface, IFEV_UP);
306 interface_write_resolv_conf();
307 netifd_log_message(L_NOTICE, "Interface '%s' is now up\n", iface->name);
308 break;
309 case IFPEV_DOWN:
310 if (iface->state == IFS_DOWN)
311 return;
312
313 netifd_log_message(L_NOTICE, "Interface '%s' is now down\n", iface->name);
314 mark_interface_down(iface);
315 interface_handle_config_change(iface);
316 break;
317 case IFPEV_LINK_LOST:
318 if (iface->state != IFS_UP)
319 return;
320
321 netifd_log_message(L_NOTICE, "Interface '%s' has lost the connection\n", iface->name);
322 mark_interface_down(iface);
323 iface->state = IFS_SETUP;
324 break;
325 }
326 }
327
328 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
329 {
330 if (iface->proto) {
331 iface->proto->free(iface->proto);
332 iface->proto = NULL;
333 }
334 iface->state = IFS_DOWN;
335 iface->proto = state;
336 if (!state)
337 return;
338
339 state->proto_event = interface_proto_cb;
340 state->iface = iface;
341 }
342
343 void
344 interface_init(struct interface *iface, const char *name,
345 struct blob_attr *config)
346 {
347 struct blob_attr *tb[IFACE_ATTR_MAX];
348 struct blob_attr *cur;
349 const char *proto_name = NULL;
350
351 strncpy(iface->name, name, sizeof(iface->name) - 1);
352 INIT_LIST_HEAD(&iface->errors);
353 INIT_LIST_HEAD(&iface->users);
354 INIT_LIST_HEAD(&iface->hotplug_list);
355 interface_ip_init(&iface->proto_ip, iface);
356 interface_ip_init(&iface->config_ip, iface);
357 avl_init(&iface->data, avl_strcmp, false, NULL);
358 iface->config_ip.enabled = false;
359
360 iface->main_dev.cb = interface_cb;
361
362 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
363 blob_data(config), blob_len(config));
364
365 if ((cur = tb[IFACE_ATTR_PROTO]))
366 proto_name = blobmsg_data(cur);
367
368 proto_attach_interface(iface, proto_name);
369
370 iface->autostart = blobmsg_get_bool_default(tb[IFACE_ATTR_AUTO], true);
371 iface->proto_ip.no_defaultroute =
372 !blobmsg_get_bool_default(tb[IFACE_ATTR_DEFAULTROUTE], true);
373
374 iface->config_autostart = iface->autostart;
375 }
376
377 void
378 interface_add(struct interface *iface, struct blob_attr *config)
379 {
380 struct blob_attr *tb[IFACE_ATTR_MAX];
381 struct blob_attr *cur;
382
383 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
384 blob_data(config), blob_len(config));
385
386 if ((cur = tb[IFACE_ATTR_IFNAME]))
387 iface->ifname = blobmsg_data(cur);
388
389 iface->config = config;
390 vlist_add(&interfaces, &iface->node, iface->name);
391 }
392
393 void
394 interface_set_l3_dev(struct interface *iface, struct device *dev)
395 {
396 bool enabled = iface->config_ip.enabled;
397 bool claimed = iface->l3_dev.claimed;
398
399 if (iface->l3_dev.dev == dev)
400 return;
401
402 interface_ip_set_enabled(&iface->config_ip, false);
403 interface_ip_flush(&iface->proto_ip);
404 device_add_user(&iface->l3_dev, dev);
405
406 if (dev) {
407 if (claimed)
408 device_claim(&iface->l3_dev);
409 interface_ip_set_enabled(&iface->config_ip, enabled);
410 }
411 }
412
413 void
414 interface_set_main_dev(struct interface *iface, struct device *dev)
415 {
416 bool set_l3 = (iface->main_dev.dev == iface->l3_dev.dev);
417 bool claimed = iface->l3_dev.claimed;
418
419 if (iface->main_dev.dev == dev)
420 return;
421
422 if (set_l3)
423 interface_set_l3_dev(iface, dev);
424
425 device_add_user(&iface->main_dev, dev);
426 if (claimed)
427 device_claim(&iface->l3_dev);
428
429 if (!iface->l3_dev.dev)
430 interface_set_l3_dev(iface, dev);
431 }
432
433 int
434 interface_remove_link(struct interface *iface, struct device *dev)
435 {
436 struct device *mdev = iface->main_dev.dev;
437
438 if (mdev && mdev->hotplug_ops)
439 return mdev->hotplug_ops->del(mdev, dev);
440
441 if (!iface->main_dev.hotplug)
442 return UBUS_STATUS_INVALID_ARGUMENT;
443
444 if (dev != iface->main_dev.dev)
445 return UBUS_STATUS_INVALID_ARGUMENT;
446
447 device_remove_user(&iface->main_dev);
448 return 0;
449 }
450
451 int
452 interface_add_link(struct interface *iface, struct device *dev)
453 {
454 struct device *mdev = iface->main_dev.dev;
455
456 if (mdev == dev)
457 return 0;
458
459 if (iface->main_dev.hotplug)
460 device_remove_user(&iface->main_dev);
461
462 if (mdev) {
463 if (mdev->hotplug_ops)
464 return mdev->hotplug_ops->add(mdev, dev);
465 else
466 return UBUS_STATUS_NOT_SUPPORTED;
467 }
468
469 interface_set_main_dev(iface, dev);
470 iface->main_dev.hotplug = true;
471 return 0;
472 }
473
474 int
475 interface_set_up(struct interface *iface)
476 {
477 int ret;
478
479 iface->autostart = true;
480
481 if (iface->state != IFS_DOWN)
482 return 0;
483
484 interface_clear_errors(iface);
485 if (!iface->available) {
486 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
487 return -1;
488 }
489
490 if (iface->main_dev.dev) {
491 ret = device_claim(&iface->main_dev);
492 if (ret)
493 return ret;
494 }
495
496 iface->state = IFS_SETUP;
497 ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
498 if (ret) {
499 mark_interface_down(iface);
500 return ret;
501 }
502
503 return 0;
504 }
505
506 int
507 interface_set_down(struct interface *iface)
508 {
509 if (!iface) {
510 vlist_for_each_element(&interfaces, iface, node)
511 __interface_set_down(iface, false);
512 } else {
513 iface->autostart = false;
514 __interface_set_down(iface, false);
515 }
516
517 return 0;
518 }
519
520 void
521 interface_start_pending(void)
522 {
523 struct interface *iface;
524
525 vlist_for_each_element(&interfaces, iface, node) {
526 if (iface->available && iface->autostart)
527 interface_set_up(iface);
528 }
529 }
530
531 static void
532 set_config_state(struct interface *iface, enum interface_config_state s)
533 {
534 iface->config_state = s;
535 if (iface->state == IFS_DOWN)
536 interface_handle_config_change(iface);
537 else
538 __interface_set_down(iface, false);
539 }
540
541 void
542 interface_update_start(struct interface *iface)
543 {
544 interface_ip_update_start(&iface->proto_ip);
545 }
546
547 void
548 interface_update_complete(struct interface *iface)
549 {
550 interface_ip_update_complete(&iface->proto_ip);
551 }
552
553 static void
554 interface_change_config(struct interface *if_old, struct interface *if_new)
555 {
556 struct blob_attr *old_config = if_old->config;
557 const char *old_ifname = if_old->ifname;
558 const struct proto_handler *proto = if_old->proto_handler;
559
560 interface_clear_errors(if_old);
561 if_old->config = if_new->config;
562 if (!if_old->config_autostart && if_new->config_autostart)
563 if_old->autostart = true;
564
565 if_old->config_autostart = if_new->config_autostart;
566 if_old->ifname = if_new->ifname;
567 if_old->proto_handler = if_new->proto_handler;
568
569 if ((!!old_ifname != !!if_new->ifname) ||
570 (old_ifname && strcmp(old_ifname, if_new->ifname) != 0) ||
571 proto != if_new->proto_handler) {
572 D(INTERFACE, "Reload interface '%s' because of ifname/proto change\n",
573 if_old->name);
574 goto reload;
575 }
576
577 if (!proto->config_params)
578 D(INTERFACE, "No config parameters for interface '%s'\n",
579 if_old->name);
580 else if (!config_check_equal(old_config, if_new->config,
581 proto->config_params)) {
582 D(INTERFACE, "Reload interface '%s because of config changes\n",
583 if_old->name);
584 goto reload;
585 }
586
587 #define UPDATE(field) ({ \
588 bool __changed = (if_old->field != if_new->field); \
589 if_old->field = if_new->field; \
590 __changed; \
591 })
592
593 if (UPDATE(metric) || UPDATE(proto_ip.no_defaultroute)) {
594 interface_ip_set_enabled(&if_old->config_ip, false);
595 interface_ip_set_enabled(&if_old->config_ip, if_new->config_ip.enabled);
596 interface_ip_set_enabled(&if_old->proto_ip, false);
597 interface_ip_set_enabled(&if_old->proto_ip, if_new->proto_ip.enabled);
598 }
599
600 #undef UPDATE
601
602 goto out;
603
604 reload:
605 set_config_state(if_old, IFC_RELOAD);
606 out:
607 free(old_config);
608 free(if_new);
609 }
610
611 static void
612 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
613 struct vlist_node *node_old)
614 {
615 struct interface *if_old = container_of(node_old, struct interface, node);
616 struct interface *if_new = container_of(node_new, struct interface, node);
617
618 if (node_old && node_new) {
619 D(INTERFACE, "Update interface '%s'\n", if_new->name);
620 interface_change_config(if_old, if_new);
621 } else if (node_old) {
622 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
623 set_config_state(if_old, IFC_REMOVE);
624 } else if (node_new) {
625 D(INTERFACE, "Create interface '%s'\n", if_new->name);
626 proto_init_interface(if_new, if_new->config);
627 interface_claim_device(if_new);
628 netifd_ubus_add_interface(if_new);
629 }
630 }
631
632
633 static void __init
634 interface_init_list(void)
635 {
636 vlist_init(&interfaces, avl_strcmp, interface_update);
637 interfaces.keep_old = true;
638 interfaces.no_delete = true;
639 }