system-dummy: add route metric information
[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, bool reload)
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             (!reload || !iface->main_dev.hotplug))
210                 device_remove_user(&iface->main_dev);
211         iface->l3_dev = &iface->main_dev;
212         interface_set_proto_state(iface, NULL);
213 }
214
215 static void
216 interface_do_free(struct interface *iface)
217 {
218         interface_cleanup(iface, false);
219         free(iface->config);
220         netifd_ubus_remove_interface(iface);
221         avl_delete(&interfaces.avl, &iface->node.avl);
222         free(iface);
223 }
224
225 static void
226 interface_do_reload(struct interface *iface)
227 {
228         interface_cleanup(iface, true);
229         proto_init_interface(iface, iface->config);
230         interface_claim_device(iface);
231 }
232
233 static void
234 interface_handle_config_change(struct interface *iface)
235 {
236         switch(iface->config_state) {
237         case IFC_NORMAL:
238                 break;
239         case IFC_RELOAD:
240                 interface_do_reload(iface);
241                 break;
242         case IFC_REMOVE:
243                 interface_do_free(iface);
244                 return;
245         }
246         if (iface->autostart && iface->available)
247                 interface_set_up(iface);
248 }
249
250 static void
251 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
252 {
253         struct interface *iface = state->iface;
254
255         switch (ev) {
256         case IFPEV_UP:
257                 if (iface->state != IFS_SETUP)
258                         return;
259
260                 interface_ip_set_enabled(&iface->config_ip, true);
261                 system_flush_routes();
262                 iface->state = IFS_UP;
263                 iface->start_time = system_get_rtime();
264                 interface_event(iface, IFEV_UP);
265                 interface_write_resolv_conf();
266                 netifd_log_message(L_NOTICE, "Interface '%s' is now up\n", iface->name);
267                 break;
268         case IFPEV_DOWN:
269                 if (iface->state == IFS_DOWN)
270                         return;
271
272                 netifd_log_message(L_NOTICE, "Interface '%s' is now down\n", iface->name);
273                 interface_ip_set_enabled(&iface->config_ip, false);
274                 system_flush_routes();
275                 mark_interface_down(iface);
276                 interface_handle_config_change(iface);
277                 break;
278         case IFPEV_LINK_LOST:
279                 if (iface->state != IFS_UP)
280                         return;
281
282                 netifd_log_message(L_NOTICE, "Interface '%s' has lost the connection\n", iface->name);
283                 if (iface->state == IFS_UP)
284                         interface_event(iface, IFEV_DOWN);
285                 iface->state = IFS_SETUP;
286                 break;
287         }
288 }
289
290 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
291 {
292         if (iface->proto) {
293                 iface->proto->free(iface->proto);
294                 iface->proto = NULL;
295         }
296         iface->state = IFS_DOWN;
297         iface->proto = state;
298         if (!state)
299                 return;
300
301         state->proto_event = interface_proto_cb;
302         state->iface = iface;
303 }
304
305 void
306 interface_init(struct interface *iface, const char *name,
307                struct blob_attr *config)
308 {
309         struct blob_attr *tb[IFACE_ATTR_MAX];
310         struct blob_attr *cur;
311         const char *proto_name = NULL;
312
313         strncpy(iface->name, name, sizeof(iface->name) - 1);
314         INIT_LIST_HEAD(&iface->errors);
315         INIT_LIST_HEAD(&iface->users);
316         INIT_LIST_HEAD(&iface->hotplug_list);
317         interface_ip_init(&iface->proto_ip, iface);
318         interface_ip_init(&iface->config_ip, iface);
319         iface->config_ip.enabled = false;
320
321         iface->main_dev.cb = interface_cb;
322         iface->l3_dev = &iface->main_dev;
323
324         blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
325                       blob_data(config), blob_len(config));
326
327         if ((cur = tb[IFACE_ATTR_PROTO]))
328                 proto_name = blobmsg_data(cur);
329
330         proto_attach_interface(iface, proto_name);
331
332         iface->autostart = blobmsg_get_bool_default(tb[IFACE_ATTR_AUTO], true);
333         iface->proto_ip.no_defaultroute =
334                 !blobmsg_get_bool_default(tb[IFACE_ATTR_DEFAULTROUTE], true);
335
336         iface->config_autostart = iface->autostart;
337 }
338
339 void
340 interface_add(struct interface *iface, struct blob_attr *config)
341 {
342         struct blob_attr *tb[IFACE_ATTR_MAX];
343         struct blob_attr *cur;
344
345         blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
346                       blob_data(config), blob_len(config));
347
348         if ((cur = tb[IFACE_ATTR_IFNAME]))
349                 iface->ifname = blobmsg_data(cur);
350
351         iface->config = config;
352         vlist_add(&interfaces, &iface->node, iface->name);
353 }
354
355 int
356 interface_remove_link(struct interface *iface, struct device *dev)
357 {
358         struct device *mdev = iface->main_dev.dev;
359
360         if (mdev && mdev->hotplug_ops)
361                 return mdev->hotplug_ops->del(mdev, dev);
362
363         if (!iface->main_dev.hotplug)
364                 return UBUS_STATUS_INVALID_ARGUMENT;
365
366         if (dev != iface->main_dev.dev)
367                 return UBUS_STATUS_INVALID_ARGUMENT;
368
369         device_remove_user(&iface->main_dev);
370         return 0;
371 }
372
373 int
374 interface_add_link(struct interface *iface, struct device *dev)
375 {
376         struct device *mdev = iface->main_dev.dev;
377
378         if (mdev == dev)
379                 return 0;
380
381         if (iface->main_dev.hotplug)
382                 device_remove_user(&iface->main_dev);
383
384         if (mdev) {
385                 if (mdev->hotplug_ops)
386                         return mdev->hotplug_ops->add(mdev, dev);
387                 else
388                         return UBUS_STATUS_NOT_SUPPORTED;
389         }
390
391         device_add_user(&iface->main_dev, dev);
392         iface->main_dev.hotplug = true;
393         return 0;
394 }
395
396 int
397 interface_set_up(struct interface *iface)
398 {
399         int ret;
400
401         iface->autostart = true;
402
403         if (iface->state != IFS_DOWN)
404                 return 0;
405
406         interface_clear_errors(iface);
407         if (!iface->available) {
408                 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
409                 return -1;
410         }
411
412         if (iface->main_dev.dev) {
413                 ret = device_claim(&iface->main_dev);
414                 if (ret)
415                         return ret;
416         }
417
418         iface->state = IFS_SETUP;
419         ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
420         if (ret) {
421                 mark_interface_down(iface);
422                 return ret;
423         }
424
425         return 0;
426 }
427
428 int
429 interface_set_down(struct interface *iface)
430 {
431         if (!iface) {
432                 vlist_for_each_element(&interfaces, iface, node)
433                         __interface_set_down(iface, false);
434         } else {
435                 iface->autostart = false;
436                 __interface_set_down(iface, false);
437         }
438
439         return 0;
440 }
441
442 void
443 interface_start_pending(void)
444 {
445         struct interface *iface;
446
447         vlist_for_each_element(&interfaces, iface, node) {
448                 if (iface->available && iface->autostart)
449                         interface_set_up(iface);
450         }
451 }
452
453 static void
454 set_config_state(struct interface *iface, enum interface_config_state s)
455 {
456         iface->config_state = s;
457         if (iface->state == IFS_DOWN)
458                 interface_handle_config_change(iface);
459         else
460                 __interface_set_down(iface, false);
461 }
462
463 void
464 interface_update_start(struct interface *iface)
465 {
466         interface_ip_update_start(&iface->proto_ip);
467 }
468
469 void
470 interface_update_complete(struct interface *iface)
471 {
472         struct device_route *route;
473
474         interface_ip_update_complete(&iface->proto_ip);
475         vlist_for_each_element(&iface->config_ip.route, route, node) {
476                 if (iface->l3_dev->dev) {
477                         system_add_route(iface->l3_dev->dev, route);
478                         route->enabled = true;
479                 }
480         }
481 }
482
483 static void
484 interface_change_config(struct interface *if_old, struct interface *if_new)
485 {
486         struct blob_attr *old_config = if_old->config;
487         const char *old_ifname = if_old->ifname;
488         const struct proto_handler *proto = if_old->proto_handler;
489
490         interface_clear_errors(if_old);
491         if_old->config = if_new->config;
492         if (!if_old->config_autostart && if_new->config_autostart)
493                 if_old->autostart = true;
494
495         if_old->config_autostart = if_new->config_autostart;
496         if_old->ifname = if_new->ifname;
497         if_old->proto_handler = if_new->proto_handler;
498
499         if ((!!old_ifname != !!if_new->ifname) ||
500             (old_ifname && strcmp(old_ifname, if_new->ifname) != 0) ||
501             proto != if_new->proto_handler) {
502                 D(INTERFACE, "Reload interface '%s' because of ifname/proto change\n",
503                   if_old->name);
504                 goto reload;
505         }
506
507         if (!proto->config_params)
508                 D(INTERFACE, "No config parameters for interface '%s'\n",
509                   if_old->name);
510         else if (!config_check_equal(old_config, if_new->config,
511                                 proto->config_params)) {
512                 D(INTERFACE, "Reload interface '%s because of config changes\n",
513                   if_old->name);
514                 goto reload;
515         }
516
517         if (if_old->proto_ip.no_defaultroute != if_new->proto_ip.no_defaultroute) {
518                 if_old->proto_ip.no_defaultroute = if_new->proto_ip.no_defaultroute;
519                 interface_ip_set_enabled(&if_old->proto_ip, if_old->proto_ip.enabled);
520         }
521
522         goto out;
523
524 reload:
525         set_config_state(if_old, IFC_RELOAD);
526 out:
527         free(old_config);
528         free(if_new);
529 }
530
531 static void
532 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
533                  struct vlist_node *node_old)
534 {
535         struct interface *if_old = container_of(node_old, struct interface, node);
536         struct interface *if_new = container_of(node_new, struct interface, node);
537
538         if (node_old && node_new) {
539                 D(INTERFACE, "Update interface '%s'\n", if_new->name);
540                 interface_change_config(if_old, if_new);
541         } else if (node_old) {
542                 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
543                 set_config_state(if_old, IFC_REMOVE);
544         } else if (node_new) {
545                 D(INTERFACE, "Create interface '%s'\n", if_new->name);
546                 proto_init_interface(if_new, if_new->config);
547                 interface_claim_device(if_new);
548                 netifd_ubus_add_interface(if_new);
549         }
550 }
551
552
553 static void __init
554 interface_init_list(void)
555 {
556         vlist_init(&interfaces, avl_strcmp, interface_update);
557         interfaces.keep_old = true;
558         interfaces.no_delete = true;
559 }