on interface free, remove the main_dev user if still present
[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
13 struct vlist_tree interfaces;
14
15 enum {
16         IFACE_ATTR_IFNAME,
17         IFACE_ATTR_PROTO,
18         IFACE_ATTR_AUTO,
19         IFACE_ATTR_MAX
20 };
21
22 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
23         [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
24         [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
25         [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL },
26 };
27
28 const struct config_param_list interface_attr_list = {
29         .n_params = IFACE_ATTR_MAX,
30         .params = iface_attrs,
31 };
32
33 static void
34 clear_interface_errors(struct interface *iface)
35 {
36         struct interface_error *error, *tmp;
37
38         list_for_each_entry_safe(error, tmp, &iface->errors, list) {
39                 list_del(&error->list);
40                 free(error);
41         }
42 }
43
44 void interface_add_error(struct interface *iface, const char *subsystem,
45                          const char *code, const char **data, int n_data)
46 {
47         struct interface_error *error;
48         int i, len = 0;
49         int *datalen = NULL;
50         char *dest;
51
52         if (n_data) {
53                 len = n_data * sizeof(char *);
54                 datalen = alloca(len);
55                 for (i = 0; i < n_data; i++) {
56                         datalen[i] = strlen(data[i]) + 1;
57                         len += datalen[i];
58                 }
59         }
60
61         error = calloc(1, sizeof(*error) + sizeof(char *) + len);
62         if (!error)
63                 return;
64
65         list_add_tail(&error->list, &iface->errors);
66         error->subsystem = subsystem;
67         error->code = code;
68
69         dest = (char *) &error->data[n_data + 1];
70         for (i = 0; i < n_data; i++) {
71                 error->data[i] = dest;
72                 memcpy(dest, data[i], datalen[i]);
73                 dest += datalen[i];
74         }
75         error->data[n_data] = NULL;
76 }
77
78 static void
79 interface_event(struct interface *iface, enum interface_event ev)
80 {
81         /* TODO */
82 }
83
84 static void
85 mark_interface_down(struct interface *iface)
86 {
87         vlist_flush_all(&iface->proto_addr);
88         vlist_flush_all(&iface->proto_route);
89         if (iface->main_dev.dev)
90                 device_release(&iface->main_dev);
91         iface->state = IFS_DOWN;
92 }
93
94 static int
95 __interface_set_up(struct interface *iface)
96 {
97         int ret;
98
99         if (iface->state != IFS_DOWN)
100                 return 0;
101
102         if (iface->main_dev.dev) {
103                 ret = device_claim(&iface->main_dev);
104                 if (ret)
105                         return ret;
106         }
107
108         iface->state = IFS_SETUP;
109         ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
110         if (ret) {
111                 mark_interface_down(iface);
112                 return ret;
113         }
114
115         return 0;
116
117 }
118
119 static void
120 __interface_set_down(struct interface *iface, bool force)
121 {
122         clear_interface_errors(iface);
123
124         if (iface->state == IFS_DOWN ||
125                 iface->state == IFS_TEARDOWN)
126                 return;
127
128         iface->state = IFS_TEARDOWN;
129         interface_event(iface, IFEV_DOWN);
130         interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
131 }
132
133 static void
134 interface_cb(struct device_user *dep, enum device_event ev)
135 {
136         struct interface *iface;
137         bool new_state;
138
139         iface = container_of(dep, struct interface, main_dev);
140         switch (ev) {
141         case DEV_EVENT_ADD:
142                 new_state = true;
143                 break;
144         case DEV_EVENT_REMOVE:
145                 new_state = false;
146                 break;
147         default:
148                 return;
149         }
150
151         interface_set_available(iface, new_state);
152 }
153
154 void
155 interface_set_available(struct interface *iface, bool new_state)
156 {
157         if (iface->available == new_state)
158                 return;
159
160         iface->available = new_state;
161
162         if (new_state) {
163                 if (iface->autostart && !config_init)
164                         interface_set_up(iface);
165         } else
166                 __interface_set_down(iface, true);
167 }
168
169 static void
170 interface_do_free(struct interface *iface)
171 {
172         if (iface->main_dev.dev)
173                 device_remove_user(&iface->main_dev);
174         interface_set_proto_state(iface, NULL);
175         free(iface->config);
176         netifd_ubus_remove_interface(iface);
177         free(iface);
178 }
179
180 static void
181 interface_handle_config_change(struct interface *iface)
182 {
183         switch(iface->config_state) {
184         case IFC_NORMAL:
185                 break;
186         case IFC_RELOAD:
187                 if (iface->autostart)
188                         interface_set_up(iface);
189                 break;
190         case IFC_REMOVE:
191                 interface_do_free(iface);
192                 break;
193         }
194 }
195
196 static void
197 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
198 {
199         struct interface *iface = state->iface;
200
201         switch (ev) {
202         case IFPEV_UP:
203                 if (iface->state != IFS_SETUP)
204                         return;
205
206                 iface->state = IFS_UP;
207                 interface_event(iface, IFEV_UP);
208                 break;
209         case IFPEV_DOWN:
210                 if (iface->state == IFS_DOWN)
211                         return;
212
213                 mark_interface_down(iface);
214                 interface_handle_config_change(iface);
215                 break;
216         case IFPEV_LINK_LOST:
217                 if (iface->state != IFS_UP)
218                         return;
219
220                 iface->state = IFS_SETUP;
221                 interface_event(iface, IFEV_DOWN);
222                 break;
223         }
224 }
225
226 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
227 {
228         if (iface->proto) {
229                 iface->proto->free(iface->proto);
230                 iface->proto = NULL;
231         }
232         iface->state = IFS_DOWN;
233         iface->proto = state;
234         if (!state)
235                 return;
236
237         state->proto_event = interface_proto_cb;
238         state->iface = iface;
239 }
240
241 void
242 interface_init(struct interface *iface, const char *name,
243                struct blob_attr *config)
244 {
245         struct blob_attr *tb[IFACE_ATTR_MAX];
246         struct blob_attr *cur;
247         const char *proto_name = NULL;
248
249         strncpy(iface->name, name, sizeof(iface->name) - 1);
250         INIT_LIST_HEAD(&iface->errors);
251
252         iface->main_dev.cb = interface_cb;
253         iface->l3_dev = &iface->main_dev;
254
255         blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
256                       blob_data(config), blob_len(config));
257
258         if ((cur = tb[IFACE_ATTR_PROTO]))
259                 proto_name = blobmsg_data(cur);
260
261         proto_attach_interface(iface, proto_name);
262
263         if ((cur = tb[IFACE_ATTR_AUTO]))
264                 iface->autostart = blobmsg_get_bool(cur);
265         else
266                 iface->autostart = true;
267         iface->config_autostart = iface->autostart;
268 }
269
270 void
271 interface_add(struct interface *iface, struct blob_attr *config)
272 {
273         struct blob_attr *tb[IFACE_ATTR_MAX];
274         struct blob_attr *cur;
275         struct device *dev;
276
277         blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
278                       blob_data(config), blob_len(config));
279
280         if ((cur = tb[IFACE_ATTR_IFNAME])) {
281                 iface->ifname = blobmsg_data(cur);
282
283                 if (iface->proto_handler &&
284                     !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
285                         dev = device_get(iface->ifname, true);
286                         if (dev)
287                                 device_add_user(&iface->main_dev, dev);
288                 }
289         }
290
291         iface->config = config;
292         vlist_add(&interfaces, &iface->node);
293 }
294
295 void
296 interface_remove_link(struct interface *iface, struct device *dev)
297 {
298         struct device *mdev = iface->main_dev.dev;
299
300         if (mdev && mdev->hotplug_ops) {
301                 mdev->hotplug_ops->del(mdev, dev);
302                 return;
303         }
304
305         device_remove_user(&iface->main_dev);
306 }
307
308 int
309 interface_add_link(struct interface *iface, struct device *dev)
310 {
311         struct device *mdev = iface->main_dev.dev;
312
313         if (mdev && mdev->hotplug_ops)
314                 return mdev->hotplug_ops->add(mdev, dev);
315
316         if (iface->main_dev.dev)
317                 interface_remove_link(iface, NULL);
318
319         device_add_user(&iface->main_dev, dev);
320
321         return 0;
322 }
323
324 int
325 interface_set_up(struct interface *iface)
326 {
327         iface->autostart = true;
328
329         if (!iface->available) {
330                 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
331                 return -1;
332         }
333
334         if (iface->state != IFS_DOWN)
335                 return 0;
336
337         return __interface_set_up(iface);
338 }
339
340 int
341 interface_set_down(struct interface *iface)
342 {
343         if (!iface) {
344                 vlist_for_each_element(&interfaces, iface, node)
345                         __interface_set_down(iface, false);
346         } else {
347                 iface->autostart = false;
348                 __interface_set_down(iface, false);
349         }
350
351         return 0;
352 }
353
354 void
355 interface_start_pending(void)
356 {
357         struct interface *iface;
358
359         vlist_for_each_element(&interfaces, iface, node) {
360                 if (iface->available && iface->autostart)
361                         interface_set_up(iface);
362         }
363 }
364
365 static void
366 set_config_state(struct interface *iface, enum interface_config_state s)
367 {
368         iface->config_state = s;
369         if (iface->state == IFS_DOWN)
370                 interface_handle_config_change(iface);
371         else
372                 interface_set_down(iface);
373 }
374
375 static void
376 interface_change_config(struct interface *if_old, struct interface *if_new)
377 {
378         struct blob_attr *old_config = if_old->config;
379
380         if_old->config = if_new->config;
381         if (!if_old->config_autostart && if_new->config_autostart)
382                 if_old->autostart = true;
383
384         if_old->config_autostart = if_new->config_autostart;
385         if_old->ifname = if_new->ifname;
386
387         set_config_state(if_old, IFC_RELOAD);
388         free(old_config);
389         free(if_new);
390 }
391
392 static void
393 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
394                  struct vlist_node *node_old)
395 {
396         struct interface *if_old = container_of(node_old, struct interface, node);
397         struct interface *if_new = container_of(node_new, struct interface, node);
398
399         if (node_old && node_new)
400                 interface_change_config(if_old, if_new);
401         else if (node_old)
402                 set_config_state(if_old, IFC_REMOVE);
403         else if (node_new) {
404                 proto_init_interface(if_new, if_new->config);
405                 interface_ip_init(if_new);
406                 netifd_ubus_add_interface(if_new);
407         }
408 }
409
410
411 static void __init
412 interface_init_list(void)
413 {
414         vlist_init(&interfaces, avl_strcmp, interface_update,
415                    struct interface, node, name);
416         interfaces.keep_old = true;
417         interfaces.no_delete = true;
418 }