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