1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (C) B.A.T.M.A.N. contributors: 3 * 4 * Marek Lindner, Simon Wunderlich 5 */ 6 7 #include "hard-interface.h" 8 #include "main.h" 9 10 #include <linux/atomic.h> 11 #include <linux/byteorder/generic.h> 12 #include <linux/compiler.h> 13 #include <linux/container_of.h> 14 #include <linux/errno.h> 15 #include <linux/gfp.h> 16 #include <linux/if.h> 17 #include <linux/if_arp.h> 18 #include <linux/if_ether.h> 19 #include <linux/kref.h> 20 #include <linux/limits.h> 21 #include <linux/list.h> 22 #include <linux/minmax.h> 23 #include <linux/mutex.h> 24 #include <linux/netdevice.h> 25 #include <linux/notifier.h> 26 #include <linux/printk.h> 27 #include <linux/rculist.h> 28 #include <linux/rtnetlink.h> 29 #include <linux/slab.h> 30 #include <linux/spinlock.h> 31 #include <net/net_namespace.h> 32 #include <net/rtnetlink.h> 33 #include <uapi/linux/batadv_packet.h> 34 35 #include "bat_v.h" 36 #include "bridge_loop_avoidance.h" 37 #include "distributed-arp-table.h" 38 #include "gateway_client.h" 39 #include "log.h" 40 #include "mesh-interface.h" 41 #include "originator.h" 42 #include "send.h" 43 #include "translation-table.h" 44 45 /** 46 * batadv_hardif_release() - release hard interface from lists and queue for 47 * free after rcu grace period 48 * @ref: kref pointer of the hard interface 49 */ 50 void batadv_hardif_release(struct kref *ref) 51 { 52 struct batadv_hard_iface *hard_iface; 53 54 hard_iface = container_of(ref, struct batadv_hard_iface, refcount); 55 netdev_put(hard_iface->net_dev, &hard_iface->dev_tracker); 56 57 kfree_rcu(hard_iface, rcu); 58 } 59 60 /** 61 * batadv_hardif_get_by_netdev() - Get hard interface object of a net_device 62 * @net_dev: net_device to search for 63 * 64 * Return: batadv_hard_iface of net_dev (with increased refcnt), NULL on errors 65 */ 66 struct batadv_hard_iface * 67 batadv_hardif_get_by_netdev(const struct net_device *net_dev) 68 { 69 struct batadv_hard_iface *hard_iface; 70 71 rcu_read_lock(); 72 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { 73 if (hard_iface->net_dev == net_dev && 74 kref_get_unless_zero(&hard_iface->refcount)) 75 goto out; 76 } 77 78 hard_iface = NULL; 79 80 out: 81 rcu_read_unlock(); 82 return hard_iface; 83 } 84 85 /** 86 * batadv_getlink_net() - return link net namespace (of use fallback) 87 * @netdev: net_device to check 88 * @fallback_net: return in case get_link_net is not available for @netdev 89 * 90 * Return: result of rtnl_link_ops->get_link_net or @fallback_net 91 */ 92 static struct net *batadv_getlink_net(const struct net_device *netdev, 93 struct net *fallback_net) 94 { 95 if (!netdev->rtnl_link_ops) 96 return fallback_net; 97 98 if (!netdev->rtnl_link_ops->get_link_net) 99 return fallback_net; 100 101 return netdev->rtnl_link_ops->get_link_net(netdev); 102 } 103 104 /** 105 * batadv_mutual_parents() - check if two devices are each others parent 106 * @dev1: 1st net dev 107 * @net1: 1st devices netns 108 * @dev2: 2nd net dev 109 * @net2: 2nd devices netns 110 * 111 * veth devices come in pairs and each is the parent of the other! 112 * 113 * Return: true if the devices are each others parent, otherwise false 114 */ 115 static bool batadv_mutual_parents(const struct net_device *dev1, 116 struct net *net1, 117 const struct net_device *dev2, 118 struct net *net2) 119 { 120 int dev1_parent_iflink = dev_get_iflink(dev1); 121 int dev2_parent_iflink = dev_get_iflink(dev2); 122 const struct net *dev1_parent_net; 123 const struct net *dev2_parent_net; 124 125 dev1_parent_net = batadv_getlink_net(dev1, net1); 126 dev2_parent_net = batadv_getlink_net(dev2, net2); 127 128 if (!dev1_parent_iflink || !dev2_parent_iflink) 129 return false; 130 131 return (dev1_parent_iflink == dev2->ifindex) && 132 (dev2_parent_iflink == dev1->ifindex) && 133 net_eq(dev1_parent_net, net2) && 134 net_eq(dev2_parent_net, net1); 135 } 136 137 /** 138 * batadv_is_on_batman_iface() - check if a device is a batman iface descendant 139 * @net_dev: the device to check 140 * 141 * If the user creates any virtual device on top of a batman-adv interface, it 142 * is important to prevent this new interface from being used to create a new 143 * mesh network (this behaviour would lead to a batman-over-batman 144 * configuration). This function recursively checks all the fathers of the 145 * device passed as argument looking for a batman-adv mesh interface. 146 * 147 * Return: true if the device is descendant of a batman-adv mesh interface (or 148 * if it is a batman-adv interface itself), false otherwise 149 */ 150 static bool batadv_is_on_batman_iface(const struct net_device *net_dev) 151 { 152 struct net *net = dev_net(net_dev); 153 struct net_device *parent_dev; 154 struct net *parent_net; 155 int iflink; 156 bool ret; 157 158 /* check if this is a batman-adv mesh interface */ 159 if (batadv_meshif_is_valid(net_dev)) 160 return true; 161 162 iflink = dev_get_iflink(net_dev); 163 if (iflink == 0) 164 return false; 165 166 parent_net = batadv_getlink_net(net_dev, net); 167 168 /* iflink to itself, most likely physical device */ 169 if (net == parent_net && iflink == net_dev->ifindex) 170 return false; 171 172 /* recurse over the parent device */ 173 parent_dev = __dev_get_by_index((struct net *)parent_net, iflink); 174 if (!parent_dev) { 175 pr_warn("Cannot find parent device. Skipping batadv-on-batadv check for %s\n", 176 net_dev->name); 177 return false; 178 } 179 180 if (batadv_mutual_parents(net_dev, net, parent_dev, parent_net)) 181 return false; 182 183 ret = batadv_is_on_batman_iface(parent_dev); 184 185 return ret; 186 } 187 188 static bool batadv_is_valid_iface(const struct net_device *net_dev) 189 { 190 if (net_dev->flags & IFF_LOOPBACK) 191 return false; 192 193 if (net_dev->type != ARPHRD_ETHER) 194 return false; 195 196 if (net_dev->addr_len != ETH_ALEN) 197 return false; 198 199 /* no batman over batman */ 200 if (batadv_is_on_batman_iface(net_dev)) 201 return false; 202 203 return true; 204 } 205 206 /** 207 * batadv_get_real_netdevice() - check if the given netdev struct is a virtual 208 * interface on top of another 'real' interface 209 * @netdev: the device to check 210 * 211 * Callers must hold the rtnl semaphore. You may want batadv_get_real_netdev() 212 * instead of this. 213 * 214 * Return: the 'real' net device or the original net device and NULL in case 215 * of an error. 216 */ 217 static struct net_device *batadv_get_real_netdevice(struct net_device *netdev) 218 { 219 struct batadv_hard_iface *hard_iface = NULL; 220 struct net_device *real_netdev = NULL; 221 struct net *real_net; 222 struct net *net; 223 int iflink; 224 225 ASSERT_RTNL(); 226 227 if (!netdev) 228 return NULL; 229 230 iflink = dev_get_iflink(netdev); 231 if (iflink == 0) { 232 dev_hold(netdev); 233 return netdev; 234 } 235 236 hard_iface = batadv_hardif_get_by_netdev(netdev); 237 if (!hard_iface || !hard_iface->mesh_iface) 238 goto out; 239 240 net = dev_net(hard_iface->mesh_iface); 241 real_net = batadv_getlink_net(netdev, net); 242 243 /* iflink to itself, most likely physical device */ 244 if (net == real_net && netdev->ifindex == iflink) { 245 real_netdev = netdev; 246 dev_hold(real_netdev); 247 goto out; 248 } 249 250 real_netdev = dev_get_by_index(real_net, iflink); 251 252 out: 253 batadv_hardif_put(hard_iface); 254 return real_netdev; 255 } 256 257 /** 258 * batadv_get_real_netdev() - check if the given net_device struct is a virtual 259 * interface on top of another 'real' interface 260 * @net_device: the device to check 261 * 262 * Return: the 'real' net device or the original net device and NULL in case 263 * of an error. 264 */ 265 struct net_device *batadv_get_real_netdev(struct net_device *net_device) 266 { 267 struct net_device *real_netdev; 268 269 rtnl_lock(); 270 real_netdev = batadv_get_real_netdevice(net_device); 271 rtnl_unlock(); 272 273 return real_netdev; 274 } 275 276 /** 277 * batadv_is_wext_netdev() - check if the given net_device struct is a 278 * wext wifi interface 279 * @net_device: the device to check 280 * 281 * Return: true if the net device is a wext wireless device, false 282 * otherwise. 283 */ 284 static bool batadv_is_wext_netdev(struct net_device *net_device) 285 { 286 if (!net_device) 287 return false; 288 289 #ifdef CONFIG_WIRELESS_EXT 290 /* pre-cfg80211 drivers have to implement WEXT, so it is possible to 291 * check for wireless_handlers != NULL 292 */ 293 if (net_device->wireless_handlers) 294 return true; 295 #endif 296 297 return false; 298 } 299 300 /** 301 * batadv_is_cfg80211_netdev() - check if the given net_device struct is a 302 * cfg80211 wifi interface 303 * @net_device: the device to check 304 * 305 * Return: true if the net device is a cfg80211 wireless device, false 306 * otherwise. 307 */ 308 static bool batadv_is_cfg80211_netdev(struct net_device *net_device) 309 { 310 if (!net_device) 311 return false; 312 313 #if IS_ENABLED(CONFIG_CFG80211) 314 /* cfg80211 drivers have to set ieee80211_ptr */ 315 if (net_device->ieee80211_ptr) 316 return true; 317 #endif 318 319 return false; 320 } 321 322 /** 323 * batadv_wifi_flags_evaluate() - calculate wifi flags for net_device 324 * @net_device: the device to check 325 * 326 * Return: batadv_hard_iface_wifi_flags flags of the device 327 */ 328 static u32 batadv_wifi_flags_evaluate(struct net_device *net_device) 329 { 330 u32 wifi_flags = 0; 331 struct net_device *real_netdev; 332 333 if (batadv_is_wext_netdev(net_device)) 334 wifi_flags |= BATADV_HARDIF_WIFI_WEXT_DIRECT; 335 336 if (batadv_is_cfg80211_netdev(net_device)) 337 wifi_flags |= BATADV_HARDIF_WIFI_CFG80211_DIRECT; 338 339 real_netdev = batadv_get_real_netdevice(net_device); 340 if (!real_netdev) 341 return wifi_flags; 342 343 if (real_netdev == net_device) 344 goto out; 345 346 if (batadv_is_wext_netdev(real_netdev)) 347 wifi_flags |= BATADV_HARDIF_WIFI_WEXT_INDIRECT; 348 349 if (batadv_is_cfg80211_netdev(real_netdev)) 350 wifi_flags |= BATADV_HARDIF_WIFI_CFG80211_INDIRECT; 351 352 out: 353 dev_put(real_netdev); 354 return wifi_flags; 355 } 356 357 /** 358 * batadv_is_cfg80211_hardif() - check if the given hardif is a cfg80211 wifi 359 * interface 360 * @hard_iface: the device to check 361 * 362 * Return: true if the net device is a cfg80211 wireless device, false 363 * otherwise. 364 */ 365 bool batadv_is_cfg80211_hardif(struct batadv_hard_iface *hard_iface) 366 { 367 u32 allowed_flags = 0; 368 369 allowed_flags |= BATADV_HARDIF_WIFI_CFG80211_DIRECT; 370 allowed_flags |= BATADV_HARDIF_WIFI_CFG80211_INDIRECT; 371 372 return !!(hard_iface->wifi_flags & allowed_flags); 373 } 374 375 /** 376 * batadv_is_wifi_hardif() - check if the given hardif is a wifi interface 377 * @hard_iface: the device to check 378 * 379 * Return: true if the net device is a 802.11 wireless device, false otherwise. 380 */ 381 bool batadv_is_wifi_hardif(struct batadv_hard_iface *hard_iface) 382 { 383 if (!hard_iface) 384 return false; 385 386 return hard_iface->wifi_flags != 0; 387 } 388 389 /** 390 * batadv_hardif_no_broadcast() - check whether (re)broadcast is necessary 391 * @if_outgoing: the outgoing interface checked and considered for (re)broadcast 392 * @orig_addr: the originator of this packet 393 * @orig_neigh: originator address of the forwarder we just got the packet from 394 * (NULL if we originated) 395 * 396 * Checks whether a packet needs to be (re)broadcasted on the given interface. 397 * 398 * Return: 399 * BATADV_HARDIF_BCAST_NORECIPIENT: No neighbor on interface 400 * BATADV_HARDIF_BCAST_DUPFWD: Just one neighbor, but it is the forwarder 401 * BATADV_HARDIF_BCAST_DUPORIG: Just one neighbor, but it is the originator 402 * BATADV_HARDIF_BCAST_OK: Several neighbors, must broadcast 403 */ 404 int batadv_hardif_no_broadcast(struct batadv_hard_iface *if_outgoing, 405 u8 *orig_addr, u8 *orig_neigh) 406 { 407 struct batadv_hardif_neigh_node *hardif_neigh; 408 struct hlist_node *first; 409 int ret = BATADV_HARDIF_BCAST_OK; 410 411 rcu_read_lock(); 412 413 /* 0 neighbors -> no (re)broadcast */ 414 first = rcu_dereference(hlist_first_rcu(&if_outgoing->neigh_list)); 415 if (!first) { 416 ret = BATADV_HARDIF_BCAST_NORECIPIENT; 417 goto out; 418 } 419 420 /* >1 neighbors -> (re)broadcast */ 421 if (rcu_dereference(hlist_next_rcu(first))) 422 goto out; 423 424 hardif_neigh = hlist_entry(first, struct batadv_hardif_neigh_node, 425 list); 426 427 /* 1 neighbor, is the originator -> no rebroadcast */ 428 if (orig_addr && batadv_compare_eth(hardif_neigh->orig, orig_addr)) { 429 ret = BATADV_HARDIF_BCAST_DUPORIG; 430 /* 1 neighbor, is the one we received from -> no rebroadcast */ 431 } else if (orig_neigh && 432 batadv_compare_eth(hardif_neigh->orig, orig_neigh)) { 433 ret = BATADV_HARDIF_BCAST_DUPFWD; 434 } 435 436 out: 437 rcu_read_unlock(); 438 return ret; 439 } 440 441 static struct batadv_hard_iface * 442 batadv_hardif_get_active(struct net_device *mesh_iface) 443 { 444 struct batadv_hard_iface *hard_iface; 445 struct list_head *iter; 446 447 rcu_read_lock(); 448 netdev_for_each_lower_private_rcu(mesh_iface, hard_iface, iter) { 449 if (hard_iface->if_status == BATADV_IF_ACTIVE && 450 kref_get_unless_zero(&hard_iface->refcount)) 451 goto out; 452 } 453 454 hard_iface = NULL; 455 456 out: 457 rcu_read_unlock(); 458 return hard_iface; 459 } 460 461 static void batadv_primary_if_update_addr(struct batadv_priv *bat_priv, 462 struct batadv_hard_iface *oldif) 463 { 464 struct batadv_hard_iface *primary_if; 465 466 primary_if = batadv_primary_if_get_selected(bat_priv); 467 if (!primary_if) 468 goto out; 469 470 batadv_dat_init_own_addr(bat_priv, primary_if); 471 batadv_bla_update_orig_address(bat_priv, primary_if, oldif); 472 out: 473 batadv_hardif_put(primary_if); 474 } 475 476 static void batadv_primary_if_select(struct batadv_priv *bat_priv, 477 struct batadv_hard_iface *new_hard_iface) 478 { 479 struct batadv_hard_iface *curr_hard_iface; 480 481 ASSERT_RTNL(); 482 483 if (new_hard_iface) 484 kref_get(&new_hard_iface->refcount); 485 486 curr_hard_iface = rcu_replace_pointer(bat_priv->primary_if, 487 new_hard_iface, 1); 488 489 if (!new_hard_iface) 490 goto out; 491 492 bat_priv->algo_ops->iface.primary_set(new_hard_iface); 493 batadv_primary_if_update_addr(bat_priv, curr_hard_iface); 494 495 out: 496 batadv_hardif_put(curr_hard_iface); 497 } 498 499 static bool 500 batadv_hardif_is_iface_up(const struct batadv_hard_iface *hard_iface) 501 { 502 if (hard_iface->net_dev->flags & IFF_UP) 503 return true; 504 505 return false; 506 } 507 508 static void batadv_check_known_mac_addr(const struct batadv_hard_iface *hard_iface) 509 { 510 struct net_device *mesh_iface = hard_iface->mesh_iface; 511 const struct batadv_hard_iface *tmp_hard_iface; 512 struct list_head *iter; 513 514 if (!mesh_iface) 515 return; 516 517 netdev_for_each_lower_private(mesh_iface, tmp_hard_iface, iter) { 518 if (tmp_hard_iface == hard_iface) 519 continue; 520 521 if (tmp_hard_iface->if_status == BATADV_IF_NOT_IN_USE) 522 continue; 523 524 if (!batadv_compare_eth(tmp_hard_iface->net_dev->dev_addr, 525 hard_iface->net_dev->dev_addr)) 526 continue; 527 528 pr_warn("The newly added mac address (%pM) already exists on: %s\n", 529 hard_iface->net_dev->dev_addr, tmp_hard_iface->net_dev->name); 530 pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n"); 531 } 532 } 533 534 /** 535 * batadv_hardif_recalc_extra_skbroom() - Recalculate skbuff extra head/tailroom 536 * @mesh_iface: netdev struct of the mesh interface 537 */ 538 static void batadv_hardif_recalc_extra_skbroom(struct net_device *mesh_iface) 539 { 540 const struct batadv_hard_iface *hard_iface; 541 unsigned short lower_header_len = ETH_HLEN; 542 unsigned short lower_headroom = 0; 543 unsigned short lower_tailroom = 0; 544 unsigned short needed_headroom; 545 struct list_head *iter; 546 547 rcu_read_lock(); 548 netdev_for_each_lower_private_rcu(mesh_iface, hard_iface, iter) { 549 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE) 550 continue; 551 552 lower_header_len = max_t(unsigned short, lower_header_len, 553 hard_iface->net_dev->hard_header_len); 554 555 lower_headroom = max_t(unsigned short, lower_headroom, 556 hard_iface->net_dev->needed_headroom); 557 558 lower_tailroom = max_t(unsigned short, lower_tailroom, 559 hard_iface->net_dev->needed_tailroom); 560 } 561 rcu_read_unlock(); 562 563 needed_headroom = lower_headroom + (lower_header_len - ETH_HLEN); 564 needed_headroom += batadv_max_header_len(); 565 566 /* fragmentation headers don't strip the unicast/... header */ 567 needed_headroom += sizeof(struct batadv_frag_packet); 568 569 mesh_iface->needed_headroom = needed_headroom; 570 mesh_iface->needed_tailroom = lower_tailroom; 571 } 572 573 /** 574 * batadv_hardif_min_mtu() - Calculate maximum MTU for mesh interface 575 * @mesh_iface: netdev struct of the mesh interface 576 * 577 * Return: MTU for the mesh-interface (limited by the minimal MTU of all active 578 * slave interfaces) 579 */ 580 int batadv_hardif_min_mtu(struct net_device *mesh_iface) 581 { 582 struct batadv_priv *bat_priv = netdev_priv(mesh_iface); 583 const struct batadv_hard_iface *hard_iface; 584 struct list_head *iter; 585 int min_mtu = INT_MAX; 586 587 rcu_read_lock(); 588 netdev_for_each_lower_private_rcu(mesh_iface, hard_iface, iter) { 589 if (hard_iface->if_status != BATADV_IF_ACTIVE && 590 hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED) 591 continue; 592 593 min_mtu = min_t(int, hard_iface->net_dev->mtu, min_mtu); 594 } 595 rcu_read_unlock(); 596 597 if (atomic_read(&bat_priv->fragmentation) == 0) 598 goto out; 599 600 /* with fragmentation enabled the maximum size of internally generated 601 * packets such as translation table exchanges or tvlv containers, etc 602 * has to be calculated 603 */ 604 min_mtu = min_t(int, min_mtu, BATADV_FRAG_MAX_FRAG_SIZE); 605 min_mtu -= sizeof(struct batadv_frag_packet); 606 min_mtu *= BATADV_FRAG_MAX_FRAGMENTS; 607 608 out: 609 /* report to the other components the maximum amount of bytes that 610 * batman-adv can send over the wire (without considering the payload 611 * overhead). For example, this value is used by TT to compute the 612 * maximum local table size 613 */ 614 atomic_set(&bat_priv->packet_size_max, min_mtu); 615 616 /* the real mesh-interface MTU is computed by removing the payload 617 * overhead from the maximum amount of bytes that was just computed. 618 */ 619 return min_t(int, min_mtu - batadv_max_header_len(), BATADV_MAX_MTU); 620 } 621 622 /** 623 * batadv_update_min_mtu() - Adjusts the MTU if a new interface with a smaller 624 * MTU appeared 625 * @mesh_iface: netdev struct of the mesh interface 626 */ 627 void batadv_update_min_mtu(struct net_device *mesh_iface) 628 { 629 struct batadv_priv *bat_priv = netdev_priv(mesh_iface); 630 int limit_mtu; 631 int mtu; 632 633 mtu = batadv_hardif_min_mtu(mesh_iface); 634 635 if (bat_priv->mtu_set_by_user) 636 limit_mtu = bat_priv->mtu_set_by_user; 637 else 638 limit_mtu = ETH_DATA_LEN; 639 640 mtu = min(mtu, limit_mtu); 641 dev_set_mtu(mesh_iface, mtu); 642 643 /* Check if the local translate table should be cleaned up to match a 644 * new (and smaller) MTU. 645 */ 646 batadv_tt_local_resize_to_mtu(mesh_iface); 647 } 648 649 static void 650 batadv_hardif_activate_interface(struct batadv_hard_iface *hard_iface) 651 { 652 struct batadv_priv *bat_priv; 653 struct batadv_hard_iface *primary_if = NULL; 654 655 if (hard_iface->if_status != BATADV_IF_INACTIVE) 656 goto out; 657 658 bat_priv = netdev_priv(hard_iface->mesh_iface); 659 660 bat_priv->algo_ops->iface.update_mac(hard_iface); 661 hard_iface->if_status = BATADV_IF_TO_BE_ACTIVATED; 662 663 /* the first active interface becomes our primary interface or 664 * the next active interface after the old primary interface was removed 665 */ 666 primary_if = batadv_primary_if_get_selected(bat_priv); 667 if (!primary_if) 668 batadv_primary_if_select(bat_priv, hard_iface); 669 670 batadv_info(hard_iface->mesh_iface, "Interface activated: %s\n", 671 hard_iface->net_dev->name); 672 673 batadv_update_min_mtu(hard_iface->mesh_iface); 674 675 if (bat_priv->algo_ops->iface.activate) 676 bat_priv->algo_ops->iface.activate(hard_iface); 677 678 out: 679 batadv_hardif_put(primary_if); 680 } 681 682 static void 683 batadv_hardif_deactivate_interface(struct batadv_hard_iface *hard_iface) 684 { 685 if (hard_iface->if_status != BATADV_IF_ACTIVE && 686 hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED) 687 return; 688 689 hard_iface->if_status = BATADV_IF_INACTIVE; 690 691 batadv_info(hard_iface->mesh_iface, "Interface deactivated: %s\n", 692 hard_iface->net_dev->name); 693 694 batadv_update_min_mtu(hard_iface->mesh_iface); 695 } 696 697 /** 698 * batadv_hardif_enable_interface() - Enslave hard interface to mesh interface 699 * @hard_iface: hard interface to add to mesh interface 700 * @mesh_iface: netdev struct of the mesh interface 701 * 702 * Return: 0 on success or negative error number in case of failure 703 */ 704 int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface, 705 struct net_device *mesh_iface) 706 { 707 struct batadv_priv *bat_priv; 708 __be16 ethertype = htons(ETH_P_BATMAN); 709 int max_header_len = batadv_max_header_len(); 710 unsigned int required_mtu; 711 unsigned int hardif_mtu; 712 int ret; 713 714 hardif_mtu = READ_ONCE(hard_iface->net_dev->mtu); 715 required_mtu = READ_ONCE(mesh_iface->mtu) + max_header_len; 716 717 if (hardif_mtu < ETH_MIN_MTU + max_header_len) 718 return -EINVAL; 719 720 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE) 721 goto out; 722 723 kref_get(&hard_iface->refcount); 724 725 netdev_hold(mesh_iface, &hard_iface->meshif_dev_tracker, GFP_ATOMIC); 726 hard_iface->mesh_iface = mesh_iface; 727 bat_priv = netdev_priv(hard_iface->mesh_iface); 728 729 ret = netdev_master_upper_dev_link(hard_iface->net_dev, 730 mesh_iface, hard_iface, NULL, NULL); 731 if (ret) 732 goto err_dev; 733 734 ret = bat_priv->algo_ops->iface.enable(hard_iface); 735 if (ret < 0) 736 goto err_upper; 737 738 hard_iface->if_status = BATADV_IF_INACTIVE; 739 740 kref_get(&hard_iface->refcount); 741 hard_iface->batman_adv_ptype.type = ethertype; 742 hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv; 743 hard_iface->batman_adv_ptype.dev = hard_iface->net_dev; 744 dev_add_pack(&hard_iface->batman_adv_ptype); 745 746 batadv_info(hard_iface->mesh_iface, "Adding interface: %s\n", 747 hard_iface->net_dev->name); 748 749 if (atomic_read(&bat_priv->fragmentation) && 750 hardif_mtu < required_mtu) 751 batadv_info(hard_iface->mesh_iface, 752 "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", 753 hard_iface->net_dev->name, hardif_mtu, 754 required_mtu); 755 756 if (!atomic_read(&bat_priv->fragmentation) && 757 hardif_mtu < required_mtu) 758 batadv_info(hard_iface->mesh_iface, 759 "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", 760 hard_iface->net_dev->name, hardif_mtu, 761 required_mtu); 762 763 batadv_check_known_mac_addr(hard_iface); 764 765 if (batadv_hardif_is_iface_up(hard_iface)) 766 batadv_hardif_activate_interface(hard_iface); 767 else 768 batadv_err(hard_iface->mesh_iface, 769 "Not using interface %s (retrying later): interface not active\n", 770 hard_iface->net_dev->name); 771 772 batadv_hardif_recalc_extra_skbroom(mesh_iface); 773 774 if (bat_priv->algo_ops->iface.enabled) 775 bat_priv->algo_ops->iface.enabled(hard_iface); 776 777 out: 778 return 0; 779 780 err_upper: 781 netdev_upper_dev_unlink(hard_iface->net_dev, mesh_iface); 782 err_dev: 783 hard_iface->mesh_iface = NULL; 784 netdev_put(mesh_iface, &hard_iface->meshif_dev_tracker); 785 batadv_hardif_put(hard_iface); 786 return ret; 787 } 788 789 /** 790 * batadv_hardif_cnt() - get number of interfaces enslaved to mesh interface 791 * @mesh_iface: mesh interface to check 792 * 793 * This function is only using RCU for locking - the result can therefore be 794 * off when another function is modifying the list at the same time. The 795 * caller can use the rtnl_lock to make sure that the count is accurate. 796 * 797 * Return: number of connected/enslaved hard interfaces 798 */ 799 static size_t batadv_hardif_cnt(struct net_device *mesh_iface) 800 { 801 struct batadv_hard_iface *hard_iface; 802 struct list_head *iter; 803 size_t count = 0; 804 805 rcu_read_lock(); 806 netdev_for_each_lower_private_rcu(mesh_iface, hard_iface, iter) 807 count++; 808 rcu_read_unlock(); 809 810 return count; 811 } 812 813 /** 814 * batadv_hardif_disable_interface() - Remove hard interface from mesh interface 815 * @hard_iface: hard interface to be removed 816 */ 817 void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface) 818 { 819 struct batadv_priv *bat_priv = netdev_priv(hard_iface->mesh_iface); 820 struct batadv_hard_iface *primary_if = NULL; 821 822 batadv_hardif_deactivate_interface(hard_iface); 823 824 if (hard_iface->if_status != BATADV_IF_INACTIVE) 825 goto out; 826 827 batadv_info(hard_iface->mesh_iface, "Removing interface: %s\n", 828 hard_iface->net_dev->name); 829 dev_remove_pack(&hard_iface->batman_adv_ptype); 830 batadv_hardif_put(hard_iface); 831 832 primary_if = batadv_primary_if_get_selected(bat_priv); 833 if (hard_iface == primary_if) { 834 struct batadv_hard_iface *new_if; 835 836 new_if = batadv_hardif_get_active(hard_iface->mesh_iface); 837 batadv_primary_if_select(bat_priv, new_if); 838 839 batadv_hardif_put(new_if); 840 } 841 842 bat_priv->algo_ops->iface.disable(hard_iface); 843 hard_iface->if_status = BATADV_IF_NOT_IN_USE; 844 845 /* delete all references to this hard_iface */ 846 batadv_purge_orig_ref(bat_priv); 847 batadv_purge_outstanding_packets(bat_priv, hard_iface); 848 netdev_put(hard_iface->mesh_iface, &hard_iface->meshif_dev_tracker); 849 850 netdev_upper_dev_unlink(hard_iface->net_dev, hard_iface->mesh_iface); 851 batadv_hardif_recalc_extra_skbroom(hard_iface->mesh_iface); 852 853 /* nobody uses this interface anymore */ 854 if (batadv_hardif_cnt(hard_iface->mesh_iface) <= 1) 855 batadv_gw_check_client_stop(bat_priv); 856 857 hard_iface->mesh_iface = NULL; 858 batadv_hardif_put(hard_iface); 859 860 out: 861 batadv_hardif_put(primary_if); 862 } 863 864 static struct batadv_hard_iface * 865 batadv_hardif_add_interface(struct net_device *net_dev) 866 { 867 struct batadv_hard_iface *hard_iface; 868 869 ASSERT_RTNL(); 870 871 if (!batadv_is_valid_iface(net_dev)) 872 return NULL; 873 874 hard_iface = kzalloc(sizeof(*hard_iface), GFP_ATOMIC); 875 if (!hard_iface) 876 return NULL; 877 878 netdev_hold(net_dev, &hard_iface->dev_tracker, GFP_ATOMIC); 879 hard_iface->net_dev = net_dev; 880 881 hard_iface->mesh_iface = NULL; 882 hard_iface->if_status = BATADV_IF_NOT_IN_USE; 883 884 INIT_LIST_HEAD(&hard_iface->list); 885 INIT_HLIST_HEAD(&hard_iface->neigh_list); 886 887 mutex_init(&hard_iface->bat_iv.ogm_buff_mutex); 888 spin_lock_init(&hard_iface->neigh_list_lock); 889 kref_init(&hard_iface->refcount); 890 891 hard_iface->num_bcasts = BATADV_NUM_BCASTS_DEFAULT; 892 hard_iface->wifi_flags = batadv_wifi_flags_evaluate(net_dev); 893 if (batadv_is_wifi_hardif(hard_iface)) 894 hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS; 895 896 atomic_set(&hard_iface->hop_penalty, 0); 897 898 batadv_v_hardif_init(hard_iface); 899 900 kref_get(&hard_iface->refcount); 901 list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list); 902 batadv_hardif_generation++; 903 904 return hard_iface; 905 } 906 907 static void batadv_hardif_remove_interface(struct batadv_hard_iface *hard_iface) 908 { 909 ASSERT_RTNL(); 910 911 /* first deactivate interface */ 912 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE) 913 batadv_hardif_disable_interface(hard_iface); 914 915 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE) 916 return; 917 918 hard_iface->if_status = BATADV_IF_TO_BE_REMOVED; 919 batadv_hardif_put(hard_iface); 920 } 921 922 /** 923 * batadv_hard_if_event_meshif() - Handle events for mesh interfaces 924 * @event: NETDEV_* event to handle 925 * @net_dev: net_device which generated an event 926 * 927 * Return: NOTIFY_* result 928 */ 929 static int batadv_hard_if_event_meshif(unsigned long event, 930 struct net_device *net_dev) 931 { 932 struct batadv_priv *bat_priv; 933 934 switch (event) { 935 case NETDEV_REGISTER: 936 bat_priv = netdev_priv(net_dev); 937 batadv_meshif_create_vlan(bat_priv, BATADV_NO_FLAGS); 938 break; 939 } 940 941 return NOTIFY_DONE; 942 } 943 944 static int batadv_hard_if_event(struct notifier_block *this, 945 unsigned long event, void *ptr) 946 { 947 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr); 948 struct batadv_hard_iface *hard_iface; 949 struct batadv_hard_iface *primary_if = NULL; 950 struct batadv_priv *bat_priv; 951 952 if (batadv_meshif_is_valid(net_dev)) 953 return batadv_hard_if_event_meshif(event, net_dev); 954 955 hard_iface = batadv_hardif_get_by_netdev(net_dev); 956 if (!hard_iface && (event == NETDEV_REGISTER || 957 event == NETDEV_POST_TYPE_CHANGE)) 958 hard_iface = batadv_hardif_add_interface(net_dev); 959 960 if (!hard_iface) 961 goto out; 962 963 switch (event) { 964 case NETDEV_UP: 965 batadv_hardif_activate_interface(hard_iface); 966 break; 967 case NETDEV_GOING_DOWN: 968 case NETDEV_DOWN: 969 batadv_hardif_deactivate_interface(hard_iface); 970 break; 971 case NETDEV_UNREGISTER: 972 case NETDEV_PRE_TYPE_CHANGE: 973 list_del_rcu(&hard_iface->list); 974 batadv_hardif_generation++; 975 976 batadv_hardif_remove_interface(hard_iface); 977 break; 978 case NETDEV_CHANGEMTU: 979 if (hard_iface->mesh_iface) 980 batadv_update_min_mtu(hard_iface->mesh_iface); 981 break; 982 case NETDEV_CHANGEADDR: 983 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE) 984 goto hardif_put; 985 986 batadv_check_known_mac_addr(hard_iface); 987 988 bat_priv = netdev_priv(hard_iface->mesh_iface); 989 bat_priv->algo_ops->iface.update_mac(hard_iface); 990 991 primary_if = batadv_primary_if_get_selected(bat_priv); 992 if (!primary_if) 993 goto hardif_put; 994 995 if (hard_iface == primary_if) 996 batadv_primary_if_update_addr(bat_priv, NULL); 997 break; 998 case NETDEV_CHANGEUPPER: 999 hard_iface->wifi_flags = batadv_wifi_flags_evaluate(net_dev); 1000 if (batadv_is_wifi_hardif(hard_iface)) 1001 hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS; 1002 break; 1003 default: 1004 break; 1005 } 1006 1007 hardif_put: 1008 batadv_hardif_put(hard_iface); 1009 out: 1010 batadv_hardif_put(primary_if); 1011 return NOTIFY_DONE; 1012 } 1013 1014 struct notifier_block batadv_hard_if_notifier = { 1015 .notifier_call = batadv_hard_if_event, 1016 }; 1017