make interface:ifname a string instead of an array
[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 "proto.h"
9 #include "ubus.h"
10 #include "config.h"
11
12 static LIST_HEAD(interfaces);
13
14 enum {
15         IFACE_ATTR_IFNAME,
16         IFACE_ATTR_PROTO,
17         IFACE_ATTR_AUTO,
18         IFACE_ATTR_MAX
19 };
20
21 static const union config_param_info iface_attr_info[IFACE_ATTR_MAX] = {
22         [IFACE_ATTR_IFNAME].type = BLOBMSG_TYPE_STRING,
23 };
24
25 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
26         [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
27         [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
28         [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL },
29 };
30
31 const struct config_param_list interface_attr_list = {
32         .n_params = IFACE_ATTR_MAX,
33         .params = iface_attrs,
34         .info = iface_attr_info,
35 };
36
37 static void
38 clear_interface_errors(struct interface *iface)
39 {
40         struct interface_error *error, *tmp;
41
42         list_for_each_entry_safe(error, tmp, &iface->errors, list) {
43                 list_del(&error->list);
44                 free(error);
45         }
46 }
47
48 void interface_add_error(struct interface *iface, const char *subsystem,
49                          const char *code, const char **data, int n_data)
50 {
51         struct interface_error *error;
52         int i, len = 0;
53         int *datalen = NULL;
54         char *dest;
55
56         if (n_data) {
57                 len = n_data * sizeof(char *);
58                 datalen = alloca(len);
59                 for (i = 0; i < n_data; i++) {
60                         datalen[i] = strlen(data[i]) + 1;
61                         len += datalen[i];
62                 }
63         }
64
65         error = calloc(1, sizeof(*error) + sizeof(char *) + len);
66         if (!error)
67                 return;
68
69         list_add_tail(&error->list, &iface->errors);
70         error->subsystem = subsystem;
71         error->code = code;
72
73         dest = (char *) &error->data[n_data + 1];
74         for (i = 0; i < n_data; i++) {
75                 error->data[i] = dest;
76                 memcpy(dest, data[i], datalen[i]);
77                 dest += datalen[i];
78         }
79         error->data[n_data] = NULL;
80 }
81
82 static void
83 interface_event(struct interface *iface, enum interface_event ev)
84 {
85         /* TODO */
86 }
87
88 static void
89 mark_interface_down(struct interface *iface)
90 {
91         interface_del_ctx_addr(iface, NULL);
92         device_release(iface->main_dev.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         ret = device_claim(iface->main_dev.dev);
105         if (ret)
106                 return ret;
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
131         interface_del_all_routes(iface);
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         if (iface->active == new_state)
154                 return;
155
156         iface->active = new_state;
157
158         if (new_state) {
159                 if (iface->autostart && !config_init)
160                         interface_set_up(iface);
161         } else
162                 __interface_set_down(iface, true);
163 }
164
165 static void
166 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
167 {
168         struct interface *iface = state->iface;
169
170         switch (ev) {
171         case IFPEV_UP:
172                 if (iface->state != IFS_SETUP)
173                         return;
174
175                 iface->state = IFS_UP;
176                 interface_event(iface, IFEV_UP);
177                 break;
178         case IFPEV_DOWN:
179                 if (iface->state == IFS_DOWN)
180                         return;
181
182                 mark_interface_down(iface);
183                 break;
184         }
185 }
186
187 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
188 {
189         if (iface->proto) {
190                 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, true);
191                 iface->proto->free(iface->proto);
192                 iface->proto = NULL;
193         }
194         iface->state = IFS_DOWN;
195         iface->proto = state;
196         if (!state)
197                 return;
198
199         state->proto_event = interface_proto_cb;
200         state->iface = iface;
201 }
202
203 struct interface *
204 interface_alloc(const char *name, struct uci_section *s, struct blob_attr *attr)
205 {
206         struct interface *iface;
207         struct blob_attr *tb[IFACE_ATTR_MAX];
208         struct blob_attr *cur;
209         struct device *dev;
210
211         iface = interface_get(name);
212         if (iface)
213                 return iface;
214
215         iface = calloc(1, sizeof(*iface));
216         iface->main_dev.cb = interface_cb;
217         iface->l3_iface = &iface->main_dev;
218         strncpy(iface->name, name, sizeof(iface->name) - 1);
219         list_add(&iface->list, &interfaces);
220         INIT_LIST_HEAD(&iface->errors);
221         INIT_LIST_HEAD(&iface->address);
222         INIT_LIST_HEAD(&iface->routes);
223
224         proto_attach_interface(iface, s);
225
226         netifd_ubus_add_interface(iface);
227
228         blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
229                       blob_data(attr), blob_len(attr));
230
231         if ((cur = tb[IFACE_ATTR_IFNAME])) {
232                 dev = device_get(blobmsg_data(cur), true);
233                 if (dev)
234                         device_add_user(&iface->main_dev, dev);
235         }
236
237         return iface;
238 }
239
240 void
241 interface_free(struct interface *iface)
242 {
243         netifd_ubus_remove_interface(iface);
244         list_del(&iface->list);
245         if (iface->proto->free)
246                 iface->proto->free(iface->proto);
247         free(iface);
248 }
249
250 struct interface *
251 interface_get(const char *name)
252 {
253         struct interface *iface;
254
255         list_for_each_entry(iface, &interfaces, list) {
256                 if (!strcmp(iface->name, name))
257                         return iface;
258         }
259         return NULL;
260 }
261
262 void
263 interface_remove_link(struct interface *iface, struct device *dev)
264 {
265         struct device *mdev = iface->main_dev.dev;
266
267         if (mdev && mdev->hotplug_ops) {
268                 mdev->hotplug_ops->del(mdev, dev);
269                 return;
270         }
271
272         device_remove_user(&iface->main_dev);
273 }
274
275 int
276 interface_add_link(struct interface *iface, struct device *dev)
277 {
278         struct device *mdev = iface->main_dev.dev;
279
280         if (mdev && mdev->hotplug_ops)
281                 return mdev->hotplug_ops->add(mdev, dev);
282
283         if (iface->main_dev.dev)
284                 interface_remove_link(iface, NULL);
285
286         device_add_user(&iface->main_dev, dev);
287
288         return 0;
289 }
290
291 int
292 interface_set_up(struct interface *iface)
293 {
294         iface->autostart = true;
295
296         if (!iface->active) {
297                 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
298                 return -1;
299         }
300
301         if (iface->state != IFS_DOWN)
302                 return 0;
303
304         return __interface_set_up(iface);
305 }
306
307 int
308 interface_set_down(struct interface *iface)
309 {
310         iface->autostart = false;
311         __interface_set_down(iface, false);
312
313         return 0;
314 }
315
316 void
317 interface_start_pending(void)
318 {
319         struct interface *iface;
320
321         list_for_each_entry(iface, &interfaces, list) {
322                 if (iface->active && iface->autostart)
323                         interface_set_up(iface);
324         }
325 }