s/set_interface_up/interface_set_up/
[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_TYPE,
16         IFACE_ATTR_IFNAME,
17         IFACE_ATTR_PROTO,
18         IFACE_ATTR_AUTO,
19         IFACE_ATTR_MAX
20 };
21
22 static const union config_param_info iface_attr_info[IFACE_ATTR_MAX] = {
23         [IFACE_ATTR_IFNAME].type = BLOBMSG_TYPE_STRING,
24 };
25
26 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
27         [IFACE_ATTR_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
28         [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
29         [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_ARRAY },
30         [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL },
31 };
32
33 const struct config_param_list interface_attr_list = {
34         .n_params = IFACE_ATTR_MAX,
35         .params = iface_attrs,
36         .info = iface_attr_info,
37 };
38
39 static void
40 clear_interface_errors(struct interface *iface)
41 {
42         struct interface_error *error, *tmp;
43
44         list_for_each_entry_safe(error, tmp, &iface->errors, list) {
45                 list_del(&error->list);
46                 free(error);
47         }
48 }
49
50 void interface_add_error(struct interface *iface, const char *subsystem,
51                          const char *code, const char **data, int n_data)
52 {
53         struct interface_error *error;
54         int i, len = 0;
55         int *datalen = NULL;
56         char *dest;
57
58         if (n_data) {
59                 len = n_data * sizeof(char *);
60                 datalen = alloca(len);
61                 for (i = 0; i < n_data; i++) {
62                         datalen[i] = strlen(data[i]) + 1;
63                         len += datalen[i];
64                 }
65         }
66
67         error = calloc(1, sizeof(*error) + sizeof(char *) + len);
68         if (!error)
69                 return;
70
71         list_add_tail(&error->list, &iface->errors);
72         error->subsystem = subsystem;
73         error->code = code;
74
75         dest = (char *) &error->data[n_data + 1];
76         for (i = 0; i < n_data; i++) {
77                 error->data[i] = dest;
78                 memcpy(dest, data[i], datalen[i]);
79                 dest += datalen[i];
80         }
81         error->data[n_data] = NULL;
82 }
83
84 static void
85 interface_event(struct interface *iface, enum interface_event ev)
86 {
87         /* TODO */
88 }
89
90 static void
91 mark_interface_down(struct interface *iface)
92 {
93         interface_del_ctx_addr(iface, NULL);
94         device_release(iface->main_dev.dev);
95         iface->state = IFS_DOWN;
96 }
97
98 static int
99 __interface_set_up(struct interface *iface)
100 {
101         int ret;
102
103         if (iface->state != IFS_DOWN)
104                 return 0;
105
106         ret = device_claim(iface->main_dev.dev);
107         if (ret)
108                 return ret;
109
110         iface->state = IFS_SETUP;
111         ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
112         if (ret) {
113                 mark_interface_down(iface);
114                 return ret;
115         }
116
117         return 0;
118
119 }
120
121 static void
122 __set_interface_down(struct interface *iface, bool force)
123 {
124         clear_interface_errors(iface);
125
126         if (iface->state == IFS_DOWN ||
127                 iface->state == IFS_TEARDOWN)
128                 return;
129
130         iface->state = IFS_TEARDOWN;
131         interface_event(iface, IFEV_DOWN);
132
133         interface_del_all_routes(iface);
134         interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
135 }
136
137 static void
138 interface_cb(struct device_user *dep, enum device_event ev)
139 {
140         struct interface *iface;
141         bool new_state;
142
143         iface = container_of(dep, struct interface, main_dev);
144         switch (ev) {
145         case DEV_EVENT_ADD:
146                 new_state = true;
147                 break;
148         case DEV_EVENT_REMOVE:
149                 new_state = false;
150                 break;
151         default:
152                 return;
153         }
154
155         if (iface->active == new_state)
156                 return;
157
158         iface->active = new_state;
159
160         if (new_state) {
161                 if (iface->autostart && !config_init)
162                         interface_set_up(iface);
163         } else
164                 __set_interface_down(iface, true);
165 }
166
167 static void
168 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
169 {
170         struct interface *iface = state->iface;
171
172         switch (ev) {
173         case IFPEV_UP:
174                 if (iface->state != IFS_SETUP)
175                         return;
176
177                 iface->state = IFS_UP;
178                 interface_event(iface, IFEV_UP);
179                 break;
180         case IFPEV_DOWN:
181                 if (iface->state == IFS_DOWN)
182                         return;
183
184                 mark_interface_down(iface);
185                 break;
186         }
187 }
188
189 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
190 {
191         if (iface->proto) {
192                 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, true);
193                 iface->proto->free(iface->proto);
194                 iface->proto = NULL;
195         }
196         iface->state = IFS_DOWN;
197         iface->proto = state;
198         if (!state)
199                 return;
200
201         state->proto_event = interface_proto_cb;
202         state->iface = iface;
203 }
204
205 struct interface *
206 alloc_interface(const char *name, struct uci_section *s, struct blob_attr *attr)
207 {
208         struct interface *iface;
209         struct blob_attr *tb[IFACE_ATTR_MAX];
210         struct blob_attr *cur;
211         struct device *dev;
212
213         iface = get_interface(name);
214         if (iface)
215                 return iface;
216
217         iface = calloc(1, sizeof(*iface));
218         iface->main_dev.cb = interface_cb;
219         iface->l3_iface = &iface->main_dev;
220         strncpy(iface->name, name, sizeof(iface->name) - 1);
221         list_add(&iface->list, &interfaces);
222         INIT_LIST_HEAD(&iface->errors);
223         INIT_LIST_HEAD(&iface->address);
224         INIT_LIST_HEAD(&iface->routes);
225
226         proto_attach_interface(iface, s);
227
228         netifd_ubus_add_interface(iface);
229
230         blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
231                       blob_data(attr), blob_len(attr));
232
233         if ((cur = tb[IFACE_ATTR_TYPE])) {
234                 if (!strcmp(blobmsg_data(cur), "bridge"))
235                         interface_attach_bridge(iface, s);
236         }
237
238         if ((cur = tb[IFACE_ATTR_IFNAME])) {
239                 dev = device_get(blobmsg_data(cur), true);
240                 if (dev)
241                         device_add_user(&iface->main_dev, dev);
242         }
243
244         return iface;
245 }
246
247 void
248 free_interface(struct interface *iface)
249 {
250         netifd_ubus_remove_interface(iface);
251         list_del(&iface->list);
252         if (iface->proto->free)
253                 iface->proto->free(iface->proto);
254         free(iface);
255 }
256
257 struct interface *
258 get_interface(const char *name)
259 {
260         struct interface *iface;
261
262         list_for_each_entry(iface, &interfaces, list) {
263                 if (!strcmp(iface->name, name))
264                         return iface;
265         }
266         return NULL;
267 }
268
269 void
270 interface_remove_link(struct interface *iface, struct device *dev)
271 {
272         struct device *mdev = iface->main_dev.dev;
273
274         if (mdev && mdev->hotplug_ops) {
275                 mdev->hotplug_ops->del(mdev, dev);
276                 return;
277         }
278
279         device_remove_user(&iface->main_dev);
280 }
281
282 int
283 interface_add_link(struct interface *iface, struct device *dev)
284 {
285         struct device *mdev = iface->main_dev.dev;
286
287         if (mdev && mdev->hotplug_ops)
288                 return mdev->hotplug_ops->add(mdev, dev);
289
290         if (iface->main_dev.dev)
291                 interface_remove_link(iface, NULL);
292
293         device_add_user(&iface->main_dev, dev);
294
295         return 0;
296 }
297
298 int
299 interface_set_up(struct interface *iface)
300 {
301         iface->autostart = true;
302
303         if (!iface->active) {
304                 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
305                 return -1;
306         }
307
308         if (iface->state != IFS_DOWN)
309                 return 0;
310
311         return __interface_set_up(iface);
312 }
313
314 int
315 set_interface_down(struct interface *iface)
316 {
317         iface->autostart = false;
318         __set_interface_down(iface, false);
319
320         return 0;
321 }
322
323 void
324 start_pending_interfaces(void)
325 {
326         struct interface *iface;
327
328         list_for_each_entry(iface, &interfaces, list) {
329                 if (iface->active && iface->autostart)
330                         interface_set_up(iface);
331         }
332 }