blob: aa11296e6bf8b3d3d2fcce2a87ca633c82725507 [file] [log] [blame]
Sven Eckelmann0046b042016-01-01 00:01:03 +01001/* Copyright (C) 2007-2016 B.A.T.M.A.N. contributors:
Marek Lindnerfc957272011-07-30 12:04:12 +02002 *
3 * Marek Lindner, Simon Wunderlich
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
Antonio Quartulliebf38fb2013-11-03 20:40:48 +010015 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Marek Lindnerfc957272011-07-30 12:04:12 +020016 */
17
Marek Lindner1c280472011-11-28 17:40:17 +080018#include "bat_algo.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020019#include "main.h"
20
21#include <linux/atomic.h>
22#include <linux/bitmap.h>
23#include <linux/bitops.h>
24#include <linux/bug.h>
25#include <linux/byteorder/generic.h>
26#include <linux/cache.h>
27#include <linux/errno.h>
28#include <linux/etherdevice.h>
29#include <linux/fs.h>
30#include <linux/if_ether.h>
31#include <linux/init.h>
32#include <linux/jiffies.h>
33#include <linux/list.h>
Sven Eckelmann77ae32e2016-01-16 10:29:53 +010034#include <linux/kref.h>
Sven Eckelmann64ae74452016-02-22 22:56:34 +010035#include <linux/lockdep.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020036#include <linux/netdevice.h>
37#include <linux/pkt_sched.h>
38#include <linux/printk.h>
39#include <linux/random.h>
40#include <linux/rculist.h>
41#include <linux/rcupdate.h>
42#include <linux/seq_file.h>
43#include <linux/skbuff.h>
44#include <linux/slab.h>
45#include <linux/spinlock.h>
46#include <linux/stddef.h>
47#include <linux/string.h>
48#include <linux/types.h>
49#include <linux/workqueue.h>
50
51#include "bitarray.h"
52#include "hard-interface.h"
53#include "hash.h"
Martin Hundebølld56b1702013-01-25 11:12:39 +010054#include "network-coding.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020055#include "originator.h"
56#include "packet.h"
57#include "routing.h"
58#include "send.h"
59#include "translation-table.h"
Marek Lindnerfc957272011-07-30 12:04:12 +020060
Antonio Quartulli791c2a22013-08-17 12:44:44 +020061/**
Martin Hundebøll95298a92014-07-15 09:41:05 +020062 * enum batadv_dup_status - duplicate status
Markus Pargmann9f52ee12014-12-26 12:41:34 +010063 * @BATADV_NO_DUP: the packet is no duplicate
Antonio Quartulli791c2a22013-08-17 12:44:44 +020064 * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for the
65 * neighbor)
66 * @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor
67 * @BATADV_PROTECTED: originator is currently protected (after reboot)
68 */
69enum batadv_dup_status {
70 BATADV_NO_DUP = 0,
71 BATADV_ORIG_DUP,
72 BATADV_NEIGH_DUP,
73 BATADV_PROTECTED,
74};
75
Antonio Quartulli24a5dee2013-04-08 09:38:12 +020076/**
77 * batadv_ring_buffer_set - update the ring buffer with the given value
78 * @lq_recv: pointer to the ring buffer
79 * @lq_index: index to store the value at
80 * @value: value to store in the ring buffer
81 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020082static void batadv_ring_buffer_set(u8 lq_recv[], u8 *lq_index, u8 value)
Antonio Quartulli24a5dee2013-04-08 09:38:12 +020083{
84 lq_recv[*lq_index] = value;
85 *lq_index = (*lq_index + 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE;
86}
87
88/**
Markus Pargmannd491dbb2014-12-26 12:41:36 +010089 * batadv_ring_buffer_avg - compute the average of all non-zero values stored
Antonio Quartulli24a5dee2013-04-08 09:38:12 +020090 * in the given ring buffer
91 * @lq_recv: pointer to the ring buffer
92 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +020093 * Return: computed average value.
Antonio Quartulli24a5dee2013-04-08 09:38:12 +020094 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020095static u8 batadv_ring_buffer_avg(const u8 lq_recv[])
Antonio Quartulli24a5dee2013-04-08 09:38:12 +020096{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020097 const u8 *ptr;
98 u16 count = 0;
99 u16 i = 0;
100 u16 sum = 0;
Antonio Quartulli24a5dee2013-04-08 09:38:12 +0200101
102 ptr = lq_recv;
103
104 while (i < BATADV_TQ_GLOBAL_WINDOW_SIZE) {
105 if (*ptr != 0) {
106 count++;
107 sum += *ptr;
108 }
109
110 i++;
111 ptr++;
112 }
113
114 if (count == 0)
115 return 0;
116
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200117 return (u8)(sum / count);
Antonio Quartulli24a5dee2013-04-08 09:38:12 +0200118}
David S. Millerd98cae64e2013-06-19 16:49:39 -0700119
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200120/**
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200121 * batadv_iv_ogm_orig_free - free the private resources allocated for this
122 * orig_node
123 * @orig_node: the orig_node for which the resources have to be free'd
124 */
125static void batadv_iv_ogm_orig_free(struct batadv_orig_node *orig_node)
126{
127 kfree(orig_node->bat_iv.bcast_own);
128 kfree(orig_node->bat_iv.bcast_own_sum);
129}
130
131/**
132 * batadv_iv_ogm_orig_add_if - change the private structures of the orig_node to
133 * include the new hard-interface
134 * @orig_node: the orig_node that has to be changed
135 * @max_if_num: the current amount of interfaces
136 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200137 * Return: 0 on success, a negative error code otherwise.
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200138 */
139static int batadv_iv_ogm_orig_add_if(struct batadv_orig_node *orig_node,
140 int max_if_num)
141{
142 void *data_ptr;
Antonio Quartulli0185dda2014-05-24 06:43:47 +0200143 size_t old_size;
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200144 int ret = -ENOMEM;
145
146 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
147
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200148 old_size = (max_if_num - 1) * sizeof(unsigned long) * BATADV_NUM_WORDS;
Antonio Quartulli0185dda2014-05-24 06:43:47 +0200149 data_ptr = kmalloc_array(max_if_num,
150 BATADV_NUM_WORDS * sizeof(unsigned long),
151 GFP_ATOMIC);
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200152 if (!data_ptr)
153 goto unlock;
154
155 memcpy(data_ptr, orig_node->bat_iv.bcast_own, old_size);
156 kfree(orig_node->bat_iv.bcast_own);
157 orig_node->bat_iv.bcast_own = data_ptr;
158
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200159 data_ptr = kmalloc_array(max_if_num, sizeof(u8), GFP_ATOMIC);
Sven Eckelmannf7dcdf52016-02-22 22:56:33 +0100160 if (!data_ptr)
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200161 goto unlock;
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200162
163 memcpy(data_ptr, orig_node->bat_iv.bcast_own_sum,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200164 (max_if_num - 1) * sizeof(u8));
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200165 kfree(orig_node->bat_iv.bcast_own_sum);
166 orig_node->bat_iv.bcast_own_sum = data_ptr;
167
168 ret = 0;
169
170unlock:
171 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
172
173 return ret;
174}
175
176/**
Sven Eckelmann64ae74452016-02-22 22:56:34 +0100177 * batadv_iv_ogm_drop_bcast_own_entry - drop section of bcast_own
178 * @orig_node: the orig_node that has to be changed
179 * @max_if_num: the current amount of interfaces
180 * @del_if_num: the index of the interface being removed
181 */
182static void
183batadv_iv_ogm_drop_bcast_own_entry(struct batadv_orig_node *orig_node,
184 int max_if_num, int del_if_num)
185{
186 size_t chunk_size;
187 size_t if_offset;
188 void *data_ptr;
189
190 lockdep_assert_held(&orig_node->bat_iv.ogm_cnt_lock);
191
192 chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS;
193 data_ptr = kmalloc_array(max_if_num, chunk_size, GFP_ATOMIC);
194 if (!data_ptr)
195 /* use old buffer when new one could not be allocated */
196 data_ptr = orig_node->bat_iv.bcast_own;
197
198 /* copy first part */
199 memmove(data_ptr, orig_node->bat_iv.bcast_own, del_if_num * chunk_size);
200
201 /* copy second part */
202 if_offset = (del_if_num + 1) * chunk_size;
203 memmove((char *)data_ptr + del_if_num * chunk_size,
204 (uint8_t *)orig_node->bat_iv.bcast_own + if_offset,
205 (max_if_num - del_if_num) * chunk_size);
206
207 /* bcast_own was shrunk down in new buffer; free old one */
208 if (orig_node->bat_iv.bcast_own != data_ptr) {
209 kfree(orig_node->bat_iv.bcast_own);
210 orig_node->bat_iv.bcast_own = data_ptr;
211 }
212}
213
214/**
215 * batadv_iv_ogm_drop_bcast_own_sum_entry - drop section of bcast_own_sum
216 * @orig_node: the orig_node that has to be changed
217 * @max_if_num: the current amount of interfaces
218 * @del_if_num: the index of the interface being removed
219 */
220static void
221batadv_iv_ogm_drop_bcast_own_sum_entry(struct batadv_orig_node *orig_node,
222 int max_if_num, int del_if_num)
223{
224 size_t if_offset;
225 void *data_ptr;
226
227 lockdep_assert_held(&orig_node->bat_iv.ogm_cnt_lock);
228
229 data_ptr = kmalloc_array(max_if_num, sizeof(u8), GFP_ATOMIC);
230 if (!data_ptr)
231 /* use old buffer when new one could not be allocated */
232 data_ptr = orig_node->bat_iv.bcast_own_sum;
233
234 memmove(data_ptr, orig_node->bat_iv.bcast_own_sum,
235 del_if_num * sizeof(u8));
236
237 if_offset = (del_if_num + 1) * sizeof(u8);
238 memmove((char *)data_ptr + del_if_num * sizeof(u8),
239 orig_node->bat_iv.bcast_own_sum + if_offset,
240 (max_if_num - del_if_num) * sizeof(u8));
241
242 /* bcast_own_sum was shrunk down in new buffer; free old one */
243 if (orig_node->bat_iv.bcast_own_sum != data_ptr) {
244 kfree(orig_node->bat_iv.bcast_own_sum);
245 orig_node->bat_iv.bcast_own_sum = data_ptr;
246 }
247}
248
249/**
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200250 * batadv_iv_ogm_orig_del_if - change the private structures of the orig_node to
251 * exclude the removed interface
252 * @orig_node: the orig_node that has to be changed
253 * @max_if_num: the current amount of interfaces
254 * @del_if_num: the index of the interface being removed
255 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200256 * Return: 0 on success, a negative error code otherwise.
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200257 */
258static int batadv_iv_ogm_orig_del_if(struct batadv_orig_node *orig_node,
259 int max_if_num, int del_if_num)
260{
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200261 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
262
Sven Eckelmann64ae74452016-02-22 22:56:34 +0100263 if (max_if_num == 0) {
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200264 kfree(orig_node->bat_iv.bcast_own);
Sven Eckelmann64ae74452016-02-22 22:56:34 +0100265 kfree(orig_node->bat_iv.bcast_own_sum);
266 orig_node->bat_iv.bcast_own = NULL;
267 orig_node->bat_iv.bcast_own_sum = NULL;
268 } else {
269 batadv_iv_ogm_drop_bcast_own_entry(orig_node, max_if_num,
270 del_if_num);
271 batadv_iv_ogm_drop_bcast_own_sum_entry(orig_node, max_if_num,
272 del_if_num);
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200273 }
274
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200275 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
276
Sven Eckelmann64ae74452016-02-22 22:56:34 +0100277 return 0;
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200278}
279
280/**
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200281 * batadv_iv_ogm_orig_get - retrieve or create (if does not exist) an originator
282 * @bat_priv: the bat priv with all the soft interface information
283 * @addr: mac address of the originator
284 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200285 * Return: the originator object corresponding to the passed mac address or NULL
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200286 * on failure.
287 * If the object does not exists it is created an initialised.
288 */
289static struct batadv_orig_node *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200290batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const u8 *addr)
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200291{
292 struct batadv_orig_node *orig_node;
293 int size, hash_added;
294
295 orig_node = batadv_orig_hash_find(bat_priv, addr);
296 if (orig_node)
297 return orig_node;
298
299 orig_node = batadv_orig_node_new(bat_priv, addr);
300 if (!orig_node)
301 return NULL;
302
303 spin_lock_init(&orig_node->bat_iv.ogm_cnt_lock);
304
305 size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS;
306 orig_node->bat_iv.bcast_own = kzalloc(size, GFP_ATOMIC);
307 if (!orig_node->bat_iv.bcast_own)
308 goto free_orig_node;
309
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200310 size = bat_priv->num_ifaces * sizeof(u8);
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200311 orig_node->bat_iv.bcast_own_sum = kzalloc(size, GFP_ATOMIC);
312 if (!orig_node->bat_iv.bcast_own_sum)
Antonio Quartullia5a5cb82014-02-15 02:17:20 +0100313 goto free_orig_node;
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200314
315 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
316 batadv_choose_orig, orig_node,
317 &orig_node->hash_entry);
318 if (hash_added != 0)
Antonio Quartullia5a5cb82014-02-15 02:17:20 +0100319 goto free_orig_node;
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200320
321 return orig_node;
322
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200323free_orig_node:
Simon Wunderlichb2262df2014-02-08 16:45:06 +0100324 /* free twice, as batadv_orig_node_new sets refcount to 2 */
Sven Eckelmann5d967312016-01-17 11:01:09 +0100325 batadv_orig_node_put(orig_node);
326 batadv_orig_node_put(orig_node);
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200327
328 return NULL;
329}
330
Sven Eckelmann56303d32012-06-05 22:31:31 +0200331static struct batadv_neigh_node *
332batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200333 const u8 *neigh_addr,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200334 struct batadv_orig_node *orig_node,
Antonio Quartulli863dd7a2013-03-25 13:49:46 +0100335 struct batadv_orig_node *orig_neigh)
Marek Lindner7ae8b282012-03-01 15:35:21 +0800336{
Marek Lindner741aa062015-07-26 04:57:43 +0800337 struct batadv_neigh_node *neigh_node;
Marek Lindner7ae8b282012-03-01 15:35:21 +0800338
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800339 neigh_node = batadv_neigh_node_get_or_create(orig_node,
340 hard_iface, neigh_addr);
Marek Lindner7ae8b282012-03-01 15:35:21 +0800341 if (!neigh_node)
342 goto out;
343
Simon Wunderlich89652332013-11-13 19:14:46 +0100344 neigh_node->orig_node = orig_neigh;
Marek Lindner7ae8b282012-03-01 15:35:21 +0800345
Marek Lindner7ae8b282012-03-01 15:35:21 +0800346out:
347 return neigh_node;
348}
349
Sven Eckelmann56303d32012-06-05 22:31:31 +0200350static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200351{
Sven Eckelmann96412692012-06-05 22:31:30 +0200352 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindner14511512012-08-02 17:20:26 +0200353 unsigned char *ogm_buff;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200354 u32 random_seqno;
Marek Lindnerd7d32ec2012-02-07 17:20:46 +0800355
356 /* randomize initial seqno to avoid collision */
357 get_random_bytes(&random_seqno, sizeof(random_seqno));
Marek Lindner14511512012-08-02 17:20:26 +0200358 atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno);
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200359
Marek Lindner14511512012-08-02 17:20:26 +0200360 hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN;
361 ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC);
362 if (!ogm_buff)
Markus Pargmann42d9f2c2014-12-26 12:41:26 +0100363 return -ENOMEM;
Marek Lindner77af7572012-02-07 17:20:48 +0800364
Marek Lindner14511512012-08-02 17:20:26 +0200365 hard_iface->bat_iv.ogm_buff = ogm_buff;
366
367 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100368 batadv_ogm_packet->packet_type = BATADV_IV_OGM;
369 batadv_ogm_packet->version = BATADV_COMPAT_VERSION;
370 batadv_ogm_packet->ttl = 2;
Sven Eckelmann96412692012-06-05 22:31:30 +0200371 batadv_ogm_packet->flags = BATADV_NO_FLAGS;
Marek Lindner414254e2013-04-23 21:39:58 +0800372 batadv_ogm_packet->reserved = 0;
Sven Eckelmann96412692012-06-05 22:31:30 +0200373 batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE;
Marek Lindner77af7572012-02-07 17:20:48 +0800374
Markus Pargmann42d9f2c2014-12-26 12:41:26 +0100375 return 0;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200376}
377
Sven Eckelmann56303d32012-06-05 22:31:31 +0200378static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
Marek Lindner00a50072012-02-07 17:20:47 +0800379{
Marek Lindner14511512012-08-02 17:20:26 +0200380 kfree(hard_iface->bat_iv.ogm_buff);
381 hard_iface->bat_iv.ogm_buff = NULL;
Marek Lindner00a50072012-02-07 17:20:47 +0800382}
383
Sven Eckelmann56303d32012-06-05 22:31:31 +0200384static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface)
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200385{
Sven Eckelmann96412692012-06-05 22:31:30 +0200386 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindner14511512012-08-02 17:20:26 +0200387 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200388
Marek Lindner14511512012-08-02 17:20:26 +0200389 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100390 ether_addr_copy(batadv_ogm_packet->orig,
391 hard_iface->net_dev->dev_addr);
392 ether_addr_copy(batadv_ogm_packet->prev_sender,
393 hard_iface->net_dev->dev_addr);
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200394}
395
Sven Eckelmann56303d32012-06-05 22:31:31 +0200396static void
397batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface)
Marek Lindnerc3229392012-03-11 06:17:50 +0800398{
Sven Eckelmann96412692012-06-05 22:31:30 +0200399 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindner14511512012-08-02 17:20:26 +0200400 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
Marek Lindnerc3229392012-03-11 06:17:50 +0800401
Marek Lindner14511512012-08-02 17:20:26 +0200402 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100403 batadv_ogm_packet->ttl = BATADV_TTL;
Marek Lindnerc3229392012-03-11 06:17:50 +0800404}
405
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200406/* when do we schedule our own ogm to be sent */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200407static unsigned long
Sven Eckelmann56303d32012-06-05 22:31:31 +0200408batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200409{
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200410 unsigned int msecs;
411
412 msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
Akinobu Mitae76e4322012-12-24 11:14:07 +0900413 msecs += prandom_u32() % (2 * BATADV_JITTER);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200414
415 return jiffies + msecs_to_jiffies(msecs);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200416}
417
418/* when do we schedule a ogm packet to be sent */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200419static unsigned long batadv_iv_ogm_fwd_send_time(void)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200420{
Akinobu Mitae76e4322012-12-24 11:14:07 +0900421 return jiffies + msecs_to_jiffies(prandom_u32() % (BATADV_JITTER / 2));
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200422}
423
424/* apply hop penalty for a normal link */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200425static u8 batadv_hop_penalty(u8 tq, const struct batadv_priv *bat_priv)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200426{
427 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200428 int new_tq;
429
430 new_tq = tq * (BATADV_TQ_MAX_VALUE - hop_penalty);
431 new_tq /= BATADV_TQ_MAX_VALUE;
432
433 return new_tq;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200434}
435
Simon Wunderlich600184b2015-11-23 19:57:21 +0100436/**
437 * batadv_iv_ogm_aggr_packet - checks if there is another OGM attached
438 * @buff_pos: current position in the skb
439 * @packet_len: total length of the skb
440 * @tvlv_len: tvlv length of the previously considered OGM
441 *
442 * Return: true if there is enough space for another OGM, false otherwise.
443 */
Markus Pargmann9fd9b192014-12-26 12:41:27 +0100444static bool batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len,
445 __be16 tvlv_len)
Marek Lindnerfc957272011-07-30 12:04:12 +0200446{
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200447 int next_buff_pos = 0;
448
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200449 next_buff_pos += buff_pos + BATADV_OGM_HLEN;
Marek Lindneref261572013-04-23 21:39:57 +0800450 next_buff_pos += ntohs(tvlv_len);
Marek Lindnerfc957272011-07-30 12:04:12 +0200451
452 return (next_buff_pos <= packet_len) &&
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200453 (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
Marek Lindnerfc957272011-07-30 12:04:12 +0200454}
455
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200456/* send a batman ogm to a given interface */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200457static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet,
458 struct batadv_hard_iface *hard_iface)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200459{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200460 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Markus Pargmannde12bae2014-12-26 12:41:28 +0100461 const char *fwd_str;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200462 u8 packet_num;
463 s16 buff_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +0200464 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200465 struct sk_buff *skb;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200466 u8 *packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200467
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200468 if (hard_iface->if_status != BATADV_IF_ACTIVE)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200469 return;
470
471 packet_num = 0;
472 buff_pos = 0;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200473 packet_pos = forw_packet->skb->data;
474 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200475
476 /* adjust all flags and log packets */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200477 while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
Marek Lindneref261572013-04-23 21:39:57 +0800478 batadv_ogm_packet->tvlv_len)) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200479 /* we might have aggregated direct link packets with an
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200480 * ordinary base packet
481 */
Sven Eckelmann8de47de2012-07-08 16:32:09 +0200482 if (forw_packet->direct_link_flags & BIT(packet_num) &&
483 forw_packet->if_incoming == hard_iface)
Sven Eckelmann96412692012-06-05 22:31:30 +0200484 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200485 else
Sven Eckelmann96412692012-06-05 22:31:30 +0200486 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200487
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200488 if (packet_num > 0 || !forw_packet->own)
489 fwd_str = "Forwarding";
490 else
491 fwd_str = "Sending own";
492
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200493 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800494 "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200495 fwd_str, (packet_num > 0 ? "aggregated " : ""),
Sven Eckelmann96412692012-06-05 22:31:30 +0200496 batadv_ogm_packet->orig,
497 ntohl(batadv_ogm_packet->seqno),
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100498 batadv_ogm_packet->tq, batadv_ogm_packet->ttl,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +0200499 ((batadv_ogm_packet->flags & BATADV_DIRECTLINK) ?
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200500 "on" : "off"),
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800501 hard_iface->net_dev->name,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200502 hard_iface->net_dev->dev_addr);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200503
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200504 buff_pos += BATADV_OGM_HLEN;
Marek Lindneref261572013-04-23 21:39:57 +0800505 buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200506 packet_num++;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200507 packet_pos = forw_packet->skb->data + buff_pos;
508 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200509 }
510
511 /* create clone because function is called more than once */
512 skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
Martin Hundebøllf8214862012-04-20 17:02:45 +0200513 if (skb) {
Sven Eckelmannd69909d2012-06-03 22:19:20 +0200514 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
515 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
Martin Hundebøllf8214862012-04-20 17:02:45 +0200516 skb->len + ETH_HLEN);
Antonio Quartulli95d39272016-01-16 16:40:15 +0800517 batadv_send_broadcast_skb(skb, hard_iface);
Martin Hundebøllf8214862012-04-20 17:02:45 +0200518 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200519}
520
521/* send a batman ogm packet */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200522static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200523{
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200524 struct net_device *soft_iface;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200525 struct batadv_priv *bat_priv;
526 struct batadv_hard_iface *primary_if = NULL;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200527
528 if (!forw_packet->if_incoming) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100529 pr_err("Error - can't forward packet: incoming iface not specified\n");
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200530 goto out;
531 }
532
533 soft_iface = forw_packet->if_incoming->soft_iface;
534 bat_priv = netdev_priv(soft_iface);
535
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100536 if (WARN_ON(!forw_packet->if_outgoing))
537 goto out;
538
539 if (WARN_ON(forw_packet->if_outgoing->soft_iface != soft_iface))
540 goto out;
541
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200542 if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200543 goto out;
544
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200545 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200546 if (!primary_if)
547 goto out;
548
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100549 /* only for one specific outgoing interface */
550 batadv_iv_ogm_send_to_if(forw_packet, forw_packet->if_outgoing);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200551
552out:
553 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100554 batadv_hardif_put(primary_if);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200555}
556
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100557/**
558 * batadv_iv_ogm_can_aggregate - find out if an OGM can be aggregated on an
559 * existing forward packet
560 * @new_bat_ogm_packet: OGM packet to be aggregated
561 * @bat_priv: the bat priv with all the soft interface information
562 * @packet_len: (total) length of the OGM
563 * @send_time: timestamp (jiffies) when the packet is to be sent
Martin Hundebøll95298a92014-07-15 09:41:05 +0200564 * @directlink: true if this is a direct link packet
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100565 * @if_incoming: interface where the packet was received
566 * @if_outgoing: interface for which the retransmission should be considered
567 * @forw_packet: the forwarded packet which should be checked
568 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200569 * Return: true if new_packet can be aggregated with forw_packet
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100570 */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200571static bool
Sven Eckelmann96412692012-06-05 22:31:30 +0200572batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200573 struct batadv_priv *bat_priv,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200574 int packet_len, unsigned long send_time,
575 bool directlink,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200576 const struct batadv_hard_iface *if_incoming,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100577 const struct batadv_hard_iface *if_outgoing,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200578 const struct batadv_forw_packet *forw_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200579{
Sven Eckelmann96412692012-06-05 22:31:30 +0200580 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200581 int aggregated_bytes = forw_packet->packet_len + packet_len;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200582 struct batadv_hard_iface *primary_if = NULL;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200583 bool res = false;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200584 unsigned long aggregation_end_time;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200585
Sven Eckelmann96412692012-06-05 22:31:30 +0200586 batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200587 aggregation_end_time = send_time;
588 aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200589
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200590 /* we can aggregate the current packet to this aggregated packet
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200591 * if:
592 *
593 * - the send time is within our MAX_AGGREGATION_MS time
594 * - the resulting packet wont be bigger than
595 * MAX_AGGREGATION_BYTES
Markus Pargmann8f34b382014-12-26 12:41:29 +0100596 * otherwise aggregation is not possible
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200597 */
Markus Pargmann8f34b382014-12-26 12:41:29 +0100598 if (!time_before(send_time, forw_packet->send_time) ||
599 !time_after_eq(aggregation_end_time, forw_packet->send_time))
600 return false;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200601
Markus Pargmann8f34b382014-12-26 12:41:29 +0100602 if (aggregated_bytes > BATADV_MAX_AGGREGATION_BYTES)
603 return false;
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100604
Markus Pargmann8f34b382014-12-26 12:41:29 +0100605 /* packet is not leaving on the same interface. */
606 if (forw_packet->if_outgoing != if_outgoing)
607 return false;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200608
Markus Pargmann8f34b382014-12-26 12:41:29 +0100609 /* check aggregation compatibility
610 * -> direct link packets are broadcasted on
611 * their interface only
612 * -> aggregate packet if the current packet is
613 * a "global" packet as well as the base
614 * packet
615 */
616 primary_if = batadv_primary_if_get_selected(bat_priv);
617 if (!primary_if)
618 return false;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200619
Markus Pargmann8f34b382014-12-26 12:41:29 +0100620 /* packets without direct link flag and high TTL
621 * are flooded through the net
622 */
623 if (!directlink &&
624 !(batadv_ogm_packet->flags & BATADV_DIRECTLINK) &&
625 batadv_ogm_packet->ttl != 1 &&
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200626
Markus Pargmann8f34b382014-12-26 12:41:29 +0100627 /* own packets originating non-primary
628 * interfaces leave only that interface
629 */
630 (!forw_packet->own ||
631 forw_packet->if_incoming == primary_if)) {
632 res = true;
633 goto out;
634 }
635
636 /* if the incoming packet is sent via this one
637 * interface only - we still can aggregate
638 */
639 if (directlink &&
640 new_bat_ogm_packet->ttl == 1 &&
641 forw_packet->if_incoming == if_incoming &&
642
643 /* packets from direct neighbors or
644 * own secondary interface packets
645 * (= secondary interface packets in general)
646 */
647 (batadv_ogm_packet->flags & BATADV_DIRECTLINK ||
648 (forw_packet->own &&
649 forw_packet->if_incoming != primary_if))) {
650 res = true;
651 goto out;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200652 }
653
654out:
655 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100656 batadv_hardif_put(primary_if);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200657 return res;
658}
659
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100660/**
661 * batadv_iv_ogm_aggregate_new - create a new aggregated packet and add this
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100662 * packet to it.
663 * @packet_buff: pointer to the OGM
664 * @packet_len: (total) length of the OGM
665 * @send_time: timestamp (jiffies) when the packet is to be sent
666 * @direct_link: whether this OGM has direct link status
667 * @if_incoming: interface where the packet was received
668 * @if_outgoing: interface for which the retransmission should be considered
669 * @own_packet: true if it is a self-generated ogm
670 */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200671static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,
672 int packet_len, unsigned long send_time,
673 bool direct_link,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200674 struct batadv_hard_iface *if_incoming,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100675 struct batadv_hard_iface *if_outgoing,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200676 int own_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200677{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200678 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
679 struct batadv_forw_packet *forw_packet_aggr;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200680 unsigned char *skb_buff;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200681 unsigned int skb_size;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200682
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200683 /* own packet should always be scheduled */
684 if (!own_packet) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200685 if (!batadv_atomic_dec_not_zero(&bat_priv->batman_queue_left)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200686 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200687 "batman packet queue full\n");
Sven Eckelmann17a86912016-04-11 13:06:40 +0200688 return;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200689 }
690 }
691
692 forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC);
Markus Pargmann940d1562014-12-26 12:41:31 +0100693 if (!forw_packet_aggr)
694 goto out_nomem;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200695
Markus Pargmann940d1562014-12-26 12:41:31 +0100696 if (atomic_read(&bat_priv->aggregated_ogms) &&
697 packet_len < BATADV_MAX_AGGREGATION_BYTES)
Sven Eckelmann5b246572012-11-04 17:11:45 +0100698 skb_size = BATADV_MAX_AGGREGATION_BYTES;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200699 else
Sven Eckelmann5b246572012-11-04 17:11:45 +0100700 skb_size = packet_len;
701
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200702 skb_size += ETH_HLEN;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200703
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200704 forw_packet_aggr->skb = netdev_alloc_skb_ip_align(NULL, skb_size);
Markus Pargmann940d1562014-12-26 12:41:31 +0100705 if (!forw_packet_aggr->skb)
706 goto out_free_forw_packet;
Simon Wunderlichc54f38c2013-07-29 17:56:44 +0200707 forw_packet_aggr->skb->priority = TC_PRIO_CONTROL;
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200708 skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200709
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200710 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
711 forw_packet_aggr->packet_len = packet_len;
712 memcpy(skb_buff, packet_buff, packet_len);
713
Sven Eckelmann17a86912016-04-11 13:06:40 +0200714 kref_get(&if_incoming->refcount);
715 kref_get(&if_outgoing->refcount);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200716 forw_packet_aggr->own = own_packet;
717 forw_packet_aggr->if_incoming = if_incoming;
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100718 forw_packet_aggr->if_outgoing = if_outgoing;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200719 forw_packet_aggr->num_packets = 0;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200720 forw_packet_aggr->direct_link_flags = BATADV_NO_FLAGS;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200721 forw_packet_aggr->send_time = send_time;
722
723 /* save packet direct link flag status */
724 if (direct_link)
725 forw_packet_aggr->direct_link_flags |= 1;
726
727 /* add new packet to packet list */
728 spin_lock_bh(&bat_priv->forw_bat_list_lock);
729 hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list);
730 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
731
732 /* start timer for this packet */
733 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
Sven Eckelmann9455e342012-05-12 02:09:37 +0200734 batadv_send_outstanding_bat_ogm_packet);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200735 queue_delayed_work(batadv_event_workqueue,
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200736 &forw_packet_aggr->delayed_work,
737 send_time - jiffies);
738
739 return;
Markus Pargmann940d1562014-12-26 12:41:31 +0100740out_free_forw_packet:
741 kfree(forw_packet_aggr);
742out_nomem:
743 if (!own_packet)
744 atomic_inc(&bat_priv->batman_queue_left);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200745}
746
747/* aggregate a new packet into the existing ogm packet */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200748static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200749 const unsigned char *packet_buff,
750 int packet_len, bool direct_link)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200751{
752 unsigned char *skb_buff;
Sven Eckelmann8de47de2012-07-08 16:32:09 +0200753 unsigned long new_direct_link_flag;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200754
755 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
756 memcpy(skb_buff, packet_buff, packet_len);
757 forw_packet_aggr->packet_len += packet_len;
758 forw_packet_aggr->num_packets++;
759
760 /* save packet direct link flag status */
Sven Eckelmann8de47de2012-07-08 16:32:09 +0200761 if (direct_link) {
762 new_direct_link_flag = BIT(forw_packet_aggr->num_packets);
763 forw_packet_aggr->direct_link_flags |= new_direct_link_flag;
764 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200765}
766
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100767/**
768 * batadv_iv_ogm_queue_add - queue up an OGM for transmission
769 * @bat_priv: the bat priv with all the soft interface information
770 * @packet_buff: pointer to the OGM
771 * @packet_len: (total) length of the OGM
772 * @if_incoming: interface where the packet was received
773 * @if_outgoing: interface for which the retransmission should be considered
774 * @own_packet: true if it is a self-generated ogm
775 * @send_time: timestamp (jiffies) when the packet is to be sent
776 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200777static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200778 unsigned char *packet_buff,
779 int packet_len,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200780 struct batadv_hard_iface *if_incoming,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100781 struct batadv_hard_iface *if_outgoing,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200782 int own_packet, unsigned long send_time)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200783{
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200784 /* _aggr -> pointer to the packet we want to aggregate with
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200785 * _pos -> pointer to the position in the queue
786 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200787 struct batadv_forw_packet *forw_packet_aggr = NULL;
788 struct batadv_forw_packet *forw_packet_pos = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200789 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200790 bool direct_link;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200791 unsigned long max_aggregation_jiffies;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200792
Sven Eckelmann96412692012-06-05 22:31:30 +0200793 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
Markus Pargmann56489152014-12-26 12:41:32 +0100794 direct_link = !!(batadv_ogm_packet->flags & BATADV_DIRECTLINK);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200795 max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200796
797 /* find position for the packet in the forward queue */
798 spin_lock_bh(&bat_priv->forw_bat_list_lock);
799 /* own packets are not to be aggregated */
Markus Pargmann56489152014-12-26 12:41:32 +0100800 if (atomic_read(&bat_priv->aggregated_ogms) && !own_packet) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800801 hlist_for_each_entry(forw_packet_pos,
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200802 &bat_priv->forw_bat_list, list) {
Sven Eckelmann96412692012-06-05 22:31:30 +0200803 if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200804 bat_priv, packet_len,
805 send_time, direct_link,
806 if_incoming,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100807 if_outgoing,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200808 forw_packet_pos)) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200809 forw_packet_aggr = forw_packet_pos;
810 break;
811 }
812 }
813 }
814
815 /* nothing to aggregate with - either aggregation disabled or no
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200816 * suitable aggregation packet found
817 */
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200818 if (!forw_packet_aggr) {
819 /* the following section can run without the lock */
820 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
821
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200822 /* if we could not aggregate this packet with one of the others
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200823 * we hold it back for a while, so that it might be aggregated
824 * later on
825 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200826 if (!own_packet && atomic_read(&bat_priv->aggregated_ogms))
827 send_time += max_aggregation_jiffies;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200828
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200829 batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
830 send_time, direct_link,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100831 if_incoming, if_outgoing,
832 own_packet);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200833 } else {
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200834 batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
835 packet_len, direct_link);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200836 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
837 }
838}
839
Sven Eckelmann56303d32012-06-05 22:31:31 +0200840static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200841 const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +0200842 struct batadv_ogm_packet *batadv_ogm_packet,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200843 bool is_single_hop_neigh,
844 bool is_from_best_next_hop,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100845 struct batadv_hard_iface *if_incoming,
846 struct batadv_hard_iface *if_outgoing)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200847{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200848 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200849 u16 tvlv_len;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200850
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100851 if (batadv_ogm_packet->ttl <= 1) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200852 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200853 return;
854 }
855
Marek Lindner13b25412012-03-11 06:17:53 +0800856 if (!is_from_best_next_hop) {
857 /* Mark the forwarded packet when it is not coming from our
858 * best next hop. We still need to forward the packet for our
859 * neighbor link quality detection to work in case the packet
860 * originated from a single hop neighbor. Otherwise we can
861 * simply drop the ogm.
862 */
863 if (is_single_hop_neigh)
Sven Eckelmann96412692012-06-05 22:31:30 +0200864 batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP;
Marek Lindner13b25412012-03-11 06:17:53 +0800865 else
866 return;
867 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200868
Marek Lindneref261572013-04-23 21:39:57 +0800869 tvlv_len = ntohs(batadv_ogm_packet->tvlv_len);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200870
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100871 batadv_ogm_packet->ttl--;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100872 ether_addr_copy(batadv_ogm_packet->prev_sender, ethhdr->h_source);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200873
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200874 /* apply hop penalty */
Sven Eckelmann96412692012-06-05 22:31:30 +0200875 batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200876 bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200877
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200878 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200879 "Forwarding packet: tq: %i, ttl: %i\n",
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100880 batadv_ogm_packet->tq, batadv_ogm_packet->ttl);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200881
Marek Lindner75cd33f2012-03-01 15:35:16 +0800882 if (is_single_hop_neigh)
Sven Eckelmann96412692012-06-05 22:31:30 +0200883 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200884 else
Sven Eckelmann96412692012-06-05 22:31:30 +0200885 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200886
Sven Eckelmann96412692012-06-05 22:31:30 +0200887 batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
Marek Lindneref261572013-04-23 21:39:57 +0800888 BATADV_OGM_HLEN + tvlv_len,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100889 if_incoming, if_outgoing, 0,
890 batadv_iv_ogm_fwd_send_time());
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200891}
892
Antonio Quartullid9896612013-04-17 17:44:43 +0200893/**
894 * batadv_iv_ogm_slide_own_bcast_window - bitshift own OGM broadcast windows for
895 * the given interface
896 * @hard_iface: the interface for which the windows have to be shifted
897 */
898static void
899batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
900{
901 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
902 struct batadv_hashtable *hash = bat_priv->orig_hash;
903 struct hlist_head *head;
904 struct batadv_orig_node *orig_node;
905 unsigned long *word;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200906 u32 i;
Antonio Quartullid9896612013-04-17 17:44:43 +0200907 size_t word_index;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200908 u8 *w;
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200909 int if_num;
Antonio Quartullid9896612013-04-17 17:44:43 +0200910
911 for (i = 0; i < hash->size; i++) {
912 head = &hash->table[i];
913
914 rcu_read_lock();
915 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200916 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Antonio Quartullid9896612013-04-17 17:44:43 +0200917 word_index = hard_iface->if_num * BATADV_NUM_WORDS;
Antonio Quartulli32db6aa2014-09-01 14:37:29 +0200918 word = &orig_node->bat_iv.bcast_own[word_index];
Antonio Quartullid9896612013-04-17 17:44:43 +0200919
920 batadv_bit_get_packet(bat_priv, word, 1, 0);
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200921 if_num = hard_iface->if_num;
922 w = &orig_node->bat_iv.bcast_own_sum[if_num];
Antonio Quartullid9896612013-04-17 17:44:43 +0200923 *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200924 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Antonio Quartullid9896612013-04-17 17:44:43 +0200925 }
926 rcu_read_unlock();
927 }
928}
929
Sven Eckelmann56303d32012-06-05 22:31:31 +0200930static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200931{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200932 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindner14511512012-08-02 17:20:26 +0200933 unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff;
Sven Eckelmann96412692012-06-05 22:31:30 +0200934 struct batadv_ogm_packet *batadv_ogm_packet;
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100935 struct batadv_hard_iface *primary_if, *tmp_hard_iface;
Marek Lindner14511512012-08-02 17:20:26 +0200936 int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200937 u32 seqno;
938 u16 tvlv_len = 0;
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100939 unsigned long send_time;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200940
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200941 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200942
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800943 if (hard_iface == primary_if) {
944 /* tt changes have to be committed before the tvlv data is
945 * appended as it may alter the tt tvlv container
946 */
947 batadv_tt_local_commit_changes(bat_priv);
Marek Lindneref261572013-04-23 21:39:57 +0800948 tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff,
949 ogm_buff_len,
950 BATADV_OGM_HLEN);
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800951 }
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800952
Marek Lindner14511512012-08-02 17:20:26 +0200953 batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff);
Marek Lindneref261572013-04-23 21:39:57 +0800954 batadv_ogm_packet->tvlv_len = htons(tvlv_len);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200955
956 /* change sequence number to network order */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200957 seqno = (u32)atomic_read(&hard_iface->bat_iv.ogm_seqno);
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200958 batadv_ogm_packet->seqno = htonl(seqno);
Marek Lindner14511512012-08-02 17:20:26 +0200959 atomic_inc(&hard_iface->bat_iv.ogm_seqno);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200960
Antonio Quartullid9896612013-04-17 17:44:43 +0200961 batadv_iv_ogm_slide_own_bcast_window(hard_iface);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200962
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100963 send_time = batadv_iv_ogm_emit_send_time(bat_priv);
964
965 if (hard_iface != primary_if) {
966 /* OGMs from secondary interfaces are only scheduled on their
967 * respective interfaces.
968 */
969 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff, *ogm_buff_len,
970 hard_iface, hard_iface, 1, send_time);
971 goto out;
972 }
973
974 /* OGMs from primary interfaces are scheduled on all
975 * interfaces.
976 */
977 rcu_read_lock();
978 list_for_each_entry_rcu(tmp_hard_iface, &batadv_hardif_list, list) {
979 if (tmp_hard_iface->soft_iface != hard_iface->soft_iface)
Sven Eckelmann87b40f52015-07-17 10:03:42 +0200980 continue;
Sven Eckelmann27353442016-03-05 16:09:16 +0100981
982 if (!kref_get_unless_zero(&tmp_hard_iface->refcount))
983 continue;
984
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100985 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff,
986 *ogm_buff_len, hard_iface,
987 tmp_hard_iface, 1, send_time);
Sven Eckelmann27353442016-03-05 16:09:16 +0100988
989 batadv_hardif_put(tmp_hard_iface);
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100990 }
991 rcu_read_unlock();
992
993out:
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200994 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100995 batadv_hardif_put(primary_if);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200996}
997
Simon Wunderlich89652332013-11-13 19:14:46 +0100998/**
999 * batadv_iv_ogm_orig_update - use OGM to update corresponding data in an
1000 * originator
1001 * @bat_priv: the bat priv with all the soft interface information
1002 * @orig_node: the orig node who originally emitted the ogm packet
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001003 * @orig_ifinfo: ifinfo for the outgoing interface of the orig_node
Simon Wunderlich89652332013-11-13 19:14:46 +01001004 * @ethhdr: Ethernet header of the OGM
1005 * @batadv_ogm_packet: the ogm packet
1006 * @if_incoming: interface where the packet was received
1007 * @if_outgoing: interface for which the retransmission should be considered
Simon Wunderlich89652332013-11-13 19:14:46 +01001008 * @dup_status: the duplicate status of this ogm packet.
1009 */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001010static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001011batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
1012 struct batadv_orig_node *orig_node,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001013 struct batadv_orig_ifinfo *orig_ifinfo,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001014 const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +02001015 const struct batadv_ogm_packet *batadv_ogm_packet,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001016 struct batadv_hard_iface *if_incoming,
Simon Wunderlich89652332013-11-13 19:14:46 +01001017 struct batadv_hard_iface *if_outgoing,
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001018 enum batadv_dup_status dup_status)
Marek Lindnerfc957272011-07-30 12:04:12 +02001019{
Simon Wunderlich89652332013-11-13 19:14:46 +01001020 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
1021 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
Sven Eckelmann4f248cf2015-06-09 20:50:49 +02001022 struct batadv_neigh_node *neigh_node = NULL;
1023 struct batadv_neigh_node *tmp_neigh_node = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001024 struct batadv_neigh_node *router = NULL;
1025 struct batadv_orig_node *orig_node_tmp;
Linus Lüssing7caf69f2012-09-18 03:01:08 +02001026 int if_num;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001027 u8 sum_orig, sum_neigh;
1028 u8 *neigh_addr;
1029 u8 tq_avg;
Marek Lindnerfc957272011-07-30 12:04:12 +02001030
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001031 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001032 "update_originator(): Searching and updating originator entry of received packet\n");
Marek Lindnerfc957272011-07-30 12:04:12 +02001033
1034 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001035 hlist_for_each_entry_rcu(tmp_neigh_node,
Marek Lindnerfc957272011-07-30 12:04:12 +02001036 &orig_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001037 neigh_addr = tmp_neigh_node->addr;
1038 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
1039 tmp_neigh_node->if_incoming == if_incoming &&
Sven Eckelmann77ae32e2016-01-16 10:29:53 +01001040 kref_get_unless_zero(&tmp_neigh_node->refcount)) {
Antonio Quartulli38dc40e2013-03-30 17:22:00 +01001041 if (WARN(neigh_node, "too many matching neigh_nodes"))
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001042 batadv_neigh_node_put(neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001043 neigh_node = tmp_neigh_node;
1044 continue;
1045 }
1046
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001047 if (dup_status != BATADV_NO_DUP)
Marek Lindnerfc957272011-07-30 12:04:12 +02001048 continue;
1049
Simon Wunderlich89652332013-11-13 19:14:46 +01001050 /* only update the entry for this outgoing interface */
1051 neigh_ifinfo = batadv_neigh_ifinfo_get(tmp_neigh_node,
1052 if_outgoing);
1053 if (!neigh_ifinfo)
1054 continue;
1055
1056 spin_lock_bh(&tmp_neigh_node->ifinfo_lock);
1057 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
1058 &neigh_ifinfo->bat_iv.tq_index, 0);
1059 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
1060 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
1061 spin_unlock_bh(&tmp_neigh_node->ifinfo_lock);
1062
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001063 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01001064 neigh_ifinfo = NULL;
Marek Lindnerfc957272011-07-30 12:04:12 +02001065 }
1066
1067 if (!neigh_node) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001068 struct batadv_orig_node *orig_tmp;
Marek Lindnerfc957272011-07-30 12:04:12 +02001069
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001070 orig_tmp = batadv_iv_ogm_orig_get(bat_priv, ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001071 if (!orig_tmp)
1072 goto unlock;
1073
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001074 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1075 ethhdr->h_source,
Antonio Quartulli863dd7a2013-03-25 13:49:46 +01001076 orig_node, orig_tmp);
Marek Lindnerfc957272011-07-30 12:04:12 +02001077
Sven Eckelmann5d967312016-01-17 11:01:09 +01001078 batadv_orig_node_put(orig_tmp);
Marek Lindnerfc957272011-07-30 12:04:12 +02001079 if (!neigh_node)
1080 goto unlock;
Markus Pargmann23badd62014-12-26 12:41:33 +01001081 } else {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001082 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001083 "Updating existing last-hop neighbor of originator\n");
Markus Pargmann23badd62014-12-26 12:41:33 +01001084 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001085
1086 rcu_read_unlock();
Simon Wunderlich89652332013-11-13 19:14:46 +01001087 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
1088 if (!neigh_ifinfo)
1089 goto out;
Marek Lindnerfc957272011-07-30 12:04:12 +02001090
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001091 neigh_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +02001092
Simon Wunderlich89652332013-11-13 19:14:46 +01001093 spin_lock_bh(&neigh_node->ifinfo_lock);
1094 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
1095 &neigh_ifinfo->bat_iv.tq_index,
Sven Eckelmann96412692012-06-05 22:31:30 +02001096 batadv_ogm_packet->tq);
Simon Wunderlich89652332013-11-13 19:14:46 +01001097 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
1098 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
1099 spin_unlock_bh(&neigh_node->ifinfo_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001100
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001101 if (dup_status == BATADV_NO_DUP) {
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001102 orig_ifinfo->last_ttl = batadv_ogm_packet->ttl;
Simon Wunderlich89652332013-11-13 19:14:46 +01001103 neigh_ifinfo->last_ttl = batadv_ogm_packet->ttl;
Marek Lindnerfc957272011-07-30 12:04:12 +02001104 }
1105
Marek Lindnerfc957272011-07-30 12:04:12 +02001106 /* if this neighbor already is our next hop there is nothing
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001107 * to change
1108 */
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001109 router = batadv_orig_router_get(orig_node, if_outgoing);
Marek Lindnerfc957272011-07-30 12:04:12 +02001110 if (router == neigh_node)
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001111 goto out;
Marek Lindnerfc957272011-07-30 12:04:12 +02001112
Simon Wunderlich89652332013-11-13 19:14:46 +01001113 if (router) {
1114 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1115 if (!router_ifinfo)
1116 goto out;
1117
1118 /* if this neighbor does not offer a better TQ we won't
1119 * consider it
1120 */
1121 if (router_ifinfo->bat_iv.tq_avg > neigh_ifinfo->bat_iv.tq_avg)
1122 goto out;
1123 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001124
1125 /* if the TQ is the same and the link not more symmetric we
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001126 * won't consider it either
1127 */
Simon Wunderlich89652332013-11-13 19:14:46 +01001128 if (router_ifinfo &&
Markus Pargmann01b97a32014-12-26 12:41:30 +01001129 neigh_ifinfo->bat_iv.tq_avg == router_ifinfo->bat_iv.tq_avg) {
Marek Lindnerfc957272011-07-30 12:04:12 +02001130 orig_node_tmp = router->orig_node;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001131 spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
Linus Lüssing7caf69f2012-09-18 03:01:08 +02001132 if_num = router->if_incoming->if_num;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001133 sum_orig = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
1134 spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001135
1136 orig_node_tmp = neigh_node->orig_node;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001137 spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
Linus Lüssing7caf69f2012-09-18 03:01:08 +02001138 if_num = neigh_node->if_incoming->if_num;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001139 sum_neigh = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
1140 spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001141
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001142 if (sum_orig >= sum_neigh)
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001143 goto out;
Marek Lindnerfc957272011-07-30 12:04:12 +02001144 }
1145
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001146 batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001147 goto out;
1148
1149unlock:
1150 rcu_read_unlock();
1151out:
1152 if (neigh_node)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001153 batadv_neigh_node_put(neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001154 if (router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001155 batadv_neigh_node_put(router);
Simon Wunderlich89652332013-11-13 19:14:46 +01001156 if (neigh_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001157 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01001158 if (router_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001159 batadv_neigh_ifinfo_put(router_ifinfo);
Marek Lindnerfc957272011-07-30 12:04:12 +02001160}
1161
Simon Wunderlich89652332013-11-13 19:14:46 +01001162/**
1163 * batadv_iv_ogm_calc_tq - calculate tq for current received ogm packet
1164 * @orig_node: the orig node who originally emitted the ogm packet
1165 * @orig_neigh_node: the orig node struct of the neighbor who sent the packet
1166 * @batadv_ogm_packet: the ogm packet
1167 * @if_incoming: interface where the packet was received
1168 * @if_outgoing: interface for which the retransmission should be considered
1169 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001170 * Return: true if the link can be considered bidirectional, false otherwise
Simon Wunderlich89652332013-11-13 19:14:46 +01001171 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001172static bool batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
1173 struct batadv_orig_node *orig_neigh_node,
1174 struct batadv_ogm_packet *batadv_ogm_packet,
1175 struct batadv_hard_iface *if_incoming,
1176 struct batadv_hard_iface *if_outgoing)
Marek Lindnerfc957272011-07-30 12:04:12 +02001177{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001178 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1179 struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
Simon Wunderlich89652332013-11-13 19:14:46 +01001180 struct batadv_neigh_ifinfo *neigh_ifinfo;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001181 u8 total_count;
1182 u8 orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001183 unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
Sven Eckelmannd285f522016-02-16 10:47:07 +01001184 int if_num;
1185 unsigned int tq_asym_penalty, inv_asym_penalty;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001186 unsigned int combined_tq;
Sven Eckelmannd285f522016-02-16 10:47:07 +01001187 unsigned int tq_iface_penalty;
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001188 bool ret = false;
Marek Lindnerfc957272011-07-30 12:04:12 +02001189
1190 /* find corresponding one hop neighbor */
1191 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001192 hlist_for_each_entry_rcu(tmp_neigh_node,
Marek Lindnerfc957272011-07-30 12:04:12 +02001193 &orig_neigh_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001194 if (!batadv_compare_eth(tmp_neigh_node->addr,
1195 orig_neigh_node->orig))
Marek Lindnerfc957272011-07-30 12:04:12 +02001196 continue;
1197
1198 if (tmp_neigh_node->if_incoming != if_incoming)
1199 continue;
1200
Sven Eckelmann77ae32e2016-01-16 10:29:53 +01001201 if (!kref_get_unless_zero(&tmp_neigh_node->refcount))
Marek Lindnerfc957272011-07-30 12:04:12 +02001202 continue;
1203
1204 neigh_node = tmp_neigh_node;
1205 break;
1206 }
1207 rcu_read_unlock();
1208
1209 if (!neigh_node)
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001210 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1211 orig_neigh_node->orig,
1212 orig_neigh_node,
Antonio Quartulli863dd7a2013-03-25 13:49:46 +01001213 orig_neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001214
1215 if (!neigh_node)
1216 goto out;
1217
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001218 /* if orig_node is direct neighbor update neigh_node last_seen */
Marek Lindnerfc957272011-07-30 12:04:12 +02001219 if (orig_node == orig_neigh_node)
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001220 neigh_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +02001221
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001222 orig_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +02001223
1224 /* find packet count of corresponding one hop neighbor */
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001225 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1226 if_num = if_incoming->if_num;
1227 orig_eq_count = orig_neigh_node->bat_iv.bcast_own_sum[if_num];
Simon Wunderlich89652332013-11-13 19:14:46 +01001228 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
1229 if (neigh_ifinfo) {
1230 neigh_rq_count = neigh_ifinfo->bat_iv.real_packet_count;
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001231 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01001232 } else {
1233 neigh_rq_count = 0;
1234 }
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001235 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001236
1237 /* pay attention to not get a value bigger than 100 % */
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001238 if (orig_eq_count > neigh_rq_count)
1239 total_count = neigh_rq_count;
1240 else
1241 total_count = orig_eq_count;
Marek Lindnerfc957272011-07-30 12:04:12 +02001242
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001243 /* if we have too few packets (too less data) we set tq_own to zero
1244 * if we receive too few packets it is not considered bidirectional
1245 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001246 if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
1247 neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
Marek Lindnerfc957272011-07-30 12:04:12 +02001248 tq_own = 0;
1249 else
1250 /* neigh_node->real_packet_count is never zero as we
1251 * only purge old information when getting new
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001252 * information
1253 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001254 tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count;
Marek Lindnerfc957272011-07-30 12:04:12 +02001255
Sven Eckelmann21a12362012-03-07 09:07:46 +01001256 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
Marek Lindnerfc957272011-07-30 12:04:12 +02001257 * affect the nearly-symmetric links only a little, but
1258 * punishes asymmetric links more. This will give a value
1259 * between 0 and TQ_MAX_VALUE
1260 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001261 neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
1262 neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
1263 neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
1264 BATADV_TQ_LOCAL_WINDOW_SIZE *
1265 BATADV_TQ_LOCAL_WINDOW_SIZE;
1266 inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
1267 inv_asym_penalty /= neigh_rq_max_cube;
1268 tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;
Marek Lindnerfc957272011-07-30 12:04:12 +02001269
Simon Wunderlichc0398762013-11-13 19:14:48 +01001270 /* penalize if the OGM is forwarded on the same interface. WiFi
1271 * interfaces and other half duplex devices suffer from throughput
1272 * drops as they can't send and receive at the same time.
1273 */
1274 tq_iface_penalty = BATADV_TQ_MAX_VALUE;
1275 if (if_outgoing && (if_incoming == if_outgoing) &&
1276 batadv_is_wifi_netdev(if_outgoing->net_dev))
1277 tq_iface_penalty = batadv_hop_penalty(BATADV_TQ_MAX_VALUE,
1278 bat_priv);
1279
1280 combined_tq = batadv_ogm_packet->tq *
1281 tq_own *
1282 tq_asym_penalty *
1283 tq_iface_penalty;
1284 combined_tq /= BATADV_TQ_MAX_VALUE *
1285 BATADV_TQ_MAX_VALUE *
1286 BATADV_TQ_MAX_VALUE;
Sven Eckelmann96412692012-06-05 22:31:30 +02001287 batadv_ogm_packet->tq = combined_tq;
Marek Lindnerfc957272011-07-30 12:04:12 +02001288
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001289 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Simon Wunderlichc0398762013-11-13 19:14:48 +01001290 "bidirectional: orig = %-15pM neigh = %-15pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, iface_penalty: %3i, total tq: %3i, if_incoming = %s, if_outgoing = %s\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001291 orig_node->orig, orig_neigh_node->orig, total_count,
Simon Wunderlichc0398762013-11-13 19:14:48 +01001292 neigh_rq_count, tq_own, tq_asym_penalty, tq_iface_penalty,
1293 batadv_ogm_packet->tq, if_incoming->net_dev->name,
1294 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT");
Marek Lindnerfc957272011-07-30 12:04:12 +02001295
1296 /* if link has the minimum required transmission quality
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001297 * consider it bidirectional
1298 */
Sven Eckelmann96412692012-06-05 22:31:30 +02001299 if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001300 ret = true;
Marek Lindnerfc957272011-07-30 12:04:12 +02001301
1302out:
1303 if (neigh_node)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001304 batadv_neigh_node_put(neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001305 return ret;
1306}
1307
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001308/**
1309 * batadv_iv_ogm_update_seqnos - process a batman packet for all interfaces,
1310 * adjust the sequence number and find out whether it is a duplicate
1311 * @ethhdr: ethernet header of the packet
1312 * @batadv_ogm_packet: OGM packet to be considered
1313 * @if_incoming: interface on which the OGM packet was received
Simon Wunderlich89652332013-11-13 19:14:46 +01001314 * @if_outgoing: interface for which the retransmission should be considered
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001315 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001316 * Return: duplicate status as enum batadv_dup_status
Marek Lindnerfc957272011-07-30 12:04:12 +02001317 */
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001318static enum batadv_dup_status
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001319batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +02001320 const struct batadv_ogm_packet *batadv_ogm_packet,
Simon Wunderlich89652332013-11-13 19:14:46 +01001321 const struct batadv_hard_iface *if_incoming,
1322 struct batadv_hard_iface *if_outgoing)
Marek Lindnerfc957272011-07-30 12:04:12 +02001323{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001324 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1325 struct batadv_orig_node *orig_node;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001326 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
Simon Wunderlich89652332013-11-13 19:14:46 +01001327 struct batadv_neigh_node *neigh_node;
1328 struct batadv_neigh_ifinfo *neigh_ifinfo;
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001329 bool is_dup;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001330 s32 seq_diff;
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001331 bool need_update = false;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001332 int set_mark;
1333 enum batadv_dup_status ret = BATADV_NO_DUP;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001334 u32 seqno = ntohl(batadv_ogm_packet->seqno);
1335 u8 *neigh_addr;
1336 u8 packet_count;
Antonio Quartulli0538f752013-09-02 12:15:01 +02001337 unsigned long *bitmap;
Marek Lindnerfc957272011-07-30 12:04:12 +02001338
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001339 orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
Marek Lindnerfc957272011-07-30 12:04:12 +02001340 if (!orig_node)
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001341 return BATADV_NO_DUP;
Marek Lindnerfc957272011-07-30 12:04:12 +02001342
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001343 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1344 if (WARN_ON(!orig_ifinfo)) {
Sven Eckelmann5d967312016-01-17 11:01:09 +01001345 batadv_orig_node_put(orig_node);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001346 return 0;
1347 }
1348
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001349 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001350 seq_diff = seqno - orig_ifinfo->last_real_seqno;
Marek Lindnerfc957272011-07-30 12:04:12 +02001351
1352 /* signalize caller that the packet is to be dropped. */
Antonio Quartulli1e5cc262012-02-26 15:39:42 +01001353 if (!hlist_empty(&orig_node->neigh_list) &&
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001354 batadv_window_protected(bat_priv, seq_diff,
Simon Wunderlich81f02682015-11-23 19:57:22 +01001355 BATADV_TQ_LOCAL_WINDOW_SIZE,
1356 &orig_ifinfo->batman_seqno_reset, NULL)) {
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001357 ret = BATADV_PROTECTED;
Marek Lindnerfc957272011-07-30 12:04:12 +02001358 goto out;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001359 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001360
1361 rcu_read_lock();
Simon Wunderlich89652332013-11-13 19:14:46 +01001362 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1363 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node,
1364 if_outgoing);
1365 if (!neigh_ifinfo)
1366 continue;
1367
1368 neigh_addr = neigh_node->addr;
1369 is_dup = batadv_test_bit(neigh_ifinfo->bat_iv.real_bits,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001370 orig_ifinfo->last_real_seqno,
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001371 seqno);
1372
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001373 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
Simon Wunderlich89652332013-11-13 19:14:46 +01001374 neigh_node->if_incoming == if_incoming) {
Marek Lindnerfc957272011-07-30 12:04:12 +02001375 set_mark = 1;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001376 if (is_dup)
1377 ret = BATADV_NEIGH_DUP;
1378 } else {
Marek Lindnerfc957272011-07-30 12:04:12 +02001379 set_mark = 0;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001380 if (is_dup && (ret != BATADV_NEIGH_DUP))
1381 ret = BATADV_ORIG_DUP;
1382 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001383
1384 /* if the window moved, set the update flag. */
Simon Wunderlich89652332013-11-13 19:14:46 +01001385 bitmap = neigh_ifinfo->bat_iv.real_bits;
Antonio Quartulli0538f752013-09-02 12:15:01 +02001386 need_update |= batadv_bit_get_packet(bat_priv, bitmap,
Sven Eckelmann0f5f9322012-05-12 02:09:25 +02001387 seq_diff, set_mark);
Marek Lindnerfc957272011-07-30 12:04:12 +02001388
Simon Wunderlich89652332013-11-13 19:14:46 +01001389 packet_count = bitmap_weight(bitmap,
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001390 BATADV_TQ_LOCAL_WINDOW_SIZE);
Simon Wunderlich89652332013-11-13 19:14:46 +01001391 neigh_ifinfo->bat_iv.real_packet_count = packet_count;
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001392 batadv_neigh_ifinfo_put(neigh_ifinfo);
Marek Lindnerfc957272011-07-30 12:04:12 +02001393 }
1394 rcu_read_unlock();
1395
1396 if (need_update) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001397 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001398 "%s updating last_seqno: old %u, new %u\n",
1399 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT",
1400 orig_ifinfo->last_real_seqno, seqno);
1401 orig_ifinfo->last_real_seqno = seqno;
Marek Lindnerfc957272011-07-30 12:04:12 +02001402 }
1403
Marek Lindnerfc957272011-07-30 12:04:12 +02001404out:
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001405 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Sven Eckelmann5d967312016-01-17 11:01:09 +01001406 batadv_orig_node_put(orig_node);
Sven Eckelmann35f94772016-01-17 11:01:13 +01001407 batadv_orig_ifinfo_put(orig_ifinfo);
Marek Lindnerfc957272011-07-30 12:04:12 +02001408 return ret;
1409}
1410
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001411/**
1412 * batadv_iv_ogm_process_per_outif - process a batman iv OGM for an outgoing if
1413 * @skb: the skb containing the OGM
Martin Hundebøll95298a92014-07-15 09:41:05 +02001414 * @ogm_offset: offset from skb->data to start of ogm header
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001415 * @orig_node: the (cached) orig node for the originator of this OGM
1416 * @if_incoming: the interface where this packet was received
1417 * @if_outgoing: the interface for which the packet should be considered
1418 */
1419static void
1420batadv_iv_ogm_process_per_outif(const struct sk_buff *skb, int ogm_offset,
1421 struct batadv_orig_node *orig_node,
1422 struct batadv_hard_iface *if_incoming,
1423 struct batadv_hard_iface *if_outgoing)
1424{
1425 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Marek Lindner4ff1e2a2015-08-04 21:09:58 +08001426 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
Sven Eckelmann4f248cf2015-06-09 20:50:49 +02001427 struct batadv_neigh_node *router = NULL;
1428 struct batadv_neigh_node *router_router = NULL;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001429 struct batadv_orig_node *orig_neigh_node;
1430 struct batadv_orig_ifinfo *orig_ifinfo;
1431 struct batadv_neigh_node *orig_neigh_router = NULL;
1432 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
1433 struct batadv_ogm_packet *ogm_packet;
1434 enum batadv_dup_status dup_status;
1435 bool is_from_best_next_hop = false;
1436 bool is_single_hop_neigh = false;
1437 bool sameseq, similar_ttl;
1438 struct sk_buff *skb_priv;
1439 struct ethhdr *ethhdr;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001440 u8 *prev_sender;
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001441 bool is_bidirect;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001442
1443 /* create a private copy of the skb, as some functions change tq value
1444 * and/or flags.
1445 */
1446 skb_priv = skb_copy(skb, GFP_ATOMIC);
1447 if (!skb_priv)
1448 return;
1449
1450 ethhdr = eth_hdr(skb_priv);
1451 ogm_packet = (struct batadv_ogm_packet *)(skb_priv->data + ogm_offset);
1452
1453 dup_status = batadv_iv_ogm_update_seqnos(ethhdr, ogm_packet,
1454 if_incoming, if_outgoing);
1455 if (batadv_compare_eth(ethhdr->h_source, ogm_packet->orig))
1456 is_single_hop_neigh = true;
1457
1458 if (dup_status == BATADV_PROTECTED) {
1459 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1460 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1461 ethhdr->h_source);
1462 goto out;
1463 }
1464
1465 if (ogm_packet->tq == 0) {
1466 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1467 "Drop packet: originator packet with tq equal 0\n");
1468 goto out;
1469 }
1470
Marek Lindner4ff1e2a2015-08-04 21:09:58 +08001471 if (is_single_hop_neigh) {
1472 hardif_neigh = batadv_hardif_neigh_get(if_incoming,
1473 ethhdr->h_source);
1474 if (hardif_neigh)
1475 hardif_neigh->last_seen = jiffies;
1476 }
1477
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001478 router = batadv_orig_router_get(orig_node, if_outgoing);
1479 if (router) {
1480 router_router = batadv_orig_router_get(router->orig_node,
1481 if_outgoing);
1482 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1483 }
1484
1485 if ((router_ifinfo && router_ifinfo->bat_iv.tq_avg != 0) &&
1486 (batadv_compare_eth(router->addr, ethhdr->h_source)))
1487 is_from_best_next_hop = true;
1488
1489 prev_sender = ogm_packet->prev_sender;
1490 /* avoid temporary routing loops */
1491 if (router && router_router &&
1492 (batadv_compare_eth(router->addr, prev_sender)) &&
1493 !(batadv_compare_eth(ogm_packet->orig, prev_sender)) &&
1494 (batadv_compare_eth(router->addr, router_router->addr))) {
1495 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1496 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1497 ethhdr->h_source);
1498 goto out;
1499 }
1500
1501 if (if_outgoing == BATADV_IF_DEFAULT)
1502 batadv_tvlv_ogm_receive(bat_priv, ogm_packet, orig_node);
1503
1504 /* if sender is a direct neighbor the sender mac equals
1505 * originator mac
1506 */
1507 if (is_single_hop_neigh)
1508 orig_neigh_node = orig_node;
1509 else
1510 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1511 ethhdr->h_source);
1512
1513 if (!orig_neigh_node)
1514 goto out;
1515
1516 /* Update nc_nodes of the originator */
1517 batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node,
1518 ogm_packet, is_single_hop_neigh);
1519
1520 orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
1521 if_outgoing);
1522
1523 /* drop packet if sender is not a direct neighbor and if we
1524 * don't route towards it
1525 */
1526 if (!is_single_hop_neigh && (!orig_neigh_router)) {
1527 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1528 "Drop packet: OGM via unknown neighbor!\n");
1529 goto out_neigh;
1530 }
1531
1532 is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
1533 ogm_packet, if_incoming,
1534 if_outgoing);
1535
1536 /* update ranking if it is not a duplicate or has the same
1537 * seqno and similar ttl as the non-duplicate
1538 */
1539 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1540 if (!orig_ifinfo)
1541 goto out_neigh;
1542
1543 sameseq = orig_ifinfo->last_real_seqno == ntohl(ogm_packet->seqno);
1544 similar_ttl = (orig_ifinfo->last_ttl - 3) <= ogm_packet->ttl;
1545
1546 if (is_bidirect && ((dup_status == BATADV_NO_DUP) ||
1547 (sameseq && similar_ttl))) {
1548 batadv_iv_ogm_orig_update(bat_priv, orig_node,
1549 orig_ifinfo, ethhdr,
1550 ogm_packet, if_incoming,
1551 if_outgoing, dup_status);
1552 }
Sven Eckelmann35f94772016-01-17 11:01:13 +01001553 batadv_orig_ifinfo_put(orig_ifinfo);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001554
Simon Wunderlichef0a9372013-11-13 19:14:49 +01001555 /* only forward for specific interface, not for the default one. */
1556 if (if_outgoing == BATADV_IF_DEFAULT)
1557 goto out_neigh;
1558
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001559 /* is single hop (direct) neighbor */
1560 if (is_single_hop_neigh) {
1561 /* OGMs from secondary interfaces should only scheduled once
1562 * per interface where it has been received, not multiple times
1563 */
1564 if ((ogm_packet->ttl <= 2) &&
1565 (if_incoming != if_outgoing)) {
1566 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1567 "Drop packet: OGM from secondary interface and wrong outgoing interface\n");
1568 goto out_neigh;
1569 }
1570 /* mark direct link on incoming interface */
1571 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1572 is_single_hop_neigh,
Simon Wunderlichef0a9372013-11-13 19:14:49 +01001573 is_from_best_next_hop, if_incoming,
1574 if_outgoing);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001575
1576 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1577 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
1578 goto out_neigh;
1579 }
1580
1581 /* multihop originator */
1582 if (!is_bidirect) {
1583 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1584 "Drop packet: not received via bidirectional link\n");
1585 goto out_neigh;
1586 }
1587
1588 if (dup_status == BATADV_NEIGH_DUP) {
1589 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1590 "Drop packet: duplicate packet received\n");
1591 goto out_neigh;
1592 }
1593
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001594 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1595 "Forwarding packet: rebroadcast originator packet\n");
1596 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1597 is_single_hop_neigh, is_from_best_next_hop,
Simon Wunderlichef0a9372013-11-13 19:14:49 +01001598 if_incoming, if_outgoing);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001599
1600out_neigh:
1601 if ((orig_neigh_node) && (!is_single_hop_neigh))
Sven Eckelmann5d967312016-01-17 11:01:09 +01001602 batadv_orig_node_put(orig_neigh_node);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001603out:
Simon Wunderlichc1e517f2014-03-26 15:46:21 +01001604 if (router_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001605 batadv_neigh_ifinfo_put(router_ifinfo);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001606 if (router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001607 batadv_neigh_node_put(router);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001608 if (router_router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001609 batadv_neigh_node_put(router_router);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001610 if (orig_neigh_router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001611 batadv_neigh_node_put(orig_neigh_router);
Marek Lindner4ff1e2a2015-08-04 21:09:58 +08001612 if (hardif_neigh)
Sven Eckelmannaccadc32016-01-17 11:01:14 +01001613 batadv_hardif_neigh_put(hardif_neigh);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001614
1615 kfree_skb(skb_priv);
1616}
1617
1618/**
1619 * batadv_iv_ogm_process - process an incoming batman iv OGM
1620 * @skb: the skb containing the OGM
1621 * @ogm_offset: offset to the OGM which should be processed (for aggregates)
1622 * @if_incoming: the interface where this packet was receved
1623 */
1624static void batadv_iv_ogm_process(const struct sk_buff *skb, int ogm_offset,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001625 struct batadv_hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +02001626{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001627 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001628 struct batadv_orig_node *orig_neigh_node, *orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001629 struct batadv_hard_iface *hard_iface;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001630 struct batadv_ogm_packet *ogm_packet;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001631 u32 if_incoming_seqno;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001632 bool has_directlink_flag;
1633 struct ethhdr *ethhdr;
1634 bool is_my_oldorig = false;
1635 bool is_my_addr = false;
1636 bool is_my_orig = false;
1637
1638 ogm_packet = (struct batadv_ogm_packet *)(skb->data + ogm_offset);
1639 ethhdr = eth_hdr(skb);
Marek Lindnerfc957272011-07-30 12:04:12 +02001640
1641 /* Silently drop when the batman packet is actually not a
1642 * correct packet.
1643 *
1644 * This might happen if a packet is padded (e.g. Ethernet has a
1645 * minimum frame length of 64 byte) and the aggregation interprets
1646 * it as an additional length.
1647 *
1648 * TODO: A more sane solution would be to have a bit in the
Sven Eckelmann96412692012-06-05 22:31:30 +02001649 * batadv_ogm_packet to detect whether the packet is the last
Marek Lindnerfc957272011-07-30 12:04:12 +02001650 * packet in an aggregation. Here we expect that the padding
1651 * is always zero (or not 0x01)
1652 */
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001653 if (ogm_packet->packet_type != BATADV_IV_OGM)
Marek Lindnerfc957272011-07-30 12:04:12 +02001654 return;
1655
1656 /* could be changed by schedule_own_packet() */
Marek Lindner14511512012-08-02 17:20:26 +02001657 if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno);
Marek Lindnerfc957272011-07-30 12:04:12 +02001658
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001659 if (ogm_packet->flags & BATADV_DIRECTLINK)
1660 has_directlink_flag = true;
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001661 else
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001662 has_directlink_flag = false;
Marek Lindnerfc957272011-07-30 12:04:12 +02001663
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001664 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001665 "Received BATMAN packet via NB: %pM, IF: %s [%pM] (from OG: %pM, via prev OG: %pM, seqno %u, tq %d, TTL %d, V %d, IDF %d)\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001666 ethhdr->h_source, if_incoming->net_dev->name,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001667 if_incoming->net_dev->dev_addr, ogm_packet->orig,
1668 ogm_packet->prev_sender, ntohl(ogm_packet->seqno),
1669 ogm_packet->tq, ogm_packet->ttl,
1670 ogm_packet->version, has_directlink_flag);
Marek Lindnerfc957272011-07-30 12:04:12 +02001671
1672 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001673 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001674 if (hard_iface->if_status != BATADV_IF_ACTIVE)
Marek Lindnerfc957272011-07-30 12:04:12 +02001675 continue;
1676
1677 if (hard_iface->soft_iface != if_incoming->soft_iface)
1678 continue;
1679
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001680 if (batadv_compare_eth(ethhdr->h_source,
1681 hard_iface->net_dev->dev_addr))
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001682 is_my_addr = true;
Marek Lindnerfc957272011-07-30 12:04:12 +02001683
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001684 if (batadv_compare_eth(ogm_packet->orig,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001685 hard_iface->net_dev->dev_addr))
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001686 is_my_orig = true;
Marek Lindnerfc957272011-07-30 12:04:12 +02001687
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001688 if (batadv_compare_eth(ogm_packet->prev_sender,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001689 hard_iface->net_dev->dev_addr))
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001690 is_my_oldorig = true;
Marek Lindnerfc957272011-07-30 12:04:12 +02001691 }
1692 rcu_read_unlock();
1693
Marek Lindnerfc957272011-07-30 12:04:12 +02001694 if (is_my_addr) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001695 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001696 "Drop packet: received my own broadcast (sender: %pM)\n",
1697 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001698 return;
1699 }
1700
Marek Lindnerfc957272011-07-30 12:04:12 +02001701 if (is_my_orig) {
1702 unsigned long *word;
1703 int offset;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001704 s32 bit_pos;
1705 s16 if_num;
1706 u8 *weight;
Marek Lindnerfc957272011-07-30 12:04:12 +02001707
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001708 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1709 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001710 if (!orig_neigh_node)
1711 return;
1712
1713 /* neighbor has to indicate direct link and it has to
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001714 * come via the corresponding interface
1715 * save packet seqno for bidirectional check
1716 */
Marek Lindnerfc957272011-07-30 12:04:12 +02001717 if (has_directlink_flag &&
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001718 batadv_compare_eth(if_incoming->net_dev->dev_addr,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001719 ogm_packet->orig)) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001720 if_num = if_incoming->if_num;
1721 offset = if_num * BATADV_NUM_WORDS;
Marek Lindnerfc957272011-07-30 12:04:12 +02001722
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001723 spin_lock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
Antonio Quartulli32db6aa2014-09-01 14:37:29 +02001724 word = &orig_neigh_node->bat_iv.bcast_own[offset];
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001725 bit_pos = if_incoming_seqno - 2;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001726 bit_pos -= ntohl(ogm_packet->seqno);
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001727 batadv_set_bit(word, bit_pos);
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001728 weight = &orig_neigh_node->bat_iv.bcast_own_sum[if_num];
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001729 *weight = bitmap_weight(word,
1730 BATADV_TQ_LOCAL_WINDOW_SIZE);
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001731 spin_unlock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001732 }
1733
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001734 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001735 "Drop packet: originator packet from myself (via neighbor)\n");
Sven Eckelmann5d967312016-01-17 11:01:09 +01001736 batadv_orig_node_put(orig_neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001737 return;
1738 }
1739
1740 if (is_my_oldorig) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001741 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001742 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1743 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001744 return;
1745 }
1746
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001747 if (ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001748 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001749 "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1750 ethhdr->h_source);
Marek Lindner13b25412012-03-11 06:17:53 +08001751 return;
1752 }
1753
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001754 orig_node = batadv_iv_ogm_orig_get(bat_priv, ogm_packet->orig);
Marek Lindnerfc957272011-07-30 12:04:12 +02001755 if (!orig_node)
1756 return;
1757
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001758 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1759 if_incoming, BATADV_IF_DEFAULT);
Marek Lindnerfc957272011-07-30 12:04:12 +02001760
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001761 rcu_read_lock();
1762 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1763 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1764 continue;
1765
1766 if (hard_iface->soft_iface != bat_priv->soft_iface)
1767 continue;
1768
Sven Eckelmann27353442016-03-05 16:09:16 +01001769 if (!kref_get_unless_zero(&hard_iface->refcount))
1770 continue;
1771
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001772 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1773 if_incoming, hard_iface);
Sven Eckelmann27353442016-03-05 16:09:16 +01001774
1775 batadv_hardif_put(hard_iface);
Marek Lindnerfc957272011-07-30 12:04:12 +02001776 }
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001777 rcu_read_unlock();
Marek Lindnerfc957272011-07-30 12:04:12 +02001778
Sven Eckelmann5d967312016-01-17 11:01:09 +01001779 batadv_orig_node_put(orig_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001780}
1781
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001782static int batadv_iv_ogm_receive(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001783 struct batadv_hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +02001784{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001785 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001786 struct batadv_ogm_packet *ogm_packet;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001787 u8 *packet_pos;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001788 int ogm_offset;
Marek Lindneref261572013-04-23 21:39:57 +08001789 bool ret;
Marek Lindnerc3e29312012-03-04 16:56:25 +08001790
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001791 ret = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
Marek Lindnerc3e29312012-03-04 16:56:25 +08001792 if (!ret)
1793 return NET_RX_DROP;
Marek Lindnerfc957272011-07-30 12:04:12 +02001794
Marek Lindneredbf12b2012-03-11 06:17:49 +08001795 /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
1796 * that does not have B.A.T.M.A.N. IV enabled ?
1797 */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001798 if (bat_priv->bat_algo_ops->bat_ogm_emit != batadv_iv_ogm_emit)
Marek Lindneredbf12b2012-03-11 06:17:49 +08001799 return NET_RX_DROP;
1800
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001801 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
1802 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
Martin Hundebøllf8214862012-04-20 17:02:45 +02001803 skb->len + ETH_HLEN);
1804
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001805 ogm_offset = 0;
1806 ogm_packet = (struct batadv_ogm_packet *)skb->data;
Marek Lindnerfc957272011-07-30 12:04:12 +02001807
1808 /* unpack the aggregated packets and process them one by one */
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001809 while (batadv_iv_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
1810 ogm_packet->tvlv_len)) {
1811 batadv_iv_ogm_process(skb, ogm_offset, if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001812
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001813 ogm_offset += BATADV_OGM_HLEN;
1814 ogm_offset += ntohs(ogm_packet->tvlv_len);
Marek Lindnerfc957272011-07-30 12:04:12 +02001815
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001816 packet_pos = skb->data + ogm_offset;
1817 ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Marek Lindnerb47506d2013-03-04 10:39:49 +08001818 }
Marek Lindnerc3e29312012-03-04 16:56:25 +08001819
1820 kfree_skb(skb);
1821 return NET_RX_SUCCESS;
Marek Lindnerfc957272011-07-30 12:04:12 +02001822}
Marek Lindner1c280472011-11-28 17:40:17 +08001823
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001824/**
1825 * batadv_iv_ogm_orig_print_neigh - print neighbors for the originator table
Simon Wunderlich89652332013-11-13 19:14:46 +01001826 * @orig_node: the orig_node for which the neighbors are printed
1827 * @if_outgoing: outgoing interface for these entries
1828 * @seq: debugfs table seq_file struct
1829 *
1830 * Must be called while holding an rcu lock.
1831 */
1832static void
1833batadv_iv_ogm_orig_print_neigh(struct batadv_orig_node *orig_node,
1834 struct batadv_hard_iface *if_outgoing,
1835 struct seq_file *seq)
1836{
1837 struct batadv_neigh_node *neigh_node;
1838 struct batadv_neigh_ifinfo *n_ifinfo;
1839
1840 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1841 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
1842 if (!n_ifinfo)
1843 continue;
1844
1845 seq_printf(seq, " %pM (%3i)",
1846 neigh_node->addr,
1847 n_ifinfo->bat_iv.tq_avg);
1848
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001849 batadv_neigh_ifinfo_put(n_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01001850 }
1851}
1852
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001853/**
1854 * batadv_iv_ogm_orig_print - print the originator table
1855 * @bat_priv: the bat priv with all the soft interface information
1856 * @seq: debugfs table seq_file struct
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001857 * @if_outgoing: the outgoing interface for which this should be printed
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001858 */
1859static void batadv_iv_ogm_orig_print(struct batadv_priv *bat_priv,
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001860 struct seq_file *seq,
1861 struct batadv_hard_iface *if_outgoing)
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001862{
Simon Wunderlich89652332013-11-13 19:14:46 +01001863 struct batadv_neigh_node *neigh_node;
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001864 struct batadv_hashtable *hash = bat_priv->orig_hash;
1865 int last_seen_msecs, last_seen_secs;
1866 struct batadv_orig_node *orig_node;
Simon Wunderlich89652332013-11-13 19:14:46 +01001867 struct batadv_neigh_ifinfo *n_ifinfo;
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001868 unsigned long last_seen_jiffies;
1869 struct hlist_head *head;
1870 int batman_count = 0;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001871 u32 i;
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001872
Antonio Quartulli925a6f32016-03-12 10:30:18 +01001873 seq_puts(seq,
1874 " Originator last-seen (#/255) Nexthop [outgoingIF]: Potential nexthops ...\n");
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001875
1876 for (i = 0; i < hash->size; i++) {
1877 head = &hash->table[i];
1878
1879 rcu_read_lock();
1880 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001881 neigh_node = batadv_orig_router_get(orig_node,
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001882 if_outgoing);
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001883 if (!neigh_node)
1884 continue;
1885
Simon Wunderlich89652332013-11-13 19:14:46 +01001886 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node,
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001887 if_outgoing);
Simon Wunderlich89652332013-11-13 19:14:46 +01001888 if (!n_ifinfo)
1889 goto next;
1890
1891 if (n_ifinfo->bat_iv.tq_avg == 0)
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001892 goto next;
1893
1894 last_seen_jiffies = jiffies - orig_node->last_seen;
1895 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
1896 last_seen_secs = last_seen_msecs / 1000;
1897 last_seen_msecs = last_seen_msecs % 1000;
1898
1899 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
1900 orig_node->orig, last_seen_secs,
Simon Wunderlich89652332013-11-13 19:14:46 +01001901 last_seen_msecs, n_ifinfo->bat_iv.tq_avg,
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001902 neigh_node->addr,
1903 neigh_node->if_incoming->net_dev->name);
1904
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001905 batadv_iv_ogm_orig_print_neigh(orig_node, if_outgoing,
1906 seq);
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001907 seq_puts(seq, "\n");
1908 batman_count++;
1909
1910next:
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001911 batadv_neigh_node_put(neigh_node);
Simon Wunderlich89652332013-11-13 19:14:46 +01001912 if (n_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001913 batadv_neigh_ifinfo_put(n_ifinfo);
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001914 }
1915 rcu_read_unlock();
1916 }
1917
1918 if (batman_count == 0)
1919 seq_puts(seq, "No batman nodes in range ...\n");
1920}
1921
Antonio Quartullia3285a82013-09-02 12:15:04 +02001922/**
Marek Lindner75874052015-08-04 21:09:57 +08001923 * batadv_iv_hardif_neigh_print - print a single hop neighbour node
1924 * @seq: neighbour table seq_file struct
1925 * @hardif_neigh: hardif neighbour information
1926 */
1927static void
1928batadv_iv_hardif_neigh_print(struct seq_file *seq,
1929 struct batadv_hardif_neigh_node *hardif_neigh)
1930{
1931 int last_secs, last_msecs;
1932
1933 last_secs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) / 1000;
1934 last_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) % 1000;
1935
1936 seq_printf(seq, " %10s %pM %4i.%03is\n",
1937 hardif_neigh->if_incoming->net_dev->name,
1938 hardif_neigh->addr, last_secs, last_msecs);
1939}
1940
1941/**
1942 * batadv_iv_ogm_neigh_print - print the single hop neighbour list
1943 * @bat_priv: the bat priv with all the soft interface information
1944 * @seq: neighbour table seq_file struct
1945 */
1946static void batadv_iv_neigh_print(struct batadv_priv *bat_priv,
1947 struct seq_file *seq)
1948{
1949 struct net_device *net_dev = (struct net_device *)seq->private;
1950 struct batadv_hardif_neigh_node *hardif_neigh;
1951 struct batadv_hard_iface *hard_iface;
1952 int batman_count = 0;
1953
Antonio Quartulli925a6f32016-03-12 10:30:18 +01001954 seq_puts(seq, " IF Neighbor last-seen\n");
Marek Lindner75874052015-08-04 21:09:57 +08001955
1956 rcu_read_lock();
1957 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1958 if (hard_iface->soft_iface != net_dev)
1959 continue;
1960
1961 hlist_for_each_entry_rcu(hardif_neigh,
1962 &hard_iface->neigh_list, list) {
1963 batadv_iv_hardif_neigh_print(seq, hardif_neigh);
1964 batman_count++;
1965 }
1966 }
1967 rcu_read_unlock();
1968
1969 if (batman_count == 0)
1970 seq_puts(seq, "No batman nodes in range ...\n");
1971}
1972
1973/**
Antonio Quartullia3285a82013-09-02 12:15:04 +02001974 * batadv_iv_ogm_neigh_cmp - compare the metrics of two neighbors
1975 * @neigh1: the first neighbor object of the comparison
Simon Wunderlich89652332013-11-13 19:14:46 +01001976 * @if_outgoing1: outgoing interface for the first neighbor
Antonio Quartullia3285a82013-09-02 12:15:04 +02001977 * @neigh2: the second neighbor object of the comparison
Simon Wunderlich89652332013-11-13 19:14:46 +01001978 * @if_outgoing2: outgoing interface for the second neighbor
Antonio Quartullia3285a82013-09-02 12:15:04 +02001979 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001980 * Return: a value less, equal to or greater than 0 if the metric via neigh1 is
Antonio Quartullia3285a82013-09-02 12:15:04 +02001981 * lower, the same as or higher than the metric via neigh2
1982 */
1983static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1,
Simon Wunderlich89652332013-11-13 19:14:46 +01001984 struct batadv_hard_iface *if_outgoing1,
1985 struct batadv_neigh_node *neigh2,
1986 struct batadv_hard_iface *if_outgoing2)
Antonio Quartullia3285a82013-09-02 12:15:04 +02001987{
Simon Wunderlich89652332013-11-13 19:14:46 +01001988 struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001989 u8 tq1, tq2;
Simon Wunderlich89652332013-11-13 19:14:46 +01001990 int diff;
Antonio Quartullia3285a82013-09-02 12:15:04 +02001991
Simon Wunderlich89652332013-11-13 19:14:46 +01001992 neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
1993 neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
Antonio Quartullia3285a82013-09-02 12:15:04 +02001994
Simon Wunderlich89652332013-11-13 19:14:46 +01001995 if (!neigh1_ifinfo || !neigh2_ifinfo) {
1996 diff = 0;
1997 goto out;
1998 }
1999
2000 tq1 = neigh1_ifinfo->bat_iv.tq_avg;
2001 tq2 = neigh2_ifinfo->bat_iv.tq_avg;
2002 diff = tq1 - tq2;
2003
2004out:
2005 if (neigh1_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01002006 batadv_neigh_ifinfo_put(neigh1_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01002007 if (neigh2_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01002008 batadv_neigh_ifinfo_put(neigh2_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01002009
2010 return diff;
Antonio Quartullia3285a82013-09-02 12:15:04 +02002011}
2012
Antonio Quartullic43c9812013-09-02 12:15:05 +02002013/**
Simon Wunderlich18165f62015-08-08 02:01:50 +02002014 * batadv_iv_ogm_neigh_is_sob - check if neigh1 is similarly good or better
2015 * than neigh2 from the metric prospective
Antonio Quartullic43c9812013-09-02 12:15:05 +02002016 * @neigh1: the first neighbor object of the comparison
Martin Hundebøll95298a92014-07-15 09:41:05 +02002017 * @if_outgoing1: outgoing interface for the first neighbor
Antonio Quartullic43c9812013-09-02 12:15:05 +02002018 * @neigh2: the second neighbor object of the comparison
Simon Wunderlich89652332013-11-13 19:14:46 +01002019 * @if_outgoing2: outgoing interface for the second neighbor
Martin Hundebøll95298a92014-07-15 09:41:05 +02002020 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002021 * Return: true if the metric via neigh1 is equally good or better than
Simon Wunderlich89652332013-11-13 19:14:46 +01002022 * the metric via neigh2, false otherwise.
2023 */
2024static bool
Simon Wunderlich18165f62015-08-08 02:01:50 +02002025batadv_iv_ogm_neigh_is_sob(struct batadv_neigh_node *neigh1,
Simon Wunderlich89652332013-11-13 19:14:46 +01002026 struct batadv_hard_iface *if_outgoing1,
2027 struct batadv_neigh_node *neigh2,
2028 struct batadv_hard_iface *if_outgoing2)
2029{
2030 struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002031 u8 tq1, tq2;
Simon Wunderlich89652332013-11-13 19:14:46 +01002032 bool ret;
2033
2034 neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
2035 neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
2036
2037 /* we can't say that the metric is better */
2038 if (!neigh1_ifinfo || !neigh2_ifinfo) {
2039 ret = false;
2040 goto out;
2041 }
2042
2043 tq1 = neigh1_ifinfo->bat_iv.tq_avg;
2044 tq2 = neigh2_ifinfo->bat_iv.tq_avg;
2045 ret = (tq1 - tq2) > -BATADV_TQ_SIMILARITY_THRESHOLD;
2046
2047out:
2048 if (neigh1_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01002049 batadv_neigh_ifinfo_put(neigh1_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01002050 if (neigh2_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01002051 batadv_neigh_ifinfo_put(neigh2_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01002052
2053 return ret;
Antonio Quartullic43c9812013-09-02 12:15:05 +02002054}
2055
Sven Eckelmann56303d32012-06-05 22:31:31 +02002056static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
Marek Lindner519d3492012-04-18 17:15:57 +08002057 .name = "BATMAN_IV",
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02002058 .bat_iface_enable = batadv_iv_ogm_iface_enable,
2059 .bat_iface_disable = batadv_iv_ogm_iface_disable,
2060 .bat_iface_update_mac = batadv_iv_ogm_iface_update_mac,
2061 .bat_primary_iface_set = batadv_iv_ogm_primary_iface_set,
2062 .bat_ogm_schedule = batadv_iv_ogm_schedule,
2063 .bat_ogm_emit = batadv_iv_ogm_emit,
Antonio Quartullia3285a82013-09-02 12:15:04 +02002064 .bat_neigh_cmp = batadv_iv_ogm_neigh_cmp,
Simon Wunderlich18165f62015-08-08 02:01:50 +02002065 .bat_neigh_is_similar_or_better = batadv_iv_ogm_neigh_is_sob,
Marek Lindner75874052015-08-04 21:09:57 +08002066 .bat_neigh_print = batadv_iv_neigh_print,
Antonio Quartulli737a2a222013-09-02 12:15:03 +02002067 .bat_orig_print = batadv_iv_ogm_orig_print,
Antonio Quartullid0015fd2013-09-03 11:10:23 +02002068 .bat_orig_free = batadv_iv_ogm_orig_free,
2069 .bat_orig_add_if = batadv_iv_ogm_orig_add_if,
2070 .bat_orig_del_if = batadv_iv_ogm_orig_del_if,
Marek Lindner1c280472011-11-28 17:40:17 +08002071};
2072
Sven Eckelmann81c524f2012-05-12 02:09:22 +02002073int __init batadv_iv_init(void)
Marek Lindner1c280472011-11-28 17:40:17 +08002074{
Marek Lindnerc3e29312012-03-04 16:56:25 +08002075 int ret;
2076
2077 /* batman originator packet */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002078 ret = batadv_recv_handler_register(BATADV_IV_OGM,
2079 batadv_iv_ogm_receive);
Marek Lindnerc3e29312012-03-04 16:56:25 +08002080 if (ret < 0)
2081 goto out;
2082
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02002083 ret = batadv_algo_register(&batadv_batman_iv);
Marek Lindnerc3e29312012-03-04 16:56:25 +08002084 if (ret < 0)
2085 goto handler_unregister;
2086
2087 goto out;
2088
2089handler_unregister:
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002090 batadv_recv_handler_unregister(BATADV_IV_OGM);
Marek Lindnerc3e29312012-03-04 16:56:25 +08002091out:
2092 return ret;
Marek Lindner1c280472011-11-28 17:40:17 +08002093}