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