2 * Copyright (C) 2008-2010 B.A.T.M.A.N. contributors:
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.
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.
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
24 #include "translation-table.h"
26 #include "soft-interface.h"
27 #include "hard-interface.h"
30 /* Returns the smallest signed integer in two's complement with the sizeof x */
31 #define smallest_signed_int(x) (1u << (7u + 8u * (sizeof(x) - 1u)))
33 /* Checks if a sequence number x is a predecessor/successor of y.
34 they handle overflows/underflows and can correctly check for a
35 predecessor/successor unless the variable sequence number has grown by
36 more then 2**(bitwidth(x)-1)-1.
37 This means that for a uint8_t with the maximum value 255, it would think:
38 * when adding nothing - it is neither a predecessor nor a successor
39 * before adding more than 127 to the starting value - it is a predecessor,
40 * when adding 128 - it is neither a predecessor nor a successor,
41 * after adding more than 127 to the starting value - it is a successor */
42 #define seq_before(x, y) ({typeof(x) _dummy = (x - y); \
43 _dummy > smallest_signed_int(_dummy); })
44 #define seq_after(x, y) seq_before(y, x)
46 struct hashtable_t *vis_hash;
47 DEFINE_SPINLOCK(vis_hash_lock);
48 static DEFINE_SPINLOCK(recv_list_lock);
49 static struct vis_info *my_vis_info;
50 static struct list_head send_list; /* always locked with vis_hash_lock */
52 static void start_vis_timer(void);
55 static void free_info(struct kref *ref)
57 struct vis_info *info = container_of(ref, struct vis_info, refcount);
58 struct recvlist_node *entry, *tmp;
61 list_del_init(&info->send_list);
62 spin_lock_irqsave(&recv_list_lock, flags);
63 list_for_each_entry_safe(entry, tmp, &info->recv_list, list) {
64 list_del(&entry->list);
67 spin_unlock_irqrestore(&recv_list_lock, flags);
71 /* Compare two vis packets, used by the hashing algorithm */
72 static int vis_info_cmp(void *data1, void *data2)
74 struct vis_info *d1, *d2;
77 return compare_orig(d1->packet.vis_orig, d2->packet.vis_orig);
80 /* hash function to choose an entry in a hash table of given size */
81 /* hash algorithm from http://en.wikipedia.org/wiki/Hash_table */
82 static int vis_info_choose(void *data, int size)
84 struct vis_info *vis_info = data;
89 key = vis_info->packet.vis_orig;
90 for (i = 0; i < ETH_ALEN; i++) {
103 /* insert interface to the list of interfaces of one originator, if it
104 * does not already exist in the list */
105 static void vis_data_insert_interface(const uint8_t *interface,
106 struct hlist_head *if_list,
109 struct if_list_entry *entry;
110 struct hlist_node *pos;
112 hlist_for_each_entry(entry, pos, if_list, list) {
113 if (compare_orig(entry->addr, (void *)interface))
117 /* its a new address, add it to the list */
118 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
121 memcpy(entry->addr, interface, ETH_ALEN);
122 entry->primary = primary;
123 hlist_add_head(&entry->list, if_list);
126 static ssize_t vis_data_read_prim_sec(char *buff, struct hlist_head *if_list)
128 struct if_list_entry *entry;
129 struct hlist_node *pos;
130 char tmp_addr_str[ETH_STR_LEN];
133 hlist_for_each_entry(entry, pos, if_list, list) {
135 len += sprintf(buff + len, "PRIMARY, ");
137 addr_to_string(tmp_addr_str, entry->addr);
138 len += sprintf(buff + len, "SEC %s, ", tmp_addr_str);
146 static ssize_t vis_data_read_entry(char *buff, struct vis_info_entry *entry,
147 uint8_t *src, bool primary)
151 addr_to_string(to, entry->dest);
152 if (primary && entry->quality == 0)
153 return sprintf(buff, "HNA %s, ", to);
154 else if (compare_orig(entry->src, src))
155 return sprintf(buff, "TQ %s %d, ", to, entry->quality);
160 ssize_t vis_fill_buffer_text(struct net_device *net_dev, char *buff,
161 size_t count, loff_t off)
164 struct vis_info *info;
165 struct vis_info_entry *entries;
166 struct bat_priv *bat_priv = netdev_priv(net_dev);
167 HLIST_HEAD(vis_if_list);
168 struct if_list_entry *entry;
169 struct hlist_node *pos, *n;
170 size_t hdr_len, tmp_len;
171 int i, bytes_written = 0;
172 char tmp_addr_str[ETH_STR_LEN];
174 int vis_server = atomic_read(&bat_priv->vis_mode);
176 if ((!bat_priv->primary_if) ||
177 (vis_server == VIS_TYPE_CLIENT_UPDATE))
182 spin_lock_irqsave(&vis_hash_lock, flags);
183 while (hash_iterate(vis_hash, &hashit)) {
184 info = hashit.bucket->data;
185 entries = (struct vis_info_entry *)
186 ((char *)info + sizeof(struct vis_info));
188 /* estimated line length */
189 if (count < bytes_written + 200)
192 for (i = 0; i < info->packet.entries; i++) {
193 if (entries[i].quality == 0)
195 vis_data_insert_interface(entries[i].src, &vis_if_list,
196 compare_orig(entries[i].src,
197 info->packet.vis_orig));
200 hlist_for_each_entry(entry, pos, &vis_if_list, list) {
201 addr_to_string(tmp_addr_str, entry->addr);
202 tmp_len = sprintf(buff + bytes_written,
203 "%s,", tmp_addr_str);
205 for (i = 0; i < info->packet.entries; i++)
206 tmp_len += vis_data_read_entry(
207 buff + bytes_written + tmp_len,
208 &entries[i], entry->addr,
211 /* add primary/secondary records */
212 if (compare_orig(entry->addr, info->packet.vis_orig))
213 tmp_len += vis_data_read_prim_sec(
214 buff + bytes_written + tmp_len,
217 tmp_len += sprintf(buff + bytes_written + tmp_len,
225 bytes_written += tmp_len;
228 hlist_for_each_entry_safe(entry, pos, n, &vis_if_list, list) {
229 hlist_del(&entry->list);
233 spin_unlock_irqrestore(&vis_hash_lock, flags);
235 return bytes_written;
238 /* add the info packet to the send list, if it was not
239 * already linked in. */
240 static void send_list_add(struct vis_info *info)
242 if (list_empty(&info->send_list)) {
243 kref_get(&info->refcount);
244 list_add_tail(&info->send_list, &send_list);
248 /* delete the info packet from the send list, if it was
250 static void send_list_del(struct vis_info *info)
252 if (!list_empty(&info->send_list)) {
253 list_del_init(&info->send_list);
254 kref_put(&info->refcount, free_info);
258 /* tries to add one entry to the receive list. */
259 static void recv_list_add(struct list_head *recv_list, char *mac)
261 struct recvlist_node *entry;
264 entry = kmalloc(sizeof(struct recvlist_node), GFP_ATOMIC);
268 memcpy(entry->mac, mac, ETH_ALEN);
269 spin_lock_irqsave(&recv_list_lock, flags);
270 list_add_tail(&entry->list, recv_list);
271 spin_unlock_irqrestore(&recv_list_lock, flags);
274 /* returns 1 if this mac is in the recv_list */
275 static int recv_list_is_in(struct list_head *recv_list, char *mac)
277 struct recvlist_node *entry;
280 spin_lock_irqsave(&recv_list_lock, flags);
281 list_for_each_entry(entry, recv_list, list) {
282 if (memcmp(entry->mac, mac, ETH_ALEN) == 0) {
283 spin_unlock_irqrestore(&recv_list_lock, flags);
287 spin_unlock_irqrestore(&recv_list_lock, flags);
291 /* try to add the packet to the vis_hash. return NULL if invalid (e.g. too old,
292 * broken.. ). vis hash must be locked outside. is_new is set when the packet
293 * is newer than old entries in the hash. */
294 static struct vis_info *add_packet(struct vis_packet *vis_packet,
295 int vis_info_len, int *is_new,
298 struct vis_info *info, *old_info;
299 struct vis_info search_elem;
303 if (vis_hash == NULL)
306 /* see if the packet is already in vis_hash */
307 memcpy(search_elem.packet.vis_orig, vis_packet->vis_orig, ETH_ALEN);
308 old_info = hash_find(vis_hash, &search_elem);
310 if (old_info != NULL) {
311 if (!seq_after(vis_packet->seqno, old_info->packet.seqno)) {
312 if (old_info->packet.seqno == vis_packet->seqno) {
313 recv_list_add(&old_info->recv_list,
314 vis_packet->sender_orig);
317 /* newer packet is already in hash. */
321 /* remove old entry */
322 hash_remove(vis_hash, old_info);
323 send_list_del(old_info);
324 kref_put(&old_info->refcount, free_info);
327 info = kmalloc(sizeof(struct vis_info) + vis_info_len, GFP_ATOMIC);
331 kref_init(&info->refcount);
332 INIT_LIST_HEAD(&info->send_list);
333 INIT_LIST_HEAD(&info->recv_list);
334 info->first_seen = jiffies;
335 memcpy(&info->packet, vis_packet,
336 sizeof(struct vis_packet) + vis_info_len);
338 /* initialize and add new packet. */
341 /* Make it a broadcast packet, if required */
343 memcpy(info->packet.target_orig, broadcastAddr, ETH_ALEN);
345 /* repair if entries is longer than packet. */
346 if (info->packet.entries * sizeof(struct vis_info_entry) > vis_info_len)
347 info->packet.entries = vis_info_len /
348 sizeof(struct vis_info_entry);
350 recv_list_add(&info->recv_list, info->packet.sender_orig);
353 if (hash_add(vis_hash, info) < 0) {
354 /* did not work (for some reason) */
355 kref_put(&old_info->refcount, free_info);
362 /* handle the server sync packet, forward if needed. */
363 void receive_server_sync_packet(struct bat_priv *bat_priv,
364 struct vis_packet *vis_packet,
367 struct vis_info *info;
368 int is_new, make_broadcast;
370 int vis_server = atomic_read(&bat_priv->vis_mode);
372 make_broadcast = (vis_server == VIS_TYPE_SERVER_SYNC);
374 spin_lock_irqsave(&vis_hash_lock, flags);
375 info = add_packet(vis_packet, vis_info_len, &is_new, make_broadcast);
379 /* only if we are server ourselves and packet is newer than the one in
381 if (vis_server == VIS_TYPE_SERVER_SYNC && is_new)
384 spin_unlock_irqrestore(&vis_hash_lock, flags);
387 /* handle an incoming client update packet and schedule forward if needed. */
388 void receive_client_update_packet(struct bat_priv *bat_priv,
389 struct vis_packet *vis_packet,
392 struct vis_info *info;
395 int vis_server = atomic_read(&bat_priv->vis_mode);
398 /* clients shall not broadcast. */
399 if (is_bcast(vis_packet->target_orig))
402 /* Are we the target for this VIS packet? */
403 if (vis_server == VIS_TYPE_SERVER_SYNC &&
404 is_my_mac(vis_packet->target_orig))
407 spin_lock_irqsave(&vis_hash_lock, flags);
408 info = add_packet(vis_packet, vis_info_len, &is_new, are_target);
411 /* note that outdated packets will be dropped at this point. */
414 /* send only if we're the target server or ... */
415 if (are_target && is_new) {
416 info->packet.vis_type = VIS_TYPE_SERVER_SYNC; /* upgrade! */
419 /* ... we're not the recipient (and thus need to forward). */
420 } else if (!is_my_mac(info->packet.target_orig)) {
424 spin_unlock_irqrestore(&vis_hash_lock, flags);
427 /* Walk the originators and find the VIS server with the best tq. Set the packet
428 * address to its address and return the best_tq.
430 * Must be called with the originator hash locked */
431 static int find_best_vis_server(struct vis_info *info)
434 struct orig_node *orig_node;
437 while (hash_iterate(orig_hash, &hashit)) {
438 orig_node = hashit.bucket->data;
439 if ((orig_node != NULL) &&
440 (orig_node->router != NULL) &&
441 (orig_node->flags & VIS_SERVER) &&
442 (orig_node->router->tq_avg > best_tq)) {
443 best_tq = orig_node->router->tq_avg;
444 memcpy(info->packet.target_orig, orig_node->orig,
451 /* Return true if the vis packet is full. */
452 static bool vis_packet_full(struct vis_info *info)
454 if (info->packet.entries + 1 >
455 (1000 - sizeof(struct vis_info)) / sizeof(struct vis_info_entry))
460 /* generates a packet of own vis data,
461 * returns 0 on success, -1 if no packet could be generated */
462 static int generate_vis_packet(struct bat_priv *bat_priv)
464 HASHIT(hashit_local);
465 HASHIT(hashit_global);
466 struct orig_node *orig_node;
467 struct vis_info *info = (struct vis_info *)my_vis_info;
468 struct vis_info_entry *entry, *entry_array;
469 struct hna_local_entry *hna_local_entry;
473 info->first_seen = jiffies;
474 info->packet.vis_type = atomic_read(&bat_priv->vis_mode);
476 spin_lock_irqsave(&orig_hash_lock, flags);
477 memcpy(info->packet.target_orig, broadcastAddr, ETH_ALEN);
478 info->packet.ttl = TTL;
479 info->packet.seqno++;
480 info->packet.entries = 0;
482 if (info->packet.vis_type == VIS_TYPE_CLIENT_UPDATE) {
483 best_tq = find_best_vis_server(info);
485 spin_unlock_irqrestore(&orig_hash_lock, flags);
490 entry_array = (struct vis_info_entry *)
491 ((char *)info + sizeof(struct vis_info));
493 while (hash_iterate(orig_hash, &hashit_global)) {
494 orig_node = hashit_global.bucket->data;
495 if (orig_node->router != NULL
496 && compare_orig(orig_node->router->addr,
498 && (orig_node->router->if_incoming->if_status ==
500 && orig_node->router->tq_avg > 0) {
502 /* fill one entry into buffer. */
503 entry = &entry_array[info->packet.entries];
505 orig_node->router->if_incoming->net_dev->dev_addr,
507 memcpy(entry->dest, orig_node->orig, ETH_ALEN);
508 entry->quality = orig_node->router->tq_avg;
509 info->packet.entries++;
511 if (vis_packet_full(info)) {
512 spin_unlock_irqrestore(&orig_hash_lock, flags);
518 spin_unlock_irqrestore(&orig_hash_lock, flags);
520 spin_lock_irqsave(&hna_local_hash_lock, flags);
521 while (hash_iterate(hna_local_hash, &hashit_local)) {
522 hna_local_entry = hashit_local.bucket->data;
523 entry = &entry_array[info->packet.entries];
524 memset(entry->src, 0, ETH_ALEN);
525 memcpy(entry->dest, hna_local_entry->addr, ETH_ALEN);
526 entry->quality = 0; /* 0 means HNA */
527 info->packet.entries++;
529 if (vis_packet_full(info)) {
530 spin_unlock_irqrestore(&hna_local_hash_lock, flags);
534 spin_unlock_irqrestore(&hna_local_hash_lock, flags);
538 /* free old vis packets. Must be called with this vis_hash_lock
540 static void purge_vis_packets(void)
543 struct vis_info *info;
545 while (hash_iterate(vis_hash, &hashit)) {
546 info = hashit.bucket->data;
547 if (info == my_vis_info) /* never purge own data. */
549 if (time_after(jiffies,
550 info->first_seen + (VIS_TIMEOUT*HZ)/1000)) {
551 hash_remove_bucket(vis_hash, &hashit);
553 kref_put(&info->refcount, free_info);
558 static void broadcast_vis_packet(struct vis_info *info, int packet_length)
561 struct orig_node *orig_node;
563 struct batman_if *batman_if;
564 uint8_t dstaddr[ETH_ALEN];
566 spin_lock_irqsave(&orig_hash_lock, flags);
568 /* send to all routers in range. */
569 while (hash_iterate(orig_hash, &hashit)) {
570 orig_node = hashit.bucket->data;
572 /* if it's a vis server and reachable, send it. */
573 if ((!orig_node) || (!orig_node->router))
575 if (!(orig_node->flags & VIS_SERVER))
577 /* don't send it if we already received the packet from
579 if (recv_list_is_in(&info->recv_list, orig_node->orig))
582 memcpy(info->packet.target_orig, orig_node->orig, ETH_ALEN);
583 batman_if = orig_node->router->if_incoming;
584 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
585 spin_unlock_irqrestore(&orig_hash_lock, flags);
587 send_raw_packet((unsigned char *)&info->packet,
588 packet_length, batman_if, dstaddr);
590 spin_lock_irqsave(&orig_hash_lock, flags);
593 spin_unlock_irqrestore(&orig_hash_lock, flags);
594 memcpy(info->packet.target_orig, broadcastAddr, ETH_ALEN);
597 static void unicast_vis_packet(struct vis_info *info, int packet_length)
599 struct orig_node *orig_node;
601 struct batman_if *batman_if;
602 uint8_t dstaddr[ETH_ALEN];
604 spin_lock_irqsave(&orig_hash_lock, flags);
605 orig_node = ((struct orig_node *)
606 hash_find(orig_hash, info->packet.target_orig));
608 if ((!orig_node) || (!orig_node->router))
611 /* don't lock while sending the packets ... we therefore
612 * copy the required data before sending */
613 batman_if = orig_node->router->if_incoming;
614 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
615 spin_unlock_irqrestore(&orig_hash_lock, flags);
617 send_raw_packet((unsigned char *)&info->packet,
618 packet_length, batman_if, dstaddr);
622 spin_unlock_irqrestore(&orig_hash_lock, flags);
625 /* only send one vis packet. called from send_vis_packets() */
626 static void send_vis_packet(struct vis_info *info)
630 if (info->packet.ttl < 2) {
631 printk(KERN_WARNING "batman-adv: Error - can't send vis packet: ttl exceeded\n");
635 memcpy(info->packet.sender_orig, mainIfAddr, ETH_ALEN);
638 packet_length = sizeof(struct vis_packet) +
639 info->packet.entries * sizeof(struct vis_info_entry);
641 if (is_bcast(info->packet.target_orig))
642 broadcast_vis_packet(info, packet_length);
644 unicast_vis_packet(info, packet_length);
645 info->packet.ttl++; /* restore TTL */
648 /* called from timer; send (and maybe generate) vis packet. */
649 static void send_vis_packets(struct work_struct *work)
651 struct vis_info *info, *temp;
653 /* FIXME: each batman_if will be attached to a softif */
654 struct bat_priv *bat_priv = netdev_priv(soft_device);
656 spin_lock_irqsave(&vis_hash_lock, flags);
660 if (generate_vis_packet(bat_priv) == 0) {
661 /* schedule if generation was successful */
662 send_list_add(my_vis_info);
665 list_for_each_entry_safe(info, temp, &send_list, send_list) {
667 kref_get(&info->refcount);
668 spin_unlock_irqrestore(&vis_hash_lock, flags);
670 send_vis_packet(info);
672 spin_lock_irqsave(&vis_hash_lock, flags);
674 kref_put(&info->refcount, free_info);
676 spin_unlock_irqrestore(&vis_hash_lock, flags);
679 static DECLARE_DELAYED_WORK(vis_timer_wq, send_vis_packets);
681 /* init the vis server. this may only be called when if_list is already
682 * initialized (e.g. bat0 is initialized, interfaces have been added) */
689 spin_lock_irqsave(&vis_hash_lock, flags);
691 vis_hash = hash_new(256, vis_info_cmp, vis_info_choose);
693 printk(KERN_ERR "batman-adv:Can't initialize vis_hash\n");
697 my_vis_info = kmalloc(1000, GFP_ATOMIC);
699 printk(KERN_ERR "batman-adv:Can't initialize vis packet\n");
703 /* prefill the vis info */
704 my_vis_info->first_seen = jiffies - atomic_read(&vis_interval);
705 INIT_LIST_HEAD(&my_vis_info->recv_list);
706 INIT_LIST_HEAD(&my_vis_info->send_list);
707 kref_init(&my_vis_info->refcount);
708 my_vis_info->packet.version = COMPAT_VERSION;
709 my_vis_info->packet.packet_type = BAT_VIS;
710 my_vis_info->packet.ttl = TTL;
711 my_vis_info->packet.seqno = 0;
712 my_vis_info->packet.entries = 0;
714 INIT_LIST_HEAD(&send_list);
716 memcpy(my_vis_info->packet.vis_orig, mainIfAddr, ETH_ALEN);
717 memcpy(my_vis_info->packet.sender_orig, mainIfAddr, ETH_ALEN);
719 if (hash_add(vis_hash, my_vis_info) < 0) {
721 "batman-adv:Can't add own vis packet into hash\n");
722 /* not in hash, need to remove it manually. */
723 kref_put(&my_vis_info->refcount, free_info);
727 spin_unlock_irqrestore(&vis_hash_lock, flags);
732 spin_unlock_irqrestore(&vis_hash_lock, flags);
737 /* Decrease the reference count on a hash item info */
738 static void free_info_ref(void *data)
740 struct vis_info *info = data;
743 kref_put(&info->refcount, free_info);
746 /* shutdown vis-server */
753 cancel_delayed_work_sync(&vis_timer_wq);
755 spin_lock_irqsave(&vis_hash_lock, flags);
756 /* properly remove, kill timers ... */
757 hash_delete(vis_hash, free_info_ref);
760 spin_unlock_irqrestore(&vis_hash_lock, flags);
763 /* schedule packets for (re)transmission */
764 static void start_vis_timer(void)
766 queue_delayed_work(bat_event_workqueue, &vis_timer_wq,
767 (atomic_read(&vis_interval) * HZ) / 1000);