• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/netifd/vlandev.c

  1 /*
  2  * netifd - network interface daemon
  3  * Copyright (C) 2014 Gioacchino Mazzurco <gio@eigenlab.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 
 15 #include <string.h>
 16 
 17 #include "netifd.h"
 18 #include "device.h"
 19 #include "interface.h"
 20 #include "system.h"
 21 
 22 enum {
 23         VLANDEV_ATTR_IFNAME,
 24         VLANDEV_ATTR_VID,
 25         __VLANDEV_ATTR_MAX
 26 };
 27 
 28 static const struct blobmsg_policy vlandev_attrs[__VLANDEV_ATTR_MAX] = {
 29         [VLANDEV_ATTR_IFNAME] = { "ifname", BLOBMSG_TYPE_STRING },
 30         [VLANDEV_ATTR_VID] = { "vid", BLOBMSG_TYPE_INT32 },
 31 };
 32 
 33 static const struct uci_blob_param_list vlandev_attr_list = {
 34         .n_params = __VLANDEV_ATTR_MAX,
 35         .params = vlandev_attrs,
 36 
 37         .n_next = 1,
 38         .next = { &device_attr_list },
 39 };
 40 
 41 static struct device_type vlan8021q_device_type;
 42 
 43 struct vlandev_device {
 44         struct device dev;
 45         struct device_user parent;
 46 
 47         device_state_cb set_state;
 48 
 49         struct blob_attr *config_data;
 50         struct blob_attr *ifname;
 51         struct vlandev_config config;
 52 };
 53 
 54 static void
 55 vlandev_base_cb(struct device_user *dev, enum device_event ev)
 56 {
 57         struct vlandev_device *mvdev = container_of(dev, struct vlandev_device, parent);
 58 
 59         switch (ev) {
 60         case DEV_EVENT_ADD:
 61                 device_set_present(&mvdev->dev, true);
 62                 break;
 63         case DEV_EVENT_REMOVE:
 64                 device_set_present(&mvdev->dev, false);
 65                 break;
 66         default:
 67                 return;
 68         }
 69 }
 70 
 71 static int
 72 vlandev_set_down(struct vlandev_device *mvdev)
 73 {
 74         mvdev->set_state(&mvdev->dev, false);
 75         system_vlandev_del(&mvdev->dev);
 76         device_release(&mvdev->parent);
 77 
 78         return 0;
 79 }
 80 
 81 static int
 82 vlandev_set_up(struct vlandev_device *mvdev)
 83 {
 84         int ret;
 85 
 86         ret = device_claim(&mvdev->parent);
 87         if (ret < 0)
 88                 return ret;
 89 
 90         ret = system_vlandev_add(&mvdev->dev, mvdev->parent.dev, &mvdev->config);
 91         if (ret < 0)
 92                 goto release;
 93 
 94         ret = mvdev->set_state(&mvdev->dev, true);
 95         if (ret)
 96                 goto delete;
 97 
 98         return 0;
 99 
100 delete:
101         system_vlandev_del(&mvdev->dev);
102 release:
103         device_release(&mvdev->parent);
104         return ret;
105 }
106 
107 static int
108 vlandev_set_state(struct device *dev, bool up)
109 {
110         struct vlandev_device *mvdev;
111 
112         D(SYSTEM, "vlandev_set_state(%s, %u)\n", dev->ifname, up);
113 
114         mvdev = container_of(dev, struct vlandev_device, dev);
115         if (up)
116                 return vlandev_set_up(mvdev);
117         else
118                 return vlandev_set_down(mvdev);
119 }
120 
121 static void
122 vlandev_free(struct device *dev)
123 {
124         struct vlandev_device *mvdev;
125 
126         mvdev = container_of(dev, struct vlandev_device, dev);
127         device_remove_user(&mvdev->parent);
128         free(mvdev->config_data);
129         free(mvdev);
130 }
131 
132 static void
133 vlandev_dump_info(struct device *dev, struct blob_buf *b)
134 {
135         struct vlandev_device *mvdev;
136 
137         mvdev = container_of(dev, struct vlandev_device, dev);
138         blobmsg_add_string(b, "parent", mvdev->parent.dev->ifname);
139         system_if_dump_info(dev, b);
140 }
141 
142 static void
143 vlandev_config_init(struct device *dev)
144 {
145         struct vlandev_device *mvdev;
146         struct device *basedev = NULL;
147 
148         mvdev = container_of(dev, struct vlandev_device, dev);
149         if (mvdev->ifname)
150                 basedev = device_get(blobmsg_data(mvdev->ifname), true);
151 
152         device_add_user(&mvdev->parent, basedev);
153 }
154 
155 static void
156 vlandev_apply_settings(struct vlandev_device *mvdev, struct blob_attr **tb)
157 {
158         struct vlandev_config *cfg = &mvdev->config;
159         struct blob_attr *cur;
160 
161         cfg->proto = (mvdev->dev.type == &vlan8021q_device_type) ?
162                 VLAN_PROTO_8021Q : VLAN_PROTO_8021AD;
163         cfg->vid = 1;
164 
165         if ((cur = tb[VLANDEV_ATTR_VID]))
166                 cfg->vid = (uint16_t) blobmsg_get_u32(cur);
167 }
168 
169 static enum dev_change_type
170 vlandev_reload(struct device *dev, struct blob_attr *attr)
171 {
172         struct blob_attr *tb_dev[__DEV_ATTR_MAX];
173         struct blob_attr *tb_mv[__VLANDEV_ATTR_MAX];
174         enum dev_change_type ret = DEV_CONFIG_APPLIED;
175         struct vlandev_device *mvdev;
176 
177         mvdev = container_of(dev, struct vlandev_device, dev);
178         attr = blob_memdup(attr);
179 
180         blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, tb_dev,
181                 blob_data(attr), blob_len(attr));
182         blobmsg_parse(vlandev_attrs, __VLANDEV_ATTR_MAX, tb_mv,
183                 blob_data(attr), blob_len(attr));
184 
185         device_init_settings(dev, tb_dev);
186         vlandev_apply_settings(mvdev, tb_mv);
187         mvdev->ifname = tb_mv[VLANDEV_ATTR_IFNAME];
188 
189         if (mvdev->config_data) {
190                 struct blob_attr *otb_dev[__DEV_ATTR_MAX];
191                 struct blob_attr *otb_mv[__VLANDEV_ATTR_MAX];
192 
193                 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb_dev,
194                         blob_data(mvdev->config_data), blob_len(mvdev->config_data));
195 
196                 if (uci_blob_diff(tb_dev, otb_dev, &device_attr_list, NULL))
197                     ret = DEV_CONFIG_RESTART;
198 
199                 blobmsg_parse(vlandev_attrs, __VLANDEV_ATTR_MAX, otb_mv,
200                         blob_data(mvdev->config_data), blob_len(mvdev->config_data));
201 
202                 if (uci_blob_diff(tb_mv, otb_mv, &vlandev_attr_list, NULL))
203                     ret = DEV_CONFIG_RESTART;
204 
205                 vlandev_config_init(dev);
206         }
207 
208         free(mvdev->config_data);
209         mvdev->config_data = attr;
210         return ret;
211 }
212 
213 static struct device *
214 vlandev_create(const char *name, struct device_type *devtype,
215                struct blob_attr *attr)
216 {
217         struct vlandev_device *mvdev;
218         struct device *dev = NULL;
219 
220         mvdev = calloc(1, sizeof(*mvdev));
221         if (!mvdev)
222                 return NULL;
223 
224         dev = &mvdev->dev;
225 
226         if (device_init(dev, devtype, name) < 0) {
227                 device_cleanup(dev);
228                 free(mvdev);
229                 return NULL;
230         }
231 
232         dev->config_pending = true;
233 
234         mvdev->set_state = dev->set_state;
235         dev->set_state = vlandev_set_state;
236 
237         dev->hotplug_ops = NULL;
238         mvdev->parent.cb = vlandev_base_cb;
239 
240         vlandev_reload(dev, attr);
241 
242         return dev;
243 }
244 
245 static struct device_type vlan8021ad_device_type = {
246         .name = "8021ad",
247         .config_params = &vlandev_attr_list,
248         .create = vlandev_create,
249         .config_init = vlandev_config_init,
250         .reload = vlandev_reload,
251         .free = vlandev_free,
252         .dump_info = vlandev_dump_info,
253 };
254 
255 static struct device_type vlan8021q_device_type = {
256         .name = "8021q",
257         .config_params = &vlandev_attr_list,
258         .create = vlandev_create,
259         .config_init = vlandev_config_init,
260         .reload = vlandev_reload,
261         .free = vlandev_free,
262         .dump_info = vlandev_dump_info,
263 };
264 
265 static void __init vlandev_device_type_init(void)
266 {
267         device_type_add(&vlan8021ad_device_type);
268         device_type_add(&vlan8021q_device_type);
269 }
270 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt