always use SIGKILL for netifd_kill_process, it is usually a last resort
[project/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         vldev->dev.default_config = true;
98
99         vldev->set_state = vldev->dev.set_state;
100         vldev->dev.set_state = vlan_set_device_state;
101
102         vldev->id = id;
103
104         vldev->dep.cb = vlan_dev_cb;
105         device_add_user(&vldev->dep, dev);
106
107         return &vldev->dev;
108 }
109
110 static char *split_vlan(char *s)
111 {
112         s = strchr(s, '.');
113         if (!s)
114                 goto out;
115
116         *s = 0;
117         s++;
118
119 out:
120         return s;
121 }
122
123 struct device *get_vlan_device_chain(const char *ifname, bool create)
124 {
125         struct device *dev = NULL;
126         char *buf, *s, *next, *err = NULL;
127         int id;
128
129         buf = strdup(ifname);
130         if (!buf)
131                 return NULL;
132
133         s = split_vlan(buf);
134         dev = device_get(buf, create);
135         if (!dev && !create)
136                 goto error;
137
138         do {
139                 next = split_vlan(s);
140                 id = strtoul(s, &err, 10);
141                 if (err && *err)
142                         goto error;
143
144                 dev = get_vlan_device(dev, id, create);
145                 if (!dev)
146                         goto error;
147
148                 s = next;
149                 if (!s)
150                         goto out;
151         } while (1);
152
153 error:
154         dev = NULL;
155 out:
156         free(buf);
157         return dev;
158 }