blob: 0f65a9de5f749719b9b778cd8c989680b0b1eb4d [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 Eckelmann56303d32012-06-05 22:31:31 +020031static void batadv_start_vis_timer(struct batadv_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{
Sven Eckelmann56303d32012-06-05 22:31:31 +020036 struct batadv_vis_info *info;
37 struct batadv_priv *bat_priv;
38 struct batadv_recvlist_node *entry, *tmp;
39
40 info = container_of(ref, struct batadv_vis_info, refcount);
41 bat_priv = info->bat_priv;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000042
43 list_del_init(&info->send_list);
Sven Eckelmann807736f2012-07-15 22:26:51 +020044 spin_lock_bh(&bat_priv->vis.list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000045 list_for_each_entry_safe(entry, tmp, &info->recv_list, list) {
46 list_del(&entry->list);
47 kfree(entry);
48 }
49
Sven Eckelmann807736f2012-07-15 22:26:51 +020050 spin_unlock_bh(&bat_priv->vis.list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000051 kfree_skb(info->skb_packet);
Sven Eckelmanndda9fc62011-01-28 18:34:06 +010052 kfree(info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000053}
54
55/* Compare two vis packets, used by the hashing algorithm */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +020056static int batadv_vis_info_cmp(const struct hlist_node *node, const void *data2)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000057{
Sven Eckelmann56303d32012-06-05 22:31:31 +020058 const struct batadv_vis_info *d1, *d2;
Sven Eckelmann96412692012-06-05 22:31:30 +020059 const struct batadv_vis_packet *p1, *p2;
Marek Lindner7aadf882011-02-18 12:28:09 +000060
Sven Eckelmann56303d32012-06-05 22:31:31 +020061 d1 = container_of(node, struct batadv_vis_info, hash_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000062 d2 = data2;
Sven Eckelmann96412692012-06-05 22:31:30 +020063 p1 = (struct batadv_vis_packet *)d1->skb_packet->data;
64 p2 = (struct batadv_vis_packet *)d2->skb_packet->data;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +020065 return batadv_compare_eth(p1->vis_orig, p2->vis_orig);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000066}
67
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +020068/* hash function to choose an entry in a hash table of given size
69 * hash algorithm from http://en.wikipedia.org/wiki/Hash_table
70 */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +020071static uint32_t batadv_vis_info_choose(const void *data, uint32_t size)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000072{
Sven Eckelmann56303d32012-06-05 22:31:31 +020073 const struct batadv_vis_info *vis_info = data;
Sven Eckelmann96412692012-06-05 22:31:30 +020074 const struct batadv_vis_packet *packet;
Sven Eckelmann747e4222011-05-14 23:14:50 +020075 const unsigned char *key;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000076 uint32_t hash = 0;
77 size_t i;
78
Sven Eckelmann96412692012-06-05 22:31:30 +020079 packet = (struct batadv_vis_packet *)vis_info->skb_packet->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000080 key = packet->vis_orig;
81 for (i = 0; i < ETH_ALEN; i++) {
82 hash += key[i];
83 hash += (hash << 10);
84 hash ^= (hash >> 6);
85 }
86
87 hash += (hash << 3);
88 hash ^= (hash >> 11);
89 hash += (hash << 15);
90
91 return hash % size;
92}
93
Sven Eckelmann56303d32012-06-05 22:31:31 +020094static struct batadv_vis_info *
95batadv_vis_hash_find(struct batadv_priv *bat_priv, const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +000096{
Sven Eckelmann807736f2012-07-15 22:26:51 +020097 struct batadv_hashtable *hash = bat_priv->vis.hash;
Marek Lindner7aadf882011-02-18 12:28:09 +000098 struct hlist_head *head;
99 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200100 struct batadv_vis_info *vis_info, *vis_info_tmp = NULL;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200101 uint32_t index;
Marek Lindner7aadf882011-02-18 12:28:09 +0000102
103 if (!hash)
104 return NULL;
105
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200106 index = batadv_vis_info_choose(data, hash->size);
Marek Lindner7aadf882011-02-18 12:28:09 +0000107 head = &hash->table[index];
108
109 rcu_read_lock();
110 hlist_for_each_entry_rcu(vis_info, node, head, hash_entry) {
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200111 if (!batadv_vis_info_cmp(node, data))
Marek Lindner7aadf882011-02-18 12:28:09 +0000112 continue;
113
114 vis_info_tmp = vis_info;
115 break;
116 }
117 rcu_read_unlock();
118
119 return vis_info_tmp;
120}
121
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000122/* insert interface to the list of interfaces of one originator, if it
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200123 * does not already exist in the list
124 */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200125static void batadv_vis_data_insert_interface(const uint8_t *interface,
126 struct hlist_head *if_list,
127 bool primary)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000128{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200129 struct batadv_if_list_entry *entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000130 struct hlist_node *pos;
131
132 hlist_for_each_entry(entry, pos, if_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200133 if (batadv_compare_eth(entry->addr, interface))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000134 return;
135 }
136
Antonio Quartulli015758d2011-07-09 17:52:13 +0200137 /* it's a new address, add it to the list */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000138 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
139 if (!entry)
140 return;
141 memcpy(entry->addr, interface, ETH_ALEN);
142 entry->primary = primary;
143 hlist_add_head(&entry->list, if_list);
144}
145
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200146static void batadv_vis_data_read_prim_sec(struct seq_file *seq,
147 const struct hlist_head *if_list)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000148{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200149 struct batadv_if_list_entry *entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000150 struct hlist_node *pos;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000151
152 hlist_for_each_entry(entry, pos, if_list, list) {
153 if (entry->primary)
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200154 seq_printf(seq, "PRIMARY, ");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000155 else
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200156 seq_printf(seq, "SEC %pM, ", entry->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000157 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000158}
159
160/* read an entry */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200161static ssize_t
162batadv_vis_data_read_entry(struct seq_file *seq,
163 const struct batadv_vis_info_entry *entry,
164 const uint8_t *src, bool primary)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000165{
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000166 if (primary && entry->quality == 0)
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200167 return seq_printf(seq, "TT %pM, ", entry->dest);
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200168 else if (batadv_compare_eth(entry->src, src))
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200169 return seq_printf(seq, "TQ %pM %d, ", entry->dest,
170 entry->quality);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000171
172 return 0;
173}
174
Sven Eckelmann56303d32012-06-05 22:31:31 +0200175static void
176batadv_vis_data_insert_interfaces(struct hlist_head *list,
177 struct batadv_vis_packet *packet,
178 struct batadv_vis_info_entry *entries)
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200179{
180 int i;
181
182 for (i = 0; i < packet->entries; i++) {
183 if (entries[i].quality == 0)
184 continue;
185
186 if (batadv_compare_eth(entries[i].src, packet->vis_orig))
187 continue;
188
189 batadv_vis_data_insert_interface(entries[i].src, list, false);
190 }
191}
192
193static void batadv_vis_data_read_entries(struct seq_file *seq,
194 struct hlist_head *list,
Sven Eckelmann96412692012-06-05 22:31:30 +0200195 struct batadv_vis_packet *packet,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200196 struct batadv_vis_info_entry *entries)
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200197{
198 int i;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200199 struct batadv_if_list_entry *entry;
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200200 struct hlist_node *pos;
201
202 hlist_for_each_entry(entry, pos, list, list) {
203 seq_printf(seq, "%pM,", entry->addr);
204
205 for (i = 0; i < packet->entries; i++)
206 batadv_vis_data_read_entry(seq, &entries[i],
207 entry->addr, entry->primary);
208
209 /* add primary/secondary records */
210 if (batadv_compare_eth(entry->addr, packet->vis_orig))
211 batadv_vis_data_read_prim_sec(seq, list);
212
213 seq_printf(seq, "\n");
214 }
215}
216
217static void batadv_vis_seq_print_text_bucket(struct seq_file *seq,
218 const struct hlist_head *head)
219{
220 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200221 struct batadv_vis_info *info;
Sven Eckelmann96412692012-06-05 22:31:30 +0200222 struct batadv_vis_packet *packet;
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200223 uint8_t *entries_pos;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200224 struct batadv_vis_info_entry *entries;
225 struct batadv_if_list_entry *entry;
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200226 struct hlist_node *pos, *n;
227
228 HLIST_HEAD(vis_if_list);
229
230 hlist_for_each_entry_rcu(info, node, head, hash_entry) {
Sven Eckelmann96412692012-06-05 22:31:30 +0200231 packet = (struct batadv_vis_packet *)info->skb_packet->data;
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200232 entries_pos = (uint8_t *)packet + sizeof(*packet);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200233 entries = (struct batadv_vis_info_entry *)entries_pos;
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200234
235 batadv_vis_data_insert_interface(packet->vis_orig, &vis_if_list,
236 true);
237 batadv_vis_data_insert_interfaces(&vis_if_list, packet,
238 entries);
239 batadv_vis_data_read_entries(seq, &vis_if_list, packet,
240 entries);
241
242 hlist_for_each_entry_safe(entry, pos, n, &vis_if_list, list) {
243 hlist_del(&entry->list);
244 kfree(entry);
245 }
246 }
247}
248
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200249int batadv_vis_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000250{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200251 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000252 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000253 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200254 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200255 struct batadv_hashtable *hash = bat_priv->vis.hash;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200256 uint32_t i;
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200257 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000258 int vis_server = atomic_read(&bat_priv->vis_mode);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000259
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200260 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200261 if (!primary_if)
262 goto out;
263
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200264 if (vis_server == BATADV_VIS_TYPE_CLIENT_UPDATE)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200265 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000266
Sven Eckelmann807736f2012-07-15 22:26:51 +0200267 spin_lock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000268 for (i = 0; i < hash->size; i++) {
269 head = &hash->table[i];
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200270 batadv_vis_seq_print_text_bucket(seq, head);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000271 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200272 spin_unlock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000273
Marek Lindner32ae9b22011-04-20 15:40:58 +0200274out:
275 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200276 batadv_hardif_free_ref(primary_if);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200277 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000278}
279
280/* add the info packet to the send list, if it was not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200281 * already linked in.
282 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200283static void batadv_send_list_add(struct batadv_priv *bat_priv,
284 struct batadv_vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000285{
286 if (list_empty(&info->send_list)) {
287 kref_get(&info->refcount);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200288 list_add_tail(&info->send_list, &bat_priv->vis.send_list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000289 }
290}
291
292/* delete the info packet from the send list, if it was
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200293 * linked in.
294 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200295static void batadv_send_list_del(struct batadv_vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000296{
297 if (!list_empty(&info->send_list)) {
298 list_del_init(&info->send_list);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200299 kref_put(&info->refcount, batadv_free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000300 }
301}
302
303/* tries to add one entry to the receive list. */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200304static void batadv_recv_list_add(struct batadv_priv *bat_priv,
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200305 struct list_head *recv_list, const char *mac)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000306{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200307 struct batadv_recvlist_node *entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000308
Sven Eckelmann704509b2011-05-14 23:14:54 +0200309 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000310 if (!entry)
311 return;
312
313 memcpy(entry->mac, mac, ETH_ALEN);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200314 spin_lock_bh(&bat_priv->vis.list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000315 list_add_tail(&entry->list, recv_list);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200316 spin_unlock_bh(&bat_priv->vis.list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000317}
318
319/* returns 1 if this mac is in the recv_list */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200320static int batadv_recv_list_is_in(struct batadv_priv *bat_priv,
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200321 const struct list_head *recv_list,
322 const char *mac)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000323{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200324 const struct batadv_recvlist_node *entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000325
Sven Eckelmann807736f2012-07-15 22:26:51 +0200326 spin_lock_bh(&bat_priv->vis.list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000327 list_for_each_entry(entry, recv_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200328 if (batadv_compare_eth(entry->mac, mac)) {
Sven Eckelmann807736f2012-07-15 22:26:51 +0200329 spin_unlock_bh(&bat_priv->vis.list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000330 return 1;
331 }
332 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200333 spin_unlock_bh(&bat_priv->vis.list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000334 return 0;
335}
336
337/* try to add the packet to the vis_hash. return NULL if invalid (e.g. too old,
338 * broken.. ). vis hash must be locked outside. is_new is set when the packet
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200339 * is newer than old entries in the hash.
340 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200341static struct batadv_vis_info *
342batadv_add_packet(struct batadv_priv *bat_priv,
343 struct batadv_vis_packet *vis_packet, int vis_info_len,
344 int *is_new, int make_broadcast)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000345{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200346 struct batadv_vis_info *info, *old_info;
Sven Eckelmann96412692012-06-05 22:31:30 +0200347 struct batadv_vis_packet *search_packet, *old_packet;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200348 struct batadv_vis_info search_elem;
Sven Eckelmann96412692012-06-05 22:31:30 +0200349 struct batadv_vis_packet *packet;
350 struct sk_buff *tmp_skb;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000351 int hash_added;
Sven Eckelmann96412692012-06-05 22:31:30 +0200352 size_t len;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200353 size_t max_entries;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000354
355 *is_new = 0;
356 /* sanity check */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200357 if (!bat_priv->vis.hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000358 return NULL;
359
360 /* see if the packet is already in vis_hash */
Sven Eckelmann704509b2011-05-14 23:14:54 +0200361 search_elem.skb_packet = dev_alloc_skb(sizeof(*search_packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000362 if (!search_elem.skb_packet)
363 return NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200364 len = sizeof(*search_packet);
365 tmp_skb = search_elem.skb_packet;
366 search_packet = (struct batadv_vis_packet *)skb_put(tmp_skb, len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000367
368 memcpy(search_packet->vis_orig, vis_packet->vis_orig, ETH_ALEN);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200369 old_info = batadv_vis_hash_find(bat_priv, &search_elem);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000370 kfree_skb(search_elem.skb_packet);
371
372 if (old_info) {
Sven Eckelmann96412692012-06-05 22:31:30 +0200373 tmp_skb = old_info->skb_packet;
374 old_packet = (struct batadv_vis_packet *)tmp_skb->data;
Sven Eckelmann3e348192012-05-16 20:23:22 +0200375 if (!batadv_seq_after(ntohl(vis_packet->seqno),
376 ntohl(old_packet->seqno))) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000377 if (old_packet->seqno == vis_packet->seqno) {
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200378 batadv_recv_list_add(bat_priv,
379 &old_info->recv_list,
380 vis_packet->sender_orig);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000381 return old_info;
382 } else {
383 /* newer packet is already in hash. */
384 return NULL;
385 }
386 }
387 /* remove old entry */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200388 batadv_hash_remove(bat_priv->vis.hash, batadv_vis_info_cmp,
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200389 batadv_vis_info_choose, old_info);
390 batadv_send_list_del(old_info);
391 kref_put(&old_info->refcount, batadv_free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000392 }
393
Sven Eckelmann704509b2011-05-14 23:14:54 +0200394 info = kmalloc(sizeof(*info), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000395 if (!info)
396 return NULL;
397
Sven Eckelmann96412692012-06-05 22:31:30 +0200398 len = sizeof(*packet) + vis_info_len;
Sven Eckelmann5b246572012-11-04 17:11:45 +0100399 info->skb_packet = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000400 if (!info->skb_packet) {
401 kfree(info);
402 return NULL;
403 }
Sven Eckelmann5b246572012-11-04 17:11:45 +0100404 skb_reserve(info->skb_packet, ETH_HLEN + NET_IP_ALIGN);
Sven Eckelmann96412692012-06-05 22:31:30 +0200405 packet = (struct batadv_vis_packet *)skb_put(info->skb_packet, len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000406
407 kref_init(&info->refcount);
408 INIT_LIST_HEAD(&info->send_list);
409 INIT_LIST_HEAD(&info->recv_list);
410 info->first_seen = jiffies;
411 info->bat_priv = bat_priv;
Sven Eckelmann96412692012-06-05 22:31:30 +0200412 memcpy(packet, vis_packet, len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000413
414 /* initialize and add new packet. */
415 *is_new = 1;
416
417 /* Make it a broadcast packet, if required */
418 if (make_broadcast)
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200419 memcpy(packet->target_orig, batadv_broadcast_addr, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000420
421 /* repair if entries is longer than packet. */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200422 max_entries = vis_info_len / sizeof(struct batadv_vis_info_entry);
423 if (packet->entries > max_entries)
424 packet->entries = max_entries;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000425
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200426 batadv_recv_list_add(bat_priv, &info->recv_list, packet->sender_orig);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000427
428 /* try to add it */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200429 hash_added = batadv_hash_add(bat_priv->vis.hash, batadv_vis_info_cmp,
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200430 batadv_vis_info_choose, info,
431 &info->hash_entry);
Antonio Quartulli1a1f37d2011-07-10 00:36:36 +0200432 if (hash_added != 0) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000433 /* did not work (for some reason) */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200434 kref_put(&info->refcount, batadv_free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000435 info = NULL;
436 }
437
438 return info;
439}
440
441/* handle the server sync packet, forward if needed. */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200442void batadv_receive_server_sync_packet(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +0200443 struct batadv_vis_packet *vis_packet,
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200444 int vis_info_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000445{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200446 struct batadv_vis_info *info;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000447 int is_new, make_broadcast;
448 int vis_server = atomic_read(&bat_priv->vis_mode);
449
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200450 make_broadcast = (vis_server == BATADV_VIS_TYPE_SERVER_SYNC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000451
Sven Eckelmann807736f2012-07-15 22:26:51 +0200452 spin_lock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200453 info = batadv_add_packet(bat_priv, vis_packet, vis_info_len,
454 &is_new, make_broadcast);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000455 if (!info)
456 goto end;
457
458 /* only if we are server ourselves and packet is newer than the one in
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200459 * hash.
460 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200461 if (vis_server == BATADV_VIS_TYPE_SERVER_SYNC && is_new)
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200462 batadv_send_list_add(bat_priv, info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000463end:
Sven Eckelmann807736f2012-07-15 22:26:51 +0200464 spin_unlock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000465}
466
467/* handle an incoming client update packet and schedule forward if needed. */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200468void batadv_receive_client_update_packet(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +0200469 struct batadv_vis_packet *vis_packet,
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200470 int vis_info_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000471{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200472 struct batadv_vis_info *info;
Sven Eckelmann96412692012-06-05 22:31:30 +0200473 struct batadv_vis_packet *packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000474 int is_new;
475 int vis_server = atomic_read(&bat_priv->vis_mode);
476 int are_target = 0;
477
478 /* clients shall not broadcast. */
479 if (is_broadcast_ether_addr(vis_packet->target_orig))
480 return;
481
482 /* Are we the target for this VIS packet? */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200483 if (vis_server == BATADV_VIS_TYPE_SERVER_SYNC &&
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200484 batadv_is_my_mac(vis_packet->target_orig))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000485 are_target = 1;
486
Sven Eckelmann807736f2012-07-15 22:26:51 +0200487 spin_lock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200488 info = batadv_add_packet(bat_priv, vis_packet, vis_info_len,
489 &is_new, are_target);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000490
491 if (!info)
492 goto end;
493 /* note that outdated packets will be dropped at this point. */
494
Sven Eckelmann96412692012-06-05 22:31:30 +0200495 packet = (struct batadv_vis_packet *)info->skb_packet->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000496
497 /* send only if we're the target server or ... */
498 if (are_target && is_new) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200499 packet->vis_type = BATADV_VIS_TYPE_SERVER_SYNC; /* upgrade! */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200500 batadv_send_list_add(bat_priv, info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000501
502 /* ... we're not the recipient (and thus need to forward). */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200503 } else if (!batadv_is_my_mac(packet->target_orig)) {
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200504 batadv_send_list_add(bat_priv, info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000505 }
506
507end:
Sven Eckelmann807736f2012-07-15 22:26:51 +0200508 spin_unlock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000509}
510
511/* Walk the originators and find the VIS server with the best tq. Set the packet
512 * address to its address and return the best_tq.
513 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200514 * Must be called with the originator hash locked
515 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200516static int batadv_find_best_vis_server(struct batadv_priv *bat_priv,
517 struct batadv_vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000518{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200519 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200520 struct batadv_neigh_node *router;
Marek Lindner7aadf882011-02-18 12:28:09 +0000521 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000522 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200523 struct batadv_orig_node *orig_node;
Sven Eckelmann96412692012-06-05 22:31:30 +0200524 struct batadv_vis_packet *packet;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200525 int best_tq = -1;
526 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000527
Sven Eckelmann96412692012-06-05 22:31:30 +0200528 packet = (struct batadv_vis_packet *)info->skb_packet->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000529
530 for (i = 0; i < hash->size; i++) {
531 head = &hash->table[i];
532
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000533 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000534 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200535 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000536 if (!router)
537 continue;
538
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200539 if ((orig_node->flags & BATADV_VIS_SERVER) &&
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000540 (router->tq_avg > best_tq)) {
541 best_tq = router->tq_avg;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000542 memcpy(packet->target_orig, orig_node->orig,
543 ETH_ALEN);
544 }
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200545 batadv_neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000546 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000547 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000548 }
549
550 return best_tq;
551}
552
553/* Return true if the vis packet is full. */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200554static bool batadv_vis_packet_full(const struct batadv_vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000555{
Sven Eckelmann96412692012-06-05 22:31:30 +0200556 const struct batadv_vis_packet *packet;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200557 size_t num;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000558
Sven Eckelmann96412692012-06-05 22:31:30 +0200559 packet = (struct batadv_vis_packet *)info->skb_packet->data;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200560 num = BATADV_MAX_VIS_PACKET_SIZE / sizeof(struct batadv_vis_info_entry);
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200561
Sven Eckelmann56303d32012-06-05 22:31:31 +0200562 if (num < packet->entries + 1)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000563 return true;
564 return false;
565}
566
567/* generates a packet of own vis data,
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200568 * returns 0 on success, -1 if no packet could be generated
569 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200570static int batadv_generate_vis_packet(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000571{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200572 struct batadv_hashtable *hash = bat_priv->orig_hash;
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 Eckelmann56303d32012-06-05 22:31:31 +0200575 struct batadv_orig_node *orig_node;
576 struct batadv_neigh_node *router;
Sven Eckelmann807736f2012-07-15 22:26:51 +0200577 struct batadv_vis_info *info = bat_priv->vis.my_info;
Sven Eckelmann96412692012-06-05 22:31:30 +0200578 struct batadv_vis_packet *packet;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200579 struct batadv_vis_info_entry *entry;
580 struct batadv_tt_common_entry *tt_common_entry;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200581 uint8_t *packet_pos;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200582 int best_tq = -1;
583 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000584
585 info->first_seen = jiffies;
Sven Eckelmann96412692012-06-05 22:31:30 +0200586 packet = (struct batadv_vis_packet *)info->skb_packet->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000587 packet->vis_type = atomic_read(&bat_priv->vis_mode);
588
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200589 memcpy(packet->target_orig, batadv_broadcast_addr, ETH_ALEN);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200590 packet->header.ttl = BATADV_TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000591 packet->seqno = htonl(ntohl(packet->seqno) + 1);
592 packet->entries = 0;
Sven Eckelmann162d5492012-06-28 11:56:52 +0200593 packet->reserved = 0;
Sven Eckelmann704509b2011-05-14 23:14:54 +0200594 skb_trim(info->skb_packet, sizeof(*packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000595
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200596 if (packet->vis_type == BATADV_VIS_TYPE_CLIENT_UPDATE) {
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200597 best_tq = batadv_find_best_vis_server(bat_priv, info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000598
Marek Lindnerd0072602011-01-19 20:01:44 +0000599 if (best_tq < 0)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200600 return best_tq;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000601 }
602
603 for (i = 0; i < hash->size; i++) {
604 head = &hash->table[i];
605
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000606 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000607 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200608 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000609 if (!router)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000610 continue;
611
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200612 if (!batadv_compare_eth(router->addr, orig_node->orig))
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000613 goto next;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000614
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200615 if (router->if_incoming->if_status != BATADV_IF_ACTIVE)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000616 goto next;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000617
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000618 if (router->tq_avg < 1)
619 goto next;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000620
621 /* fill one entry into buffer. */
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200622 packet_pos = skb_put(info->skb_packet, sizeof(*entry));
623 entry = (struct batadv_vis_info_entry *)packet_pos;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000624 memcpy(entry->src,
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000625 router->if_incoming->net_dev->dev_addr,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000626 ETH_ALEN);
627 memcpy(entry->dest, orig_node->orig, ETH_ALEN);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000628 entry->quality = router->tq_avg;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000629 packet->entries++;
630
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000631next:
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200632 batadv_neigh_node_free_ref(router);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000633
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200634 if (batadv_vis_packet_full(info))
Marek Lindnerd0072602011-01-19 20:01:44 +0000635 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000636 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000637 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000638 }
639
Sven Eckelmann807736f2012-07-15 22:26:51 +0200640 hash = bat_priv->tt.local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000641
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000642 for (i = 0; i < hash->size; i++) {
643 head = &hash->table[i];
644
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200645 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100646 hlist_for_each_entry_rcu(tt_common_entry, node, head,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200647 hash_entry) {
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200648 packet_pos = skb_put(info->skb_packet, sizeof(*entry));
649 entry = (struct batadv_vis_info_entry *)packet_pos;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000650 memset(entry->src, 0, ETH_ALEN);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100651 memcpy(entry->dest, tt_common_entry->addr, ETH_ALEN);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200652 entry->quality = 0; /* 0 means TT */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000653 packet->entries++;
654
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200655 if (batadv_vis_packet_full(info))
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200656 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000657 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200658 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000659 }
660
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000661 return 0;
Marek Lindnerd0072602011-01-19 20:01:44 +0000662
663unlock:
664 rcu_read_unlock();
665 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000666}
667
668/* free old vis packets. Must be called with this vis_hash_lock
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200669 * held
670 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200671static void batadv_purge_vis_packets(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000672{
Antonio Quartullic90681b2011-10-05 17:05:25 +0200673 uint32_t i;
Sven Eckelmann807736f2012-07-15 22:26:51 +0200674 struct batadv_hashtable *hash = bat_priv->vis.hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000675 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000676 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200677 struct batadv_vis_info *info;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000678
679 for (i = 0; i < hash->size; i++) {
680 head = &hash->table[i];
681
Marek Lindner7aadf882011-02-18 12:28:09 +0000682 hlist_for_each_entry_safe(info, node, node_tmp,
683 head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000684 /* never purge own data. */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200685 if (info == bat_priv->vis.my_info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000686 continue;
687
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200688 if (batadv_has_timed_out(info->first_seen,
Sven Eckelmannedbf7ff2012-06-03 22:19:16 +0200689 BATADV_VIS_TIMEOUT)) {
Marek Lindner7aadf882011-02-18 12:28:09 +0000690 hlist_del(node);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200691 batadv_send_list_del(info);
692 kref_put(&info->refcount, batadv_free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000693 }
694 }
695 }
696}
697
Sven Eckelmann56303d32012-06-05 22:31:31 +0200698static void batadv_broadcast_vis_packet(struct batadv_priv *bat_priv,
699 struct batadv_vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000700{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200701 struct batadv_hashtable *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000702 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000703 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200704 struct batadv_orig_node *orig_node;
Sven Eckelmann96412692012-06-05 22:31:30 +0200705 struct batadv_vis_packet *packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000706 struct sk_buff *skb;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200707 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000708
709
Sven Eckelmann96412692012-06-05 22:31:30 +0200710 packet = (struct batadv_vis_packet *)info->skb_packet->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000711
712 /* send to all routers in range. */
713 for (i = 0; i < hash->size; i++) {
714 head = &hash->table[i];
715
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000716 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000717 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000718 /* if it's a vis server and reachable, send it. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200719 if (!(orig_node->flags & BATADV_VIS_SERVER))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000720 continue;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000721
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000722 /* don't send it if we already received the packet from
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200723 * this node.
724 */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200725 if (batadv_recv_list_is_in(bat_priv, &info->recv_list,
Martin Hundebøllbb351ba2012-10-16 16:13:48 +0200726 orig_node->orig))
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000727 continue;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000728
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000729 memcpy(packet->target_orig, orig_node->orig, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000730 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
Martin Hundebøllbb351ba2012-10-16 16:13:48 +0200731 if (!skb)
732 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000733
Martin Hundebøllbb351ba2012-10-16 16:13:48 +0200734 if (!batadv_send_skb_to_orig(skb, orig_node, NULL))
735 kfree_skb(skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000736 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000737 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000738 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000739}
740
Sven Eckelmann56303d32012-06-05 22:31:31 +0200741static void batadv_unicast_vis_packet(struct batadv_priv *bat_priv,
742 struct batadv_vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000743{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200744 struct batadv_orig_node *orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000745 struct sk_buff *skb;
Sven Eckelmann96412692012-06-05 22:31:30 +0200746 struct batadv_vis_packet *packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000747
Sven Eckelmann96412692012-06-05 22:31:30 +0200748 packet = (struct batadv_vis_packet *)info->skb_packet->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000749
Sven Eckelmannda641192012-05-12 13:48:56 +0200750 orig_node = batadv_orig_hash_find(bat_priv, packet->target_orig);
Marek Lindner44524fc2011-02-10 14:33:53 +0000751 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000752 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000753
Martin Hundebøllbb351ba2012-10-16 16:13:48 +0200754 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
755 if (!skb)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000756 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000757
Martin Hundebøllbb351ba2012-10-16 16:13:48 +0200758 if (!batadv_send_skb_to_orig(skb, orig_node, NULL))
759 kfree_skb(skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000760
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000761out:
Marek Lindner44524fc2011-02-10 14:33:53 +0000762 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200763 batadv_orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000764}
765
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200766/* only send one vis packet. called from batadv_send_vis_packets() */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200767static void batadv_send_vis_packet(struct batadv_priv *bat_priv,
768 struct batadv_vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000769{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200770 struct batadv_hard_iface *primary_if;
Sven Eckelmann96412692012-06-05 22:31:30 +0200771 struct batadv_vis_packet *packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000772
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200773 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200774 if (!primary_if)
775 goto out;
776
Sven Eckelmann96412692012-06-05 22:31:30 +0200777 packet = (struct batadv_vis_packet *)info->skb_packet->data;
Sven Eckelmann76543d12011-11-20 15:47:38 +0100778 if (packet->header.ttl < 2) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000779 pr_debug("Error - can't send vis packet: ttl exceeded\n");
Marek Lindner32ae9b22011-04-20 15:40:58 +0200780 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000781 }
782
Marek Lindner32ae9b22011-04-20 15:40:58 +0200783 memcpy(packet->sender_orig, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmann76543d12011-11-20 15:47:38 +0100784 packet->header.ttl--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000785
786 if (is_broadcast_ether_addr(packet->target_orig))
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200787 batadv_broadcast_vis_packet(bat_priv, info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000788 else
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200789 batadv_unicast_vis_packet(bat_priv, info);
Sven Eckelmann76543d12011-11-20 15:47:38 +0100790 packet->header.ttl++; /* restore TTL */
Marek Lindner32ae9b22011-04-20 15:40:58 +0200791
792out:
793 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200794 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000795}
796
797/* called from timer; send (and maybe generate) vis packet. */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200798static void batadv_send_vis_packets(struct work_struct *work)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000799{
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200800 struct delayed_work *delayed_work;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200801 struct batadv_priv *bat_priv;
Sven Eckelmann807736f2012-07-15 22:26:51 +0200802 struct batadv_priv_vis *priv_vis;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200803 struct batadv_vis_info *info;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000804
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200805 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200806 priv_vis = container_of(delayed_work, struct batadv_priv_vis, work);
807 bat_priv = container_of(priv_vis, struct batadv_priv, vis);
808 spin_lock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200809 batadv_purge_vis_packets(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000810
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200811 if (batadv_generate_vis_packet(bat_priv) == 0) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000812 /* schedule if generation was successful */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200813 batadv_send_list_add(bat_priv, bat_priv->vis.my_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000814 }
815
Sven Eckelmann807736f2012-07-15 22:26:51 +0200816 while (!list_empty(&bat_priv->vis.send_list)) {
817 info = list_first_entry(&bat_priv->vis.send_list,
Sven Eckelmann1181e1d2011-01-28 18:34:07 +0100818 typeof(*info), send_list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000819
820 kref_get(&info->refcount);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200821 spin_unlock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000822
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200823 batadv_send_vis_packet(bat_priv, info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000824
Sven Eckelmann807736f2012-07-15 22:26:51 +0200825 spin_lock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200826 batadv_send_list_del(info);
827 kref_put(&info->refcount, batadv_free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000828 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200829 spin_unlock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200830 batadv_start_vis_timer(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000831}
832
833/* init the vis server. this may only be called when if_list is already
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200834 * initialized (e.g. bat0 is initialized, interfaces have been added)
835 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200836int batadv_vis_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000837{
Sven Eckelmann96412692012-06-05 22:31:30 +0200838 struct batadv_vis_packet *packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000839 int hash_added;
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200840 unsigned int len;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200841 unsigned long first_seen;
Sven Eckelmann96412692012-06-05 22:31:30 +0200842 struct sk_buff *tmp_skb;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000843
Sven Eckelmann807736f2012-07-15 22:26:51 +0200844 if (bat_priv->vis.hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200845 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000846
Sven Eckelmann807736f2012-07-15 22:26:51 +0200847 spin_lock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000848
Sven Eckelmann807736f2012-07-15 22:26:51 +0200849 bat_priv->vis.hash = batadv_hash_new(256);
850 if (!bat_priv->vis.hash) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000851 pr_err("Can't initialize vis_hash\n");
852 goto err;
853 }
854
Sven Eckelmann807736f2012-07-15 22:26:51 +0200855 bat_priv->vis.my_info = kmalloc(BATADV_MAX_VIS_PACKET_SIZE, GFP_ATOMIC);
856 if (!bat_priv->vis.my_info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000857 goto err;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000858
Sven Eckelmann5b246572012-11-04 17:11:45 +0100859 len = sizeof(*packet) + BATADV_MAX_VIS_PACKET_SIZE;
860 len += ETH_HLEN + NET_IP_ALIGN;
Sven Eckelmann807736f2012-07-15 22:26:51 +0200861 bat_priv->vis.my_info->skb_packet = dev_alloc_skb(len);
862 if (!bat_priv->vis.my_info->skb_packet)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000863 goto free_info;
864
Sven Eckelmann5b246572012-11-04 17:11:45 +0100865 skb_reserve(bat_priv->vis.my_info->skb_packet, ETH_HLEN + NET_IP_ALIGN);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200866 tmp_skb = bat_priv->vis.my_info->skb_packet;
Sven Eckelmann96412692012-06-05 22:31:30 +0200867 packet = (struct batadv_vis_packet *)skb_put(tmp_skb, sizeof(*packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000868
869 /* prefill the vis info */
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200870 first_seen = jiffies - msecs_to_jiffies(BATADV_VIS_INTERVAL);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200871 bat_priv->vis.my_info->first_seen = first_seen;
872 INIT_LIST_HEAD(&bat_priv->vis.my_info->recv_list);
873 INIT_LIST_HEAD(&bat_priv->vis.my_info->send_list);
874 kref_init(&bat_priv->vis.my_info->refcount);
875 bat_priv->vis.my_info->bat_priv = bat_priv;
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200876 packet->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200877 packet->header.packet_type = BATADV_VIS;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200878 packet->header.ttl = BATADV_TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000879 packet->seqno = 0;
Sven Eckelmann162d5492012-06-28 11:56:52 +0200880 packet->reserved = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000881 packet->entries = 0;
882
Sven Eckelmann807736f2012-07-15 22:26:51 +0200883 INIT_LIST_HEAD(&bat_priv->vis.send_list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000884
Sven Eckelmann807736f2012-07-15 22:26:51 +0200885 hash_added = batadv_hash_add(bat_priv->vis.hash, batadv_vis_info_cmp,
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200886 batadv_vis_info_choose,
Sven Eckelmann807736f2012-07-15 22:26:51 +0200887 bat_priv->vis.my_info,
888 &bat_priv->vis.my_info->hash_entry);
Antonio Quartulli1a1f37d2011-07-10 00:36:36 +0200889 if (hash_added != 0) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000890 pr_err("Can't add own vis packet into hash\n");
891 /* not in hash, need to remove it manually. */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200892 kref_put(&bat_priv->vis.my_info->refcount, batadv_free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000893 goto err;
894 }
895
Sven Eckelmann807736f2012-07-15 22:26:51 +0200896 spin_unlock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200897 batadv_start_vis_timer(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +0200898 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000899
900free_info:
Sven Eckelmann807736f2012-07-15 22:26:51 +0200901 kfree(bat_priv->vis.my_info);
902 bat_priv->vis.my_info = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000903err:
Sven Eckelmann807736f2012-07-15 22:26:51 +0200904 spin_unlock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200905 batadv_vis_quit(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +0200906 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000907}
908
909/* Decrease the reference count on a hash item info */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200910static void batadv_free_info_ref(struct hlist_node *node, void *arg)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000911{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200912 struct batadv_vis_info *info;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000913
Sven Eckelmann56303d32012-06-05 22:31:31 +0200914 info = container_of(node, struct batadv_vis_info, hash_entry);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200915 batadv_send_list_del(info);
916 kref_put(&info->refcount, batadv_free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000917}
918
919/* shutdown vis-server */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200920void batadv_vis_quit(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000921{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200922 if (!bat_priv->vis.hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000923 return;
924
Sven Eckelmann807736f2012-07-15 22:26:51 +0200925 cancel_delayed_work_sync(&bat_priv->vis.work);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000926
Sven Eckelmann807736f2012-07-15 22:26:51 +0200927 spin_lock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000928 /* properly remove, kill timers ... */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200929 batadv_hash_delete(bat_priv->vis.hash, batadv_free_info_ref, NULL);
930 bat_priv->vis.hash = NULL;
931 bat_priv->vis.my_info = NULL;
932 spin_unlock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000933}
934
935/* schedule packets for (re)transmission */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200936static void batadv_start_vis_timer(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000937{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200938 INIT_DELAYED_WORK(&bat_priv->vis.work, batadv_send_vis_packets);
939 queue_delayed_work(batadv_event_workqueue, &bat_priv->vis.work,
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200940 msecs_to_jiffies(BATADV_VIS_INTERVAL));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000941}