remove the uci dependency from protocol handlers
[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);
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);
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 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         const char *proto_name = NULL;
211
212         iface = interface_get(name);
213         if (iface)
214                 return iface;
215
216         iface = calloc(1, sizeof(*iface));
217         iface->main_dev.cb = interface_cb;
218         iface->l3_iface = &iface->main_dev;
219         strncpy(iface->name, name, sizeof(iface->name) - 1);
220         list_add_tail(&iface->list, &interfaces);
221         INIT_LIST_HEAD(&iface->errors);
222         INIT_LIST_HEAD(&iface->address);
223         INIT_LIST_HEAD(&iface->routes);
224
225         blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
226                       blob_data(attr), blob_len(attr));
227
228         if ((cur = tb[IFACE_ATTR_PROTO]))
229                 proto_name = blobmsg_data(cur);
230
231         proto_attach_interface(iface, proto_name);
232
233         if ((cur = tb[IFACE_ATTR_IFNAME])) {
234                 dev = device_get(blobmsg_data(cur), true);
235                 if (dev)
236                         device_add_user(&iface->main_dev, dev);
237         }
238
239         netifd_ubus_add_interface(iface);
240
241         return iface;
242 }
243
244 void
245 interface_free(struct interface *iface)
246 {
247         netifd_ubus_remove_interface(iface);
248         list_del(&iface->list);
249         if (iface->proto->free)
250                 iface->proto->free(iface->proto);
251         free(iface);
252 }
253
254 struct interface *
255 interface_get(const char *name)
256 {
257         struct interface *iface;
258
259         list_for_each_entry(iface, &interfaces, list) {
260                 if (!strcmp(iface->name, name))
261                         return iface;
262         }
263         return NULL;
264 }
265
266 void
267 interface_remove_link(struct interface *iface, struct device *dev)
268 {
269         struct device *mdev = iface->main_dev.dev;
270
271         if (mdev && mdev->hotplug_ops) {
272                 mdev->hotplug_ops->del(mdev, dev);
273                 return;
274         }
275
276         device_remove_user(&iface->main_dev);
277 }
278
279 int
280 interface_add_link(struct interface *iface, struct device *dev)
281 {
282         struct device *mdev = iface->main_dev.dev;
283
284         if (mdev && mdev->hotplug_ops)
285                 return mdev->hotplug_ops->add(mdev, dev);
286
287         if (iface->main_dev.dev)
288                 interface_remove_link(iface, NULL);
289
290         device_add_user(&iface->main_dev, dev);
291
292         return 0;
293 }
294
295 int
296 interface_set_up(struct interface *iface)
297 {
298         iface->autostart = true;
299
300         if (!iface->active) {
301                 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
302                 return -1;
303         }
304
305         if (iface->state != IFS_DOWN)
306                 return 0;
307
308         return __interface_set_up(iface);
309 }
310
311 int
312 interface_set_down(struct interface *iface)
313 {
314         iface->autostart = false;
315         __interface_set_down(iface, false);
316
317         return 0;
318 }
319
320 void
321 interface_start_pending(void)
322 {
323         struct interface *iface;
324
325         list_for_each_entry(iface, &interfaces, list) {
326                 if (iface->active && iface->autostart)
327                         interface_set_up(iface);
328         }
329 }