1 /* Copyright (C) 2007-2015 B.A.T.M.A.N. contributors: 2 * 3 * Marek Lindner, Simon Wunderlich 4 * 5 * This program is free software; you can redistribute it and/or 6 * modify it under the terms of version 2 of the GNU General Public 7 * License as published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope that it will be useful, but 10 * WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #include "hard-interface.h" 19 #include "main.h" 20 21 #include <linux/bug.h> 22 #include <linux/byteorder/generic.h> 23 #include <linux/errno.h> 24 #include <linux/fs.h> 25 #include <linux/if_arp.h> 26 #include <linux/if_ether.h> 27 #include <linux/if.h> 28 #include <linux/kernel.h> 29 #include <linux/list.h> 30 #include <linux/netdevice.h> 31 #include <linux/printk.h> 32 #include <linux/rculist.h> 33 #include <linux/rtnetlink.h> 34 #include <linux/slab.h> 35 #include <linux/spinlock.h> 36 #include <linux/workqueue.h> 37 #include <net/net_namespace.h> 38 39 #include "bridge_loop_avoidance.h" 40 #include "debugfs.h" 41 #include "distributed-arp-table.h" 42 #include "gateway_client.h" 43 #include "originator.h" 44 #include "packet.h" 45 #include "send.h" 46 #include "soft-interface.h" 47 #include "sysfs.h" 48 #include "translation-table.h" 49 50 void batadv_hardif_free_rcu(struct rcu_head *rcu) 51 { 52 struct batadv_hard_iface *hard_iface; 53 54 hard_iface = container_of(rcu, struct batadv_hard_iface, rcu); 55 dev_put(hard_iface->net_dev); 56 kfree(hard_iface); 57 } 58 59 struct batadv_hard_iface * 60 batadv_hardif_get_by_netdev(const struct net_device *net_dev) 61 { 62 struct batadv_hard_iface *hard_iface; 63 64 rcu_read_lock(); 65 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { 66 if (hard_iface->net_dev == net_dev && 67 atomic_inc_not_zero(&hard_iface->refcount)) 68 goto out; 69 } 70 71 hard_iface = NULL; 72 73 out: 74 rcu_read_unlock(); 75 return hard_iface; 76 } 77 78 /** 79 * batadv_mutual_parents - check if two devices are each others parent 80 * @dev1: 1st net_device 81 * @dev2: 2nd net_device 82 * 83 * veth devices come in pairs and each is the parent of the other! 84 * 85 * Return: true if the devices are each others parent, otherwise false 86 */ 87 static bool batadv_mutual_parents(const struct net_device *dev1, 88 const struct net_device *dev2) 89 { 90 int dev1_parent_iflink = dev_get_iflink(dev1); 91 int dev2_parent_iflink = dev_get_iflink(dev2); 92 93 if (!dev1_parent_iflink || !dev2_parent_iflink) 94 return false; 95 96 return (dev1_parent_iflink == dev2->ifindex) && 97 (dev2_parent_iflink == dev1->ifindex); 98 } 99 100 /** 101 * batadv_is_on_batman_iface - check if a device is a batman iface descendant 102 * @net_dev: the device to check 103 * 104 * If the user creates any virtual device on top of a batman-adv interface, it 105 * is important to prevent this new interface to be used to create a new mesh 106 * network (this behaviour would lead to a batman-over-batman configuration). 107 * This function recursively checks all the fathers of the device passed as 108 * argument looking for a batman-adv soft interface. 109 * 110 * Returns true if the device is descendant of a batman-adv mesh interface (or 111 * if it is a batman-adv interface itself), false otherwise 112 */ 113 static bool batadv_is_on_batman_iface(const struct net_device *net_dev) 114 { 115 struct net_device *parent_dev; 116 bool ret; 117 118 /* check if this is a batman-adv mesh interface */ 119 if (batadv_softif_is_valid(net_dev)) 120 return true; 121 122 /* no more parents..stop recursion */ 123 if (dev_get_iflink(net_dev) == 0 || 124 dev_get_iflink(net_dev) == net_dev->ifindex) 125 return false; 126 127 /* recurse over the parent device */ 128 parent_dev = __dev_get_by_index(&init_net, dev_get_iflink(net_dev)); 129 /* if we got a NULL parent_dev there is something broken.. */ 130 if (WARN(!parent_dev, "Cannot find parent device")) 131 return false; 132 133 if (batadv_mutual_parents(net_dev, parent_dev)) 134 return false; 135 136 ret = batadv_is_on_batman_iface(parent_dev); 137 138 return ret; 139 } 140 141 static int batadv_is_valid_iface(const struct net_device *net_dev) 142 { 143 if (net_dev->flags & IFF_LOOPBACK) 144 return 0; 145 146 if (net_dev->type != ARPHRD_ETHER) 147 return 0; 148 149 if (net_dev->addr_len != ETH_ALEN) 150 return 0; 151 152 /* no batman over batman */ 153 if (batadv_is_on_batman_iface(net_dev)) 154 return 0; 155 156 return 1; 157 } 158 159 /** 160 * batadv_is_wifi_netdev - check if the given net_device struct is a wifi 161 * interface 162 * @net_device: the device to check 163 * 164 * Returns true if the net device is a 802.11 wireless device, false otherwise. 165 */ 166 bool batadv_is_wifi_netdev(struct net_device *net_device) 167 { 168 if (!net_device) 169 return false; 170 171 #ifdef CONFIG_WIRELESS_EXT 172 /* pre-cfg80211 drivers have to implement WEXT, so it is possible to 173 * check for wireless_handlers != NULL 174 */ 175 if (net_device->wireless_handlers) 176 return true; 177 #endif 178 179 /* cfg80211 drivers have to set ieee80211_ptr */ 180 if (net_device->ieee80211_ptr) 181 return true; 182 183 return false; 184 } 185 186 static struct batadv_hard_iface * 187 batadv_hardif_get_active(const struct net_device *soft_iface) 188 { 189 struct batadv_hard_iface *hard_iface; 190 191 rcu_read_lock(); 192 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { 193 if (hard_iface->soft_iface != soft_iface) 194 continue; 195 196 if (hard_iface->if_status == BATADV_IF_ACTIVE && 197 atomic_inc_not_zero(&hard_iface->refcount)) 198 goto out; 199 } 200 201 hard_iface = NULL; 202 203 out: 204 rcu_read_unlock(); 205 return hard_iface; 206 } 207 208 static void batadv_primary_if_update_addr(struct batadv_priv *bat_priv, 209 struct batadv_hard_iface *oldif) 210 { 211 struct batadv_hard_iface *primary_if; 212 213 primary_if = batadv_primary_if_get_selected(bat_priv); 214 if (!primary_if) 215 goto out; 216 217 batadv_dat_init_own_addr(bat_priv, primary_if); 218 batadv_bla_update_orig_address(bat_priv, primary_if, oldif); 219 out: 220 if (primary_if) 221 batadv_hardif_free_ref(primary_if); 222 } 223 224 static void batadv_primary_if_select(struct batadv_priv *bat_priv, 225 struct batadv_hard_iface *new_hard_iface) 226 { 227 struct batadv_hard_iface *curr_hard_iface; 228 229 ASSERT_RTNL(); 230 231 if (new_hard_iface && !atomic_inc_not_zero(&new_hard_iface->refcount)) 232 new_hard_iface = NULL; 233 234 curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1); 235 rcu_assign_pointer(bat_priv->primary_if, new_hard_iface); 236 237 if (!new_hard_iface) 238 goto out; 239 240 bat_priv->bat_algo_ops->bat_primary_iface_set(new_hard_iface); 241 batadv_primary_if_update_addr(bat_priv, curr_hard_iface); 242 243 out: 244 if (curr_hard_iface) 245 batadv_hardif_free_ref(curr_hard_iface); 246 } 247 248 static bool 249 batadv_hardif_is_iface_up(const struct batadv_hard_iface *hard_iface) 250 { 251 if (hard_iface->net_dev->flags & IFF_UP) 252 return true; 253 254 return false; 255 } 256 257 static void batadv_check_known_mac_addr(const struct net_device *net_dev) 258 { 259 const struct batadv_hard_iface *hard_iface; 260 261 rcu_read_lock(); 262 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { 263 if ((hard_iface->if_status != BATADV_IF_ACTIVE) && 264 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED)) 265 continue; 266 267 if (hard_iface->net_dev == net_dev) 268 continue; 269 270 if (!batadv_compare_eth(hard_iface->net_dev->dev_addr, 271 net_dev->dev_addr)) 272 continue; 273 274 pr_warn("The newly added mac address (%pM) already exists on: %s\n", 275 net_dev->dev_addr, hard_iface->net_dev->name); 276 pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n"); 277 } 278 rcu_read_unlock(); 279 } 280 281 /** 282 * batadv_hardif_recalc_extra_skbroom() - Recalculate skbuff extra head/tailroom 283 * @soft_iface: netdev struct of the mesh interface 284 */ 285 static void batadv_hardif_recalc_extra_skbroom(struct net_device *soft_iface) 286 { 287 const struct batadv_hard_iface *hard_iface; 288 unsigned short lower_header_len = ETH_HLEN; 289 unsigned short lower_headroom = 0; 290 unsigned short lower_tailroom = 0; 291 unsigned short needed_headroom; 292 293 rcu_read_lock(); 294 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { 295 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE) 296 continue; 297 298 if (hard_iface->soft_iface != soft_iface) 299 continue; 300 301 lower_header_len = max_t(unsigned short, lower_header_len, 302 hard_iface->net_dev->hard_header_len); 303 304 lower_headroom = max_t(unsigned short, lower_headroom, 305 hard_iface->net_dev->needed_headroom); 306 307 lower_tailroom = max_t(unsigned short, lower_tailroom, 308 hard_iface->net_dev->needed_tailroom); 309 } 310 rcu_read_unlock(); 311 312 needed_headroom = lower_headroom + (lower_header_len - ETH_HLEN); 313 needed_headroom += batadv_max_header_len(); 314 315 soft_iface->needed_headroom = needed_headroom; 316 soft_iface->needed_tailroom = lower_tailroom; 317 } 318 319 int batadv_hardif_min_mtu(struct net_device *soft_iface) 320 { 321 struct batadv_priv *bat_priv = netdev_priv(soft_iface); 322 const struct batadv_hard_iface *hard_iface; 323 int min_mtu = INT_MAX; 324 325 rcu_read_lock(); 326 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { 327 if ((hard_iface->if_status != BATADV_IF_ACTIVE) && 328 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED)) 329 continue; 330 331 if (hard_iface->soft_iface != soft_iface) 332 continue; 333 334 min_mtu = min_t(int, hard_iface->net_dev->mtu, min_mtu); 335 } 336 rcu_read_unlock(); 337 338 if (atomic_read(&bat_priv->fragmentation) == 0) 339 goto out; 340 341 /* with fragmentation enabled the maximum size of internally generated 342 * packets such as translation table exchanges or tvlv containers, etc 343 * has to be calculated 344 */ 345 min_mtu = min_t(int, min_mtu, BATADV_FRAG_MAX_FRAG_SIZE); 346 min_mtu -= sizeof(struct batadv_frag_packet); 347 min_mtu *= BATADV_FRAG_MAX_FRAGMENTS; 348 349 out: 350 /* report to the other components the maximum amount of bytes that 351 * batman-adv can send over the wire (without considering the payload 352 * overhead). For example, this value is used by TT to compute the 353 * maximum local table table size 354 */ 355 atomic_set(&bat_priv->packet_size_max, min_mtu); 356 357 /* the real soft-interface MTU is computed by removing the payload 358 * overhead from the maximum amount of bytes that was just computed. 359 * 360 * However batman-adv does not support MTUs bigger than ETH_DATA_LEN 361 */ 362 return min_t(int, min_mtu - batadv_max_header_len(), ETH_DATA_LEN); 363 } 364 365 /* adjusts the MTU if a new interface with a smaller MTU appeared. */ 366 void batadv_update_min_mtu(struct net_device *soft_iface) 367 { 368 soft_iface->mtu = batadv_hardif_min_mtu(soft_iface); 369 370 /* Check if the local translate table should be cleaned up to match a 371 * new (and smaller) MTU. 372 */ 373 batadv_tt_local_resize_to_mtu(soft_iface); 374 } 375 376 static void 377 batadv_hardif_activate_interface(struct batadv_hard_iface *hard_iface) 378 { 379 struct batadv_priv *bat_priv; 380 struct batadv_hard_iface *primary_if = NULL; 381 382 if (hard_iface->if_status != BATADV_IF_INACTIVE) 383 goto out; 384 385 bat_priv = netdev_priv(hard_iface->soft_iface); 386 387 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface); 388 hard_iface->if_status = BATADV_IF_TO_BE_ACTIVATED; 389 390 /* the first active interface becomes our primary interface or 391 * the next active interface after the old primary interface was removed 392 */ 393 primary_if = batadv_primary_if_get_selected(bat_priv); 394 if (!primary_if) 395 batadv_primary_if_select(bat_priv, hard_iface); 396 397 batadv_info(hard_iface->soft_iface, "Interface activated: %s\n", 398 hard_iface->net_dev->name); 399 400 batadv_update_min_mtu(hard_iface->soft_iface); 401 402 out: 403 if (primary_if) 404 batadv_hardif_free_ref(primary_if); 405 } 406 407 static void 408 batadv_hardif_deactivate_interface(struct batadv_hard_iface *hard_iface) 409 { 410 if ((hard_iface->if_status != BATADV_IF_ACTIVE) && 411 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED)) 412 return; 413 414 hard_iface->if_status = BATADV_IF_INACTIVE; 415 416 batadv_info(hard_iface->soft_iface, "Interface deactivated: %s\n", 417 hard_iface->net_dev->name); 418 419 batadv_update_min_mtu(hard_iface->soft_iface); 420 } 421 422 /** 423 * batadv_master_del_slave - remove hard_iface from the current master interface 424 * @slave: the interface enslaved in another master 425 * @master: the master from which slave has to be removed 426 * 427 * Invoke ndo_del_slave on master passing slave as argument. In this way slave 428 * is free'd and master can correctly change its internal state. 429 * Return 0 on success, a negative value representing the error otherwise 430 */ 431 static int batadv_master_del_slave(struct batadv_hard_iface *slave, 432 struct net_device *master) 433 { 434 int ret; 435 436 if (!master) 437 return 0; 438 439 ret = -EBUSY; 440 if (master->netdev_ops->ndo_del_slave) 441 ret = master->netdev_ops->ndo_del_slave(master, slave->net_dev); 442 443 return ret; 444 } 445 446 int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface, 447 const char *iface_name) 448 { 449 struct batadv_priv *bat_priv; 450 struct net_device *soft_iface, *master; 451 __be16 ethertype = htons(ETH_P_BATMAN); 452 int max_header_len = batadv_max_header_len(); 453 int ret; 454 455 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE) 456 goto out; 457 458 if (!atomic_inc_not_zero(&hard_iface->refcount)) 459 goto out; 460 461 soft_iface = dev_get_by_name(&init_net, iface_name); 462 463 if (!soft_iface) { 464 soft_iface = batadv_softif_create(iface_name); 465 466 if (!soft_iface) { 467 ret = -ENOMEM; 468 goto err; 469 } 470 471 /* dev_get_by_name() increases the reference counter for us */ 472 dev_hold(soft_iface); 473 } 474 475 if (!batadv_softif_is_valid(soft_iface)) { 476 pr_err("Can't create batman mesh interface %s: already exists as regular interface\n", 477 soft_iface->name); 478 ret = -EINVAL; 479 goto err_dev; 480 } 481 482 /* check if the interface is enslaved in another virtual one and 483 * in that case unlink it first 484 */ 485 master = netdev_master_upper_dev_get(hard_iface->net_dev); 486 ret = batadv_master_del_slave(hard_iface, master); 487 if (ret) 488 goto err_dev; 489 490 hard_iface->soft_iface = soft_iface; 491 bat_priv = netdev_priv(hard_iface->soft_iface); 492 493 ret = netdev_master_upper_dev_link(hard_iface->net_dev, 494 soft_iface, NULL, NULL); 495 if (ret) 496 goto err_dev; 497 498 ret = bat_priv->bat_algo_ops->bat_iface_enable(hard_iface); 499 if (ret < 0) 500 goto err_upper; 501 502 hard_iface->if_num = bat_priv->num_ifaces; 503 bat_priv->num_ifaces++; 504 hard_iface->if_status = BATADV_IF_INACTIVE; 505 ret = batadv_orig_hash_add_if(hard_iface, bat_priv->num_ifaces); 506 if (ret < 0) { 507 bat_priv->bat_algo_ops->bat_iface_disable(hard_iface); 508 bat_priv->num_ifaces--; 509 hard_iface->if_status = BATADV_IF_NOT_IN_USE; 510 goto err_upper; 511 } 512 513 hard_iface->batman_adv_ptype.type = ethertype; 514 hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv; 515 hard_iface->batman_adv_ptype.dev = hard_iface->net_dev; 516 dev_add_pack(&hard_iface->batman_adv_ptype); 517 518 batadv_info(hard_iface->soft_iface, "Adding interface: %s\n", 519 hard_iface->net_dev->name); 520 521 if (atomic_read(&bat_priv->fragmentation) && 522 hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len) 523 batadv_info(hard_iface->soft_iface, 524 "The MTU of interface %s is too small (%i) to handle the transport of batman-adv packets. Packets going over this interface will be fragmented on layer2 which could impact the performance. Setting the MTU to %i would solve the problem.\n", 525 hard_iface->net_dev->name, hard_iface->net_dev->mtu, 526 ETH_DATA_LEN + max_header_len); 527 528 if (!atomic_read(&bat_priv->fragmentation) && 529 hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len) 530 batadv_info(hard_iface->soft_iface, 531 "The MTU of interface %s is too small (%i) to handle the transport of batman-adv packets. If you experience problems getting traffic through try increasing the MTU to %i.\n", 532 hard_iface->net_dev->name, hard_iface->net_dev->mtu, 533 ETH_DATA_LEN + max_header_len); 534 535 if (batadv_hardif_is_iface_up(hard_iface)) 536 batadv_hardif_activate_interface(hard_iface); 537 else 538 batadv_err(hard_iface->soft_iface, 539 "Not using interface %s (retrying later): interface not active\n", 540 hard_iface->net_dev->name); 541 542 batadv_hardif_recalc_extra_skbroom(soft_iface); 543 544 /* begin scheduling originator messages on that interface */ 545 batadv_schedule_bat_ogm(hard_iface); 546 547 out: 548 return 0; 549 550 err_upper: 551 netdev_upper_dev_unlink(hard_iface->net_dev, soft_iface); 552 err_dev: 553 hard_iface->soft_iface = NULL; 554 dev_put(soft_iface); 555 err: 556 batadv_hardif_free_ref(hard_iface); 557 return ret; 558 } 559 560 void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface, 561 enum batadv_hard_if_cleanup autodel) 562 { 563 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); 564 struct batadv_hard_iface *primary_if = NULL; 565 566 if (hard_iface->if_status == BATADV_IF_ACTIVE) 567 batadv_hardif_deactivate_interface(hard_iface); 568 569 if (hard_iface->if_status != BATADV_IF_INACTIVE) 570 goto out; 571 572 batadv_info(hard_iface->soft_iface, "Removing interface: %s\n", 573 hard_iface->net_dev->name); 574 dev_remove_pack(&hard_iface->batman_adv_ptype); 575 576 bat_priv->num_ifaces--; 577 batadv_orig_hash_del_if(hard_iface, bat_priv->num_ifaces); 578 579 primary_if = batadv_primary_if_get_selected(bat_priv); 580 if (hard_iface == primary_if) { 581 struct batadv_hard_iface *new_if; 582 583 new_if = batadv_hardif_get_active(hard_iface->soft_iface); 584 batadv_primary_if_select(bat_priv, new_if); 585 586 if (new_if) 587 batadv_hardif_free_ref(new_if); 588 } 589 590 bat_priv->bat_algo_ops->bat_iface_disable(hard_iface); 591 hard_iface->if_status = BATADV_IF_NOT_IN_USE; 592 593 /* delete all references to this hard_iface */ 594 batadv_purge_orig_ref(bat_priv); 595 batadv_purge_outstanding_packets(bat_priv, hard_iface); 596 dev_put(hard_iface->soft_iface); 597 598 netdev_upper_dev_unlink(hard_iface->net_dev, hard_iface->soft_iface); 599 batadv_hardif_recalc_extra_skbroom(hard_iface->soft_iface); 600 601 /* nobody uses this interface anymore */ 602 if (!bat_priv->num_ifaces) { 603 batadv_gw_check_client_stop(bat_priv); 604 605 if (autodel == BATADV_IF_CLEANUP_AUTO) 606 batadv_softif_destroy_sysfs(hard_iface->soft_iface); 607 } 608 609 hard_iface->soft_iface = NULL; 610 batadv_hardif_free_ref(hard_iface); 611 612 out: 613 if (primary_if) 614 batadv_hardif_free_ref(primary_if); 615 } 616 617 /** 618 * batadv_hardif_remove_interface_finish - cleans up the remains of a hardif 619 * @work: work queue item 620 * 621 * Free the parts of the hard interface which can not be removed under 622 * rtnl lock (to prevent deadlock situations). 623 */ 624 static void batadv_hardif_remove_interface_finish(struct work_struct *work) 625 { 626 struct batadv_hard_iface *hard_iface; 627 628 hard_iface = container_of(work, struct batadv_hard_iface, 629 cleanup_work); 630 631 batadv_debugfs_del_hardif(hard_iface); 632 batadv_sysfs_del_hardif(&hard_iface->hardif_obj); 633 batadv_hardif_free_ref(hard_iface); 634 } 635 636 static struct batadv_hard_iface * 637 batadv_hardif_add_interface(struct net_device *net_dev) 638 { 639 struct batadv_hard_iface *hard_iface; 640 int ret; 641 642 ASSERT_RTNL(); 643 644 ret = batadv_is_valid_iface(net_dev); 645 if (ret != 1) 646 goto out; 647 648 dev_hold(net_dev); 649 650 hard_iface = kzalloc(sizeof(*hard_iface), GFP_ATOMIC); 651 if (!hard_iface) 652 goto release_dev; 653 654 ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev); 655 if (ret) 656 goto free_if; 657 658 hard_iface->if_num = -1; 659 hard_iface->net_dev = net_dev; 660 hard_iface->soft_iface = NULL; 661 hard_iface->if_status = BATADV_IF_NOT_IN_USE; 662 663 ret = batadv_debugfs_add_hardif(hard_iface); 664 if (ret) 665 goto free_sysfs; 666 667 INIT_LIST_HEAD(&hard_iface->list); 668 INIT_HLIST_HEAD(&hard_iface->neigh_list); 669 INIT_WORK(&hard_iface->cleanup_work, 670 batadv_hardif_remove_interface_finish); 671 672 spin_lock_init(&hard_iface->neigh_list_lock); 673 674 hard_iface->num_bcasts = BATADV_NUM_BCASTS_DEFAULT; 675 if (batadv_is_wifi_netdev(net_dev)) 676 hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS; 677 678 /* extra reference for return */ 679 atomic_set(&hard_iface->refcount, 2); 680 681 batadv_check_known_mac_addr(hard_iface->net_dev); 682 list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list); 683 684 return hard_iface; 685 686 free_sysfs: 687 batadv_sysfs_del_hardif(&hard_iface->hardif_obj); 688 free_if: 689 kfree(hard_iface); 690 release_dev: 691 dev_put(net_dev); 692 out: 693 return NULL; 694 } 695 696 static void batadv_hardif_remove_interface(struct batadv_hard_iface *hard_iface) 697 { 698 ASSERT_RTNL(); 699 700 /* first deactivate interface */ 701 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE) 702 batadv_hardif_disable_interface(hard_iface, 703 BATADV_IF_CLEANUP_AUTO); 704 705 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE) 706 return; 707 708 hard_iface->if_status = BATADV_IF_TO_BE_REMOVED; 709 queue_work(batadv_event_workqueue, &hard_iface->cleanup_work); 710 } 711 712 void batadv_hardif_remove_interfaces(void) 713 { 714 struct batadv_hard_iface *hard_iface, *hard_iface_tmp; 715 716 rtnl_lock(); 717 list_for_each_entry_safe(hard_iface, hard_iface_tmp, 718 &batadv_hardif_list, list) { 719 list_del_rcu(&hard_iface->list); 720 batadv_hardif_remove_interface(hard_iface); 721 } 722 rtnl_unlock(); 723 } 724 725 static int batadv_hard_if_event(struct notifier_block *this, 726 unsigned long event, void *ptr) 727 { 728 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr); 729 struct batadv_hard_iface *hard_iface; 730 struct batadv_hard_iface *primary_if = NULL; 731 struct batadv_priv *bat_priv; 732 733 if (batadv_softif_is_valid(net_dev) && event == NETDEV_REGISTER) { 734 batadv_sysfs_add_meshif(net_dev); 735 bat_priv = netdev_priv(net_dev); 736 batadv_softif_create_vlan(bat_priv, BATADV_NO_FLAGS); 737 return NOTIFY_DONE; 738 } 739 740 hard_iface = batadv_hardif_get_by_netdev(net_dev); 741 if (!hard_iface && (event == NETDEV_REGISTER || 742 event == NETDEV_POST_TYPE_CHANGE)) 743 hard_iface = batadv_hardif_add_interface(net_dev); 744 745 if (!hard_iface) 746 goto out; 747 748 switch (event) { 749 case NETDEV_UP: 750 batadv_hardif_activate_interface(hard_iface); 751 break; 752 case NETDEV_GOING_DOWN: 753 case NETDEV_DOWN: 754 batadv_hardif_deactivate_interface(hard_iface); 755 break; 756 case NETDEV_UNREGISTER: 757 case NETDEV_PRE_TYPE_CHANGE: 758 list_del_rcu(&hard_iface->list); 759 760 batadv_hardif_remove_interface(hard_iface); 761 break; 762 case NETDEV_CHANGEMTU: 763 if (hard_iface->soft_iface) 764 batadv_update_min_mtu(hard_iface->soft_iface); 765 break; 766 case NETDEV_CHANGEADDR: 767 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE) 768 goto hardif_put; 769 770 batadv_check_known_mac_addr(hard_iface->net_dev); 771 772 bat_priv = netdev_priv(hard_iface->soft_iface); 773 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface); 774 775 primary_if = batadv_primary_if_get_selected(bat_priv); 776 if (!primary_if) 777 goto hardif_put; 778 779 if (hard_iface == primary_if) 780 batadv_primary_if_update_addr(bat_priv, NULL); 781 break; 782 default: 783 break; 784 } 785 786 hardif_put: 787 batadv_hardif_free_ref(hard_iface); 788 out: 789 if (primary_if) 790 batadv_hardif_free_ref(primary_if); 791 return NOTIFY_DONE; 792 } 793 794 struct notifier_block batadv_hard_if_notifier = { 795 .notifier_call = batadv_hard_if_event, 796 }; 797