add a generic option for disabling the automatic default route on interfaces
[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 && iface->proto_handler &&
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 void
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                 mdev->hotplug_ops->del(mdev, dev);
361                 return;
362         }
363
364         device_remove_user(&iface->main_dev);
365 }
366
367 int
368 interface_add_link(struct interface *iface, struct device *dev)
369 {
370         struct device *mdev = iface->main_dev.dev;
371
372         if (mdev && mdev->hotplug_ops)
373                 return mdev->hotplug_ops->add(mdev, dev);
374
375         if (iface->main_dev.dev)
376                 interface_remove_link(iface, NULL);
377
378         device_add_user(&iface->main_dev, dev);
379
380         return 0;
381 }
382
383 int
384 interface_set_up(struct interface *iface)
385 {
386         int ret;
387
388         iface->autostart = true;
389
390         if (iface->state != IFS_DOWN)
391                 return 0;
392
393         interface_clear_errors(iface);
394         if (!iface->available) {
395                 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
396                 return -1;
397         }
398
399         if (iface->main_dev.dev) {
400                 ret = device_claim(&iface->main_dev);
401                 if (ret)
402                         return ret;
403         }
404
405         iface->state = IFS_SETUP;
406         ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
407         if (ret) {
408                 mark_interface_down(iface);
409                 return ret;
410         }
411
412         return 0;
413 }
414
415 int
416 interface_set_down(struct interface *iface)
417 {
418         if (!iface) {
419                 vlist_for_each_element(&interfaces, iface, node)
420                         __interface_set_down(iface, false);
421         } else {
422                 iface->autostart = false;
423                 __interface_set_down(iface, false);
424         }
425
426         return 0;
427 }
428
429 void
430 interface_start_pending(void)
431 {
432         struct interface *iface;
433
434         vlist_for_each_element(&interfaces, iface, node) {
435                 if (iface->available && iface->autostart)
436                         interface_set_up(iface);
437         }
438 }
439
440 static void
441 set_config_state(struct interface *iface, enum interface_config_state s)
442 {
443         iface->config_state = s;
444         if (iface->state == IFS_DOWN)
445                 interface_handle_config_change(iface);
446         else
447                 __interface_set_down(iface, false);
448 }
449
450 void
451 interface_update_start(struct interface *iface)
452 {
453         interface_ip_update_start(&iface->proto_ip);
454 }
455
456 void
457 interface_update_complete(struct interface *iface)
458 {
459         struct device_route *route;
460
461         interface_ip_update_complete(&iface->proto_ip);
462         vlist_for_each_element(&iface->config_ip.route, route, node) {
463                 if (iface->l3_dev->dev) {
464                         system_add_route(iface->l3_dev->dev, route);
465                         route->enabled = true;
466                 }
467         }
468 }
469
470 static void
471 interface_change_config(struct interface *if_old, struct interface *if_new)
472 {
473         struct blob_attr *old_config = if_old->config;
474         const char *old_ifname = if_old->ifname;
475         const struct proto_handler *proto = if_old->proto_handler;
476
477         interface_clear_errors(if_old);
478         if_old->config = if_new->config;
479         if (!if_old->config_autostart && if_new->config_autostart)
480                 if_old->autostart = true;
481
482         if_old->config_autostart = if_new->config_autostart;
483         if_old->ifname = if_new->ifname;
484         if_old->proto_handler = if_new->proto_handler;
485
486         if ((!!old_ifname != !!if_new->ifname) ||
487             (old_ifname && strcmp(old_ifname, if_new->ifname) != 0) ||
488             proto != if_new->proto_handler) {
489                 D(INTERFACE, "Reload interface '%s' because of ifname/proto change\n",
490                   if_old->name);
491                 goto reload;
492         }
493
494         if (!proto->config_params)
495                 D(INTERFACE, "No config parameters for interface '%s'\n",
496                   if_old->name);
497         else if (!config_check_equal(old_config, if_new->config,
498                                 proto->config_params)) {
499                 D(INTERFACE, "Reload interface '%s because of config changes\n",
500                   if_old->name);
501                 goto reload;
502         }
503
504         if (if_old->proto_ip.no_defaultroute != if_new->proto_ip.no_defaultroute) {
505                 if_old->proto_ip.no_defaultroute = if_new->proto_ip.no_defaultroute;
506                 interface_ip_set_enabled(&if_old->proto_ip, if_old->proto_ip.enabled);
507         }
508
509         goto out;
510
511 reload:
512         set_config_state(if_old, IFC_RELOAD);
513 out:
514         free(old_config);
515         free(if_new);
516 }
517
518 static void
519 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
520                  struct vlist_node *node_old)
521 {
522         struct interface *if_old = container_of(node_old, struct interface, node);
523         struct interface *if_new = container_of(node_new, struct interface, node);
524
525         if (node_old && node_new) {
526                 D(INTERFACE, "Update interface '%s'\n", if_new->name);
527                 interface_change_config(if_old, if_new);
528         } else if (node_old) {
529                 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
530                 set_config_state(if_old, IFC_REMOVE);
531         } else if (node_new) {
532                 D(INTERFACE, "Create interface '%s'\n", if_new->name);
533                 proto_init_interface(if_new, if_new->config);
534                 interface_claim_device(if_new);
535                 netifd_ubus_add_interface(if_new);
536         }
537 }
538
539
540 static void __init
541 interface_init_list(void)
542 {
543         vlist_init(&interfaces, avl_strcmp, interface_update,
544                    struct interface, node, name);
545         interfaces.keep_old = true;
546         interfaces.no_delete = true;
547 }