add functions for adding dns servers to the proto list, hook them up in proto-static.c
[oweals/netifd.git] / vlan.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4
5 #include "netifd.h"
6 #include "system.h"
7
8 struct vlan_device {
9         struct device dev;
10         struct device_user dep;
11
12         device_state_cb set_state;
13         int id;
14 };
15
16 static void free_vlan_if(struct device *iface)
17 {
18         struct vlan_device *vldev;
19
20         vldev = container_of(iface, struct vlan_device, dev);
21         device_remove_user(&vldev->dep);
22         device_cleanup(&vldev->dev);
23         free(vldev);
24 }
25
26 static int vlan_set_device_state(struct device *dev, bool up)
27 {
28         struct vlan_device *vldev;
29         int ret = 0;
30
31         vldev = container_of(dev, struct vlan_device, dev);
32         if (!up) {
33                 vldev->set_state(dev, false);
34                 system_vlan_del(dev);
35                 device_release(&vldev->dep);
36                 return 0;
37         }
38
39         ret = device_claim(&vldev->dep);
40         if (ret)
41                 return ret;
42
43         system_vlan_add(vldev->dep.dev, vldev->id);
44         ret = vldev->set_state(dev, true);
45         if (ret)
46                 device_release(&vldev->dep);
47
48         return ret;
49 }
50
51 static void vlan_dev_cb(struct device_user *dep, enum device_event ev)
52 {
53         struct vlan_device *vldev;
54
55         vldev = container_of(dep, struct vlan_device, dep);
56         switch(ev) {
57         case DEV_EVENT_ADD:
58                 device_set_present(&vldev->dev, true);
59                 break;
60         case DEV_EVENT_REMOVE:
61                 device_set_present(&vldev->dev, false);
62                 break;
63         default:
64                 break;
65         }
66 }
67
68 static struct device *get_vlan_device(struct device *dev, int id, bool create)
69 {
70         static const struct device_type vlan_type = {
71                 .name = "VLAN",
72                 .config_params = &device_attr_list,
73                 .free = free_vlan_if,
74         };
75         struct vlan_device *vldev;
76         struct device_user *dep;
77
78         /* look for an existing interface before creating a new one */
79         list_for_each_entry(dep, &dev->users, list) {
80                 if (dep->cb != vlan_dev_cb)
81                         continue;
82
83                 vldev = container_of(dep, struct vlan_device, dep);
84                 if (vldev->id != id)
85                         continue;
86
87                 return &vldev->dev;
88         }
89
90         if (!create)
91                 return NULL;
92
93         vldev = calloc(1, sizeof(*vldev));
94         snprintf(vldev->dev.ifname, IFNAMSIZ, "%s.%d", dev->ifname, id);
95
96         device_init(&vldev->dev, &vlan_type, NULL);
97
98         vldev->set_state = vldev->dev.set_state;
99         vldev->dev.set_state = vlan_set_device_state;
100
101         vldev->id = id;
102
103         vldev->dep.cb = vlan_dev_cb;
104         device_add_user(&vldev->dep, dev);
105
106         return &vldev->dev;
107 }
108
109 static char *split_vlan(char *s)
110 {
111         s = strchr(s, '.');
112         if (!s)
113                 goto out;
114
115         *s = 0;
116         s++;
117
118 out:
119         return s;
120 }
121
122 struct device *get_vlan_device_chain(const char *ifname, bool create)
123 {
124         struct device *dev = NULL;
125         char *buf, *s, *next, *err = NULL;
126         int id;
127
128         buf = strdup(ifname);
129         if (!buf)
130                 return NULL;
131
132         s = split_vlan(buf);
133         dev = device_get(buf, create);
134         if (!dev && !create)
135                 goto error;
136
137         do {
138                 next = split_vlan(s);
139                 id = strtoul(s, &err, 10);
140                 if (err && *err)
141                         goto error;
142
143                 dev = get_vlan_device(dev, id, create);
144                 if (!dev)
145                         goto error;
146
147                 s = next;
148                 if (!s)
149                         goto out;
150         } while (1);
151
152 error:
153         dev = NULL;
154 out:
155         free(buf);
156         return dev;
157 }