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