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