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