blob: be7b5cc71d28c06d1779e2be28132c17705241d0 [file] [log] [blame]
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001/*
Sven Eckelmann64afe352011-01-27 10:38:15 +01002 * Copyright (C) 2007-2011 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00003 *
4 * Marek Lindner, Simon Wunderlich
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301, USA
19 *
20 */
21
22#include "main.h"
23#include "translation-table.h"
24#include "soft-interface.h"
Marek Lindner32ae9b22011-04-20 15:40:58 +020025#include "hard-interface.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000026#include "hash.h"
27#include "originator.h"
28
Antonio Quartulli2dafb492011-05-05 08:42:45 +020029static void tt_local_purge(struct work_struct *work);
30static void _tt_global_del_orig(struct bat_priv *bat_priv,
31 struct tt_global_entry *tt_global_entry,
Sven Eckelmann747e4222011-05-14 23:14:50 +020032 const char *message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000033
Marek Lindner7aadf882011-02-18 12:28:09 +000034/* returns 1 if they are the same mac addr */
Sven Eckelmann747e4222011-05-14 23:14:50 +020035static int compare_ltt(const struct hlist_node *node, const void *data2)
Marek Lindner7aadf882011-02-18 12:28:09 +000036{
Sven Eckelmann747e4222011-05-14 23:14:50 +020037 const void *data1 = container_of(node, struct tt_local_entry,
38 hash_entry);
Marek Lindner7aadf882011-02-18 12:28:09 +000039
40 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
41}
42
43/* returns 1 if they are the same mac addr */
Sven Eckelmann747e4222011-05-14 23:14:50 +020044static int compare_gtt(const struct hlist_node *node, const void *data2)
Marek Lindner7aadf882011-02-18 12:28:09 +000045{
Sven Eckelmann747e4222011-05-14 23:14:50 +020046 const void *data1 = container_of(node, struct tt_global_entry,
47 hash_entry);
Marek Lindner7aadf882011-02-18 12:28:09 +000048
49 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
50}
51
Antonio Quartulli2dafb492011-05-05 08:42:45 +020052static void tt_local_start_timer(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000053{
Antonio Quartulli2dafb492011-05-05 08:42:45 +020054 INIT_DELAYED_WORK(&bat_priv->tt_work, tt_local_purge);
55 queue_delayed_work(bat_event_workqueue, &bat_priv->tt_work, 10 * HZ);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000056}
57
Antonio Quartulli2dafb492011-05-05 08:42:45 +020058static struct tt_local_entry *tt_local_hash_find(struct bat_priv *bat_priv,
Sven Eckelmann747e4222011-05-14 23:14:50 +020059 const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +000060{
Antonio Quartulli2dafb492011-05-05 08:42:45 +020061 struct hashtable_t *hash = bat_priv->tt_local_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +000062 struct hlist_head *head;
63 struct hlist_node *node;
Antonio Quartulli2dafb492011-05-05 08:42:45 +020064 struct tt_local_entry *tt_local_entry, *tt_local_entry_tmp = NULL;
Marek Lindner7aadf882011-02-18 12:28:09 +000065 int index;
66
67 if (!hash)
68 return NULL;
69
70 index = choose_orig(data, hash->size);
71 head = &hash->table[index];
72
73 rcu_read_lock();
Antonio Quartulli2dafb492011-05-05 08:42:45 +020074 hlist_for_each_entry_rcu(tt_local_entry, node, head, hash_entry) {
75 if (!compare_eth(tt_local_entry, data))
Marek Lindner7aadf882011-02-18 12:28:09 +000076 continue;
77
Antonio Quartulli2dafb492011-05-05 08:42:45 +020078 tt_local_entry_tmp = tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +000079 break;
80 }
81 rcu_read_unlock();
82
Antonio Quartulli2dafb492011-05-05 08:42:45 +020083 return tt_local_entry_tmp;
Marek Lindner7aadf882011-02-18 12:28:09 +000084}
85
Antonio Quartulli2dafb492011-05-05 08:42:45 +020086static struct tt_global_entry *tt_global_hash_find(struct bat_priv *bat_priv,
Sven Eckelmann747e4222011-05-14 23:14:50 +020087 const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +000088{
Antonio Quartulli2dafb492011-05-05 08:42:45 +020089 struct hashtable_t *hash = bat_priv->tt_global_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +000090 struct hlist_head *head;
91 struct hlist_node *node;
Antonio Quartulli2dafb492011-05-05 08:42:45 +020092 struct tt_global_entry *tt_global_entry;
93 struct tt_global_entry *tt_global_entry_tmp = NULL;
Marek Lindner7aadf882011-02-18 12:28:09 +000094 int index;
95
96 if (!hash)
97 return NULL;
98
99 index = choose_orig(data, hash->size);
100 head = &hash->table[index];
101
102 rcu_read_lock();
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200103 hlist_for_each_entry_rcu(tt_global_entry, node, head, hash_entry) {
104 if (!compare_eth(tt_global_entry, data))
Marek Lindner7aadf882011-02-18 12:28:09 +0000105 continue;
106
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200107 tt_global_entry_tmp = tt_global_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000108 break;
109 }
110 rcu_read_unlock();
111
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200112 return tt_global_entry_tmp;
Marek Lindner7aadf882011-02-18 12:28:09 +0000113}
114
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200115int tt_local_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000116{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200117 if (bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000118 return 1;
119
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200120 bat_priv->tt_local_hash = hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000121
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200122 if (!bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000123 return 0;
124
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200125 atomic_set(&bat_priv->tt_local_changed, 0);
126 tt_local_start_timer(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000127
128 return 1;
129}
130
Sven Eckelmann747e4222011-05-14 23:14:50 +0200131void tt_local_add(struct net_device *soft_iface, const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000132{
133 struct bat_priv *bat_priv = netdev_priv(soft_iface);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200134 struct tt_local_entry *tt_local_entry;
135 struct tt_global_entry *tt_global_entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000136 int required_bytes;
137
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200138 spin_lock_bh(&bat_priv->tt_lhash_lock);
139 tt_local_entry = tt_local_hash_find(bat_priv, addr);
140 spin_unlock_bh(&bat_priv->tt_lhash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000141
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200142 if (tt_local_entry) {
143 tt_local_entry->last_seen = jiffies;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000144 return;
145 }
146
147 /* only announce as many hosts as possible in the batman-packet and
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200148 space in batman_packet->num_tt That also should give a limit to
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000149 MAC-flooding. */
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200150 required_bytes = (bat_priv->num_local_tt + 1) * ETH_ALEN;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000151 required_bytes += BAT_PACKET_LEN;
152
153 if ((required_bytes > ETH_DATA_LEN) ||
154 (atomic_read(&bat_priv->aggregated_ogms) &&
155 required_bytes > MAX_AGGREGATION_BYTES) ||
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200156 (bat_priv->num_local_tt + 1 > 255)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000157 bat_dbg(DBG_ROUTES, bat_priv,
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200158 "Can't add new local tt entry (%pM): "
159 "number of local tt entries exceeds packet size\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000160 addr);
161 return;
162 }
163
164 bat_dbg(DBG_ROUTES, bat_priv,
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200165 "Creating new local tt entry: %pM\n", addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000166
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200167 tt_local_entry = kmalloc(sizeof(struct tt_local_entry), GFP_ATOMIC);
168 if (!tt_local_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000169 return;
170
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200171 memcpy(tt_local_entry->addr, addr, ETH_ALEN);
172 tt_local_entry->last_seen = jiffies;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000173
174 /* the batman interface mac address should never be purged */
Marek Lindner39901e72011-02-18 12:28:08 +0000175 if (compare_eth(addr, soft_iface->dev_addr))
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200176 tt_local_entry->never_purge = 1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000177 else
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200178 tt_local_entry->never_purge = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000179
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200180 spin_lock_bh(&bat_priv->tt_lhash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000181
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200182 hash_add(bat_priv->tt_local_hash, compare_ltt, choose_orig,
183 tt_local_entry, &tt_local_entry->hash_entry);
184 bat_priv->num_local_tt++;
185 atomic_set(&bat_priv->tt_local_changed, 1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000186
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200187 spin_unlock_bh(&bat_priv->tt_lhash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000188
189 /* remove address from global hash if present */
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200190 spin_lock_bh(&bat_priv->tt_ghash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000191
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200192 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000193
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200194 if (tt_global_entry)
195 _tt_global_del_orig(bat_priv, tt_global_entry,
196 "local tt received");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000197
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200198 spin_unlock_bh(&bat_priv->tt_ghash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000199}
200
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200201int tt_local_fill_buffer(struct bat_priv *bat_priv,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000202 unsigned char *buff, int buff_len)
203{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200204 struct hashtable_t *hash = bat_priv->tt_local_hash;
205 struct tt_local_entry *tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000206 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000207 struct hlist_head *head;
Marek Lindner7aadf882011-02-18 12:28:09 +0000208 int i, count = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000209
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200210 spin_lock_bh(&bat_priv->tt_lhash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000211
212 for (i = 0; i < hash->size; i++) {
213 head = &hash->table[i];
214
Marek Lindner7aadf882011-02-18 12:28:09 +0000215 rcu_read_lock();
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200216 hlist_for_each_entry_rcu(tt_local_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000217 head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000218 if (buff_len < (count + 1) * ETH_ALEN)
219 break;
220
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200221 memcpy(buff + (count * ETH_ALEN), tt_local_entry->addr,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000222 ETH_ALEN);
223
224 count++;
225 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000226 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000227 }
228
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200229 /* if we did not get all new local tts see you next time ;-) */
230 if (count == bat_priv->num_local_tt)
231 atomic_set(&bat_priv->tt_local_changed, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000232
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200233 spin_unlock_bh(&bat_priv->tt_lhash_lock);
Sven Eckelmann53320fe2010-12-20 10:32:03 -0800234 return count;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000235}
236
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200237int tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000238{
239 struct net_device *net_dev = (struct net_device *)seq->private;
240 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200241 struct hashtable_t *hash = bat_priv->tt_local_hash;
242 struct tt_local_entry *tt_local_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200243 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000244 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000245 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000246 size_t buf_size, pos;
247 char *buff;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200248 int i, ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000249
Marek Lindner32ae9b22011-04-20 15:40:58 +0200250 primary_if = primary_if_get_selected(bat_priv);
251 if (!primary_if) {
252 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
253 "please specify interfaces to enable it\n",
254 net_dev->name);
255 goto out;
256 }
257
258 if (primary_if->if_status != IF_ACTIVE) {
259 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
260 "primary interface not active\n",
261 net_dev->name);
262 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000263 }
264
265 seq_printf(seq, "Locally retrieved addresses (from %s) "
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200266 "announced via TT:\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000267 net_dev->name);
268
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200269 spin_lock_bh(&bat_priv->tt_lhash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000270
271 buf_size = 1;
272 /* Estimate length for: " * xx:xx:xx:xx:xx:xx\n" */
273 for (i = 0; i < hash->size; i++) {
274 head = &hash->table[i];
275
Marek Lindner7aadf882011-02-18 12:28:09 +0000276 rcu_read_lock();
277 __hlist_for_each_rcu(node, head)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000278 buf_size += 21;
Marek Lindner7aadf882011-02-18 12:28:09 +0000279 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000280 }
281
282 buff = kmalloc(buf_size, GFP_ATOMIC);
283 if (!buff) {
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200284 spin_unlock_bh(&bat_priv->tt_lhash_lock);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200285 ret = -ENOMEM;
286 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000287 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000288
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000289 buff[0] = '\0';
290 pos = 0;
291
292 for (i = 0; i < hash->size; i++) {
293 head = &hash->table[i];
294
Marek Lindner7aadf882011-02-18 12:28:09 +0000295 rcu_read_lock();
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200296 hlist_for_each_entry_rcu(tt_local_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000297 head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000298 pos += snprintf(buff + pos, 22, " * %pM\n",
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200299 tt_local_entry->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000300 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000301 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000302 }
303
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200304 spin_unlock_bh(&bat_priv->tt_lhash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000305
306 seq_printf(seq, "%s", buff);
307 kfree(buff);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200308out:
309 if (primary_if)
310 hardif_free_ref(primary_if);
311 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000312}
313
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200314static void _tt_local_del(struct hlist_node *node, void *arg)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000315{
316 struct bat_priv *bat_priv = (struct bat_priv *)arg;
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200317 void *data = container_of(node, struct tt_local_entry, hash_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000318
319 kfree(data);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200320 bat_priv->num_local_tt--;
321 atomic_set(&bat_priv->tt_local_changed, 1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000322}
323
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200324static void tt_local_del(struct bat_priv *bat_priv,
Sven Eckelmann747e4222011-05-14 23:14:50 +0200325 struct tt_local_entry *tt_local_entry,
326 const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000327{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200328 bat_dbg(DBG_ROUTES, bat_priv, "Deleting local tt entry (%pM): %s\n",
329 tt_local_entry->addr, message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000330
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200331 hash_remove(bat_priv->tt_local_hash, compare_ltt, choose_orig,
332 tt_local_entry->addr);
333 _tt_local_del(&tt_local_entry->hash_entry, bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000334}
335
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200336void tt_local_remove(struct bat_priv *bat_priv,
Sven Eckelmann747e4222011-05-14 23:14:50 +0200337 const uint8_t *addr, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000338{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200339 struct tt_local_entry *tt_local_entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000340
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200341 spin_lock_bh(&bat_priv->tt_lhash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000342
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200343 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000344
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200345 if (tt_local_entry)
346 tt_local_del(bat_priv, tt_local_entry, message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000347
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200348 spin_unlock_bh(&bat_priv->tt_lhash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000349}
350
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200351static void tt_local_purge(struct work_struct *work)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000352{
353 struct delayed_work *delayed_work =
354 container_of(work, struct delayed_work, work);
355 struct bat_priv *bat_priv =
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200356 container_of(delayed_work, struct bat_priv, tt_work);
357 struct hashtable_t *hash = bat_priv->tt_local_hash;
358 struct tt_local_entry *tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000359 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000360 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000361 unsigned long timeout;
Marek Lindner7aadf882011-02-18 12:28:09 +0000362 int i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000363
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200364 spin_lock_bh(&bat_priv->tt_lhash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000365
366 for (i = 0; i < hash->size; i++) {
367 head = &hash->table[i];
368
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200369 hlist_for_each_entry_safe(tt_local_entry, node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +0000370 head, hash_entry) {
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200371 if (tt_local_entry->never_purge)
Marek Lindner7aadf882011-02-18 12:28:09 +0000372 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000373
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200374 timeout = tt_local_entry->last_seen;
375 timeout += TT_LOCAL_TIMEOUT * HZ;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000376
Marek Lindner7aadf882011-02-18 12:28:09 +0000377 if (time_before(jiffies, timeout))
378 continue;
379
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200380 tt_local_del(bat_priv, tt_local_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +0000381 "address timed out");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000382 }
383 }
384
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200385 spin_unlock_bh(&bat_priv->tt_lhash_lock);
386 tt_local_start_timer(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000387}
388
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200389void tt_local_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000390{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200391 if (!bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000392 return;
393
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200394 cancel_delayed_work_sync(&bat_priv->tt_work);
395 hash_delete(bat_priv->tt_local_hash, _tt_local_del, bat_priv);
396 bat_priv->tt_local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000397}
398
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200399int tt_global_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000400{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200401 if (bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000402 return 1;
403
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200404 bat_priv->tt_global_hash = hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000405
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200406 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000407 return 0;
408
409 return 1;
410}
411
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200412void tt_global_add_orig(struct bat_priv *bat_priv,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000413 struct orig_node *orig_node,
Sven Eckelmann747e4222011-05-14 23:14:50 +0200414 const unsigned char *tt_buff, int tt_buff_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000415{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200416 struct tt_global_entry *tt_global_entry;
417 struct tt_local_entry *tt_local_entry;
418 int tt_buff_count = 0;
Sven Eckelmann747e4222011-05-14 23:14:50 +0200419 const unsigned char *tt_ptr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000420
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200421 while ((tt_buff_count + 1) * ETH_ALEN <= tt_buff_len) {
422 spin_lock_bh(&bat_priv->tt_ghash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000423
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200424 tt_ptr = tt_buff + (tt_buff_count * ETH_ALEN);
425 tt_global_entry = tt_global_hash_find(bat_priv, tt_ptr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000426
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200427 if (!tt_global_entry) {
428 spin_unlock_bh(&bat_priv->tt_ghash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000429
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200430 tt_global_entry =
431 kmalloc(sizeof(struct tt_global_entry),
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000432 GFP_ATOMIC);
433
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200434 if (!tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000435 break;
436
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200437 memcpy(tt_global_entry->addr, tt_ptr, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000438
439 bat_dbg(DBG_ROUTES, bat_priv,
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200440 "Creating new global tt entry: "
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000441 "%pM (via %pM)\n",
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200442 tt_global_entry->addr, orig_node->orig);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000443
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200444 spin_lock_bh(&bat_priv->tt_ghash_lock);
445 hash_add(bat_priv->tt_global_hash, compare_gtt,
446 choose_orig, tt_global_entry,
447 &tt_global_entry->hash_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000448
449 }
450
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200451 tt_global_entry->orig_node = orig_node;
452 spin_unlock_bh(&bat_priv->tt_ghash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000453
454 /* remove address from local hash if present */
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200455 spin_lock_bh(&bat_priv->tt_lhash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000456
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200457 tt_ptr = tt_buff + (tt_buff_count * ETH_ALEN);
458 tt_local_entry = tt_local_hash_find(bat_priv, tt_ptr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000459
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200460 if (tt_local_entry)
461 tt_local_del(bat_priv, tt_local_entry,
462 "global tt received");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000463
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200464 spin_unlock_bh(&bat_priv->tt_lhash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000465
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200466 tt_buff_count++;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000467 }
468
469 /* initialize, and overwrite if malloc succeeds */
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200470 orig_node->tt_buff = NULL;
471 orig_node->tt_buff_len = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000472
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200473 if (tt_buff_len > 0) {
474 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
475 if (orig_node->tt_buff) {
476 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
477 orig_node->tt_buff_len = tt_buff_len;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000478 }
479 }
480}
481
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200482int tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000483{
484 struct net_device *net_dev = (struct net_device *)seq->private;
485 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200486 struct hashtable_t *hash = bat_priv->tt_global_hash;
487 struct tt_global_entry *tt_global_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200488 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000489 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000490 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000491 size_t buf_size, pos;
492 char *buff;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200493 int i, ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000494
Marek Lindner32ae9b22011-04-20 15:40:58 +0200495 primary_if = primary_if_get_selected(bat_priv);
496 if (!primary_if) {
497 ret = seq_printf(seq, "BATMAN mesh %s disabled - please "
498 "specify interfaces to enable it\n",
499 net_dev->name);
500 goto out;
501 }
502
503 if (primary_if->if_status != IF_ACTIVE) {
504 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
505 "primary interface not active\n",
506 net_dev->name);
507 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000508 }
509
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200510 seq_printf(seq,
511 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000512 net_dev->name);
513
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200514 spin_lock_bh(&bat_priv->tt_ghash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000515
516 buf_size = 1;
517 /* Estimate length for: " * xx:xx:xx:xx:xx:xx via xx:xx:xx:xx:xx:xx\n"*/
518 for (i = 0; i < hash->size; i++) {
519 head = &hash->table[i];
520
Marek Lindner7aadf882011-02-18 12:28:09 +0000521 rcu_read_lock();
522 __hlist_for_each_rcu(node, head)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000523 buf_size += 43;
Marek Lindner7aadf882011-02-18 12:28:09 +0000524 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000525 }
526
527 buff = kmalloc(buf_size, GFP_ATOMIC);
528 if (!buff) {
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200529 spin_unlock_bh(&bat_priv->tt_ghash_lock);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200530 ret = -ENOMEM;
531 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000532 }
533 buff[0] = '\0';
534 pos = 0;
535
536 for (i = 0; i < hash->size; i++) {
537 head = &hash->table[i];
538
Marek Lindner7aadf882011-02-18 12:28:09 +0000539 rcu_read_lock();
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200540 hlist_for_each_entry_rcu(tt_global_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000541 head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000542 pos += snprintf(buff + pos, 44,
543 " * %pM via %pM\n",
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200544 tt_global_entry->addr,
545 tt_global_entry->orig_node->orig);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000546 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000547 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000548 }
549
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200550 spin_unlock_bh(&bat_priv->tt_ghash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000551
552 seq_printf(seq, "%s", buff);
553 kfree(buff);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200554out:
555 if (primary_if)
556 hardif_free_ref(primary_if);
557 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000558}
559
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200560static void _tt_global_del_orig(struct bat_priv *bat_priv,
561 struct tt_global_entry *tt_global_entry,
Sven Eckelmann747e4222011-05-14 23:14:50 +0200562 const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000563{
564 bat_dbg(DBG_ROUTES, bat_priv,
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200565 "Deleting global tt entry %pM (via %pM): %s\n",
566 tt_global_entry->addr, tt_global_entry->orig_node->orig,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000567 message);
568
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200569 hash_remove(bat_priv->tt_global_hash, compare_gtt, choose_orig,
570 tt_global_entry->addr);
571 kfree(tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000572}
573
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200574void tt_global_del_orig(struct bat_priv *bat_priv,
Sven Eckelmann747e4222011-05-14 23:14:50 +0200575 struct orig_node *orig_node, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000576{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200577 struct tt_global_entry *tt_global_entry;
578 int tt_buff_count = 0;
579 unsigned char *tt_ptr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000580
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200581 if (orig_node->tt_buff_len == 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000582 return;
583
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200584 spin_lock_bh(&bat_priv->tt_ghash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000585
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200586 while ((tt_buff_count + 1) * ETH_ALEN <= orig_node->tt_buff_len) {
587 tt_ptr = orig_node->tt_buff + (tt_buff_count * ETH_ALEN);
588 tt_global_entry = tt_global_hash_find(bat_priv, tt_ptr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000589
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200590 if ((tt_global_entry) &&
591 (tt_global_entry->orig_node == orig_node))
592 _tt_global_del_orig(bat_priv, tt_global_entry,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000593 message);
594
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200595 tt_buff_count++;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000596 }
597
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200598 spin_unlock_bh(&bat_priv->tt_ghash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000599
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200600 orig_node->tt_buff_len = 0;
601 kfree(orig_node->tt_buff);
602 orig_node->tt_buff = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000603}
604
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200605static void tt_global_del(struct hlist_node *node, void *arg)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000606{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200607 void *data = container_of(node, struct tt_global_entry, hash_entry);
Marek Lindner7aadf882011-02-18 12:28:09 +0000608
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000609 kfree(data);
610}
611
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200612void tt_global_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000613{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200614 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000615 return;
616
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200617 hash_delete(bat_priv->tt_global_hash, tt_global_del, NULL);
618 bat_priv->tt_global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000619}
620
Sven Eckelmann747e4222011-05-14 23:14:50 +0200621struct orig_node *transtable_search(struct bat_priv *bat_priv,
622 const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000623{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200624 struct tt_global_entry *tt_global_entry;
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000625 struct orig_node *orig_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000626
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200627 spin_lock_bh(&bat_priv->tt_ghash_lock);
628 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Marek Lindner7aadf882011-02-18 12:28:09 +0000629
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200630 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000631 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000632
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200633 if (!atomic_inc_not_zero(&tt_global_entry->orig_node->refcount))
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000634 goto out;
635
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200636 orig_node = tt_global_entry->orig_node;
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000637
638out:
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200639 spin_unlock_bh(&bat_priv->tt_ghash_lock);
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000640 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000641}