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