1 /* 2 * Copyright (C) 2007-2011 B.A.T.M.A.N. contributors: 3 * 4 * Marek Lindner, Simon Wunderlich 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of version 2 of the GNU General Public 8 * License as published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, but 11 * WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 18 * 02110-1301, USA 19 * 20 */ 21 22 #include "main.h" 23 #include "soft-interface.h" 24 #include "hard-interface.h" 25 #include "routing.h" 26 #include "send.h" 27 #include "bat_debugfs.h" 28 #include "translation-table.h" 29 #include "hash.h" 30 #include "gateway_common.h" 31 #include "gateway_client.h" 32 #include "bat_sysfs.h" 33 #include <linux/slab.h> 34 #include <linux/ethtool.h> 35 #include <linux/etherdevice.h> 36 #include <linux/if_vlan.h> 37 #include "unicast.h" 38 39 40 static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd); 41 static void bat_get_drvinfo(struct net_device *dev, 42 struct ethtool_drvinfo *info); 43 static u32 bat_get_msglevel(struct net_device *dev); 44 static void bat_set_msglevel(struct net_device *dev, u32 value); 45 static u32 bat_get_link(struct net_device *dev); 46 static u32 bat_get_rx_csum(struct net_device *dev); 47 static int bat_set_rx_csum(struct net_device *dev, u32 data); 48 49 static const struct ethtool_ops bat_ethtool_ops = { 50 .get_settings = bat_get_settings, 51 .get_drvinfo = bat_get_drvinfo, 52 .get_msglevel = bat_get_msglevel, 53 .set_msglevel = bat_set_msglevel, 54 .get_link = bat_get_link, 55 .get_rx_csum = bat_get_rx_csum, 56 .set_rx_csum = bat_set_rx_csum 57 }; 58 59 int my_skb_head_push(struct sk_buff *skb, unsigned int len) 60 { 61 int result; 62 63 /** 64 * TODO: We must check if we can release all references to non-payload 65 * data using skb_header_release in our skbs to allow skb_cow_header to 66 * work optimally. This means that those skbs are not allowed to read 67 * or write any data which is before the current position of skb->data 68 * after that call and thus allow other skbs with the same data buffer 69 * to write freely in that area. 70 */ 71 result = skb_cow_head(skb, len); 72 if (result < 0) 73 return result; 74 75 skb_push(skb, len); 76 return 0; 77 } 78 79 static void softif_neigh_free_rcu(struct rcu_head *rcu) 80 { 81 struct softif_neigh *softif_neigh; 82 83 softif_neigh = container_of(rcu, struct softif_neigh, rcu); 84 kfree(softif_neigh); 85 } 86 87 static void softif_neigh_free_ref(struct softif_neigh *softif_neigh) 88 { 89 if (atomic_dec_and_test(&softif_neigh->refcount)) 90 call_rcu(&softif_neigh->rcu, softif_neigh_free_rcu); 91 } 92 93 void softif_neigh_purge(struct bat_priv *bat_priv) 94 { 95 struct softif_neigh *softif_neigh, *softif_neigh_tmp; 96 struct hlist_node *node, *node_tmp; 97 98 spin_lock_bh(&bat_priv->softif_neigh_lock); 99 100 hlist_for_each_entry_safe(softif_neigh, node, node_tmp, 101 &bat_priv->softif_neigh_list, list) { 102 103 if ((!time_after(jiffies, softif_neigh->last_seen + 104 msecs_to_jiffies(SOFTIF_NEIGH_TIMEOUT))) && 105 (atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE)) 106 continue; 107 108 hlist_del_rcu(&softif_neigh->list); 109 110 if (bat_priv->softif_neigh == softif_neigh) { 111 bat_dbg(DBG_ROUTES, bat_priv, 112 "Current mesh exit point '%pM' vanished " 113 "(vid: %d).\n", 114 softif_neigh->addr, softif_neigh->vid); 115 softif_neigh_tmp = bat_priv->softif_neigh; 116 bat_priv->softif_neigh = NULL; 117 softif_neigh_free_ref(softif_neigh_tmp); 118 } 119 120 softif_neigh_free_ref(softif_neigh); 121 } 122 123 spin_unlock_bh(&bat_priv->softif_neigh_lock); 124 } 125 126 static struct softif_neigh *softif_neigh_get(struct bat_priv *bat_priv, 127 uint8_t *addr, short vid) 128 { 129 struct softif_neigh *softif_neigh; 130 struct hlist_node *node; 131 132 rcu_read_lock(); 133 hlist_for_each_entry_rcu(softif_neigh, node, 134 &bat_priv->softif_neigh_list, list) { 135 if (!compare_eth(softif_neigh->addr, addr)) 136 continue; 137 138 if (softif_neigh->vid != vid) 139 continue; 140 141 if (!atomic_inc_not_zero(&softif_neigh->refcount)) 142 continue; 143 144 softif_neigh->last_seen = jiffies; 145 goto out; 146 } 147 148 softif_neigh = kzalloc(sizeof(struct softif_neigh), GFP_ATOMIC); 149 if (!softif_neigh) 150 goto out; 151 152 memcpy(softif_neigh->addr, addr, ETH_ALEN); 153 softif_neigh->vid = vid; 154 softif_neigh->last_seen = jiffies; 155 /* initialize with 2 - caller decrements counter by one */ 156 atomic_set(&softif_neigh->refcount, 2); 157 158 INIT_HLIST_NODE(&softif_neigh->list); 159 spin_lock_bh(&bat_priv->softif_neigh_lock); 160 hlist_add_head_rcu(&softif_neigh->list, &bat_priv->softif_neigh_list); 161 spin_unlock_bh(&bat_priv->softif_neigh_lock); 162 163 out: 164 rcu_read_unlock(); 165 return softif_neigh; 166 } 167 168 int softif_neigh_seq_print_text(struct seq_file *seq, void *offset) 169 { 170 struct net_device *net_dev = (struct net_device *)seq->private; 171 struct bat_priv *bat_priv = netdev_priv(net_dev); 172 struct softif_neigh *softif_neigh; 173 struct hlist_node *node; 174 175 if (!bat_priv->primary_if) { 176 return seq_printf(seq, "BATMAN mesh %s disabled - " 177 "please specify interfaces to enable it\n", 178 net_dev->name); 179 } 180 181 seq_printf(seq, "Softif neighbor list (%s)\n", net_dev->name); 182 183 rcu_read_lock(); 184 hlist_for_each_entry_rcu(softif_neigh, node, 185 &bat_priv->softif_neigh_list, list) 186 seq_printf(seq, "%s %pM (vid: %d)\n", 187 bat_priv->softif_neigh == softif_neigh 188 ? "=>" : " ", softif_neigh->addr, 189 softif_neigh->vid); 190 rcu_read_unlock(); 191 192 return 0; 193 } 194 195 static void softif_batman_recv(struct sk_buff *skb, struct net_device *dev, 196 short vid) 197 { 198 struct bat_priv *bat_priv = netdev_priv(dev); 199 struct ethhdr *ethhdr = (struct ethhdr *)skb->data; 200 struct batman_packet *batman_packet; 201 struct softif_neigh *softif_neigh, *softif_neigh_tmp; 202 203 if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) 204 batman_packet = (struct batman_packet *) 205 (skb->data + ETH_HLEN + VLAN_HLEN); 206 else 207 batman_packet = (struct batman_packet *)(skb->data + ETH_HLEN); 208 209 if (batman_packet->version != COMPAT_VERSION) 210 goto err; 211 212 if (batman_packet->packet_type != BAT_PACKET) 213 goto err; 214 215 if (!(batman_packet->flags & PRIMARIES_FIRST_HOP)) 216 goto err; 217 218 if (is_my_mac(batman_packet->orig)) 219 goto err; 220 221 softif_neigh = softif_neigh_get(bat_priv, batman_packet->orig, vid); 222 223 if (!softif_neigh) 224 goto err; 225 226 if (bat_priv->softif_neigh == softif_neigh) 227 goto out; 228 229 /* we got a neighbor but its mac is 'bigger' than ours */ 230 if (memcmp(bat_priv->primary_if->net_dev->dev_addr, 231 softif_neigh->addr, ETH_ALEN) < 0) 232 goto out; 233 234 /* switch to new 'smallest neighbor' */ 235 if ((bat_priv->softif_neigh) && 236 (memcmp(softif_neigh->addr, bat_priv->softif_neigh->addr, 237 ETH_ALEN) < 0)) { 238 bat_dbg(DBG_ROUTES, bat_priv, 239 "Changing mesh exit point from %pM (vid: %d) " 240 "to %pM (vid: %d).\n", 241 bat_priv->softif_neigh->addr, 242 bat_priv->softif_neigh->vid, 243 softif_neigh->addr, softif_neigh->vid); 244 softif_neigh_tmp = bat_priv->softif_neigh; 245 bat_priv->softif_neigh = softif_neigh; 246 softif_neigh_free_ref(softif_neigh_tmp); 247 /* we need to hold the additional reference */ 248 goto err; 249 } 250 251 /* close own batX device and use softif_neigh as exit node */ 252 if ((!bat_priv->softif_neigh) && 253 (memcmp(softif_neigh->addr, 254 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN) < 0)) { 255 bat_dbg(DBG_ROUTES, bat_priv, 256 "Setting mesh exit point to %pM (vid: %d).\n", 257 softif_neigh->addr, softif_neigh->vid); 258 bat_priv->softif_neigh = softif_neigh; 259 /* we need to hold the additional reference */ 260 goto err; 261 } 262 263 out: 264 softif_neigh_free_ref(softif_neigh); 265 err: 266 kfree_skb(skb); 267 return; 268 } 269 270 static int interface_open(struct net_device *dev) 271 { 272 netif_start_queue(dev); 273 return 0; 274 } 275 276 static int interface_release(struct net_device *dev) 277 { 278 netif_stop_queue(dev); 279 return 0; 280 } 281 282 static struct net_device_stats *interface_stats(struct net_device *dev) 283 { 284 struct bat_priv *bat_priv = netdev_priv(dev); 285 return &bat_priv->stats; 286 } 287 288 static int interface_set_mac_addr(struct net_device *dev, void *p) 289 { 290 struct bat_priv *bat_priv = netdev_priv(dev); 291 struct sockaddr *addr = p; 292 293 if (!is_valid_ether_addr(addr->sa_data)) 294 return -EADDRNOTAVAIL; 295 296 /* only modify hna-table if it has been initialised before */ 297 if (atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE) { 298 hna_local_remove(bat_priv, dev->dev_addr, 299 "mac address changed"); 300 hna_local_add(dev, addr->sa_data); 301 } 302 303 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN); 304 return 0; 305 } 306 307 static int interface_change_mtu(struct net_device *dev, int new_mtu) 308 { 309 /* check ranges */ 310 if ((new_mtu < 68) || (new_mtu > hardif_min_mtu(dev))) 311 return -EINVAL; 312 313 dev->mtu = new_mtu; 314 315 return 0; 316 } 317 318 int interface_tx(struct sk_buff *skb, struct net_device *soft_iface) 319 { 320 struct ethhdr *ethhdr = (struct ethhdr *)skb->data; 321 struct bat_priv *bat_priv = netdev_priv(soft_iface); 322 struct bcast_packet *bcast_packet; 323 struct vlan_ethhdr *vhdr; 324 int data_len = skb->len, ret; 325 short vid = -1; 326 bool do_bcast = false; 327 328 if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE) 329 goto dropped; 330 331 soft_iface->trans_start = jiffies; 332 333 switch (ntohs(ethhdr->h_proto)) { 334 case ETH_P_8021Q: 335 vhdr = (struct vlan_ethhdr *)skb->data; 336 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK; 337 338 if (ntohs(vhdr->h_vlan_encapsulated_proto) != ETH_P_BATMAN) 339 break; 340 341 /* fall through */ 342 case ETH_P_BATMAN: 343 softif_batman_recv(skb, soft_iface, vid); 344 goto end; 345 } 346 347 /** 348 * if we have a another chosen mesh exit node in range 349 * it will transport the packets to the mesh 350 */ 351 if ((bat_priv->softif_neigh) && (bat_priv->softif_neigh->vid == vid)) 352 goto dropped; 353 354 /* TODO: check this for locks */ 355 hna_local_add(soft_iface, ethhdr->h_source); 356 357 if (is_multicast_ether_addr(ethhdr->h_dest)) { 358 ret = gw_is_target(bat_priv, skb); 359 360 if (ret < 0) 361 goto dropped; 362 363 if (ret == 0) 364 do_bcast = true; 365 } 366 367 /* ethernet packet should be broadcasted */ 368 if (do_bcast) { 369 if (!bat_priv->primary_if) 370 goto dropped; 371 372 if (my_skb_head_push(skb, sizeof(struct bcast_packet)) < 0) 373 goto dropped; 374 375 bcast_packet = (struct bcast_packet *)skb->data; 376 bcast_packet->version = COMPAT_VERSION; 377 bcast_packet->ttl = TTL; 378 379 /* batman packet type: broadcast */ 380 bcast_packet->packet_type = BAT_BCAST; 381 382 /* hw address of first interface is the orig mac because only 383 * this mac is known throughout the mesh */ 384 memcpy(bcast_packet->orig, 385 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN); 386 387 /* set broadcast sequence number */ 388 bcast_packet->seqno = 389 htonl(atomic_inc_return(&bat_priv->bcast_seqno)); 390 391 add_bcast_packet_to_list(bat_priv, skb); 392 393 /* a copy is stored in the bcast list, therefore removing 394 * the original skb. */ 395 kfree_skb(skb); 396 397 /* unicast packet */ 398 } else { 399 ret = unicast_send_skb(skb, bat_priv); 400 if (ret != 0) 401 goto dropped_freed; 402 } 403 404 bat_priv->stats.tx_packets++; 405 bat_priv->stats.tx_bytes += data_len; 406 goto end; 407 408 dropped: 409 kfree_skb(skb); 410 dropped_freed: 411 bat_priv->stats.tx_dropped++; 412 end: 413 return NETDEV_TX_OK; 414 } 415 416 void interface_rx(struct net_device *soft_iface, 417 struct sk_buff *skb, struct hard_iface *recv_if, 418 int hdr_size) 419 { 420 struct bat_priv *bat_priv = netdev_priv(soft_iface); 421 struct unicast_packet *unicast_packet; 422 struct ethhdr *ethhdr; 423 struct vlan_ethhdr *vhdr; 424 short vid = -1; 425 int ret; 426 427 /* check if enough space is available for pulling, and pull */ 428 if (!pskb_may_pull(skb, hdr_size)) 429 goto dropped; 430 431 skb_pull_rcsum(skb, hdr_size); 432 skb_reset_mac_header(skb); 433 434 ethhdr = (struct ethhdr *)skb_mac_header(skb); 435 436 switch (ntohs(ethhdr->h_proto)) { 437 case ETH_P_8021Q: 438 vhdr = (struct vlan_ethhdr *)skb->data; 439 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK; 440 441 if (ntohs(vhdr->h_vlan_encapsulated_proto) != ETH_P_BATMAN) 442 break; 443 444 /* fall through */ 445 case ETH_P_BATMAN: 446 goto dropped; 447 } 448 449 /** 450 * if we have a another chosen mesh exit node in range 451 * it will transport the packets to the non-mesh network 452 */ 453 if ((bat_priv->softif_neigh) && (bat_priv->softif_neigh->vid == vid)) { 454 skb_push(skb, hdr_size); 455 unicast_packet = (struct unicast_packet *)skb->data; 456 457 if ((unicast_packet->packet_type != BAT_UNICAST) && 458 (unicast_packet->packet_type != BAT_UNICAST_FRAG)) 459 goto dropped; 460 461 skb_reset_mac_header(skb); 462 463 memcpy(unicast_packet->dest, 464 bat_priv->softif_neigh->addr, ETH_ALEN); 465 ret = route_unicast_packet(skb, recv_if); 466 if (ret == NET_RX_DROP) 467 goto dropped; 468 469 goto out; 470 } 471 472 /* skb->dev & skb->pkt_type are set here */ 473 if (unlikely(!pskb_may_pull(skb, ETH_HLEN))) 474 goto dropped; 475 skb->protocol = eth_type_trans(skb, soft_iface); 476 477 /* should not be neccesary anymore as we use skb_pull_rcsum() 478 * TODO: please verify this and remove this TODO 479 * -- Dec 21st 2009, Simon Wunderlich */ 480 481 /* skb->ip_summed = CHECKSUM_UNNECESSARY;*/ 482 483 bat_priv->stats.rx_packets++; 484 bat_priv->stats.rx_bytes += skb->len + sizeof(struct ethhdr); 485 486 soft_iface->last_rx = jiffies; 487 488 netif_rx(skb); 489 return; 490 491 dropped: 492 kfree_skb(skb); 493 out: 494 return; 495 } 496 497 #ifdef HAVE_NET_DEVICE_OPS 498 static const struct net_device_ops bat_netdev_ops = { 499 .ndo_open = interface_open, 500 .ndo_stop = interface_release, 501 .ndo_get_stats = interface_stats, 502 .ndo_set_mac_address = interface_set_mac_addr, 503 .ndo_change_mtu = interface_change_mtu, 504 .ndo_start_xmit = interface_tx, 505 .ndo_validate_addr = eth_validate_addr 506 }; 507 #endif 508 509 static void interface_setup(struct net_device *dev) 510 { 511 struct bat_priv *priv = netdev_priv(dev); 512 char dev_addr[ETH_ALEN]; 513 514 ether_setup(dev); 515 516 #ifdef HAVE_NET_DEVICE_OPS 517 dev->netdev_ops = &bat_netdev_ops; 518 #else 519 dev->open = interface_open; 520 dev->stop = interface_release; 521 dev->get_stats = interface_stats; 522 dev->set_mac_address = interface_set_mac_addr; 523 dev->change_mtu = interface_change_mtu; 524 dev->hard_start_xmit = interface_tx; 525 #endif 526 dev->destructor = free_netdev; 527 528 /** 529 * can't call min_mtu, because the needed variables 530 * have not been initialized yet 531 */ 532 dev->mtu = ETH_DATA_LEN; 533 dev->hard_header_len = BAT_HEADER_LEN; /* reserve more space in the 534 * skbuff for our header */ 535 536 /* generate random address */ 537 random_ether_addr(dev_addr); 538 memcpy(dev->dev_addr, dev_addr, ETH_ALEN); 539 540 SET_ETHTOOL_OPS(dev, &bat_ethtool_ops); 541 542 memset(priv, 0, sizeof(struct bat_priv)); 543 } 544 545 struct net_device *softif_create(char *name) 546 { 547 struct net_device *soft_iface; 548 struct bat_priv *bat_priv; 549 int ret; 550 551 soft_iface = alloc_netdev(sizeof(struct bat_priv) , name, 552 interface_setup); 553 554 if (!soft_iface) { 555 pr_err("Unable to allocate the batman interface: %s\n", name); 556 goto out; 557 } 558 559 ret = register_netdev(soft_iface); 560 if (ret < 0) { 561 pr_err("Unable to register the batman interface '%s': %i\n", 562 name, ret); 563 goto free_soft_iface; 564 } 565 566 bat_priv = netdev_priv(soft_iface); 567 568 atomic_set(&bat_priv->aggregated_ogms, 1); 569 atomic_set(&bat_priv->bonding, 0); 570 atomic_set(&bat_priv->vis_mode, VIS_TYPE_CLIENT_UPDATE); 571 atomic_set(&bat_priv->gw_mode, GW_MODE_OFF); 572 atomic_set(&bat_priv->gw_sel_class, 20); 573 atomic_set(&bat_priv->gw_bandwidth, 41); 574 atomic_set(&bat_priv->orig_interval, 1000); 575 atomic_set(&bat_priv->hop_penalty, 10); 576 atomic_set(&bat_priv->log_level, 0); 577 atomic_set(&bat_priv->fragmentation, 1); 578 atomic_set(&bat_priv->bcast_queue_left, BCAST_QUEUE_LEN); 579 atomic_set(&bat_priv->batman_queue_left, BATMAN_QUEUE_LEN); 580 581 atomic_set(&bat_priv->mesh_state, MESH_INACTIVE); 582 atomic_set(&bat_priv->bcast_seqno, 1); 583 atomic_set(&bat_priv->hna_local_changed, 0); 584 585 bat_priv->primary_if = NULL; 586 bat_priv->num_ifaces = 0; 587 bat_priv->softif_neigh = NULL; 588 589 ret = sysfs_add_meshif(soft_iface); 590 if (ret < 0) 591 goto unreg_soft_iface; 592 593 ret = debugfs_add_meshif(soft_iface); 594 if (ret < 0) 595 goto unreg_sysfs; 596 597 ret = mesh_init(soft_iface); 598 if (ret < 0) 599 goto unreg_debugfs; 600 601 return soft_iface; 602 603 unreg_debugfs: 604 debugfs_del_meshif(soft_iface); 605 unreg_sysfs: 606 sysfs_del_meshif(soft_iface); 607 unreg_soft_iface: 608 unregister_netdev(soft_iface); 609 return NULL; 610 611 free_soft_iface: 612 free_netdev(soft_iface); 613 out: 614 return NULL; 615 } 616 617 void softif_destroy(struct net_device *soft_iface) 618 { 619 debugfs_del_meshif(soft_iface); 620 sysfs_del_meshif(soft_iface); 621 mesh_free(soft_iface); 622 unregister_netdevice(soft_iface); 623 } 624 625 int softif_is_valid(struct net_device *net_dev) 626 { 627 #ifdef HAVE_NET_DEVICE_OPS 628 if (net_dev->netdev_ops->ndo_start_xmit == interface_tx) 629 return 1; 630 #else 631 if (net_dev->hard_start_xmit == interface_tx) 632 return 1; 633 #endif 634 635 return 0; 636 } 637 638 /* ethtool */ 639 static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) 640 { 641 cmd->supported = 0; 642 cmd->advertising = 0; 643 cmd->speed = SPEED_10; 644 cmd->duplex = DUPLEX_FULL; 645 cmd->port = PORT_TP; 646 cmd->phy_address = 0; 647 cmd->transceiver = XCVR_INTERNAL; 648 cmd->autoneg = AUTONEG_DISABLE; 649 cmd->maxtxpkt = 0; 650 cmd->maxrxpkt = 0; 651 652 return 0; 653 } 654 655 static void bat_get_drvinfo(struct net_device *dev, 656 struct ethtool_drvinfo *info) 657 { 658 strcpy(info->driver, "B.A.T.M.A.N. advanced"); 659 strcpy(info->version, SOURCE_VERSION); 660 strcpy(info->fw_version, "N/A"); 661 strcpy(info->bus_info, "batman"); 662 } 663 664 static u32 bat_get_msglevel(struct net_device *dev) 665 { 666 return -EOPNOTSUPP; 667 } 668 669 static void bat_set_msglevel(struct net_device *dev, u32 value) 670 { 671 } 672 673 static u32 bat_get_link(struct net_device *dev) 674 { 675 return 1; 676 } 677 678 static u32 bat_get_rx_csum(struct net_device *dev) 679 { 680 return 0; 681 } 682 683 static int bat_set_rx_csum(struct net_device *dev, u32 data) 684 { 685 return -EOPNOTSUPP; 686 } 687