move the DUMP_SUFFIX define to the right place
[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 static LIST_HEAD(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         device_release(&iface->main_dev);
90         iface->state = IFS_DOWN;
91 }
92
93 static int
94 __interface_set_up(struct interface *iface)
95 {
96         int ret;
97
98         if (iface->state != IFS_DOWN)
99                 return 0;
100
101         ret = device_claim(&iface->main_dev);
102         if (ret)
103                 return ret;
104
105         iface->state = IFS_SETUP;
106         ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
107         if (ret) {
108                 mark_interface_down(iface);
109                 return ret;
110         }
111
112         return 0;
113
114 }
115
116 static void
117 __interface_set_down(struct interface *iface, bool force)
118 {
119         clear_interface_errors(iface);
120
121         if (iface->state == IFS_DOWN ||
122                 iface->state == IFS_TEARDOWN)
123                 return;
124
125         iface->state = IFS_TEARDOWN;
126         interface_event(iface, IFEV_DOWN);
127         interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
128 }
129
130 static void
131 interface_cb(struct device_user *dep, enum device_event ev)
132 {
133         struct interface *iface;
134         bool new_state;
135
136         iface = container_of(dep, struct interface, main_dev);
137         switch (ev) {
138         case DEV_EVENT_ADD:
139                 new_state = true;
140                 break;
141         case DEV_EVENT_REMOVE:
142                 new_state = false;
143                 break;
144         default:
145                 return;
146         }
147
148         if (iface->available == new_state)
149                 return;
150
151         iface->available = new_state;
152
153         if (new_state) {
154                 if (iface->autostart && !config_init)
155                         interface_set_up(iface);
156         } else
157                 __interface_set_down(iface, true);
158 }
159
160 static void
161 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
162 {
163         struct interface *iface = state->iface;
164
165         switch (ev) {
166         case IFPEV_UP:
167                 if (iface->state != IFS_SETUP)
168                         return;
169
170                 iface->state = IFS_UP;
171                 interface_event(iface, IFEV_UP);
172                 break;
173         case IFPEV_DOWN:
174                 if (iface->state == IFS_DOWN)
175                         return;
176
177                 mark_interface_down(iface);
178                 break;
179         }
180 }
181
182 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
183 {
184         if (iface->proto) {
185                 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, true);
186                 iface->proto->free(iface->proto);
187                 iface->proto = NULL;
188         }
189         iface->state = IFS_DOWN;
190         iface->proto = state;
191         if (!state)
192                 return;
193
194         state->proto_event = interface_proto_cb;
195         state->iface = iface;
196 }
197
198 struct interface *
199 interface_alloc(const char *name, struct blob_attr *attr)
200 {
201         struct interface *iface;
202         struct blob_attr *tb[IFACE_ATTR_MAX];
203         struct blob_attr *cur;
204         struct device *dev;
205         const char *proto_name = NULL;
206
207         iface = interface_get(name);
208         if (iface)
209                 return iface;
210
211         iface = calloc(1, sizeof(*iface));
212         iface->main_dev.cb = interface_cb;
213         iface->l3_iface = &iface->main_dev;
214         strncpy(iface->name, name, sizeof(iface->name) - 1);
215         list_add_tail(&iface->list, &interfaces);
216         INIT_LIST_HEAD(&iface->errors);
217
218         interface_ip_init(iface);
219
220         blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
221                       blob_data(attr), blob_len(attr));
222
223         if ((cur = tb[IFACE_ATTR_PROTO]))
224                 proto_name = blobmsg_data(cur);
225
226         proto_attach_interface(iface, proto_name);
227
228         if ((cur = tb[IFACE_ATTR_IFNAME])) {
229                 dev = device_get(blobmsg_data(cur), true);
230                 if (dev)
231                         device_add_user(&iface->main_dev, dev);
232         }
233
234         if ((cur = tb[IFACE_ATTR_AUTO]))
235                 iface->autostart = blobmsg_get_bool(cur);
236         else
237                 iface->autostart = true;
238
239         netifd_ubus_add_interface(iface);
240         config_set_state(&iface->config, attr);
241
242         return iface;
243 }
244
245 void
246 interface_free(struct interface *iface)
247 {
248         netifd_ubus_remove_interface(iface);
249         list_del(&iface->list);
250         if (iface->proto->free)
251                 iface->proto->free(iface->proto);
252         free(iface);
253 }
254
255 struct interface *
256 interface_get(const char *name)
257 {
258         struct interface *iface;
259
260         list_for_each_entry(iface, &interfaces, list) {
261                 if (!strcmp(iface->name, name))
262                         return iface;
263         }
264         return NULL;
265 }
266
267 void
268 interface_remove_link(struct interface *iface, struct device *dev)
269 {
270         struct device *mdev = iface->main_dev.dev;
271
272         if (mdev && mdev->hotplug_ops) {
273                 mdev->hotplug_ops->del(mdev, dev);
274                 return;
275         }
276
277         device_remove_user(&iface->main_dev);
278 }
279
280 int
281 interface_add_link(struct interface *iface, struct device *dev)
282 {
283         struct device *mdev = iface->main_dev.dev;
284
285         if (mdev && mdev->hotplug_ops)
286                 return mdev->hotplug_ops->add(mdev, dev);
287
288         if (iface->main_dev.dev)
289                 interface_remove_link(iface, NULL);
290
291         device_add_user(&iface->main_dev, dev);
292
293         return 0;
294 }
295
296 int
297 interface_set_up(struct interface *iface)
298 {
299         iface->autostart = true;
300
301         if (!iface->available) {
302                 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
303                 return -1;
304         }
305
306         if (iface->state != IFS_DOWN)
307                 return 0;
308
309         return __interface_set_up(iface);
310 }
311
312 int
313 interface_set_down(struct interface *iface)
314 {
315         iface->autostart = false;
316         __interface_set_down(iface, false);
317
318         return 0;
319 }
320
321 void
322 interface_start_pending(void)
323 {
324         struct interface *iface;
325
326         list_for_each_entry(iface, &interfaces, list) {
327                 if (iface->available && iface->autostart)
328                         interface_set_up(iface);
329         }
330 }