1 /* Copyright (C) 2007-2013 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, write to the Free Software 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 17 * 02110-1301, USA 18 */ 19 20 #include "main.h" 21 #include "distributed-arp-table.h" 22 #include "hard-interface.h" 23 #include "soft-interface.h" 24 #include "send.h" 25 #include "translation-table.h" 26 #include "routing.h" 27 #include "sysfs.h" 28 #include "originator.h" 29 #include "hash.h" 30 #include "bridge_loop_avoidance.h" 31 32 #include <linux/if_arp.h> 33 #include <linux/if_ether.h> 34 35 void batadv_hardif_free_rcu(struct rcu_head *rcu) 36 { 37 struct batadv_hard_iface *hard_iface; 38 39 hard_iface = container_of(rcu, struct batadv_hard_iface, rcu); 40 dev_put(hard_iface->net_dev); 41 kfree(hard_iface); 42 } 43 44 struct batadv_hard_iface * 45 batadv_hardif_get_by_netdev(const struct net_device *net_dev) 46 { 47 struct batadv_hard_iface *hard_iface; 48 49 rcu_read_lock(); 50 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { 51 if (hard_iface->net_dev == net_dev && 52 atomic_inc_not_zero(&hard_iface->refcount)) 53 goto out; 54 } 55 56 hard_iface = NULL; 57 58 out: 59 rcu_read_unlock(); 60 return hard_iface; 61 } 62 63 /** 64 * batadv_is_on_batman_iface - check if a device is a batman iface descendant 65 * @net_dev: the device to check 66 * 67 * If the user creates any virtual device on top of a batman-adv interface, it 68 * is important to prevent this new interface to be used to create a new mesh 69 * network (this behaviour would lead to a batman-over-batman configuration). 70 * This function recursively checks all the fathers of the device passed as 71 * argument looking for a batman-adv soft interface. 72 * 73 * Returns true if the device is descendant of a batman-adv mesh interface (or 74 * if it is a batman-adv interface itself), false otherwise 75 */ 76 static bool batadv_is_on_batman_iface(const struct net_device *net_dev) 77 { 78 struct net_device *parent_dev; 79 bool ret; 80 81 /* check if this is a batman-adv mesh interface */ 82 if (batadv_softif_is_valid(net_dev)) 83 return true; 84 85 /* no more parents..stop recursion */ 86 if (net_dev->iflink == net_dev->ifindex) 87 return false; 88 89 /* recurse over the parent device */ 90 parent_dev = dev_get_by_index(&init_net, net_dev->iflink); 91 /* if we got a NULL parent_dev there is something broken.. */ 92 if (WARN(!parent_dev, "Cannot find parent device")) 93 return false; 94 95 ret = batadv_is_on_batman_iface(parent_dev); 96 97 if (parent_dev) 98 dev_put(parent_dev); 99 return ret; 100 } 101 102 static int batadv_is_valid_iface(const struct net_device *net_dev) 103 { 104 if (net_dev->flags & IFF_LOOPBACK) 105 return 0; 106 107 if (net_dev->type != ARPHRD_ETHER) 108 return 0; 109 110 if (net_dev->addr_len != ETH_ALEN) 111 return 0; 112 113 /* no batman over batman */ 114 if (batadv_is_on_batman_iface(net_dev)) 115 return 0; 116 117 return 1; 118 } 119 120 static struct batadv_hard_iface * 121 batadv_hardif_get_active(const struct net_device *soft_iface) 122 { 123 struct batadv_hard_iface *hard_iface; 124 125 rcu_read_lock(); 126 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { 127 if (hard_iface->soft_iface != soft_iface) 128 continue; 129 130 if (hard_iface->if_status == BATADV_IF_ACTIVE && 131 atomic_inc_not_zero(&hard_iface->refcount)) 132 goto out; 133 } 134 135 hard_iface = NULL; 136 137 out: 138 rcu_read_unlock(); 139 return hard_iface; 140 } 141 142 static void batadv_primary_if_update_addr(struct batadv_priv *bat_priv, 143 struct batadv_hard_iface *oldif) 144 { 145 struct batadv_vis_packet *vis_packet; 146 struct batadv_hard_iface *primary_if; 147 struct sk_buff *skb; 148 149 primary_if = batadv_primary_if_get_selected(bat_priv); 150 if (!primary_if) 151 goto out; 152 153 batadv_dat_init_own_addr(bat_priv, primary_if); 154 155 skb = bat_priv->vis.my_info->skb_packet; 156 vis_packet = (struct batadv_vis_packet *)skb->data; 157 memcpy(vis_packet->vis_orig, primary_if->net_dev->dev_addr, ETH_ALEN); 158 memcpy(vis_packet->sender_orig, 159 primary_if->net_dev->dev_addr, ETH_ALEN); 160 161 batadv_bla_update_orig_address(bat_priv, primary_if, oldif); 162 out: 163 if (primary_if) 164 batadv_hardif_free_ref(primary_if); 165 } 166 167 static void batadv_primary_if_select(struct batadv_priv *bat_priv, 168 struct batadv_hard_iface *new_hard_iface) 169 { 170 struct batadv_hard_iface *curr_hard_iface; 171 172 ASSERT_RTNL(); 173 174 if (new_hard_iface && !atomic_inc_not_zero(&new_hard_iface->refcount)) 175 new_hard_iface = NULL; 176 177 curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1); 178 rcu_assign_pointer(bat_priv->primary_if, new_hard_iface); 179 180 if (!new_hard_iface) 181 goto out; 182 183 bat_priv->bat_algo_ops->bat_primary_iface_set(new_hard_iface); 184 batadv_primary_if_update_addr(bat_priv, curr_hard_iface); 185 186 out: 187 if (curr_hard_iface) 188 batadv_hardif_free_ref(curr_hard_iface); 189 } 190 191 static bool 192 batadv_hardif_is_iface_up(const struct batadv_hard_iface *hard_iface) 193 { 194 if (hard_iface->net_dev->flags & IFF_UP) 195 return true; 196 197 return false; 198 } 199 200 static void batadv_check_known_mac_addr(const struct net_device *net_dev) 201 { 202 const struct batadv_hard_iface *hard_iface; 203 204 rcu_read_lock(); 205 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { 206 if ((hard_iface->if_status != BATADV_IF_ACTIVE) && 207 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED)) 208 continue; 209 210 if (hard_iface->net_dev == net_dev) 211 continue; 212 213 if (!batadv_compare_eth(hard_iface->net_dev->dev_addr, 214 net_dev->dev_addr)) 215 continue; 216 217 pr_warn("The newly added mac address (%pM) already exists on: %s\n", 218 net_dev->dev_addr, hard_iface->net_dev->name); 219 pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n"); 220 } 221 rcu_read_unlock(); 222 } 223 224 int batadv_hardif_min_mtu(struct net_device *soft_iface) 225 { 226 const struct batadv_priv *bat_priv = netdev_priv(soft_iface); 227 const struct batadv_hard_iface *hard_iface; 228 /* allow big frames if all devices are capable to do so 229 * (have MTU > 1500 + BAT_HEADER_LEN) 230 */ 231 int min_mtu = ETH_DATA_LEN; 232 233 if (atomic_read(&bat_priv->fragmentation)) 234 goto out; 235 236 rcu_read_lock(); 237 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { 238 if ((hard_iface->if_status != BATADV_IF_ACTIVE) && 239 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED)) 240 continue; 241 242 if (hard_iface->soft_iface != soft_iface) 243 continue; 244 245 min_mtu = min_t(int, 246 hard_iface->net_dev->mtu - BATADV_HEADER_LEN, 247 min_mtu); 248 } 249 rcu_read_unlock(); 250 out: 251 return min_mtu; 252 } 253 254 /* adjusts the MTU if a new interface with a smaller MTU appeared. */ 255 void batadv_update_min_mtu(struct net_device *soft_iface) 256 { 257 int min_mtu; 258 259 min_mtu = batadv_hardif_min_mtu(soft_iface); 260 if (soft_iface->mtu != min_mtu) 261 soft_iface->mtu = min_mtu; 262 } 263 264 static void 265 batadv_hardif_activate_interface(struct batadv_hard_iface *hard_iface) 266 { 267 struct batadv_priv *bat_priv; 268 struct batadv_hard_iface *primary_if = NULL; 269 270 if (hard_iface->if_status != BATADV_IF_INACTIVE) 271 goto out; 272 273 bat_priv = netdev_priv(hard_iface->soft_iface); 274 275 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface); 276 hard_iface->if_status = BATADV_IF_TO_BE_ACTIVATED; 277 278 /* the first active interface becomes our primary interface or 279 * the next active interface after the old primary interface was removed 280 */ 281 primary_if = batadv_primary_if_get_selected(bat_priv); 282 if (!primary_if) 283 batadv_primary_if_select(bat_priv, hard_iface); 284 285 batadv_info(hard_iface->soft_iface, "Interface activated: %s\n", 286 hard_iface->net_dev->name); 287 288 batadv_update_min_mtu(hard_iface->soft_iface); 289 290 out: 291 if (primary_if) 292 batadv_hardif_free_ref(primary_if); 293 } 294 295 static void 296 batadv_hardif_deactivate_interface(struct batadv_hard_iface *hard_iface) 297 { 298 if ((hard_iface->if_status != BATADV_IF_ACTIVE) && 299 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED)) 300 return; 301 302 hard_iface->if_status = BATADV_IF_INACTIVE; 303 304 batadv_info(hard_iface->soft_iface, "Interface deactivated: %s\n", 305 hard_iface->net_dev->name); 306 307 batadv_update_min_mtu(hard_iface->soft_iface); 308 } 309 310 int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface, 311 const char *iface_name) 312 { 313 struct batadv_priv *bat_priv; 314 struct net_device *soft_iface; 315 __be16 ethertype = __constant_htons(ETH_P_BATMAN); 316 int ret; 317 318 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE) 319 goto out; 320 321 if (!atomic_inc_not_zero(&hard_iface->refcount)) 322 goto out; 323 324 /* hard-interface is part of a bridge */ 325 if (hard_iface->net_dev->priv_flags & IFF_BRIDGE_PORT) 326 pr_err("You are about to enable batman-adv on '%s' which already is part of a bridge. Unless you know exactly what you are doing this is probably wrong and won't work the way you think it would.\n", 327 hard_iface->net_dev->name); 328 329 soft_iface = dev_get_by_name(&init_net, iface_name); 330 331 if (!soft_iface) { 332 soft_iface = batadv_softif_create(iface_name); 333 334 if (!soft_iface) { 335 ret = -ENOMEM; 336 goto err; 337 } 338 339 /* dev_get_by_name() increases the reference counter for us */ 340 dev_hold(soft_iface); 341 } 342 343 if (!batadv_softif_is_valid(soft_iface)) { 344 pr_err("Can't create batman mesh interface %s: already exists as regular interface\n", 345 soft_iface->name); 346 ret = -EINVAL; 347 goto err_dev; 348 } 349 350 hard_iface->soft_iface = soft_iface; 351 bat_priv = netdev_priv(hard_iface->soft_iface); 352 353 ret = bat_priv->bat_algo_ops->bat_iface_enable(hard_iface); 354 if (ret < 0) 355 goto err_dev; 356 357 hard_iface->if_num = bat_priv->num_ifaces; 358 bat_priv->num_ifaces++; 359 hard_iface->if_status = BATADV_IF_INACTIVE; 360 ret = batadv_orig_hash_add_if(hard_iface, bat_priv->num_ifaces); 361 if (ret < 0) { 362 bat_priv->bat_algo_ops->bat_iface_disable(hard_iface); 363 bat_priv->num_ifaces--; 364 hard_iface->if_status = BATADV_IF_NOT_IN_USE; 365 goto err_dev; 366 } 367 368 hard_iface->batman_adv_ptype.type = ethertype; 369 hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv; 370 hard_iface->batman_adv_ptype.dev = hard_iface->net_dev; 371 dev_add_pack(&hard_iface->batman_adv_ptype); 372 373 atomic_set(&hard_iface->frag_seqno, 1); 374 batadv_info(hard_iface->soft_iface, "Adding interface: %s\n", 375 hard_iface->net_dev->name); 376 377 if (atomic_read(&bat_priv->fragmentation) && 378 hard_iface->net_dev->mtu < ETH_DATA_LEN + BATADV_HEADER_LEN) 379 batadv_info(hard_iface->soft_iface, 380 "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 %zi would solve the problem.\n", 381 hard_iface->net_dev->name, hard_iface->net_dev->mtu, 382 ETH_DATA_LEN + BATADV_HEADER_LEN); 383 384 if (!atomic_read(&bat_priv->fragmentation) && 385 hard_iface->net_dev->mtu < ETH_DATA_LEN + BATADV_HEADER_LEN) 386 batadv_info(hard_iface->soft_iface, 387 "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 %zi.\n", 388 hard_iface->net_dev->name, hard_iface->net_dev->mtu, 389 ETH_DATA_LEN + BATADV_HEADER_LEN); 390 391 if (batadv_hardif_is_iface_up(hard_iface)) 392 batadv_hardif_activate_interface(hard_iface); 393 else 394 batadv_err(hard_iface->soft_iface, 395 "Not using interface %s (retrying later): interface not active\n", 396 hard_iface->net_dev->name); 397 398 /* begin scheduling originator messages on that interface */ 399 batadv_schedule_bat_ogm(hard_iface); 400 401 out: 402 return 0; 403 404 err_dev: 405 dev_put(soft_iface); 406 err: 407 batadv_hardif_free_ref(hard_iface); 408 return ret; 409 } 410 411 void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface) 412 { 413 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); 414 struct batadv_hard_iface *primary_if = NULL; 415 416 if (hard_iface->if_status == BATADV_IF_ACTIVE) 417 batadv_hardif_deactivate_interface(hard_iface); 418 419 if (hard_iface->if_status != BATADV_IF_INACTIVE) 420 goto out; 421 422 batadv_info(hard_iface->soft_iface, "Removing interface: %s\n", 423 hard_iface->net_dev->name); 424 dev_remove_pack(&hard_iface->batman_adv_ptype); 425 426 bat_priv->num_ifaces--; 427 batadv_orig_hash_del_if(hard_iface, bat_priv->num_ifaces); 428 429 primary_if = batadv_primary_if_get_selected(bat_priv); 430 if (hard_iface == primary_if) { 431 struct batadv_hard_iface *new_if; 432 433 new_if = batadv_hardif_get_active(hard_iface->soft_iface); 434 batadv_primary_if_select(bat_priv, new_if); 435 436 if (new_if) 437 batadv_hardif_free_ref(new_if); 438 } 439 440 bat_priv->bat_algo_ops->bat_iface_disable(hard_iface); 441 hard_iface->if_status = BATADV_IF_NOT_IN_USE; 442 443 /* delete all references to this hard_iface */ 444 batadv_purge_orig_ref(bat_priv); 445 batadv_purge_outstanding_packets(bat_priv, hard_iface); 446 dev_put(hard_iface->soft_iface); 447 448 /* nobody uses this interface anymore */ 449 if (!bat_priv->num_ifaces) 450 batadv_softif_destroy(hard_iface->soft_iface); 451 452 hard_iface->soft_iface = NULL; 453 batadv_hardif_free_ref(hard_iface); 454 455 out: 456 if (primary_if) 457 batadv_hardif_free_ref(primary_if); 458 } 459 460 /** 461 * batadv_hardif_remove_interface_finish - cleans up the remains of a hardif 462 * @work: work queue item 463 * 464 * Free the parts of the hard interface which can not be removed under 465 * rtnl lock (to prevent deadlock situations). 466 */ 467 static void batadv_hardif_remove_interface_finish(struct work_struct *work) 468 { 469 struct batadv_hard_iface *hard_iface; 470 471 hard_iface = container_of(work, struct batadv_hard_iface, 472 cleanup_work); 473 474 batadv_sysfs_del_hardif(&hard_iface->hardif_obj); 475 batadv_hardif_free_ref(hard_iface); 476 } 477 478 static struct batadv_hard_iface * 479 batadv_hardif_add_interface(struct net_device *net_dev) 480 { 481 struct batadv_hard_iface *hard_iface; 482 int ret; 483 484 ASSERT_RTNL(); 485 486 ret = batadv_is_valid_iface(net_dev); 487 if (ret != 1) 488 goto out; 489 490 dev_hold(net_dev); 491 492 hard_iface = kmalloc(sizeof(*hard_iface), GFP_ATOMIC); 493 if (!hard_iface) 494 goto release_dev; 495 496 ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev); 497 if (ret) 498 goto free_if; 499 500 hard_iface->if_num = -1; 501 hard_iface->net_dev = net_dev; 502 hard_iface->soft_iface = NULL; 503 hard_iface->if_status = BATADV_IF_NOT_IN_USE; 504 INIT_LIST_HEAD(&hard_iface->list); 505 INIT_WORK(&hard_iface->cleanup_work, 506 batadv_hardif_remove_interface_finish); 507 508 /* extra reference for return */ 509 atomic_set(&hard_iface->refcount, 2); 510 511 batadv_check_known_mac_addr(hard_iface->net_dev); 512 list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list); 513 514 /* This can't be called via a bat_priv callback because 515 * we have no bat_priv yet. 516 */ 517 atomic_set(&hard_iface->bat_iv.ogm_seqno, 1); 518 hard_iface->bat_iv.ogm_buff = NULL; 519 520 return hard_iface; 521 522 free_if: 523 kfree(hard_iface); 524 release_dev: 525 dev_put(net_dev); 526 out: 527 return NULL; 528 } 529 530 static void batadv_hardif_remove_interface(struct batadv_hard_iface *hard_iface) 531 { 532 ASSERT_RTNL(); 533 534 /* first deactivate interface */ 535 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE) 536 batadv_hardif_disable_interface(hard_iface); 537 538 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE) 539 return; 540 541 hard_iface->if_status = BATADV_IF_TO_BE_REMOVED; 542 queue_work(batadv_event_workqueue, &hard_iface->cleanup_work); 543 } 544 545 void batadv_hardif_remove_interfaces(void) 546 { 547 struct batadv_hard_iface *hard_iface, *hard_iface_tmp; 548 549 rtnl_lock(); 550 list_for_each_entry_safe(hard_iface, hard_iface_tmp, 551 &batadv_hardif_list, list) { 552 list_del_rcu(&hard_iface->list); 553 batadv_hardif_remove_interface(hard_iface); 554 } 555 rtnl_unlock(); 556 } 557 558 static int batadv_hard_if_event(struct notifier_block *this, 559 unsigned long event, void *ptr) 560 { 561 struct net_device *net_dev = ptr; 562 struct batadv_hard_iface *hard_iface; 563 struct batadv_hard_iface *primary_if = NULL; 564 struct batadv_priv *bat_priv; 565 566 hard_iface = batadv_hardif_get_by_netdev(net_dev); 567 if (!hard_iface && event == NETDEV_REGISTER) 568 hard_iface = batadv_hardif_add_interface(net_dev); 569 570 if (!hard_iface) 571 goto out; 572 573 switch (event) { 574 case NETDEV_UP: 575 batadv_hardif_activate_interface(hard_iface); 576 break; 577 case NETDEV_GOING_DOWN: 578 case NETDEV_DOWN: 579 batadv_hardif_deactivate_interface(hard_iface); 580 break; 581 case NETDEV_UNREGISTER: 582 list_del_rcu(&hard_iface->list); 583 584 batadv_hardif_remove_interface(hard_iface); 585 break; 586 case NETDEV_CHANGEMTU: 587 if (hard_iface->soft_iface) 588 batadv_update_min_mtu(hard_iface->soft_iface); 589 break; 590 case NETDEV_CHANGEADDR: 591 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE) 592 goto hardif_put; 593 594 batadv_check_known_mac_addr(hard_iface->net_dev); 595 596 bat_priv = netdev_priv(hard_iface->soft_iface); 597 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface); 598 599 primary_if = batadv_primary_if_get_selected(bat_priv); 600 if (!primary_if) 601 goto hardif_put; 602 603 if (hard_iface == primary_if) 604 batadv_primary_if_update_addr(bat_priv, NULL); 605 break; 606 default: 607 break; 608 } 609 610 hardif_put: 611 batadv_hardif_free_ref(hard_iface); 612 out: 613 if (primary_if) 614 batadv_hardif_free_ref(primary_if); 615 return NOTIFY_DONE; 616 } 617 618 /* This function returns true if the interface represented by ifindex is a 619 * 802.11 wireless device 620 */ 621 bool batadv_is_wifi_iface(int ifindex) 622 { 623 struct net_device *net_device = NULL; 624 bool ret = false; 625 626 if (ifindex == BATADV_NULL_IFINDEX) 627 goto out; 628 629 net_device = dev_get_by_index(&init_net, ifindex); 630 if (!net_device) 631 goto out; 632 633 #ifdef CONFIG_WIRELESS_EXT 634 /* pre-cfg80211 drivers have to implement WEXT, so it is possible to 635 * check for wireless_handlers != NULL 636 */ 637 if (net_device->wireless_handlers) 638 ret = true; 639 else 640 #endif 641 /* cfg80211 drivers have to set ieee80211_ptr */ 642 if (net_device->ieee80211_ptr) 643 ret = true; 644 out: 645 if (net_device) 646 dev_put(net_device); 647 return ret; 648 } 649 650 struct notifier_block batadv_hard_if_notifier = { 651 .notifier_call = batadv_hard_if_event, 652 }; 653