move interface address handling to the device module, clean up arguments to system_...
[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
11 static LIST_HEAD(interfaces);
12
13 static void
14 clear_interface_errors(struct interface *iface)
15 {
16 struct interface_error *error, *tmp;
17
18 list_for_each_entry_safe(error, tmp, &iface->errors, list) {
19 list_del(&error->list);
20 free(error);
21 }
22 }
23
24 void interface_add_error(struct interface *iface, const char *subsystem,
25 const char *code, const char **data, int n_data)
26 {
27 struct interface_error *error;
28 int i, len = 0;
29 int *datalen = NULL;
30 char *dest;
31
32 if (n_data) {
33 len = n_data * sizeof(char *);
34 datalen = alloca(len);
35 for (i = 0; i < n_data; i++) {
36 datalen[i] = strlen(data[i]) + 1;
37 len += datalen[i];
38 }
39 }
40
41 error = calloc(1, sizeof(*error) + sizeof(char *) + len);
42 if (!error)
43 return;
44
45 list_add_tail(&error->list, &iface->errors);
46 error->subsystem = subsystem;
47 error->code = code;
48
49 dest = (char *) &error->data[n_data + 1];
50 for (i = 0; i < n_data; i++) {
51 error->data[i] = dest;
52 memcpy(dest, data[i], datalen[i]);
53 dest += datalen[i];
54 }
55 error->data[n_data] = NULL;
56 }
57
58 static void
59 interface_event(struct interface *iface, enum interface_event ev)
60 {
61 /* TODO */
62 }
63
64 static void
65 mark_interface_down(struct interface *iface)
66 {
67 interface_del_ctx_addr(iface, NULL);
68 release_device(iface->main_dev.dev);
69 iface->state = IFS_DOWN;
70 }
71
72 static int
73 __set_interface_up(struct interface *iface)
74 {
75 int ret;
76
77 if (iface->state != IFS_DOWN)
78 return 0;
79
80 ret = claim_device(iface->main_dev.dev);
81 if (ret)
82 return ret;
83
84 iface->state = IFS_SETUP;
85 ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
86 if (ret) {
87 mark_interface_down(iface);
88 return ret;
89 }
90
91 return 0;
92
93 }
94
95 static void
96 __set_interface_down(struct interface *iface, bool force)
97 {
98 clear_interface_errors(iface);
99
100 if (iface->state == IFS_DOWN ||
101 iface->state == IFS_TEARDOWN)
102 return;
103
104 iface->state = IFS_TEARDOWN;
105 interface_event(iface, IFEV_DOWN);
106
107 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
108 }
109
110 static void
111 interface_cb(struct device_user *dep, enum device_event ev)
112 {
113 struct interface *iface;
114 bool new_state;
115
116 iface = container_of(dep, struct interface, main_dev);
117 switch (ev) {
118 case DEV_EVENT_ADD:
119 new_state = true;
120 break;
121 case DEV_EVENT_REMOVE:
122 new_state = false;
123 break;
124 default:
125 return;
126 }
127
128 if (iface->active == new_state)
129 return;
130
131 iface->active = new_state;
132
133 if (new_state) {
134 if (iface->autostart && !config_init)
135 set_interface_up(iface);
136 } else
137 __set_interface_down(iface, true);
138 }
139
140 static void
141 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
142 {
143 struct interface *iface = state->iface;
144
145 switch (ev) {
146 case IFPEV_UP:
147 if (iface->state != IFS_SETUP)
148 return;
149
150 iface->state = IFS_UP;
151 interface_event(iface, IFEV_UP);
152 break;
153 case IFPEV_DOWN:
154 if (iface->state == IFS_DOWN)
155 return;
156
157 mark_interface_down(iface);
158 break;
159 }
160 }
161
162 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
163 {
164 if (iface->proto) {
165 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, true);
166 iface->proto->free(iface->proto);
167 iface->proto = NULL;
168 }
169 iface->state = IFS_DOWN;
170 iface->proto = state;
171 if (!state)
172 return;
173
174 state->proto_event = interface_proto_cb;
175 state->iface = iface;
176 }
177
178 struct interface *
179 alloc_interface(const char *name, struct uci_section *s)
180 {
181 struct interface *iface;
182
183 iface = get_interface(name);
184 if (iface)
185 return iface;
186
187 iface = calloc(1, sizeof(*iface));
188 iface->main_dev.cb = interface_cb;
189 iface->l3_iface = &iface->main_dev;
190 strncpy(iface->name, name, sizeof(iface->name) - 1);
191 list_add(&iface->list, &interfaces);
192 INIT_LIST_HEAD(&iface->errors);
193 INIT_LIST_HEAD(&iface->address);
194 INIT_LIST_HEAD(&iface->routes);
195
196 proto_attach_interface(iface, s);
197
198 netifd_ubus_add_interface(iface);
199
200 return iface;
201 }
202
203 void
204 free_interface(struct interface *iface)
205 {
206 netifd_ubus_remove_interface(iface);
207 list_del(&iface->list);
208 if (iface->proto->free)
209 iface->proto->free(iface->proto);
210 free(iface);
211 }
212
213 struct interface *
214 get_interface(const char *name)
215 {
216 struct interface *iface;
217
218 list_for_each_entry(iface, &interfaces, list) {
219 if (!strcmp(iface->name, name))
220 return iface;
221 }
222 return NULL;
223 }
224
225 void
226 interface_remove_link(struct interface *iface, struct device *dev)
227 {
228 struct device *mdev = iface->main_dev.dev;
229
230 if (mdev && mdev->hotplug_ops) {
231 mdev->hotplug_ops->del(mdev, dev);
232 return;
233 }
234
235 remove_device_user(&iface->main_dev);
236 }
237
238 int
239 interface_add_link(struct interface *iface, struct device *dev)
240 {
241 struct device *mdev = iface->main_dev.dev;
242
243 if (mdev && mdev->hotplug_ops)
244 return mdev->hotplug_ops->add(mdev, dev);
245
246 if (iface->main_dev.dev)
247 interface_remove_link(iface, NULL);
248
249 add_device_user(&iface->main_dev, dev);
250
251 return 0;
252 }
253
254 int
255 set_interface_up(struct interface *iface)
256 {
257 iface->autostart = true;
258
259 if (!iface->active) {
260 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
261 return -1;
262 }
263
264 if (iface->state != IFS_DOWN)
265 return 0;
266
267 return __set_interface_up(iface);
268 }
269
270 int
271 set_interface_down(struct interface *iface)
272 {
273 iface->autostart = false;
274 __set_interface_down(iface, false);
275
276 return 0;
277 }
278
279 void
280 start_pending_interfaces(void)
281 {
282 struct interface *iface;
283
284 list_for_each_entry(iface, &interfaces, list) {
285 if (iface->active && iface->autostart)
286 set_interface_up(iface);
287 }
288 }