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