fix duplicate device release on interface down
[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;
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)
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)
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
188         interface_set_proto_state(iface, get_default_proto());
189         if (!iface->proto) {
190                 free(iface);
191                 return NULL;
192         }
193
194         iface->main_dev.cb = interface_cb;
195         iface->l3_iface = &iface->main_dev;
196         strncpy(iface->name, name, sizeof(iface->name) - 1);
197         list_add(&iface->list, &interfaces);
198         INIT_LIST_HEAD(&iface->errors);
199
200         netifd_ubus_add_interface(iface);
201
202         return iface;
203 }
204
205 void
206 free_interface(struct interface *iface)
207 {
208         netifd_ubus_remove_interface(iface);
209         list_del(&iface->list);
210         if (iface->proto->free)
211                 iface->proto->free(iface->proto);
212         free(iface);
213 }
214
215 struct interface *
216 get_interface(const char *name)
217 {
218         struct interface *iface;
219
220         list_for_each_entry(iface, &interfaces, list) {
221                 if (!strcmp(iface->name, name))
222                         return iface;
223         }
224         return NULL;
225 }
226
227 void
228 interface_remove_link(struct interface *iface, struct device *dev)
229 {
230         struct device *mdev = iface->main_dev.dev;
231
232         if (mdev && mdev->hotplug_ops) {
233                 mdev->hotplug_ops->del(mdev, dev);
234                 return;
235         }
236
237         remove_device_user(&iface->main_dev);
238 }
239
240 int
241 interface_add_link(struct interface *iface, struct device *dev)
242 {
243         struct device *mdev = iface->main_dev.dev;
244
245         if (mdev && mdev->hotplug_ops)
246                 return mdev->hotplug_ops->add(mdev, dev);
247
248         if (iface->main_dev.dev)
249                 interface_remove_link(iface, NULL);
250
251         add_device_user(&iface->main_dev, dev);
252
253         return 0;
254 }
255
256 int
257 set_interface_up(struct interface *iface)
258 {
259         iface->autostart = true;
260
261         if (!iface->active) {
262                 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
263                 return -1;
264         }
265
266         if (iface->state != IFS_DOWN)
267                 return 0;
268
269         return __set_interface_up(iface);
270 }
271
272 int
273 set_interface_down(struct interface *iface)
274 {
275         iface->autostart = false;
276         __set_interface_down(iface, false);
277
278         return 0;
279 }