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