blob: 60eb9b7ca8d10643fc7c807fc36d8b8632312c2d [file] [log] [blame]
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001/* Copyright (C) 2008-2012 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002 *
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
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000018 */
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
Sven Eckelmann347c80f2012-06-03 22:19:07 +020029#define BATADV_MAX_VIS_PACKET_SIZE 1000
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000030
Antonio Quartullidec05072012-11-10 11:00:32 +010031/* hash class keys */
32static struct lock_class_key batadv_vis_hash_lock_class_key;
33
Sven Eckelmann56303d32012-06-05 22:31:31 +020034static void batadv_start_vis_timer(struct batadv_priv *bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000035
36/* free the info */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +020037static void batadv_free_info(struct kref *ref)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000038{
Sven Eckelmann56303d32012-06-05 22:31:31 +020039 struct batadv_vis_info *info;
40 struct batadv_priv *bat_priv;
41 struct batadv_recvlist_node *entry, *tmp;
42
43 info = container_of(ref, struct batadv_vis_info, refcount);
44 bat_priv = info->bat_priv;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000045
46 list_del_init(&info->send_list);
Sven Eckelmann807736f2012-07-15 22:26:51 +020047 spin_lock_bh(&bat_priv->vis.list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000048 list_for_each_entry_safe(entry, tmp, &info->recv_list, list) {
49 list_del(&entry->list);
50 kfree(entry);
51 }
52
Sven Eckelmann807736f2012-07-15 22:26:51 +020053 spin_unlock_bh(&bat_priv->vis.list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000054 kfree_skb(info->skb_packet);
Sven Eckelmanndda9fc62011-01-28 18:34:06 +010055 kfree(info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000056}
57
58/* Compare two vis packets, used by the hashing algorithm */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +020059static int batadv_vis_info_cmp(const struct hlist_node *node, const void *data2)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000060{
Sven Eckelmann56303d32012-06-05 22:31:31 +020061 const struct batadv_vis_info *d1, *d2;
Sven Eckelmann96412692012-06-05 22:31:30 +020062 const struct batadv_vis_packet *p1, *p2;
Marek Lindner7aadf882011-02-18 12:28:09 +000063
Sven Eckelmann56303d32012-06-05 22:31:31 +020064 d1 = container_of(node, struct batadv_vis_info, hash_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000065 d2 = data2;
Sven Eckelmann96412692012-06-05 22:31:30 +020066 p1 = (struct batadv_vis_packet *)d1->skb_packet->data;
67 p2 = (struct batadv_vis_packet *)d2->skb_packet->data;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +020068 return batadv_compare_eth(p1->vis_orig, p2->vis_orig);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000069}
70
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +020071/* hash function to choose an entry in a hash table of given size
72 * hash algorithm from http://en.wikipedia.org/wiki/Hash_table
73 */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +020074static uint32_t batadv_vis_info_choose(const void *data, uint32_t size)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000075{
Sven Eckelmann56303d32012-06-05 22:31:31 +020076 const struct batadv_vis_info *vis_info = data;
Sven Eckelmann96412692012-06-05 22:31:30 +020077 const struct batadv_vis_packet *packet;
Sven Eckelmann747e4222011-05-14 23:14:50 +020078 const unsigned char *key;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000079 uint32_t hash = 0;
80 size_t i;
81
Sven Eckelmann96412692012-06-05 22:31:30 +020082 packet = (struct batadv_vis_packet *)vis_info->skb_packet->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000083 key = packet->vis_orig;
84 for (i = 0; i < ETH_ALEN; i++) {
85 hash += key[i];
86 hash += (hash << 10);
87 hash ^= (hash >> 6);
88 }
89
90 hash += (hash << 3);
91 hash ^= (hash >> 11);
92 hash += (hash << 15);
93
94 return hash % size;
95}
96
Sven Eckelmann56303d32012-06-05 22:31:31 +020097static struct batadv_vis_info *
98batadv_vis_hash_find(struct batadv_priv *bat_priv, const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +000099{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200100 struct batadv_hashtable *hash = bat_priv->vis.hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000101 struct hlist_head *head;
102 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200103 struct batadv_vis_info *vis_info, *vis_info_tmp = NULL;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200104 uint32_t index;
Marek Lindner7aadf882011-02-18 12:28:09 +0000105
106 if (!hash)
107 return NULL;
108
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200109 index = batadv_vis_info_choose(data, hash->size);
Marek Lindner7aadf882011-02-18 12:28:09 +0000110 head = &hash->table[index];
111
112 rcu_read_lock();
113 hlist_for_each_entry_rcu(vis_info, node, head, hash_entry) {
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200114 if (!batadv_vis_info_cmp(node, data))
Marek Lindner7aadf882011-02-18 12:28:09 +0000115 continue;
116
117 vis_info_tmp = vis_info;
118 break;
119 }
120 rcu_read_unlock();
121
122 return vis_info_tmp;
123}
124
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000125/* insert interface to the list of interfaces of one originator, if it
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200126 * does not already exist in the list
127 */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200128static void batadv_vis_data_insert_interface(const uint8_t *interface,
129 struct hlist_head *if_list,
130 bool primary)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000131{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200132 struct batadv_if_list_entry *entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000133 struct hlist_node *pos;
134
135 hlist_for_each_entry(entry, pos, if_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200136 if (batadv_compare_eth(entry->addr, interface))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000137 return;
138 }
139
Antonio Quartulli015758d2011-07-09 17:52:13 +0200140 /* it's a new address, add it to the list */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000141 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
142 if (!entry)
143 return;
144 memcpy(entry->addr, interface, ETH_ALEN);
145 entry->primary = primary;
146 hlist_add_head(&entry->list, if_list);
147}
148
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200149static void batadv_vis_data_read_prim_sec(struct seq_file *seq,
150 const struct hlist_head *if_list)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000151{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200152 struct batadv_if_list_entry *entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000153 struct hlist_node *pos;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000154
155 hlist_for_each_entry(entry, pos, if_list, list) {
156 if (entry->primary)
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200157 seq_printf(seq, "PRIMARY, ");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000158 else
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200159 seq_printf(seq, "SEC %pM, ", entry->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000160 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000161}
162
163/* read an entry */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200164static ssize_t
165batadv_vis_data_read_entry(struct seq_file *seq,
166 const struct batadv_vis_info_entry *entry,
167 const uint8_t *src, bool primary)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000168{
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000169 if (primary && entry->quality == 0)
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200170 return seq_printf(seq, "TT %pM, ", entry->dest);
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200171 else if (batadv_compare_eth(entry->src, src))
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200172 return seq_printf(seq, "TQ %pM %d, ", entry->dest,
173 entry->quality);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000174
175 return 0;
176}
177
Sven Eckelmann56303d32012-06-05 22:31:31 +0200178static void
179batadv_vis_data_insert_interfaces(struct hlist_head *list,
180 struct batadv_vis_packet *packet,
181 struct batadv_vis_info_entry *entries)
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200182{
183 int i;
184
185 for (i = 0; i < packet->entries; i++) {
186 if (entries[i].quality == 0)
187 continue;
188
189 if (batadv_compare_eth(entries[i].src, packet->vis_orig))
190 continue;
191
192 batadv_vis_data_insert_interface(entries[i].src, list, false);
193 }
194}
195
196static void batadv_vis_data_read_entries(struct seq_file *seq,
197 struct hlist_head *list,
Sven Eckelmann96412692012-06-05 22:31:30 +0200198 struct batadv_vis_packet *packet,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200199 struct batadv_vis_info_entry *entries)
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200200{
201 int i;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200202 struct batadv_if_list_entry *entry;
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200203 struct hlist_node *pos;
204
205 hlist_for_each_entry(entry, pos, list, list) {
206 seq_printf(seq, "%pM,", entry->addr);
207
208 for (i = 0; i < packet->entries; i++)
209 batadv_vis_data_read_entry(seq, &entries[i],
210 entry->addr, entry->primary);
211
212 /* add primary/secondary records */
213 if (batadv_compare_eth(entry->addr, packet->vis_orig))
214 batadv_vis_data_read_prim_sec(seq, list);
215
216 seq_printf(seq, "\n");
217 }
218}
219
220static void batadv_vis_seq_print_text_bucket(struct seq_file *seq,
221 const struct hlist_head *head)
222{
223 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200224 struct batadv_vis_info *info;
Sven Eckelmann96412692012-06-05 22:31:30 +0200225 struct batadv_vis_packet *packet;
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200226 uint8_t *entries_pos;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200227 struct batadv_vis_info_entry *entries;
228 struct batadv_if_list_entry *entry;
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200229 struct hlist_node *pos, *n;
230
231 HLIST_HEAD(vis_if_list);
232
233 hlist_for_each_entry_rcu(info, node, head, hash_entry) {
Sven Eckelmann96412692012-06-05 22:31:30 +0200234 packet = (struct batadv_vis_packet *)info->skb_packet->data;
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200235 entries_pos = (uint8_t *)packet + sizeof(*packet);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200236 entries = (struct batadv_vis_info_entry *)entries_pos;
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200237
238 batadv_vis_data_insert_interface(packet->vis_orig, &vis_if_list,
239 true);
240 batadv_vis_data_insert_interfaces(&vis_if_list, packet,
241 entries);
242 batadv_vis_data_read_entries(seq, &vis_if_list, packet,
243 entries);
244
245 hlist_for_each_entry_safe(entry, pos, n, &vis_if_list, list) {
246 hlist_del(&entry->list);
247 kfree(entry);
248 }
249 }
250}
251
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200252int batadv_vis_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000253{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200254 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000255 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000256 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200257 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200258 struct batadv_hashtable *hash = bat_priv->vis.hash;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200259 uint32_t i;
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200260 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000261 int vis_server = atomic_read(&bat_priv->vis_mode);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000262
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200263 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200264 if (!primary_if)
265 goto out;
266
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200267 if (vis_server == BATADV_VIS_TYPE_CLIENT_UPDATE)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200268 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000269
Sven Eckelmann807736f2012-07-15 22:26:51 +0200270 spin_lock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000271 for (i = 0; i < hash->size; i++) {
272 head = &hash->table[i];
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200273 batadv_vis_seq_print_text_bucket(seq, head);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000274 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200275 spin_unlock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000276
Marek Lindner32ae9b22011-04-20 15:40:58 +0200277out:
278 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200279 batadv_hardif_free_ref(primary_if);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200280 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000281}
282
283/* add the info packet to the send list, if it was not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200284 * already linked in.
285 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200286static void batadv_send_list_add(struct batadv_priv *bat_priv,
287 struct batadv_vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000288{
289 if (list_empty(&info->send_list)) {
290 kref_get(&info->refcount);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200291 list_add_tail(&info->send_list, &bat_priv->vis.send_list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000292 }
293}
294
295/* delete the info packet from the send list, if it was
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200296 * linked in.
297 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200298static void batadv_send_list_del(struct batadv_vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000299{
300 if (!list_empty(&info->send_list)) {
301 list_del_init(&info->send_list);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200302 kref_put(&info->refcount, batadv_free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000303 }
304}
305
306/* tries to add one entry to the receive list. */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200307static void batadv_recv_list_add(struct batadv_priv *bat_priv,
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200308 struct list_head *recv_list, const char *mac)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000309{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200310 struct batadv_recvlist_node *entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000311
Sven Eckelmann704509b2011-05-14 23:14:54 +0200312 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000313 if (!entry)
314 return;
315
316 memcpy(entry->mac, mac, ETH_ALEN);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200317 spin_lock_bh(&bat_priv->vis.list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000318 list_add_tail(&entry->list, recv_list);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200319 spin_unlock_bh(&bat_priv->vis.list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000320}
321
322/* returns 1 if this mac is in the recv_list */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200323static int batadv_recv_list_is_in(struct batadv_priv *bat_priv,
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200324 const struct list_head *recv_list,
325 const char *mac)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000326{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200327 const struct batadv_recvlist_node *entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000328
Sven Eckelmann807736f2012-07-15 22:26:51 +0200329 spin_lock_bh(&bat_priv->vis.list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000330 list_for_each_entry(entry, recv_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200331 if (batadv_compare_eth(entry->mac, mac)) {
Sven Eckelmann807736f2012-07-15 22:26:51 +0200332 spin_unlock_bh(&bat_priv->vis.list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000333 return 1;
334 }
335 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200336 spin_unlock_bh(&bat_priv->vis.list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000337 return 0;
338}
339
340/* try to add the packet to the vis_hash. return NULL if invalid (e.g. too old,
341 * broken.. ). vis hash must be locked outside. is_new is set when the packet
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200342 * is newer than old entries in the hash.
343 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200344static struct batadv_vis_info *
345batadv_add_packet(struct batadv_priv *bat_priv,
346 struct batadv_vis_packet *vis_packet, int vis_info_len,
347 int *is_new, int make_broadcast)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000348{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200349 struct batadv_vis_info *info, *old_info;
Sven Eckelmann96412692012-06-05 22:31:30 +0200350 struct batadv_vis_packet *search_packet, *old_packet;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200351 struct batadv_vis_info search_elem;
Sven Eckelmann96412692012-06-05 22:31:30 +0200352 struct batadv_vis_packet *packet;
353 struct sk_buff *tmp_skb;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000354 int hash_added;
Sven Eckelmann96412692012-06-05 22:31:30 +0200355 size_t len;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200356 size_t max_entries;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000357
358 *is_new = 0;
359 /* sanity check */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200360 if (!bat_priv->vis.hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000361 return NULL;
362
363 /* see if the packet is already in vis_hash */
Sven Eckelmann704509b2011-05-14 23:14:54 +0200364 search_elem.skb_packet = dev_alloc_skb(sizeof(*search_packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000365 if (!search_elem.skb_packet)
366 return NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200367 len = sizeof(*search_packet);
368 tmp_skb = search_elem.skb_packet;
369 search_packet = (struct batadv_vis_packet *)skb_put(tmp_skb, len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000370
371 memcpy(search_packet->vis_orig, vis_packet->vis_orig, ETH_ALEN);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200372 old_info = batadv_vis_hash_find(bat_priv, &search_elem);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000373 kfree_skb(search_elem.skb_packet);
374
375 if (old_info) {
Sven Eckelmann96412692012-06-05 22:31:30 +0200376 tmp_skb = old_info->skb_packet;
377 old_packet = (struct batadv_vis_packet *)tmp_skb->data;
Sven Eckelmann3e348192012-05-16 20:23:22 +0200378 if (!batadv_seq_after(ntohl(vis_packet->seqno),
379 ntohl(old_packet->seqno))) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000380 if (old_packet->seqno == vis_packet->seqno) {
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200381 batadv_recv_list_add(bat_priv,
382 &old_info->recv_list,
383 vis_packet->sender_orig);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000384 return old_info;
385 } else {
386 /* newer packet is already in hash. */
387 return NULL;
388 }
389 }
390 /* remove old entry */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200391 batadv_hash_remove(bat_priv->vis.hash, batadv_vis_info_cmp,
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200392 batadv_vis_info_choose, old_info);
393 batadv_send_list_del(old_info);
394 kref_put(&old_info->refcount, batadv_free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000395 }
396
Sven Eckelmann704509b2011-05-14 23:14:54 +0200397 info = kmalloc(sizeof(*info), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000398 if (!info)
399 return NULL;
400
Sven Eckelmann96412692012-06-05 22:31:30 +0200401 len = sizeof(*packet) + vis_info_len;
Sven Eckelmann5b246572012-11-04 17:11:45 +0100402 info->skb_packet = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000403 if (!info->skb_packet) {
404 kfree(info);
405 return NULL;
406 }
Sven Eckelmann5b246572012-11-04 17:11:45 +0100407 skb_reserve(info->skb_packet, ETH_HLEN + NET_IP_ALIGN);
Sven Eckelmann96412692012-06-05 22:31:30 +0200408 packet = (struct batadv_vis_packet *)skb_put(info->skb_packet, len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000409
410 kref_init(&info->refcount);
411 INIT_LIST_HEAD(&info->send_list);
412 INIT_LIST_HEAD(&info->recv_list);
413 info->first_seen = jiffies;
414 info->bat_priv = bat_priv;
Sven Eckelmann96412692012-06-05 22:31:30 +0200415 memcpy(packet, vis_packet, len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000416
417 /* initialize and add new packet. */
418 *is_new = 1;
419
420 /* Make it a broadcast packet, if required */
421 if (make_broadcast)
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200422 memcpy(packet->target_orig, batadv_broadcast_addr, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000423
424 /* repair if entries is longer than packet. */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200425 max_entries = vis_info_len / sizeof(struct batadv_vis_info_entry);
426 if (packet->entries > max_entries)
427 packet->entries = max_entries;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000428
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200429 batadv_recv_list_add(bat_priv, &info->recv_list, packet->sender_orig);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000430
431 /* try to add it */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200432 hash_added = batadv_hash_add(bat_priv->vis.hash, batadv_vis_info_cmp,
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200433 batadv_vis_info_choose, info,
434 &info->hash_entry);
Antonio Quartulli1a1f37d2011-07-10 00:36:36 +0200435 if (hash_added != 0) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000436 /* did not work (for some reason) */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200437 kref_put(&info->refcount, batadv_free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000438 info = NULL;
439 }
440
441 return info;
442}
443
444/* handle the server sync packet, forward if needed. */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200445void batadv_receive_server_sync_packet(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +0200446 struct batadv_vis_packet *vis_packet,
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200447 int vis_info_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000448{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200449 struct batadv_vis_info *info;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000450 int is_new, make_broadcast;
451 int vis_server = atomic_read(&bat_priv->vis_mode);
452
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200453 make_broadcast = (vis_server == BATADV_VIS_TYPE_SERVER_SYNC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000454
Sven Eckelmann807736f2012-07-15 22:26:51 +0200455 spin_lock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200456 info = batadv_add_packet(bat_priv, vis_packet, vis_info_len,
457 &is_new, make_broadcast);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000458 if (!info)
459 goto end;
460
461 /* only if we are server ourselves and packet is newer than the one in
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200462 * hash.
463 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200464 if (vis_server == BATADV_VIS_TYPE_SERVER_SYNC && is_new)
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200465 batadv_send_list_add(bat_priv, info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000466end:
Sven Eckelmann807736f2012-07-15 22:26:51 +0200467 spin_unlock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000468}
469
470/* handle an incoming client update packet and schedule forward if needed. */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200471void batadv_receive_client_update_packet(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +0200472 struct batadv_vis_packet *vis_packet,
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200473 int vis_info_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000474{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200475 struct batadv_vis_info *info;
Sven Eckelmann96412692012-06-05 22:31:30 +0200476 struct batadv_vis_packet *packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000477 int is_new;
478 int vis_server = atomic_read(&bat_priv->vis_mode);
479 int are_target = 0;
480
481 /* clients shall not broadcast. */
482 if (is_broadcast_ether_addr(vis_packet->target_orig))
483 return;
484
485 /* Are we the target for this VIS packet? */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200486 if (vis_server == BATADV_VIS_TYPE_SERVER_SYNC &&
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200487 batadv_is_my_mac(vis_packet->target_orig))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000488 are_target = 1;
489
Sven Eckelmann807736f2012-07-15 22:26:51 +0200490 spin_lock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200491 info = batadv_add_packet(bat_priv, vis_packet, vis_info_len,
492 &is_new, are_target);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000493
494 if (!info)
495 goto end;
496 /* note that outdated packets will be dropped at this point. */
497
Sven Eckelmann96412692012-06-05 22:31:30 +0200498 packet = (struct batadv_vis_packet *)info->skb_packet->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000499
500 /* send only if we're the target server or ... */
501 if (are_target && is_new) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200502 packet->vis_type = BATADV_VIS_TYPE_SERVER_SYNC; /* upgrade! */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200503 batadv_send_list_add(bat_priv, info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000504
505 /* ... we're not the recipient (and thus need to forward). */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200506 } else if (!batadv_is_my_mac(packet->target_orig)) {
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200507 batadv_send_list_add(bat_priv, info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000508 }
509
510end:
Sven Eckelmann807736f2012-07-15 22:26:51 +0200511 spin_unlock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000512}
513
514/* Walk the originators and find the VIS server with the best tq. Set the packet
515 * address to its address and return the best_tq.
516 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200517 * Must be called with the originator hash locked
518 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200519static int batadv_find_best_vis_server(struct batadv_priv *bat_priv,
520 struct batadv_vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000521{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200522 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200523 struct batadv_neigh_node *router;
Marek Lindner7aadf882011-02-18 12:28:09 +0000524 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000525 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200526 struct batadv_orig_node *orig_node;
Sven Eckelmann96412692012-06-05 22:31:30 +0200527 struct batadv_vis_packet *packet;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200528 int best_tq = -1;
529 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000530
Sven Eckelmann96412692012-06-05 22:31:30 +0200531 packet = (struct batadv_vis_packet *)info->skb_packet->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000532
533 for (i = 0; i < hash->size; i++) {
534 head = &hash->table[i];
535
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000536 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000537 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200538 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000539 if (!router)
540 continue;
541
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200542 if ((orig_node->flags & BATADV_VIS_SERVER) &&
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000543 (router->tq_avg > best_tq)) {
544 best_tq = router->tq_avg;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000545 memcpy(packet->target_orig, orig_node->orig,
546 ETH_ALEN);
547 }
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200548 batadv_neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000549 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000550 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000551 }
552
553 return best_tq;
554}
555
556/* Return true if the vis packet is full. */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200557static bool batadv_vis_packet_full(const struct batadv_vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000558{
Sven Eckelmann96412692012-06-05 22:31:30 +0200559 const struct batadv_vis_packet *packet;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200560 size_t num;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000561
Sven Eckelmann96412692012-06-05 22:31:30 +0200562 packet = (struct batadv_vis_packet *)info->skb_packet->data;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200563 num = BATADV_MAX_VIS_PACKET_SIZE / sizeof(struct batadv_vis_info_entry);
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200564
Sven Eckelmann56303d32012-06-05 22:31:31 +0200565 if (num < packet->entries + 1)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000566 return true;
567 return false;
568}
569
570/* generates a packet of own vis data,
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200571 * returns 0 on success, -1 if no packet could be generated
572 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200573static int batadv_generate_vis_packet(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000574{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200575 struct batadv_hashtable *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000576 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000577 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200578 struct batadv_orig_node *orig_node;
579 struct batadv_neigh_node *router;
Sven Eckelmann807736f2012-07-15 22:26:51 +0200580 struct batadv_vis_info *info = bat_priv->vis.my_info;
Sven Eckelmann96412692012-06-05 22:31:30 +0200581 struct batadv_vis_packet *packet;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200582 struct batadv_vis_info_entry *entry;
583 struct batadv_tt_common_entry *tt_common_entry;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200584 uint8_t *packet_pos;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200585 int best_tq = -1;
586 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000587
588 info->first_seen = jiffies;
Sven Eckelmann96412692012-06-05 22:31:30 +0200589 packet = (struct batadv_vis_packet *)info->skb_packet->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000590 packet->vis_type = atomic_read(&bat_priv->vis_mode);
591
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200592 memcpy(packet->target_orig, batadv_broadcast_addr, ETH_ALEN);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200593 packet->header.ttl = BATADV_TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000594 packet->seqno = htonl(ntohl(packet->seqno) + 1);
595 packet->entries = 0;
Sven Eckelmann162d5492012-06-28 11:56:52 +0200596 packet->reserved = 0;
Sven Eckelmann704509b2011-05-14 23:14:54 +0200597 skb_trim(info->skb_packet, sizeof(*packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000598
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200599 if (packet->vis_type == BATADV_VIS_TYPE_CLIENT_UPDATE) {
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200600 best_tq = batadv_find_best_vis_server(bat_priv, info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000601
Marek Lindnerd0072602011-01-19 20:01:44 +0000602 if (best_tq < 0)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200603 return best_tq;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000604 }
605
606 for (i = 0; i < hash->size; i++) {
607 head = &hash->table[i];
608
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000609 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000610 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200611 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000612 if (!router)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000613 continue;
614
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200615 if (!batadv_compare_eth(router->addr, orig_node->orig))
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000616 goto next;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000617
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200618 if (router->if_incoming->if_status != BATADV_IF_ACTIVE)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000619 goto next;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000620
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000621 if (router->tq_avg < 1)
622 goto next;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000623
624 /* fill one entry into buffer. */
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200625 packet_pos = skb_put(info->skb_packet, sizeof(*entry));
626 entry = (struct batadv_vis_info_entry *)packet_pos;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000627 memcpy(entry->src,
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000628 router->if_incoming->net_dev->dev_addr,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000629 ETH_ALEN);
630 memcpy(entry->dest, orig_node->orig, ETH_ALEN);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000631 entry->quality = router->tq_avg;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000632 packet->entries++;
633
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000634next:
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200635 batadv_neigh_node_free_ref(router);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000636
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200637 if (batadv_vis_packet_full(info))
Marek Lindnerd0072602011-01-19 20:01:44 +0000638 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000639 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000640 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000641 }
642
Sven Eckelmann807736f2012-07-15 22:26:51 +0200643 hash = bat_priv->tt.local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000644
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000645 for (i = 0; i < hash->size; i++) {
646 head = &hash->table[i];
647
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200648 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100649 hlist_for_each_entry_rcu(tt_common_entry, node, head,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200650 hash_entry) {
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200651 packet_pos = skb_put(info->skb_packet, sizeof(*entry));
652 entry = (struct batadv_vis_info_entry *)packet_pos;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000653 memset(entry->src, 0, ETH_ALEN);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100654 memcpy(entry->dest, tt_common_entry->addr, ETH_ALEN);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200655 entry->quality = 0; /* 0 means TT */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000656 packet->entries++;
657
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200658 if (batadv_vis_packet_full(info))
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200659 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000660 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200661 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000662 }
663
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000664 return 0;
Marek Lindnerd0072602011-01-19 20:01:44 +0000665
666unlock:
667 rcu_read_unlock();
668 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000669}
670
671/* free old vis packets. Must be called with this vis_hash_lock
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200672 * held
673 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200674static void batadv_purge_vis_packets(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000675{
Antonio Quartullic90681b2011-10-05 17:05:25 +0200676 uint32_t i;
Sven Eckelmann807736f2012-07-15 22:26:51 +0200677 struct batadv_hashtable *hash = bat_priv->vis.hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000678 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000679 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200680 struct batadv_vis_info *info;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000681
682 for (i = 0; i < hash->size; i++) {
683 head = &hash->table[i];
684
Marek Lindner7aadf882011-02-18 12:28:09 +0000685 hlist_for_each_entry_safe(info, node, node_tmp,
686 head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000687 /* never purge own data. */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200688 if (info == bat_priv->vis.my_info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000689 continue;
690
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200691 if (batadv_has_timed_out(info->first_seen,
Sven Eckelmannedbf7ff2012-06-03 22:19:16 +0200692 BATADV_VIS_TIMEOUT)) {
Marek Lindner7aadf882011-02-18 12:28:09 +0000693 hlist_del(node);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200694 batadv_send_list_del(info);
695 kref_put(&info->refcount, batadv_free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000696 }
697 }
698 }
699}
700
Sven Eckelmann56303d32012-06-05 22:31:31 +0200701static void batadv_broadcast_vis_packet(struct batadv_priv *bat_priv,
702 struct batadv_vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000703{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200704 struct batadv_hashtable *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000705 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000706 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200707 struct batadv_orig_node *orig_node;
Sven Eckelmann96412692012-06-05 22:31:30 +0200708 struct batadv_vis_packet *packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000709 struct sk_buff *skb;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200710 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000711
712
Sven Eckelmann96412692012-06-05 22:31:30 +0200713 packet = (struct batadv_vis_packet *)info->skb_packet->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000714
715 /* send to all routers in range. */
716 for (i = 0; i < hash->size; i++) {
717 head = &hash->table[i];
718
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000719 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000720 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000721 /* if it's a vis server and reachable, send it. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200722 if (!(orig_node->flags & BATADV_VIS_SERVER))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000723 continue;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000724
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000725 /* don't send it if we already received the packet from
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200726 * this node.
727 */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200728 if (batadv_recv_list_is_in(bat_priv, &info->recv_list,
Martin Hundebøllbb351ba2012-10-16 16:13:48 +0200729 orig_node->orig))
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000730 continue;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000731
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000732 memcpy(packet->target_orig, orig_node->orig, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000733 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
Martin Hundebøllbb351ba2012-10-16 16:13:48 +0200734 if (!skb)
735 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000736
Martin Hundebøllbb351ba2012-10-16 16:13:48 +0200737 if (!batadv_send_skb_to_orig(skb, orig_node, NULL))
738 kfree_skb(skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000739 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000740 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000741 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000742}
743
Sven Eckelmann56303d32012-06-05 22:31:31 +0200744static void batadv_unicast_vis_packet(struct batadv_priv *bat_priv,
745 struct batadv_vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000746{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200747 struct batadv_orig_node *orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000748 struct sk_buff *skb;
Sven Eckelmann96412692012-06-05 22:31:30 +0200749 struct batadv_vis_packet *packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000750
Sven Eckelmann96412692012-06-05 22:31:30 +0200751 packet = (struct batadv_vis_packet *)info->skb_packet->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000752
Sven Eckelmannda641192012-05-12 13:48:56 +0200753 orig_node = batadv_orig_hash_find(bat_priv, packet->target_orig);
Marek Lindner44524fc2011-02-10 14:33:53 +0000754 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000755 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000756
Martin Hundebøllbb351ba2012-10-16 16:13:48 +0200757 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
758 if (!skb)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000759 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000760
Martin Hundebøllbb351ba2012-10-16 16:13:48 +0200761 if (!batadv_send_skb_to_orig(skb, orig_node, NULL))
762 kfree_skb(skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000763
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000764out:
Marek Lindner44524fc2011-02-10 14:33:53 +0000765 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200766 batadv_orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000767}
768
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200769/* only send one vis packet. called from batadv_send_vis_packets() */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200770static void batadv_send_vis_packet(struct batadv_priv *bat_priv,
771 struct batadv_vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000772{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200773 struct batadv_hard_iface *primary_if;
Sven Eckelmann96412692012-06-05 22:31:30 +0200774 struct batadv_vis_packet *packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000775
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200776 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200777 if (!primary_if)
778 goto out;
779
Sven Eckelmann96412692012-06-05 22:31:30 +0200780 packet = (struct batadv_vis_packet *)info->skb_packet->data;
Sven Eckelmann76543d12011-11-20 15:47:38 +0100781 if (packet->header.ttl < 2) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000782 pr_debug("Error - can't send vis packet: ttl exceeded\n");
Marek Lindner32ae9b22011-04-20 15:40:58 +0200783 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000784 }
785
Marek Lindner32ae9b22011-04-20 15:40:58 +0200786 memcpy(packet->sender_orig, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmann76543d12011-11-20 15:47:38 +0100787 packet->header.ttl--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000788
789 if (is_broadcast_ether_addr(packet->target_orig))
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200790 batadv_broadcast_vis_packet(bat_priv, info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000791 else
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200792 batadv_unicast_vis_packet(bat_priv, info);
Sven Eckelmann76543d12011-11-20 15:47:38 +0100793 packet->header.ttl++; /* restore TTL */
Marek Lindner32ae9b22011-04-20 15:40:58 +0200794
795out:
796 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200797 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000798}
799
800/* called from timer; send (and maybe generate) vis packet. */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200801static void batadv_send_vis_packets(struct work_struct *work)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000802{
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200803 struct delayed_work *delayed_work;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200804 struct batadv_priv *bat_priv;
Sven Eckelmann807736f2012-07-15 22:26:51 +0200805 struct batadv_priv_vis *priv_vis;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200806 struct batadv_vis_info *info;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000807
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200808 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200809 priv_vis = container_of(delayed_work, struct batadv_priv_vis, work);
810 bat_priv = container_of(priv_vis, struct batadv_priv, vis);
811 spin_lock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200812 batadv_purge_vis_packets(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000813
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200814 if (batadv_generate_vis_packet(bat_priv) == 0) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000815 /* schedule if generation was successful */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200816 batadv_send_list_add(bat_priv, bat_priv->vis.my_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000817 }
818
Sven Eckelmann807736f2012-07-15 22:26:51 +0200819 while (!list_empty(&bat_priv->vis.send_list)) {
820 info = list_first_entry(&bat_priv->vis.send_list,
Sven Eckelmann1181e1d2011-01-28 18:34:07 +0100821 typeof(*info), send_list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000822
823 kref_get(&info->refcount);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200824 spin_unlock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000825
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200826 batadv_send_vis_packet(bat_priv, info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000827
Sven Eckelmann807736f2012-07-15 22:26:51 +0200828 spin_lock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200829 batadv_send_list_del(info);
830 kref_put(&info->refcount, batadv_free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000831 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200832 spin_unlock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200833 batadv_start_vis_timer(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000834}
835
836/* init the vis server. this may only be called when if_list is already
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200837 * initialized (e.g. bat0 is initialized, interfaces have been added)
838 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200839int batadv_vis_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000840{
Sven Eckelmann96412692012-06-05 22:31:30 +0200841 struct batadv_vis_packet *packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000842 int hash_added;
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200843 unsigned int len;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200844 unsigned long first_seen;
Sven Eckelmann96412692012-06-05 22:31:30 +0200845 struct sk_buff *tmp_skb;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000846
Sven Eckelmann807736f2012-07-15 22:26:51 +0200847 if (bat_priv->vis.hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200848 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000849
Sven Eckelmann807736f2012-07-15 22:26:51 +0200850 spin_lock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000851
Sven Eckelmann807736f2012-07-15 22:26:51 +0200852 bat_priv->vis.hash = batadv_hash_new(256);
853 if (!bat_priv->vis.hash) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000854 pr_err("Can't initialize vis_hash\n");
855 goto err;
856 }
857
Antonio Quartullidec05072012-11-10 11:00:32 +0100858 batadv_hash_set_lock_class(bat_priv->vis.hash,
859 &batadv_vis_hash_lock_class_key);
860
Sven Eckelmann807736f2012-07-15 22:26:51 +0200861 bat_priv->vis.my_info = kmalloc(BATADV_MAX_VIS_PACKET_SIZE, GFP_ATOMIC);
862 if (!bat_priv->vis.my_info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000863 goto err;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000864
Sven Eckelmann5b246572012-11-04 17:11:45 +0100865 len = sizeof(*packet) + BATADV_MAX_VIS_PACKET_SIZE;
866 len += ETH_HLEN + NET_IP_ALIGN;
Sven Eckelmann807736f2012-07-15 22:26:51 +0200867 bat_priv->vis.my_info->skb_packet = dev_alloc_skb(len);
868 if (!bat_priv->vis.my_info->skb_packet)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000869 goto free_info;
870
Sven Eckelmann5b246572012-11-04 17:11:45 +0100871 skb_reserve(bat_priv->vis.my_info->skb_packet, ETH_HLEN + NET_IP_ALIGN);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200872 tmp_skb = bat_priv->vis.my_info->skb_packet;
Sven Eckelmann96412692012-06-05 22:31:30 +0200873 packet = (struct batadv_vis_packet *)skb_put(tmp_skb, sizeof(*packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000874
875 /* prefill the vis info */
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200876 first_seen = jiffies - msecs_to_jiffies(BATADV_VIS_INTERVAL);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200877 bat_priv->vis.my_info->first_seen = first_seen;
878 INIT_LIST_HEAD(&bat_priv->vis.my_info->recv_list);
879 INIT_LIST_HEAD(&bat_priv->vis.my_info->send_list);
880 kref_init(&bat_priv->vis.my_info->refcount);
881 bat_priv->vis.my_info->bat_priv = bat_priv;
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200882 packet->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200883 packet->header.packet_type = BATADV_VIS;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200884 packet->header.ttl = BATADV_TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000885 packet->seqno = 0;
Sven Eckelmann162d5492012-06-28 11:56:52 +0200886 packet->reserved = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000887 packet->entries = 0;
888
Sven Eckelmann807736f2012-07-15 22:26:51 +0200889 INIT_LIST_HEAD(&bat_priv->vis.send_list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000890
Sven Eckelmann807736f2012-07-15 22:26:51 +0200891 hash_added = batadv_hash_add(bat_priv->vis.hash, batadv_vis_info_cmp,
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200892 batadv_vis_info_choose,
Sven Eckelmann807736f2012-07-15 22:26:51 +0200893 bat_priv->vis.my_info,
894 &bat_priv->vis.my_info->hash_entry);
Antonio Quartulli1a1f37d2011-07-10 00:36:36 +0200895 if (hash_added != 0) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000896 pr_err("Can't add own vis packet into hash\n");
897 /* not in hash, need to remove it manually. */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200898 kref_put(&bat_priv->vis.my_info->refcount, batadv_free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000899 goto err;
900 }
901
Sven Eckelmann807736f2012-07-15 22:26:51 +0200902 spin_unlock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200903 batadv_start_vis_timer(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +0200904 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000905
906free_info:
Sven Eckelmann807736f2012-07-15 22:26:51 +0200907 kfree(bat_priv->vis.my_info);
908 bat_priv->vis.my_info = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000909err:
Sven Eckelmann807736f2012-07-15 22:26:51 +0200910 spin_unlock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200911 batadv_vis_quit(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +0200912 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000913}
914
915/* Decrease the reference count on a hash item info */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200916static void batadv_free_info_ref(struct hlist_node *node, void *arg)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000917{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200918 struct batadv_vis_info *info;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000919
Sven Eckelmann56303d32012-06-05 22:31:31 +0200920 info = container_of(node, struct batadv_vis_info, hash_entry);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200921 batadv_send_list_del(info);
922 kref_put(&info->refcount, batadv_free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000923}
924
925/* shutdown vis-server */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200926void batadv_vis_quit(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000927{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200928 if (!bat_priv->vis.hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000929 return;
930
Sven Eckelmann807736f2012-07-15 22:26:51 +0200931 cancel_delayed_work_sync(&bat_priv->vis.work);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000932
Sven Eckelmann807736f2012-07-15 22:26:51 +0200933 spin_lock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000934 /* properly remove, kill timers ... */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200935 batadv_hash_delete(bat_priv->vis.hash, batadv_free_info_ref, NULL);
936 bat_priv->vis.hash = NULL;
937 bat_priv->vis.my_info = NULL;
938 spin_unlock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000939}
940
941/* schedule packets for (re)transmission */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200942static void batadv_start_vis_timer(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000943{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200944 INIT_DELAYED_WORK(&bat_priv->vis.work, batadv_send_vis_packets);
945 queue_delayed_work(batadv_event_workqueue, &bat_priv->vis.work,
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200946 msecs_to_jiffies(BATADV_VIS_INTERVAL));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000947}