bitarray.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2006-2018 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. #include "bitarray.h"
  19. #include "main.h"
  20. #include <linux/bitmap.h>
  21. #include "log.h"
  22. /* shift the packet array by n places. */
  23. static void batadv_bitmap_shift_left(unsigned long *seq_bits, s32 n)
  24. {
  25. if (n <= 0 || n >= BATADV_TQ_LOCAL_WINDOW_SIZE)
  26. return;
  27. bitmap_shift_left(seq_bits, seq_bits, n, BATADV_TQ_LOCAL_WINDOW_SIZE);
  28. }
  29. /**
  30. * batadv_bit_get_packet() - receive and process one packet within the sequence
  31. * number window
  32. * @priv: the bat priv with all the soft interface information
  33. * @seq_bits: pointer to the sequence number receive packet
  34. * @seq_num_diff: difference between the current/received sequence number and
  35. * the last sequence number
  36. * @set_mark: whether this packet should be marked in seq_bits
  37. *
  38. * Return: true if the window was moved (either new or very old),
  39. * false if the window was not moved/shifted.
  40. */
  41. bool batadv_bit_get_packet(void *priv, unsigned long *seq_bits,
  42. s32 seq_num_diff, int set_mark)
  43. {
  44. struct batadv_priv *bat_priv = priv;
  45. /* sequence number is slightly older. We already got a sequence number
  46. * higher than this one, so we just mark it.
  47. */
  48. if (seq_num_diff <= 0 && seq_num_diff > -BATADV_TQ_LOCAL_WINDOW_SIZE) {
  49. if (set_mark)
  50. batadv_set_bit(seq_bits, -seq_num_diff);
  51. return false;
  52. }
  53. /* sequence number is slightly newer, so we shift the window and
  54. * set the mark if required
  55. */
  56. if (seq_num_diff > 0 && seq_num_diff < BATADV_TQ_LOCAL_WINDOW_SIZE) {
  57. batadv_bitmap_shift_left(seq_bits, seq_num_diff);
  58. if (set_mark)
  59. batadv_set_bit(seq_bits, 0);
  60. return true;
  61. }
  62. /* sequence number is much newer, probably missed a lot of packets */
  63. if (seq_num_diff >= BATADV_TQ_LOCAL_WINDOW_SIZE &&
  64. seq_num_diff < BATADV_EXPECTED_SEQNO_RANGE) {
  65. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  66. "We missed a lot of packets (%i) !\n",
  67. seq_num_diff - 1);
  68. bitmap_zero(seq_bits, BATADV_TQ_LOCAL_WINDOW_SIZE);
  69. if (set_mark)
  70. batadv_set_bit(seq_bits, 0);
  71. return true;
  72. }
  73. /* received a much older packet. The other host either restarted
  74. * or the old packet got delayed somewhere in the network. The
  75. * packet should be dropped without calling this function if the
  76. * seqno window is protected.
  77. *
  78. * seq_num_diff <= -BATADV_TQ_LOCAL_WINDOW_SIZE
  79. * or
  80. * seq_num_diff >= BATADV_EXPECTED_SEQNO_RANGE
  81. */
  82. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  83. "Other host probably restarted!\n");
  84. bitmap_zero(seq_bits, BATADV_TQ_LOCAL_WINDOW_SIZE);
  85. if (set_mark)
  86. batadv_set_bit(seq_bits, 0);
  87. return true;
  88. }