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