blob: 7f04a6acf14e5d0b638c3d091f1c1b7566c27b84 [file] [log] [blame]
Sven Eckelmann7db7d9f2017-11-19 15:05:11 +01001// SPDX-License-Identifier: GPL-2.0
Sven Eckelmann7a79d712018-12-31 23:59:59 +01002/* Copyright (C) 2006-2019 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00003 *
4 * Simon Wunderlich, Marek Lindner
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00005 */
6
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00007#include "bitarray.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +02008#include "main.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00009
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020010#include <linux/bitmap.h>
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000011
Sven Eckelmannba412082016-05-15 23:48:31 +020012#include "log.h"
13
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000014/* shift the packet array by n places. */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020015static void batadv_bitmap_shift_left(unsigned long *seq_bits, s32 n)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000016{
Sven Eckelmann42d0b042012-06-03 22:19:17 +020017 if (n <= 0 || n >= BATADV_TQ_LOCAL_WINDOW_SIZE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000018 return;
19
Sven Eckelmann42d0b042012-06-03 22:19:17 +020020 bitmap_shift_left(seq_bits, seq_bits, n, BATADV_TQ_LOCAL_WINDOW_SIZE);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000021}
22
Sven Eckelmann7afcbbe2015-10-31 12:29:29 +010023/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +010024 * batadv_bit_get_packet() - receive and process one packet within the sequence
Sven Eckelmann7afcbbe2015-10-31 12:29:29 +010025 * number window
26 * @priv: the bat priv with all the soft interface information
27 * @seq_bits: pointer to the sequence number receive packet
28 * @seq_num_diff: difference between the current/received sequence number and
29 * the last sequence number
30 * @set_mark: whether this packet should be marked in seq_bits
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000031 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +010032 * Return: true if the window was moved (either new or very old),
33 * false if the window was not moved/shifted.
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000034 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +010035bool batadv_bit_get_packet(void *priv, unsigned long *seq_bits,
36 s32 seq_num_diff, int set_mark)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000037{
Sven Eckelmann56303d32012-06-05 22:31:31 +020038 struct batadv_priv *bat_priv = priv;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000039
40 /* sequence number is slightly older. We already got a sequence number
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +020041 * higher than this one, so we just mark it.
42 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +020043 if (seq_num_diff <= 0 && seq_num_diff > -BATADV_TQ_LOCAL_WINDOW_SIZE) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000044 if (set_mark)
Sven Eckelmann9b4a1152012-05-12 13:48:53 +020045 batadv_set_bit(seq_bits, -seq_num_diff);
Sven Eckelmann4b426b12016-02-22 21:02:39 +010046 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000047 }
48
49 /* sequence number is slightly newer, so we shift the window and
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +020050 * set the mark if required
51 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +020052 if (seq_num_diff > 0 && seq_num_diff < BATADV_TQ_LOCAL_WINDOW_SIZE) {
Sven Eckelmann0f5f93222012-05-12 02:09:25 +020053 batadv_bitmap_shift_left(seq_bits, seq_num_diff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000054
55 if (set_mark)
Sven Eckelmann9b4a1152012-05-12 13:48:53 +020056 batadv_set_bit(seq_bits, 0);
Sven Eckelmann4b426b12016-02-22 21:02:39 +010057 return true;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000058 }
59
60 /* sequence number is much newer, probably missed a lot of packets */
Sven Eckelmann42d0b042012-06-03 22:19:17 +020061 if (seq_num_diff >= BATADV_TQ_LOCAL_WINDOW_SIZE &&
62 seq_num_diff < BATADV_EXPECTED_SEQNO_RANGE) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +020063 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +020064 "We missed a lot of packets (%i) !\n",
65 seq_num_diff - 1);
Sven Eckelmann42d0b042012-06-03 22:19:17 +020066 bitmap_zero(seq_bits, BATADV_TQ_LOCAL_WINDOW_SIZE);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000067 if (set_mark)
Sven Eckelmann9b4a1152012-05-12 13:48:53 +020068 batadv_set_bit(seq_bits, 0);
Sven Eckelmann4b426b12016-02-22 21:02:39 +010069 return true;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000070 }
71
72 /* received a much older packet. The other host either restarted
73 * or the old packet got delayed somewhere in the network. The
74 * packet should be dropped without calling this function if the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +020075 * seqno window is protected.
Sven Eckelmann8e7c15d2012-08-20 10:26:47 +020076 *
77 * seq_num_diff <= -BATADV_TQ_LOCAL_WINDOW_SIZE
78 * or
79 * seq_num_diff >= BATADV_EXPECTED_SEQNO_RANGE
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +020080 */
Sven Eckelmann8e7c15d2012-08-20 10:26:47 +020081 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
82 "Other host probably restarted!\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000083
Sven Eckelmann8e7c15d2012-08-20 10:26:47 +020084 bitmap_zero(seq_bits, BATADV_TQ_LOCAL_WINDOW_SIZE);
85 if (set_mark)
86 batadv_set_bit(seq_bits, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000087
Sven Eckelmann4b426b12016-02-22 21:02:39 +010088 return true;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000089}