linux/net/batman-adv/vis.c
<<
>>
Prefs
   1/* Copyright (C) 2008-2012 B.A.T.M.A.N. contributors:
   2 *
   3 * 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 "send.h"
  22#include "translation-table.h"
  23#include "vis.h"
  24#include "soft-interface.h"
  25#include "hard-interface.h"
  26#include "hash.h"
  27#include "originator.h"
  28
  29#define BATADV_MAX_VIS_PACKET_SIZE 1000
  30
  31static void batadv_start_vis_timer(struct batadv_priv *bat_priv);
  32
  33/* free the info */
  34static void batadv_free_info(struct kref *ref)
  35{
  36        struct batadv_vis_info *info;
  37        struct batadv_priv *bat_priv;
  38        struct batadv_recvlist_node *entry, *tmp;
  39
  40        info = container_of(ref, struct batadv_vis_info, refcount);
  41        bat_priv = info->bat_priv;
  42
  43        list_del_init(&info->send_list);
  44        spin_lock_bh(&bat_priv->vis_list_lock);
  45        list_for_each_entry_safe(entry, tmp, &info->recv_list, list) {
  46                list_del(&entry->list);
  47                kfree(entry);
  48        }
  49
  50        spin_unlock_bh(&bat_priv->vis_list_lock);
  51        kfree_skb(info->skb_packet);
  52        kfree(info);
  53}
  54
  55/* Compare two vis packets, used by the hashing algorithm */
  56static int batadv_vis_info_cmp(const struct hlist_node *node, const void *data2)
  57{
  58        const struct batadv_vis_info *d1, *d2;
  59        const struct batadv_vis_packet *p1, *p2;
  60
  61        d1 = container_of(node, struct batadv_vis_info, hash_entry);
  62        d2 = data2;
  63        p1 = (struct batadv_vis_packet *)d1->skb_packet->data;
  64        p2 = (struct batadv_vis_packet *)d2->skb_packet->data;
  65        return batadv_compare_eth(p1->vis_orig, p2->vis_orig);
  66}
  67
  68/* hash function to choose an entry in a hash table of given size
  69 * hash algorithm from http://en.wikipedia.org/wiki/Hash_table
  70 */
  71static uint32_t batadv_vis_info_choose(const void *data, uint32_t size)
  72{
  73        const struct batadv_vis_info *vis_info = data;
  74        const struct batadv_vis_packet *packet;
  75        const unsigned char *key;
  76        uint32_t hash = 0;
  77        size_t i;
  78
  79        packet = (struct batadv_vis_packet *)vis_info->skb_packet->data;
  80        key = packet->vis_orig;
  81        for (i = 0; i < ETH_ALEN; i++) {
  82                hash += key[i];
  83                hash += (hash << 10);
  84                hash ^= (hash >> 6);
  85        }
  86
  87        hash += (hash << 3);
  88        hash ^= (hash >> 11);
  89        hash += (hash << 15);
  90
  91        return hash % size;
  92}
  93
  94static struct batadv_vis_info *
  95batadv_vis_hash_find(struct batadv_priv *bat_priv, const void *data)
  96{
  97        struct batadv_hashtable *hash = bat_priv->vis_hash;
  98        struct hlist_head *head;
  99        struct hlist_node *node;
 100        struct batadv_vis_info *vis_info, *vis_info_tmp = NULL;
 101        uint32_t index;
 102
 103        if (!hash)
 104                return NULL;
 105
 106        index = batadv_vis_info_choose(data, hash->size);
 107        head = &hash->table[index];
 108
 109        rcu_read_lock();
 110        hlist_for_each_entry_rcu(vis_info, node, head, hash_entry) {
 111                if (!batadv_vis_info_cmp(node, data))
 112                        continue;
 113
 114                vis_info_tmp = vis_info;
 115                break;
 116        }
 117        rcu_read_unlock();
 118
 119        return vis_info_tmp;
 120}
 121
 122/* insert interface to the list of interfaces of one originator, if it
 123 * does not already exist in the list
 124 */
 125static void batadv_vis_data_insert_interface(const uint8_t *interface,
 126                                             struct hlist_head *if_list,
 127                                             bool primary)
 128{
 129        struct batadv_if_list_entry *entry;
 130        struct hlist_node *pos;
 131
 132        hlist_for_each_entry(entry, pos, if_list, list) {
 133                if (batadv_compare_eth(entry->addr, interface))
 134                        return;
 135        }
 136
 137        /* it's a new address, add it to the list */
 138        entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
 139        if (!entry)
 140                return;
 141        memcpy(entry->addr, interface, ETH_ALEN);
 142        entry->primary = primary;
 143        hlist_add_head(&entry->list, if_list);
 144}
 145
 146static void batadv_vis_data_read_prim_sec(struct seq_file *seq,
 147                                          const struct hlist_head *if_list)
 148{
 149        struct batadv_if_list_entry *entry;
 150        struct hlist_node *pos;
 151
 152        hlist_for_each_entry(entry, pos, if_list, list) {
 153                if (entry->primary)
 154                        seq_printf(seq, "PRIMARY, ");
 155                else
 156                        seq_printf(seq,  "SEC %pM, ", entry->addr);
 157        }
 158}
 159
 160/* read an entry  */
 161static ssize_t
 162batadv_vis_data_read_entry(struct seq_file *seq,
 163                           const struct batadv_vis_info_entry *entry,
 164                           const uint8_t *src, bool primary)
 165{
 166        if (primary && entry->quality == 0)
 167                return seq_printf(seq, "TT %pM, ", entry->dest);
 168        else if (batadv_compare_eth(entry->src, src))
 169                return seq_printf(seq, "TQ %pM %d, ", entry->dest,
 170                                  entry->quality);
 171
 172        return 0;
 173}
 174
 175static void
 176batadv_vis_data_insert_interfaces(struct hlist_head *list,
 177                                  struct batadv_vis_packet *packet,
 178                                  struct batadv_vis_info_entry *entries)
 179{
 180        int i;
 181
 182        for (i = 0; i < packet->entries; i++) {
 183                if (entries[i].quality == 0)
 184                        continue;
 185
 186                if (batadv_compare_eth(entries[i].src, packet->vis_orig))
 187                        continue;
 188
 189                batadv_vis_data_insert_interface(entries[i].src, list, false);
 190        }
 191}
 192
 193static void batadv_vis_data_read_entries(struct seq_file *seq,
 194                                         struct hlist_head *list,
 195                                         struct batadv_vis_packet *packet,
 196                                         struct batadv_vis_info_entry *entries)
 197{
 198        int i;
 199        struct batadv_if_list_entry *entry;
 200        struct hlist_node *pos;
 201
 202        hlist_for_each_entry(entry, pos, list, list) {
 203                seq_printf(seq, "%pM,", entry->addr);
 204
 205                for (i = 0; i < packet->entries; i++)
 206                        batadv_vis_data_read_entry(seq, &entries[i],
 207                                                   entry->addr, entry->primary);
 208
 209                /* add primary/secondary records */
 210                if (batadv_compare_eth(entry->addr, packet->vis_orig))
 211                        batadv_vis_data_read_prim_sec(seq, list);
 212
 213                seq_printf(seq, "\n");
 214        }
 215}
 216
 217static void batadv_vis_seq_print_text_bucket(struct seq_file *seq,
 218                                             const struct hlist_head *head)
 219{
 220        struct hlist_node *node;
 221        struct batadv_vis_info *info;
 222        struct batadv_vis_packet *packet;
 223        uint8_t *entries_pos;
 224        struct batadv_vis_info_entry *entries;
 225        struct batadv_if_list_entry *entry;
 226        struct hlist_node *pos, *n;
 227
 228        HLIST_HEAD(vis_if_list);
 229
 230        hlist_for_each_entry_rcu(info, node, head, hash_entry) {
 231                packet = (struct batadv_vis_packet *)info->skb_packet->data;
 232                entries_pos = (uint8_t *)packet + sizeof(*packet);
 233                entries = (struct batadv_vis_info_entry *)entries_pos;
 234
 235                batadv_vis_data_insert_interface(packet->vis_orig, &vis_if_list,
 236                                                 true);
 237                batadv_vis_data_insert_interfaces(&vis_if_list, packet,
 238                                                  entries);
 239                batadv_vis_data_read_entries(seq, &vis_if_list, packet,
 240                                             entries);
 241
 242                hlist_for_each_entry_safe(entry, pos, n, &vis_if_list, list) {
 243                        hlist_del(&entry->list);
 244                        kfree(entry);
 245                }
 246        }
 247}
 248
 249int batadv_vis_seq_print_text(struct seq_file *seq, void *offset)
 250{
 251        struct batadv_hard_iface *primary_if;
 252        struct hlist_head *head;
 253        struct net_device *net_dev = (struct net_device *)seq->private;
 254        struct batadv_priv *bat_priv = netdev_priv(net_dev);
 255        struct batadv_hashtable *hash = bat_priv->vis_hash;
 256        uint32_t i;
 257        int ret = 0;
 258        int vis_server = atomic_read(&bat_priv->vis_mode);
 259
 260        primary_if = batadv_primary_if_get_selected(bat_priv);
 261        if (!primary_if)
 262                goto out;
 263
 264        if (vis_server == BATADV_VIS_TYPE_CLIENT_UPDATE)
 265                goto out;
 266
 267        spin_lock_bh(&bat_priv->vis_hash_lock);
 268        for (i = 0; i < hash->size; i++) {
 269                head = &hash->table[i];
 270                batadv_vis_seq_print_text_bucket(seq, head);
 271        }
 272        spin_unlock_bh(&bat_priv->vis_hash_lock);
 273
 274out:
 275        if (primary_if)
 276                batadv_hardif_free_ref(primary_if);
 277        return ret;
 278}
 279
 280/* add the info packet to the send list, if it was not
 281 * already linked in.
 282 */
 283static void batadv_send_list_add(struct batadv_priv *bat_priv,
 284                                 struct batadv_vis_info *info)
 285{
 286        if (list_empty(&info->send_list)) {
 287                kref_get(&info->refcount);
 288                list_add_tail(&info->send_list, &bat_priv->vis_send_list);
 289        }
 290}
 291
 292/* delete the info packet from the send list, if it was
 293 * linked in.
 294 */
 295static void batadv_send_list_del(struct batadv_vis_info *info)
 296{
 297        if (!list_empty(&info->send_list)) {
 298                list_del_init(&info->send_list);
 299                kref_put(&info->refcount, batadv_free_info);
 300        }
 301}
 302
 303/* tries to add one entry to the receive list. */
 304static void batadv_recv_list_add(struct batadv_priv *bat_priv,
 305                                 struct list_head *recv_list, const char *mac)
 306{
 307        struct batadv_recvlist_node *entry;
 308
 309        entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
 310        if (!entry)
 311                return;
 312
 313        memcpy(entry->mac, mac, ETH_ALEN);
 314        spin_lock_bh(&bat_priv->vis_list_lock);
 315        list_add_tail(&entry->list, recv_list);
 316        spin_unlock_bh(&bat_priv->vis_list_lock);
 317}
 318
 319/* returns 1 if this mac is in the recv_list */
 320static int batadv_recv_list_is_in(struct batadv_priv *bat_priv,
 321                                  const struct list_head *recv_list,
 322                                  const char *mac)
 323{
 324        const struct batadv_recvlist_node *entry;
 325
 326        spin_lock_bh(&bat_priv->vis_list_lock);
 327        list_for_each_entry(entry, recv_list, list) {
 328                if (batadv_compare_eth(entry->mac, mac)) {
 329                        spin_unlock_bh(&bat_priv->vis_list_lock);
 330                        return 1;
 331                }
 332        }
 333        spin_unlock_bh(&bat_priv->vis_list_lock);
 334        return 0;
 335}
 336
 337/* try to add the packet to the vis_hash. return NULL if invalid (e.g. too old,
 338 * broken.. ).  vis hash must be locked outside.  is_new is set when the packet
 339 * is newer than old entries in the hash.
 340 */
 341static struct batadv_vis_info *
 342batadv_add_packet(struct batadv_priv *bat_priv,
 343                  struct batadv_vis_packet *vis_packet, int vis_info_len,
 344                  int *is_new, int make_broadcast)
 345{
 346        struct batadv_vis_info *info, *old_info;
 347        struct batadv_vis_packet *search_packet, *old_packet;
 348        struct batadv_vis_info search_elem;
 349        struct batadv_vis_packet *packet;
 350        struct sk_buff *tmp_skb;
 351        int hash_added;
 352        size_t len;
 353        size_t max_entries;
 354
 355        *is_new = 0;
 356        /* sanity check */
 357        if (!bat_priv->vis_hash)
 358                return NULL;
 359
 360        /* see if the packet is already in vis_hash */
 361        search_elem.skb_packet = dev_alloc_skb(sizeof(*search_packet));
 362        if (!search_elem.skb_packet)
 363                return NULL;
 364        len = sizeof(*search_packet);
 365        tmp_skb = search_elem.skb_packet;
 366        search_packet = (struct batadv_vis_packet *)skb_put(tmp_skb, len);
 367
 368        memcpy(search_packet->vis_orig, vis_packet->vis_orig, ETH_ALEN);
 369        old_info = batadv_vis_hash_find(bat_priv, &search_elem);
 370        kfree_skb(search_elem.skb_packet);
 371
 372        if (old_info) {
 373                tmp_skb = old_info->skb_packet;
 374                old_packet = (struct batadv_vis_packet *)tmp_skb->data;
 375                if (!batadv_seq_after(ntohl(vis_packet->seqno),
 376                                      ntohl(old_packet->seqno))) {
 377                        if (old_packet->seqno == vis_packet->seqno) {
 378                                batadv_recv_list_add(bat_priv,
 379                                                     &old_info->recv_list,
 380                                                     vis_packet->sender_orig);
 381                                return old_info;
 382                        } else {
 383                                /* newer packet is already in hash. */
 384                                return NULL;
 385                        }
 386                }
 387                /* remove old entry */
 388                batadv_hash_remove(bat_priv->vis_hash, batadv_vis_info_cmp,
 389                                   batadv_vis_info_choose, old_info);
 390                batadv_send_list_del(old_info);
 391                kref_put(&old_info->refcount, batadv_free_info);
 392        }
 393
 394        info = kmalloc(sizeof(*info), GFP_ATOMIC);
 395        if (!info)
 396                return NULL;
 397
 398        len = sizeof(*packet) + vis_info_len;
 399        info->skb_packet = dev_alloc_skb(len + ETH_HLEN);
 400        if (!info->skb_packet) {
 401                kfree(info);
 402                return NULL;
 403        }
 404        skb_reserve(info->skb_packet, ETH_HLEN);
 405        packet = (struct batadv_vis_packet *)skb_put(info->skb_packet, len);
 406
 407        kref_init(&info->refcount);
 408        INIT_LIST_HEAD(&info->send_list);
 409        INIT_LIST_HEAD(&info->recv_list);
 410        info->first_seen = jiffies;
 411        info->bat_priv = bat_priv;
 412        memcpy(packet, vis_packet, len);
 413
 414        /* initialize and add new packet. */
 415        *is_new = 1;
 416
 417        /* Make it a broadcast packet, if required */
 418        if (make_broadcast)
 419                memcpy(packet->target_orig, batadv_broadcast_addr, ETH_ALEN);
 420
 421        /* repair if entries is longer than packet. */
 422        max_entries = vis_info_len / sizeof(struct batadv_vis_info_entry);
 423        if (packet->entries > max_entries)
 424                packet->entries = max_entries;
 425
 426        batadv_recv_list_add(bat_priv, &info->recv_list, packet->sender_orig);
 427
 428        /* try to add it */
 429        hash_added = batadv_hash_add(bat_priv->vis_hash, batadv_vis_info_cmp,
 430                                     batadv_vis_info_choose, info,
 431                                     &info->hash_entry);
 432        if (hash_added != 0) {
 433                /* did not work (for some reason) */
 434                kref_put(&info->refcount, batadv_free_info);
 435                info = NULL;
 436        }
 437
 438        return info;
 439}
 440
 441/* handle the server sync packet, forward if needed. */
 442void batadv_receive_server_sync_packet(struct batadv_priv *bat_priv,
 443                                       struct batadv_vis_packet *vis_packet,
 444                                       int vis_info_len)
 445{
 446        struct batadv_vis_info *info;
 447        int is_new, make_broadcast;
 448        int vis_server = atomic_read(&bat_priv->vis_mode);
 449
 450        make_broadcast = (vis_server == BATADV_VIS_TYPE_SERVER_SYNC);
 451
 452        spin_lock_bh(&bat_priv->vis_hash_lock);
 453        info = batadv_add_packet(bat_priv, vis_packet, vis_info_len,
 454                                 &is_new, make_broadcast);
 455        if (!info)
 456                goto end;
 457
 458        /* only if we are server ourselves and packet is newer than the one in
 459         * hash.
 460         */
 461        if (vis_server == BATADV_VIS_TYPE_SERVER_SYNC && is_new)
 462                batadv_send_list_add(bat_priv, info);
 463end:
 464        spin_unlock_bh(&bat_priv->vis_hash_lock);
 465}
 466
 467/* handle an incoming client update packet and schedule forward if needed. */
 468void batadv_receive_client_update_packet(struct batadv_priv *bat_priv,
 469                                         struct batadv_vis_packet *vis_packet,
 470                                         int vis_info_len)
 471{
 472        struct batadv_vis_info *info;
 473        struct batadv_vis_packet *packet;
 474        int is_new;
 475        int vis_server = atomic_read(&bat_priv->vis_mode);
 476        int are_target = 0;
 477
 478        /* clients shall not broadcast. */
 479        if (is_broadcast_ether_addr(vis_packet->target_orig))
 480                return;
 481
 482        /* Are we the target for this VIS packet? */
 483        if (vis_server == BATADV_VIS_TYPE_SERVER_SYNC   &&
 484            batadv_is_my_mac(vis_packet->target_orig))
 485                are_target = 1;
 486
 487        spin_lock_bh(&bat_priv->vis_hash_lock);
 488        info = batadv_add_packet(bat_priv, vis_packet, vis_info_len,
 489                                 &is_new, are_target);
 490
 491        if (!info)
 492                goto end;
 493        /* note that outdated packets will be dropped at this point. */
 494
 495        packet = (struct batadv_vis_packet *)info->skb_packet->data;
 496
 497        /* send only if we're the target server or ... */
 498        if (are_target && is_new) {
 499                packet->vis_type = BATADV_VIS_TYPE_SERVER_SYNC; /* upgrade! */
 500                batadv_send_list_add(bat_priv, info);
 501
 502                /* ... we're not the recipient (and thus need to forward). */
 503        } else if (!batadv_is_my_mac(packet->target_orig)) {
 504                batadv_send_list_add(bat_priv, info);
 505        }
 506
 507end:
 508        spin_unlock_bh(&bat_priv->vis_hash_lock);
 509}
 510
 511/* Walk the originators and find the VIS server with the best tq. Set the packet
 512 * address to its address and return the best_tq.
 513 *
 514 * Must be called with the originator hash locked
 515 */
 516static int batadv_find_best_vis_server(struct batadv_priv *bat_priv,
 517                                       struct batadv_vis_info *info)
 518{
 519        struct batadv_hashtable *hash = bat_priv->orig_hash;
 520        struct batadv_neigh_node *router;
 521        struct hlist_node *node;
 522        struct hlist_head *head;
 523        struct batadv_orig_node *orig_node;
 524        struct batadv_vis_packet *packet;
 525        int best_tq = -1;
 526        uint32_t i;
 527
 528        packet = (struct batadv_vis_packet *)info->skb_packet->data;
 529
 530        for (i = 0; i < hash->size; i++) {
 531                head = &hash->table[i];
 532
 533                rcu_read_lock();
 534                hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
 535                        router = batadv_orig_node_get_router(orig_node);
 536                        if (!router)
 537                                continue;
 538
 539                        if ((orig_node->flags & BATADV_VIS_SERVER) &&
 540                            (router->tq_avg > best_tq)) {
 541                                best_tq = router->tq_avg;
 542                                memcpy(packet->target_orig, orig_node->orig,
 543                                       ETH_ALEN);
 544                        }
 545                        batadv_neigh_node_free_ref(router);
 546                }
 547                rcu_read_unlock();
 548        }
 549
 550        return best_tq;
 551}
 552
 553/* Return true if the vis packet is full. */
 554static bool batadv_vis_packet_full(const struct batadv_vis_info *info)
 555{
 556        const struct batadv_vis_packet *packet;
 557        size_t num;
 558
 559        packet = (struct batadv_vis_packet *)info->skb_packet->data;
 560        num = BATADV_MAX_VIS_PACKET_SIZE / sizeof(struct batadv_vis_info_entry);
 561
 562        if (num < packet->entries + 1)
 563                return true;
 564        return false;
 565}
 566
 567/* generates a packet of own vis data,
 568 * returns 0 on success, -1 if no packet could be generated
 569 */
 570static int batadv_generate_vis_packet(struct batadv_priv *bat_priv)
 571{
 572        struct batadv_hashtable *hash = bat_priv->orig_hash;
 573        struct hlist_node *node;
 574        struct hlist_head *head;
 575        struct batadv_orig_node *orig_node;
 576        struct batadv_neigh_node *router;
 577        struct batadv_vis_info *info = bat_priv->my_vis_info;
 578        struct batadv_vis_packet *packet;
 579        struct batadv_vis_info_entry *entry;
 580        struct batadv_tt_common_entry *tt_common_entry;
 581        int best_tq = -1;
 582        uint32_t i;
 583
 584        info->first_seen = jiffies;
 585        packet = (struct batadv_vis_packet *)info->skb_packet->data;
 586        packet->vis_type = atomic_read(&bat_priv->vis_mode);
 587
 588        memcpy(packet->target_orig, batadv_broadcast_addr, ETH_ALEN);
 589        packet->header.ttl = BATADV_TTL;
 590        packet->seqno = htonl(ntohl(packet->seqno) + 1);
 591        packet->entries = 0;
 592        packet->reserved = 0;
 593        skb_trim(info->skb_packet, sizeof(*packet));
 594
 595        if (packet->vis_type == BATADV_VIS_TYPE_CLIENT_UPDATE) {
 596                best_tq = batadv_find_best_vis_server(bat_priv, info);
 597
 598                if (best_tq < 0)
 599                        return best_tq;
 600        }
 601
 602        for (i = 0; i < hash->size; i++) {
 603                head = &hash->table[i];
 604
 605                rcu_read_lock();
 606                hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
 607                        router = batadv_orig_node_get_router(orig_node);
 608                        if (!router)
 609                                continue;
 610
 611                        if (!batadv_compare_eth(router->addr, orig_node->orig))
 612                                goto next;
 613
 614                        if (router->if_incoming->if_status != BATADV_IF_ACTIVE)
 615                                goto next;
 616
 617                        if (router->tq_avg < 1)
 618                                goto next;
 619
 620                        /* fill one entry into buffer. */
 621                        entry = (struct batadv_vis_info_entry *)
 622                                      skb_put(info->skb_packet, sizeof(*entry));
 623                        memcpy(entry->src,
 624                               router->if_incoming->net_dev->dev_addr,
 625                               ETH_ALEN);
 626                        memcpy(entry->dest, orig_node->orig, ETH_ALEN);
 627                        entry->quality = router->tq_avg;
 628                        packet->entries++;
 629
 630next:
 631                        batadv_neigh_node_free_ref(router);
 632
 633                        if (batadv_vis_packet_full(info))
 634                                goto unlock;
 635                }
 636                rcu_read_unlock();
 637        }
 638
 639        hash = bat_priv->tt_local_hash;
 640
 641        for (i = 0; i < hash->size; i++) {
 642                head = &hash->table[i];
 643
 644                rcu_read_lock();
 645                hlist_for_each_entry_rcu(tt_common_entry, node, head,
 646                                         hash_entry) {
 647                        entry = (struct batadv_vis_info_entry *)
 648                                        skb_put(info->skb_packet,
 649                                                sizeof(*entry));
 650                        memset(entry->src, 0, ETH_ALEN);
 651                        memcpy(entry->dest, tt_common_entry->addr, ETH_ALEN);
 652                        entry->quality = 0; /* 0 means TT */
 653                        packet->entries++;
 654
 655                        if (batadv_vis_packet_full(info))
 656                                goto unlock;
 657                }
 658                rcu_read_unlock();
 659        }
 660
 661        return 0;
 662
 663unlock:
 664        rcu_read_unlock();
 665        return 0;
 666}
 667
 668/* free old vis packets. Must be called with this vis_hash_lock
 669 * held
 670 */
 671static void batadv_purge_vis_packets(struct batadv_priv *bat_priv)
 672{
 673        uint32_t i;
 674        struct batadv_hashtable *hash = bat_priv->vis_hash;
 675        struct hlist_node *node, *node_tmp;
 676        struct hlist_head *head;
 677        struct batadv_vis_info *info;
 678
 679        for (i = 0; i < hash->size; i++) {
 680                head = &hash->table[i];
 681
 682                hlist_for_each_entry_safe(info, node, node_tmp,
 683                                          head, hash_entry) {
 684                        /* never purge own data. */
 685                        if (info == bat_priv->my_vis_info)
 686                                continue;
 687
 688                        if (batadv_has_timed_out(info->first_seen,
 689                                                 BATADV_VIS_TIMEOUT)) {
 690                                hlist_del(node);
 691                                batadv_send_list_del(info);
 692                                kref_put(&info->refcount, batadv_free_info);
 693                        }
 694                }
 695        }
 696}
 697
 698static void batadv_broadcast_vis_packet(struct batadv_priv *bat_priv,
 699                                        struct batadv_vis_info *info)
 700{
 701        struct batadv_neigh_node *router;
 702        struct batadv_hashtable *hash = bat_priv->orig_hash;
 703        struct hlist_node *node;
 704        struct hlist_head *head;
 705        struct batadv_orig_node *orig_node;
 706        struct batadv_vis_packet *packet;
 707        struct sk_buff *skb;
 708        struct batadv_hard_iface *hard_iface;
 709        uint8_t dstaddr[ETH_ALEN];
 710        uint32_t i;
 711
 712
 713        packet = (struct batadv_vis_packet *)info->skb_packet->data;
 714
 715        /* send to all routers in range. */
 716        for (i = 0; i < hash->size; i++) {
 717                head = &hash->table[i];
 718
 719                rcu_read_lock();
 720                hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
 721                        /* if it's a vis server and reachable, send it. */
 722                        if (!(orig_node->flags & BATADV_VIS_SERVER))
 723                                continue;
 724
 725                        router = batadv_orig_node_get_router(orig_node);
 726                        if (!router)
 727                                continue;
 728
 729                        /* don't send it if we already received the packet from
 730                         * this node.
 731                         */
 732                        if (batadv_recv_list_is_in(bat_priv, &info->recv_list,
 733                                                   orig_node->orig)) {
 734                                batadv_neigh_node_free_ref(router);
 735                                continue;
 736                        }
 737
 738                        memcpy(packet->target_orig, orig_node->orig, ETH_ALEN);
 739                        hard_iface = router->if_incoming;
 740                        memcpy(dstaddr, router->addr, ETH_ALEN);
 741
 742                        batadv_neigh_node_free_ref(router);
 743
 744                        skb = skb_clone(info->skb_packet, GFP_ATOMIC);
 745                        if (skb)
 746                                batadv_send_skb_packet(skb, hard_iface,
 747                                                       dstaddr);
 748
 749                }
 750                rcu_read_unlock();
 751        }
 752}
 753
 754static void batadv_unicast_vis_packet(struct batadv_priv *bat_priv,
 755                                      struct batadv_vis_info *info)
 756{
 757        struct batadv_orig_node *orig_node;
 758        struct batadv_neigh_node *router = NULL;
 759        struct sk_buff *skb;
 760        struct batadv_vis_packet *packet;
 761
 762        packet = (struct batadv_vis_packet *)info->skb_packet->data;
 763
 764        orig_node = batadv_orig_hash_find(bat_priv, packet->target_orig);
 765        if (!orig_node)
 766                goto out;
 767
 768        router = batadv_orig_node_get_router(orig_node);
 769        if (!router)
 770                goto out;
 771
 772        skb = skb_clone(info->skb_packet, GFP_ATOMIC);
 773        if (skb)
 774                batadv_send_skb_packet(skb, router->if_incoming, router->addr);
 775
 776out:
 777        if (router)
 778                batadv_neigh_node_free_ref(router);
 779        if (orig_node)
 780                batadv_orig_node_free_ref(orig_node);
 781}
 782
 783/* only send one vis packet. called from batadv_send_vis_packets() */
 784static void batadv_send_vis_packet(struct batadv_priv *bat_priv,
 785                                   struct batadv_vis_info *info)
 786{
 787        struct batadv_hard_iface *primary_if;
 788        struct batadv_vis_packet *packet;
 789
 790        primary_if = batadv_primary_if_get_selected(bat_priv);
 791        if (!primary_if)
 792                goto out;
 793
 794        packet = (struct batadv_vis_packet *)info->skb_packet->data;
 795        if (packet->header.ttl < 2) {
 796                pr_debug("Error - can't send vis packet: ttl exceeded\n");
 797                goto out;
 798        }
 799
 800        memcpy(packet->sender_orig, primary_if->net_dev->dev_addr, ETH_ALEN);
 801        packet->header.ttl--;
 802
 803        if (is_broadcast_ether_addr(packet->target_orig))
 804                batadv_broadcast_vis_packet(bat_priv, info);
 805        else
 806                batadv_unicast_vis_packet(bat_priv, info);
 807        packet->header.ttl++; /* restore TTL */
 808
 809out:
 810        if (primary_if)
 811                batadv_hardif_free_ref(primary_if);
 812}
 813
 814/* called from timer; send (and maybe generate) vis packet. */
 815static void batadv_send_vis_packets(struct work_struct *work)
 816{
 817        struct delayed_work *delayed_work =
 818                container_of(work, struct delayed_work, work);
 819        struct batadv_priv *bat_priv;
 820        struct batadv_vis_info *info;
 821
 822        bat_priv = container_of(delayed_work, struct batadv_priv, vis_work);
 823        spin_lock_bh(&bat_priv->vis_hash_lock);
 824        batadv_purge_vis_packets(bat_priv);
 825
 826        if (batadv_generate_vis_packet(bat_priv) == 0) {
 827                /* schedule if generation was successful */
 828                batadv_send_list_add(bat_priv, bat_priv->my_vis_info);
 829        }
 830
 831        while (!list_empty(&bat_priv->vis_send_list)) {
 832                info = list_first_entry(&bat_priv->vis_send_list,
 833                                        typeof(*info), send_list);
 834
 835                kref_get(&info->refcount);
 836                spin_unlock_bh(&bat_priv->vis_hash_lock);
 837
 838                batadv_send_vis_packet(bat_priv, info);
 839
 840                spin_lock_bh(&bat_priv->vis_hash_lock);
 841                batadv_send_list_del(info);
 842                kref_put(&info->refcount, batadv_free_info);
 843        }
 844        spin_unlock_bh(&bat_priv->vis_hash_lock);
 845        batadv_start_vis_timer(bat_priv);
 846}
 847
 848/* init the vis server. this may only be called when if_list is already
 849 * initialized (e.g. bat0 is initialized, interfaces have been added)
 850 */
 851int batadv_vis_init(struct batadv_priv *bat_priv)
 852{
 853        struct batadv_vis_packet *packet;
 854        int hash_added;
 855        unsigned int len;
 856        unsigned long first_seen;
 857        struct sk_buff *tmp_skb;
 858
 859        if (bat_priv->vis_hash)
 860                return 0;
 861
 862        spin_lock_bh(&bat_priv->vis_hash_lock);
 863
 864        bat_priv->vis_hash = batadv_hash_new(256);
 865        if (!bat_priv->vis_hash) {
 866                pr_err("Can't initialize vis_hash\n");
 867                goto err;
 868        }
 869
 870        bat_priv->my_vis_info = kmalloc(BATADV_MAX_VIS_PACKET_SIZE, GFP_ATOMIC);
 871        if (!bat_priv->my_vis_info)
 872                goto err;
 873
 874        len = sizeof(*packet) + BATADV_MAX_VIS_PACKET_SIZE + ETH_HLEN;
 875        bat_priv->my_vis_info->skb_packet = dev_alloc_skb(len);
 876        if (!bat_priv->my_vis_info->skb_packet)
 877                goto free_info;
 878
 879        skb_reserve(bat_priv->my_vis_info->skb_packet, ETH_HLEN);
 880        tmp_skb = bat_priv->my_vis_info->skb_packet;
 881        packet = (struct batadv_vis_packet *)skb_put(tmp_skb, sizeof(*packet));
 882
 883        /* prefill the vis info */
 884        first_seen = jiffies - msecs_to_jiffies(BATADV_VIS_INTERVAL);
 885        bat_priv->my_vis_info->first_seen = first_seen;
 886        INIT_LIST_HEAD(&bat_priv->my_vis_info->recv_list);
 887        INIT_LIST_HEAD(&bat_priv->my_vis_info->send_list);
 888        kref_init(&bat_priv->my_vis_info->refcount);
 889        bat_priv->my_vis_info->bat_priv = bat_priv;
 890        packet->header.version = BATADV_COMPAT_VERSION;
 891        packet->header.packet_type = BATADV_VIS;
 892        packet->header.ttl = BATADV_TTL;
 893        packet->seqno = 0;
 894        packet->reserved = 0;
 895        packet->entries = 0;
 896
 897        INIT_LIST_HEAD(&bat_priv->vis_send_list);
 898
 899        hash_added = batadv_hash_add(bat_priv->vis_hash, batadv_vis_info_cmp,
 900                                     batadv_vis_info_choose,
 901                                     bat_priv->my_vis_info,
 902                                     &bat_priv->my_vis_info->hash_entry);
 903        if (hash_added != 0) {
 904                pr_err("Can't add own vis packet into hash\n");
 905                /* not in hash, need to remove it manually. */
 906                kref_put(&bat_priv->my_vis_info->refcount, batadv_free_info);
 907                goto err;
 908        }
 909
 910        spin_unlock_bh(&bat_priv->vis_hash_lock);
 911        batadv_start_vis_timer(bat_priv);
 912        return 0;
 913
 914free_info:
 915        kfree(bat_priv->my_vis_info);
 916        bat_priv->my_vis_info = NULL;
 917err:
 918        spin_unlock_bh(&bat_priv->vis_hash_lock);
 919        batadv_vis_quit(bat_priv);
 920        return -ENOMEM;
 921}
 922
 923/* Decrease the reference count on a hash item info */
 924static void batadv_free_info_ref(struct hlist_node *node, void *arg)
 925{
 926        struct batadv_vis_info *info;
 927
 928        info = container_of(node, struct batadv_vis_info, hash_entry);
 929        batadv_send_list_del(info);
 930        kref_put(&info->refcount, batadv_free_info);
 931}
 932
 933/* shutdown vis-server */
 934void batadv_vis_quit(struct batadv_priv *bat_priv)
 935{
 936        if (!bat_priv->vis_hash)
 937                return;
 938
 939        cancel_delayed_work_sync(&bat_priv->vis_work);
 940
 941        spin_lock_bh(&bat_priv->vis_hash_lock);
 942        /* properly remove, kill timers ... */
 943        batadv_hash_delete(bat_priv->vis_hash, batadv_free_info_ref, NULL);
 944        bat_priv->vis_hash = NULL;
 945        bat_priv->my_vis_info = NULL;
 946        spin_unlock_bh(&bat_priv->vis_hash_lock);
 947}
 948
 949/* schedule packets for (re)transmission */
 950static void batadv_start_vis_timer(struct batadv_priv *bat_priv)
 951{
 952        INIT_DELAYED_WORK(&bat_priv->vis_work, batadv_send_vis_packets);
 953        queue_delayed_work(batadv_event_workqueue, &bat_priv->vis_work,
 954                           msecs_to_jiffies(BATADV_VIS_INTERVAL));
 955}
 956
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.