// SPDX-License-Identifier: GPL-2.01/* Copyright (C) B.A.T.M.A.N. contributors:2*3* Simon Wunderlich, Marek Lindner4*/56#include "bitarray.h"7#include "main.h"89#include <linux/bitmap.h>1011#include "log.h"1213/* shift the packet array by n places. */14static void batadv_bitmap_shift_left(unsigned long *seq_bits, s32 n)15{16if (n <= 0 || n >= BATADV_TQ_LOCAL_WINDOW_SIZE)17return;1819bitmap_shift_left(seq_bits, seq_bits, n, BATADV_TQ_LOCAL_WINDOW_SIZE);20}2122/**23* batadv_bit_get_packet() - receive and process one packet within the sequence24* number window25* @priv: the bat priv with all the mesh interface information26* @seq_bits: pointer to the sequence number receive packet27* @seq_num_diff: difference between the current/received sequence number and28* the last sequence number29* @set_mark: whether this packet should be marked in seq_bits30*31* Return: true if the window was moved (either new or very old),32* false if the window was not moved/shifted.33*/34bool batadv_bit_get_packet(void *priv, unsigned long *seq_bits,35s32 seq_num_diff, int set_mark)36{37struct batadv_priv *bat_priv = priv;3839/* sequence number is slightly older. We already got a sequence number40* higher than this one, so we just mark it.41*/42if (seq_num_diff <= 0 && seq_num_diff > -BATADV_TQ_LOCAL_WINDOW_SIZE) {43if (set_mark)44batadv_set_bit(seq_bits, -seq_num_diff);45return false;46}4748/* sequence number is slightly newer, so we shift the window and49* set the mark if required50*/51if (seq_num_diff > 0 && seq_num_diff < BATADV_TQ_LOCAL_WINDOW_SIZE) {52batadv_bitmap_shift_left(seq_bits, seq_num_diff);5354if (set_mark)55batadv_set_bit(seq_bits, 0);56return true;57}5859/* sequence number is much newer, probably missed a lot of packets */60if (seq_num_diff >= BATADV_TQ_LOCAL_WINDOW_SIZE &&61seq_num_diff < BATADV_EXPECTED_SEQNO_RANGE) {62batadv_dbg(BATADV_DBG_BATMAN, bat_priv,63"We missed a lot of packets (%i) !\n",64seq_num_diff - 1);65bitmap_zero(seq_bits, BATADV_TQ_LOCAL_WINDOW_SIZE);66if (set_mark)67batadv_set_bit(seq_bits, 0);68return true;69}7071/* received a much older packet. The other host either restarted72* or the old packet got delayed somewhere in the network. The73* packet should be dropped without calling this function if the74* seqno window is protected.75*76* seq_num_diff <= -BATADV_TQ_LOCAL_WINDOW_SIZE77* or78* seq_num_diff >= BATADV_EXPECTED_SEQNO_RANGE79*/80batadv_dbg(BATADV_DBG_BATMAN, bat_priv,81"Other host probably restarted!\n");8283bitmap_zero(seq_bits, BATADV_TQ_LOCAL_WINDOW_SIZE);84if (set_mark)85batadv_set_bit(seq_bits, 0);8687return true;88}899091