proto-shell: fix parsing route netmask
[project/netifd.git] / device.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <assert.h>
5
6 #include <sys/types.h>
7 #include <sys/socket.h>
8 #include <net/ethernet.h>
9
10 #ifdef linux
11 #include <netinet/ether.h>
12 #endif
13
14 #include "netifd.h"
15 #include "system.h"
16 #include "config.h"
17
18 static struct avl_tree devices;
19
20 static const struct blobmsg_policy dev_attrs[__DEV_ATTR_MAX] = {
21         [DEV_ATTR_TYPE] = { "type", BLOBMSG_TYPE_STRING },
22         [DEV_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
23         [DEV_ATTR_IFNAME] = { "ifname", BLOBMSG_TYPE_ARRAY },
24         [DEV_ATTR_MTU] = { "mtu", BLOBMSG_TYPE_INT32 },
25         [DEV_ATTR_MACADDR] = { "macaddr", BLOBMSG_TYPE_STRING },
26         [DEV_ATTR_TXQUEUELEN] = { "txqueuelen", BLOBMSG_TYPE_INT32 },
27 };
28
29 const struct config_param_list device_attr_list = {
30         .n_params = __DEV_ATTR_MAX,
31         .params = dev_attrs,
32 };
33
34 static struct device *
35 simple_device_create(struct blob_attr *attr)
36 {
37         struct blob_attr *tb[__DEV_ATTR_MAX];
38         struct device *dev = NULL;
39         const char *name;
40
41         blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb, blob_data(attr), blob_len(attr));
42         if (!tb[DEV_ATTR_NAME])
43                 return NULL;
44
45         name = blobmsg_data(tb[DEV_ATTR_NAME]);
46         if (!name)
47                 return NULL;
48
49         dev = device_get(name, true);
50         if (!dev)
51                 return NULL;
52
53         device_init_settings(dev, tb);
54
55         return dev;
56 }
57
58 static void simple_device_free(struct device *dev)
59 {
60         device_cleanup(dev);
61         free(dev);
62 }
63
64 const struct device_type simple_device_type = {
65         .name = "Network device",
66         .config_params = &device_attr_list,
67
68         .create = simple_device_create,
69         .check_state = system_if_check,
70         .free = simple_device_free,
71 };
72
73 void
74 device_init_settings(struct device *dev, struct blob_attr **tb)
75 {
76         struct blob_attr *cur;
77         struct ether_addr *ea;
78
79         dev->flags = 0;
80
81         if ((cur = tb[DEV_ATTR_MTU])) {
82                 dev->mtu = blobmsg_get_u32(cur);
83                 dev->flags |= DEV_OPT_MTU;
84         }
85
86         if ((cur = tb[DEV_ATTR_TXQUEUELEN])) {
87                 dev->txqueuelen = blobmsg_get_u32(cur);
88                 dev->flags |= DEV_OPT_TXQUEUELEN;
89         }
90
91         if ((cur = tb[DEV_ATTR_MACADDR])) {
92                 ea = ether_aton(blob_data(cur));
93                 if (ea) {
94                         memcpy(dev->macaddr, ea, sizeof(dev->macaddr));
95                         dev->flags |= DEV_OPT_MACADDR;
96                 }
97         }
98 }
99
100 static void __init dev_init(void)
101 {
102         avl_init(&devices, avl_strcmp, true, NULL);
103 }
104
105 static void device_broadcast_event(struct device *dev, enum device_event ev)
106 {
107         struct device_user *dep, *tmp;
108
109         list_for_each_entry_safe(dep, tmp, &dev->users, list) {
110                 if (!dep->cb)
111                         continue;
112
113                 dep->cb(dep, ev);
114         }
115 }
116
117 static int set_device_state(struct device *dev, bool state)
118 {
119         if (state)
120                 system_if_up(dev);
121         else
122                 system_if_down(dev);
123
124         return 0;
125 }
126
127 int device_claim(struct device_user *dep)
128 {
129         struct device *dev = dep->dev;
130         int ret;
131
132         if (dep->claimed)
133                 return 0;
134
135         dep->claimed = true;
136         D(DEVICE, "claim device %s, new refcount: %d\n", dev->ifname, dev->active + 1);
137         if (++dev->active != 1)
138                 return 0;
139
140         device_broadcast_event(dev, DEV_EVENT_SETUP);
141         ret = dev->set_state(dev, true);
142         if (ret == 0)
143                 device_broadcast_event(dev, DEV_EVENT_UP);
144         else {
145                 D(DEVICE, "claim device %s failed: %d\n", dev->ifname, ret);
146                 dev->active = 0;
147                 dep->claimed = false;
148         }
149
150         return ret;
151 }
152
153 void device_release(struct device_user *dep)
154 {
155         struct device *dev = dep->dev;
156
157         if (!dep->claimed)
158                 return;
159
160         dep->claimed = false;
161         dev->active--;
162         D(DEVICE, "release device %s, new refcount: %d\n", dev->ifname, dev->active);
163         assert(dev->active >= 0);
164
165         if (dev->active)
166                 return;
167
168         device_broadcast_event(dev, DEV_EVENT_TEARDOWN);
169         dev->set_state(dev, false);
170         device_broadcast_event(dev, DEV_EVENT_DOWN);
171 }
172
173 int device_check_state(struct device *dev)
174 {
175         if (!dev->type->check_state)
176                 return 0;
177
178         return dev->type->check_state(dev);
179 }
180
181 void device_init_virtual(struct device *dev, const struct device_type *type, const char *name)
182 {
183         assert(dev);
184         assert(type);
185
186         if (name)
187                 strncpy(dev->ifname, name, IFNAMSIZ);
188
189         D(DEVICE, "Initialize device '%s'\n", dev->ifname);
190         INIT_LIST_HEAD(&dev->users);
191         dev->type = type;
192 }
193
194 int device_init(struct device *dev, const struct device_type *type, const char *ifname)
195 {
196         int ret;
197
198         device_init_virtual(dev, type, ifname);
199
200         if (!dev->set_state)
201                 dev->set_state = set_device_state;
202
203         dev->avl.key = dev->ifname;
204
205         ret = avl_insert(&devices, &dev->avl);
206         if (ret < 0)
207                 return ret;
208
209         system_if_clear_state(dev);
210         device_check_state(dev);
211
212         return 0;
213 }
214
215 static struct device *
216 device_create_default(const char *name)
217 {
218         struct device *dev;
219
220         D(DEVICE, "Create simple device '%s'\n", name);
221         dev = calloc(1, sizeof(*dev));
222         device_init(dev, &simple_device_type, name);
223         dev->default_config = true;
224         return dev;
225 }
226
227 struct device *
228 device_get(const char *name, bool create)
229 {
230         struct device *dev;
231
232         if (strchr(name, '.'))
233                 return get_vlan_device_chain(name, create);
234
235         dev = avl_find_element(&devices, name, dev, avl);
236         if (dev)
237                 return dev;
238
239         if (!create)
240                 return NULL;
241
242         return device_create_default(name);
243 }
244
245 static void
246 device_delete(struct device *dev)
247 {
248         if (!dev->avl.key)
249                 return;
250
251         D(DEVICE, "Delete device '%s' from list\n", dev->ifname);
252         avl_delete(&devices, &dev->avl);
253         dev->avl.key = NULL;
254 }
255
256 void device_cleanup(struct device *dev)
257 {
258         struct device_user *dep, *tmp;
259
260         D(DEVICE, "Clean up device '%s'\n", dev->ifname);
261         list_for_each_entry_safe(dep, tmp, &dev->users, list) {
262                 if (!dep->cb)
263                         continue;
264
265                 dep->cb(dep, DEV_EVENT_REMOVE);
266                 device_release(dep);
267         }
268
269         device_delete(dev);
270 }
271
272 void device_set_present(struct device *dev, bool state)
273 {
274         if (dev->present == state)
275                 return;
276
277         D(DEVICE, "Device '%s' %s present\n", dev->ifname, state ? "is now" : "is no longer" );
278         dev->present = state;
279         device_broadcast_event(dev, state ? DEV_EVENT_ADD : DEV_EVENT_REMOVE);
280 }
281
282 void device_add_user(struct device_user *dep, struct device *dev)
283 {
284         dep->dev = dev;
285         list_add_tail(&dep->list, &dev->users);
286         if (dep->cb && dev->present) {
287                 dep->cb(dep, DEV_EVENT_ADD);
288                 if (dev->active)
289                         dep->cb(dep, DEV_EVENT_UP);
290         }
291 }
292
293 static void
294 __device_free_unused(struct device *dev)
295 {
296         if (!list_empty(&dev->users) || dev->current_config || config_init)
297                 return;
298
299         device_free(dev);
300 }
301
302 void device_remove_user(struct device_user *dep)
303 {
304         struct device *dev = dep->dev;
305
306         if (dep->claimed)
307                 device_release(dep);
308
309         list_del(&dep->list);
310         dep->dev = NULL;
311         __device_free_unused(dev);
312 }
313
314 void
315 device_free_unused(struct device *dev)
316 {
317         struct device *tmp;
318
319         if (dev)
320                 return __device_free_unused(dev);
321
322         avl_for_each_element_safe(&devices, dev, avl, tmp)
323                 __device_free_unused(dev);
324 }
325
326 void
327 device_init_pending(void)
328 {
329         struct device *dev, *tmp;
330
331         avl_for_each_element_safe(&devices, dev, avl, tmp) {
332                 if (!dev->config_pending)
333                         continue;
334
335                 dev->type->config_init(dev);
336                 dev->config_pending = false;
337         }
338 }
339
340 enum dev_change_type
341 device_reload_config(struct device *dev, struct blob_attr *attr)
342 {
343         struct blob_attr *tb[__DEV_ATTR_MAX];
344         const struct config_param_list *cfg = dev->type->config_params;
345
346         if (config_check_equal(dev->config, attr, cfg))
347                 return DEV_CONFIG_NO_CHANGE;
348
349         if (cfg == &device_attr_list) {
350                 memset(tb, 0, sizeof(tb));
351
352                 if (dev->config)
353                         blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb,
354                                 blob_data(attr), blob_len(attr));
355
356                 device_init_settings(dev, tb);
357                 return DEV_CONFIG_APPLIED;
358         } else
359                 return DEV_CONFIG_RECREATE;
360 }
361
362 static enum dev_change_type
363 device_check_config(struct device *dev, const struct device_type *type,
364                     struct blob_attr *attr)
365 {
366         if (type != dev->type)
367                 return DEV_CONFIG_RECREATE;
368
369         if (dev->type->reload)
370                 return dev->type->reload(dev, attr);
371
372         return device_reload_config(dev, attr);
373 }
374
375 static void
376 device_replace(struct device *dev, struct device *odev)
377 {
378         struct device_user *dep, *tmp;
379         bool present = odev->present;
380
381         if (present)
382                 device_set_present(odev, false);
383
384         list_for_each_entry_safe(dep, tmp, &odev->users, list) {
385                 device_release(dep);
386                 list_move_tail(&dep->list, &dev->users);
387                 dep->dev = dev;
388         }
389         device_free(odev);
390
391         if (present)
392                 device_set_present(dev, true);
393 }
394
395 void
396 device_reset_config(void)
397 {
398         struct device *dev;
399
400         avl_for_each_element(&devices, dev, avl)
401                 dev->current_config = false;
402 }
403
404 void
405 device_reset_old(void)
406 {
407         struct device *dev, *tmp, *ndev;
408
409         avl_for_each_element_safe(&devices, dev, avl, tmp) {
410                 if (dev->current_config || dev->default_config)
411                         continue;
412
413                 if (dev->type != &simple_device_type)
414                         continue;
415
416                 ndev = device_create_default(dev->ifname);
417                 device_replace(ndev, dev);
418         }
419 }
420
421 struct device *
422 device_create(const char *name, const struct device_type *type,
423               struct blob_attr *config)
424 {
425         struct device *odev = NULL, *dev;
426         enum dev_change_type change;
427
428         config = config_memdup(config);
429         if (!config)
430                 return NULL;
431
432         odev = device_get(name, false);
433         if (odev) {
434                 odev->current_config = true;
435                 change = device_check_config(odev, type, config);
436                 switch (change) {
437                 case DEV_CONFIG_APPLIED:
438                         D(DEVICE, "Device '%s': config applied\n", odev->ifname);
439                         free(odev->config);
440                         odev->config = config;
441                         if (odev->present) {
442                                 device_set_present(odev, false);
443                                 device_set_present(odev, true);
444                         }
445                         return odev;
446                 case DEV_CONFIG_NO_CHANGE:
447                         D(DEVICE, "Device '%s': no configuration change\n", odev->ifname);
448                         free(config);
449                         return odev;
450                 case DEV_CONFIG_RECREATE:
451                         D(DEVICE, "Device '%s': recreate device\n", odev->ifname);
452                         device_delete(odev);
453                         break;
454                 }
455         } else
456                 D(DEVICE, "Create new device '%s' (%s)\n", name, type->name);
457
458         dev = type->create(config);
459         if (!dev)
460                 return NULL;
461
462         dev->current_config = true;
463         dev->config = config;
464         if (odev)
465                 device_replace(dev, odev);
466
467         if (!config_init && dev->config_pending)
468                 type->config_init(dev);
469
470         return dev;
471 }
472
473 void
474 device_dump_status(struct blob_buf *b, struct device *dev)
475 {
476         void *c, *s;
477
478         if (!dev) {
479                 avl_for_each_element(&devices, dev, avl) {
480                         if (!dev->present)
481                                 continue;
482                         c = blobmsg_open_table(b, dev->ifname);
483                         device_dump_status(b, dev);
484                         blobmsg_close_table(b, c);
485                 }
486
487                 return;
488         }
489
490         if (!dev->present)
491                 return;
492
493         blobmsg_add_string(b, "type", dev->type->name);
494         blobmsg_add_u8(b, "up", !!dev->active);
495         if (dev->type->dump_info)
496                 dev->type->dump_info(dev, b);
497
498         s = blobmsg_open_table(b, "statistics");
499         if (dev->type->dump_stats)
500                 dev->type->dump_stats(dev, b);
501         else
502                 system_if_dump_stats(dev, b);
503         blobmsg_close_table(b, s);
504 }