apply basic device settings for hotplug devices, e.g. mtu and txqueuelen
[project/netifd.git] / tunnel.c
1 /*
2  * netifd - network interface daemon
3  * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2
7  * as published by the Free Software Foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14 #include "netifd.h"
15 #include "device.h"
16 #include "config.h"
17 #include "system.h"
18
19 struct tunnel {
20         struct device dev;
21         device_state_cb set_state;
22 };
23
24 static int
25 tunnel_set_state(struct device *dev, bool up)
26 {
27         struct tunnel *tun = container_of(dev, struct tunnel, dev);
28         int ret;
29
30         if (up) {
31                 ret = system_add_ip_tunnel(dev->ifname, dev->config);
32                 if (ret != 0)
33                         return ret;
34         }
35
36         ret = tun->set_state(dev, up);
37         if (ret || !up)
38                 system_del_ip_tunnel(dev->ifname);
39
40         return ret;
41 }
42
43 static struct device *
44 tunnel_create(const char *name, struct blob_attr *attr)
45 {
46         struct tunnel *tun;
47         struct device *dev;
48
49         tun = calloc(1, sizeof(*tun));
50         dev = &tun->dev;
51         device_init(dev, &tunnel_device_type, name);
52         tun->set_state = dev->set_state;
53         dev->set_state = tunnel_set_state;
54         device_set_present(dev, true);
55
56         return dev;
57 }
58
59 static void
60 tunnel_free(struct device *dev)
61 {
62         struct tunnel *tun = container_of(dev, struct tunnel, dev);
63
64         free(tun);
65 }
66
67 const struct device_type tunnel_device_type = {
68         .name = "IP tunnel",
69         .config_params = &tunnel_attr_list,
70
71         .create = tunnel_create,
72         .free = tunnel_free,
73 };
74
75