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