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 #include <assert.h> 18 19 #include <sys/types.h> 20 #include <sys/socket.h> 21 22 #include <libubox/list.h> 23 24 #include "netifd.h" 25 #include "system.h" 26 #include "config.h" 27 #include "wireless.h" 28 #include "ubus.h" 29 30 static struct list_head devtypes = LIST_HEAD_INIT(devtypes); 31 static struct avl_tree devices; 32 static struct blob_buf b; 33 34 static const struct blobmsg_policy dev_attrs[__DEV_ATTR_MAX] = { 35 [DEV_ATTR_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING }, 36 [DEV_ATTR_MTU] = { .name = "mtu", .type = BLOBMSG_TYPE_INT32 }, 37 [DEV_ATTR_MTU6] = { .name = "mtu6", .type = BLOBMSG_TYPE_INT32 }, 38 [DEV_ATTR_MACADDR] = { .name = "macaddr", .type = BLOBMSG_TYPE_STRING }, 39 [DEV_ATTR_TXQUEUELEN] = { .name = "txqueuelen", .type = BLOBMSG_TYPE_INT32 }, 40 [DEV_ATTR_ENABLED] = { .name = "enabled", .type = BLOBMSG_TYPE_BOOL }, 41 [DEV_ATTR_IPV6] = { .name = "ipv6", .type = BLOBMSG_TYPE_BOOL }, 42 [DEV_ATTR_IP6SEGMENTROUTING] = { .name = "ip6segmentrouting", .type = BLOBMSG_TYPE_BOOL }, 43 [DEV_ATTR_PROMISC] = { .name = "promisc", .type = BLOBMSG_TYPE_BOOL }, 44 [DEV_ATTR_RPFILTER] = { .name = "rpfilter", .type = BLOBMSG_TYPE_STRING }, 45 [DEV_ATTR_ACCEPTLOCAL] = { .name = "acceptlocal", .type = BLOBMSG_TYPE_BOOL }, 46 [DEV_ATTR_IGMPVERSION] = { .name = "igmpversion", .type = BLOBMSG_TYPE_INT32 }, 47 [DEV_ATTR_MLDVERSION] = { .name = "mldversion", .type = BLOBMSG_TYPE_INT32 }, 48 [DEV_ATTR_NEIGHREACHABLETIME] = { .name = "neighreachabletime", .type = BLOBMSG_TYPE_INT32 }, 49 [DEV_ATTR_NEIGHGCSTALETIME] = { .name = "neighgcstaletime", .type = BLOBMSG_TYPE_INT32 }, 50 [DEV_ATTR_DADTRANSMITS] = { .name = "dadtransmits", .type = BLOBMSG_TYPE_INT32 }, 51 [DEV_ATTR_MULTICAST_TO_UNICAST] = { .name = "multicast_to_unicast", .type = BLOBMSG_TYPE_BOOL }, 52 [DEV_ATTR_MULTICAST_ROUTER] = { .name = "multicast_router", .type = BLOBMSG_TYPE_INT32 }, 53 [DEV_ATTR_MULTICAST_FAST_LEAVE] = { .name = "multicast_fast_leave", . type = BLOBMSG_TYPE_BOOL }, 54 [DEV_ATTR_MULTICAST] = { .name ="multicast", .type = BLOBMSG_TYPE_BOOL }, 55 [DEV_ATTR_LEARNING] = { .name ="learning", .type = BLOBMSG_TYPE_BOOL }, 56 [DEV_ATTR_UNICAST_FLOOD] = { .name ="unicast_flood", .type = BLOBMSG_TYPE_BOOL }, 57 [DEV_ATTR_SENDREDIRECTS] = { .name = "sendredirects", .type = BLOBMSG_TYPE_BOOL }, 58 [DEV_ATTR_NEIGHLOCKTIME] = { .name = "neighlocktime", .type = BLOBMSG_TYPE_INT32 }, 59 [DEV_ATTR_ISOLATE] = { .name = "isolate", .type = BLOBMSG_TYPE_BOOL }, 60 [DEV_ATTR_DROP_V4_UNICAST_IN_L2_MULTICAST] = { .name = "drop_v4_unicast_in_l2_multicast", .type = BLOBMSG_TYPE_BOOL }, 61 [DEV_ATTR_DROP_V6_UNICAST_IN_L2_MULTICAST] = { .name = "drop_v6_unicast_in_l2_multicast", .type = BLOBMSG_TYPE_BOOL }, 62 [DEV_ATTR_DROP_GRATUITOUS_ARP] = { .name = "drop_gratuitous_arp", .type = BLOBMSG_TYPE_BOOL }, 63 [DEV_ATTR_DROP_UNSOLICITED_NA] = { .name = "drop_unsolicited_na", .type = BLOBMSG_TYPE_BOOL }, 64 [DEV_ATTR_ARP_ACCEPT] = { .name = "arp_accept", .type = BLOBMSG_TYPE_BOOL }, 65 [DEV_ATTR_AUTH] = { .name = "auth", .type = BLOBMSG_TYPE_BOOL }, 66 [DEV_ATTR_AUTH_VLAN] = { .name = "auth_vlan", BLOBMSG_TYPE_ARRAY }, 67 [DEV_ATTR_SPEED] = { .name = "speed", .type = BLOBMSG_TYPE_INT32 }, 68 [DEV_ATTR_DUPLEX] = { .name = "duplex", .type = BLOBMSG_TYPE_BOOL }, 69 [DEV_ATTR_VLAN] = { .name = "vlan", .type = BLOBMSG_TYPE_ARRAY }, 70 [DEV_ATTR_PAUSE] = { .name = "pause", .type = BLOBMSG_TYPE_BOOL }, 71 [DEV_ATTR_ASYM_PAUSE] = { .name = "asym_pause", .type = BLOBMSG_TYPE_BOOL }, 72 [DEV_ATTR_RXPAUSE] = { .name = "rxpause", .type = BLOBMSG_TYPE_BOOL }, 73 [DEV_ATTR_TXPAUSE] = { .name = "txpause", .type = BLOBMSG_TYPE_BOOL }, 74 [DEV_ATTR_AUTONEG] = { .name = "autoneg", .type = BLOBMSG_TYPE_BOOL }, 75 [DEV_ATTR_GRO] = { .name = "gro", .type = BLOBMSG_TYPE_BOOL }, 76 [DEV_ATTR_MASTER] = { .name = "conduit", .type = BLOBMSG_TYPE_STRING }, 77 [DEV_ATTR_EEE] = { .name = "eee", .type = BLOBMSG_TYPE_BOOL }, 78 [DEV_ATTR_TAGS] = { .name = "tags", .type = BLOBMSG_TYPE_ARRAY }, 79 }; 80 81 const struct uci_blob_param_list device_attr_list = { 82 .n_params = __DEV_ATTR_MAX, 83 .params = dev_attrs, 84 }; 85 86 static int __devlock = 0; 87 88 int device_type_add(struct device_type *devtype) 89 { 90 if (device_type_get(devtype->name)) { 91 netifd_log_message(L_WARNING, "Device handler '%s' already exists\n", 92 devtype->name); 93 return 1; 94 } 95 96 netifd_log_message(L_NOTICE, "Added device handler type: %s\n", 97 devtype->name); 98 99 list_add(&devtype->list, &devtypes); 100 return 0; 101 } 102 103 struct device_type * 104 device_type_get(const char *tname) 105 { 106 struct device_type *cur; 107 108 list_for_each_entry(cur, &devtypes, list) 109 if (!strcmp(cur->name, tname)) 110 return cur; 111 112 return NULL; 113 } 114 115 static int device_vlan_len(struct kvlist *kv, const void *data) 116 { 117 return sizeof(unsigned int); 118 } 119 120 void device_vlan_update(bool done) 121 { 122 struct device *dev; 123 124 avl_for_each_element(&devices, dev, avl) { 125 if (!dev->vlans.update) 126 continue; 127 128 if (!done) { 129 if (dev->vlan_aliases.get_len) 130 kvlist_free(&dev->vlan_aliases); 131 else 132 kvlist_init(&dev->vlan_aliases, device_vlan_len); 133 vlist_update(&dev->vlans); 134 } else { 135 vlist_flush(&dev->vlans); 136 137 if (dev->type->vlan_update) 138 dev->type->vlan_update(dev); 139 } 140 } 141 } 142 143 void device_stp_init(void) 144 { 145 struct device *dev; 146 147 avl_for_each_element(&devices, dev, avl) { 148 if (!dev->type->stp_init) 149 continue; 150 151 dev->type->stp_init(dev); 152 } 153 } 154 155 static int set_device_state(struct device *dev, bool state) 156 { 157 if (state) { 158 /* Get ifindex for all devices being enabled so a valid */ 159 /* ifindex is in place avoiding possible race conditions */ 160 device_set_ifindex(dev, system_if_resolve(dev)); 161 if (!dev->ifindex) 162 return -1; 163 164 system_if_get_settings(dev, &dev->orig_settings); 165 /* Only keep orig settings based on what needs to be set */ 166 dev->orig_settings.valid_flags = dev->orig_settings.flags; 167 dev->orig_settings.flags &= dev->settings.flags; 168 system_if_apply_settings(dev, &dev->settings, dev->settings.flags); 169 170 if (!dev->external) 171 system_if_up(dev); 172 173 system_if_apply_settings_after_up(dev, &dev->settings); 174 } else { 175 if (!dev->external) 176 system_if_down(dev); 177 system_if_apply_settings(dev, &dev->orig_settings, dev->orig_settings.flags); 178 179 /* Restore any settings present in UCI which may have 180 * failed to apply so that they will be re-attempted 181 * the next time the device is brought up */ 182 dev->settings.flags |= dev->settings.valid_flags; 183 } 184 185 return 0; 186 } 187 188 static int 189 simple_device_set_state(struct device *dev, bool state) 190 { 191 struct device *pdev; 192 int ret = 0; 193 194 pdev = dev->parent.dev; 195 if (state && !pdev) { 196 pdev = system_if_get_parent(dev); 197 if (pdev) 198 device_add_user(&dev->parent, pdev); 199 } 200 201 if (pdev) { 202 if (state) 203 ret = device_claim(&dev->parent); 204 else 205 device_release(&dev->parent); 206 207 if (ret < 0) 208 return ret; 209 } 210 return set_device_state(dev, state); 211 } 212 213 static struct device * 214 simple_device_create(const char *name, struct device_type *devtype, 215 struct blob_attr *attr) 216 { 217 struct blob_attr *tb[__DEV_ATTR_MAX]; 218 struct device *dev = NULL; 219 220 /* device type is unused for simple devices */ 221 devtype = NULL; 222 223 blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb, blob_data(attr), blob_len(attr)); 224 dev = device_get(name, true); 225 if (!dev) 226 return NULL; 227 228 dev->set_state = simple_device_set_state; 229 device_init_settings(dev, tb); 230 231 return dev; 232 } 233 234 static void simple_device_free(struct device *dev) 235 { 236 if (dev->parent.dev) 237 device_remove_user(&dev->parent); 238 free(dev); 239 } 240 241 struct device_type simple_device_type = { 242 .name = "Network device", 243 .config_params = &device_attr_list, 244 245 .create = simple_device_create, 246 .check_state = system_if_check, 247 .free = simple_device_free, 248 }; 249 250 void 251 device_merge_settings(struct device *dev, struct device_settings *n) 252 { 253 struct device_settings *os = &dev->orig_settings; 254 struct device_settings *s = &dev->settings; 255 256 memset(n, 0, sizeof(*n)); 257 n->mtu = s->flags & DEV_OPT_MTU ? s->mtu : os->mtu; 258 n->mtu6 = s->flags & DEV_OPT_MTU6 ? s->mtu6 : os->mtu6; 259 n->txqueuelen = s->flags & DEV_OPT_TXQUEUELEN ? 260 s->txqueuelen : os->txqueuelen; 261 memcpy(n->macaddr, 262 (s->flags & (DEV_OPT_MACADDR|DEV_OPT_DEFAULT_MACADDR) ? s->macaddr : os->macaddr), 263 sizeof(n->macaddr)); 264 n->ipv6 = s->flags & DEV_OPT_IPV6 ? s->ipv6 : os->ipv6; 265 n->ip6segmentrouting = s->flags & DEV_OPT_IP6SEGMENTROUTING ? s->ip6segmentrouting : os->ip6segmentrouting; 266 n->promisc = s->flags & DEV_OPT_PROMISC ? s->promisc : os->promisc; 267 n->rpfilter = s->flags & DEV_OPT_RPFILTER ? s->rpfilter : os->rpfilter; 268 n->acceptlocal = s->flags & DEV_OPT_ACCEPTLOCAL ? s->acceptlocal : os->acceptlocal; 269 n->igmpversion = s->flags & DEV_OPT_IGMPVERSION ? s->igmpversion : os->igmpversion; 270 n->mldversion = s->flags & DEV_OPT_MLDVERSION ? s->mldversion : os->mldversion; 271 n->neigh4reachabletime = s->flags & DEV_OPT_NEIGHREACHABLETIME ? 272 s->neigh4reachabletime : os->neigh4reachabletime; 273 n->neigh6reachabletime = s->flags & DEV_OPT_NEIGHREACHABLETIME ? 274 s->neigh6reachabletime : os->neigh6reachabletime; 275 n->neigh4gcstaletime = s->flags & DEV_OPT_NEIGHGCSTALETIME ? 276 s->neigh4gcstaletime : os->neigh4gcstaletime; 277 n->neigh6gcstaletime = s->flags & DEV_OPT_NEIGHGCSTALETIME ? 278 s->neigh6gcstaletime : os->neigh6gcstaletime; 279 n->neigh4locktime = s->flags & DEV_OPT_NEIGHLOCKTIME ? 280 s->neigh4locktime : os->neigh4locktime; 281 n->dadtransmits = s->flags & DEV_OPT_DADTRANSMITS ? 282 s->dadtransmits : os->dadtransmits; 283 n->multicast = s->flags & DEV_OPT_MULTICAST ? 284 s->multicast : os->multicast; 285 n->multicast_to_unicast = s->multicast_to_unicast; 286 n->multicast_router = s->multicast_router; 287 n->multicast_fast_leave = s->multicast_fast_leave; 288 n->learning = s->learning; 289 n->unicast_flood = s->unicast_flood; 290 n->sendredirects = s->flags & DEV_OPT_SENDREDIRECTS ? 291 s->sendredirects : os->sendredirects; 292 n->drop_v4_unicast_in_l2_multicast = s->flags & DEV_OPT_DROP_V4_UNICAST_IN_L2_MULTICAST ? 293 s->drop_v4_unicast_in_l2_multicast : os->drop_v4_unicast_in_l2_multicast; 294 n->drop_v6_unicast_in_l2_multicast = s->flags & DEV_OPT_DROP_V6_UNICAST_IN_L2_MULTICAST ? 295 s->drop_v6_unicast_in_l2_multicast : os->drop_v6_unicast_in_l2_multicast; 296 n->drop_gratuitous_arp = s->flags & DEV_OPT_DROP_GRATUITOUS_ARP ? 297 s->drop_gratuitous_arp : os->drop_gratuitous_arp; 298 n->drop_unsolicited_na = s->flags & DEV_OPT_DROP_UNSOLICITED_NA ? 299 s->drop_unsolicited_na : os->drop_unsolicited_na; 300 n->arp_accept = s->flags & DEV_OPT_ARP_ACCEPT ? 301 s->arp_accept : os->arp_accept; 302 n->auth = s->flags & DEV_OPT_AUTH ? s->auth : os->auth; 303 n->speed = s->flags & DEV_OPT_SPEED ? s->speed : os->speed; 304 n->duplex = s->flags & DEV_OPT_DUPLEX ? s->duplex : os->duplex; 305 n->pause = s->flags & DEV_OPT_PAUSE ? s->pause : os->pause; 306 n->asym_pause = s->flags & DEV_OPT_ASYM_PAUSE ? s->asym_pause : os->asym_pause; 307 n->rxpause = s->flags & DEV_OPT_RXPAUSE ? s->rxpause : os->rxpause; 308 n->txpause = s->flags & DEV_OPT_TXPAUSE ? s->txpause : os->txpause; 309 n->autoneg = s->flags & DEV_OPT_AUTONEG ? s->autoneg : os->autoneg; 310 n->gro = s->flags & DEV_OPT_GRO ? s->gro : os->gro; 311 n->eee = s->flags & DEV_OPT_EEE ? s->eee : os->eee; 312 n->master_ifindex = s->flags & DEV_OPT_MASTER ? s->master_ifindex : os->master_ifindex; 313 n->flags = s->flags | os->flags | os->valid_flags; 314 } 315 316 static bool device_fill_vlan_range(struct device_vlan_range *r, const char *val) 317 { 318 unsigned long cur_start, cur_end; 319 char *sep; 320 321 cur_start = strtoul(val, &sep, 0); 322 cur_end = cur_start; 323 324 if (*sep == '-') 325 cur_end = strtoul(sep + 1, &sep, 0); 326 if (*sep || cur_end < cur_start) 327 return false; 328 329 r->start = cur_start; 330 r->end = cur_end; 331 332 return true; 333 } 334 335 static void 336 device_set_extra_vlans(struct device *dev, struct blob_attr *data) 337 { 338 struct blob_attr *cur; 339 int n_vlans; 340 size_t rem; 341 342 dev->n_extra_vlan = 0; 343 if (!data) 344 return; 345 346 n_vlans = blobmsg_check_array(data, BLOBMSG_TYPE_STRING); 347 if (n_vlans < 1) 348 return; 349 350 dev->extra_vlan = realloc(dev->extra_vlan, n_vlans * sizeof(*dev->extra_vlan)); 351 blobmsg_for_each_attr(cur, data, rem) 352 if (device_fill_vlan_range(&dev->extra_vlan[dev->n_extra_vlan], 353 blobmsg_get_string(cur))) 354 dev->n_extra_vlan++; 355 } 356 357 void 358 device_init_settings(struct device *dev, struct blob_attr **tb) 359 { 360 struct device_settings *s = &dev->settings; 361 struct blob_attr *cur; 362 struct ether_addr *ea; 363 bool disabled = false; 364 365 if (dev->wireless) 366 s->flags &= DEV_OPT_ISOLATE; 367 else 368 s->flags = 0; 369 if ((cur = tb[DEV_ATTR_ENABLED])) 370 disabled = !blobmsg_get_bool(cur); 371 372 if ((cur = tb[DEV_ATTR_MTU]) && blobmsg_get_u32(cur) >= 68) { 373 s->mtu = blobmsg_get_u32(cur); 374 s->flags |= DEV_OPT_MTU; 375 } 376 377 if ((cur = tb[DEV_ATTR_MTU6]) && blobmsg_get_u32(cur) >= 1280) { 378 s->mtu6 = blobmsg_get_u32(cur); 379 s->flags |= DEV_OPT_MTU6; 380 } 381 382 if ((cur = tb[DEV_ATTR_TXQUEUELEN])) { 383 s->txqueuelen = blobmsg_get_u32(cur); 384 s->flags |= DEV_OPT_TXQUEUELEN; 385 } 386 387 if ((cur = tb[DEV_ATTR_MACADDR])) { 388 ea = ether_aton(blobmsg_data(cur)); 389 if (ea) { 390 memcpy(s->macaddr, ea, 6); 391 s->flags |= DEV_OPT_MACADDR; 392 } 393 } 394 395 if ((cur = tb[DEV_ATTR_IPV6])) { 396 s->ipv6 = blobmsg_get_bool(cur); 397 s->flags |= DEV_OPT_IPV6; 398 } 399 400 if ((cur = tb[DEV_ATTR_IP6SEGMENTROUTING])) { 401 s->ip6segmentrouting = blobmsg_get_bool(cur); 402 s->flags |= DEV_OPT_IP6SEGMENTROUTING; 403 } 404 405 if ((cur = tb[DEV_ATTR_PROMISC])) { 406 s->promisc = blobmsg_get_bool(cur); 407 s->flags |= DEV_OPT_PROMISC; 408 } 409 410 if ((cur = tb[DEV_ATTR_RPFILTER])) { 411 if (system_resolve_rpfilter(blobmsg_data(cur), &s->rpfilter)) 412 s->flags |= DEV_OPT_RPFILTER; 413 else 414 D(DEVICE, "Failed to resolve rpfilter: %s", (char *) blobmsg_data(cur)); 415 } 416 417 if ((cur = tb[DEV_ATTR_ACCEPTLOCAL])) { 418 s->acceptlocal = blobmsg_get_bool(cur); 419 s->flags |= DEV_OPT_ACCEPTLOCAL; 420 } 421 422 if ((cur = tb[DEV_ATTR_IGMPVERSION])) { 423 s->igmpversion = blobmsg_get_u32(cur); 424 if (s->igmpversion >= 1 && s->igmpversion <= 3) 425 s->flags |= DEV_OPT_IGMPVERSION; 426 else 427 D(DEVICE, "Failed to resolve igmpversion: %d", blobmsg_get_u32(cur)); 428 } 429 430 if ((cur = tb[DEV_ATTR_MLDVERSION])) { 431 s->mldversion = blobmsg_get_u32(cur); 432 if (s->mldversion >= 1 && s->mldversion <= 2) 433 s->flags |= DEV_OPT_MLDVERSION; 434 else 435 D(DEVICE, "Failed to resolve mldversion: %d", blobmsg_get_u32(cur)); 436 } 437 438 if ((cur = tb[DEV_ATTR_NEIGHREACHABLETIME])) { 439 s->neigh6reachabletime = s->neigh4reachabletime = blobmsg_get_u32(cur); 440 s->flags |= DEV_OPT_NEIGHREACHABLETIME; 441 } 442 443 if ((cur = tb[DEV_ATTR_NEIGHGCSTALETIME])) { 444 s->neigh6gcstaletime = s->neigh4gcstaletime = blobmsg_get_u32(cur); 445 s->flags |= DEV_OPT_NEIGHGCSTALETIME; 446 } 447 448 if ((cur = tb[DEV_ATTR_NEIGHLOCKTIME])) { 449 s->neigh4locktime = blobmsg_get_u32(cur); 450 s->flags |= DEV_OPT_NEIGHLOCKTIME; 451 } 452 453 if ((cur = tb[DEV_ATTR_DADTRANSMITS])) { 454 s->dadtransmits = blobmsg_get_u32(cur); 455 s->flags |= DEV_OPT_DADTRANSMITS; 456 } 457 458 if ((cur = tb[DEV_ATTR_MULTICAST_TO_UNICAST])) { 459 s->multicast_to_unicast = blobmsg_get_bool(cur); 460 s->flags |= DEV_OPT_MULTICAST_TO_UNICAST; 461 } 462 463 if ((cur = tb[DEV_ATTR_MULTICAST_ROUTER])) { 464 s->multicast_router = blobmsg_get_u32(cur); 465 if (s->multicast_router <= 2) 466 s->flags |= DEV_OPT_MULTICAST_ROUTER; 467 else 468 D(DEVICE, "Invalid value: %d - (Use 0: never, 1: learn, 2: always)", blobmsg_get_u32(cur)); 469 } 470 471 if ((cur = tb[DEV_ATTR_MULTICAST_FAST_LEAVE])) { 472 s->multicast_fast_leave = blobmsg_get_bool(cur); 473 s->flags |= DEV_OPT_MULTICAST_FAST_LEAVE; 474 } 475 476 if ((cur = tb[DEV_ATTR_MULTICAST])) { 477 s->multicast = blobmsg_get_bool(cur); 478 s->flags |= DEV_OPT_MULTICAST; 479 } 480 481 if ((cur = tb[DEV_ATTR_LEARNING])) { 482 s->learning = blobmsg_get_bool(cur); 483 s->flags |= DEV_OPT_LEARNING; 484 } 485 486 if ((cur = tb[DEV_ATTR_UNICAST_FLOOD])) { 487 s->unicast_flood = blobmsg_get_bool(cur); 488 s->flags |= DEV_OPT_UNICAST_FLOOD; 489 } 490 491 if ((cur = tb[DEV_ATTR_SENDREDIRECTS])) { 492 s->sendredirects = blobmsg_get_bool(cur); 493 s->flags |= DEV_OPT_SENDREDIRECTS; 494 } 495 496 if ((cur = tb[DEV_ATTR_ISOLATE])) { 497 s->isolate = blobmsg_get_bool(cur); 498 s->flags |= DEV_OPT_ISOLATE; 499 } 500 501 if ((cur = tb[DEV_ATTR_DROP_V4_UNICAST_IN_L2_MULTICAST])) { 502 s->drop_v4_unicast_in_l2_multicast = blobmsg_get_bool(cur); 503 s->flags |= DEV_OPT_DROP_V4_UNICAST_IN_L2_MULTICAST; 504 } 505 506 if ((cur = tb[DEV_ATTR_DROP_V6_UNICAST_IN_L2_MULTICAST])) { 507 s->drop_v6_unicast_in_l2_multicast = blobmsg_get_bool(cur); 508 s->flags |= DEV_OPT_DROP_V6_UNICAST_IN_L2_MULTICAST; 509 } 510 511 if ((cur = tb[DEV_ATTR_DROP_GRATUITOUS_ARP])) { 512 s->drop_gratuitous_arp = blobmsg_get_bool(cur); 513 s->flags |= DEV_OPT_DROP_GRATUITOUS_ARP; 514 } 515 516 if ((cur = tb[DEV_ATTR_DROP_UNSOLICITED_NA])) { 517 s->drop_unsolicited_na = blobmsg_get_bool(cur); 518 s->flags |= DEV_OPT_DROP_UNSOLICITED_NA; 519 } 520 521 if ((cur = tb[DEV_ATTR_ARP_ACCEPT])) { 522 s->arp_accept = blobmsg_get_bool(cur); 523 s->flags |= DEV_OPT_ARP_ACCEPT; 524 } 525 526 if ((cur = tb[DEV_ATTR_AUTH])) { 527 s->auth = blobmsg_get_bool(cur); 528 s->flags |= DEV_OPT_AUTH; 529 } 530 531 if ((cur = tb[DEV_ATTR_SPEED])) { 532 s->speed = blobmsg_get_u32(cur); 533 s->flags |= DEV_OPT_SPEED; 534 } 535 536 if ((cur = tb[DEV_ATTR_DUPLEX])) { 537 s->duplex = blobmsg_get_bool(cur); 538 s->flags |= DEV_OPT_DUPLEX; 539 } 540 541 if ((cur = tb[DEV_ATTR_PAUSE])) { 542 s->pause = blobmsg_get_bool(cur); 543 s->flags |= DEV_OPT_PAUSE; 544 } 545 546 if ((cur = tb[DEV_ATTR_ASYM_PAUSE])) { 547 s->asym_pause = blobmsg_get_bool(cur); 548 s->flags |= DEV_OPT_ASYM_PAUSE; 549 } 550 551 if ((cur = tb[DEV_ATTR_RXPAUSE])) { 552 s->rxpause = blobmsg_get_bool(cur); 553 s->flags |= DEV_OPT_RXPAUSE; 554 } 555 556 if ((cur = tb[DEV_ATTR_TXPAUSE])) { 557 s->txpause = blobmsg_get_bool(cur); 558 s->flags |= DEV_OPT_TXPAUSE; 559 } 560 561 if ((cur = tb[DEV_ATTR_AUTONEG])) { 562 s->autoneg = blobmsg_get_bool(cur); 563 s->flags |= DEV_OPT_AUTONEG; 564 } 565 566 if ((cur = tb[DEV_ATTR_GRO])) { 567 s->gro = blobmsg_get_bool(cur); 568 s->flags |= DEV_OPT_GRO; 569 } 570 571 if ((cur = tb[DEV_ATTR_MASTER])) { 572 char *ifname = blobmsg_get_string(cur); 573 s->master_ifindex = if_nametoindex(ifname); 574 s->flags |= DEV_OPT_MASTER; 575 } 576 577 if ((cur = tb[DEV_ATTR_EEE])) { 578 s->eee = blobmsg_get_bool(cur); 579 s->flags |= DEV_OPT_EEE; 580 } 581 582 /* Remember the settings present in UCI */ 583 s->valid_flags = s->flags; 584 585 cur = tb[DEV_ATTR_AUTH_VLAN]; 586 free(dev->config_auth_vlans); 587 dev->config_auth_vlans = cur ? blob_memdup(cur) : NULL; 588 589 cur = tb[DEV_ATTR_TAGS]; 590 free(dev->tags); 591 dev->tags = cur ? blob_memdup(cur) : NULL; 592 593 device_set_extra_vlans(dev, tb[DEV_ATTR_VLAN]); 594 device_set_disabled(dev, disabled); 595 } 596 597 static void __init dev_init(void) 598 { 599 avl_init(&devices, avl_strcmp, true, NULL); 600 } 601 602 static int device_release_cb(void *ctx, struct safe_list *list) 603 { 604 struct device_user *dep = container_of(list, struct device_user, list); 605 606 if (!dep->dev || !dep->claimed) 607 return 0; 608 609 device_release(dep); 610 return 0; 611 } 612 613 static int device_broadcast_cb(void *ctx, struct safe_list *list) 614 { 615 struct device_user *dep = container_of(list, struct device_user, list); 616 int *ev = ctx; 617 618 /* device might have been removed by an earlier callback */ 619 if (!dep->dev) 620 return 0; 621 622 if (dep->cb) 623 dep->cb(dep, *ev); 624 return 0; 625 } 626 627 const char *device_event_name(enum device_event ev) 628 { 629 static const char * const event_names[] = { 630 [DEV_EVENT_ADD] = "add", 631 [DEV_EVENT_REMOVE] = "remove", 632 [DEV_EVENT_UPDATE_IFNAME] = "update_ifname", 633 [DEV_EVENT_UPDATE_IFINDEX] = "update_ifindex", 634 [DEV_EVENT_SETUP] = "setup", 635 [DEV_EVENT_TEARDOWN] = "teardown", 636 [DEV_EVENT_UP] = "up", 637 [DEV_EVENT_DOWN] = "down", 638 [DEV_EVENT_AUTH_UP] = "auth_up", 639 [DEV_EVENT_LINK_UP] = "link_up", 640 [DEV_EVENT_LINK_DOWN] = "link_down", 641 [DEV_EVENT_TOPO_CHANGE] = "topo_change", 642 }; 643 644 if (ev >= ARRAY_SIZE(event_names) || !event_names[ev]) 645 return "unknown"; 646 647 return event_names[ev]; 648 } 649 650 void __device_broadcast_event(struct device *dev, enum device_event ev) 651 { 652 const char *ev_name; 653 int dev_ev = ev; 654 655 safe_list_for_each(&dev->aliases, device_broadcast_cb, &dev_ev); 656 safe_list_for_each(&dev->users, device_broadcast_cb, &dev_ev); 657 658 switch (ev) { 659 case DEV_EVENT_ADD: 660 case DEV_EVENT_REMOVE: 661 case DEV_EVENT_UP: 662 case DEV_EVENT_DOWN: 663 case DEV_EVENT_AUTH_UP: 664 case DEV_EVENT_LINK_UP: 665 case DEV_EVENT_LINK_DOWN: 666 case DEV_EVENT_TOPO_CHANGE: 667 break; 668 default: 669 return; 670 } 671 672 ev_name = device_event_name(ev); 673 if (!dev->ifname[0]) 674 return; 675 676 blob_buf_init(&b, 0); 677 blobmsg_add_string(&b, "name", dev->ifname); 678 blobmsg_add_u8(&b, "auth_status", dev->auth_status); 679 blobmsg_add_u8(&b, "present", dev->present); 680 blobmsg_add_u8(&b, "active", dev->active); 681 blobmsg_add_u8(&b, "link_active", dev->link_active); 682 netifd_ubus_device_notify(ev_name, b.head, -1); 683 } 684 685 static void 686 device_fill_default_settings(struct device *dev) 687 { 688 struct device_settings *s = &dev->settings; 689 struct ether_addr *ea; 690 const char *master; 691 int ret; 692 693 if (!(s->flags & DEV_OPT_MACADDR)) { 694 ea = config_get_default_macaddr(dev->ifname); 695 if (ea) { 696 memcpy(s->macaddr, ea, 6); 697 s->flags |= DEV_OPT_DEFAULT_MACADDR; 698 } 699 } 700 701 if (!(s->flags & DEV_OPT_GRO)) { 702 ret = config_get_default_gro(dev->ifname); 703 if (ret >= 0) { 704 s->gro = ret; 705 s->flags |= DEV_OPT_GRO; 706 } 707 } 708 709 if (!(s->flags & DEV_OPT_MASTER)) { 710 master = config_get_default_conduit(dev->ifname); 711 if (master) { 712 s->master_ifindex = if_nametoindex(master); 713 s->flags |= DEV_OPT_MASTER; 714 } 715 } 716 } 717 718 int device_claim(struct device_user *dep) 719 { 720 struct device *dev = dep->dev; 721 int ret = 0; 722 723 if (dep->claimed) 724 return 0; 725 726 if (!dev) 727 return -1; 728 729 dep->claimed = true; 730 D(DEVICE, "Claim %s %s, new active count: %d", dev->type->name, dev->ifname, dev->active + 1); 731 if (++dev->active != 1) 732 return 0; 733 734 device_broadcast_event(dev, DEV_EVENT_SETUP); 735 device_fill_default_settings(dev); 736 ret = dev->set_state(dev, true); 737 if (ret == 0) 738 device_broadcast_event(dev, DEV_EVENT_UP); 739 else { 740 D(DEVICE, "claim %s %s failed: %d", dev->type->name, dev->ifname, ret); 741 dev->active = 0; 742 dep->claimed = false; 743 } 744 745 return ret; 746 } 747 748 void device_release(struct device_user *dep) 749 { 750 struct device *dev = dep->dev; 751 752 if (!dep->claimed) 753 return; 754 755 dep->claimed = false; 756 dev->active--; 757 D(DEVICE, "Release %s %s, new active count: %d", dev->type->name, dev->ifname, dev->active); 758 assert(dev->active >= 0); 759 760 if (dev->active) 761 return; 762 763 device_broadcast_event(dev, DEV_EVENT_TEARDOWN); 764 dev->set_state(dev, false); 765 766 if (dev->active) 767 return; 768 769 device_broadcast_event(dev, DEV_EVENT_DOWN); 770 } 771 772 int device_check_state(struct device *dev) 773 { 774 if (!dev->type->check_state) 775 return simple_device_type.check_state(dev); 776 777 return dev->type->check_state(dev); 778 } 779 780 int device_init_virtual(struct device *dev, struct device_type *type, const char *name) 781 { 782 assert(dev); 783 assert(type); 784 785 D(DEVICE, "Initialize device '%s'", name ? name : ""); 786 INIT_SAFE_LIST(&dev->users); 787 INIT_SAFE_LIST(&dev->aliases); 788 dev->type = type; 789 790 if (name) { 791 int ret; 792 793 ret = device_set_ifname(dev, name); 794 if (ret < 0) { 795 netifd_log_message(L_WARNING, "Failed to initalize device '%s'\n", name); 796 return ret; 797 } 798 } 799 800 if (!dev->set_state) 801 dev->set_state = set_device_state; 802 803 return 0; 804 } 805 806 int device_init(struct device *dev, struct device_type *type, const char *ifname) 807 { 808 int ret; 809 810 ret = device_init_virtual(dev, type, ifname); 811 if (ret < 0) 812 return ret; 813 814 dev->avl.key = dev->ifname; 815 816 ret = avl_insert(&devices, &dev->avl); 817 if (ret < 0) 818 return ret; 819 820 system_if_clear_state(dev); 821 822 return 0; 823 } 824 825 static struct device * 826 device_create_default(const char *name, bool external) 827 { 828 struct device *dev; 829 830 if (!external && system_if_force_external(name)) 831 return NULL; 832 833 D(DEVICE, "Create simple device '%s'", name); 834 dev = calloc(1, sizeof(*dev)); 835 if (!dev) 836 return NULL; 837 838 dev->external = external; 839 dev->set_state = simple_device_set_state; 840 841 if (device_init(dev, &simple_device_type, name) < 0) { 842 device_cleanup(dev); 843 free(dev); 844 return NULL; 845 } 846 847 dev->default_config = true; 848 device_check_state(dev); 849 850 return dev; 851 } 852 853 struct device * 854 device_find(const char *name) 855 { 856 struct device *dev; 857 858 return avl_find_element(&devices, name, dev, avl); 859 } 860 861 struct device * 862 __device_get(const char *name, int create, bool check_vlan) 863 { 864 struct device *dev; 865 866 dev = avl_find_element(&devices, name, dev, avl); 867 868 if (!dev && check_vlan && strchr(name, '.')) 869 return get_vlan_device_chain(name, create); 870 871 if (name[0] == '@') 872 return device_alias_get(name + 1); 873 874 if (dev) { 875 if (create > 1 && !dev->external) { 876 dev->external = true; 877 device_set_present(dev, true); 878 } 879 return dev; 880 } 881 882 if (!create) 883 return NULL; 884 885 return device_create_default(name, create > 1); 886 } 887 888 static void 889 device_delete(struct device *dev) 890 { 891 if (!dev->avl.key) 892 return; 893 894 D(DEVICE, "Delete device '%s' from list", dev->ifname); 895 avl_delete(&devices, &dev->avl); 896 dev->avl.key = NULL; 897 } 898 899 static int device_cleanup_cb(void *ctx, struct safe_list *list) 900 { 901 struct device_user *dep = container_of(list, struct device_user, list); 902 if (dep->cb) 903 dep->cb(dep, DEV_EVENT_REMOVE); 904 905 device_release(dep); 906 return 0; 907 } 908 909 void device_cleanup(struct device *dev) 910 { 911 D(DEVICE, "Clean up device '%s'", dev->ifname); 912 safe_list_for_each(&dev->users, device_cleanup_cb, NULL); 913 safe_list_for_each(&dev->aliases, device_cleanup_cb, NULL); 914 device_delete(dev); 915 } 916 917 static void __device_set_present(struct device *dev, bool state, bool force) 918 { 919 if (dev->present == state && !force) 920 return; 921 922 dev->present = state; 923 device_broadcast_event(dev, state ? DEV_EVENT_ADD : DEV_EVENT_REMOVE); 924 } 925 926 void 927 device_refresh_present(struct device *dev) 928 { 929 bool state = dev->sys_present; 930 931 if (dev->disabled || dev->deferred) 932 state = false; 933 934 D(DEVICE, "refresh device %s present: sys=%d disabled=%d deferred=%d", 935 dev->ifname, dev->sys_present, dev->disabled, dev->deferred); 936 __device_set_present(dev, state, false); 937 } 938 939 void 940 device_set_auth_status(struct device *dev, bool value, struct blob_attr *vlans) 941 { 942 if (!value) 943 vlans = NULL; 944 else if (!blob_attr_equal(vlans, dev->auth_vlans)) 945 device_set_auth_status(dev, false, NULL); 946 947 free(dev->auth_vlans); 948 dev->auth_vlans = vlans ? blob_memdup(vlans) : NULL; 949 950 if (dev->auth_status == value) 951 return; 952 953 dev->auth_status = value; 954 if (!dev->present) 955 return; 956 957 if (dev->auth_status) { 958 device_broadcast_event(dev, DEV_EVENT_AUTH_UP); 959 return; 960 } 961 962 device_broadcast_event(dev, DEV_EVENT_LINK_DOWN); 963 if (!dev->link_active) 964 return; 965 966 device_broadcast_event(dev, DEV_EVENT_LINK_UP); 967 } 968 969 void _device_set_present(struct device *dev, bool state) 970 { 971 if (dev->sys_present == state) 972 return; 973 974 D(DEVICE, "%s '%s' %s present", dev->type->name, dev->ifname, state ? "is now" : "is no longer" ); 975 dev->sys_present = state; 976 if (!state) 977 __device_set_present(dev, state, true); 978 else 979 device_refresh_present(dev); 980 if (!state) 981 safe_list_for_each(&dev->users, device_release_cb, NULL); 982 } 983 984 void device_set_link(struct device *dev, bool state) 985 { 986 if (dev->link_active == state) 987 return; 988 989 netifd_log_message(L_NOTICE, "%s '%s' link is %s\n", dev->type->name, dev->ifname, state ? "up" : "down" ); 990 991 dev->link_active = state; 992 if (!state) 993 dev->auth_status = false; 994 device_broadcast_event(dev, state ? DEV_EVENT_LINK_UP : DEV_EVENT_LINK_DOWN); 995 } 996 997 void device_set_ifindex(struct device *dev, int ifindex) 998 { 999 if (dev->ifindex == ifindex) 1000 return; 1001 1002 dev->ifindex = ifindex; 1003 device_broadcast_event(dev, DEV_EVENT_UPDATE_IFINDEX); 1004 } 1005 1006 int device_set_ifname(struct device *dev, const char *name) 1007 { 1008 int ret = 0; 1009 1010 if (!strcmp(dev->ifname, name)) 1011 return 0; 1012 1013 if (strlen(name) > sizeof(dev->ifname) - 1) { 1014 netifd_log_message(L_WARNING, "Cannot set device name: '%s' is longer than max size %zd\n", 1015 name, sizeof(dev->ifname) - 1); 1016 return -1; 1017 } 1018 1019 if (dev->avl.key) 1020 avl_delete(&devices, &dev->avl); 1021 1022 strcpy(dev->ifname, name); 1023 1024 if (dev->avl.key) 1025 ret = avl_insert(&devices, &dev->avl); 1026 1027 if (ret == 0) 1028 device_broadcast_event(dev, DEV_EVENT_UPDATE_IFNAME); 1029 1030 return ret; 1031 } 1032 1033 static int device_refcount(struct device *dev) 1034 { 1035 struct list_head *list; 1036 int count = 0; 1037 1038 list_for_each(list, &dev->users.list) 1039 count++; 1040 1041 list_for_each(list, &dev->aliases.list) 1042 count++; 1043 1044 return count; 1045 } 1046 1047 static void 1048 __device_add_user(struct device_user *dep, struct device *dev) 1049 { 1050 struct safe_list *head; 1051 1052 dep->dev = dev; 1053 1054 if (dep->alias) 1055 head = &dev->aliases; 1056 else 1057 head = &dev->users; 1058 1059 safe_list_add(&dep->list, head); 1060 D(DEVICE, "Add user for device '%s', refcount=%d", dev->ifname, device_refcount(dev)); 1061 1062 if (dep->cb && dev->present) { 1063 dep->cb(dep, DEV_EVENT_ADD); 1064 if (dev->active) 1065 dep->cb(dep, DEV_EVENT_UP); 1066 1067 if (dev->link_active) 1068 dep->cb(dep, DEV_EVENT_LINK_UP); 1069 } 1070 } 1071 1072 void device_add_user(struct device_user *dep, struct device *dev) 1073 { 1074 if (dep->dev == dev) 1075 return; 1076 1077 if (dep->dev) 1078 device_remove_user(dep); 1079 1080 if (!dev) 1081 return; 1082 1083 __device_add_user(dep, dev); 1084 } 1085 1086 static void 1087 device_free(struct device *dev) 1088 { 1089 __devlock++; 1090 free(dev->auth_vlans); 1091 free(dev->config); 1092 device_cleanup(dev); 1093 free(dev->tags); 1094 free(dev->config_auth_vlans); 1095 free(dev->extra_vlan); 1096 dev->type->free(dev); 1097 __devlock--; 1098 } 1099 1100 static void 1101 __device_free_unused(struct uloop_timeout *timeout) 1102 { 1103 struct device *dev, *tmp; 1104 1105 avl_for_each_element_safe(&devices, dev, avl, tmp) { 1106 if (!safe_list_empty(&dev->users) || 1107 !safe_list_empty(&dev->aliases) || 1108 dev->current_config) 1109 continue; 1110 1111 device_free(dev); 1112 } 1113 } 1114 1115 void device_free_unused(void) 1116 { 1117 static struct uloop_timeout free_timer = { 1118 .cb = __device_free_unused, 1119 }; 1120 1121 uloop_timeout_set(&free_timer, 1); 1122 } 1123 1124 void device_remove_user(struct device_user *dep) 1125 { 1126 struct device *dev = dep->dev; 1127 1128 if (!dep->dev) 1129 return; 1130 1131 dep->hotplug = false; 1132 if (dep->claimed) 1133 device_release(dep); 1134 1135 safe_list_del(&dep->list); 1136 dep->dev = NULL; 1137 D(DEVICE, "Remove user for device '%s', refcount=%d", dev->ifname, device_refcount(dev)); 1138 device_free_unused(); 1139 } 1140 1141 void 1142 device_init_pending(void) 1143 { 1144 struct device *dev, *tmp; 1145 1146 avl_for_each_element_safe(&devices, dev, avl, tmp) { 1147 if (!dev->config_pending) 1148 continue; 1149 1150 dev->type->config_init(dev); 1151 dev->config_pending = false; 1152 device_check_state(dev); 1153 } 1154 } 1155 1156 bool 1157 device_check_ip6segmentrouting(void) 1158 { 1159 struct device *dev; 1160 bool ip6segmentrouting = false; 1161 1162 avl_for_each_element(&devices, dev, avl) 1163 ip6segmentrouting |= dev->settings.ip6segmentrouting; 1164 1165 return ip6segmentrouting; 1166 } 1167 1168 static enum dev_change_type 1169 device_set_config(struct device *dev, struct device_type *type, 1170 struct blob_attr *attr) 1171 { 1172 struct blob_attr *tb[__DEV_ATTR_MAX]; 1173 const struct uci_blob_param_list *cfg = type->config_params; 1174 1175 if (type != dev->type) 1176 return DEV_CONFIG_RECREATE; 1177 1178 if (dev->type->reload) 1179 return dev->type->reload(dev, attr); 1180 1181 if (uci_blob_check_equal(dev->config, attr, cfg)) 1182 return DEV_CONFIG_NO_CHANGE; 1183 1184 if (cfg == &device_attr_list) { 1185 memset(tb, 0, sizeof(tb)); 1186 1187 if (attr) 1188 blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb, 1189 blob_data(attr), blob_len(attr)); 1190 1191 device_init_settings(dev, tb); 1192 return DEV_CONFIG_RESTART; 1193 } else 1194 return DEV_CONFIG_RECREATE; 1195 } 1196 1197 enum dev_change_type 1198 device_apply_config(struct device *dev, struct device_type *type, 1199 struct blob_attr *config) 1200 { 1201 enum dev_change_type change; 1202 1203 change = device_set_config(dev, type, config); 1204 switch (change) { 1205 case DEV_CONFIG_RESTART: 1206 case DEV_CONFIG_APPLIED: 1207 D(DEVICE, "Device '%s': config applied", dev->ifname); 1208 config = blob_memdup(config); 1209 free(dev->config); 1210 dev->config = config; 1211 if (change == DEV_CONFIG_RESTART && dev->present) { 1212 int ret = 0; 1213 1214 device_set_present(dev, false); 1215 if (dev->active) { 1216 ret = dev->set_state(dev, false); 1217 if (!ret) 1218 ret = dev->set_state(dev, true); 1219 } 1220 if (!ret) 1221 device_set_present(dev, true); 1222 } 1223 break; 1224 case DEV_CONFIG_NO_CHANGE: 1225 D(DEVICE, "Device '%s': no configuration change", dev->ifname); 1226 break; 1227 case DEV_CONFIG_RECREATE: 1228 break; 1229 } 1230 1231 return change; 1232 } 1233 1234 static void 1235 device_replace(struct device *dev, struct device *odev) 1236 { 1237 struct device_user *dep; 1238 1239 __devlock++; 1240 if (odev->present) 1241 device_set_present(odev, false); 1242 1243 while (!list_empty(&odev->users.list)) { 1244 dep = list_first_entry(&odev->users.list, struct device_user, list.list); 1245 device_release(dep); 1246 if (!dep->dev) 1247 continue; 1248 1249 safe_list_del(&dep->list); 1250 __device_add_user(dep, dev); 1251 } 1252 __devlock--; 1253 1254 device_free(odev); 1255 } 1256 1257 void 1258 device_reset_config(void) 1259 { 1260 struct device *dev; 1261 1262 avl_for_each_element(&devices, dev, avl) 1263 dev->current_config = false; 1264 } 1265 1266 void 1267 device_reset_old(void) 1268 { 1269 struct device *dev, *tmp, *ndev; 1270 1271 avl_for_each_element_safe(&devices, dev, avl, tmp) { 1272 if (dev->current_config || dev->default_config) 1273 continue; 1274 1275 if (dev->type != &simple_device_type) 1276 continue; 1277 1278 ndev = device_create_default(dev->ifname, dev->external); 1279 if (!ndev) 1280 continue; 1281 1282 device_replace(ndev, dev); 1283 } 1284 } 1285 1286 struct device * 1287 device_create(const char *name, struct device_type *type, 1288 struct blob_attr *config) 1289 { 1290 struct device *odev = NULL, *dev; 1291 enum dev_change_type change; 1292 1293 odev = device_find(name); 1294 if (odev) { 1295 odev->current_config = true; 1296 change = device_apply_config(odev, type, config); 1297 switch (change) { 1298 case DEV_CONFIG_RECREATE: 1299 D(DEVICE, "Device '%s': recreate device", odev->ifname); 1300 device_delete(odev); 1301 break; 1302 default: 1303 return odev; 1304 } 1305 } else 1306 D(DEVICE, "Create new device '%s' (%s)", name, type->name); 1307 1308 config = blob_memdup(config); 1309 if (!config) 1310 return NULL; 1311 1312 dev = type->create(name, type, config); 1313 if (!dev) 1314 return NULL; 1315 1316 dev->current_config = true; 1317 dev->config = config; 1318 if (odev) 1319 device_replace(dev, odev); 1320 1321 if (!config_init && dev->config_pending) { 1322 type->config_init(dev); 1323 dev->config_pending = false; 1324 } 1325 1326 device_check_state(dev); 1327 1328 return dev; 1329 } 1330 1331 void 1332 device_dump_status(struct blob_buf *b, struct device *dev) 1333 { 1334 struct device_settings st; 1335 void *c, *s; 1336 1337 if (!dev) { 1338 avl_for_each_element(&devices, dev, avl) { 1339 if (!dev->present) 1340 continue; 1341 c = blobmsg_open_table(b, dev->ifname); 1342 device_dump_status(b, dev); 1343 blobmsg_close_table(b, c); 1344 } 1345 1346 return; 1347 } 1348 1349 blobmsg_add_u8(b, "external", dev->external); 1350 blobmsg_add_u8(b, "present", dev->present); 1351 blobmsg_add_string(b, "type", dev->type->name); 1352 1353 if (!dev->present) 1354 return; 1355 1356 blobmsg_add_u8(b, "up", !!dev->active); 1357 blobmsg_add_u8(b, "carrier", !!dev->link_active); 1358 blobmsg_add_u8(b, "auth_status", !!dev->auth_status); 1359 1360 if (dev->tags) 1361 blobmsg_add_blob(b, dev->tags); 1362 1363 if (dev->type->dump_info) 1364 dev->type->dump_info(dev, b); 1365 else 1366 system_if_dump_info(dev, b); 1367 1368 if (dev->active) { 1369 device_merge_settings(dev, &st); 1370 if (st.flags & DEV_OPT_MASTER) { 1371 char buf[64], *devname; 1372 1373 devname = if_indextoname(st.master_ifindex, buf); 1374 if (devname) 1375 blobmsg_add_string(b, "conduit", devname); 1376 } 1377 if (st.flags & DEV_OPT_MTU) 1378 blobmsg_add_u32(b, "mtu", st.mtu); 1379 if (st.flags & DEV_OPT_MTU6) 1380 blobmsg_add_u32(b, "mtu6", st.mtu6); 1381 if (st.flags & DEV_OPT_MACADDR) 1382 blobmsg_add_string(b, "macaddr", format_macaddr(st.macaddr)); 1383 if (st.flags & DEV_OPT_TXQUEUELEN) 1384 blobmsg_add_u32(b, "txqueuelen", st.txqueuelen); 1385 if (st.flags & DEV_OPT_IPV6) 1386 blobmsg_add_u8(b, "ipv6", st.ipv6); 1387 if (st.flags & DEV_OPT_IP6SEGMENTROUTING) 1388 blobmsg_add_u8(b, "ip6segmentrouting", st.ip6segmentrouting); 1389 if (st.flags & DEV_OPT_PROMISC) 1390 blobmsg_add_u8(b, "promisc", st.promisc); 1391 if (st.flags & DEV_OPT_RPFILTER) 1392 blobmsg_add_u32(b, "rpfilter", st.rpfilter); 1393 if (st.flags & DEV_OPT_ACCEPTLOCAL) 1394 blobmsg_add_u8(b, "acceptlocal", st.acceptlocal); 1395 if (st.flags & DEV_OPT_IGMPVERSION) 1396 blobmsg_add_u32(b, "igmpversion", st.igmpversion); 1397 if (st.flags & DEV_OPT_MLDVERSION) 1398 blobmsg_add_u32(b, "mldversion", st.mldversion); 1399 if (st.flags & DEV_OPT_NEIGHREACHABLETIME) { 1400 blobmsg_add_u32(b, "neigh4reachabletime", st.neigh4reachabletime); 1401 blobmsg_add_u32(b, "neigh6reachabletime", st.neigh6reachabletime); 1402 } 1403 if (st.flags & DEV_OPT_NEIGHGCSTALETIME) { 1404 blobmsg_add_u32(b, "neigh4gcstaletime", st.neigh4gcstaletime); 1405 blobmsg_add_u32(b, "neigh6gcstaletime", st.neigh6gcstaletime); 1406 } 1407 if (st.flags & DEV_OPT_NEIGHLOCKTIME) 1408 blobmsg_add_u32(b, "neigh4locktime", st.neigh4locktime); 1409 if (st.flags & DEV_OPT_DADTRANSMITS) 1410 blobmsg_add_u32(b, "dadtransmits", st.dadtransmits); 1411 if (st.flags & DEV_OPT_MULTICAST_TO_UNICAST) 1412 blobmsg_add_u8(b, "multicast_to_unicast", st.multicast_to_unicast); 1413 if (st.flags & DEV_OPT_MULTICAST_ROUTER) 1414 blobmsg_add_u32(b, "multicast_router", st.multicast_router); 1415 if (st.flags & DEV_OPT_MULTICAST_FAST_LEAVE) 1416 blobmsg_add_u8(b, "multicast_fast_leave", st.multicast_fast_leave); 1417 if (st.flags & DEV_OPT_MULTICAST) 1418 blobmsg_add_u8(b, "multicast", st.multicast); 1419 if (st.flags & DEV_OPT_LEARNING) 1420 blobmsg_add_u8(b, "learning", st.learning); 1421 if (st.flags & DEV_OPT_UNICAST_FLOOD) 1422 blobmsg_add_u8(b, "unicast_flood", st.unicast_flood); 1423 if (st.flags & DEV_OPT_SENDREDIRECTS) 1424 blobmsg_add_u8(b, "sendredirects", st.sendredirects); 1425 if (st.flags & DEV_OPT_DROP_V4_UNICAST_IN_L2_MULTICAST) 1426 blobmsg_add_u8(b, "drop_v4_unicast_in_l2_multicast", st.drop_v4_unicast_in_l2_multicast); 1427 if (st.flags & DEV_OPT_DROP_V6_UNICAST_IN_L2_MULTICAST) 1428 blobmsg_add_u8(b, "drop_v6_unicast_in_l2_multicast", st.drop_v6_unicast_in_l2_multicast); 1429 if (st.flags & DEV_OPT_DROP_GRATUITOUS_ARP) 1430 blobmsg_add_u8(b, "drop_gratuitous_arp", st.drop_gratuitous_arp); 1431 if (st.flags & DEV_OPT_DROP_UNSOLICITED_NA) 1432 blobmsg_add_u8(b, "drop_unsolicited_na", st.drop_unsolicited_na); 1433 if (st.flags & DEV_OPT_ARP_ACCEPT) 1434 blobmsg_add_u8(b, "arp_accept", st.arp_accept); 1435 if (st.flags & DEV_OPT_AUTH) 1436 blobmsg_add_u8(b, "auth", st.auth); 1437 if (st.flags & DEV_OPT_GRO) 1438 blobmsg_add_u8(b, "gro", st.gro); 1439 if (st.flags & DEV_OPT_EEE) 1440 blobmsg_add_u8(b, "eee", st.eee); 1441 } 1442 1443 s = blobmsg_open_table(b, "statistics"); 1444 if (dev->type->dump_stats) 1445 dev->type->dump_stats(dev, b); 1446 else 1447 system_if_dump_stats(dev, b); 1448 blobmsg_close_table(b, s); 1449 } 1450 1451 static void __init simple_device_type_init(void) 1452 { 1453 device_type_add(&simple_device_type); 1454 } 1455 1456 void device_hotplug_event(const char *name, bool add) 1457 { 1458 struct device *dev; 1459 1460 wireless_device_hotplug_event(name, add); 1461 1462 dev = device_find(name); 1463 if (!dev || dev->type != &simple_device_type) 1464 return; 1465 1466 device_set_present(dev, add); 1467 } 1468
This page was automatically generated by LXR 0.3.1. • OpenWrt