1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (C) 2006-2017 B.A.T.M.A.N. contributors: 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 16 * along with this program; if not, see <http://www.gnu.org/licenses/>. 17 */ 18 19 #include "bitarray.h" 20 #include "main.h" 21 22 #include <linux/bitmap.h> 23 24 #include "log.h" 25 26 /* shift the packet array by n places. */ 27 static void batadv_bitmap_shift_left(unsigned long *seq_bits, s32 n) 28 { 29 if (n <= 0 || n >= BATADV_TQ_LOCAL_WINDOW_SIZE) 30 return; 31 32 bitmap_shift_left(seq_bits, seq_bits, n, BATADV_TQ_LOCAL_WINDOW_SIZE); 33 } 34 35 /** 36 * batadv_bit_get_packet() - receive and process one packet within the sequence 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 43 * 44 * Return: true if the window was moved (either new or very old), 45 * false if the window was not moved/shifted. 46 */ 47 bool batadv_bit_get_packet(void *priv, unsigned long *seq_bits, 48 s32 seq_num_diff, int set_mark) 49 { 50 struct batadv_priv *bat_priv = priv; 51 52 /* sequence number is slightly older. We already got a sequence number 53 * higher than this one, so we just mark it. 54 */ 55 if (seq_num_diff <= 0 && seq_num_diff > -BATADV_TQ_LOCAL_WINDOW_SIZE) { 56 if (set_mark) 57 batadv_set_bit(seq_bits, -seq_num_diff); 58 return false; 59 } 60 61 /* sequence number is slightly newer, so we shift the window and 62 * set the mark if required 63 */ 64 if (seq_num_diff > 0 && seq_num_diff < BATADV_TQ_LOCAL_WINDOW_SIZE) { 65 batadv_bitmap_shift_left(seq_bits, seq_num_diff); 66 67 if (set_mark) 68 batadv_set_bit(seq_bits, 0); 69 return true; 70 } 71 72 /* sequence number is much newer, probably missed a lot of packets */ 73 if (seq_num_diff >= BATADV_TQ_LOCAL_WINDOW_SIZE && 74 seq_num_diff < BATADV_EXPECTED_SEQNO_RANGE) { 75 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 76 "We missed a lot of packets (%i) !\n", 77 seq_num_diff - 1); 78 bitmap_zero(seq_bits, BATADV_TQ_LOCAL_WINDOW_SIZE); 79 if (set_mark) 80 batadv_set_bit(seq_bits, 0); 81 return true; 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 87 * seqno window is protected. 88 * 89 * seq_num_diff <= -BATADV_TQ_LOCAL_WINDOW_SIZE 90 * or 91 * seq_num_diff >= BATADV_EXPECTED_SEQNO_RANGE 92 */ 93 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 94 "Other host probably restarted!\n"); 95 96 bitmap_zero(seq_bits, BATADV_TQ_LOCAL_WINDOW_SIZE); 97 if (set_mark) 98 batadv_set_bit(seq_bits, 0); 99 100 return true; 101 } 102