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