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