fix a compiler warning
[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         release_device(iface->main_dev.dev);
68         iface->state = IFS_DOWN;
69 }
70
71 static int
72 __set_interface_up(struct interface *iface)
73 {
74         int ret;
75
76         if (iface->state != IFS_DOWN)
77                 return 0;
78
79         ret = claim_device(iface->main_dev.dev);
80         if (ret)
81                 return ret;
82
83         iface->state = IFS_SETUP;
84         ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
85         if (ret) {
86                 mark_interface_down(iface);
87                 return ret;
88         }
89
90         return 0;
91
92 }
93
94 static void
95 __set_interface_down(struct interface *iface, bool force)
96 {
97         clear_interface_errors(iface);
98
99         if (iface->state == IFS_DOWN ||
100                 iface->state == IFS_TEARDOWN)
101                 return;
102
103         iface->state = IFS_TEARDOWN;
104         interface_event(iface, IFEV_DOWN);
105
106         interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
107 }
108
109 static void
110 interface_cb(struct device_user *dep, enum device_event ev)
111 {
112         struct interface *iface;
113         bool new_state;
114
115         iface = container_of(dep, struct interface, main_dev);
116         switch (ev) {
117         case DEV_EVENT_ADD:
118                 new_state = true;
119                 break;
120         case DEV_EVENT_REMOVE:
121                 new_state = false;
122                 break;
123         default:
124                 return;
125         }
126
127         if (iface->active == new_state)
128                 return;
129
130         iface->active = new_state;
131
132         if (new_state) {
133                 if (iface->autostart && !config_init)
134                         set_interface_up(iface);
135         } else
136                 __set_interface_down(iface, true);
137 }
138
139 static void
140 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
141 {
142         struct interface *iface = state->iface;
143
144         switch (ev) {
145         case IFPEV_UP:
146                 if (iface->state != IFS_SETUP)
147                         return;
148
149                 iface->state = IFS_UP;
150                 interface_event(iface, IFEV_UP);
151                 break;
152         case IFPEV_DOWN:
153                 if (iface->state == IFS_DOWN)
154                         return;
155
156                 mark_interface_down(iface);
157                 break;
158         }
159 }
160
161 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
162 {
163         if (iface->proto) {
164                 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, true);
165                 iface->proto->free(iface->proto);
166                 iface->proto = NULL;
167         }
168         iface->state = IFS_DOWN;
169         iface->proto = state;
170         if (!state)
171                 return;
172
173         state->proto_event = interface_proto_cb;
174         state->iface = iface;
175 }
176
177 struct interface *
178 alloc_interface(const char *name, struct uci_section *s)
179 {
180         struct interface *iface;
181
182         iface = get_interface(name);
183         if (iface)
184                 return iface;
185
186         iface = calloc(1, sizeof(*iface));
187         iface->main_dev.cb = interface_cb;
188         iface->l3_iface = &iface->main_dev;
189         strncpy(iface->name, name, sizeof(iface->name) - 1);
190         list_add(&iface->list, &interfaces);
191         INIT_LIST_HEAD(&iface->errors);
192
193         proto_attach_interface(iface, s);
194
195         netifd_ubus_add_interface(iface);
196
197         return iface;
198 }
199
200 void
201 free_interface(struct interface *iface)
202 {
203         netifd_ubus_remove_interface(iface);
204         list_del(&iface->list);
205         if (iface->proto->free)
206                 iface->proto->free(iface->proto);
207         free(iface);
208 }
209
210 struct interface *
211 get_interface(const char *name)
212 {
213         struct interface *iface;
214
215         list_for_each_entry(iface, &interfaces, list) {
216                 if (!strcmp(iface->name, name))
217                         return iface;
218         }
219         return NULL;
220 }
221
222 void
223 interface_remove_link(struct interface *iface, struct device *dev)
224 {
225         struct device *mdev = iface->main_dev.dev;
226
227         if (mdev && mdev->hotplug_ops) {
228                 mdev->hotplug_ops->del(mdev, dev);
229                 return;
230         }
231
232         remove_device_user(&iface->main_dev);
233 }
234
235 int
236 interface_add_link(struct interface *iface, struct device *dev)
237 {
238         struct device *mdev = iface->main_dev.dev;
239
240         if (mdev && mdev->hotplug_ops)
241                 return mdev->hotplug_ops->add(mdev, dev);
242
243         if (iface->main_dev.dev)
244                 interface_remove_link(iface, NULL);
245
246         add_device_user(&iface->main_dev, dev);
247
248         return 0;
249 }
250
251 int
252 set_interface_up(struct interface *iface)
253 {
254         iface->autostart = true;
255
256         if (!iface->active) {
257                 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
258                 return -1;
259         }
260
261         if (iface->state != IFS_DOWN)
262                 return 0;
263
264         return __set_interface_up(iface);
265 }
266
267 int
268 set_interface_down(struct interface *iface)
269 {
270         iface->autostart = false;
271         __set_interface_down(iface, false);
272
273         return 0;
274 }
275
276 void
277 start_pending_interfaces(void)
278 {
279         struct interface *iface;
280
281         list_for_each_entry(iface, &interfaces, list) {
282                 if (iface->active && iface->autostart)
283                         set_interface_up(iface);
284         }
285 }