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