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