blob: 309493d9128a7ff3c17d55e1cc6eae3e7940344e [file] [log] [blame]
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001/* Copyright (C) 2008-2012 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002 *
3 * Simon Wunderlich
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000018 */
19
20#include "main.h"
21#include "send.h"
22#include "translation-table.h"
23#include "vis.h"
24#include "soft-interface.h"
25#include "hard-interface.h"
26#include "hash.h"
27#include "originator.h"
28
Sven Eckelmann347c80f2012-06-03 22:19:07 +020029#define BATADV_MAX_VIS_PACKET_SIZE 1000
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000030
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;
Sven Eckelmann96412692012-06-05 22:31:30 +020056 const struct batadv_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;
Sven Eckelmann96412692012-06-05 22:31:30 +020060 p1 = (struct batadv_vis_packet *)d1->skb_packet->data;
61 p2 = (struct batadv_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;
Sven Eckelmann96412692012-06-05 22:31:30 +020071 const struct batadv_vis_packet *packet;
Sven Eckelmann747e4222011-05-14 23:14:50 +020072 const unsigned char *key;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000073 uint32_t hash = 0;
74 size_t i;
75
Sven Eckelmann96412692012-06-05 22:31:30 +020076 packet = (struct batadv_vis_packet *)vis_info->skb_packet->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000077 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{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020094 struct batadv_hashtable *hash = bat_priv->vis_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +000095 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 Eckelmann28afd3c2012-05-16 20:23:23 +0200143static void batadv_vis_data_read_prim_sec(struct seq_file *seq,
144 const struct hlist_head *if_list)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000145{
146 struct if_list_entry *entry;
147 struct hlist_node *pos;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000148
149 hlist_for_each_entry(entry, pos, if_list, list) {
150 if (entry->primary)
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200151 seq_printf(seq, "PRIMARY, ");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000152 else
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200153 seq_printf(seq, "SEC %pM, ", entry->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000154 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000155}
156
157/* read an entry */
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200158static ssize_t batadv_vis_data_read_entry(struct seq_file *seq,
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200159 const struct vis_info_entry *entry,
160 const uint8_t *src, bool primary)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000161{
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000162 if (primary && entry->quality == 0)
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200163 return seq_printf(seq, "TT %pM, ", entry->dest);
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200164 else if (batadv_compare_eth(entry->src, src))
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200165 return seq_printf(seq, "TQ %pM %d, ", entry->dest,
166 entry->quality);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000167
168 return 0;
169}
170
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200171static void batadv_vis_data_insert_interfaces(struct hlist_head *list,
Sven Eckelmann96412692012-06-05 22:31:30 +0200172 struct batadv_vis_packet *packet,
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200173 struct vis_info_entry *entries)
174{
175 int i;
176
177 for (i = 0; i < packet->entries; i++) {
178 if (entries[i].quality == 0)
179 continue;
180
181 if (batadv_compare_eth(entries[i].src, packet->vis_orig))
182 continue;
183
184 batadv_vis_data_insert_interface(entries[i].src, list, false);
185 }
186}
187
188static void batadv_vis_data_read_entries(struct seq_file *seq,
189 struct hlist_head *list,
Sven Eckelmann96412692012-06-05 22:31:30 +0200190 struct batadv_vis_packet *packet,
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200191 struct vis_info_entry *entries)
192{
193 int i;
194 struct if_list_entry *entry;
195 struct hlist_node *pos;
196
197 hlist_for_each_entry(entry, pos, list, list) {
198 seq_printf(seq, "%pM,", entry->addr);
199
200 for (i = 0; i < packet->entries; i++)
201 batadv_vis_data_read_entry(seq, &entries[i],
202 entry->addr, entry->primary);
203
204 /* add primary/secondary records */
205 if (batadv_compare_eth(entry->addr, packet->vis_orig))
206 batadv_vis_data_read_prim_sec(seq, list);
207
208 seq_printf(seq, "\n");
209 }
210}
211
212static void batadv_vis_seq_print_text_bucket(struct seq_file *seq,
213 const struct hlist_head *head)
214{
215 struct hlist_node *node;
216 struct vis_info *info;
Sven Eckelmann96412692012-06-05 22:31:30 +0200217 struct batadv_vis_packet *packet;
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200218 uint8_t *entries_pos;
219 struct vis_info_entry *entries;
220 struct if_list_entry *entry;
221 struct hlist_node *pos, *n;
222
223 HLIST_HEAD(vis_if_list);
224
225 hlist_for_each_entry_rcu(info, node, head, hash_entry) {
Sven Eckelmann96412692012-06-05 22:31:30 +0200226 packet = (struct batadv_vis_packet *)info->skb_packet->data;
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200227 entries_pos = (uint8_t *)packet + sizeof(*packet);
228 entries = (struct vis_info_entry *)entries_pos;
229
230 batadv_vis_data_insert_interface(packet->vis_orig, &vis_if_list,
231 true);
232 batadv_vis_data_insert_interfaces(&vis_if_list, packet,
233 entries);
234 batadv_vis_data_read_entries(seq, &vis_if_list, packet,
235 entries);
236
237 hlist_for_each_entry_safe(entry, pos, n, &vis_if_list, list) {
238 hlist_del(&entry->list);
239 kfree(entry);
240 }
241 }
242}
243
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200244int batadv_vis_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000245{
Marek Lindner32ae9b22011-04-20 15:40:58 +0200246 struct hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000247 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000248 struct net_device *net_dev = (struct net_device *)seq->private;
249 struct bat_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200250 struct batadv_hashtable *hash = bat_priv->vis_hash;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200251 uint32_t i;
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200252 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000253 int vis_server = atomic_read(&bat_priv->vis_mode);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000254
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200255 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200256 if (!primary_if)
257 goto out;
258
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200259 if (vis_server == BATADV_VIS_TYPE_CLIENT_UPDATE)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200260 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000261
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000262 spin_lock_bh(&bat_priv->vis_hash_lock);
263 for (i = 0; i < hash->size; i++) {
264 head = &hash->table[i];
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200265 batadv_vis_seq_print_text_bucket(seq, head);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000266 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000267 spin_unlock_bh(&bat_priv->vis_hash_lock);
268
Marek Lindner32ae9b22011-04-20 15:40:58 +0200269out:
270 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200271 batadv_hardif_free_ref(primary_if);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200272 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000273}
274
275/* add the info packet to the send list, if it was not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200276 * already linked in.
277 */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200278static void batadv_send_list_add(struct bat_priv *bat_priv,
279 struct vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000280{
281 if (list_empty(&info->send_list)) {
282 kref_get(&info->refcount);
283 list_add_tail(&info->send_list, &bat_priv->vis_send_list);
284 }
285}
286
287/* delete the info packet from the send list, if it was
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200288 * linked in.
289 */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200290static void batadv_send_list_del(struct vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000291{
292 if (!list_empty(&info->send_list)) {
293 list_del_init(&info->send_list);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200294 kref_put(&info->refcount, batadv_free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000295 }
296}
297
298/* tries to add one entry to the receive list. */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200299static void batadv_recv_list_add(struct bat_priv *bat_priv,
300 struct list_head *recv_list, const char *mac)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000301{
302 struct recvlist_node *entry;
303
Sven Eckelmann704509b2011-05-14 23:14:54 +0200304 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000305 if (!entry)
306 return;
307
308 memcpy(entry->mac, mac, ETH_ALEN);
309 spin_lock_bh(&bat_priv->vis_list_lock);
310 list_add_tail(&entry->list, recv_list);
311 spin_unlock_bh(&bat_priv->vis_list_lock);
312}
313
314/* returns 1 if this mac is in the recv_list */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200315static int batadv_recv_list_is_in(struct bat_priv *bat_priv,
316 const struct list_head *recv_list,
317 const char *mac)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000318{
Sven Eckelmann747e4222011-05-14 23:14:50 +0200319 const struct recvlist_node *entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000320
321 spin_lock_bh(&bat_priv->vis_list_lock);
322 list_for_each_entry(entry, recv_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200323 if (batadv_compare_eth(entry->mac, mac)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000324 spin_unlock_bh(&bat_priv->vis_list_lock);
325 return 1;
326 }
327 }
328 spin_unlock_bh(&bat_priv->vis_list_lock);
329 return 0;
330}
331
332/* try to add the packet to the vis_hash. return NULL if invalid (e.g. too old,
333 * broken.. ). vis hash must be locked outside. is_new is set when the packet
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200334 * is newer than old entries in the hash.
335 */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200336static struct vis_info *batadv_add_packet(struct bat_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +0200337 struct batadv_vis_packet *vis_packet,
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200338 int vis_info_len, int *is_new,
339 int make_broadcast)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000340{
341 struct vis_info *info, *old_info;
Sven Eckelmann96412692012-06-05 22:31:30 +0200342 struct batadv_vis_packet *search_packet, *old_packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000343 struct vis_info search_elem;
Sven Eckelmann96412692012-06-05 22:31:30 +0200344 struct batadv_vis_packet *packet;
345 struct sk_buff *tmp_skb;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000346 int hash_added;
Sven Eckelmann96412692012-06-05 22:31:30 +0200347 size_t len;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000348
349 *is_new = 0;
350 /* sanity check */
351 if (!bat_priv->vis_hash)
352 return NULL;
353
354 /* see if the packet is already in vis_hash */
Sven Eckelmann704509b2011-05-14 23:14:54 +0200355 search_elem.skb_packet = dev_alloc_skb(sizeof(*search_packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000356 if (!search_elem.skb_packet)
357 return NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200358 len = sizeof(*search_packet);
359 tmp_skb = search_elem.skb_packet;
360 search_packet = (struct batadv_vis_packet *)skb_put(tmp_skb, len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000361
362 memcpy(search_packet->vis_orig, vis_packet->vis_orig, ETH_ALEN);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200363 old_info = batadv_vis_hash_find(bat_priv, &search_elem);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000364 kfree_skb(search_elem.skb_packet);
365
366 if (old_info) {
Sven Eckelmann96412692012-06-05 22:31:30 +0200367 tmp_skb = old_info->skb_packet;
368 old_packet = (struct batadv_vis_packet *)tmp_skb->data;
Sven Eckelmann3e348192012-05-16 20:23:22 +0200369 if (!batadv_seq_after(ntohl(vis_packet->seqno),
370 ntohl(old_packet->seqno))) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000371 if (old_packet->seqno == vis_packet->seqno) {
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200372 batadv_recv_list_add(bat_priv,
373 &old_info->recv_list,
374 vis_packet->sender_orig);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000375 return old_info;
376 } else {
377 /* newer packet is already in hash. */
378 return NULL;
379 }
380 }
381 /* remove old entry */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200382 batadv_hash_remove(bat_priv->vis_hash, batadv_vis_info_cmp,
383 batadv_vis_info_choose, old_info);
384 batadv_send_list_del(old_info);
385 kref_put(&old_info->refcount, batadv_free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000386 }
387
Sven Eckelmann704509b2011-05-14 23:14:54 +0200388 info = kmalloc(sizeof(*info), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000389 if (!info)
390 return NULL;
391
Sven Eckelmann96412692012-06-05 22:31:30 +0200392 len = sizeof(*packet) + vis_info_len;
393 info->skb_packet = dev_alloc_skb(len + ETH_HLEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000394 if (!info->skb_packet) {
395 kfree(info);
396 return NULL;
397 }
Antonio Quartulli0d125072012-02-18 11:27:34 +0100398 skb_reserve(info->skb_packet, ETH_HLEN);
Sven Eckelmann96412692012-06-05 22:31:30 +0200399 packet = (struct batadv_vis_packet *)skb_put(info->skb_packet, len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000400
401 kref_init(&info->refcount);
402 INIT_LIST_HEAD(&info->send_list);
403 INIT_LIST_HEAD(&info->recv_list);
404 info->first_seen = jiffies;
405 info->bat_priv = bat_priv;
Sven Eckelmann96412692012-06-05 22:31:30 +0200406 memcpy(packet, vis_packet, len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000407
408 /* initialize and add new packet. */
409 *is_new = 1;
410
411 /* Make it a broadcast packet, if required */
412 if (make_broadcast)
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200413 memcpy(packet->target_orig, batadv_broadcast_addr, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000414
415 /* repair if entries is longer than packet. */
416 if (packet->entries * sizeof(struct vis_info_entry) > vis_info_len)
417 packet->entries = vis_info_len / sizeof(struct vis_info_entry);
418
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200419 batadv_recv_list_add(bat_priv, &info->recv_list, packet->sender_orig);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000420
421 /* try to add it */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200422 hash_added = batadv_hash_add(bat_priv->vis_hash, batadv_vis_info_cmp,
423 batadv_vis_info_choose, info,
424 &info->hash_entry);
Antonio Quartulli1a1f37d2011-07-10 00:36:36 +0200425 if (hash_added != 0) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000426 /* did not work (for some reason) */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200427 kref_put(&info->refcount, batadv_free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000428 info = NULL;
429 }
430
431 return info;
432}
433
434/* handle the server sync packet, forward if needed. */
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200435void batadv_receive_server_sync_packet(struct bat_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +0200436 struct batadv_vis_packet *vis_packet,
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200437 int vis_info_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000438{
439 struct vis_info *info;
440 int is_new, make_broadcast;
441 int vis_server = atomic_read(&bat_priv->vis_mode);
442
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200443 make_broadcast = (vis_server == BATADV_VIS_TYPE_SERVER_SYNC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000444
445 spin_lock_bh(&bat_priv->vis_hash_lock);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200446 info = batadv_add_packet(bat_priv, vis_packet, vis_info_len,
447 &is_new, make_broadcast);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000448 if (!info)
449 goto end;
450
451 /* only if we are server ourselves and packet is newer than the one in
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200452 * hash.
453 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200454 if (vis_server == BATADV_VIS_TYPE_SERVER_SYNC && is_new)
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200455 batadv_send_list_add(bat_priv, info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000456end:
457 spin_unlock_bh(&bat_priv->vis_hash_lock);
458}
459
460/* handle an incoming client update packet and schedule forward if needed. */
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200461void batadv_receive_client_update_packet(struct bat_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +0200462 struct batadv_vis_packet *vis_packet,
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200463 int vis_info_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000464{
465 struct vis_info *info;
Sven Eckelmann96412692012-06-05 22:31:30 +0200466 struct batadv_vis_packet *packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000467 int is_new;
468 int vis_server = atomic_read(&bat_priv->vis_mode);
469 int are_target = 0;
470
471 /* clients shall not broadcast. */
472 if (is_broadcast_ether_addr(vis_packet->target_orig))
473 return;
474
475 /* Are we the target for this VIS packet? */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200476 if (vis_server == BATADV_VIS_TYPE_SERVER_SYNC &&
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200477 batadv_is_my_mac(vis_packet->target_orig))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000478 are_target = 1;
479
480 spin_lock_bh(&bat_priv->vis_hash_lock);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200481 info = batadv_add_packet(bat_priv, vis_packet, vis_info_len,
482 &is_new, are_target);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000483
484 if (!info)
485 goto end;
486 /* note that outdated packets will be dropped at this point. */
487
Sven Eckelmann96412692012-06-05 22:31:30 +0200488 packet = (struct batadv_vis_packet *)info->skb_packet->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000489
490 /* send only if we're the target server or ... */
491 if (are_target && is_new) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200492 packet->vis_type = BATADV_VIS_TYPE_SERVER_SYNC; /* upgrade! */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200493 batadv_send_list_add(bat_priv, info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000494
495 /* ... we're not the recipient (and thus need to forward). */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200496 } else if (!batadv_is_my_mac(packet->target_orig)) {
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200497 batadv_send_list_add(bat_priv, info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000498 }
499
500end:
501 spin_unlock_bh(&bat_priv->vis_hash_lock);
502}
503
504/* Walk the originators and find the VIS server with the best tq. Set the packet
505 * address to its address and return the best_tq.
506 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200507 * Must be called with the originator hash locked
508 */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200509static int batadv_find_best_vis_server(struct bat_priv *bat_priv,
510 struct vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000511{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200512 struct batadv_hashtable *hash = bat_priv->orig_hash;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000513 struct neigh_node *router;
Marek Lindner7aadf882011-02-18 12:28:09 +0000514 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000515 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000516 struct orig_node *orig_node;
Sven Eckelmann96412692012-06-05 22:31:30 +0200517 struct batadv_vis_packet *packet;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200518 int best_tq = -1;
519 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000520
Sven Eckelmann96412692012-06-05 22:31:30 +0200521 packet = (struct batadv_vis_packet *)info->skb_packet->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000522
523 for (i = 0; i < hash->size; i++) {
524 head = &hash->table[i];
525
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000526 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000527 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200528 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000529 if (!router)
530 continue;
531
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200532 if ((orig_node->flags & BATADV_VIS_SERVER) &&
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000533 (router->tq_avg > best_tq)) {
534 best_tq = router->tq_avg;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000535 memcpy(packet->target_orig, orig_node->orig,
536 ETH_ALEN);
537 }
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200538 batadv_neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000539 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000540 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000541 }
542
543 return best_tq;
544}
545
546/* Return true if the vis packet is full. */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200547static bool batadv_vis_packet_full(const struct vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000548{
Sven Eckelmann96412692012-06-05 22:31:30 +0200549 const struct batadv_vis_packet *packet;
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200550 size_t num_items;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000551
Sven Eckelmann96412692012-06-05 22:31:30 +0200552 packet = (struct batadv_vis_packet *)info->skb_packet->data;
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200553 num_items = BATADV_MAX_VIS_PACKET_SIZE / sizeof(struct vis_info_entry);
554
555 if (num_items < packet->entries + 1)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000556 return true;
557 return false;
558}
559
560/* generates a packet of own vis data,
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200561 * returns 0 on success, -1 if no packet could be generated
562 */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200563static int batadv_generate_vis_packet(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000564{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200565 struct batadv_hashtable *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000566 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000567 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000568 struct orig_node *orig_node;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000569 struct neigh_node *router;
Sven Eckelmann958ca592011-05-14 23:14:53 +0200570 struct vis_info *info = bat_priv->my_vis_info;
Sven Eckelmann96412692012-06-05 22:31:30 +0200571 struct batadv_vis_packet *packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000572 struct vis_info_entry *entry;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100573 struct tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200574 int best_tq = -1;
575 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000576
577 info->first_seen = jiffies;
Sven Eckelmann96412692012-06-05 22:31:30 +0200578 packet = (struct batadv_vis_packet *)info->skb_packet->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000579 packet->vis_type = atomic_read(&bat_priv->vis_mode);
580
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200581 memcpy(packet->target_orig, batadv_broadcast_addr, ETH_ALEN);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200582 packet->header.ttl = BATADV_TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000583 packet->seqno = htonl(ntohl(packet->seqno) + 1);
584 packet->entries = 0;
Sven Eckelmann704509b2011-05-14 23:14:54 +0200585 skb_trim(info->skb_packet, sizeof(*packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000586
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200587 if (packet->vis_type == BATADV_VIS_TYPE_CLIENT_UPDATE) {
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200588 best_tq = batadv_find_best_vis_server(bat_priv, info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000589
Marek Lindnerd0072602011-01-19 20:01:44 +0000590 if (best_tq < 0)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200591 return best_tq;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000592 }
593
594 for (i = 0; i < hash->size; i++) {
595 head = &hash->table[i];
596
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000597 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000598 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200599 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000600 if (!router)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000601 continue;
602
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200603 if (!batadv_compare_eth(router->addr, orig_node->orig))
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000604 goto next;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000605
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200606 if (router->if_incoming->if_status != BATADV_IF_ACTIVE)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000607 goto next;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000608
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000609 if (router->tq_avg < 1)
610 goto next;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000611
612 /* fill one entry into buffer. */
613 entry = (struct vis_info_entry *)
614 skb_put(info->skb_packet, sizeof(*entry));
615 memcpy(entry->src,
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000616 router->if_incoming->net_dev->dev_addr,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000617 ETH_ALEN);
618 memcpy(entry->dest, orig_node->orig, ETH_ALEN);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000619 entry->quality = router->tq_avg;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000620 packet->entries++;
621
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000622next:
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200623 batadv_neigh_node_free_ref(router);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000624
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200625 if (batadv_vis_packet_full(info))
Marek Lindnerd0072602011-01-19 20:01:44 +0000626 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000627 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000628 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000629 }
630
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200631 hash = bat_priv->tt_local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000632
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000633 for (i = 0; i < hash->size; i++) {
634 head = &hash->table[i];
635
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200636 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100637 hlist_for_each_entry_rcu(tt_common_entry, node, head,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200638 hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000639 entry = (struct vis_info_entry *)
640 skb_put(info->skb_packet,
641 sizeof(*entry));
642 memset(entry->src, 0, ETH_ALEN);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100643 memcpy(entry->dest, tt_common_entry->addr, ETH_ALEN);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200644 entry->quality = 0; /* 0 means TT */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000645 packet->entries++;
646
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200647 if (batadv_vis_packet_full(info))
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200648 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000649 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200650 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000651 }
652
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000653 return 0;
Marek Lindnerd0072602011-01-19 20:01:44 +0000654
655unlock:
656 rcu_read_unlock();
657 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000658}
659
660/* free old vis packets. Must be called with this vis_hash_lock
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200661 * held
662 */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200663static void batadv_purge_vis_packets(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000664{
Antonio Quartullic90681b2011-10-05 17:05:25 +0200665 uint32_t i;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200666 struct batadv_hashtable *hash = bat_priv->vis_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000667 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000668 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000669 struct vis_info *info;
670
671 for (i = 0; i < hash->size; i++) {
672 head = &hash->table[i];
673
Marek Lindner7aadf882011-02-18 12:28:09 +0000674 hlist_for_each_entry_safe(info, node, node_tmp,
675 head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000676 /* never purge own data. */
677 if (info == bat_priv->my_vis_info)
678 continue;
679
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200680 if (batadv_has_timed_out(info->first_seen,
Sven Eckelmannedbf7ff2012-06-03 22:19:16 +0200681 BATADV_VIS_TIMEOUT)) {
Marek Lindner7aadf882011-02-18 12:28:09 +0000682 hlist_del(node);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200683 batadv_send_list_del(info);
684 kref_put(&info->refcount, batadv_free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000685 }
686 }
687 }
688}
689
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200690static void batadv_broadcast_vis_packet(struct bat_priv *bat_priv,
691 struct vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000692{
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000693 struct neigh_node *router;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200694 struct batadv_hashtable *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000695 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000696 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000697 struct orig_node *orig_node;
Sven Eckelmann96412692012-06-05 22:31:30 +0200698 struct batadv_vis_packet *packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000699 struct sk_buff *skb;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000700 struct hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000701 uint8_t dstaddr[ETH_ALEN];
Antonio Quartullic90681b2011-10-05 17:05:25 +0200702 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000703
704
Sven Eckelmann96412692012-06-05 22:31:30 +0200705 packet = (struct batadv_vis_packet *)info->skb_packet->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000706
707 /* send to all routers in range. */
708 for (i = 0; i < hash->size; i++) {
709 head = &hash->table[i];
710
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000711 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000712 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000713 /* if it's a vis server and reachable, send it. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200714 if (!(orig_node->flags & BATADV_VIS_SERVER))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000715 continue;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000716
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200717 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000718 if (!router)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000719 continue;
720
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000721 /* don't send it if we already received the packet from
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200722 * this node.
723 */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200724 if (batadv_recv_list_is_in(bat_priv, &info->recv_list,
725 orig_node->orig)) {
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200726 batadv_neigh_node_free_ref(router);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000727 continue;
728 }
729
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000730 memcpy(packet->target_orig, orig_node->orig, ETH_ALEN);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000731 hard_iface = router->if_incoming;
732 memcpy(dstaddr, router->addr, ETH_ALEN);
733
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200734 batadv_neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000735
736 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
737 if (skb)
Sven Eckelmann9455e342012-05-12 02:09:37 +0200738 batadv_send_skb_packet(skb, hard_iface,
739 dstaddr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000740
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000741 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000742 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000743 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000744}
745
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200746static void batadv_unicast_vis_packet(struct bat_priv *bat_priv,
747 struct vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000748{
749 struct orig_node *orig_node;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000750 struct neigh_node *router = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000751 struct sk_buff *skb;
Sven Eckelmann96412692012-06-05 22:31:30 +0200752 struct batadv_vis_packet *packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000753
Sven Eckelmann96412692012-06-05 22:31:30 +0200754 packet = (struct batadv_vis_packet *)info->skb_packet->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000755
Sven Eckelmannda641192012-05-12 13:48:56 +0200756 orig_node = batadv_orig_hash_find(bat_priv, packet->target_orig);
Marek Lindner44524fc2011-02-10 14:33:53 +0000757 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000758 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000759
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200760 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000761 if (!router)
762 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000763
764 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
765 if (skb)
Sven Eckelmann9455e342012-05-12 02:09:37 +0200766 batadv_send_skb_packet(skb, router->if_incoming, router->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000767
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000768out:
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000769 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200770 batadv_neigh_node_free_ref(router);
Marek Lindner44524fc2011-02-10 14:33:53 +0000771 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200772 batadv_orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000773}
774
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200775/* only send one vis packet. called from batadv_send_vis_packets() */
776static void batadv_send_vis_packet(struct bat_priv *bat_priv,
777 struct vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000778{
Marek Lindner32ae9b22011-04-20 15:40:58 +0200779 struct hard_iface *primary_if;
Sven Eckelmann96412692012-06-05 22:31:30 +0200780 struct batadv_vis_packet *packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000781
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200782 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200783 if (!primary_if)
784 goto out;
785
Sven Eckelmann96412692012-06-05 22:31:30 +0200786 packet = (struct batadv_vis_packet *)info->skb_packet->data;
Sven Eckelmann76543d12011-11-20 15:47:38 +0100787 if (packet->header.ttl < 2) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000788 pr_debug("Error - can't send vis packet: ttl exceeded\n");
Marek Lindner32ae9b22011-04-20 15:40:58 +0200789 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000790 }
791
Marek Lindner32ae9b22011-04-20 15:40:58 +0200792 memcpy(packet->sender_orig, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmann76543d12011-11-20 15:47:38 +0100793 packet->header.ttl--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000794
795 if (is_broadcast_ether_addr(packet->target_orig))
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200796 batadv_broadcast_vis_packet(bat_priv, info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000797 else
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200798 batadv_unicast_vis_packet(bat_priv, info);
Sven Eckelmann76543d12011-11-20 15:47:38 +0100799 packet->header.ttl++; /* restore TTL */
Marek Lindner32ae9b22011-04-20 15:40:58 +0200800
801out:
802 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200803 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000804}
805
806/* called from timer; send (and maybe generate) vis packet. */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200807static void batadv_send_vis_packets(struct work_struct *work)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000808{
809 struct delayed_work *delayed_work =
810 container_of(work, struct delayed_work, work);
811 struct bat_priv *bat_priv =
812 container_of(delayed_work, struct bat_priv, vis_work);
Sven Eckelmann1181e1d2011-01-28 18:34:07 +0100813 struct vis_info *info;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000814
815 spin_lock_bh(&bat_priv->vis_hash_lock);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200816 batadv_purge_vis_packets(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000817
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200818 if (batadv_generate_vis_packet(bat_priv) == 0) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000819 /* schedule if generation was successful */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200820 batadv_send_list_add(bat_priv, bat_priv->my_vis_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000821 }
822
Sven Eckelmann1181e1d2011-01-28 18:34:07 +0100823 while (!list_empty(&bat_priv->vis_send_list)) {
824 info = list_first_entry(&bat_priv->vis_send_list,
825 typeof(*info), send_list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000826
827 kref_get(&info->refcount);
828 spin_unlock_bh(&bat_priv->vis_hash_lock);
829
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200830 batadv_send_vis_packet(bat_priv, info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000831
832 spin_lock_bh(&bat_priv->vis_hash_lock);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200833 batadv_send_list_del(info);
834 kref_put(&info->refcount, batadv_free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000835 }
836 spin_unlock_bh(&bat_priv->vis_hash_lock);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200837 batadv_start_vis_timer(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000838}
839
840/* init the vis server. this may only be called when if_list is already
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200841 * initialized (e.g. bat0 is initialized, interfaces have been added)
842 */
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200843int batadv_vis_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000844{
Sven Eckelmann96412692012-06-05 22:31:30 +0200845 struct batadv_vis_packet *packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000846 int hash_added;
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200847 unsigned int len;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200848 unsigned long first_seen;
Sven Eckelmann96412692012-06-05 22:31:30 +0200849 struct sk_buff *tmp_skb;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000850
851 if (bat_priv->vis_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200852 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000853
854 spin_lock_bh(&bat_priv->vis_hash_lock);
855
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200856 bat_priv->vis_hash = batadv_hash_new(256);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000857 if (!bat_priv->vis_hash) {
858 pr_err("Can't initialize vis_hash\n");
859 goto err;
860 }
861
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200862 bat_priv->my_vis_info = kmalloc(BATADV_MAX_VIS_PACKET_SIZE, GFP_ATOMIC);
Joe Perches320f4222011-08-29 14:17:24 -0700863 if (!bat_priv->my_vis_info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000864 goto err;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000865
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200866 len = sizeof(*packet) + BATADV_MAX_VIS_PACKET_SIZE + ETH_HLEN;
867 bat_priv->my_vis_info->skb_packet = dev_alloc_skb(len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000868 if (!bat_priv->my_vis_info->skb_packet)
869 goto free_info;
870
Antonio Quartulli0d125072012-02-18 11:27:34 +0100871 skb_reserve(bat_priv->my_vis_info->skb_packet, ETH_HLEN);
Sven Eckelmann96412692012-06-05 22:31:30 +0200872 tmp_skb = bat_priv->my_vis_info->skb_packet;
873 packet = (struct batadv_vis_packet *)skb_put(tmp_skb, sizeof(*packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000874
875 /* prefill the vis info */
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200876 first_seen = jiffies - msecs_to_jiffies(BATADV_VIS_INTERVAL);
877 bat_priv->my_vis_info->first_seen = first_seen;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000878 INIT_LIST_HEAD(&bat_priv->my_vis_info->recv_list);
879 INIT_LIST_HEAD(&bat_priv->my_vis_info->send_list);
880 kref_init(&bat_priv->my_vis_info->refcount);
881 bat_priv->my_vis_info->bat_priv = bat_priv;
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200882 packet->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200883 packet->header.packet_type = BATADV_VIS;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200884 packet->header.ttl = BATADV_TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000885 packet->seqno = 0;
886 packet->entries = 0;
887
888 INIT_LIST_HEAD(&bat_priv->vis_send_list);
889
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200890 hash_added = batadv_hash_add(bat_priv->vis_hash, batadv_vis_info_cmp,
891 batadv_vis_info_choose,
892 bat_priv->my_vis_info,
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200893 &bat_priv->my_vis_info->hash_entry);
Antonio Quartulli1a1f37d2011-07-10 00:36:36 +0200894 if (hash_added != 0) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000895 pr_err("Can't add own vis packet into hash\n");
896 /* not in hash, need to remove it manually. */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200897 kref_put(&bat_priv->my_vis_info->refcount, batadv_free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000898 goto err;
899 }
900
901 spin_unlock_bh(&bat_priv->vis_hash_lock);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200902 batadv_start_vis_timer(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +0200903 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000904
905free_info:
906 kfree(bat_priv->my_vis_info);
907 bat_priv->my_vis_info = NULL;
908err:
909 spin_unlock_bh(&bat_priv->vis_hash_lock);
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200910 batadv_vis_quit(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +0200911 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000912}
913
914/* Decrease the reference count on a hash item info */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200915static void batadv_free_info_ref(struct hlist_node *node, void *arg)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000916{
Marek Lindner7aadf882011-02-18 12:28:09 +0000917 struct vis_info *info;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000918
Marek Lindner7aadf882011-02-18 12:28:09 +0000919 info = container_of(node, struct vis_info, hash_entry);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200920 batadv_send_list_del(info);
921 kref_put(&info->refcount, batadv_free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000922}
923
924/* shutdown vis-server */
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200925void batadv_vis_quit(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000926{
927 if (!bat_priv->vis_hash)
928 return;
929
930 cancel_delayed_work_sync(&bat_priv->vis_work);
931
932 spin_lock_bh(&bat_priv->vis_hash_lock);
933 /* properly remove, kill timers ... */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200934 batadv_hash_delete(bat_priv->vis_hash, batadv_free_info_ref, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000935 bat_priv->vis_hash = NULL;
936 bat_priv->my_vis_info = NULL;
937 spin_unlock_bh(&bat_priv->vis_hash_lock);
938}
939
940/* schedule packets for (re)transmission */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200941static void batadv_start_vis_timer(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000942{
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200943 INIT_DELAYED_WORK(&bat_priv->vis_work, batadv_send_vis_packets);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200944 queue_delayed_work(batadv_event_workqueue, &bat_priv->vis_work,
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200945 msecs_to_jiffies(BATADV_VIS_INTERVAL));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000946}