blob: be09a98838252f4f0c23cec0625930cf896cd0ff [file] [log] [blame]
Sven Eckelmann7db7d9f2017-11-19 15:05:11 +01001// SPDX-License-Identifier: GPL-2.0
Sven Eckelmann6b1aea82018-01-01 00:00:00 +01002/* Copyright (C) 2007-2018 B.A.T.M.A.N. contributors:
Marek Lindnerfc957272011-07-30 12:04:12 +02003 *
4 * Marek Lindner, Simon Wunderlich
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
Antonio Quartulliebf38fb2013-11-03 20:40:48 +010016 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Marek Lindnerfc957272011-07-30 12:04:12 +020017 */
18
Sven Eckelmanna2d08162016-05-15 11:07:46 +020019#include "bat_iv_ogm.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020020#include "main.h"
21
22#include <linux/atomic.h>
23#include <linux/bitmap.h>
24#include <linux/bitops.h>
25#include <linux/bug.h>
26#include <linux/byteorder/generic.h>
27#include <linux/cache.h>
28#include <linux/errno.h>
29#include <linux/etherdevice.h>
Sven Eckelmannb92b94a2017-11-19 17:12:02 +010030#include <linux/gfp.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020031#include <linux/if_ether.h>
32#include <linux/init.h>
33#include <linux/jiffies.h>
Antonio Quartullif0d97252016-05-03 01:45:34 +080034#include <linux/kernel.h>
Sven Eckelmann77ae32e2016-01-16 10:29:53 +010035#include <linux/kref.h>
Sven Eckelmannfcafa5e2016-05-15 11:07:42 +020036#include <linux/list.h>
Sven Eckelmann64ae74452016-02-22 22:56:34 +010037#include <linux/lockdep.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020038#include <linux/netdevice.h>
Matthias Schiffer024f99c2016-07-03 13:31:40 +020039#include <linux/netlink.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020040#include <linux/pkt_sched.h>
41#include <linux/printk.h>
42#include <linux/random.h>
43#include <linux/rculist.h>
44#include <linux/rcupdate.h>
45#include <linux/seq_file.h>
46#include <linux/skbuff.h>
47#include <linux/slab.h>
48#include <linux/spinlock.h>
49#include <linux/stddef.h>
50#include <linux/string.h>
51#include <linux/types.h>
52#include <linux/workqueue.h>
Matthias Schiffer024f99c2016-07-03 13:31:40 +020053#include <net/genetlink.h>
54#include <net/netlink.h>
Sven Eckelmannfec149f2017-12-21 10:17:41 +010055#include <uapi/linux/batadv_packet.h>
Matthias Schiffer024f99c2016-07-03 13:31:40 +020056#include <uapi/linux/batman_adv.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020057
Sven Eckelmanna2d08162016-05-15 11:07:46 +020058#include "bat_algo.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020059#include "bitarray.h"
Antonio Quartulli34d99cf2016-07-03 12:46:33 +020060#include "gateway_client.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020061#include "hard-interface.h"
62#include "hash.h"
Sven Eckelmannba412082016-05-15 23:48:31 +020063#include "log.h"
Matthias Schiffer024f99c2016-07-03 13:31:40 +020064#include "netlink.h"
Martin Hundebølld56b1702013-01-25 11:12:39 +010065#include "network-coding.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020066#include "originator.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020067#include "routing.h"
68#include "send.h"
69#include "translation-table.h"
Markus Pargmann1f8dce42016-05-15 11:07:43 +020070#include "tvlv.h"
Marek Lindnerfc957272011-07-30 12:04:12 +020071
Antonio Quartullif0d97252016-05-03 01:45:34 +080072static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work);
73
Antonio Quartulli791c2a22013-08-17 12:44:44 +020074/**
Martin Hundebøll95298a92014-07-15 09:41:05 +020075 * enum batadv_dup_status - duplicate status
Antonio Quartulli791c2a22013-08-17 12:44:44 +020076 */
77enum batadv_dup_status {
Sven Eckelmann8b84cc42017-12-02 19:51:48 +010078 /** @BATADV_NO_DUP: the packet is no duplicate */
Antonio Quartulli791c2a22013-08-17 12:44:44 +020079 BATADV_NO_DUP = 0,
Sven Eckelmann8b84cc42017-12-02 19:51:48 +010080
81 /**
82 * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for
83 * the neighbor)
84 */
Antonio Quartulli791c2a22013-08-17 12:44:44 +020085 BATADV_ORIG_DUP,
Sven Eckelmann8b84cc42017-12-02 19:51:48 +010086
87 /** @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor */
Antonio Quartulli791c2a22013-08-17 12:44:44 +020088 BATADV_NEIGH_DUP,
Sven Eckelmann8b84cc42017-12-02 19:51:48 +010089
90 /**
91 * @BATADV_PROTECTED: originator is currently protected (after reboot)
92 */
Antonio Quartulli791c2a22013-08-17 12:44:44 +020093 BATADV_PROTECTED,
94};
95
Antonio Quartulli24a5dee2013-04-08 09:38:12 +020096/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +010097 * batadv_ring_buffer_set() - update the ring buffer with the given value
Antonio Quartulli24a5dee2013-04-08 09:38:12 +020098 * @lq_recv: pointer to the ring buffer
99 * @lq_index: index to store the value at
100 * @value: value to store in the ring buffer
101 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200102static void batadv_ring_buffer_set(u8 lq_recv[], u8 *lq_index, u8 value)
Antonio Quartulli24a5dee2013-04-08 09:38:12 +0200103{
104 lq_recv[*lq_index] = value;
105 *lq_index = (*lq_index + 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE;
106}
107
108/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100109 * batadv_ring_buffer_avg() - compute the average of all non-zero values stored
Antonio Quartulli24a5dee2013-04-08 09:38:12 +0200110 * in the given ring buffer
111 * @lq_recv: pointer to the ring buffer
112 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200113 * Return: computed average value.
Antonio Quartulli24a5dee2013-04-08 09:38:12 +0200114 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200115static u8 batadv_ring_buffer_avg(const u8 lq_recv[])
Antonio Quartulli24a5dee2013-04-08 09:38:12 +0200116{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200117 const u8 *ptr;
118 u16 count = 0;
119 u16 i = 0;
120 u16 sum = 0;
Antonio Quartulli24a5dee2013-04-08 09:38:12 +0200121
122 ptr = lq_recv;
123
124 while (i < BATADV_TQ_GLOBAL_WINDOW_SIZE) {
125 if (*ptr != 0) {
126 count++;
127 sum += *ptr;
128 }
129
130 i++;
131 ptr++;
132 }
133
134 if (count == 0)
135 return 0;
136
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200137 return (u8)(sum / count);
Antonio Quartulli24a5dee2013-04-08 09:38:12 +0200138}
David S. Millerd98cae64e2013-06-19 16:49:39 -0700139
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200140/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100141 * batadv_iv_ogm_orig_free() - free the private resources allocated for this
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200142 * orig_node
143 * @orig_node: the orig_node for which the resources have to be free'd
144 */
145static void batadv_iv_ogm_orig_free(struct batadv_orig_node *orig_node)
146{
147 kfree(orig_node->bat_iv.bcast_own);
148 kfree(orig_node->bat_iv.bcast_own_sum);
149}
150
151/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100152 * batadv_iv_ogm_orig_add_if() - change the private structures of the orig_node
153 * to include the new hard-interface
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200154 * @orig_node: the orig_node that has to be changed
155 * @max_if_num: the current amount of interfaces
156 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200157 * Return: 0 on success, a negative error code otherwise.
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200158 */
159static int batadv_iv_ogm_orig_add_if(struct batadv_orig_node *orig_node,
Sven Eckelmannf22e0892017-12-26 15:14:01 +0100160 unsigned int max_if_num)
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200161{
162 void *data_ptr;
Antonio Quartulli0185dda2014-05-24 06:43:47 +0200163 size_t old_size;
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200164 int ret = -ENOMEM;
165
166 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
167
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200168 old_size = (max_if_num - 1) * sizeof(unsigned long) * BATADV_NUM_WORDS;
Antonio Quartulli0185dda2014-05-24 06:43:47 +0200169 data_ptr = kmalloc_array(max_if_num,
170 BATADV_NUM_WORDS * sizeof(unsigned long),
171 GFP_ATOMIC);
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200172 if (!data_ptr)
173 goto unlock;
174
175 memcpy(data_ptr, orig_node->bat_iv.bcast_own, old_size);
176 kfree(orig_node->bat_iv.bcast_own);
177 orig_node->bat_iv.bcast_own = data_ptr;
178
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200179 data_ptr = kmalloc_array(max_if_num, sizeof(u8), GFP_ATOMIC);
Sven Eckelmannf7dcdf52016-02-22 22:56:33 +0100180 if (!data_ptr)
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200181 goto unlock;
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200182
183 memcpy(data_ptr, orig_node->bat_iv.bcast_own_sum,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200184 (max_if_num - 1) * sizeof(u8));
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200185 kfree(orig_node->bat_iv.bcast_own_sum);
186 orig_node->bat_iv.bcast_own_sum = data_ptr;
187
188 ret = 0;
189
190unlock:
191 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
192
193 return ret;
194}
195
196/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100197 * batadv_iv_ogm_drop_bcast_own_entry() - drop section of bcast_own
Sven Eckelmann64ae74452016-02-22 22:56:34 +0100198 * @orig_node: the orig_node that has to be changed
199 * @max_if_num: the current amount of interfaces
200 * @del_if_num: the index of the interface being removed
201 */
202static void
203batadv_iv_ogm_drop_bcast_own_entry(struct batadv_orig_node *orig_node,
Sven Eckelmannf22e0892017-12-26 15:14:01 +0100204 unsigned int max_if_num,
205 unsigned int del_if_num)
Sven Eckelmann64ae74452016-02-22 22:56:34 +0100206{
207 size_t chunk_size;
208 size_t if_offset;
209 void *data_ptr;
210
211 lockdep_assert_held(&orig_node->bat_iv.ogm_cnt_lock);
212
213 chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS;
214 data_ptr = kmalloc_array(max_if_num, chunk_size, GFP_ATOMIC);
215 if (!data_ptr)
216 /* use old buffer when new one could not be allocated */
217 data_ptr = orig_node->bat_iv.bcast_own;
218
219 /* copy first part */
220 memmove(data_ptr, orig_node->bat_iv.bcast_own, del_if_num * chunk_size);
221
222 /* copy second part */
223 if_offset = (del_if_num + 1) * chunk_size;
224 memmove((char *)data_ptr + del_if_num * chunk_size,
225 (uint8_t *)orig_node->bat_iv.bcast_own + if_offset,
226 (max_if_num - del_if_num) * chunk_size);
227
228 /* bcast_own was shrunk down in new buffer; free old one */
229 if (orig_node->bat_iv.bcast_own != data_ptr) {
230 kfree(orig_node->bat_iv.bcast_own);
231 orig_node->bat_iv.bcast_own = data_ptr;
232 }
233}
234
235/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100236 * batadv_iv_ogm_drop_bcast_own_sum_entry() - drop section of bcast_own_sum
Sven Eckelmann64ae74452016-02-22 22:56:34 +0100237 * @orig_node: the orig_node that has to be changed
238 * @max_if_num: the current amount of interfaces
239 * @del_if_num: the index of the interface being removed
240 */
241static void
242batadv_iv_ogm_drop_bcast_own_sum_entry(struct batadv_orig_node *orig_node,
Sven Eckelmannf22e0892017-12-26 15:14:01 +0100243 unsigned int max_if_num,
244 unsigned int del_if_num)
Sven Eckelmann64ae74452016-02-22 22:56:34 +0100245{
246 size_t if_offset;
247 void *data_ptr;
248
249 lockdep_assert_held(&orig_node->bat_iv.ogm_cnt_lock);
250
251 data_ptr = kmalloc_array(max_if_num, sizeof(u8), GFP_ATOMIC);
252 if (!data_ptr)
253 /* use old buffer when new one could not be allocated */
254 data_ptr = orig_node->bat_iv.bcast_own_sum;
255
256 memmove(data_ptr, orig_node->bat_iv.bcast_own_sum,
257 del_if_num * sizeof(u8));
258
259 if_offset = (del_if_num + 1) * sizeof(u8);
260 memmove((char *)data_ptr + del_if_num * sizeof(u8),
261 orig_node->bat_iv.bcast_own_sum + if_offset,
262 (max_if_num - del_if_num) * sizeof(u8));
263
264 /* bcast_own_sum was shrunk down in new buffer; free old one */
265 if (orig_node->bat_iv.bcast_own_sum != data_ptr) {
266 kfree(orig_node->bat_iv.bcast_own_sum);
267 orig_node->bat_iv.bcast_own_sum = data_ptr;
268 }
269}
270
271/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100272 * batadv_iv_ogm_orig_del_if() - change the private structures of the orig_node
273 * to exclude the removed interface
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200274 * @orig_node: the orig_node that has to be changed
275 * @max_if_num: the current amount of interfaces
276 * @del_if_num: the index of the interface being removed
277 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200278 * Return: 0 on success, a negative error code otherwise.
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200279 */
280static int batadv_iv_ogm_orig_del_if(struct batadv_orig_node *orig_node,
Sven Eckelmannf22e0892017-12-26 15:14:01 +0100281 unsigned int max_if_num,
282 unsigned int del_if_num)
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200283{
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200284 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
285
Sven Eckelmann64ae74452016-02-22 22:56:34 +0100286 if (max_if_num == 0) {
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200287 kfree(orig_node->bat_iv.bcast_own);
Sven Eckelmann64ae74452016-02-22 22:56:34 +0100288 kfree(orig_node->bat_iv.bcast_own_sum);
289 orig_node->bat_iv.bcast_own = NULL;
290 orig_node->bat_iv.bcast_own_sum = NULL;
291 } else {
292 batadv_iv_ogm_drop_bcast_own_entry(orig_node, max_if_num,
293 del_if_num);
294 batadv_iv_ogm_drop_bcast_own_sum_entry(orig_node, max_if_num,
295 del_if_num);
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200296 }
297
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200298 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
299
Sven Eckelmann64ae74452016-02-22 22:56:34 +0100300 return 0;
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200301}
302
303/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100304 * batadv_iv_ogm_orig_get() - retrieve or create (if does not exist) an
305 * originator
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200306 * @bat_priv: the bat priv with all the soft interface information
307 * @addr: mac address of the originator
308 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200309 * Return: the originator object corresponding to the passed mac address or NULL
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200310 * on failure.
311 * If the object does not exists it is created an initialised.
312 */
313static struct batadv_orig_node *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200314batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const u8 *addr)
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200315{
316 struct batadv_orig_node *orig_node;
Sven Eckelmannf22e0892017-12-26 15:14:01 +0100317 int hash_added;
318 size_t size;
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200319
320 orig_node = batadv_orig_hash_find(bat_priv, addr);
321 if (orig_node)
322 return orig_node;
323
324 orig_node = batadv_orig_node_new(bat_priv, addr);
325 if (!orig_node)
326 return NULL;
327
328 spin_lock_init(&orig_node->bat_iv.ogm_cnt_lock);
329
330 size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS;
331 orig_node->bat_iv.bcast_own = kzalloc(size, GFP_ATOMIC);
332 if (!orig_node->bat_iv.bcast_own)
333 goto free_orig_node;
334
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200335 size = bat_priv->num_ifaces * sizeof(u8);
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200336 orig_node->bat_iv.bcast_own_sum = kzalloc(size, GFP_ATOMIC);
337 if (!orig_node->bat_iv.bcast_own_sum)
Antonio Quartullia5a5cb82014-02-15 02:17:20 +0100338 goto free_orig_node;
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200339
Sven Eckelmann55db2d52016-07-15 17:39:21 +0200340 kref_get(&orig_node->refcount);
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200341 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
342 batadv_choose_orig, orig_node,
343 &orig_node->hash_entry);
344 if (hash_added != 0)
Sven Eckelmann55db2d52016-07-15 17:39:21 +0200345 goto free_orig_node_hash;
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200346
347 return orig_node;
348
Sven Eckelmann55db2d52016-07-15 17:39:21 +0200349free_orig_node_hash:
Sven Eckelmann5d967312016-01-17 11:01:09 +0100350 batadv_orig_node_put(orig_node);
Sven Eckelmann55db2d52016-07-15 17:39:21 +0200351free_orig_node:
Sven Eckelmann5d967312016-01-17 11:01:09 +0100352 batadv_orig_node_put(orig_node);
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200353
354 return NULL;
355}
356
Sven Eckelmann56303d32012-06-05 22:31:31 +0200357static struct batadv_neigh_node *
358batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200359 const u8 *neigh_addr,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200360 struct batadv_orig_node *orig_node,
Antonio Quartulli863dd7a2013-03-25 13:49:46 +0100361 struct batadv_orig_node *orig_neigh)
Marek Lindner7ae8b282012-03-01 15:35:21 +0800362{
Marek Lindner741aa062015-07-26 04:57:43 +0800363 struct batadv_neigh_node *neigh_node;
Marek Lindner7ae8b282012-03-01 15:35:21 +0800364
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800365 neigh_node = batadv_neigh_node_get_or_create(orig_node,
366 hard_iface, neigh_addr);
Marek Lindner7ae8b282012-03-01 15:35:21 +0800367 if (!neigh_node)
368 goto out;
369
Simon Wunderlich89652332013-11-13 19:14:46 +0100370 neigh_node->orig_node = orig_neigh;
Marek Lindner7ae8b282012-03-01 15:35:21 +0800371
Marek Lindner7ae8b282012-03-01 15:35:21 +0800372out:
373 return neigh_node;
374}
375
Sven Eckelmann56303d32012-06-05 22:31:31 +0200376static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200377{
Sven Eckelmann96412692012-06-05 22:31:30 +0200378 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindner14511512012-08-02 17:20:26 +0200379 unsigned char *ogm_buff;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200380 u32 random_seqno;
Marek Lindnerd7d32ec2012-02-07 17:20:46 +0800381
382 /* randomize initial seqno to avoid collision */
383 get_random_bytes(&random_seqno, sizeof(random_seqno));
Marek Lindner14511512012-08-02 17:20:26 +0200384 atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno);
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200385
Marek Lindner14511512012-08-02 17:20:26 +0200386 hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN;
387 ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC);
388 if (!ogm_buff)
Markus Pargmann42d9f2c2014-12-26 12:41:26 +0100389 return -ENOMEM;
Marek Lindner77af7572012-02-07 17:20:48 +0800390
Marek Lindner14511512012-08-02 17:20:26 +0200391 hard_iface->bat_iv.ogm_buff = ogm_buff;
392
393 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100394 batadv_ogm_packet->packet_type = BATADV_IV_OGM;
395 batadv_ogm_packet->version = BATADV_COMPAT_VERSION;
396 batadv_ogm_packet->ttl = 2;
Sven Eckelmann96412692012-06-05 22:31:30 +0200397 batadv_ogm_packet->flags = BATADV_NO_FLAGS;
Marek Lindner414254e2013-04-23 21:39:58 +0800398 batadv_ogm_packet->reserved = 0;
Sven Eckelmann96412692012-06-05 22:31:30 +0200399 batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE;
Marek Lindner77af7572012-02-07 17:20:48 +0800400
Markus Pargmann42d9f2c2014-12-26 12:41:26 +0100401 return 0;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200402}
403
Sven Eckelmann56303d32012-06-05 22:31:31 +0200404static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
Marek Lindner00a50072012-02-07 17:20:47 +0800405{
Marek Lindner14511512012-08-02 17:20:26 +0200406 kfree(hard_iface->bat_iv.ogm_buff);
407 hard_iface->bat_iv.ogm_buff = NULL;
Marek Lindner00a50072012-02-07 17:20:47 +0800408}
409
Sven Eckelmann56303d32012-06-05 22:31:31 +0200410static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface)
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200411{
Sven Eckelmann96412692012-06-05 22:31:30 +0200412 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindner14511512012-08-02 17:20:26 +0200413 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200414
Marek Lindner14511512012-08-02 17:20:26 +0200415 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100416 ether_addr_copy(batadv_ogm_packet->orig,
417 hard_iface->net_dev->dev_addr);
418 ether_addr_copy(batadv_ogm_packet->prev_sender,
419 hard_iface->net_dev->dev_addr);
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200420}
421
Sven Eckelmann56303d32012-06-05 22:31:31 +0200422static void
423batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface)
Marek Lindnerc3229392012-03-11 06:17:50 +0800424{
Sven Eckelmann96412692012-06-05 22:31:30 +0200425 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindner14511512012-08-02 17:20:26 +0200426 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
Marek Lindnerc3229392012-03-11 06:17:50 +0800427
Marek Lindner14511512012-08-02 17:20:26 +0200428 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100429 batadv_ogm_packet->ttl = BATADV_TTL;
Marek Lindnerc3229392012-03-11 06:17:50 +0800430}
431
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200432/* when do we schedule our own ogm to be sent */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200433static unsigned long
Sven Eckelmann56303d32012-06-05 22:31:31 +0200434batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200435{
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200436 unsigned int msecs;
437
438 msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
Akinobu Mitae76e4322012-12-24 11:14:07 +0900439 msecs += prandom_u32() % (2 * BATADV_JITTER);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200440
441 return jiffies + msecs_to_jiffies(msecs);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200442}
443
444/* when do we schedule a ogm packet to be sent */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200445static unsigned long batadv_iv_ogm_fwd_send_time(void)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200446{
Akinobu Mitae76e4322012-12-24 11:14:07 +0900447 return jiffies + msecs_to_jiffies(prandom_u32() % (BATADV_JITTER / 2));
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200448}
449
450/* apply hop penalty for a normal link */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200451static u8 batadv_hop_penalty(u8 tq, const struct batadv_priv *bat_priv)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200452{
453 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200454 int new_tq;
455
456 new_tq = tq * (BATADV_TQ_MAX_VALUE - hop_penalty);
457 new_tq /= BATADV_TQ_MAX_VALUE;
458
459 return new_tq;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200460}
461
Simon Wunderlich600184b2015-11-23 19:57:21 +0100462/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100463 * batadv_iv_ogm_aggr_packet() - checks if there is another OGM attached
Simon Wunderlich600184b2015-11-23 19:57:21 +0100464 * @buff_pos: current position in the skb
465 * @packet_len: total length of the skb
466 * @tvlv_len: tvlv length of the previously considered OGM
467 *
468 * Return: true if there is enough space for another OGM, false otherwise.
469 */
Markus Pargmann9fd9b192014-12-26 12:41:27 +0100470static bool batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len,
471 __be16 tvlv_len)
Marek Lindnerfc957272011-07-30 12:04:12 +0200472{
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200473 int next_buff_pos = 0;
474
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200475 next_buff_pos += buff_pos + BATADV_OGM_HLEN;
Marek Lindneref261572013-04-23 21:39:57 +0800476 next_buff_pos += ntohs(tvlv_len);
Marek Lindnerfc957272011-07-30 12:04:12 +0200477
478 return (next_buff_pos <= packet_len) &&
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200479 (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
Marek Lindnerfc957272011-07-30 12:04:12 +0200480}
481
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200482/* send a batman ogm to a given interface */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200483static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet,
484 struct batadv_hard_iface *hard_iface)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200485{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200486 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Markus Pargmannde12bae2014-12-26 12:41:28 +0100487 const char *fwd_str;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200488 u8 packet_num;
489 s16 buff_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +0200490 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200491 struct sk_buff *skb;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200492 u8 *packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200493
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200494 if (hard_iface->if_status != BATADV_IF_ACTIVE)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200495 return;
496
497 packet_num = 0;
498 buff_pos = 0;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200499 packet_pos = forw_packet->skb->data;
500 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200501
502 /* adjust all flags and log packets */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200503 while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
Marek Lindneref261572013-04-23 21:39:57 +0800504 batadv_ogm_packet->tvlv_len)) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200505 /* we might have aggregated direct link packets with an
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200506 * ordinary base packet
507 */
Sven Eckelmann8de47de2012-07-08 16:32:09 +0200508 if (forw_packet->direct_link_flags & BIT(packet_num) &&
509 forw_packet->if_incoming == hard_iface)
Sven Eckelmann96412692012-06-05 22:31:30 +0200510 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200511 else
Sven Eckelmann96412692012-06-05 22:31:30 +0200512 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200513
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200514 if (packet_num > 0 || !forw_packet->own)
515 fwd_str = "Forwarding";
516 else
517 fwd_str = "Sending own";
518
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200519 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800520 "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200521 fwd_str, (packet_num > 0 ? "aggregated " : ""),
Sven Eckelmann96412692012-06-05 22:31:30 +0200522 batadv_ogm_packet->orig,
523 ntohl(batadv_ogm_packet->seqno),
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100524 batadv_ogm_packet->tq, batadv_ogm_packet->ttl,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +0200525 ((batadv_ogm_packet->flags & BATADV_DIRECTLINK) ?
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200526 "on" : "off"),
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800527 hard_iface->net_dev->name,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200528 hard_iface->net_dev->dev_addr);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200529
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200530 buff_pos += BATADV_OGM_HLEN;
Marek Lindneref261572013-04-23 21:39:57 +0800531 buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200532 packet_num++;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200533 packet_pos = forw_packet->skb->data + buff_pos;
534 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200535 }
536
537 /* create clone because function is called more than once */
538 skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
Martin Hundebøllf8214862012-04-20 17:02:45 +0200539 if (skb) {
Sven Eckelmannd69909d2012-06-03 22:19:20 +0200540 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
541 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
Martin Hundebøllf8214862012-04-20 17:02:45 +0200542 skb->len + ETH_HLEN);
Antonio Quartulli95d39272016-01-16 16:40:15 +0800543 batadv_send_broadcast_skb(skb, hard_iface);
Martin Hundebøllf8214862012-04-20 17:02:45 +0200544 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200545}
546
547/* send a batman ogm packet */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200548static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200549{
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200550 struct net_device *soft_iface;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200551
552 if (!forw_packet->if_incoming) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100553 pr_err("Error - can't forward packet: incoming iface not specified\n");
Linus LĂĽssingf55a2e82016-06-14 22:56:50 +0200554 return;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200555 }
556
557 soft_iface = forw_packet->if_incoming->soft_iface;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200558
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100559 if (WARN_ON(!forw_packet->if_outgoing))
Linus LĂĽssingf55a2e82016-06-14 22:56:50 +0200560 return;
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100561
562 if (WARN_ON(forw_packet->if_outgoing->soft_iface != soft_iface))
Linus LĂĽssingf55a2e82016-06-14 22:56:50 +0200563 return;
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100564
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200565 if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE)
Linus LĂĽssingf55a2e82016-06-14 22:56:50 +0200566 return;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200567
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100568 /* only for one specific outgoing interface */
569 batadv_iv_ogm_send_to_if(forw_packet, forw_packet->if_outgoing);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200570}
571
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100572/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100573 * batadv_iv_ogm_can_aggregate() - find out if an OGM can be aggregated on an
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100574 * existing forward packet
575 * @new_bat_ogm_packet: OGM packet to be aggregated
576 * @bat_priv: the bat priv with all the soft interface information
577 * @packet_len: (total) length of the OGM
578 * @send_time: timestamp (jiffies) when the packet is to be sent
Martin Hundebøll95298a92014-07-15 09:41:05 +0200579 * @directlink: true if this is a direct link packet
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100580 * @if_incoming: interface where the packet was received
581 * @if_outgoing: interface for which the retransmission should be considered
582 * @forw_packet: the forwarded packet which should be checked
583 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200584 * Return: true if new_packet can be aggregated with forw_packet
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100585 */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200586static bool
Sven Eckelmann96412692012-06-05 22:31:30 +0200587batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200588 struct batadv_priv *bat_priv,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200589 int packet_len, unsigned long send_time,
590 bool directlink,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200591 const struct batadv_hard_iface *if_incoming,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100592 const struct batadv_hard_iface *if_outgoing,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200593 const struct batadv_forw_packet *forw_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200594{
Sven Eckelmann96412692012-06-05 22:31:30 +0200595 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200596 int aggregated_bytes = forw_packet->packet_len + packet_len;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200597 struct batadv_hard_iface *primary_if = NULL;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200598 bool res = false;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200599 unsigned long aggregation_end_time;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200600
Sven Eckelmann96412692012-06-05 22:31:30 +0200601 batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200602 aggregation_end_time = send_time;
603 aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200604
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200605 /* we can aggregate the current packet to this aggregated packet
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200606 * if:
607 *
608 * - the send time is within our MAX_AGGREGATION_MS time
609 * - the resulting packet wont be bigger than
610 * MAX_AGGREGATION_BYTES
Markus Pargmann8f34b382014-12-26 12:41:29 +0100611 * otherwise aggregation is not possible
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200612 */
Markus Pargmann8f34b382014-12-26 12:41:29 +0100613 if (!time_before(send_time, forw_packet->send_time) ||
614 !time_after_eq(aggregation_end_time, forw_packet->send_time))
615 return false;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200616
Markus Pargmann8f34b382014-12-26 12:41:29 +0100617 if (aggregated_bytes > BATADV_MAX_AGGREGATION_BYTES)
618 return false;
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100619
Markus Pargmann8f34b382014-12-26 12:41:29 +0100620 /* packet is not leaving on the same interface. */
621 if (forw_packet->if_outgoing != if_outgoing)
622 return false;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200623
Markus Pargmann8f34b382014-12-26 12:41:29 +0100624 /* check aggregation compatibility
625 * -> direct link packets are broadcasted on
626 * their interface only
627 * -> aggregate packet if the current packet is
628 * a "global" packet as well as the base
629 * packet
630 */
631 primary_if = batadv_primary_if_get_selected(bat_priv);
632 if (!primary_if)
633 return false;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200634
Markus Pargmann8f34b382014-12-26 12:41:29 +0100635 /* packets without direct link flag and high TTL
636 * are flooded through the net
637 */
638 if (!directlink &&
639 !(batadv_ogm_packet->flags & BATADV_DIRECTLINK) &&
640 batadv_ogm_packet->ttl != 1 &&
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200641
Markus Pargmann8f34b382014-12-26 12:41:29 +0100642 /* own packets originating non-primary
643 * interfaces leave only that interface
644 */
645 (!forw_packet->own ||
646 forw_packet->if_incoming == primary_if)) {
647 res = true;
648 goto out;
649 }
650
651 /* if the incoming packet is sent via this one
652 * interface only - we still can aggregate
653 */
654 if (directlink &&
655 new_bat_ogm_packet->ttl == 1 &&
656 forw_packet->if_incoming == if_incoming &&
657
658 /* packets from direct neighbors or
659 * own secondary interface packets
660 * (= secondary interface packets in general)
661 */
662 (batadv_ogm_packet->flags & BATADV_DIRECTLINK ||
663 (forw_packet->own &&
664 forw_packet->if_incoming != primary_if))) {
665 res = true;
666 goto out;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200667 }
668
669out:
670 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100671 batadv_hardif_put(primary_if);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200672 return res;
673}
674
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100675/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100676 * batadv_iv_ogm_aggregate_new() - create a new aggregated packet and add this
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100677 * packet to it.
678 * @packet_buff: pointer to the OGM
679 * @packet_len: (total) length of the OGM
680 * @send_time: timestamp (jiffies) when the packet is to be sent
681 * @direct_link: whether this OGM has direct link status
682 * @if_incoming: interface where the packet was received
683 * @if_outgoing: interface for which the retransmission should be considered
684 * @own_packet: true if it is a self-generated ogm
685 */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200686static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,
687 int packet_len, unsigned long send_time,
688 bool direct_link,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200689 struct batadv_hard_iface *if_incoming,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100690 struct batadv_hard_iface *if_outgoing,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200691 int own_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200692{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200693 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
694 struct batadv_forw_packet *forw_packet_aggr;
Linus LĂĽssing99ba18e2017-02-17 11:17:06 +0100695 struct sk_buff *skb;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200696 unsigned char *skb_buff;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200697 unsigned int skb_size;
Linus LĂĽssinga65e5482016-06-20 21:39:54 +0200698 atomic_t *queue_left = own_packet ? NULL : &bat_priv->batman_queue_left;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200699
Markus Pargmann940d1562014-12-26 12:41:31 +0100700 if (atomic_read(&bat_priv->aggregated_ogms) &&
701 packet_len < BATADV_MAX_AGGREGATION_BYTES)
Sven Eckelmann5b246572012-11-04 17:11:45 +0100702 skb_size = BATADV_MAX_AGGREGATION_BYTES;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200703 else
Sven Eckelmann5b246572012-11-04 17:11:45 +0100704 skb_size = packet_len;
705
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200706 skb_size += ETH_HLEN;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200707
Linus LĂĽssing99ba18e2017-02-17 11:17:06 +0100708 skb = netdev_alloc_skb_ip_align(NULL, skb_size);
709 if (!skb)
710 return;
711
712 forw_packet_aggr = batadv_forw_packet_alloc(if_incoming, if_outgoing,
713 queue_left, bat_priv, skb);
714 if (!forw_packet_aggr) {
715 kfree_skb(skb);
Linus LĂĽssinga65e5482016-06-20 21:39:54 +0200716 return;
717 }
718
Simon Wunderlichc54f38c2013-07-29 17:56:44 +0200719 forw_packet_aggr->skb->priority = TC_PRIO_CONTROL;
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200720 skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200721
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200722 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
723 forw_packet_aggr->packet_len = packet_len;
724 memcpy(skb_buff, packet_buff, packet_len);
725
726 forw_packet_aggr->own = own_packet;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200727 forw_packet_aggr->direct_link_flags = BATADV_NO_FLAGS;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200728 forw_packet_aggr->send_time = send_time;
729
730 /* save packet direct link flag status */
731 if (direct_link)
732 forw_packet_aggr->direct_link_flags |= 1;
733
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200734 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
Antonio Quartullif0d97252016-05-03 01:45:34 +0800735 batadv_iv_send_outstanding_bat_ogm_packet);
Linus LĂĽssing9b4aec62016-11-01 09:44:44 +0100736
737 batadv_forw_packet_ogmv1_queue(bat_priv, forw_packet_aggr, send_time);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200738}
739
740/* aggregate a new packet into the existing ogm packet */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200741static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200742 const unsigned char *packet_buff,
743 int packet_len, bool direct_link)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200744{
Sven Eckelmann8de47de2012-07-08 16:32:09 +0200745 unsigned long new_direct_link_flag;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200746
Sven Eckelmanne04de482017-06-18 09:59:28 +0200747 skb_put_data(forw_packet_aggr->skb, packet_buff, packet_len);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200748 forw_packet_aggr->packet_len += packet_len;
749 forw_packet_aggr->num_packets++;
750
751 /* save packet direct link flag status */
Sven Eckelmann8de47de2012-07-08 16:32:09 +0200752 if (direct_link) {
753 new_direct_link_flag = BIT(forw_packet_aggr->num_packets);
754 forw_packet_aggr->direct_link_flags |= new_direct_link_flag;
755 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200756}
757
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100758/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100759 * batadv_iv_ogm_queue_add() - queue up an OGM for transmission
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100760 * @bat_priv: the bat priv with all the soft interface information
761 * @packet_buff: pointer to the OGM
762 * @packet_len: (total) length of the OGM
763 * @if_incoming: interface where the packet was received
764 * @if_outgoing: interface for which the retransmission should be considered
765 * @own_packet: true if it is a self-generated ogm
766 * @send_time: timestamp (jiffies) when the packet is to be sent
767 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200768static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200769 unsigned char *packet_buff,
770 int packet_len,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200771 struct batadv_hard_iface *if_incoming,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100772 struct batadv_hard_iface *if_outgoing,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200773 int own_packet, unsigned long send_time)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200774{
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200775 /* _aggr -> pointer to the packet we want to aggregate with
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200776 * _pos -> pointer to the position in the queue
777 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200778 struct batadv_forw_packet *forw_packet_aggr = NULL;
779 struct batadv_forw_packet *forw_packet_pos = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200780 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200781 bool direct_link;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200782 unsigned long max_aggregation_jiffies;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200783
Sven Eckelmann96412692012-06-05 22:31:30 +0200784 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
Markus Pargmann56489152014-12-26 12:41:32 +0100785 direct_link = !!(batadv_ogm_packet->flags & BATADV_DIRECTLINK);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200786 max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200787
788 /* find position for the packet in the forward queue */
789 spin_lock_bh(&bat_priv->forw_bat_list_lock);
790 /* own packets are not to be aggregated */
Markus Pargmann56489152014-12-26 12:41:32 +0100791 if (atomic_read(&bat_priv->aggregated_ogms) && !own_packet) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800792 hlist_for_each_entry(forw_packet_pos,
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200793 &bat_priv->forw_bat_list, list) {
Sven Eckelmann96412692012-06-05 22:31:30 +0200794 if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200795 bat_priv, packet_len,
796 send_time, direct_link,
797 if_incoming,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100798 if_outgoing,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200799 forw_packet_pos)) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200800 forw_packet_aggr = forw_packet_pos;
801 break;
802 }
803 }
804 }
805
806 /* nothing to aggregate with - either aggregation disabled or no
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200807 * suitable aggregation packet found
808 */
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200809 if (!forw_packet_aggr) {
810 /* the following section can run without the lock */
811 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
812
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200813 /* if we could not aggregate this packet with one of the others
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200814 * we hold it back for a while, so that it might be aggregated
815 * later on
816 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200817 if (!own_packet && atomic_read(&bat_priv->aggregated_ogms))
818 send_time += max_aggregation_jiffies;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200819
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200820 batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
821 send_time, direct_link,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100822 if_incoming, if_outgoing,
823 own_packet);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200824 } else {
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200825 batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
826 packet_len, direct_link);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200827 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
828 }
829}
830
Sven Eckelmann56303d32012-06-05 22:31:31 +0200831static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200832 const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +0200833 struct batadv_ogm_packet *batadv_ogm_packet,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200834 bool is_single_hop_neigh,
835 bool is_from_best_next_hop,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100836 struct batadv_hard_iface *if_incoming,
837 struct batadv_hard_iface *if_outgoing)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200838{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200839 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200840 u16 tvlv_len;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200841
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100842 if (batadv_ogm_packet->ttl <= 1) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200843 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200844 return;
845 }
846
Marek Lindner13b25412012-03-11 06:17:53 +0800847 if (!is_from_best_next_hop) {
848 /* Mark the forwarded packet when it is not coming from our
849 * best next hop. We still need to forward the packet for our
850 * neighbor link quality detection to work in case the packet
851 * originated from a single hop neighbor. Otherwise we can
852 * simply drop the ogm.
853 */
854 if (is_single_hop_neigh)
Sven Eckelmann96412692012-06-05 22:31:30 +0200855 batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP;
Marek Lindner13b25412012-03-11 06:17:53 +0800856 else
857 return;
858 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200859
Marek Lindneref261572013-04-23 21:39:57 +0800860 tvlv_len = ntohs(batadv_ogm_packet->tvlv_len);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200861
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100862 batadv_ogm_packet->ttl--;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100863 ether_addr_copy(batadv_ogm_packet->prev_sender, ethhdr->h_source);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200864
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200865 /* apply hop penalty */
Sven Eckelmann96412692012-06-05 22:31:30 +0200866 batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200867 bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200868
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200869 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200870 "Forwarding packet: tq: %i, ttl: %i\n",
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100871 batadv_ogm_packet->tq, batadv_ogm_packet->ttl);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200872
Marek Lindner75cd33f2012-03-01 15:35:16 +0800873 if (is_single_hop_neigh)
Sven Eckelmann96412692012-06-05 22:31:30 +0200874 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200875 else
Sven Eckelmann96412692012-06-05 22:31:30 +0200876 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200877
Sven Eckelmann96412692012-06-05 22:31:30 +0200878 batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
Marek Lindneref261572013-04-23 21:39:57 +0800879 BATADV_OGM_HLEN + tvlv_len,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100880 if_incoming, if_outgoing, 0,
881 batadv_iv_ogm_fwd_send_time());
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200882}
883
Antonio Quartullid9896612013-04-17 17:44:43 +0200884/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100885 * batadv_iv_ogm_slide_own_bcast_window() - bitshift own OGM broadcast windows
886 * for the given interface
Antonio Quartullid9896612013-04-17 17:44:43 +0200887 * @hard_iface: the interface for which the windows have to be shifted
888 */
889static void
890batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
891{
892 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
893 struct batadv_hashtable *hash = bat_priv->orig_hash;
894 struct hlist_head *head;
895 struct batadv_orig_node *orig_node;
896 unsigned long *word;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200897 u32 i;
Antonio Quartullid9896612013-04-17 17:44:43 +0200898 size_t word_index;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200899 u8 *w;
Sven Eckelmannf22e0892017-12-26 15:14:01 +0100900 unsigned int if_num;
Antonio Quartullid9896612013-04-17 17:44:43 +0200901
902 for (i = 0; i < hash->size; i++) {
903 head = &hash->table[i];
904
905 rcu_read_lock();
906 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200907 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Antonio Quartullid9896612013-04-17 17:44:43 +0200908 word_index = hard_iface->if_num * BATADV_NUM_WORDS;
Antonio Quartulli32db6aa2014-09-01 14:37:29 +0200909 word = &orig_node->bat_iv.bcast_own[word_index];
Antonio Quartullid9896612013-04-17 17:44:43 +0200910
911 batadv_bit_get_packet(bat_priv, word, 1, 0);
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200912 if_num = hard_iface->if_num;
913 w = &orig_node->bat_iv.bcast_own_sum[if_num];
Antonio Quartullid9896612013-04-17 17:44:43 +0200914 *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200915 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Antonio Quartullid9896612013-04-17 17:44:43 +0200916 }
917 rcu_read_unlock();
918 }
919}
920
Sven Eckelmann56303d32012-06-05 22:31:31 +0200921static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200922{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200923 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindner14511512012-08-02 17:20:26 +0200924 unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff;
Sven Eckelmann96412692012-06-05 22:31:30 +0200925 struct batadv_ogm_packet *batadv_ogm_packet;
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100926 struct batadv_hard_iface *primary_if, *tmp_hard_iface;
Marek Lindner14511512012-08-02 17:20:26 +0200927 int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200928 u32 seqno;
929 u16 tvlv_len = 0;
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100930 unsigned long send_time;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200931
Sven Eckelmann825ffe12017-08-23 21:52:13 +0200932 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE ||
933 hard_iface->if_status == BATADV_IF_TO_BE_REMOVED)
Antonio Quartullif0d97252016-05-03 01:45:34 +0800934 return;
935
936 /* the interface gets activated here to avoid race conditions between
937 * the moment of activating the interface in
938 * hardif_activate_interface() where the originator mac is set and
939 * outdated packets (especially uninitialized mac addresses) in the
940 * packet queue
941 */
942 if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED)
943 hard_iface->if_status = BATADV_IF_ACTIVE;
944
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200945 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200946
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800947 if (hard_iface == primary_if) {
948 /* tt changes have to be committed before the tvlv data is
949 * appended as it may alter the tt tvlv container
950 */
951 batadv_tt_local_commit_changes(bat_priv);
Marek Lindneref261572013-04-23 21:39:57 +0800952 tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff,
953 ogm_buff_len,
954 BATADV_OGM_HLEN);
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800955 }
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800956
Marek Lindner14511512012-08-02 17:20:26 +0200957 batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff);
Marek Lindneref261572013-04-23 21:39:57 +0800958 batadv_ogm_packet->tvlv_len = htons(tvlv_len);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200959
960 /* change sequence number to network order */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200961 seqno = (u32)atomic_read(&hard_iface->bat_iv.ogm_seqno);
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200962 batadv_ogm_packet->seqno = htonl(seqno);
Marek Lindner14511512012-08-02 17:20:26 +0200963 atomic_inc(&hard_iface->bat_iv.ogm_seqno);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200964
Antonio Quartullid9896612013-04-17 17:44:43 +0200965 batadv_iv_ogm_slide_own_bcast_window(hard_iface);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200966
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100967 send_time = batadv_iv_ogm_emit_send_time(bat_priv);
968
969 if (hard_iface != primary_if) {
970 /* OGMs from secondary interfaces are only scheduled on their
971 * respective interfaces.
972 */
973 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff, *ogm_buff_len,
974 hard_iface, hard_iface, 1, send_time);
975 goto out;
976 }
977
978 /* OGMs from primary interfaces are scheduled on all
979 * interfaces.
980 */
981 rcu_read_lock();
982 list_for_each_entry_rcu(tmp_hard_iface, &batadv_hardif_list, list) {
983 if (tmp_hard_iface->soft_iface != hard_iface->soft_iface)
Sven Eckelmann87b40f52015-07-17 10:03:42 +0200984 continue;
Sven Eckelmann27353442016-03-05 16:09:16 +0100985
986 if (!kref_get_unless_zero(&tmp_hard_iface->refcount))
987 continue;
988
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100989 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff,
990 *ogm_buff_len, hard_iface,
991 tmp_hard_iface, 1, send_time);
Sven Eckelmann27353442016-03-05 16:09:16 +0100992
993 batadv_hardif_put(tmp_hard_iface);
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100994 }
995 rcu_read_unlock();
996
997out:
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200998 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100999 batadv_hardif_put(primary_if);
Marek Lindnerb9dacc52011-08-03 09:09:30 +02001000}
1001
Simon Wunderlich89652332013-11-13 19:14:46 +01001002/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001003 * batadv_iv_ogm_orig_update() - use OGM to update corresponding data in an
Simon Wunderlich89652332013-11-13 19:14:46 +01001004 * originator
1005 * @bat_priv: the bat priv with all the soft interface information
1006 * @orig_node: the orig node who originally emitted the ogm packet
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001007 * @orig_ifinfo: ifinfo for the outgoing interface of the orig_node
Simon Wunderlich89652332013-11-13 19:14:46 +01001008 * @ethhdr: Ethernet header of the OGM
1009 * @batadv_ogm_packet: the ogm packet
1010 * @if_incoming: interface where the packet was received
1011 * @if_outgoing: interface for which the retransmission should be considered
Simon Wunderlich89652332013-11-13 19:14:46 +01001012 * @dup_status: the duplicate status of this ogm packet.
1013 */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001014static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001015batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
1016 struct batadv_orig_node *orig_node,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001017 struct batadv_orig_ifinfo *orig_ifinfo,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001018 const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +02001019 const struct batadv_ogm_packet *batadv_ogm_packet,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001020 struct batadv_hard_iface *if_incoming,
Simon Wunderlich89652332013-11-13 19:14:46 +01001021 struct batadv_hard_iface *if_outgoing,
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001022 enum batadv_dup_status dup_status)
Marek Lindnerfc957272011-07-30 12:04:12 +02001023{
Simon Wunderlich89652332013-11-13 19:14:46 +01001024 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
1025 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
Sven Eckelmann4f248cf2015-06-09 20:50:49 +02001026 struct batadv_neigh_node *neigh_node = NULL;
1027 struct batadv_neigh_node *tmp_neigh_node = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001028 struct batadv_neigh_node *router = NULL;
1029 struct batadv_orig_node *orig_node_tmp;
Sven Eckelmannf22e0892017-12-26 15:14:01 +01001030 unsigned int if_num;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001031 u8 sum_orig, sum_neigh;
1032 u8 *neigh_addr;
1033 u8 tq_avg;
Marek Lindnerfc957272011-07-30 12:04:12 +02001034
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001035 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann22f05022017-05-19 13:02:00 +02001036 "%s(): Searching and updating originator entry of received packet\n",
1037 __func__);
Marek Lindnerfc957272011-07-30 12:04:12 +02001038
1039 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001040 hlist_for_each_entry_rcu(tmp_neigh_node,
Marek Lindnerfc957272011-07-30 12:04:12 +02001041 &orig_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001042 neigh_addr = tmp_neigh_node->addr;
1043 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
1044 tmp_neigh_node->if_incoming == if_incoming &&
Sven Eckelmann77ae32e2016-01-16 10:29:53 +01001045 kref_get_unless_zero(&tmp_neigh_node->refcount)) {
Antonio Quartulli38dc40e2013-03-30 17:22:00 +01001046 if (WARN(neigh_node, "too many matching neigh_nodes"))
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001047 batadv_neigh_node_put(neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001048 neigh_node = tmp_neigh_node;
1049 continue;
1050 }
1051
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001052 if (dup_status != BATADV_NO_DUP)
Marek Lindnerfc957272011-07-30 12:04:12 +02001053 continue;
1054
Simon Wunderlich89652332013-11-13 19:14:46 +01001055 /* only update the entry for this outgoing interface */
1056 neigh_ifinfo = batadv_neigh_ifinfo_get(tmp_neigh_node,
1057 if_outgoing);
1058 if (!neigh_ifinfo)
1059 continue;
1060
1061 spin_lock_bh(&tmp_neigh_node->ifinfo_lock);
1062 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
1063 &neigh_ifinfo->bat_iv.tq_index, 0);
1064 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
1065 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
1066 spin_unlock_bh(&tmp_neigh_node->ifinfo_lock);
1067
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001068 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01001069 neigh_ifinfo = NULL;
Marek Lindnerfc957272011-07-30 12:04:12 +02001070 }
1071
1072 if (!neigh_node) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001073 struct batadv_orig_node *orig_tmp;
Marek Lindnerfc957272011-07-30 12:04:12 +02001074
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001075 orig_tmp = batadv_iv_ogm_orig_get(bat_priv, ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001076 if (!orig_tmp)
1077 goto unlock;
1078
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001079 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1080 ethhdr->h_source,
Antonio Quartulli863dd7a2013-03-25 13:49:46 +01001081 orig_node, orig_tmp);
Marek Lindnerfc957272011-07-30 12:04:12 +02001082
Sven Eckelmann5d967312016-01-17 11:01:09 +01001083 batadv_orig_node_put(orig_tmp);
Marek Lindnerfc957272011-07-30 12:04:12 +02001084 if (!neigh_node)
1085 goto unlock;
Markus Pargmann23badd62014-12-26 12:41:33 +01001086 } else {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001087 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001088 "Updating existing last-hop neighbor of originator\n");
Markus Pargmann23badd62014-12-26 12:41:33 +01001089 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001090
1091 rcu_read_unlock();
Simon Wunderlich89652332013-11-13 19:14:46 +01001092 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
1093 if (!neigh_ifinfo)
1094 goto out;
Marek Lindnerfc957272011-07-30 12:04:12 +02001095
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001096 neigh_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +02001097
Simon Wunderlich89652332013-11-13 19:14:46 +01001098 spin_lock_bh(&neigh_node->ifinfo_lock);
1099 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
1100 &neigh_ifinfo->bat_iv.tq_index,
Sven Eckelmann96412692012-06-05 22:31:30 +02001101 batadv_ogm_packet->tq);
Simon Wunderlich89652332013-11-13 19:14:46 +01001102 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
1103 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
1104 spin_unlock_bh(&neigh_node->ifinfo_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001105
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001106 if (dup_status == BATADV_NO_DUP) {
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001107 orig_ifinfo->last_ttl = batadv_ogm_packet->ttl;
Simon Wunderlich89652332013-11-13 19:14:46 +01001108 neigh_ifinfo->last_ttl = batadv_ogm_packet->ttl;
Marek Lindnerfc957272011-07-30 12:04:12 +02001109 }
1110
Marek Lindnerfc957272011-07-30 12:04:12 +02001111 /* if this neighbor already is our next hop there is nothing
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001112 * to change
1113 */
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001114 router = batadv_orig_router_get(orig_node, if_outgoing);
Marek Lindnerfc957272011-07-30 12:04:12 +02001115 if (router == neigh_node)
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001116 goto out;
Marek Lindnerfc957272011-07-30 12:04:12 +02001117
Simon Wunderlich89652332013-11-13 19:14:46 +01001118 if (router) {
1119 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1120 if (!router_ifinfo)
1121 goto out;
1122
1123 /* if this neighbor does not offer a better TQ we won't
1124 * consider it
1125 */
1126 if (router_ifinfo->bat_iv.tq_avg > neigh_ifinfo->bat_iv.tq_avg)
1127 goto out;
1128 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001129
1130 /* if the TQ is the same and the link not more symmetric we
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001131 * won't consider it either
1132 */
Simon Wunderlich89652332013-11-13 19:14:46 +01001133 if (router_ifinfo &&
Markus Pargmann01b97a32014-12-26 12:41:30 +01001134 neigh_ifinfo->bat_iv.tq_avg == router_ifinfo->bat_iv.tq_avg) {
Marek Lindnerfc957272011-07-30 12:04:12 +02001135 orig_node_tmp = router->orig_node;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001136 spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
Linus LĂĽssing7caf69f2012-09-18 03:01:08 +02001137 if_num = router->if_incoming->if_num;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001138 sum_orig = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
1139 spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001140
1141 orig_node_tmp = neigh_node->orig_node;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001142 spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
Linus LĂĽssing7caf69f2012-09-18 03:01:08 +02001143 if_num = neigh_node->if_incoming->if_num;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001144 sum_neigh = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
1145 spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001146
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001147 if (sum_orig >= sum_neigh)
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001148 goto out;
Marek Lindnerfc957272011-07-30 12:04:12 +02001149 }
1150
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001151 batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001152 goto out;
1153
1154unlock:
1155 rcu_read_unlock();
1156out:
1157 if (neigh_node)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001158 batadv_neigh_node_put(neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001159 if (router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001160 batadv_neigh_node_put(router);
Simon Wunderlich89652332013-11-13 19:14:46 +01001161 if (neigh_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001162 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01001163 if (router_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001164 batadv_neigh_ifinfo_put(router_ifinfo);
Marek Lindnerfc957272011-07-30 12:04:12 +02001165}
1166
Simon Wunderlich89652332013-11-13 19:14:46 +01001167/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001168 * batadv_iv_ogm_calc_tq() - calculate tq for current received ogm packet
Simon Wunderlich89652332013-11-13 19:14:46 +01001169 * @orig_node: the orig node who originally emitted the ogm packet
1170 * @orig_neigh_node: the orig node struct of the neighbor who sent the packet
1171 * @batadv_ogm_packet: the ogm packet
1172 * @if_incoming: interface where the packet was received
1173 * @if_outgoing: interface for which the retransmission should be considered
1174 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001175 * Return: true if the link can be considered bidirectional, false otherwise
Simon Wunderlich89652332013-11-13 19:14:46 +01001176 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001177static bool batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
1178 struct batadv_orig_node *orig_neigh_node,
1179 struct batadv_ogm_packet *batadv_ogm_packet,
1180 struct batadv_hard_iface *if_incoming,
1181 struct batadv_hard_iface *if_outgoing)
Marek Lindnerfc957272011-07-30 12:04:12 +02001182{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001183 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1184 struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
Simon Wunderlich89652332013-11-13 19:14:46 +01001185 struct batadv_neigh_ifinfo *neigh_ifinfo;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001186 u8 total_count;
1187 u8 orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001188 unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
Sven Eckelmannf22e0892017-12-26 15:14:01 +01001189 unsigned int if_num;
Sven Eckelmannd285f522016-02-16 10:47:07 +01001190 unsigned int tq_asym_penalty, inv_asym_penalty;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001191 unsigned int combined_tq;
Sven Eckelmannd285f522016-02-16 10:47:07 +01001192 unsigned int tq_iface_penalty;
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001193 bool ret = false;
Marek Lindnerfc957272011-07-30 12:04:12 +02001194
1195 /* find corresponding one hop neighbor */
1196 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001197 hlist_for_each_entry_rcu(tmp_neigh_node,
Marek Lindnerfc957272011-07-30 12:04:12 +02001198 &orig_neigh_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001199 if (!batadv_compare_eth(tmp_neigh_node->addr,
1200 orig_neigh_node->orig))
Marek Lindnerfc957272011-07-30 12:04:12 +02001201 continue;
1202
1203 if (tmp_neigh_node->if_incoming != if_incoming)
1204 continue;
1205
Sven Eckelmann77ae32e2016-01-16 10:29:53 +01001206 if (!kref_get_unless_zero(&tmp_neigh_node->refcount))
Marek Lindnerfc957272011-07-30 12:04:12 +02001207 continue;
1208
1209 neigh_node = tmp_neigh_node;
1210 break;
1211 }
1212 rcu_read_unlock();
1213
1214 if (!neigh_node)
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001215 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1216 orig_neigh_node->orig,
1217 orig_neigh_node,
Antonio Quartulli863dd7a2013-03-25 13:49:46 +01001218 orig_neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001219
1220 if (!neigh_node)
1221 goto out;
1222
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001223 /* if orig_node is direct neighbor update neigh_node last_seen */
Marek Lindnerfc957272011-07-30 12:04:12 +02001224 if (orig_node == orig_neigh_node)
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001225 neigh_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +02001226
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001227 orig_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +02001228
1229 /* find packet count of corresponding one hop neighbor */
Sven Eckelmann5ba7dcf2017-12-03 11:26:45 +01001230 spin_lock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001231 if_num = if_incoming->if_num;
1232 orig_eq_count = orig_neigh_node->bat_iv.bcast_own_sum[if_num];
Simon Wunderlich89652332013-11-13 19:14:46 +01001233 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
1234 if (neigh_ifinfo) {
1235 neigh_rq_count = neigh_ifinfo->bat_iv.real_packet_count;
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001236 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01001237 } else {
1238 neigh_rq_count = 0;
1239 }
Sven Eckelmann5ba7dcf2017-12-03 11:26:45 +01001240 spin_unlock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001241
1242 /* pay attention to not get a value bigger than 100 % */
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001243 if (orig_eq_count > neigh_rq_count)
1244 total_count = neigh_rq_count;
1245 else
1246 total_count = orig_eq_count;
Marek Lindnerfc957272011-07-30 12:04:12 +02001247
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001248 /* if we have too few packets (too less data) we set tq_own to zero
1249 * if we receive too few packets it is not considered bidirectional
1250 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001251 if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
1252 neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
Marek Lindnerfc957272011-07-30 12:04:12 +02001253 tq_own = 0;
1254 else
1255 /* neigh_node->real_packet_count is never zero as we
1256 * only purge old information when getting new
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001257 * information
1258 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001259 tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count;
Marek Lindnerfc957272011-07-30 12:04:12 +02001260
Sven Eckelmann21a12362012-03-07 09:07:46 +01001261 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
Marek Lindnerfc957272011-07-30 12:04:12 +02001262 * affect the nearly-symmetric links only a little, but
1263 * punishes asymmetric links more. This will give a value
1264 * between 0 and TQ_MAX_VALUE
1265 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001266 neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
1267 neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
1268 neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
1269 BATADV_TQ_LOCAL_WINDOW_SIZE *
1270 BATADV_TQ_LOCAL_WINDOW_SIZE;
1271 inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
1272 inv_asym_penalty /= neigh_rq_max_cube;
1273 tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;
Marek Lindnerfc957272011-07-30 12:04:12 +02001274
Simon Wunderlichc0398762013-11-13 19:14:48 +01001275 /* penalize if the OGM is forwarded on the same interface. WiFi
1276 * interfaces and other half duplex devices suffer from throughput
1277 * drops as they can't send and receive at the same time.
1278 */
1279 tq_iface_penalty = BATADV_TQ_MAX_VALUE;
Sven Eckelmann825ffe12017-08-23 21:52:13 +02001280 if (if_outgoing && if_incoming == if_outgoing &&
Sven Eckelmann10b1bbb2016-09-30 15:21:03 +02001281 batadv_is_wifi_hardif(if_outgoing))
Simon Wunderlichc0398762013-11-13 19:14:48 +01001282 tq_iface_penalty = batadv_hop_penalty(BATADV_TQ_MAX_VALUE,
1283 bat_priv);
1284
1285 combined_tq = batadv_ogm_packet->tq *
1286 tq_own *
1287 tq_asym_penalty *
1288 tq_iface_penalty;
1289 combined_tq /= BATADV_TQ_MAX_VALUE *
1290 BATADV_TQ_MAX_VALUE *
1291 BATADV_TQ_MAX_VALUE;
Sven Eckelmann96412692012-06-05 22:31:30 +02001292 batadv_ogm_packet->tq = combined_tq;
Marek Lindnerfc957272011-07-30 12:04:12 +02001293
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001294 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann6a04be82017-06-18 10:12:30 +02001295 "bidirectional: orig = %pM neigh = %pM => 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 +02001296 orig_node->orig, orig_neigh_node->orig, total_count,
Simon Wunderlichc0398762013-11-13 19:14:48 +01001297 neigh_rq_count, tq_own, tq_asym_penalty, tq_iface_penalty,
1298 batadv_ogm_packet->tq, if_incoming->net_dev->name,
1299 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT");
Marek Lindnerfc957272011-07-30 12:04:12 +02001300
1301 /* if link has the minimum required transmission quality
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001302 * consider it bidirectional
1303 */
Sven Eckelmann96412692012-06-05 22:31:30 +02001304 if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001305 ret = true;
Marek Lindnerfc957272011-07-30 12:04:12 +02001306
1307out:
1308 if (neigh_node)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001309 batadv_neigh_node_put(neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001310 return ret;
1311}
1312
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001313/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001314 * batadv_iv_ogm_update_seqnos() - process a batman packet for all interfaces,
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001315 * adjust the sequence number and find out whether it is a duplicate
1316 * @ethhdr: ethernet header of the packet
1317 * @batadv_ogm_packet: OGM packet to be considered
1318 * @if_incoming: interface on which the OGM packet was received
Simon Wunderlich89652332013-11-13 19:14:46 +01001319 * @if_outgoing: interface for which the retransmission should be considered
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001320 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001321 * Return: duplicate status as enum batadv_dup_status
Marek Lindnerfc957272011-07-30 12:04:12 +02001322 */
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001323static enum batadv_dup_status
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001324batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +02001325 const struct batadv_ogm_packet *batadv_ogm_packet,
Simon Wunderlich89652332013-11-13 19:14:46 +01001326 const struct batadv_hard_iface *if_incoming,
1327 struct batadv_hard_iface *if_outgoing)
Marek Lindnerfc957272011-07-30 12:04:12 +02001328{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001329 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1330 struct batadv_orig_node *orig_node;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001331 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
Simon Wunderlich89652332013-11-13 19:14:46 +01001332 struct batadv_neigh_node *neigh_node;
1333 struct batadv_neigh_ifinfo *neigh_ifinfo;
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001334 bool is_dup;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001335 s32 seq_diff;
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001336 bool need_update = false;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001337 int set_mark;
1338 enum batadv_dup_status ret = BATADV_NO_DUP;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001339 u32 seqno = ntohl(batadv_ogm_packet->seqno);
1340 u8 *neigh_addr;
1341 u8 packet_count;
Antonio Quartulli0538f752013-09-02 12:15:01 +02001342 unsigned long *bitmap;
Marek Lindnerfc957272011-07-30 12:04:12 +02001343
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001344 orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
Marek Lindnerfc957272011-07-30 12:04:12 +02001345 if (!orig_node)
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001346 return BATADV_NO_DUP;
Marek Lindnerfc957272011-07-30 12:04:12 +02001347
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001348 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1349 if (WARN_ON(!orig_ifinfo)) {
Sven Eckelmann5d967312016-01-17 11:01:09 +01001350 batadv_orig_node_put(orig_node);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001351 return 0;
1352 }
1353
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001354 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001355 seq_diff = seqno - orig_ifinfo->last_real_seqno;
Marek Lindnerfc957272011-07-30 12:04:12 +02001356
1357 /* signalize caller that the packet is to be dropped. */
Antonio Quartulli1e5cc262012-02-26 15:39:42 +01001358 if (!hlist_empty(&orig_node->neigh_list) &&
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001359 batadv_window_protected(bat_priv, seq_diff,
Simon Wunderlich81f02682015-11-23 19:57:22 +01001360 BATADV_TQ_LOCAL_WINDOW_SIZE,
1361 &orig_ifinfo->batman_seqno_reset, NULL)) {
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001362 ret = BATADV_PROTECTED;
Marek Lindnerfc957272011-07-30 12:04:12 +02001363 goto out;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001364 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001365
1366 rcu_read_lock();
Simon Wunderlich89652332013-11-13 19:14:46 +01001367 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1368 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node,
1369 if_outgoing);
1370 if (!neigh_ifinfo)
1371 continue;
1372
1373 neigh_addr = neigh_node->addr;
1374 is_dup = batadv_test_bit(neigh_ifinfo->bat_iv.real_bits,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001375 orig_ifinfo->last_real_seqno,
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001376 seqno);
1377
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001378 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
Simon Wunderlich89652332013-11-13 19:14:46 +01001379 neigh_node->if_incoming == if_incoming) {
Marek Lindnerfc957272011-07-30 12:04:12 +02001380 set_mark = 1;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001381 if (is_dup)
1382 ret = BATADV_NEIGH_DUP;
1383 } else {
Marek Lindnerfc957272011-07-30 12:04:12 +02001384 set_mark = 0;
Sven Eckelmann825ffe12017-08-23 21:52:13 +02001385 if (is_dup && ret != BATADV_NEIGH_DUP)
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001386 ret = BATADV_ORIG_DUP;
1387 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001388
1389 /* if the window moved, set the update flag. */
Simon Wunderlich89652332013-11-13 19:14:46 +01001390 bitmap = neigh_ifinfo->bat_iv.real_bits;
Antonio Quartulli0538f752013-09-02 12:15:01 +02001391 need_update |= batadv_bit_get_packet(bat_priv, bitmap,
Sven Eckelmann0f5f93222012-05-12 02:09:25 +02001392 seq_diff, set_mark);
Marek Lindnerfc957272011-07-30 12:04:12 +02001393
Simon Wunderlich89652332013-11-13 19:14:46 +01001394 packet_count = bitmap_weight(bitmap,
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001395 BATADV_TQ_LOCAL_WINDOW_SIZE);
Simon Wunderlich89652332013-11-13 19:14:46 +01001396 neigh_ifinfo->bat_iv.real_packet_count = packet_count;
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001397 batadv_neigh_ifinfo_put(neigh_ifinfo);
Marek Lindnerfc957272011-07-30 12:04:12 +02001398 }
1399 rcu_read_unlock();
1400
1401 if (need_update) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001402 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001403 "%s updating last_seqno: old %u, new %u\n",
1404 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT",
1405 orig_ifinfo->last_real_seqno, seqno);
1406 orig_ifinfo->last_real_seqno = seqno;
Marek Lindnerfc957272011-07-30 12:04:12 +02001407 }
1408
Marek Lindnerfc957272011-07-30 12:04:12 +02001409out:
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001410 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Sven Eckelmann5d967312016-01-17 11:01:09 +01001411 batadv_orig_node_put(orig_node);
Sven Eckelmann35f94772016-01-17 11:01:13 +01001412 batadv_orig_ifinfo_put(orig_ifinfo);
Marek Lindnerfc957272011-07-30 12:04:12 +02001413 return ret;
1414}
1415
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001416/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001417 * batadv_iv_ogm_process_per_outif() - process a batman iv OGM for an outgoing
1418 * interface
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001419 * @skb: the skb containing the OGM
Martin Hundebøll95298a92014-07-15 09:41:05 +02001420 * @ogm_offset: offset from skb->data to start of ogm header
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001421 * @orig_node: the (cached) orig node for the originator of this OGM
1422 * @if_incoming: the interface where this packet was received
1423 * @if_outgoing: the interface for which the packet should be considered
1424 */
1425static void
1426batadv_iv_ogm_process_per_outif(const struct sk_buff *skb, int ogm_offset,
1427 struct batadv_orig_node *orig_node,
1428 struct batadv_hard_iface *if_incoming,
1429 struct batadv_hard_iface *if_outgoing)
1430{
1431 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Marek Lindner4ff1e2a2015-08-04 21:09:58 +08001432 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
Sven Eckelmann4f248cf2015-06-09 20:50:49 +02001433 struct batadv_neigh_node *router = NULL;
1434 struct batadv_neigh_node *router_router = NULL;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001435 struct batadv_orig_node *orig_neigh_node;
1436 struct batadv_orig_ifinfo *orig_ifinfo;
1437 struct batadv_neigh_node *orig_neigh_router = NULL;
1438 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
1439 struct batadv_ogm_packet *ogm_packet;
1440 enum batadv_dup_status dup_status;
1441 bool is_from_best_next_hop = false;
1442 bool is_single_hop_neigh = false;
1443 bool sameseq, similar_ttl;
1444 struct sk_buff *skb_priv;
1445 struct ethhdr *ethhdr;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001446 u8 *prev_sender;
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001447 bool is_bidirect;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001448
1449 /* create a private copy of the skb, as some functions change tq value
1450 * and/or flags.
1451 */
1452 skb_priv = skb_copy(skb, GFP_ATOMIC);
1453 if (!skb_priv)
1454 return;
1455
1456 ethhdr = eth_hdr(skb_priv);
1457 ogm_packet = (struct batadv_ogm_packet *)(skb_priv->data + ogm_offset);
1458
1459 dup_status = batadv_iv_ogm_update_seqnos(ethhdr, ogm_packet,
1460 if_incoming, if_outgoing);
1461 if (batadv_compare_eth(ethhdr->h_source, ogm_packet->orig))
1462 is_single_hop_neigh = true;
1463
1464 if (dup_status == BATADV_PROTECTED) {
1465 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1466 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1467 ethhdr->h_source);
1468 goto out;
1469 }
1470
1471 if (ogm_packet->tq == 0) {
1472 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1473 "Drop packet: originator packet with tq equal 0\n");
1474 goto out;
1475 }
1476
Marek Lindner4ff1e2a2015-08-04 21:09:58 +08001477 if (is_single_hop_neigh) {
1478 hardif_neigh = batadv_hardif_neigh_get(if_incoming,
1479 ethhdr->h_source);
1480 if (hardif_neigh)
1481 hardif_neigh->last_seen = jiffies;
1482 }
1483
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001484 router = batadv_orig_router_get(orig_node, if_outgoing);
1485 if (router) {
1486 router_router = batadv_orig_router_get(router->orig_node,
1487 if_outgoing);
1488 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1489 }
1490
1491 if ((router_ifinfo && router_ifinfo->bat_iv.tq_avg != 0) &&
1492 (batadv_compare_eth(router->addr, ethhdr->h_source)))
1493 is_from_best_next_hop = true;
1494
1495 prev_sender = ogm_packet->prev_sender;
1496 /* avoid temporary routing loops */
1497 if (router && router_router &&
1498 (batadv_compare_eth(router->addr, prev_sender)) &&
1499 !(batadv_compare_eth(ogm_packet->orig, prev_sender)) &&
1500 (batadv_compare_eth(router->addr, router_router->addr))) {
1501 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1502 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1503 ethhdr->h_source);
1504 goto out;
1505 }
1506
1507 if (if_outgoing == BATADV_IF_DEFAULT)
1508 batadv_tvlv_ogm_receive(bat_priv, ogm_packet, orig_node);
1509
1510 /* if sender is a direct neighbor the sender mac equals
1511 * originator mac
1512 */
1513 if (is_single_hop_neigh)
1514 orig_neigh_node = orig_node;
1515 else
1516 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1517 ethhdr->h_source);
1518
1519 if (!orig_neigh_node)
1520 goto out;
1521
1522 /* Update nc_nodes of the originator */
1523 batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node,
1524 ogm_packet, is_single_hop_neigh);
1525
1526 orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
1527 if_outgoing);
1528
1529 /* drop packet if sender is not a direct neighbor and if we
1530 * don't route towards it
1531 */
Sven Eckelmann825ffe12017-08-23 21:52:13 +02001532 if (!is_single_hop_neigh && !orig_neigh_router) {
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001533 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1534 "Drop packet: OGM via unknown neighbor!\n");
1535 goto out_neigh;
1536 }
1537
1538 is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
1539 ogm_packet, if_incoming,
1540 if_outgoing);
1541
1542 /* update ranking if it is not a duplicate or has the same
1543 * seqno and similar ttl as the non-duplicate
1544 */
1545 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1546 if (!orig_ifinfo)
1547 goto out_neigh;
1548
1549 sameseq = orig_ifinfo->last_real_seqno == ntohl(ogm_packet->seqno);
1550 similar_ttl = (orig_ifinfo->last_ttl - 3) <= ogm_packet->ttl;
1551
Sven Eckelmann825ffe12017-08-23 21:52:13 +02001552 if (is_bidirect && (dup_status == BATADV_NO_DUP ||
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001553 (sameseq && similar_ttl))) {
1554 batadv_iv_ogm_orig_update(bat_priv, orig_node,
1555 orig_ifinfo, ethhdr,
1556 ogm_packet, if_incoming,
1557 if_outgoing, dup_status);
1558 }
Sven Eckelmann35f94772016-01-17 11:01:13 +01001559 batadv_orig_ifinfo_put(orig_ifinfo);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001560
Simon Wunderlichef0a9372013-11-13 19:14:49 +01001561 /* only forward for specific interface, not for the default one. */
1562 if (if_outgoing == BATADV_IF_DEFAULT)
1563 goto out_neigh;
1564
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001565 /* is single hop (direct) neighbor */
1566 if (is_single_hop_neigh) {
1567 /* OGMs from secondary interfaces should only scheduled once
1568 * per interface where it has been received, not multiple times
1569 */
Sven Eckelmann825ffe12017-08-23 21:52:13 +02001570 if (ogm_packet->ttl <= 2 &&
1571 if_incoming != if_outgoing) {
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001572 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1573 "Drop packet: OGM from secondary interface and wrong outgoing interface\n");
1574 goto out_neigh;
1575 }
1576 /* mark direct link on incoming interface */
1577 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1578 is_single_hop_neigh,
Simon Wunderlichef0a9372013-11-13 19:14:49 +01001579 is_from_best_next_hop, if_incoming,
1580 if_outgoing);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001581
1582 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1583 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
1584 goto out_neigh;
1585 }
1586
1587 /* multihop originator */
1588 if (!is_bidirect) {
1589 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1590 "Drop packet: not received via bidirectional link\n");
1591 goto out_neigh;
1592 }
1593
1594 if (dup_status == BATADV_NEIGH_DUP) {
1595 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1596 "Drop packet: duplicate packet received\n");
1597 goto out_neigh;
1598 }
1599
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001600 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1601 "Forwarding packet: rebroadcast originator packet\n");
1602 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1603 is_single_hop_neigh, is_from_best_next_hop,
Simon Wunderlichef0a9372013-11-13 19:14:49 +01001604 if_incoming, if_outgoing);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001605
1606out_neigh:
Sven Eckelmann825ffe12017-08-23 21:52:13 +02001607 if (orig_neigh_node && !is_single_hop_neigh)
Sven Eckelmann5d967312016-01-17 11:01:09 +01001608 batadv_orig_node_put(orig_neigh_node);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001609out:
Simon Wunderlichc1e517f2014-03-26 15:46:21 +01001610 if (router_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001611 batadv_neigh_ifinfo_put(router_ifinfo);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001612 if (router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001613 batadv_neigh_node_put(router);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001614 if (router_router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001615 batadv_neigh_node_put(router_router);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001616 if (orig_neigh_router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001617 batadv_neigh_node_put(orig_neigh_router);
Marek Lindner4ff1e2a2015-08-04 21:09:58 +08001618 if (hardif_neigh)
Sven Eckelmannaccadc32016-01-17 11:01:14 +01001619 batadv_hardif_neigh_put(hardif_neigh);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001620
Sven Eckelmannbd687fe2016-07-17 21:04:00 +02001621 consume_skb(skb_priv);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001622}
1623
1624/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001625 * batadv_iv_ogm_process() - process an incoming batman iv OGM
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001626 * @skb: the skb containing the OGM
1627 * @ogm_offset: offset to the OGM which should be processed (for aggregates)
1628 * @if_incoming: the interface where this packet was receved
1629 */
1630static void batadv_iv_ogm_process(const struct sk_buff *skb, int ogm_offset,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001631 struct batadv_hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +02001632{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001633 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001634 struct batadv_orig_node *orig_neigh_node, *orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001635 struct batadv_hard_iface *hard_iface;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001636 struct batadv_ogm_packet *ogm_packet;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001637 u32 if_incoming_seqno;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001638 bool has_directlink_flag;
1639 struct ethhdr *ethhdr;
1640 bool is_my_oldorig = false;
1641 bool is_my_addr = false;
1642 bool is_my_orig = false;
1643
1644 ogm_packet = (struct batadv_ogm_packet *)(skb->data + ogm_offset);
1645 ethhdr = eth_hdr(skb);
Marek Lindnerfc957272011-07-30 12:04:12 +02001646
1647 /* Silently drop when the batman packet is actually not a
1648 * correct packet.
1649 *
1650 * This might happen if a packet is padded (e.g. Ethernet has a
1651 * minimum frame length of 64 byte) and the aggregation interprets
1652 * it as an additional length.
1653 *
1654 * TODO: A more sane solution would be to have a bit in the
Sven Eckelmann96412692012-06-05 22:31:30 +02001655 * batadv_ogm_packet to detect whether the packet is the last
Marek Lindnerfc957272011-07-30 12:04:12 +02001656 * packet in an aggregation. Here we expect that the padding
1657 * is always zero (or not 0x01)
1658 */
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001659 if (ogm_packet->packet_type != BATADV_IV_OGM)
Marek Lindnerfc957272011-07-30 12:04:12 +02001660 return;
1661
1662 /* could be changed by schedule_own_packet() */
Marek Lindner14511512012-08-02 17:20:26 +02001663 if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno);
Marek Lindnerfc957272011-07-30 12:04:12 +02001664
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001665 if (ogm_packet->flags & BATADV_DIRECTLINK)
1666 has_directlink_flag = true;
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001667 else
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001668 has_directlink_flag = false;
Marek Lindnerfc957272011-07-30 12:04:12 +02001669
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001670 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001671 "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 +02001672 ethhdr->h_source, if_incoming->net_dev->name,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001673 if_incoming->net_dev->dev_addr, ogm_packet->orig,
1674 ogm_packet->prev_sender, ntohl(ogm_packet->seqno),
1675 ogm_packet->tq, ogm_packet->ttl,
1676 ogm_packet->version, has_directlink_flag);
Marek Lindnerfc957272011-07-30 12:04:12 +02001677
1678 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001679 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001680 if (hard_iface->if_status != BATADV_IF_ACTIVE)
Marek Lindnerfc957272011-07-30 12:04:12 +02001681 continue;
1682
1683 if (hard_iface->soft_iface != if_incoming->soft_iface)
1684 continue;
1685
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001686 if (batadv_compare_eth(ethhdr->h_source,
1687 hard_iface->net_dev->dev_addr))
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001688 is_my_addr = true;
Marek Lindnerfc957272011-07-30 12:04:12 +02001689
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001690 if (batadv_compare_eth(ogm_packet->orig,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001691 hard_iface->net_dev->dev_addr))
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001692 is_my_orig = true;
Marek Lindnerfc957272011-07-30 12:04:12 +02001693
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001694 if (batadv_compare_eth(ogm_packet->prev_sender,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001695 hard_iface->net_dev->dev_addr))
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001696 is_my_oldorig = true;
Marek Lindnerfc957272011-07-30 12:04:12 +02001697 }
1698 rcu_read_unlock();
1699
Marek Lindnerfc957272011-07-30 12:04:12 +02001700 if (is_my_addr) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001701 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001702 "Drop packet: received my own broadcast (sender: %pM)\n",
1703 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001704 return;
1705 }
1706
Marek Lindnerfc957272011-07-30 12:04:12 +02001707 if (is_my_orig) {
1708 unsigned long *word;
Sven Eckelmannf22e0892017-12-26 15:14:01 +01001709 size_t offset;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001710 s32 bit_pos;
Sven Eckelmannf22e0892017-12-26 15:14:01 +01001711 unsigned int if_num;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001712 u8 *weight;
Marek Lindnerfc957272011-07-30 12:04:12 +02001713
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001714 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1715 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001716 if (!orig_neigh_node)
1717 return;
1718
1719 /* neighbor has to indicate direct link and it has to
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001720 * come via the corresponding interface
1721 * save packet seqno for bidirectional check
1722 */
Marek Lindnerfc957272011-07-30 12:04:12 +02001723 if (has_directlink_flag &&
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001724 batadv_compare_eth(if_incoming->net_dev->dev_addr,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001725 ogm_packet->orig)) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001726 if_num = if_incoming->if_num;
1727 offset = if_num * BATADV_NUM_WORDS;
Marek Lindnerfc957272011-07-30 12:04:12 +02001728
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001729 spin_lock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
Antonio Quartulli32db6aa2014-09-01 14:37:29 +02001730 word = &orig_neigh_node->bat_iv.bcast_own[offset];
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001731 bit_pos = if_incoming_seqno - 2;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001732 bit_pos -= ntohl(ogm_packet->seqno);
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001733 batadv_set_bit(word, bit_pos);
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001734 weight = &orig_neigh_node->bat_iv.bcast_own_sum[if_num];
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001735 *weight = bitmap_weight(word,
1736 BATADV_TQ_LOCAL_WINDOW_SIZE);
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001737 spin_unlock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001738 }
1739
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001740 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001741 "Drop packet: originator packet from myself (via neighbor)\n");
Sven Eckelmann5d967312016-01-17 11:01:09 +01001742 batadv_orig_node_put(orig_neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001743 return;
1744 }
1745
1746 if (is_my_oldorig) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001747 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001748 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1749 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001750 return;
1751 }
1752
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001753 if (ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001754 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001755 "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1756 ethhdr->h_source);
Marek Lindner13b25412012-03-11 06:17:53 +08001757 return;
1758 }
1759
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001760 orig_node = batadv_iv_ogm_orig_get(bat_priv, ogm_packet->orig);
Marek Lindnerfc957272011-07-30 12:04:12 +02001761 if (!orig_node)
1762 return;
1763
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001764 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1765 if_incoming, BATADV_IF_DEFAULT);
Marek Lindnerfc957272011-07-30 12:04:12 +02001766
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001767 rcu_read_lock();
1768 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1769 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1770 continue;
1771
1772 if (hard_iface->soft_iface != bat_priv->soft_iface)
1773 continue;
1774
Sven Eckelmann27353442016-03-05 16:09:16 +01001775 if (!kref_get_unless_zero(&hard_iface->refcount))
1776 continue;
1777
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001778 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1779 if_incoming, hard_iface);
Sven Eckelmann27353442016-03-05 16:09:16 +01001780
1781 batadv_hardif_put(hard_iface);
Marek Lindnerfc957272011-07-30 12:04:12 +02001782 }
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001783 rcu_read_unlock();
Marek Lindnerfc957272011-07-30 12:04:12 +02001784
Sven Eckelmann5d967312016-01-17 11:01:09 +01001785 batadv_orig_node_put(orig_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001786}
1787
Antonio Quartullif0d97252016-05-03 01:45:34 +08001788static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work)
1789{
1790 struct delayed_work *delayed_work;
1791 struct batadv_forw_packet *forw_packet;
1792 struct batadv_priv *bat_priv;
Sven Eckelmannbd687fe2016-07-17 21:04:00 +02001793 bool dropped = false;
Antonio Quartullif0d97252016-05-03 01:45:34 +08001794
1795 delayed_work = to_delayed_work(work);
1796 forw_packet = container_of(delayed_work, struct batadv_forw_packet,
1797 delayed_work);
1798 bat_priv = netdev_priv(forw_packet->if_incoming->soft_iface);
Antonio Quartullif0d97252016-05-03 01:45:34 +08001799
Sven Eckelmannbd687fe2016-07-17 21:04:00 +02001800 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING) {
1801 dropped = true;
Antonio Quartullif0d97252016-05-03 01:45:34 +08001802 goto out;
Sven Eckelmannbd687fe2016-07-17 21:04:00 +02001803 }
Antonio Quartullif0d97252016-05-03 01:45:34 +08001804
1805 batadv_iv_ogm_emit(forw_packet);
1806
1807 /* we have to have at least one packet in the queue to determine the
1808 * queues wake up time unless we are shutting down.
1809 *
1810 * only re-schedule if this is the "original" copy, e.g. the OGM of the
1811 * primary interface should only be rescheduled once per period, but
1812 * this function will be called for the forw_packet instances of the
1813 * other secondary interfaces as well.
1814 */
1815 if (forw_packet->own &&
1816 forw_packet->if_incoming == forw_packet->if_outgoing)
1817 batadv_iv_ogm_schedule(forw_packet->if_incoming);
1818
1819out:
Linus LĂĽssing9b4aec62016-11-01 09:44:44 +01001820 /* do we get something for free()? */
1821 if (batadv_forw_packet_steal(forw_packet,
1822 &bat_priv->forw_bat_list_lock))
1823 batadv_forw_packet_free(forw_packet, dropped);
Antonio Quartullif0d97252016-05-03 01:45:34 +08001824}
1825
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001826static int batadv_iv_ogm_receive(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001827 struct batadv_hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +02001828{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001829 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001830 struct batadv_ogm_packet *ogm_packet;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001831 u8 *packet_pos;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001832 int ogm_offset;
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001833 bool res;
1834 int ret = NET_RX_DROP;
Marek Lindnerc3e29312012-03-04 16:56:25 +08001835
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001836 res = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
1837 if (!res)
1838 goto free_skb;
Marek Lindnerfc957272011-07-30 12:04:12 +02001839
Marek Lindneredbf12b2012-03-11 06:17:49 +08001840 /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
1841 * that does not have B.A.T.M.A.N. IV enabled ?
1842 */
Antonio Quartulli29824a52016-05-25 23:27:31 +08001843 if (bat_priv->algo_ops->iface.enable != batadv_iv_ogm_iface_enable)
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001844 goto free_skb;
Marek Lindneredbf12b2012-03-11 06:17:49 +08001845
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001846 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
1847 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
Martin Hundebøllf8214862012-04-20 17:02:45 +02001848 skb->len + ETH_HLEN);
1849
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001850 ogm_offset = 0;
1851 ogm_packet = (struct batadv_ogm_packet *)skb->data;
Marek Lindnerfc957272011-07-30 12:04:12 +02001852
1853 /* unpack the aggregated packets and process them one by one */
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001854 while (batadv_iv_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
1855 ogm_packet->tvlv_len)) {
1856 batadv_iv_ogm_process(skb, ogm_offset, if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001857
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001858 ogm_offset += BATADV_OGM_HLEN;
1859 ogm_offset += ntohs(ogm_packet->tvlv_len);
Marek Lindnerfc957272011-07-30 12:04:12 +02001860
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001861 packet_pos = skb->data + ogm_offset;
1862 ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Marek Lindnerb47506d2013-03-04 10:39:49 +08001863 }
Marek Lindnerc3e29312012-03-04 16:56:25 +08001864
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001865 ret = NET_RX_SUCCESS;
1866
1867free_skb:
1868 if (ret == NET_RX_SUCCESS)
1869 consume_skb(skb);
1870 else
1871 kfree_skb(skb);
1872
1873 return ret;
Marek Lindnerfc957272011-07-30 12:04:12 +02001874}
Marek Lindner1c280472011-11-28 17:40:17 +08001875
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02001876#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001877/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001878 * batadv_iv_ogm_orig_print_neigh() - print neighbors for the originator table
Simon Wunderlich89652332013-11-13 19:14:46 +01001879 * @orig_node: the orig_node for which the neighbors are printed
1880 * @if_outgoing: outgoing interface for these entries
1881 * @seq: debugfs table seq_file struct
1882 *
1883 * Must be called while holding an rcu lock.
1884 */
1885static void
1886batadv_iv_ogm_orig_print_neigh(struct batadv_orig_node *orig_node,
1887 struct batadv_hard_iface *if_outgoing,
1888 struct seq_file *seq)
1889{
1890 struct batadv_neigh_node *neigh_node;
1891 struct batadv_neigh_ifinfo *n_ifinfo;
1892
1893 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1894 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
1895 if (!n_ifinfo)
1896 continue;
1897
1898 seq_printf(seq, " %pM (%3i)",
1899 neigh_node->addr,
1900 n_ifinfo->bat_iv.tq_avg);
1901
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001902 batadv_neigh_ifinfo_put(n_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01001903 }
1904}
1905
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001906/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001907 * batadv_iv_ogm_orig_print() - print the originator table
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001908 * @bat_priv: the bat priv with all the soft interface information
1909 * @seq: debugfs table seq_file struct
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001910 * @if_outgoing: the outgoing interface for which this should be printed
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001911 */
1912static void batadv_iv_ogm_orig_print(struct batadv_priv *bat_priv,
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001913 struct seq_file *seq,
1914 struct batadv_hard_iface *if_outgoing)
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001915{
Simon Wunderlich89652332013-11-13 19:14:46 +01001916 struct batadv_neigh_node *neigh_node;
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001917 struct batadv_hashtable *hash = bat_priv->orig_hash;
1918 int last_seen_msecs, last_seen_secs;
1919 struct batadv_orig_node *orig_node;
Simon Wunderlich89652332013-11-13 19:14:46 +01001920 struct batadv_neigh_ifinfo *n_ifinfo;
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001921 unsigned long last_seen_jiffies;
1922 struct hlist_head *head;
1923 int batman_count = 0;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001924 u32 i;
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001925
Antonio Quartulli925a6f32016-03-12 10:30:18 +01001926 seq_puts(seq,
1927 " Originator last-seen (#/255) Nexthop [outgoingIF]: Potential nexthops ...\n");
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001928
1929 for (i = 0; i < hash->size; i++) {
1930 head = &hash->table[i];
1931
1932 rcu_read_lock();
1933 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001934 neigh_node = batadv_orig_router_get(orig_node,
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001935 if_outgoing);
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001936 if (!neigh_node)
1937 continue;
1938
Simon Wunderlich89652332013-11-13 19:14:46 +01001939 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node,
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001940 if_outgoing);
Simon Wunderlich89652332013-11-13 19:14:46 +01001941 if (!n_ifinfo)
1942 goto next;
1943
1944 if (n_ifinfo->bat_iv.tq_avg == 0)
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001945 goto next;
1946
1947 last_seen_jiffies = jiffies - orig_node->last_seen;
1948 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
1949 last_seen_secs = last_seen_msecs / 1000;
1950 last_seen_msecs = last_seen_msecs % 1000;
1951
1952 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
1953 orig_node->orig, last_seen_secs,
Simon Wunderlich89652332013-11-13 19:14:46 +01001954 last_seen_msecs, n_ifinfo->bat_iv.tq_avg,
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001955 neigh_node->addr,
1956 neigh_node->if_incoming->net_dev->name);
1957
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001958 batadv_iv_ogm_orig_print_neigh(orig_node, if_outgoing,
1959 seq);
Markus Elfring626caae2017-05-06 17:50:13 +02001960 seq_putc(seq, '\n');
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001961 batman_count++;
1962
1963next:
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001964 batadv_neigh_node_put(neigh_node);
Simon Wunderlich89652332013-11-13 19:14:46 +01001965 if (n_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001966 batadv_neigh_ifinfo_put(n_ifinfo);
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001967 }
1968 rcu_read_unlock();
1969 }
1970
1971 if (batman_count == 0)
1972 seq_puts(seq, "No batman nodes in range ...\n");
1973}
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02001974#endif
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001975
Antonio Quartullia3285a82013-09-02 12:15:04 +02001976/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001977 * batadv_iv_ogm_neigh_get_tq_avg() - Get the TQ average for a neighbour on a
Matthias Schiffer024f99c2016-07-03 13:31:40 +02001978 * given outgoing interface.
1979 * @neigh_node: Neighbour of interest
1980 * @if_outgoing: Outgoing interface of interest
1981 * @tq_avg: Pointer of where to store the TQ average
1982 *
1983 * Return: False if no average TQ available, otherwise true.
1984 */
1985static bool
1986batadv_iv_ogm_neigh_get_tq_avg(struct batadv_neigh_node *neigh_node,
1987 struct batadv_hard_iface *if_outgoing,
1988 u8 *tq_avg)
1989{
1990 struct batadv_neigh_ifinfo *n_ifinfo;
1991
1992 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
1993 if (!n_ifinfo)
1994 return false;
1995
1996 *tq_avg = n_ifinfo->bat_iv.tq_avg;
1997 batadv_neigh_ifinfo_put(n_ifinfo);
1998
1999 return true;
2000}
2001
2002/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002003 * batadv_iv_ogm_orig_dump_subentry() - Dump an originator subentry into a
Matthias Schiffer024f99c2016-07-03 13:31:40 +02002004 * message
2005 * @msg: Netlink message to dump into
2006 * @portid: Port making netlink request
2007 * @seq: Sequence number of netlink message
2008 * @bat_priv: The bat priv with all the soft interface information
2009 * @if_outgoing: Limit dump to entries with this outgoing interface
2010 * @orig_node: Originator to dump
2011 * @neigh_node: Single hops neighbour
2012 * @best: Is the best originator
2013 *
2014 * Return: Error code, or 0 on success
2015 */
2016static int
2017batadv_iv_ogm_orig_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
2018 struct batadv_priv *bat_priv,
2019 struct batadv_hard_iface *if_outgoing,
2020 struct batadv_orig_node *orig_node,
2021 struct batadv_neigh_node *neigh_node,
2022 bool best)
2023{
2024 void *hdr;
2025 u8 tq_avg;
2026 unsigned int last_seen_msecs;
2027
2028 last_seen_msecs = jiffies_to_msecs(jiffies - orig_node->last_seen);
2029
2030 if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node, if_outgoing, &tq_avg))
2031 return 0;
2032
2033 if (if_outgoing != BATADV_IF_DEFAULT &&
2034 if_outgoing != neigh_node->if_incoming)
2035 return 0;
2036
2037 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2038 NLM_F_MULTI, BATADV_CMD_GET_ORIGINATORS);
2039 if (!hdr)
2040 return -ENOBUFS;
2041
2042 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
2043 orig_node->orig) ||
2044 nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
2045 neigh_node->addr) ||
2046 nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
2047 neigh_node->if_incoming->net_dev->ifindex) ||
2048 nla_put_u8(msg, BATADV_ATTR_TQ, tq_avg) ||
2049 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
2050 last_seen_msecs))
2051 goto nla_put_failure;
2052
2053 if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
2054 goto nla_put_failure;
2055
2056 genlmsg_end(msg, hdr);
2057 return 0;
2058
2059 nla_put_failure:
2060 genlmsg_cancel(msg, hdr);
2061 return -EMSGSIZE;
2062}
2063
2064/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002065 * batadv_iv_ogm_orig_dump_entry() - Dump an originator entry into a message
Matthias Schiffer024f99c2016-07-03 13:31:40 +02002066 * @msg: Netlink message to dump into
2067 * @portid: Port making netlink request
2068 * @seq: Sequence number of netlink message
2069 * @bat_priv: The bat priv with all the soft interface information
2070 * @if_outgoing: Limit dump to entries with this outgoing interface
2071 * @orig_node: Originator to dump
2072 * @sub_s: Number of sub entries to skip
2073 *
2074 * This function assumes the caller holds rcu_read_lock().
2075 *
2076 * Return: Error code, or 0 on success
2077 */
2078static int
2079batadv_iv_ogm_orig_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
2080 struct batadv_priv *bat_priv,
2081 struct batadv_hard_iface *if_outgoing,
2082 struct batadv_orig_node *orig_node, int *sub_s)
2083{
2084 struct batadv_neigh_node *neigh_node_best;
2085 struct batadv_neigh_node *neigh_node;
2086 int sub = 0;
2087 bool best;
2088 u8 tq_avg_best;
2089
2090 neigh_node_best = batadv_orig_router_get(orig_node, if_outgoing);
2091 if (!neigh_node_best)
2092 goto out;
2093
2094 if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node_best, if_outgoing,
2095 &tq_avg_best))
2096 goto out;
2097
2098 if (tq_avg_best == 0)
2099 goto out;
2100
2101 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
2102 if (sub++ < *sub_s)
2103 continue;
2104
2105 best = (neigh_node == neigh_node_best);
2106
2107 if (batadv_iv_ogm_orig_dump_subentry(msg, portid, seq,
2108 bat_priv, if_outgoing,
2109 orig_node, neigh_node,
2110 best)) {
2111 batadv_neigh_node_put(neigh_node_best);
2112
2113 *sub_s = sub - 1;
2114 return -EMSGSIZE;
2115 }
2116 }
2117
2118 out:
2119 if (neigh_node_best)
2120 batadv_neigh_node_put(neigh_node_best);
2121
2122 *sub_s = 0;
2123 return 0;
2124}
2125
2126/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002127 * batadv_iv_ogm_orig_dump_bucket() - Dump an originator bucket into a
Matthias Schiffer024f99c2016-07-03 13:31:40 +02002128 * message
2129 * @msg: Netlink message to dump into
2130 * @portid: Port making netlink request
2131 * @seq: Sequence number of netlink message
2132 * @bat_priv: The bat priv with all the soft interface information
2133 * @if_outgoing: Limit dump to entries with this outgoing interface
2134 * @head: Bucket to be dumped
2135 * @idx_s: Number of entries to be skipped
2136 * @sub: Number of sub entries to be skipped
2137 *
2138 * Return: Error code, or 0 on success
2139 */
2140static int
2141batadv_iv_ogm_orig_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
2142 struct batadv_priv *bat_priv,
2143 struct batadv_hard_iface *if_outgoing,
2144 struct hlist_head *head, int *idx_s, int *sub)
2145{
2146 struct batadv_orig_node *orig_node;
2147 int idx = 0;
2148
2149 rcu_read_lock();
2150 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
2151 if (idx++ < *idx_s)
2152 continue;
2153
2154 if (batadv_iv_ogm_orig_dump_entry(msg, portid, seq, bat_priv,
2155 if_outgoing, orig_node,
2156 sub)) {
2157 rcu_read_unlock();
2158 *idx_s = idx - 1;
2159 return -EMSGSIZE;
2160 }
2161 }
2162 rcu_read_unlock();
2163
2164 *idx_s = 0;
2165 *sub = 0;
2166 return 0;
2167}
2168
2169/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002170 * batadv_iv_ogm_orig_dump() - Dump the originators into a message
Matthias Schiffer024f99c2016-07-03 13:31:40 +02002171 * @msg: Netlink message to dump into
2172 * @cb: Control block containing additional options
2173 * @bat_priv: The bat priv with all the soft interface information
2174 * @if_outgoing: Limit dump to entries with this outgoing interface
2175 */
2176static void
2177batadv_iv_ogm_orig_dump(struct sk_buff *msg, struct netlink_callback *cb,
2178 struct batadv_priv *bat_priv,
2179 struct batadv_hard_iface *if_outgoing)
2180{
2181 struct batadv_hashtable *hash = bat_priv->orig_hash;
2182 struct hlist_head *head;
2183 int bucket = cb->args[0];
2184 int idx = cb->args[1];
2185 int sub = cb->args[2];
2186 int portid = NETLINK_CB(cb->skb).portid;
2187
2188 while (bucket < hash->size) {
2189 head = &hash->table[bucket];
2190
2191 if (batadv_iv_ogm_orig_dump_bucket(msg, portid,
2192 cb->nlh->nlmsg_seq,
2193 bat_priv, if_outgoing, head,
2194 &idx, &sub))
2195 break;
2196
2197 bucket++;
2198 }
2199
2200 cb->args[0] = bucket;
2201 cb->args[1] = idx;
2202 cb->args[2] = sub;
2203}
2204
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002205#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Matthias Schiffer024f99c2016-07-03 13:31:40 +02002206/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002207 * batadv_iv_hardif_neigh_print() - print a single hop neighbour node
Marek Lindner75874052015-08-04 21:09:57 +08002208 * @seq: neighbour table seq_file struct
2209 * @hardif_neigh: hardif neighbour information
2210 */
2211static void
2212batadv_iv_hardif_neigh_print(struct seq_file *seq,
2213 struct batadv_hardif_neigh_node *hardif_neigh)
2214{
2215 int last_secs, last_msecs;
2216
2217 last_secs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) / 1000;
2218 last_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) % 1000;
2219
2220 seq_printf(seq, " %10s %pM %4i.%03is\n",
2221 hardif_neigh->if_incoming->net_dev->name,
2222 hardif_neigh->addr, last_secs, last_msecs);
2223}
2224
2225/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002226 * batadv_iv_ogm_neigh_print() - print the single hop neighbour list
Marek Lindner75874052015-08-04 21:09:57 +08002227 * @bat_priv: the bat priv with all the soft interface information
2228 * @seq: neighbour table seq_file struct
2229 */
2230static void batadv_iv_neigh_print(struct batadv_priv *bat_priv,
2231 struct seq_file *seq)
2232{
2233 struct net_device *net_dev = (struct net_device *)seq->private;
2234 struct batadv_hardif_neigh_node *hardif_neigh;
2235 struct batadv_hard_iface *hard_iface;
2236 int batman_count = 0;
2237
Antonio Quartulli925a6f32016-03-12 10:30:18 +01002238 seq_puts(seq, " IF Neighbor last-seen\n");
Marek Lindner75874052015-08-04 21:09:57 +08002239
2240 rcu_read_lock();
2241 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
2242 if (hard_iface->soft_iface != net_dev)
2243 continue;
2244
2245 hlist_for_each_entry_rcu(hardif_neigh,
2246 &hard_iface->neigh_list, list) {
2247 batadv_iv_hardif_neigh_print(seq, hardif_neigh);
2248 batman_count++;
2249 }
2250 }
2251 rcu_read_unlock();
2252
2253 if (batman_count == 0)
2254 seq_puts(seq, "No batman nodes in range ...\n");
2255}
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002256#endif
Marek Lindner75874052015-08-04 21:09:57 +08002257
2258/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002259 * batadv_iv_ogm_neigh_diff() - calculate tq difference of two neighbors
Markus Pargmann57b12502016-07-03 11:07:14 +02002260 * @neigh1: the first neighbor object of the comparison
2261 * @if_outgoing1: outgoing interface for the first neighbor
2262 * @neigh2: the second neighbor object of the comparison
2263 * @if_outgoing2: outgoing interface for the second neighbor
2264 * @diff: pointer to integer receiving the calculated difference
2265 *
2266 * The content of *@diff is only valid when this function returns true.
2267 * It is less, equal to or greater than 0 if the metric via neigh1 is lower,
2268 * the same as or higher than the metric via neigh2
2269 *
2270 * Return: true when the difference could be calculated, false otherwise
2271 */
2272static bool batadv_iv_ogm_neigh_diff(struct batadv_neigh_node *neigh1,
2273 struct batadv_hard_iface *if_outgoing1,
2274 struct batadv_neigh_node *neigh2,
2275 struct batadv_hard_iface *if_outgoing2,
2276 int *diff)
2277{
2278 struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
2279 u8 tq1, tq2;
2280 bool ret = true;
2281
2282 neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
2283 neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
2284
2285 if (!neigh1_ifinfo || !neigh2_ifinfo) {
2286 ret = false;
2287 goto out;
2288 }
2289
2290 tq1 = neigh1_ifinfo->bat_iv.tq_avg;
2291 tq2 = neigh2_ifinfo->bat_iv.tq_avg;
2292 *diff = (int)tq1 - (int)tq2;
2293
2294out:
2295 if (neigh1_ifinfo)
2296 batadv_neigh_ifinfo_put(neigh1_ifinfo);
2297 if (neigh2_ifinfo)
2298 batadv_neigh_ifinfo_put(neigh2_ifinfo);
2299
2300 return ret;
2301}
2302
2303/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002304 * batadv_iv_ogm_neigh_dump_neigh() - Dump a neighbour into a netlink message
Matthias Schiffer024f99c2016-07-03 13:31:40 +02002305 * @msg: Netlink message to dump into
2306 * @portid: Port making netlink request
2307 * @seq: Sequence number of netlink message
2308 * @hardif_neigh: Neighbour to be dumped
2309 *
2310 * Return: Error code, or 0 on success
2311 */
2312static int
2313batadv_iv_ogm_neigh_dump_neigh(struct sk_buff *msg, u32 portid, u32 seq,
2314 struct batadv_hardif_neigh_node *hardif_neigh)
2315{
2316 void *hdr;
2317 unsigned int last_seen_msecs;
2318
2319 last_seen_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen);
2320
2321 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2322 NLM_F_MULTI, BATADV_CMD_GET_NEIGHBORS);
2323 if (!hdr)
2324 return -ENOBUFS;
2325
2326 if (nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
2327 hardif_neigh->addr) ||
2328 nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
2329 hardif_neigh->if_incoming->net_dev->ifindex) ||
2330 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
2331 last_seen_msecs))
2332 goto nla_put_failure;
2333
2334 genlmsg_end(msg, hdr);
2335 return 0;
2336
2337 nla_put_failure:
2338 genlmsg_cancel(msg, hdr);
2339 return -EMSGSIZE;
2340}
2341
2342/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002343 * batadv_iv_ogm_neigh_dump_hardif() - Dump the neighbours of a hard interface
Matthias Schiffer024f99c2016-07-03 13:31:40 +02002344 * into a message
2345 * @msg: Netlink message to dump into
2346 * @portid: Port making netlink request
2347 * @seq: Sequence number of netlink message
2348 * @bat_priv: The bat priv with all the soft interface information
2349 * @hard_iface: Hard interface to dump the neighbours for
2350 * @idx_s: Number of entries to skip
2351 *
2352 * This function assumes the caller holds rcu_read_lock().
2353 *
2354 * Return: Error code, or 0 on success
2355 */
2356static int
2357batadv_iv_ogm_neigh_dump_hardif(struct sk_buff *msg, u32 portid, u32 seq,
2358 struct batadv_priv *bat_priv,
2359 struct batadv_hard_iface *hard_iface,
2360 int *idx_s)
2361{
2362 struct batadv_hardif_neigh_node *hardif_neigh;
2363 int idx = 0;
2364
2365 hlist_for_each_entry_rcu(hardif_neigh,
2366 &hard_iface->neigh_list, list) {
2367 if (idx++ < *idx_s)
2368 continue;
2369
2370 if (batadv_iv_ogm_neigh_dump_neigh(msg, portid, seq,
2371 hardif_neigh)) {
2372 *idx_s = idx - 1;
2373 return -EMSGSIZE;
2374 }
2375 }
2376
2377 *idx_s = 0;
2378 return 0;
2379}
2380
2381/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002382 * batadv_iv_ogm_neigh_dump() - Dump the neighbours into a message
Matthias Schiffer024f99c2016-07-03 13:31:40 +02002383 * @msg: Netlink message to dump into
2384 * @cb: Control block containing additional options
2385 * @bat_priv: The bat priv with all the soft interface information
2386 * @single_hardif: Limit dump to this hard interfaace
2387 */
2388static void
2389batadv_iv_ogm_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb,
2390 struct batadv_priv *bat_priv,
2391 struct batadv_hard_iface *single_hardif)
2392{
2393 struct batadv_hard_iface *hard_iface;
2394 int i_hardif = 0;
2395 int i_hardif_s = cb->args[0];
2396 int idx = cb->args[1];
2397 int portid = NETLINK_CB(cb->skb).portid;
2398
2399 rcu_read_lock();
2400 if (single_hardif) {
2401 if (i_hardif_s == 0) {
2402 if (batadv_iv_ogm_neigh_dump_hardif(msg, portid,
2403 cb->nlh->nlmsg_seq,
2404 bat_priv,
2405 single_hardif,
2406 &idx) == 0)
2407 i_hardif++;
2408 }
2409 } else {
2410 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list,
2411 list) {
2412 if (hard_iface->soft_iface != bat_priv->soft_iface)
2413 continue;
2414
2415 if (i_hardif++ < i_hardif_s)
2416 continue;
2417
2418 if (batadv_iv_ogm_neigh_dump_hardif(msg, portid,
2419 cb->nlh->nlmsg_seq,
2420 bat_priv,
2421 hard_iface, &idx)) {
2422 i_hardif--;
2423 break;
2424 }
2425 }
2426 }
2427 rcu_read_unlock();
2428
2429 cb->args[0] = i_hardif;
2430 cb->args[1] = idx;
2431}
2432
2433/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002434 * batadv_iv_ogm_neigh_cmp() - compare the metrics of two neighbors
Antonio Quartullia3285a82013-09-02 12:15:04 +02002435 * @neigh1: the first neighbor object of the comparison
Simon Wunderlich89652332013-11-13 19:14:46 +01002436 * @if_outgoing1: outgoing interface for the first neighbor
Antonio Quartullia3285a82013-09-02 12:15:04 +02002437 * @neigh2: the second neighbor object of the comparison
Simon Wunderlich89652332013-11-13 19:14:46 +01002438 * @if_outgoing2: outgoing interface for the second neighbor
Antonio Quartullia3285a82013-09-02 12:15:04 +02002439 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002440 * Return: a value less, equal to or greater than 0 if the metric via neigh1 is
Antonio Quartullia3285a82013-09-02 12:15:04 +02002441 * lower, the same as or higher than the metric via neigh2
2442 */
2443static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1,
Simon Wunderlich89652332013-11-13 19:14:46 +01002444 struct batadv_hard_iface *if_outgoing1,
2445 struct batadv_neigh_node *neigh2,
2446 struct batadv_hard_iface *if_outgoing2)
Antonio Quartullia3285a82013-09-02 12:15:04 +02002447{
Markus Pargmann57b12502016-07-03 11:07:14 +02002448 bool ret;
Simon Wunderlich89652332013-11-13 19:14:46 +01002449 int diff;
Antonio Quartullia3285a82013-09-02 12:15:04 +02002450
Markus Pargmann57b12502016-07-03 11:07:14 +02002451 ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
2452 if_outgoing2, &diff);
2453 if (!ret)
2454 return 0;
Simon Wunderlich89652332013-11-13 19:14:46 +01002455
2456 return diff;
Antonio Quartullia3285a82013-09-02 12:15:04 +02002457}
2458
Antonio Quartullic43c9812013-09-02 12:15:05 +02002459/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002460 * batadv_iv_ogm_neigh_is_sob() - check if neigh1 is similarly good or better
Simon Wunderlich18165f62015-08-08 02:01:50 +02002461 * than neigh2 from the metric prospective
Antonio Quartullic43c9812013-09-02 12:15:05 +02002462 * @neigh1: the first neighbor object of the comparison
Martin Hundebøll95298a92014-07-15 09:41:05 +02002463 * @if_outgoing1: outgoing interface for the first neighbor
Antonio Quartullic43c9812013-09-02 12:15:05 +02002464 * @neigh2: the second neighbor object of the comparison
Simon Wunderlich89652332013-11-13 19:14:46 +01002465 * @if_outgoing2: outgoing interface for the second neighbor
Martin Hundebøll95298a92014-07-15 09:41:05 +02002466 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002467 * Return: true if the metric via neigh1 is equally good or better than
Simon Wunderlich89652332013-11-13 19:14:46 +01002468 * the metric via neigh2, false otherwise.
2469 */
2470static bool
Simon Wunderlich18165f62015-08-08 02:01:50 +02002471batadv_iv_ogm_neigh_is_sob(struct batadv_neigh_node *neigh1,
Simon Wunderlich89652332013-11-13 19:14:46 +01002472 struct batadv_hard_iface *if_outgoing1,
2473 struct batadv_neigh_node *neigh2,
2474 struct batadv_hard_iface *if_outgoing2)
2475{
Simon Wunderlich89652332013-11-13 19:14:46 +01002476 bool ret;
Markus Pargmann57b12502016-07-03 11:07:14 +02002477 int diff;
Simon Wunderlich89652332013-11-13 19:14:46 +01002478
Markus Pargmann57b12502016-07-03 11:07:14 +02002479 ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
2480 if_outgoing2, &diff);
2481 if (!ret)
2482 return false;
Simon Wunderlich89652332013-11-13 19:14:46 +01002483
Markus Pargmann57b12502016-07-03 11:07:14 +02002484 ret = diff > -BATADV_TQ_SIMILARITY_THRESHOLD;
Simon Wunderlich89652332013-11-13 19:14:46 +01002485 return ret;
Antonio Quartullic43c9812013-09-02 12:15:05 +02002486}
2487
Antonio Quartullif0d97252016-05-03 01:45:34 +08002488static void batadv_iv_iface_activate(struct batadv_hard_iface *hard_iface)
2489{
2490 /* begin scheduling originator messages on that interface */
2491 batadv_iv_ogm_schedule(hard_iface);
2492}
2493
Sven Eckelmann1a9070e2017-03-04 15:48:50 +01002494/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002495 * batadv_iv_init_sel_class() - initialize GW selection class
Sven Eckelmann1a9070e2017-03-04 15:48:50 +01002496 * @bat_priv: the bat priv with all the soft interface information
2497 */
2498static void batadv_iv_init_sel_class(struct batadv_priv *bat_priv)
2499{
2500 /* set default TQ difference threshold to 20 */
2501 atomic_set(&bat_priv->gw.sel_class, 20);
2502}
2503
Antonio Quartulli34d99cf2016-07-03 12:46:33 +02002504static struct batadv_gw_node *
2505batadv_iv_gw_get_best_gw_node(struct batadv_priv *bat_priv)
2506{
2507 struct batadv_neigh_node *router;
2508 struct batadv_neigh_ifinfo *router_ifinfo;
2509 struct batadv_gw_node *gw_node, *curr_gw = NULL;
2510 u64 max_gw_factor = 0;
2511 u64 tmp_gw_factor = 0;
2512 u8 max_tq = 0;
2513 u8 tq_avg;
2514 struct batadv_orig_node *orig_node;
2515
2516 rcu_read_lock();
Sven Eckelmann70ea5ce2016-07-27 12:31:08 +02002517 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
Antonio Quartulli34d99cf2016-07-03 12:46:33 +02002518 orig_node = gw_node->orig_node;
2519 router = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
2520 if (!router)
2521 continue;
2522
2523 router_ifinfo = batadv_neigh_ifinfo_get(router,
2524 BATADV_IF_DEFAULT);
2525 if (!router_ifinfo)
2526 goto next;
2527
2528 if (!kref_get_unless_zero(&gw_node->refcount))
2529 goto next;
2530
2531 tq_avg = router_ifinfo->bat_iv.tq_avg;
2532
2533 switch (atomic_read(&bat_priv->gw.sel_class)) {
2534 case 1: /* fast connection */
2535 tmp_gw_factor = tq_avg * tq_avg;
2536 tmp_gw_factor *= gw_node->bandwidth_down;
2537 tmp_gw_factor *= 100 * 100;
2538 tmp_gw_factor >>= 18;
2539
Sven Eckelmann825ffe12017-08-23 21:52:13 +02002540 if (tmp_gw_factor > max_gw_factor ||
2541 (tmp_gw_factor == max_gw_factor &&
2542 tq_avg > max_tq)) {
Antonio Quartulli34d99cf2016-07-03 12:46:33 +02002543 if (curr_gw)
2544 batadv_gw_node_put(curr_gw);
2545 curr_gw = gw_node;
2546 kref_get(&curr_gw->refcount);
2547 }
2548 break;
2549
2550 default: /* 2: stable connection (use best statistic)
2551 * 3: fast-switch (use best statistic but change as
2552 * soon as a better gateway appears)
2553 * XX: late-switch (use best statistic but change as
2554 * soon as a better gateway appears which has
2555 * $routing_class more tq points)
2556 */
2557 if (tq_avg > max_tq) {
2558 if (curr_gw)
2559 batadv_gw_node_put(curr_gw);
2560 curr_gw = gw_node;
2561 kref_get(&curr_gw->refcount);
2562 }
2563 break;
2564 }
2565
2566 if (tq_avg > max_tq)
2567 max_tq = tq_avg;
2568
2569 if (tmp_gw_factor > max_gw_factor)
2570 max_gw_factor = tmp_gw_factor;
2571
2572 batadv_gw_node_put(gw_node);
2573
2574next:
2575 batadv_neigh_node_put(router);
2576 if (router_ifinfo)
2577 batadv_neigh_ifinfo_put(router_ifinfo);
2578 }
2579 rcu_read_unlock();
2580
2581 return curr_gw;
2582}
2583
2584static bool batadv_iv_gw_is_eligible(struct batadv_priv *bat_priv,
2585 struct batadv_orig_node *curr_gw_orig,
2586 struct batadv_orig_node *orig_node)
2587{
2588 struct batadv_neigh_ifinfo *router_orig_ifinfo = NULL;
2589 struct batadv_neigh_ifinfo *router_gw_ifinfo = NULL;
2590 struct batadv_neigh_node *router_gw = NULL;
2591 struct batadv_neigh_node *router_orig = NULL;
2592 u8 gw_tq_avg, orig_tq_avg;
2593 bool ret = false;
2594
2595 /* dynamic re-election is performed only on fast or late switch */
2596 if (atomic_read(&bat_priv->gw.sel_class) <= 2)
2597 return false;
2598
2599 router_gw = batadv_orig_router_get(curr_gw_orig, BATADV_IF_DEFAULT);
2600 if (!router_gw) {
2601 ret = true;
2602 goto out;
2603 }
2604
2605 router_gw_ifinfo = batadv_neigh_ifinfo_get(router_gw,
2606 BATADV_IF_DEFAULT);
2607 if (!router_gw_ifinfo) {
2608 ret = true;
2609 goto out;
2610 }
2611
2612 router_orig = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
2613 if (!router_orig)
2614 goto out;
2615
2616 router_orig_ifinfo = batadv_neigh_ifinfo_get(router_orig,
2617 BATADV_IF_DEFAULT);
2618 if (!router_orig_ifinfo)
2619 goto out;
2620
2621 gw_tq_avg = router_gw_ifinfo->bat_iv.tq_avg;
2622 orig_tq_avg = router_orig_ifinfo->bat_iv.tq_avg;
2623
2624 /* the TQ value has to be better */
2625 if (orig_tq_avg < gw_tq_avg)
2626 goto out;
2627
2628 /* if the routing class is greater than 3 the value tells us how much
2629 * greater the TQ value of the new gateway must be
2630 */
2631 if ((atomic_read(&bat_priv->gw.sel_class) > 3) &&
2632 (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw.sel_class)))
2633 goto out;
2634
2635 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
2636 "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n",
2637 gw_tq_avg, orig_tq_avg);
2638
2639 ret = true;
2640out:
2641 if (router_gw_ifinfo)
2642 batadv_neigh_ifinfo_put(router_gw_ifinfo);
2643 if (router_orig_ifinfo)
2644 batadv_neigh_ifinfo_put(router_orig_ifinfo);
2645 if (router_gw)
2646 batadv_neigh_node_put(router_gw);
2647 if (router_orig)
2648 batadv_neigh_node_put(router_orig);
2649
2650 return ret;
2651}
2652
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002653#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Antonio Quartulli34d99cf2016-07-03 12:46:33 +02002654/* fails if orig_node has no router */
2655static int batadv_iv_gw_write_buffer_text(struct batadv_priv *bat_priv,
2656 struct seq_file *seq,
2657 const struct batadv_gw_node *gw_node)
2658{
2659 struct batadv_gw_node *curr_gw;
2660 struct batadv_neigh_node *router;
2661 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
2662 int ret = -1;
2663
2664 router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
2665 if (!router)
2666 goto out;
2667
2668 router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
2669 if (!router_ifinfo)
2670 goto out;
2671
2672 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
2673
2674 seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n",
2675 (curr_gw == gw_node ? "=>" : " "),
2676 gw_node->orig_node->orig,
2677 router_ifinfo->bat_iv.tq_avg, router->addr,
2678 router->if_incoming->net_dev->name,
2679 gw_node->bandwidth_down / 10,
2680 gw_node->bandwidth_down % 10,
2681 gw_node->bandwidth_up / 10,
2682 gw_node->bandwidth_up % 10);
2683 ret = seq_has_overflowed(seq) ? -1 : 0;
2684
2685 if (curr_gw)
2686 batadv_gw_node_put(curr_gw);
2687out:
2688 if (router_ifinfo)
2689 batadv_neigh_ifinfo_put(router_ifinfo);
2690 if (router)
2691 batadv_neigh_node_put(router);
2692 return ret;
2693}
2694
2695static void batadv_iv_gw_print(struct batadv_priv *bat_priv,
2696 struct seq_file *seq)
2697{
2698 struct batadv_gw_node *gw_node;
2699 int gw_count = 0;
2700
2701 seq_puts(seq,
2702 " Gateway (#/255) Nexthop [outgoingIF]: advertised uplink bandwidth\n");
2703
2704 rcu_read_lock();
Sven Eckelmann70ea5ce2016-07-27 12:31:08 +02002705 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
Antonio Quartulli34d99cf2016-07-03 12:46:33 +02002706 /* fails if orig_node has no router */
2707 if (batadv_iv_gw_write_buffer_text(bat_priv, seq, gw_node) < 0)
2708 continue;
2709
2710 gw_count++;
2711 }
2712 rcu_read_unlock();
2713
2714 if (gw_count == 0)
2715 seq_puts(seq, "No gateways in range ...\n");
2716}
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002717#endif
Antonio Quartulli34d99cf2016-07-03 12:46:33 +02002718
Andrew Lunnefb766a2016-07-03 13:31:43 +02002719/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002720 * batadv_iv_gw_dump_entry() - Dump a gateway into a message
Andrew Lunnefb766a2016-07-03 13:31:43 +02002721 * @msg: Netlink message to dump into
2722 * @portid: Port making netlink request
2723 * @seq: Sequence number of netlink message
2724 * @bat_priv: The bat priv with all the soft interface information
2725 * @gw_node: Gateway to be dumped
2726 *
2727 * Return: Error code, or 0 on success
2728 */
2729static int batadv_iv_gw_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
2730 struct batadv_priv *bat_priv,
2731 struct batadv_gw_node *gw_node)
2732{
2733 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
2734 struct batadv_neigh_node *router;
2735 struct batadv_gw_node *curr_gw;
Sven Eckelmann10d57022018-02-19 14:08:52 +01002736 int ret = 0;
Andrew Lunnefb766a2016-07-03 13:31:43 +02002737 void *hdr;
2738
2739 router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
2740 if (!router)
2741 goto out;
2742
2743 router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
2744 if (!router_ifinfo)
2745 goto out;
2746
2747 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
2748
2749 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2750 NLM_F_MULTI, BATADV_CMD_GET_GATEWAYS);
2751 if (!hdr) {
2752 ret = -ENOBUFS;
2753 goto out;
2754 }
2755
2756 ret = -EMSGSIZE;
2757
2758 if (curr_gw == gw_node)
2759 if (nla_put_flag(msg, BATADV_ATTR_FLAG_BEST)) {
2760 genlmsg_cancel(msg, hdr);
2761 goto out;
2762 }
2763
2764 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
2765 gw_node->orig_node->orig) ||
2766 nla_put_u8(msg, BATADV_ATTR_TQ, router_ifinfo->bat_iv.tq_avg) ||
2767 nla_put(msg, BATADV_ATTR_ROUTER, ETH_ALEN,
2768 router->addr) ||
2769 nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
2770 router->if_incoming->net_dev->name) ||
2771 nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_DOWN,
2772 gw_node->bandwidth_down) ||
2773 nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_UP,
2774 gw_node->bandwidth_up)) {
2775 genlmsg_cancel(msg, hdr);
2776 goto out;
2777 }
2778
2779 genlmsg_end(msg, hdr);
2780 ret = 0;
2781
2782out:
2783 if (router_ifinfo)
2784 batadv_neigh_ifinfo_put(router_ifinfo);
2785 if (router)
2786 batadv_neigh_node_put(router);
2787 return ret;
2788}
2789
2790/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002791 * batadv_iv_gw_dump() - Dump gateways into a message
Andrew Lunnefb766a2016-07-03 13:31:43 +02002792 * @msg: Netlink message to dump into
2793 * @cb: Control block containing additional options
2794 * @bat_priv: The bat priv with all the soft interface information
2795 */
2796static void batadv_iv_gw_dump(struct sk_buff *msg, struct netlink_callback *cb,
2797 struct batadv_priv *bat_priv)
2798{
2799 int portid = NETLINK_CB(cb->skb).portid;
2800 struct batadv_gw_node *gw_node;
2801 int idx_skip = cb->args[0];
2802 int idx = 0;
2803
2804 rcu_read_lock();
Sven Eckelmann70ea5ce2016-07-27 12:31:08 +02002805 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
Andrew Lunnefb766a2016-07-03 13:31:43 +02002806 if (idx++ < idx_skip)
2807 continue;
2808
2809 if (batadv_iv_gw_dump_entry(msg, portid, cb->nlh->nlmsg_seq,
2810 bat_priv, gw_node)) {
2811 idx_skip = idx - 1;
2812 goto unlock;
2813 }
2814 }
2815
2816 idx_skip = idx;
2817unlock:
2818 rcu_read_unlock();
2819
2820 cb->args[0] = idx_skip;
2821}
2822
Sven Eckelmann56303d32012-06-05 22:31:31 +02002823static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
Marek Lindner519d3492012-04-18 17:15:57 +08002824 .name = "BATMAN_IV",
Antonio Quartulli29824a52016-05-25 23:27:31 +08002825 .iface = {
2826 .activate = batadv_iv_iface_activate,
2827 .enable = batadv_iv_ogm_iface_enable,
2828 .disable = batadv_iv_ogm_iface_disable,
2829 .update_mac = batadv_iv_ogm_iface_update_mac,
2830 .primary_set = batadv_iv_ogm_primary_iface_set,
2831 },
2832 .neigh = {
2833 .cmp = batadv_iv_ogm_neigh_cmp,
2834 .is_similar_or_better = batadv_iv_ogm_neigh_is_sob,
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002835#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Antonio Quartulli29824a52016-05-25 23:27:31 +08002836 .print = batadv_iv_neigh_print,
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002837#endif
Matthias Schiffer024f99c2016-07-03 13:31:40 +02002838 .dump = batadv_iv_ogm_neigh_dump,
Antonio Quartulli29824a52016-05-25 23:27:31 +08002839 },
2840 .orig = {
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002841#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Antonio Quartulli29824a52016-05-25 23:27:31 +08002842 .print = batadv_iv_ogm_orig_print,
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002843#endif
Matthias Schiffer024f99c2016-07-03 13:31:40 +02002844 .dump = batadv_iv_ogm_orig_dump,
Antonio Quartulli29824a52016-05-25 23:27:31 +08002845 .free = batadv_iv_ogm_orig_free,
2846 .add_if = batadv_iv_ogm_orig_add_if,
2847 .del_if = batadv_iv_ogm_orig_del_if,
2848 },
Antonio Quartulli34d99cf2016-07-03 12:46:33 +02002849 .gw = {
Sven Eckelmann1a9070e2017-03-04 15:48:50 +01002850 .init_sel_class = batadv_iv_init_sel_class,
Antonio Quartulli34d99cf2016-07-03 12:46:33 +02002851 .get_best_gw_node = batadv_iv_gw_get_best_gw_node,
2852 .is_eligible = batadv_iv_gw_is_eligible,
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002853#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Antonio Quartulli34d99cf2016-07-03 12:46:33 +02002854 .print = batadv_iv_gw_print,
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002855#endif
Andrew Lunnefb766a2016-07-03 13:31:43 +02002856 .dump = batadv_iv_gw_dump,
Antonio Quartulli34d99cf2016-07-03 12:46:33 +02002857 },
Marek Lindner1c280472011-11-28 17:40:17 +08002858};
2859
Sven Eckelmannff15c272017-12-02 19:51:53 +01002860/**
2861 * batadv_iv_init() - B.A.T.M.A.N. IV initialization function
2862 *
2863 * Return: 0 on success or negative error number in case of failure
2864 */
Sven Eckelmann81c524f2012-05-12 02:09:22 +02002865int __init batadv_iv_init(void)
Marek Lindner1c280472011-11-28 17:40:17 +08002866{
Marek Lindnerc3e29312012-03-04 16:56:25 +08002867 int ret;
2868
2869 /* batman originator packet */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002870 ret = batadv_recv_handler_register(BATADV_IV_OGM,
2871 batadv_iv_ogm_receive);
Marek Lindnerc3e29312012-03-04 16:56:25 +08002872 if (ret < 0)
2873 goto out;
2874
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02002875 ret = batadv_algo_register(&batadv_batman_iv);
Marek Lindnerc3e29312012-03-04 16:56:25 +08002876 if (ret < 0)
2877 goto handler_unregister;
2878
2879 goto out;
2880
2881handler_unregister:
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002882 batadv_recv_handler_unregister(BATADV_IV_OGM);
Marek Lindnerc3e29312012-03-04 16:56:25 +08002883out:
2884 return ret;
Marek Lindner1c280472011-11-28 17:40:17 +08002885}