blob: 57e9962c7090a7925a6d373c5edfb4b6a7ee8d96 [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);
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200160 if (!data_ptr) {
161 kfree(orig_node->bat_iv.bcast_own);
162 goto unlock;
163 }
164
165 memcpy(data_ptr, orig_node->bat_iv.bcast_own_sum,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200166 (max_if_num - 1) * sizeof(u8));
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200167 kfree(orig_node->bat_iv.bcast_own_sum);
168 orig_node->bat_iv.bcast_own_sum = data_ptr;
169
170 ret = 0;
171
172unlock:
173 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
174
175 return ret;
176}
177
178/**
Sven Eckelmann64ae74452016-02-22 22:56:34 +0100179 * batadv_iv_ogm_drop_bcast_own_entry - drop section of bcast_own
180 * @orig_node: the orig_node that has to be changed
181 * @max_if_num: the current amount of interfaces
182 * @del_if_num: the index of the interface being removed
183 */
184static void
185batadv_iv_ogm_drop_bcast_own_entry(struct batadv_orig_node *orig_node,
186 int max_if_num, int del_if_num)
187{
188 size_t chunk_size;
189 size_t if_offset;
190 void *data_ptr;
191
192 lockdep_assert_held(&orig_node->bat_iv.ogm_cnt_lock);
193
194 chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS;
195 data_ptr = kmalloc_array(max_if_num, chunk_size, GFP_ATOMIC);
196 if (!data_ptr)
197 /* use old buffer when new one could not be allocated */
198 data_ptr = orig_node->bat_iv.bcast_own;
199
200 /* copy first part */
201 memmove(data_ptr, orig_node->bat_iv.bcast_own, del_if_num * chunk_size);
202
203 /* copy second part */
204 if_offset = (del_if_num + 1) * chunk_size;
205 memmove((char *)data_ptr + del_if_num * chunk_size,
206 (uint8_t *)orig_node->bat_iv.bcast_own + if_offset,
207 (max_if_num - del_if_num) * chunk_size);
208
209 /* bcast_own was shrunk down in new buffer; free old one */
210 if (orig_node->bat_iv.bcast_own != data_ptr) {
211 kfree(orig_node->bat_iv.bcast_own);
212 orig_node->bat_iv.bcast_own = data_ptr;
213 }
214}
215
216/**
217 * batadv_iv_ogm_drop_bcast_own_sum_entry - drop section of bcast_own_sum
218 * @orig_node: the orig_node that has to be changed
219 * @max_if_num: the current amount of interfaces
220 * @del_if_num: the index of the interface being removed
221 */
222static void
223batadv_iv_ogm_drop_bcast_own_sum_entry(struct batadv_orig_node *orig_node,
224 int max_if_num, int del_if_num)
225{
226 size_t if_offset;
227 void *data_ptr;
228
229 lockdep_assert_held(&orig_node->bat_iv.ogm_cnt_lock);
230
231 data_ptr = kmalloc_array(max_if_num, sizeof(u8), GFP_ATOMIC);
232 if (!data_ptr)
233 /* use old buffer when new one could not be allocated */
234 data_ptr = orig_node->bat_iv.bcast_own_sum;
235
236 memmove(data_ptr, orig_node->bat_iv.bcast_own_sum,
237 del_if_num * sizeof(u8));
238
239 if_offset = (del_if_num + 1) * sizeof(u8);
240 memmove((char *)data_ptr + del_if_num * sizeof(u8),
241 orig_node->bat_iv.bcast_own_sum + if_offset,
242 (max_if_num - del_if_num) * sizeof(u8));
243
244 /* bcast_own_sum was shrunk down in new buffer; free old one */
245 if (orig_node->bat_iv.bcast_own_sum != data_ptr) {
246 kfree(orig_node->bat_iv.bcast_own_sum);
247 orig_node->bat_iv.bcast_own_sum = data_ptr;
248 }
249}
250
251/**
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200252 * batadv_iv_ogm_orig_del_if - change the private structures of the orig_node to
253 * exclude the removed interface
254 * @orig_node: the orig_node that has to be changed
255 * @max_if_num: the current amount of interfaces
256 * @del_if_num: the index of the interface being removed
257 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200258 * Return: 0 on success, a negative error code otherwise.
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200259 */
260static int batadv_iv_ogm_orig_del_if(struct batadv_orig_node *orig_node,
261 int max_if_num, int del_if_num)
262{
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200263 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
264
Sven Eckelmann64ae74452016-02-22 22:56:34 +0100265 if (max_if_num == 0) {
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200266 kfree(orig_node->bat_iv.bcast_own);
Sven Eckelmann64ae74452016-02-22 22:56:34 +0100267 kfree(orig_node->bat_iv.bcast_own_sum);
268 orig_node->bat_iv.bcast_own = NULL;
269 orig_node->bat_iv.bcast_own_sum = NULL;
270 } else {
271 batadv_iv_ogm_drop_bcast_own_entry(orig_node, max_if_num,
272 del_if_num);
273 batadv_iv_ogm_drop_bcast_own_sum_entry(orig_node, max_if_num,
274 del_if_num);
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200275 }
276
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200277 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
278
Sven Eckelmann64ae74452016-02-22 22:56:34 +0100279 return 0;
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200280}
281
282/**
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200283 * batadv_iv_ogm_orig_get - retrieve or create (if does not exist) an originator
284 * @bat_priv: the bat priv with all the soft interface information
285 * @addr: mac address of the originator
286 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200287 * Return: the originator object corresponding to the passed mac address or NULL
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200288 * on failure.
289 * If the object does not exists it is created an initialised.
290 */
291static struct batadv_orig_node *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200292batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const u8 *addr)
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200293{
294 struct batadv_orig_node *orig_node;
295 int size, hash_added;
296
297 orig_node = batadv_orig_hash_find(bat_priv, addr);
298 if (orig_node)
299 return orig_node;
300
301 orig_node = batadv_orig_node_new(bat_priv, addr);
302 if (!orig_node)
303 return NULL;
304
305 spin_lock_init(&orig_node->bat_iv.ogm_cnt_lock);
306
307 size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS;
308 orig_node->bat_iv.bcast_own = kzalloc(size, GFP_ATOMIC);
309 if (!orig_node->bat_iv.bcast_own)
310 goto free_orig_node;
311
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200312 size = bat_priv->num_ifaces * sizeof(u8);
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200313 orig_node->bat_iv.bcast_own_sum = kzalloc(size, GFP_ATOMIC);
314 if (!orig_node->bat_iv.bcast_own_sum)
Antonio Quartullia5a5cb82014-02-15 02:17:20 +0100315 goto free_orig_node;
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200316
317 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
318 batadv_choose_orig, orig_node,
319 &orig_node->hash_entry);
320 if (hash_added != 0)
Antonio Quartullia5a5cb82014-02-15 02:17:20 +0100321 goto free_orig_node;
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200322
323 return orig_node;
324
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200325free_orig_node:
Simon Wunderlichb2262df2014-02-08 16:45:06 +0100326 /* free twice, as batadv_orig_node_new sets refcount to 2 */
Sven Eckelmann5d967312016-01-17 11:01:09 +0100327 batadv_orig_node_put(orig_node);
328 batadv_orig_node_put(orig_node);
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200329
330 return NULL;
331}
332
Sven Eckelmann56303d32012-06-05 22:31:31 +0200333static struct batadv_neigh_node *
334batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200335 const u8 *neigh_addr,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200336 struct batadv_orig_node *orig_node,
Antonio Quartulli863dd7a2013-03-25 13:49:46 +0100337 struct batadv_orig_node *orig_neigh)
Marek Lindner7ae8b282012-03-01 15:35:21 +0800338{
Marek Lindner741aa062015-07-26 04:57:43 +0800339 struct batadv_neigh_node *neigh_node;
Marek Lindner7ae8b282012-03-01 15:35:21 +0800340
Marek Lindner3f32f8a2015-07-26 04:59:15 +0800341 neigh_node = batadv_neigh_node_new(orig_node, hard_iface, neigh_addr);
Marek Lindner7ae8b282012-03-01 15:35:21 +0800342 if (!neigh_node)
343 goto out;
344
Simon Wunderlich89652332013-11-13 19:14:46 +0100345 neigh_node->orig_node = orig_neigh;
Marek Lindner7ae8b282012-03-01 15:35:21 +0800346
Marek Lindner7ae8b282012-03-01 15:35:21 +0800347out:
348 return neigh_node;
349}
350
Sven Eckelmann56303d32012-06-05 22:31:31 +0200351static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200352{
Sven Eckelmann96412692012-06-05 22:31:30 +0200353 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindner14511512012-08-02 17:20:26 +0200354 unsigned char *ogm_buff;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200355 u32 random_seqno;
Marek Lindnerd7d32ec2012-02-07 17:20:46 +0800356
357 /* randomize initial seqno to avoid collision */
358 get_random_bytes(&random_seqno, sizeof(random_seqno));
Marek Lindner14511512012-08-02 17:20:26 +0200359 atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno);
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200360
Marek Lindner14511512012-08-02 17:20:26 +0200361 hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN;
362 ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC);
363 if (!ogm_buff)
Markus Pargmann42d9f2c2014-12-26 12:41:26 +0100364 return -ENOMEM;
Marek Lindner77af7572012-02-07 17:20:48 +0800365
Marek Lindner14511512012-08-02 17:20:26 +0200366 hard_iface->bat_iv.ogm_buff = ogm_buff;
367
368 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100369 batadv_ogm_packet->packet_type = BATADV_IV_OGM;
370 batadv_ogm_packet->version = BATADV_COMPAT_VERSION;
371 batadv_ogm_packet->ttl = 2;
Sven Eckelmann96412692012-06-05 22:31:30 +0200372 batadv_ogm_packet->flags = BATADV_NO_FLAGS;
Marek Lindner414254e2013-04-23 21:39:58 +0800373 batadv_ogm_packet->reserved = 0;
Sven Eckelmann96412692012-06-05 22:31:30 +0200374 batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE;
Marek Lindner77af7572012-02-07 17:20:48 +0800375
Markus Pargmann42d9f2c2014-12-26 12:41:26 +0100376 return 0;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200377}
378
Sven Eckelmann56303d32012-06-05 22:31:31 +0200379static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
Marek Lindner00a50072012-02-07 17:20:47 +0800380{
Marek Lindner14511512012-08-02 17:20:26 +0200381 kfree(hard_iface->bat_iv.ogm_buff);
382 hard_iface->bat_iv.ogm_buff = NULL;
Marek Lindner00a50072012-02-07 17:20:47 +0800383}
384
Sven Eckelmann56303d32012-06-05 22:31:31 +0200385static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface)
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200386{
Sven Eckelmann96412692012-06-05 22:31:30 +0200387 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindner14511512012-08-02 17:20:26 +0200388 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200389
Marek Lindner14511512012-08-02 17:20:26 +0200390 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100391 ether_addr_copy(batadv_ogm_packet->orig,
392 hard_iface->net_dev->dev_addr);
393 ether_addr_copy(batadv_ogm_packet->prev_sender,
394 hard_iface->net_dev->dev_addr);
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200395}
396
Sven Eckelmann56303d32012-06-05 22:31:31 +0200397static void
398batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface)
Marek Lindnerc3229392012-03-11 06:17:50 +0800399{
Sven Eckelmann96412692012-06-05 22:31:30 +0200400 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindner14511512012-08-02 17:20:26 +0200401 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
Marek Lindnerc3229392012-03-11 06:17:50 +0800402
Marek Lindner14511512012-08-02 17:20:26 +0200403 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100404 batadv_ogm_packet->ttl = BATADV_TTL;
Marek Lindnerc3229392012-03-11 06:17:50 +0800405}
406
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200407/* when do we schedule our own ogm to be sent */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200408static unsigned long
Sven Eckelmann56303d32012-06-05 22:31:31 +0200409batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200410{
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200411 unsigned int msecs;
412
413 msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
Akinobu Mitae76e4322012-12-24 11:14:07 +0900414 msecs += prandom_u32() % (2 * BATADV_JITTER);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200415
416 return jiffies + msecs_to_jiffies(msecs);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200417}
418
419/* when do we schedule a ogm packet to be sent */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200420static unsigned long batadv_iv_ogm_fwd_send_time(void)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200421{
Akinobu Mitae76e4322012-12-24 11:14:07 +0900422 return jiffies + msecs_to_jiffies(prandom_u32() % (BATADV_JITTER / 2));
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200423}
424
425/* apply hop penalty for a normal link */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200426static u8 batadv_hop_penalty(u8 tq, const struct batadv_priv *bat_priv)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200427{
428 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200429 int new_tq;
430
431 new_tq = tq * (BATADV_TQ_MAX_VALUE - hop_penalty);
432 new_tq /= BATADV_TQ_MAX_VALUE;
433
434 return new_tq;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200435}
436
Simon Wunderlich600184b2015-11-23 19:57:21 +0100437/**
438 * batadv_iv_ogm_aggr_packet - checks if there is another OGM attached
439 * @buff_pos: current position in the skb
440 * @packet_len: total length of the skb
441 * @tvlv_len: tvlv length of the previously considered OGM
442 *
443 * Return: true if there is enough space for another OGM, false otherwise.
444 */
Markus Pargmann9fd9b192014-12-26 12:41:27 +0100445static bool batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len,
446 __be16 tvlv_len)
Marek Lindnerfc957272011-07-30 12:04:12 +0200447{
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200448 int next_buff_pos = 0;
449
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200450 next_buff_pos += buff_pos + BATADV_OGM_HLEN;
Marek Lindneref261572013-04-23 21:39:57 +0800451 next_buff_pos += ntohs(tvlv_len);
Marek Lindnerfc957272011-07-30 12:04:12 +0200452
453 return (next_buff_pos <= packet_len) &&
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200454 (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
Marek Lindnerfc957272011-07-30 12:04:12 +0200455}
456
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200457/* send a batman ogm to a given interface */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200458static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet,
459 struct batadv_hard_iface *hard_iface)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200460{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200461 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Markus Pargmannde12bae2014-12-26 12:41:28 +0100462 const char *fwd_str;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200463 u8 packet_num;
464 s16 buff_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +0200465 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200466 struct sk_buff *skb;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200467 u8 *packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200468
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200469 if (hard_iface->if_status != BATADV_IF_ACTIVE)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200470 return;
471
472 packet_num = 0;
473 buff_pos = 0;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200474 packet_pos = forw_packet->skb->data;
475 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200476
477 /* adjust all flags and log packets */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200478 while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
Marek Lindneref261572013-04-23 21:39:57 +0800479 batadv_ogm_packet->tvlv_len)) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200480 /* we might have aggregated direct link packets with an
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200481 * ordinary base packet
482 */
Sven Eckelmann8de47de2012-07-08 16:32:09 +0200483 if (forw_packet->direct_link_flags & BIT(packet_num) &&
484 forw_packet->if_incoming == hard_iface)
Sven Eckelmann96412692012-06-05 22:31:30 +0200485 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200486 else
Sven Eckelmann96412692012-06-05 22:31:30 +0200487 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200488
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200489 if (packet_num > 0 || !forw_packet->own)
490 fwd_str = "Forwarding";
491 else
492 fwd_str = "Sending own";
493
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200494 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800495 "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200496 fwd_str, (packet_num > 0 ? "aggregated " : ""),
Sven Eckelmann96412692012-06-05 22:31:30 +0200497 batadv_ogm_packet->orig,
498 ntohl(batadv_ogm_packet->seqno),
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100499 batadv_ogm_packet->tq, batadv_ogm_packet->ttl,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +0200500 ((batadv_ogm_packet->flags & BATADV_DIRECTLINK) ?
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200501 "on" : "off"),
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800502 hard_iface->net_dev->name,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200503 hard_iface->net_dev->dev_addr);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200504
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200505 buff_pos += BATADV_OGM_HLEN;
Marek Lindneref261572013-04-23 21:39:57 +0800506 buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200507 packet_num++;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200508 packet_pos = forw_packet->skb->data + buff_pos;
509 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200510 }
511
512 /* create clone because function is called more than once */
513 skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
Martin Hundebøllf8214862012-04-20 17:02:45 +0200514 if (skb) {
Sven Eckelmannd69909d2012-06-03 22:19:20 +0200515 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
516 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
Martin Hundebøllf8214862012-04-20 17:02:45 +0200517 skb->len + ETH_HLEN);
Antonio Quartulli95d39272016-01-16 16:40:15 +0800518 batadv_send_broadcast_skb(skb, hard_iface);
Martin Hundebøllf8214862012-04-20 17:02:45 +0200519 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200520}
521
522/* send a batman ogm packet */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200523static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200524{
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200525 struct net_device *soft_iface;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200526 struct batadv_priv *bat_priv;
527 struct batadv_hard_iface *primary_if = NULL;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200528
529 if (!forw_packet->if_incoming) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100530 pr_err("Error - can't forward packet: incoming iface not specified\n");
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200531 goto out;
532 }
533
534 soft_iface = forw_packet->if_incoming->soft_iface;
535 bat_priv = netdev_priv(soft_iface);
536
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100537 if (WARN_ON(!forw_packet->if_outgoing))
538 goto out;
539
540 if (WARN_ON(forw_packet->if_outgoing->soft_iface != soft_iface))
541 goto out;
542
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200543 if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200544 goto out;
545
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200546 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200547 if (!primary_if)
548 goto out;
549
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100550 /* only for one specific outgoing interface */
551 batadv_iv_ogm_send_to_if(forw_packet, forw_packet->if_outgoing);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200552
553out:
554 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100555 batadv_hardif_put(primary_if);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200556}
557
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100558/**
559 * batadv_iv_ogm_can_aggregate - find out if an OGM can be aggregated on an
560 * existing forward packet
561 * @new_bat_ogm_packet: OGM packet to be aggregated
562 * @bat_priv: the bat priv with all the soft interface information
563 * @packet_len: (total) length of the OGM
564 * @send_time: timestamp (jiffies) when the packet is to be sent
Martin Hundebøll95298a92014-07-15 09:41:05 +0200565 * @directlink: true if this is a direct link packet
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100566 * @if_incoming: interface where the packet was received
567 * @if_outgoing: interface for which the retransmission should be considered
568 * @forw_packet: the forwarded packet which should be checked
569 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200570 * Return: true if new_packet can be aggregated with forw_packet
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100571 */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200572static bool
Sven Eckelmann96412692012-06-05 22:31:30 +0200573batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200574 struct batadv_priv *bat_priv,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200575 int packet_len, unsigned long send_time,
576 bool directlink,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200577 const struct batadv_hard_iface *if_incoming,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100578 const struct batadv_hard_iface *if_outgoing,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200579 const struct batadv_forw_packet *forw_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200580{
Sven Eckelmann96412692012-06-05 22:31:30 +0200581 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200582 int aggregated_bytes = forw_packet->packet_len + packet_len;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200583 struct batadv_hard_iface *primary_if = NULL;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200584 bool res = false;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200585 unsigned long aggregation_end_time;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200586
Sven Eckelmann96412692012-06-05 22:31:30 +0200587 batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200588 aggregation_end_time = send_time;
589 aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200590
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200591 /* we can aggregate the current packet to this aggregated packet
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200592 * if:
593 *
594 * - the send time is within our MAX_AGGREGATION_MS time
595 * - the resulting packet wont be bigger than
596 * MAX_AGGREGATION_BYTES
Markus Pargmann8f34b382014-12-26 12:41:29 +0100597 * otherwise aggregation is not possible
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200598 */
Markus Pargmann8f34b382014-12-26 12:41:29 +0100599 if (!time_before(send_time, forw_packet->send_time) ||
600 !time_after_eq(aggregation_end_time, forw_packet->send_time))
601 return false;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200602
Markus Pargmann8f34b382014-12-26 12:41:29 +0100603 if (aggregated_bytes > BATADV_MAX_AGGREGATION_BYTES)
604 return false;
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100605
Markus Pargmann8f34b382014-12-26 12:41:29 +0100606 /* packet is not leaving on the same interface. */
607 if (forw_packet->if_outgoing != if_outgoing)
608 return false;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200609
Markus Pargmann8f34b382014-12-26 12:41:29 +0100610 /* check aggregation compatibility
611 * -> direct link packets are broadcasted on
612 * their interface only
613 * -> aggregate packet if the current packet is
614 * a "global" packet as well as the base
615 * packet
616 */
617 primary_if = batadv_primary_if_get_selected(bat_priv);
618 if (!primary_if)
619 return false;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200620
Markus Pargmann8f34b382014-12-26 12:41:29 +0100621 /* packets without direct link flag and high TTL
622 * are flooded through the net
623 */
624 if (!directlink &&
625 !(batadv_ogm_packet->flags & BATADV_DIRECTLINK) &&
626 batadv_ogm_packet->ttl != 1 &&
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200627
Markus Pargmann8f34b382014-12-26 12:41:29 +0100628 /* own packets originating non-primary
629 * interfaces leave only that interface
630 */
631 (!forw_packet->own ||
632 forw_packet->if_incoming == primary_if)) {
633 res = true;
634 goto out;
635 }
636
637 /* if the incoming packet is sent via this one
638 * interface only - we still can aggregate
639 */
640 if (directlink &&
641 new_bat_ogm_packet->ttl == 1 &&
642 forw_packet->if_incoming == if_incoming &&
643
644 /* packets from direct neighbors or
645 * own secondary interface packets
646 * (= secondary interface packets in general)
647 */
648 (batadv_ogm_packet->flags & BATADV_DIRECTLINK ||
649 (forw_packet->own &&
650 forw_packet->if_incoming != primary_if))) {
651 res = true;
652 goto out;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200653 }
654
655out:
656 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100657 batadv_hardif_put(primary_if);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200658 return res;
659}
660
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100661/**
662 * batadv_iv_ogm_aggregate_new - create a new aggregated packet and add this
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100663 * packet to it.
664 * @packet_buff: pointer to the OGM
665 * @packet_len: (total) length of the OGM
666 * @send_time: timestamp (jiffies) when the packet is to be sent
667 * @direct_link: whether this OGM has direct link status
668 * @if_incoming: interface where the packet was received
669 * @if_outgoing: interface for which the retransmission should be considered
670 * @own_packet: true if it is a self-generated ogm
671 */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200672static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,
673 int packet_len, unsigned long send_time,
674 bool direct_link,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200675 struct batadv_hard_iface *if_incoming,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100676 struct batadv_hard_iface *if_outgoing,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200677 int own_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200678{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200679 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
680 struct batadv_forw_packet *forw_packet_aggr;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200681 unsigned char *skb_buff;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200682 unsigned int skb_size;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200683
Sven Eckelmann7a659d52016-01-16 10:29:54 +0100684 if (!kref_get_unless_zero(&if_incoming->refcount))
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200685 return;
686
Sven Eckelmann7a659d52016-01-16 10:29:54 +0100687 if (!kref_get_unless_zero(&if_outgoing->refcount))
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100688 goto out_free_incoming;
689
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200690 /* own packet should always be scheduled */
691 if (!own_packet) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200692 if (!batadv_atomic_dec_not_zero(&bat_priv->batman_queue_left)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200693 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200694 "batman packet queue full\n");
Markus Pargmann940d1562014-12-26 12:41:31 +0100695 goto out_free_outgoing;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200696 }
697 }
698
699 forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC);
Markus Pargmann940d1562014-12-26 12:41:31 +0100700 if (!forw_packet_aggr)
701 goto out_nomem;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200702
Markus Pargmann940d1562014-12-26 12:41:31 +0100703 if (atomic_read(&bat_priv->aggregated_ogms) &&
704 packet_len < BATADV_MAX_AGGREGATION_BYTES)
Sven Eckelmann5b246572012-11-04 17:11:45 +0100705 skb_size = BATADV_MAX_AGGREGATION_BYTES;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200706 else
Sven Eckelmann5b246572012-11-04 17:11:45 +0100707 skb_size = packet_len;
708
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200709 skb_size += ETH_HLEN;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200710
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200711 forw_packet_aggr->skb = netdev_alloc_skb_ip_align(NULL, skb_size);
Markus Pargmann940d1562014-12-26 12:41:31 +0100712 if (!forw_packet_aggr->skb)
713 goto out_free_forw_packet;
Simon Wunderlichc54f38c2013-07-29 17:56:44 +0200714 forw_packet_aggr->skb->priority = TC_PRIO_CONTROL;
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200715 skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200716
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200717 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
718 forw_packet_aggr->packet_len = packet_len;
719 memcpy(skb_buff, packet_buff, packet_len);
720
721 forw_packet_aggr->own = own_packet;
722 forw_packet_aggr->if_incoming = if_incoming;
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100723 forw_packet_aggr->if_outgoing = if_outgoing;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200724 forw_packet_aggr->num_packets = 0;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200725 forw_packet_aggr->direct_link_flags = BATADV_NO_FLAGS;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200726 forw_packet_aggr->send_time = send_time;
727
728 /* save packet direct link flag status */
729 if (direct_link)
730 forw_packet_aggr->direct_link_flags |= 1;
731
732 /* add new packet to packet list */
733 spin_lock_bh(&bat_priv->forw_bat_list_lock);
734 hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list);
735 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
736
737 /* start timer for this packet */
738 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
Sven Eckelmann9455e342012-05-12 02:09:37 +0200739 batadv_send_outstanding_bat_ogm_packet);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200740 queue_delayed_work(batadv_event_workqueue,
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200741 &forw_packet_aggr->delayed_work,
742 send_time - jiffies);
743
744 return;
Markus Pargmann940d1562014-12-26 12:41:31 +0100745out_free_forw_packet:
746 kfree(forw_packet_aggr);
747out_nomem:
748 if (!own_packet)
749 atomic_inc(&bat_priv->batman_queue_left);
750out_free_outgoing:
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100751 batadv_hardif_put(if_outgoing);
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100752out_free_incoming:
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100753 batadv_hardif_put(if_incoming);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200754}
755
756/* aggregate a new packet into the existing ogm packet */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200757static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200758 const unsigned char *packet_buff,
759 int packet_len, bool direct_link)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200760{
761 unsigned char *skb_buff;
Sven Eckelmann8de47de2012-07-08 16:32:09 +0200762 unsigned long new_direct_link_flag;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200763
764 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
765 memcpy(skb_buff, packet_buff, packet_len);
766 forw_packet_aggr->packet_len += packet_len;
767 forw_packet_aggr->num_packets++;
768
769 /* save packet direct link flag status */
Sven Eckelmann8de47de2012-07-08 16:32:09 +0200770 if (direct_link) {
771 new_direct_link_flag = BIT(forw_packet_aggr->num_packets);
772 forw_packet_aggr->direct_link_flags |= new_direct_link_flag;
773 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200774}
775
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100776/**
777 * batadv_iv_ogm_queue_add - queue up an OGM for transmission
778 * @bat_priv: the bat priv with all the soft interface information
779 * @packet_buff: pointer to the OGM
780 * @packet_len: (total) length of the OGM
781 * @if_incoming: interface where the packet was received
782 * @if_outgoing: interface for which the retransmission should be considered
783 * @own_packet: true if it is a self-generated ogm
784 * @send_time: timestamp (jiffies) when the packet is to be sent
785 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200786static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200787 unsigned char *packet_buff,
788 int packet_len,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200789 struct batadv_hard_iface *if_incoming,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100790 struct batadv_hard_iface *if_outgoing,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200791 int own_packet, unsigned long send_time)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200792{
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200793 /* _aggr -> pointer to the packet we want to aggregate with
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200794 * _pos -> pointer to the position in the queue
795 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200796 struct batadv_forw_packet *forw_packet_aggr = NULL;
797 struct batadv_forw_packet *forw_packet_pos = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200798 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200799 bool direct_link;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200800 unsigned long max_aggregation_jiffies;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200801
Sven Eckelmann96412692012-06-05 22:31:30 +0200802 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
Markus Pargmann56489152014-12-26 12:41:32 +0100803 direct_link = !!(batadv_ogm_packet->flags & BATADV_DIRECTLINK);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200804 max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200805
806 /* find position for the packet in the forward queue */
807 spin_lock_bh(&bat_priv->forw_bat_list_lock);
808 /* own packets are not to be aggregated */
Markus Pargmann56489152014-12-26 12:41:32 +0100809 if (atomic_read(&bat_priv->aggregated_ogms) && !own_packet) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800810 hlist_for_each_entry(forw_packet_pos,
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200811 &bat_priv->forw_bat_list, list) {
Sven Eckelmann96412692012-06-05 22:31:30 +0200812 if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200813 bat_priv, packet_len,
814 send_time, direct_link,
815 if_incoming,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100816 if_outgoing,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200817 forw_packet_pos)) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200818 forw_packet_aggr = forw_packet_pos;
819 break;
820 }
821 }
822 }
823
824 /* nothing to aggregate with - either aggregation disabled or no
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200825 * suitable aggregation packet found
826 */
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200827 if (!forw_packet_aggr) {
828 /* the following section can run without the lock */
829 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
830
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200831 /* if we could not aggregate this packet with one of the others
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200832 * we hold it back for a while, so that it might be aggregated
833 * later on
834 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200835 if (!own_packet && atomic_read(&bat_priv->aggregated_ogms))
836 send_time += max_aggregation_jiffies;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200837
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200838 batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
839 send_time, direct_link,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100840 if_incoming, if_outgoing,
841 own_packet);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200842 } else {
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200843 batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
844 packet_len, direct_link);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200845 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
846 }
847}
848
Sven Eckelmann56303d32012-06-05 22:31:31 +0200849static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200850 const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +0200851 struct batadv_ogm_packet *batadv_ogm_packet,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200852 bool is_single_hop_neigh,
853 bool is_from_best_next_hop,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100854 struct batadv_hard_iface *if_incoming,
855 struct batadv_hard_iface *if_outgoing)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200856{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200857 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200858 u16 tvlv_len;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200859
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100860 if (batadv_ogm_packet->ttl <= 1) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200861 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200862 return;
863 }
864
Marek Lindner13b25412012-03-11 06:17:53 +0800865 if (!is_from_best_next_hop) {
866 /* Mark the forwarded packet when it is not coming from our
867 * best next hop. We still need to forward the packet for our
868 * neighbor link quality detection to work in case the packet
869 * originated from a single hop neighbor. Otherwise we can
870 * simply drop the ogm.
871 */
872 if (is_single_hop_neigh)
Sven Eckelmann96412692012-06-05 22:31:30 +0200873 batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP;
Marek Lindner13b25412012-03-11 06:17:53 +0800874 else
875 return;
876 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200877
Marek Lindneref261572013-04-23 21:39:57 +0800878 tvlv_len = ntohs(batadv_ogm_packet->tvlv_len);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200879
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100880 batadv_ogm_packet->ttl--;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100881 ether_addr_copy(batadv_ogm_packet->prev_sender, ethhdr->h_source);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200882
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200883 /* apply hop penalty */
Sven Eckelmann96412692012-06-05 22:31:30 +0200884 batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200885 bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200886
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200887 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200888 "Forwarding packet: tq: %i, ttl: %i\n",
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100889 batadv_ogm_packet->tq, batadv_ogm_packet->ttl);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200890
Marek Lindner75cd33f2012-03-01 15:35:16 +0800891 if (is_single_hop_neigh)
Sven Eckelmann96412692012-06-05 22:31:30 +0200892 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200893 else
Sven Eckelmann96412692012-06-05 22:31:30 +0200894 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200895
Sven Eckelmann96412692012-06-05 22:31:30 +0200896 batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
Marek Lindneref261572013-04-23 21:39:57 +0800897 BATADV_OGM_HLEN + tvlv_len,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100898 if_incoming, if_outgoing, 0,
899 batadv_iv_ogm_fwd_send_time());
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200900}
901
Antonio Quartullid9896612013-04-17 17:44:43 +0200902/**
903 * batadv_iv_ogm_slide_own_bcast_window - bitshift own OGM broadcast windows for
904 * the given interface
905 * @hard_iface: the interface for which the windows have to be shifted
906 */
907static void
908batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
909{
910 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
911 struct batadv_hashtable *hash = bat_priv->orig_hash;
912 struct hlist_head *head;
913 struct batadv_orig_node *orig_node;
914 unsigned long *word;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200915 u32 i;
Antonio Quartullid9896612013-04-17 17:44:43 +0200916 size_t word_index;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200917 u8 *w;
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200918 int if_num;
Antonio Quartullid9896612013-04-17 17:44:43 +0200919
920 for (i = 0; i < hash->size; i++) {
921 head = &hash->table[i];
922
923 rcu_read_lock();
924 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200925 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Antonio Quartullid9896612013-04-17 17:44:43 +0200926 word_index = hard_iface->if_num * BATADV_NUM_WORDS;
Antonio Quartulli32db6aa2014-09-01 14:37:29 +0200927 word = &orig_node->bat_iv.bcast_own[word_index];
Antonio Quartullid9896612013-04-17 17:44:43 +0200928
929 batadv_bit_get_packet(bat_priv, word, 1, 0);
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200930 if_num = hard_iface->if_num;
931 w = &orig_node->bat_iv.bcast_own_sum[if_num];
Antonio Quartullid9896612013-04-17 17:44:43 +0200932 *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200933 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Antonio Quartullid9896612013-04-17 17:44:43 +0200934 }
935 rcu_read_unlock();
936 }
937}
938
Sven Eckelmann56303d32012-06-05 22:31:31 +0200939static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200940{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200941 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindner14511512012-08-02 17:20:26 +0200942 unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff;
Sven Eckelmann96412692012-06-05 22:31:30 +0200943 struct batadv_ogm_packet *batadv_ogm_packet;
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100944 struct batadv_hard_iface *primary_if, *tmp_hard_iface;
Marek Lindner14511512012-08-02 17:20:26 +0200945 int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200946 u32 seqno;
947 u16 tvlv_len = 0;
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100948 unsigned long send_time;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200949
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200950 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200951
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800952 if (hard_iface == primary_if) {
953 /* tt changes have to be committed before the tvlv data is
954 * appended as it may alter the tt tvlv container
955 */
956 batadv_tt_local_commit_changes(bat_priv);
Marek Lindneref261572013-04-23 21:39:57 +0800957 tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff,
958 ogm_buff_len,
959 BATADV_OGM_HLEN);
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800960 }
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800961
Marek Lindner14511512012-08-02 17:20:26 +0200962 batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff);
Marek Lindneref261572013-04-23 21:39:57 +0800963 batadv_ogm_packet->tvlv_len = htons(tvlv_len);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200964
965 /* change sequence number to network order */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200966 seqno = (u32)atomic_read(&hard_iface->bat_iv.ogm_seqno);
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200967 batadv_ogm_packet->seqno = htonl(seqno);
Marek Lindner14511512012-08-02 17:20:26 +0200968 atomic_inc(&hard_iface->bat_iv.ogm_seqno);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200969
Antonio Quartullid9896612013-04-17 17:44:43 +0200970 batadv_iv_ogm_slide_own_bcast_window(hard_iface);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200971
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100972 send_time = batadv_iv_ogm_emit_send_time(bat_priv);
973
974 if (hard_iface != primary_if) {
975 /* OGMs from secondary interfaces are only scheduled on their
976 * respective interfaces.
977 */
978 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff, *ogm_buff_len,
979 hard_iface, hard_iface, 1, send_time);
980 goto out;
981 }
982
983 /* OGMs from primary interfaces are scheduled on all
984 * interfaces.
985 */
986 rcu_read_lock();
987 list_for_each_entry_rcu(tmp_hard_iface, &batadv_hardif_list, list) {
988 if (tmp_hard_iface->soft_iface != hard_iface->soft_iface)
Sven Eckelmann87b40f52015-07-17 10:03:42 +0200989 continue;
Sven Eckelmann27353442016-03-05 16:09:16 +0100990
991 if (!kref_get_unless_zero(&tmp_hard_iface->refcount))
992 continue;
993
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100994 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff,
995 *ogm_buff_len, hard_iface,
996 tmp_hard_iface, 1, send_time);
Sven Eckelmann27353442016-03-05 16:09:16 +0100997
998 batadv_hardif_put(tmp_hard_iface);
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100999 }
1000 rcu_read_unlock();
1001
1002out:
Marek Lindnerb9dacc52011-08-03 09:09:30 +02001003 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001004 batadv_hardif_put(primary_if);
Marek Lindnerb9dacc52011-08-03 09:09:30 +02001005}
1006
Simon Wunderlich89652332013-11-13 19:14:46 +01001007/**
1008 * batadv_iv_ogm_orig_update - use OGM to update corresponding data in an
1009 * originator
1010 * @bat_priv: the bat priv with all the soft interface information
1011 * @orig_node: the orig node who originally emitted the ogm packet
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001012 * @orig_ifinfo: ifinfo for the outgoing interface of the orig_node
Simon Wunderlich89652332013-11-13 19:14:46 +01001013 * @ethhdr: Ethernet header of the OGM
1014 * @batadv_ogm_packet: the ogm packet
1015 * @if_incoming: interface where the packet was received
1016 * @if_outgoing: interface for which the retransmission should be considered
Simon Wunderlich89652332013-11-13 19:14:46 +01001017 * @dup_status: the duplicate status of this ogm packet.
1018 */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001019static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001020batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
1021 struct batadv_orig_node *orig_node,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001022 struct batadv_orig_ifinfo *orig_ifinfo,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001023 const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +02001024 const struct batadv_ogm_packet *batadv_ogm_packet,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001025 struct batadv_hard_iface *if_incoming,
Simon Wunderlich89652332013-11-13 19:14:46 +01001026 struct batadv_hard_iface *if_outgoing,
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001027 enum batadv_dup_status dup_status)
Marek Lindnerfc957272011-07-30 12:04:12 +02001028{
Simon Wunderlich89652332013-11-13 19:14:46 +01001029 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
1030 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
Sven Eckelmann4f248cf2015-06-09 20:50:49 +02001031 struct batadv_neigh_node *neigh_node = NULL;
1032 struct batadv_neigh_node *tmp_neigh_node = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001033 struct batadv_neigh_node *router = NULL;
1034 struct batadv_orig_node *orig_node_tmp;
Linus Lüssing7caf69f2012-09-18 03:01:08 +02001035 int if_num;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001036 u8 sum_orig, sum_neigh;
1037 u8 *neigh_addr;
1038 u8 tq_avg;
Marek Lindnerfc957272011-07-30 12:04:12 +02001039
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001040 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001041 "update_originator(): Searching and updating originator entry of received packet\n");
Marek Lindnerfc957272011-07-30 12:04:12 +02001042
1043 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001044 hlist_for_each_entry_rcu(tmp_neigh_node,
Marek Lindnerfc957272011-07-30 12:04:12 +02001045 &orig_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001046 neigh_addr = tmp_neigh_node->addr;
1047 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
1048 tmp_neigh_node->if_incoming == if_incoming &&
Sven Eckelmann77ae32e2016-01-16 10:29:53 +01001049 kref_get_unless_zero(&tmp_neigh_node->refcount)) {
Antonio Quartulli38dc40e2013-03-30 17:22:00 +01001050 if (WARN(neigh_node, "too many matching neigh_nodes"))
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001051 batadv_neigh_node_put(neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001052 neigh_node = tmp_neigh_node;
1053 continue;
1054 }
1055
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001056 if (dup_status != BATADV_NO_DUP)
Marek Lindnerfc957272011-07-30 12:04:12 +02001057 continue;
1058
Simon Wunderlich89652332013-11-13 19:14:46 +01001059 /* only update the entry for this outgoing interface */
1060 neigh_ifinfo = batadv_neigh_ifinfo_get(tmp_neigh_node,
1061 if_outgoing);
1062 if (!neigh_ifinfo)
1063 continue;
1064
1065 spin_lock_bh(&tmp_neigh_node->ifinfo_lock);
1066 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
1067 &neigh_ifinfo->bat_iv.tq_index, 0);
1068 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
1069 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
1070 spin_unlock_bh(&tmp_neigh_node->ifinfo_lock);
1071
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001072 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01001073 neigh_ifinfo = NULL;
Marek Lindnerfc957272011-07-30 12:04:12 +02001074 }
1075
1076 if (!neigh_node) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001077 struct batadv_orig_node *orig_tmp;
Marek Lindnerfc957272011-07-30 12:04:12 +02001078
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001079 orig_tmp = batadv_iv_ogm_orig_get(bat_priv, ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001080 if (!orig_tmp)
1081 goto unlock;
1082
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001083 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1084 ethhdr->h_source,
Antonio Quartulli863dd7a2013-03-25 13:49:46 +01001085 orig_node, orig_tmp);
Marek Lindnerfc957272011-07-30 12:04:12 +02001086
Sven Eckelmann5d967312016-01-17 11:01:09 +01001087 batadv_orig_node_put(orig_tmp);
Marek Lindnerfc957272011-07-30 12:04:12 +02001088 if (!neigh_node)
1089 goto unlock;
Markus Pargmann23badd62014-12-26 12:41:33 +01001090 } else {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001091 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001092 "Updating existing last-hop neighbor of originator\n");
Markus Pargmann23badd62014-12-26 12:41:33 +01001093 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001094
1095 rcu_read_unlock();
Simon Wunderlich89652332013-11-13 19:14:46 +01001096 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
1097 if (!neigh_ifinfo)
1098 goto out;
Marek Lindnerfc957272011-07-30 12:04:12 +02001099
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001100 neigh_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +02001101
Simon Wunderlich89652332013-11-13 19:14:46 +01001102 spin_lock_bh(&neigh_node->ifinfo_lock);
1103 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
1104 &neigh_ifinfo->bat_iv.tq_index,
Sven Eckelmann96412692012-06-05 22:31:30 +02001105 batadv_ogm_packet->tq);
Simon Wunderlich89652332013-11-13 19:14:46 +01001106 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
1107 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
1108 spin_unlock_bh(&neigh_node->ifinfo_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001109
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001110 if (dup_status == BATADV_NO_DUP) {
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001111 orig_ifinfo->last_ttl = batadv_ogm_packet->ttl;
Simon Wunderlich89652332013-11-13 19:14:46 +01001112 neigh_ifinfo->last_ttl = batadv_ogm_packet->ttl;
Marek Lindnerfc957272011-07-30 12:04:12 +02001113 }
1114
Marek Lindnerfc957272011-07-30 12:04:12 +02001115 /* if this neighbor already is our next hop there is nothing
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001116 * to change
1117 */
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001118 router = batadv_orig_router_get(orig_node, if_outgoing);
Marek Lindnerfc957272011-07-30 12:04:12 +02001119 if (router == neigh_node)
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001120 goto out;
Marek Lindnerfc957272011-07-30 12:04:12 +02001121
Simon Wunderlich89652332013-11-13 19:14:46 +01001122 if (router) {
1123 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1124 if (!router_ifinfo)
1125 goto out;
1126
1127 /* if this neighbor does not offer a better TQ we won't
1128 * consider it
1129 */
1130 if (router_ifinfo->bat_iv.tq_avg > neigh_ifinfo->bat_iv.tq_avg)
1131 goto out;
1132 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001133
1134 /* if the TQ is the same and the link not more symmetric we
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001135 * won't consider it either
1136 */
Simon Wunderlich89652332013-11-13 19:14:46 +01001137 if (router_ifinfo &&
Markus Pargmann01b97a32014-12-26 12:41:30 +01001138 neigh_ifinfo->bat_iv.tq_avg == router_ifinfo->bat_iv.tq_avg) {
Marek Lindnerfc957272011-07-30 12:04:12 +02001139 orig_node_tmp = router->orig_node;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001140 spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
Linus Lüssing7caf69f2012-09-18 03:01:08 +02001141 if_num = router->if_incoming->if_num;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001142 sum_orig = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
1143 spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001144
1145 orig_node_tmp = neigh_node->orig_node;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001146 spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
Linus Lüssing7caf69f2012-09-18 03:01:08 +02001147 if_num = neigh_node->if_incoming->if_num;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001148 sum_neigh = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
1149 spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001150
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001151 if (sum_orig >= sum_neigh)
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001152 goto out;
Marek Lindnerfc957272011-07-30 12:04:12 +02001153 }
1154
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001155 batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001156 goto out;
1157
1158unlock:
1159 rcu_read_unlock();
1160out:
1161 if (neigh_node)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001162 batadv_neigh_node_put(neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001163 if (router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001164 batadv_neigh_node_put(router);
Simon Wunderlich89652332013-11-13 19:14:46 +01001165 if (neigh_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001166 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01001167 if (router_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001168 batadv_neigh_ifinfo_put(router_ifinfo);
Marek Lindnerfc957272011-07-30 12:04:12 +02001169}
1170
Simon Wunderlich89652332013-11-13 19:14:46 +01001171/**
1172 * batadv_iv_ogm_calc_tq - calculate tq for current received ogm packet
1173 * @orig_node: the orig node who originally emitted the ogm packet
1174 * @orig_neigh_node: the orig node struct of the neighbor who sent the packet
1175 * @batadv_ogm_packet: the ogm packet
1176 * @if_incoming: interface where the packet was received
1177 * @if_outgoing: interface for which the retransmission should be considered
1178 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001179 * Return: 1 if the link can be considered bidirectional, 0 otherwise
Simon Wunderlich89652332013-11-13 19:14:46 +01001180 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001181static int batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
1182 struct batadv_orig_node *orig_neigh_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02001183 struct batadv_ogm_packet *batadv_ogm_packet,
Simon Wunderlich89652332013-11-13 19:14:46 +01001184 struct batadv_hard_iface *if_incoming,
1185 struct batadv_hard_iface *if_outgoing)
Marek Lindnerfc957272011-07-30 12:04:12 +02001186{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001187 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1188 struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
Simon Wunderlich89652332013-11-13 19:14:46 +01001189 struct batadv_neigh_ifinfo *neigh_ifinfo;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001190 u8 total_count;
1191 u8 orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001192 unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001193 int tq_asym_penalty, inv_asym_penalty, if_num, ret = 0;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001194 unsigned int combined_tq;
Simon Wunderlichc0398762013-11-13 19:14:48 +01001195 int tq_iface_penalty;
Marek Lindnerfc957272011-07-30 12:04:12 +02001196
1197 /* find corresponding one hop neighbor */
1198 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001199 hlist_for_each_entry_rcu(tmp_neigh_node,
Marek Lindnerfc957272011-07-30 12:04:12 +02001200 &orig_neigh_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001201 if (!batadv_compare_eth(tmp_neigh_node->addr,
1202 orig_neigh_node->orig))
Marek Lindnerfc957272011-07-30 12:04:12 +02001203 continue;
1204
1205 if (tmp_neigh_node->if_incoming != if_incoming)
1206 continue;
1207
Sven Eckelmann77ae32e2016-01-16 10:29:53 +01001208 if (!kref_get_unless_zero(&tmp_neigh_node->refcount))
Marek Lindnerfc957272011-07-30 12:04:12 +02001209 continue;
1210
1211 neigh_node = tmp_neigh_node;
1212 break;
1213 }
1214 rcu_read_unlock();
1215
1216 if (!neigh_node)
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001217 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1218 orig_neigh_node->orig,
1219 orig_neigh_node,
Antonio Quartulli863dd7a2013-03-25 13:49:46 +01001220 orig_neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001221
1222 if (!neigh_node)
1223 goto out;
1224
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001225 /* if orig_node is direct neighbor update neigh_node last_seen */
Marek Lindnerfc957272011-07-30 12:04:12 +02001226 if (orig_node == orig_neigh_node)
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001227 neigh_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +02001228
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001229 orig_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +02001230
1231 /* find packet count of corresponding one hop neighbor */
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001232 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1233 if_num = if_incoming->if_num;
1234 orig_eq_count = orig_neigh_node->bat_iv.bcast_own_sum[if_num];
Simon Wunderlich89652332013-11-13 19:14:46 +01001235 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
1236 if (neigh_ifinfo) {
1237 neigh_rq_count = neigh_ifinfo->bat_iv.real_packet_count;
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001238 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01001239 } else {
1240 neigh_rq_count = 0;
1241 }
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001242 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001243
1244 /* pay attention to not get a value bigger than 100 % */
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001245 if (orig_eq_count > neigh_rq_count)
1246 total_count = neigh_rq_count;
1247 else
1248 total_count = orig_eq_count;
Marek Lindnerfc957272011-07-30 12:04:12 +02001249
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001250 /* if we have too few packets (too less data) we set tq_own to zero
1251 * if we receive too few packets it is not considered bidirectional
1252 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001253 if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
1254 neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
Marek Lindnerfc957272011-07-30 12:04:12 +02001255 tq_own = 0;
1256 else
1257 /* neigh_node->real_packet_count is never zero as we
1258 * only purge old information when getting new
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001259 * information
1260 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001261 tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count;
Marek Lindnerfc957272011-07-30 12:04:12 +02001262
Sven Eckelmann21a12362012-03-07 09:07:46 +01001263 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
Marek Lindnerfc957272011-07-30 12:04:12 +02001264 * affect the nearly-symmetric links only a little, but
1265 * punishes asymmetric links more. This will give a value
1266 * between 0 and TQ_MAX_VALUE
1267 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001268 neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
1269 neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
1270 neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
1271 BATADV_TQ_LOCAL_WINDOW_SIZE *
1272 BATADV_TQ_LOCAL_WINDOW_SIZE;
1273 inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
1274 inv_asym_penalty /= neigh_rq_max_cube;
1275 tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;
Marek Lindnerfc957272011-07-30 12:04:12 +02001276
Simon Wunderlichc0398762013-11-13 19:14:48 +01001277 /* penalize if the OGM is forwarded on the same interface. WiFi
1278 * interfaces and other half duplex devices suffer from throughput
1279 * drops as they can't send and receive at the same time.
1280 */
1281 tq_iface_penalty = BATADV_TQ_MAX_VALUE;
1282 if (if_outgoing && (if_incoming == if_outgoing) &&
1283 batadv_is_wifi_netdev(if_outgoing->net_dev))
1284 tq_iface_penalty = batadv_hop_penalty(BATADV_TQ_MAX_VALUE,
1285 bat_priv);
1286
1287 combined_tq = batadv_ogm_packet->tq *
1288 tq_own *
1289 tq_asym_penalty *
1290 tq_iface_penalty;
1291 combined_tq /= BATADV_TQ_MAX_VALUE *
1292 BATADV_TQ_MAX_VALUE *
1293 BATADV_TQ_MAX_VALUE;
Sven Eckelmann96412692012-06-05 22:31:30 +02001294 batadv_ogm_packet->tq = combined_tq;
Marek Lindnerfc957272011-07-30 12:04:12 +02001295
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001296 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Simon Wunderlichc0398762013-11-13 19:14:48 +01001297 "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 +02001298 orig_node->orig, orig_neigh_node->orig, total_count,
Simon Wunderlichc0398762013-11-13 19:14:48 +01001299 neigh_rq_count, tq_own, tq_asym_penalty, tq_iface_penalty,
1300 batadv_ogm_packet->tq, if_incoming->net_dev->name,
1301 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT");
Marek Lindnerfc957272011-07-30 12:04:12 +02001302
1303 /* if link has the minimum required transmission quality
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001304 * consider it bidirectional
1305 */
Sven Eckelmann96412692012-06-05 22:31:30 +02001306 if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
Marek Lindnerfc957272011-07-30 12:04:12 +02001307 ret = 1;
1308
1309out:
1310 if (neigh_node)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001311 batadv_neigh_node_put(neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001312 return ret;
1313}
1314
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001315/**
1316 * batadv_iv_ogm_update_seqnos - process a batman packet for all interfaces,
1317 * adjust the sequence number and find out whether it is a duplicate
1318 * @ethhdr: ethernet header of the packet
1319 * @batadv_ogm_packet: OGM packet to be considered
1320 * @if_incoming: interface on which the OGM packet was received
Simon Wunderlich89652332013-11-13 19:14:46 +01001321 * @if_outgoing: interface for which the retransmission should be considered
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001322 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001323 * Return: duplicate status as enum batadv_dup_status
Marek Lindnerfc957272011-07-30 12:04:12 +02001324 */
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001325static enum batadv_dup_status
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001326batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +02001327 const struct batadv_ogm_packet *batadv_ogm_packet,
Simon Wunderlich89652332013-11-13 19:14:46 +01001328 const struct batadv_hard_iface *if_incoming,
1329 struct batadv_hard_iface *if_outgoing)
Marek Lindnerfc957272011-07-30 12:04:12 +02001330{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001331 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1332 struct batadv_orig_node *orig_node;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001333 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
Simon Wunderlich89652332013-11-13 19:14:46 +01001334 struct batadv_neigh_node *neigh_node;
1335 struct batadv_neigh_ifinfo *neigh_ifinfo;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001336 int is_dup;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001337 s32 seq_diff;
Marek Lindnerfc957272011-07-30 12:04:12 +02001338 int need_update = 0;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001339 int set_mark;
1340 enum batadv_dup_status ret = BATADV_NO_DUP;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001341 u32 seqno = ntohl(batadv_ogm_packet->seqno);
1342 u8 *neigh_addr;
1343 u8 packet_count;
Antonio Quartulli0538f752013-09-02 12:15:01 +02001344 unsigned long *bitmap;
Marek Lindnerfc957272011-07-30 12:04:12 +02001345
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001346 orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
Marek Lindnerfc957272011-07-30 12:04:12 +02001347 if (!orig_node)
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001348 return BATADV_NO_DUP;
Marek Lindnerfc957272011-07-30 12:04:12 +02001349
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001350 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1351 if (WARN_ON(!orig_ifinfo)) {
Sven Eckelmann5d967312016-01-17 11:01:09 +01001352 batadv_orig_node_put(orig_node);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001353 return 0;
1354 }
1355
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001356 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001357 seq_diff = seqno - orig_ifinfo->last_real_seqno;
Marek Lindnerfc957272011-07-30 12:04:12 +02001358
1359 /* signalize caller that the packet is to be dropped. */
Antonio Quartulli1e5cc262012-02-26 15:39:42 +01001360 if (!hlist_empty(&orig_node->neigh_list) &&
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001361 batadv_window_protected(bat_priv, seq_diff,
Simon Wunderlich81f02682015-11-23 19:57:22 +01001362 BATADV_TQ_LOCAL_WINDOW_SIZE,
1363 &orig_ifinfo->batman_seqno_reset, NULL)) {
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001364 ret = BATADV_PROTECTED;
Marek Lindnerfc957272011-07-30 12:04:12 +02001365 goto out;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001366 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001367
1368 rcu_read_lock();
Simon Wunderlich89652332013-11-13 19:14:46 +01001369 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1370 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node,
1371 if_outgoing);
1372 if (!neigh_ifinfo)
1373 continue;
1374
1375 neigh_addr = neigh_node->addr;
1376 is_dup = batadv_test_bit(neigh_ifinfo->bat_iv.real_bits,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001377 orig_ifinfo->last_real_seqno,
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001378 seqno);
1379
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001380 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
Simon Wunderlich89652332013-11-13 19:14:46 +01001381 neigh_node->if_incoming == if_incoming) {
Marek Lindnerfc957272011-07-30 12:04:12 +02001382 set_mark = 1;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001383 if (is_dup)
1384 ret = BATADV_NEIGH_DUP;
1385 } else {
Marek Lindnerfc957272011-07-30 12:04:12 +02001386 set_mark = 0;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001387 if (is_dup && (ret != BATADV_NEIGH_DUP))
1388 ret = BATADV_ORIG_DUP;
1389 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001390
1391 /* if the window moved, set the update flag. */
Simon Wunderlich89652332013-11-13 19:14:46 +01001392 bitmap = neigh_ifinfo->bat_iv.real_bits;
Antonio Quartulli0538f752013-09-02 12:15:01 +02001393 need_update |= batadv_bit_get_packet(bat_priv, bitmap,
Sven Eckelmann0f5f9322012-05-12 02:09:25 +02001394 seq_diff, set_mark);
Marek Lindnerfc957272011-07-30 12:04:12 +02001395
Simon Wunderlich89652332013-11-13 19:14:46 +01001396 packet_count = bitmap_weight(bitmap,
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001397 BATADV_TQ_LOCAL_WINDOW_SIZE);
Simon Wunderlich89652332013-11-13 19:14:46 +01001398 neigh_ifinfo->bat_iv.real_packet_count = packet_count;
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001399 batadv_neigh_ifinfo_put(neigh_ifinfo);
Marek Lindnerfc957272011-07-30 12:04:12 +02001400 }
1401 rcu_read_unlock();
1402
1403 if (need_update) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001404 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001405 "%s updating last_seqno: old %u, new %u\n",
1406 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT",
1407 orig_ifinfo->last_real_seqno, seqno);
1408 orig_ifinfo->last_real_seqno = seqno;
Marek Lindnerfc957272011-07-30 12:04:12 +02001409 }
1410
Marek Lindnerfc957272011-07-30 12:04:12 +02001411out:
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001412 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Sven Eckelmann5d967312016-01-17 11:01:09 +01001413 batadv_orig_node_put(orig_node);
Sven Eckelmann35f94772016-01-17 11:01:13 +01001414 batadv_orig_ifinfo_put(orig_ifinfo);
Marek Lindnerfc957272011-07-30 12:04:12 +02001415 return ret;
1416}
1417
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001418/**
1419 * batadv_iv_ogm_process_per_outif - process a batman iv OGM for an outgoing if
1420 * @skb: the skb containing the OGM
Martin Hundebøll95298a92014-07-15 09:41:05 +02001421 * @ogm_offset: offset from skb->data to start of ogm header
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001422 * @orig_node: the (cached) orig node for the originator of this OGM
1423 * @if_incoming: the interface where this packet was received
1424 * @if_outgoing: the interface for which the packet should be considered
1425 */
1426static void
1427batadv_iv_ogm_process_per_outif(const struct sk_buff *skb, int ogm_offset,
1428 struct batadv_orig_node *orig_node,
1429 struct batadv_hard_iface *if_incoming,
1430 struct batadv_hard_iface *if_outgoing)
1431{
1432 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Marek Lindner4ff1e2a2015-08-04 21:09:58 +08001433 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
Sven Eckelmann4f248cf2015-06-09 20:50:49 +02001434 struct batadv_neigh_node *router = NULL;
1435 struct batadv_neigh_node *router_router = NULL;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001436 struct batadv_orig_node *orig_neigh_node;
1437 struct batadv_orig_ifinfo *orig_ifinfo;
1438 struct batadv_neigh_node *orig_neigh_router = NULL;
1439 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
1440 struct batadv_ogm_packet *ogm_packet;
1441 enum batadv_dup_status dup_status;
1442 bool is_from_best_next_hop = false;
1443 bool is_single_hop_neigh = false;
1444 bool sameseq, similar_ttl;
1445 struct sk_buff *skb_priv;
1446 struct ethhdr *ethhdr;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001447 u8 *prev_sender;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001448 int is_bidirect;
1449
1450 /* create a private copy of the skb, as some functions change tq value
1451 * and/or flags.
1452 */
1453 skb_priv = skb_copy(skb, GFP_ATOMIC);
1454 if (!skb_priv)
1455 return;
1456
1457 ethhdr = eth_hdr(skb_priv);
1458 ogm_packet = (struct batadv_ogm_packet *)(skb_priv->data + ogm_offset);
1459
1460 dup_status = batadv_iv_ogm_update_seqnos(ethhdr, ogm_packet,
1461 if_incoming, if_outgoing);
1462 if (batadv_compare_eth(ethhdr->h_source, ogm_packet->orig))
1463 is_single_hop_neigh = true;
1464
1465 if (dup_status == BATADV_PROTECTED) {
1466 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1467 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1468 ethhdr->h_source);
1469 goto out;
1470 }
1471
1472 if (ogm_packet->tq == 0) {
1473 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1474 "Drop packet: originator packet with tq equal 0\n");
1475 goto out;
1476 }
1477
Marek Lindner4ff1e2a2015-08-04 21:09:58 +08001478 if (is_single_hop_neigh) {
1479 hardif_neigh = batadv_hardif_neigh_get(if_incoming,
1480 ethhdr->h_source);
1481 if (hardif_neigh)
1482 hardif_neigh->last_seen = jiffies;
1483 }
1484
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001485 router = batadv_orig_router_get(orig_node, if_outgoing);
1486 if (router) {
1487 router_router = batadv_orig_router_get(router->orig_node,
1488 if_outgoing);
1489 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1490 }
1491
1492 if ((router_ifinfo && router_ifinfo->bat_iv.tq_avg != 0) &&
1493 (batadv_compare_eth(router->addr, ethhdr->h_source)))
1494 is_from_best_next_hop = true;
1495
1496 prev_sender = ogm_packet->prev_sender;
1497 /* avoid temporary routing loops */
1498 if (router && router_router &&
1499 (batadv_compare_eth(router->addr, prev_sender)) &&
1500 !(batadv_compare_eth(ogm_packet->orig, prev_sender)) &&
1501 (batadv_compare_eth(router->addr, router_router->addr))) {
1502 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1503 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1504 ethhdr->h_source);
1505 goto out;
1506 }
1507
1508 if (if_outgoing == BATADV_IF_DEFAULT)
1509 batadv_tvlv_ogm_receive(bat_priv, ogm_packet, orig_node);
1510
1511 /* if sender is a direct neighbor the sender mac equals
1512 * originator mac
1513 */
1514 if (is_single_hop_neigh)
1515 orig_neigh_node = orig_node;
1516 else
1517 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1518 ethhdr->h_source);
1519
1520 if (!orig_neigh_node)
1521 goto out;
1522
1523 /* Update nc_nodes of the originator */
1524 batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node,
1525 ogm_packet, is_single_hop_neigh);
1526
1527 orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
1528 if_outgoing);
1529
1530 /* drop packet if sender is not a direct neighbor and if we
1531 * don't route towards it
1532 */
1533 if (!is_single_hop_neigh && (!orig_neigh_router)) {
1534 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1535 "Drop packet: OGM via unknown neighbor!\n");
1536 goto out_neigh;
1537 }
1538
1539 is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
1540 ogm_packet, if_incoming,
1541 if_outgoing);
1542
1543 /* update ranking if it is not a duplicate or has the same
1544 * seqno and similar ttl as the non-duplicate
1545 */
1546 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1547 if (!orig_ifinfo)
1548 goto out_neigh;
1549
1550 sameseq = orig_ifinfo->last_real_seqno == ntohl(ogm_packet->seqno);
1551 similar_ttl = (orig_ifinfo->last_ttl - 3) <= ogm_packet->ttl;
1552
1553 if (is_bidirect && ((dup_status == BATADV_NO_DUP) ||
1554 (sameseq && similar_ttl))) {
1555 batadv_iv_ogm_orig_update(bat_priv, orig_node,
1556 orig_ifinfo, ethhdr,
1557 ogm_packet, if_incoming,
1558 if_outgoing, dup_status);
1559 }
Sven Eckelmann35f94772016-01-17 11:01:13 +01001560 batadv_orig_ifinfo_put(orig_ifinfo);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001561
Simon Wunderlichef0a9372013-11-13 19:14:49 +01001562 /* only forward for specific interface, not for the default one. */
1563 if (if_outgoing == BATADV_IF_DEFAULT)
1564 goto out_neigh;
1565
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001566 /* is single hop (direct) neighbor */
1567 if (is_single_hop_neigh) {
1568 /* OGMs from secondary interfaces should only scheduled once
1569 * per interface where it has been received, not multiple times
1570 */
1571 if ((ogm_packet->ttl <= 2) &&
1572 (if_incoming != if_outgoing)) {
1573 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1574 "Drop packet: OGM from secondary interface and wrong outgoing interface\n");
1575 goto out_neigh;
1576 }
1577 /* mark direct link on incoming interface */
1578 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1579 is_single_hop_neigh,
Simon Wunderlichef0a9372013-11-13 19:14:49 +01001580 is_from_best_next_hop, if_incoming,
1581 if_outgoing);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001582
1583 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1584 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
1585 goto out_neigh;
1586 }
1587
1588 /* multihop originator */
1589 if (!is_bidirect) {
1590 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1591 "Drop packet: not received via bidirectional link\n");
1592 goto out_neigh;
1593 }
1594
1595 if (dup_status == BATADV_NEIGH_DUP) {
1596 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1597 "Drop packet: duplicate packet received\n");
1598 goto out_neigh;
1599 }
1600
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001601 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1602 "Forwarding packet: rebroadcast originator packet\n");
1603 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1604 is_single_hop_neigh, is_from_best_next_hop,
Simon Wunderlichef0a9372013-11-13 19:14:49 +01001605 if_incoming, if_outgoing);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001606
1607out_neigh:
1608 if ((orig_neigh_node) && (!is_single_hop_neigh))
Sven Eckelmann5d967312016-01-17 11:01:09 +01001609 batadv_orig_node_put(orig_neigh_node);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001610out:
Simon Wunderlichc1e517f2014-03-26 15:46:21 +01001611 if (router_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001612 batadv_neigh_ifinfo_put(router_ifinfo);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001613 if (router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001614 batadv_neigh_node_put(router);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001615 if (router_router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001616 batadv_neigh_node_put(router_router);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001617 if (orig_neigh_router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001618 batadv_neigh_node_put(orig_neigh_router);
Marek Lindner4ff1e2a2015-08-04 21:09:58 +08001619 if (hardif_neigh)
Sven Eckelmannaccadc32016-01-17 11:01:14 +01001620 batadv_hardif_neigh_put(hardif_neigh);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001621
1622 kfree_skb(skb_priv);
1623}
1624
1625/**
1626 * batadv_iv_ogm_process - process an incoming batman iv OGM
1627 * @skb: the skb containing the OGM
1628 * @ogm_offset: offset to the OGM which should be processed (for aggregates)
1629 * @if_incoming: the interface where this packet was receved
1630 */
1631static void batadv_iv_ogm_process(const struct sk_buff *skb, int ogm_offset,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001632 struct batadv_hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +02001633{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001634 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001635 struct batadv_orig_node *orig_neigh_node, *orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001636 struct batadv_hard_iface *hard_iface;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001637 struct batadv_ogm_packet *ogm_packet;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001638 u32 if_incoming_seqno;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001639 bool has_directlink_flag;
1640 struct ethhdr *ethhdr;
1641 bool is_my_oldorig = false;
1642 bool is_my_addr = false;
1643 bool is_my_orig = false;
1644
1645 ogm_packet = (struct batadv_ogm_packet *)(skb->data + ogm_offset);
1646 ethhdr = eth_hdr(skb);
Marek Lindnerfc957272011-07-30 12:04:12 +02001647
1648 /* Silently drop when the batman packet is actually not a
1649 * correct packet.
1650 *
1651 * This might happen if a packet is padded (e.g. Ethernet has a
1652 * minimum frame length of 64 byte) and the aggregation interprets
1653 * it as an additional length.
1654 *
1655 * TODO: A more sane solution would be to have a bit in the
Sven Eckelmann96412692012-06-05 22:31:30 +02001656 * batadv_ogm_packet to detect whether the packet is the last
Marek Lindnerfc957272011-07-30 12:04:12 +02001657 * packet in an aggregation. Here we expect that the padding
1658 * is always zero (or not 0x01)
1659 */
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001660 if (ogm_packet->packet_type != BATADV_IV_OGM)
Marek Lindnerfc957272011-07-30 12:04:12 +02001661 return;
1662
1663 /* could be changed by schedule_own_packet() */
Marek Lindner14511512012-08-02 17:20:26 +02001664 if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno);
Marek Lindnerfc957272011-07-30 12:04:12 +02001665
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001666 if (ogm_packet->flags & BATADV_DIRECTLINK)
1667 has_directlink_flag = true;
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001668 else
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001669 has_directlink_flag = false;
Marek Lindnerfc957272011-07-30 12:04:12 +02001670
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001671 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001672 "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 +02001673 ethhdr->h_source, if_incoming->net_dev->name,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001674 if_incoming->net_dev->dev_addr, ogm_packet->orig,
1675 ogm_packet->prev_sender, ntohl(ogm_packet->seqno),
1676 ogm_packet->tq, ogm_packet->ttl,
1677 ogm_packet->version, has_directlink_flag);
Marek Lindnerfc957272011-07-30 12:04:12 +02001678
1679 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001680 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001681 if (hard_iface->if_status != BATADV_IF_ACTIVE)
Marek Lindnerfc957272011-07-30 12:04:12 +02001682 continue;
1683
1684 if (hard_iface->soft_iface != if_incoming->soft_iface)
1685 continue;
1686
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001687 if (batadv_compare_eth(ethhdr->h_source,
1688 hard_iface->net_dev->dev_addr))
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001689 is_my_addr = true;
Marek Lindnerfc957272011-07-30 12:04:12 +02001690
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001691 if (batadv_compare_eth(ogm_packet->orig,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001692 hard_iface->net_dev->dev_addr))
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001693 is_my_orig = true;
Marek Lindnerfc957272011-07-30 12:04:12 +02001694
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001695 if (batadv_compare_eth(ogm_packet->prev_sender,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001696 hard_iface->net_dev->dev_addr))
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001697 is_my_oldorig = true;
Marek Lindnerfc957272011-07-30 12:04:12 +02001698 }
1699 rcu_read_unlock();
1700
Marek Lindnerfc957272011-07-30 12:04:12 +02001701 if (is_my_addr) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001702 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001703 "Drop packet: received my own broadcast (sender: %pM)\n",
1704 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001705 return;
1706 }
1707
Marek Lindnerfc957272011-07-30 12:04:12 +02001708 if (is_my_orig) {
1709 unsigned long *word;
1710 int offset;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001711 s32 bit_pos;
1712 s16 if_num;
1713 u8 *weight;
Marek Lindnerfc957272011-07-30 12:04:12 +02001714
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001715 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1716 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001717 if (!orig_neigh_node)
1718 return;
1719
1720 /* neighbor has to indicate direct link and it has to
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001721 * come via the corresponding interface
1722 * save packet seqno for bidirectional check
1723 */
Marek Lindnerfc957272011-07-30 12:04:12 +02001724 if (has_directlink_flag &&
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001725 batadv_compare_eth(if_incoming->net_dev->dev_addr,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001726 ogm_packet->orig)) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001727 if_num = if_incoming->if_num;
1728 offset = if_num * BATADV_NUM_WORDS;
Marek Lindnerfc957272011-07-30 12:04:12 +02001729
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001730 spin_lock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
Antonio Quartulli32db6aa2014-09-01 14:37:29 +02001731 word = &orig_neigh_node->bat_iv.bcast_own[offset];
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001732 bit_pos = if_incoming_seqno - 2;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001733 bit_pos -= ntohl(ogm_packet->seqno);
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001734 batadv_set_bit(word, bit_pos);
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001735 weight = &orig_neigh_node->bat_iv.bcast_own_sum[if_num];
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001736 *weight = bitmap_weight(word,
1737 BATADV_TQ_LOCAL_WINDOW_SIZE);
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001738 spin_unlock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001739 }
1740
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: originator packet from myself (via neighbor)\n");
Sven Eckelmann5d967312016-01-17 11:01:09 +01001743 batadv_orig_node_put(orig_neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001744 return;
1745 }
1746
1747 if (is_my_oldorig) {
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 rebroadcast echos (sender: %pM)\n",
1750 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001751 return;
1752 }
1753
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001754 if (ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001755 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001756 "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1757 ethhdr->h_source);
Marek Lindner13b25412012-03-11 06:17:53 +08001758 return;
1759 }
1760
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001761 orig_node = batadv_iv_ogm_orig_get(bat_priv, ogm_packet->orig);
Marek Lindnerfc957272011-07-30 12:04:12 +02001762 if (!orig_node)
1763 return;
1764
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001765 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1766 if_incoming, BATADV_IF_DEFAULT);
Marek Lindnerfc957272011-07-30 12:04:12 +02001767
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001768 rcu_read_lock();
1769 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1770 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1771 continue;
1772
1773 if (hard_iface->soft_iface != bat_priv->soft_iface)
1774 continue;
1775
Sven Eckelmann27353442016-03-05 16:09:16 +01001776 if (!kref_get_unless_zero(&hard_iface->refcount))
1777 continue;
1778
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001779 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1780 if_incoming, hard_iface);
Sven Eckelmann27353442016-03-05 16:09:16 +01001781
1782 batadv_hardif_put(hard_iface);
Marek Lindnerfc957272011-07-30 12:04:12 +02001783 }
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001784 rcu_read_unlock();
Marek Lindnerfc957272011-07-30 12:04:12 +02001785
Sven Eckelmann5d967312016-01-17 11:01:09 +01001786 batadv_orig_node_put(orig_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001787}
1788
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001789static int batadv_iv_ogm_receive(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001790 struct batadv_hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +02001791{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001792 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001793 struct batadv_ogm_packet *ogm_packet;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001794 u8 *packet_pos;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001795 int ogm_offset;
Marek Lindneref261572013-04-23 21:39:57 +08001796 bool ret;
Marek Lindnerc3e29312012-03-04 16:56:25 +08001797
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001798 ret = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
Marek Lindnerc3e29312012-03-04 16:56:25 +08001799 if (!ret)
1800 return NET_RX_DROP;
Marek Lindnerfc957272011-07-30 12:04:12 +02001801
Marek Lindneredbf12b2012-03-11 06:17:49 +08001802 /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
1803 * that does not have B.A.T.M.A.N. IV enabled ?
1804 */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001805 if (bat_priv->bat_algo_ops->bat_ogm_emit != batadv_iv_ogm_emit)
Marek Lindneredbf12b2012-03-11 06:17:49 +08001806 return NET_RX_DROP;
1807
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001808 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
1809 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
Martin Hundebøllf8214862012-04-20 17:02:45 +02001810 skb->len + ETH_HLEN);
1811
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001812 ogm_offset = 0;
1813 ogm_packet = (struct batadv_ogm_packet *)skb->data;
Marek Lindnerfc957272011-07-30 12:04:12 +02001814
1815 /* unpack the aggregated packets and process them one by one */
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001816 while (batadv_iv_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
1817 ogm_packet->tvlv_len)) {
1818 batadv_iv_ogm_process(skb, ogm_offset, if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001819
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001820 ogm_offset += BATADV_OGM_HLEN;
1821 ogm_offset += ntohs(ogm_packet->tvlv_len);
Marek Lindnerfc957272011-07-30 12:04:12 +02001822
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001823 packet_pos = skb->data + ogm_offset;
1824 ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Marek Lindnerb47506d2013-03-04 10:39:49 +08001825 }
Marek Lindnerc3e29312012-03-04 16:56:25 +08001826
1827 kfree_skb(skb);
1828 return NET_RX_SUCCESS;
Marek Lindnerfc957272011-07-30 12:04:12 +02001829}
Marek Lindner1c280472011-11-28 17:40:17 +08001830
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001831/**
1832 * batadv_iv_ogm_orig_print_neigh - print neighbors for the originator table
Simon Wunderlich89652332013-11-13 19:14:46 +01001833 * @orig_node: the orig_node for which the neighbors are printed
1834 * @if_outgoing: outgoing interface for these entries
1835 * @seq: debugfs table seq_file struct
1836 *
1837 * Must be called while holding an rcu lock.
1838 */
1839static void
1840batadv_iv_ogm_orig_print_neigh(struct batadv_orig_node *orig_node,
1841 struct batadv_hard_iface *if_outgoing,
1842 struct seq_file *seq)
1843{
1844 struct batadv_neigh_node *neigh_node;
1845 struct batadv_neigh_ifinfo *n_ifinfo;
1846
1847 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1848 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
1849 if (!n_ifinfo)
1850 continue;
1851
1852 seq_printf(seq, " %pM (%3i)",
1853 neigh_node->addr,
1854 n_ifinfo->bat_iv.tq_avg);
1855
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001856 batadv_neigh_ifinfo_put(n_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01001857 }
1858}
1859
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001860/**
1861 * batadv_iv_ogm_orig_print - print the originator table
1862 * @bat_priv: the bat priv with all the soft interface information
1863 * @seq: debugfs table seq_file struct
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001864 * @if_outgoing: the outgoing interface for which this should be printed
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001865 */
1866static void batadv_iv_ogm_orig_print(struct batadv_priv *bat_priv,
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001867 struct seq_file *seq,
1868 struct batadv_hard_iface *if_outgoing)
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001869{
Simon Wunderlich89652332013-11-13 19:14:46 +01001870 struct batadv_neigh_node *neigh_node;
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001871 struct batadv_hashtable *hash = bat_priv->orig_hash;
1872 int last_seen_msecs, last_seen_secs;
1873 struct batadv_orig_node *orig_node;
Simon Wunderlich89652332013-11-13 19:14:46 +01001874 struct batadv_neigh_ifinfo *n_ifinfo;
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001875 unsigned long last_seen_jiffies;
1876 struct hlist_head *head;
1877 int batman_count = 0;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001878 u32 i;
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001879
Antonio Quartulli925a6f32016-03-12 10:30:18 +01001880 seq_puts(seq,
1881 " Originator last-seen (#/255) Nexthop [outgoingIF]: Potential nexthops ...\n");
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001882
1883 for (i = 0; i < hash->size; i++) {
1884 head = &hash->table[i];
1885
1886 rcu_read_lock();
1887 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001888 neigh_node = batadv_orig_router_get(orig_node,
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001889 if_outgoing);
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001890 if (!neigh_node)
1891 continue;
1892
Simon Wunderlich89652332013-11-13 19:14:46 +01001893 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node,
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001894 if_outgoing);
Simon Wunderlich89652332013-11-13 19:14:46 +01001895 if (!n_ifinfo)
1896 goto next;
1897
1898 if (n_ifinfo->bat_iv.tq_avg == 0)
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001899 goto next;
1900
1901 last_seen_jiffies = jiffies - orig_node->last_seen;
1902 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
1903 last_seen_secs = last_seen_msecs / 1000;
1904 last_seen_msecs = last_seen_msecs % 1000;
1905
1906 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
1907 orig_node->orig, last_seen_secs,
Simon Wunderlich89652332013-11-13 19:14:46 +01001908 last_seen_msecs, n_ifinfo->bat_iv.tq_avg,
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001909 neigh_node->addr,
1910 neigh_node->if_incoming->net_dev->name);
1911
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001912 batadv_iv_ogm_orig_print_neigh(orig_node, if_outgoing,
1913 seq);
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001914 seq_puts(seq, "\n");
1915 batman_count++;
1916
1917next:
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001918 batadv_neigh_node_put(neigh_node);
Simon Wunderlich89652332013-11-13 19:14:46 +01001919 if (n_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001920 batadv_neigh_ifinfo_put(n_ifinfo);
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001921 }
1922 rcu_read_unlock();
1923 }
1924
1925 if (batman_count == 0)
1926 seq_puts(seq, "No batman nodes in range ...\n");
1927}
1928
Antonio Quartullia3285a82013-09-02 12:15:04 +02001929/**
Marek Lindner75874052015-08-04 21:09:57 +08001930 * batadv_iv_hardif_neigh_print - print a single hop neighbour node
1931 * @seq: neighbour table seq_file struct
1932 * @hardif_neigh: hardif neighbour information
1933 */
1934static void
1935batadv_iv_hardif_neigh_print(struct seq_file *seq,
1936 struct batadv_hardif_neigh_node *hardif_neigh)
1937{
1938 int last_secs, last_msecs;
1939
1940 last_secs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) / 1000;
1941 last_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) % 1000;
1942
1943 seq_printf(seq, " %10s %pM %4i.%03is\n",
1944 hardif_neigh->if_incoming->net_dev->name,
1945 hardif_neigh->addr, last_secs, last_msecs);
1946}
1947
1948/**
1949 * batadv_iv_ogm_neigh_print - print the single hop neighbour list
1950 * @bat_priv: the bat priv with all the soft interface information
1951 * @seq: neighbour table seq_file struct
1952 */
1953static void batadv_iv_neigh_print(struct batadv_priv *bat_priv,
1954 struct seq_file *seq)
1955{
1956 struct net_device *net_dev = (struct net_device *)seq->private;
1957 struct batadv_hardif_neigh_node *hardif_neigh;
1958 struct batadv_hard_iface *hard_iface;
1959 int batman_count = 0;
1960
Antonio Quartulli925a6f32016-03-12 10:30:18 +01001961 seq_puts(seq, " IF Neighbor last-seen\n");
Marek Lindner75874052015-08-04 21:09:57 +08001962
1963 rcu_read_lock();
1964 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1965 if (hard_iface->soft_iface != net_dev)
1966 continue;
1967
1968 hlist_for_each_entry_rcu(hardif_neigh,
1969 &hard_iface->neigh_list, list) {
1970 batadv_iv_hardif_neigh_print(seq, hardif_neigh);
1971 batman_count++;
1972 }
1973 }
1974 rcu_read_unlock();
1975
1976 if (batman_count == 0)
1977 seq_puts(seq, "No batman nodes in range ...\n");
1978}
1979
1980/**
Antonio Quartullia3285a82013-09-02 12:15:04 +02001981 * batadv_iv_ogm_neigh_cmp - compare the metrics of two neighbors
1982 * @neigh1: the first neighbor object of the comparison
Simon Wunderlich89652332013-11-13 19:14:46 +01001983 * @if_outgoing1: outgoing interface for the first neighbor
Antonio Quartullia3285a82013-09-02 12:15:04 +02001984 * @neigh2: the second neighbor object of the comparison
Simon Wunderlich89652332013-11-13 19:14:46 +01001985 * @if_outgoing2: outgoing interface for the second neighbor
Antonio Quartullia3285a82013-09-02 12:15:04 +02001986 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001987 * Return: a value less, equal to or greater than 0 if the metric via neigh1 is
Antonio Quartullia3285a82013-09-02 12:15:04 +02001988 * lower, the same as or higher than the metric via neigh2
1989 */
1990static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1,
Simon Wunderlich89652332013-11-13 19:14:46 +01001991 struct batadv_hard_iface *if_outgoing1,
1992 struct batadv_neigh_node *neigh2,
1993 struct batadv_hard_iface *if_outgoing2)
Antonio Quartullia3285a82013-09-02 12:15:04 +02001994{
Simon Wunderlich89652332013-11-13 19:14:46 +01001995 struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001996 u8 tq1, tq2;
Simon Wunderlich89652332013-11-13 19:14:46 +01001997 int diff;
Antonio Quartullia3285a82013-09-02 12:15:04 +02001998
Simon Wunderlich89652332013-11-13 19:14:46 +01001999 neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
2000 neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
Antonio Quartullia3285a82013-09-02 12:15:04 +02002001
Simon Wunderlich89652332013-11-13 19:14:46 +01002002 if (!neigh1_ifinfo || !neigh2_ifinfo) {
2003 diff = 0;
2004 goto out;
2005 }
2006
2007 tq1 = neigh1_ifinfo->bat_iv.tq_avg;
2008 tq2 = neigh2_ifinfo->bat_iv.tq_avg;
2009 diff = tq1 - tq2;
2010
2011out:
2012 if (neigh1_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01002013 batadv_neigh_ifinfo_put(neigh1_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01002014 if (neigh2_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01002015 batadv_neigh_ifinfo_put(neigh2_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01002016
2017 return diff;
Antonio Quartullia3285a82013-09-02 12:15:04 +02002018}
2019
Antonio Quartullic43c9812013-09-02 12:15:05 +02002020/**
Simon Wunderlich18165f62015-08-08 02:01:50 +02002021 * batadv_iv_ogm_neigh_is_sob - check if neigh1 is similarly good or better
2022 * than neigh2 from the metric prospective
Antonio Quartullic43c9812013-09-02 12:15:05 +02002023 * @neigh1: the first neighbor object of the comparison
Martin Hundebøll95298a92014-07-15 09:41:05 +02002024 * @if_outgoing1: outgoing interface for the first neighbor
Antonio Quartullic43c9812013-09-02 12:15:05 +02002025 * @neigh2: the second neighbor object of the comparison
Simon Wunderlich89652332013-11-13 19:14:46 +01002026 * @if_outgoing2: outgoing interface for the second neighbor
Martin Hundebøll95298a92014-07-15 09:41:05 +02002027 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002028 * Return: true if the metric via neigh1 is equally good or better than
Simon Wunderlich89652332013-11-13 19:14:46 +01002029 * the metric via neigh2, false otherwise.
2030 */
2031static bool
Simon Wunderlich18165f62015-08-08 02:01:50 +02002032batadv_iv_ogm_neigh_is_sob(struct batadv_neigh_node *neigh1,
Simon Wunderlich89652332013-11-13 19:14:46 +01002033 struct batadv_hard_iface *if_outgoing1,
2034 struct batadv_neigh_node *neigh2,
2035 struct batadv_hard_iface *if_outgoing2)
2036{
2037 struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002038 u8 tq1, tq2;
Simon Wunderlich89652332013-11-13 19:14:46 +01002039 bool ret;
2040
2041 neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
2042 neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
2043
2044 /* we can't say that the metric is better */
2045 if (!neigh1_ifinfo || !neigh2_ifinfo) {
2046 ret = false;
2047 goto out;
2048 }
2049
2050 tq1 = neigh1_ifinfo->bat_iv.tq_avg;
2051 tq2 = neigh2_ifinfo->bat_iv.tq_avg;
2052 ret = (tq1 - tq2) > -BATADV_TQ_SIMILARITY_THRESHOLD;
2053
2054out:
2055 if (neigh1_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01002056 batadv_neigh_ifinfo_put(neigh1_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01002057 if (neigh2_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01002058 batadv_neigh_ifinfo_put(neigh2_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01002059
2060 return ret;
Antonio Quartullic43c9812013-09-02 12:15:05 +02002061}
2062
Sven Eckelmann56303d32012-06-05 22:31:31 +02002063static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
Marek Lindner519d3492012-04-18 17:15:57 +08002064 .name = "BATMAN_IV",
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02002065 .bat_iface_enable = batadv_iv_ogm_iface_enable,
2066 .bat_iface_disable = batadv_iv_ogm_iface_disable,
2067 .bat_iface_update_mac = batadv_iv_ogm_iface_update_mac,
2068 .bat_primary_iface_set = batadv_iv_ogm_primary_iface_set,
2069 .bat_ogm_schedule = batadv_iv_ogm_schedule,
2070 .bat_ogm_emit = batadv_iv_ogm_emit,
Antonio Quartullia3285a82013-09-02 12:15:04 +02002071 .bat_neigh_cmp = batadv_iv_ogm_neigh_cmp,
Simon Wunderlich18165f62015-08-08 02:01:50 +02002072 .bat_neigh_is_similar_or_better = batadv_iv_ogm_neigh_is_sob,
Marek Lindner75874052015-08-04 21:09:57 +08002073 .bat_neigh_print = batadv_iv_neigh_print,
Antonio Quartulli737a2a222013-09-02 12:15:03 +02002074 .bat_orig_print = batadv_iv_ogm_orig_print,
Antonio Quartullid0015fd2013-09-03 11:10:23 +02002075 .bat_orig_free = batadv_iv_ogm_orig_free,
2076 .bat_orig_add_if = batadv_iv_ogm_orig_add_if,
2077 .bat_orig_del_if = batadv_iv_ogm_orig_del_if,
Marek Lindner1c280472011-11-28 17:40:17 +08002078};
2079
Sven Eckelmann81c524f2012-05-12 02:09:22 +02002080int __init batadv_iv_init(void)
Marek Lindner1c280472011-11-28 17:40:17 +08002081{
Marek Lindnerc3e29312012-03-04 16:56:25 +08002082 int ret;
2083
2084 /* batman originator packet */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002085 ret = batadv_recv_handler_register(BATADV_IV_OGM,
2086 batadv_iv_ogm_receive);
Marek Lindnerc3e29312012-03-04 16:56:25 +08002087 if (ret < 0)
2088 goto out;
2089
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02002090 ret = batadv_algo_register(&batadv_batman_iv);
Marek Lindnerc3e29312012-03-04 16:56:25 +08002091 if (ret < 0)
2092 goto handler_unregister;
2093
2094 goto out;
2095
2096handler_unregister:
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002097 batadv_recv_handler_unregister(BATADV_IV_OGM);
Marek Lindnerc3e29312012-03-04 16:56:25 +08002098out:
2099 return ret;
Marek Lindner1c280472011-11-28 17:40:17 +08002100}