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 #include <inttypes.h> 17 18 #include "netifd.h" 19 #include "device.h" 20 #include "interface.h" 21 #include "system.h" 22 #include "utils.h" 23 24 enum { 25 VLANDEV_ATTR_IFNAME, 26 VLANDEV_ATTR_VID, 27 VLANDEV_ATTR_INGRESS_QOS_MAPPING, 28 VLANDEV_ATTR_EGRESS_QOS_MAPPING, 29 __VLANDEV_ATTR_MAX 30 }; 31 32 static const struct blobmsg_policy vlandev_attrs[__VLANDEV_ATTR_MAX] = { 33 [VLANDEV_ATTR_IFNAME] = { "ifname", BLOBMSG_TYPE_STRING }, 34 [VLANDEV_ATTR_VID] = { "vid", BLOBMSG_TYPE_STRING }, 35 [VLANDEV_ATTR_INGRESS_QOS_MAPPING] = { "ingress_qos_mapping", BLOBMSG_TYPE_ARRAY }, 36 [VLANDEV_ATTR_EGRESS_QOS_MAPPING] = { "egress_qos_mapping", BLOBMSG_TYPE_ARRAY }, 37 }; 38 39 static const struct uci_blob_param_list vlandev_attr_list = { 40 .n_params = __VLANDEV_ATTR_MAX, 41 .params = vlandev_attrs, 42 43 .n_next = 1, 44 .next = { &device_attr_list }, 45 }; 46 47 static struct device_type vlan8021q_device_type; 48 static struct blob_buf b; 49 50 struct vlandev_device { 51 struct device dev; 52 struct device_user parent; 53 54 device_state_cb set_state; 55 56 struct blob_attr *config_data; 57 struct blob_attr *ifname; 58 struct blob_attr *vid; 59 60 struct vlandev_config config; 61 }; 62 63 static int 64 __vlandev_hotplug_op(struct device *dev, struct device *member, struct blob_attr *vlan, bool add) 65 { 66 struct vlandev_device *mvdev = container_of(dev, struct vlandev_device, dev); 67 void *a; 68 69 dev = mvdev->parent.dev; 70 if (!dev || !dev->hotplug_ops) 71 return UBUS_STATUS_NOT_SUPPORTED; 72 73 blob_buf_init(&b, 0); 74 a = blobmsg_open_array(&b, "vlans"); 75 blobmsg_printf(&b, NULL, "%d", mvdev->config.vid); 76 blobmsg_close_array(&b, a); 77 78 if (add) 79 return dev->hotplug_ops->add(dev, member, blobmsg_data(b.head)); 80 else 81 return dev->hotplug_ops->del(dev, member, blobmsg_data(b.head)); 82 } 83 84 static int 85 vlandev_hotplug_add(struct device *dev, struct device *member, struct blob_attr *vlan) 86 { 87 return __vlandev_hotplug_op(dev, member, vlan, true); 88 } 89 90 static int 91 vlandev_hotplug_del(struct device *dev, struct device *member, struct blob_attr *vlan) 92 { 93 return __vlandev_hotplug_op(dev, member, vlan, false); 94 } 95 96 static int 97 vlandev_hotplug_prepare(struct device *dev, struct device **bridge_dev) 98 { 99 struct vlandev_device *mvdev = container_of(dev, struct vlandev_device, dev); 100 101 dev = mvdev->parent.dev; 102 if (!dev || !dev->hotplug_ops) 103 return UBUS_STATUS_NOT_SUPPORTED; 104 105 return dev->hotplug_ops->prepare(dev, bridge_dev); 106 } 107 108 static void vlandev_hotplug_check(struct vlandev_device *mvdev) 109 { 110 static const struct device_hotplug_ops hotplug_ops = { 111 .prepare = vlandev_hotplug_prepare, 112 .add = vlandev_hotplug_add, 113 .del = vlandev_hotplug_del 114 }; 115 struct device *dev = mvdev->parent.dev; 116 117 if (!dev || !dev->hotplug_ops || avl_is_empty(&dev->vlans.avl) || 118 mvdev->dev.type != &vlan8021q_device_type) { 119 mvdev->dev.hotplug_ops = NULL; 120 return; 121 } 122 123 mvdev->dev.hotplug_ops = &hotplug_ops; 124 } 125 126 127 static void 128 vlandev_base_cb(struct device_user *dev, enum device_event ev) 129 { 130 struct vlandev_device *mvdev = container_of(dev, struct vlandev_device, parent); 131 132 switch (ev) { 133 case DEV_EVENT_ADD: 134 device_set_present(&mvdev->dev, true); 135 break; 136 case DEV_EVENT_REMOVE: 137 device_set_present(&mvdev->dev, false); 138 break; 139 case DEV_EVENT_UPDATE_IFNAME: 140 vlandev_hotplug_check(mvdev); 141 break; 142 case DEV_EVENT_TOPO_CHANGE: 143 /* Propagate topo changes */ 144 device_broadcast_event(&mvdev->dev, DEV_EVENT_TOPO_CHANGE); 145 break; 146 default: 147 return; 148 } 149 } 150 151 static int 152 vlandev_set_down(struct vlandev_device *mvdev) 153 { 154 mvdev->set_state(&mvdev->dev, false); 155 system_vlandev_del(&mvdev->dev); 156 device_release(&mvdev->parent); 157 158 return 0; 159 } 160 161 static int 162 vlandev_set_up(struct vlandev_device *mvdev) 163 { 164 int ret; 165 166 ret = device_claim(&mvdev->parent); 167 if (ret < 0) 168 return ret; 169 170 ret = system_vlandev_add(&mvdev->dev, mvdev->parent.dev, &mvdev->config); 171 if (ret < 0) 172 goto release; 173 174 ret = mvdev->set_state(&mvdev->dev, true); 175 if (ret) 176 goto delete; 177 178 return 0; 179 180 delete: 181 system_vlandev_del(&mvdev->dev); 182 release: 183 device_release(&mvdev->parent); 184 return ret; 185 } 186 187 static int 188 vlandev_set_state(struct device *dev, bool up) 189 { 190 struct vlandev_device *mvdev; 191 192 D(SYSTEM, "vlandev_set_state(%s, %u)\n", dev->ifname, up); 193 194 mvdev = container_of(dev, struct vlandev_device, dev); 195 if (up) 196 return vlandev_set_up(mvdev); 197 else 198 return vlandev_set_down(mvdev); 199 } 200 201 static void 202 vlandev_free(struct device *dev) 203 { 204 struct vlandev_device *mvdev; 205 206 mvdev = container_of(dev, struct vlandev_device, dev); 207 device_remove_user(&mvdev->parent); 208 free(mvdev->config_data); 209 vlist_simple_flush_all(&mvdev->config.ingress_qos_mapping_list); 210 vlist_simple_flush_all(&mvdev->config.egress_qos_mapping_list); 211 free(mvdev); 212 } 213 214 static void vlandev_qos_mapping_dump(struct blob_buf *b, const char *name, const struct vlist_simple_tree *qos_mapping_li) 215 { 216 const struct vlan_qos_mapping *elem; 217 void *a, *t; 218 219 a = blobmsg_open_array(b, name); 220 221 vlist_simple_for_each_element(qos_mapping_li, elem, node) { 222 t = blobmsg_open_table(b, NULL); 223 224 blobmsg_add_u32(b, "from", elem->from); 225 blobmsg_add_u32(b, "to", elem->to); 226 227 blobmsg_close_table(b, t); 228 } 229 230 blobmsg_close_array(b, a); 231 } 232 233 static void 234 vlandev_dump_info(struct device *dev, struct blob_buf *b) 235 { 236 struct vlandev_device *mvdev; 237 238 mvdev = container_of(dev, struct vlandev_device, dev); 239 blobmsg_add_string(b, "parent", mvdev->parent.dev->ifname); 240 system_if_dump_info(dev, b); 241 blobmsg_add_u32(b, "vid", mvdev->config.vid); 242 vlandev_qos_mapping_dump(b, "ingress_qos_mapping", &mvdev->config.ingress_qos_mapping_list); 243 vlandev_qos_mapping_dump(b, "egress_qos_mapping", &mvdev->config.egress_qos_mapping_list); 244 } 245 246 static uint16_t 247 vlandev_get_vid(struct device *dev, const char *id_str) 248 { 249 unsigned long id; 250 uint16_t *alias_id; 251 char *err; 252 253 id = strtoul(id_str, &err, 10); 254 if (err && *err) { 255 if (!dev) 256 return 1; 257 258 alias_id = kvlist_get(&dev->vlan_aliases, id_str); 259 if (!alias_id) 260 return 1; 261 262 id = *alias_id; 263 } 264 265 return (uint16_t)id; 266 } 267 268 static void 269 vlandev_config_init(struct device *dev) 270 { 271 struct vlandev_device *mvdev; 272 struct device *basedev = NULL; 273 274 mvdev = container_of(dev, struct vlandev_device, dev); 275 if (mvdev->ifname) 276 basedev = device_get(blobmsg_data(mvdev->ifname), true); 277 278 if (mvdev->vid) 279 mvdev->config.vid = vlandev_get_vid(basedev, blobmsg_get_string(mvdev->vid)); 280 else 281 mvdev->config.vid = 1; 282 283 device_add_user(&mvdev->parent, basedev); 284 vlandev_hotplug_check(mvdev); 285 } 286 287 static void vlandev_qos_mapping_list_apply(struct vlist_simple_tree *qos_mapping_li, struct blob_attr *list) 288 { 289 struct blob_attr *cur; 290 struct vlan_qos_mapping *qos_mapping; 291 int rem, rc; 292 293 blobmsg_for_each_attr(cur, list, rem) { 294 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING) 295 continue; 296 297 if (!blobmsg_check_attr(cur, false)) 298 continue; 299 300 qos_mapping = calloc(1, sizeof(*qos_mapping)); 301 if (!qos_mapping) 302 continue; 303 304 rc = sscanf(blobmsg_data(cur), "%" PRIu32 ":%" PRIu32, &qos_mapping->from, &qos_mapping->to); 305 if (rc != 2) { 306 free(qos_mapping); 307 continue; 308 } 309 vlist_simple_add(qos_mapping_li, &qos_mapping->node); 310 } 311 } 312 313 static void 314 vlandev_apply_settings(struct vlandev_device *mvdev, struct blob_attr **tb) 315 { 316 struct vlandev_config *cfg = &mvdev->config; 317 struct blob_attr *cur; 318 319 cfg->proto = (mvdev->dev.type == &vlan8021q_device_type) ? 320 VLAN_PROTO_8021Q : VLAN_PROTO_8021AD; 321 322 vlist_simple_update(&cfg->ingress_qos_mapping_list); 323 vlist_simple_update(&cfg->egress_qos_mapping_list); 324 325 if ((cur = tb[VLANDEV_ATTR_INGRESS_QOS_MAPPING])) 326 vlandev_qos_mapping_list_apply(&cfg->ingress_qos_mapping_list, cur); 327 328 if ((cur = tb[VLANDEV_ATTR_EGRESS_QOS_MAPPING])) 329 vlandev_qos_mapping_list_apply(&cfg->egress_qos_mapping_list, cur); 330 331 vlist_simple_flush(&cfg->ingress_qos_mapping_list); 332 vlist_simple_flush(&cfg->egress_qos_mapping_list); 333 } 334 335 static enum dev_change_type 336 vlandev_reload(struct device *dev, struct blob_attr *attr) 337 { 338 struct blob_attr *tb_dev[__DEV_ATTR_MAX]; 339 struct blob_attr *tb_mv[__VLANDEV_ATTR_MAX]; 340 enum dev_change_type ret = DEV_CONFIG_APPLIED; 341 struct vlandev_device *mvdev; 342 343 mvdev = container_of(dev, struct vlandev_device, dev); 344 attr = blob_memdup(attr); 345 346 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, tb_dev, 347 blob_data(attr), blob_len(attr)); 348 blobmsg_parse(vlandev_attrs, __VLANDEV_ATTR_MAX, tb_mv, 349 blob_data(attr), blob_len(attr)); 350 351 device_init_settings(dev, tb_dev); 352 vlandev_apply_settings(mvdev, tb_mv); 353 mvdev->ifname = tb_mv[VLANDEV_ATTR_IFNAME]; 354 mvdev->vid = tb_mv[VLANDEV_ATTR_VID]; 355 356 if (mvdev->config_data) { 357 struct blob_attr *otb_dev[__DEV_ATTR_MAX]; 358 struct blob_attr *otb_mv[__VLANDEV_ATTR_MAX]; 359 360 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb_dev, 361 blob_data(mvdev->config_data), blob_len(mvdev->config_data)); 362 363 if (uci_blob_diff(tb_dev, otb_dev, &device_attr_list, NULL)) 364 ret = DEV_CONFIG_RESTART; 365 366 blobmsg_parse(vlandev_attrs, __VLANDEV_ATTR_MAX, otb_mv, 367 blob_data(mvdev->config_data), blob_len(mvdev->config_data)); 368 369 if (uci_blob_diff(tb_mv, otb_mv, &vlandev_attr_list, NULL)) 370 ret = DEV_CONFIG_RESTART; 371 372 vlandev_config_init(dev); 373 } 374 375 free(mvdev->config_data); 376 mvdev->config_data = attr; 377 return ret; 378 } 379 380 static struct device * 381 vlandev_create(const char *name, struct device_type *devtype, 382 struct blob_attr *attr) 383 { 384 struct vlandev_device *mvdev; 385 struct device *dev = NULL; 386 387 mvdev = calloc(1, sizeof(*mvdev)); 388 if (!mvdev) 389 return NULL; 390 391 vlist_simple_init(&mvdev->config.ingress_qos_mapping_list, 392 struct vlan_qos_mapping, node); 393 vlist_simple_init(&mvdev->config.egress_qos_mapping_list, 394 struct vlan_qos_mapping, node); 395 396 dev = &mvdev->dev; 397 398 if (device_init(dev, devtype, name) < 0) { 399 device_cleanup(dev); 400 free(mvdev); 401 return NULL; 402 } 403 404 dev->config_pending = true; 405 406 mvdev->set_state = dev->set_state; 407 dev->set_state = vlandev_set_state; 408 409 dev->hotplug_ops = NULL; 410 mvdev->parent.cb = vlandev_base_cb; 411 412 vlandev_reload(dev, attr); 413 414 return dev; 415 } 416 417 static struct device_type vlan8021ad_device_type = { 418 .name = "8021ad", 419 .config_params = &vlandev_attr_list, 420 .create = vlandev_create, 421 .config_init = vlandev_config_init, 422 .reload = vlandev_reload, 423 .free = vlandev_free, 424 .dump_info = vlandev_dump_info, 425 }; 426 427 static struct device_type vlan8021q_device_type = { 428 .name = "8021q", 429 .config_params = &vlandev_attr_list, 430 .create = vlandev_create, 431 .config_init = vlandev_config_init, 432 .reload = vlandev_reload, 433 .free = vlandev_free, 434 .dump_info = vlandev_dump_info, 435 }; 436 437 static void __init vlandev_device_type_init(void) 438 { 439 device_type_add(&vlan8021ad_device_type); 440 device_type_add(&vlan8021q_device_type); 441 } 442
This page was automatically generated by LXR 0.3.1. • OpenWrt