blob: 561f76968d5e8151dcab28b96a39a9739f4cedf5 [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
Sven Eckelmann704509b2011-05-14 23:14:54 +0200167 tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200168 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{
Sven Eckelmann5f718c22011-05-14 23:14:52 +0200316 struct bat_priv *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
Sven Eckelmann704509b2011-05-14 23:14:54 +0200430 tt_global_entry = kmalloc(sizeof(*tt_global_entry),
431 GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000432
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200433 if (!tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000434 break;
435
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200436 memcpy(tt_global_entry->addr, tt_ptr, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000437
438 bat_dbg(DBG_ROUTES, bat_priv,
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200439 "Creating new global tt entry: "
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000440 "%pM (via %pM)\n",
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200441 tt_global_entry->addr, orig_node->orig);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000442
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200443 spin_lock_bh(&bat_priv->tt_ghash_lock);
444 hash_add(bat_priv->tt_global_hash, compare_gtt,
445 choose_orig, tt_global_entry,
446 &tt_global_entry->hash_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000447
448 }
449
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200450 tt_global_entry->orig_node = orig_node;
451 spin_unlock_bh(&bat_priv->tt_ghash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000452
453 /* remove address from local hash if present */
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200454 spin_lock_bh(&bat_priv->tt_lhash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000455
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200456 tt_ptr = tt_buff + (tt_buff_count * ETH_ALEN);
457 tt_local_entry = tt_local_hash_find(bat_priv, tt_ptr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000458
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200459 if (tt_local_entry)
460 tt_local_del(bat_priv, tt_local_entry,
461 "global tt received");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000462
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200463 spin_unlock_bh(&bat_priv->tt_lhash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000464
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200465 tt_buff_count++;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000466 }
467
468 /* initialize, and overwrite if malloc succeeds */
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200469 orig_node->tt_buff = NULL;
470 orig_node->tt_buff_len = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000471
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200472 if (tt_buff_len > 0) {
473 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
474 if (orig_node->tt_buff) {
475 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
476 orig_node->tt_buff_len = tt_buff_len;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000477 }
478 }
479}
480
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200481int tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000482{
483 struct net_device *net_dev = (struct net_device *)seq->private;
484 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200485 struct hashtable_t *hash = bat_priv->tt_global_hash;
486 struct tt_global_entry *tt_global_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200487 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000488 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000489 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000490 size_t buf_size, pos;
491 char *buff;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200492 int i, ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000493
Marek Lindner32ae9b22011-04-20 15:40:58 +0200494 primary_if = primary_if_get_selected(bat_priv);
495 if (!primary_if) {
496 ret = seq_printf(seq, "BATMAN mesh %s disabled - please "
497 "specify interfaces to enable it\n",
498 net_dev->name);
499 goto out;
500 }
501
502 if (primary_if->if_status != IF_ACTIVE) {
503 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
504 "primary interface not active\n",
505 net_dev->name);
506 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000507 }
508
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200509 seq_printf(seq,
510 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000511 net_dev->name);
512
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200513 spin_lock_bh(&bat_priv->tt_ghash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000514
515 buf_size = 1;
516 /* Estimate length for: " * xx:xx:xx:xx:xx:xx via xx:xx:xx:xx:xx:xx\n"*/
517 for (i = 0; i < hash->size; i++) {
518 head = &hash->table[i];
519
Marek Lindner7aadf882011-02-18 12:28:09 +0000520 rcu_read_lock();
521 __hlist_for_each_rcu(node, head)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000522 buf_size += 43;
Marek Lindner7aadf882011-02-18 12:28:09 +0000523 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000524 }
525
526 buff = kmalloc(buf_size, GFP_ATOMIC);
527 if (!buff) {
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200528 spin_unlock_bh(&bat_priv->tt_ghash_lock);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200529 ret = -ENOMEM;
530 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000531 }
532 buff[0] = '\0';
533 pos = 0;
534
535 for (i = 0; i < hash->size; i++) {
536 head = &hash->table[i];
537
Marek Lindner7aadf882011-02-18 12:28:09 +0000538 rcu_read_lock();
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200539 hlist_for_each_entry_rcu(tt_global_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000540 head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000541 pos += snprintf(buff + pos, 44,
542 " * %pM via %pM\n",
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200543 tt_global_entry->addr,
544 tt_global_entry->orig_node->orig);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000545 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000546 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000547 }
548
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200549 spin_unlock_bh(&bat_priv->tt_ghash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000550
551 seq_printf(seq, "%s", buff);
552 kfree(buff);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200553out:
554 if (primary_if)
555 hardif_free_ref(primary_if);
556 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000557}
558
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200559static void _tt_global_del_orig(struct bat_priv *bat_priv,
560 struct tt_global_entry *tt_global_entry,
Sven Eckelmann747e4222011-05-14 23:14:50 +0200561 const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000562{
563 bat_dbg(DBG_ROUTES, bat_priv,
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200564 "Deleting global tt entry %pM (via %pM): %s\n",
565 tt_global_entry->addr, tt_global_entry->orig_node->orig,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000566 message);
567
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200568 hash_remove(bat_priv->tt_global_hash, compare_gtt, choose_orig,
569 tt_global_entry->addr);
570 kfree(tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000571}
572
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200573void tt_global_del_orig(struct bat_priv *bat_priv,
Sven Eckelmann747e4222011-05-14 23:14:50 +0200574 struct orig_node *orig_node, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000575{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200576 struct tt_global_entry *tt_global_entry;
577 int tt_buff_count = 0;
578 unsigned char *tt_ptr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000579
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200580 if (orig_node->tt_buff_len == 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000581 return;
582
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200583 spin_lock_bh(&bat_priv->tt_ghash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000584
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200585 while ((tt_buff_count + 1) * ETH_ALEN <= orig_node->tt_buff_len) {
586 tt_ptr = orig_node->tt_buff + (tt_buff_count * ETH_ALEN);
587 tt_global_entry = tt_global_hash_find(bat_priv, tt_ptr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000588
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200589 if ((tt_global_entry) &&
590 (tt_global_entry->orig_node == orig_node))
591 _tt_global_del_orig(bat_priv, tt_global_entry,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000592 message);
593
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200594 tt_buff_count++;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000595 }
596
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200597 spin_unlock_bh(&bat_priv->tt_ghash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000598
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200599 orig_node->tt_buff_len = 0;
600 kfree(orig_node->tt_buff);
601 orig_node->tt_buff = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000602}
603
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200604static void tt_global_del(struct hlist_node *node, void *arg)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000605{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200606 void *data = container_of(node, struct tt_global_entry, hash_entry);
Marek Lindner7aadf882011-02-18 12:28:09 +0000607
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000608 kfree(data);
609}
610
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200611void tt_global_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000612{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200613 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000614 return;
615
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200616 hash_delete(bat_priv->tt_global_hash, tt_global_del, NULL);
617 bat_priv->tt_global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000618}
619
Sven Eckelmann747e4222011-05-14 23:14:50 +0200620struct orig_node *transtable_search(struct bat_priv *bat_priv,
621 const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000622{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200623 struct tt_global_entry *tt_global_entry;
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000624 struct orig_node *orig_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000625
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200626 spin_lock_bh(&bat_priv->tt_ghash_lock);
627 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Marek Lindner7aadf882011-02-18 12:28:09 +0000628
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200629 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000630 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000631
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200632 if (!atomic_inc_not_zero(&tt_global_entry->orig_node->refcount))
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000633 goto out;
634
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200635 orig_node = tt_global_entry->orig_node;
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000636
637out:
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200638 spin_unlock_bh(&bat_priv->tt_ghash_lock);
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000639 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000640}