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