blob: d8ea31a58457c1cc58d7ff753b8101d3ca0b7320 [file] [log] [blame]
Antonio Quartulli0b873932013-01-04 03:05:31 +01001/* Copyright (C) 2008-2013 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002 *
3 * Simon Wunderlich
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000018 */
19
20#include "main.h"
21#include "send.h"
22#include "translation-table.h"
23#include "vis.h"
24#include "soft-interface.h"
25#include "hard-interface.h"
26#include "hash.h"
27#include "originator.h"
28
Sven Eckelmann347c80f2012-06-03 22:19:07 +020029#define BATADV_MAX_VIS_PACKET_SIZE 1000
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000030
Antonio Quartullidec05072012-11-10 11:00:32 +010031/* hash class keys */
32static struct lock_class_key batadv_vis_hash_lock_class_key;
33
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000034/* free the info */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +020035static void batadv_free_info(struct kref *ref)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000036{
Sven Eckelmann56303d32012-06-05 22:31:31 +020037 struct batadv_vis_info *info;
38 struct batadv_priv *bat_priv;
Marek Lindner28500f02012-12-25 17:03:23 +080039 struct batadv_vis_recvlist_node *entry, *tmp;
Sven Eckelmann56303d32012-06-05 22:31:31 +020040
41 info = container_of(ref, struct batadv_vis_info, refcount);
42 bat_priv = info->bat_priv;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000043
44 list_del_init(&info->send_list);
Sven Eckelmann807736f2012-07-15 22:26:51 +020045 spin_lock_bh(&bat_priv->vis.list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000046 list_for_each_entry_safe(entry, tmp, &info->recv_list, list) {
47 list_del(&entry->list);
48 kfree(entry);
49 }
50
Sven Eckelmann807736f2012-07-15 22:26:51 +020051 spin_unlock_bh(&bat_priv->vis.list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000052 kfree_skb(info->skb_packet);
Sven Eckelmanndda9fc62011-01-28 18:34:06 +010053 kfree(info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000054}
55
56/* Compare two vis packets, used by the hashing algorithm */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +020057static int batadv_vis_info_cmp(const struct hlist_node *node, const void *data2)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000058{
Sven Eckelmann56303d32012-06-05 22:31:31 +020059 const struct batadv_vis_info *d1, *d2;
Sven Eckelmann96412692012-06-05 22:31:30 +020060 const struct batadv_vis_packet *p1, *p2;
Marek Lindner7aadf882011-02-18 12:28:09 +000061
Sven Eckelmann56303d32012-06-05 22:31:31 +020062 d1 = container_of(node, struct batadv_vis_info, hash_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000063 d2 = data2;
Sven Eckelmann96412692012-06-05 22:31:30 +020064 p1 = (struct batadv_vis_packet *)d1->skb_packet->data;
65 p2 = (struct batadv_vis_packet *)d2->skb_packet->data;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +020066 return batadv_compare_eth(p1->vis_orig, p2->vis_orig);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000067}
68
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +020069/* hash function to choose an entry in a hash table of given size
70 * hash algorithm from http://en.wikipedia.org/wiki/Hash_table
71 */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +020072static uint32_t batadv_vis_info_choose(const void *data, uint32_t size)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000073{
Sven Eckelmann56303d32012-06-05 22:31:31 +020074 const struct batadv_vis_info *vis_info = data;
Sven Eckelmann96412692012-06-05 22:31:30 +020075 const struct batadv_vis_packet *packet;
Sven Eckelmann747e4222011-05-14 23:14:50 +020076 const unsigned char *key;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000077 uint32_t hash = 0;
78 size_t i;
79
Sven Eckelmann96412692012-06-05 22:31:30 +020080 packet = (struct batadv_vis_packet *)vis_info->skb_packet->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000081 key = packet->vis_orig;
82 for (i = 0; i < ETH_ALEN; i++) {
83 hash += key[i];
84 hash += (hash << 10);
85 hash ^= (hash >> 6);
86 }
87
88 hash += (hash << 3);
89 hash ^= (hash >> 11);
90 hash += (hash << 15);
91
92 return hash % size;
93}
94
Sven Eckelmann56303d32012-06-05 22:31:31 +020095static struct batadv_vis_info *
96batadv_vis_hash_find(struct batadv_priv *bat_priv, const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +000097{
Sven Eckelmann807736f2012-07-15 22:26:51 +020098 struct batadv_hashtable *hash = bat_priv->vis.hash;
Marek Lindner7aadf882011-02-18 12:28:09 +000099 struct hlist_head *head;
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();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800110 hlist_for_each_entry_rcu(vis_info, head, hash_entry) {
111 if (!batadv_vis_info_cmp(&vis_info->hash_entry, 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{
Marek Lindner015b4ae2012-12-25 17:03:22 +0800129 struct batadv_vis_if_list_entry *entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000130
Sasha Levinb67bfe02013-02-27 17:06:00 -0800131 hlist_for_each_entry(entry, if_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200132 if (batadv_compare_eth(entry->addr, interface))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000133 return;
134 }
135
Antonio Quartulli015758d2011-07-09 17:52:13 +0200136 /* it's a new address, add it to the list */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000137 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
138 if (!entry)
139 return;
140 memcpy(entry->addr, interface, ETH_ALEN);
141 entry->primary = primary;
142 hlist_add_head(&entry->list, if_list);
143}
144
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200145static void batadv_vis_data_read_prim_sec(struct seq_file *seq,
146 const struct hlist_head *if_list)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000147{
Marek Lindner015b4ae2012-12-25 17:03:22 +0800148 struct batadv_vis_if_list_entry *entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000149
Sasha Levinb67bfe02013-02-27 17:06:00 -0800150 hlist_for_each_entry(entry, if_list, list) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000151 if (entry->primary)
Antonio Quartulli0c814652013-03-21 09:23:29 +0100152 seq_puts(seq, "PRIMARY, ");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000153 else
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200154 seq_printf(seq, "SEC %pM, ", entry->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000155 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000156}
157
158/* read an entry */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200159static ssize_t
160batadv_vis_data_read_entry(struct seq_file *seq,
161 const struct batadv_vis_info_entry *entry,
162 const uint8_t *src, bool primary)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000163{
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000164 if (primary && entry->quality == 0)
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200165 return seq_printf(seq, "TT %pM, ", entry->dest);
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200166 else if (batadv_compare_eth(entry->src, src))
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200167 return seq_printf(seq, "TQ %pM %d, ", entry->dest,
168 entry->quality);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000169
170 return 0;
171}
172
Sven Eckelmann56303d32012-06-05 22:31:31 +0200173static void
174batadv_vis_data_insert_interfaces(struct hlist_head *list,
175 struct batadv_vis_packet *packet,
176 struct batadv_vis_info_entry *entries)
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200177{
178 int i;
179
180 for (i = 0; i < packet->entries; i++) {
181 if (entries[i].quality == 0)
182 continue;
183
184 if (batadv_compare_eth(entries[i].src, packet->vis_orig))
185 continue;
186
187 batadv_vis_data_insert_interface(entries[i].src, list, false);
188 }
189}
190
191static void batadv_vis_data_read_entries(struct seq_file *seq,
192 struct hlist_head *list,
Sven Eckelmann96412692012-06-05 22:31:30 +0200193 struct batadv_vis_packet *packet,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200194 struct batadv_vis_info_entry *entries)
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200195{
196 int i;
Marek Lindner015b4ae2012-12-25 17:03:22 +0800197 struct batadv_vis_if_list_entry *entry;
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200198
Sasha Levinb67bfe02013-02-27 17:06:00 -0800199 hlist_for_each_entry(entry, list, list) {
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200200 seq_printf(seq, "%pM,", entry->addr);
201
202 for (i = 0; i < packet->entries; i++)
203 batadv_vis_data_read_entry(seq, &entries[i],
204 entry->addr, entry->primary);
205
206 /* add primary/secondary records */
207 if (batadv_compare_eth(entry->addr, packet->vis_orig))
208 batadv_vis_data_read_prim_sec(seq, list);
209
Antonio Quartulli0c814652013-03-21 09:23:29 +0100210 seq_puts(seq, "\n");
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200211 }
212}
213
214static void batadv_vis_seq_print_text_bucket(struct seq_file *seq,
215 const struct hlist_head *head)
216{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200217 struct batadv_vis_info *info;
Sven Eckelmann96412692012-06-05 22:31:30 +0200218 struct batadv_vis_packet *packet;
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200219 uint8_t *entries_pos;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200220 struct batadv_vis_info_entry *entries;
Marek Lindner015b4ae2012-12-25 17:03:22 +0800221 struct batadv_vis_if_list_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800222 struct hlist_node *n;
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200223
224 HLIST_HEAD(vis_if_list);
225
Sasha Levinb67bfe02013-02-27 17:06:00 -0800226 hlist_for_each_entry_rcu(info, head, hash_entry) {
Sven Eckelmann96412692012-06-05 22:31:30 +0200227 packet = (struct batadv_vis_packet *)info->skb_packet->data;
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200228 entries_pos = (uint8_t *)packet + sizeof(*packet);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200229 entries = (struct batadv_vis_info_entry *)entries_pos;
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200230
231 batadv_vis_data_insert_interface(packet->vis_orig, &vis_if_list,
232 true);
233 batadv_vis_data_insert_interfaces(&vis_if_list, packet,
234 entries);
235 batadv_vis_data_read_entries(seq, &vis_if_list, packet,
236 entries);
237
Sasha Levinb67bfe02013-02-27 17:06:00 -0800238 hlist_for_each_entry_safe(entry, n, &vis_if_list, list) {
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200239 hlist_del(&entry->list);
240 kfree(entry);
241 }
242 }
243}
244
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200245int batadv_vis_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000246{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200247 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000248 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000249 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200250 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200251 struct batadv_hashtable *hash = bat_priv->vis.hash;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200252 uint32_t i;
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200253 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000254 int vis_server = atomic_read(&bat_priv->vis_mode);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000255
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200256 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200257 if (!primary_if)
258 goto out;
259
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200260 if (vis_server == BATADV_VIS_TYPE_CLIENT_UPDATE)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200261 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000262
Sven Eckelmann807736f2012-07-15 22:26:51 +0200263 spin_lock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000264 for (i = 0; i < hash->size; i++) {
265 head = &hash->table[i];
Sven Eckelmann28afd3c2012-05-16 20:23:23 +0200266 batadv_vis_seq_print_text_bucket(seq, head);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000267 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200268 spin_unlock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000269
Marek Lindner32ae9b22011-04-20 15:40:58 +0200270out:
271 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200272 batadv_hardif_free_ref(primary_if);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200273 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000274}
275
276/* add the info packet to the send list, if it was not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200277 * already linked in.
278 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200279static void batadv_send_list_add(struct batadv_priv *bat_priv,
280 struct batadv_vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000281{
282 if (list_empty(&info->send_list)) {
283 kref_get(&info->refcount);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200284 list_add_tail(&info->send_list, &bat_priv->vis.send_list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000285 }
286}
287
288/* delete the info packet from the send list, if it was
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200289 * linked in.
290 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200291static void batadv_send_list_del(struct batadv_vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000292{
293 if (!list_empty(&info->send_list)) {
294 list_del_init(&info->send_list);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200295 kref_put(&info->refcount, batadv_free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000296 }
297}
298
299/* tries to add one entry to the receive list. */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200300static void batadv_recv_list_add(struct batadv_priv *bat_priv,
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200301 struct list_head *recv_list, const char *mac)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000302{
Marek Lindner28500f02012-12-25 17:03:23 +0800303 struct batadv_vis_recvlist_node *entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000304
Sven Eckelmann704509b2011-05-14 23:14:54 +0200305 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000306 if (!entry)
307 return;
308
309 memcpy(entry->mac, mac, ETH_ALEN);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200310 spin_lock_bh(&bat_priv->vis.list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000311 list_add_tail(&entry->list, recv_list);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200312 spin_unlock_bh(&bat_priv->vis.list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000313}
314
315/* returns 1 if this mac is in the recv_list */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200316static int batadv_recv_list_is_in(struct batadv_priv *bat_priv,
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200317 const struct list_head *recv_list,
318 const char *mac)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000319{
Marek Lindner28500f02012-12-25 17:03:23 +0800320 const struct batadv_vis_recvlist_node *entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000321
Sven Eckelmann807736f2012-07-15 22:26:51 +0200322 spin_lock_bh(&bat_priv->vis.list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000323 list_for_each_entry(entry, recv_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200324 if (batadv_compare_eth(entry->mac, mac)) {
Sven Eckelmann807736f2012-07-15 22:26:51 +0200325 spin_unlock_bh(&bat_priv->vis.list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000326 return 1;
327 }
328 }
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 0;
331}
332
333/* try to add the packet to the vis_hash. return NULL if invalid (e.g. too old,
334 * broken.. ). vis hash must be locked outside. is_new is set when the packet
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200335 * is newer than old entries in the hash.
336 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200337static struct batadv_vis_info *
338batadv_add_packet(struct batadv_priv *bat_priv,
339 struct batadv_vis_packet *vis_packet, int vis_info_len,
340 int *is_new, int make_broadcast)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000341{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200342 struct batadv_vis_info *info, *old_info;
Sven Eckelmann96412692012-06-05 22:31:30 +0200343 struct batadv_vis_packet *search_packet, *old_packet;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200344 struct batadv_vis_info search_elem;
Sven Eckelmann96412692012-06-05 22:31:30 +0200345 struct batadv_vis_packet *packet;
346 struct sk_buff *tmp_skb;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000347 int hash_added;
Sven Eckelmann96412692012-06-05 22:31:30 +0200348 size_t len;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200349 size_t max_entries;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000350
351 *is_new = 0;
352 /* sanity check */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200353 if (!bat_priv->vis.hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000354 return NULL;
355
356 /* see if the packet is already in vis_hash */
Sven Eckelmann704509b2011-05-14 23:14:54 +0200357 search_elem.skb_packet = dev_alloc_skb(sizeof(*search_packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000358 if (!search_elem.skb_packet)
359 return NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200360 len = sizeof(*search_packet);
361 tmp_skb = search_elem.skb_packet;
362 search_packet = (struct batadv_vis_packet *)skb_put(tmp_skb, len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000363
364 memcpy(search_packet->vis_orig, vis_packet->vis_orig, ETH_ALEN);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200365 old_info = batadv_vis_hash_find(bat_priv, &search_elem);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000366 kfree_skb(search_elem.skb_packet);
367
368 if (old_info) {
Sven Eckelmann96412692012-06-05 22:31:30 +0200369 tmp_skb = old_info->skb_packet;
370 old_packet = (struct batadv_vis_packet *)tmp_skb->data;
Sven Eckelmann3e348192012-05-16 20:23:22 +0200371 if (!batadv_seq_after(ntohl(vis_packet->seqno),
372 ntohl(old_packet->seqno))) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000373 if (old_packet->seqno == vis_packet->seqno) {
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200374 batadv_recv_list_add(bat_priv,
375 &old_info->recv_list,
376 vis_packet->sender_orig);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000377 return old_info;
378 } else {
379 /* newer packet is already in hash. */
380 return NULL;
381 }
382 }
383 /* remove old entry */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200384 batadv_hash_remove(bat_priv->vis.hash, batadv_vis_info_cmp,
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200385 batadv_vis_info_choose, old_info);
386 batadv_send_list_del(old_info);
387 kref_put(&old_info->refcount, batadv_free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000388 }
389
Sven Eckelmann704509b2011-05-14 23:14:54 +0200390 info = kmalloc(sizeof(*info), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000391 if (!info)
392 return NULL;
393
Sven Eckelmann96412692012-06-05 22:31:30 +0200394 len = sizeof(*packet) + vis_info_len;
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200395 info->skb_packet = netdev_alloc_skb_ip_align(NULL, len + ETH_HLEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000396 if (!info->skb_packet) {
397 kfree(info);
398 return NULL;
399 }
Simon Wunderlichc54f38c2013-07-29 17:56:44 +0200400 info->skb_packet->priority = TC_PRIO_CONTROL;
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200401 skb_reserve(info->skb_packet, ETH_HLEN);
Sven Eckelmann96412692012-06-05 22:31:30 +0200402 packet = (struct batadv_vis_packet *)skb_put(info->skb_packet, len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000403
404 kref_init(&info->refcount);
405 INIT_LIST_HEAD(&info->send_list);
406 INIT_LIST_HEAD(&info->recv_list);
407 info->first_seen = jiffies;
408 info->bat_priv = bat_priv;
Sven Eckelmann96412692012-06-05 22:31:30 +0200409 memcpy(packet, vis_packet, len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000410
411 /* initialize and add new packet. */
412 *is_new = 1;
413
414 /* Make it a broadcast packet, if required */
415 if (make_broadcast)
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200416 memcpy(packet->target_orig, batadv_broadcast_addr, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000417
418 /* repair if entries is longer than packet. */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200419 max_entries = vis_info_len / sizeof(struct batadv_vis_info_entry);
420 if (packet->entries > max_entries)
421 packet->entries = max_entries;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000422
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200423 batadv_recv_list_add(bat_priv, &info->recv_list, packet->sender_orig);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000424
425 /* try to add it */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200426 hash_added = batadv_hash_add(bat_priv->vis.hash, batadv_vis_info_cmp,
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200427 batadv_vis_info_choose, info,
428 &info->hash_entry);
Antonio Quartulli1a1f37d2011-07-10 00:36:36 +0200429 if (hash_added != 0) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000430 /* did not work (for some reason) */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200431 kref_put(&info->refcount, batadv_free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000432 info = NULL;
433 }
434
435 return info;
436}
437
438/* handle the server sync packet, forward if needed. */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200439void batadv_receive_server_sync_packet(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +0200440 struct batadv_vis_packet *vis_packet,
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200441 int vis_info_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000442{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200443 struct batadv_vis_info *info;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000444 int is_new, make_broadcast;
445 int vis_server = atomic_read(&bat_priv->vis_mode);
446
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200447 make_broadcast = (vis_server == BATADV_VIS_TYPE_SERVER_SYNC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000448
Sven Eckelmann807736f2012-07-15 22:26:51 +0200449 spin_lock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200450 info = batadv_add_packet(bat_priv, vis_packet, vis_info_len,
451 &is_new, make_broadcast);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000452 if (!info)
453 goto end;
454
455 /* only if we are server ourselves and packet is newer than the one in
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200456 * hash.
457 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200458 if (vis_server == BATADV_VIS_TYPE_SERVER_SYNC && is_new)
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200459 batadv_send_list_add(bat_priv, info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000460end:
Sven Eckelmann807736f2012-07-15 22:26:51 +0200461 spin_unlock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000462}
463
464/* handle an incoming client update packet and schedule forward if needed. */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200465void batadv_receive_client_update_packet(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +0200466 struct batadv_vis_packet *vis_packet,
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200467 int vis_info_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000468{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200469 struct batadv_vis_info *info;
Sven Eckelmann96412692012-06-05 22:31:30 +0200470 struct batadv_vis_packet *packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000471 int is_new;
472 int vis_server = atomic_read(&bat_priv->vis_mode);
473 int are_target = 0;
474
475 /* clients shall not broadcast. */
476 if (is_broadcast_ether_addr(vis_packet->target_orig))
477 return;
478
479 /* Are we the target for this VIS packet? */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200480 if (vis_server == BATADV_VIS_TYPE_SERVER_SYNC &&
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200481 batadv_is_my_mac(bat_priv, vis_packet->target_orig))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000482 are_target = 1;
483
Sven Eckelmann807736f2012-07-15 22:26:51 +0200484 spin_lock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200485 info = batadv_add_packet(bat_priv, vis_packet, vis_info_len,
486 &is_new, are_target);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000487
488 if (!info)
489 goto end;
490 /* note that outdated packets will be dropped at this point. */
491
Sven Eckelmann96412692012-06-05 22:31:30 +0200492 packet = (struct batadv_vis_packet *)info->skb_packet->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000493
494 /* send only if we're the target server or ... */
495 if (are_target && is_new) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200496 packet->vis_type = BATADV_VIS_TYPE_SERVER_SYNC; /* upgrade! */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200497 batadv_send_list_add(bat_priv, info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000498
499 /* ... we're not the recipient (and thus need to forward). */
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200500 } else if (!batadv_is_my_mac(bat_priv, packet->target_orig)) {
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200501 batadv_send_list_add(bat_priv, info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000502 }
503
504end:
Sven Eckelmann807736f2012-07-15 22:26:51 +0200505 spin_unlock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000506}
507
508/* Walk the originators and find the VIS server with the best tq. Set the packet
509 * address to its address and return the best_tq.
510 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200511 * Must be called with the originator hash locked
512 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200513static int batadv_find_best_vis_server(struct batadv_priv *bat_priv,
514 struct batadv_vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000515{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200516 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200517 struct batadv_neigh_node *router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000518 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200519 struct batadv_orig_node *orig_node;
Sven Eckelmann96412692012-06-05 22:31:30 +0200520 struct batadv_vis_packet *packet;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200521 int best_tq = -1;
522 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000523
Sven Eckelmann96412692012-06-05 22:31:30 +0200524 packet = (struct batadv_vis_packet *)info->skb_packet->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000525
526 for (i = 0; i < hash->size; i++) {
527 head = &hash->table[i];
528
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000529 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800530 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200531 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000532 if (!router)
533 continue;
534
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200535 if ((orig_node->flags & BATADV_VIS_SERVER) &&
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000536 (router->tq_avg > best_tq)) {
537 best_tq = router->tq_avg;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000538 memcpy(packet->target_orig, orig_node->orig,
539 ETH_ALEN);
540 }
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200541 batadv_neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000542 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000543 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000544 }
545
546 return best_tq;
547}
548
549/* Return true if the vis packet is full. */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200550static bool batadv_vis_packet_full(const struct batadv_vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000551{
Sven Eckelmann96412692012-06-05 22:31:30 +0200552 const struct batadv_vis_packet *packet;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200553 size_t num;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000554
Sven Eckelmann96412692012-06-05 22:31:30 +0200555 packet = (struct batadv_vis_packet *)info->skb_packet->data;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200556 num = BATADV_MAX_VIS_PACKET_SIZE / sizeof(struct batadv_vis_info_entry);
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200557
Sven Eckelmann56303d32012-06-05 22:31:31 +0200558 if (num < packet->entries + 1)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000559 return true;
560 return false;
561}
562
563/* generates a packet of own vis data,
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200564 * returns 0 on success, -1 if no packet could be generated
565 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200566static int batadv_generate_vis_packet(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000567{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200568 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000569 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200570 struct batadv_orig_node *orig_node;
571 struct batadv_neigh_node *router;
Sven Eckelmann807736f2012-07-15 22:26:51 +0200572 struct batadv_vis_info *info = bat_priv->vis.my_info;
Sven Eckelmann96412692012-06-05 22:31:30 +0200573 struct batadv_vis_packet *packet;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200574 struct batadv_vis_info_entry *entry;
575 struct batadv_tt_common_entry *tt_common_entry;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200576 uint8_t *packet_pos;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200577 int best_tq = -1;
578 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000579
580 info->first_seen = jiffies;
Sven Eckelmann96412692012-06-05 22:31:30 +0200581 packet = (struct batadv_vis_packet *)info->skb_packet->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000582 packet->vis_type = atomic_read(&bat_priv->vis_mode);
583
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200584 memcpy(packet->target_orig, batadv_broadcast_addr, ETH_ALEN);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200585 packet->header.ttl = BATADV_TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000586 packet->seqno = htonl(ntohl(packet->seqno) + 1);
587 packet->entries = 0;
Sven Eckelmann162d5492012-06-28 11:56:52 +0200588 packet->reserved = 0;
Sven Eckelmann704509b2011-05-14 23:14:54 +0200589 skb_trim(info->skb_packet, sizeof(*packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000590
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200591 if (packet->vis_type == BATADV_VIS_TYPE_CLIENT_UPDATE) {
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200592 best_tq = batadv_find_best_vis_server(bat_priv, info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000593
Marek Lindnerd0072602011-01-19 20:01:44 +0000594 if (best_tq < 0)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200595 return best_tq;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000596 }
597
598 for (i = 0; i < hash->size; i++) {
599 head = &hash->table[i];
600
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000601 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800602 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200603 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000604 if (!router)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000605 continue;
606
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200607 if (!batadv_compare_eth(router->addr, orig_node->orig))
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000608 goto next;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000609
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200610 if (router->if_incoming->if_status != BATADV_IF_ACTIVE)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000611 goto next;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000612
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000613 if (router->tq_avg < 1)
614 goto next;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000615
616 /* fill one entry into buffer. */
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200617 packet_pos = skb_put(info->skb_packet, sizeof(*entry));
618 entry = (struct batadv_vis_info_entry *)packet_pos;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000619 memcpy(entry->src,
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000620 router->if_incoming->net_dev->dev_addr,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000621 ETH_ALEN);
622 memcpy(entry->dest, orig_node->orig, ETH_ALEN);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000623 entry->quality = router->tq_avg;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000624 packet->entries++;
625
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000626next:
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200627 batadv_neigh_node_free_ref(router);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000628
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200629 if (batadv_vis_packet_full(info))
Marek Lindnerd0072602011-01-19 20:01:44 +0000630 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000631 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000632 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000633 }
634
Sven Eckelmann807736f2012-07-15 22:26:51 +0200635 hash = bat_priv->tt.local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000636
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000637 for (i = 0; i < hash->size; i++) {
638 head = &hash->table[i];
639
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200640 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800641 hlist_for_each_entry_rcu(tt_common_entry, head,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200642 hash_entry) {
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200643 packet_pos = skb_put(info->skb_packet, sizeof(*entry));
644 entry = (struct batadv_vis_info_entry *)packet_pos;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000645 memset(entry->src, 0, ETH_ALEN);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100646 memcpy(entry->dest, tt_common_entry->addr, ETH_ALEN);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200647 entry->quality = 0; /* 0 means TT */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000648 packet->entries++;
649
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200650 if (batadv_vis_packet_full(info))
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200651 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000652 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200653 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000654 }
655
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000656 return 0;
Marek Lindnerd0072602011-01-19 20:01:44 +0000657
658unlock:
659 rcu_read_unlock();
660 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000661}
662
663/* free old vis packets. Must be called with this vis_hash_lock
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200664 * held
665 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200666static void batadv_purge_vis_packets(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000667{
Antonio Quartullic90681b2011-10-05 17:05:25 +0200668 uint32_t i;
Sven Eckelmann807736f2012-07-15 22:26:51 +0200669 struct batadv_hashtable *hash = bat_priv->vis.hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800670 struct hlist_node *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000671 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200672 struct batadv_vis_info *info;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000673
674 for (i = 0; i < hash->size; i++) {
675 head = &hash->table[i];
676
Sasha Levinb67bfe02013-02-27 17:06:00 -0800677 hlist_for_each_entry_safe(info, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +0000678 head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000679 /* never purge own data. */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200680 if (info == bat_priv->vis.my_info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000681 continue;
682
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200683 if (batadv_has_timed_out(info->first_seen,
Sven Eckelmannedbf7ff2012-06-03 22:19:16 +0200684 BATADV_VIS_TIMEOUT)) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800685 hlist_del(&info->hash_entry);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200686 batadv_send_list_del(info);
687 kref_put(&info->refcount, batadv_free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000688 }
689 }
690 }
691}
692
Sven Eckelmann56303d32012-06-05 22:31:31 +0200693static void batadv_broadcast_vis_packet(struct batadv_priv *bat_priv,
694 struct batadv_vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000695{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200696 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000697 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200698 struct batadv_orig_node *orig_node;
Sven Eckelmann96412692012-06-05 22:31:30 +0200699 struct batadv_vis_packet *packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000700 struct sk_buff *skb;
Martin Hundebølle91ecfc2013-04-20 13:54:39 +0200701 uint32_t i, res;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000702
703
Sven Eckelmann96412692012-06-05 22:31:30 +0200704 packet = (struct batadv_vis_packet *)info->skb_packet->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000705
706 /* send to all routers in range. */
707 for (i = 0; i < hash->size; i++) {
708 head = &hash->table[i];
709
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000710 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800711 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000712 /* if it's a vis server and reachable, send it. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200713 if (!(orig_node->flags & BATADV_VIS_SERVER))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000714 continue;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000715
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000716 /* don't send it if we already received the packet from
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200717 * this node.
718 */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200719 if (batadv_recv_list_is_in(bat_priv, &info->recv_list,
Martin Hundebøllbb351ba2012-10-16 16:13:48 +0200720 orig_node->orig))
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000721 continue;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000722
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000723 memcpy(packet->target_orig, orig_node->orig, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000724 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
Martin Hundebøllbb351ba2012-10-16 16:13:48 +0200725 if (!skb)
726 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000727
Martin Hundebølle91ecfc2013-04-20 13:54:39 +0200728 res = batadv_send_skb_to_orig(skb, orig_node, NULL);
729 if (res == NET_XMIT_DROP)
Martin Hundebøllbb351ba2012-10-16 16:13:48 +0200730 kfree_skb(skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000731 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000732 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000733 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000734}
735
Sven Eckelmann56303d32012-06-05 22:31:31 +0200736static void batadv_unicast_vis_packet(struct batadv_priv *bat_priv,
737 struct batadv_vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000738{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200739 struct batadv_orig_node *orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000740 struct sk_buff *skb;
Sven Eckelmann96412692012-06-05 22:31:30 +0200741 struct batadv_vis_packet *packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000742
Sven Eckelmann96412692012-06-05 22:31:30 +0200743 packet = (struct batadv_vis_packet *)info->skb_packet->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000744
Sven Eckelmannda641192012-05-12 13:48:56 +0200745 orig_node = batadv_orig_hash_find(bat_priv, packet->target_orig);
Marek Lindner44524fc2011-02-10 14:33:53 +0000746 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000747 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000748
Martin Hundebøllbb351ba2012-10-16 16:13:48 +0200749 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
750 if (!skb)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000751 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000752
Martin Hundebølle91ecfc2013-04-20 13:54:39 +0200753 if (batadv_send_skb_to_orig(skb, orig_node, NULL) == NET_XMIT_DROP)
Martin Hundebøllbb351ba2012-10-16 16:13:48 +0200754 kfree_skb(skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000755
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000756out:
Marek Lindner44524fc2011-02-10 14:33:53 +0000757 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200758 batadv_orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000759}
760
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200761/* only send one vis packet. called from batadv_send_vis_packets() */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200762static void batadv_send_vis_packet(struct batadv_priv *bat_priv,
763 struct batadv_vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000764{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200765 struct batadv_hard_iface *primary_if;
Sven Eckelmann96412692012-06-05 22:31:30 +0200766 struct batadv_vis_packet *packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000767
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200768 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200769 if (!primary_if)
770 goto out;
771
Sven Eckelmann96412692012-06-05 22:31:30 +0200772 packet = (struct batadv_vis_packet *)info->skb_packet->data;
Sven Eckelmann76543d12011-11-20 15:47:38 +0100773 if (packet->header.ttl < 2) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000774 pr_debug("Error - can't send vis packet: ttl exceeded\n");
Marek Lindner32ae9b22011-04-20 15:40:58 +0200775 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000776 }
777
Marek Lindner32ae9b22011-04-20 15:40:58 +0200778 memcpy(packet->sender_orig, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmann76543d12011-11-20 15:47:38 +0100779 packet->header.ttl--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000780
781 if (is_broadcast_ether_addr(packet->target_orig))
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200782 batadv_broadcast_vis_packet(bat_priv, info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000783 else
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200784 batadv_unicast_vis_packet(bat_priv, info);
Sven Eckelmann76543d12011-11-20 15:47:38 +0100785 packet->header.ttl++; /* restore TTL */
Marek Lindner32ae9b22011-04-20 15:40:58 +0200786
787out:
788 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200789 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000790}
791
792/* called from timer; send (and maybe generate) vis packet. */
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200793static void batadv_send_vis_packets(struct work_struct *work)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000794{
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200795 struct delayed_work *delayed_work;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200796 struct batadv_priv *bat_priv;
Sven Eckelmann807736f2012-07-15 22:26:51 +0200797 struct batadv_priv_vis *priv_vis;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200798 struct batadv_vis_info *info;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000799
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200800 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200801 priv_vis = container_of(delayed_work, struct batadv_priv_vis, work);
802 bat_priv = container_of(priv_vis, struct batadv_priv, vis);
803 spin_lock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200804 batadv_purge_vis_packets(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000805
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200806 if (batadv_generate_vis_packet(bat_priv) == 0) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000807 /* schedule if generation was successful */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200808 batadv_send_list_add(bat_priv, bat_priv->vis.my_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000809 }
810
Sven Eckelmann807736f2012-07-15 22:26:51 +0200811 while (!list_empty(&bat_priv->vis.send_list)) {
812 info = list_first_entry(&bat_priv->vis.send_list,
Sven Eckelmann1181e1d2011-01-28 18:34:07 +0100813 typeof(*info), send_list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000814
815 kref_get(&info->refcount);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200816 spin_unlock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000817
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200818 batadv_send_vis_packet(bat_priv, info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000819
Sven Eckelmann807736f2012-07-15 22:26:51 +0200820 spin_lock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200821 batadv_send_list_del(info);
822 kref_put(&info->refcount, batadv_free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000823 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200824 spin_unlock_bh(&bat_priv->vis.hash_lock);
Antonio Quartulli72414442012-12-25 13:14:37 +0100825
826 queue_delayed_work(batadv_event_workqueue, &bat_priv->vis.work,
827 msecs_to_jiffies(BATADV_VIS_INTERVAL));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000828}
829
830/* init the vis server. this may only be called when if_list is already
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200831 * initialized (e.g. bat0 is initialized, interfaces have been added)
832 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200833int batadv_vis_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000834{
Sven Eckelmann96412692012-06-05 22:31:30 +0200835 struct batadv_vis_packet *packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000836 int hash_added;
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200837 unsigned int len;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200838 unsigned long first_seen;
Sven Eckelmann96412692012-06-05 22:31:30 +0200839 struct sk_buff *tmp_skb;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000840
Sven Eckelmann807736f2012-07-15 22:26:51 +0200841 if (bat_priv->vis.hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200842 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000843
Sven Eckelmann807736f2012-07-15 22:26:51 +0200844 spin_lock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000845
Sven Eckelmann807736f2012-07-15 22:26:51 +0200846 bat_priv->vis.hash = batadv_hash_new(256);
847 if (!bat_priv->vis.hash) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000848 pr_err("Can't initialize vis_hash\n");
849 goto err;
850 }
851
Antonio Quartullidec05072012-11-10 11:00:32 +0100852 batadv_hash_set_lock_class(bat_priv->vis.hash,
853 &batadv_vis_hash_lock_class_key);
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
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200859 len = sizeof(*packet) + BATADV_MAX_VIS_PACKET_SIZE + ETH_HLEN;
860 bat_priv->vis.my_info->skb_packet = netdev_alloc_skb_ip_align(NULL,
861 len);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200862 if (!bat_priv->vis.my_info->skb_packet)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000863 goto free_info;
864
Simon Wunderlichc54f38c2013-07-29 17:56:44 +0200865 bat_priv->vis.my_info->skb_packet->priority = TC_PRIO_CONTROL;
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200866 skb_reserve(bat_priv->vis.my_info->skb_packet, ETH_HLEN);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200867 tmp_skb = bat_priv->vis.my_info->skb_packet;
Sven Eckelmann96412692012-06-05 22:31:30 +0200868 packet = (struct batadv_vis_packet *)skb_put(tmp_skb, sizeof(*packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000869
870 /* prefill the vis info */
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200871 first_seen = jiffies - msecs_to_jiffies(BATADV_VIS_INTERVAL);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200872 bat_priv->vis.my_info->first_seen = first_seen;
873 INIT_LIST_HEAD(&bat_priv->vis.my_info->recv_list);
874 INIT_LIST_HEAD(&bat_priv->vis.my_info->send_list);
875 kref_init(&bat_priv->vis.my_info->refcount);
876 bat_priv->vis.my_info->bat_priv = bat_priv;
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200877 packet->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200878 packet->header.packet_type = BATADV_VIS;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200879 packet->header.ttl = BATADV_TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000880 packet->seqno = 0;
Sven Eckelmann162d5492012-06-28 11:56:52 +0200881 packet->reserved = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000882 packet->entries = 0;
883
Sven Eckelmann807736f2012-07-15 22:26:51 +0200884 INIT_LIST_HEAD(&bat_priv->vis.send_list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000885
Sven Eckelmann807736f2012-07-15 22:26:51 +0200886 hash_added = batadv_hash_add(bat_priv->vis.hash, batadv_vis_info_cmp,
Sven Eckelmanneaad8ad2012-05-16 20:23:18 +0200887 batadv_vis_info_choose,
Sven Eckelmann807736f2012-07-15 22:26:51 +0200888 bat_priv->vis.my_info,
889 &bat_priv->vis.my_info->hash_entry);
Antonio Quartulli1a1f37d2011-07-10 00:36:36 +0200890 if (hash_added != 0) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000891 pr_err("Can't add own vis packet into hash\n");
892 /* not in hash, need to remove it manually. */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200893 kref_put(&bat_priv->vis.my_info->refcount, batadv_free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000894 goto err;
895 }
896
Sven Eckelmann807736f2012-07-15 22:26:51 +0200897 spin_unlock_bh(&bat_priv->vis.hash_lock);
Antonio Quartulli72414442012-12-25 13:14:37 +0100898
899 INIT_DELAYED_WORK(&bat_priv->vis.work, batadv_send_vis_packets);
900 queue_delayed_work(batadv_event_workqueue, &bat_priv->vis.work,
901 msecs_to_jiffies(BATADV_VIS_INTERVAL));
902
Sven Eckelmann5346c352012-05-05 13:27:28 +0200903 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000904
905free_info:
Sven Eckelmann807736f2012-07-15 22:26:51 +0200906 kfree(bat_priv->vis.my_info);
907 bat_priv->vis.my_info = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000908err:
Sven Eckelmann807736f2012-07-15 22:26:51 +0200909 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{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200917 struct batadv_vis_info *info;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000918
Sven Eckelmann56303d32012-06-05 22:31:31 +0200919 info = container_of(node, struct batadv_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 Eckelmann56303d32012-06-05 22:31:31 +0200925void batadv_vis_quit(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000926{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200927 if (!bat_priv->vis.hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000928 return;
929
Sven Eckelmann807736f2012-07-15 22:26:51 +0200930 cancel_delayed_work_sync(&bat_priv->vis.work);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000931
Sven Eckelmann807736f2012-07-15 22:26:51 +0200932 spin_lock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000933 /* properly remove, kill timers ... */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200934 batadv_hash_delete(bat_priv->vis.hash, batadv_free_info_ref, NULL);
935 bat_priv->vis.hash = NULL;
936 bat_priv->vis.my_info = NULL;
937 spin_unlock_bh(&bat_priv->vis.hash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000938}