Commit | Line | Data |
---|---|---|
7db7d9f3 | 1 | // SPDX-License-Identifier: GPL-2.0 |
7a79d717 | 2 | /* Copyright (C) 2006-2019 B.A.T.M.A.N. contributors: |
c6c8fea2 SE |
3 | * |
4 | * Simon Wunderlich, Marek Lindner | |
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 | |
ebf38fb7 | 16 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
c6c8fea2 SE |
17 | */ |
18 | ||
c6c8fea2 | 19 | #include "bitarray.h" |
1e2c2a4f | 20 | #include "main.h" |
c6c8fea2 | 21 | |
1e2c2a4f | 22 | #include <linux/bitmap.h> |
c6c8fea2 | 23 | |
ba412080 SE |
24 | #include "log.h" |
25 | ||
c6c8fea2 | 26 | /* shift the packet array by n places. */ |
6b5e971a | 27 | static void batadv_bitmap_shift_left(unsigned long *seq_bits, s32 n) |
c6c8fea2 | 28 | { |
42d0b044 | 29 | if (n <= 0 || n >= BATADV_TQ_LOCAL_WINDOW_SIZE) |
c6c8fea2 SE |
30 | return; |
31 | ||
42d0b044 | 32 | bitmap_shift_left(seq_bits, seq_bits, n, BATADV_TQ_LOCAL_WINDOW_SIZE); |
c6c8fea2 SE |
33 | } |
34 | ||
7afcbbef | 35 | /** |
7e9a8c2c | 36 | * batadv_bit_get_packet() - receive and process one packet within the sequence |
7afcbbef SE |
37 | * number window |
38 | * @priv: the bat priv with all the soft interface information | |
39 | * @seq_bits: pointer to the sequence number receive packet | |
40 | * @seq_num_diff: difference between the current/received sequence number and | |
41 | * the last sequence number | |
42 | * @set_mark: whether this packet should be marked in seq_bits | |
c6c8fea2 | 43 | * |
4b426b10 SE |
44 | * Return: true if the window was moved (either new or very old), |
45 | * false if the window was not moved/shifted. | |
c6c8fea2 | 46 | */ |
4b426b10 SE |
47 | bool batadv_bit_get_packet(void *priv, unsigned long *seq_bits, |
48 | s32 seq_num_diff, int set_mark) | |
c6c8fea2 | 49 | { |
56303d34 | 50 | struct batadv_priv *bat_priv = priv; |
c6c8fea2 SE |
51 | |
52 | /* sequence number is slightly older. We already got a sequence number | |
9cfc7bd6 SE |
53 | * higher than this one, so we just mark it. |
54 | */ | |
42d0b044 | 55 | if (seq_num_diff <= 0 && seq_num_diff > -BATADV_TQ_LOCAL_WINDOW_SIZE) { |
c6c8fea2 | 56 | if (set_mark) |
9b4a1159 | 57 | batadv_set_bit(seq_bits, -seq_num_diff); |
4b426b10 | 58 | return false; |
c6c8fea2 SE |
59 | } |
60 | ||
61 | /* sequence number is slightly newer, so we shift the window and | |
9cfc7bd6 SE |
62 | * set the mark if required |
63 | */ | |
42d0b044 | 64 | if (seq_num_diff > 0 && seq_num_diff < BATADV_TQ_LOCAL_WINDOW_SIZE) { |
0f5f9322 | 65 | batadv_bitmap_shift_left(seq_bits, seq_num_diff); |
c6c8fea2 SE |
66 | |
67 | if (set_mark) | |
9b4a1159 | 68 | batadv_set_bit(seq_bits, 0); |
4b426b10 | 69 | return true; |
c6c8fea2 SE |
70 | } |
71 | ||
72 | /* sequence number is much newer, probably missed a lot of packets */ | |
42d0b044 SE |
73 | if (seq_num_diff >= BATADV_TQ_LOCAL_WINDOW_SIZE && |
74 | seq_num_diff < BATADV_EXPECTED_SEQNO_RANGE) { | |
39c75a51 | 75 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
1eda58bf SE |
76 | "We missed a lot of packets (%i) !\n", |
77 | seq_num_diff - 1); | |
42d0b044 | 78 | bitmap_zero(seq_bits, BATADV_TQ_LOCAL_WINDOW_SIZE); |
c6c8fea2 | 79 | if (set_mark) |
9b4a1159 | 80 | batadv_set_bit(seq_bits, 0); |
4b426b10 | 81 | return true; |
c6c8fea2 SE |
82 | } |
83 | ||
84 | /* received a much older packet. The other host either restarted | |
85 | * or the old packet got delayed somewhere in the network. The | |
86 | * packet should be dropped without calling this function if the | |
9cfc7bd6 | 87 | * seqno window is protected. |
8e7c15d6 SE |
88 | * |
89 | * seq_num_diff <= -BATADV_TQ_LOCAL_WINDOW_SIZE | |
90 | * or | |
91 | * seq_num_diff >= BATADV_EXPECTED_SEQNO_RANGE | |
9cfc7bd6 | 92 | */ |
8e7c15d6 SE |
93 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
94 | "Other host probably restarted!\n"); | |
c6c8fea2 | 95 | |
8e7c15d6 SE |
96 | bitmap_zero(seq_bits, BATADV_TQ_LOCAL_WINDOW_SIZE); |
97 | if (set_mark) | |
98 | batadv_set_bit(seq_bits, 0); | |
c6c8fea2 | 99 | |
4b426b10 | 100 | return true; |
c6c8fea2 | 101 | } |