blob: 607b1015a7612b004380d3bcbdbb3549bd7be03d [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
29#define MAX_VIS_PACKET_SIZE 1000
30
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +020031static void batadv_start_vis_timer(struct bat_priv *bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000032
33/* free the info */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +020034static void batadv_free_info(struct kref *ref)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000035{
36 struct vis_info *info = container_of(ref, struct vis_info, refcount);
37 struct bat_priv *bat_priv = info->bat_priv;
38 struct recvlist_node *entry, *tmp;
39
40 list_del_init(&info->send_list);
41 spin_lock_bh(&bat_priv->vis_list_lock);
42 list_for_each_entry_safe(entry, tmp, &info->recv_list, list) {
43 list_del(&entry->list);
44 kfree(entry);
45 }
46
47 spin_unlock_bh(&bat_priv->vis_list_lock);
48 kfree_skb(info->skb_packet);
Sven Eckelmanndda9fc62011-01-28 18:34:06 +010049 kfree(info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000050}
51
52/* Compare two vis packets, used by the hashing algorithm */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +020053static int batadv_vis_info_cmp(const struct hlist_node *node, const void *data2)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000054{
Sven Eckelmann747e4222011-05-14 23:14:50 +020055 const struct vis_info *d1, *d2;
56 const struct vis_packet *p1, *p2;
Marek Lindner7aadf882011-02-18 12:28:09 +000057
58 d1 = container_of(node, struct vis_info, hash_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000059 d2 = data2;
60 p1 = (struct vis_packet *)d1->skb_packet->data;
61 p2 = (struct vis_packet *)d2->skb_packet->data;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +020062 return batadv_compare_eth(p1->vis_orig, p2->vis_orig);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000063}
64
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +020065/* hash function to choose an entry in a hash table of given size
66 * hash algorithm from http://en.wikipedia.org/wiki/Hash_table
67 */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +020068static uint32_t batadv_vis_info_choose(const void *data, uint32_t size)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000069{
Sven Eckelmann747e4222011-05-14 23:14:50 +020070 const struct vis_info *vis_info = data;
71 const struct vis_packet *packet;
72 const unsigned char *key;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000073 uint32_t hash = 0;
74 size_t i;
75
76 packet = (struct vis_packet *)vis_info->skb_packet->data;
77 key = packet->vis_orig;
78 for (i = 0; i < ETH_ALEN; i++) {
79 hash += key[i];
80 hash += (hash << 10);
81 hash ^= (hash >> 6);
82 }
83
84 hash += (hash << 3);
85 hash ^= (hash >> 11);
86 hash += (hash << 15);
87
88 return hash % size;
89}
90
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +020091static struct vis_info *batadv_vis_hash_find(struct bat_priv *bat_priv,
92 const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +000093{
94 struct hashtable_t *hash = bat_priv->vis_hash;
95 struct hlist_head *head;
96 struct hlist_node *node;
97 struct vis_info *vis_info, *vis_info_tmp = NULL;
Antonio Quartullic90681b2011-10-05 17:05:25 +020098 uint32_t index;
Marek Lindner7aadf882011-02-18 12:28:09 +000099
100 if (!hash)
101 return NULL;
102
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200103 index = batadv_vis_info_choose(data, hash->size);
Marek Lindner7aadf882011-02-18 12:28:09 +0000104 head = &hash->table[index];
105
106 rcu_read_lock();
107 hlist_for_each_entry_rcu(vis_info, node, head, hash_entry) {
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200108 if (!batadv_vis_info_cmp(node, data))
Marek Lindner7aadf882011-02-18 12:28:09 +0000109 continue;
110
111 vis_info_tmp = vis_info;
112 break;
113 }
114 rcu_read_unlock();
115
116 return vis_info_tmp;
117}
118
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000119/* insert interface to the list of interfaces of one originator, if it
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200120 * does not already exist in the list
121 */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200122static void batadv_vis_data_insert_interface(const uint8_t *interface,
123 struct hlist_head *if_list,
124 bool primary)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000125{
126 struct if_list_entry *entry;
127 struct hlist_node *pos;
128
129 hlist_for_each_entry(entry, pos, if_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200130 if (batadv_compare_eth(entry->addr, interface))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000131 return;
132 }
133
Antonio Quartulli015758d2011-07-09 17:52:13 +0200134 /* it's a new address, add it to the list */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000135 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
136 if (!entry)
137 return;
138 memcpy(entry->addr, interface, ETH_ALEN);
139 entry->primary = primary;
140 hlist_add_head(&entry->list, if_list);
141}
142
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200143static ssize_t batadv_vis_prim_sec(char *buff, const struct hlist_head *if_list)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000144{
145 struct if_list_entry *entry;
146 struct hlist_node *pos;
147 size_t len = 0;
148
149 hlist_for_each_entry(entry, pos, if_list, list) {
150 if (entry->primary)
151 len += sprintf(buff + len, "PRIMARY, ");
152 else
153 len += sprintf(buff + len, "SEC %pM, ", entry->addr);
154 }
155
156 return len;
157}
158
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200159static size_t batadv_vis_cnt_prim_sec(struct hlist_head *if_list)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000160{
161 struct if_list_entry *entry;
162 struct hlist_node *pos;
163 size_t count = 0;
164
165 hlist_for_each_entry(entry, pos, if_list, list) {
166 if (entry->primary)
167 count += 9;
168 else
169 count += 23;
170 }
171
172 return count;
173}
174
175/* read an entry */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200176static ssize_t batadv_vis_data_read_entry(char *buff,
177 const struct vis_info_entry *entry,
178 const uint8_t *src, bool primary)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000179{
180 /* maximal length: max(4+17+2, 3+17+1+3+2) == 26 */
181 if (primary && entry->quality == 0)
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200182 return sprintf(buff, "TT %pM, ", entry->dest);
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200183 else if (batadv_compare_eth(entry->src, src))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000184 return sprintf(buff, "TQ %pM %d, ", entry->dest,
185 entry->quality);
186
187 return 0;
188}
189
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200190int batadv_vis_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000191{
Marek Lindner32ae9b22011-04-20 15:40:58 +0200192 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000193 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000194 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000195 struct vis_info *info;
196 struct vis_packet *packet;
197 struct vis_info_entry *entries;
198 struct net_device *net_dev = (struct net_device *)seq->private;
199 struct bat_priv *bat_priv = netdev_priv(net_dev);
200 struct hashtable_t *hash = bat_priv->vis_hash;
201 HLIST_HEAD(vis_if_list);
202 struct if_list_entry *entry;
203 struct hlist_node *pos, *n;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200204 uint32_t i;
205 int j, ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000206 int vis_server = atomic_read(&bat_priv->vis_mode);
207 size_t buff_pos, buf_size;
208 char *buff;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000209
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200210 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200211 if (!primary_if)
212 goto out;
213
214 if (vis_server == VIS_TYPE_CLIENT_UPDATE)
215 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000216
217 buf_size = 1;
218 /* Estimate length */
219 spin_lock_bh(&bat_priv->vis_hash_lock);
220 for (i = 0; i < hash->size; i++) {
221 head = &hash->table[i];
222
Marek Lindner7aadf882011-02-18 12:28:09 +0000223 rcu_read_lock();
224 hlist_for_each_entry_rcu(info, node, head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000225 packet = (struct vis_packet *)info->skb_packet->data;
226 entries = (struct vis_info_entry *)
Sven Eckelmann704509b2011-05-14 23:14:54 +0200227 ((char *)packet + sizeof(*packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000228
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200229 batadv_vis_data_insert_interface(packet->vis_orig,
230 &vis_if_list, true);
Matthias Schifferbeeb96a2012-05-05 17:51:53 +0200231
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000232 for (j = 0; j < packet->entries; j++) {
233 if (entries[j].quality == 0)
234 continue;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200235 if (batadv_compare_eth(entries[j].src,
236 packet->vis_orig))
Matthias Schifferbeeb96a2012-05-05 17:51:53 +0200237 continue;
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200238 batadv_vis_data_insert_interface(entries[j].src,
239 &vis_if_list,
240 false);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000241 }
242
243 hlist_for_each_entry(entry, pos, &vis_if_list, list) {
244 buf_size += 18 + 26 * packet->entries;
245
246 /* add primary/secondary records */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200247 if (batadv_compare_eth(entry->addr,
248 packet->vis_orig))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000249 buf_size +=
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200250 batadv_vis_cnt_prim_sec(&vis_if_list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000251
252 buf_size += 1;
253 }
254
255 hlist_for_each_entry_safe(entry, pos, n, &vis_if_list,
256 list) {
257 hlist_del(&entry->list);
258 kfree(entry);
259 }
260 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000261 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000262 }
263
264 buff = kmalloc(buf_size, GFP_ATOMIC);
265 if (!buff) {
266 spin_unlock_bh(&bat_priv->vis_hash_lock);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200267 ret = -ENOMEM;
268 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000269 }
270 buff[0] = '\0';
271 buff_pos = 0;
272
273 for (i = 0; i < hash->size; i++) {
274 head = &hash->table[i];
275
Marek Lindner7aadf882011-02-18 12:28:09 +0000276 rcu_read_lock();
277 hlist_for_each_entry_rcu(info, node, head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000278 packet = (struct vis_packet *)info->skb_packet->data;
279 entries = (struct vis_info_entry *)
Sven Eckelmann704509b2011-05-14 23:14:54 +0200280 ((char *)packet + sizeof(*packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000281
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200282 batadv_vis_data_insert_interface(packet->vis_orig,
283 &vis_if_list, true);
Matthias Schifferbeeb96a2012-05-05 17:51:53 +0200284
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000285 for (j = 0; j < packet->entries; j++) {
286 if (entries[j].quality == 0)
287 continue;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200288 if (batadv_compare_eth(entries[j].src,
289 packet->vis_orig))
Matthias Schifferbeeb96a2012-05-05 17:51:53 +0200290 continue;
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200291 batadv_vis_data_insert_interface(entries[j].src,
292 &vis_if_list,
293 false);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000294 }
295
296 hlist_for_each_entry(entry, pos, &vis_if_list, list) {
297 buff_pos += sprintf(buff + buff_pos, "%pM,",
298 entry->addr);
299
Linus Lüssingdd58ddc2011-01-25 21:56:16 +0000300 for (j = 0; j < packet->entries; j++)
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200301 buff_pos += batadv_vis_data_read_entry(
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000302 buff + buff_pos,
Linus Lüssingdd58ddc2011-01-25 21:56:16 +0000303 &entries[j],
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000304 entry->addr,
305 entry->primary);
306
307 /* add primary/secondary records */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200308 if (batadv_compare_eth(entry->addr,
309 packet->vis_orig))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000310 buff_pos +=
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200311 batadv_vis_prim_sec(buff + buff_pos,
312 &vis_if_list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000313
314 buff_pos += sprintf(buff + buff_pos, "\n");
315 }
316
317 hlist_for_each_entry_safe(entry, pos, n, &vis_if_list,
318 list) {
319 hlist_del(&entry->list);
320 kfree(entry);
321 }
322 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000323 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000324 }
325
326 spin_unlock_bh(&bat_priv->vis_hash_lock);
327
328 seq_printf(seq, "%s", buff);
329 kfree(buff);
330
Marek Lindner32ae9b22011-04-20 15:40:58 +0200331out:
332 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200333 batadv_hardif_free_ref(primary_if);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200334 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000335}
336
337/* add the info packet to the send list, if it was not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200338 * already linked in.
339 */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200340static void batadv_send_list_add(struct bat_priv *bat_priv,
341 struct vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000342{
343 if (list_empty(&info->send_list)) {
344 kref_get(&info->refcount);
345 list_add_tail(&info->send_list, &bat_priv->vis_send_list);
346 }
347}
348
349/* delete the info packet from the send list, if it was
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200350 * linked in.
351 */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200352static void batadv_send_list_del(struct vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000353{
354 if (!list_empty(&info->send_list)) {
355 list_del_init(&info->send_list);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200356 kref_put(&info->refcount, batadv_free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000357 }
358}
359
360/* tries to add one entry to the receive list. */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200361static void batadv_recv_list_add(struct bat_priv *bat_priv,
362 struct list_head *recv_list, const char *mac)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000363{
364 struct recvlist_node *entry;
365
Sven Eckelmann704509b2011-05-14 23:14:54 +0200366 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000367 if (!entry)
368 return;
369
370 memcpy(entry->mac, mac, ETH_ALEN);
371 spin_lock_bh(&bat_priv->vis_list_lock);
372 list_add_tail(&entry->list, recv_list);
373 spin_unlock_bh(&bat_priv->vis_list_lock);
374}
375
376/* returns 1 if this mac is in the recv_list */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200377static int batadv_recv_list_is_in(struct bat_priv *bat_priv,
378 const struct list_head *recv_list,
379 const char *mac)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000380{
Sven Eckelmann747e4222011-05-14 23:14:50 +0200381 const struct recvlist_node *entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000382
383 spin_lock_bh(&bat_priv->vis_list_lock);
384 list_for_each_entry(entry, recv_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200385 if (batadv_compare_eth(entry->mac, mac)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000386 spin_unlock_bh(&bat_priv->vis_list_lock);
387 return 1;
388 }
389 }
390 spin_unlock_bh(&bat_priv->vis_list_lock);
391 return 0;
392}
393
394/* try to add the packet to the vis_hash. return NULL if invalid (e.g. too old,
395 * broken.. ). vis hash must be locked outside. is_new is set when the packet
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200396 * is newer than old entries in the hash.
397 */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200398static struct vis_info *batadv_add_packet(struct bat_priv *bat_priv,
399 struct vis_packet *vis_packet,
400 int vis_info_len, int *is_new,
401 int make_broadcast)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000402{
403 struct vis_info *info, *old_info;
404 struct vis_packet *search_packet, *old_packet;
405 struct vis_info search_elem;
406 struct vis_packet *packet;
407 int hash_added;
408
409 *is_new = 0;
410 /* sanity check */
411 if (!bat_priv->vis_hash)
412 return NULL;
413
414 /* see if the packet is already in vis_hash */
Sven Eckelmann704509b2011-05-14 23:14:54 +0200415 search_elem.skb_packet = dev_alloc_skb(sizeof(*search_packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000416 if (!search_elem.skb_packet)
417 return NULL;
418 search_packet = (struct vis_packet *)skb_put(search_elem.skb_packet,
Sven Eckelmann704509b2011-05-14 23:14:54 +0200419 sizeof(*search_packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000420
421 memcpy(search_packet->vis_orig, vis_packet->vis_orig, ETH_ALEN);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200422 old_info = batadv_vis_hash_find(bat_priv, &search_elem);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000423 kfree_skb(search_elem.skb_packet);
424
425 if (old_info) {
426 old_packet = (struct vis_packet *)old_info->skb_packet->data;
427 if (!seq_after(ntohl(vis_packet->seqno),
428 ntohl(old_packet->seqno))) {
429 if (old_packet->seqno == vis_packet->seqno) {
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200430 batadv_recv_list_add(bat_priv,
431 &old_info->recv_list,
432 vis_packet->sender_orig);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000433 return old_info;
434 } else {
435 /* newer packet is already in hash. */
436 return NULL;
437 }
438 }
439 /* remove old entry */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200440 batadv_hash_remove(bat_priv->vis_hash, batadv_vis_info_cmp,
441 batadv_vis_info_choose, old_info);
442 batadv_send_list_del(old_info);
443 kref_put(&old_info->refcount, batadv_free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000444 }
445
Sven Eckelmann704509b2011-05-14 23:14:54 +0200446 info = kmalloc(sizeof(*info), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000447 if (!info)
448 return NULL;
449
Sven Eckelmann704509b2011-05-14 23:14:54 +0200450 info->skb_packet = dev_alloc_skb(sizeof(*packet) + vis_info_len +
Antonio Quartulli0d125072012-02-18 11:27:34 +0100451 ETH_HLEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000452 if (!info->skb_packet) {
453 kfree(info);
454 return NULL;
455 }
Antonio Quartulli0d125072012-02-18 11:27:34 +0100456 skb_reserve(info->skb_packet, ETH_HLEN);
Sven Eckelmann704509b2011-05-14 23:14:54 +0200457 packet = (struct vis_packet *)skb_put(info->skb_packet, sizeof(*packet)
458 + vis_info_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000459
460 kref_init(&info->refcount);
461 INIT_LIST_HEAD(&info->send_list);
462 INIT_LIST_HEAD(&info->recv_list);
463 info->first_seen = jiffies;
464 info->bat_priv = bat_priv;
Sven Eckelmann704509b2011-05-14 23:14:54 +0200465 memcpy(packet, vis_packet, sizeof(*packet) + vis_info_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000466
467 /* initialize and add new packet. */
468 *is_new = 1;
469
470 /* Make it a broadcast packet, if required */
471 if (make_broadcast)
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200472 memcpy(packet->target_orig, batadv_broadcast_addr, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000473
474 /* repair if entries is longer than packet. */
475 if (packet->entries * sizeof(struct vis_info_entry) > vis_info_len)
476 packet->entries = vis_info_len / sizeof(struct vis_info_entry);
477
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200478 batadv_recv_list_add(bat_priv, &info->recv_list, packet->sender_orig);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000479
480 /* try to add it */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200481 hash_added = batadv_hash_add(bat_priv->vis_hash, batadv_vis_info_cmp,
482 batadv_vis_info_choose, info,
483 &info->hash_entry);
Antonio Quartulli1a1f37d2011-07-10 00:36:36 +0200484 if (hash_added != 0) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000485 /* did not work (for some reason) */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200486 kref_put(&info->refcount, batadv_free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000487 info = NULL;
488 }
489
490 return info;
491}
492
493/* handle the server sync packet, forward if needed. */
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200494void batadv_receive_server_sync_packet(struct bat_priv *bat_priv,
495 struct vis_packet *vis_packet,
496 int vis_info_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000497{
498 struct vis_info *info;
499 int is_new, make_broadcast;
500 int vis_server = atomic_read(&bat_priv->vis_mode);
501
502 make_broadcast = (vis_server == VIS_TYPE_SERVER_SYNC);
503
504 spin_lock_bh(&bat_priv->vis_hash_lock);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200505 info = batadv_add_packet(bat_priv, vis_packet, vis_info_len,
506 &is_new, make_broadcast);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000507 if (!info)
508 goto end;
509
510 /* only if we are server ourselves and packet is newer than the one in
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200511 * hash.
512 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000513 if (vis_server == VIS_TYPE_SERVER_SYNC && is_new)
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200514 batadv_send_list_add(bat_priv, info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000515end:
516 spin_unlock_bh(&bat_priv->vis_hash_lock);
517}
518
519/* handle an incoming client update packet and schedule forward if needed. */
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200520void batadv_receive_client_update_packet(struct bat_priv *bat_priv,
521 struct vis_packet *vis_packet,
522 int vis_info_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000523{
524 struct vis_info *info;
525 struct vis_packet *packet;
526 int is_new;
527 int vis_server = atomic_read(&bat_priv->vis_mode);
528 int are_target = 0;
529
530 /* clients shall not broadcast. */
531 if (is_broadcast_ether_addr(vis_packet->target_orig))
532 return;
533
534 /* Are we the target for this VIS packet? */
535 if (vis_server == VIS_TYPE_SERVER_SYNC &&
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200536 batadv_is_my_mac(vis_packet->target_orig))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000537 are_target = 1;
538
539 spin_lock_bh(&bat_priv->vis_hash_lock);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200540 info = batadv_add_packet(bat_priv, vis_packet, vis_info_len,
541 &is_new, are_target);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000542
543 if (!info)
544 goto end;
545 /* note that outdated packets will be dropped at this point. */
546
547 packet = (struct vis_packet *)info->skb_packet->data;
548
549 /* send only if we're the target server or ... */
550 if (are_target && is_new) {
551 packet->vis_type = VIS_TYPE_SERVER_SYNC; /* upgrade! */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200552 batadv_send_list_add(bat_priv, info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000553
554 /* ... we're not the recipient (and thus need to forward). */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200555 } else if (!batadv_is_my_mac(packet->target_orig)) {
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200556 batadv_send_list_add(bat_priv, info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000557 }
558
559end:
560 spin_unlock_bh(&bat_priv->vis_hash_lock);
561}
562
563/* Walk the originators and find the VIS server with the best tq. Set the packet
564 * address to its address and return the best_tq.
565 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200566 * Must be called with the originator hash locked
567 */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200568static int batadv_find_best_vis_server(struct bat_priv *bat_priv,
569 struct vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000570{
571 struct hashtable_t *hash = bat_priv->orig_hash;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000572 struct neigh_node *router;
Marek Lindner7aadf882011-02-18 12:28:09 +0000573 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000574 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000575 struct orig_node *orig_node;
576 struct vis_packet *packet;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200577 int best_tq = -1;
578 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000579
580 packet = (struct vis_packet *)info->skb_packet->data;
581
582 for (i = 0; i < hash->size; i++) {
583 head = &hash->table[i];
584
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000585 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000586 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200587 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000588 if (!router)
589 continue;
590
591 if ((orig_node->flags & VIS_SERVER) &&
592 (router->tq_avg > best_tq)) {
593 best_tq = router->tq_avg;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000594 memcpy(packet->target_orig, orig_node->orig,
595 ETH_ALEN);
596 }
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200597 batadv_neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000598 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000599 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000600 }
601
602 return best_tq;
603}
604
605/* Return true if the vis packet is full. */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200606static bool batadv_vis_packet_full(const struct vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000607{
Sven Eckelmann747e4222011-05-14 23:14:50 +0200608 const struct vis_packet *packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000609 packet = (struct vis_packet *)info->skb_packet->data;
610
611 if (MAX_VIS_PACKET_SIZE / sizeof(struct vis_info_entry)
612 < packet->entries + 1)
613 return true;
614 return false;
615}
616
617/* generates a packet of own vis data,
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200618 * returns 0 on success, -1 if no packet could be generated
619 */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200620static int batadv_generate_vis_packet(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000621{
622 struct hashtable_t *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000623 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000624 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000625 struct orig_node *orig_node;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000626 struct neigh_node *router;
Sven Eckelmann958ca592011-05-14 23:14:53 +0200627 struct vis_info *info = bat_priv->my_vis_info;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000628 struct vis_packet *packet = (struct vis_packet *)info->skb_packet->data;
629 struct vis_info_entry *entry;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100630 struct tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200631 int best_tq = -1;
632 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000633
634 info->first_seen = jiffies;
635 packet->vis_type = atomic_read(&bat_priv->vis_mode);
636
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200637 memcpy(packet->target_orig, batadv_broadcast_addr, ETH_ALEN);
Sven Eckelmann76543d12011-11-20 15:47:38 +0100638 packet->header.ttl = TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000639 packet->seqno = htonl(ntohl(packet->seqno) + 1);
640 packet->entries = 0;
Sven Eckelmann704509b2011-05-14 23:14:54 +0200641 skb_trim(info->skb_packet, sizeof(*packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000642
643 if (packet->vis_type == VIS_TYPE_CLIENT_UPDATE) {
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200644 best_tq = batadv_find_best_vis_server(bat_priv, info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000645
Marek Lindnerd0072602011-01-19 20:01:44 +0000646 if (best_tq < 0)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200647 return best_tq;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000648 }
649
650 for (i = 0; i < hash->size; i++) {
651 head = &hash->table[i];
652
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000653 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000654 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200655 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000656 if (!router)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000657 continue;
658
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200659 if (!batadv_compare_eth(router->addr, orig_node->orig))
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000660 goto next;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000661
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000662 if (router->if_incoming->if_status != IF_ACTIVE)
663 goto next;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000664
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000665 if (router->tq_avg < 1)
666 goto next;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000667
668 /* fill one entry into buffer. */
669 entry = (struct vis_info_entry *)
670 skb_put(info->skb_packet, sizeof(*entry));
671 memcpy(entry->src,
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000672 router->if_incoming->net_dev->dev_addr,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000673 ETH_ALEN);
674 memcpy(entry->dest, orig_node->orig, ETH_ALEN);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000675 entry->quality = router->tq_avg;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000676 packet->entries++;
677
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000678next:
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200679 batadv_neigh_node_free_ref(router);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000680
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200681 if (batadv_vis_packet_full(info))
Marek Lindnerd0072602011-01-19 20:01:44 +0000682 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000683 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000684 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000685 }
686
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200687 hash = bat_priv->tt_local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000688
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000689 for (i = 0; i < hash->size; i++) {
690 head = &hash->table[i];
691
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200692 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100693 hlist_for_each_entry_rcu(tt_common_entry, node, head,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200694 hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000695 entry = (struct vis_info_entry *)
696 skb_put(info->skb_packet,
697 sizeof(*entry));
698 memset(entry->src, 0, ETH_ALEN);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100699 memcpy(entry->dest, tt_common_entry->addr, ETH_ALEN);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200700 entry->quality = 0; /* 0 means TT */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000701 packet->entries++;
702
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200703 if (batadv_vis_packet_full(info))
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200704 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000705 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200706 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000707 }
708
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000709 return 0;
Marek Lindnerd0072602011-01-19 20:01:44 +0000710
711unlock:
712 rcu_read_unlock();
713 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000714}
715
716/* free old vis packets. Must be called with this vis_hash_lock
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200717 * held
718 */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200719static void batadv_purge_vis_packets(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000720{
Antonio Quartullic90681b2011-10-05 17:05:25 +0200721 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000722 struct hashtable_t *hash = bat_priv->vis_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000723 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000724 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000725 struct vis_info *info;
726
727 for (i = 0; i < hash->size; i++) {
728 head = &hash->table[i];
729
Marek Lindner7aadf882011-02-18 12:28:09 +0000730 hlist_for_each_entry_safe(info, node, node_tmp,
731 head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000732 /* never purge own data. */
733 if (info == bat_priv->my_vis_info)
734 continue;
735
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200736 if (batadv_has_timed_out(info->first_seen,
737 VIS_TIMEOUT)) {
Marek Lindner7aadf882011-02-18 12:28:09 +0000738 hlist_del(node);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200739 batadv_send_list_del(info);
740 kref_put(&info->refcount, batadv_free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000741 }
742 }
743 }
744}
745
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200746static void batadv_broadcast_vis_packet(struct bat_priv *bat_priv,
747 struct vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000748{
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000749 struct neigh_node *router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000750 struct hashtable_t *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000751 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000752 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000753 struct orig_node *orig_node;
754 struct vis_packet *packet;
755 struct sk_buff *skb;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000756 struct hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000757 uint8_t dstaddr[ETH_ALEN];
Antonio Quartullic90681b2011-10-05 17:05:25 +0200758 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000759
760
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000761 packet = (struct vis_packet *)info->skb_packet->data;
762
763 /* send to all routers in range. */
764 for (i = 0; i < hash->size; i++) {
765 head = &hash->table[i];
766
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000767 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000768 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000769 /* if it's a vis server and reachable, send it. */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000770 if (!(orig_node->flags & VIS_SERVER))
771 continue;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000772
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200773 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000774 if (!router)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000775 continue;
776
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000777 /* don't send it if we already received the packet from
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200778 * this node.
779 */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200780 if (batadv_recv_list_is_in(bat_priv, &info->recv_list,
781 orig_node->orig)) {
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200782 batadv_neigh_node_free_ref(router);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000783 continue;
784 }
785
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000786 memcpy(packet->target_orig, orig_node->orig, ETH_ALEN);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000787 hard_iface = router->if_incoming;
788 memcpy(dstaddr, router->addr, ETH_ALEN);
789
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200790 batadv_neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000791
792 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
793 if (skb)
Sven Eckelmann9455e342012-05-12 02:09:37 +0200794 batadv_send_skb_packet(skb, hard_iface,
795 dstaddr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000796
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000797 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000798 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000799 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000800}
801
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200802static void batadv_unicast_vis_packet(struct bat_priv *bat_priv,
803 struct vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000804{
805 struct orig_node *orig_node;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000806 struct neigh_node *router = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000807 struct sk_buff *skb;
808 struct vis_packet *packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000809
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000810 packet = (struct vis_packet *)info->skb_packet->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000811
Sven Eckelmannda641192012-05-12 13:48:56 +0200812 orig_node = batadv_orig_hash_find(bat_priv, packet->target_orig);
Marek Lindner44524fc2011-02-10 14:33:53 +0000813 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000814 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000815
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200816 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000817 if (!router)
818 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000819
820 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
821 if (skb)
Sven Eckelmann9455e342012-05-12 02:09:37 +0200822 batadv_send_skb_packet(skb, router->if_incoming, router->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000823
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000824out:
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000825 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200826 batadv_neigh_node_free_ref(router);
Marek Lindner44524fc2011-02-10 14:33:53 +0000827 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200828 batadv_orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000829}
830
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200831/* only send one vis packet. called from batadv_send_vis_packets() */
832static void batadv_send_vis_packet(struct bat_priv *bat_priv,
833 struct vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000834{
Marek Lindner32ae9b22011-04-20 15:40:58 +0200835 struct hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000836 struct vis_packet *packet;
837
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200838 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200839 if (!primary_if)
840 goto out;
841
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000842 packet = (struct vis_packet *)info->skb_packet->data;
Sven Eckelmann76543d12011-11-20 15:47:38 +0100843 if (packet->header.ttl < 2) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000844 pr_debug("Error - can't send vis packet: ttl exceeded\n");
Marek Lindner32ae9b22011-04-20 15:40:58 +0200845 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000846 }
847
Marek Lindner32ae9b22011-04-20 15:40:58 +0200848 memcpy(packet->sender_orig, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmann76543d12011-11-20 15:47:38 +0100849 packet->header.ttl--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000850
851 if (is_broadcast_ether_addr(packet->target_orig))
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200852 batadv_broadcast_vis_packet(bat_priv, info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000853 else
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200854 batadv_unicast_vis_packet(bat_priv, info);
Sven Eckelmann76543d12011-11-20 15:47:38 +0100855 packet->header.ttl++; /* restore TTL */
Marek Lindner32ae9b22011-04-20 15:40:58 +0200856
857out:
858 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200859 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000860}
861
862/* called from timer; send (and maybe generate) vis packet. */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200863static void batadv_send_vis_packets(struct work_struct *work)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000864{
865 struct delayed_work *delayed_work =
866 container_of(work, struct delayed_work, work);
867 struct bat_priv *bat_priv =
868 container_of(delayed_work, struct bat_priv, vis_work);
Sven Eckelmann1181e1d2011-01-28 18:34:07 +0100869 struct vis_info *info;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000870
871 spin_lock_bh(&bat_priv->vis_hash_lock);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200872 batadv_purge_vis_packets(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000873
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200874 if (batadv_generate_vis_packet(bat_priv) == 0) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000875 /* schedule if generation was successful */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200876 batadv_send_list_add(bat_priv, bat_priv->my_vis_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000877 }
878
Sven Eckelmann1181e1d2011-01-28 18:34:07 +0100879 while (!list_empty(&bat_priv->vis_send_list)) {
880 info = list_first_entry(&bat_priv->vis_send_list,
881 typeof(*info), send_list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000882
883 kref_get(&info->refcount);
884 spin_unlock_bh(&bat_priv->vis_hash_lock);
885
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200886 batadv_send_vis_packet(bat_priv, info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000887
888 spin_lock_bh(&bat_priv->vis_hash_lock);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200889 batadv_send_list_del(info);
890 kref_put(&info->refcount, batadv_free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000891 }
892 spin_unlock_bh(&bat_priv->vis_hash_lock);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200893 batadv_start_vis_timer(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000894}
895
896/* init the vis server. this may only be called when if_list is already
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200897 * initialized (e.g. bat0 is initialized, interfaces have been added)
898 */
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200899int batadv_vis_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000900{
901 struct vis_packet *packet;
902 int hash_added;
903
904 if (bat_priv->vis_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200905 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000906
907 spin_lock_bh(&bat_priv->vis_hash_lock);
908
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200909 bat_priv->vis_hash = batadv_hash_new(256);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000910 if (!bat_priv->vis_hash) {
911 pr_err("Can't initialize vis_hash\n");
912 goto err;
913 }
914
915 bat_priv->my_vis_info = kmalloc(MAX_VIS_PACKET_SIZE, GFP_ATOMIC);
Joe Perches320f4222011-08-29 14:17:24 -0700916 if (!bat_priv->my_vis_info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000917 goto err;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000918
Sven Eckelmann704509b2011-05-14 23:14:54 +0200919 bat_priv->my_vis_info->skb_packet = dev_alloc_skb(sizeof(*packet) +
920 MAX_VIS_PACKET_SIZE +
Antonio Quartulli0d125072012-02-18 11:27:34 +0100921 ETH_HLEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000922 if (!bat_priv->my_vis_info->skb_packet)
923 goto free_info;
924
Antonio Quartulli0d125072012-02-18 11:27:34 +0100925 skb_reserve(bat_priv->my_vis_info->skb_packet, ETH_HLEN);
Sven Eckelmann704509b2011-05-14 23:14:54 +0200926 packet = (struct vis_packet *)skb_put(bat_priv->my_vis_info->skb_packet,
927 sizeof(*packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000928
929 /* prefill the vis info */
930 bat_priv->my_vis_info->first_seen = jiffies -
931 msecs_to_jiffies(VIS_INTERVAL);
932 INIT_LIST_HEAD(&bat_priv->my_vis_info->recv_list);
933 INIT_LIST_HEAD(&bat_priv->my_vis_info->send_list);
934 kref_init(&bat_priv->my_vis_info->refcount);
935 bat_priv->my_vis_info->bat_priv = bat_priv;
Sven Eckelmann76543d12011-11-20 15:47:38 +0100936 packet->header.version = COMPAT_VERSION;
937 packet->header.packet_type = BAT_VIS;
938 packet->header.ttl = TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000939 packet->seqno = 0;
940 packet->entries = 0;
941
942 INIT_LIST_HEAD(&bat_priv->vis_send_list);
943
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200944 hash_added = batadv_hash_add(bat_priv->vis_hash, batadv_vis_info_cmp,
945 batadv_vis_info_choose,
946 bat_priv->my_vis_info,
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200947 &bat_priv->my_vis_info->hash_entry);
Antonio Quartulli1a1f37d2011-07-10 00:36:36 +0200948 if (hash_added != 0) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000949 pr_err("Can't add own vis packet into hash\n");
950 /* not in hash, need to remove it manually. */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200951 kref_put(&bat_priv->my_vis_info->refcount, batadv_free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000952 goto err;
953 }
954
955 spin_unlock_bh(&bat_priv->vis_hash_lock);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200956 batadv_start_vis_timer(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +0200957 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000958
959free_info:
960 kfree(bat_priv->my_vis_info);
961 bat_priv->my_vis_info = NULL;
962err:
963 spin_unlock_bh(&bat_priv->vis_hash_lock);
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200964 batadv_vis_quit(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +0200965 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000966}
967
968/* Decrease the reference count on a hash item info */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200969static void batadv_free_info_ref(struct hlist_node *node, void *arg)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000970{
Marek Lindner7aadf882011-02-18 12:28:09 +0000971 struct vis_info *info;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000972
Marek Lindner7aadf882011-02-18 12:28:09 +0000973 info = container_of(node, struct vis_info, hash_entry);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200974 batadv_send_list_del(info);
975 kref_put(&info->refcount, batadv_free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000976}
977
978/* shutdown vis-server */
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200979void batadv_vis_quit(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000980{
981 if (!bat_priv->vis_hash)
982 return;
983
984 cancel_delayed_work_sync(&bat_priv->vis_work);
985
986 spin_lock_bh(&bat_priv->vis_hash_lock);
987 /* properly remove, kill timers ... */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200988 batadv_hash_delete(bat_priv->vis_hash, batadv_free_info_ref, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000989 bat_priv->vis_hash = NULL;
990 bat_priv->my_vis_info = NULL;
991 spin_unlock_bh(&bat_priv->vis_hash_lock);
992}
993
994/* schedule packets for (re)transmission */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200995static void batadv_start_vis_timer(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000996{
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200997 INIT_DELAYED_WORK(&bat_priv->vis_work, batadv_send_vis_packets);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200998 queue_delayed_work(batadv_event_workqueue, &bat_priv->vis_work,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000999 msecs_to_jiffies(VIS_INTERVAL));
1000}