Merge branch 'stable/bug.fixes' of git://git.kernel.org/pub/scm/linux/kernel/git...
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / batman-adv / routing.c
CommitLineData
c6c8fea2 1/*
64afe353 2 * Copyright (C) 2007-2011 B.A.T.M.A.N. contributors:
c6c8fea2
SE
3 *
4 * Marek Lindner, Simon Wunderlich
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301, USA
19 *
20 */
21
22#include "main.h"
23#include "routing.h"
24#include "send.h"
25#include "hash.h"
26#include "soft-interface.h"
27#include "hard-interface.h"
28#include "icmp_socket.h"
29#include "translation-table.h"
30#include "originator.h"
c6c8fea2
SE
31#include "ring_buffer.h"
32#include "vis.h"
33#include "aggregation.h"
34#include "gateway_common.h"
35#include "gateway_client.h"
36#include "unicast.h"
37
e6c10f43 38void slide_own_bcast_window(struct hard_iface *hard_iface)
c6c8fea2 39{
e6c10f43 40 struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
c6c8fea2 41 struct hashtable_t *hash = bat_priv->orig_hash;
7aadf889 42 struct hlist_node *node;
c6c8fea2 43 struct hlist_head *head;
c6c8fea2
SE
44 struct orig_node *orig_node;
45 unsigned long *word;
46 int i;
47 size_t word_index;
48
c6c8fea2
SE
49 for (i = 0; i < hash->size; i++) {
50 head = &hash->table[i];
51
fb778ea1 52 rcu_read_lock();
7aadf889 53 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
2ae2daf6 54 spin_lock_bh(&orig_node->ogm_cnt_lock);
e6c10f43 55 word_index = hard_iface->if_num * NUM_WORDS;
c6c8fea2
SE
56 word = &(orig_node->bcast_own[word_index]);
57
58 bit_get_packet(bat_priv, word, 1, 0);
e6c10f43 59 orig_node->bcast_own_sum[hard_iface->if_num] =
c6c8fea2 60 bit_packet_count(word);
2ae2daf6 61 spin_unlock_bh(&orig_node->ogm_cnt_lock);
c6c8fea2 62 }
fb778ea1 63 rcu_read_unlock();
c6c8fea2 64 }
c6c8fea2
SE
65}
66
a73105b8
AQ
67static void update_transtable(struct bat_priv *bat_priv,
68 struct orig_node *orig_node,
69 const unsigned char *tt_buff,
70 uint8_t tt_num_changes, uint8_t ttvn,
71 uint16_t tt_crc)
c6c8fea2 72{
a73105b8
AQ
73 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
74 bool full_table = true;
75
76 /* the ttvn increased by one -> we can apply the attached changes */
77 if (ttvn - orig_ttvn == 1) {
78 /* the OGM could not contain the changes because they were too
79 * many to fit in one frame or because they have already been
80 * sent TT_OGM_APPEND_MAX times. In this case send a tt
81 * request */
82 if (!tt_num_changes) {
83 full_table = false;
84 goto request_table;
85 }
86
87 tt_update_changes(bat_priv, orig_node, tt_num_changes, ttvn,
88 (struct tt_change *)tt_buff);
89
90 /* Even if we received the crc into the OGM, we prefer
91 * to recompute it to spot any possible inconsistency
92 * in the global table */
a73105b8 93 orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
a7f9becb
AQ
94
95 /* The ttvn alone is not enough to guarantee consistency
96 * because a single value could repesent different states
97 * (due to the wrap around). Thus a node has to check whether
98 * the resulting table (after applying the changes) is still
99 * consistent or not. E.g. a node could disconnect while its
100 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
101 * checking the CRC value is mandatory to detect the
102 * inconsistency */
103 if (orig_node->tt_crc != tt_crc)
104 goto request_table;
105
cc47f66e
AQ
106 /* Roaming phase is over: tables are in sync again. I can
107 * unset the flag */
108 orig_node->tt_poss_change = false;
a73105b8
AQ
109 } else {
110 /* if we missed more than one change or our tables are not
111 * in sync anymore -> request fresh tt data */
112 if (ttvn != orig_ttvn || orig_node->tt_crc != tt_crc) {
113request_table:
114 bat_dbg(DBG_TT, bat_priv, "TT inconsistency for %pM. "
115 "Need to retrieve the correct information "
116 "(ttvn: %u last_ttvn: %u crc: %u last_crc: "
117 "%u num_changes: %u)\n", orig_node->orig, ttvn,
118 orig_ttvn, tt_crc, orig_node->tt_crc,
119 tt_num_changes);
120 send_tt_request(bat_priv, orig_node, ttvn, tt_crc,
121 full_table);
122 return;
123 }
c6c8fea2
SE
124 }
125}
126
a73105b8
AQ
127static void update_route(struct bat_priv *bat_priv,
128 struct orig_node *orig_node,
129 struct neigh_node *neigh_node)
c6c8fea2 130{
e1a5382f
LL
131 struct neigh_node *curr_router;
132
133 curr_router = orig_node_get_router(orig_node);
a8e7f4bc 134
c6c8fea2 135 /* route deleted */
e1a5382f 136 if ((curr_router) && (!neigh_node)) {
c6c8fea2
SE
137 bat_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n",
138 orig_node->orig);
2dafb49d 139 tt_global_del_orig(bat_priv, orig_node,
a73105b8 140 "Deleted route towards originator");
c6c8fea2 141
e1a5382f
LL
142 /* route added */
143 } else if ((!curr_router) && (neigh_node)) {
c6c8fea2
SE
144
145 bat_dbg(DBG_ROUTES, bat_priv,
146 "Adding route towards: %pM (via %pM)\n",
147 orig_node->orig, neigh_node->addr);
e1a5382f 148 /* route changed */
bb899b89 149 } else if (neigh_node && curr_router) {
c6c8fea2
SE
150 bat_dbg(DBG_ROUTES, bat_priv,
151 "Changing route towards: %pM "
152 "(now via %pM - was via %pM)\n",
153 orig_node->orig, neigh_node->addr,
e1a5382f 154 curr_router->addr);
c6c8fea2
SE
155 }
156
e1a5382f
LL
157 if (curr_router)
158 neigh_node_free_ref(curr_router);
159
160 /* increase refcount of new best neighbor */
44524fcd
ML
161 if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount))
162 neigh_node = NULL;
e1a5382f
LL
163
164 spin_lock_bh(&orig_node->neigh_list_lock);
165 rcu_assign_pointer(orig_node->router, neigh_node);
166 spin_unlock_bh(&orig_node->neigh_list_lock);
167
168 /* decrease refcount of previous best neighbor */
169 if (curr_router)
170 neigh_node_free_ref(curr_router);
c6c8fea2
SE
171}
172
c6c8fea2 173void update_routes(struct bat_priv *bat_priv, struct orig_node *orig_node,
a73105b8 174 struct neigh_node *neigh_node)
c6c8fea2 175{
e1a5382f 176 struct neigh_node *router = NULL;
c6c8fea2
SE
177
178 if (!orig_node)
e1a5382f
LL
179 goto out;
180
181 router = orig_node_get_router(orig_node);
c6c8fea2 182
e1a5382f 183 if (router != neigh_node)
a73105b8 184 update_route(bat_priv, orig_node, neigh_node);
e1a5382f
LL
185
186out:
187 if (router)
188 neigh_node_free_ref(router);
c6c8fea2
SE
189}
190
191static int is_bidirectional_neigh(struct orig_node *orig_node,
192 struct orig_node *orig_neigh_node,
193 struct batman_packet *batman_packet,
e6c10f43 194 struct hard_iface *if_incoming)
c6c8fea2
SE
195{
196 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1605d0d6 197 struct neigh_node *neigh_node = NULL, *tmp_neigh_node;
9591a79f 198 struct hlist_node *node;
b4e17054 199 uint8_t total_count;
0ede9f41
ML
200 uint8_t orig_eq_count, neigh_rq_count, tq_own;
201 int tq_asym_penalty, ret = 0;
c6c8fea2 202
27aea212
DF
203 /* find corresponding one hop neighbor */
204 rcu_read_lock();
205 hlist_for_each_entry_rcu(tmp_neigh_node, node,
206 &orig_neigh_node->neigh_list, list) {
1605d0d6 207
27aea212
DF
208 if (!compare_eth(tmp_neigh_node->addr, orig_neigh_node->orig))
209 continue;
c6c8fea2 210
27aea212
DF
211 if (tmp_neigh_node->if_incoming != if_incoming)
212 continue;
c6c8fea2 213
27aea212
DF
214 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
215 continue;
1605d0d6 216
27aea212
DF
217 neigh_node = tmp_neigh_node;
218 break;
219 }
220 rcu_read_unlock();
1605d0d6 221
27aea212
DF
222 if (!neigh_node)
223 neigh_node = create_neighbor(orig_neigh_node,
224 orig_neigh_node,
225 orig_neigh_node->orig,
226 if_incoming);
1605d0d6 227
27aea212
DF
228 if (!neigh_node)
229 goto out;
c6c8fea2 230
27aea212
DF
231 /* if orig_node is direct neighbour update neigh_node last_valid */
232 if (orig_node == orig_neigh_node)
233 neigh_node->last_valid = jiffies;
c6c8fea2
SE
234
235 orig_node->last_valid = jiffies;
236
27aea212 237 /* find packet count of corresponding one hop neighbor */
0ede9f41
ML
238 spin_lock_bh(&orig_node->ogm_cnt_lock);
239 orig_eq_count = orig_neigh_node->bcast_own_sum[if_incoming->if_num];
240 neigh_rq_count = neigh_node->real_packet_count;
241 spin_unlock_bh(&orig_node->ogm_cnt_lock);
242
c6c8fea2 243 /* pay attention to not get a value bigger than 100 % */
0ede9f41
ML
244 total_count = (orig_eq_count > neigh_rq_count ?
245 neigh_rq_count : orig_eq_count);
c6c8fea2
SE
246
247 /* if we have too few packets (too less data) we set tq_own to zero */
248 /* if we receive too few packets it is not considered bidirectional */
249 if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
0ede9f41
ML
250 (neigh_rq_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
251 tq_own = 0;
c6c8fea2
SE
252 else
253 /* neigh_node->real_packet_count is never zero as we
254 * only purge old information when getting new
255 * information */
0ede9f41 256 tq_own = (TQ_MAX_VALUE * total_count) / neigh_rq_count;
c6c8fea2
SE
257
258 /*
259 * 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
260 * affect the nearly-symmetric links only a little, but
261 * punishes asymmetric links more. This will give a value
262 * between 0 and TQ_MAX_VALUE
263 */
0ede9f41
ML
264 tq_asym_penalty = TQ_MAX_VALUE - (TQ_MAX_VALUE *
265 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
266 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
267 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count)) /
268 (TQ_LOCAL_WINDOW_SIZE *
269 TQ_LOCAL_WINDOW_SIZE *
270 TQ_LOCAL_WINDOW_SIZE);
271
272 batman_packet->tq = ((batman_packet->tq * tq_own * tq_asym_penalty) /
273 (TQ_MAX_VALUE * TQ_MAX_VALUE));
c6c8fea2
SE
274
275 bat_dbg(DBG_BATMAN, bat_priv,
276 "bidirectional: "
277 "orig = %-15pM neigh = %-15pM => own_bcast = %2i, "
278 "real recv = %2i, local tq: %3i, asym_penalty: %3i, "
279 "total tq: %3i\n",
280 orig_node->orig, orig_neigh_node->orig, total_count,
0ede9f41 281 neigh_rq_count, tq_own, tq_asym_penalty, batman_packet->tq);
c6c8fea2
SE
282
283 /* if link has the minimum required transmission quality
284 * consider it bidirectional */
285 if (batman_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
a775eb84
ML
286 ret = 1;
287
a775eb84
ML
288out:
289 if (neigh_node)
44524fcd 290 neigh_node_free_ref(neigh_node);
a775eb84 291 return ret;
c6c8fea2
SE
292}
293
a4c135c5
SW
294/* caller must hold the neigh_list_lock */
295void bonding_candidate_del(struct orig_node *orig_node,
296 struct neigh_node *neigh_node)
297{
298 /* this neighbor is not part of our candidate list */
299 if (list_empty(&neigh_node->bonding_list))
300 goto out;
301
302 list_del_rcu(&neigh_node->bonding_list);
a4c135c5 303 INIT_LIST_HEAD(&neigh_node->bonding_list);
44524fcd 304 neigh_node_free_ref(neigh_node);
a4c135c5
SW
305 atomic_dec(&orig_node->bond_candidates);
306
307out:
308 return;
309}
310
311static void bonding_candidate_add(struct orig_node *orig_node,
312 struct neigh_node *neigh_node)
313{
314 struct hlist_node *node;
e1a5382f
LL
315 struct neigh_node *tmp_neigh_node, *router = NULL;
316 uint8_t interference_candidate = 0;
a4c135c5
SW
317
318 spin_lock_bh(&orig_node->neigh_list_lock);
319
320 /* only consider if it has the same primary address ... */
39901e71
ML
321 if (!compare_eth(orig_node->orig,
322 neigh_node->orig_node->primary_addr))
a4c135c5
SW
323 goto candidate_del;
324
e1a5382f
LL
325 router = orig_node_get_router(orig_node);
326 if (!router)
a4c135c5
SW
327 goto candidate_del;
328
a4c135c5 329 /* ... and is good enough to be considered */
e1a5382f 330 if (neigh_node->tq_avg < router->tq_avg - BONDING_TQ_THRESHOLD)
a4c135c5
SW
331 goto candidate_del;
332
333 /**
334 * check if we have another candidate with the same mac address or
335 * interface. If we do, we won't select this candidate because of
336 * possible interference.
337 */
338 hlist_for_each_entry_rcu(tmp_neigh_node, node,
339 &orig_node->neigh_list, list) {
340
341 if (tmp_neigh_node == neigh_node)
342 continue;
343
344 /* we only care if the other candidate is even
345 * considered as candidate. */
346 if (list_empty(&tmp_neigh_node->bonding_list))
347 continue;
348
349 if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
39901e71 350 (compare_eth(neigh_node->addr, tmp_neigh_node->addr))) {
a4c135c5
SW
351 interference_candidate = 1;
352 break;
353 }
354 }
355
356 /* don't care further if it is an interference candidate */
357 if (interference_candidate)
358 goto candidate_del;
359
360 /* this neighbor already is part of our candidate list */
361 if (!list_empty(&neigh_node->bonding_list))
362 goto out;
363
44524fcd
ML
364 if (!atomic_inc_not_zero(&neigh_node->refcount))
365 goto out;
366
a4c135c5 367 list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
a4c135c5
SW
368 atomic_inc(&orig_node->bond_candidates);
369 goto out;
370
371candidate_del:
372 bonding_candidate_del(orig_node, neigh_node);
373
374out:
375 spin_unlock_bh(&orig_node->neigh_list_lock);
e1a5382f
LL
376
377 if (router)
378 neigh_node_free_ref(router);
a4c135c5
SW
379}
380
381/* copy primary address for bonding */
747e4221 382static void bonding_save_primary(const struct orig_node *orig_node,
a4c135c5 383 struct orig_node *orig_neigh_node,
747e4221 384 const struct batman_packet *batman_packet)
a4c135c5
SW
385{
386 if (!(batman_packet->flags & PRIMARIES_FIRST_HOP))
387 return;
388
389 memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
390}
391
747e4221
SE
392static void update_orig(struct bat_priv *bat_priv, struct orig_node *orig_node,
393 const struct ethhdr *ethhdr,
394 const struct batman_packet *batman_packet,
e6c10f43 395 struct hard_iface *if_incoming,
a73105b8 396 const unsigned char *tt_buff, int is_duplicate)
c6c8fea2
SE
397{
398 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
e1a5382f 399 struct neigh_node *router = NULL;
2ae2daf6 400 struct orig_node *orig_node_tmp;
9591a79f 401 struct hlist_node *node;
2ae2daf6 402 uint8_t bcast_own_sum_orig, bcast_own_sum_neigh;
c6c8fea2
SE
403
404 bat_dbg(DBG_BATMAN, bat_priv, "update_originator(): "
405 "Searching and updating originator entry of received packet\n");
406
f987ed6e
ML
407 rcu_read_lock();
408 hlist_for_each_entry_rcu(tmp_neigh_node, node,
409 &orig_node->neigh_list, list) {
39901e71 410 if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
44524fcd
ML
411 (tmp_neigh_node->if_incoming == if_incoming) &&
412 atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
413 if (neigh_node)
414 neigh_node_free_ref(neigh_node);
c6c8fea2
SE
415 neigh_node = tmp_neigh_node;
416 continue;
417 }
418
419 if (is_duplicate)
420 continue;
421
68003903 422 spin_lock_bh(&tmp_neigh_node->tq_lock);
c6c8fea2
SE
423 ring_buffer_set(tmp_neigh_node->tq_recv,
424 &tmp_neigh_node->tq_index, 0);
425 tmp_neigh_node->tq_avg =
426 ring_buffer_avg(tmp_neigh_node->tq_recv);
68003903 427 spin_unlock_bh(&tmp_neigh_node->tq_lock);
c6c8fea2
SE
428 }
429
430 if (!neigh_node) {
431 struct orig_node *orig_tmp;
432
433 orig_tmp = get_orig_node(bat_priv, ethhdr->h_source);
434 if (!orig_tmp)
a775eb84 435 goto unlock;
c6c8fea2
SE
436
437 neigh_node = create_neighbor(orig_node, orig_tmp,
438 ethhdr->h_source, if_incoming);
16b1aba8 439
7b36e8ee 440 orig_node_free_ref(orig_tmp);
c6c8fea2 441 if (!neigh_node)
a775eb84 442 goto unlock;
c6c8fea2
SE
443 } else
444 bat_dbg(DBG_BATMAN, bat_priv,
445 "Updating existing last-hop neighbor of originator\n");
446
a775eb84
ML
447 rcu_read_unlock();
448
c6c8fea2
SE
449 orig_node->flags = batman_packet->flags;
450 neigh_node->last_valid = jiffies;
451
68003903 452 spin_lock_bh(&neigh_node->tq_lock);
c6c8fea2
SE
453 ring_buffer_set(neigh_node->tq_recv,
454 &neigh_node->tq_index,
455 batman_packet->tq);
456 neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
68003903 457 spin_unlock_bh(&neigh_node->tq_lock);
c6c8fea2
SE
458
459 if (!is_duplicate) {
460 orig_node->last_ttl = batman_packet->ttl;
461 neigh_node->last_ttl = batman_packet->ttl;
462 }
463
a4c135c5
SW
464 bonding_candidate_add(orig_node, neigh_node);
465
c6c8fea2
SE
466 /* if this neighbor already is our next hop there is nothing
467 * to change */
e1a5382f
LL
468 router = orig_node_get_router(orig_node);
469 if (router == neigh_node)
2dafb49d 470 goto update_tt;
c6c8fea2
SE
471
472 /* if this neighbor does not offer a better TQ we won't consider it */
e1a5382f 473 if (router && (router->tq_avg > neigh_node->tq_avg))
2dafb49d 474 goto update_tt;
c6c8fea2
SE
475
476 /* if the TQ is the same and the link not more symetric we
477 * won't consider it either */
e1a5382f
LL
478 if (router && (neigh_node->tq_avg == router->tq_avg)) {
479 orig_node_tmp = router->orig_node;
2ae2daf6
ML
480 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
481 bcast_own_sum_orig =
482 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
483 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
484
485 orig_node_tmp = neigh_node->orig_node;
486 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
487 bcast_own_sum_neigh =
488 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
489 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
490
491 if (bcast_own_sum_orig >= bcast_own_sum_neigh)
2dafb49d 492 goto update_tt;
2ae2daf6 493 }
c6c8fea2 494
a73105b8 495 update_routes(bat_priv, orig_node, neigh_node);
c6c8fea2 496
2dafb49d 497update_tt:
a73105b8
AQ
498 /* I have to check for transtable changes only if the OGM has been
499 * sent through a primary interface */
500 if (((batman_packet->orig != ethhdr->h_source) &&
501 (batman_packet->ttl > 2)) ||
502 (batman_packet->flags & PRIMARIES_FIRST_HOP))
503 update_transtable(bat_priv, orig_node, tt_buff,
504 batman_packet->tt_num_changes,
505 batman_packet->ttvn,
506 batman_packet->tt_crc);
c6c8fea2 507
c6c8fea2
SE
508 if (orig_node->gw_flags != batman_packet->gw_flags)
509 gw_node_update(bat_priv, orig_node, batman_packet->gw_flags);
510
511 orig_node->gw_flags = batman_packet->gw_flags;
512
513 /* restart gateway selection if fast or late switching was enabled */
514 if ((orig_node->gw_flags) &&
515 (atomic_read(&bat_priv->gw_mode) == GW_MODE_CLIENT) &&
516 (atomic_read(&bat_priv->gw_sel_class) > 2))
517 gw_check_election(bat_priv, orig_node);
a775eb84
ML
518
519 goto out;
520
521unlock:
522 rcu_read_unlock();
523out:
524 if (neigh_node)
44524fcd 525 neigh_node_free_ref(neigh_node);
e1a5382f
LL
526 if (router)
527 neigh_node_free_ref(router);
c6c8fea2
SE
528}
529
530/* checks whether the host restarted and is in the protection time.
531 * returns:
532 * 0 if the packet is to be accepted
533 * 1 if the packet is to be ignored.
534 */
535static int window_protected(struct bat_priv *bat_priv,
536 int32_t seq_num_diff,
537 unsigned long *last_reset)
538{
539 if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE)
540 || (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
541 if (time_after(jiffies, *last_reset +
542 msecs_to_jiffies(RESET_PROTECTION_MS))) {
543
544 *last_reset = jiffies;
545 bat_dbg(DBG_BATMAN, bat_priv,
546 "old packet received, start protection\n");
547
548 return 0;
549 } else
550 return 1;
551 }
552 return 0;
553}
554
555/* processes a batman packet for all interfaces, adjusts the sequence number and
556 * finds out whether it is a duplicate.
557 * returns:
558 * 1 the packet is a duplicate
559 * 0 the packet has not yet been received
560 * -1 the packet is old and has been received while the seqno window
561 * was protected. Caller should drop it.
562 */
b2c44a53 563static int count_real_packets(const struct ethhdr *ethhdr,
747e4221
SE
564 const struct batman_packet *batman_packet,
565 const struct hard_iface *if_incoming)
c6c8fea2
SE
566{
567 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
568 struct orig_node *orig_node;
569 struct neigh_node *tmp_neigh_node;
9591a79f 570 struct hlist_node *node;
b2c44a53 571 int is_duplicate = 0;
c6c8fea2
SE
572 int32_t seq_diff;
573 int need_update = 0;
0ede9f41 574 int set_mark, ret = -1;
c6c8fea2
SE
575
576 orig_node = get_orig_node(bat_priv, batman_packet->orig);
577 if (!orig_node)
578 return 0;
579
0ede9f41 580 spin_lock_bh(&orig_node->ogm_cnt_lock);
c6c8fea2
SE
581 seq_diff = batman_packet->seqno - orig_node->last_real_seqno;
582
583 /* signalize caller that the packet is to be dropped. */
584 if (window_protected(bat_priv, seq_diff,
585 &orig_node->batman_seqno_reset))
0ede9f41 586 goto out;
c6c8fea2 587
f987ed6e
ML
588 rcu_read_lock();
589 hlist_for_each_entry_rcu(tmp_neigh_node, node,
590 &orig_node->neigh_list, list) {
c6c8fea2
SE
591
592 is_duplicate |= get_bit_status(tmp_neigh_node->real_bits,
593 orig_node->last_real_seqno,
594 batman_packet->seqno);
595
39901e71 596 if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
c6c8fea2
SE
597 (tmp_neigh_node->if_incoming == if_incoming))
598 set_mark = 1;
599 else
600 set_mark = 0;
601
602 /* if the window moved, set the update flag. */
603 need_update |= bit_get_packet(bat_priv,
604 tmp_neigh_node->real_bits,
605 seq_diff, set_mark);
606
607 tmp_neigh_node->real_packet_count =
608 bit_packet_count(tmp_neigh_node->real_bits);
609 }
f987ed6e 610 rcu_read_unlock();
c6c8fea2
SE
611
612 if (need_update) {
613 bat_dbg(DBG_BATMAN, bat_priv,
614 "updating last_seqno: old %d, new %d\n",
615 orig_node->last_real_seqno, batman_packet->seqno);
616 orig_node->last_real_seqno = batman_packet->seqno;
617 }
618
0ede9f41 619 ret = is_duplicate;
16b1aba8 620
0ede9f41
ML
621out:
622 spin_unlock_bh(&orig_node->ogm_cnt_lock);
7b36e8ee 623 orig_node_free_ref(orig_node);
0ede9f41 624 return ret;
c6c8fea2
SE
625}
626
747e4221 627void receive_bat_packet(const struct ethhdr *ethhdr,
a4c135c5 628 struct batman_packet *batman_packet,
a73105b8 629 const unsigned char *tt_buff,
e6c10f43 630 struct hard_iface *if_incoming)
c6c8fea2
SE
631{
632 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
e6c10f43 633 struct hard_iface *hard_iface;
c6c8fea2 634 struct orig_node *orig_neigh_node, *orig_node;
e1a5382f
LL
635 struct neigh_node *router = NULL, *router_router = NULL;
636 struct neigh_node *orig_neigh_router = NULL;
b4e17054
SE
637 int has_directlink_flag;
638 int is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
639 int is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
b2c44a53 640 int is_duplicate;
c6c8fea2
SE
641 uint32_t if_incoming_seqno;
642
643 /* Silently drop when the batman packet is actually not a
644 * correct packet.
645 *
646 * This might happen if a packet is padded (e.g. Ethernet has a
647 * minimum frame length of 64 byte) and the aggregation interprets
648 * it as an additional length.
649 *
650 * TODO: A more sane solution would be to have a bit in the
651 * batman_packet to detect whether the packet is the last
652 * packet in an aggregation. Here we expect that the padding
653 * is always zero (or not 0x01)
654 */
655 if (batman_packet->packet_type != BAT_PACKET)
656 return;
657
658 /* could be changed by schedule_own_packet() */
659 if_incoming_seqno = atomic_read(&if_incoming->seqno);
660
661 has_directlink_flag = (batman_packet->flags & DIRECTLINK ? 1 : 0);
662
39901e71
ML
663 is_single_hop_neigh = (compare_eth(ethhdr->h_source,
664 batman_packet->orig) ? 1 : 0);
c6c8fea2
SE
665
666 bat_dbg(DBG_BATMAN, bat_priv,
667 "Received BATMAN packet via NB: %pM, IF: %s [%pM] "
a73105b8
AQ
668 "(from OG: %pM, via prev OG: %pM, seqno %d, ttvn %u, "
669 "crc %u, changes %u, td %d, TTL %d, V %d, IDF %d)\n",
c6c8fea2
SE
670 ethhdr->h_source, if_incoming->net_dev->name,
671 if_incoming->net_dev->dev_addr, batman_packet->orig,
672 batman_packet->prev_sender, batman_packet->seqno,
a73105b8
AQ
673 batman_packet->ttvn, batman_packet->tt_crc,
674 batman_packet->tt_num_changes, batman_packet->tq,
675 batman_packet->ttl, batman_packet->version,
c6c8fea2
SE
676 has_directlink_flag);
677
678 rcu_read_lock();
e6c10f43
ML
679 list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
680 if (hard_iface->if_status != IF_ACTIVE)
c6c8fea2
SE
681 continue;
682
e6c10f43 683 if (hard_iface->soft_iface != if_incoming->soft_iface)
c6c8fea2
SE
684 continue;
685
39901e71 686 if (compare_eth(ethhdr->h_source,
e6c10f43 687 hard_iface->net_dev->dev_addr))
c6c8fea2
SE
688 is_my_addr = 1;
689
39901e71 690 if (compare_eth(batman_packet->orig,
e6c10f43 691 hard_iface->net_dev->dev_addr))
c6c8fea2
SE
692 is_my_orig = 1;
693
39901e71 694 if (compare_eth(batman_packet->prev_sender,
e6c10f43 695 hard_iface->net_dev->dev_addr))
c6c8fea2
SE
696 is_my_oldorig = 1;
697
44e92bc8 698 if (is_broadcast_ether_addr(ethhdr->h_source))
c6c8fea2
SE
699 is_broadcast = 1;
700 }
701 rcu_read_unlock();
702
703 if (batman_packet->version != COMPAT_VERSION) {
704 bat_dbg(DBG_BATMAN, bat_priv,
705 "Drop packet: incompatible batman version (%i)\n",
706 batman_packet->version);
707 return;
708 }
709
710 if (is_my_addr) {
711 bat_dbg(DBG_BATMAN, bat_priv,
712 "Drop packet: received my own broadcast (sender: %pM"
713 ")\n",
714 ethhdr->h_source);
715 return;
716 }
717
718 if (is_broadcast) {
719 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
720 "ignoring all packets with broadcast source addr (sender: %pM"
721 ")\n", ethhdr->h_source);
722 return;
723 }
724
725 if (is_my_orig) {
726 unsigned long *word;
727 int offset;
728
729 orig_neigh_node = get_orig_node(bat_priv, ethhdr->h_source);
c6c8fea2
SE
730 if (!orig_neigh_node)
731 return;
732
733 /* neighbor has to indicate direct link and it has to
734 * come via the corresponding interface */
d1829fa0 735 /* save packet seqno for bidirectional check */
c6c8fea2 736 if (has_directlink_flag &&
39901e71 737 compare_eth(if_incoming->net_dev->dev_addr,
d1829fa0 738 batman_packet->orig)) {
c6c8fea2 739 offset = if_incoming->if_num * NUM_WORDS;
2ae2daf6
ML
740
741 spin_lock_bh(&orig_neigh_node->ogm_cnt_lock);
c6c8fea2 742 word = &(orig_neigh_node->bcast_own[offset]);
d1829fa0
DF
743 bit_mark(word,
744 if_incoming_seqno - batman_packet->seqno - 2);
c6c8fea2
SE
745 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
746 bit_packet_count(word);
2ae2daf6 747 spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock);
c6c8fea2
SE
748 }
749
750 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
751 "originator packet from myself (via neighbor)\n");
7b36e8ee 752 orig_node_free_ref(orig_neigh_node);
c6c8fea2
SE
753 return;
754 }
755
756 if (is_my_oldorig) {
757 bat_dbg(DBG_BATMAN, bat_priv,
758 "Drop packet: ignoring all rebroadcast echos (sender: "
759 "%pM)\n", ethhdr->h_source);
760 return;
761 }
762
763 orig_node = get_orig_node(bat_priv, batman_packet->orig);
764 if (!orig_node)
765 return;
766
767 is_duplicate = count_real_packets(ethhdr, batman_packet, if_incoming);
768
769 if (is_duplicate == -1) {
770 bat_dbg(DBG_BATMAN, bat_priv,
771 "Drop packet: packet within seqno protection time "
772 "(sender: %pM)\n", ethhdr->h_source);
16b1aba8 773 goto out;
c6c8fea2
SE
774 }
775
776 if (batman_packet->tq == 0) {
777 bat_dbg(DBG_BATMAN, bat_priv,
778 "Drop packet: originator packet with tq equal 0\n");
16b1aba8 779 goto out;
c6c8fea2
SE
780 }
781
e1a5382f
LL
782 router = orig_node_get_router(orig_node);
783 if (router)
784 router_router = orig_node_get_router(router->orig_node);
785
c6c8fea2 786 /* avoid temporary routing loops */
e1a5382f
LL
787 if (router && router_router &&
788 (compare_eth(router->addr, batman_packet->prev_sender)) &&
39901e71 789 !(compare_eth(batman_packet->orig, batman_packet->prev_sender)) &&
e1a5382f 790 (compare_eth(router->addr, router_router->addr))) {
c6c8fea2
SE
791 bat_dbg(DBG_BATMAN, bat_priv,
792 "Drop packet: ignoring all rebroadcast packets that "
793 "may make me loop (sender: %pM)\n", ethhdr->h_source);
16b1aba8 794 goto out;
c6c8fea2
SE
795 }
796
797 /* if sender is a direct neighbor the sender mac equals
798 * originator mac */
799 orig_neigh_node = (is_single_hop_neigh ?
800 orig_node :
801 get_orig_node(bat_priv, ethhdr->h_source));
802 if (!orig_neigh_node)
d0072609 803 goto out;
c6c8fea2 804
e1a5382f
LL
805 orig_neigh_router = orig_node_get_router(orig_neigh_node);
806
c6c8fea2
SE
807 /* drop packet if sender is not a direct neighbor and if we
808 * don't route towards it */
e1a5382f 809 if (!is_single_hop_neigh && (!orig_neigh_router)) {
c6c8fea2
SE
810 bat_dbg(DBG_BATMAN, bat_priv,
811 "Drop packet: OGM via unknown neighbor!\n");
16b1aba8 812 goto out_neigh;
c6c8fea2
SE
813 }
814
815 is_bidirectional = is_bidirectional_neigh(orig_node, orig_neigh_node,
816 batman_packet, if_incoming);
817
a4c135c5
SW
818 bonding_save_primary(orig_node, orig_neigh_node, batman_packet);
819
c6c8fea2
SE
820 /* update ranking if it is not a duplicate or has the same
821 * seqno and similar ttl as the non-duplicate */
822 if (is_bidirectional &&
823 (!is_duplicate ||
824 ((orig_node->last_real_seqno == batman_packet->seqno) &&
825 (orig_node->last_ttl - 3 <= batman_packet->ttl))))
826 update_orig(bat_priv, orig_node, ethhdr, batman_packet,
a73105b8 827 if_incoming, tt_buff, is_duplicate);
c6c8fea2 828
c6c8fea2
SE
829 /* is single hop (direct) neighbor */
830 if (is_single_hop_neigh) {
831
832 /* mark direct link on incoming interface */
833 schedule_forward_packet(orig_node, ethhdr, batman_packet,
a73105b8 834 1, if_incoming);
c6c8fea2
SE
835
836 bat_dbg(DBG_BATMAN, bat_priv, "Forwarding packet: "
837 "rebroadcast neighbor packet with direct link flag\n");
16b1aba8 838 goto out_neigh;
c6c8fea2
SE
839 }
840
841 /* multihop originator */
842 if (!is_bidirectional) {
843 bat_dbg(DBG_BATMAN, bat_priv,
844 "Drop packet: not received via bidirectional link\n");
16b1aba8 845 goto out_neigh;
c6c8fea2
SE
846 }
847
848 if (is_duplicate) {
849 bat_dbg(DBG_BATMAN, bat_priv,
850 "Drop packet: duplicate packet received\n");
16b1aba8 851 goto out_neigh;
c6c8fea2
SE
852 }
853
854 bat_dbg(DBG_BATMAN, bat_priv,
855 "Forwarding packet: rebroadcast originator packet\n");
856 schedule_forward_packet(orig_node, ethhdr, batman_packet,
a73105b8 857 0, if_incoming);
16b1aba8
ML
858
859out_neigh:
7b36e8ee
ML
860 if ((orig_neigh_node) && (!is_single_hop_neigh))
861 orig_node_free_ref(orig_neigh_node);
16b1aba8 862out:
e1a5382f
LL
863 if (router)
864 neigh_node_free_ref(router);
865 if (router_router)
866 neigh_node_free_ref(router_router);
867 if (orig_neigh_router)
868 neigh_node_free_ref(orig_neigh_router);
869
7b36e8ee 870 orig_node_free_ref(orig_node);
c6c8fea2
SE
871}
872
e6c10f43 873int recv_bat_packet(struct sk_buff *skb, struct hard_iface *hard_iface)
c6c8fea2 874{
c6c8fea2
SE
875 struct ethhdr *ethhdr;
876
877 /* drop packet if it has not necessary minimum size */
878 if (unlikely(!pskb_may_pull(skb, sizeof(struct batman_packet))))
879 return NET_RX_DROP;
880
881 ethhdr = (struct ethhdr *)skb_mac_header(skb);
882
883 /* packet with broadcast indication but unicast recipient */
884 if (!is_broadcast_ether_addr(ethhdr->h_dest))
885 return NET_RX_DROP;
886
887 /* packet with broadcast sender address */
888 if (is_broadcast_ether_addr(ethhdr->h_source))
889 return NET_RX_DROP;
890
891 /* create a copy of the skb, if needed, to modify it. */
892 if (skb_cow(skb, 0) < 0)
893 return NET_RX_DROP;
894
895 /* keep skb linear */
896 if (skb_linearize(skb) < 0)
897 return NET_RX_DROP;
898
899 ethhdr = (struct ethhdr *)skb_mac_header(skb);
900
c6c8fea2
SE
901 receive_aggr_bat_packet(ethhdr,
902 skb->data,
903 skb_headlen(skb),
e6c10f43 904 hard_iface);
c6c8fea2
SE
905
906 kfree_skb(skb);
907 return NET_RX_SUCCESS;
908}
909
910static int recv_my_icmp_packet(struct bat_priv *bat_priv,
911 struct sk_buff *skb, size_t icmp_len)
912{
32ae9b22 913 struct hard_iface *primary_if = NULL;
44524fcd 914 struct orig_node *orig_node = NULL;
e1a5382f 915 struct neigh_node *router = NULL;
c6c8fea2 916 struct icmp_packet_rr *icmp_packet;
44524fcd 917 int ret = NET_RX_DROP;
c6c8fea2
SE
918
919 icmp_packet = (struct icmp_packet_rr *)skb->data;
c6c8fea2
SE
920
921 /* add data to device queue */
922 if (icmp_packet->msg_type != ECHO_REQUEST) {
923 bat_socket_receive_packet(icmp_packet, icmp_len);
44524fcd 924 goto out;
c6c8fea2
SE
925 }
926
32ae9b22
ML
927 primary_if = primary_if_get_selected(bat_priv);
928 if (!primary_if)
44524fcd 929 goto out;
c6c8fea2
SE
930
931 /* answer echo request (ping) */
932 /* get routing information */
7aadf889 933 orig_node = orig_hash_find(bat_priv, icmp_packet->orig);
44524fcd 934 if (!orig_node)
e1a5382f 935 goto out;
c6c8fea2 936
e1a5382f
LL
937 router = orig_node_get_router(orig_node);
938 if (!router)
939 goto out;
c6c8fea2 940
44524fcd
ML
941 /* create a copy of the skb, if needed, to modify it. */
942 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
943 goto out;
944
945 icmp_packet = (struct icmp_packet_rr *)skb->data;
946
947 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
32ae9b22 948 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
44524fcd
ML
949 icmp_packet->msg_type = ECHO_REPLY;
950 icmp_packet->ttl = TTL;
951
e1a5382f 952 send_skb_packet(skb, router->if_incoming, router->addr);
44524fcd 953 ret = NET_RX_SUCCESS;
c6c8fea2 954
44524fcd 955out:
32ae9b22
ML
956 if (primary_if)
957 hardif_free_ref(primary_if);
e1a5382f
LL
958 if (router)
959 neigh_node_free_ref(router);
44524fcd 960 if (orig_node)
7b36e8ee 961 orig_node_free_ref(orig_node);
c6c8fea2
SE
962 return ret;
963}
964
965static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
74ef1153 966 struct sk_buff *skb)
c6c8fea2 967{
32ae9b22 968 struct hard_iface *primary_if = NULL;
44524fcd 969 struct orig_node *orig_node = NULL;
e1a5382f 970 struct neigh_node *router = NULL;
c6c8fea2 971 struct icmp_packet *icmp_packet;
44524fcd 972 int ret = NET_RX_DROP;
c6c8fea2
SE
973
974 icmp_packet = (struct icmp_packet *)skb->data;
c6c8fea2
SE
975
976 /* send TTL exceeded if packet is an echo request (traceroute) */
977 if (icmp_packet->msg_type != ECHO_REQUEST) {
978 pr_debug("Warning - can't forward icmp packet from %pM to "
979 "%pM: ttl exceeded\n", icmp_packet->orig,
980 icmp_packet->dst);
44524fcd 981 goto out;
c6c8fea2
SE
982 }
983
32ae9b22
ML
984 primary_if = primary_if_get_selected(bat_priv);
985 if (!primary_if)
44524fcd 986 goto out;
c6c8fea2
SE
987
988 /* get routing information */
7aadf889 989 orig_node = orig_hash_find(bat_priv, icmp_packet->orig);
44524fcd 990 if (!orig_node)
e1a5382f 991 goto out;
c6c8fea2 992
e1a5382f
LL
993 router = orig_node_get_router(orig_node);
994 if (!router)
995 goto out;
c6c8fea2 996
44524fcd
ML
997 /* create a copy of the skb, if needed, to modify it. */
998 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
999 goto out;
c6c8fea2 1000
44524fcd 1001 icmp_packet = (struct icmp_packet *)skb->data;
c6c8fea2 1002
44524fcd 1003 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
32ae9b22 1004 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
44524fcd
ML
1005 icmp_packet->msg_type = TTL_EXCEEDED;
1006 icmp_packet->ttl = TTL;
1007
e1a5382f 1008 send_skb_packet(skb, router->if_incoming, router->addr);
44524fcd 1009 ret = NET_RX_SUCCESS;
c6c8fea2 1010
44524fcd 1011out:
32ae9b22
ML
1012 if (primary_if)
1013 hardif_free_ref(primary_if);
e1a5382f
LL
1014 if (router)
1015 neigh_node_free_ref(router);
44524fcd 1016 if (orig_node)
7b36e8ee 1017 orig_node_free_ref(orig_node);
c6c8fea2
SE
1018 return ret;
1019}
1020
1021
e6c10f43 1022int recv_icmp_packet(struct sk_buff *skb, struct hard_iface *recv_if)
c6c8fea2
SE
1023{
1024 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1025 struct icmp_packet_rr *icmp_packet;
1026 struct ethhdr *ethhdr;
44524fcd 1027 struct orig_node *orig_node = NULL;
e1a5382f 1028 struct neigh_node *router = NULL;
c6c8fea2 1029 int hdr_size = sizeof(struct icmp_packet);
44524fcd 1030 int ret = NET_RX_DROP;
c6c8fea2
SE
1031
1032 /**
1033 * we truncate all incoming icmp packets if they don't match our size
1034 */
1035 if (skb->len >= sizeof(struct icmp_packet_rr))
1036 hdr_size = sizeof(struct icmp_packet_rr);
1037
1038 /* drop packet if it has not necessary minimum size */
1039 if (unlikely(!pskb_may_pull(skb, hdr_size)))
44524fcd 1040 goto out;
c6c8fea2
SE
1041
1042 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1043
1044 /* packet with unicast indication but broadcast recipient */
1045 if (is_broadcast_ether_addr(ethhdr->h_dest))
44524fcd 1046 goto out;
c6c8fea2
SE
1047
1048 /* packet with broadcast sender address */
1049 if (is_broadcast_ether_addr(ethhdr->h_source))
44524fcd 1050 goto out;
c6c8fea2
SE
1051
1052 /* not for me */
1053 if (!is_my_mac(ethhdr->h_dest))
44524fcd 1054 goto out;
c6c8fea2
SE
1055
1056 icmp_packet = (struct icmp_packet_rr *)skb->data;
1057
1058 /* add record route information if not full */
1059 if ((hdr_size == sizeof(struct icmp_packet_rr)) &&
1060 (icmp_packet->rr_cur < BAT_RR_LEN)) {
1061 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
1062 ethhdr->h_dest, ETH_ALEN);
1063 icmp_packet->rr_cur++;
1064 }
1065
1066 /* packet for me */
1067 if (is_my_mac(icmp_packet->dst))
1068 return recv_my_icmp_packet(bat_priv, skb, hdr_size);
1069
1070 /* TTL exceeded */
1071 if (icmp_packet->ttl < 2)
74ef1153 1072 return recv_icmp_ttl_exceeded(bat_priv, skb);
c6c8fea2 1073
c6c8fea2 1074 /* get routing information */
7aadf889 1075 orig_node = orig_hash_find(bat_priv, icmp_packet->dst);
44524fcd 1076 if (!orig_node)
e1a5382f 1077 goto out;
c6c8fea2 1078
e1a5382f
LL
1079 router = orig_node_get_router(orig_node);
1080 if (!router)
1081 goto out;
c6c8fea2 1082
44524fcd
ML
1083 /* create a copy of the skb, if needed, to modify it. */
1084 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
1085 goto out;
c6c8fea2 1086
44524fcd 1087 icmp_packet = (struct icmp_packet_rr *)skb->data;
c6c8fea2 1088
44524fcd
ML
1089 /* decrement ttl */
1090 icmp_packet->ttl--;
1091
1092 /* route it */
e1a5382f 1093 send_skb_packet(skb, router->if_incoming, router->addr);
44524fcd 1094 ret = NET_RX_SUCCESS;
c6c8fea2 1095
44524fcd 1096out:
e1a5382f
LL
1097 if (router)
1098 neigh_node_free_ref(router);
44524fcd 1099 if (orig_node)
7b36e8ee 1100 orig_node_free_ref(orig_node);
c6c8fea2
SE
1101 return ret;
1102}
1103
55158629
LL
1104/* In the bonding case, send the packets in a round
1105 * robin fashion over the remaining interfaces.
1106 *
1107 * This method rotates the bonding list and increases the
1108 * returned router's refcount. */
1109static struct neigh_node *find_bond_router(struct orig_node *primary_orig,
747e4221 1110 const struct hard_iface *recv_if)
55158629
LL
1111{
1112 struct neigh_node *tmp_neigh_node;
1113 struct neigh_node *router = NULL, *first_candidate = NULL;
1114
1115 rcu_read_lock();
1116 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
1117 bonding_list) {
1118 if (!first_candidate)
1119 first_candidate = tmp_neigh_node;
1120
1121 /* recv_if == NULL on the first node. */
1122 if (tmp_neigh_node->if_incoming == recv_if)
1123 continue;
1124
1125 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
1126 continue;
1127
1128 router = tmp_neigh_node;
1129 break;
1130 }
1131
1132 /* use the first candidate if nothing was found. */
1133 if (!router && first_candidate &&
1134 atomic_inc_not_zero(&first_candidate->refcount))
1135 router = first_candidate;
1136
1137 if (!router)
1138 goto out;
1139
1140 /* selected should point to the next element
1141 * after the current router */
1142 spin_lock_bh(&primary_orig->neigh_list_lock);
1143 /* this is a list_move(), which unfortunately
1144 * does not exist as rcu version */
1145 list_del_rcu(&primary_orig->bond_list);
1146 list_add_rcu(&primary_orig->bond_list,
1147 &router->bonding_list);
1148 spin_unlock_bh(&primary_orig->neigh_list_lock);
1149
1150out:
1151 rcu_read_unlock();
1152 return router;
1153}
1154
1155/* Interface Alternating: Use the best of the
1156 * remaining candidates which are not using
1157 * this interface.
1158 *
1159 * Increases the returned router's refcount */
1160static struct neigh_node *find_ifalter_router(struct orig_node *primary_orig,
747e4221 1161 const struct hard_iface *recv_if)
55158629
LL
1162{
1163 struct neigh_node *tmp_neigh_node;
1164 struct neigh_node *router = NULL, *first_candidate = NULL;
1165
1166 rcu_read_lock();
1167 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
1168 bonding_list) {
1169 if (!first_candidate)
1170 first_candidate = tmp_neigh_node;
1171
1172 /* recv_if == NULL on the first node. */
1173 if (tmp_neigh_node->if_incoming == recv_if)
1174 continue;
1175
1176 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
1177 continue;
1178
1179 /* if we don't have a router yet
1180 * or this one is better, choose it. */
1181 if ((!router) ||
1182 (tmp_neigh_node->tq_avg > router->tq_avg)) {
1183 /* decrement refcount of
1184 * previously selected router */
1185 if (router)
1186 neigh_node_free_ref(router);
1187
1188 router = tmp_neigh_node;
1189 atomic_inc_not_zero(&router->refcount);
1190 }
1191
1192 neigh_node_free_ref(tmp_neigh_node);
1193 }
1194
1195 /* use the first candidate if nothing was found. */
1196 if (!router && first_candidate &&
1197 atomic_inc_not_zero(&first_candidate->refcount))
1198 router = first_candidate;
1199
1200 rcu_read_unlock();
1201 return router;
1202}
1203
a73105b8
AQ
1204int recv_tt_query(struct sk_buff *skb, struct hard_iface *recv_if)
1205{
1206 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1207 struct tt_query_packet *tt_query;
1208 struct ethhdr *ethhdr;
1209
1210 /* drop packet if it has not necessary minimum size */
1211 if (unlikely(!pskb_may_pull(skb, sizeof(struct tt_query_packet))))
1212 goto out;
1213
1214 /* I could need to modify it */
1215 if (skb_cow(skb, sizeof(struct tt_query_packet)) < 0)
1216 goto out;
1217
1218 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1219
1220 /* packet with unicast indication but broadcast recipient */
1221 if (is_broadcast_ether_addr(ethhdr->h_dest))
1222 goto out;
1223
1224 /* packet with broadcast sender address */
1225 if (is_broadcast_ether_addr(ethhdr->h_source))
1226 goto out;
1227
1228 tt_query = (struct tt_query_packet *)skb->data;
1229
1230 tt_query->tt_data = ntohs(tt_query->tt_data);
1231
1232 switch (tt_query->flags & TT_QUERY_TYPE_MASK) {
1233 case TT_REQUEST:
1234 /* If we cannot provide an answer the tt_request is
1235 * forwarded */
1236 if (!send_tt_response(bat_priv, tt_query)) {
1237 bat_dbg(DBG_TT, bat_priv,
1238 "Routing TT_REQUEST to %pM [%c]\n",
1239 tt_query->dst,
1240 (tt_query->flags & TT_FULL_TABLE ? 'F' : '.'));
1241 tt_query->tt_data = htons(tt_query->tt_data);
1242 return route_unicast_packet(skb, recv_if);
1243 }
1244 break;
1245 case TT_RESPONSE:
1246 /* packet needs to be linearised to access the TT changes */
1247 if (skb_linearize(skb) < 0)
1248 goto out;
1249
1250 if (is_my_mac(tt_query->dst))
1251 handle_tt_response(bat_priv, tt_query);
1252 else {
1253 bat_dbg(DBG_TT, bat_priv,
1254 "Routing TT_RESPONSE to %pM [%c]\n",
1255 tt_query->dst,
1256 (tt_query->flags & TT_FULL_TABLE ? 'F' : '.'));
1257 tt_query->tt_data = htons(tt_query->tt_data);
1258 return route_unicast_packet(skb, recv_if);
1259 }
1260 break;
1261 }
1262
1263out:
1264 /* returning NET_RX_DROP will make the caller function kfree the skb */
1265 return NET_RX_DROP;
1266}
1267
cc47f66e
AQ
1268int recv_roam_adv(struct sk_buff *skb, struct hard_iface *recv_if)
1269{
1270 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1271 struct roam_adv_packet *roam_adv_packet;
1272 struct orig_node *orig_node;
1273 struct ethhdr *ethhdr;
1274
1275 /* drop packet if it has not necessary minimum size */
1276 if (unlikely(!pskb_may_pull(skb, sizeof(struct roam_adv_packet))))
1277 goto out;
1278
1279 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1280
1281 /* packet with unicast indication but broadcast recipient */
1282 if (is_broadcast_ether_addr(ethhdr->h_dest))
1283 goto out;
1284
1285 /* packet with broadcast sender address */
1286 if (is_broadcast_ether_addr(ethhdr->h_source))
1287 goto out;
1288
1289 roam_adv_packet = (struct roam_adv_packet *)skb->data;
1290
1291 if (!is_my_mac(roam_adv_packet->dst))
1292 return route_unicast_packet(skb, recv_if);
1293
1294 orig_node = orig_hash_find(bat_priv, roam_adv_packet->src);
1295 if (!orig_node)
1296 goto out;
1297
1298 bat_dbg(DBG_TT, bat_priv, "Received ROAMING_ADV from %pM "
1299 "(client %pM)\n", roam_adv_packet->src,
1300 roam_adv_packet->client);
1301
1302 tt_global_add(bat_priv, orig_node, roam_adv_packet->client,
1303 atomic_read(&orig_node->last_ttvn) + 1, true);
1304
1305 /* Roaming phase starts: I have new information but the ttvn has not
1306 * been incremented yet. This flag will make me check all the incoming
1307 * packets for the correct destination. */
1308 bat_priv->tt_poss_change = true;
1309
1310 orig_node_free_ref(orig_node);
1311out:
1312 /* returning NET_RX_DROP will make the caller function kfree the skb */
1313 return NET_RX_DROP;
1314}
1315
c6c8fea2 1316/* find a suitable router for this originator, and use
a4c135c5
SW
1317 * bonding if possible. increases the found neighbors
1318 * refcount.*/
c6c8fea2
SE
1319struct neigh_node *find_router(struct bat_priv *bat_priv,
1320 struct orig_node *orig_node,
747e4221 1321 const struct hard_iface *recv_if)
c6c8fea2
SE
1322{
1323 struct orig_node *primary_orig_node;
1324 struct orig_node *router_orig;
55158629 1325 struct neigh_node *router;
c6c8fea2
SE
1326 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
1327 int bonding_enabled;
1328
1329 if (!orig_node)
1330 return NULL;
1331
e1a5382f
LL
1332 router = orig_node_get_router(orig_node);
1333 if (!router)
01df2b65 1334 goto err;
c6c8fea2
SE
1335
1336 /* without bonding, the first node should
1337 * always choose the default router. */
c6c8fea2
SE
1338 bonding_enabled = atomic_read(&bat_priv->bonding);
1339
a4c135c5
SW
1340 rcu_read_lock();
1341 /* select default router to output */
e1a5382f 1342 router_orig = router->orig_node;
01df2b65
ML
1343 if (!router_orig)
1344 goto err_unlock;
a4c135c5 1345
a4c135c5
SW
1346 if ((!recv_if) && (!bonding_enabled))
1347 goto return_router;
c6c8fea2
SE
1348
1349 /* if we have something in the primary_addr, we can search
1350 * for a potential bonding candidate. */
39901e71 1351 if (compare_eth(router_orig->primary_addr, zero_mac))
a4c135c5 1352 goto return_router;
c6c8fea2
SE
1353
1354 /* find the orig_node which has the primary interface. might
1355 * even be the same as our router_orig in many cases */
1356
39901e71 1357 if (compare_eth(router_orig->primary_addr, router_orig->orig)) {
c6c8fea2
SE
1358 primary_orig_node = router_orig;
1359 } else {
7aadf889
ML
1360 primary_orig_node = orig_hash_find(bat_priv,
1361 router_orig->primary_addr);
c6c8fea2 1362 if (!primary_orig_node)
a4c135c5 1363 goto return_router;
7aadf889 1364
7b36e8ee 1365 orig_node_free_ref(primary_orig_node);
c6c8fea2
SE
1366 }
1367
1368 /* with less than 2 candidates, we can't do any
1369 * bonding and prefer the original router. */
a4c135c5
SW
1370 if (atomic_read(&primary_orig_node->bond_candidates) < 2)
1371 goto return_router;
c6c8fea2 1372
c6c8fea2
SE
1373 /* all nodes between should choose a candidate which
1374 * is is not on the interface where the packet came
1375 * in. */
a4c135c5 1376
44524fcd 1377 neigh_node_free_ref(router);
c6c8fea2 1378
55158629
LL
1379 if (bonding_enabled)
1380 router = find_bond_router(primary_orig_node, recv_if);
1381 else
1382 router = find_ifalter_router(primary_orig_node, recv_if);
c6c8fea2 1383
a4c135c5 1384return_router:
e2cbc11c
AQ
1385 if (router && router->if_incoming->if_status != IF_ACTIVE)
1386 goto err_unlock;
1387
a4c135c5 1388 rcu_read_unlock();
c6c8fea2 1389 return router;
01df2b65
ML
1390err_unlock:
1391 rcu_read_unlock();
1392err:
1393 if (router)
1394 neigh_node_free_ref(router);
1395 return NULL;
c6c8fea2
SE
1396}
1397
1398static int check_unicast_packet(struct sk_buff *skb, int hdr_size)
1399{
1400 struct ethhdr *ethhdr;
1401
1402 /* drop packet if it has not necessary minimum size */
1403 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1404 return -1;
1405
1406 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1407
1408 /* packet with unicast indication but broadcast recipient */
1409 if (is_broadcast_ether_addr(ethhdr->h_dest))
1410 return -1;
1411
1412 /* packet with broadcast sender address */
1413 if (is_broadcast_ether_addr(ethhdr->h_source))
1414 return -1;
1415
1416 /* not for me */
1417 if (!is_my_mac(ethhdr->h_dest))
1418 return -1;
1419
1420 return 0;
1421}
1422
7cefb149 1423int route_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
c6c8fea2
SE
1424{
1425 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
44524fcd
ML
1426 struct orig_node *orig_node = NULL;
1427 struct neigh_node *neigh_node = NULL;
c6c8fea2
SE
1428 struct unicast_packet *unicast_packet;
1429 struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
44524fcd 1430 int ret = NET_RX_DROP;
c6c8fea2
SE
1431 struct sk_buff *new_skb;
1432
1433 unicast_packet = (struct unicast_packet *)skb->data;
1434
1435 /* TTL exceeded */
1436 if (unicast_packet->ttl < 2) {
1437 pr_debug("Warning - can't forward unicast packet from %pM to "
1438 "%pM: ttl exceeded\n", ethhdr->h_source,
1439 unicast_packet->dest);
44524fcd 1440 goto out;
c6c8fea2
SE
1441 }
1442
1443 /* get routing information */
7aadf889
ML
1444 orig_node = orig_hash_find(bat_priv, unicast_packet->dest);
1445
44524fcd 1446 if (!orig_node)
b5a6f69c 1447 goto out;
c6c8fea2 1448
a4c135c5 1449 /* find_router() increases neigh_nodes refcount if found. */
44524fcd 1450 neigh_node = find_router(bat_priv, orig_node, recv_if);
c6c8fea2 1451
d0072609 1452 if (!neigh_node)
44524fcd 1453 goto out;
c6c8fea2
SE
1454
1455 /* create a copy of the skb, if needed, to modify it. */
1456 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
44524fcd 1457 goto out;
c6c8fea2
SE
1458
1459 unicast_packet = (struct unicast_packet *)skb->data;
1460
1461 if (unicast_packet->packet_type == BAT_UNICAST &&
1462 atomic_read(&bat_priv->fragmentation) &&
d0072609
ML
1463 skb->len > neigh_node->if_incoming->net_dev->mtu) {
1464 ret = frag_send_skb(skb, bat_priv,
1465 neigh_node->if_incoming, neigh_node->addr);
1466 goto out;
1467 }
c6c8fea2
SE
1468
1469 if (unicast_packet->packet_type == BAT_UNICAST_FRAG &&
d0072609 1470 frag_can_reassemble(skb, neigh_node->if_incoming->net_dev->mtu)) {
c6c8fea2
SE
1471
1472 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
1473
1474 if (ret == NET_RX_DROP)
44524fcd 1475 goto out;
c6c8fea2
SE
1476
1477 /* packet was buffered for late merge */
44524fcd
ML
1478 if (!new_skb) {
1479 ret = NET_RX_SUCCESS;
1480 goto out;
1481 }
c6c8fea2
SE
1482
1483 skb = new_skb;
1484 unicast_packet = (struct unicast_packet *)skb->data;
1485 }
1486
1487 /* decrement ttl */
1488 unicast_packet->ttl--;
1489
1490 /* route it */
d0072609 1491 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
44524fcd 1492 ret = NET_RX_SUCCESS;
c6c8fea2 1493
44524fcd
ML
1494out:
1495 if (neigh_node)
1496 neigh_node_free_ref(neigh_node);
1497 if (orig_node)
7b36e8ee 1498 orig_node_free_ref(orig_node);
44524fcd 1499 return ret;
c6c8fea2
SE
1500}
1501
a73105b8
AQ
1502static int check_unicast_ttvn(struct bat_priv *bat_priv,
1503 struct sk_buff *skb) {
1504 uint8_t curr_ttvn;
1505 struct orig_node *orig_node;
1506 struct ethhdr *ethhdr;
1507 struct hard_iface *primary_if;
1508 struct unicast_packet *unicast_packet;
cc47f66e 1509 bool tt_poss_change;
a73105b8
AQ
1510
1511 /* I could need to modify it */
1512 if (skb_cow(skb, sizeof(struct unicast_packet)) < 0)
1513 return 0;
1514
1515 unicast_packet = (struct unicast_packet *)skb->data;
1516
cc47f66e
AQ
1517 if (is_my_mac(unicast_packet->dest)) {
1518 tt_poss_change = bat_priv->tt_poss_change;
a73105b8 1519 curr_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
cc47f66e 1520 } else {
a73105b8
AQ
1521 orig_node = orig_hash_find(bat_priv, unicast_packet->dest);
1522
1523 if (!orig_node)
1524 return 0;
1525
1526 curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
cc47f66e 1527 tt_poss_change = orig_node->tt_poss_change;
a73105b8
AQ
1528 orig_node_free_ref(orig_node);
1529 }
1530
1531 /* Check whether I have to reroute the packet */
cc47f66e 1532 if (seq_before(unicast_packet->ttvn, curr_ttvn) || tt_poss_change) {
a73105b8
AQ
1533 /* Linearize the skb before accessing it */
1534 if (skb_linearize(skb) < 0)
1535 return 0;
1536
1537 ethhdr = (struct ethhdr *)(skb->data +
1538 sizeof(struct unicast_packet));
a73105b8
AQ
1539 orig_node = transtable_search(bat_priv, ethhdr->h_dest);
1540
1541 if (!orig_node) {
1542 if (!is_my_client(bat_priv, ethhdr->h_dest))
1543 return 0;
1544 primary_if = primary_if_get_selected(bat_priv);
1545 if (!primary_if)
1546 return 0;
1547 memcpy(unicast_packet->dest,
1548 primary_if->net_dev->dev_addr, ETH_ALEN);
1549 hardif_free_ref(primary_if);
1550 } else {
1551 memcpy(unicast_packet->dest, orig_node->orig,
1552 ETH_ALEN);
1553 curr_ttvn = (uint8_t)
1554 atomic_read(&orig_node->last_ttvn);
1555 orig_node_free_ref(orig_node);
1556 }
1557
1558 bat_dbg(DBG_ROUTES, bat_priv, "TTVN mismatch (old_ttvn %u "
1559 "new_ttvn %u)! Rerouting unicast packet (for %pM) to "
1560 "%pM\n", unicast_packet->ttvn, curr_ttvn,
1561 ethhdr->h_dest, unicast_packet->dest);
1562
1563 unicast_packet->ttvn = curr_ttvn;
1564 }
1565 return 1;
1566}
1567
e6c10f43 1568int recv_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
c6c8fea2 1569{
a73105b8 1570 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
c6c8fea2 1571 struct unicast_packet *unicast_packet;
704509b8 1572 int hdr_size = sizeof(*unicast_packet);
c6c8fea2
SE
1573
1574 if (check_unicast_packet(skb, hdr_size) < 0)
1575 return NET_RX_DROP;
1576
a73105b8
AQ
1577 if (!check_unicast_ttvn(bat_priv, skb))
1578 return NET_RX_DROP;
1579
c6c8fea2
SE
1580 unicast_packet = (struct unicast_packet *)skb->data;
1581
1582 /* packet for me */
1583 if (is_my_mac(unicast_packet->dest)) {
1584 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
1585 return NET_RX_SUCCESS;
1586 }
1587
7cefb149 1588 return route_unicast_packet(skb, recv_if);
c6c8fea2
SE
1589}
1590
e6c10f43 1591int recv_ucast_frag_packet(struct sk_buff *skb, struct hard_iface *recv_if)
c6c8fea2
SE
1592{
1593 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1594 struct unicast_frag_packet *unicast_packet;
704509b8 1595 int hdr_size = sizeof(*unicast_packet);
c6c8fea2
SE
1596 struct sk_buff *new_skb = NULL;
1597 int ret;
1598
1599 if (check_unicast_packet(skb, hdr_size) < 0)
1600 return NET_RX_DROP;
1601
a73105b8
AQ
1602 if (!check_unicast_ttvn(bat_priv, skb))
1603 return NET_RX_DROP;
1604
c6c8fea2
SE
1605 unicast_packet = (struct unicast_frag_packet *)skb->data;
1606
1607 /* packet for me */
1608 if (is_my_mac(unicast_packet->dest)) {
1609
1610 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
1611
1612 if (ret == NET_RX_DROP)
1613 return NET_RX_DROP;
1614
1615 /* packet was buffered for late merge */
1616 if (!new_skb)
1617 return NET_RX_SUCCESS;
1618
1619 interface_rx(recv_if->soft_iface, new_skb, recv_if,
1620 sizeof(struct unicast_packet));
1621 return NET_RX_SUCCESS;
1622 }
1623
7cefb149 1624 return route_unicast_packet(skb, recv_if);
c6c8fea2
SE
1625}
1626
1627
e6c10f43 1628int recv_bcast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
c6c8fea2
SE
1629{
1630 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
f3e0008f 1631 struct orig_node *orig_node = NULL;
c6c8fea2
SE
1632 struct bcast_packet *bcast_packet;
1633 struct ethhdr *ethhdr;
704509b8 1634 int hdr_size = sizeof(*bcast_packet);
f3e0008f 1635 int ret = NET_RX_DROP;
c6c8fea2
SE
1636 int32_t seq_diff;
1637
1638 /* drop packet if it has not necessary minimum size */
1639 if (unlikely(!pskb_may_pull(skb, hdr_size)))
f3e0008f 1640 goto out;
c6c8fea2
SE
1641
1642 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1643
1644 /* packet with broadcast indication but unicast recipient */
1645 if (!is_broadcast_ether_addr(ethhdr->h_dest))
f3e0008f 1646 goto out;
c6c8fea2
SE
1647
1648 /* packet with broadcast sender address */
1649 if (is_broadcast_ether_addr(ethhdr->h_source))
f3e0008f 1650 goto out;
c6c8fea2
SE
1651
1652 /* ignore broadcasts sent by myself */
1653 if (is_my_mac(ethhdr->h_source))
f3e0008f 1654 goto out;
c6c8fea2
SE
1655
1656 bcast_packet = (struct bcast_packet *)skb->data;
1657
1658 /* ignore broadcasts originated by myself */
1659 if (is_my_mac(bcast_packet->orig))
f3e0008f 1660 goto out;
c6c8fea2
SE
1661
1662 if (bcast_packet->ttl < 2)
f3e0008f 1663 goto out;
c6c8fea2 1664
7aadf889 1665 orig_node = orig_hash_find(bat_priv, bcast_packet->orig);
f3e0008f
ML
1666
1667 if (!orig_node)
b5a6f69c 1668 goto out;
c6c8fea2 1669
f3e0008f 1670 spin_lock_bh(&orig_node->bcast_seqno_lock);
c6c8fea2
SE
1671
1672 /* check whether the packet is a duplicate */
f3e0008f
ML
1673 if (get_bit_status(orig_node->bcast_bits, orig_node->last_bcast_seqno,
1674 ntohl(bcast_packet->seqno)))
1675 goto spin_unlock;
c6c8fea2
SE
1676
1677 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1678
1679 /* check whether the packet is old and the host just restarted. */
1680 if (window_protected(bat_priv, seq_diff,
f3e0008f
ML
1681 &orig_node->bcast_seqno_reset))
1682 goto spin_unlock;
c6c8fea2
SE
1683
1684 /* mark broadcast in flood history, update window position
1685 * if required. */
1686 if (bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
1687 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1688
f3e0008f 1689 spin_unlock_bh(&orig_node->bcast_seqno_lock);
f3e0008f 1690
c6c8fea2 1691 /* rebroadcast packet */
8698529d 1692 add_bcast_packet_to_list(bat_priv, skb, 1);
c6c8fea2
SE
1693
1694 /* broadcast for me */
1695 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
f3e0008f
ML
1696 ret = NET_RX_SUCCESS;
1697 goto out;
c6c8fea2 1698
f3e0008f
ML
1699spin_unlock:
1700 spin_unlock_bh(&orig_node->bcast_seqno_lock);
f3e0008f
ML
1701out:
1702 if (orig_node)
7b36e8ee 1703 orig_node_free_ref(orig_node);
f3e0008f 1704 return ret;
c6c8fea2
SE
1705}
1706
e6c10f43 1707int recv_vis_packet(struct sk_buff *skb, struct hard_iface *recv_if)
c6c8fea2
SE
1708{
1709 struct vis_packet *vis_packet;
1710 struct ethhdr *ethhdr;
1711 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
704509b8 1712 int hdr_size = sizeof(*vis_packet);
c6c8fea2
SE
1713
1714 /* keep skb linear */
1715 if (skb_linearize(skb) < 0)
1716 return NET_RX_DROP;
1717
1718 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1719 return NET_RX_DROP;
1720
1721 vis_packet = (struct vis_packet *)skb->data;
1722 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1723
1724 /* not for me */
1725 if (!is_my_mac(ethhdr->h_dest))
1726 return NET_RX_DROP;
1727
1728 /* ignore own packets */
1729 if (is_my_mac(vis_packet->vis_orig))
1730 return NET_RX_DROP;
1731
1732 if (is_my_mac(vis_packet->sender_orig))
1733 return NET_RX_DROP;
1734
1735 switch (vis_packet->vis_type) {
1736 case VIS_TYPE_SERVER_SYNC:
1737 receive_server_sync_packet(bat_priv, vis_packet,
1738 skb_headlen(skb));
1739 break;
1740
1741 case VIS_TYPE_CLIENT_UPDATE:
1742 receive_client_update_packet(bat_priv, vis_packet,
1743 skb_headlen(skb));
1744 break;
1745
1746 default: /* ignore unknown packet */
1747 break;
1748 }
1749
1750 /* We take a copy of the data in the packet, so we should
1751 always free the skbuf. */
1752 return NET_RX_DROP;
1753}