Commit 7c1fd91da5a5eecc91674991199940c05f87cb3a

Authored by Antonio Quartulli
1 parent 28709878b6

batman-adv: substitute tt_poss_change with a per-tt_entry flag

tt_poss_change is a node-wide flag which tells whether the node is in a roaming
state (a client recently moved to/away from it) in order to let it apply special
re-routing rules. However this flag does not give a clear idea of the current
state because it is not possible to understand *which client* is actually
involved in the roaming. For this reason a better approach has been chosen:
instead of using a node-wide variable, the roaming state is now given by a
per-tt_entry ROAM flag which, in case of packet coming through the node, tells
the node whether the real destination is in roaming state or not.

With this flag change, batadv_check_unicast_ttvn() has also been rearranged in
order to better fit the new re-routing logic and to be much more readable.

Signed-off-by: Antonio Quartulli <ordex@autistici.org>

Showing 6 changed files with 156 additions and 75 deletions Inline Diff

net/batman-adv/originator.c
1 /* Copyright (C) 2009-2012 B.A.T.M.A.N. contributors: 1 /* Copyright (C) 2009-2012 B.A.T.M.A.N. contributors:
2 * 2 *
3 * Marek Lindner, Simon Wunderlich 3 * Marek Lindner, Simon Wunderlich
4 * 4 *
5 * This program is free software; you can redistribute it and/or 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 6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation. 7 * License as published by the Free Software Foundation.
8 * 8 *
9 * This program is distributed in the hope that it will be useful, but 9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of 10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details. 12 * General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 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 15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA 17 * 02110-1301, USA
18 */ 18 */
19 19
20 #include "main.h" 20 #include "main.h"
21 #include "distributed-arp-table.h" 21 #include "distributed-arp-table.h"
22 #include "originator.h" 22 #include "originator.h"
23 #include "hash.h" 23 #include "hash.h"
24 #include "translation-table.h" 24 #include "translation-table.h"
25 #include "routing.h" 25 #include "routing.h"
26 #include "gateway_client.h" 26 #include "gateway_client.h"
27 #include "hard-interface.h" 27 #include "hard-interface.h"
28 #include "unicast.h" 28 #include "unicast.h"
29 #include "soft-interface.h" 29 #include "soft-interface.h"
30 #include "bridge_loop_avoidance.h" 30 #include "bridge_loop_avoidance.h"
31 31
32 static void batadv_purge_orig(struct work_struct *work); 32 static void batadv_purge_orig(struct work_struct *work);
33 33
34 static void batadv_start_purge_timer(struct batadv_priv *bat_priv) 34 static void batadv_start_purge_timer(struct batadv_priv *bat_priv)
35 { 35 {
36 INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig); 36 INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig);
37 queue_delayed_work(batadv_event_workqueue, 37 queue_delayed_work(batadv_event_workqueue,
38 &bat_priv->orig_work, msecs_to_jiffies(1000)); 38 &bat_priv->orig_work, msecs_to_jiffies(1000));
39 } 39 }
40 40
41 /* returns 1 if they are the same originator */ 41 /* returns 1 if they are the same originator */
42 static int batadv_compare_orig(const struct hlist_node *node, const void *data2) 42 static int batadv_compare_orig(const struct hlist_node *node, const void *data2)
43 { 43 {
44 const void *data1 = container_of(node, struct batadv_orig_node, 44 const void *data1 = container_of(node, struct batadv_orig_node,
45 hash_entry); 45 hash_entry);
46 46
47 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0); 47 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
48 } 48 }
49 49
50 int batadv_originator_init(struct batadv_priv *bat_priv) 50 int batadv_originator_init(struct batadv_priv *bat_priv)
51 { 51 {
52 if (bat_priv->orig_hash) 52 if (bat_priv->orig_hash)
53 return 0; 53 return 0;
54 54
55 bat_priv->orig_hash = batadv_hash_new(1024); 55 bat_priv->orig_hash = batadv_hash_new(1024);
56 56
57 if (!bat_priv->orig_hash) 57 if (!bat_priv->orig_hash)
58 goto err; 58 goto err;
59 59
60 batadv_start_purge_timer(bat_priv); 60 batadv_start_purge_timer(bat_priv);
61 return 0; 61 return 0;
62 62
63 err: 63 err:
64 return -ENOMEM; 64 return -ENOMEM;
65 } 65 }
66 66
67 void batadv_neigh_node_free_ref(struct batadv_neigh_node *neigh_node) 67 void batadv_neigh_node_free_ref(struct batadv_neigh_node *neigh_node)
68 { 68 {
69 if (atomic_dec_and_test(&neigh_node->refcount)) 69 if (atomic_dec_and_test(&neigh_node->refcount))
70 kfree_rcu(neigh_node, rcu); 70 kfree_rcu(neigh_node, rcu);
71 } 71 }
72 72
73 /* increases the refcounter of a found router */ 73 /* increases the refcounter of a found router */
74 struct batadv_neigh_node * 74 struct batadv_neigh_node *
75 batadv_orig_node_get_router(struct batadv_orig_node *orig_node) 75 batadv_orig_node_get_router(struct batadv_orig_node *orig_node)
76 { 76 {
77 struct batadv_neigh_node *router; 77 struct batadv_neigh_node *router;
78 78
79 rcu_read_lock(); 79 rcu_read_lock();
80 router = rcu_dereference(orig_node->router); 80 router = rcu_dereference(orig_node->router);
81 81
82 if (router && !atomic_inc_not_zero(&router->refcount)) 82 if (router && !atomic_inc_not_zero(&router->refcount))
83 router = NULL; 83 router = NULL;
84 84
85 rcu_read_unlock(); 85 rcu_read_unlock();
86 return router; 86 return router;
87 } 87 }
88 88
89 struct batadv_neigh_node * 89 struct batadv_neigh_node *
90 batadv_neigh_node_new(struct batadv_hard_iface *hard_iface, 90 batadv_neigh_node_new(struct batadv_hard_iface *hard_iface,
91 const uint8_t *neigh_addr, uint32_t seqno) 91 const uint8_t *neigh_addr, uint32_t seqno)
92 { 92 {
93 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); 93 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
94 struct batadv_neigh_node *neigh_node; 94 struct batadv_neigh_node *neigh_node;
95 95
96 neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC); 96 neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
97 if (!neigh_node) 97 if (!neigh_node)
98 goto out; 98 goto out;
99 99
100 INIT_HLIST_NODE(&neigh_node->list); 100 INIT_HLIST_NODE(&neigh_node->list);
101 101
102 memcpy(neigh_node->addr, neigh_addr, ETH_ALEN); 102 memcpy(neigh_node->addr, neigh_addr, ETH_ALEN);
103 spin_lock_init(&neigh_node->lq_update_lock); 103 spin_lock_init(&neigh_node->lq_update_lock);
104 104
105 /* extra reference for return */ 105 /* extra reference for return */
106 atomic_set(&neigh_node->refcount, 2); 106 atomic_set(&neigh_node->refcount, 2);
107 107
108 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 108 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
109 "Creating new neighbor %pM, initial seqno %d\n", 109 "Creating new neighbor %pM, initial seqno %d\n",
110 neigh_addr, seqno); 110 neigh_addr, seqno);
111 111
112 out: 112 out:
113 return neigh_node; 113 return neigh_node;
114 } 114 }
115 115
116 static void batadv_orig_node_free_rcu(struct rcu_head *rcu) 116 static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
117 { 117 {
118 struct hlist_node *node, *node_tmp; 118 struct hlist_node *node, *node_tmp;
119 struct batadv_neigh_node *neigh_node, *tmp_neigh_node; 119 struct batadv_neigh_node *neigh_node, *tmp_neigh_node;
120 struct batadv_orig_node *orig_node; 120 struct batadv_orig_node *orig_node;
121 121
122 orig_node = container_of(rcu, struct batadv_orig_node, rcu); 122 orig_node = container_of(rcu, struct batadv_orig_node, rcu);
123 123
124 spin_lock_bh(&orig_node->neigh_list_lock); 124 spin_lock_bh(&orig_node->neigh_list_lock);
125 125
126 /* for all bonding members ... */ 126 /* for all bonding members ... */
127 list_for_each_entry_safe(neigh_node, tmp_neigh_node, 127 list_for_each_entry_safe(neigh_node, tmp_neigh_node,
128 &orig_node->bond_list, bonding_list) { 128 &orig_node->bond_list, bonding_list) {
129 list_del_rcu(&neigh_node->bonding_list); 129 list_del_rcu(&neigh_node->bonding_list);
130 batadv_neigh_node_free_ref(neigh_node); 130 batadv_neigh_node_free_ref(neigh_node);
131 } 131 }
132 132
133 /* for all neighbors towards this originator ... */ 133 /* for all neighbors towards this originator ... */
134 hlist_for_each_entry_safe(neigh_node, node, node_tmp, 134 hlist_for_each_entry_safe(neigh_node, node, node_tmp,
135 &orig_node->neigh_list, list) { 135 &orig_node->neigh_list, list) {
136 hlist_del_rcu(&neigh_node->list); 136 hlist_del_rcu(&neigh_node->list);
137 batadv_neigh_node_free_ref(neigh_node); 137 batadv_neigh_node_free_ref(neigh_node);
138 } 138 }
139 139
140 spin_unlock_bh(&orig_node->neigh_list_lock); 140 spin_unlock_bh(&orig_node->neigh_list_lock);
141 141
142 batadv_frag_list_free(&orig_node->frag_list); 142 batadv_frag_list_free(&orig_node->frag_list);
143 batadv_tt_global_del_orig(orig_node->bat_priv, orig_node, 143 batadv_tt_global_del_orig(orig_node->bat_priv, orig_node,
144 "originator timed out"); 144 "originator timed out");
145 145
146 kfree(orig_node->tt_buff); 146 kfree(orig_node->tt_buff);
147 kfree(orig_node->bcast_own); 147 kfree(orig_node->bcast_own);
148 kfree(orig_node->bcast_own_sum); 148 kfree(orig_node->bcast_own_sum);
149 kfree(orig_node); 149 kfree(orig_node);
150 } 150 }
151 151
152 void batadv_orig_node_free_ref(struct batadv_orig_node *orig_node) 152 void batadv_orig_node_free_ref(struct batadv_orig_node *orig_node)
153 { 153 {
154 if (atomic_dec_and_test(&orig_node->refcount)) 154 if (atomic_dec_and_test(&orig_node->refcount))
155 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu); 155 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
156 } 156 }
157 157
158 void batadv_originator_free(struct batadv_priv *bat_priv) 158 void batadv_originator_free(struct batadv_priv *bat_priv)
159 { 159 {
160 struct batadv_hashtable *hash = bat_priv->orig_hash; 160 struct batadv_hashtable *hash = bat_priv->orig_hash;
161 struct hlist_node *node, *node_tmp; 161 struct hlist_node *node, *node_tmp;
162 struct hlist_head *head; 162 struct hlist_head *head;
163 spinlock_t *list_lock; /* spinlock to protect write access */ 163 spinlock_t *list_lock; /* spinlock to protect write access */
164 struct batadv_orig_node *orig_node; 164 struct batadv_orig_node *orig_node;
165 uint32_t i; 165 uint32_t i;
166 166
167 if (!hash) 167 if (!hash)
168 return; 168 return;
169 169
170 cancel_delayed_work_sync(&bat_priv->orig_work); 170 cancel_delayed_work_sync(&bat_priv->orig_work);
171 171
172 bat_priv->orig_hash = NULL; 172 bat_priv->orig_hash = NULL;
173 173
174 for (i = 0; i < hash->size; i++) { 174 for (i = 0; i < hash->size; i++) {
175 head = &hash->table[i]; 175 head = &hash->table[i];
176 list_lock = &hash->list_locks[i]; 176 list_lock = &hash->list_locks[i];
177 177
178 spin_lock_bh(list_lock); 178 spin_lock_bh(list_lock);
179 hlist_for_each_entry_safe(orig_node, node, node_tmp, 179 hlist_for_each_entry_safe(orig_node, node, node_tmp,
180 head, hash_entry) { 180 head, hash_entry) {
181 181
182 hlist_del_rcu(node); 182 hlist_del_rcu(node);
183 batadv_orig_node_free_ref(orig_node); 183 batadv_orig_node_free_ref(orig_node);
184 } 184 }
185 spin_unlock_bh(list_lock); 185 spin_unlock_bh(list_lock);
186 } 186 }
187 187
188 batadv_hash_destroy(hash); 188 batadv_hash_destroy(hash);
189 } 189 }
190 190
191 /* this function finds or creates an originator entry for the given 191 /* this function finds or creates an originator entry for the given
192 * address if it does not exits 192 * address if it does not exits
193 */ 193 */
194 struct batadv_orig_node *batadv_get_orig_node(struct batadv_priv *bat_priv, 194 struct batadv_orig_node *batadv_get_orig_node(struct batadv_priv *bat_priv,
195 const uint8_t *addr) 195 const uint8_t *addr)
196 { 196 {
197 struct batadv_orig_node *orig_node; 197 struct batadv_orig_node *orig_node;
198 int size; 198 int size;
199 int hash_added; 199 int hash_added;
200 unsigned long reset_time; 200 unsigned long reset_time;
201 201
202 orig_node = batadv_orig_hash_find(bat_priv, addr); 202 orig_node = batadv_orig_hash_find(bat_priv, addr);
203 if (orig_node) 203 if (orig_node)
204 return orig_node; 204 return orig_node;
205 205
206 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 206 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
207 "Creating new originator: %pM\n", addr); 207 "Creating new originator: %pM\n", addr);
208 208
209 orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC); 209 orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
210 if (!orig_node) 210 if (!orig_node)
211 return NULL; 211 return NULL;
212 212
213 INIT_HLIST_HEAD(&orig_node->neigh_list); 213 INIT_HLIST_HEAD(&orig_node->neigh_list);
214 INIT_LIST_HEAD(&orig_node->bond_list); 214 INIT_LIST_HEAD(&orig_node->bond_list);
215 spin_lock_init(&orig_node->ogm_cnt_lock); 215 spin_lock_init(&orig_node->ogm_cnt_lock);
216 spin_lock_init(&orig_node->bcast_seqno_lock); 216 spin_lock_init(&orig_node->bcast_seqno_lock);
217 spin_lock_init(&orig_node->neigh_list_lock); 217 spin_lock_init(&orig_node->neigh_list_lock);
218 spin_lock_init(&orig_node->tt_buff_lock); 218 spin_lock_init(&orig_node->tt_buff_lock);
219 219
220 /* extra reference for return */ 220 /* extra reference for return */
221 atomic_set(&orig_node->refcount, 2); 221 atomic_set(&orig_node->refcount, 2);
222 222
223 orig_node->tt_initialised = false; 223 orig_node->tt_initialised = false;
224 orig_node->tt_poss_change = false;
225 orig_node->bat_priv = bat_priv; 224 orig_node->bat_priv = bat_priv;
226 memcpy(orig_node->orig, addr, ETH_ALEN); 225 memcpy(orig_node->orig, addr, ETH_ALEN);
227 batadv_dat_init_orig_node_addr(orig_node); 226 batadv_dat_init_orig_node_addr(orig_node);
228 orig_node->router = NULL; 227 orig_node->router = NULL;
229 orig_node->tt_crc = 0; 228 orig_node->tt_crc = 0;
230 atomic_set(&orig_node->last_ttvn, 0); 229 atomic_set(&orig_node->last_ttvn, 0);
231 orig_node->tt_buff = NULL; 230 orig_node->tt_buff = NULL;
232 orig_node->tt_buff_len = 0; 231 orig_node->tt_buff_len = 0;
233 atomic_set(&orig_node->tt_size, 0); 232 atomic_set(&orig_node->tt_size, 0);
234 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS); 233 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
235 orig_node->bcast_seqno_reset = reset_time; 234 orig_node->bcast_seqno_reset = reset_time;
236 orig_node->batman_seqno_reset = reset_time; 235 orig_node->batman_seqno_reset = reset_time;
237 236
238 atomic_set(&orig_node->bond_candidates, 0); 237 atomic_set(&orig_node->bond_candidates, 0);
239 238
240 size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS; 239 size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS;
241 240
242 orig_node->bcast_own = kzalloc(size, GFP_ATOMIC); 241 orig_node->bcast_own = kzalloc(size, GFP_ATOMIC);
243 if (!orig_node->bcast_own) 242 if (!orig_node->bcast_own)
244 goto free_orig_node; 243 goto free_orig_node;
245 244
246 size = bat_priv->num_ifaces * sizeof(uint8_t); 245 size = bat_priv->num_ifaces * sizeof(uint8_t);
247 orig_node->bcast_own_sum = kzalloc(size, GFP_ATOMIC); 246 orig_node->bcast_own_sum = kzalloc(size, GFP_ATOMIC);
248 247
249 INIT_LIST_HEAD(&orig_node->frag_list); 248 INIT_LIST_HEAD(&orig_node->frag_list);
250 orig_node->last_frag_packet = 0; 249 orig_node->last_frag_packet = 0;
251 250
252 if (!orig_node->bcast_own_sum) 251 if (!orig_node->bcast_own_sum)
253 goto free_bcast_own; 252 goto free_bcast_own;
254 253
255 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig, 254 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
256 batadv_choose_orig, orig_node, 255 batadv_choose_orig, orig_node,
257 &orig_node->hash_entry); 256 &orig_node->hash_entry);
258 if (hash_added != 0) 257 if (hash_added != 0)
259 goto free_bcast_own_sum; 258 goto free_bcast_own_sum;
260 259
261 return orig_node; 260 return orig_node;
262 free_bcast_own_sum: 261 free_bcast_own_sum:
263 kfree(orig_node->bcast_own_sum); 262 kfree(orig_node->bcast_own_sum);
264 free_bcast_own: 263 free_bcast_own:
265 kfree(orig_node->bcast_own); 264 kfree(orig_node->bcast_own);
266 free_orig_node: 265 free_orig_node:
267 kfree(orig_node); 266 kfree(orig_node);
268 return NULL; 267 return NULL;
269 } 268 }
270 269
271 static bool 270 static bool
272 batadv_purge_orig_neighbors(struct batadv_priv *bat_priv, 271 batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
273 struct batadv_orig_node *orig_node, 272 struct batadv_orig_node *orig_node,
274 struct batadv_neigh_node **best_neigh_node) 273 struct batadv_neigh_node **best_neigh_node)
275 { 274 {
276 struct hlist_node *node, *node_tmp; 275 struct hlist_node *node, *node_tmp;
277 struct batadv_neigh_node *neigh_node; 276 struct batadv_neigh_node *neigh_node;
278 bool neigh_purged = false; 277 bool neigh_purged = false;
279 unsigned long last_seen; 278 unsigned long last_seen;
280 struct batadv_hard_iface *if_incoming; 279 struct batadv_hard_iface *if_incoming;
281 280
282 *best_neigh_node = NULL; 281 *best_neigh_node = NULL;
283 282
284 spin_lock_bh(&orig_node->neigh_list_lock); 283 spin_lock_bh(&orig_node->neigh_list_lock);
285 284
286 /* for all neighbors towards this originator ... */ 285 /* for all neighbors towards this originator ... */
287 hlist_for_each_entry_safe(neigh_node, node, node_tmp, 286 hlist_for_each_entry_safe(neigh_node, node, node_tmp,
288 &orig_node->neigh_list, list) { 287 &orig_node->neigh_list, list) {
289 288
290 last_seen = neigh_node->last_seen; 289 last_seen = neigh_node->last_seen;
291 if_incoming = neigh_node->if_incoming; 290 if_incoming = neigh_node->if_incoming;
292 291
293 if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) || 292 if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) ||
294 (if_incoming->if_status == BATADV_IF_INACTIVE) || 293 (if_incoming->if_status == BATADV_IF_INACTIVE) ||
295 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) || 294 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
296 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) { 295 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) {
297 296
298 if ((if_incoming->if_status == BATADV_IF_INACTIVE) || 297 if ((if_incoming->if_status == BATADV_IF_INACTIVE) ||
299 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) || 298 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
300 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) 299 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED))
301 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 300 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
302 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n", 301 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
303 orig_node->orig, neigh_node->addr, 302 orig_node->orig, neigh_node->addr,
304 if_incoming->net_dev->name); 303 if_incoming->net_dev->name);
305 else 304 else
306 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 305 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
307 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n", 306 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
308 orig_node->orig, neigh_node->addr, 307 orig_node->orig, neigh_node->addr,
309 jiffies_to_msecs(last_seen)); 308 jiffies_to_msecs(last_seen));
310 309
311 neigh_purged = true; 310 neigh_purged = true;
312 311
313 hlist_del_rcu(&neigh_node->list); 312 hlist_del_rcu(&neigh_node->list);
314 batadv_bonding_candidate_del(orig_node, neigh_node); 313 batadv_bonding_candidate_del(orig_node, neigh_node);
315 batadv_neigh_node_free_ref(neigh_node); 314 batadv_neigh_node_free_ref(neigh_node);
316 } else { 315 } else {
317 if ((!*best_neigh_node) || 316 if ((!*best_neigh_node) ||
318 (neigh_node->tq_avg > (*best_neigh_node)->tq_avg)) 317 (neigh_node->tq_avg > (*best_neigh_node)->tq_avg))
319 *best_neigh_node = neigh_node; 318 *best_neigh_node = neigh_node;
320 } 319 }
321 } 320 }
322 321
323 spin_unlock_bh(&orig_node->neigh_list_lock); 322 spin_unlock_bh(&orig_node->neigh_list_lock);
324 return neigh_purged; 323 return neigh_purged;
325 } 324 }
326 325
327 static bool batadv_purge_orig_node(struct batadv_priv *bat_priv, 326 static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
328 struct batadv_orig_node *orig_node) 327 struct batadv_orig_node *orig_node)
329 { 328 {
330 struct batadv_neigh_node *best_neigh_node; 329 struct batadv_neigh_node *best_neigh_node;
331 330
332 if (batadv_has_timed_out(orig_node->last_seen, 331 if (batadv_has_timed_out(orig_node->last_seen,
333 2 * BATADV_PURGE_TIMEOUT)) { 332 2 * BATADV_PURGE_TIMEOUT)) {
334 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 333 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
335 "Originator timeout: originator %pM, last_seen %u\n", 334 "Originator timeout: originator %pM, last_seen %u\n",
336 orig_node->orig, 335 orig_node->orig,
337 jiffies_to_msecs(orig_node->last_seen)); 336 jiffies_to_msecs(orig_node->last_seen));
338 return true; 337 return true;
339 } else { 338 } else {
340 if (batadv_purge_orig_neighbors(bat_priv, orig_node, 339 if (batadv_purge_orig_neighbors(bat_priv, orig_node,
341 &best_neigh_node)) 340 &best_neigh_node))
342 batadv_update_route(bat_priv, orig_node, 341 batadv_update_route(bat_priv, orig_node,
343 best_neigh_node); 342 best_neigh_node);
344 } 343 }
345 344
346 return false; 345 return false;
347 } 346 }
348 347
349 static void _batadv_purge_orig(struct batadv_priv *bat_priv) 348 static void _batadv_purge_orig(struct batadv_priv *bat_priv)
350 { 349 {
351 struct batadv_hashtable *hash = bat_priv->orig_hash; 350 struct batadv_hashtable *hash = bat_priv->orig_hash;
352 struct hlist_node *node, *node_tmp; 351 struct hlist_node *node, *node_tmp;
353 struct hlist_head *head; 352 struct hlist_head *head;
354 spinlock_t *list_lock; /* spinlock to protect write access */ 353 spinlock_t *list_lock; /* spinlock to protect write access */
355 struct batadv_orig_node *orig_node; 354 struct batadv_orig_node *orig_node;
356 uint32_t i; 355 uint32_t i;
357 356
358 if (!hash) 357 if (!hash)
359 return; 358 return;
360 359
361 /* for all origins... */ 360 /* for all origins... */
362 for (i = 0; i < hash->size; i++) { 361 for (i = 0; i < hash->size; i++) {
363 head = &hash->table[i]; 362 head = &hash->table[i];
364 list_lock = &hash->list_locks[i]; 363 list_lock = &hash->list_locks[i];
365 364
366 spin_lock_bh(list_lock); 365 spin_lock_bh(list_lock);
367 hlist_for_each_entry_safe(orig_node, node, node_tmp, 366 hlist_for_each_entry_safe(orig_node, node, node_tmp,
368 head, hash_entry) { 367 head, hash_entry) {
369 if (batadv_purge_orig_node(bat_priv, orig_node)) { 368 if (batadv_purge_orig_node(bat_priv, orig_node)) {
370 if (orig_node->gw_flags) 369 if (orig_node->gw_flags)
371 batadv_gw_node_delete(bat_priv, 370 batadv_gw_node_delete(bat_priv,
372 orig_node); 371 orig_node);
373 hlist_del_rcu(node); 372 hlist_del_rcu(node);
374 batadv_orig_node_free_ref(orig_node); 373 batadv_orig_node_free_ref(orig_node);
375 continue; 374 continue;
376 } 375 }
377 376
378 if (batadv_has_timed_out(orig_node->last_frag_packet, 377 if (batadv_has_timed_out(orig_node->last_frag_packet,
379 BATADV_FRAG_TIMEOUT)) 378 BATADV_FRAG_TIMEOUT))
380 batadv_frag_list_free(&orig_node->frag_list); 379 batadv_frag_list_free(&orig_node->frag_list);
381 } 380 }
382 spin_unlock_bh(list_lock); 381 spin_unlock_bh(list_lock);
383 } 382 }
384 383
385 batadv_gw_node_purge(bat_priv); 384 batadv_gw_node_purge(bat_priv);
386 batadv_gw_election(bat_priv); 385 batadv_gw_election(bat_priv);
387 } 386 }
388 387
389 static void batadv_purge_orig(struct work_struct *work) 388 static void batadv_purge_orig(struct work_struct *work)
390 { 389 {
391 struct delayed_work *delayed_work; 390 struct delayed_work *delayed_work;
392 struct batadv_priv *bat_priv; 391 struct batadv_priv *bat_priv;
393 392
394 delayed_work = container_of(work, struct delayed_work, work); 393 delayed_work = container_of(work, struct delayed_work, work);
395 bat_priv = container_of(delayed_work, struct batadv_priv, orig_work); 394 bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
396 _batadv_purge_orig(bat_priv); 395 _batadv_purge_orig(bat_priv);
397 batadv_start_purge_timer(bat_priv); 396 batadv_start_purge_timer(bat_priv);
398 } 397 }
399 398
400 void batadv_purge_orig_ref(struct batadv_priv *bat_priv) 399 void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
401 { 400 {
402 _batadv_purge_orig(bat_priv); 401 _batadv_purge_orig(bat_priv);
403 } 402 }
404 403
405 int batadv_orig_seq_print_text(struct seq_file *seq, void *offset) 404 int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
406 { 405 {
407 struct net_device *net_dev = (struct net_device *)seq->private; 406 struct net_device *net_dev = (struct net_device *)seq->private;
408 struct batadv_priv *bat_priv = netdev_priv(net_dev); 407 struct batadv_priv *bat_priv = netdev_priv(net_dev);
409 struct batadv_hashtable *hash = bat_priv->orig_hash; 408 struct batadv_hashtable *hash = bat_priv->orig_hash;
410 struct hlist_node *node, *node_tmp; 409 struct hlist_node *node, *node_tmp;
411 struct hlist_head *head; 410 struct hlist_head *head;
412 struct batadv_hard_iface *primary_if; 411 struct batadv_hard_iface *primary_if;
413 struct batadv_orig_node *orig_node; 412 struct batadv_orig_node *orig_node;
414 struct batadv_neigh_node *neigh_node, *neigh_node_tmp; 413 struct batadv_neigh_node *neigh_node, *neigh_node_tmp;
415 int batman_count = 0; 414 int batman_count = 0;
416 int last_seen_secs; 415 int last_seen_secs;
417 int last_seen_msecs; 416 int last_seen_msecs;
418 unsigned long last_seen_jiffies; 417 unsigned long last_seen_jiffies;
419 uint32_t i; 418 uint32_t i;
420 419
421 primary_if = batadv_seq_print_text_primary_if_get(seq); 420 primary_if = batadv_seq_print_text_primary_if_get(seq);
422 if (!primary_if) 421 if (!primary_if)
423 goto out; 422 goto out;
424 423
425 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s)]\n", 424 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s)]\n",
426 BATADV_SOURCE_VERSION, primary_if->net_dev->name, 425 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
427 primary_if->net_dev->dev_addr, net_dev->name); 426 primary_if->net_dev->dev_addr, net_dev->name);
428 seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n", 427 seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
429 "Originator", "last-seen", "#", BATADV_TQ_MAX_VALUE, 428 "Originator", "last-seen", "#", BATADV_TQ_MAX_VALUE,
430 "Nexthop", "outgoingIF", "Potential nexthops"); 429 "Nexthop", "outgoingIF", "Potential nexthops");
431 430
432 for (i = 0; i < hash->size; i++) { 431 for (i = 0; i < hash->size; i++) {
433 head = &hash->table[i]; 432 head = &hash->table[i];
434 433
435 rcu_read_lock(); 434 rcu_read_lock();
436 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) { 435 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
437 neigh_node = batadv_orig_node_get_router(orig_node); 436 neigh_node = batadv_orig_node_get_router(orig_node);
438 if (!neigh_node) 437 if (!neigh_node)
439 continue; 438 continue;
440 439
441 if (neigh_node->tq_avg == 0) 440 if (neigh_node->tq_avg == 0)
442 goto next; 441 goto next;
443 442
444 last_seen_jiffies = jiffies - orig_node->last_seen; 443 last_seen_jiffies = jiffies - orig_node->last_seen;
445 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies); 444 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
446 last_seen_secs = last_seen_msecs / 1000; 445 last_seen_secs = last_seen_msecs / 1000;
447 last_seen_msecs = last_seen_msecs % 1000; 446 last_seen_msecs = last_seen_msecs % 1000;
448 447
449 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:", 448 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
450 orig_node->orig, last_seen_secs, 449 orig_node->orig, last_seen_secs,
451 last_seen_msecs, neigh_node->tq_avg, 450 last_seen_msecs, neigh_node->tq_avg,
452 neigh_node->addr, 451 neigh_node->addr,
453 neigh_node->if_incoming->net_dev->name); 452 neigh_node->if_incoming->net_dev->name);
454 453
455 hlist_for_each_entry_rcu(neigh_node_tmp, node_tmp, 454 hlist_for_each_entry_rcu(neigh_node_tmp, node_tmp,
456 &orig_node->neigh_list, list) { 455 &orig_node->neigh_list, list) {
457 seq_printf(seq, " %pM (%3i)", 456 seq_printf(seq, " %pM (%3i)",
458 neigh_node_tmp->addr, 457 neigh_node_tmp->addr,
459 neigh_node_tmp->tq_avg); 458 neigh_node_tmp->tq_avg);
460 } 459 }
461 460
462 seq_printf(seq, "\n"); 461 seq_printf(seq, "\n");
463 batman_count++; 462 batman_count++;
464 463
465 next: 464 next:
466 batadv_neigh_node_free_ref(neigh_node); 465 batadv_neigh_node_free_ref(neigh_node);
467 } 466 }
468 rcu_read_unlock(); 467 rcu_read_unlock();
469 } 468 }
470 469
471 if (batman_count == 0) 470 if (batman_count == 0)
472 seq_printf(seq, "No batman nodes in range ...\n"); 471 seq_printf(seq, "No batman nodes in range ...\n");
473 472
474 out: 473 out:
475 if (primary_if) 474 if (primary_if)
476 batadv_hardif_free_ref(primary_if); 475 batadv_hardif_free_ref(primary_if);
477 return 0; 476 return 0;
478 } 477 }
479 478
480 static int batadv_orig_node_add_if(struct batadv_orig_node *orig_node, 479 static int batadv_orig_node_add_if(struct batadv_orig_node *orig_node,
481 int max_if_num) 480 int max_if_num)
482 { 481 {
483 void *data_ptr; 482 void *data_ptr;
484 size_t data_size, old_size; 483 size_t data_size, old_size;
485 484
486 data_size = max_if_num * sizeof(unsigned long) * BATADV_NUM_WORDS; 485 data_size = max_if_num * sizeof(unsigned long) * BATADV_NUM_WORDS;
487 old_size = (max_if_num - 1) * sizeof(unsigned long) * BATADV_NUM_WORDS; 486 old_size = (max_if_num - 1) * sizeof(unsigned long) * BATADV_NUM_WORDS;
488 data_ptr = kmalloc(data_size, GFP_ATOMIC); 487 data_ptr = kmalloc(data_size, GFP_ATOMIC);
489 if (!data_ptr) 488 if (!data_ptr)
490 return -ENOMEM; 489 return -ENOMEM;
491 490
492 memcpy(data_ptr, orig_node->bcast_own, old_size); 491 memcpy(data_ptr, orig_node->bcast_own, old_size);
493 kfree(orig_node->bcast_own); 492 kfree(orig_node->bcast_own);
494 orig_node->bcast_own = data_ptr; 493 orig_node->bcast_own = data_ptr;
495 494
496 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC); 495 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
497 if (!data_ptr) 496 if (!data_ptr)
498 return -ENOMEM; 497 return -ENOMEM;
499 498
500 memcpy(data_ptr, orig_node->bcast_own_sum, 499 memcpy(data_ptr, orig_node->bcast_own_sum,
501 (max_if_num - 1) * sizeof(uint8_t)); 500 (max_if_num - 1) * sizeof(uint8_t));
502 kfree(orig_node->bcast_own_sum); 501 kfree(orig_node->bcast_own_sum);
503 orig_node->bcast_own_sum = data_ptr; 502 orig_node->bcast_own_sum = data_ptr;
504 503
505 return 0; 504 return 0;
506 } 505 }
507 506
508 int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface, 507 int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface,
509 int max_if_num) 508 int max_if_num)
510 { 509 {
511 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); 510 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
512 struct batadv_hashtable *hash = bat_priv->orig_hash; 511 struct batadv_hashtable *hash = bat_priv->orig_hash;
513 struct hlist_node *node; 512 struct hlist_node *node;
514 struct hlist_head *head; 513 struct hlist_head *head;
515 struct batadv_orig_node *orig_node; 514 struct batadv_orig_node *orig_node;
516 uint32_t i; 515 uint32_t i;
517 int ret; 516 int ret;
518 517
519 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on 518 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
520 * if_num 519 * if_num
521 */ 520 */
522 for (i = 0; i < hash->size; i++) { 521 for (i = 0; i < hash->size; i++) {
523 head = &hash->table[i]; 522 head = &hash->table[i];
524 523
525 rcu_read_lock(); 524 rcu_read_lock();
526 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) { 525 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
527 spin_lock_bh(&orig_node->ogm_cnt_lock); 526 spin_lock_bh(&orig_node->ogm_cnt_lock);
528 ret = batadv_orig_node_add_if(orig_node, max_if_num); 527 ret = batadv_orig_node_add_if(orig_node, max_if_num);
529 spin_unlock_bh(&orig_node->ogm_cnt_lock); 528 spin_unlock_bh(&orig_node->ogm_cnt_lock);
530 529
531 if (ret == -ENOMEM) 530 if (ret == -ENOMEM)
532 goto err; 531 goto err;
533 } 532 }
534 rcu_read_unlock(); 533 rcu_read_unlock();
535 } 534 }
536 535
537 return 0; 536 return 0;
538 537
539 err: 538 err:
540 rcu_read_unlock(); 539 rcu_read_unlock();
541 return -ENOMEM; 540 return -ENOMEM;
542 } 541 }
543 542
544 static int batadv_orig_node_del_if(struct batadv_orig_node *orig_node, 543 static int batadv_orig_node_del_if(struct batadv_orig_node *orig_node,
545 int max_if_num, int del_if_num) 544 int max_if_num, int del_if_num)
546 { 545 {
547 void *data_ptr = NULL; 546 void *data_ptr = NULL;
548 int chunk_size; 547 int chunk_size;
549 548
550 /* last interface was removed */ 549 /* last interface was removed */
551 if (max_if_num == 0) 550 if (max_if_num == 0)
552 goto free_bcast_own; 551 goto free_bcast_own;
553 552
554 chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS; 553 chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS;
555 data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC); 554 data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC);
556 if (!data_ptr) 555 if (!data_ptr)
557 return -ENOMEM; 556 return -ENOMEM;
558 557
559 /* copy first part */ 558 /* copy first part */
560 memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size); 559 memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size);
561 560
562 /* copy second part */ 561 /* copy second part */
563 memcpy((char *)data_ptr + del_if_num * chunk_size, 562 memcpy((char *)data_ptr + del_if_num * chunk_size,
564 orig_node->bcast_own + ((del_if_num + 1) * chunk_size), 563 orig_node->bcast_own + ((del_if_num + 1) * chunk_size),
565 (max_if_num - del_if_num) * chunk_size); 564 (max_if_num - del_if_num) * chunk_size);
566 565
567 free_bcast_own: 566 free_bcast_own:
568 kfree(orig_node->bcast_own); 567 kfree(orig_node->bcast_own);
569 orig_node->bcast_own = data_ptr; 568 orig_node->bcast_own = data_ptr;
570 569
571 if (max_if_num == 0) 570 if (max_if_num == 0)
572 goto free_own_sum; 571 goto free_own_sum;
573 572
574 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC); 573 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
575 if (!data_ptr) 574 if (!data_ptr)
576 return -ENOMEM; 575 return -ENOMEM;
577 576
578 memcpy(data_ptr, orig_node->bcast_own_sum, 577 memcpy(data_ptr, orig_node->bcast_own_sum,
579 del_if_num * sizeof(uint8_t)); 578 del_if_num * sizeof(uint8_t));
580 579
581 memcpy((char *)data_ptr + del_if_num * sizeof(uint8_t), 580 memcpy((char *)data_ptr + del_if_num * sizeof(uint8_t),
582 orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)), 581 orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)),
583 (max_if_num - del_if_num) * sizeof(uint8_t)); 582 (max_if_num - del_if_num) * sizeof(uint8_t));
584 583
585 free_own_sum: 584 free_own_sum:
586 kfree(orig_node->bcast_own_sum); 585 kfree(orig_node->bcast_own_sum);
587 orig_node->bcast_own_sum = data_ptr; 586 orig_node->bcast_own_sum = data_ptr;
588 587
589 return 0; 588 return 0;
590 } 589 }
591 590
592 int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface, 591 int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
593 int max_if_num) 592 int max_if_num)
594 { 593 {
595 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); 594 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
596 struct batadv_hashtable *hash = bat_priv->orig_hash; 595 struct batadv_hashtable *hash = bat_priv->orig_hash;
597 struct hlist_node *node; 596 struct hlist_node *node;
598 struct hlist_head *head; 597 struct hlist_head *head;
599 struct batadv_hard_iface *hard_iface_tmp; 598 struct batadv_hard_iface *hard_iface_tmp;
600 struct batadv_orig_node *orig_node; 599 struct batadv_orig_node *orig_node;
601 uint32_t i; 600 uint32_t i;
602 int ret; 601 int ret;
603 602
604 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on 603 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
605 * if_num 604 * if_num
606 */ 605 */
607 for (i = 0; i < hash->size; i++) { 606 for (i = 0; i < hash->size; i++) {
608 head = &hash->table[i]; 607 head = &hash->table[i];
609 608
610 rcu_read_lock(); 609 rcu_read_lock();
611 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) { 610 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
612 spin_lock_bh(&orig_node->ogm_cnt_lock); 611 spin_lock_bh(&orig_node->ogm_cnt_lock);
613 ret = batadv_orig_node_del_if(orig_node, max_if_num, 612 ret = batadv_orig_node_del_if(orig_node, max_if_num,
614 hard_iface->if_num); 613 hard_iface->if_num);
615 spin_unlock_bh(&orig_node->ogm_cnt_lock); 614 spin_unlock_bh(&orig_node->ogm_cnt_lock);
616 615
617 if (ret == -ENOMEM) 616 if (ret == -ENOMEM)
618 goto err; 617 goto err;
619 } 618 }
620 rcu_read_unlock(); 619 rcu_read_unlock();
621 } 620 }
622 621
623 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */ 622 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
624 rcu_read_lock(); 623 rcu_read_lock();
625 list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) { 624 list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) {
626 if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE) 625 if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE)
627 continue; 626 continue;
628 627
629 if (hard_iface == hard_iface_tmp) 628 if (hard_iface == hard_iface_tmp)
630 continue; 629 continue;
631 630
632 if (hard_iface->soft_iface != hard_iface_tmp->soft_iface) 631 if (hard_iface->soft_iface != hard_iface_tmp->soft_iface)
633 continue; 632 continue;
634 633
635 if (hard_iface_tmp->if_num > hard_iface->if_num) 634 if (hard_iface_tmp->if_num > hard_iface->if_num)
636 hard_iface_tmp->if_num--; 635 hard_iface_tmp->if_num--;
637 } 636 }
638 rcu_read_unlock(); 637 rcu_read_unlock();
639 638
640 hard_iface->if_num = -1; 639 hard_iface->if_num = -1;
641 return 0; 640 return 0;
642 641
643 err: 642 err:
644 rcu_read_unlock(); 643 rcu_read_unlock();
645 return -ENOMEM; 644 return -ENOMEM;
646 } 645 }
647 646
net/batman-adv/routing.c
1 /* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors: 1 /* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
2 * 2 *
3 * Marek Lindner, Simon Wunderlich 3 * Marek Lindner, Simon Wunderlich
4 * 4 *
5 * This program is free software; you can redistribute it and/or 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 6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation. 7 * License as published by the Free Software Foundation.
8 * 8 *
9 * This program is distributed in the hope that it will be useful, but 9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of 10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details. 12 * General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 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 15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA 17 * 02110-1301, USA
18 */ 18 */
19 19
20 #include "main.h" 20 #include "main.h"
21 #include "routing.h" 21 #include "routing.h"
22 #include "send.h" 22 #include "send.h"
23 #include "soft-interface.h" 23 #include "soft-interface.h"
24 #include "hard-interface.h" 24 #include "hard-interface.h"
25 #include "icmp_socket.h" 25 #include "icmp_socket.h"
26 #include "translation-table.h" 26 #include "translation-table.h"
27 #include "originator.h" 27 #include "originator.h"
28 #include "vis.h" 28 #include "vis.h"
29 #include "unicast.h" 29 #include "unicast.h"
30 #include "bridge_loop_avoidance.h" 30 #include "bridge_loop_avoidance.h"
31 #include "distributed-arp-table.h" 31 #include "distributed-arp-table.h"
32 32
33 static int batadv_route_unicast_packet(struct sk_buff *skb, 33 static int batadv_route_unicast_packet(struct sk_buff *skb,
34 struct batadv_hard_iface *recv_if); 34 struct batadv_hard_iface *recv_if);
35 35
36 void batadv_slide_own_bcast_window(struct batadv_hard_iface *hard_iface) 36 void batadv_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
37 { 37 {
38 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); 38 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
39 struct batadv_hashtable *hash = bat_priv->orig_hash; 39 struct batadv_hashtable *hash = bat_priv->orig_hash;
40 struct hlist_node *node; 40 struct hlist_node *node;
41 struct hlist_head *head; 41 struct hlist_head *head;
42 struct batadv_orig_node *orig_node; 42 struct batadv_orig_node *orig_node;
43 unsigned long *word; 43 unsigned long *word;
44 uint32_t i; 44 uint32_t i;
45 size_t word_index; 45 size_t word_index;
46 uint8_t *w; 46 uint8_t *w;
47 47
48 for (i = 0; i < hash->size; i++) { 48 for (i = 0; i < hash->size; i++) {
49 head = &hash->table[i]; 49 head = &hash->table[i];
50 50
51 rcu_read_lock(); 51 rcu_read_lock();
52 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) { 52 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
53 spin_lock_bh(&orig_node->ogm_cnt_lock); 53 spin_lock_bh(&orig_node->ogm_cnt_lock);
54 word_index = hard_iface->if_num * BATADV_NUM_WORDS; 54 word_index = hard_iface->if_num * BATADV_NUM_WORDS;
55 word = &(orig_node->bcast_own[word_index]); 55 word = &(orig_node->bcast_own[word_index]);
56 56
57 batadv_bit_get_packet(bat_priv, word, 1, 0); 57 batadv_bit_get_packet(bat_priv, word, 1, 0);
58 w = &orig_node->bcast_own_sum[hard_iface->if_num]; 58 w = &orig_node->bcast_own_sum[hard_iface->if_num];
59 *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE); 59 *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
60 spin_unlock_bh(&orig_node->ogm_cnt_lock); 60 spin_unlock_bh(&orig_node->ogm_cnt_lock);
61 } 61 }
62 rcu_read_unlock(); 62 rcu_read_unlock();
63 } 63 }
64 } 64 }
65 65
66 static void _batadv_update_route(struct batadv_priv *bat_priv, 66 static void _batadv_update_route(struct batadv_priv *bat_priv,
67 struct batadv_orig_node *orig_node, 67 struct batadv_orig_node *orig_node,
68 struct batadv_neigh_node *neigh_node) 68 struct batadv_neigh_node *neigh_node)
69 { 69 {
70 struct batadv_neigh_node *curr_router; 70 struct batadv_neigh_node *curr_router;
71 71
72 curr_router = batadv_orig_node_get_router(orig_node); 72 curr_router = batadv_orig_node_get_router(orig_node);
73 73
74 /* route deleted */ 74 /* route deleted */
75 if ((curr_router) && (!neigh_node)) { 75 if ((curr_router) && (!neigh_node)) {
76 batadv_dbg(BATADV_DBG_ROUTES, bat_priv, 76 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
77 "Deleting route towards: %pM\n", orig_node->orig); 77 "Deleting route towards: %pM\n", orig_node->orig);
78 batadv_tt_global_del_orig(bat_priv, orig_node, 78 batadv_tt_global_del_orig(bat_priv, orig_node,
79 "Deleted route towards originator"); 79 "Deleted route towards originator");
80 80
81 /* route added */ 81 /* route added */
82 } else if ((!curr_router) && (neigh_node)) { 82 } else if ((!curr_router) && (neigh_node)) {
83 83
84 batadv_dbg(BATADV_DBG_ROUTES, bat_priv, 84 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
85 "Adding route towards: %pM (via %pM)\n", 85 "Adding route towards: %pM (via %pM)\n",
86 orig_node->orig, neigh_node->addr); 86 orig_node->orig, neigh_node->addr);
87 /* route changed */ 87 /* route changed */
88 } else if (neigh_node && curr_router) { 88 } else if (neigh_node && curr_router) {
89 batadv_dbg(BATADV_DBG_ROUTES, bat_priv, 89 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
90 "Changing route towards: %pM (now via %pM - was via %pM)\n", 90 "Changing route towards: %pM (now via %pM - was via %pM)\n",
91 orig_node->orig, neigh_node->addr, 91 orig_node->orig, neigh_node->addr,
92 curr_router->addr); 92 curr_router->addr);
93 } 93 }
94 94
95 if (curr_router) 95 if (curr_router)
96 batadv_neigh_node_free_ref(curr_router); 96 batadv_neigh_node_free_ref(curr_router);
97 97
98 /* increase refcount of new best neighbor */ 98 /* increase refcount of new best neighbor */
99 if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount)) 99 if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount))
100 neigh_node = NULL; 100 neigh_node = NULL;
101 101
102 spin_lock_bh(&orig_node->neigh_list_lock); 102 spin_lock_bh(&orig_node->neigh_list_lock);
103 rcu_assign_pointer(orig_node->router, neigh_node); 103 rcu_assign_pointer(orig_node->router, neigh_node);
104 spin_unlock_bh(&orig_node->neigh_list_lock); 104 spin_unlock_bh(&orig_node->neigh_list_lock);
105 105
106 /* decrease refcount of previous best neighbor */ 106 /* decrease refcount of previous best neighbor */
107 if (curr_router) 107 if (curr_router)
108 batadv_neigh_node_free_ref(curr_router); 108 batadv_neigh_node_free_ref(curr_router);
109 } 109 }
110 110
111 void batadv_update_route(struct batadv_priv *bat_priv, 111 void batadv_update_route(struct batadv_priv *bat_priv,
112 struct batadv_orig_node *orig_node, 112 struct batadv_orig_node *orig_node,
113 struct batadv_neigh_node *neigh_node) 113 struct batadv_neigh_node *neigh_node)
114 { 114 {
115 struct batadv_neigh_node *router = NULL; 115 struct batadv_neigh_node *router = NULL;
116 116
117 if (!orig_node) 117 if (!orig_node)
118 goto out; 118 goto out;
119 119
120 router = batadv_orig_node_get_router(orig_node); 120 router = batadv_orig_node_get_router(orig_node);
121 121
122 if (router != neigh_node) 122 if (router != neigh_node)
123 _batadv_update_route(bat_priv, orig_node, neigh_node); 123 _batadv_update_route(bat_priv, orig_node, neigh_node);
124 124
125 out: 125 out:
126 if (router) 126 if (router)
127 batadv_neigh_node_free_ref(router); 127 batadv_neigh_node_free_ref(router);
128 } 128 }
129 129
130 /* caller must hold the neigh_list_lock */ 130 /* caller must hold the neigh_list_lock */
131 void batadv_bonding_candidate_del(struct batadv_orig_node *orig_node, 131 void batadv_bonding_candidate_del(struct batadv_orig_node *orig_node,
132 struct batadv_neigh_node *neigh_node) 132 struct batadv_neigh_node *neigh_node)
133 { 133 {
134 /* this neighbor is not part of our candidate list */ 134 /* this neighbor is not part of our candidate list */
135 if (list_empty(&neigh_node->bonding_list)) 135 if (list_empty(&neigh_node->bonding_list))
136 goto out; 136 goto out;
137 137
138 list_del_rcu(&neigh_node->bonding_list); 138 list_del_rcu(&neigh_node->bonding_list);
139 INIT_LIST_HEAD(&neigh_node->bonding_list); 139 INIT_LIST_HEAD(&neigh_node->bonding_list);
140 batadv_neigh_node_free_ref(neigh_node); 140 batadv_neigh_node_free_ref(neigh_node);
141 atomic_dec(&orig_node->bond_candidates); 141 atomic_dec(&orig_node->bond_candidates);
142 142
143 out: 143 out:
144 return; 144 return;
145 } 145 }
146 146
147 void batadv_bonding_candidate_add(struct batadv_orig_node *orig_node, 147 void batadv_bonding_candidate_add(struct batadv_orig_node *orig_node,
148 struct batadv_neigh_node *neigh_node) 148 struct batadv_neigh_node *neigh_node)
149 { 149 {
150 struct hlist_node *node; 150 struct hlist_node *node;
151 struct batadv_neigh_node *tmp_neigh_node, *router = NULL; 151 struct batadv_neigh_node *tmp_neigh_node, *router = NULL;
152 uint8_t interference_candidate = 0; 152 uint8_t interference_candidate = 0;
153 153
154 spin_lock_bh(&orig_node->neigh_list_lock); 154 spin_lock_bh(&orig_node->neigh_list_lock);
155 155
156 /* only consider if it has the same primary address ... */ 156 /* only consider if it has the same primary address ... */
157 if (!batadv_compare_eth(orig_node->orig, 157 if (!batadv_compare_eth(orig_node->orig,
158 neigh_node->orig_node->primary_addr)) 158 neigh_node->orig_node->primary_addr))
159 goto candidate_del; 159 goto candidate_del;
160 160
161 router = batadv_orig_node_get_router(orig_node); 161 router = batadv_orig_node_get_router(orig_node);
162 if (!router) 162 if (!router)
163 goto candidate_del; 163 goto candidate_del;
164 164
165 /* ... and is good enough to be considered */ 165 /* ... and is good enough to be considered */
166 if (neigh_node->tq_avg < router->tq_avg - BATADV_BONDING_TQ_THRESHOLD) 166 if (neigh_node->tq_avg < router->tq_avg - BATADV_BONDING_TQ_THRESHOLD)
167 goto candidate_del; 167 goto candidate_del;
168 168
169 /* check if we have another candidate with the same mac address or 169 /* check if we have another candidate with the same mac address or
170 * interface. If we do, we won't select this candidate because of 170 * interface. If we do, we won't select this candidate because of
171 * possible interference. 171 * possible interference.
172 */ 172 */
173 hlist_for_each_entry_rcu(tmp_neigh_node, node, 173 hlist_for_each_entry_rcu(tmp_neigh_node, node,
174 &orig_node->neigh_list, list) { 174 &orig_node->neigh_list, list) {
175 175
176 if (tmp_neigh_node == neigh_node) 176 if (tmp_neigh_node == neigh_node)
177 continue; 177 continue;
178 178
179 /* we only care if the other candidate is even 179 /* we only care if the other candidate is even
180 * considered as candidate. 180 * considered as candidate.
181 */ 181 */
182 if (list_empty(&tmp_neigh_node->bonding_list)) 182 if (list_empty(&tmp_neigh_node->bonding_list))
183 continue; 183 continue;
184 184
185 if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) || 185 if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
186 (batadv_compare_eth(neigh_node->addr, 186 (batadv_compare_eth(neigh_node->addr,
187 tmp_neigh_node->addr))) { 187 tmp_neigh_node->addr))) {
188 interference_candidate = 1; 188 interference_candidate = 1;
189 break; 189 break;
190 } 190 }
191 } 191 }
192 192
193 /* don't care further if it is an interference candidate */ 193 /* don't care further if it is an interference candidate */
194 if (interference_candidate) 194 if (interference_candidate)
195 goto candidate_del; 195 goto candidate_del;
196 196
197 /* this neighbor already is part of our candidate list */ 197 /* this neighbor already is part of our candidate list */
198 if (!list_empty(&neigh_node->bonding_list)) 198 if (!list_empty(&neigh_node->bonding_list))
199 goto out; 199 goto out;
200 200
201 if (!atomic_inc_not_zero(&neigh_node->refcount)) 201 if (!atomic_inc_not_zero(&neigh_node->refcount))
202 goto out; 202 goto out;
203 203
204 list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list); 204 list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
205 atomic_inc(&orig_node->bond_candidates); 205 atomic_inc(&orig_node->bond_candidates);
206 goto out; 206 goto out;
207 207
208 candidate_del: 208 candidate_del:
209 batadv_bonding_candidate_del(orig_node, neigh_node); 209 batadv_bonding_candidate_del(orig_node, neigh_node);
210 210
211 out: 211 out:
212 spin_unlock_bh(&orig_node->neigh_list_lock); 212 spin_unlock_bh(&orig_node->neigh_list_lock);
213 213
214 if (router) 214 if (router)
215 batadv_neigh_node_free_ref(router); 215 batadv_neigh_node_free_ref(router);
216 } 216 }
217 217
218 /* copy primary address for bonding */ 218 /* copy primary address for bonding */
219 void 219 void
220 batadv_bonding_save_primary(const struct batadv_orig_node *orig_node, 220 batadv_bonding_save_primary(const struct batadv_orig_node *orig_node,
221 struct batadv_orig_node *orig_neigh_node, 221 struct batadv_orig_node *orig_neigh_node,
222 const struct batadv_ogm_packet *batman_ogm_packet) 222 const struct batadv_ogm_packet *batman_ogm_packet)
223 { 223 {
224 if (!(batman_ogm_packet->flags & BATADV_PRIMARIES_FIRST_HOP)) 224 if (!(batman_ogm_packet->flags & BATADV_PRIMARIES_FIRST_HOP))
225 return; 225 return;
226 226
227 memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN); 227 memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
228 } 228 }
229 229
230 /* checks whether the host restarted and is in the protection time. 230 /* checks whether the host restarted and is in the protection time.
231 * returns: 231 * returns:
232 * 0 if the packet is to be accepted 232 * 0 if the packet is to be accepted
233 * 1 if the packet is to be ignored. 233 * 1 if the packet is to be ignored.
234 */ 234 */
235 int batadv_window_protected(struct batadv_priv *bat_priv, int32_t seq_num_diff, 235 int batadv_window_protected(struct batadv_priv *bat_priv, int32_t seq_num_diff,
236 unsigned long *last_reset) 236 unsigned long *last_reset)
237 { 237 {
238 if (seq_num_diff <= -BATADV_TQ_LOCAL_WINDOW_SIZE || 238 if (seq_num_diff <= -BATADV_TQ_LOCAL_WINDOW_SIZE ||
239 seq_num_diff >= BATADV_EXPECTED_SEQNO_RANGE) { 239 seq_num_diff >= BATADV_EXPECTED_SEQNO_RANGE) {
240 if (!batadv_has_timed_out(*last_reset, 240 if (!batadv_has_timed_out(*last_reset,
241 BATADV_RESET_PROTECTION_MS)) 241 BATADV_RESET_PROTECTION_MS))
242 return 1; 242 return 1;
243 243
244 *last_reset = jiffies; 244 *last_reset = jiffies;
245 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 245 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
246 "old packet received, start protection\n"); 246 "old packet received, start protection\n");
247 } 247 }
248 248
249 return 0; 249 return 0;
250 } 250 }
251 251
252 bool batadv_check_management_packet(struct sk_buff *skb, 252 bool batadv_check_management_packet(struct sk_buff *skb,
253 struct batadv_hard_iface *hard_iface, 253 struct batadv_hard_iface *hard_iface,
254 int header_len) 254 int header_len)
255 { 255 {
256 struct ethhdr *ethhdr; 256 struct ethhdr *ethhdr;
257 257
258 /* drop packet if it has not necessary minimum size */ 258 /* drop packet if it has not necessary minimum size */
259 if (unlikely(!pskb_may_pull(skb, header_len))) 259 if (unlikely(!pskb_may_pull(skb, header_len)))
260 return false; 260 return false;
261 261
262 ethhdr = (struct ethhdr *)skb_mac_header(skb); 262 ethhdr = (struct ethhdr *)skb_mac_header(skb);
263 263
264 /* packet with broadcast indication but unicast recipient */ 264 /* packet with broadcast indication but unicast recipient */
265 if (!is_broadcast_ether_addr(ethhdr->h_dest)) 265 if (!is_broadcast_ether_addr(ethhdr->h_dest))
266 return false; 266 return false;
267 267
268 /* packet with broadcast sender address */ 268 /* packet with broadcast sender address */
269 if (is_broadcast_ether_addr(ethhdr->h_source)) 269 if (is_broadcast_ether_addr(ethhdr->h_source))
270 return false; 270 return false;
271 271
272 /* create a copy of the skb, if needed, to modify it. */ 272 /* create a copy of the skb, if needed, to modify it. */
273 if (skb_cow(skb, 0) < 0) 273 if (skb_cow(skb, 0) < 0)
274 return false; 274 return false;
275 275
276 /* keep skb linear */ 276 /* keep skb linear */
277 if (skb_linearize(skb) < 0) 277 if (skb_linearize(skb) < 0)
278 return false; 278 return false;
279 279
280 return true; 280 return true;
281 } 281 }
282 282
283 static int batadv_recv_my_icmp_packet(struct batadv_priv *bat_priv, 283 static int batadv_recv_my_icmp_packet(struct batadv_priv *bat_priv,
284 struct sk_buff *skb, size_t icmp_len) 284 struct sk_buff *skb, size_t icmp_len)
285 { 285 {
286 struct batadv_hard_iface *primary_if = NULL; 286 struct batadv_hard_iface *primary_if = NULL;
287 struct batadv_orig_node *orig_node = NULL; 287 struct batadv_orig_node *orig_node = NULL;
288 struct batadv_neigh_node *router = NULL; 288 struct batadv_neigh_node *router = NULL;
289 struct batadv_icmp_packet_rr *icmp_packet; 289 struct batadv_icmp_packet_rr *icmp_packet;
290 int ret = NET_RX_DROP; 290 int ret = NET_RX_DROP;
291 291
292 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data; 292 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
293 293
294 /* add data to device queue */ 294 /* add data to device queue */
295 if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) { 295 if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) {
296 batadv_socket_receive_packet(icmp_packet, icmp_len); 296 batadv_socket_receive_packet(icmp_packet, icmp_len);
297 goto out; 297 goto out;
298 } 298 }
299 299
300 primary_if = batadv_primary_if_get_selected(bat_priv); 300 primary_if = batadv_primary_if_get_selected(bat_priv);
301 if (!primary_if) 301 if (!primary_if)
302 goto out; 302 goto out;
303 303
304 /* answer echo request (ping) */ 304 /* answer echo request (ping) */
305 /* get routing information */ 305 /* get routing information */
306 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig); 306 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
307 if (!orig_node) 307 if (!orig_node)
308 goto out; 308 goto out;
309 309
310 router = batadv_orig_node_get_router(orig_node); 310 router = batadv_orig_node_get_router(orig_node);
311 if (!router) 311 if (!router)
312 goto out; 312 goto out;
313 313
314 /* create a copy of the skb, if needed, to modify it. */ 314 /* create a copy of the skb, if needed, to modify it. */
315 if (skb_cow(skb, ETH_HLEN) < 0) 315 if (skb_cow(skb, ETH_HLEN) < 0)
316 goto out; 316 goto out;
317 317
318 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data; 318 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
319 319
320 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN); 320 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
321 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN); 321 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
322 icmp_packet->msg_type = BATADV_ECHO_REPLY; 322 icmp_packet->msg_type = BATADV_ECHO_REPLY;
323 icmp_packet->header.ttl = BATADV_TTL; 323 icmp_packet->header.ttl = BATADV_TTL;
324 324
325 batadv_send_skb_packet(skb, router->if_incoming, router->addr); 325 batadv_send_skb_packet(skb, router->if_incoming, router->addr);
326 ret = NET_RX_SUCCESS; 326 ret = NET_RX_SUCCESS;
327 327
328 out: 328 out:
329 if (primary_if) 329 if (primary_if)
330 batadv_hardif_free_ref(primary_if); 330 batadv_hardif_free_ref(primary_if);
331 if (router) 331 if (router)
332 batadv_neigh_node_free_ref(router); 332 batadv_neigh_node_free_ref(router);
333 if (orig_node) 333 if (orig_node)
334 batadv_orig_node_free_ref(orig_node); 334 batadv_orig_node_free_ref(orig_node);
335 return ret; 335 return ret;
336 } 336 }
337 337
338 static int batadv_recv_icmp_ttl_exceeded(struct batadv_priv *bat_priv, 338 static int batadv_recv_icmp_ttl_exceeded(struct batadv_priv *bat_priv,
339 struct sk_buff *skb) 339 struct sk_buff *skb)
340 { 340 {
341 struct batadv_hard_iface *primary_if = NULL; 341 struct batadv_hard_iface *primary_if = NULL;
342 struct batadv_orig_node *orig_node = NULL; 342 struct batadv_orig_node *orig_node = NULL;
343 struct batadv_neigh_node *router = NULL; 343 struct batadv_neigh_node *router = NULL;
344 struct batadv_icmp_packet *icmp_packet; 344 struct batadv_icmp_packet *icmp_packet;
345 int ret = NET_RX_DROP; 345 int ret = NET_RX_DROP;
346 346
347 icmp_packet = (struct batadv_icmp_packet *)skb->data; 347 icmp_packet = (struct batadv_icmp_packet *)skb->data;
348 348
349 /* send TTL exceeded if packet is an echo request (traceroute) */ 349 /* send TTL exceeded if packet is an echo request (traceroute) */
350 if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) { 350 if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) {
351 pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n", 351 pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
352 icmp_packet->orig, icmp_packet->dst); 352 icmp_packet->orig, icmp_packet->dst);
353 goto out; 353 goto out;
354 } 354 }
355 355
356 primary_if = batadv_primary_if_get_selected(bat_priv); 356 primary_if = batadv_primary_if_get_selected(bat_priv);
357 if (!primary_if) 357 if (!primary_if)
358 goto out; 358 goto out;
359 359
360 /* get routing information */ 360 /* get routing information */
361 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig); 361 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
362 if (!orig_node) 362 if (!orig_node)
363 goto out; 363 goto out;
364 364
365 router = batadv_orig_node_get_router(orig_node); 365 router = batadv_orig_node_get_router(orig_node);
366 if (!router) 366 if (!router)
367 goto out; 367 goto out;
368 368
369 /* create a copy of the skb, if needed, to modify it. */ 369 /* create a copy of the skb, if needed, to modify it. */
370 if (skb_cow(skb, ETH_HLEN) < 0) 370 if (skb_cow(skb, ETH_HLEN) < 0)
371 goto out; 371 goto out;
372 372
373 icmp_packet = (struct batadv_icmp_packet *)skb->data; 373 icmp_packet = (struct batadv_icmp_packet *)skb->data;
374 374
375 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN); 375 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
376 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN); 376 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
377 icmp_packet->msg_type = BATADV_TTL_EXCEEDED; 377 icmp_packet->msg_type = BATADV_TTL_EXCEEDED;
378 icmp_packet->header.ttl = BATADV_TTL; 378 icmp_packet->header.ttl = BATADV_TTL;
379 379
380 batadv_send_skb_packet(skb, router->if_incoming, router->addr); 380 batadv_send_skb_packet(skb, router->if_incoming, router->addr);
381 ret = NET_RX_SUCCESS; 381 ret = NET_RX_SUCCESS;
382 382
383 out: 383 out:
384 if (primary_if) 384 if (primary_if)
385 batadv_hardif_free_ref(primary_if); 385 batadv_hardif_free_ref(primary_if);
386 if (router) 386 if (router)
387 batadv_neigh_node_free_ref(router); 387 batadv_neigh_node_free_ref(router);
388 if (orig_node) 388 if (orig_node)
389 batadv_orig_node_free_ref(orig_node); 389 batadv_orig_node_free_ref(orig_node);
390 return ret; 390 return ret;
391 } 391 }
392 392
393 393
394 int batadv_recv_icmp_packet(struct sk_buff *skb, 394 int batadv_recv_icmp_packet(struct sk_buff *skb,
395 struct batadv_hard_iface *recv_if) 395 struct batadv_hard_iface *recv_if)
396 { 396 {
397 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface); 397 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
398 struct batadv_icmp_packet_rr *icmp_packet; 398 struct batadv_icmp_packet_rr *icmp_packet;
399 struct ethhdr *ethhdr; 399 struct ethhdr *ethhdr;
400 struct batadv_orig_node *orig_node = NULL; 400 struct batadv_orig_node *orig_node = NULL;
401 struct batadv_neigh_node *router = NULL; 401 struct batadv_neigh_node *router = NULL;
402 int hdr_size = sizeof(struct batadv_icmp_packet); 402 int hdr_size = sizeof(struct batadv_icmp_packet);
403 int ret = NET_RX_DROP; 403 int ret = NET_RX_DROP;
404 404
405 /* we truncate all incoming icmp packets if they don't match our size */ 405 /* we truncate all incoming icmp packets if they don't match our size */
406 if (skb->len >= sizeof(struct batadv_icmp_packet_rr)) 406 if (skb->len >= sizeof(struct batadv_icmp_packet_rr))
407 hdr_size = sizeof(struct batadv_icmp_packet_rr); 407 hdr_size = sizeof(struct batadv_icmp_packet_rr);
408 408
409 /* drop packet if it has not necessary minimum size */ 409 /* drop packet if it has not necessary minimum size */
410 if (unlikely(!pskb_may_pull(skb, hdr_size))) 410 if (unlikely(!pskb_may_pull(skb, hdr_size)))
411 goto out; 411 goto out;
412 412
413 ethhdr = (struct ethhdr *)skb_mac_header(skb); 413 ethhdr = (struct ethhdr *)skb_mac_header(skb);
414 414
415 /* packet with unicast indication but broadcast recipient */ 415 /* packet with unicast indication but broadcast recipient */
416 if (is_broadcast_ether_addr(ethhdr->h_dest)) 416 if (is_broadcast_ether_addr(ethhdr->h_dest))
417 goto out; 417 goto out;
418 418
419 /* packet with broadcast sender address */ 419 /* packet with broadcast sender address */
420 if (is_broadcast_ether_addr(ethhdr->h_source)) 420 if (is_broadcast_ether_addr(ethhdr->h_source))
421 goto out; 421 goto out;
422 422
423 /* not for me */ 423 /* not for me */
424 if (!batadv_is_my_mac(ethhdr->h_dest)) 424 if (!batadv_is_my_mac(ethhdr->h_dest))
425 goto out; 425 goto out;
426 426
427 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data; 427 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
428 428
429 /* add record route information if not full */ 429 /* add record route information if not full */
430 if ((hdr_size == sizeof(struct batadv_icmp_packet_rr)) && 430 if ((hdr_size == sizeof(struct batadv_icmp_packet_rr)) &&
431 (icmp_packet->rr_cur < BATADV_RR_LEN)) { 431 (icmp_packet->rr_cur < BATADV_RR_LEN)) {
432 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]), 432 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
433 ethhdr->h_dest, ETH_ALEN); 433 ethhdr->h_dest, ETH_ALEN);
434 icmp_packet->rr_cur++; 434 icmp_packet->rr_cur++;
435 } 435 }
436 436
437 /* packet for me */ 437 /* packet for me */
438 if (batadv_is_my_mac(icmp_packet->dst)) 438 if (batadv_is_my_mac(icmp_packet->dst))
439 return batadv_recv_my_icmp_packet(bat_priv, skb, hdr_size); 439 return batadv_recv_my_icmp_packet(bat_priv, skb, hdr_size);
440 440
441 /* TTL exceeded */ 441 /* TTL exceeded */
442 if (icmp_packet->header.ttl < 2) 442 if (icmp_packet->header.ttl < 2)
443 return batadv_recv_icmp_ttl_exceeded(bat_priv, skb); 443 return batadv_recv_icmp_ttl_exceeded(bat_priv, skb);
444 444
445 /* get routing information */ 445 /* get routing information */
446 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->dst); 446 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->dst);
447 if (!orig_node) 447 if (!orig_node)
448 goto out; 448 goto out;
449 449
450 router = batadv_orig_node_get_router(orig_node); 450 router = batadv_orig_node_get_router(orig_node);
451 if (!router) 451 if (!router)
452 goto out; 452 goto out;
453 453
454 /* create a copy of the skb, if needed, to modify it. */ 454 /* create a copy of the skb, if needed, to modify it. */
455 if (skb_cow(skb, ETH_HLEN) < 0) 455 if (skb_cow(skb, ETH_HLEN) < 0)
456 goto out; 456 goto out;
457 457
458 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data; 458 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
459 459
460 /* decrement ttl */ 460 /* decrement ttl */
461 icmp_packet->header.ttl--; 461 icmp_packet->header.ttl--;
462 462
463 /* route it */ 463 /* route it */
464 batadv_send_skb_packet(skb, router->if_incoming, router->addr); 464 batadv_send_skb_packet(skb, router->if_incoming, router->addr);
465 ret = NET_RX_SUCCESS; 465 ret = NET_RX_SUCCESS;
466 466
467 out: 467 out:
468 if (router) 468 if (router)
469 batadv_neigh_node_free_ref(router); 469 batadv_neigh_node_free_ref(router);
470 if (orig_node) 470 if (orig_node)
471 batadv_orig_node_free_ref(orig_node); 471 batadv_orig_node_free_ref(orig_node);
472 return ret; 472 return ret;
473 } 473 }
474 474
475 /* In the bonding case, send the packets in a round 475 /* In the bonding case, send the packets in a round
476 * robin fashion over the remaining interfaces. 476 * robin fashion over the remaining interfaces.
477 * 477 *
478 * This method rotates the bonding list and increases the 478 * This method rotates the bonding list and increases the
479 * returned router's refcount. 479 * returned router's refcount.
480 */ 480 */
481 static struct batadv_neigh_node * 481 static struct batadv_neigh_node *
482 batadv_find_bond_router(struct batadv_orig_node *primary_orig, 482 batadv_find_bond_router(struct batadv_orig_node *primary_orig,
483 const struct batadv_hard_iface *recv_if) 483 const struct batadv_hard_iface *recv_if)
484 { 484 {
485 struct batadv_neigh_node *tmp_neigh_node; 485 struct batadv_neigh_node *tmp_neigh_node;
486 struct batadv_neigh_node *router = NULL, *first_candidate = NULL; 486 struct batadv_neigh_node *router = NULL, *first_candidate = NULL;
487 487
488 rcu_read_lock(); 488 rcu_read_lock();
489 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list, 489 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
490 bonding_list) { 490 bonding_list) {
491 if (!first_candidate) 491 if (!first_candidate)
492 first_candidate = tmp_neigh_node; 492 first_candidate = tmp_neigh_node;
493 493
494 /* recv_if == NULL on the first node. */ 494 /* recv_if == NULL on the first node. */
495 if (tmp_neigh_node->if_incoming == recv_if) 495 if (tmp_neigh_node->if_incoming == recv_if)
496 continue; 496 continue;
497 497
498 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount)) 498 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
499 continue; 499 continue;
500 500
501 router = tmp_neigh_node; 501 router = tmp_neigh_node;
502 break; 502 break;
503 } 503 }
504 504
505 /* use the first candidate if nothing was found. */ 505 /* use the first candidate if nothing was found. */
506 if (!router && first_candidate && 506 if (!router && first_candidate &&
507 atomic_inc_not_zero(&first_candidate->refcount)) 507 atomic_inc_not_zero(&first_candidate->refcount))
508 router = first_candidate; 508 router = first_candidate;
509 509
510 if (!router) 510 if (!router)
511 goto out; 511 goto out;
512 512
513 /* selected should point to the next element 513 /* selected should point to the next element
514 * after the current router 514 * after the current router
515 */ 515 */
516 spin_lock_bh(&primary_orig->neigh_list_lock); 516 spin_lock_bh(&primary_orig->neigh_list_lock);
517 /* this is a list_move(), which unfortunately 517 /* this is a list_move(), which unfortunately
518 * does not exist as rcu version 518 * does not exist as rcu version
519 */ 519 */
520 list_del_rcu(&primary_orig->bond_list); 520 list_del_rcu(&primary_orig->bond_list);
521 list_add_rcu(&primary_orig->bond_list, 521 list_add_rcu(&primary_orig->bond_list,
522 &router->bonding_list); 522 &router->bonding_list);
523 spin_unlock_bh(&primary_orig->neigh_list_lock); 523 spin_unlock_bh(&primary_orig->neigh_list_lock);
524 524
525 out: 525 out:
526 rcu_read_unlock(); 526 rcu_read_unlock();
527 return router; 527 return router;
528 } 528 }
529 529
530 /* Interface Alternating: Use the best of the 530 /* Interface Alternating: Use the best of the
531 * remaining candidates which are not using 531 * remaining candidates which are not using
532 * this interface. 532 * this interface.
533 * 533 *
534 * Increases the returned router's refcount 534 * Increases the returned router's refcount
535 */ 535 */
536 static struct batadv_neigh_node * 536 static struct batadv_neigh_node *
537 batadv_find_ifalter_router(struct batadv_orig_node *primary_orig, 537 batadv_find_ifalter_router(struct batadv_orig_node *primary_orig,
538 const struct batadv_hard_iface *recv_if) 538 const struct batadv_hard_iface *recv_if)
539 { 539 {
540 struct batadv_neigh_node *tmp_neigh_node; 540 struct batadv_neigh_node *tmp_neigh_node;
541 struct batadv_neigh_node *router = NULL, *first_candidate = NULL; 541 struct batadv_neigh_node *router = NULL, *first_candidate = NULL;
542 542
543 rcu_read_lock(); 543 rcu_read_lock();
544 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list, 544 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
545 bonding_list) { 545 bonding_list) {
546 if (!first_candidate) 546 if (!first_candidate)
547 first_candidate = tmp_neigh_node; 547 first_candidate = tmp_neigh_node;
548 548
549 /* recv_if == NULL on the first node. */ 549 /* recv_if == NULL on the first node. */
550 if (tmp_neigh_node->if_incoming == recv_if) 550 if (tmp_neigh_node->if_incoming == recv_if)
551 continue; 551 continue;
552 552
553 if (router && tmp_neigh_node->tq_avg <= router->tq_avg) 553 if (router && tmp_neigh_node->tq_avg <= router->tq_avg)
554 continue; 554 continue;
555 555
556 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount)) 556 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
557 continue; 557 continue;
558 558
559 /* decrement refcount of previously selected router */ 559 /* decrement refcount of previously selected router */
560 if (router) 560 if (router)
561 batadv_neigh_node_free_ref(router); 561 batadv_neigh_node_free_ref(router);
562 562
563 /* we found a better router (or at least one valid router) */ 563 /* we found a better router (or at least one valid router) */
564 router = tmp_neigh_node; 564 router = tmp_neigh_node;
565 } 565 }
566 566
567 /* use the first candidate if nothing was found. */ 567 /* use the first candidate if nothing was found. */
568 if (!router && first_candidate && 568 if (!router && first_candidate &&
569 atomic_inc_not_zero(&first_candidate->refcount)) 569 atomic_inc_not_zero(&first_candidate->refcount))
570 router = first_candidate; 570 router = first_candidate;
571 571
572 rcu_read_unlock(); 572 rcu_read_unlock();
573 return router; 573 return router;
574 } 574 }
575 575
576 static int batadv_check_unicast_packet(struct sk_buff *skb, int hdr_size) 576 static int batadv_check_unicast_packet(struct sk_buff *skb, int hdr_size)
577 { 577 {
578 struct ethhdr *ethhdr; 578 struct ethhdr *ethhdr;
579 579
580 /* drop packet if it has not necessary minimum size */ 580 /* drop packet if it has not necessary minimum size */
581 if (unlikely(!pskb_may_pull(skb, hdr_size))) 581 if (unlikely(!pskb_may_pull(skb, hdr_size)))
582 return -1; 582 return -1;
583 583
584 ethhdr = (struct ethhdr *)skb_mac_header(skb); 584 ethhdr = (struct ethhdr *)skb_mac_header(skb);
585 585
586 /* packet with unicast indication but broadcast recipient */ 586 /* packet with unicast indication but broadcast recipient */
587 if (is_broadcast_ether_addr(ethhdr->h_dest)) 587 if (is_broadcast_ether_addr(ethhdr->h_dest))
588 return -1; 588 return -1;
589 589
590 /* packet with broadcast sender address */ 590 /* packet with broadcast sender address */
591 if (is_broadcast_ether_addr(ethhdr->h_source)) 591 if (is_broadcast_ether_addr(ethhdr->h_source))
592 return -1; 592 return -1;
593 593
594 /* not for me */ 594 /* not for me */
595 if (!batadv_is_my_mac(ethhdr->h_dest)) 595 if (!batadv_is_my_mac(ethhdr->h_dest))
596 return -1; 596 return -1;
597 597
598 return 0; 598 return 0;
599 } 599 }
600 600
601 int batadv_recv_tt_query(struct sk_buff *skb, struct batadv_hard_iface *recv_if) 601 int batadv_recv_tt_query(struct sk_buff *skb, struct batadv_hard_iface *recv_if)
602 { 602 {
603 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface); 603 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
604 struct batadv_tt_query_packet *tt_query; 604 struct batadv_tt_query_packet *tt_query;
605 uint16_t tt_size; 605 uint16_t tt_size;
606 int hdr_size = sizeof(*tt_query); 606 int hdr_size = sizeof(*tt_query);
607 char tt_flag; 607 char tt_flag;
608 size_t packet_size; 608 size_t packet_size;
609 609
610 if (batadv_check_unicast_packet(skb, hdr_size) < 0) 610 if (batadv_check_unicast_packet(skb, hdr_size) < 0)
611 return NET_RX_DROP; 611 return NET_RX_DROP;
612 612
613 /* I could need to modify it */ 613 /* I could need to modify it */
614 if (skb_cow(skb, sizeof(struct batadv_tt_query_packet)) < 0) 614 if (skb_cow(skb, sizeof(struct batadv_tt_query_packet)) < 0)
615 goto out; 615 goto out;
616 616
617 tt_query = (struct batadv_tt_query_packet *)skb->data; 617 tt_query = (struct batadv_tt_query_packet *)skb->data;
618 618
619 switch (tt_query->flags & BATADV_TT_QUERY_TYPE_MASK) { 619 switch (tt_query->flags & BATADV_TT_QUERY_TYPE_MASK) {
620 case BATADV_TT_REQUEST: 620 case BATADV_TT_REQUEST:
621 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX); 621 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
622 622
623 /* If we cannot provide an answer the tt_request is 623 /* If we cannot provide an answer the tt_request is
624 * forwarded 624 * forwarded
625 */ 625 */
626 if (!batadv_send_tt_response(bat_priv, tt_query)) { 626 if (!batadv_send_tt_response(bat_priv, tt_query)) {
627 if (tt_query->flags & BATADV_TT_FULL_TABLE) 627 if (tt_query->flags & BATADV_TT_FULL_TABLE)
628 tt_flag = 'F'; 628 tt_flag = 'F';
629 else 629 else
630 tt_flag = '.'; 630 tt_flag = '.';
631 631
632 batadv_dbg(BATADV_DBG_TT, bat_priv, 632 batadv_dbg(BATADV_DBG_TT, bat_priv,
633 "Routing TT_REQUEST to %pM [%c]\n", 633 "Routing TT_REQUEST to %pM [%c]\n",
634 tt_query->dst, 634 tt_query->dst,
635 tt_flag); 635 tt_flag);
636 return batadv_route_unicast_packet(skb, recv_if); 636 return batadv_route_unicast_packet(skb, recv_if);
637 } 637 }
638 break; 638 break;
639 case BATADV_TT_RESPONSE: 639 case BATADV_TT_RESPONSE:
640 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX); 640 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
641 641
642 if (batadv_is_my_mac(tt_query->dst)) { 642 if (batadv_is_my_mac(tt_query->dst)) {
643 /* packet needs to be linearized to access the TT 643 /* packet needs to be linearized to access the TT
644 * changes 644 * changes
645 */ 645 */
646 if (skb_linearize(skb) < 0) 646 if (skb_linearize(skb) < 0)
647 goto out; 647 goto out;
648 /* skb_linearize() possibly changed skb->data */ 648 /* skb_linearize() possibly changed skb->data */
649 tt_query = (struct batadv_tt_query_packet *)skb->data; 649 tt_query = (struct batadv_tt_query_packet *)skb->data;
650 650
651 tt_size = batadv_tt_len(ntohs(tt_query->tt_data)); 651 tt_size = batadv_tt_len(ntohs(tt_query->tt_data));
652 652
653 /* Ensure we have all the claimed data */ 653 /* Ensure we have all the claimed data */
654 packet_size = sizeof(struct batadv_tt_query_packet); 654 packet_size = sizeof(struct batadv_tt_query_packet);
655 packet_size += tt_size; 655 packet_size += tt_size;
656 if (unlikely(skb_headlen(skb) < packet_size)) 656 if (unlikely(skb_headlen(skb) < packet_size))
657 goto out; 657 goto out;
658 658
659 batadv_handle_tt_response(bat_priv, tt_query); 659 batadv_handle_tt_response(bat_priv, tt_query);
660 } else { 660 } else {
661 if (tt_query->flags & BATADV_TT_FULL_TABLE) 661 if (tt_query->flags & BATADV_TT_FULL_TABLE)
662 tt_flag = 'F'; 662 tt_flag = 'F';
663 else 663 else
664 tt_flag = '.'; 664 tt_flag = '.';
665 batadv_dbg(BATADV_DBG_TT, bat_priv, 665 batadv_dbg(BATADV_DBG_TT, bat_priv,
666 "Routing TT_RESPONSE to %pM [%c]\n", 666 "Routing TT_RESPONSE to %pM [%c]\n",
667 tt_query->dst, 667 tt_query->dst,
668 tt_flag); 668 tt_flag);
669 return batadv_route_unicast_packet(skb, recv_if); 669 return batadv_route_unicast_packet(skb, recv_if);
670 } 670 }
671 break; 671 break;
672 } 672 }
673 673
674 out: 674 out:
675 /* returning NET_RX_DROP will make the caller function kfree the skb */ 675 /* returning NET_RX_DROP will make the caller function kfree the skb */
676 return NET_RX_DROP; 676 return NET_RX_DROP;
677 } 677 }
678 678
679 int batadv_recv_roam_adv(struct sk_buff *skb, struct batadv_hard_iface *recv_if) 679 int batadv_recv_roam_adv(struct sk_buff *skb, struct batadv_hard_iface *recv_if)
680 { 680 {
681 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface); 681 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
682 struct batadv_roam_adv_packet *roam_adv_packet; 682 struct batadv_roam_adv_packet *roam_adv_packet;
683 struct batadv_orig_node *orig_node; 683 struct batadv_orig_node *orig_node;
684 684
685 if (batadv_check_unicast_packet(skb, sizeof(*roam_adv_packet)) < 0) 685 if (batadv_check_unicast_packet(skb, sizeof(*roam_adv_packet)) < 0)
686 goto out; 686 goto out;
687 687
688 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX); 688 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
689 689
690 roam_adv_packet = (struct batadv_roam_adv_packet *)skb->data; 690 roam_adv_packet = (struct batadv_roam_adv_packet *)skb->data;
691 691
692 if (!batadv_is_my_mac(roam_adv_packet->dst)) 692 if (!batadv_is_my_mac(roam_adv_packet->dst))
693 return batadv_route_unicast_packet(skb, recv_if); 693 return batadv_route_unicast_packet(skb, recv_if);
694 694
695 /* check if it is a backbone gateway. we don't accept 695 /* check if it is a backbone gateway. we don't accept
696 * roaming advertisement from it, as it has the same 696 * roaming advertisement from it, as it has the same
697 * entries as we have. 697 * entries as we have.
698 */ 698 */
699 if (batadv_bla_is_backbone_gw_orig(bat_priv, roam_adv_packet->src)) 699 if (batadv_bla_is_backbone_gw_orig(bat_priv, roam_adv_packet->src))
700 goto out; 700 goto out;
701 701
702 orig_node = batadv_orig_hash_find(bat_priv, roam_adv_packet->src); 702 orig_node = batadv_orig_hash_find(bat_priv, roam_adv_packet->src);
703 if (!orig_node) 703 if (!orig_node)
704 goto out; 704 goto out;
705 705
706 batadv_dbg(BATADV_DBG_TT, bat_priv, 706 batadv_dbg(BATADV_DBG_TT, bat_priv,
707 "Received ROAMING_ADV from %pM (client %pM)\n", 707 "Received ROAMING_ADV from %pM (client %pM)\n",
708 roam_adv_packet->src, roam_adv_packet->client); 708 roam_adv_packet->src, roam_adv_packet->client);
709 709
710 batadv_tt_global_add(bat_priv, orig_node, roam_adv_packet->client, 710 batadv_tt_global_add(bat_priv, orig_node, roam_adv_packet->client,
711 BATADV_TT_CLIENT_ROAM, 711 BATADV_TT_CLIENT_ROAM,
712 atomic_read(&orig_node->last_ttvn) + 1); 712 atomic_read(&orig_node->last_ttvn) + 1);
713 713
714 /* Roaming phase starts: I have new information but the ttvn has not
715 * been incremented yet. This flag will make me check all the incoming
716 * packets for the correct destination.
717 */
718 bat_priv->tt.poss_change = true;
719
720 batadv_orig_node_free_ref(orig_node); 714 batadv_orig_node_free_ref(orig_node);
721 out: 715 out:
722 /* returning NET_RX_DROP will make the caller function kfree the skb */ 716 /* returning NET_RX_DROP will make the caller function kfree the skb */
723 return NET_RX_DROP; 717 return NET_RX_DROP;
724 } 718 }
725 719
726 /* find a suitable router for this originator, and use 720 /* find a suitable router for this originator, and use
727 * bonding if possible. increases the found neighbors 721 * bonding if possible. increases the found neighbors
728 * refcount. 722 * refcount.
729 */ 723 */
730 struct batadv_neigh_node * 724 struct batadv_neigh_node *
731 batadv_find_router(struct batadv_priv *bat_priv, 725 batadv_find_router(struct batadv_priv *bat_priv,
732 struct batadv_orig_node *orig_node, 726 struct batadv_orig_node *orig_node,
733 const struct batadv_hard_iface *recv_if) 727 const struct batadv_hard_iface *recv_if)
734 { 728 {
735 struct batadv_orig_node *primary_orig_node; 729 struct batadv_orig_node *primary_orig_node;
736 struct batadv_orig_node *router_orig; 730 struct batadv_orig_node *router_orig;
737 struct batadv_neigh_node *router; 731 struct batadv_neigh_node *router;
738 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0}; 732 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
739 int bonding_enabled; 733 int bonding_enabled;
740 uint8_t *primary_addr; 734 uint8_t *primary_addr;
741 735
742 if (!orig_node) 736 if (!orig_node)
743 return NULL; 737 return NULL;
744 738
745 router = batadv_orig_node_get_router(orig_node); 739 router = batadv_orig_node_get_router(orig_node);
746 if (!router) 740 if (!router)
747 goto err; 741 goto err;
748 742
749 /* without bonding, the first node should 743 /* without bonding, the first node should
750 * always choose the default router. 744 * always choose the default router.
751 */ 745 */
752 bonding_enabled = atomic_read(&bat_priv->bonding); 746 bonding_enabled = atomic_read(&bat_priv->bonding);
753 747
754 rcu_read_lock(); 748 rcu_read_lock();
755 /* select default router to output */ 749 /* select default router to output */
756 router_orig = router->orig_node; 750 router_orig = router->orig_node;
757 if (!router_orig) 751 if (!router_orig)
758 goto err_unlock; 752 goto err_unlock;
759 753
760 if ((!recv_if) && (!bonding_enabled)) 754 if ((!recv_if) && (!bonding_enabled))
761 goto return_router; 755 goto return_router;
762 756
763 primary_addr = router_orig->primary_addr; 757 primary_addr = router_orig->primary_addr;
764 758
765 /* if we have something in the primary_addr, we can search 759 /* if we have something in the primary_addr, we can search
766 * for a potential bonding candidate. 760 * for a potential bonding candidate.
767 */ 761 */
768 if (batadv_compare_eth(primary_addr, zero_mac)) 762 if (batadv_compare_eth(primary_addr, zero_mac))
769 goto return_router; 763 goto return_router;
770 764
771 /* find the orig_node which has the primary interface. might 765 /* find the orig_node which has the primary interface. might
772 * even be the same as our router_orig in many cases 766 * even be the same as our router_orig in many cases
773 */ 767 */
774 if (batadv_compare_eth(primary_addr, router_orig->orig)) { 768 if (batadv_compare_eth(primary_addr, router_orig->orig)) {
775 primary_orig_node = router_orig; 769 primary_orig_node = router_orig;
776 } else { 770 } else {
777 primary_orig_node = batadv_orig_hash_find(bat_priv, 771 primary_orig_node = batadv_orig_hash_find(bat_priv,
778 primary_addr); 772 primary_addr);
779 if (!primary_orig_node) 773 if (!primary_orig_node)
780 goto return_router; 774 goto return_router;
781 775
782 batadv_orig_node_free_ref(primary_orig_node); 776 batadv_orig_node_free_ref(primary_orig_node);
783 } 777 }
784 778
785 /* with less than 2 candidates, we can't do any 779 /* with less than 2 candidates, we can't do any
786 * bonding and prefer the original router. 780 * bonding and prefer the original router.
787 */ 781 */
788 if (atomic_read(&primary_orig_node->bond_candidates) < 2) 782 if (atomic_read(&primary_orig_node->bond_candidates) < 2)
789 goto return_router; 783 goto return_router;
790 784
791 /* all nodes between should choose a candidate which 785 /* all nodes between should choose a candidate which
792 * is is not on the interface where the packet came 786 * is is not on the interface where the packet came
793 * in. 787 * in.
794 */ 788 */
795 batadv_neigh_node_free_ref(router); 789 batadv_neigh_node_free_ref(router);
796 790
797 if (bonding_enabled) 791 if (bonding_enabled)
798 router = batadv_find_bond_router(primary_orig_node, recv_if); 792 router = batadv_find_bond_router(primary_orig_node, recv_if);
799 else 793 else
800 router = batadv_find_ifalter_router(primary_orig_node, recv_if); 794 router = batadv_find_ifalter_router(primary_orig_node, recv_if);
801 795
802 return_router: 796 return_router:
803 if (router && router->if_incoming->if_status != BATADV_IF_ACTIVE) 797 if (router && router->if_incoming->if_status != BATADV_IF_ACTIVE)
804 goto err_unlock; 798 goto err_unlock;
805 799
806 rcu_read_unlock(); 800 rcu_read_unlock();
807 return router; 801 return router;
808 err_unlock: 802 err_unlock:
809 rcu_read_unlock(); 803 rcu_read_unlock();
810 err: 804 err:
811 if (router) 805 if (router)
812 batadv_neigh_node_free_ref(router); 806 batadv_neigh_node_free_ref(router);
813 return NULL; 807 return NULL;
814 } 808 }
815 809
816 static int batadv_route_unicast_packet(struct sk_buff *skb, 810 static int batadv_route_unicast_packet(struct sk_buff *skb,
817 struct batadv_hard_iface *recv_if) 811 struct batadv_hard_iface *recv_if)
818 { 812 {
819 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface); 813 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
820 struct batadv_orig_node *orig_node = NULL; 814 struct batadv_orig_node *orig_node = NULL;
821 struct batadv_neigh_node *neigh_node = NULL; 815 struct batadv_neigh_node *neigh_node = NULL;
822 struct batadv_unicast_packet *unicast_packet; 816 struct batadv_unicast_packet *unicast_packet;
823 struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb); 817 struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
824 int ret = NET_RX_DROP; 818 int ret = NET_RX_DROP;
825 struct sk_buff *new_skb; 819 struct sk_buff *new_skb;
826 820
827 unicast_packet = (struct batadv_unicast_packet *)skb->data; 821 unicast_packet = (struct batadv_unicast_packet *)skb->data;
828 822
829 /* TTL exceeded */ 823 /* TTL exceeded */
830 if (unicast_packet->header.ttl < 2) { 824 if (unicast_packet->header.ttl < 2) {
831 pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n", 825 pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
832 ethhdr->h_source, unicast_packet->dest); 826 ethhdr->h_source, unicast_packet->dest);
833 goto out; 827 goto out;
834 } 828 }
835 829
836 /* get routing information */ 830 /* get routing information */
837 orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest); 831 orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
838 832
839 if (!orig_node) 833 if (!orig_node)
840 goto out; 834 goto out;
841 835
842 /* find_router() increases neigh_nodes refcount if found. */ 836 /* find_router() increases neigh_nodes refcount if found. */
843 neigh_node = batadv_find_router(bat_priv, orig_node, recv_if); 837 neigh_node = batadv_find_router(bat_priv, orig_node, recv_if);
844 838
845 if (!neigh_node) 839 if (!neigh_node)
846 goto out; 840 goto out;
847 841
848 /* create a copy of the skb, if needed, to modify it. */ 842 /* create a copy of the skb, if needed, to modify it. */
849 if (skb_cow(skb, ETH_HLEN) < 0) 843 if (skb_cow(skb, ETH_HLEN) < 0)
850 goto out; 844 goto out;
851 845
852 unicast_packet = (struct batadv_unicast_packet *)skb->data; 846 unicast_packet = (struct batadv_unicast_packet *)skb->data;
853 847
854 if (unicast_packet->header.packet_type == BATADV_UNICAST && 848 if (unicast_packet->header.packet_type == BATADV_UNICAST &&
855 atomic_read(&bat_priv->fragmentation) && 849 atomic_read(&bat_priv->fragmentation) &&
856 skb->len > neigh_node->if_incoming->net_dev->mtu) { 850 skb->len > neigh_node->if_incoming->net_dev->mtu) {
857 ret = batadv_frag_send_skb(skb, bat_priv, 851 ret = batadv_frag_send_skb(skb, bat_priv,
858 neigh_node->if_incoming, 852 neigh_node->if_incoming,
859 neigh_node->addr); 853 neigh_node->addr);
860 goto out; 854 goto out;
861 } 855 }
862 856
863 if (unicast_packet->header.packet_type == BATADV_UNICAST_FRAG && 857 if (unicast_packet->header.packet_type == BATADV_UNICAST_FRAG &&
864 batadv_frag_can_reassemble(skb, 858 batadv_frag_can_reassemble(skb,
865 neigh_node->if_incoming->net_dev->mtu)) { 859 neigh_node->if_incoming->net_dev->mtu)) {
866 860
867 ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb); 861 ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
868 862
869 if (ret == NET_RX_DROP) 863 if (ret == NET_RX_DROP)
870 goto out; 864 goto out;
871 865
872 /* packet was buffered for late merge */ 866 /* packet was buffered for late merge */
873 if (!new_skb) { 867 if (!new_skb) {
874 ret = NET_RX_SUCCESS; 868 ret = NET_RX_SUCCESS;
875 goto out; 869 goto out;
876 } 870 }
877 871
878 skb = new_skb; 872 skb = new_skb;
879 unicast_packet = (struct batadv_unicast_packet *)skb->data; 873 unicast_packet = (struct batadv_unicast_packet *)skb->data;
880 } 874 }
881 875
882 /* decrement ttl */ 876 /* decrement ttl */
883 unicast_packet->header.ttl--; 877 unicast_packet->header.ttl--;
884 878
885 /* Update stats counter */ 879 /* Update stats counter */
886 batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD); 880 batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
887 batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES, 881 batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
888 skb->len + ETH_HLEN); 882 skb->len + ETH_HLEN);
889 883
890 /* route it */ 884 /* route it */
891 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr); 885 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
892 ret = NET_RX_SUCCESS; 886 ret = NET_RX_SUCCESS;
893 887
894 out: 888 out:
895 if (neigh_node) 889 if (neigh_node)
896 batadv_neigh_node_free_ref(neigh_node); 890 batadv_neigh_node_free_ref(neigh_node);
897 if (orig_node) 891 if (orig_node)
898 batadv_orig_node_free_ref(orig_node); 892 batadv_orig_node_free_ref(orig_node);
899 return ret; 893 return ret;
900 } 894 }
901 895
896 /**
897 * batadv_reroute_unicast_packet - update the unicast header for re-routing
898 * @bat_priv: the bat priv with all the soft interface information
899 * @unicast_packet: the unicast header to be updated
900 * @dst_addr: the payload destination
901 *
902 * Search the translation table for dst_addr and update the unicast header with
903 * the new corresponding information (originator address where the destination
904 * client currently is and its known TTVN)
905 *
906 * Returns true if the packet header has been updated, false otherwise
907 */
908 static bool
909 batadv_reroute_unicast_packet(struct batadv_priv *bat_priv,
910 struct batadv_unicast_packet *unicast_packet,
911 uint8_t *dst_addr)
912 {
913 struct batadv_orig_node *orig_node = NULL;
914 struct batadv_hard_iface *primary_if = NULL;
915 bool ret = false;
916 uint8_t *orig_addr, orig_ttvn;
917
918 if (batadv_is_my_client(bat_priv, dst_addr)) {
919 primary_if = batadv_primary_if_get_selected(bat_priv);
920 if (!primary_if)
921 goto out;
922 orig_addr = primary_if->net_dev->dev_addr;
923 orig_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
924 } else {
925 orig_node = batadv_transtable_search(bat_priv, NULL, dst_addr);
926 if (!orig_node)
927 goto out;
928
929 if (batadv_compare_eth(orig_node->orig, unicast_packet->dest))
930 goto out;
931
932 orig_addr = orig_node->orig;
933 orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
934 }
935
936 /* update the packet header */
937 memcpy(unicast_packet->dest, orig_addr, ETH_ALEN);
938 unicast_packet->ttvn = orig_ttvn;
939
940 ret = true;
941 out:
942 if (primary_if)
943 batadv_hardif_free_ref(primary_if);
944 if (orig_node)
945 batadv_orig_node_free_ref(orig_node);
946
947 return ret;
948 }
949
902 static int batadv_check_unicast_ttvn(struct batadv_priv *bat_priv, 950 static int batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
903 struct sk_buff *skb) { 951 struct sk_buff *skb) {
904 uint8_t curr_ttvn; 952 uint8_t curr_ttvn, old_ttvn;
905 struct batadv_orig_node *orig_node; 953 struct batadv_orig_node *orig_node;
906 struct ethhdr *ethhdr; 954 struct ethhdr *ethhdr;
907 struct batadv_hard_iface *primary_if; 955 struct batadv_hard_iface *primary_if;
908 struct batadv_unicast_packet *unicast_packet; 956 struct batadv_unicast_packet *unicast_packet;
909 bool tt_poss_change;
910 int is_old_ttvn; 957 int is_old_ttvn;
911 958
912 /* check if there is enough data before accessing it */ 959 /* check if there is enough data before accessing it */
913 if (pskb_may_pull(skb, sizeof(*unicast_packet) + ETH_HLEN) < 0) 960 if (pskb_may_pull(skb, sizeof(*unicast_packet) + ETH_HLEN) < 0)
914 return 0; 961 return 0;
915 962
916 /* create a copy of the skb (in case of for re-routing) to modify it. */ 963 /* create a copy of the skb (in case of for re-routing) to modify it. */
917 if (skb_cow(skb, sizeof(*unicast_packet)) < 0) 964 if (skb_cow(skb, sizeof(*unicast_packet)) < 0)
918 return 0; 965 return 0;
919 966
920 unicast_packet = (struct batadv_unicast_packet *)skb->data; 967 unicast_packet = (struct batadv_unicast_packet *)skb->data;
968 ethhdr = (struct ethhdr *)(skb->data + sizeof(*unicast_packet));
921 969
922 if (batadv_is_my_mac(unicast_packet->dest)) { 970 /* check if the destination client was served by this node and it is now
923 tt_poss_change = bat_priv->tt.poss_change; 971 * roaming. In this case, it means that the node has got a ROAM_ADV
924 curr_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn); 972 * message and that it knows the new destination in the mesh to re-route
925 } else { 973 * the packet to
974 */
975 if (batadv_tt_local_client_is_roaming(bat_priv, ethhdr->h_dest)) {
976 if (batadv_reroute_unicast_packet(bat_priv, unicast_packet,
977 ethhdr->h_dest))
978 net_ratelimited_function(batadv_dbg, BATADV_DBG_TT,
979 bat_priv,
980 "Rerouting unicast packet to %pM (dst=%pM): Local Roaming\n",
981 unicast_packet->dest,
982 ethhdr->h_dest);
983 /* at this point the mesh destination should have been
984 * substituted with the originator address found in the global
985 * table. If not, let the packet go untouched anyway because
986 * there is nothing the node can do
987 */
988 return 1;
989 }
990
991 /* retrieve the TTVN known by this node for the packet destination. This
992 * value is used later to check if the node which sent (or re-routed
993 * last time) the packet had an updated information or not
994 */
995 curr_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
996 if (!batadv_is_my_mac(unicast_packet->dest)) {
926 orig_node = batadv_orig_hash_find(bat_priv, 997 orig_node = batadv_orig_hash_find(bat_priv,
927 unicast_packet->dest); 998 unicast_packet->dest);
928 999 /* if it is not possible to find the orig_node representing the
1000 * destination, the packet can immediately be dropped as it will
1001 * not be possible to deliver it
1002 */
929 if (!orig_node) 1003 if (!orig_node)
930 return 0; 1004 return 0;
931 1005
932 curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn); 1006 curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
933 tt_poss_change = orig_node->tt_poss_change;
934 batadv_orig_node_free_ref(orig_node); 1007 batadv_orig_node_free_ref(orig_node);
935 } 1008 }
936 1009
937 /* Check whether I have to reroute the packet */ 1010 /* check if the TTVN contained in the packet is fresher than what the
1011 * node knows
1012 */
938 is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn); 1013 is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn);
939 if (is_old_ttvn || tt_poss_change) { 1014 if (!is_old_ttvn)
940 /* check if there is enough data before accessing it */ 1015 return 1;
941 if (pskb_may_pull(skb, sizeof(struct batadv_unicast_packet) +
942 ETH_HLEN) < 0)
943 return 0;
944 1016
945 ethhdr = (struct ethhdr *)(skb->data + sizeof(*unicast_packet)); 1017 old_ttvn = unicast_packet->ttvn;
1018 /* the packet was forged based on outdated network information. Its
1019 * destination can possibly be updated and forwarded towards the new
1020 * target host
1021 */
1022 if (batadv_reroute_unicast_packet(bat_priv, unicast_packet,
1023 ethhdr->h_dest)) {
1024 net_ratelimited_function(batadv_dbg, BATADV_DBG_TT, bat_priv,
1025 "Rerouting unicast packet to %pM (dst=%pM): TTVN mismatch old_ttvn=%u new_ttvn=%u\n",
1026 unicast_packet->dest, ethhdr->h_dest,
1027 old_ttvn, curr_ttvn);
1028 return 1;
1029 }
946 1030
947 /* we don't have an updated route for this client, so we should 1031 /* the packet has not been re-routed: either the destination is
948 * not try to reroute the packet!! 1032 * currently served by this node or there is no destination at all and
949 */ 1033 * it is possible to drop the packet
950 if (batadv_tt_global_client_is_roaming(bat_priv, 1034 */
951 ethhdr->h_dest)) 1035 if (!batadv_is_my_client(bat_priv, ethhdr->h_dest))
952 return 1; 1036 return 0;
953 1037
954 orig_node = batadv_transtable_search(bat_priv, NULL, 1038 /* update the header in order to let the packet be delivered to this
955 ethhdr->h_dest); 1039 * node's soft interface
1040 */
1041 primary_if = batadv_primary_if_get_selected(bat_priv);
1042 if (!primary_if)
1043 return 0;
956 1044
957 if (!orig_node) { 1045 memcpy(unicast_packet->dest, primary_if->net_dev->dev_addr, ETH_ALEN);
958 if (!batadv_is_my_client(bat_priv, ethhdr->h_dest))
959 return 0;
960 primary_if = batadv_primary_if_get_selected(bat_priv);
961 if (!primary_if)
962 return 0;
963 memcpy(unicast_packet->dest,
964 primary_if->net_dev->dev_addr, ETH_ALEN);
965 batadv_hardif_free_ref(primary_if);
966 } else {
967 memcpy(unicast_packet->dest, orig_node->orig,
968 ETH_ALEN);
969 curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
970 batadv_orig_node_free_ref(orig_node);
971 }
972 1046
973 net_ratelimited_function(batadv_dbg, BATADV_DBG_TT, bat_priv, 1047 batadv_hardif_free_ref(primary_if);
974 "TTVN mismatch (old_ttvn %u new_ttvn %u)! Rerouting unicast packet (for %pM) to %pM\n",
975 unicast_packet->ttvn, curr_ttvn,
976 ethhdr->h_dest, unicast_packet->dest);
977 1048
978 unicast_packet->ttvn = curr_ttvn; 1049 unicast_packet->ttvn = curr_ttvn;
979 } 1050
980 return 1; 1051 return 1;
981 } 1052 }
982 1053
983 int batadv_recv_unicast_packet(struct sk_buff *skb, 1054 int batadv_recv_unicast_packet(struct sk_buff *skb,
984 struct batadv_hard_iface *recv_if) 1055 struct batadv_hard_iface *recv_if)
985 { 1056 {
986 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface); 1057 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
987 struct batadv_unicast_packet *unicast_packet; 1058 struct batadv_unicast_packet *unicast_packet;
988 struct batadv_unicast_4addr_packet *unicast_4addr_packet; 1059 struct batadv_unicast_4addr_packet *unicast_4addr_packet;
989 uint8_t *orig_addr; 1060 uint8_t *orig_addr;
990 struct batadv_orig_node *orig_node = NULL; 1061 struct batadv_orig_node *orig_node = NULL;
991 int hdr_size = sizeof(*unicast_packet); 1062 int hdr_size = sizeof(*unicast_packet);
992 bool is4addr; 1063 bool is4addr;
993 1064
994 unicast_packet = (struct batadv_unicast_packet *)skb->data; 1065 unicast_packet = (struct batadv_unicast_packet *)skb->data;
995 unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data; 1066 unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
996 1067
997 is4addr = unicast_packet->header.packet_type == BATADV_UNICAST_4ADDR; 1068 is4addr = unicast_packet->header.packet_type == BATADV_UNICAST_4ADDR;
998 /* the caller function should have already pulled 2 bytes */ 1069 /* the caller function should have already pulled 2 bytes */
999 if (is4addr) 1070 if (is4addr)
1000 hdr_size = sizeof(*unicast_4addr_packet); 1071 hdr_size = sizeof(*unicast_4addr_packet);
1001 1072
1002 if (batadv_check_unicast_packet(skb, hdr_size) < 0) 1073 if (batadv_check_unicast_packet(skb, hdr_size) < 0)
1003 return NET_RX_DROP; 1074 return NET_RX_DROP;
1004 1075
1005 if (!batadv_check_unicast_ttvn(bat_priv, skb)) 1076 if (!batadv_check_unicast_ttvn(bat_priv, skb))
1006 return NET_RX_DROP; 1077 return NET_RX_DROP;
1007 1078
1008 /* packet for me */ 1079 /* packet for me */
1009 if (batadv_is_my_mac(unicast_packet->dest)) { 1080 if (batadv_is_my_mac(unicast_packet->dest)) {
1010 if (is4addr) { 1081 if (is4addr) {
1011 batadv_dat_inc_counter(bat_priv, 1082 batadv_dat_inc_counter(bat_priv,
1012 unicast_4addr_packet->subtype); 1083 unicast_4addr_packet->subtype);
1013 orig_addr = unicast_4addr_packet->src; 1084 orig_addr = unicast_4addr_packet->src;
1014 orig_node = batadv_orig_hash_find(bat_priv, orig_addr); 1085 orig_node = batadv_orig_hash_find(bat_priv, orig_addr);
1015 } 1086 }
1016 1087
1017 if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb, 1088 if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb,
1018 hdr_size)) 1089 hdr_size))
1019 goto rx_success; 1090 goto rx_success;
1020 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb, 1091 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb,
1021 hdr_size)) 1092 hdr_size))
1022 goto rx_success; 1093 goto rx_success;
1023 1094
1024 batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size, 1095 batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
1025 orig_node); 1096 orig_node);
1026 1097
1027 rx_success: 1098 rx_success:
1028 if (orig_node) 1099 if (orig_node)
1029 batadv_orig_node_free_ref(orig_node); 1100 batadv_orig_node_free_ref(orig_node);
1030 1101
1031 return NET_RX_SUCCESS; 1102 return NET_RX_SUCCESS;
1032 } 1103 }
1033 1104
1034 return batadv_route_unicast_packet(skb, recv_if); 1105 return batadv_route_unicast_packet(skb, recv_if);
1035 } 1106 }
1036 1107
1037 int batadv_recv_ucast_frag_packet(struct sk_buff *skb, 1108 int batadv_recv_ucast_frag_packet(struct sk_buff *skb,
1038 struct batadv_hard_iface *recv_if) 1109 struct batadv_hard_iface *recv_if)
1039 { 1110 {
1040 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface); 1111 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1041 struct batadv_unicast_frag_packet *unicast_packet; 1112 struct batadv_unicast_frag_packet *unicast_packet;
1042 int hdr_size = sizeof(*unicast_packet); 1113 int hdr_size = sizeof(*unicast_packet);
1043 struct sk_buff *new_skb = NULL; 1114 struct sk_buff *new_skb = NULL;
1044 int ret; 1115 int ret;
1045 1116
1046 if (batadv_check_unicast_packet(skb, hdr_size) < 0) 1117 if (batadv_check_unicast_packet(skb, hdr_size) < 0)
1047 return NET_RX_DROP; 1118 return NET_RX_DROP;
1048 1119
1049 if (!batadv_check_unicast_ttvn(bat_priv, skb)) 1120 if (!batadv_check_unicast_ttvn(bat_priv, skb))
1050 return NET_RX_DROP; 1121 return NET_RX_DROP;
1051 1122
1052 unicast_packet = (struct batadv_unicast_frag_packet *)skb->data; 1123 unicast_packet = (struct batadv_unicast_frag_packet *)skb->data;
1053 1124
1054 /* packet for me */ 1125 /* packet for me */
1055 if (batadv_is_my_mac(unicast_packet->dest)) { 1126 if (batadv_is_my_mac(unicast_packet->dest)) {
1056 1127
1057 ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb); 1128 ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
1058 1129
1059 if (ret == NET_RX_DROP) 1130 if (ret == NET_RX_DROP)
1060 return NET_RX_DROP; 1131 return NET_RX_DROP;
1061 1132
1062 /* packet was buffered for late merge */ 1133 /* packet was buffered for late merge */
1063 if (!new_skb) 1134 if (!new_skb)
1064 return NET_RX_SUCCESS; 1135 return NET_RX_SUCCESS;
1065 1136
1066 if (batadv_dat_snoop_incoming_arp_request(bat_priv, new_skb, 1137 if (batadv_dat_snoop_incoming_arp_request(bat_priv, new_skb,
1067 hdr_size)) 1138 hdr_size))
1068 goto rx_success; 1139 goto rx_success;
1069 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, new_skb, 1140 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, new_skb,
1070 hdr_size)) 1141 hdr_size))
1071 goto rx_success; 1142 goto rx_success;
1072 1143
1073 batadv_interface_rx(recv_if->soft_iface, new_skb, recv_if, 1144 batadv_interface_rx(recv_if->soft_iface, new_skb, recv_if,
1074 sizeof(struct batadv_unicast_packet), NULL); 1145 sizeof(struct batadv_unicast_packet), NULL);
1075 1146
1076 rx_success: 1147 rx_success:
1077 return NET_RX_SUCCESS; 1148 return NET_RX_SUCCESS;
1078 } 1149 }
1079 1150
1080 return batadv_route_unicast_packet(skb, recv_if); 1151 return batadv_route_unicast_packet(skb, recv_if);
1081 } 1152 }
1082 1153
1083 1154
1084 int batadv_recv_bcast_packet(struct sk_buff *skb, 1155 int batadv_recv_bcast_packet(struct sk_buff *skb,
1085 struct batadv_hard_iface *recv_if) 1156 struct batadv_hard_iface *recv_if)
1086 { 1157 {
1087 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface); 1158 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1088 struct batadv_orig_node *orig_node = NULL; 1159 struct batadv_orig_node *orig_node = NULL;
1089 struct batadv_bcast_packet *bcast_packet; 1160 struct batadv_bcast_packet *bcast_packet;
1090 struct ethhdr *ethhdr; 1161 struct ethhdr *ethhdr;
1091 int hdr_size = sizeof(*bcast_packet); 1162 int hdr_size = sizeof(*bcast_packet);
1092 int ret = NET_RX_DROP; 1163 int ret = NET_RX_DROP;
1093 int32_t seq_diff; 1164 int32_t seq_diff;
1094 1165
1095 /* drop packet if it has not necessary minimum size */ 1166 /* drop packet if it has not necessary minimum size */
1096 if (unlikely(!pskb_may_pull(skb, hdr_size))) 1167 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1097 goto out; 1168 goto out;
1098 1169
1099 ethhdr = (struct ethhdr *)skb_mac_header(skb); 1170 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1100 1171
1101 /* packet with broadcast indication but unicast recipient */ 1172 /* packet with broadcast indication but unicast recipient */
1102 if (!is_broadcast_ether_addr(ethhdr->h_dest)) 1173 if (!is_broadcast_ether_addr(ethhdr->h_dest))
1103 goto out; 1174 goto out;
1104 1175
1105 /* packet with broadcast sender address */ 1176 /* packet with broadcast sender address */
1106 if (is_broadcast_ether_addr(ethhdr->h_source)) 1177 if (is_broadcast_ether_addr(ethhdr->h_source))
1107 goto out; 1178 goto out;
1108 1179
1109 /* ignore broadcasts sent by myself */ 1180 /* ignore broadcasts sent by myself */
1110 if (batadv_is_my_mac(ethhdr->h_source)) 1181 if (batadv_is_my_mac(ethhdr->h_source))
1111 goto out; 1182 goto out;
1112 1183
1113 bcast_packet = (struct batadv_bcast_packet *)skb->data; 1184 bcast_packet = (struct batadv_bcast_packet *)skb->data;
1114 1185
1115 /* ignore broadcasts originated by myself */ 1186 /* ignore broadcasts originated by myself */
1116 if (batadv_is_my_mac(bcast_packet->orig)) 1187 if (batadv_is_my_mac(bcast_packet->orig))
1117 goto out; 1188 goto out;
1118 1189
1119 if (bcast_packet->header.ttl < 2) 1190 if (bcast_packet->header.ttl < 2)
1120 goto out; 1191 goto out;
1121 1192
1122 orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig); 1193 orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
1123 1194
1124 if (!orig_node) 1195 if (!orig_node)
1125 goto out; 1196 goto out;
1126 1197
1127 spin_lock_bh(&orig_node->bcast_seqno_lock); 1198 spin_lock_bh(&orig_node->bcast_seqno_lock);
1128 1199
1129 /* check whether the packet is a duplicate */ 1200 /* check whether the packet is a duplicate */
1130 if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno, 1201 if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
1131 ntohl(bcast_packet->seqno))) 1202 ntohl(bcast_packet->seqno)))
1132 goto spin_unlock; 1203 goto spin_unlock;
1133 1204
1134 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno; 1205 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1135 1206
1136 /* check whether the packet is old and the host just restarted. */ 1207 /* check whether the packet is old and the host just restarted. */
1137 if (batadv_window_protected(bat_priv, seq_diff, 1208 if (batadv_window_protected(bat_priv, seq_diff,
1138 &orig_node->bcast_seqno_reset)) 1209 &orig_node->bcast_seqno_reset))
1139 goto spin_unlock; 1210 goto spin_unlock;
1140 1211
1141 /* mark broadcast in flood history, update window position 1212 /* mark broadcast in flood history, update window position
1142 * if required. 1213 * if required.
1143 */ 1214 */
1144 if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1)) 1215 if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
1145 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno); 1216 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1146 1217
1147 spin_unlock_bh(&orig_node->bcast_seqno_lock); 1218 spin_unlock_bh(&orig_node->bcast_seqno_lock);
1148 1219
1149 /* keep skb linear for crc calculation */ 1220 /* keep skb linear for crc calculation */
1150 if (skb_linearize(skb) < 0) 1221 if (skb_linearize(skb) < 0)
1151 goto out; 1222 goto out;
1152 1223
1153 bcast_packet = (struct batadv_bcast_packet *)skb->data; 1224 bcast_packet = (struct batadv_bcast_packet *)skb->data;
1154 1225
1155 /* check whether this has been sent by another originator before */ 1226 /* check whether this has been sent by another originator before */
1156 if (batadv_bla_check_bcast_duplist(bat_priv, bcast_packet, skb->len)) 1227 if (batadv_bla_check_bcast_duplist(bat_priv, bcast_packet, skb->len))
1157 goto out; 1228 goto out;
1158 1229
1159 /* rebroadcast packet */ 1230 /* rebroadcast packet */
1160 batadv_add_bcast_packet_to_list(bat_priv, skb, 1); 1231 batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
1161 1232
1162 /* don't hand the broadcast up if it is from an originator 1233 /* don't hand the broadcast up if it is from an originator
1163 * from the same backbone. 1234 * from the same backbone.
1164 */ 1235 */
1165 if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size)) 1236 if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
1166 goto out; 1237 goto out;
1167 1238
1168 if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb, hdr_size)) 1239 if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb, hdr_size))
1169 goto rx_success; 1240 goto rx_success;
1170 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb, hdr_size)) 1241 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb, hdr_size))
1171 goto rx_success; 1242 goto rx_success;
1172 1243
1173 /* broadcast for me */ 1244 /* broadcast for me */
1174 batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size, 1245 batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
1175 orig_node); 1246 orig_node);
1176 1247
1177 rx_success: 1248 rx_success:
1178 ret = NET_RX_SUCCESS; 1249 ret = NET_RX_SUCCESS;
1179 goto out; 1250 goto out;
1180 1251
1181 spin_unlock: 1252 spin_unlock:
1182 spin_unlock_bh(&orig_node->bcast_seqno_lock); 1253 spin_unlock_bh(&orig_node->bcast_seqno_lock);
1183 out: 1254 out:
1184 if (orig_node) 1255 if (orig_node)
1185 batadv_orig_node_free_ref(orig_node); 1256 batadv_orig_node_free_ref(orig_node);
1186 return ret; 1257 return ret;
1187 } 1258 }
1188 1259
1189 int batadv_recv_vis_packet(struct sk_buff *skb, 1260 int batadv_recv_vis_packet(struct sk_buff *skb,
1190 struct batadv_hard_iface *recv_if) 1261 struct batadv_hard_iface *recv_if)
1191 { 1262 {
1192 struct batadv_vis_packet *vis_packet; 1263 struct batadv_vis_packet *vis_packet;
1193 struct ethhdr *ethhdr; 1264 struct ethhdr *ethhdr;
1194 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface); 1265 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1195 int hdr_size = sizeof(*vis_packet); 1266 int hdr_size = sizeof(*vis_packet);
1196 1267
1197 /* keep skb linear */ 1268 /* keep skb linear */
1198 if (skb_linearize(skb) < 0) 1269 if (skb_linearize(skb) < 0)
1199 return NET_RX_DROP; 1270 return NET_RX_DROP;
1200 1271
1201 if (unlikely(!pskb_may_pull(skb, hdr_size))) 1272 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1202 return NET_RX_DROP; 1273 return NET_RX_DROP;
1203 1274
1204 vis_packet = (struct batadv_vis_packet *)skb->data; 1275 vis_packet = (struct batadv_vis_packet *)skb->data;
1205 ethhdr = (struct ethhdr *)skb_mac_header(skb); 1276 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1206 1277
1207 /* not for me */ 1278 /* not for me */
1208 if (!batadv_is_my_mac(ethhdr->h_dest)) 1279 if (!batadv_is_my_mac(ethhdr->h_dest))
1209 return NET_RX_DROP; 1280 return NET_RX_DROP;
1210 1281
net/batman-adv/soft-interface.c
1 /* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors: 1 /* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
2 * 2 *
3 * Marek Lindner, Simon Wunderlich 3 * Marek Lindner, Simon Wunderlich
4 * 4 *
5 * This program is free software; you can redistribute it and/or 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 6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation. 7 * License as published by the Free Software Foundation.
8 * 8 *
9 * This program is distributed in the hope that it will be useful, but 9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of 10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details. 12 * General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 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 15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA 17 * 02110-1301, USA
18 */ 18 */
19 19
20 #include "main.h" 20 #include "main.h"
21 #include "soft-interface.h" 21 #include "soft-interface.h"
22 #include "hard-interface.h" 22 #include "hard-interface.h"
23 #include "distributed-arp-table.h" 23 #include "distributed-arp-table.h"
24 #include "routing.h" 24 #include "routing.h"
25 #include "send.h" 25 #include "send.h"
26 #include "debugfs.h" 26 #include "debugfs.h"
27 #include "translation-table.h" 27 #include "translation-table.h"
28 #include "hash.h" 28 #include "hash.h"
29 #include "gateway_common.h" 29 #include "gateway_common.h"
30 #include "gateway_client.h" 30 #include "gateway_client.h"
31 #include "sysfs.h" 31 #include "sysfs.h"
32 #include "originator.h" 32 #include "originator.h"
33 #include <linux/slab.h> 33 #include <linux/slab.h>
34 #include <linux/ethtool.h> 34 #include <linux/ethtool.h>
35 #include <linux/etherdevice.h> 35 #include <linux/etherdevice.h>
36 #include <linux/if_vlan.h> 36 #include <linux/if_vlan.h>
37 #include "unicast.h" 37 #include "unicast.h"
38 #include "bridge_loop_avoidance.h" 38 #include "bridge_loop_avoidance.h"
39 39
40 40
41 static int batadv_get_settings(struct net_device *dev, struct ethtool_cmd *cmd); 41 static int batadv_get_settings(struct net_device *dev, struct ethtool_cmd *cmd);
42 static void batadv_get_drvinfo(struct net_device *dev, 42 static void batadv_get_drvinfo(struct net_device *dev,
43 struct ethtool_drvinfo *info); 43 struct ethtool_drvinfo *info);
44 static u32 batadv_get_msglevel(struct net_device *dev); 44 static u32 batadv_get_msglevel(struct net_device *dev);
45 static void batadv_set_msglevel(struct net_device *dev, u32 value); 45 static void batadv_set_msglevel(struct net_device *dev, u32 value);
46 static u32 batadv_get_link(struct net_device *dev); 46 static u32 batadv_get_link(struct net_device *dev);
47 static void batadv_get_strings(struct net_device *dev, u32 stringset, u8 *data); 47 static void batadv_get_strings(struct net_device *dev, u32 stringset, u8 *data);
48 static void batadv_get_ethtool_stats(struct net_device *dev, 48 static void batadv_get_ethtool_stats(struct net_device *dev,
49 struct ethtool_stats *stats, u64 *data); 49 struct ethtool_stats *stats, u64 *data);
50 static int batadv_get_sset_count(struct net_device *dev, int stringset); 50 static int batadv_get_sset_count(struct net_device *dev, int stringset);
51 51
52 static const struct ethtool_ops batadv_ethtool_ops = { 52 static const struct ethtool_ops batadv_ethtool_ops = {
53 .get_settings = batadv_get_settings, 53 .get_settings = batadv_get_settings,
54 .get_drvinfo = batadv_get_drvinfo, 54 .get_drvinfo = batadv_get_drvinfo,
55 .get_msglevel = batadv_get_msglevel, 55 .get_msglevel = batadv_get_msglevel,
56 .set_msglevel = batadv_set_msglevel, 56 .set_msglevel = batadv_set_msglevel,
57 .get_link = batadv_get_link, 57 .get_link = batadv_get_link,
58 .get_strings = batadv_get_strings, 58 .get_strings = batadv_get_strings,
59 .get_ethtool_stats = batadv_get_ethtool_stats, 59 .get_ethtool_stats = batadv_get_ethtool_stats,
60 .get_sset_count = batadv_get_sset_count, 60 .get_sset_count = batadv_get_sset_count,
61 }; 61 };
62 62
63 int batadv_skb_head_push(struct sk_buff *skb, unsigned int len) 63 int batadv_skb_head_push(struct sk_buff *skb, unsigned int len)
64 { 64 {
65 int result; 65 int result;
66 66
67 /* TODO: We must check if we can release all references to non-payload 67 /* TODO: We must check if we can release all references to non-payload
68 * data using skb_header_release in our skbs to allow skb_cow_header to 68 * data using skb_header_release in our skbs to allow skb_cow_header to
69 * work optimally. This means that those skbs are not allowed to read 69 * work optimally. This means that those skbs are not allowed to read
70 * or write any data which is before the current position of skb->data 70 * or write any data which is before the current position of skb->data
71 * after that call and thus allow other skbs with the same data buffer 71 * after that call and thus allow other skbs with the same data buffer
72 * to write freely in that area. 72 * to write freely in that area.
73 */ 73 */
74 result = skb_cow_head(skb, len); 74 result = skb_cow_head(skb, len);
75 if (result < 0) 75 if (result < 0)
76 return result; 76 return result;
77 77
78 skb_push(skb, len); 78 skb_push(skb, len);
79 return 0; 79 return 0;
80 } 80 }
81 81
82 static int batadv_interface_open(struct net_device *dev) 82 static int batadv_interface_open(struct net_device *dev)
83 { 83 {
84 netif_start_queue(dev); 84 netif_start_queue(dev);
85 return 0; 85 return 0;
86 } 86 }
87 87
88 static int batadv_interface_release(struct net_device *dev) 88 static int batadv_interface_release(struct net_device *dev)
89 { 89 {
90 netif_stop_queue(dev); 90 netif_stop_queue(dev);
91 return 0; 91 return 0;
92 } 92 }
93 93
94 static struct net_device_stats *batadv_interface_stats(struct net_device *dev) 94 static struct net_device_stats *batadv_interface_stats(struct net_device *dev)
95 { 95 {
96 struct batadv_priv *bat_priv = netdev_priv(dev); 96 struct batadv_priv *bat_priv = netdev_priv(dev);
97 struct net_device_stats *stats = &bat_priv->stats; 97 struct net_device_stats *stats = &bat_priv->stats;
98 98
99 stats->tx_packets = batadv_sum_counter(bat_priv, BATADV_CNT_TX); 99 stats->tx_packets = batadv_sum_counter(bat_priv, BATADV_CNT_TX);
100 stats->tx_bytes = batadv_sum_counter(bat_priv, BATADV_CNT_TX_BYTES); 100 stats->tx_bytes = batadv_sum_counter(bat_priv, BATADV_CNT_TX_BYTES);
101 stats->tx_dropped = batadv_sum_counter(bat_priv, BATADV_CNT_TX_DROPPED); 101 stats->tx_dropped = batadv_sum_counter(bat_priv, BATADV_CNT_TX_DROPPED);
102 stats->rx_packets = batadv_sum_counter(bat_priv, BATADV_CNT_RX); 102 stats->rx_packets = batadv_sum_counter(bat_priv, BATADV_CNT_RX);
103 stats->rx_bytes = batadv_sum_counter(bat_priv, BATADV_CNT_RX_BYTES); 103 stats->rx_bytes = batadv_sum_counter(bat_priv, BATADV_CNT_RX_BYTES);
104 return stats; 104 return stats;
105 } 105 }
106 106
107 static int batadv_interface_set_mac_addr(struct net_device *dev, void *p) 107 static int batadv_interface_set_mac_addr(struct net_device *dev, void *p)
108 { 108 {
109 struct batadv_priv *bat_priv = netdev_priv(dev); 109 struct batadv_priv *bat_priv = netdev_priv(dev);
110 struct sockaddr *addr = p; 110 struct sockaddr *addr = p;
111 uint8_t old_addr[ETH_ALEN]; 111 uint8_t old_addr[ETH_ALEN];
112 112
113 if (!is_valid_ether_addr(addr->sa_data)) 113 if (!is_valid_ether_addr(addr->sa_data))
114 return -EADDRNOTAVAIL; 114 return -EADDRNOTAVAIL;
115 115
116 memcpy(old_addr, dev->dev_addr, ETH_ALEN); 116 memcpy(old_addr, dev->dev_addr, ETH_ALEN);
117 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN); 117 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
118 118
119 /* only modify transtable if it has been initialized before */ 119 /* only modify transtable if it has been initialized before */
120 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE) { 120 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE) {
121 batadv_tt_local_remove(bat_priv, old_addr, 121 batadv_tt_local_remove(bat_priv, old_addr,
122 "mac address changed", false); 122 "mac address changed", false);
123 batadv_tt_local_add(dev, addr->sa_data, BATADV_NULL_IFINDEX); 123 batadv_tt_local_add(dev, addr->sa_data, BATADV_NULL_IFINDEX);
124 } 124 }
125 125
126 dev->addr_assign_type &= ~NET_ADDR_RANDOM; 126 dev->addr_assign_type &= ~NET_ADDR_RANDOM;
127 return 0; 127 return 0;
128 } 128 }
129 129
130 static int batadv_interface_change_mtu(struct net_device *dev, int new_mtu) 130 static int batadv_interface_change_mtu(struct net_device *dev, int new_mtu)
131 { 131 {
132 /* check ranges */ 132 /* check ranges */
133 if ((new_mtu < 68) || (new_mtu > batadv_hardif_min_mtu(dev))) 133 if ((new_mtu < 68) || (new_mtu > batadv_hardif_min_mtu(dev)))
134 return -EINVAL; 134 return -EINVAL;
135 135
136 dev->mtu = new_mtu; 136 dev->mtu = new_mtu;
137 137
138 return 0; 138 return 0;
139 } 139 }
140 140
141 static int batadv_interface_tx(struct sk_buff *skb, 141 static int batadv_interface_tx(struct sk_buff *skb,
142 struct net_device *soft_iface) 142 struct net_device *soft_iface)
143 { 143 {
144 struct ethhdr *ethhdr = (struct ethhdr *)skb->data; 144 struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
145 struct batadv_priv *bat_priv = netdev_priv(soft_iface); 145 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
146 struct batadv_hard_iface *primary_if = NULL; 146 struct batadv_hard_iface *primary_if = NULL;
147 struct batadv_bcast_packet *bcast_packet; 147 struct batadv_bcast_packet *bcast_packet;
148 struct vlan_ethhdr *vhdr; 148 struct vlan_ethhdr *vhdr;
149 __be16 ethertype = __constant_htons(BATADV_ETH_P_BATMAN); 149 __be16 ethertype = __constant_htons(BATADV_ETH_P_BATMAN);
150 static const uint8_t stp_addr[ETH_ALEN] = {0x01, 0x80, 0xC2, 0x00, 150 static const uint8_t stp_addr[ETH_ALEN] = {0x01, 0x80, 0xC2, 0x00,
151 0x00, 0x00}; 151 0x00, 0x00};
152 static const uint8_t ectp_addr[ETH_ALEN] = {0xCF, 0x00, 0x00, 0x00, 152 static const uint8_t ectp_addr[ETH_ALEN] = {0xCF, 0x00, 0x00, 0x00,
153 0x00, 0x00}; 153 0x00, 0x00};
154 unsigned int header_len = 0; 154 unsigned int header_len = 0;
155 int data_len = skb->len, ret; 155 int data_len = skb->len, ret;
156 short vid __maybe_unused = -1; 156 short vid __maybe_unused = -1;
157 bool do_bcast = false; 157 bool do_bcast = false;
158 uint32_t seqno; 158 uint32_t seqno;
159 unsigned long brd_delay = 1; 159 unsigned long brd_delay = 1;
160 160
161 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE) 161 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
162 goto dropped; 162 goto dropped;
163 163
164 soft_iface->trans_start = jiffies; 164 soft_iface->trans_start = jiffies;
165 165
166 switch (ntohs(ethhdr->h_proto)) { 166 switch (ntohs(ethhdr->h_proto)) {
167 case ETH_P_8021Q: 167 case ETH_P_8021Q:
168 vhdr = (struct vlan_ethhdr *)skb->data; 168 vhdr = (struct vlan_ethhdr *)skb->data;
169 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK; 169 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
170 170
171 if (vhdr->h_vlan_encapsulated_proto != ethertype) 171 if (vhdr->h_vlan_encapsulated_proto != ethertype)
172 break; 172 break;
173 173
174 /* fall through */ 174 /* fall through */
175 case BATADV_ETH_P_BATMAN: 175 case BATADV_ETH_P_BATMAN:
176 goto dropped; 176 goto dropped;
177 } 177 }
178 178
179 if (batadv_bla_tx(bat_priv, skb, vid)) 179 if (batadv_bla_tx(bat_priv, skb, vid))
180 goto dropped; 180 goto dropped;
181 181
182 /* Register the client MAC in the transtable */ 182 /* Register the client MAC in the transtable */
183 batadv_tt_local_add(soft_iface, ethhdr->h_source, skb->skb_iif); 183 batadv_tt_local_add(soft_iface, ethhdr->h_source, skb->skb_iif);
184 184
185 /* don't accept stp packets. STP does not help in meshes. 185 /* don't accept stp packets. STP does not help in meshes.
186 * better use the bridge loop avoidance ... 186 * better use the bridge loop avoidance ...
187 * 187 *
188 * The same goes for ECTP sent at least by some Cisco Switches, 188 * The same goes for ECTP sent at least by some Cisco Switches,
189 * it might confuse the mesh when used with bridge loop avoidance. 189 * it might confuse the mesh when used with bridge loop avoidance.
190 */ 190 */
191 if (batadv_compare_eth(ethhdr->h_dest, stp_addr)) 191 if (batadv_compare_eth(ethhdr->h_dest, stp_addr))
192 goto dropped; 192 goto dropped;
193 193
194 if (batadv_compare_eth(ethhdr->h_dest, ectp_addr)) 194 if (batadv_compare_eth(ethhdr->h_dest, ectp_addr))
195 goto dropped; 195 goto dropped;
196 196
197 if (is_multicast_ether_addr(ethhdr->h_dest)) { 197 if (is_multicast_ether_addr(ethhdr->h_dest)) {
198 do_bcast = true; 198 do_bcast = true;
199 199
200 switch (atomic_read(&bat_priv->gw_mode)) { 200 switch (atomic_read(&bat_priv->gw_mode)) {
201 case BATADV_GW_MODE_SERVER: 201 case BATADV_GW_MODE_SERVER:
202 /* gateway servers should not send dhcp 202 /* gateway servers should not send dhcp
203 * requests into the mesh 203 * requests into the mesh
204 */ 204 */
205 ret = batadv_gw_is_dhcp_target(skb, &header_len); 205 ret = batadv_gw_is_dhcp_target(skb, &header_len);
206 if (ret) 206 if (ret)
207 goto dropped; 207 goto dropped;
208 break; 208 break;
209 case BATADV_GW_MODE_CLIENT: 209 case BATADV_GW_MODE_CLIENT:
210 /* gateway clients should send dhcp requests 210 /* gateway clients should send dhcp requests
211 * via unicast to their gateway 211 * via unicast to their gateway
212 */ 212 */
213 ret = batadv_gw_is_dhcp_target(skb, &header_len); 213 ret = batadv_gw_is_dhcp_target(skb, &header_len);
214 if (ret) 214 if (ret)
215 do_bcast = false; 215 do_bcast = false;
216 break; 216 break;
217 case BATADV_GW_MODE_OFF: 217 case BATADV_GW_MODE_OFF:
218 default: 218 default:
219 break; 219 break;
220 } 220 }
221 } 221 }
222 222
223 /* ethernet packet should be broadcasted */ 223 /* ethernet packet should be broadcasted */
224 if (do_bcast) { 224 if (do_bcast) {
225 primary_if = batadv_primary_if_get_selected(bat_priv); 225 primary_if = batadv_primary_if_get_selected(bat_priv);
226 if (!primary_if) 226 if (!primary_if)
227 goto dropped; 227 goto dropped;
228 228
229 /* in case of ARP request, we do not immediately broadcasti the 229 /* in case of ARP request, we do not immediately broadcasti the
230 * packet, instead we first wait for DAT to try to retrieve the 230 * packet, instead we first wait for DAT to try to retrieve the
231 * correct ARP entry 231 * correct ARP entry
232 */ 232 */
233 if (batadv_dat_snoop_outgoing_arp_request(bat_priv, skb)) 233 if (batadv_dat_snoop_outgoing_arp_request(bat_priv, skb))
234 brd_delay = msecs_to_jiffies(ARP_REQ_DELAY); 234 brd_delay = msecs_to_jiffies(ARP_REQ_DELAY);
235 235
236 if (batadv_skb_head_push(skb, sizeof(*bcast_packet)) < 0) 236 if (batadv_skb_head_push(skb, sizeof(*bcast_packet)) < 0)
237 goto dropped; 237 goto dropped;
238 238
239 bcast_packet = (struct batadv_bcast_packet *)skb->data; 239 bcast_packet = (struct batadv_bcast_packet *)skb->data;
240 bcast_packet->header.version = BATADV_COMPAT_VERSION; 240 bcast_packet->header.version = BATADV_COMPAT_VERSION;
241 bcast_packet->header.ttl = BATADV_TTL; 241 bcast_packet->header.ttl = BATADV_TTL;
242 242
243 /* batman packet type: broadcast */ 243 /* batman packet type: broadcast */
244 bcast_packet->header.packet_type = BATADV_BCAST; 244 bcast_packet->header.packet_type = BATADV_BCAST;
245 bcast_packet->reserved = 0; 245 bcast_packet->reserved = 0;
246 246
247 /* hw address of first interface is the orig mac because only 247 /* hw address of first interface is the orig mac because only
248 * this mac is known throughout the mesh 248 * this mac is known throughout the mesh
249 */ 249 */
250 memcpy(bcast_packet->orig, 250 memcpy(bcast_packet->orig,
251 primary_if->net_dev->dev_addr, ETH_ALEN); 251 primary_if->net_dev->dev_addr, ETH_ALEN);
252 252
253 /* set broadcast sequence number */ 253 /* set broadcast sequence number */
254 seqno = atomic_inc_return(&bat_priv->bcast_seqno); 254 seqno = atomic_inc_return(&bat_priv->bcast_seqno);
255 bcast_packet->seqno = htonl(seqno); 255 bcast_packet->seqno = htonl(seqno);
256 256
257 batadv_add_bcast_packet_to_list(bat_priv, skb, brd_delay); 257 batadv_add_bcast_packet_to_list(bat_priv, skb, brd_delay);
258 258
259 /* a copy is stored in the bcast list, therefore removing 259 /* a copy is stored in the bcast list, therefore removing
260 * the original skb. 260 * the original skb.
261 */ 261 */
262 kfree_skb(skb); 262 kfree_skb(skb);
263 263
264 /* unicast packet */ 264 /* unicast packet */
265 } else { 265 } else {
266 if (atomic_read(&bat_priv->gw_mode) != BATADV_GW_MODE_OFF) { 266 if (atomic_read(&bat_priv->gw_mode) != BATADV_GW_MODE_OFF) {
267 ret = batadv_gw_out_of_range(bat_priv, skb, ethhdr); 267 ret = batadv_gw_out_of_range(bat_priv, skb, ethhdr);
268 if (ret) 268 if (ret)
269 goto dropped; 269 goto dropped;
270 } 270 }
271 271
272 if (batadv_dat_snoop_outgoing_arp_request(bat_priv, skb)) 272 if (batadv_dat_snoop_outgoing_arp_request(bat_priv, skb))
273 goto dropped; 273 goto dropped;
274 274
275 batadv_dat_snoop_outgoing_arp_reply(bat_priv, skb); 275 batadv_dat_snoop_outgoing_arp_reply(bat_priv, skb);
276 276
277 ret = batadv_unicast_send_skb(bat_priv, skb); 277 ret = batadv_unicast_send_skb(bat_priv, skb);
278 if (ret != 0) 278 if (ret != 0)
279 goto dropped_freed; 279 goto dropped_freed;
280 } 280 }
281 281
282 batadv_inc_counter(bat_priv, BATADV_CNT_TX); 282 batadv_inc_counter(bat_priv, BATADV_CNT_TX);
283 batadv_add_counter(bat_priv, BATADV_CNT_TX_BYTES, data_len); 283 batadv_add_counter(bat_priv, BATADV_CNT_TX_BYTES, data_len);
284 goto end; 284 goto end;
285 285
286 dropped: 286 dropped:
287 kfree_skb(skb); 287 kfree_skb(skb);
288 dropped_freed: 288 dropped_freed:
289 batadv_inc_counter(bat_priv, BATADV_CNT_TX_DROPPED); 289 batadv_inc_counter(bat_priv, BATADV_CNT_TX_DROPPED);
290 end: 290 end:
291 if (primary_if) 291 if (primary_if)
292 batadv_hardif_free_ref(primary_if); 292 batadv_hardif_free_ref(primary_if);
293 return NETDEV_TX_OK; 293 return NETDEV_TX_OK;
294 } 294 }
295 295
296 void batadv_interface_rx(struct net_device *soft_iface, 296 void batadv_interface_rx(struct net_device *soft_iface,
297 struct sk_buff *skb, struct batadv_hard_iface *recv_if, 297 struct sk_buff *skb, struct batadv_hard_iface *recv_if,
298 int hdr_size, struct batadv_orig_node *orig_node) 298 int hdr_size, struct batadv_orig_node *orig_node)
299 { 299 {
300 struct batadv_priv *bat_priv = netdev_priv(soft_iface); 300 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
301 struct ethhdr *ethhdr; 301 struct ethhdr *ethhdr;
302 struct vlan_ethhdr *vhdr; 302 struct vlan_ethhdr *vhdr;
303 struct batadv_header *batadv_header = (struct batadv_header *)skb->data; 303 struct batadv_header *batadv_header = (struct batadv_header *)skb->data;
304 short vid __maybe_unused = -1; 304 short vid __maybe_unused = -1;
305 __be16 ethertype = __constant_htons(BATADV_ETH_P_BATMAN); 305 __be16 ethertype = __constant_htons(BATADV_ETH_P_BATMAN);
306 bool is_bcast; 306 bool is_bcast;
307 307
308 is_bcast = (batadv_header->packet_type == BATADV_BCAST); 308 is_bcast = (batadv_header->packet_type == BATADV_BCAST);
309 309
310 /* check if enough space is available for pulling, and pull */ 310 /* check if enough space is available for pulling, and pull */
311 if (!pskb_may_pull(skb, hdr_size)) 311 if (!pskb_may_pull(skb, hdr_size))
312 goto dropped; 312 goto dropped;
313 313
314 skb_pull_rcsum(skb, hdr_size); 314 skb_pull_rcsum(skb, hdr_size);
315 skb_reset_mac_header(skb); 315 skb_reset_mac_header(skb);
316 316
317 ethhdr = (struct ethhdr *)skb_mac_header(skb); 317 ethhdr = (struct ethhdr *)skb_mac_header(skb);
318 318
319 switch (ntohs(ethhdr->h_proto)) { 319 switch (ntohs(ethhdr->h_proto)) {
320 case ETH_P_8021Q: 320 case ETH_P_8021Q:
321 vhdr = (struct vlan_ethhdr *)skb->data; 321 vhdr = (struct vlan_ethhdr *)skb->data;
322 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK; 322 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
323 323
324 if (vhdr->h_vlan_encapsulated_proto != ethertype) 324 if (vhdr->h_vlan_encapsulated_proto != ethertype)
325 break; 325 break;
326 326
327 /* fall through */ 327 /* fall through */
328 case BATADV_ETH_P_BATMAN: 328 case BATADV_ETH_P_BATMAN:
329 goto dropped; 329 goto dropped;
330 } 330 }
331 331
332 /* skb->dev & skb->pkt_type are set here */ 332 /* skb->dev & skb->pkt_type are set here */
333 if (unlikely(!pskb_may_pull(skb, ETH_HLEN))) 333 if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
334 goto dropped; 334 goto dropped;
335 skb->protocol = eth_type_trans(skb, soft_iface); 335 skb->protocol = eth_type_trans(skb, soft_iface);
336 336
337 /* should not be necessary anymore as we use skb_pull_rcsum() 337 /* should not be necessary anymore as we use skb_pull_rcsum()
338 * TODO: please verify this and remove this TODO 338 * TODO: please verify this and remove this TODO
339 * -- Dec 21st 2009, Simon Wunderlich 339 * -- Dec 21st 2009, Simon Wunderlich
340 */ 340 */
341 341
342 /* skb->ip_summed = CHECKSUM_UNNECESSARY; */ 342 /* skb->ip_summed = CHECKSUM_UNNECESSARY; */
343 343
344 batadv_inc_counter(bat_priv, BATADV_CNT_RX); 344 batadv_inc_counter(bat_priv, BATADV_CNT_RX);
345 batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES, 345 batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
346 skb->len + ETH_HLEN); 346 skb->len + ETH_HLEN);
347 347
348 soft_iface->last_rx = jiffies; 348 soft_iface->last_rx = jiffies;
349 349
350 if (orig_node) 350 if (orig_node)
351 batadv_tt_add_temporary_global_entry(bat_priv, orig_node, 351 batadv_tt_add_temporary_global_entry(bat_priv, orig_node,
352 ethhdr->h_source); 352 ethhdr->h_source);
353 353
354 if (batadv_is_ap_isolated(bat_priv, ethhdr->h_source, ethhdr->h_dest)) 354 if (batadv_is_ap_isolated(bat_priv, ethhdr->h_source, ethhdr->h_dest))
355 goto dropped; 355 goto dropped;
356 356
357 /* Let the bridge loop avoidance check the packet. If will 357 /* Let the bridge loop avoidance check the packet. If will
358 * not handle it, we can safely push it up. 358 * not handle it, we can safely push it up.
359 */ 359 */
360 if (batadv_bla_rx(bat_priv, skb, vid, is_bcast)) 360 if (batadv_bla_rx(bat_priv, skb, vid, is_bcast))
361 goto out; 361 goto out;
362 362
363 netif_rx(skb); 363 netif_rx(skb);
364 goto out; 364 goto out;
365 365
366 dropped: 366 dropped:
367 kfree_skb(skb); 367 kfree_skb(skb);
368 out: 368 out:
369 return; 369 return;
370 } 370 }
371 371
372 /* batman-adv network devices have devices nesting below it and are a special 372 /* batman-adv network devices have devices nesting below it and are a special
373 * "super class" of normal network devices; split their locks off into a 373 * "super class" of normal network devices; split their locks off into a
374 * separate class since they always nest. 374 * separate class since they always nest.
375 */ 375 */
376 static struct lock_class_key batadv_netdev_xmit_lock_key; 376 static struct lock_class_key batadv_netdev_xmit_lock_key;
377 static struct lock_class_key batadv_netdev_addr_lock_key; 377 static struct lock_class_key batadv_netdev_addr_lock_key;
378 378
379 /** 379 /**
380 * batadv_set_lockdep_class_one - Set lockdep class for a single tx queue 380 * batadv_set_lockdep_class_one - Set lockdep class for a single tx queue
381 * @dev: device which owns the tx queue 381 * @dev: device which owns the tx queue
382 * @txq: tx queue to modify 382 * @txq: tx queue to modify
383 * @_unused: always NULL 383 * @_unused: always NULL
384 */ 384 */
385 static void batadv_set_lockdep_class_one(struct net_device *dev, 385 static void batadv_set_lockdep_class_one(struct net_device *dev,
386 struct netdev_queue *txq, 386 struct netdev_queue *txq,
387 void *_unused) 387 void *_unused)
388 { 388 {
389 lockdep_set_class(&txq->_xmit_lock, &batadv_netdev_xmit_lock_key); 389 lockdep_set_class(&txq->_xmit_lock, &batadv_netdev_xmit_lock_key);
390 } 390 }
391 391
392 /** 392 /**
393 * batadv_set_lockdep_class - Set txq and addr_list lockdep class 393 * batadv_set_lockdep_class - Set txq and addr_list lockdep class
394 * @dev: network device to modify 394 * @dev: network device to modify
395 */ 395 */
396 static void batadv_set_lockdep_class(struct net_device *dev) 396 static void batadv_set_lockdep_class(struct net_device *dev)
397 { 397 {
398 lockdep_set_class(&dev->addr_list_lock, &batadv_netdev_addr_lock_key); 398 lockdep_set_class(&dev->addr_list_lock, &batadv_netdev_addr_lock_key);
399 netdev_for_each_tx_queue(dev, batadv_set_lockdep_class_one, NULL); 399 netdev_for_each_tx_queue(dev, batadv_set_lockdep_class_one, NULL);
400 } 400 }
401 401
402 /** 402 /**
403 * batadv_softif_init - Late stage initialization of soft interface 403 * batadv_softif_init - Late stage initialization of soft interface
404 * @dev: registered network device to modify 404 * @dev: registered network device to modify
405 * 405 *
406 * Returns error code on failures 406 * Returns error code on failures
407 */ 407 */
408 static int batadv_softif_init(struct net_device *dev) 408 static int batadv_softif_init(struct net_device *dev)
409 { 409 {
410 batadv_set_lockdep_class(dev); 410 batadv_set_lockdep_class(dev);
411 411
412 return 0; 412 return 0;
413 } 413 }
414 414
415 static const struct net_device_ops batadv_netdev_ops = { 415 static const struct net_device_ops batadv_netdev_ops = {
416 .ndo_init = batadv_softif_init, 416 .ndo_init = batadv_softif_init,
417 .ndo_open = batadv_interface_open, 417 .ndo_open = batadv_interface_open,
418 .ndo_stop = batadv_interface_release, 418 .ndo_stop = batadv_interface_release,
419 .ndo_get_stats = batadv_interface_stats, 419 .ndo_get_stats = batadv_interface_stats,
420 .ndo_set_mac_address = batadv_interface_set_mac_addr, 420 .ndo_set_mac_address = batadv_interface_set_mac_addr,
421 .ndo_change_mtu = batadv_interface_change_mtu, 421 .ndo_change_mtu = batadv_interface_change_mtu,
422 .ndo_start_xmit = batadv_interface_tx, 422 .ndo_start_xmit = batadv_interface_tx,
423 .ndo_validate_addr = eth_validate_addr 423 .ndo_validate_addr = eth_validate_addr
424 }; 424 };
425 425
426 static void batadv_interface_setup(struct net_device *dev) 426 static void batadv_interface_setup(struct net_device *dev)
427 { 427 {
428 struct batadv_priv *priv = netdev_priv(dev); 428 struct batadv_priv *priv = netdev_priv(dev);
429 429
430 ether_setup(dev); 430 ether_setup(dev);
431 431
432 dev->netdev_ops = &batadv_netdev_ops; 432 dev->netdev_ops = &batadv_netdev_ops;
433 dev->destructor = free_netdev; 433 dev->destructor = free_netdev;
434 dev->tx_queue_len = 0; 434 dev->tx_queue_len = 0;
435 435
436 /* can't call min_mtu, because the needed variables 436 /* can't call min_mtu, because the needed variables
437 * have not been initialized yet 437 * have not been initialized yet
438 */ 438 */
439 dev->mtu = ETH_DATA_LEN; 439 dev->mtu = ETH_DATA_LEN;
440 /* reserve more space in the skbuff for our header */ 440 /* reserve more space in the skbuff for our header */
441 dev->hard_header_len = BATADV_HEADER_LEN; 441 dev->hard_header_len = BATADV_HEADER_LEN;
442 442
443 /* generate random address */ 443 /* generate random address */
444 eth_hw_addr_random(dev); 444 eth_hw_addr_random(dev);
445 445
446 SET_ETHTOOL_OPS(dev, &batadv_ethtool_ops); 446 SET_ETHTOOL_OPS(dev, &batadv_ethtool_ops);
447 447
448 memset(priv, 0, sizeof(*priv)); 448 memset(priv, 0, sizeof(*priv));
449 } 449 }
450 450
451 struct net_device *batadv_softif_create(const char *name) 451 struct net_device *batadv_softif_create(const char *name)
452 { 452 {
453 struct net_device *soft_iface; 453 struct net_device *soft_iface;
454 struct batadv_priv *bat_priv; 454 struct batadv_priv *bat_priv;
455 int ret; 455 int ret;
456 size_t cnt_len = sizeof(uint64_t) * BATADV_CNT_NUM; 456 size_t cnt_len = sizeof(uint64_t) * BATADV_CNT_NUM;
457 457
458 soft_iface = alloc_netdev(sizeof(*bat_priv), name, 458 soft_iface = alloc_netdev(sizeof(*bat_priv), name,
459 batadv_interface_setup); 459 batadv_interface_setup);
460 460
461 if (!soft_iface) 461 if (!soft_iface)
462 goto out; 462 goto out;
463 463
464 bat_priv = netdev_priv(soft_iface); 464 bat_priv = netdev_priv(soft_iface);
465 465
466 /* batadv_interface_stats() needs to be available as soon as 466 /* batadv_interface_stats() needs to be available as soon as
467 * register_netdevice() has been called 467 * register_netdevice() has been called
468 */ 468 */
469 bat_priv->bat_counters = __alloc_percpu(cnt_len, __alignof__(uint64_t)); 469 bat_priv->bat_counters = __alloc_percpu(cnt_len, __alignof__(uint64_t));
470 if (!bat_priv->bat_counters) 470 if (!bat_priv->bat_counters)
471 goto free_soft_iface; 471 goto free_soft_iface;
472 472
473 ret = register_netdevice(soft_iface); 473 ret = register_netdevice(soft_iface);
474 if (ret < 0) { 474 if (ret < 0) {
475 pr_err("Unable to register the batman interface '%s': %i\n", 475 pr_err("Unable to register the batman interface '%s': %i\n",
476 name, ret); 476 name, ret);
477 goto free_bat_counters; 477 goto free_bat_counters;
478 } 478 }
479 479
480 atomic_set(&bat_priv->aggregated_ogms, 1); 480 atomic_set(&bat_priv->aggregated_ogms, 1);
481 atomic_set(&bat_priv->bonding, 0); 481 atomic_set(&bat_priv->bonding, 0);
482 atomic_set(&bat_priv->bridge_loop_avoidance, 0); 482 atomic_set(&bat_priv->bridge_loop_avoidance, 0);
483 #ifdef CONFIG_BATMAN_ADV_DAT 483 #ifdef CONFIG_BATMAN_ADV_DAT
484 atomic_set(&bat_priv->distributed_arp_table, 1); 484 atomic_set(&bat_priv->distributed_arp_table, 1);
485 #endif 485 #endif
486 atomic_set(&bat_priv->ap_isolation, 0); 486 atomic_set(&bat_priv->ap_isolation, 0);
487 atomic_set(&bat_priv->vis_mode, BATADV_VIS_TYPE_CLIENT_UPDATE); 487 atomic_set(&bat_priv->vis_mode, BATADV_VIS_TYPE_CLIENT_UPDATE);
488 atomic_set(&bat_priv->gw_mode, BATADV_GW_MODE_OFF); 488 atomic_set(&bat_priv->gw_mode, BATADV_GW_MODE_OFF);
489 atomic_set(&bat_priv->gw_sel_class, 20); 489 atomic_set(&bat_priv->gw_sel_class, 20);
490 atomic_set(&bat_priv->gw_bandwidth, 41); 490 atomic_set(&bat_priv->gw_bandwidth, 41);
491 atomic_set(&bat_priv->orig_interval, 1000); 491 atomic_set(&bat_priv->orig_interval, 1000);
492 atomic_set(&bat_priv->hop_penalty, 30); 492 atomic_set(&bat_priv->hop_penalty, 30);
493 atomic_set(&bat_priv->log_level, 0); 493 atomic_set(&bat_priv->log_level, 0);
494 atomic_set(&bat_priv->fragmentation, 1); 494 atomic_set(&bat_priv->fragmentation, 1);
495 atomic_set(&bat_priv->bcast_queue_left, BATADV_BCAST_QUEUE_LEN); 495 atomic_set(&bat_priv->bcast_queue_left, BATADV_BCAST_QUEUE_LEN);
496 atomic_set(&bat_priv->batman_queue_left, BATADV_BATMAN_QUEUE_LEN); 496 atomic_set(&bat_priv->batman_queue_left, BATADV_BATMAN_QUEUE_LEN);
497 497
498 atomic_set(&bat_priv->mesh_state, BATADV_MESH_INACTIVE); 498 atomic_set(&bat_priv->mesh_state, BATADV_MESH_INACTIVE);
499 atomic_set(&bat_priv->bcast_seqno, 1); 499 atomic_set(&bat_priv->bcast_seqno, 1);
500 atomic_set(&bat_priv->tt.vn, 0); 500 atomic_set(&bat_priv->tt.vn, 0);
501 atomic_set(&bat_priv->tt.local_changes, 0); 501 atomic_set(&bat_priv->tt.local_changes, 0);
502 atomic_set(&bat_priv->tt.ogm_append_cnt, 0); 502 atomic_set(&bat_priv->tt.ogm_append_cnt, 0);
503 #ifdef CONFIG_BATMAN_ADV_BLA 503 #ifdef CONFIG_BATMAN_ADV_BLA
504 atomic_set(&bat_priv->bla.num_requests, 0); 504 atomic_set(&bat_priv->bla.num_requests, 0);
505 #endif 505 #endif
506 bat_priv->tt.last_changeset = NULL; 506 bat_priv->tt.last_changeset = NULL;
507 bat_priv->tt.last_changeset_len = 0; 507 bat_priv->tt.last_changeset_len = 0;
508 bat_priv->tt.poss_change = false;
509 508
510 bat_priv->primary_if = NULL; 509 bat_priv->primary_if = NULL;
511 bat_priv->num_ifaces = 0; 510 bat_priv->num_ifaces = 0;
512 511
513 ret = batadv_algo_select(bat_priv, batadv_routing_algo); 512 ret = batadv_algo_select(bat_priv, batadv_routing_algo);
514 if (ret < 0) 513 if (ret < 0)
515 goto unreg_soft_iface; 514 goto unreg_soft_iface;
516 515
517 ret = batadv_sysfs_add_meshif(soft_iface); 516 ret = batadv_sysfs_add_meshif(soft_iface);
518 if (ret < 0) 517 if (ret < 0)
519 goto unreg_soft_iface; 518 goto unreg_soft_iface;
520 519
521 ret = batadv_debugfs_add_meshif(soft_iface); 520 ret = batadv_debugfs_add_meshif(soft_iface);
522 if (ret < 0) 521 if (ret < 0)
523 goto unreg_sysfs; 522 goto unreg_sysfs;
524 523
525 ret = batadv_mesh_init(soft_iface); 524 ret = batadv_mesh_init(soft_iface);
526 if (ret < 0) 525 if (ret < 0)
527 goto unreg_debugfs; 526 goto unreg_debugfs;
528 527
529 return soft_iface; 528 return soft_iface;
530 529
531 unreg_debugfs: 530 unreg_debugfs:
532 batadv_debugfs_del_meshif(soft_iface); 531 batadv_debugfs_del_meshif(soft_iface);
533 unreg_sysfs: 532 unreg_sysfs:
534 batadv_sysfs_del_meshif(soft_iface); 533 batadv_sysfs_del_meshif(soft_iface);
535 unreg_soft_iface: 534 unreg_soft_iface:
536 free_percpu(bat_priv->bat_counters); 535 free_percpu(bat_priv->bat_counters);
537 unregister_netdevice(soft_iface); 536 unregister_netdevice(soft_iface);
538 return NULL; 537 return NULL;
539 538
540 free_bat_counters: 539 free_bat_counters:
541 free_percpu(bat_priv->bat_counters); 540 free_percpu(bat_priv->bat_counters);
542 free_soft_iface: 541 free_soft_iface:
543 free_netdev(soft_iface); 542 free_netdev(soft_iface);
544 out: 543 out:
545 return NULL; 544 return NULL;
546 } 545 }
547 546
548 void batadv_softif_destroy(struct net_device *soft_iface) 547 void batadv_softif_destroy(struct net_device *soft_iface)
549 { 548 {
550 batadv_debugfs_del_meshif(soft_iface); 549 batadv_debugfs_del_meshif(soft_iface);
551 batadv_sysfs_del_meshif(soft_iface); 550 batadv_sysfs_del_meshif(soft_iface);
552 batadv_mesh_free(soft_iface); 551 batadv_mesh_free(soft_iface);
553 unregister_netdevice(soft_iface); 552 unregister_netdevice(soft_iface);
554 } 553 }
555 554
556 int batadv_softif_is_valid(const struct net_device *net_dev) 555 int batadv_softif_is_valid(const struct net_device *net_dev)
557 { 556 {
558 if (net_dev->netdev_ops->ndo_start_xmit == batadv_interface_tx) 557 if (net_dev->netdev_ops->ndo_start_xmit == batadv_interface_tx)
559 return 1; 558 return 1;
560 559
561 return 0; 560 return 0;
562 } 561 }
563 562
564 /* ethtool */ 563 /* ethtool */
565 static int batadv_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) 564 static int batadv_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
566 { 565 {
567 cmd->supported = 0; 566 cmd->supported = 0;
568 cmd->advertising = 0; 567 cmd->advertising = 0;
569 ethtool_cmd_speed_set(cmd, SPEED_10); 568 ethtool_cmd_speed_set(cmd, SPEED_10);
570 cmd->duplex = DUPLEX_FULL; 569 cmd->duplex = DUPLEX_FULL;
571 cmd->port = PORT_TP; 570 cmd->port = PORT_TP;
572 cmd->phy_address = 0; 571 cmd->phy_address = 0;
573 cmd->transceiver = XCVR_INTERNAL; 572 cmd->transceiver = XCVR_INTERNAL;
574 cmd->autoneg = AUTONEG_DISABLE; 573 cmd->autoneg = AUTONEG_DISABLE;
575 cmd->maxtxpkt = 0; 574 cmd->maxtxpkt = 0;
576 cmd->maxrxpkt = 0; 575 cmd->maxrxpkt = 0;
577 576
578 return 0; 577 return 0;
579 } 578 }
580 579
581 static void batadv_get_drvinfo(struct net_device *dev, 580 static void batadv_get_drvinfo(struct net_device *dev,
582 struct ethtool_drvinfo *info) 581 struct ethtool_drvinfo *info)
583 { 582 {
584 strcpy(info->driver, "B.A.T.M.A.N. advanced"); 583 strcpy(info->driver, "B.A.T.M.A.N. advanced");
585 strcpy(info->version, BATADV_SOURCE_VERSION); 584 strcpy(info->version, BATADV_SOURCE_VERSION);
586 strcpy(info->fw_version, "N/A"); 585 strcpy(info->fw_version, "N/A");
587 strcpy(info->bus_info, "batman"); 586 strcpy(info->bus_info, "batman");
588 } 587 }
589 588
590 static u32 batadv_get_msglevel(struct net_device *dev) 589 static u32 batadv_get_msglevel(struct net_device *dev)
591 { 590 {
592 return -EOPNOTSUPP; 591 return -EOPNOTSUPP;
593 } 592 }
594 593
595 static void batadv_set_msglevel(struct net_device *dev, u32 value) 594 static void batadv_set_msglevel(struct net_device *dev, u32 value)
596 { 595 {
597 } 596 }
598 597
599 static u32 batadv_get_link(struct net_device *dev) 598 static u32 batadv_get_link(struct net_device *dev)
600 { 599 {
601 return 1; 600 return 1;
602 } 601 }
603 602
604 /* Inspired by drivers/net/ethernet/dlink/sundance.c:1702 603 /* Inspired by drivers/net/ethernet/dlink/sundance.c:1702
605 * Declare each description string in struct.name[] to get fixed sized buffer 604 * Declare each description string in struct.name[] to get fixed sized buffer
606 * and compile time checking for strings longer than ETH_GSTRING_LEN. 605 * and compile time checking for strings longer than ETH_GSTRING_LEN.
607 */ 606 */
608 static const struct { 607 static const struct {
609 const char name[ETH_GSTRING_LEN]; 608 const char name[ETH_GSTRING_LEN];
610 } batadv_counters_strings[] = { 609 } batadv_counters_strings[] = {
611 { "tx" }, 610 { "tx" },
612 { "tx_bytes" }, 611 { "tx_bytes" },
613 { "tx_dropped" }, 612 { "tx_dropped" },
614 { "rx" }, 613 { "rx" },
615 { "rx_bytes" }, 614 { "rx_bytes" },
616 { "forward" }, 615 { "forward" },
617 { "forward_bytes" }, 616 { "forward_bytes" },
618 { "mgmt_tx" }, 617 { "mgmt_tx" },
619 { "mgmt_tx_bytes" }, 618 { "mgmt_tx_bytes" },
620 { "mgmt_rx" }, 619 { "mgmt_rx" },
621 { "mgmt_rx_bytes" }, 620 { "mgmt_rx_bytes" },
622 { "tt_request_tx" }, 621 { "tt_request_tx" },
623 { "tt_request_rx" }, 622 { "tt_request_rx" },
624 { "tt_response_tx" }, 623 { "tt_response_tx" },
625 { "tt_response_rx" }, 624 { "tt_response_rx" },
626 { "tt_roam_adv_tx" }, 625 { "tt_roam_adv_tx" },
627 { "tt_roam_adv_rx" }, 626 { "tt_roam_adv_rx" },
628 #ifdef CONFIG_BATMAN_ADV_DAT 627 #ifdef CONFIG_BATMAN_ADV_DAT
629 { "dat_get_tx" }, 628 { "dat_get_tx" },
630 { "dat_get_rx" }, 629 { "dat_get_rx" },
631 { "dat_put_tx" }, 630 { "dat_put_tx" },
632 { "dat_put_rx" }, 631 { "dat_put_rx" },
633 { "dat_cached_reply_tx" }, 632 { "dat_cached_reply_tx" },
634 #endif 633 #endif
635 }; 634 };
636 635
637 static void batadv_get_strings(struct net_device *dev, uint32_t stringset, 636 static void batadv_get_strings(struct net_device *dev, uint32_t stringset,
638 uint8_t *data) 637 uint8_t *data)
639 { 638 {
640 if (stringset == ETH_SS_STATS) 639 if (stringset == ETH_SS_STATS)
641 memcpy(data, batadv_counters_strings, 640 memcpy(data, batadv_counters_strings,
642 sizeof(batadv_counters_strings)); 641 sizeof(batadv_counters_strings));
643 } 642 }
644 643
645 static void batadv_get_ethtool_stats(struct net_device *dev, 644 static void batadv_get_ethtool_stats(struct net_device *dev,
646 struct ethtool_stats *stats, 645 struct ethtool_stats *stats,
647 uint64_t *data) 646 uint64_t *data)
648 { 647 {
649 struct batadv_priv *bat_priv = netdev_priv(dev); 648 struct batadv_priv *bat_priv = netdev_priv(dev);
650 int i; 649 int i;
651 650
652 for (i = 0; i < BATADV_CNT_NUM; i++) 651 for (i = 0; i < BATADV_CNT_NUM; i++)
653 data[i] = batadv_sum_counter(bat_priv, i); 652 data[i] = batadv_sum_counter(bat_priv, i);
654 } 653 }
655 654
656 static int batadv_get_sset_count(struct net_device *dev, int stringset) 655 static int batadv_get_sset_count(struct net_device *dev, int stringset)
657 { 656 {
658 if (stringset == ETH_SS_STATS) 657 if (stringset == ETH_SS_STATS)
659 return BATADV_CNT_NUM; 658 return BATADV_CNT_NUM;
660 659
661 return -EOPNOTSUPP; 660 return -EOPNOTSUPP;
662 } 661 }
663 662
net/batman-adv/translation-table.c
1 /* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors: 1 /* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
2 * 2 *
3 * Marek Lindner, Simon Wunderlich, Antonio Quartulli 3 * Marek Lindner, Simon Wunderlich, Antonio Quartulli
4 * 4 *
5 * This program is free software; you can redistribute it and/or 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 6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation. 7 * License as published by the Free Software Foundation.
8 * 8 *
9 * This program is distributed in the hope that it will be useful, but 9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of 10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details. 12 * General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 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 15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA 17 * 02110-1301, USA
18 */ 18 */
19 19
20 #include "main.h" 20 #include "main.h"
21 #include "translation-table.h" 21 #include "translation-table.h"
22 #include "soft-interface.h" 22 #include "soft-interface.h"
23 #include "hard-interface.h" 23 #include "hard-interface.h"
24 #include "send.h" 24 #include "send.h"
25 #include "hash.h" 25 #include "hash.h"
26 #include "originator.h" 26 #include "originator.h"
27 #include "routing.h" 27 #include "routing.h"
28 #include "bridge_loop_avoidance.h" 28 #include "bridge_loop_avoidance.h"
29 29
30 #include <linux/crc16.h> 30 #include <linux/crc16.h>
31 31
32 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client, 32 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
33 struct batadv_orig_node *orig_node); 33 struct batadv_orig_node *orig_node);
34 static void batadv_tt_purge(struct work_struct *work); 34 static void batadv_tt_purge(struct work_struct *work);
35 static void 35 static void
36 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry); 36 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
37 static void batadv_tt_global_del(struct batadv_priv *bat_priv, 37 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
38 struct batadv_orig_node *orig_node, 38 struct batadv_orig_node *orig_node,
39 const unsigned char *addr, 39 const unsigned char *addr,
40 const char *message, bool roaming); 40 const char *message, bool roaming);
41 41
42 /* returns 1 if they are the same mac addr */ 42 /* returns 1 if they are the same mac addr */
43 static int batadv_compare_tt(const struct hlist_node *node, const void *data2) 43 static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
44 { 44 {
45 const void *data1 = container_of(node, struct batadv_tt_common_entry, 45 const void *data1 = container_of(node, struct batadv_tt_common_entry,
46 hash_entry); 46 hash_entry);
47 47
48 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0); 48 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
49 } 49 }
50 50
51 static void batadv_tt_start_timer(struct batadv_priv *bat_priv) 51 static void batadv_tt_start_timer(struct batadv_priv *bat_priv)
52 { 52 {
53 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge); 53 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
54 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work, 54 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
55 msecs_to_jiffies(5000)); 55 msecs_to_jiffies(5000));
56 } 56 }
57 57
58 static struct batadv_tt_common_entry * 58 static struct batadv_tt_common_entry *
59 batadv_tt_hash_find(struct batadv_hashtable *hash, const void *data) 59 batadv_tt_hash_find(struct batadv_hashtable *hash, const void *data)
60 { 60 {
61 struct hlist_head *head; 61 struct hlist_head *head;
62 struct hlist_node *node; 62 struct hlist_node *node;
63 struct batadv_tt_common_entry *tt_common_entry; 63 struct batadv_tt_common_entry *tt_common_entry;
64 struct batadv_tt_common_entry *tt_common_entry_tmp = NULL; 64 struct batadv_tt_common_entry *tt_common_entry_tmp = NULL;
65 uint32_t index; 65 uint32_t index;
66 66
67 if (!hash) 67 if (!hash)
68 return NULL; 68 return NULL;
69 69
70 index = batadv_choose_orig(data, hash->size); 70 index = batadv_choose_orig(data, hash->size);
71 head = &hash->table[index]; 71 head = &hash->table[index];
72 72
73 rcu_read_lock(); 73 rcu_read_lock();
74 hlist_for_each_entry_rcu(tt_common_entry, node, head, hash_entry) { 74 hlist_for_each_entry_rcu(tt_common_entry, node, head, hash_entry) {
75 if (!batadv_compare_eth(tt_common_entry, data)) 75 if (!batadv_compare_eth(tt_common_entry, data))
76 continue; 76 continue;
77 77
78 if (!atomic_inc_not_zero(&tt_common_entry->refcount)) 78 if (!atomic_inc_not_zero(&tt_common_entry->refcount))
79 continue; 79 continue;
80 80
81 tt_common_entry_tmp = tt_common_entry; 81 tt_common_entry_tmp = tt_common_entry;
82 break; 82 break;
83 } 83 }
84 rcu_read_unlock(); 84 rcu_read_unlock();
85 85
86 return tt_common_entry_tmp; 86 return tt_common_entry_tmp;
87 } 87 }
88 88
89 static struct batadv_tt_local_entry * 89 static struct batadv_tt_local_entry *
90 batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const void *data) 90 batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const void *data)
91 { 91 {
92 struct batadv_tt_common_entry *tt_common_entry; 92 struct batadv_tt_common_entry *tt_common_entry;
93 struct batadv_tt_local_entry *tt_local_entry = NULL; 93 struct batadv_tt_local_entry *tt_local_entry = NULL;
94 94
95 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, data); 95 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, data);
96 if (tt_common_entry) 96 if (tt_common_entry)
97 tt_local_entry = container_of(tt_common_entry, 97 tt_local_entry = container_of(tt_common_entry,
98 struct batadv_tt_local_entry, 98 struct batadv_tt_local_entry,
99 common); 99 common);
100 return tt_local_entry; 100 return tt_local_entry;
101 } 101 }
102 102
103 static struct batadv_tt_global_entry * 103 static struct batadv_tt_global_entry *
104 batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const void *data) 104 batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const void *data)
105 { 105 {
106 struct batadv_tt_common_entry *tt_common_entry; 106 struct batadv_tt_common_entry *tt_common_entry;
107 struct batadv_tt_global_entry *tt_global_entry = NULL; 107 struct batadv_tt_global_entry *tt_global_entry = NULL;
108 108
109 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, data); 109 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, data);
110 if (tt_common_entry) 110 if (tt_common_entry)
111 tt_global_entry = container_of(tt_common_entry, 111 tt_global_entry = container_of(tt_common_entry,
112 struct batadv_tt_global_entry, 112 struct batadv_tt_global_entry,
113 common); 113 common);
114 return tt_global_entry; 114 return tt_global_entry;
115 115
116 } 116 }
117 117
118 static void 118 static void
119 batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry) 119 batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
120 { 120 {
121 if (atomic_dec_and_test(&tt_local_entry->common.refcount)) 121 if (atomic_dec_and_test(&tt_local_entry->common.refcount))
122 kfree_rcu(tt_local_entry, common.rcu); 122 kfree_rcu(tt_local_entry, common.rcu);
123 } 123 }
124 124
125 static void batadv_tt_global_entry_free_rcu(struct rcu_head *rcu) 125 static void batadv_tt_global_entry_free_rcu(struct rcu_head *rcu)
126 { 126 {
127 struct batadv_tt_common_entry *tt_common_entry; 127 struct batadv_tt_common_entry *tt_common_entry;
128 struct batadv_tt_global_entry *tt_global_entry; 128 struct batadv_tt_global_entry *tt_global_entry;
129 129
130 tt_common_entry = container_of(rcu, struct batadv_tt_common_entry, rcu); 130 tt_common_entry = container_of(rcu, struct batadv_tt_common_entry, rcu);
131 tt_global_entry = container_of(tt_common_entry, 131 tt_global_entry = container_of(tt_common_entry,
132 struct batadv_tt_global_entry, common); 132 struct batadv_tt_global_entry, common);
133 133
134 kfree(tt_global_entry); 134 kfree(tt_global_entry);
135 } 135 }
136 136
137 static void 137 static void
138 batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry) 138 batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
139 { 139 {
140 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) { 140 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
141 batadv_tt_global_del_orig_list(tt_global_entry); 141 batadv_tt_global_del_orig_list(tt_global_entry);
142 call_rcu(&tt_global_entry->common.rcu, 142 call_rcu(&tt_global_entry->common.rcu,
143 batadv_tt_global_entry_free_rcu); 143 batadv_tt_global_entry_free_rcu);
144 } 144 }
145 } 145 }
146 146
147 static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu) 147 static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
148 { 148 {
149 struct batadv_tt_orig_list_entry *orig_entry; 149 struct batadv_tt_orig_list_entry *orig_entry;
150 150
151 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu); 151 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
152 batadv_orig_node_free_ref(orig_entry->orig_node); 152 batadv_orig_node_free_ref(orig_entry->orig_node);
153 kfree(orig_entry); 153 kfree(orig_entry);
154 } 154 }
155 155
156 static void 156 static void
157 batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry) 157 batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
158 { 158 {
159 if (!atomic_dec_and_test(&orig_entry->refcount)) 159 if (!atomic_dec_and_test(&orig_entry->refcount))
160 return; 160 return;
161 /* to avoid race conditions, immediately decrease the tt counter */ 161 /* to avoid race conditions, immediately decrease the tt counter */
162 atomic_dec(&orig_entry->orig_node->tt_size); 162 atomic_dec(&orig_entry->orig_node->tt_size);
163 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu); 163 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
164 } 164 }
165 165
166 static void batadv_tt_local_event(struct batadv_priv *bat_priv, 166 static void batadv_tt_local_event(struct batadv_priv *bat_priv,
167 const uint8_t *addr, uint8_t flags) 167 const uint8_t *addr, uint8_t flags)
168 { 168 {
169 struct batadv_tt_change_node *tt_change_node, *entry, *safe; 169 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
170 bool event_removed = false; 170 bool event_removed = false;
171 bool del_op_requested, del_op_entry; 171 bool del_op_requested, del_op_entry;
172 172
173 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC); 173 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
174 174
175 if (!tt_change_node) 175 if (!tt_change_node)
176 return; 176 return;
177 177
178 tt_change_node->change.flags = flags; 178 tt_change_node->change.flags = flags;
179 memcpy(tt_change_node->change.addr, addr, ETH_ALEN); 179 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
180 180
181 del_op_requested = flags & BATADV_TT_CLIENT_DEL; 181 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
182 182
183 /* check for ADD+DEL or DEL+ADD events */ 183 /* check for ADD+DEL or DEL+ADD events */
184 spin_lock_bh(&bat_priv->tt.changes_list_lock); 184 spin_lock_bh(&bat_priv->tt.changes_list_lock);
185 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list, 185 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
186 list) { 186 list) {
187 if (!batadv_compare_eth(entry->change.addr, addr)) 187 if (!batadv_compare_eth(entry->change.addr, addr))
188 continue; 188 continue;
189 189
190 /* DEL+ADD in the same orig interval have no effect and can be 190 /* DEL+ADD in the same orig interval have no effect and can be
191 * removed to avoid silly behaviour on the receiver side. The 191 * removed to avoid silly behaviour on the receiver side. The
192 * other way around (ADD+DEL) can happen in case of roaming of 192 * other way around (ADD+DEL) can happen in case of roaming of
193 * a client still in the NEW state. Roaming of NEW clients is 193 * a client still in the NEW state. Roaming of NEW clients is
194 * now possible due to automatically recognition of "temporary" 194 * now possible due to automatically recognition of "temporary"
195 * clients 195 * clients
196 */ 196 */
197 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL; 197 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
198 if (!del_op_requested && del_op_entry) 198 if (!del_op_requested && del_op_entry)
199 goto del; 199 goto del;
200 if (del_op_requested && !del_op_entry) 200 if (del_op_requested && !del_op_entry)
201 goto del; 201 goto del;
202 continue; 202 continue;
203 del: 203 del:
204 list_del(&entry->list); 204 list_del(&entry->list);
205 kfree(entry); 205 kfree(entry);
206 kfree(tt_change_node); 206 kfree(tt_change_node);
207 event_removed = true; 207 event_removed = true;
208 goto unlock; 208 goto unlock;
209 } 209 }
210 210
211 /* track the change in the OGMinterval list */ 211 /* track the change in the OGMinterval list */
212 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list); 212 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
213 213
214 unlock: 214 unlock:
215 spin_unlock_bh(&bat_priv->tt.changes_list_lock); 215 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
216 216
217 if (event_removed) 217 if (event_removed)
218 atomic_dec(&bat_priv->tt.local_changes); 218 atomic_dec(&bat_priv->tt.local_changes);
219 else 219 else
220 atomic_inc(&bat_priv->tt.local_changes); 220 atomic_inc(&bat_priv->tt.local_changes);
221 } 221 }
222 222
223 int batadv_tt_len(int changes_num) 223 int batadv_tt_len(int changes_num)
224 { 224 {
225 return changes_num * sizeof(struct batadv_tt_change); 225 return changes_num * sizeof(struct batadv_tt_change);
226 } 226 }
227 227
228 static int batadv_tt_local_init(struct batadv_priv *bat_priv) 228 static int batadv_tt_local_init(struct batadv_priv *bat_priv)
229 { 229 {
230 if (bat_priv->tt.local_hash) 230 if (bat_priv->tt.local_hash)
231 return 0; 231 return 0;
232 232
233 bat_priv->tt.local_hash = batadv_hash_new(1024); 233 bat_priv->tt.local_hash = batadv_hash_new(1024);
234 234
235 if (!bat_priv->tt.local_hash) 235 if (!bat_priv->tt.local_hash)
236 return -ENOMEM; 236 return -ENOMEM;
237 237
238 return 0; 238 return 0;
239 } 239 }
240 240
241 void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr, 241 void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
242 int ifindex) 242 int ifindex)
243 { 243 {
244 struct batadv_priv *bat_priv = netdev_priv(soft_iface); 244 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
245 struct batadv_tt_local_entry *tt_local_entry = NULL; 245 struct batadv_tt_local_entry *tt_local_entry = NULL;
246 struct batadv_tt_global_entry *tt_global_entry = NULL; 246 struct batadv_tt_global_entry *tt_global_entry = NULL;
247 struct hlist_head *head; 247 struct hlist_head *head;
248 struct hlist_node *node; 248 struct hlist_node *node;
249 struct batadv_tt_orig_list_entry *orig_entry; 249 struct batadv_tt_orig_list_entry *orig_entry;
250 int hash_added; 250 int hash_added;
251 251
252 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr); 252 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
253 253
254 if (tt_local_entry) { 254 if (tt_local_entry) {
255 tt_local_entry->last_seen = jiffies; 255 tt_local_entry->last_seen = jiffies;
256 /* possibly unset the BATADV_TT_CLIENT_PENDING flag */ 256 /* possibly unset the BATADV_TT_CLIENT_PENDING flag */
257 tt_local_entry->common.flags &= ~BATADV_TT_CLIENT_PENDING; 257 tt_local_entry->common.flags &= ~BATADV_TT_CLIENT_PENDING;
258 goto out; 258 goto out;
259 } 259 }
260 260
261 tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC); 261 tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
262 if (!tt_local_entry) 262 if (!tt_local_entry)
263 goto out; 263 goto out;
264 264
265 batadv_dbg(BATADV_DBG_TT, bat_priv, 265 batadv_dbg(BATADV_DBG_TT, bat_priv,
266 "Creating new local tt entry: %pM (ttvn: %d)\n", addr, 266 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
267 (uint8_t)atomic_read(&bat_priv->tt.vn)); 267 (uint8_t)atomic_read(&bat_priv->tt.vn));
268 268
269 memcpy(tt_local_entry->common.addr, addr, ETH_ALEN); 269 memcpy(tt_local_entry->common.addr, addr, ETH_ALEN);
270 tt_local_entry->common.flags = BATADV_NO_FLAGS; 270 tt_local_entry->common.flags = BATADV_NO_FLAGS;
271 if (batadv_is_wifi_iface(ifindex)) 271 if (batadv_is_wifi_iface(ifindex))
272 tt_local_entry->common.flags |= BATADV_TT_CLIENT_WIFI; 272 tt_local_entry->common.flags |= BATADV_TT_CLIENT_WIFI;
273 atomic_set(&tt_local_entry->common.refcount, 2); 273 atomic_set(&tt_local_entry->common.refcount, 2);
274 tt_local_entry->last_seen = jiffies; 274 tt_local_entry->last_seen = jiffies;
275 tt_local_entry->common.added_at = tt_local_entry->last_seen; 275 tt_local_entry->common.added_at = tt_local_entry->last_seen;
276 276
277 /* the batman interface mac address should never be purged */ 277 /* the batman interface mac address should never be purged */
278 if (batadv_compare_eth(addr, soft_iface->dev_addr)) 278 if (batadv_compare_eth(addr, soft_iface->dev_addr))
279 tt_local_entry->common.flags |= BATADV_TT_CLIENT_NOPURGE; 279 tt_local_entry->common.flags |= BATADV_TT_CLIENT_NOPURGE;
280 280
281 /* The local entry has to be marked as NEW to avoid to send it in 281 /* The local entry has to be marked as NEW to avoid to send it in
282 * a full table response going out before the next ttvn increment 282 * a full table response going out before the next ttvn increment
283 * (consistency check) 283 * (consistency check)
284 */ 284 */
285 tt_local_entry->common.flags |= BATADV_TT_CLIENT_NEW; 285 tt_local_entry->common.flags |= BATADV_TT_CLIENT_NEW;
286 286
287 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt, 287 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
288 batadv_choose_orig, 288 batadv_choose_orig,
289 &tt_local_entry->common, 289 &tt_local_entry->common,
290 &tt_local_entry->common.hash_entry); 290 &tt_local_entry->common.hash_entry);
291 291
292 if (unlikely(hash_added != 0)) { 292 if (unlikely(hash_added != 0)) {
293 /* remove the reference for the hash */ 293 /* remove the reference for the hash */
294 batadv_tt_local_entry_free_ref(tt_local_entry); 294 batadv_tt_local_entry_free_ref(tt_local_entry);
295 goto out; 295 goto out;
296 } 296 }
297 297
298 batadv_tt_local_event(bat_priv, addr, tt_local_entry->common.flags); 298 batadv_tt_local_event(bat_priv, addr, tt_local_entry->common.flags);
299 299
300 /* remove address from global hash if present */ 300 /* remove address from global hash if present */
301 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr); 301 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
302 302
303 /* Check whether it is a roaming! */ 303 /* Check whether it is a roaming! */
304 if (tt_global_entry) { 304 if (tt_global_entry) {
305 /* These node are probably going to update their tt table */ 305 /* These node are probably going to update their tt table */
306 head = &tt_global_entry->orig_list; 306 head = &tt_global_entry->orig_list;
307 rcu_read_lock(); 307 rcu_read_lock();
308 hlist_for_each_entry_rcu(orig_entry, node, head, list) { 308 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
309 orig_entry->orig_node->tt_poss_change = true;
310
311 batadv_send_roam_adv(bat_priv, 309 batadv_send_roam_adv(bat_priv,
312 tt_global_entry->common.addr, 310 tt_global_entry->common.addr,
313 orig_entry->orig_node); 311 orig_entry->orig_node);
314 } 312 }
315 rcu_read_unlock(); 313 rcu_read_unlock();
316 /* The global entry has to be marked as ROAMING and 314 /* The global entry has to be marked as ROAMING and
317 * has to be kept for consistency purpose 315 * has to be kept for consistency purpose
318 */ 316 */
319 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM; 317 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
320 tt_global_entry->roam_at = jiffies; 318 tt_global_entry->roam_at = jiffies;
321 } 319 }
322 out: 320 out:
323 if (tt_local_entry) 321 if (tt_local_entry)
324 batadv_tt_local_entry_free_ref(tt_local_entry); 322 batadv_tt_local_entry_free_ref(tt_local_entry);
325 if (tt_global_entry) 323 if (tt_global_entry)
326 batadv_tt_global_entry_free_ref(tt_global_entry); 324 batadv_tt_global_entry_free_ref(tt_global_entry);
327 } 325 }
328 326
329 static void batadv_tt_realloc_packet_buff(unsigned char **packet_buff, 327 static void batadv_tt_realloc_packet_buff(unsigned char **packet_buff,
330 int *packet_buff_len, 328 int *packet_buff_len,
331 int min_packet_len, 329 int min_packet_len,
332 int new_packet_len) 330 int new_packet_len)
333 { 331 {
334 unsigned char *new_buff; 332 unsigned char *new_buff;
335 333
336 new_buff = kmalloc(new_packet_len, GFP_ATOMIC); 334 new_buff = kmalloc(new_packet_len, GFP_ATOMIC);
337 335
338 /* keep old buffer if kmalloc should fail */ 336 /* keep old buffer if kmalloc should fail */
339 if (new_buff) { 337 if (new_buff) {
340 memcpy(new_buff, *packet_buff, min_packet_len); 338 memcpy(new_buff, *packet_buff, min_packet_len);
341 kfree(*packet_buff); 339 kfree(*packet_buff);
342 *packet_buff = new_buff; 340 *packet_buff = new_buff;
343 *packet_buff_len = new_packet_len; 341 *packet_buff_len = new_packet_len;
344 } 342 }
345 } 343 }
346 344
347 static void batadv_tt_prepare_packet_buff(struct batadv_priv *bat_priv, 345 static void batadv_tt_prepare_packet_buff(struct batadv_priv *bat_priv,
348 unsigned char **packet_buff, 346 unsigned char **packet_buff,
349 int *packet_buff_len, 347 int *packet_buff_len,
350 int min_packet_len) 348 int min_packet_len)
351 { 349 {
352 struct batadv_hard_iface *primary_if; 350 struct batadv_hard_iface *primary_if;
353 int req_len; 351 int req_len;
354 352
355 primary_if = batadv_primary_if_get_selected(bat_priv); 353 primary_if = batadv_primary_if_get_selected(bat_priv);
356 354
357 req_len = min_packet_len; 355 req_len = min_packet_len;
358 req_len += batadv_tt_len(atomic_read(&bat_priv->tt.local_changes)); 356 req_len += batadv_tt_len(atomic_read(&bat_priv->tt.local_changes));
359 357
360 /* if we have too many changes for one packet don't send any 358 /* if we have too many changes for one packet don't send any
361 * and wait for the tt table request which will be fragmented 359 * and wait for the tt table request which will be fragmented
362 */ 360 */
363 if ((!primary_if) || (req_len > primary_if->soft_iface->mtu)) 361 if ((!primary_if) || (req_len > primary_if->soft_iface->mtu))
364 req_len = min_packet_len; 362 req_len = min_packet_len;
365 363
366 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len, 364 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
367 min_packet_len, req_len); 365 min_packet_len, req_len);
368 366
369 if (primary_if) 367 if (primary_if)
370 batadv_hardif_free_ref(primary_if); 368 batadv_hardif_free_ref(primary_if);
371 } 369 }
372 370
373 static int batadv_tt_changes_fill_buff(struct batadv_priv *bat_priv, 371 static int batadv_tt_changes_fill_buff(struct batadv_priv *bat_priv,
374 unsigned char **packet_buff, 372 unsigned char **packet_buff,
375 int *packet_buff_len, 373 int *packet_buff_len,
376 int min_packet_len) 374 int min_packet_len)
377 { 375 {
378 struct batadv_tt_change_node *entry, *safe; 376 struct batadv_tt_change_node *entry, *safe;
379 int count = 0, tot_changes = 0, new_len; 377 int count = 0, tot_changes = 0, new_len;
380 unsigned char *tt_buff; 378 unsigned char *tt_buff;
381 379
382 batadv_tt_prepare_packet_buff(bat_priv, packet_buff, 380 batadv_tt_prepare_packet_buff(bat_priv, packet_buff,
383 packet_buff_len, min_packet_len); 381 packet_buff_len, min_packet_len);
384 382
385 new_len = *packet_buff_len - min_packet_len; 383 new_len = *packet_buff_len - min_packet_len;
386 tt_buff = *packet_buff + min_packet_len; 384 tt_buff = *packet_buff + min_packet_len;
387 385
388 if (new_len > 0) 386 if (new_len > 0)
389 tot_changes = new_len / batadv_tt_len(1); 387 tot_changes = new_len / batadv_tt_len(1);
390 388
391 spin_lock_bh(&bat_priv->tt.changes_list_lock); 389 spin_lock_bh(&bat_priv->tt.changes_list_lock);
392 atomic_set(&bat_priv->tt.local_changes, 0); 390 atomic_set(&bat_priv->tt.local_changes, 0);
393 391
394 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list, 392 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
395 list) { 393 list) {
396 if (count < tot_changes) { 394 if (count < tot_changes) {
397 memcpy(tt_buff + batadv_tt_len(count), 395 memcpy(tt_buff + batadv_tt_len(count),
398 &entry->change, sizeof(struct batadv_tt_change)); 396 &entry->change, sizeof(struct batadv_tt_change));
399 count++; 397 count++;
400 } 398 }
401 list_del(&entry->list); 399 list_del(&entry->list);
402 kfree(entry); 400 kfree(entry);
403 } 401 }
404 spin_unlock_bh(&bat_priv->tt.changes_list_lock); 402 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
405 403
406 /* Keep the buffer for possible tt_request */ 404 /* Keep the buffer for possible tt_request */
407 spin_lock_bh(&bat_priv->tt.last_changeset_lock); 405 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
408 kfree(bat_priv->tt.last_changeset); 406 kfree(bat_priv->tt.last_changeset);
409 bat_priv->tt.last_changeset_len = 0; 407 bat_priv->tt.last_changeset_len = 0;
410 bat_priv->tt.last_changeset = NULL; 408 bat_priv->tt.last_changeset = NULL;
411 /* check whether this new OGM has no changes due to size problems */ 409 /* check whether this new OGM has no changes due to size problems */
412 if (new_len > 0) { 410 if (new_len > 0) {
413 /* if kmalloc() fails we will reply with the full table 411 /* if kmalloc() fails we will reply with the full table
414 * instead of providing the diff 412 * instead of providing the diff
415 */ 413 */
416 bat_priv->tt.last_changeset = kmalloc(new_len, GFP_ATOMIC); 414 bat_priv->tt.last_changeset = kmalloc(new_len, GFP_ATOMIC);
417 if (bat_priv->tt.last_changeset) { 415 if (bat_priv->tt.last_changeset) {
418 memcpy(bat_priv->tt.last_changeset, tt_buff, new_len); 416 memcpy(bat_priv->tt.last_changeset, tt_buff, new_len);
419 bat_priv->tt.last_changeset_len = new_len; 417 bat_priv->tt.last_changeset_len = new_len;
420 } 418 }
421 } 419 }
422 spin_unlock_bh(&bat_priv->tt.last_changeset_lock); 420 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
423 421
424 return count; 422 return count;
425 } 423 }
426 424
427 int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset) 425 int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
428 { 426 {
429 struct net_device *net_dev = (struct net_device *)seq->private; 427 struct net_device *net_dev = (struct net_device *)seq->private;
430 struct batadv_priv *bat_priv = netdev_priv(net_dev); 428 struct batadv_priv *bat_priv = netdev_priv(net_dev);
431 struct batadv_hashtable *hash = bat_priv->tt.local_hash; 429 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
432 struct batadv_tt_common_entry *tt_common_entry; 430 struct batadv_tt_common_entry *tt_common_entry;
433 struct batadv_hard_iface *primary_if; 431 struct batadv_hard_iface *primary_if;
434 struct hlist_node *node; 432 struct hlist_node *node;
435 struct hlist_head *head; 433 struct hlist_head *head;
436 uint32_t i; 434 uint32_t i;
437 435
438 primary_if = batadv_seq_print_text_primary_if_get(seq); 436 primary_if = batadv_seq_print_text_primary_if_get(seq);
439 if (!primary_if) 437 if (!primary_if)
440 goto out; 438 goto out;
441 439
442 seq_printf(seq, 440 seq_printf(seq,
443 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n", 441 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
444 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn)); 442 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn));
445 443
446 for (i = 0; i < hash->size; i++) { 444 for (i = 0; i < hash->size; i++) {
447 head = &hash->table[i]; 445 head = &hash->table[i];
448 446
449 rcu_read_lock(); 447 rcu_read_lock();
450 hlist_for_each_entry_rcu(tt_common_entry, node, 448 hlist_for_each_entry_rcu(tt_common_entry, node,
451 head, hash_entry) { 449 head, hash_entry) {
452 seq_printf(seq, " * %pM [%c%c%c%c%c]\n", 450 seq_printf(seq, " * %pM [%c%c%c%c%c]\n",
453 tt_common_entry->addr, 451 tt_common_entry->addr,
454 (tt_common_entry->flags & 452 (tt_common_entry->flags &
455 BATADV_TT_CLIENT_ROAM ? 'R' : '.'), 453 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
456 (tt_common_entry->flags & 454 (tt_common_entry->flags &
457 BATADV_TT_CLIENT_NOPURGE ? 'P' : '.'), 455 BATADV_TT_CLIENT_NOPURGE ? 'P' : '.'),
458 (tt_common_entry->flags & 456 (tt_common_entry->flags &
459 BATADV_TT_CLIENT_NEW ? 'N' : '.'), 457 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
460 (tt_common_entry->flags & 458 (tt_common_entry->flags &
461 BATADV_TT_CLIENT_PENDING ? 'X' : '.'), 459 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
462 (tt_common_entry->flags & 460 (tt_common_entry->flags &
463 BATADV_TT_CLIENT_WIFI ? 'W' : '.')); 461 BATADV_TT_CLIENT_WIFI ? 'W' : '.'));
464 } 462 }
465 rcu_read_unlock(); 463 rcu_read_unlock();
466 } 464 }
467 out: 465 out:
468 if (primary_if) 466 if (primary_if)
469 batadv_hardif_free_ref(primary_if); 467 batadv_hardif_free_ref(primary_if);
470 return 0; 468 return 0;
471 } 469 }
472 470
473 static void 471 static void
474 batadv_tt_local_set_pending(struct batadv_priv *bat_priv, 472 batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
475 struct batadv_tt_local_entry *tt_local_entry, 473 struct batadv_tt_local_entry *tt_local_entry,
476 uint16_t flags, const char *message) 474 uint16_t flags, const char *message)
477 { 475 {
478 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr, 476 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
479 tt_local_entry->common.flags | flags); 477 tt_local_entry->common.flags | flags);
480 478
481 /* The local client has to be marked as "pending to be removed" but has 479 /* The local client has to be marked as "pending to be removed" but has
482 * to be kept in the table in order to send it in a full table 480 * to be kept in the table in order to send it in a full table
483 * response issued before the net ttvn increment (consistency check) 481 * response issued before the net ttvn increment (consistency check)
484 */ 482 */
485 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING; 483 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
486 484
487 batadv_dbg(BATADV_DBG_TT, bat_priv, 485 batadv_dbg(BATADV_DBG_TT, bat_priv,
488 "Local tt entry (%pM) pending to be removed: %s\n", 486 "Local tt entry (%pM) pending to be removed: %s\n",
489 tt_local_entry->common.addr, message); 487 tt_local_entry->common.addr, message);
490 } 488 }
491 489
492 /** 490 /**
493 * batadv_tt_local_remove - logically remove an entry from the local table 491 * batadv_tt_local_remove - logically remove an entry from the local table
494 * @bat_priv: the bat priv with all the soft interface information 492 * @bat_priv: the bat priv with all the soft interface information
495 * @addr: the MAC address of the client to remove 493 * @addr: the MAC address of the client to remove
496 * @message: message to append to the log on deletion 494 * @message: message to append to the log on deletion
497 * @roaming: true if the deletion is due to a roaming event 495 * @roaming: true if the deletion is due to a roaming event
498 * 496 *
499 * Returns the flags assigned to the local entry before being deleted 497 * Returns the flags assigned to the local entry before being deleted
500 */ 498 */
501 uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv, 499 uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
502 const uint8_t *addr, const char *message, 500 const uint8_t *addr, const char *message,
503 bool roaming) 501 bool roaming)
504 { 502 {
505 struct batadv_tt_local_entry *tt_local_entry = NULL; 503 struct batadv_tt_local_entry *tt_local_entry = NULL;
506 uint16_t flags, curr_flags = BATADV_NO_FLAGS; 504 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
507 505
508 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr); 506 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
509 if (!tt_local_entry) 507 if (!tt_local_entry)
510 goto out; 508 goto out;
511 509
512 curr_flags = tt_local_entry->common.flags; 510 curr_flags = tt_local_entry->common.flags;
513 511
514 flags = BATADV_TT_CLIENT_DEL; 512 flags = BATADV_TT_CLIENT_DEL;
515 if (roaming) 513 if (roaming) {
516 flags |= BATADV_TT_CLIENT_ROAM; 514 flags |= BATADV_TT_CLIENT_ROAM;
515 /* mark the local client as ROAMed */
516 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
517 }
517 518
518 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags, message); 519 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags, message);
519 520
520 out: 521 out:
521 if (tt_local_entry) 522 if (tt_local_entry)
522 batadv_tt_local_entry_free_ref(tt_local_entry); 523 batadv_tt_local_entry_free_ref(tt_local_entry);
523 524
524 return curr_flags; 525 return curr_flags;
525 } 526 }
526 527
527 static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv, 528 static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
528 struct hlist_head *head) 529 struct hlist_head *head)
529 { 530 {
530 struct batadv_tt_local_entry *tt_local_entry; 531 struct batadv_tt_local_entry *tt_local_entry;
531 struct batadv_tt_common_entry *tt_common_entry; 532 struct batadv_tt_common_entry *tt_common_entry;
532 struct hlist_node *node, *node_tmp; 533 struct hlist_node *node, *node_tmp;
533 534
534 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, head, 535 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, head,
535 hash_entry) { 536 hash_entry) {
536 tt_local_entry = container_of(tt_common_entry, 537 tt_local_entry = container_of(tt_common_entry,
537 struct batadv_tt_local_entry, 538 struct batadv_tt_local_entry,
538 common); 539 common);
539 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE) 540 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
540 continue; 541 continue;
541 542
542 /* entry already marked for deletion */ 543 /* entry already marked for deletion */
543 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) 544 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
544 continue; 545 continue;
545 546
546 if (!batadv_has_timed_out(tt_local_entry->last_seen, 547 if (!batadv_has_timed_out(tt_local_entry->last_seen,
547 BATADV_TT_LOCAL_TIMEOUT)) 548 BATADV_TT_LOCAL_TIMEOUT))
548 continue; 549 continue;
549 550
550 batadv_tt_local_set_pending(bat_priv, tt_local_entry, 551 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
551 BATADV_TT_CLIENT_DEL, "timed out"); 552 BATADV_TT_CLIENT_DEL, "timed out");
552 } 553 }
553 } 554 }
554 555
555 static void batadv_tt_local_purge(struct batadv_priv *bat_priv) 556 static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
556 { 557 {
557 struct batadv_hashtable *hash = bat_priv->tt.local_hash; 558 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
558 struct hlist_head *head; 559 struct hlist_head *head;
559 spinlock_t *list_lock; /* protects write access to the hash lists */ 560 spinlock_t *list_lock; /* protects write access to the hash lists */
560 uint32_t i; 561 uint32_t i;
561 562
562 for (i = 0; i < hash->size; i++) { 563 for (i = 0; i < hash->size; i++) {
563 head = &hash->table[i]; 564 head = &hash->table[i];
564 list_lock = &hash->list_locks[i]; 565 list_lock = &hash->list_locks[i];
565 566
566 spin_lock_bh(list_lock); 567 spin_lock_bh(list_lock);
567 batadv_tt_local_purge_list(bat_priv, head); 568 batadv_tt_local_purge_list(bat_priv, head);
568 spin_unlock_bh(list_lock); 569 spin_unlock_bh(list_lock);
569 } 570 }
570 571
571 } 572 }
572 573
573 static void batadv_tt_local_table_free(struct batadv_priv *bat_priv) 574 static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
574 { 575 {
575 struct batadv_hashtable *hash; 576 struct batadv_hashtable *hash;
576 spinlock_t *list_lock; /* protects write access to the hash lists */ 577 spinlock_t *list_lock; /* protects write access to the hash lists */
577 struct batadv_tt_common_entry *tt_common_entry; 578 struct batadv_tt_common_entry *tt_common_entry;
578 struct batadv_tt_local_entry *tt_local; 579 struct batadv_tt_local_entry *tt_local;
579 struct hlist_node *node, *node_tmp; 580 struct hlist_node *node, *node_tmp;
580 struct hlist_head *head; 581 struct hlist_head *head;
581 uint32_t i; 582 uint32_t i;
582 583
583 if (!bat_priv->tt.local_hash) 584 if (!bat_priv->tt.local_hash)
584 return; 585 return;
585 586
586 hash = bat_priv->tt.local_hash; 587 hash = bat_priv->tt.local_hash;
587 588
588 for (i = 0; i < hash->size; i++) { 589 for (i = 0; i < hash->size; i++) {
589 head = &hash->table[i]; 590 head = &hash->table[i];
590 list_lock = &hash->list_locks[i]; 591 list_lock = &hash->list_locks[i];
591 592
592 spin_lock_bh(list_lock); 593 spin_lock_bh(list_lock);
593 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, 594 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
594 head, hash_entry) { 595 head, hash_entry) {
595 hlist_del_rcu(node); 596 hlist_del_rcu(node);
596 tt_local = container_of(tt_common_entry, 597 tt_local = container_of(tt_common_entry,
597 struct batadv_tt_local_entry, 598 struct batadv_tt_local_entry,
598 common); 599 common);
599 batadv_tt_local_entry_free_ref(tt_local); 600 batadv_tt_local_entry_free_ref(tt_local);
600 } 601 }
601 spin_unlock_bh(list_lock); 602 spin_unlock_bh(list_lock);
602 } 603 }
603 604
604 batadv_hash_destroy(hash); 605 batadv_hash_destroy(hash);
605 606
606 bat_priv->tt.local_hash = NULL; 607 bat_priv->tt.local_hash = NULL;
607 } 608 }
608 609
609 static int batadv_tt_global_init(struct batadv_priv *bat_priv) 610 static int batadv_tt_global_init(struct batadv_priv *bat_priv)
610 { 611 {
611 if (bat_priv->tt.global_hash) 612 if (bat_priv->tt.global_hash)
612 return 0; 613 return 0;
613 614
614 bat_priv->tt.global_hash = batadv_hash_new(1024); 615 bat_priv->tt.global_hash = batadv_hash_new(1024);
615 616
616 if (!bat_priv->tt.global_hash) 617 if (!bat_priv->tt.global_hash)
617 return -ENOMEM; 618 return -ENOMEM;
618 619
619 return 0; 620 return 0;
620 } 621 }
621 622
622 static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv) 623 static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
623 { 624 {
624 struct batadv_tt_change_node *entry, *safe; 625 struct batadv_tt_change_node *entry, *safe;
625 626
626 spin_lock_bh(&bat_priv->tt.changes_list_lock); 627 spin_lock_bh(&bat_priv->tt.changes_list_lock);
627 628
628 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list, 629 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
629 list) { 630 list) {
630 list_del(&entry->list); 631 list_del(&entry->list);
631 kfree(entry); 632 kfree(entry);
632 } 633 }
633 634
634 atomic_set(&bat_priv->tt.local_changes, 0); 635 atomic_set(&bat_priv->tt.local_changes, 0);
635 spin_unlock_bh(&bat_priv->tt.changes_list_lock); 636 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
636 } 637 }
637 638
638 /* retrieves the orig_tt_list_entry belonging to orig_node from the 639 /* retrieves the orig_tt_list_entry belonging to orig_node from the
639 * batadv_tt_global_entry list 640 * batadv_tt_global_entry list
640 * 641 *
641 * returns it with an increased refcounter, NULL if not found 642 * returns it with an increased refcounter, NULL if not found
642 */ 643 */
643 static struct batadv_tt_orig_list_entry * 644 static struct batadv_tt_orig_list_entry *
644 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry, 645 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
645 const struct batadv_orig_node *orig_node) 646 const struct batadv_orig_node *orig_node)
646 { 647 {
647 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL; 648 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
648 const struct hlist_head *head; 649 const struct hlist_head *head;
649 struct hlist_node *node; 650 struct hlist_node *node;
650 651
651 rcu_read_lock(); 652 rcu_read_lock();
652 head = &entry->orig_list; 653 head = &entry->orig_list;
653 hlist_for_each_entry_rcu(tmp_orig_entry, node, head, list) { 654 hlist_for_each_entry_rcu(tmp_orig_entry, node, head, list) {
654 if (tmp_orig_entry->orig_node != orig_node) 655 if (tmp_orig_entry->orig_node != orig_node)
655 continue; 656 continue;
656 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount)) 657 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
657 continue; 658 continue;
658 659
659 orig_entry = tmp_orig_entry; 660 orig_entry = tmp_orig_entry;
660 break; 661 break;
661 } 662 }
662 rcu_read_unlock(); 663 rcu_read_unlock();
663 664
664 return orig_entry; 665 return orig_entry;
665 } 666 }
666 667
667 /* find out if an orig_node is already in the list of a tt_global_entry. 668 /* find out if an orig_node is already in the list of a tt_global_entry.
668 * returns true if found, false otherwise 669 * returns true if found, false otherwise
669 */ 670 */
670 static bool 671 static bool
671 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry, 672 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
672 const struct batadv_orig_node *orig_node) 673 const struct batadv_orig_node *orig_node)
673 { 674 {
674 struct batadv_tt_orig_list_entry *orig_entry; 675 struct batadv_tt_orig_list_entry *orig_entry;
675 bool found = false; 676 bool found = false;
676 677
677 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node); 678 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
678 if (orig_entry) { 679 if (orig_entry) {
679 found = true; 680 found = true;
680 batadv_tt_orig_list_entry_free_ref(orig_entry); 681 batadv_tt_orig_list_entry_free_ref(orig_entry);
681 } 682 }
682 683
683 return found; 684 return found;
684 } 685 }
685 686
686 static void 687 static void
687 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global, 688 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
688 struct batadv_orig_node *orig_node, int ttvn) 689 struct batadv_orig_node *orig_node, int ttvn)
689 { 690 {
690 struct batadv_tt_orig_list_entry *orig_entry; 691 struct batadv_tt_orig_list_entry *orig_entry;
691 692
692 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node); 693 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
693 if (orig_entry) { 694 if (orig_entry) {
694 /* refresh the ttvn: the current value could be a bogus one that 695 /* refresh the ttvn: the current value could be a bogus one that
695 * was added during a "temporary client detection" 696 * was added during a "temporary client detection"
696 */ 697 */
697 orig_entry->ttvn = ttvn; 698 orig_entry->ttvn = ttvn;
698 goto out; 699 goto out;
699 } 700 }
700 701
701 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC); 702 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
702 if (!orig_entry) 703 if (!orig_entry)
703 goto out; 704 goto out;
704 705
705 INIT_HLIST_NODE(&orig_entry->list); 706 INIT_HLIST_NODE(&orig_entry->list);
706 atomic_inc(&orig_node->refcount); 707 atomic_inc(&orig_node->refcount);
707 atomic_inc(&orig_node->tt_size); 708 atomic_inc(&orig_node->tt_size);
708 orig_entry->orig_node = orig_node; 709 orig_entry->orig_node = orig_node;
709 orig_entry->ttvn = ttvn; 710 orig_entry->ttvn = ttvn;
710 atomic_set(&orig_entry->refcount, 2); 711 atomic_set(&orig_entry->refcount, 2);
711 712
712 spin_lock_bh(&tt_global->list_lock); 713 spin_lock_bh(&tt_global->list_lock);
713 hlist_add_head_rcu(&orig_entry->list, 714 hlist_add_head_rcu(&orig_entry->list,
714 &tt_global->orig_list); 715 &tt_global->orig_list);
715 spin_unlock_bh(&tt_global->list_lock); 716 spin_unlock_bh(&tt_global->list_lock);
716 out: 717 out:
717 if (orig_entry) 718 if (orig_entry)
718 batadv_tt_orig_list_entry_free_ref(orig_entry); 719 batadv_tt_orig_list_entry_free_ref(orig_entry);
719 } 720 }
720 721
721 /* caller must hold orig_node refcount */ 722 /* caller must hold orig_node refcount */
722 int batadv_tt_global_add(struct batadv_priv *bat_priv, 723 int batadv_tt_global_add(struct batadv_priv *bat_priv,
723 struct batadv_orig_node *orig_node, 724 struct batadv_orig_node *orig_node,
724 const unsigned char *tt_addr, uint8_t flags, 725 const unsigned char *tt_addr, uint8_t flags,
725 uint8_t ttvn) 726 uint8_t ttvn)
726 { 727 {
727 struct batadv_tt_global_entry *tt_global_entry = NULL; 728 struct batadv_tt_global_entry *tt_global_entry = NULL;
728 int ret = 0; 729 int ret = 0;
729 int hash_added; 730 int hash_added;
730 struct batadv_tt_common_entry *common; 731 struct batadv_tt_common_entry *common;
731 uint16_t local_flags; 732 uint16_t local_flags;
732 733
733 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr); 734 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
734 735
735 if (!tt_global_entry) { 736 if (!tt_global_entry) {
736 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC); 737 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
737 if (!tt_global_entry) 738 if (!tt_global_entry)
738 goto out; 739 goto out;
739 740
740 common = &tt_global_entry->common; 741 common = &tt_global_entry->common;
741 memcpy(common->addr, tt_addr, ETH_ALEN); 742 memcpy(common->addr, tt_addr, ETH_ALEN);
742 743
743 common->flags = flags; 744 common->flags = flags;
744 tt_global_entry->roam_at = 0; 745 tt_global_entry->roam_at = 0;
745 /* node must store current time in case of roaming. This is 746 /* node must store current time in case of roaming. This is
746 * needed to purge this entry out on timeout (if nobody claims 747 * needed to purge this entry out on timeout (if nobody claims
747 * it) 748 * it)
748 */ 749 */
749 if (flags & BATADV_TT_CLIENT_ROAM) 750 if (flags & BATADV_TT_CLIENT_ROAM)
750 tt_global_entry->roam_at = jiffies; 751 tt_global_entry->roam_at = jiffies;
751 atomic_set(&common->refcount, 2); 752 atomic_set(&common->refcount, 2);
752 common->added_at = jiffies; 753 common->added_at = jiffies;
753 754
754 INIT_HLIST_HEAD(&tt_global_entry->orig_list); 755 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
755 spin_lock_init(&tt_global_entry->list_lock); 756 spin_lock_init(&tt_global_entry->list_lock);
756 757
757 hash_added = batadv_hash_add(bat_priv->tt.global_hash, 758 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
758 batadv_compare_tt, 759 batadv_compare_tt,
759 batadv_choose_orig, common, 760 batadv_choose_orig, common,
760 &common->hash_entry); 761 &common->hash_entry);
761 762
762 if (unlikely(hash_added != 0)) { 763 if (unlikely(hash_added != 0)) {
763 /* remove the reference for the hash */ 764 /* remove the reference for the hash */
764 batadv_tt_global_entry_free_ref(tt_global_entry); 765 batadv_tt_global_entry_free_ref(tt_global_entry);
765 goto out_remove; 766 goto out_remove;
766 } 767 }
767 } else { 768 } else {
768 /* If there is already a global entry, we can use this one for 769 /* If there is already a global entry, we can use this one for
769 * our processing. 770 * our processing.
770 * But if we are trying to add a temporary client we can exit 771 * But if we are trying to add a temporary client we can exit
771 * directly because the temporary information should never 772 * directly because the temporary information should never
772 * override any already known client state (whatever it is) 773 * override any already known client state (whatever it is)
773 */ 774 */
774 if (flags & BATADV_TT_CLIENT_TEMP) 775 if (flags & BATADV_TT_CLIENT_TEMP)
775 goto out; 776 goto out;
776 777
777 /* if the client was temporary added before receiving the first 778 /* if the client was temporary added before receiving the first
778 * OGM announcing it, we have to clear the TEMP flag 779 * OGM announcing it, we have to clear the TEMP flag
779 */ 780 */
780 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_TEMP; 781 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_TEMP;
781 782
782 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only 783 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
783 * one originator left in the list and we previously received a 784 * one originator left in the list and we previously received a
784 * delete + roaming change for this originator. 785 * delete + roaming change for this originator.
785 * 786 *
786 * We should first delete the old originator before adding the 787 * We should first delete the old originator before adding the
787 * new one. 788 * new one.
788 */ 789 */
789 if (tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM) { 790 if (tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM) {
790 batadv_tt_global_del_orig_list(tt_global_entry); 791 batadv_tt_global_del_orig_list(tt_global_entry);
791 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM; 792 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
792 tt_global_entry->roam_at = 0; 793 tt_global_entry->roam_at = 0;
793 } 794 }
794 } 795 }
795 /* add the new orig_entry (if needed) or update it */ 796 /* add the new orig_entry (if needed) or update it */
796 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn); 797 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
797 798
798 batadv_dbg(BATADV_DBG_TT, bat_priv, 799 batadv_dbg(BATADV_DBG_TT, bat_priv,
799 "Creating new global tt entry: %pM (via %pM)\n", 800 "Creating new global tt entry: %pM (via %pM)\n",
800 tt_global_entry->common.addr, orig_node->orig); 801 tt_global_entry->common.addr, orig_node->orig);
801 ret = 1; 802 ret = 1;
802 803
803 out_remove: 804 out_remove:
804 805
805 /* remove address from local hash if present */ 806 /* remove address from local hash if present */
806 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, 807 local_flags = batadv_tt_local_remove(bat_priv, tt_addr,
807 "global tt received", 808 "global tt received",
808 flags & BATADV_TT_CLIENT_ROAM); 809 flags & BATADV_TT_CLIENT_ROAM);
809 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI; 810 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
810 811
811 out: 812 out:
812 if (tt_global_entry) 813 if (tt_global_entry)
813 batadv_tt_global_entry_free_ref(tt_global_entry); 814 batadv_tt_global_entry_free_ref(tt_global_entry);
814 return ret; 815 return ret;
815 } 816 }
816 817
817 /* print all orig nodes who announce the address for this global entry. 818 /* print all orig nodes who announce the address for this global entry.
818 * it is assumed that the caller holds rcu_read_lock(); 819 * it is assumed that the caller holds rcu_read_lock();
819 */ 820 */
820 static void 821 static void
821 batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry, 822 batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
822 struct seq_file *seq) 823 struct seq_file *seq)
823 { 824 {
824 struct hlist_head *head; 825 struct hlist_head *head;
825 struct hlist_node *node; 826 struct hlist_node *node;
826 struct batadv_tt_orig_list_entry *orig_entry; 827 struct batadv_tt_orig_list_entry *orig_entry;
827 struct batadv_tt_common_entry *tt_common_entry; 828 struct batadv_tt_common_entry *tt_common_entry;
828 uint16_t flags; 829 uint16_t flags;
829 uint8_t last_ttvn; 830 uint8_t last_ttvn;
830 831
831 tt_common_entry = &tt_global_entry->common; 832 tt_common_entry = &tt_global_entry->common;
832 833
833 head = &tt_global_entry->orig_list; 834 head = &tt_global_entry->orig_list;
834 835
835 hlist_for_each_entry_rcu(orig_entry, node, head, list) { 836 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
836 flags = tt_common_entry->flags; 837 flags = tt_common_entry->flags;
837 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn); 838 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
838 seq_printf(seq, " * %pM (%3u) via %pM (%3u) [%c%c%c]\n", 839 seq_printf(seq, " * %pM (%3u) via %pM (%3u) [%c%c%c]\n",
839 tt_global_entry->common.addr, orig_entry->ttvn, 840 tt_global_entry->common.addr, orig_entry->ttvn,
840 orig_entry->orig_node->orig, last_ttvn, 841 orig_entry->orig_node->orig, last_ttvn,
841 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'), 842 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
842 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'), 843 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
843 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.')); 844 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
844 } 845 }
845 } 846 }
846 847
847 int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset) 848 int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
848 { 849 {
849 struct net_device *net_dev = (struct net_device *)seq->private; 850 struct net_device *net_dev = (struct net_device *)seq->private;
850 struct batadv_priv *bat_priv = netdev_priv(net_dev); 851 struct batadv_priv *bat_priv = netdev_priv(net_dev);
851 struct batadv_hashtable *hash = bat_priv->tt.global_hash; 852 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
852 struct batadv_tt_common_entry *tt_common_entry; 853 struct batadv_tt_common_entry *tt_common_entry;
853 struct batadv_tt_global_entry *tt_global; 854 struct batadv_tt_global_entry *tt_global;
854 struct batadv_hard_iface *primary_if; 855 struct batadv_hard_iface *primary_if;
855 struct hlist_node *node; 856 struct hlist_node *node;
856 struct hlist_head *head; 857 struct hlist_head *head;
857 uint32_t i; 858 uint32_t i;
858 859
859 primary_if = batadv_seq_print_text_primary_if_get(seq); 860 primary_if = batadv_seq_print_text_primary_if_get(seq);
860 if (!primary_if) 861 if (!primary_if)
861 goto out; 862 goto out;
862 863
863 seq_printf(seq, 864 seq_printf(seq,
864 "Globally announced TT entries received via the mesh %s\n", 865 "Globally announced TT entries received via the mesh %s\n",
865 net_dev->name); 866 net_dev->name);
866 seq_printf(seq, " %-13s %s %-15s %s %s\n", 867 seq_printf(seq, " %-13s %s %-15s %s %s\n",
867 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags"); 868 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
868 869
869 for (i = 0; i < hash->size; i++) { 870 for (i = 0; i < hash->size; i++) {
870 head = &hash->table[i]; 871 head = &hash->table[i];
871 872
872 rcu_read_lock(); 873 rcu_read_lock();
873 hlist_for_each_entry_rcu(tt_common_entry, node, 874 hlist_for_each_entry_rcu(tt_common_entry, node,
874 head, hash_entry) { 875 head, hash_entry) {
875 tt_global = container_of(tt_common_entry, 876 tt_global = container_of(tt_common_entry,
876 struct batadv_tt_global_entry, 877 struct batadv_tt_global_entry,
877 common); 878 common);
878 batadv_tt_global_print_entry(tt_global, seq); 879 batadv_tt_global_print_entry(tt_global, seq);
879 } 880 }
880 rcu_read_unlock(); 881 rcu_read_unlock();
881 } 882 }
882 out: 883 out:
883 if (primary_if) 884 if (primary_if)
884 batadv_hardif_free_ref(primary_if); 885 batadv_hardif_free_ref(primary_if);
885 return 0; 886 return 0;
886 } 887 }
887 888
888 /* deletes the orig list of a tt_global_entry */ 889 /* deletes the orig list of a tt_global_entry */
889 static void 890 static void
890 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry) 891 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
891 { 892 {
892 struct hlist_head *head; 893 struct hlist_head *head;
893 struct hlist_node *node, *safe; 894 struct hlist_node *node, *safe;
894 struct batadv_tt_orig_list_entry *orig_entry; 895 struct batadv_tt_orig_list_entry *orig_entry;
895 896
896 spin_lock_bh(&tt_global_entry->list_lock); 897 spin_lock_bh(&tt_global_entry->list_lock);
897 head = &tt_global_entry->orig_list; 898 head = &tt_global_entry->orig_list;
898 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) { 899 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
899 hlist_del_rcu(node); 900 hlist_del_rcu(node);
900 batadv_tt_orig_list_entry_free_ref(orig_entry); 901 batadv_tt_orig_list_entry_free_ref(orig_entry);
901 } 902 }
902 spin_unlock_bh(&tt_global_entry->list_lock); 903 spin_unlock_bh(&tt_global_entry->list_lock);
903 904
904 } 905 }
905 906
906 static void 907 static void
907 batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv, 908 batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
908 struct batadv_tt_global_entry *tt_global_entry, 909 struct batadv_tt_global_entry *tt_global_entry,
909 struct batadv_orig_node *orig_node, 910 struct batadv_orig_node *orig_node,
910 const char *message) 911 const char *message)
911 { 912 {
912 struct hlist_head *head; 913 struct hlist_head *head;
913 struct hlist_node *node, *safe; 914 struct hlist_node *node, *safe;
914 struct batadv_tt_orig_list_entry *orig_entry; 915 struct batadv_tt_orig_list_entry *orig_entry;
915 916
916 spin_lock_bh(&tt_global_entry->list_lock); 917 spin_lock_bh(&tt_global_entry->list_lock);
917 head = &tt_global_entry->orig_list; 918 head = &tt_global_entry->orig_list;
918 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) { 919 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
919 if (orig_entry->orig_node == orig_node) { 920 if (orig_entry->orig_node == orig_node) {
920 batadv_dbg(BATADV_DBG_TT, bat_priv, 921 batadv_dbg(BATADV_DBG_TT, bat_priv,
921 "Deleting %pM from global tt entry %pM: %s\n", 922 "Deleting %pM from global tt entry %pM: %s\n",
922 orig_node->orig, 923 orig_node->orig,
923 tt_global_entry->common.addr, message); 924 tt_global_entry->common.addr, message);
924 hlist_del_rcu(node); 925 hlist_del_rcu(node);
925 batadv_tt_orig_list_entry_free_ref(orig_entry); 926 batadv_tt_orig_list_entry_free_ref(orig_entry);
926 } 927 }
927 } 928 }
928 spin_unlock_bh(&tt_global_entry->list_lock); 929 spin_unlock_bh(&tt_global_entry->list_lock);
929 } 930 }
930 931
931 static void 932 static void
932 batadv_tt_global_del_struct(struct batadv_priv *bat_priv, 933 batadv_tt_global_del_struct(struct batadv_priv *bat_priv,
933 struct batadv_tt_global_entry *tt_global_entry, 934 struct batadv_tt_global_entry *tt_global_entry,
934 const char *message) 935 const char *message)
935 { 936 {
936 batadv_dbg(BATADV_DBG_TT, bat_priv, 937 batadv_dbg(BATADV_DBG_TT, bat_priv,
937 "Deleting global tt entry %pM: %s\n", 938 "Deleting global tt entry %pM: %s\n",
938 tt_global_entry->common.addr, message); 939 tt_global_entry->common.addr, message);
939 940
940 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt, 941 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
941 batadv_choose_orig, tt_global_entry->common.addr); 942 batadv_choose_orig, tt_global_entry->common.addr);
942 batadv_tt_global_entry_free_ref(tt_global_entry); 943 batadv_tt_global_entry_free_ref(tt_global_entry);
943 944
944 } 945 }
945 946
946 /* If the client is to be deleted, we check if it is the last origantor entry 947 /* If the client is to be deleted, we check if it is the last origantor entry
947 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the 948 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
948 * timer, otherwise we simply remove the originator scheduled for deletion. 949 * timer, otherwise we simply remove the originator scheduled for deletion.
949 */ 950 */
950 static void 951 static void
951 batadv_tt_global_del_roaming(struct batadv_priv *bat_priv, 952 batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
952 struct batadv_tt_global_entry *tt_global_entry, 953 struct batadv_tt_global_entry *tt_global_entry,
953 struct batadv_orig_node *orig_node, 954 struct batadv_orig_node *orig_node,
954 const char *message) 955 const char *message)
955 { 956 {
956 bool last_entry = true; 957 bool last_entry = true;
957 struct hlist_head *head; 958 struct hlist_head *head;
958 struct hlist_node *node; 959 struct hlist_node *node;
959 struct batadv_tt_orig_list_entry *orig_entry; 960 struct batadv_tt_orig_list_entry *orig_entry;
960 961
961 /* no local entry exists, case 1: 962 /* no local entry exists, case 1:
962 * Check if this is the last one or if other entries exist. 963 * Check if this is the last one or if other entries exist.
963 */ 964 */
964 965
965 rcu_read_lock(); 966 rcu_read_lock();
966 head = &tt_global_entry->orig_list; 967 head = &tt_global_entry->orig_list;
967 hlist_for_each_entry_rcu(orig_entry, node, head, list) { 968 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
968 if (orig_entry->orig_node != orig_node) { 969 if (orig_entry->orig_node != orig_node) {
969 last_entry = false; 970 last_entry = false;
970 break; 971 break;
971 } 972 }
972 } 973 }
973 rcu_read_unlock(); 974 rcu_read_unlock();
974 975
975 if (last_entry) { 976 if (last_entry) {
976 /* its the last one, mark for roaming. */ 977 /* its the last one, mark for roaming. */
977 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM; 978 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
978 tt_global_entry->roam_at = jiffies; 979 tt_global_entry->roam_at = jiffies;
979 } else 980 } else
980 /* there is another entry, we can simply delete this 981 /* there is another entry, we can simply delete this
981 * one and can still use the other one. 982 * one and can still use the other one.
982 */ 983 */
983 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry, 984 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
984 orig_node, message); 985 orig_node, message);
985 } 986 }
986 987
987 988
988 989
989 static void batadv_tt_global_del(struct batadv_priv *bat_priv, 990 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
990 struct batadv_orig_node *orig_node, 991 struct batadv_orig_node *orig_node,
991 const unsigned char *addr, 992 const unsigned char *addr,
992 const char *message, bool roaming) 993 const char *message, bool roaming)
993 { 994 {
994 struct batadv_tt_global_entry *tt_global_entry = NULL; 995 struct batadv_tt_global_entry *tt_global_entry = NULL;
995 struct batadv_tt_local_entry *local_entry = NULL; 996 struct batadv_tt_local_entry *local_entry = NULL;
996 997
997 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr); 998 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
998 if (!tt_global_entry) 999 if (!tt_global_entry)
999 goto out; 1000 goto out;
1000 1001
1001 if (!roaming) { 1002 if (!roaming) {
1002 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry, 1003 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1003 orig_node, message); 1004 orig_node, message);
1004 1005
1005 if (hlist_empty(&tt_global_entry->orig_list)) 1006 if (hlist_empty(&tt_global_entry->orig_list))
1006 batadv_tt_global_del_struct(bat_priv, tt_global_entry, 1007 batadv_tt_global_del_struct(bat_priv, tt_global_entry,
1007 message); 1008 message);
1008 1009
1009 goto out; 1010 goto out;
1010 } 1011 }
1011 1012
1012 /* if we are deleting a global entry due to a roam 1013 /* if we are deleting a global entry due to a roam
1013 * event, there are two possibilities: 1014 * event, there are two possibilities:
1014 * 1) the client roamed from node A to node B => if there 1015 * 1) the client roamed from node A to node B => if there
1015 * is only one originator left for this client, we mark 1016 * is only one originator left for this client, we mark
1016 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we 1017 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
1017 * wait for node B to claim it. In case of timeout 1018 * wait for node B to claim it. In case of timeout
1018 * the entry is purged. 1019 * the entry is purged.
1019 * 1020 *
1020 * If there are other originators left, we directly delete 1021 * If there are other originators left, we directly delete
1021 * the originator. 1022 * the originator.
1022 * 2) the client roamed to us => we can directly delete 1023 * 2) the client roamed to us => we can directly delete
1023 * the global entry, since it is useless now. 1024 * the global entry, since it is useless now.
1024 */ 1025 */
1025 local_entry = batadv_tt_local_hash_find(bat_priv, 1026 local_entry = batadv_tt_local_hash_find(bat_priv,
1026 tt_global_entry->common.addr); 1027 tt_global_entry->common.addr);
1027 if (local_entry) { 1028 if (local_entry) {
1028 /* local entry exists, case 2: client roamed to us. */ 1029 /* local entry exists, case 2: client roamed to us. */
1029 batadv_tt_global_del_orig_list(tt_global_entry); 1030 batadv_tt_global_del_orig_list(tt_global_entry);
1030 batadv_tt_global_del_struct(bat_priv, tt_global_entry, message); 1031 batadv_tt_global_del_struct(bat_priv, tt_global_entry, message);
1031 } else 1032 } else
1032 /* no local entry exists, case 1: check for roaming */ 1033 /* no local entry exists, case 1: check for roaming */
1033 batadv_tt_global_del_roaming(bat_priv, tt_global_entry, 1034 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1034 orig_node, message); 1035 orig_node, message);
1035 1036
1036 1037
1037 out: 1038 out:
1038 if (tt_global_entry) 1039 if (tt_global_entry)
1039 batadv_tt_global_entry_free_ref(tt_global_entry); 1040 batadv_tt_global_entry_free_ref(tt_global_entry);
1040 if (local_entry) 1041 if (local_entry)
1041 batadv_tt_local_entry_free_ref(local_entry); 1042 batadv_tt_local_entry_free_ref(local_entry);
1042 } 1043 }
1043 1044
1044 void batadv_tt_global_del_orig(struct batadv_priv *bat_priv, 1045 void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1045 struct batadv_orig_node *orig_node, 1046 struct batadv_orig_node *orig_node,
1046 const char *message) 1047 const char *message)
1047 { 1048 {
1048 struct batadv_tt_global_entry *tt_global; 1049 struct batadv_tt_global_entry *tt_global;
1049 struct batadv_tt_common_entry *tt_common_entry; 1050 struct batadv_tt_common_entry *tt_common_entry;
1050 uint32_t i; 1051 uint32_t i;
1051 struct batadv_hashtable *hash = bat_priv->tt.global_hash; 1052 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1052 struct hlist_node *node, *safe; 1053 struct hlist_node *node, *safe;
1053 struct hlist_head *head; 1054 struct hlist_head *head;
1054 spinlock_t *list_lock; /* protects write access to the hash lists */ 1055 spinlock_t *list_lock; /* protects write access to the hash lists */
1055 1056
1056 if (!hash) 1057 if (!hash)
1057 return; 1058 return;
1058 1059
1059 for (i = 0; i < hash->size; i++) { 1060 for (i = 0; i < hash->size; i++) {
1060 head = &hash->table[i]; 1061 head = &hash->table[i];
1061 list_lock = &hash->list_locks[i]; 1062 list_lock = &hash->list_locks[i];
1062 1063
1063 spin_lock_bh(list_lock); 1064 spin_lock_bh(list_lock);
1064 hlist_for_each_entry_safe(tt_common_entry, node, safe, 1065 hlist_for_each_entry_safe(tt_common_entry, node, safe,
1065 head, hash_entry) { 1066 head, hash_entry) {
1066 tt_global = container_of(tt_common_entry, 1067 tt_global = container_of(tt_common_entry,
1067 struct batadv_tt_global_entry, 1068 struct batadv_tt_global_entry,
1068 common); 1069 common);
1069 1070
1070 batadv_tt_global_del_orig_entry(bat_priv, tt_global, 1071 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
1071 orig_node, message); 1072 orig_node, message);
1072 1073
1073 if (hlist_empty(&tt_global->orig_list)) { 1074 if (hlist_empty(&tt_global->orig_list)) {
1074 batadv_dbg(BATADV_DBG_TT, bat_priv, 1075 batadv_dbg(BATADV_DBG_TT, bat_priv,
1075 "Deleting global tt entry %pM: %s\n", 1076 "Deleting global tt entry %pM: %s\n",
1076 tt_global->common.addr, message); 1077 tt_global->common.addr, message);
1077 hlist_del_rcu(node); 1078 hlist_del_rcu(node);
1078 batadv_tt_global_entry_free_ref(tt_global); 1079 batadv_tt_global_entry_free_ref(tt_global);
1079 } 1080 }
1080 } 1081 }
1081 spin_unlock_bh(list_lock); 1082 spin_unlock_bh(list_lock);
1082 } 1083 }
1083 orig_node->tt_initialised = false; 1084 orig_node->tt_initialised = false;
1084 } 1085 }
1085 1086
1086 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global, 1087 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1087 char **msg) 1088 char **msg)
1088 { 1089 {
1089 bool purge = false; 1090 bool purge = false;
1090 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT; 1091 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1091 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT; 1092 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
1092 1093
1093 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) && 1094 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1094 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) { 1095 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1095 purge = true; 1096 purge = true;
1096 *msg = "Roaming timeout\n"; 1097 *msg = "Roaming timeout\n";
1097 } 1098 }
1098 1099
1099 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) && 1100 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1100 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) { 1101 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1101 purge = true; 1102 purge = true;
1102 *msg = "Temporary client timeout\n"; 1103 *msg = "Temporary client timeout\n";
1103 } 1104 }
1104 1105
1105 return purge; 1106 return purge;
1106 } 1107 }
1107 1108
1108 static void batadv_tt_global_purge(struct batadv_priv *bat_priv) 1109 static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
1109 { 1110 {
1110 struct batadv_hashtable *hash = bat_priv->tt.global_hash; 1111 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1111 struct hlist_head *head; 1112 struct hlist_head *head;
1112 struct hlist_node *node, *node_tmp; 1113 struct hlist_node *node, *node_tmp;
1113 spinlock_t *list_lock; /* protects write access to the hash lists */ 1114 spinlock_t *list_lock; /* protects write access to the hash lists */
1114 uint32_t i; 1115 uint32_t i;
1115 char *msg = NULL; 1116 char *msg = NULL;
1116 struct batadv_tt_common_entry *tt_common; 1117 struct batadv_tt_common_entry *tt_common;
1117 struct batadv_tt_global_entry *tt_global; 1118 struct batadv_tt_global_entry *tt_global;
1118 1119
1119 for (i = 0; i < hash->size; i++) { 1120 for (i = 0; i < hash->size; i++) {
1120 head = &hash->table[i]; 1121 head = &hash->table[i];
1121 list_lock = &hash->list_locks[i]; 1122 list_lock = &hash->list_locks[i];
1122 1123
1123 spin_lock_bh(list_lock); 1124 spin_lock_bh(list_lock);
1124 hlist_for_each_entry_safe(tt_common, node, node_tmp, head, 1125 hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
1125 hash_entry) { 1126 hash_entry) {
1126 tt_global = container_of(tt_common, 1127 tt_global = container_of(tt_common,
1127 struct batadv_tt_global_entry, 1128 struct batadv_tt_global_entry,
1128 common); 1129 common);
1129 1130
1130 if (!batadv_tt_global_to_purge(tt_global, &msg)) 1131 if (!batadv_tt_global_to_purge(tt_global, &msg))
1131 continue; 1132 continue;
1132 1133
1133 batadv_dbg(BATADV_DBG_TT, bat_priv, 1134 batadv_dbg(BATADV_DBG_TT, bat_priv,
1134 "Deleting global tt entry (%pM): %s\n", 1135 "Deleting global tt entry (%pM): %s\n",
1135 tt_global->common.addr, msg); 1136 tt_global->common.addr, msg);
1136 1137
1137 hlist_del_rcu(node); 1138 hlist_del_rcu(node);
1138 1139
1139 batadv_tt_global_entry_free_ref(tt_global); 1140 batadv_tt_global_entry_free_ref(tt_global);
1140 } 1141 }
1141 spin_unlock_bh(list_lock); 1142 spin_unlock_bh(list_lock);
1142 } 1143 }
1143 } 1144 }
1144 1145
1145 static void batadv_tt_global_table_free(struct batadv_priv *bat_priv) 1146 static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
1146 { 1147 {
1147 struct batadv_hashtable *hash; 1148 struct batadv_hashtable *hash;
1148 spinlock_t *list_lock; /* protects write access to the hash lists */ 1149 spinlock_t *list_lock; /* protects write access to the hash lists */
1149 struct batadv_tt_common_entry *tt_common_entry; 1150 struct batadv_tt_common_entry *tt_common_entry;
1150 struct batadv_tt_global_entry *tt_global; 1151 struct batadv_tt_global_entry *tt_global;
1151 struct hlist_node *node, *node_tmp; 1152 struct hlist_node *node, *node_tmp;
1152 struct hlist_head *head; 1153 struct hlist_head *head;
1153 uint32_t i; 1154 uint32_t i;
1154 1155
1155 if (!bat_priv->tt.global_hash) 1156 if (!bat_priv->tt.global_hash)
1156 return; 1157 return;
1157 1158
1158 hash = bat_priv->tt.global_hash; 1159 hash = bat_priv->tt.global_hash;
1159 1160
1160 for (i = 0; i < hash->size; i++) { 1161 for (i = 0; i < hash->size; i++) {
1161 head = &hash->table[i]; 1162 head = &hash->table[i];
1162 list_lock = &hash->list_locks[i]; 1163 list_lock = &hash->list_locks[i];
1163 1164
1164 spin_lock_bh(list_lock); 1165 spin_lock_bh(list_lock);
1165 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, 1166 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
1166 head, hash_entry) { 1167 head, hash_entry) {
1167 hlist_del_rcu(node); 1168 hlist_del_rcu(node);
1168 tt_global = container_of(tt_common_entry, 1169 tt_global = container_of(tt_common_entry,
1169 struct batadv_tt_global_entry, 1170 struct batadv_tt_global_entry,
1170 common); 1171 common);
1171 batadv_tt_global_entry_free_ref(tt_global); 1172 batadv_tt_global_entry_free_ref(tt_global);
1172 } 1173 }
1173 spin_unlock_bh(list_lock); 1174 spin_unlock_bh(list_lock);
1174 } 1175 }
1175 1176
1176 batadv_hash_destroy(hash); 1177 batadv_hash_destroy(hash);
1177 1178
1178 bat_priv->tt.global_hash = NULL; 1179 bat_priv->tt.global_hash = NULL;
1179 } 1180 }
1180 1181
1181 static bool 1182 static bool
1182 _batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry, 1183 _batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1183 struct batadv_tt_global_entry *tt_global_entry) 1184 struct batadv_tt_global_entry *tt_global_entry)
1184 { 1185 {
1185 bool ret = false; 1186 bool ret = false;
1186 1187
1187 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI && 1188 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1188 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI) 1189 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
1189 ret = true; 1190 ret = true;
1190 1191
1191 return ret; 1192 return ret;
1192 } 1193 }
1193 1194
1194 struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv, 1195 struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1195 const uint8_t *src, 1196 const uint8_t *src,
1196 const uint8_t *addr) 1197 const uint8_t *addr)
1197 { 1198 {
1198 struct batadv_tt_local_entry *tt_local_entry = NULL; 1199 struct batadv_tt_local_entry *tt_local_entry = NULL;
1199 struct batadv_tt_global_entry *tt_global_entry = NULL; 1200 struct batadv_tt_global_entry *tt_global_entry = NULL;
1200 struct batadv_orig_node *orig_node = NULL; 1201 struct batadv_orig_node *orig_node = NULL;
1201 struct batadv_neigh_node *router = NULL; 1202 struct batadv_neigh_node *router = NULL;
1202 struct hlist_head *head; 1203 struct hlist_head *head;
1203 struct hlist_node *node; 1204 struct hlist_node *node;
1204 struct batadv_tt_orig_list_entry *orig_entry; 1205 struct batadv_tt_orig_list_entry *orig_entry;
1205 int best_tq; 1206 int best_tq;
1206 1207
1207 if (src && atomic_read(&bat_priv->ap_isolation)) { 1208 if (src && atomic_read(&bat_priv->ap_isolation)) {
1208 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src); 1209 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
1209 if (!tt_local_entry) 1210 if (!tt_local_entry)
1210 goto out; 1211 goto out;
1211 } 1212 }
1212 1213
1213 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr); 1214 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
1214 if (!tt_global_entry) 1215 if (!tt_global_entry)
1215 goto out; 1216 goto out;
1216 1217
1217 /* check whether the clients should not communicate due to AP 1218 /* check whether the clients should not communicate due to AP
1218 * isolation 1219 * isolation
1219 */ 1220 */
1220 if (tt_local_entry && 1221 if (tt_local_entry &&
1221 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry)) 1222 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
1222 goto out; 1223 goto out;
1223 1224
1224 best_tq = 0; 1225 best_tq = 0;
1225 1226
1226 rcu_read_lock(); 1227 rcu_read_lock();
1227 head = &tt_global_entry->orig_list; 1228 head = &tt_global_entry->orig_list;
1228 hlist_for_each_entry_rcu(orig_entry, node, head, list) { 1229 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
1229 router = batadv_orig_node_get_router(orig_entry->orig_node); 1230 router = batadv_orig_node_get_router(orig_entry->orig_node);
1230 if (!router) 1231 if (!router)
1231 continue; 1232 continue;
1232 1233
1233 if (router->tq_avg > best_tq) { 1234 if (router->tq_avg > best_tq) {
1234 orig_node = orig_entry->orig_node; 1235 orig_node = orig_entry->orig_node;
1235 best_tq = router->tq_avg; 1236 best_tq = router->tq_avg;
1236 } 1237 }
1237 batadv_neigh_node_free_ref(router); 1238 batadv_neigh_node_free_ref(router);
1238 } 1239 }
1239 /* found anything? */ 1240 /* found anything? */
1240 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount)) 1241 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1241 orig_node = NULL; 1242 orig_node = NULL;
1242 rcu_read_unlock(); 1243 rcu_read_unlock();
1243 out: 1244 out:
1244 if (tt_global_entry) 1245 if (tt_global_entry)
1245 batadv_tt_global_entry_free_ref(tt_global_entry); 1246 batadv_tt_global_entry_free_ref(tt_global_entry);
1246 if (tt_local_entry) 1247 if (tt_local_entry)
1247 batadv_tt_local_entry_free_ref(tt_local_entry); 1248 batadv_tt_local_entry_free_ref(tt_local_entry);
1248 1249
1249 return orig_node; 1250 return orig_node;
1250 } 1251 }
1251 1252
1252 /* Calculates the checksum of the local table of a given orig_node */ 1253 /* Calculates the checksum of the local table of a given orig_node */
1253 static uint16_t batadv_tt_global_crc(struct batadv_priv *bat_priv, 1254 static uint16_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
1254 struct batadv_orig_node *orig_node) 1255 struct batadv_orig_node *orig_node)
1255 { 1256 {
1256 uint16_t total = 0, total_one; 1257 uint16_t total = 0, total_one;
1257 struct batadv_hashtable *hash = bat_priv->tt.global_hash; 1258 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1258 struct batadv_tt_common_entry *tt_common; 1259 struct batadv_tt_common_entry *tt_common;
1259 struct batadv_tt_global_entry *tt_global; 1260 struct batadv_tt_global_entry *tt_global;
1260 struct hlist_node *node; 1261 struct hlist_node *node;
1261 struct hlist_head *head; 1262 struct hlist_head *head;
1262 uint32_t i; 1263 uint32_t i;
1263 int j; 1264 int j;
1264 1265
1265 for (i = 0; i < hash->size; i++) { 1266 for (i = 0; i < hash->size; i++) {
1266 head = &hash->table[i]; 1267 head = &hash->table[i];
1267 1268
1268 rcu_read_lock(); 1269 rcu_read_lock();
1269 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) { 1270 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
1270 tt_global = container_of(tt_common, 1271 tt_global = container_of(tt_common,
1271 struct batadv_tt_global_entry, 1272 struct batadv_tt_global_entry,
1272 common); 1273 common);
1273 /* Roaming clients are in the global table for 1274 /* Roaming clients are in the global table for
1274 * consistency only. They don't have to be 1275 * consistency only. They don't have to be
1275 * taken into account while computing the 1276 * taken into account while computing the
1276 * global crc 1277 * global crc
1277 */ 1278 */
1278 if (tt_common->flags & BATADV_TT_CLIENT_ROAM) 1279 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
1279 continue; 1280 continue;
1280 /* Temporary clients have not been announced yet, so 1281 /* Temporary clients have not been announced yet, so
1281 * they have to be skipped while computing the global 1282 * they have to be skipped while computing the global
1282 * crc 1283 * crc
1283 */ 1284 */
1284 if (tt_common->flags & BATADV_TT_CLIENT_TEMP) 1285 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
1285 continue; 1286 continue;
1286 1287
1287 /* find out if this global entry is announced by this 1288 /* find out if this global entry is announced by this
1288 * originator 1289 * originator
1289 */ 1290 */
1290 if (!batadv_tt_global_entry_has_orig(tt_global, 1291 if (!batadv_tt_global_entry_has_orig(tt_global,
1291 orig_node)) 1292 orig_node))
1292 continue; 1293 continue;
1293 1294
1294 total_one = 0; 1295 total_one = 0;
1295 for (j = 0; j < ETH_ALEN; j++) 1296 for (j = 0; j < ETH_ALEN; j++)
1296 total_one = crc16_byte(total_one, 1297 total_one = crc16_byte(total_one,
1297 tt_common->addr[j]); 1298 tt_common->addr[j]);
1298 total ^= total_one; 1299 total ^= total_one;
1299 } 1300 }
1300 rcu_read_unlock(); 1301 rcu_read_unlock();
1301 } 1302 }
1302 1303
1303 return total; 1304 return total;
1304 } 1305 }
1305 1306
1306 /* Calculates the checksum of the local table */ 1307 /* Calculates the checksum of the local table */
1307 static uint16_t batadv_tt_local_crc(struct batadv_priv *bat_priv) 1308 static uint16_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
1308 { 1309 {
1309 uint16_t total = 0, total_one; 1310 uint16_t total = 0, total_one;
1310 struct batadv_hashtable *hash = bat_priv->tt.local_hash; 1311 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1311 struct batadv_tt_common_entry *tt_common; 1312 struct batadv_tt_common_entry *tt_common;
1312 struct hlist_node *node; 1313 struct hlist_node *node;
1313 struct hlist_head *head; 1314 struct hlist_head *head;
1314 uint32_t i; 1315 uint32_t i;
1315 int j; 1316 int j;
1316 1317
1317 for (i = 0; i < hash->size; i++) { 1318 for (i = 0; i < hash->size; i++) {
1318 head = &hash->table[i]; 1319 head = &hash->table[i];
1319 1320
1320 rcu_read_lock(); 1321 rcu_read_lock();
1321 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) { 1322 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
1322 /* not yet committed clients have not to be taken into 1323 /* not yet committed clients have not to be taken into
1323 * account while computing the CRC 1324 * account while computing the CRC
1324 */ 1325 */
1325 if (tt_common->flags & BATADV_TT_CLIENT_NEW) 1326 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
1326 continue; 1327 continue;
1327 total_one = 0; 1328 total_one = 0;
1328 for (j = 0; j < ETH_ALEN; j++) 1329 for (j = 0; j < ETH_ALEN; j++)
1329 total_one = crc16_byte(total_one, 1330 total_one = crc16_byte(total_one,
1330 tt_common->addr[j]); 1331 tt_common->addr[j]);
1331 total ^= total_one; 1332 total ^= total_one;
1332 } 1333 }
1333 rcu_read_unlock(); 1334 rcu_read_unlock();
1334 } 1335 }
1335 1336
1336 return total; 1337 return total;
1337 } 1338 }
1338 1339
1339 static void batadv_tt_req_list_free(struct batadv_priv *bat_priv) 1340 static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
1340 { 1341 {
1341 struct batadv_tt_req_node *node, *safe; 1342 struct batadv_tt_req_node *node, *safe;
1342 1343
1343 spin_lock_bh(&bat_priv->tt.req_list_lock); 1344 spin_lock_bh(&bat_priv->tt.req_list_lock);
1344 1345
1345 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) { 1346 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
1346 list_del(&node->list); 1347 list_del(&node->list);
1347 kfree(node); 1348 kfree(node);
1348 } 1349 }
1349 1350
1350 spin_unlock_bh(&bat_priv->tt.req_list_lock); 1351 spin_unlock_bh(&bat_priv->tt.req_list_lock);
1351 } 1352 }
1352 1353
1353 static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv, 1354 static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
1354 struct batadv_orig_node *orig_node, 1355 struct batadv_orig_node *orig_node,
1355 const unsigned char *tt_buff, 1356 const unsigned char *tt_buff,
1356 uint8_t tt_num_changes) 1357 uint8_t tt_num_changes)
1357 { 1358 {
1358 uint16_t tt_buff_len = batadv_tt_len(tt_num_changes); 1359 uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
1359 1360
1360 /* Replace the old buffer only if I received something in the 1361 /* Replace the old buffer only if I received something in the
1361 * last OGM (the OGM could carry no changes) 1362 * last OGM (the OGM could carry no changes)
1362 */ 1363 */
1363 spin_lock_bh(&orig_node->tt_buff_lock); 1364 spin_lock_bh(&orig_node->tt_buff_lock);
1364 if (tt_buff_len > 0) { 1365 if (tt_buff_len > 0) {
1365 kfree(orig_node->tt_buff); 1366 kfree(orig_node->tt_buff);
1366 orig_node->tt_buff_len = 0; 1367 orig_node->tt_buff_len = 0;
1367 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC); 1368 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1368 if (orig_node->tt_buff) { 1369 if (orig_node->tt_buff) {
1369 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len); 1370 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1370 orig_node->tt_buff_len = tt_buff_len; 1371 orig_node->tt_buff_len = tt_buff_len;
1371 } 1372 }
1372 } 1373 }
1373 spin_unlock_bh(&orig_node->tt_buff_lock); 1374 spin_unlock_bh(&orig_node->tt_buff_lock);
1374 } 1375 }
1375 1376
1376 static void batadv_tt_req_purge(struct batadv_priv *bat_priv) 1377 static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
1377 { 1378 {
1378 struct batadv_tt_req_node *node, *safe; 1379 struct batadv_tt_req_node *node, *safe;
1379 1380
1380 spin_lock_bh(&bat_priv->tt.req_list_lock); 1381 spin_lock_bh(&bat_priv->tt.req_list_lock);
1381 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) { 1382 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
1382 if (batadv_has_timed_out(node->issued_at, 1383 if (batadv_has_timed_out(node->issued_at,
1383 BATADV_TT_REQUEST_TIMEOUT)) { 1384 BATADV_TT_REQUEST_TIMEOUT)) {
1384 list_del(&node->list); 1385 list_del(&node->list);
1385 kfree(node); 1386 kfree(node);
1386 } 1387 }
1387 } 1388 }
1388 spin_unlock_bh(&bat_priv->tt.req_list_lock); 1389 spin_unlock_bh(&bat_priv->tt.req_list_lock);
1389 } 1390 }
1390 1391
1391 /* returns the pointer to the new tt_req_node struct if no request 1392 /* returns the pointer to the new tt_req_node struct if no request
1392 * has already been issued for this orig_node, NULL otherwise 1393 * has already been issued for this orig_node, NULL otherwise
1393 */ 1394 */
1394 static struct batadv_tt_req_node * 1395 static struct batadv_tt_req_node *
1395 batadv_new_tt_req_node(struct batadv_priv *bat_priv, 1396 batadv_new_tt_req_node(struct batadv_priv *bat_priv,
1396 struct batadv_orig_node *orig_node) 1397 struct batadv_orig_node *orig_node)
1397 { 1398 {
1398 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL; 1399 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
1399 1400
1400 spin_lock_bh(&bat_priv->tt.req_list_lock); 1401 spin_lock_bh(&bat_priv->tt.req_list_lock);
1401 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) { 1402 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
1402 if (batadv_compare_eth(tt_req_node_tmp, orig_node) && 1403 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
1403 !batadv_has_timed_out(tt_req_node_tmp->issued_at, 1404 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
1404 BATADV_TT_REQUEST_TIMEOUT)) 1405 BATADV_TT_REQUEST_TIMEOUT))
1405 goto unlock; 1406 goto unlock;
1406 } 1407 }
1407 1408
1408 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC); 1409 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1409 if (!tt_req_node) 1410 if (!tt_req_node)
1410 goto unlock; 1411 goto unlock;
1411 1412
1412 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN); 1413 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1413 tt_req_node->issued_at = jiffies; 1414 tt_req_node->issued_at = jiffies;
1414 1415
1415 list_add(&tt_req_node->list, &bat_priv->tt.req_list); 1416 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
1416 unlock: 1417 unlock:
1417 spin_unlock_bh(&bat_priv->tt.req_list_lock); 1418 spin_unlock_bh(&bat_priv->tt.req_list_lock);
1418 return tt_req_node; 1419 return tt_req_node;
1419 } 1420 }
1420 1421
1421 /* data_ptr is useless here, but has to be kept to respect the prototype */ 1422 /* data_ptr is useless here, but has to be kept to respect the prototype */
1422 static int batadv_tt_local_valid_entry(const void *entry_ptr, 1423 static int batadv_tt_local_valid_entry(const void *entry_ptr,
1423 const void *data_ptr) 1424 const void *data_ptr)
1424 { 1425 {
1425 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr; 1426 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1426 1427
1427 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW) 1428 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
1428 return 0; 1429 return 0;
1429 return 1; 1430 return 1;
1430 } 1431 }
1431 1432
1432 static int batadv_tt_global_valid(const void *entry_ptr, 1433 static int batadv_tt_global_valid(const void *entry_ptr,
1433 const void *data_ptr) 1434 const void *data_ptr)
1434 { 1435 {
1435 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr; 1436 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1436 const struct batadv_tt_global_entry *tt_global_entry; 1437 const struct batadv_tt_global_entry *tt_global_entry;
1437 const struct batadv_orig_node *orig_node = data_ptr; 1438 const struct batadv_orig_node *orig_node = data_ptr;
1438 1439
1439 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM || 1440 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
1440 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP) 1441 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
1441 return 0; 1442 return 0;
1442 1443
1443 tt_global_entry = container_of(tt_common_entry, 1444 tt_global_entry = container_of(tt_common_entry,
1444 struct batadv_tt_global_entry, 1445 struct batadv_tt_global_entry,
1445 common); 1446 common);
1446 1447
1447 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node); 1448 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
1448 } 1449 }
1449 1450
1450 static struct sk_buff * 1451 static struct sk_buff *
1451 batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn, 1452 batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
1452 struct batadv_hashtable *hash, 1453 struct batadv_hashtable *hash,
1453 struct batadv_hard_iface *primary_if, 1454 struct batadv_hard_iface *primary_if,
1454 int (*valid_cb)(const void *, const void *), 1455 int (*valid_cb)(const void *, const void *),
1455 void *cb_data) 1456 void *cb_data)
1456 { 1457 {
1457 struct batadv_tt_common_entry *tt_common_entry; 1458 struct batadv_tt_common_entry *tt_common_entry;
1458 struct batadv_tt_query_packet *tt_response; 1459 struct batadv_tt_query_packet *tt_response;
1459 struct batadv_tt_change *tt_change; 1460 struct batadv_tt_change *tt_change;
1460 struct hlist_node *node; 1461 struct hlist_node *node;
1461 struct hlist_head *head; 1462 struct hlist_head *head;
1462 struct sk_buff *skb = NULL; 1463 struct sk_buff *skb = NULL;
1463 uint16_t tt_tot, tt_count; 1464 uint16_t tt_tot, tt_count;
1464 ssize_t tt_query_size = sizeof(struct batadv_tt_query_packet); 1465 ssize_t tt_query_size = sizeof(struct batadv_tt_query_packet);
1465 uint32_t i; 1466 uint32_t i;
1466 size_t len; 1467 size_t len;
1467 1468
1468 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) { 1469 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1469 tt_len = primary_if->soft_iface->mtu - tt_query_size; 1470 tt_len = primary_if->soft_iface->mtu - tt_query_size;
1470 tt_len -= tt_len % sizeof(struct batadv_tt_change); 1471 tt_len -= tt_len % sizeof(struct batadv_tt_change);
1471 } 1472 }
1472 tt_tot = tt_len / sizeof(struct batadv_tt_change); 1473 tt_tot = tt_len / sizeof(struct batadv_tt_change);
1473 1474
1474 len = tt_query_size + tt_len; 1475 len = tt_query_size + tt_len;
1475 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN); 1476 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
1476 if (!skb) 1477 if (!skb)
1477 goto out; 1478 goto out;
1478 1479
1479 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN); 1480 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
1480 tt_response = (struct batadv_tt_query_packet *)skb_put(skb, len); 1481 tt_response = (struct batadv_tt_query_packet *)skb_put(skb, len);
1481 tt_response->ttvn = ttvn; 1482 tt_response->ttvn = ttvn;
1482 1483
1483 tt_change = (struct batadv_tt_change *)(skb->data + tt_query_size); 1484 tt_change = (struct batadv_tt_change *)(skb->data + tt_query_size);
1484 tt_count = 0; 1485 tt_count = 0;
1485 1486
1486 rcu_read_lock(); 1487 rcu_read_lock();
1487 for (i = 0; i < hash->size; i++) { 1488 for (i = 0; i < hash->size; i++) {
1488 head = &hash->table[i]; 1489 head = &hash->table[i];
1489 1490
1490 hlist_for_each_entry_rcu(tt_common_entry, node, 1491 hlist_for_each_entry_rcu(tt_common_entry, node,
1491 head, hash_entry) { 1492 head, hash_entry) {
1492 if (tt_count == tt_tot) 1493 if (tt_count == tt_tot)
1493 break; 1494 break;
1494 1495
1495 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data))) 1496 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
1496 continue; 1497 continue;
1497 1498
1498 memcpy(tt_change->addr, tt_common_entry->addr, 1499 memcpy(tt_change->addr, tt_common_entry->addr,
1499 ETH_ALEN); 1500 ETH_ALEN);
1500 tt_change->flags = BATADV_NO_FLAGS; 1501 tt_change->flags = BATADV_NO_FLAGS;
1501 1502
1502 tt_count++; 1503 tt_count++;
1503 tt_change++; 1504 tt_change++;
1504 } 1505 }
1505 } 1506 }
1506 rcu_read_unlock(); 1507 rcu_read_unlock();
1507 1508
1508 /* store in the message the number of entries we have successfully 1509 /* store in the message the number of entries we have successfully
1509 * copied 1510 * copied
1510 */ 1511 */
1511 tt_response->tt_data = htons(tt_count); 1512 tt_response->tt_data = htons(tt_count);
1512 1513
1513 out: 1514 out:
1514 return skb; 1515 return skb;
1515 } 1516 }
1516 1517
1517 static int batadv_send_tt_request(struct batadv_priv *bat_priv, 1518 static int batadv_send_tt_request(struct batadv_priv *bat_priv,
1518 struct batadv_orig_node *dst_orig_node, 1519 struct batadv_orig_node *dst_orig_node,
1519 uint8_t ttvn, uint16_t tt_crc, 1520 uint8_t ttvn, uint16_t tt_crc,
1520 bool full_table) 1521 bool full_table)
1521 { 1522 {
1522 struct sk_buff *skb = NULL; 1523 struct sk_buff *skb = NULL;
1523 struct batadv_tt_query_packet *tt_request; 1524 struct batadv_tt_query_packet *tt_request;
1524 struct batadv_neigh_node *neigh_node = NULL; 1525 struct batadv_neigh_node *neigh_node = NULL;
1525 struct batadv_hard_iface *primary_if; 1526 struct batadv_hard_iface *primary_if;
1526 struct batadv_tt_req_node *tt_req_node = NULL; 1527 struct batadv_tt_req_node *tt_req_node = NULL;
1527 int ret = 1; 1528 int ret = 1;
1528 size_t tt_req_len; 1529 size_t tt_req_len;
1529 1530
1530 primary_if = batadv_primary_if_get_selected(bat_priv); 1531 primary_if = batadv_primary_if_get_selected(bat_priv);
1531 if (!primary_if) 1532 if (!primary_if)
1532 goto out; 1533 goto out;
1533 1534
1534 /* The new tt_req will be issued only if I'm not waiting for a 1535 /* The new tt_req will be issued only if I'm not waiting for a
1535 * reply from the same orig_node yet 1536 * reply from the same orig_node yet
1536 */ 1537 */
1537 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node); 1538 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
1538 if (!tt_req_node) 1539 if (!tt_req_node)
1539 goto out; 1540 goto out;
1540 1541
1541 skb = dev_alloc_skb(sizeof(*tt_request) + ETH_HLEN + NET_IP_ALIGN); 1542 skb = dev_alloc_skb(sizeof(*tt_request) + ETH_HLEN + NET_IP_ALIGN);
1542 if (!skb) 1543 if (!skb)
1543 goto out; 1544 goto out;
1544 1545
1545 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN); 1546 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
1546 1547
1547 tt_req_len = sizeof(*tt_request); 1548 tt_req_len = sizeof(*tt_request);
1548 tt_request = (struct batadv_tt_query_packet *)skb_put(skb, tt_req_len); 1549 tt_request = (struct batadv_tt_query_packet *)skb_put(skb, tt_req_len);
1549 1550
1550 tt_request->header.packet_type = BATADV_TT_QUERY; 1551 tt_request->header.packet_type = BATADV_TT_QUERY;
1551 tt_request->header.version = BATADV_COMPAT_VERSION; 1552 tt_request->header.version = BATADV_COMPAT_VERSION;
1552 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN); 1553 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1553 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN); 1554 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
1554 tt_request->header.ttl = BATADV_TTL; 1555 tt_request->header.ttl = BATADV_TTL;
1555 tt_request->ttvn = ttvn; 1556 tt_request->ttvn = ttvn;
1556 tt_request->tt_data = htons(tt_crc); 1557 tt_request->tt_data = htons(tt_crc);
1557 tt_request->flags = BATADV_TT_REQUEST; 1558 tt_request->flags = BATADV_TT_REQUEST;
1558 1559
1559 if (full_table) 1560 if (full_table)
1560 tt_request->flags |= BATADV_TT_FULL_TABLE; 1561 tt_request->flags |= BATADV_TT_FULL_TABLE;
1561 1562
1562 neigh_node = batadv_orig_node_get_router(dst_orig_node); 1563 neigh_node = batadv_orig_node_get_router(dst_orig_node);
1563 if (!neigh_node) 1564 if (!neigh_node)
1564 goto out; 1565 goto out;
1565 1566
1566 batadv_dbg(BATADV_DBG_TT, bat_priv, 1567 batadv_dbg(BATADV_DBG_TT, bat_priv,
1567 "Sending TT_REQUEST to %pM via %pM [%c]\n", 1568 "Sending TT_REQUEST to %pM via %pM [%c]\n",
1568 dst_orig_node->orig, neigh_node->addr, 1569 dst_orig_node->orig, neigh_node->addr,
1569 (full_table ? 'F' : '.')); 1570 (full_table ? 'F' : '.'));
1570 1571
1571 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX); 1572 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
1572 1573
1573 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr); 1574 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1574 ret = 0; 1575 ret = 0;
1575 1576
1576 out: 1577 out:
1577 if (neigh_node) 1578 if (neigh_node)
1578 batadv_neigh_node_free_ref(neigh_node); 1579 batadv_neigh_node_free_ref(neigh_node);
1579 if (primary_if) 1580 if (primary_if)
1580 batadv_hardif_free_ref(primary_if); 1581 batadv_hardif_free_ref(primary_if);
1581 if (ret) 1582 if (ret)
1582 kfree_skb(skb); 1583 kfree_skb(skb);
1583 if (ret && tt_req_node) { 1584 if (ret && tt_req_node) {
1584 spin_lock_bh(&bat_priv->tt.req_list_lock); 1585 spin_lock_bh(&bat_priv->tt.req_list_lock);
1585 list_del(&tt_req_node->list); 1586 list_del(&tt_req_node->list);
1586 spin_unlock_bh(&bat_priv->tt.req_list_lock); 1587 spin_unlock_bh(&bat_priv->tt.req_list_lock);
1587 kfree(tt_req_node); 1588 kfree(tt_req_node);
1588 } 1589 }
1589 return ret; 1590 return ret;
1590 } 1591 }
1591 1592
1592 static bool 1593 static bool
1593 batadv_send_other_tt_response(struct batadv_priv *bat_priv, 1594 batadv_send_other_tt_response(struct batadv_priv *bat_priv,
1594 struct batadv_tt_query_packet *tt_request) 1595 struct batadv_tt_query_packet *tt_request)
1595 { 1596 {
1596 struct batadv_orig_node *req_dst_orig_node = NULL; 1597 struct batadv_orig_node *req_dst_orig_node = NULL;
1597 struct batadv_orig_node *res_dst_orig_node = NULL; 1598 struct batadv_orig_node *res_dst_orig_node = NULL;
1598 struct batadv_neigh_node *neigh_node = NULL; 1599 struct batadv_neigh_node *neigh_node = NULL;
1599 struct batadv_hard_iface *primary_if = NULL; 1600 struct batadv_hard_iface *primary_if = NULL;
1600 uint8_t orig_ttvn, req_ttvn, ttvn; 1601 uint8_t orig_ttvn, req_ttvn, ttvn;
1601 int ret = false; 1602 int ret = false;
1602 unsigned char *tt_buff; 1603 unsigned char *tt_buff;
1603 bool full_table; 1604 bool full_table;
1604 uint16_t tt_len, tt_tot; 1605 uint16_t tt_len, tt_tot;
1605 struct sk_buff *skb = NULL; 1606 struct sk_buff *skb = NULL;
1606 struct batadv_tt_query_packet *tt_response; 1607 struct batadv_tt_query_packet *tt_response;
1607 uint8_t *packet_pos; 1608 uint8_t *packet_pos;
1608 size_t len; 1609 size_t len;
1609 1610
1610 batadv_dbg(BATADV_DBG_TT, bat_priv, 1611 batadv_dbg(BATADV_DBG_TT, bat_priv,
1611 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n", 1612 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1612 tt_request->src, tt_request->ttvn, tt_request->dst, 1613 tt_request->src, tt_request->ttvn, tt_request->dst,
1613 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.')); 1614 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1614 1615
1615 /* Let's get the orig node of the REAL destination */ 1616 /* Let's get the orig node of the REAL destination */
1616 req_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->dst); 1617 req_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->dst);
1617 if (!req_dst_orig_node) 1618 if (!req_dst_orig_node)
1618 goto out; 1619 goto out;
1619 1620
1620 res_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->src); 1621 res_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
1621 if (!res_dst_orig_node) 1622 if (!res_dst_orig_node)
1622 goto out; 1623 goto out;
1623 1624
1624 neigh_node = batadv_orig_node_get_router(res_dst_orig_node); 1625 neigh_node = batadv_orig_node_get_router(res_dst_orig_node);
1625 if (!neigh_node) 1626 if (!neigh_node)
1626 goto out; 1627 goto out;
1627 1628
1628 primary_if = batadv_primary_if_get_selected(bat_priv); 1629 primary_if = batadv_primary_if_get_selected(bat_priv);
1629 if (!primary_if) 1630 if (!primary_if)
1630 goto out; 1631 goto out;
1631 1632
1632 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn); 1633 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1633 req_ttvn = tt_request->ttvn; 1634 req_ttvn = tt_request->ttvn;
1634 1635
1635 /* I don't have the requested data */ 1636 /* I don't have the requested data */
1636 if (orig_ttvn != req_ttvn || 1637 if (orig_ttvn != req_ttvn ||
1637 tt_request->tt_data != htons(req_dst_orig_node->tt_crc)) 1638 tt_request->tt_data != htons(req_dst_orig_node->tt_crc))
1638 goto out; 1639 goto out;
1639 1640
1640 /* If the full table has been explicitly requested */ 1641 /* If the full table has been explicitly requested */
1641 if (tt_request->flags & BATADV_TT_FULL_TABLE || 1642 if (tt_request->flags & BATADV_TT_FULL_TABLE ||
1642 !req_dst_orig_node->tt_buff) 1643 !req_dst_orig_node->tt_buff)
1643 full_table = true; 1644 full_table = true;
1644 else 1645 else
1645 full_table = false; 1646 full_table = false;
1646 1647
1647 /* In this version, fragmentation is not implemented, then 1648 /* In this version, fragmentation is not implemented, then
1648 * I'll send only one packet with as much TT entries as I can 1649 * I'll send only one packet with as much TT entries as I can
1649 */ 1650 */
1650 if (!full_table) { 1651 if (!full_table) {
1651 spin_lock_bh(&req_dst_orig_node->tt_buff_lock); 1652 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1652 tt_len = req_dst_orig_node->tt_buff_len; 1653 tt_len = req_dst_orig_node->tt_buff_len;
1653 tt_tot = tt_len / sizeof(struct batadv_tt_change); 1654 tt_tot = tt_len / sizeof(struct batadv_tt_change);
1654 1655
1655 len = sizeof(*tt_response) + tt_len; 1656 len = sizeof(*tt_response) + tt_len;
1656 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN); 1657 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
1657 if (!skb) 1658 if (!skb)
1658 goto unlock; 1659 goto unlock;
1659 1660
1660 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN); 1661 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
1661 packet_pos = skb_put(skb, len); 1662 packet_pos = skb_put(skb, len);
1662 tt_response = (struct batadv_tt_query_packet *)packet_pos; 1663 tt_response = (struct batadv_tt_query_packet *)packet_pos;
1663 tt_response->ttvn = req_ttvn; 1664 tt_response->ttvn = req_ttvn;
1664 tt_response->tt_data = htons(tt_tot); 1665 tt_response->tt_data = htons(tt_tot);
1665 1666
1666 tt_buff = skb->data + sizeof(*tt_response); 1667 tt_buff = skb->data + sizeof(*tt_response);
1667 /* Copy the last orig_node's OGM buffer */ 1668 /* Copy the last orig_node's OGM buffer */
1668 memcpy(tt_buff, req_dst_orig_node->tt_buff, 1669 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1669 req_dst_orig_node->tt_buff_len); 1670 req_dst_orig_node->tt_buff_len);
1670 1671
1671 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock); 1672 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1672 } else { 1673 } else {
1673 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size); 1674 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
1674 tt_len *= sizeof(struct batadv_tt_change); 1675 tt_len *= sizeof(struct batadv_tt_change);
1675 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn); 1676 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1676 1677
1677 skb = batadv_tt_response_fill_table(tt_len, ttvn, 1678 skb = batadv_tt_response_fill_table(tt_len, ttvn,
1678 bat_priv->tt.global_hash, 1679 bat_priv->tt.global_hash,
1679 primary_if, 1680 primary_if,
1680 batadv_tt_global_valid, 1681 batadv_tt_global_valid,
1681 req_dst_orig_node); 1682 req_dst_orig_node);
1682 if (!skb) 1683 if (!skb)
1683 goto out; 1684 goto out;
1684 1685
1685 tt_response = (struct batadv_tt_query_packet *)skb->data; 1686 tt_response = (struct batadv_tt_query_packet *)skb->data;
1686 } 1687 }
1687 1688
1688 tt_response->header.packet_type = BATADV_TT_QUERY; 1689 tt_response->header.packet_type = BATADV_TT_QUERY;
1689 tt_response->header.version = BATADV_COMPAT_VERSION; 1690 tt_response->header.version = BATADV_COMPAT_VERSION;
1690 tt_response->header.ttl = BATADV_TTL; 1691 tt_response->header.ttl = BATADV_TTL;
1691 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN); 1692 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1692 memcpy(tt_response->dst, tt_request->src, ETH_ALEN); 1693 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1693 tt_response->flags = BATADV_TT_RESPONSE; 1694 tt_response->flags = BATADV_TT_RESPONSE;
1694 1695
1695 if (full_table) 1696 if (full_table)
1696 tt_response->flags |= BATADV_TT_FULL_TABLE; 1697 tt_response->flags |= BATADV_TT_FULL_TABLE;
1697 1698
1698 batadv_dbg(BATADV_DBG_TT, bat_priv, 1699 batadv_dbg(BATADV_DBG_TT, bat_priv,
1699 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n", 1700 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1700 res_dst_orig_node->orig, neigh_node->addr, 1701 res_dst_orig_node->orig, neigh_node->addr,
1701 req_dst_orig_node->orig, req_ttvn); 1702 req_dst_orig_node->orig, req_ttvn);
1702 1703
1703 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX); 1704 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
1704 1705
1705 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr); 1706 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1706 ret = true; 1707 ret = true;
1707 goto out; 1708 goto out;
1708 1709
1709 unlock: 1710 unlock:
1710 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock); 1711 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1711 1712
1712 out: 1713 out:
1713 if (res_dst_orig_node) 1714 if (res_dst_orig_node)
1714 batadv_orig_node_free_ref(res_dst_orig_node); 1715 batadv_orig_node_free_ref(res_dst_orig_node);
1715 if (req_dst_orig_node) 1716 if (req_dst_orig_node)
1716 batadv_orig_node_free_ref(req_dst_orig_node); 1717 batadv_orig_node_free_ref(req_dst_orig_node);
1717 if (neigh_node) 1718 if (neigh_node)
1718 batadv_neigh_node_free_ref(neigh_node); 1719 batadv_neigh_node_free_ref(neigh_node);
1719 if (primary_if) 1720 if (primary_if)
1720 batadv_hardif_free_ref(primary_if); 1721 batadv_hardif_free_ref(primary_if);
1721 if (!ret) 1722 if (!ret)
1722 kfree_skb(skb); 1723 kfree_skb(skb);
1723 return ret; 1724 return ret;
1724 1725
1725 } 1726 }
1726 1727
1727 static bool 1728 static bool
1728 batadv_send_my_tt_response(struct batadv_priv *bat_priv, 1729 batadv_send_my_tt_response(struct batadv_priv *bat_priv,
1729 struct batadv_tt_query_packet *tt_request) 1730 struct batadv_tt_query_packet *tt_request)
1730 { 1731 {
1731 struct batadv_orig_node *orig_node = NULL; 1732 struct batadv_orig_node *orig_node = NULL;
1732 struct batadv_neigh_node *neigh_node = NULL; 1733 struct batadv_neigh_node *neigh_node = NULL;
1733 struct batadv_hard_iface *primary_if = NULL; 1734 struct batadv_hard_iface *primary_if = NULL;
1734 uint8_t my_ttvn, req_ttvn, ttvn; 1735 uint8_t my_ttvn, req_ttvn, ttvn;
1735 int ret = false; 1736 int ret = false;
1736 unsigned char *tt_buff; 1737 unsigned char *tt_buff;
1737 bool full_table; 1738 bool full_table;
1738 uint16_t tt_len, tt_tot; 1739 uint16_t tt_len, tt_tot;
1739 struct sk_buff *skb = NULL; 1740 struct sk_buff *skb = NULL;
1740 struct batadv_tt_query_packet *tt_response; 1741 struct batadv_tt_query_packet *tt_response;
1741 uint8_t *packet_pos; 1742 uint8_t *packet_pos;
1742 size_t len; 1743 size_t len;
1743 1744
1744 batadv_dbg(BATADV_DBG_TT, bat_priv, 1745 batadv_dbg(BATADV_DBG_TT, bat_priv,
1745 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n", 1746 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1746 tt_request->src, tt_request->ttvn, 1747 tt_request->src, tt_request->ttvn,
1747 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.')); 1748 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1748 1749
1749 1750
1750 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn); 1751 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
1751 req_ttvn = tt_request->ttvn; 1752 req_ttvn = tt_request->ttvn;
1752 1753
1753 orig_node = batadv_orig_hash_find(bat_priv, tt_request->src); 1754 orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
1754 if (!orig_node) 1755 if (!orig_node)
1755 goto out; 1756 goto out;
1756 1757
1757 neigh_node = batadv_orig_node_get_router(orig_node); 1758 neigh_node = batadv_orig_node_get_router(orig_node);
1758 if (!neigh_node) 1759 if (!neigh_node)
1759 goto out; 1760 goto out;
1760 1761
1761 primary_if = batadv_primary_if_get_selected(bat_priv); 1762 primary_if = batadv_primary_if_get_selected(bat_priv);
1762 if (!primary_if) 1763 if (!primary_if)
1763 goto out; 1764 goto out;
1764 1765
1765 /* If the full table has been explicitly requested or the gap 1766 /* If the full table has been explicitly requested or the gap
1766 * is too big send the whole local translation table 1767 * is too big send the whole local translation table
1767 */ 1768 */
1768 if (tt_request->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn || 1769 if (tt_request->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
1769 !bat_priv->tt.last_changeset) 1770 !bat_priv->tt.last_changeset)
1770 full_table = true; 1771 full_table = true;
1771 else 1772 else
1772 full_table = false; 1773 full_table = false;
1773 1774
1774 /* In this version, fragmentation is not implemented, then 1775 /* In this version, fragmentation is not implemented, then
1775 * I'll send only one packet with as much TT entries as I can 1776 * I'll send only one packet with as much TT entries as I can
1776 */ 1777 */
1777 if (!full_table) { 1778 if (!full_table) {
1778 spin_lock_bh(&bat_priv->tt.last_changeset_lock); 1779 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1779 tt_len = bat_priv->tt.last_changeset_len; 1780 tt_len = bat_priv->tt.last_changeset_len;
1780 tt_tot = tt_len / sizeof(struct batadv_tt_change); 1781 tt_tot = tt_len / sizeof(struct batadv_tt_change);
1781 1782
1782 len = sizeof(*tt_response) + tt_len; 1783 len = sizeof(*tt_response) + tt_len;
1783 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN); 1784 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
1784 if (!skb) 1785 if (!skb)
1785 goto unlock; 1786 goto unlock;
1786 1787
1787 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN); 1788 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
1788 packet_pos = skb_put(skb, len); 1789 packet_pos = skb_put(skb, len);
1789 tt_response = (struct batadv_tt_query_packet *)packet_pos; 1790 tt_response = (struct batadv_tt_query_packet *)packet_pos;
1790 tt_response->ttvn = req_ttvn; 1791 tt_response->ttvn = req_ttvn;
1791 tt_response->tt_data = htons(tt_tot); 1792 tt_response->tt_data = htons(tt_tot);
1792 1793
1793 tt_buff = skb->data + sizeof(*tt_response); 1794 tt_buff = skb->data + sizeof(*tt_response);
1794 memcpy(tt_buff, bat_priv->tt.last_changeset, 1795 memcpy(tt_buff, bat_priv->tt.last_changeset,
1795 bat_priv->tt.last_changeset_len); 1796 bat_priv->tt.last_changeset_len);
1796 spin_unlock_bh(&bat_priv->tt.last_changeset_lock); 1797 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1797 } else { 1798 } else {
1798 tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num); 1799 tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num);
1799 tt_len *= sizeof(struct batadv_tt_change); 1800 tt_len *= sizeof(struct batadv_tt_change);
1800 ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn); 1801 ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
1801 1802
1802 skb = batadv_tt_response_fill_table(tt_len, ttvn, 1803 skb = batadv_tt_response_fill_table(tt_len, ttvn,
1803 bat_priv->tt.local_hash, 1804 bat_priv->tt.local_hash,
1804 primary_if, 1805 primary_if,
1805 batadv_tt_local_valid_entry, 1806 batadv_tt_local_valid_entry,
1806 NULL); 1807 NULL);
1807 if (!skb) 1808 if (!skb)
1808 goto out; 1809 goto out;
1809 1810
1810 tt_response = (struct batadv_tt_query_packet *)skb->data; 1811 tt_response = (struct batadv_tt_query_packet *)skb->data;
1811 } 1812 }
1812 1813
1813 tt_response->header.packet_type = BATADV_TT_QUERY; 1814 tt_response->header.packet_type = BATADV_TT_QUERY;
1814 tt_response->header.version = BATADV_COMPAT_VERSION; 1815 tt_response->header.version = BATADV_COMPAT_VERSION;
1815 tt_response->header.ttl = BATADV_TTL; 1816 tt_response->header.ttl = BATADV_TTL;
1816 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN); 1817 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1817 memcpy(tt_response->dst, tt_request->src, ETH_ALEN); 1818 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1818 tt_response->flags = BATADV_TT_RESPONSE; 1819 tt_response->flags = BATADV_TT_RESPONSE;
1819 1820
1820 if (full_table) 1821 if (full_table)
1821 tt_response->flags |= BATADV_TT_FULL_TABLE; 1822 tt_response->flags |= BATADV_TT_FULL_TABLE;
1822 1823
1823 batadv_dbg(BATADV_DBG_TT, bat_priv, 1824 batadv_dbg(BATADV_DBG_TT, bat_priv,
1824 "Sending TT_RESPONSE to %pM via %pM [%c]\n", 1825 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1825 orig_node->orig, neigh_node->addr, 1826 orig_node->orig, neigh_node->addr,
1826 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.')); 1827 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1827 1828
1828 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX); 1829 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
1829 1830
1830 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr); 1831 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1831 ret = true; 1832 ret = true;
1832 goto out; 1833 goto out;
1833 1834
1834 unlock: 1835 unlock:
1835 spin_unlock_bh(&bat_priv->tt.last_changeset_lock); 1836 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1836 out: 1837 out:
1837 if (orig_node) 1838 if (orig_node)
1838 batadv_orig_node_free_ref(orig_node); 1839 batadv_orig_node_free_ref(orig_node);
1839 if (neigh_node) 1840 if (neigh_node)
1840 batadv_neigh_node_free_ref(neigh_node); 1841 batadv_neigh_node_free_ref(neigh_node);
1841 if (primary_if) 1842 if (primary_if)
1842 batadv_hardif_free_ref(primary_if); 1843 batadv_hardif_free_ref(primary_if);
1843 if (!ret) 1844 if (!ret)
1844 kfree_skb(skb); 1845 kfree_skb(skb);
1845 /* This packet was for me, so it doesn't need to be re-routed */ 1846 /* This packet was for me, so it doesn't need to be re-routed */
1846 return true; 1847 return true;
1847 } 1848 }
1848 1849
1849 bool batadv_send_tt_response(struct batadv_priv *bat_priv, 1850 bool batadv_send_tt_response(struct batadv_priv *bat_priv,
1850 struct batadv_tt_query_packet *tt_request) 1851 struct batadv_tt_query_packet *tt_request)
1851 { 1852 {
1852 if (batadv_is_my_mac(tt_request->dst)) { 1853 if (batadv_is_my_mac(tt_request->dst)) {
1853 /* don't answer backbone gws! */ 1854 /* don't answer backbone gws! */
1854 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src)) 1855 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src))
1855 return true; 1856 return true;
1856 1857
1857 return batadv_send_my_tt_response(bat_priv, tt_request); 1858 return batadv_send_my_tt_response(bat_priv, tt_request);
1858 } else { 1859 } else {
1859 return batadv_send_other_tt_response(bat_priv, tt_request); 1860 return batadv_send_other_tt_response(bat_priv, tt_request);
1860 } 1861 }
1861 } 1862 }
1862 1863
1863 static void _batadv_tt_update_changes(struct batadv_priv *bat_priv, 1864 static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
1864 struct batadv_orig_node *orig_node, 1865 struct batadv_orig_node *orig_node,
1865 struct batadv_tt_change *tt_change, 1866 struct batadv_tt_change *tt_change,
1866 uint16_t tt_num_changes, uint8_t ttvn) 1867 uint16_t tt_num_changes, uint8_t ttvn)
1867 { 1868 {
1868 int i; 1869 int i;
1869 int roams; 1870 int roams;
1870 1871
1871 for (i = 0; i < tt_num_changes; i++) { 1872 for (i = 0; i < tt_num_changes; i++) {
1872 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) { 1873 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
1873 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM; 1874 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
1874 batadv_tt_global_del(bat_priv, orig_node, 1875 batadv_tt_global_del(bat_priv, orig_node,
1875 (tt_change + i)->addr, 1876 (tt_change + i)->addr,
1876 "tt removed by changes", 1877 "tt removed by changes",
1877 roams); 1878 roams);
1878 } else { 1879 } else {
1879 if (!batadv_tt_global_add(bat_priv, orig_node, 1880 if (!batadv_tt_global_add(bat_priv, orig_node,
1880 (tt_change + i)->addr, 1881 (tt_change + i)->addr,
1881 (tt_change + i)->flags, ttvn)) 1882 (tt_change + i)->flags, ttvn))
1882 /* In case of problem while storing a 1883 /* In case of problem while storing a
1883 * global_entry, we stop the updating 1884 * global_entry, we stop the updating
1884 * procedure without committing the 1885 * procedure without committing the
1885 * ttvn change. This will avoid to send 1886 * ttvn change. This will avoid to send
1886 * corrupted data on tt_request 1887 * corrupted data on tt_request
1887 */ 1888 */
1888 return; 1889 return;
1889 } 1890 }
1890 } 1891 }
1891 orig_node->tt_initialised = true; 1892 orig_node->tt_initialised = true;
1892 } 1893 }
1893 1894
1894 static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv, 1895 static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
1895 struct batadv_tt_query_packet *tt_response) 1896 struct batadv_tt_query_packet *tt_response)
1896 { 1897 {
1897 struct batadv_orig_node *orig_node = NULL; 1898 struct batadv_orig_node *orig_node = NULL;
1898 1899
1899 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src); 1900 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
1900 if (!orig_node) 1901 if (!orig_node)
1901 goto out; 1902 goto out;
1902 1903
1903 /* Purge the old table first.. */ 1904 /* Purge the old table first.. */
1904 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table"); 1905 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
1905 1906
1906 _batadv_tt_update_changes(bat_priv, orig_node, 1907 _batadv_tt_update_changes(bat_priv, orig_node,
1907 (struct batadv_tt_change *)(tt_response + 1), 1908 (struct batadv_tt_change *)(tt_response + 1),
1908 ntohs(tt_response->tt_data), 1909 ntohs(tt_response->tt_data),
1909 tt_response->ttvn); 1910 tt_response->ttvn);
1910 1911
1911 spin_lock_bh(&orig_node->tt_buff_lock); 1912 spin_lock_bh(&orig_node->tt_buff_lock);
1912 kfree(orig_node->tt_buff); 1913 kfree(orig_node->tt_buff);
1913 orig_node->tt_buff_len = 0; 1914 orig_node->tt_buff_len = 0;
1914 orig_node->tt_buff = NULL; 1915 orig_node->tt_buff = NULL;
1915 spin_unlock_bh(&orig_node->tt_buff_lock); 1916 spin_unlock_bh(&orig_node->tt_buff_lock);
1916 1917
1917 atomic_set(&orig_node->last_ttvn, tt_response->ttvn); 1918 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
1918 1919
1919 out: 1920 out:
1920 if (orig_node) 1921 if (orig_node)
1921 batadv_orig_node_free_ref(orig_node); 1922 batadv_orig_node_free_ref(orig_node);
1922 } 1923 }
1923 1924
1924 static void batadv_tt_update_changes(struct batadv_priv *bat_priv, 1925 static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
1925 struct batadv_orig_node *orig_node, 1926 struct batadv_orig_node *orig_node,
1926 uint16_t tt_num_changes, uint8_t ttvn, 1927 uint16_t tt_num_changes, uint8_t ttvn,
1927 struct batadv_tt_change *tt_change) 1928 struct batadv_tt_change *tt_change)
1928 { 1929 {
1929 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, 1930 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
1930 tt_num_changes, ttvn); 1931 tt_num_changes, ttvn);
1931 1932
1932 batadv_tt_save_orig_buffer(bat_priv, orig_node, 1933 batadv_tt_save_orig_buffer(bat_priv, orig_node,
1933 (unsigned char *)tt_change, tt_num_changes); 1934 (unsigned char *)tt_change, tt_num_changes);
1934 atomic_set(&orig_node->last_ttvn, ttvn); 1935 atomic_set(&orig_node->last_ttvn, ttvn);
1935 } 1936 }
1936 1937
1937 bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr) 1938 bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr)
1938 { 1939 {
1939 struct batadv_tt_local_entry *tt_local_entry = NULL; 1940 struct batadv_tt_local_entry *tt_local_entry = NULL;
1940 bool ret = false; 1941 bool ret = false;
1941 1942
1942 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr); 1943 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
1943 if (!tt_local_entry) 1944 if (!tt_local_entry)
1944 goto out; 1945 goto out;
1945 /* Check if the client has been logically deleted (but is kept for 1946 /* Check if the client has been logically deleted (but is kept for
1946 * consistency purpose) 1947 * consistency purpose)
1947 */ 1948 */
1948 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) 1949 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
1950 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
1949 goto out; 1951 goto out;
1950 ret = true; 1952 ret = true;
1951 out: 1953 out:
1952 if (tt_local_entry) 1954 if (tt_local_entry)
1953 batadv_tt_local_entry_free_ref(tt_local_entry); 1955 batadv_tt_local_entry_free_ref(tt_local_entry);
1954 return ret; 1956 return ret;
1955 } 1957 }
1956 1958
1957 void batadv_handle_tt_response(struct batadv_priv *bat_priv, 1959 void batadv_handle_tt_response(struct batadv_priv *bat_priv,
1958 struct batadv_tt_query_packet *tt_response) 1960 struct batadv_tt_query_packet *tt_response)
1959 { 1961 {
1960 struct batadv_tt_req_node *node, *safe; 1962 struct batadv_tt_req_node *node, *safe;
1961 struct batadv_orig_node *orig_node = NULL; 1963 struct batadv_orig_node *orig_node = NULL;
1962 struct batadv_tt_change *tt_change; 1964 struct batadv_tt_change *tt_change;
1963 1965
1964 batadv_dbg(BATADV_DBG_TT, bat_priv, 1966 batadv_dbg(BATADV_DBG_TT, bat_priv,
1965 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n", 1967 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
1966 tt_response->src, tt_response->ttvn, 1968 tt_response->src, tt_response->ttvn,
1967 ntohs(tt_response->tt_data), 1969 ntohs(tt_response->tt_data),
1968 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.')); 1970 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1969 1971
1970 /* we should have never asked a backbone gw */ 1972 /* we should have never asked a backbone gw */
1971 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src)) 1973 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
1972 goto out; 1974 goto out;
1973 1975
1974 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src); 1976 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
1975 if (!orig_node) 1977 if (!orig_node)
1976 goto out; 1978 goto out;
1977 1979
1978 if (tt_response->flags & BATADV_TT_FULL_TABLE) { 1980 if (tt_response->flags & BATADV_TT_FULL_TABLE) {
1979 batadv_tt_fill_gtable(bat_priv, tt_response); 1981 batadv_tt_fill_gtable(bat_priv, tt_response);
1980 } else { 1982 } else {
1981 tt_change = (struct batadv_tt_change *)(tt_response + 1); 1983 tt_change = (struct batadv_tt_change *)(tt_response + 1);
1982 batadv_tt_update_changes(bat_priv, orig_node, 1984 batadv_tt_update_changes(bat_priv, orig_node,
1983 ntohs(tt_response->tt_data), 1985 ntohs(tt_response->tt_data),
1984 tt_response->ttvn, tt_change); 1986 tt_response->ttvn, tt_change);
1985 } 1987 }
1986 1988
1987 /* Delete the tt_req_node from pending tt_requests list */ 1989 /* Delete the tt_req_node from pending tt_requests list */
1988 spin_lock_bh(&bat_priv->tt.req_list_lock); 1990 spin_lock_bh(&bat_priv->tt.req_list_lock);
1989 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) { 1991 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
1990 if (!batadv_compare_eth(node->addr, tt_response->src)) 1992 if (!batadv_compare_eth(node->addr, tt_response->src))
1991 continue; 1993 continue;
1992 list_del(&node->list); 1994 list_del(&node->list);
1993 kfree(node); 1995 kfree(node);
1994 } 1996 }
1995 spin_unlock_bh(&bat_priv->tt.req_list_lock); 1997 spin_unlock_bh(&bat_priv->tt.req_list_lock);
1996 1998
1997 /* Recalculate the CRC for this orig_node and store it */ 1999 /* Recalculate the CRC for this orig_node and store it */
1998 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node); 2000 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
1999 /* Roaming phase is over: tables are in sync again. I can
2000 * unset the flag
2001 */
2002 orig_node->tt_poss_change = false;
2003 out: 2001 out:
2004 if (orig_node) 2002 if (orig_node)
2005 batadv_orig_node_free_ref(orig_node); 2003 batadv_orig_node_free_ref(orig_node);
2006 } 2004 }
2007 2005
2008 int batadv_tt_init(struct batadv_priv *bat_priv) 2006 int batadv_tt_init(struct batadv_priv *bat_priv)
2009 { 2007 {
2010 int ret; 2008 int ret;
2011 2009
2012 ret = batadv_tt_local_init(bat_priv); 2010 ret = batadv_tt_local_init(bat_priv);
2013 if (ret < 0) 2011 if (ret < 0)
2014 return ret; 2012 return ret;
2015 2013
2016 ret = batadv_tt_global_init(bat_priv); 2014 ret = batadv_tt_global_init(bat_priv);
2017 if (ret < 0) 2015 if (ret < 0)
2018 return ret; 2016 return ret;
2019 2017
2020 batadv_tt_start_timer(bat_priv); 2018 batadv_tt_start_timer(bat_priv);
2021 2019
2022 return 1; 2020 return 1;
2023 } 2021 }
2024 2022
2025 static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv) 2023 static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
2026 { 2024 {
2027 struct batadv_tt_roam_node *node, *safe; 2025 struct batadv_tt_roam_node *node, *safe;
2028 2026
2029 spin_lock_bh(&bat_priv->tt.roam_list_lock); 2027 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2030 2028
2031 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) { 2029 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
2032 list_del(&node->list); 2030 list_del(&node->list);
2033 kfree(node); 2031 kfree(node);
2034 } 2032 }
2035 2033
2036 spin_unlock_bh(&bat_priv->tt.roam_list_lock); 2034 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2037 } 2035 }
2038 2036
2039 static void batadv_tt_roam_purge(struct batadv_priv *bat_priv) 2037 static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
2040 { 2038 {
2041 struct batadv_tt_roam_node *node, *safe; 2039 struct batadv_tt_roam_node *node, *safe;
2042 2040
2043 spin_lock_bh(&bat_priv->tt.roam_list_lock); 2041 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2044 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) { 2042 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
2045 if (!batadv_has_timed_out(node->first_time, 2043 if (!batadv_has_timed_out(node->first_time,
2046 BATADV_ROAMING_MAX_TIME)) 2044 BATADV_ROAMING_MAX_TIME))
2047 continue; 2045 continue;
2048 2046
2049 list_del(&node->list); 2047 list_del(&node->list);
2050 kfree(node); 2048 kfree(node);
2051 } 2049 }
2052 spin_unlock_bh(&bat_priv->tt.roam_list_lock); 2050 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2053 } 2051 }
2054 2052
2055 /* This function checks whether the client already reached the 2053 /* This function checks whether the client already reached the
2056 * maximum number of possible roaming phases. In this case the ROAMING_ADV 2054 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2057 * will not be sent. 2055 * will not be sent.
2058 * 2056 *
2059 * returns true if the ROAMING_ADV can be sent, false otherwise 2057 * returns true if the ROAMING_ADV can be sent, false otherwise
2060 */ 2058 */
2061 static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv, 2059 static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
2062 uint8_t *client) 2060 uint8_t *client)
2063 { 2061 {
2064 struct batadv_tt_roam_node *tt_roam_node; 2062 struct batadv_tt_roam_node *tt_roam_node;
2065 bool ret = false; 2063 bool ret = false;
2066 2064
2067 spin_lock_bh(&bat_priv->tt.roam_list_lock); 2065 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2068 /* The new tt_req will be issued only if I'm not waiting for a 2066 /* The new tt_req will be issued only if I'm not waiting for a
2069 * reply from the same orig_node yet 2067 * reply from the same orig_node yet
2070 */ 2068 */
2071 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) { 2069 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
2072 if (!batadv_compare_eth(tt_roam_node->addr, client)) 2070 if (!batadv_compare_eth(tt_roam_node->addr, client))
2073 continue; 2071 continue;
2074 2072
2075 if (batadv_has_timed_out(tt_roam_node->first_time, 2073 if (batadv_has_timed_out(tt_roam_node->first_time,
2076 BATADV_ROAMING_MAX_TIME)) 2074 BATADV_ROAMING_MAX_TIME))
2077 continue; 2075 continue;
2078 2076
2079 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter)) 2077 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
2080 /* Sorry, you roamed too many times! */ 2078 /* Sorry, you roamed too many times! */
2081 goto unlock; 2079 goto unlock;
2082 ret = true; 2080 ret = true;
2083 break; 2081 break;
2084 } 2082 }
2085 2083
2086 if (!ret) { 2084 if (!ret) {
2087 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC); 2085 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2088 if (!tt_roam_node) 2086 if (!tt_roam_node)
2089 goto unlock; 2087 goto unlock;
2090 2088
2091 tt_roam_node->first_time = jiffies; 2089 tt_roam_node->first_time = jiffies;
2092 atomic_set(&tt_roam_node->counter, 2090 atomic_set(&tt_roam_node->counter,
2093 BATADV_ROAMING_MAX_COUNT - 1); 2091 BATADV_ROAMING_MAX_COUNT - 1);
2094 memcpy(tt_roam_node->addr, client, ETH_ALEN); 2092 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2095 2093
2096 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list); 2094 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
2097 ret = true; 2095 ret = true;
2098 } 2096 }
2099 2097
2100 unlock: 2098 unlock:
2101 spin_unlock_bh(&bat_priv->tt.roam_list_lock); 2099 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2102 return ret; 2100 return ret;
2103 } 2101 }
2104 2102
2105 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client, 2103 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
2106 struct batadv_orig_node *orig_node) 2104 struct batadv_orig_node *orig_node)
2107 { 2105 {
2108 struct batadv_neigh_node *neigh_node = NULL; 2106 struct batadv_neigh_node *neigh_node = NULL;
2109 struct sk_buff *skb = NULL; 2107 struct sk_buff *skb = NULL;
2110 struct batadv_roam_adv_packet *roam_adv_packet; 2108 struct batadv_roam_adv_packet *roam_adv_packet;
2111 int ret = 1; 2109 int ret = 1;
2112 struct batadv_hard_iface *primary_if; 2110 struct batadv_hard_iface *primary_if;
2113 size_t len = sizeof(*roam_adv_packet); 2111 size_t len = sizeof(*roam_adv_packet);
2114 2112
2115 /* before going on we have to check whether the client has 2113 /* before going on we have to check whether the client has
2116 * already roamed to us too many times 2114 * already roamed to us too many times
2117 */ 2115 */
2118 if (!batadv_tt_check_roam_count(bat_priv, client)) 2116 if (!batadv_tt_check_roam_count(bat_priv, client))
2119 goto out; 2117 goto out;
2120 2118
2121 skb = dev_alloc_skb(sizeof(*roam_adv_packet) + ETH_HLEN + NET_IP_ALIGN); 2119 skb = dev_alloc_skb(sizeof(*roam_adv_packet) + ETH_HLEN + NET_IP_ALIGN);
2122 if (!skb) 2120 if (!skb)
2123 goto out; 2121 goto out;
2124 2122
2125 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN); 2123 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
2126 2124
2127 roam_adv_packet = (struct batadv_roam_adv_packet *)skb_put(skb, len); 2125 roam_adv_packet = (struct batadv_roam_adv_packet *)skb_put(skb, len);
2128 2126
2129 roam_adv_packet->header.packet_type = BATADV_ROAM_ADV; 2127 roam_adv_packet->header.packet_type = BATADV_ROAM_ADV;
2130 roam_adv_packet->header.version = BATADV_COMPAT_VERSION; 2128 roam_adv_packet->header.version = BATADV_COMPAT_VERSION;
2131 roam_adv_packet->header.ttl = BATADV_TTL; 2129 roam_adv_packet->header.ttl = BATADV_TTL;
2132 roam_adv_packet->reserved = 0; 2130 roam_adv_packet->reserved = 0;
2133 primary_if = batadv_primary_if_get_selected(bat_priv); 2131 primary_if = batadv_primary_if_get_selected(bat_priv);
2134 if (!primary_if) 2132 if (!primary_if)
2135 goto out; 2133 goto out;
2136 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN); 2134 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
2137 batadv_hardif_free_ref(primary_if); 2135 batadv_hardif_free_ref(primary_if);
2138 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN); 2136 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
2139 memcpy(roam_adv_packet->client, client, ETH_ALEN); 2137 memcpy(roam_adv_packet->client, client, ETH_ALEN);
2140 2138
2141 neigh_node = batadv_orig_node_get_router(orig_node); 2139 neigh_node = batadv_orig_node_get_router(orig_node);
2142 if (!neigh_node) 2140 if (!neigh_node)
2143 goto out; 2141 goto out;
2144 2142
2145 batadv_dbg(BATADV_DBG_TT, bat_priv, 2143 batadv_dbg(BATADV_DBG_TT, bat_priv,
2146 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n", 2144 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
2147 orig_node->orig, client, neigh_node->addr); 2145 orig_node->orig, client, neigh_node->addr);
2148 2146
2149 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX); 2147 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
2150 2148
2151 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr); 2149 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
2152 ret = 0; 2150 ret = 0;
2153 2151
2154 out: 2152 out:
2155 if (neigh_node) 2153 if (neigh_node)
2156 batadv_neigh_node_free_ref(neigh_node); 2154 batadv_neigh_node_free_ref(neigh_node);
2157 if (ret) 2155 if (ret)
2158 kfree_skb(skb); 2156 kfree_skb(skb);
2159 return; 2157 return;
2160 } 2158 }
2161 2159
2162 static void batadv_tt_purge(struct work_struct *work) 2160 static void batadv_tt_purge(struct work_struct *work)
2163 { 2161 {
2164 struct delayed_work *delayed_work; 2162 struct delayed_work *delayed_work;
2165 struct batadv_priv_tt *priv_tt; 2163 struct batadv_priv_tt *priv_tt;
2166 struct batadv_priv *bat_priv; 2164 struct batadv_priv *bat_priv;
2167 2165
2168 delayed_work = container_of(work, struct delayed_work, work); 2166 delayed_work = container_of(work, struct delayed_work, work);
2169 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work); 2167 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2170 bat_priv = container_of(priv_tt, struct batadv_priv, tt); 2168 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
2171 2169
2172 batadv_tt_local_purge(bat_priv); 2170 batadv_tt_local_purge(bat_priv);
2173 batadv_tt_global_purge(bat_priv); 2171 batadv_tt_global_purge(bat_priv);
2174 batadv_tt_req_purge(bat_priv); 2172 batadv_tt_req_purge(bat_priv);
2175 batadv_tt_roam_purge(bat_priv); 2173 batadv_tt_roam_purge(bat_priv);
2176 2174
2177 batadv_tt_start_timer(bat_priv); 2175 batadv_tt_start_timer(bat_priv);
2178 } 2176 }
2179 2177
2180 void batadv_tt_free(struct batadv_priv *bat_priv) 2178 void batadv_tt_free(struct batadv_priv *bat_priv)
2181 { 2179 {
2182 cancel_delayed_work_sync(&bat_priv->tt.work); 2180 cancel_delayed_work_sync(&bat_priv->tt.work);
2183 2181
2184 batadv_tt_local_table_free(bat_priv); 2182 batadv_tt_local_table_free(bat_priv);
2185 batadv_tt_global_table_free(bat_priv); 2183 batadv_tt_global_table_free(bat_priv);
2186 batadv_tt_req_list_free(bat_priv); 2184 batadv_tt_req_list_free(bat_priv);
2187 batadv_tt_changes_list_free(bat_priv); 2185 batadv_tt_changes_list_free(bat_priv);
2188 batadv_tt_roam_list_free(bat_priv); 2186 batadv_tt_roam_list_free(bat_priv);
2189 2187
2190 kfree(bat_priv->tt.last_changeset); 2188 kfree(bat_priv->tt.last_changeset);
2191 } 2189 }
2192 2190
2193 /* This function will enable or disable the specified flags for all the entries 2191 /* This function will enable or disable the specified flags for all the entries
2194 * in the given hash table and returns the number of modified entries 2192 * in the given hash table and returns the number of modified entries
2195 */ 2193 */
2196 static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash, 2194 static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
2197 uint16_t flags, bool enable) 2195 uint16_t flags, bool enable)
2198 { 2196 {
2199 uint32_t i; 2197 uint32_t i;
2200 uint16_t changed_num = 0; 2198 uint16_t changed_num = 0;
2201 struct hlist_head *head; 2199 struct hlist_head *head;
2202 struct hlist_node *node; 2200 struct hlist_node *node;
2203 struct batadv_tt_common_entry *tt_common_entry; 2201 struct batadv_tt_common_entry *tt_common_entry;
2204 2202
2205 if (!hash) 2203 if (!hash)
2206 goto out; 2204 goto out;
2207 2205
2208 for (i = 0; i < hash->size; i++) { 2206 for (i = 0; i < hash->size; i++) {
2209 head = &hash->table[i]; 2207 head = &hash->table[i];
2210 2208
2211 rcu_read_lock(); 2209 rcu_read_lock();
2212 hlist_for_each_entry_rcu(tt_common_entry, node, 2210 hlist_for_each_entry_rcu(tt_common_entry, node,
2213 head, hash_entry) { 2211 head, hash_entry) {
2214 if (enable) { 2212 if (enable) {
2215 if ((tt_common_entry->flags & flags) == flags) 2213 if ((tt_common_entry->flags & flags) == flags)
2216 continue; 2214 continue;
2217 tt_common_entry->flags |= flags; 2215 tt_common_entry->flags |= flags;
2218 } else { 2216 } else {
2219 if (!(tt_common_entry->flags & flags)) 2217 if (!(tt_common_entry->flags & flags))
2220 continue; 2218 continue;
2221 tt_common_entry->flags &= ~flags; 2219 tt_common_entry->flags &= ~flags;
2222 } 2220 }
2223 changed_num++; 2221 changed_num++;
2224 } 2222 }
2225 rcu_read_unlock(); 2223 rcu_read_unlock();
2226 } 2224 }
2227 out: 2225 out:
2228 return changed_num; 2226 return changed_num;
2229 } 2227 }
2230 2228
2231 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */ 2229 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
2232 static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv) 2230 static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
2233 { 2231 {
2234 struct batadv_hashtable *hash = bat_priv->tt.local_hash; 2232 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2235 struct batadv_tt_common_entry *tt_common; 2233 struct batadv_tt_common_entry *tt_common;
2236 struct batadv_tt_local_entry *tt_local; 2234 struct batadv_tt_local_entry *tt_local;
2237 struct hlist_node *node, *node_tmp; 2235 struct hlist_node *node, *node_tmp;
2238 struct hlist_head *head; 2236 struct hlist_head *head;
2239 spinlock_t *list_lock; /* protects write access to the hash lists */ 2237 spinlock_t *list_lock; /* protects write access to the hash lists */
2240 uint32_t i; 2238 uint32_t i;
2241 2239
2242 if (!hash) 2240 if (!hash)
2243 return; 2241 return;
2244 2242
2245 for (i = 0; i < hash->size; i++) { 2243 for (i = 0; i < hash->size; i++) {
2246 head = &hash->table[i]; 2244 head = &hash->table[i];
2247 list_lock = &hash->list_locks[i]; 2245 list_lock = &hash->list_locks[i];
2248 2246
2249 spin_lock_bh(list_lock); 2247 spin_lock_bh(list_lock);
2250 hlist_for_each_entry_safe(tt_common, node, node_tmp, head, 2248 hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
2251 hash_entry) { 2249 hash_entry) {
2252 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING)) 2250 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
2253 continue; 2251 continue;
2254 2252
2255 batadv_dbg(BATADV_DBG_TT, bat_priv, 2253 batadv_dbg(BATADV_DBG_TT, bat_priv,
2256 "Deleting local tt entry (%pM): pending\n", 2254 "Deleting local tt entry (%pM): pending\n",
2257 tt_common->addr); 2255 tt_common->addr);
2258 2256
2259 atomic_dec(&bat_priv->tt.local_entry_num); 2257 atomic_dec(&bat_priv->tt.local_entry_num);
2260 hlist_del_rcu(node); 2258 hlist_del_rcu(node);
2261 tt_local = container_of(tt_common, 2259 tt_local = container_of(tt_common,
2262 struct batadv_tt_local_entry, 2260 struct batadv_tt_local_entry,
2263 common); 2261 common);
2264 batadv_tt_local_entry_free_ref(tt_local); 2262 batadv_tt_local_entry_free_ref(tt_local);
2265 } 2263 }
2266 spin_unlock_bh(list_lock); 2264 spin_unlock_bh(list_lock);
2267 } 2265 }
2268 2266
2269 } 2267 }
2270 2268
2271 static int batadv_tt_commit_changes(struct batadv_priv *bat_priv, 2269 static int batadv_tt_commit_changes(struct batadv_priv *bat_priv,
2272 unsigned char **packet_buff, 2270 unsigned char **packet_buff,
2273 int *packet_buff_len, int packet_min_len) 2271 int *packet_buff_len, int packet_min_len)
2274 { 2272 {
2275 uint16_t changed_num = 0; 2273 uint16_t changed_num = 0;
2276 2274
2277 if (atomic_read(&bat_priv->tt.local_changes) < 1) 2275 if (atomic_read(&bat_priv->tt.local_changes) < 1)
2278 return -ENOENT; 2276 return -ENOENT;
2279 2277
2280 changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash, 2278 changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash,
2281 BATADV_TT_CLIENT_NEW, false); 2279 BATADV_TT_CLIENT_NEW, false);
2282 2280
2283 /* all reset entries have to be counted as local entries */ 2281 /* all reset entries have to be counted as local entries */
2284 atomic_add(changed_num, &bat_priv->tt.local_entry_num); 2282 atomic_add(changed_num, &bat_priv->tt.local_entry_num);
2285 batadv_tt_local_purge_pending_clients(bat_priv); 2283 batadv_tt_local_purge_pending_clients(bat_priv);
2286 bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv); 2284 bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv);
2287 2285
2288 /* Increment the TTVN only once per OGM interval */ 2286 /* Increment the TTVN only once per OGM interval */
2289 atomic_inc(&bat_priv->tt.vn); 2287 atomic_inc(&bat_priv->tt.vn);
2290 batadv_dbg(BATADV_DBG_TT, bat_priv, 2288 batadv_dbg(BATADV_DBG_TT, bat_priv,
2291 "Local changes committed, updating to ttvn %u\n", 2289 "Local changes committed, updating to ttvn %u\n",
2292 (uint8_t)atomic_read(&bat_priv->tt.vn)); 2290 (uint8_t)atomic_read(&bat_priv->tt.vn));
2293 bat_priv->tt.poss_change = false;
2294 2291
2295 /* reset the sending counter */ 2292 /* reset the sending counter */
2296 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX); 2293 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
2297 2294
2298 return batadv_tt_changes_fill_buff(bat_priv, packet_buff, 2295 return batadv_tt_changes_fill_buff(bat_priv, packet_buff,
2299 packet_buff_len, packet_min_len); 2296 packet_buff_len, packet_min_len);
2300 } 2297 }
2301 2298
2302 /* when calling this function (hard_iface == primary_if) has to be true */ 2299 /* when calling this function (hard_iface == primary_if) has to be true */
2303 int batadv_tt_append_diff(struct batadv_priv *bat_priv, 2300 int batadv_tt_append_diff(struct batadv_priv *bat_priv,
2304 unsigned char **packet_buff, int *packet_buff_len, 2301 unsigned char **packet_buff, int *packet_buff_len,
2305 int packet_min_len) 2302 int packet_min_len)
2306 { 2303 {
2307 int tt_num_changes; 2304 int tt_num_changes;
2308 2305
2309 /* if at least one change happened */ 2306 /* if at least one change happened */
2310 tt_num_changes = batadv_tt_commit_changes(bat_priv, packet_buff, 2307 tt_num_changes = batadv_tt_commit_changes(bat_priv, packet_buff,
2311 packet_buff_len, 2308 packet_buff_len,
2312 packet_min_len); 2309 packet_min_len);
2313 2310
2314 /* if the changes have been sent often enough */ 2311 /* if the changes have been sent often enough */
2315 if ((tt_num_changes < 0) && 2312 if ((tt_num_changes < 0) &&
2316 (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))) { 2313 (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))) {
2317 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len, 2314 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
2318 packet_min_len, packet_min_len); 2315 packet_min_len, packet_min_len);
2319 tt_num_changes = 0; 2316 tt_num_changes = 0;
2320 } 2317 }
2321 2318
2322 return tt_num_changes; 2319 return tt_num_changes;
2323 } 2320 }
2324 2321
2325 bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src, 2322 bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
2326 uint8_t *dst) 2323 uint8_t *dst)
2327 { 2324 {
2328 struct batadv_tt_local_entry *tt_local_entry = NULL; 2325 struct batadv_tt_local_entry *tt_local_entry = NULL;
2329 struct batadv_tt_global_entry *tt_global_entry = NULL; 2326 struct batadv_tt_global_entry *tt_global_entry = NULL;
2330 bool ret = false; 2327 bool ret = false;
2331 2328
2332 if (!atomic_read(&bat_priv->ap_isolation)) 2329 if (!atomic_read(&bat_priv->ap_isolation))
2333 goto out; 2330 goto out;
2334 2331
2335 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst); 2332 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
2336 if (!tt_local_entry) 2333 if (!tt_local_entry)
2337 goto out; 2334 goto out;
2338 2335
2339 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src); 2336 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
2340 if (!tt_global_entry) 2337 if (!tt_global_entry)
2341 goto out; 2338 goto out;
2342 2339
2343 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry)) 2340 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
2344 goto out; 2341 goto out;
2345 2342
2346 ret = true; 2343 ret = true;
2347 2344
2348 out: 2345 out:
2349 if (tt_global_entry) 2346 if (tt_global_entry)
2350 batadv_tt_global_entry_free_ref(tt_global_entry); 2347 batadv_tt_global_entry_free_ref(tt_global_entry);
2351 if (tt_local_entry) 2348 if (tt_local_entry)
2352 batadv_tt_local_entry_free_ref(tt_local_entry); 2349 batadv_tt_local_entry_free_ref(tt_local_entry);
2353 return ret; 2350 return ret;
2354 } 2351 }
2355 2352
2356 void batadv_tt_update_orig(struct batadv_priv *bat_priv, 2353 void batadv_tt_update_orig(struct batadv_priv *bat_priv,
2357 struct batadv_orig_node *orig_node, 2354 struct batadv_orig_node *orig_node,
2358 const unsigned char *tt_buff, uint8_t tt_num_changes, 2355 const unsigned char *tt_buff, uint8_t tt_num_changes,
2359 uint8_t ttvn, uint16_t tt_crc) 2356 uint8_t ttvn, uint16_t tt_crc)
2360 { 2357 {
2361 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn); 2358 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2362 bool full_table = true; 2359 bool full_table = true;
2363 struct batadv_tt_change *tt_change; 2360 struct batadv_tt_change *tt_change;
2364 2361
2365 /* don't care about a backbone gateways updates. */ 2362 /* don't care about a backbone gateways updates. */
2366 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig)) 2363 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2367 return; 2364 return;
2368 2365
2369 /* orig table not initialised AND first diff is in the OGM OR the ttvn 2366 /* orig table not initialised AND first diff is in the OGM OR the ttvn
2370 * increased by one -> we can apply the attached changes 2367 * increased by one -> we can apply the attached changes
2371 */ 2368 */
2372 if ((!orig_node->tt_initialised && ttvn == 1) || 2369 if ((!orig_node->tt_initialised && ttvn == 1) ||
2373 ttvn - orig_ttvn == 1) { 2370 ttvn - orig_ttvn == 1) {
2374 /* the OGM could not contain the changes due to their size or 2371 /* the OGM could not contain the changes due to their size or
2375 * because they have already been sent BATADV_TT_OGM_APPEND_MAX 2372 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
2376 * times. 2373 * times.
2377 * In this case send a tt request 2374 * In this case send a tt request
2378 */ 2375 */
2379 if (!tt_num_changes) { 2376 if (!tt_num_changes) {
2380 full_table = false; 2377 full_table = false;
2381 goto request_table; 2378 goto request_table;
2382 } 2379 }
2383 2380
2384 tt_change = (struct batadv_tt_change *)tt_buff; 2381 tt_change = (struct batadv_tt_change *)tt_buff;
2385 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes, 2382 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
2386 ttvn, tt_change); 2383 ttvn, tt_change);
2387 2384
2388 /* Even if we received the precomputed crc with the OGM, we 2385 /* Even if we received the precomputed crc with the OGM, we
2389 * prefer to recompute it to spot any possible inconsistency 2386 * prefer to recompute it to spot any possible inconsistency
2390 * in the global table 2387 * in the global table
2391 */ 2388 */
2392 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node); 2389 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
2393 2390
2394 /* The ttvn alone is not enough to guarantee consistency 2391 /* The ttvn alone is not enough to guarantee consistency
2395 * because a single value could represent different states 2392 * because a single value could represent different states
2396 * (due to the wrap around). Thus a node has to check whether 2393 * (due to the wrap around). Thus a node has to check whether
2397 * the resulting table (after applying the changes) is still 2394 * the resulting table (after applying the changes) is still
2398 * consistent or not. E.g. a node could disconnect while its 2395 * consistent or not. E.g. a node could disconnect while its
2399 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case 2396 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2400 * checking the CRC value is mandatory to detect the 2397 * checking the CRC value is mandatory to detect the
2401 * inconsistency 2398 * inconsistency
2402 */ 2399 */
2403 if (orig_node->tt_crc != tt_crc) 2400 if (orig_node->tt_crc != tt_crc)
2404 goto request_table; 2401 goto request_table;
2405
2406 /* Roaming phase is over: tables are in sync again. I can
2407 * unset the flag
2408 */
2409 orig_node->tt_poss_change = false;
2410 } else { 2402 } else {
2411 /* if we missed more than one change or our tables are not 2403 /* if we missed more than one change or our tables are not
2412 * in sync anymore -> request fresh tt data 2404 * in sync anymore -> request fresh tt data
2413 */ 2405 */
2414 if (!orig_node->tt_initialised || ttvn != orig_ttvn || 2406 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2415 orig_node->tt_crc != tt_crc) { 2407 orig_node->tt_crc != tt_crc) {
2416 request_table: 2408 request_table:
2417 batadv_dbg(BATADV_DBG_TT, bat_priv, 2409 batadv_dbg(BATADV_DBG_TT, bat_priv,
2418 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %u last_crc: %u num_changes: %u)\n", 2410 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %u last_crc: %u num_changes: %u)\n",
2419 orig_node->orig, ttvn, orig_ttvn, tt_crc, 2411 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2420 orig_node->tt_crc, tt_num_changes); 2412 orig_node->tt_crc, tt_num_changes);
2421 batadv_send_tt_request(bat_priv, orig_node, ttvn, 2413 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2422 tt_crc, full_table); 2414 tt_crc, full_table);
2423 return; 2415 return;
2424 } 2416 }
2425 } 2417 }
2426 } 2418 }
2427 2419
2428 /* returns true whether we know that the client has moved from its old 2420 /* returns true whether we know that the client has moved from its old
2429 * originator to another one. This entry is kept is still kept for consistency 2421 * originator to another one. This entry is kept is still kept for consistency
2430 * purposes 2422 * purposes
2431 */ 2423 */
2432 bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv, 2424 bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
2433 uint8_t *addr) 2425 uint8_t *addr)
2434 { 2426 {
2435 struct batadv_tt_global_entry *tt_global_entry; 2427 struct batadv_tt_global_entry *tt_global_entry;
2436 bool ret = false; 2428 bool ret = false;
2437 2429
2438 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr); 2430 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
2439 if (!tt_global_entry) 2431 if (!tt_global_entry)
2440 goto out; 2432 goto out;
2441 2433
2442 ret = !!(tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM); 2434 ret = !!(tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM);
2443 batadv_tt_global_entry_free_ref(tt_global_entry); 2435 batadv_tt_global_entry_free_ref(tt_global_entry);
2444 out: 2436 out:
2445 return ret; 2437 return ret;
2438 }
2439
2440 /**
2441 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
2442 * @bat_priv: the bat priv with all the soft interface information
2443 * @addr: the MAC address of the local client to query
2444 *
2445 * Returns true if the local client is known to be roaming (it is not served by
2446 * this node anymore) or not. If yes, the client is still present in the table
2447 * to keep the latter consistent with the node TTVN
2448 */
2449 bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
2450 uint8_t *addr)
2451 {
2452 struct batadv_tt_local_entry *tt_local_entry;
2453 bool ret = false;
2454
2455 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
2456 if (!tt_local_entry)
2457 goto out;
2458
2459 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
2460 batadv_tt_local_entry_free_ref(tt_local_entry);
2461 out:
2462 return ret;
2463
2446 } 2464 }
2447 2465
2448 bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv, 2466 bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
2449 struct batadv_orig_node *orig_node, 2467 struct batadv_orig_node *orig_node,
2450 const unsigned char *addr) 2468 const unsigned char *addr)
2451 { 2469 {
2452 bool ret = false; 2470 bool ret = false;
2453 2471
2454 if (!batadv_tt_global_add(bat_priv, orig_node, addr, 2472 if (!batadv_tt_global_add(bat_priv, orig_node, addr,
net/batman-adv/translation-table.h
1 /* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors: 1 /* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
2 * 2 *
3 * Marek Lindner, Simon Wunderlich, Antonio Quartulli 3 * Marek Lindner, Simon Wunderlich, Antonio Quartulli
4 * 4 *
5 * This program is free software; you can redistribute it and/or 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 6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation. 7 * License as published by the Free Software Foundation.
8 * 8 *
9 * This program is distributed in the hope that it will be useful, but 9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of 10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details. 12 * General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 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 15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA 17 * 02110-1301, USA
18 */ 18 */
19 19
20 #ifndef _NET_BATMAN_ADV_TRANSLATION_TABLE_H_ 20 #ifndef _NET_BATMAN_ADV_TRANSLATION_TABLE_H_
21 #define _NET_BATMAN_ADV_TRANSLATION_TABLE_H_ 21 #define _NET_BATMAN_ADV_TRANSLATION_TABLE_H_
22 22
23 int batadv_tt_len(int changes_num); 23 int batadv_tt_len(int changes_num);
24 int batadv_tt_init(struct batadv_priv *bat_priv); 24 int batadv_tt_init(struct batadv_priv *bat_priv);
25 void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr, 25 void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
26 int ifindex); 26 int ifindex);
27 uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv, 27 uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
28 const uint8_t *addr, const char *message, 28 const uint8_t *addr, const char *message,
29 bool roaming); 29 bool roaming);
30 int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset); 30 int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset);
31 void batadv_tt_global_add_orig(struct batadv_priv *bat_priv, 31 void batadv_tt_global_add_orig(struct batadv_priv *bat_priv,
32 struct batadv_orig_node *orig_node, 32 struct batadv_orig_node *orig_node,
33 const unsigned char *tt_buff, int tt_buff_len); 33 const unsigned char *tt_buff, int tt_buff_len);
34 int batadv_tt_global_add(struct batadv_priv *bat_priv, 34 int batadv_tt_global_add(struct batadv_priv *bat_priv,
35 struct batadv_orig_node *orig_node, 35 struct batadv_orig_node *orig_node,
36 const unsigned char *addr, uint8_t flags, 36 const unsigned char *addr, uint8_t flags,
37 uint8_t ttvn); 37 uint8_t ttvn);
38 int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset); 38 int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset);
39 void batadv_tt_global_del_orig(struct batadv_priv *bat_priv, 39 void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
40 struct batadv_orig_node *orig_node, 40 struct batadv_orig_node *orig_node,
41 const char *message); 41 const char *message);
42 struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv, 42 struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
43 const uint8_t *src, 43 const uint8_t *src,
44 const uint8_t *addr); 44 const uint8_t *addr);
45 void batadv_tt_free(struct batadv_priv *bat_priv); 45 void batadv_tt_free(struct batadv_priv *bat_priv);
46 bool batadv_send_tt_response(struct batadv_priv *bat_priv, 46 bool batadv_send_tt_response(struct batadv_priv *bat_priv,
47 struct batadv_tt_query_packet *tt_request); 47 struct batadv_tt_query_packet *tt_request);
48 bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr); 48 bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr);
49 void batadv_handle_tt_response(struct batadv_priv *bat_priv, 49 void batadv_handle_tt_response(struct batadv_priv *bat_priv,
50 struct batadv_tt_query_packet *tt_response); 50 struct batadv_tt_query_packet *tt_response);
51 bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src, 51 bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
52 uint8_t *dst); 52 uint8_t *dst);
53 void batadv_tt_update_orig(struct batadv_priv *bat_priv, 53 void batadv_tt_update_orig(struct batadv_priv *bat_priv,
54 struct batadv_orig_node *orig_node, 54 struct batadv_orig_node *orig_node,
55 const unsigned char *tt_buff, uint8_t tt_num_changes, 55 const unsigned char *tt_buff, uint8_t tt_num_changes,
56 uint8_t ttvn, uint16_t tt_crc); 56 uint8_t ttvn, uint16_t tt_crc);
57 int batadv_tt_append_diff(struct batadv_priv *bat_priv, 57 int batadv_tt_append_diff(struct batadv_priv *bat_priv,
58 unsigned char **packet_buff, int *packet_buff_len, 58 unsigned char **packet_buff, int *packet_buff_len,
59 int packet_min_len); 59 int packet_min_len);
60 bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv, 60 bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
61 uint8_t *addr); 61 uint8_t *addr);
62 bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
63 uint8_t *addr);
62 bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv, 64 bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
63 struct batadv_orig_node *orig_node, 65 struct batadv_orig_node *orig_node,
64 const unsigned char *addr); 66 const unsigned char *addr);
65 67
66 #endif /* _NET_BATMAN_ADV_TRANSLATION_TABLE_H_ */ 68 #endif /* _NET_BATMAN_ADV_TRANSLATION_TABLE_H_ */
67 69
net/batman-adv/types.h
1 /* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors: 1 /* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
2 * 2 *
3 * Marek Lindner, Simon Wunderlich 3 * Marek Lindner, Simon Wunderlich
4 * 4 *
5 * This program is free software; you can redistribute it and/or 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 6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation. 7 * License as published by the Free Software Foundation.
8 * 8 *
9 * This program is distributed in the hope that it will be useful, but 9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of 10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details. 12 * General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 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 15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA 17 * 02110-1301, USA
18 */ 18 */
19 19
20 #ifndef _NET_BATMAN_ADV_TYPES_H_ 20 #ifndef _NET_BATMAN_ADV_TYPES_H_
21 #define _NET_BATMAN_ADV_TYPES_H_ 21 #define _NET_BATMAN_ADV_TYPES_H_
22 22
23 #include "packet.h" 23 #include "packet.h"
24 #include "bitarray.h" 24 #include "bitarray.h"
25 #include <linux/kernel.h> 25 #include <linux/kernel.h>
26 26
27 #define BATADV_HEADER_LEN \ 27 #define BATADV_HEADER_LEN \
28 (ETH_HLEN + max(sizeof(struct batadv_unicast_packet), \ 28 (ETH_HLEN + max(sizeof(struct batadv_unicast_packet), \
29 sizeof(struct batadv_bcast_packet))) 29 sizeof(struct batadv_bcast_packet)))
30 30
31 #ifdef CONFIG_BATMAN_ADV_DAT 31 #ifdef CONFIG_BATMAN_ADV_DAT
32 32
33 /* batadv_dat_addr_t is the type used for all DHT addresses. If it is changed, 33 /* batadv_dat_addr_t is the type used for all DHT addresses. If it is changed,
34 * BATADV_DAT_ADDR_MAX is changed as well. 34 * BATADV_DAT_ADDR_MAX is changed as well.
35 * 35 *
36 * *Please be careful: batadv_dat_addr_t must be UNSIGNED* 36 * *Please be careful: batadv_dat_addr_t must be UNSIGNED*
37 */ 37 */
38 #define batadv_dat_addr_t uint16_t 38 #define batadv_dat_addr_t uint16_t
39 39
40 #endif /* CONFIG_BATMAN_ADV_DAT */ 40 #endif /* CONFIG_BATMAN_ADV_DAT */
41 41
42 /** 42 /**
43 * struct batadv_hard_iface_bat_iv - per hard interface B.A.T.M.A.N. IV data 43 * struct batadv_hard_iface_bat_iv - per hard interface B.A.T.M.A.N. IV data
44 * @ogm_buff: buffer holding the OGM packet 44 * @ogm_buff: buffer holding the OGM packet
45 * @ogm_buff_len: length of the OGM packet buffer 45 * @ogm_buff_len: length of the OGM packet buffer
46 * @ogm_seqno: OGM sequence number - used to identify each OGM 46 * @ogm_seqno: OGM sequence number - used to identify each OGM
47 */ 47 */
48 struct batadv_hard_iface_bat_iv { 48 struct batadv_hard_iface_bat_iv {
49 unsigned char *ogm_buff; 49 unsigned char *ogm_buff;
50 int ogm_buff_len; 50 int ogm_buff_len;
51 atomic_t ogm_seqno; 51 atomic_t ogm_seqno;
52 }; 52 };
53 53
54 struct batadv_hard_iface { 54 struct batadv_hard_iface {
55 struct list_head list; 55 struct list_head list;
56 int16_t if_num; 56 int16_t if_num;
57 char if_status; 57 char if_status;
58 struct net_device *net_dev; 58 struct net_device *net_dev;
59 atomic_t frag_seqno; 59 atomic_t frag_seqno;
60 struct kobject *hardif_obj; 60 struct kobject *hardif_obj;
61 atomic_t refcount; 61 atomic_t refcount;
62 struct packet_type batman_adv_ptype; 62 struct packet_type batman_adv_ptype;
63 struct net_device *soft_iface; 63 struct net_device *soft_iface;
64 struct rcu_head rcu; 64 struct rcu_head rcu;
65 struct batadv_hard_iface_bat_iv bat_iv; 65 struct batadv_hard_iface_bat_iv bat_iv;
66 }; 66 };
67 67
68 /** 68 /**
69 * struct batadv_orig_node - structure for orig_list maintaining nodes of mesh 69 * struct batadv_orig_node - structure for orig_list maintaining nodes of mesh
70 * @primary_addr: hosts primary interface address 70 * @primary_addr: hosts primary interface address
71 * @last_seen: when last packet from this node was received 71 * @last_seen: when last packet from this node was received
72 * @bcast_seqno_reset: time when the broadcast seqno window was reset 72 * @bcast_seqno_reset: time when the broadcast seqno window was reset
73 * @batman_seqno_reset: time when the batman seqno window was reset 73 * @batman_seqno_reset: time when the batman seqno window was reset
74 * @gw_flags: flags related to gateway class 74 * @gw_flags: flags related to gateway class
75 * @flags: for now only VIS_SERVER flag 75 * @flags: for now only VIS_SERVER flag
76 * @last_real_seqno: last and best known sequence number 76 * @last_real_seqno: last and best known sequence number
77 * @last_ttl: ttl of last received packet 77 * @last_ttl: ttl of last received packet
78 * @last_bcast_seqno: last broadcast sequence number received by this host 78 * @last_bcast_seqno: last broadcast sequence number received by this host
79 * 79 *
80 * @candidates: how many candidates are available 80 * @candidates: how many candidates are available
81 * @selected: next bonding candidate 81 * @selected: next bonding candidate
82 */ 82 */
83 struct batadv_orig_node { 83 struct batadv_orig_node {
84 uint8_t orig[ETH_ALEN]; 84 uint8_t orig[ETH_ALEN];
85 uint8_t primary_addr[ETH_ALEN]; 85 uint8_t primary_addr[ETH_ALEN];
86 struct batadv_neigh_node __rcu *router; /* rcu protected pointer */ 86 struct batadv_neigh_node __rcu *router; /* rcu protected pointer */
87 #ifdef CONFIG_BATMAN_ADV_DAT 87 #ifdef CONFIG_BATMAN_ADV_DAT
88 batadv_dat_addr_t dat_addr; 88 batadv_dat_addr_t dat_addr;
89 #endif 89 #endif
90 unsigned long *bcast_own; 90 unsigned long *bcast_own;
91 uint8_t *bcast_own_sum; 91 uint8_t *bcast_own_sum;
92 unsigned long last_seen; 92 unsigned long last_seen;
93 unsigned long bcast_seqno_reset; 93 unsigned long bcast_seqno_reset;
94 unsigned long batman_seqno_reset; 94 unsigned long batman_seqno_reset;
95 uint8_t gw_flags; 95 uint8_t gw_flags;
96 uint8_t flags; 96 uint8_t flags;
97 atomic_t last_ttvn; /* last seen translation table version number */ 97 atomic_t last_ttvn; /* last seen translation table version number */
98 uint16_t tt_crc; 98 uint16_t tt_crc;
99 unsigned char *tt_buff; 99 unsigned char *tt_buff;
100 int16_t tt_buff_len; 100 int16_t tt_buff_len;
101 spinlock_t tt_buff_lock; /* protects tt_buff */ 101 spinlock_t tt_buff_lock; /* protects tt_buff */
102 atomic_t tt_size; 102 atomic_t tt_size;
103 bool tt_initialised; 103 bool tt_initialised;
104 /* The tt_poss_change flag is used to detect an ongoing roaming phase.
105 * If true, then I sent a Roaming_adv to this orig_node and I have to
106 * inspect every packet directed to it to check whether it is still
107 * the true destination or not. This flag will be reset to false as
108 * soon as I receive a new TTVN from this orig_node
109 */
110 bool tt_poss_change;
111 uint32_t last_real_seqno; 104 uint32_t last_real_seqno;
112 uint8_t last_ttl; 105 uint8_t last_ttl;
113 DECLARE_BITMAP(bcast_bits, BATADV_TQ_LOCAL_WINDOW_SIZE); 106 DECLARE_BITMAP(bcast_bits, BATADV_TQ_LOCAL_WINDOW_SIZE);
114 uint32_t last_bcast_seqno; 107 uint32_t last_bcast_seqno;
115 struct hlist_head neigh_list; 108 struct hlist_head neigh_list;
116 struct list_head frag_list; 109 struct list_head frag_list;
117 spinlock_t neigh_list_lock; /* protects neigh_list and router */ 110 spinlock_t neigh_list_lock; /* protects neigh_list and router */
118 atomic_t refcount; 111 atomic_t refcount;
119 struct rcu_head rcu; 112 struct rcu_head rcu;
120 struct hlist_node hash_entry; 113 struct hlist_node hash_entry;
121 struct batadv_priv *bat_priv; 114 struct batadv_priv *bat_priv;
122 unsigned long last_frag_packet; 115 unsigned long last_frag_packet;
123 /* ogm_cnt_lock protects: bcast_own, bcast_own_sum, 116 /* ogm_cnt_lock protects: bcast_own, bcast_own_sum,
124 * neigh_node->real_bits, neigh_node->real_packet_count 117 * neigh_node->real_bits, neigh_node->real_packet_count
125 */ 118 */
126 spinlock_t ogm_cnt_lock; 119 spinlock_t ogm_cnt_lock;
127 /* bcast_seqno_lock protects bcast_bits, last_bcast_seqno */ 120 /* bcast_seqno_lock protects bcast_bits, last_bcast_seqno */
128 spinlock_t bcast_seqno_lock; 121 spinlock_t bcast_seqno_lock;
129 spinlock_t tt_list_lock; /* protects tt_list */ 122 spinlock_t tt_list_lock; /* protects tt_list */
130 atomic_t bond_candidates; 123 atomic_t bond_candidates;
131 struct list_head bond_list; 124 struct list_head bond_list;
132 }; 125 };
133 126
134 struct batadv_gw_node { 127 struct batadv_gw_node {
135 struct hlist_node list; 128 struct hlist_node list;
136 struct batadv_orig_node *orig_node; 129 struct batadv_orig_node *orig_node;
137 unsigned long deleted; 130 unsigned long deleted;
138 atomic_t refcount; 131 atomic_t refcount;
139 struct rcu_head rcu; 132 struct rcu_head rcu;
140 }; 133 };
141 134
142 /* batadv_neigh_node 135 /* batadv_neigh_node
143 * @last_seen: when last packet via this neighbor was received 136 * @last_seen: when last packet via this neighbor was received
144 */ 137 */
145 struct batadv_neigh_node { 138 struct batadv_neigh_node {
146 struct hlist_node list; 139 struct hlist_node list;
147 uint8_t addr[ETH_ALEN]; 140 uint8_t addr[ETH_ALEN];
148 uint8_t real_packet_count; 141 uint8_t real_packet_count;
149 uint8_t tq_recv[BATADV_TQ_GLOBAL_WINDOW_SIZE]; 142 uint8_t tq_recv[BATADV_TQ_GLOBAL_WINDOW_SIZE];
150 uint8_t tq_index; 143 uint8_t tq_index;
151 uint8_t tq_avg; 144 uint8_t tq_avg;
152 uint8_t last_ttl; 145 uint8_t last_ttl;
153 struct list_head bonding_list; 146 struct list_head bonding_list;
154 unsigned long last_seen; 147 unsigned long last_seen;
155 DECLARE_BITMAP(real_bits, BATADV_TQ_LOCAL_WINDOW_SIZE); 148 DECLARE_BITMAP(real_bits, BATADV_TQ_LOCAL_WINDOW_SIZE);
156 atomic_t refcount; 149 atomic_t refcount;
157 struct rcu_head rcu; 150 struct rcu_head rcu;
158 struct batadv_orig_node *orig_node; 151 struct batadv_orig_node *orig_node;
159 struct batadv_hard_iface *if_incoming; 152 struct batadv_hard_iface *if_incoming;
160 spinlock_t lq_update_lock; /* protects: tq_recv, tq_index */ 153 spinlock_t lq_update_lock; /* protects: tq_recv, tq_index */
161 }; 154 };
162 155
163 #ifdef CONFIG_BATMAN_ADV_BLA 156 #ifdef CONFIG_BATMAN_ADV_BLA
164 struct batadv_bcast_duplist_entry { 157 struct batadv_bcast_duplist_entry {
165 uint8_t orig[ETH_ALEN]; 158 uint8_t orig[ETH_ALEN];
166 uint16_t crc; 159 uint16_t crc;
167 unsigned long entrytime; 160 unsigned long entrytime;
168 }; 161 };
169 #endif 162 #endif
170 163
171 enum batadv_counters { 164 enum batadv_counters {
172 BATADV_CNT_TX, 165 BATADV_CNT_TX,
173 BATADV_CNT_TX_BYTES, 166 BATADV_CNT_TX_BYTES,
174 BATADV_CNT_TX_DROPPED, 167 BATADV_CNT_TX_DROPPED,
175 BATADV_CNT_RX, 168 BATADV_CNT_RX,
176 BATADV_CNT_RX_BYTES, 169 BATADV_CNT_RX_BYTES,
177 BATADV_CNT_FORWARD, 170 BATADV_CNT_FORWARD,
178 BATADV_CNT_FORWARD_BYTES, 171 BATADV_CNT_FORWARD_BYTES,
179 BATADV_CNT_MGMT_TX, 172 BATADV_CNT_MGMT_TX,
180 BATADV_CNT_MGMT_TX_BYTES, 173 BATADV_CNT_MGMT_TX_BYTES,
181 BATADV_CNT_MGMT_RX, 174 BATADV_CNT_MGMT_RX,
182 BATADV_CNT_MGMT_RX_BYTES, 175 BATADV_CNT_MGMT_RX_BYTES,
183 BATADV_CNT_TT_REQUEST_TX, 176 BATADV_CNT_TT_REQUEST_TX,
184 BATADV_CNT_TT_REQUEST_RX, 177 BATADV_CNT_TT_REQUEST_RX,
185 BATADV_CNT_TT_RESPONSE_TX, 178 BATADV_CNT_TT_RESPONSE_TX,
186 BATADV_CNT_TT_RESPONSE_RX, 179 BATADV_CNT_TT_RESPONSE_RX,
187 BATADV_CNT_TT_ROAM_ADV_TX, 180 BATADV_CNT_TT_ROAM_ADV_TX,
188 BATADV_CNT_TT_ROAM_ADV_RX, 181 BATADV_CNT_TT_ROAM_ADV_RX,
189 #ifdef CONFIG_BATMAN_ADV_DAT 182 #ifdef CONFIG_BATMAN_ADV_DAT
190 BATADV_CNT_DAT_GET_TX, 183 BATADV_CNT_DAT_GET_TX,
191 BATADV_CNT_DAT_GET_RX, 184 BATADV_CNT_DAT_GET_RX,
192 BATADV_CNT_DAT_PUT_TX, 185 BATADV_CNT_DAT_PUT_TX,
193 BATADV_CNT_DAT_PUT_RX, 186 BATADV_CNT_DAT_PUT_RX,
194 BATADV_CNT_DAT_CACHED_REPLY_TX, 187 BATADV_CNT_DAT_CACHED_REPLY_TX,
195 #endif 188 #endif
196 BATADV_CNT_NUM, 189 BATADV_CNT_NUM,
197 }; 190 };
198 191
199 /** 192 /**
200 * struct batadv_priv_tt - per mesh interface translation table data 193 * struct batadv_priv_tt - per mesh interface translation table data
201 * @vn: translation table version number 194 * @vn: translation table version number
202 * @local_changes: changes registered in an originator interval 195 * @local_changes: changes registered in an originator interval
203 * @poss_change: Detect an ongoing roaming phase. If true, then this node 196 * @poss_change: Detect an ongoing roaming phase. If true, then this node
204 * received a roaming_adv and has to inspect every packet directed to it to 197 * received a roaming_adv and has to inspect every packet directed to it to
205 * check whether it still is the true destination or not. This flag will be 198 * check whether it still is the true destination or not. This flag will be
206 * reset to false as soon as the this node's ttvn is increased 199 * reset to false as soon as the this node's ttvn is increased
207 * @changes_list: tracks tt local changes within an originator interval 200 * @changes_list: tracks tt local changes within an originator interval
208 * @req_list: list of pending tt_requests 201 * @req_list: list of pending tt_requests
209 * @local_crc: Checksum of the local table, recomputed before sending a new OGM 202 * @local_crc: Checksum of the local table, recomputed before sending a new OGM
210 */ 203 */
211 struct batadv_priv_tt { 204 struct batadv_priv_tt {
212 atomic_t vn; 205 atomic_t vn;
213 atomic_t ogm_append_cnt; 206 atomic_t ogm_append_cnt;
214 atomic_t local_changes; 207 atomic_t local_changes;
215 bool poss_change;
216 struct list_head changes_list; 208 struct list_head changes_list;
217 struct batadv_hashtable *local_hash; 209 struct batadv_hashtable *local_hash;
218 struct batadv_hashtable *global_hash; 210 struct batadv_hashtable *global_hash;
219 struct list_head req_list; 211 struct list_head req_list;
220 struct list_head roam_list; 212 struct list_head roam_list;
221 spinlock_t changes_list_lock; /* protects changes */ 213 spinlock_t changes_list_lock; /* protects changes */
222 spinlock_t req_list_lock; /* protects req_list */ 214 spinlock_t req_list_lock; /* protects req_list */
223 spinlock_t roam_list_lock; /* protects roam_list */ 215 spinlock_t roam_list_lock; /* protects roam_list */
224 atomic_t local_entry_num; 216 atomic_t local_entry_num;
225 uint16_t local_crc; 217 uint16_t local_crc;
226 unsigned char *last_changeset; 218 unsigned char *last_changeset;
227 int16_t last_changeset_len; 219 int16_t last_changeset_len;
228 spinlock_t last_changeset_lock; /* protects last_changeset */ 220 spinlock_t last_changeset_lock; /* protects last_changeset */
229 struct delayed_work work; 221 struct delayed_work work;
230 }; 222 };
231 223
232 #ifdef CONFIG_BATMAN_ADV_BLA 224 #ifdef CONFIG_BATMAN_ADV_BLA
233 struct batadv_priv_bla { 225 struct batadv_priv_bla {
234 atomic_t num_requests; /* number of bla requests in flight */ 226 atomic_t num_requests; /* number of bla requests in flight */
235 struct batadv_hashtable *claim_hash; 227 struct batadv_hashtable *claim_hash;
236 struct batadv_hashtable *backbone_hash; 228 struct batadv_hashtable *backbone_hash;
237 struct batadv_bcast_duplist_entry bcast_duplist[BATADV_DUPLIST_SIZE]; 229 struct batadv_bcast_duplist_entry bcast_duplist[BATADV_DUPLIST_SIZE];
238 int bcast_duplist_curr; 230 int bcast_duplist_curr;
239 /* protects bcast_duplist and bcast_duplist_curr */ 231 /* protects bcast_duplist and bcast_duplist_curr */
240 spinlock_t bcast_duplist_lock; 232 spinlock_t bcast_duplist_lock;
241 struct batadv_bla_claim_dst claim_dest; 233 struct batadv_bla_claim_dst claim_dest;
242 struct delayed_work work; 234 struct delayed_work work;
243 }; 235 };
244 #endif 236 #endif
245 237
246 struct batadv_priv_gw { 238 struct batadv_priv_gw {
247 struct hlist_head list; 239 struct hlist_head list;
248 spinlock_t list_lock; /* protects gw_list and curr_gw */ 240 spinlock_t list_lock; /* protects gw_list and curr_gw */
249 struct batadv_gw_node __rcu *curr_gw; /* rcu protected pointer */ 241 struct batadv_gw_node __rcu *curr_gw; /* rcu protected pointer */
250 atomic_t reselect; 242 atomic_t reselect;
251 }; 243 };
252 244
253 struct batadv_priv_vis { 245 struct batadv_priv_vis {
254 struct list_head send_list; 246 struct list_head send_list;
255 struct batadv_hashtable *hash; 247 struct batadv_hashtable *hash;
256 spinlock_t hash_lock; /* protects hash */ 248 spinlock_t hash_lock; /* protects hash */
257 spinlock_t list_lock; /* protects info::recv_list */ 249 spinlock_t list_lock; /* protects info::recv_list */
258 struct delayed_work work; 250 struct delayed_work work;
259 struct batadv_vis_info *my_info; 251 struct batadv_vis_info *my_info;
260 }; 252 };
261 253
262 /** 254 /**
263 * struct batadv_priv_dat - per mesh interface DAT private data 255 * struct batadv_priv_dat - per mesh interface DAT private data
264 * @addr: node DAT address 256 * @addr: node DAT address
265 * @hash: hashtable representing the local ARP cache 257 * @hash: hashtable representing the local ARP cache
266 * @work: work queue callback item for cache purging 258 * @work: work queue callback item for cache purging
267 */ 259 */
268 #ifdef CONFIG_BATMAN_ADV_DAT 260 #ifdef CONFIG_BATMAN_ADV_DAT
269 struct batadv_priv_dat { 261 struct batadv_priv_dat {
270 batadv_dat_addr_t addr; 262 batadv_dat_addr_t addr;
271 struct batadv_hashtable *hash; 263 struct batadv_hashtable *hash;
272 struct delayed_work work; 264 struct delayed_work work;
273 }; 265 };
274 #endif 266 #endif
275 267
276 struct batadv_priv { 268 struct batadv_priv {
277 atomic_t mesh_state; 269 atomic_t mesh_state;
278 struct net_device_stats stats; 270 struct net_device_stats stats;
279 uint64_t __percpu *bat_counters; /* Per cpu counters */ 271 uint64_t __percpu *bat_counters; /* Per cpu counters */
280 atomic_t aggregated_ogms; /* boolean */ 272 atomic_t aggregated_ogms; /* boolean */
281 atomic_t bonding; /* boolean */ 273 atomic_t bonding; /* boolean */
282 atomic_t fragmentation; /* boolean */ 274 atomic_t fragmentation; /* boolean */
283 atomic_t ap_isolation; /* boolean */ 275 atomic_t ap_isolation; /* boolean */
284 atomic_t bridge_loop_avoidance; /* boolean */ 276 atomic_t bridge_loop_avoidance; /* boolean */
285 #ifdef CONFIG_BATMAN_ADV_DAT 277 #ifdef CONFIG_BATMAN_ADV_DAT
286 atomic_t distributed_arp_table; /* boolean */ 278 atomic_t distributed_arp_table; /* boolean */
287 #endif 279 #endif
288 atomic_t vis_mode; /* VIS_TYPE_* */ 280 atomic_t vis_mode; /* VIS_TYPE_* */
289 atomic_t gw_mode; /* GW_MODE_* */ 281 atomic_t gw_mode; /* GW_MODE_* */
290 atomic_t gw_sel_class; /* uint */ 282 atomic_t gw_sel_class; /* uint */
291 atomic_t gw_bandwidth; /* gw bandwidth */ 283 atomic_t gw_bandwidth; /* gw bandwidth */
292 atomic_t orig_interval; /* uint */ 284 atomic_t orig_interval; /* uint */
293 atomic_t hop_penalty; /* uint */ 285 atomic_t hop_penalty; /* uint */
294 atomic_t log_level; /* uint */ 286 atomic_t log_level; /* uint */
295 atomic_t bcast_seqno; 287 atomic_t bcast_seqno;
296 atomic_t bcast_queue_left; 288 atomic_t bcast_queue_left;
297 atomic_t batman_queue_left; 289 atomic_t batman_queue_left;
298 char num_ifaces; 290 char num_ifaces;
299 struct batadv_debug_log *debug_log; 291 struct batadv_debug_log *debug_log;
300 struct kobject *mesh_obj; 292 struct kobject *mesh_obj;
301 struct dentry *debug_dir; 293 struct dentry *debug_dir;
302 struct hlist_head forw_bat_list; 294 struct hlist_head forw_bat_list;
303 struct hlist_head forw_bcast_list; 295 struct hlist_head forw_bcast_list;
304 struct batadv_hashtable *orig_hash; 296 struct batadv_hashtable *orig_hash;
305 spinlock_t forw_bat_list_lock; /* protects forw_bat_list */ 297 spinlock_t forw_bat_list_lock; /* protects forw_bat_list */
306 spinlock_t forw_bcast_list_lock; /* protects forw_bcast_list */ 298 spinlock_t forw_bcast_list_lock; /* protects forw_bcast_list */
307 struct delayed_work orig_work; 299 struct delayed_work orig_work;
308 struct batadv_hard_iface __rcu *primary_if; /* rcu protected pointer */ 300 struct batadv_hard_iface __rcu *primary_if; /* rcu protected pointer */
309 struct batadv_algo_ops *bat_algo_ops; 301 struct batadv_algo_ops *bat_algo_ops;
310 #ifdef CONFIG_BATMAN_ADV_BLA 302 #ifdef CONFIG_BATMAN_ADV_BLA
311 struct batadv_priv_bla bla; 303 struct batadv_priv_bla bla;
312 #endif 304 #endif
313 struct batadv_priv_gw gw; 305 struct batadv_priv_gw gw;
314 struct batadv_priv_tt tt; 306 struct batadv_priv_tt tt;
315 struct batadv_priv_vis vis; 307 struct batadv_priv_vis vis;
316 #ifdef CONFIG_BATMAN_ADV_DAT 308 #ifdef CONFIG_BATMAN_ADV_DAT
317 struct batadv_priv_dat dat; 309 struct batadv_priv_dat dat;
318 #endif 310 #endif
319 }; 311 };
320 312
321 struct batadv_socket_client { 313 struct batadv_socket_client {
322 struct list_head queue_list; 314 struct list_head queue_list;
323 unsigned int queue_len; 315 unsigned int queue_len;
324 unsigned char index; 316 unsigned char index;
325 spinlock_t lock; /* protects queue_list, queue_len, index */ 317 spinlock_t lock; /* protects queue_list, queue_len, index */
326 wait_queue_head_t queue_wait; 318 wait_queue_head_t queue_wait;
327 struct batadv_priv *bat_priv; 319 struct batadv_priv *bat_priv;
328 }; 320 };
329 321
330 struct batadv_socket_packet { 322 struct batadv_socket_packet {
331 struct list_head list; 323 struct list_head list;
332 size_t icmp_len; 324 size_t icmp_len;
333 struct batadv_icmp_packet_rr icmp_packet; 325 struct batadv_icmp_packet_rr icmp_packet;
334 }; 326 };
335 327
336 struct batadv_tt_common_entry { 328 struct batadv_tt_common_entry {
337 uint8_t addr[ETH_ALEN]; 329 uint8_t addr[ETH_ALEN];
338 struct hlist_node hash_entry; 330 struct hlist_node hash_entry;
339 uint16_t flags; 331 uint16_t flags;
340 unsigned long added_at; 332 unsigned long added_at;
341 atomic_t refcount; 333 atomic_t refcount;
342 struct rcu_head rcu; 334 struct rcu_head rcu;
343 }; 335 };
344 336
345 struct batadv_tt_local_entry { 337 struct batadv_tt_local_entry {
346 struct batadv_tt_common_entry common; 338 struct batadv_tt_common_entry common;
347 unsigned long last_seen; 339 unsigned long last_seen;
348 }; 340 };
349 341
350 struct batadv_tt_global_entry { 342 struct batadv_tt_global_entry {
351 struct batadv_tt_common_entry common; 343 struct batadv_tt_common_entry common;
352 struct hlist_head orig_list; 344 struct hlist_head orig_list;
353 spinlock_t list_lock; /* protects the list */ 345 spinlock_t list_lock; /* protects the list */
354 unsigned long roam_at; /* time at which TT_GLOBAL_ROAM was set */ 346 unsigned long roam_at; /* time at which TT_GLOBAL_ROAM was set */
355 }; 347 };
356 348
357 struct batadv_tt_orig_list_entry { 349 struct batadv_tt_orig_list_entry {
358 struct batadv_orig_node *orig_node; 350 struct batadv_orig_node *orig_node;
359 uint8_t ttvn; 351 uint8_t ttvn;
360 atomic_t refcount; 352 atomic_t refcount;
361 struct rcu_head rcu; 353 struct rcu_head rcu;
362 struct hlist_node list; 354 struct hlist_node list;
363 }; 355 };
364 356
365 #ifdef CONFIG_BATMAN_ADV_BLA 357 #ifdef CONFIG_BATMAN_ADV_BLA
366 struct batadv_backbone_gw { 358 struct batadv_backbone_gw {
367 uint8_t orig[ETH_ALEN]; 359 uint8_t orig[ETH_ALEN];
368 short vid; /* used VLAN ID */ 360 short vid; /* used VLAN ID */
369 struct hlist_node hash_entry; 361 struct hlist_node hash_entry;
370 struct batadv_priv *bat_priv; 362 struct batadv_priv *bat_priv;
371 unsigned long lasttime; /* last time we heard of this backbone gw */ 363 unsigned long lasttime; /* last time we heard of this backbone gw */
372 atomic_t wait_periods; 364 atomic_t wait_periods;
373 atomic_t request_sent; 365 atomic_t request_sent;
374 atomic_t refcount; 366 atomic_t refcount;
375 struct rcu_head rcu; 367 struct rcu_head rcu;
376 uint16_t crc; /* crc checksum over all claims */ 368 uint16_t crc; /* crc checksum over all claims */
377 }; 369 };
378 370
379 struct batadv_claim { 371 struct batadv_claim {
380 uint8_t addr[ETH_ALEN]; 372 uint8_t addr[ETH_ALEN];
381 short vid; 373 short vid;
382 struct batadv_backbone_gw *backbone_gw; 374 struct batadv_backbone_gw *backbone_gw;
383 unsigned long lasttime; /* last time we heard of claim (locals only) */ 375 unsigned long lasttime; /* last time we heard of claim (locals only) */
384 struct rcu_head rcu; 376 struct rcu_head rcu;
385 atomic_t refcount; 377 atomic_t refcount;
386 struct hlist_node hash_entry; 378 struct hlist_node hash_entry;
387 }; 379 };
388 #endif 380 #endif
389 381
390 struct batadv_tt_change_node { 382 struct batadv_tt_change_node {
391 struct list_head list; 383 struct list_head list;
392 struct batadv_tt_change change; 384 struct batadv_tt_change change;
393 }; 385 };
394 386
395 struct batadv_tt_req_node { 387 struct batadv_tt_req_node {
396 uint8_t addr[ETH_ALEN]; 388 uint8_t addr[ETH_ALEN];
397 unsigned long issued_at; 389 unsigned long issued_at;
398 struct list_head list; 390 struct list_head list;
399 }; 391 };
400 392
401 struct batadv_tt_roam_node { 393 struct batadv_tt_roam_node {
402 uint8_t addr[ETH_ALEN]; 394 uint8_t addr[ETH_ALEN];
403 atomic_t counter; 395 atomic_t counter;
404 unsigned long first_time; 396 unsigned long first_time;
405 struct list_head list; 397 struct list_head list;
406 }; 398 };
407 399
408 /* forw_packet - structure for forw_list maintaining packets to be 400 /* forw_packet - structure for forw_list maintaining packets to be
409 * send/forwarded 401 * send/forwarded
410 */ 402 */
411 struct batadv_forw_packet { 403 struct batadv_forw_packet {
412 struct hlist_node list; 404 struct hlist_node list;
413 unsigned long send_time; 405 unsigned long send_time;
414 uint8_t own; 406 uint8_t own;
415 struct sk_buff *skb; 407 struct sk_buff *skb;
416 uint16_t packet_len; 408 uint16_t packet_len;
417 uint32_t direct_link_flags; 409 uint32_t direct_link_flags;
418 uint8_t num_packets; 410 uint8_t num_packets;
419 struct delayed_work delayed_work; 411 struct delayed_work delayed_work;
420 struct batadv_hard_iface *if_incoming; 412 struct batadv_hard_iface *if_incoming;
421 }; 413 };
422 414
423 /* While scanning for vis-entries of a particular vis-originator 415 /* While scanning for vis-entries of a particular vis-originator
424 * this list collects its interfaces to create a subgraph/cluster 416 * this list collects its interfaces to create a subgraph/cluster
425 * out of them later 417 * out of them later
426 */ 418 */
427 struct batadv_if_list_entry { 419 struct batadv_if_list_entry {
428 uint8_t addr[ETH_ALEN]; 420 uint8_t addr[ETH_ALEN];
429 bool primary; 421 bool primary;
430 struct hlist_node list; 422 struct hlist_node list;
431 }; 423 };
432 424
433 struct batadv_debug_log { 425 struct batadv_debug_log {
434 char log_buff[BATADV_LOG_BUF_LEN]; 426 char log_buff[BATADV_LOG_BUF_LEN];
435 unsigned long log_start; 427 unsigned long log_start;
436 unsigned long log_end; 428 unsigned long log_end;
437 spinlock_t lock; /* protects log_buff, log_start and log_end */ 429 spinlock_t lock; /* protects log_buff, log_start and log_end */
438 wait_queue_head_t queue_wait; 430 wait_queue_head_t queue_wait;
439 }; 431 };
440 432
441 struct batadv_frag_packet_list_entry { 433 struct batadv_frag_packet_list_entry {
442 struct list_head list; 434 struct list_head list;
443 uint16_t seqno; 435 uint16_t seqno;
444 struct sk_buff *skb; 436 struct sk_buff *skb;
445 }; 437 };
446 438
447 struct batadv_vis_info { 439 struct batadv_vis_info {
448 unsigned long first_seen; 440 unsigned long first_seen;
449 /* list of server-neighbors we received a vis-packet 441 /* list of server-neighbors we received a vis-packet
450 * from. we should not reply to them. 442 * from. we should not reply to them.
451 */ 443 */
452 struct list_head recv_list; 444 struct list_head recv_list;
453 struct list_head send_list; 445 struct list_head send_list;
454 struct kref refcount; 446 struct kref refcount;
455 struct hlist_node hash_entry; 447 struct hlist_node hash_entry;
456 struct batadv_priv *bat_priv; 448 struct batadv_priv *bat_priv;
457 /* this packet might be part of the vis send queue. */ 449 /* this packet might be part of the vis send queue. */
458 struct sk_buff *skb_packet; 450 struct sk_buff *skb_packet;
459 /* vis_info may follow here */ 451 /* vis_info may follow here */
460 } __packed; 452 } __packed;
461 453
462 struct batadv_vis_info_entry { 454 struct batadv_vis_info_entry {
463 uint8_t src[ETH_ALEN]; 455 uint8_t src[ETH_ALEN];
464 uint8_t dest[ETH_ALEN]; 456 uint8_t dest[ETH_ALEN];
465 uint8_t quality; /* quality = 0 client */ 457 uint8_t quality; /* quality = 0 client */
466 } __packed; 458 } __packed;
467 459
468 struct batadv_recvlist_node { 460 struct batadv_recvlist_node {
469 struct list_head list; 461 struct list_head list;
470 uint8_t mac[ETH_ALEN]; 462 uint8_t mac[ETH_ALEN];
471 }; 463 };
472 464
473 struct batadv_algo_ops { 465 struct batadv_algo_ops {
474 struct hlist_node list; 466 struct hlist_node list;
475 char *name; 467 char *name;
476 /* init routing info when hard-interface is enabled */ 468 /* init routing info when hard-interface is enabled */
477 int (*bat_iface_enable)(struct batadv_hard_iface *hard_iface); 469 int (*bat_iface_enable)(struct batadv_hard_iface *hard_iface);
478 /* de-init routing info when hard-interface is disabled */ 470 /* de-init routing info when hard-interface is disabled */
479 void (*bat_iface_disable)(struct batadv_hard_iface *hard_iface); 471 void (*bat_iface_disable)(struct batadv_hard_iface *hard_iface);
480 /* (re-)init mac addresses of the protocol information 472 /* (re-)init mac addresses of the protocol information
481 * belonging to this hard-interface 473 * belonging to this hard-interface
482 */ 474 */
483 void (*bat_iface_update_mac)(struct batadv_hard_iface *hard_iface); 475 void (*bat_iface_update_mac)(struct batadv_hard_iface *hard_iface);
484 /* called when primary interface is selected / changed */ 476 /* called when primary interface is selected / changed */
485 void (*bat_primary_iface_set)(struct batadv_hard_iface *hard_iface); 477 void (*bat_primary_iface_set)(struct batadv_hard_iface *hard_iface);
486 /* prepare a new outgoing OGM for the send queue */ 478 /* prepare a new outgoing OGM for the send queue */
487 void (*bat_ogm_schedule)(struct batadv_hard_iface *hard_iface); 479 void (*bat_ogm_schedule)(struct batadv_hard_iface *hard_iface);
488 /* send scheduled OGM */ 480 /* send scheduled OGM */
489 void (*bat_ogm_emit)(struct batadv_forw_packet *forw_packet); 481 void (*bat_ogm_emit)(struct batadv_forw_packet *forw_packet);
490 }; 482 };
491 483
492 /** 484 /**
493 * struct batadv_dat_entry - it is a single entry of batman-adv ARP backend. It 485 * struct batadv_dat_entry - it is a single entry of batman-adv ARP backend. It
494 * is used to stored ARP entries needed for the global DAT cache 486 * is used to stored ARP entries needed for the global DAT cache
495 * @ip: the IPv4 corresponding to this DAT/ARP entry 487 * @ip: the IPv4 corresponding to this DAT/ARP entry
496 * @mac_addr: the MAC address associated to the stored IPv4 488 * @mac_addr: the MAC address associated to the stored IPv4
497 * @last_update: time in jiffies when this entry was refreshed last time 489 * @last_update: time in jiffies when this entry was refreshed last time
498 * @hash_entry: hlist node for batadv_priv_dat::hash 490 * @hash_entry: hlist node for batadv_priv_dat::hash
499 * @refcount: number of contexts the object is used 491 * @refcount: number of contexts the object is used
500 * @rcu: struct used for freeing in an RCU-safe manner 492 * @rcu: struct used for freeing in an RCU-safe manner
501 */ 493 */
502 struct batadv_dat_entry { 494 struct batadv_dat_entry {
503 __be32 ip; 495 __be32 ip;
504 uint8_t mac_addr[ETH_ALEN]; 496 uint8_t mac_addr[ETH_ALEN];
505 unsigned long last_update; 497 unsigned long last_update;
506 struct hlist_node hash_entry; 498 struct hlist_node hash_entry;
507 atomic_t refcount; 499 atomic_t refcount;
508 struct rcu_head rcu; 500 struct rcu_head rcu;
509 }; 501 };
510 502
511 /** 503 /**
512 * struct batadv_dat_candidate - candidate destination for DAT operations 504 * struct batadv_dat_candidate - candidate destination for DAT operations
513 * @type: the type of the selected candidate. It can one of the following: 505 * @type: the type of the selected candidate. It can one of the following:
514 * - BATADV_DAT_CANDIDATE_NOT_FOUND 506 * - BATADV_DAT_CANDIDATE_NOT_FOUND
515 * - BATADV_DAT_CANDIDATE_ORIG 507 * - BATADV_DAT_CANDIDATE_ORIG
516 * @orig_node: if type is BATADV_DAT_CANDIDATE_ORIG this field points to the 508 * @orig_node: if type is BATADV_DAT_CANDIDATE_ORIG this field points to the
517 * corresponding originator node structure 509 * corresponding originator node structure
518 */ 510 */
519 struct batadv_dat_candidate { 511 struct batadv_dat_candidate {
520 int type; 512 int type;
521 struct batadv_orig_node *orig_node; 513 struct batadv_orig_node *orig_node;
522 }; 514 };
523 515
524 #endif /* _NET_BATMAN_ADV_TYPES_H_ */ 516 #endif /* _NET_BATMAN_ADV_TYPES_H_ */
525 517