blob: 2b663622bdb450f47d9112980fbd85479f53e7c5 [file] [log] [blame]
Sven Eckelmann0046b042016-01-01 00:01:03 +01001/* Copyright (C) 2007-2016 B.A.T.M.A.N. contributors:
Marek Lindnerfc957272011-07-30 12:04:12 +02002 *
3 * Marek Lindner, Simon Wunderlich
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
Antonio Quartulliebf38fb2013-11-03 20:40:48 +010015 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Marek Lindnerfc957272011-07-30 12:04:12 +020016 */
17
Sven Eckelmanna2d08162016-05-15 11:07:46 +020018#include "bat_iv_ogm.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020019#include "main.h"
20
21#include <linux/atomic.h>
22#include <linux/bitmap.h>
23#include <linux/bitops.h>
24#include <linux/bug.h>
25#include <linux/byteorder/generic.h>
26#include <linux/cache.h>
27#include <linux/errno.h>
28#include <linux/etherdevice.h>
29#include <linux/fs.h>
30#include <linux/if_ether.h>
31#include <linux/init.h>
32#include <linux/jiffies.h>
Antonio Quartullif0d97252016-05-03 01:45:34 +080033#include <linux/kernel.h>
Sven Eckelmann77ae32e2016-01-16 10:29:53 +010034#include <linux/kref.h>
Sven Eckelmannfcafa5e2016-05-15 11:07:42 +020035#include <linux/list.h>
Sven Eckelmann64ae74452016-02-22 22:56:34 +010036#include <linux/lockdep.h>
Sven Eckelmann1f9fd182020-03-16 23:31:04 +010037#include <linux/mutex.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>
55#include <uapi/linux/batman_adv.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020056
Sven Eckelmanna2d08162016-05-15 11:07:46 +020057#include "bat_algo.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020058#include "bitarray.h"
Antonio Quartulli34d99cf2016-07-03 12:46:33 +020059#include "gateway_client.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020060#include "hard-interface.h"
61#include "hash.h"
Sven Eckelmannba412082016-05-15 23:48:31 +020062#include "log.h"
Matthias Schiffer024f99c2016-07-03 13:31:40 +020063#include "netlink.h"
Martin Hundebølld56b1702013-01-25 11:12:39 +010064#include "network-coding.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020065#include "originator.h"
66#include "packet.h"
67#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
Markus Pargmann9f52ee12014-12-26 12:41:34 +010076 * @BATADV_NO_DUP: the packet is no duplicate
Antonio Quartulli791c2a22013-08-17 12:44:44 +020077 * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for the
78 * neighbor)
79 * @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor
80 * @BATADV_PROTECTED: originator is currently protected (after reboot)
81 */
82enum batadv_dup_status {
83 BATADV_NO_DUP = 0,
84 BATADV_ORIG_DUP,
85 BATADV_NEIGH_DUP,
86 BATADV_PROTECTED,
87};
88
Antonio Quartulli24a5dee2013-04-08 09:38:12 +020089/**
90 * batadv_ring_buffer_set - update the ring buffer with the given value
91 * @lq_recv: pointer to the ring buffer
92 * @lq_index: index to store the value at
93 * @value: value to store in the ring buffer
94 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020095static void batadv_ring_buffer_set(u8 lq_recv[], u8 *lq_index, u8 value)
Antonio Quartulli24a5dee2013-04-08 09:38:12 +020096{
97 lq_recv[*lq_index] = value;
98 *lq_index = (*lq_index + 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE;
99}
100
101/**
Markus Pargmannd491dbb2014-12-26 12:41:36 +0100102 * batadv_ring_buffer_avg - compute the average of all non-zero values stored
Antonio Quartulli24a5dee2013-04-08 09:38:12 +0200103 * in the given ring buffer
104 * @lq_recv: pointer to the ring buffer
105 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200106 * Return: computed average value.
Antonio Quartulli24a5dee2013-04-08 09:38:12 +0200107 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200108static u8 batadv_ring_buffer_avg(const u8 lq_recv[])
Antonio Quartulli24a5dee2013-04-08 09:38:12 +0200109{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200110 const u8 *ptr;
111 u16 count = 0;
112 u16 i = 0;
113 u16 sum = 0;
Antonio Quartulli24a5dee2013-04-08 09:38:12 +0200114
115 ptr = lq_recv;
116
117 while (i < BATADV_TQ_GLOBAL_WINDOW_SIZE) {
118 if (*ptr != 0) {
119 count++;
120 sum += *ptr;
121 }
122
123 i++;
124 ptr++;
125 }
126
127 if (count == 0)
128 return 0;
129
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200130 return (u8)(sum / count);
Antonio Quartulli24a5dee2013-04-08 09:38:12 +0200131}
David S. Millerd98cae64e2013-06-19 16:49:39 -0700132
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200133/**
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200134 * batadv_iv_ogm_orig_free - free the private resources allocated for this
135 * orig_node
136 * @orig_node: the orig_node for which the resources have to be free'd
137 */
138static void batadv_iv_ogm_orig_free(struct batadv_orig_node *orig_node)
139{
140 kfree(orig_node->bat_iv.bcast_own);
141 kfree(orig_node->bat_iv.bcast_own_sum);
142}
143
144/**
145 * batadv_iv_ogm_orig_add_if - change the private structures of the orig_node to
146 * include the new hard-interface
147 * @orig_node: the orig_node that has to be changed
148 * @max_if_num: the current amount of interfaces
149 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200150 * Return: 0 on success, a negative error code otherwise.
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200151 */
152static int batadv_iv_ogm_orig_add_if(struct batadv_orig_node *orig_node,
Sven Eckelmann8142ea62020-03-16 23:30:53 +0100153 unsigned int max_if_num)
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200154{
155 void *data_ptr;
Antonio Quartulli0185dda2014-05-24 06:43:47 +0200156 size_t old_size;
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200157 int ret = -ENOMEM;
158
159 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
160
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200161 old_size = (max_if_num - 1) * sizeof(unsigned long) * BATADV_NUM_WORDS;
Antonio Quartulli0185dda2014-05-24 06:43:47 +0200162 data_ptr = kmalloc_array(max_if_num,
163 BATADV_NUM_WORDS * sizeof(unsigned long),
164 GFP_ATOMIC);
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200165 if (!data_ptr)
166 goto unlock;
167
168 memcpy(data_ptr, orig_node->bat_iv.bcast_own, old_size);
169 kfree(orig_node->bat_iv.bcast_own);
170 orig_node->bat_iv.bcast_own = data_ptr;
171
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200172 data_ptr = kmalloc_array(max_if_num, sizeof(u8), GFP_ATOMIC);
Sven Eckelmannf7dcdf52016-02-22 22:56:33 +0100173 if (!data_ptr)
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200174 goto unlock;
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200175
176 memcpy(data_ptr, orig_node->bat_iv.bcast_own_sum,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200177 (max_if_num - 1) * sizeof(u8));
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200178 kfree(orig_node->bat_iv.bcast_own_sum);
179 orig_node->bat_iv.bcast_own_sum = data_ptr;
180
181 ret = 0;
182
183unlock:
184 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
185
186 return ret;
187}
188
189/**
Sven Eckelmann64ae74452016-02-22 22:56:34 +0100190 * batadv_iv_ogm_drop_bcast_own_entry - drop section of bcast_own
191 * @orig_node: the orig_node that has to be changed
192 * @max_if_num: the current amount of interfaces
193 * @del_if_num: the index of the interface being removed
194 */
195static void
196batadv_iv_ogm_drop_bcast_own_entry(struct batadv_orig_node *orig_node,
Sven Eckelmann8142ea62020-03-16 23:30:53 +0100197 unsigned int max_if_num,
198 unsigned int del_if_num)
Sven Eckelmann64ae74452016-02-22 22:56:34 +0100199{
200 size_t chunk_size;
201 size_t if_offset;
202 void *data_ptr;
203
204 lockdep_assert_held(&orig_node->bat_iv.ogm_cnt_lock);
205
206 chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS;
207 data_ptr = kmalloc_array(max_if_num, chunk_size, GFP_ATOMIC);
208 if (!data_ptr)
209 /* use old buffer when new one could not be allocated */
210 data_ptr = orig_node->bat_iv.bcast_own;
211
212 /* copy first part */
213 memmove(data_ptr, orig_node->bat_iv.bcast_own, del_if_num * chunk_size);
214
215 /* copy second part */
216 if_offset = (del_if_num + 1) * chunk_size;
217 memmove((char *)data_ptr + del_if_num * chunk_size,
218 (uint8_t *)orig_node->bat_iv.bcast_own + if_offset,
219 (max_if_num - del_if_num) * chunk_size);
220
221 /* bcast_own was shrunk down in new buffer; free old one */
222 if (orig_node->bat_iv.bcast_own != data_ptr) {
223 kfree(orig_node->bat_iv.bcast_own);
224 orig_node->bat_iv.bcast_own = data_ptr;
225 }
226}
227
228/**
229 * batadv_iv_ogm_drop_bcast_own_sum_entry - drop section of bcast_own_sum
230 * @orig_node: the orig_node that has to be changed
231 * @max_if_num: the current amount of interfaces
232 * @del_if_num: the index of the interface being removed
233 */
234static void
235batadv_iv_ogm_drop_bcast_own_sum_entry(struct batadv_orig_node *orig_node,
Sven Eckelmann8142ea62020-03-16 23:30:53 +0100236 unsigned int max_if_num,
237 unsigned int del_if_num)
Sven Eckelmann64ae74452016-02-22 22:56:34 +0100238{
239 size_t if_offset;
240 void *data_ptr;
241
242 lockdep_assert_held(&orig_node->bat_iv.ogm_cnt_lock);
243
244 data_ptr = kmalloc_array(max_if_num, sizeof(u8), GFP_ATOMIC);
245 if (!data_ptr)
246 /* use old buffer when new one could not be allocated */
247 data_ptr = orig_node->bat_iv.bcast_own_sum;
248
249 memmove(data_ptr, orig_node->bat_iv.bcast_own_sum,
250 del_if_num * sizeof(u8));
251
252 if_offset = (del_if_num + 1) * sizeof(u8);
253 memmove((char *)data_ptr + del_if_num * sizeof(u8),
254 orig_node->bat_iv.bcast_own_sum + if_offset,
255 (max_if_num - del_if_num) * sizeof(u8));
256
257 /* bcast_own_sum was shrunk down in new buffer; free old one */
258 if (orig_node->bat_iv.bcast_own_sum != data_ptr) {
259 kfree(orig_node->bat_iv.bcast_own_sum);
260 orig_node->bat_iv.bcast_own_sum = data_ptr;
261 }
262}
263
264/**
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200265 * batadv_iv_ogm_orig_del_if - change the private structures of the orig_node to
266 * exclude the removed interface
267 * @orig_node: the orig_node that has to be changed
268 * @max_if_num: the current amount of interfaces
269 * @del_if_num: the index of the interface being removed
270 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200271 * Return: 0 on success, a negative error code otherwise.
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200272 */
273static int batadv_iv_ogm_orig_del_if(struct batadv_orig_node *orig_node,
Sven Eckelmann8142ea62020-03-16 23:30:53 +0100274 unsigned int max_if_num,
275 unsigned int del_if_num)
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200276{
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200277 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
278
Sven Eckelmann64ae74452016-02-22 22:56:34 +0100279 if (max_if_num == 0) {
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200280 kfree(orig_node->bat_iv.bcast_own);
Sven Eckelmann64ae74452016-02-22 22:56:34 +0100281 kfree(orig_node->bat_iv.bcast_own_sum);
282 orig_node->bat_iv.bcast_own = NULL;
283 orig_node->bat_iv.bcast_own_sum = NULL;
284 } else {
285 batadv_iv_ogm_drop_bcast_own_entry(orig_node, max_if_num,
286 del_if_num);
287 batadv_iv_ogm_drop_bcast_own_sum_entry(orig_node, max_if_num,
288 del_if_num);
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200289 }
290
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200291 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
292
Sven Eckelmann64ae74452016-02-22 22:56:34 +0100293 return 0;
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200294}
295
296/**
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200297 * batadv_iv_ogm_orig_get - retrieve or create (if does not exist) an originator
298 * @bat_priv: the bat priv with all the soft interface information
299 * @addr: mac address of the originator
300 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200301 * Return: the originator object corresponding to the passed mac address or NULL
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200302 * on failure.
303 * If the object does not exists it is created an initialised.
304 */
305static struct batadv_orig_node *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200306batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const u8 *addr)
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200307{
308 struct batadv_orig_node *orig_node;
Sven Eckelmann8142ea62020-03-16 23:30:53 +0100309 int hash_added;
310 size_t size;
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200311
312 orig_node = batadv_orig_hash_find(bat_priv, addr);
313 if (orig_node)
314 return orig_node;
315
316 orig_node = batadv_orig_node_new(bat_priv, addr);
317 if (!orig_node)
318 return NULL;
319
320 spin_lock_init(&orig_node->bat_iv.ogm_cnt_lock);
321
322 size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS;
323 orig_node->bat_iv.bcast_own = kzalloc(size, GFP_ATOMIC);
324 if (!orig_node->bat_iv.bcast_own)
325 goto free_orig_node;
326
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200327 size = bat_priv->num_ifaces * sizeof(u8);
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200328 orig_node->bat_iv.bcast_own_sum = kzalloc(size, GFP_ATOMIC);
329 if (!orig_node->bat_iv.bcast_own_sum)
Antonio Quartullia5a5cb82014-02-15 02:17:20 +0100330 goto free_orig_node;
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200331
Sven Eckelmann55db2d52016-07-15 17:39:21 +0200332 kref_get(&orig_node->refcount);
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200333 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
334 batadv_choose_orig, orig_node,
335 &orig_node->hash_entry);
336 if (hash_added != 0)
Sven Eckelmann55db2d52016-07-15 17:39:21 +0200337 goto free_orig_node_hash;
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200338
339 return orig_node;
340
Sven Eckelmann55db2d52016-07-15 17:39:21 +0200341free_orig_node_hash:
Sven Eckelmann5d967312016-01-17 11:01:09 +0100342 batadv_orig_node_put(orig_node);
Sven Eckelmann55db2d52016-07-15 17:39:21 +0200343free_orig_node:
Sven Eckelmann5d967312016-01-17 11:01:09 +0100344 batadv_orig_node_put(orig_node);
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200345
346 return NULL;
347}
348
Sven Eckelmann56303d32012-06-05 22:31:31 +0200349static struct batadv_neigh_node *
350batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200351 const u8 *neigh_addr,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200352 struct batadv_orig_node *orig_node,
Antonio Quartulli863dd7a2013-03-25 13:49:46 +0100353 struct batadv_orig_node *orig_neigh)
Marek Lindner7ae8b282012-03-01 15:35:21 +0800354{
Marek Lindner741aa062015-07-26 04:57:43 +0800355 struct batadv_neigh_node *neigh_node;
Marek Lindner7ae8b282012-03-01 15:35:21 +0800356
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800357 neigh_node = batadv_neigh_node_get_or_create(orig_node,
358 hard_iface, neigh_addr);
Marek Lindner7ae8b282012-03-01 15:35:21 +0800359 if (!neigh_node)
360 goto out;
361
Simon Wunderlich89652332013-11-13 19:14:46 +0100362 neigh_node->orig_node = orig_neigh;
Marek Lindner7ae8b282012-03-01 15:35:21 +0800363
Marek Lindner7ae8b282012-03-01 15:35:21 +0800364out:
365 return neigh_node;
366}
367
Sven Eckelmann56303d32012-06-05 22:31:31 +0200368static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200369{
Sven Eckelmann96412692012-06-05 22:31:30 +0200370 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindner14511512012-08-02 17:20:26 +0200371 unsigned char *ogm_buff;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200372 u32 random_seqno;
Marek Lindnerd7d32ec2012-02-07 17:20:46 +0800373
Sven Eckelmann1f9fd182020-03-16 23:31:04 +0100374 mutex_lock(&hard_iface->bat_iv.ogm_buff_mutex);
375
Marek Lindnerd7d32ec2012-02-07 17:20:46 +0800376 /* randomize initial seqno to avoid collision */
377 get_random_bytes(&random_seqno, sizeof(random_seqno));
Marek Lindner14511512012-08-02 17:20:26 +0200378 atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno);
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200379
Marek Lindner14511512012-08-02 17:20:26 +0200380 hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN;
381 ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC);
Sven Eckelmann1f9fd182020-03-16 23:31:04 +0100382 if (!ogm_buff) {
383 mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex);
Markus Pargmann42d9f2c2014-12-26 12:41:26 +0100384 return -ENOMEM;
Sven Eckelmann1f9fd182020-03-16 23:31:04 +0100385 }
Marek Lindner77af7572012-02-07 17:20:48 +0800386
Marek Lindner14511512012-08-02 17:20:26 +0200387 hard_iface->bat_iv.ogm_buff = ogm_buff;
388
389 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100390 batadv_ogm_packet->packet_type = BATADV_IV_OGM;
391 batadv_ogm_packet->version = BATADV_COMPAT_VERSION;
392 batadv_ogm_packet->ttl = 2;
Sven Eckelmann96412692012-06-05 22:31:30 +0200393 batadv_ogm_packet->flags = BATADV_NO_FLAGS;
Marek Lindner414254e2013-04-23 21:39:58 +0800394 batadv_ogm_packet->reserved = 0;
Sven Eckelmann96412692012-06-05 22:31:30 +0200395 batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE;
Marek Lindner77af7572012-02-07 17:20:48 +0800396
Sven Eckelmann1f9fd182020-03-16 23:31:04 +0100397 mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex);
398
Markus Pargmann42d9f2c2014-12-26 12:41:26 +0100399 return 0;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200400}
401
Sven Eckelmann56303d32012-06-05 22:31:31 +0200402static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
Marek Lindner00a50072012-02-07 17:20:47 +0800403{
Sven Eckelmann1f9fd182020-03-16 23:31:04 +0100404 mutex_lock(&hard_iface->bat_iv.ogm_buff_mutex);
405
Marek Lindner14511512012-08-02 17:20:26 +0200406 kfree(hard_iface->bat_iv.ogm_buff);
407 hard_iface->bat_iv.ogm_buff = NULL;
Sven Eckelmann1f9fd182020-03-16 23:31:04 +0100408
409 mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex);
Marek Lindner00a50072012-02-07 17:20:47 +0800410}
411
Sven Eckelmann56303d32012-06-05 22:31:31 +0200412static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface)
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200413{
Sven Eckelmann96412692012-06-05 22:31:30 +0200414 struct batadv_ogm_packet *batadv_ogm_packet;
Sven Eckelmann1f9fd182020-03-16 23:31:04 +0100415 void *ogm_buff;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200416
Sven Eckelmann1f9fd182020-03-16 23:31:04 +0100417 mutex_lock(&hard_iface->bat_iv.ogm_buff_mutex);
418
419 ogm_buff = hard_iface->bat_iv.ogm_buff;
420 if (!ogm_buff)
421 goto unlock;
422
423 batadv_ogm_packet = ogm_buff;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100424 ether_addr_copy(batadv_ogm_packet->orig,
425 hard_iface->net_dev->dev_addr);
426 ether_addr_copy(batadv_ogm_packet->prev_sender,
427 hard_iface->net_dev->dev_addr);
Sven Eckelmann1f9fd182020-03-16 23:31:04 +0100428
429unlock:
430 mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex);
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200431}
432
Sven Eckelmann56303d32012-06-05 22:31:31 +0200433static void
434batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface)
Marek Lindnerc3229392012-03-11 06:17:50 +0800435{
Sven Eckelmann96412692012-06-05 22:31:30 +0200436 struct batadv_ogm_packet *batadv_ogm_packet;
Sven Eckelmann1f9fd182020-03-16 23:31:04 +0100437 void *ogm_buff;
Marek Lindnerc3229392012-03-11 06:17:50 +0800438
Sven Eckelmann1f9fd182020-03-16 23:31:04 +0100439 mutex_lock(&hard_iface->bat_iv.ogm_buff_mutex);
440
441 ogm_buff = hard_iface->bat_iv.ogm_buff;
442 if (!ogm_buff)
443 goto unlock;
444
445 batadv_ogm_packet = ogm_buff;
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100446 batadv_ogm_packet->ttl = BATADV_TTL;
Sven Eckelmann1f9fd182020-03-16 23:31:04 +0100447
448unlock:
449 mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex);
Marek Lindnerc3229392012-03-11 06:17:50 +0800450}
451
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200452/* when do we schedule our own ogm to be sent */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200453static unsigned long
Sven Eckelmann56303d32012-06-05 22:31:31 +0200454batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200455{
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200456 unsigned int msecs;
457
458 msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
Akinobu Mitae76e4322012-12-24 11:14:07 +0900459 msecs += prandom_u32() % (2 * BATADV_JITTER);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200460
461 return jiffies + msecs_to_jiffies(msecs);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200462}
463
464/* when do we schedule a ogm packet to be sent */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200465static unsigned long batadv_iv_ogm_fwd_send_time(void)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200466{
Akinobu Mitae76e4322012-12-24 11:14:07 +0900467 return jiffies + msecs_to_jiffies(prandom_u32() % (BATADV_JITTER / 2));
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200468}
469
470/* apply hop penalty for a normal link */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200471static u8 batadv_hop_penalty(u8 tq, const struct batadv_priv *bat_priv)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200472{
473 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200474 int new_tq;
475
476 new_tq = tq * (BATADV_TQ_MAX_VALUE - hop_penalty);
477 new_tq /= BATADV_TQ_MAX_VALUE;
478
479 return new_tq;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200480}
481
Simon Wunderlich600184b2015-11-23 19:57:21 +0100482/**
483 * batadv_iv_ogm_aggr_packet - checks if there is another OGM attached
484 * @buff_pos: current position in the skb
485 * @packet_len: total length of the skb
Sven Eckelmann6c59cb12019-08-22 08:55:36 +0200486 * @ogm_packet: potential OGM in buffer
Simon Wunderlich600184b2015-11-23 19:57:21 +0100487 *
488 * Return: true if there is enough space for another OGM, false otherwise.
489 */
Sven Eckelmann6c59cb12019-08-22 08:55:36 +0200490static bool
491batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len,
492 const struct batadv_ogm_packet *ogm_packet)
Marek Lindnerfc957272011-07-30 12:04:12 +0200493{
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200494 int next_buff_pos = 0;
495
Sven Eckelmann6c59cb12019-08-22 08:55:36 +0200496 /* check if there is enough space for the header */
497 next_buff_pos += buff_pos + sizeof(*ogm_packet);
498 if (next_buff_pos > packet_len)
499 return false;
500
501 /* check if there is enough space for the optional TVLV */
502 next_buff_pos += ntohs(ogm_packet->tvlv_len);
Marek Lindnerfc957272011-07-30 12:04:12 +0200503
504 return (next_buff_pos <= packet_len) &&
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200505 (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
Marek Lindnerfc957272011-07-30 12:04:12 +0200506}
507
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200508/* send a batman ogm to a given interface */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200509static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet,
510 struct batadv_hard_iface *hard_iface)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200511{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200512 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Markus Pargmannde12bae2014-12-26 12:41:28 +0100513 const char *fwd_str;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200514 u8 packet_num;
515 s16 buff_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +0200516 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200517 struct sk_buff *skb;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200518 u8 *packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200519
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200520 if (hard_iface->if_status != BATADV_IF_ACTIVE)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200521 return;
522
523 packet_num = 0;
524 buff_pos = 0;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200525 packet_pos = forw_packet->skb->data;
526 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200527
528 /* adjust all flags and log packets */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200529 while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
Sven Eckelmann6c59cb12019-08-22 08:55:36 +0200530 batadv_ogm_packet)) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200531 /* we might have aggregated direct link packets with an
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200532 * ordinary base packet
533 */
Sven Eckelmann8de47de2012-07-08 16:32:09 +0200534 if (forw_packet->direct_link_flags & BIT(packet_num) &&
535 forw_packet->if_incoming == hard_iface)
Sven Eckelmann96412692012-06-05 22:31:30 +0200536 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200537 else
Sven Eckelmann96412692012-06-05 22:31:30 +0200538 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200539
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200540 if (packet_num > 0 || !forw_packet->own)
541 fwd_str = "Forwarding";
542 else
543 fwd_str = "Sending own";
544
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200545 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800546 "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200547 fwd_str, (packet_num > 0 ? "aggregated " : ""),
Sven Eckelmann96412692012-06-05 22:31:30 +0200548 batadv_ogm_packet->orig,
549 ntohl(batadv_ogm_packet->seqno),
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100550 batadv_ogm_packet->tq, batadv_ogm_packet->ttl,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +0200551 ((batadv_ogm_packet->flags & BATADV_DIRECTLINK) ?
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200552 "on" : "off"),
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800553 hard_iface->net_dev->name,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200554 hard_iface->net_dev->dev_addr);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200555
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200556 buff_pos += BATADV_OGM_HLEN;
Marek Lindneref261572013-04-23 21:39:57 +0800557 buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200558 packet_num++;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200559 packet_pos = forw_packet->skb->data + buff_pos;
560 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200561 }
562
563 /* create clone because function is called more than once */
564 skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
Martin Hundebøllf8214862012-04-20 17:02:45 +0200565 if (skb) {
Sven Eckelmannd69909d2012-06-03 22:19:20 +0200566 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
567 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
Martin Hundebøllf8214862012-04-20 17:02:45 +0200568 skb->len + ETH_HLEN);
Antonio Quartulli95d39272016-01-16 16:40:15 +0800569 batadv_send_broadcast_skb(skb, hard_iface);
Martin Hundebøllf8214862012-04-20 17:02:45 +0200570 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200571}
572
573/* send a batman ogm packet */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200574static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200575{
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200576 struct net_device *soft_iface;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200577
578 if (!forw_packet->if_incoming) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100579 pr_err("Error - can't forward packet: incoming iface not specified\n");
Linus LĂĽssingf55a2e82016-06-14 22:56:50 +0200580 return;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200581 }
582
583 soft_iface = forw_packet->if_incoming->soft_iface;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200584
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100585 if (WARN_ON(!forw_packet->if_outgoing))
Linus LĂĽssingf55a2e82016-06-14 22:56:50 +0200586 return;
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100587
588 if (WARN_ON(forw_packet->if_outgoing->soft_iface != soft_iface))
Linus LĂĽssingf55a2e82016-06-14 22:56:50 +0200589 return;
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100590
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200591 if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE)
Linus LĂĽssingf55a2e82016-06-14 22:56:50 +0200592 return;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200593
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100594 /* only for one specific outgoing interface */
595 batadv_iv_ogm_send_to_if(forw_packet, forw_packet->if_outgoing);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200596}
597
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100598/**
599 * batadv_iv_ogm_can_aggregate - find out if an OGM can be aggregated on an
600 * existing forward packet
601 * @new_bat_ogm_packet: OGM packet to be aggregated
602 * @bat_priv: the bat priv with all the soft interface information
603 * @packet_len: (total) length of the OGM
604 * @send_time: timestamp (jiffies) when the packet is to be sent
Martin Hundebøll95298a92014-07-15 09:41:05 +0200605 * @directlink: true if this is a direct link packet
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100606 * @if_incoming: interface where the packet was received
607 * @if_outgoing: interface for which the retransmission should be considered
608 * @forw_packet: the forwarded packet which should be checked
609 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200610 * Return: true if new_packet can be aggregated with forw_packet
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100611 */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200612static bool
Sven Eckelmann96412692012-06-05 22:31:30 +0200613batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200614 struct batadv_priv *bat_priv,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200615 int packet_len, unsigned long send_time,
616 bool directlink,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200617 const struct batadv_hard_iface *if_incoming,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100618 const struct batadv_hard_iface *if_outgoing,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200619 const struct batadv_forw_packet *forw_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200620{
Sven Eckelmann96412692012-06-05 22:31:30 +0200621 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200622 int aggregated_bytes = forw_packet->packet_len + packet_len;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200623 struct batadv_hard_iface *primary_if = NULL;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200624 bool res = false;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200625 unsigned long aggregation_end_time;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200626
Sven Eckelmann96412692012-06-05 22:31:30 +0200627 batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200628 aggregation_end_time = send_time;
629 aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200630
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200631 /* we can aggregate the current packet to this aggregated packet
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200632 * if:
633 *
634 * - the send time is within our MAX_AGGREGATION_MS time
635 * - the resulting packet wont be bigger than
636 * MAX_AGGREGATION_BYTES
Markus Pargmann8f34b382014-12-26 12:41:29 +0100637 * otherwise aggregation is not possible
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200638 */
Markus Pargmann8f34b382014-12-26 12:41:29 +0100639 if (!time_before(send_time, forw_packet->send_time) ||
640 !time_after_eq(aggregation_end_time, forw_packet->send_time))
641 return false;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200642
Markus Pargmann8f34b382014-12-26 12:41:29 +0100643 if (aggregated_bytes > BATADV_MAX_AGGREGATION_BYTES)
644 return false;
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100645
Markus Pargmann8f34b382014-12-26 12:41:29 +0100646 /* packet is not leaving on the same interface. */
647 if (forw_packet->if_outgoing != if_outgoing)
648 return false;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200649
Markus Pargmann8f34b382014-12-26 12:41:29 +0100650 /* check aggregation compatibility
651 * -> direct link packets are broadcasted on
652 * their interface only
653 * -> aggregate packet if the current packet is
654 * a "global" packet as well as the base
655 * packet
656 */
657 primary_if = batadv_primary_if_get_selected(bat_priv);
658 if (!primary_if)
659 return false;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200660
Markus Pargmann8f34b382014-12-26 12:41:29 +0100661 /* packets without direct link flag and high TTL
662 * are flooded through the net
663 */
664 if (!directlink &&
665 !(batadv_ogm_packet->flags & BATADV_DIRECTLINK) &&
666 batadv_ogm_packet->ttl != 1 &&
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200667
Markus Pargmann8f34b382014-12-26 12:41:29 +0100668 /* own packets originating non-primary
669 * interfaces leave only that interface
670 */
671 (!forw_packet->own ||
672 forw_packet->if_incoming == primary_if)) {
673 res = true;
674 goto out;
675 }
676
677 /* if the incoming packet is sent via this one
678 * interface only - we still can aggregate
679 */
680 if (directlink &&
681 new_bat_ogm_packet->ttl == 1 &&
682 forw_packet->if_incoming == if_incoming &&
683
684 /* packets from direct neighbors or
685 * own secondary interface packets
686 * (= secondary interface packets in general)
687 */
688 (batadv_ogm_packet->flags & BATADV_DIRECTLINK ||
689 (forw_packet->own &&
690 forw_packet->if_incoming != primary_if))) {
691 res = true;
692 goto out;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200693 }
694
695out:
696 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100697 batadv_hardif_put(primary_if);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200698 return res;
699}
700
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100701/**
702 * batadv_iv_ogm_aggregate_new - create a new aggregated packet and add this
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100703 * packet to it.
704 * @packet_buff: pointer to the OGM
705 * @packet_len: (total) length of the OGM
706 * @send_time: timestamp (jiffies) when the packet is to be sent
707 * @direct_link: whether this OGM has direct link status
708 * @if_incoming: interface where the packet was received
709 * @if_outgoing: interface for which the retransmission should be considered
710 * @own_packet: true if it is a self-generated ogm
711 */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200712static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,
713 int packet_len, unsigned long send_time,
714 bool direct_link,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200715 struct batadv_hard_iface *if_incoming,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100716 struct batadv_hard_iface *if_outgoing,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200717 int own_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200718{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200719 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
720 struct batadv_forw_packet *forw_packet_aggr;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200721 unsigned char *skb_buff;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200722 unsigned int skb_size;
Linus LĂĽssinga65e5482016-06-20 21:39:54 +0200723 atomic_t *queue_left = own_packet ? NULL : &bat_priv->batman_queue_left;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200724
Linus LĂĽssinga65e5482016-06-20 21:39:54 +0200725 forw_packet_aggr = batadv_forw_packet_alloc(if_incoming, if_outgoing,
726 queue_left, bat_priv);
Markus Pargmann940d1562014-12-26 12:41:31 +0100727 if (!forw_packet_aggr)
Linus LĂĽssinga65e5482016-06-20 21:39:54 +0200728 return;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200729
Markus Pargmann940d1562014-12-26 12:41:31 +0100730 if (atomic_read(&bat_priv->aggregated_ogms) &&
731 packet_len < BATADV_MAX_AGGREGATION_BYTES)
Sven Eckelmann5b246572012-11-04 17:11:45 +0100732 skb_size = BATADV_MAX_AGGREGATION_BYTES;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200733 else
Sven Eckelmann5b246572012-11-04 17:11:45 +0100734 skb_size = packet_len;
735
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200736 skb_size += ETH_HLEN;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200737
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200738 forw_packet_aggr->skb = netdev_alloc_skb_ip_align(NULL, skb_size);
Linus LĂĽssinga65e5482016-06-20 21:39:54 +0200739 if (!forw_packet_aggr->skb) {
740 batadv_forw_packet_free(forw_packet_aggr);
741 return;
742 }
743
Simon Wunderlichc54f38c2013-07-29 17:56:44 +0200744 forw_packet_aggr->skb->priority = TC_PRIO_CONTROL;
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200745 skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200746
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200747 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
748 forw_packet_aggr->packet_len = packet_len;
749 memcpy(skb_buff, packet_buff, packet_len);
750
751 forw_packet_aggr->own = own_packet;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200752 forw_packet_aggr->direct_link_flags = BATADV_NO_FLAGS;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200753 forw_packet_aggr->send_time = send_time;
754
755 /* save packet direct link flag status */
756 if (direct_link)
757 forw_packet_aggr->direct_link_flags |= 1;
758
759 /* add new packet to packet list */
760 spin_lock_bh(&bat_priv->forw_bat_list_lock);
761 hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list);
762 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
763
764 /* start timer for this packet */
765 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
Antonio Quartullif0d97252016-05-03 01:45:34 +0800766 batadv_iv_send_outstanding_bat_ogm_packet);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200767 queue_delayed_work(batadv_event_workqueue,
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200768 &forw_packet_aggr->delayed_work,
769 send_time - jiffies);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200770}
771
772/* aggregate a new packet into the existing ogm packet */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200773static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200774 const unsigned char *packet_buff,
775 int packet_len, bool direct_link)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200776{
777 unsigned char *skb_buff;
Sven Eckelmann8de47de2012-07-08 16:32:09 +0200778 unsigned long new_direct_link_flag;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200779
780 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
781 memcpy(skb_buff, packet_buff, packet_len);
782 forw_packet_aggr->packet_len += packet_len;
783 forw_packet_aggr->num_packets++;
784
785 /* save packet direct link flag status */
Sven Eckelmann8de47de2012-07-08 16:32:09 +0200786 if (direct_link) {
787 new_direct_link_flag = BIT(forw_packet_aggr->num_packets);
788 forw_packet_aggr->direct_link_flags |= new_direct_link_flag;
789 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200790}
791
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100792/**
793 * batadv_iv_ogm_queue_add - queue up an OGM for transmission
794 * @bat_priv: the bat priv with all the soft interface information
795 * @packet_buff: pointer to the OGM
796 * @packet_len: (total) length of the OGM
797 * @if_incoming: interface where the packet was received
798 * @if_outgoing: interface for which the retransmission should be considered
799 * @own_packet: true if it is a self-generated ogm
800 * @send_time: timestamp (jiffies) when the packet is to be sent
801 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200802static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200803 unsigned char *packet_buff,
804 int packet_len,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200805 struct batadv_hard_iface *if_incoming,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100806 struct batadv_hard_iface *if_outgoing,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200807 int own_packet, unsigned long send_time)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200808{
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200809 /* _aggr -> pointer to the packet we want to aggregate with
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200810 * _pos -> pointer to the position in the queue
811 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200812 struct batadv_forw_packet *forw_packet_aggr = NULL;
813 struct batadv_forw_packet *forw_packet_pos = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200814 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200815 bool direct_link;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200816 unsigned long max_aggregation_jiffies;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200817
Sven Eckelmann96412692012-06-05 22:31:30 +0200818 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
Markus Pargmann56489152014-12-26 12:41:32 +0100819 direct_link = !!(batadv_ogm_packet->flags & BATADV_DIRECTLINK);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200820 max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200821
822 /* find position for the packet in the forward queue */
823 spin_lock_bh(&bat_priv->forw_bat_list_lock);
824 /* own packets are not to be aggregated */
Markus Pargmann56489152014-12-26 12:41:32 +0100825 if (atomic_read(&bat_priv->aggregated_ogms) && !own_packet) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800826 hlist_for_each_entry(forw_packet_pos,
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200827 &bat_priv->forw_bat_list, list) {
Sven Eckelmann96412692012-06-05 22:31:30 +0200828 if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200829 bat_priv, packet_len,
830 send_time, direct_link,
831 if_incoming,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100832 if_outgoing,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200833 forw_packet_pos)) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200834 forw_packet_aggr = forw_packet_pos;
835 break;
836 }
837 }
838 }
839
840 /* nothing to aggregate with - either aggregation disabled or no
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200841 * suitable aggregation packet found
842 */
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200843 if (!forw_packet_aggr) {
844 /* the following section can run without the lock */
845 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
846
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200847 /* if we could not aggregate this packet with one of the others
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200848 * we hold it back for a while, so that it might be aggregated
849 * later on
850 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200851 if (!own_packet && atomic_read(&bat_priv->aggregated_ogms))
852 send_time += max_aggregation_jiffies;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200853
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200854 batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
855 send_time, direct_link,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100856 if_incoming, if_outgoing,
857 own_packet);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200858 } else {
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200859 batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
860 packet_len, direct_link);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200861 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
862 }
863}
864
Sven Eckelmann56303d32012-06-05 22:31:31 +0200865static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200866 const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +0200867 struct batadv_ogm_packet *batadv_ogm_packet,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200868 bool is_single_hop_neigh,
869 bool is_from_best_next_hop,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100870 struct batadv_hard_iface *if_incoming,
871 struct batadv_hard_iface *if_outgoing)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200872{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200873 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200874 u16 tvlv_len;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200875
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100876 if (batadv_ogm_packet->ttl <= 1) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200877 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200878 return;
879 }
880
Marek Lindner13b25412012-03-11 06:17:53 +0800881 if (!is_from_best_next_hop) {
882 /* Mark the forwarded packet when it is not coming from our
883 * best next hop. We still need to forward the packet for our
884 * neighbor link quality detection to work in case the packet
885 * originated from a single hop neighbor. Otherwise we can
886 * simply drop the ogm.
887 */
888 if (is_single_hop_neigh)
Sven Eckelmann96412692012-06-05 22:31:30 +0200889 batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP;
Marek Lindner13b25412012-03-11 06:17:53 +0800890 else
891 return;
892 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200893
Marek Lindneref261572013-04-23 21:39:57 +0800894 tvlv_len = ntohs(batadv_ogm_packet->tvlv_len);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200895
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100896 batadv_ogm_packet->ttl--;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100897 ether_addr_copy(batadv_ogm_packet->prev_sender, ethhdr->h_source);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200898
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200899 /* apply hop penalty */
Sven Eckelmann96412692012-06-05 22:31:30 +0200900 batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200901 bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200902
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200903 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200904 "Forwarding packet: tq: %i, ttl: %i\n",
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100905 batadv_ogm_packet->tq, batadv_ogm_packet->ttl);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200906
Marek Lindner75cd33f2012-03-01 15:35:16 +0800907 if (is_single_hop_neigh)
Sven Eckelmann96412692012-06-05 22:31:30 +0200908 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200909 else
Sven Eckelmann96412692012-06-05 22:31:30 +0200910 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200911
Sven Eckelmann96412692012-06-05 22:31:30 +0200912 batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
Marek Lindneref261572013-04-23 21:39:57 +0800913 BATADV_OGM_HLEN + tvlv_len,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100914 if_incoming, if_outgoing, 0,
915 batadv_iv_ogm_fwd_send_time());
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200916}
917
Antonio Quartullid9896612013-04-17 17:44:43 +0200918/**
919 * batadv_iv_ogm_slide_own_bcast_window - bitshift own OGM broadcast windows for
920 * the given interface
921 * @hard_iface: the interface for which the windows have to be shifted
922 */
923static void
924batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
925{
926 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
927 struct batadv_hashtable *hash = bat_priv->orig_hash;
928 struct hlist_head *head;
929 struct batadv_orig_node *orig_node;
930 unsigned long *word;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200931 u32 i;
Antonio Quartullid9896612013-04-17 17:44:43 +0200932 size_t word_index;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200933 u8 *w;
Sven Eckelmann8142ea62020-03-16 23:30:53 +0100934 unsigned int if_num;
Antonio Quartullid9896612013-04-17 17:44:43 +0200935
936 for (i = 0; i < hash->size; i++) {
937 head = &hash->table[i];
938
939 rcu_read_lock();
940 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200941 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Antonio Quartullid9896612013-04-17 17:44:43 +0200942 word_index = hard_iface->if_num * BATADV_NUM_WORDS;
Antonio Quartulli32db6aa2014-09-01 14:37:29 +0200943 word = &orig_node->bat_iv.bcast_own[word_index];
Antonio Quartullid9896612013-04-17 17:44:43 +0200944
945 batadv_bit_get_packet(bat_priv, word, 1, 0);
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200946 if_num = hard_iface->if_num;
947 w = &orig_node->bat_iv.bcast_own_sum[if_num];
Antonio Quartullid9896612013-04-17 17:44:43 +0200948 *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200949 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Antonio Quartullid9896612013-04-17 17:44:43 +0200950 }
951 rcu_read_unlock();
952 }
953}
954
Sven Eckelmann1f9fd182020-03-16 23:31:04 +0100955/**
956 * batadv_iv_ogm_schedule_buff() - schedule submission of hardif ogm buffer
957 * @hard_iface: interface whose ogm buffer should be transmitted
958 */
959static void batadv_iv_ogm_schedule_buff(struct batadv_hard_iface *hard_iface)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200960{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200961 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindner14511512012-08-02 17:20:26 +0200962 unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff;
Sven Eckelmann96412692012-06-05 22:31:30 +0200963 struct batadv_ogm_packet *batadv_ogm_packet;
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100964 struct batadv_hard_iface *primary_if, *tmp_hard_iface;
Marek Lindner14511512012-08-02 17:20:26 +0200965 int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200966 u32 seqno;
967 u16 tvlv_len = 0;
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100968 unsigned long send_time;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200969
Sven Eckelmann1f9fd182020-03-16 23:31:04 +0100970 lockdep_assert_held(&hard_iface->bat_iv.ogm_buff_mutex);
Antonio Quartullif0d97252016-05-03 01:45:34 +0800971
Sven Eckelmannbaed34e2020-03-16 23:31:05 +0100972 /* interface already disabled by batadv_iv_ogm_iface_disable */
973 if (!*ogm_buff)
974 return;
975
Antonio Quartullif0d97252016-05-03 01:45:34 +0800976 /* the interface gets activated here to avoid race conditions between
977 * the moment of activating the interface in
978 * hardif_activate_interface() where the originator mac is set and
979 * outdated packets (especially uninitialized mac addresses) in the
980 * packet queue
981 */
982 if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED)
983 hard_iface->if_status = BATADV_IF_ACTIVE;
984
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200985 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200986
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800987 if (hard_iface == primary_if) {
988 /* tt changes have to be committed before the tvlv data is
989 * appended as it may alter the tt tvlv container
990 */
991 batadv_tt_local_commit_changes(bat_priv);
Marek Lindneref261572013-04-23 21:39:57 +0800992 tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff,
993 ogm_buff_len,
994 BATADV_OGM_HLEN);
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800995 }
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800996
Marek Lindner14511512012-08-02 17:20:26 +0200997 batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff);
Marek Lindneref261572013-04-23 21:39:57 +0800998 batadv_ogm_packet->tvlv_len = htons(tvlv_len);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200999
1000 /* change sequence number to network order */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001001 seqno = (u32)atomic_read(&hard_iface->bat_iv.ogm_seqno);
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001002 batadv_ogm_packet->seqno = htonl(seqno);
Marek Lindner14511512012-08-02 17:20:26 +02001003 atomic_inc(&hard_iface->bat_iv.ogm_seqno);
Marek Lindnerb9dacc52011-08-03 09:09:30 +02001004
Antonio Quartullid9896612013-04-17 17:44:43 +02001005 batadv_iv_ogm_slide_own_bcast_window(hard_iface);
Marek Lindnerb9dacc52011-08-03 09:09:30 +02001006
Simon Wunderlichef0a9372013-11-13 19:14:49 +01001007 send_time = batadv_iv_ogm_emit_send_time(bat_priv);
1008
1009 if (hard_iface != primary_if) {
1010 /* OGMs from secondary interfaces are only scheduled on their
1011 * respective interfaces.
1012 */
1013 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff, *ogm_buff_len,
1014 hard_iface, hard_iface, 1, send_time);
1015 goto out;
1016 }
1017
1018 /* OGMs from primary interfaces are scheduled on all
1019 * interfaces.
1020 */
1021 rcu_read_lock();
1022 list_for_each_entry_rcu(tmp_hard_iface, &batadv_hardif_list, list) {
1023 if (tmp_hard_iface->soft_iface != hard_iface->soft_iface)
Sven Eckelmann87b40f52015-07-17 10:03:42 +02001024 continue;
Sven Eckelmann27353442016-03-05 16:09:16 +01001025
1026 if (!kref_get_unless_zero(&tmp_hard_iface->refcount))
1027 continue;
1028
Simon Wunderlichef0a9372013-11-13 19:14:49 +01001029 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff,
1030 *ogm_buff_len, hard_iface,
1031 tmp_hard_iface, 1, send_time);
Sven Eckelmann27353442016-03-05 16:09:16 +01001032
1033 batadv_hardif_put(tmp_hard_iface);
Simon Wunderlichef0a9372013-11-13 19:14:49 +01001034 }
1035 rcu_read_unlock();
1036
1037out:
Marek Lindnerb9dacc52011-08-03 09:09:30 +02001038 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001039 batadv_hardif_put(primary_if);
Marek Lindnerb9dacc52011-08-03 09:09:30 +02001040}
1041
Sven Eckelmann1f9fd182020-03-16 23:31:04 +01001042static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
1043{
1044 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE ||
1045 hard_iface->if_status == BATADV_IF_TO_BE_REMOVED)
1046 return;
1047
1048 mutex_lock(&hard_iface->bat_iv.ogm_buff_mutex);
1049 batadv_iv_ogm_schedule_buff(hard_iface);
1050 mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex);
1051}
1052
Simon Wunderlich89652332013-11-13 19:14:46 +01001053/**
1054 * batadv_iv_ogm_orig_update - use OGM to update corresponding data in an
1055 * originator
1056 * @bat_priv: the bat priv with all the soft interface information
1057 * @orig_node: the orig node who originally emitted the ogm packet
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001058 * @orig_ifinfo: ifinfo for the outgoing interface of the orig_node
Simon Wunderlich89652332013-11-13 19:14:46 +01001059 * @ethhdr: Ethernet header of the OGM
1060 * @batadv_ogm_packet: the ogm packet
1061 * @if_incoming: interface where the packet was received
1062 * @if_outgoing: interface for which the retransmission should be considered
Simon Wunderlich89652332013-11-13 19:14:46 +01001063 * @dup_status: the duplicate status of this ogm packet.
1064 */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001065static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001066batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
1067 struct batadv_orig_node *orig_node,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001068 struct batadv_orig_ifinfo *orig_ifinfo,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001069 const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +02001070 const struct batadv_ogm_packet *batadv_ogm_packet,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001071 struct batadv_hard_iface *if_incoming,
Simon Wunderlich89652332013-11-13 19:14:46 +01001072 struct batadv_hard_iface *if_outgoing,
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001073 enum batadv_dup_status dup_status)
Marek Lindnerfc957272011-07-30 12:04:12 +02001074{
Simon Wunderlich89652332013-11-13 19:14:46 +01001075 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
1076 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
Sven Eckelmann4f248cf2015-06-09 20:50:49 +02001077 struct batadv_neigh_node *neigh_node = NULL;
1078 struct batadv_neigh_node *tmp_neigh_node = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001079 struct batadv_neigh_node *router = NULL;
1080 struct batadv_orig_node *orig_node_tmp;
Sven Eckelmann8142ea62020-03-16 23:30:53 +01001081 unsigned int if_num;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001082 u8 sum_orig, sum_neigh;
1083 u8 *neigh_addr;
1084 u8 tq_avg;
Marek Lindnerfc957272011-07-30 12:04:12 +02001085
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001086 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001087 "update_originator(): Searching and updating originator entry of received packet\n");
Marek Lindnerfc957272011-07-30 12:04:12 +02001088
1089 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001090 hlist_for_each_entry_rcu(tmp_neigh_node,
Marek Lindnerfc957272011-07-30 12:04:12 +02001091 &orig_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001092 neigh_addr = tmp_neigh_node->addr;
1093 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
1094 tmp_neigh_node->if_incoming == if_incoming &&
Sven Eckelmann77ae32e2016-01-16 10:29:53 +01001095 kref_get_unless_zero(&tmp_neigh_node->refcount)) {
Antonio Quartulli38dc40e2013-03-30 17:22:00 +01001096 if (WARN(neigh_node, "too many matching neigh_nodes"))
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001097 batadv_neigh_node_put(neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001098 neigh_node = tmp_neigh_node;
1099 continue;
1100 }
1101
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001102 if (dup_status != BATADV_NO_DUP)
Marek Lindnerfc957272011-07-30 12:04:12 +02001103 continue;
1104
Simon Wunderlich89652332013-11-13 19:14:46 +01001105 /* only update the entry for this outgoing interface */
1106 neigh_ifinfo = batadv_neigh_ifinfo_get(tmp_neigh_node,
1107 if_outgoing);
1108 if (!neigh_ifinfo)
1109 continue;
1110
1111 spin_lock_bh(&tmp_neigh_node->ifinfo_lock);
1112 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
1113 &neigh_ifinfo->bat_iv.tq_index, 0);
1114 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
1115 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
1116 spin_unlock_bh(&tmp_neigh_node->ifinfo_lock);
1117
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001118 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01001119 neigh_ifinfo = NULL;
Marek Lindnerfc957272011-07-30 12:04:12 +02001120 }
1121
1122 if (!neigh_node) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001123 struct batadv_orig_node *orig_tmp;
Marek Lindnerfc957272011-07-30 12:04:12 +02001124
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001125 orig_tmp = batadv_iv_ogm_orig_get(bat_priv, ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001126 if (!orig_tmp)
1127 goto unlock;
1128
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001129 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1130 ethhdr->h_source,
Antonio Quartulli863dd7a2013-03-25 13:49:46 +01001131 orig_node, orig_tmp);
Marek Lindnerfc957272011-07-30 12:04:12 +02001132
Sven Eckelmann5d967312016-01-17 11:01:09 +01001133 batadv_orig_node_put(orig_tmp);
Marek Lindnerfc957272011-07-30 12:04:12 +02001134 if (!neigh_node)
1135 goto unlock;
Markus Pargmann23badd62014-12-26 12:41:33 +01001136 } else {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001137 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001138 "Updating existing last-hop neighbor of originator\n");
Markus Pargmann23badd62014-12-26 12:41:33 +01001139 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001140
1141 rcu_read_unlock();
Simon Wunderlich89652332013-11-13 19:14:46 +01001142 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
1143 if (!neigh_ifinfo)
1144 goto out;
Marek Lindnerfc957272011-07-30 12:04:12 +02001145
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001146 neigh_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +02001147
Simon Wunderlich89652332013-11-13 19:14:46 +01001148 spin_lock_bh(&neigh_node->ifinfo_lock);
1149 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
1150 &neigh_ifinfo->bat_iv.tq_index,
Sven Eckelmann96412692012-06-05 22:31:30 +02001151 batadv_ogm_packet->tq);
Simon Wunderlich89652332013-11-13 19:14:46 +01001152 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
1153 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
1154 spin_unlock_bh(&neigh_node->ifinfo_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001155
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001156 if (dup_status == BATADV_NO_DUP) {
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001157 orig_ifinfo->last_ttl = batadv_ogm_packet->ttl;
Simon Wunderlich89652332013-11-13 19:14:46 +01001158 neigh_ifinfo->last_ttl = batadv_ogm_packet->ttl;
Marek Lindnerfc957272011-07-30 12:04:12 +02001159 }
1160
Marek Lindnerfc957272011-07-30 12:04:12 +02001161 /* if this neighbor already is our next hop there is nothing
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001162 * to change
1163 */
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001164 router = batadv_orig_router_get(orig_node, if_outgoing);
Marek Lindnerfc957272011-07-30 12:04:12 +02001165 if (router == neigh_node)
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001166 goto out;
Marek Lindnerfc957272011-07-30 12:04:12 +02001167
Simon Wunderlich89652332013-11-13 19:14:46 +01001168 if (router) {
1169 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1170 if (!router_ifinfo)
1171 goto out;
1172
1173 /* if this neighbor does not offer a better TQ we won't
1174 * consider it
1175 */
1176 if (router_ifinfo->bat_iv.tq_avg > neigh_ifinfo->bat_iv.tq_avg)
1177 goto out;
1178 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001179
1180 /* if the TQ is the same and the link not more symmetric we
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001181 * won't consider it either
1182 */
Simon Wunderlich89652332013-11-13 19:14:46 +01001183 if (router_ifinfo &&
Markus Pargmann01b97a32014-12-26 12:41:30 +01001184 neigh_ifinfo->bat_iv.tq_avg == router_ifinfo->bat_iv.tq_avg) {
Marek Lindnerfc957272011-07-30 12:04:12 +02001185 orig_node_tmp = router->orig_node;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001186 spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
Linus LĂĽssing7caf69f2012-09-18 03:01:08 +02001187 if_num = router->if_incoming->if_num;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001188 sum_orig = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
1189 spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001190
1191 orig_node_tmp = neigh_node->orig_node;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001192 spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
Linus LĂĽssing7caf69f2012-09-18 03:01:08 +02001193 if_num = neigh_node->if_incoming->if_num;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001194 sum_neigh = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
1195 spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001196
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001197 if (sum_orig >= sum_neigh)
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001198 goto out;
Marek Lindnerfc957272011-07-30 12:04:12 +02001199 }
1200
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001201 batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001202 goto out;
1203
1204unlock:
1205 rcu_read_unlock();
1206out:
1207 if (neigh_node)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001208 batadv_neigh_node_put(neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001209 if (router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001210 batadv_neigh_node_put(router);
Simon Wunderlich89652332013-11-13 19:14:46 +01001211 if (neigh_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001212 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01001213 if (router_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001214 batadv_neigh_ifinfo_put(router_ifinfo);
Marek Lindnerfc957272011-07-30 12:04:12 +02001215}
1216
Simon Wunderlich89652332013-11-13 19:14:46 +01001217/**
1218 * batadv_iv_ogm_calc_tq - calculate tq for current received ogm packet
1219 * @orig_node: the orig node who originally emitted the ogm packet
1220 * @orig_neigh_node: the orig node struct of the neighbor who sent the packet
1221 * @batadv_ogm_packet: the ogm packet
1222 * @if_incoming: interface where the packet was received
1223 * @if_outgoing: interface for which the retransmission should be considered
1224 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001225 * Return: true if the link can be considered bidirectional, false otherwise
Simon Wunderlich89652332013-11-13 19:14:46 +01001226 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001227static bool batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
1228 struct batadv_orig_node *orig_neigh_node,
1229 struct batadv_ogm_packet *batadv_ogm_packet,
1230 struct batadv_hard_iface *if_incoming,
1231 struct batadv_hard_iface *if_outgoing)
Marek Lindnerfc957272011-07-30 12:04:12 +02001232{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001233 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1234 struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
Simon Wunderlich89652332013-11-13 19:14:46 +01001235 struct batadv_neigh_ifinfo *neigh_ifinfo;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001236 u8 total_count;
1237 u8 orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001238 unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
Sven Eckelmann8142ea62020-03-16 23:30:53 +01001239 unsigned int if_num;
Sven Eckelmannd285f522016-02-16 10:47:07 +01001240 unsigned int tq_asym_penalty, inv_asym_penalty;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001241 unsigned int combined_tq;
Sven Eckelmannd285f522016-02-16 10:47:07 +01001242 unsigned int tq_iface_penalty;
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001243 bool ret = false;
Marek Lindnerfc957272011-07-30 12:04:12 +02001244
1245 /* find corresponding one hop neighbor */
1246 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001247 hlist_for_each_entry_rcu(tmp_neigh_node,
Marek Lindnerfc957272011-07-30 12:04:12 +02001248 &orig_neigh_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001249 if (!batadv_compare_eth(tmp_neigh_node->addr,
1250 orig_neigh_node->orig))
Marek Lindnerfc957272011-07-30 12:04:12 +02001251 continue;
1252
1253 if (tmp_neigh_node->if_incoming != if_incoming)
1254 continue;
1255
Sven Eckelmann77ae32e2016-01-16 10:29:53 +01001256 if (!kref_get_unless_zero(&tmp_neigh_node->refcount))
Marek Lindnerfc957272011-07-30 12:04:12 +02001257 continue;
1258
1259 neigh_node = tmp_neigh_node;
1260 break;
1261 }
1262 rcu_read_unlock();
1263
1264 if (!neigh_node)
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001265 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1266 orig_neigh_node->orig,
1267 orig_neigh_node,
Antonio Quartulli863dd7a2013-03-25 13:49:46 +01001268 orig_neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001269
1270 if (!neigh_node)
1271 goto out;
1272
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001273 /* if orig_node is direct neighbor update neigh_node last_seen */
Marek Lindnerfc957272011-07-30 12:04:12 +02001274 if (orig_node == orig_neigh_node)
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001275 neigh_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +02001276
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001277 orig_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +02001278
1279 /* find packet count of corresponding one hop neighbor */
Sven Eckelmann13e6a852020-03-16 23:30:52 +01001280 spin_lock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001281 if_num = if_incoming->if_num;
1282 orig_eq_count = orig_neigh_node->bat_iv.bcast_own_sum[if_num];
Simon Wunderlich89652332013-11-13 19:14:46 +01001283 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
1284 if (neigh_ifinfo) {
1285 neigh_rq_count = neigh_ifinfo->bat_iv.real_packet_count;
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001286 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01001287 } else {
1288 neigh_rq_count = 0;
1289 }
Sven Eckelmann13e6a852020-03-16 23:30:52 +01001290 spin_unlock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001291
1292 /* pay attention to not get a value bigger than 100 % */
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001293 if (orig_eq_count > neigh_rq_count)
1294 total_count = neigh_rq_count;
1295 else
1296 total_count = orig_eq_count;
Marek Lindnerfc957272011-07-30 12:04:12 +02001297
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001298 /* if we have too few packets (too less data) we set tq_own to zero
1299 * if we receive too few packets it is not considered bidirectional
1300 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001301 if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
1302 neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
Marek Lindnerfc957272011-07-30 12:04:12 +02001303 tq_own = 0;
1304 else
1305 /* neigh_node->real_packet_count is never zero as we
1306 * only purge old information when getting new
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001307 * information
1308 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001309 tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count;
Marek Lindnerfc957272011-07-30 12:04:12 +02001310
Sven Eckelmann21a12362012-03-07 09:07:46 +01001311 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
Marek Lindnerfc957272011-07-30 12:04:12 +02001312 * affect the nearly-symmetric links only a little, but
1313 * punishes asymmetric links more. This will give a value
1314 * between 0 and TQ_MAX_VALUE
1315 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001316 neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
1317 neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
1318 neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
1319 BATADV_TQ_LOCAL_WINDOW_SIZE *
1320 BATADV_TQ_LOCAL_WINDOW_SIZE;
1321 inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
1322 inv_asym_penalty /= neigh_rq_max_cube;
1323 tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;
Marek Lindnerfc957272011-07-30 12:04:12 +02001324
Simon Wunderlichc0398762013-11-13 19:14:48 +01001325 /* penalize if the OGM is forwarded on the same interface. WiFi
1326 * interfaces and other half duplex devices suffer from throughput
1327 * drops as they can't send and receive at the same time.
1328 */
1329 tq_iface_penalty = BATADV_TQ_MAX_VALUE;
1330 if (if_outgoing && (if_incoming == if_outgoing) &&
1331 batadv_is_wifi_netdev(if_outgoing->net_dev))
1332 tq_iface_penalty = batadv_hop_penalty(BATADV_TQ_MAX_VALUE,
1333 bat_priv);
1334
1335 combined_tq = batadv_ogm_packet->tq *
1336 tq_own *
1337 tq_asym_penalty *
1338 tq_iface_penalty;
1339 combined_tq /= BATADV_TQ_MAX_VALUE *
1340 BATADV_TQ_MAX_VALUE *
1341 BATADV_TQ_MAX_VALUE;
Sven Eckelmann96412692012-06-05 22:31:30 +02001342 batadv_ogm_packet->tq = combined_tq;
Marek Lindnerfc957272011-07-30 12:04:12 +02001343
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001344 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Simon Wunderlichc0398762013-11-13 19:14:48 +01001345 "bidirectional: orig = %-15pM neigh = %-15pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, iface_penalty: %3i, total tq: %3i, if_incoming = %s, if_outgoing = %s\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001346 orig_node->orig, orig_neigh_node->orig, total_count,
Simon Wunderlichc0398762013-11-13 19:14:48 +01001347 neigh_rq_count, tq_own, tq_asym_penalty, tq_iface_penalty,
1348 batadv_ogm_packet->tq, if_incoming->net_dev->name,
1349 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT");
Marek Lindnerfc957272011-07-30 12:04:12 +02001350
1351 /* if link has the minimum required transmission quality
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001352 * consider it bidirectional
1353 */
Sven Eckelmann96412692012-06-05 22:31:30 +02001354 if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001355 ret = true;
Marek Lindnerfc957272011-07-30 12:04:12 +02001356
1357out:
1358 if (neigh_node)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001359 batadv_neigh_node_put(neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001360 return ret;
1361}
1362
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001363/**
1364 * batadv_iv_ogm_update_seqnos - process a batman packet for all interfaces,
1365 * adjust the sequence number and find out whether it is a duplicate
1366 * @ethhdr: ethernet header of the packet
1367 * @batadv_ogm_packet: OGM packet to be considered
1368 * @if_incoming: interface on which the OGM packet was received
Simon Wunderlich89652332013-11-13 19:14:46 +01001369 * @if_outgoing: interface for which the retransmission should be considered
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001370 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001371 * Return: duplicate status as enum batadv_dup_status
Marek Lindnerfc957272011-07-30 12:04:12 +02001372 */
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001373static enum batadv_dup_status
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001374batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +02001375 const struct batadv_ogm_packet *batadv_ogm_packet,
Simon Wunderlich89652332013-11-13 19:14:46 +01001376 const struct batadv_hard_iface *if_incoming,
1377 struct batadv_hard_iface *if_outgoing)
Marek Lindnerfc957272011-07-30 12:04:12 +02001378{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001379 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1380 struct batadv_orig_node *orig_node;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001381 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
Simon Wunderlich89652332013-11-13 19:14:46 +01001382 struct batadv_neigh_node *neigh_node;
1383 struct batadv_neigh_ifinfo *neigh_ifinfo;
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001384 bool is_dup;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001385 s32 seq_diff;
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001386 bool need_update = false;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001387 int set_mark;
1388 enum batadv_dup_status ret = BATADV_NO_DUP;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001389 u32 seqno = ntohl(batadv_ogm_packet->seqno);
1390 u8 *neigh_addr;
1391 u8 packet_count;
Antonio Quartulli0538f752013-09-02 12:15:01 +02001392 unsigned long *bitmap;
Marek Lindnerfc957272011-07-30 12:04:12 +02001393
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001394 orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
Marek Lindnerfc957272011-07-30 12:04:12 +02001395 if (!orig_node)
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001396 return BATADV_NO_DUP;
Marek Lindnerfc957272011-07-30 12:04:12 +02001397
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001398 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1399 if (WARN_ON(!orig_ifinfo)) {
Sven Eckelmann5d967312016-01-17 11:01:09 +01001400 batadv_orig_node_put(orig_node);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001401 return 0;
1402 }
1403
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001404 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001405 seq_diff = seqno - orig_ifinfo->last_real_seqno;
Marek Lindnerfc957272011-07-30 12:04:12 +02001406
1407 /* signalize caller that the packet is to be dropped. */
Antonio Quartulli1e5cc262012-02-26 15:39:42 +01001408 if (!hlist_empty(&orig_node->neigh_list) &&
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001409 batadv_window_protected(bat_priv, seq_diff,
Simon Wunderlich81f02682015-11-23 19:57:22 +01001410 BATADV_TQ_LOCAL_WINDOW_SIZE,
1411 &orig_ifinfo->batman_seqno_reset, NULL)) {
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001412 ret = BATADV_PROTECTED;
Marek Lindnerfc957272011-07-30 12:04:12 +02001413 goto out;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001414 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001415
1416 rcu_read_lock();
Simon Wunderlich89652332013-11-13 19:14:46 +01001417 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1418 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node,
1419 if_outgoing);
1420 if (!neigh_ifinfo)
1421 continue;
1422
1423 neigh_addr = neigh_node->addr;
1424 is_dup = batadv_test_bit(neigh_ifinfo->bat_iv.real_bits,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001425 orig_ifinfo->last_real_seqno,
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001426 seqno);
1427
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001428 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
Simon Wunderlich89652332013-11-13 19:14:46 +01001429 neigh_node->if_incoming == if_incoming) {
Marek Lindnerfc957272011-07-30 12:04:12 +02001430 set_mark = 1;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001431 if (is_dup)
1432 ret = BATADV_NEIGH_DUP;
1433 } else {
Marek Lindnerfc957272011-07-30 12:04:12 +02001434 set_mark = 0;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001435 if (is_dup && (ret != BATADV_NEIGH_DUP))
1436 ret = BATADV_ORIG_DUP;
1437 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001438
1439 /* if the window moved, set the update flag. */
Simon Wunderlich89652332013-11-13 19:14:46 +01001440 bitmap = neigh_ifinfo->bat_iv.real_bits;
Antonio Quartulli0538f752013-09-02 12:15:01 +02001441 need_update |= batadv_bit_get_packet(bat_priv, bitmap,
Sven Eckelmann0f5f9322012-05-12 02:09:25 +02001442 seq_diff, set_mark);
Marek Lindnerfc957272011-07-30 12:04:12 +02001443
Simon Wunderlich89652332013-11-13 19:14:46 +01001444 packet_count = bitmap_weight(bitmap,
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001445 BATADV_TQ_LOCAL_WINDOW_SIZE);
Simon Wunderlich89652332013-11-13 19:14:46 +01001446 neigh_ifinfo->bat_iv.real_packet_count = packet_count;
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001447 batadv_neigh_ifinfo_put(neigh_ifinfo);
Marek Lindnerfc957272011-07-30 12:04:12 +02001448 }
1449 rcu_read_unlock();
1450
1451 if (need_update) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001452 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001453 "%s updating last_seqno: old %u, new %u\n",
1454 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT",
1455 orig_ifinfo->last_real_seqno, seqno);
1456 orig_ifinfo->last_real_seqno = seqno;
Marek Lindnerfc957272011-07-30 12:04:12 +02001457 }
1458
Marek Lindnerfc957272011-07-30 12:04:12 +02001459out:
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001460 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Sven Eckelmann5d967312016-01-17 11:01:09 +01001461 batadv_orig_node_put(orig_node);
Sven Eckelmann35f94772016-01-17 11:01:13 +01001462 batadv_orig_ifinfo_put(orig_ifinfo);
Marek Lindnerfc957272011-07-30 12:04:12 +02001463 return ret;
1464}
1465
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001466/**
1467 * batadv_iv_ogm_process_per_outif - process a batman iv OGM for an outgoing if
1468 * @skb: the skb containing the OGM
Martin Hundebøll95298a92014-07-15 09:41:05 +02001469 * @ogm_offset: offset from skb->data to start of ogm header
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001470 * @orig_node: the (cached) orig node for the originator of this OGM
1471 * @if_incoming: the interface where this packet was received
1472 * @if_outgoing: the interface for which the packet should be considered
1473 */
1474static void
1475batadv_iv_ogm_process_per_outif(const struct sk_buff *skb, int ogm_offset,
1476 struct batadv_orig_node *orig_node,
1477 struct batadv_hard_iface *if_incoming,
1478 struct batadv_hard_iface *if_outgoing)
1479{
1480 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Marek Lindner4ff1e2a2015-08-04 21:09:58 +08001481 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
Sven Eckelmann4f248cf2015-06-09 20:50:49 +02001482 struct batadv_neigh_node *router = NULL;
1483 struct batadv_neigh_node *router_router = NULL;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001484 struct batadv_orig_node *orig_neigh_node;
1485 struct batadv_orig_ifinfo *orig_ifinfo;
1486 struct batadv_neigh_node *orig_neigh_router = NULL;
1487 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
1488 struct batadv_ogm_packet *ogm_packet;
1489 enum batadv_dup_status dup_status;
1490 bool is_from_best_next_hop = false;
1491 bool is_single_hop_neigh = false;
1492 bool sameseq, similar_ttl;
1493 struct sk_buff *skb_priv;
1494 struct ethhdr *ethhdr;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001495 u8 *prev_sender;
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001496 bool is_bidirect;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001497
1498 /* create a private copy of the skb, as some functions change tq value
1499 * and/or flags.
1500 */
1501 skb_priv = skb_copy(skb, GFP_ATOMIC);
1502 if (!skb_priv)
1503 return;
1504
1505 ethhdr = eth_hdr(skb_priv);
1506 ogm_packet = (struct batadv_ogm_packet *)(skb_priv->data + ogm_offset);
1507
1508 dup_status = batadv_iv_ogm_update_seqnos(ethhdr, ogm_packet,
1509 if_incoming, if_outgoing);
1510 if (batadv_compare_eth(ethhdr->h_source, ogm_packet->orig))
1511 is_single_hop_neigh = true;
1512
1513 if (dup_status == BATADV_PROTECTED) {
1514 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1515 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1516 ethhdr->h_source);
1517 goto out;
1518 }
1519
1520 if (ogm_packet->tq == 0) {
1521 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1522 "Drop packet: originator packet with tq equal 0\n");
1523 goto out;
1524 }
1525
Marek Lindner4ff1e2a2015-08-04 21:09:58 +08001526 if (is_single_hop_neigh) {
1527 hardif_neigh = batadv_hardif_neigh_get(if_incoming,
1528 ethhdr->h_source);
1529 if (hardif_neigh)
1530 hardif_neigh->last_seen = jiffies;
1531 }
1532
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001533 router = batadv_orig_router_get(orig_node, if_outgoing);
1534 if (router) {
1535 router_router = batadv_orig_router_get(router->orig_node,
1536 if_outgoing);
1537 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1538 }
1539
1540 if ((router_ifinfo && router_ifinfo->bat_iv.tq_avg != 0) &&
1541 (batadv_compare_eth(router->addr, ethhdr->h_source)))
1542 is_from_best_next_hop = true;
1543
1544 prev_sender = ogm_packet->prev_sender;
1545 /* avoid temporary routing loops */
1546 if (router && router_router &&
1547 (batadv_compare_eth(router->addr, prev_sender)) &&
1548 !(batadv_compare_eth(ogm_packet->orig, prev_sender)) &&
1549 (batadv_compare_eth(router->addr, router_router->addr))) {
1550 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1551 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1552 ethhdr->h_source);
1553 goto out;
1554 }
1555
1556 if (if_outgoing == BATADV_IF_DEFAULT)
1557 batadv_tvlv_ogm_receive(bat_priv, ogm_packet, orig_node);
1558
1559 /* if sender is a direct neighbor the sender mac equals
1560 * originator mac
1561 */
1562 if (is_single_hop_neigh)
1563 orig_neigh_node = orig_node;
1564 else
1565 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1566 ethhdr->h_source);
1567
1568 if (!orig_neigh_node)
1569 goto out;
1570
1571 /* Update nc_nodes of the originator */
1572 batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node,
1573 ogm_packet, is_single_hop_neigh);
1574
1575 orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
1576 if_outgoing);
1577
1578 /* drop packet if sender is not a direct neighbor and if we
1579 * don't route towards it
1580 */
1581 if (!is_single_hop_neigh && (!orig_neigh_router)) {
1582 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1583 "Drop packet: OGM via unknown neighbor!\n");
1584 goto out_neigh;
1585 }
1586
1587 is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
1588 ogm_packet, if_incoming,
1589 if_outgoing);
1590
1591 /* update ranking if it is not a duplicate or has the same
1592 * seqno and similar ttl as the non-duplicate
1593 */
1594 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1595 if (!orig_ifinfo)
1596 goto out_neigh;
1597
1598 sameseq = orig_ifinfo->last_real_seqno == ntohl(ogm_packet->seqno);
1599 similar_ttl = (orig_ifinfo->last_ttl - 3) <= ogm_packet->ttl;
1600
1601 if (is_bidirect && ((dup_status == BATADV_NO_DUP) ||
1602 (sameseq && similar_ttl))) {
1603 batadv_iv_ogm_orig_update(bat_priv, orig_node,
1604 orig_ifinfo, ethhdr,
1605 ogm_packet, if_incoming,
1606 if_outgoing, dup_status);
1607 }
Sven Eckelmann35f94772016-01-17 11:01:13 +01001608 batadv_orig_ifinfo_put(orig_ifinfo);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001609
Simon Wunderlichef0a9372013-11-13 19:14:49 +01001610 /* only forward for specific interface, not for the default one. */
1611 if (if_outgoing == BATADV_IF_DEFAULT)
1612 goto out_neigh;
1613
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001614 /* is single hop (direct) neighbor */
1615 if (is_single_hop_neigh) {
1616 /* OGMs from secondary interfaces should only scheduled once
1617 * per interface where it has been received, not multiple times
1618 */
1619 if ((ogm_packet->ttl <= 2) &&
1620 (if_incoming != if_outgoing)) {
1621 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1622 "Drop packet: OGM from secondary interface and wrong outgoing interface\n");
1623 goto out_neigh;
1624 }
1625 /* mark direct link on incoming interface */
1626 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1627 is_single_hop_neigh,
Simon Wunderlichef0a9372013-11-13 19:14:49 +01001628 is_from_best_next_hop, if_incoming,
1629 if_outgoing);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001630
1631 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1632 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
1633 goto out_neigh;
1634 }
1635
1636 /* multihop originator */
1637 if (!is_bidirect) {
1638 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1639 "Drop packet: not received via bidirectional link\n");
1640 goto out_neigh;
1641 }
1642
1643 if (dup_status == BATADV_NEIGH_DUP) {
1644 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1645 "Drop packet: duplicate packet received\n");
1646 goto out_neigh;
1647 }
1648
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001649 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1650 "Forwarding packet: rebroadcast originator packet\n");
1651 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1652 is_single_hop_neigh, is_from_best_next_hop,
Simon Wunderlichef0a9372013-11-13 19:14:49 +01001653 if_incoming, if_outgoing);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001654
1655out_neigh:
1656 if ((orig_neigh_node) && (!is_single_hop_neigh))
Sven Eckelmann5d967312016-01-17 11:01:09 +01001657 batadv_orig_node_put(orig_neigh_node);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001658out:
Simon Wunderlichc1e517f2014-03-26 15:46:21 +01001659 if (router_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001660 batadv_neigh_ifinfo_put(router_ifinfo);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001661 if (router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001662 batadv_neigh_node_put(router);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001663 if (router_router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001664 batadv_neigh_node_put(router_router);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001665 if (orig_neigh_router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001666 batadv_neigh_node_put(orig_neigh_router);
Marek Lindner4ff1e2a2015-08-04 21:09:58 +08001667 if (hardif_neigh)
Sven Eckelmannaccadc32016-01-17 11:01:14 +01001668 batadv_hardif_neigh_put(hardif_neigh);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001669
1670 kfree_skb(skb_priv);
1671}
1672
1673/**
1674 * batadv_iv_ogm_process - process an incoming batman iv OGM
1675 * @skb: the skb containing the OGM
1676 * @ogm_offset: offset to the OGM which should be processed (for aggregates)
1677 * @if_incoming: the interface where this packet was receved
1678 */
1679static void batadv_iv_ogm_process(const struct sk_buff *skb, int ogm_offset,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001680 struct batadv_hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +02001681{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001682 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001683 struct batadv_orig_node *orig_neigh_node, *orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001684 struct batadv_hard_iface *hard_iface;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001685 struct batadv_ogm_packet *ogm_packet;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001686 u32 if_incoming_seqno;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001687 bool has_directlink_flag;
1688 struct ethhdr *ethhdr;
1689 bool is_my_oldorig = false;
1690 bool is_my_addr = false;
1691 bool is_my_orig = false;
1692
1693 ogm_packet = (struct batadv_ogm_packet *)(skb->data + ogm_offset);
1694 ethhdr = eth_hdr(skb);
Marek Lindnerfc957272011-07-30 12:04:12 +02001695
1696 /* Silently drop when the batman packet is actually not a
1697 * correct packet.
1698 *
1699 * This might happen if a packet is padded (e.g. Ethernet has a
1700 * minimum frame length of 64 byte) and the aggregation interprets
1701 * it as an additional length.
1702 *
1703 * TODO: A more sane solution would be to have a bit in the
Sven Eckelmann96412692012-06-05 22:31:30 +02001704 * batadv_ogm_packet to detect whether the packet is the last
Marek Lindnerfc957272011-07-30 12:04:12 +02001705 * packet in an aggregation. Here we expect that the padding
1706 * is always zero (or not 0x01)
1707 */
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001708 if (ogm_packet->packet_type != BATADV_IV_OGM)
Marek Lindnerfc957272011-07-30 12:04:12 +02001709 return;
1710
1711 /* could be changed by schedule_own_packet() */
Marek Lindner14511512012-08-02 17:20:26 +02001712 if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno);
Marek Lindnerfc957272011-07-30 12:04:12 +02001713
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001714 if (ogm_packet->flags & BATADV_DIRECTLINK)
1715 has_directlink_flag = true;
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001716 else
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001717 has_directlink_flag = false;
Marek Lindnerfc957272011-07-30 12:04:12 +02001718
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001719 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001720 "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 +02001721 ethhdr->h_source, if_incoming->net_dev->name,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001722 if_incoming->net_dev->dev_addr, ogm_packet->orig,
1723 ogm_packet->prev_sender, ntohl(ogm_packet->seqno),
1724 ogm_packet->tq, ogm_packet->ttl,
1725 ogm_packet->version, has_directlink_flag);
Marek Lindnerfc957272011-07-30 12:04:12 +02001726
1727 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001728 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001729 if (hard_iface->if_status != BATADV_IF_ACTIVE)
Marek Lindnerfc957272011-07-30 12:04:12 +02001730 continue;
1731
1732 if (hard_iface->soft_iface != if_incoming->soft_iface)
1733 continue;
1734
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001735 if (batadv_compare_eth(ethhdr->h_source,
1736 hard_iface->net_dev->dev_addr))
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001737 is_my_addr = true;
Marek Lindnerfc957272011-07-30 12:04:12 +02001738
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001739 if (batadv_compare_eth(ogm_packet->orig,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001740 hard_iface->net_dev->dev_addr))
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001741 is_my_orig = true;
Marek Lindnerfc957272011-07-30 12:04:12 +02001742
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001743 if (batadv_compare_eth(ogm_packet->prev_sender,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001744 hard_iface->net_dev->dev_addr))
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001745 is_my_oldorig = true;
Marek Lindnerfc957272011-07-30 12:04:12 +02001746 }
1747 rcu_read_unlock();
1748
Marek Lindnerfc957272011-07-30 12:04:12 +02001749 if (is_my_addr) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001750 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001751 "Drop packet: received my own broadcast (sender: %pM)\n",
1752 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001753 return;
1754 }
1755
Marek Lindnerfc957272011-07-30 12:04:12 +02001756 if (is_my_orig) {
1757 unsigned long *word;
Sven Eckelmann8142ea62020-03-16 23:30:53 +01001758 size_t offset;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001759 s32 bit_pos;
Sven Eckelmann8142ea62020-03-16 23:30:53 +01001760 unsigned int if_num;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001761 u8 *weight;
Marek Lindnerfc957272011-07-30 12:04:12 +02001762
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001763 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1764 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001765 if (!orig_neigh_node)
1766 return;
1767
1768 /* neighbor has to indicate direct link and it has to
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001769 * come via the corresponding interface
1770 * save packet seqno for bidirectional check
1771 */
Marek Lindnerfc957272011-07-30 12:04:12 +02001772 if (has_directlink_flag &&
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001773 batadv_compare_eth(if_incoming->net_dev->dev_addr,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001774 ogm_packet->orig)) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001775 if_num = if_incoming->if_num;
1776 offset = if_num * BATADV_NUM_WORDS;
Marek Lindnerfc957272011-07-30 12:04:12 +02001777
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001778 spin_lock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
Antonio Quartulli32db6aa2014-09-01 14:37:29 +02001779 word = &orig_neigh_node->bat_iv.bcast_own[offset];
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001780 bit_pos = if_incoming_seqno - 2;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001781 bit_pos -= ntohl(ogm_packet->seqno);
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001782 batadv_set_bit(word, bit_pos);
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001783 weight = &orig_neigh_node->bat_iv.bcast_own_sum[if_num];
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001784 *weight = bitmap_weight(word,
1785 BATADV_TQ_LOCAL_WINDOW_SIZE);
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001786 spin_unlock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001787 }
1788
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001789 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001790 "Drop packet: originator packet from myself (via neighbor)\n");
Sven Eckelmann5d967312016-01-17 11:01:09 +01001791 batadv_orig_node_put(orig_neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001792 return;
1793 }
1794
1795 if (is_my_oldorig) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001796 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001797 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1798 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001799 return;
1800 }
1801
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001802 if (ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001803 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001804 "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1805 ethhdr->h_source);
Marek Lindner13b25412012-03-11 06:17:53 +08001806 return;
1807 }
1808
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001809 orig_node = batadv_iv_ogm_orig_get(bat_priv, ogm_packet->orig);
Marek Lindnerfc957272011-07-30 12:04:12 +02001810 if (!orig_node)
1811 return;
1812
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001813 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1814 if_incoming, BATADV_IF_DEFAULT);
Marek Lindnerfc957272011-07-30 12:04:12 +02001815
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001816 rcu_read_lock();
1817 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1818 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1819 continue;
1820
1821 if (hard_iface->soft_iface != bat_priv->soft_iface)
1822 continue;
1823
Sven Eckelmann27353442016-03-05 16:09:16 +01001824 if (!kref_get_unless_zero(&hard_iface->refcount))
1825 continue;
1826
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001827 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1828 if_incoming, hard_iface);
Sven Eckelmann27353442016-03-05 16:09:16 +01001829
1830 batadv_hardif_put(hard_iface);
Marek Lindnerfc957272011-07-30 12:04:12 +02001831 }
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001832 rcu_read_unlock();
Marek Lindnerfc957272011-07-30 12:04:12 +02001833
Sven Eckelmann5d967312016-01-17 11:01:09 +01001834 batadv_orig_node_put(orig_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001835}
1836
Antonio Quartullif0d97252016-05-03 01:45:34 +08001837static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work)
1838{
1839 struct delayed_work *delayed_work;
1840 struct batadv_forw_packet *forw_packet;
1841 struct batadv_priv *bat_priv;
1842
1843 delayed_work = to_delayed_work(work);
1844 forw_packet = container_of(delayed_work, struct batadv_forw_packet,
1845 delayed_work);
1846 bat_priv = netdev_priv(forw_packet->if_incoming->soft_iface);
1847 spin_lock_bh(&bat_priv->forw_bat_list_lock);
1848 hlist_del(&forw_packet->list);
1849 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
1850
1851 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
1852 goto out;
1853
1854 batadv_iv_ogm_emit(forw_packet);
1855
1856 /* we have to have at least one packet in the queue to determine the
1857 * queues wake up time unless we are shutting down.
1858 *
1859 * only re-schedule if this is the "original" copy, e.g. the OGM of the
1860 * primary interface should only be rescheduled once per period, but
1861 * this function will be called for the forw_packet instances of the
1862 * other secondary interfaces as well.
1863 */
1864 if (forw_packet->own &&
1865 forw_packet->if_incoming == forw_packet->if_outgoing)
1866 batadv_iv_ogm_schedule(forw_packet->if_incoming);
1867
1868out:
Antonio Quartullif0d97252016-05-03 01:45:34 +08001869 batadv_forw_packet_free(forw_packet);
1870}
1871
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001872static int batadv_iv_ogm_receive(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001873 struct batadv_hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +02001874{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001875 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001876 struct batadv_ogm_packet *ogm_packet;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001877 u8 *packet_pos;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001878 int ogm_offset;
Marek Lindneref261572013-04-23 21:39:57 +08001879 bool ret;
Marek Lindnerc3e29312012-03-04 16:56:25 +08001880
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001881 ret = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
Marek Lindnerc3e29312012-03-04 16:56:25 +08001882 if (!ret)
1883 return NET_RX_DROP;
Marek Lindnerfc957272011-07-30 12:04:12 +02001884
Marek Lindneredbf12b2012-03-11 06:17:49 +08001885 /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
1886 * that does not have B.A.T.M.A.N. IV enabled ?
1887 */
Antonio Quartulli29824a52016-05-25 23:27:31 +08001888 if (bat_priv->algo_ops->iface.enable != batadv_iv_ogm_iface_enable)
Marek Lindneredbf12b2012-03-11 06:17:49 +08001889 return NET_RX_DROP;
1890
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001891 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
1892 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
Martin Hundebøllf8214862012-04-20 17:02:45 +02001893 skb->len + ETH_HLEN);
1894
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001895 ogm_offset = 0;
1896 ogm_packet = (struct batadv_ogm_packet *)skb->data;
Marek Lindnerfc957272011-07-30 12:04:12 +02001897
1898 /* unpack the aggregated packets and process them one by one */
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001899 while (batadv_iv_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
Sven Eckelmann6c59cb12019-08-22 08:55:36 +02001900 ogm_packet)) {
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001901 batadv_iv_ogm_process(skb, ogm_offset, if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001902
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001903 ogm_offset += BATADV_OGM_HLEN;
1904 ogm_offset += ntohs(ogm_packet->tvlv_len);
Marek Lindnerfc957272011-07-30 12:04:12 +02001905
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001906 packet_pos = skb->data + ogm_offset;
1907 ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Marek Lindnerb47506d2013-03-04 10:39:49 +08001908 }
Marek Lindnerc3e29312012-03-04 16:56:25 +08001909
1910 kfree_skb(skb);
1911 return NET_RX_SUCCESS;
Marek Lindnerfc957272011-07-30 12:04:12 +02001912}
Marek Lindner1c280472011-11-28 17:40:17 +08001913
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02001914#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001915/**
1916 * batadv_iv_ogm_orig_print_neigh - print neighbors for the originator table
Simon Wunderlich89652332013-11-13 19:14:46 +01001917 * @orig_node: the orig_node for which the neighbors are printed
1918 * @if_outgoing: outgoing interface for these entries
1919 * @seq: debugfs table seq_file struct
1920 *
1921 * Must be called while holding an rcu lock.
1922 */
1923static void
1924batadv_iv_ogm_orig_print_neigh(struct batadv_orig_node *orig_node,
1925 struct batadv_hard_iface *if_outgoing,
1926 struct seq_file *seq)
1927{
1928 struct batadv_neigh_node *neigh_node;
1929 struct batadv_neigh_ifinfo *n_ifinfo;
1930
1931 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1932 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
1933 if (!n_ifinfo)
1934 continue;
1935
1936 seq_printf(seq, " %pM (%3i)",
1937 neigh_node->addr,
1938 n_ifinfo->bat_iv.tq_avg);
1939
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001940 batadv_neigh_ifinfo_put(n_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01001941 }
1942}
1943
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001944/**
1945 * batadv_iv_ogm_orig_print - print the originator table
1946 * @bat_priv: the bat priv with all the soft interface information
1947 * @seq: debugfs table seq_file struct
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001948 * @if_outgoing: the outgoing interface for which this should be printed
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001949 */
1950static void batadv_iv_ogm_orig_print(struct batadv_priv *bat_priv,
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001951 struct seq_file *seq,
1952 struct batadv_hard_iface *if_outgoing)
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001953{
Simon Wunderlich89652332013-11-13 19:14:46 +01001954 struct batadv_neigh_node *neigh_node;
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001955 struct batadv_hashtable *hash = bat_priv->orig_hash;
1956 int last_seen_msecs, last_seen_secs;
1957 struct batadv_orig_node *orig_node;
Simon Wunderlich89652332013-11-13 19:14:46 +01001958 struct batadv_neigh_ifinfo *n_ifinfo;
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001959 unsigned long last_seen_jiffies;
1960 struct hlist_head *head;
1961 int batman_count = 0;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001962 u32 i;
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001963
Antonio Quartulli925a6f32016-03-12 10:30:18 +01001964 seq_puts(seq,
1965 " Originator last-seen (#/255) Nexthop [outgoingIF]: Potential nexthops ...\n");
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001966
1967 for (i = 0; i < hash->size; i++) {
1968 head = &hash->table[i];
1969
1970 rcu_read_lock();
1971 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001972 neigh_node = batadv_orig_router_get(orig_node,
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001973 if_outgoing);
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001974 if (!neigh_node)
1975 continue;
1976
Simon Wunderlich89652332013-11-13 19:14:46 +01001977 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node,
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001978 if_outgoing);
Simon Wunderlich89652332013-11-13 19:14:46 +01001979 if (!n_ifinfo)
1980 goto next;
1981
1982 if (n_ifinfo->bat_iv.tq_avg == 0)
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001983 goto next;
1984
1985 last_seen_jiffies = jiffies - orig_node->last_seen;
1986 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
1987 last_seen_secs = last_seen_msecs / 1000;
1988 last_seen_msecs = last_seen_msecs % 1000;
1989
1990 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
1991 orig_node->orig, last_seen_secs,
Simon Wunderlich89652332013-11-13 19:14:46 +01001992 last_seen_msecs, n_ifinfo->bat_iv.tq_avg,
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001993 neigh_node->addr,
1994 neigh_node->if_incoming->net_dev->name);
1995
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001996 batadv_iv_ogm_orig_print_neigh(orig_node, if_outgoing,
1997 seq);
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001998 seq_puts(seq, "\n");
1999 batman_count++;
2000
2001next:
Sven Eckelmann25bb2502016-01-17 11:01:11 +01002002 batadv_neigh_node_put(neigh_node);
Simon Wunderlich89652332013-11-13 19:14:46 +01002003 if (n_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01002004 batadv_neigh_ifinfo_put(n_ifinfo);
Antonio Quartulli737a2a222013-09-02 12:15:03 +02002005 }
2006 rcu_read_unlock();
2007 }
2008
2009 if (batman_count == 0)
2010 seq_puts(seq, "No batman nodes in range ...\n");
2011}
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002012#endif
Antonio Quartulli737a2a222013-09-02 12:15:03 +02002013
Antonio Quartullia3285a82013-09-02 12:15:04 +02002014/**
Matthias Schiffer024f99c2016-07-03 13:31:40 +02002015 * batadv_iv_ogm_neigh_get_tq_avg - Get the TQ average for a neighbour on a
2016 * given outgoing interface.
2017 * @neigh_node: Neighbour of interest
2018 * @if_outgoing: Outgoing interface of interest
2019 * @tq_avg: Pointer of where to store the TQ average
2020 *
2021 * Return: False if no average TQ available, otherwise true.
2022 */
2023static bool
2024batadv_iv_ogm_neigh_get_tq_avg(struct batadv_neigh_node *neigh_node,
2025 struct batadv_hard_iface *if_outgoing,
2026 u8 *tq_avg)
2027{
2028 struct batadv_neigh_ifinfo *n_ifinfo;
2029
2030 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
2031 if (!n_ifinfo)
2032 return false;
2033
2034 *tq_avg = n_ifinfo->bat_iv.tq_avg;
2035 batadv_neigh_ifinfo_put(n_ifinfo);
2036
2037 return true;
2038}
2039
2040/**
2041 * batadv_iv_ogm_orig_dump_subentry - Dump an originator subentry into a
2042 * message
2043 * @msg: Netlink message to dump into
2044 * @portid: Port making netlink request
2045 * @seq: Sequence number of netlink message
2046 * @bat_priv: The bat priv with all the soft interface information
2047 * @if_outgoing: Limit dump to entries with this outgoing interface
2048 * @orig_node: Originator to dump
2049 * @neigh_node: Single hops neighbour
2050 * @best: Is the best originator
2051 *
2052 * Return: Error code, or 0 on success
2053 */
2054static int
2055batadv_iv_ogm_orig_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
2056 struct batadv_priv *bat_priv,
2057 struct batadv_hard_iface *if_outgoing,
2058 struct batadv_orig_node *orig_node,
2059 struct batadv_neigh_node *neigh_node,
2060 bool best)
2061{
2062 void *hdr;
2063 u8 tq_avg;
2064 unsigned int last_seen_msecs;
2065
2066 last_seen_msecs = jiffies_to_msecs(jiffies - orig_node->last_seen);
2067
2068 if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node, if_outgoing, &tq_avg))
2069 return 0;
2070
2071 if (if_outgoing != BATADV_IF_DEFAULT &&
2072 if_outgoing != neigh_node->if_incoming)
2073 return 0;
2074
2075 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2076 NLM_F_MULTI, BATADV_CMD_GET_ORIGINATORS);
2077 if (!hdr)
2078 return -ENOBUFS;
2079
2080 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
2081 orig_node->orig) ||
2082 nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
2083 neigh_node->addr) ||
2084 nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
2085 neigh_node->if_incoming->net_dev->ifindex) ||
2086 nla_put_u8(msg, BATADV_ATTR_TQ, tq_avg) ||
2087 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
2088 last_seen_msecs))
2089 goto nla_put_failure;
2090
2091 if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
2092 goto nla_put_failure;
2093
2094 genlmsg_end(msg, hdr);
2095 return 0;
2096
2097 nla_put_failure:
2098 genlmsg_cancel(msg, hdr);
2099 return -EMSGSIZE;
2100}
2101
2102/**
2103 * batadv_iv_ogm_orig_dump_entry - Dump an originator entry into a message
2104 * @msg: Netlink message to dump into
2105 * @portid: Port making netlink request
2106 * @seq: Sequence number of netlink message
2107 * @bat_priv: The bat priv with all the soft interface information
2108 * @if_outgoing: Limit dump to entries with this outgoing interface
2109 * @orig_node: Originator to dump
2110 * @sub_s: Number of sub entries to skip
2111 *
2112 * This function assumes the caller holds rcu_read_lock().
2113 *
2114 * Return: Error code, or 0 on success
2115 */
2116static int
2117batadv_iv_ogm_orig_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
2118 struct batadv_priv *bat_priv,
2119 struct batadv_hard_iface *if_outgoing,
2120 struct batadv_orig_node *orig_node, int *sub_s)
2121{
2122 struct batadv_neigh_node *neigh_node_best;
2123 struct batadv_neigh_node *neigh_node;
2124 int sub = 0;
2125 bool best;
2126 u8 tq_avg_best;
2127
2128 neigh_node_best = batadv_orig_router_get(orig_node, if_outgoing);
2129 if (!neigh_node_best)
2130 goto out;
2131
2132 if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node_best, if_outgoing,
2133 &tq_avg_best))
2134 goto out;
2135
2136 if (tq_avg_best == 0)
2137 goto out;
2138
2139 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
2140 if (sub++ < *sub_s)
2141 continue;
2142
2143 best = (neigh_node == neigh_node_best);
2144
2145 if (batadv_iv_ogm_orig_dump_subentry(msg, portid, seq,
2146 bat_priv, if_outgoing,
2147 orig_node, neigh_node,
2148 best)) {
2149 batadv_neigh_node_put(neigh_node_best);
2150
2151 *sub_s = sub - 1;
2152 return -EMSGSIZE;
2153 }
2154 }
2155
2156 out:
2157 if (neigh_node_best)
2158 batadv_neigh_node_put(neigh_node_best);
2159
2160 *sub_s = 0;
2161 return 0;
2162}
2163
2164/**
2165 * batadv_iv_ogm_orig_dump_bucket - Dump an originator bucket into a
2166 * message
2167 * @msg: Netlink message to dump into
2168 * @portid: Port making netlink request
2169 * @seq: Sequence number of netlink message
2170 * @bat_priv: The bat priv with all the soft interface information
2171 * @if_outgoing: Limit dump to entries with this outgoing interface
2172 * @head: Bucket to be dumped
2173 * @idx_s: Number of entries to be skipped
2174 * @sub: Number of sub entries to be skipped
2175 *
2176 * Return: Error code, or 0 on success
2177 */
2178static int
2179batadv_iv_ogm_orig_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
2180 struct batadv_priv *bat_priv,
2181 struct batadv_hard_iface *if_outgoing,
2182 struct hlist_head *head, int *idx_s, int *sub)
2183{
2184 struct batadv_orig_node *orig_node;
2185 int idx = 0;
2186
2187 rcu_read_lock();
2188 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
2189 if (idx++ < *idx_s)
2190 continue;
2191
2192 if (batadv_iv_ogm_orig_dump_entry(msg, portid, seq, bat_priv,
2193 if_outgoing, orig_node,
2194 sub)) {
2195 rcu_read_unlock();
2196 *idx_s = idx - 1;
2197 return -EMSGSIZE;
2198 }
2199 }
2200 rcu_read_unlock();
2201
2202 *idx_s = 0;
2203 *sub = 0;
2204 return 0;
2205}
2206
2207/**
2208 * batadv_iv_ogm_orig_dump - Dump the originators into a message
2209 * @msg: Netlink message to dump into
2210 * @cb: Control block containing additional options
2211 * @bat_priv: The bat priv with all the soft interface information
2212 * @if_outgoing: Limit dump to entries with this outgoing interface
2213 */
2214static void
2215batadv_iv_ogm_orig_dump(struct sk_buff *msg, struct netlink_callback *cb,
2216 struct batadv_priv *bat_priv,
2217 struct batadv_hard_iface *if_outgoing)
2218{
2219 struct batadv_hashtable *hash = bat_priv->orig_hash;
2220 struct hlist_head *head;
2221 int bucket = cb->args[0];
2222 int idx = cb->args[1];
2223 int sub = cb->args[2];
2224 int portid = NETLINK_CB(cb->skb).portid;
2225
2226 while (bucket < hash->size) {
2227 head = &hash->table[bucket];
2228
2229 if (batadv_iv_ogm_orig_dump_bucket(msg, portid,
2230 cb->nlh->nlmsg_seq,
2231 bat_priv, if_outgoing, head,
2232 &idx, &sub))
2233 break;
2234
2235 bucket++;
2236 }
2237
2238 cb->args[0] = bucket;
2239 cb->args[1] = idx;
2240 cb->args[2] = sub;
2241}
2242
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002243#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Matthias Schiffer024f99c2016-07-03 13:31:40 +02002244/**
Marek Lindner75874052015-08-04 21:09:57 +08002245 * batadv_iv_hardif_neigh_print - print a single hop neighbour node
2246 * @seq: neighbour table seq_file struct
2247 * @hardif_neigh: hardif neighbour information
2248 */
2249static void
2250batadv_iv_hardif_neigh_print(struct seq_file *seq,
2251 struct batadv_hardif_neigh_node *hardif_neigh)
2252{
2253 int last_secs, last_msecs;
2254
2255 last_secs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) / 1000;
2256 last_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) % 1000;
2257
2258 seq_printf(seq, " %10s %pM %4i.%03is\n",
2259 hardif_neigh->if_incoming->net_dev->name,
2260 hardif_neigh->addr, last_secs, last_msecs);
2261}
2262
2263/**
2264 * batadv_iv_ogm_neigh_print - print the single hop neighbour list
2265 * @bat_priv: the bat priv with all the soft interface information
2266 * @seq: neighbour table seq_file struct
2267 */
2268static void batadv_iv_neigh_print(struct batadv_priv *bat_priv,
2269 struct seq_file *seq)
2270{
2271 struct net_device *net_dev = (struct net_device *)seq->private;
2272 struct batadv_hardif_neigh_node *hardif_neigh;
2273 struct batadv_hard_iface *hard_iface;
2274 int batman_count = 0;
2275
Antonio Quartulli925a6f32016-03-12 10:30:18 +01002276 seq_puts(seq, " IF Neighbor last-seen\n");
Marek Lindner75874052015-08-04 21:09:57 +08002277
2278 rcu_read_lock();
2279 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
2280 if (hard_iface->soft_iface != net_dev)
2281 continue;
2282
2283 hlist_for_each_entry_rcu(hardif_neigh,
2284 &hard_iface->neigh_list, list) {
2285 batadv_iv_hardif_neigh_print(seq, hardif_neigh);
2286 batman_count++;
2287 }
2288 }
2289 rcu_read_unlock();
2290
2291 if (batman_count == 0)
2292 seq_puts(seq, "No batman nodes in range ...\n");
2293}
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002294#endif
Marek Lindner75874052015-08-04 21:09:57 +08002295
2296/**
Markus Pargmann57b12502016-07-03 11:07:14 +02002297 * batadv_iv_ogm_neigh_diff - calculate tq difference of two neighbors
2298 * @neigh1: the first neighbor object of the comparison
2299 * @if_outgoing1: outgoing interface for the first neighbor
2300 * @neigh2: the second neighbor object of the comparison
2301 * @if_outgoing2: outgoing interface for the second neighbor
2302 * @diff: pointer to integer receiving the calculated difference
2303 *
2304 * The content of *@diff is only valid when this function returns true.
2305 * It is less, equal to or greater than 0 if the metric via neigh1 is lower,
2306 * the same as or higher than the metric via neigh2
2307 *
2308 * Return: true when the difference could be calculated, false otherwise
2309 */
2310static bool batadv_iv_ogm_neigh_diff(struct batadv_neigh_node *neigh1,
2311 struct batadv_hard_iface *if_outgoing1,
2312 struct batadv_neigh_node *neigh2,
2313 struct batadv_hard_iface *if_outgoing2,
2314 int *diff)
2315{
2316 struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
2317 u8 tq1, tq2;
2318 bool ret = true;
2319
2320 neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
2321 neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
2322
2323 if (!neigh1_ifinfo || !neigh2_ifinfo) {
2324 ret = false;
2325 goto out;
2326 }
2327
2328 tq1 = neigh1_ifinfo->bat_iv.tq_avg;
2329 tq2 = neigh2_ifinfo->bat_iv.tq_avg;
2330 *diff = (int)tq1 - (int)tq2;
2331
2332out:
2333 if (neigh1_ifinfo)
2334 batadv_neigh_ifinfo_put(neigh1_ifinfo);
2335 if (neigh2_ifinfo)
2336 batadv_neigh_ifinfo_put(neigh2_ifinfo);
2337
2338 return ret;
2339}
2340
2341/**
Matthias Schiffer024f99c2016-07-03 13:31:40 +02002342 * batadv_iv_ogm_neigh_dump_neigh - Dump a neighbour into a netlink message
2343 * @msg: Netlink message to dump into
2344 * @portid: Port making netlink request
2345 * @seq: Sequence number of netlink message
2346 * @hardif_neigh: Neighbour to be dumped
2347 *
2348 * Return: Error code, or 0 on success
2349 */
2350static int
2351batadv_iv_ogm_neigh_dump_neigh(struct sk_buff *msg, u32 portid, u32 seq,
2352 struct batadv_hardif_neigh_node *hardif_neigh)
2353{
2354 void *hdr;
2355 unsigned int last_seen_msecs;
2356
2357 last_seen_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen);
2358
2359 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2360 NLM_F_MULTI, BATADV_CMD_GET_NEIGHBORS);
2361 if (!hdr)
2362 return -ENOBUFS;
2363
2364 if (nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
2365 hardif_neigh->addr) ||
2366 nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
2367 hardif_neigh->if_incoming->net_dev->ifindex) ||
2368 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
2369 last_seen_msecs))
2370 goto nla_put_failure;
2371
2372 genlmsg_end(msg, hdr);
2373 return 0;
2374
2375 nla_put_failure:
2376 genlmsg_cancel(msg, hdr);
2377 return -EMSGSIZE;
2378}
2379
2380/**
2381 * batadv_iv_ogm_neigh_dump_hardif - Dump the neighbours of a hard interface
2382 * into a message
2383 * @msg: Netlink message to dump into
2384 * @portid: Port making netlink request
2385 * @seq: Sequence number of netlink message
2386 * @bat_priv: The bat priv with all the soft interface information
2387 * @hard_iface: Hard interface to dump the neighbours for
2388 * @idx_s: Number of entries to skip
2389 *
2390 * This function assumes the caller holds rcu_read_lock().
2391 *
2392 * Return: Error code, or 0 on success
2393 */
2394static int
2395batadv_iv_ogm_neigh_dump_hardif(struct sk_buff *msg, u32 portid, u32 seq,
2396 struct batadv_priv *bat_priv,
2397 struct batadv_hard_iface *hard_iface,
2398 int *idx_s)
2399{
2400 struct batadv_hardif_neigh_node *hardif_neigh;
2401 int idx = 0;
2402
2403 hlist_for_each_entry_rcu(hardif_neigh,
2404 &hard_iface->neigh_list, list) {
2405 if (idx++ < *idx_s)
2406 continue;
2407
2408 if (batadv_iv_ogm_neigh_dump_neigh(msg, portid, seq,
2409 hardif_neigh)) {
2410 *idx_s = idx - 1;
2411 return -EMSGSIZE;
2412 }
2413 }
2414
2415 *idx_s = 0;
2416 return 0;
2417}
2418
2419/**
2420 * batadv_iv_ogm_neigh_dump - Dump the neighbours into a message
2421 * @msg: Netlink message to dump into
2422 * @cb: Control block containing additional options
2423 * @bat_priv: The bat priv with all the soft interface information
2424 * @single_hardif: Limit dump to this hard interfaace
2425 */
2426static void
2427batadv_iv_ogm_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb,
2428 struct batadv_priv *bat_priv,
2429 struct batadv_hard_iface *single_hardif)
2430{
2431 struct batadv_hard_iface *hard_iface;
2432 int i_hardif = 0;
2433 int i_hardif_s = cb->args[0];
2434 int idx = cb->args[1];
2435 int portid = NETLINK_CB(cb->skb).portid;
2436
2437 rcu_read_lock();
2438 if (single_hardif) {
2439 if (i_hardif_s == 0) {
2440 if (batadv_iv_ogm_neigh_dump_hardif(msg, portid,
2441 cb->nlh->nlmsg_seq,
2442 bat_priv,
2443 single_hardif,
2444 &idx) == 0)
2445 i_hardif++;
2446 }
2447 } else {
2448 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list,
2449 list) {
2450 if (hard_iface->soft_iface != bat_priv->soft_iface)
2451 continue;
2452
2453 if (i_hardif++ < i_hardif_s)
2454 continue;
2455
2456 if (batadv_iv_ogm_neigh_dump_hardif(msg, portid,
2457 cb->nlh->nlmsg_seq,
2458 bat_priv,
2459 hard_iface, &idx)) {
2460 i_hardif--;
2461 break;
2462 }
2463 }
2464 }
2465 rcu_read_unlock();
2466
2467 cb->args[0] = i_hardif;
2468 cb->args[1] = idx;
2469}
2470
2471/**
Antonio Quartullia3285a82013-09-02 12:15:04 +02002472 * batadv_iv_ogm_neigh_cmp - compare the metrics of two neighbors
2473 * @neigh1: the first neighbor object of the comparison
Simon Wunderlich89652332013-11-13 19:14:46 +01002474 * @if_outgoing1: outgoing interface for the first neighbor
Antonio Quartullia3285a82013-09-02 12:15:04 +02002475 * @neigh2: the second neighbor object of the comparison
Simon Wunderlich89652332013-11-13 19:14:46 +01002476 * @if_outgoing2: outgoing interface for the second neighbor
Antonio Quartullia3285a82013-09-02 12:15:04 +02002477 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002478 * Return: a value less, equal to or greater than 0 if the metric via neigh1 is
Antonio Quartullia3285a82013-09-02 12:15:04 +02002479 * lower, the same as or higher than the metric via neigh2
2480 */
2481static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1,
Simon Wunderlich89652332013-11-13 19:14:46 +01002482 struct batadv_hard_iface *if_outgoing1,
2483 struct batadv_neigh_node *neigh2,
2484 struct batadv_hard_iface *if_outgoing2)
Antonio Quartullia3285a82013-09-02 12:15:04 +02002485{
Markus Pargmann57b12502016-07-03 11:07:14 +02002486 bool ret;
Simon Wunderlich89652332013-11-13 19:14:46 +01002487 int diff;
Antonio Quartullia3285a82013-09-02 12:15:04 +02002488
Markus Pargmann57b12502016-07-03 11:07:14 +02002489 ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
2490 if_outgoing2, &diff);
2491 if (!ret)
2492 return 0;
Simon Wunderlich89652332013-11-13 19:14:46 +01002493
2494 return diff;
Antonio Quartullia3285a82013-09-02 12:15:04 +02002495}
2496
Antonio Quartullic43c9812013-09-02 12:15:05 +02002497/**
Simon Wunderlich18165f62015-08-08 02:01:50 +02002498 * batadv_iv_ogm_neigh_is_sob - check if neigh1 is similarly good or better
2499 * than neigh2 from the metric prospective
Antonio Quartullic43c9812013-09-02 12:15:05 +02002500 * @neigh1: the first neighbor object of the comparison
Martin Hundebøll95298a92014-07-15 09:41:05 +02002501 * @if_outgoing1: outgoing interface for the first neighbor
Antonio Quartullic43c9812013-09-02 12:15:05 +02002502 * @neigh2: the second neighbor object of the comparison
Simon Wunderlich89652332013-11-13 19:14:46 +01002503 * @if_outgoing2: outgoing interface for the second neighbor
Martin Hundebøll95298a92014-07-15 09:41:05 +02002504 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002505 * Return: true if the metric via neigh1 is equally good or better than
Simon Wunderlich89652332013-11-13 19:14:46 +01002506 * the metric via neigh2, false otherwise.
2507 */
2508static bool
Simon Wunderlich18165f62015-08-08 02:01:50 +02002509batadv_iv_ogm_neigh_is_sob(struct batadv_neigh_node *neigh1,
Simon Wunderlich89652332013-11-13 19:14:46 +01002510 struct batadv_hard_iface *if_outgoing1,
2511 struct batadv_neigh_node *neigh2,
2512 struct batadv_hard_iface *if_outgoing2)
2513{
Simon Wunderlich89652332013-11-13 19:14:46 +01002514 bool ret;
Markus Pargmann57b12502016-07-03 11:07:14 +02002515 int diff;
Simon Wunderlich89652332013-11-13 19:14:46 +01002516
Markus Pargmann57b12502016-07-03 11:07:14 +02002517 ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
2518 if_outgoing2, &diff);
2519 if (!ret)
2520 return false;
Simon Wunderlich89652332013-11-13 19:14:46 +01002521
Markus Pargmann57b12502016-07-03 11:07:14 +02002522 ret = diff > -BATADV_TQ_SIMILARITY_THRESHOLD;
Simon Wunderlich89652332013-11-13 19:14:46 +01002523 return ret;
Antonio Quartullic43c9812013-09-02 12:15:05 +02002524}
2525
Sven Eckelmann5f6dd572020-03-16 23:31:02 +01002526static void batadv_iv_iface_enabled(struct batadv_hard_iface *hard_iface)
Antonio Quartullif0d97252016-05-03 01:45:34 +08002527{
2528 /* begin scheduling originator messages on that interface */
2529 batadv_iv_ogm_schedule(hard_iface);
2530}
2531
Sven Eckelmann4ae2c062020-03-16 23:30:44 +01002532/**
2533 * batadv_iv_init_sel_class - initialize GW selection class
2534 * @bat_priv: the bat priv with all the soft interface information
2535 */
2536static void batadv_iv_init_sel_class(struct batadv_priv *bat_priv)
2537{
2538 /* set default TQ difference threshold to 20 */
2539 atomic_set(&bat_priv->gw.sel_class, 20);
2540}
2541
Antonio Quartulli34d99cf2016-07-03 12:46:33 +02002542static struct batadv_gw_node *
2543batadv_iv_gw_get_best_gw_node(struct batadv_priv *bat_priv)
2544{
2545 struct batadv_neigh_node *router;
2546 struct batadv_neigh_ifinfo *router_ifinfo;
2547 struct batadv_gw_node *gw_node, *curr_gw = NULL;
2548 u64 max_gw_factor = 0;
2549 u64 tmp_gw_factor = 0;
2550 u8 max_tq = 0;
2551 u8 tq_avg;
2552 struct batadv_orig_node *orig_node;
2553
2554 rcu_read_lock();
2555 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) {
2556 orig_node = gw_node->orig_node;
2557 router = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
2558 if (!router)
2559 continue;
2560
2561 router_ifinfo = batadv_neigh_ifinfo_get(router,
2562 BATADV_IF_DEFAULT);
2563 if (!router_ifinfo)
2564 goto next;
2565
2566 if (!kref_get_unless_zero(&gw_node->refcount))
2567 goto next;
2568
2569 tq_avg = router_ifinfo->bat_iv.tq_avg;
2570
2571 switch (atomic_read(&bat_priv->gw.sel_class)) {
2572 case 1: /* fast connection */
2573 tmp_gw_factor = tq_avg * tq_avg;
2574 tmp_gw_factor *= gw_node->bandwidth_down;
2575 tmp_gw_factor *= 100 * 100;
2576 tmp_gw_factor >>= 18;
2577
2578 if ((tmp_gw_factor > max_gw_factor) ||
2579 ((tmp_gw_factor == max_gw_factor) &&
2580 (tq_avg > max_tq))) {
2581 if (curr_gw)
2582 batadv_gw_node_put(curr_gw);
2583 curr_gw = gw_node;
2584 kref_get(&curr_gw->refcount);
2585 }
2586 break;
2587
2588 default: /* 2: stable connection (use best statistic)
2589 * 3: fast-switch (use best statistic but change as
2590 * soon as a better gateway appears)
2591 * XX: late-switch (use best statistic but change as
2592 * soon as a better gateway appears which has
2593 * $routing_class more tq points)
2594 */
2595 if (tq_avg > max_tq) {
2596 if (curr_gw)
2597 batadv_gw_node_put(curr_gw);
2598 curr_gw = gw_node;
2599 kref_get(&curr_gw->refcount);
2600 }
2601 break;
2602 }
2603
2604 if (tq_avg > max_tq)
2605 max_tq = tq_avg;
2606
2607 if (tmp_gw_factor > max_gw_factor)
2608 max_gw_factor = tmp_gw_factor;
2609
2610 batadv_gw_node_put(gw_node);
2611
2612next:
2613 batadv_neigh_node_put(router);
2614 if (router_ifinfo)
2615 batadv_neigh_ifinfo_put(router_ifinfo);
2616 }
2617 rcu_read_unlock();
2618
2619 return curr_gw;
2620}
2621
2622static bool batadv_iv_gw_is_eligible(struct batadv_priv *bat_priv,
2623 struct batadv_orig_node *curr_gw_orig,
2624 struct batadv_orig_node *orig_node)
2625{
2626 struct batadv_neigh_ifinfo *router_orig_ifinfo = NULL;
2627 struct batadv_neigh_ifinfo *router_gw_ifinfo = NULL;
2628 struct batadv_neigh_node *router_gw = NULL;
2629 struct batadv_neigh_node *router_orig = NULL;
2630 u8 gw_tq_avg, orig_tq_avg;
2631 bool ret = false;
2632
2633 /* dynamic re-election is performed only on fast or late switch */
2634 if (atomic_read(&bat_priv->gw.sel_class) <= 2)
2635 return false;
2636
2637 router_gw = batadv_orig_router_get(curr_gw_orig, BATADV_IF_DEFAULT);
2638 if (!router_gw) {
2639 ret = true;
2640 goto out;
2641 }
2642
2643 router_gw_ifinfo = batadv_neigh_ifinfo_get(router_gw,
2644 BATADV_IF_DEFAULT);
2645 if (!router_gw_ifinfo) {
2646 ret = true;
2647 goto out;
2648 }
2649
2650 router_orig = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
2651 if (!router_orig)
2652 goto out;
2653
2654 router_orig_ifinfo = batadv_neigh_ifinfo_get(router_orig,
2655 BATADV_IF_DEFAULT);
2656 if (!router_orig_ifinfo)
2657 goto out;
2658
2659 gw_tq_avg = router_gw_ifinfo->bat_iv.tq_avg;
2660 orig_tq_avg = router_orig_ifinfo->bat_iv.tq_avg;
2661
2662 /* the TQ value has to be better */
2663 if (orig_tq_avg < gw_tq_avg)
2664 goto out;
2665
2666 /* if the routing class is greater than 3 the value tells us how much
2667 * greater the TQ value of the new gateway must be
2668 */
2669 if ((atomic_read(&bat_priv->gw.sel_class) > 3) &&
2670 (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw.sel_class)))
2671 goto out;
2672
2673 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
2674 "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n",
2675 gw_tq_avg, orig_tq_avg);
2676
2677 ret = true;
2678out:
2679 if (router_gw_ifinfo)
2680 batadv_neigh_ifinfo_put(router_gw_ifinfo);
2681 if (router_orig_ifinfo)
2682 batadv_neigh_ifinfo_put(router_orig_ifinfo);
2683 if (router_gw)
2684 batadv_neigh_node_put(router_gw);
2685 if (router_orig)
2686 batadv_neigh_node_put(router_orig);
2687
2688 return ret;
2689}
2690
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002691#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Antonio Quartulli34d99cf2016-07-03 12:46:33 +02002692/* fails if orig_node has no router */
2693static int batadv_iv_gw_write_buffer_text(struct batadv_priv *bat_priv,
2694 struct seq_file *seq,
2695 const struct batadv_gw_node *gw_node)
2696{
2697 struct batadv_gw_node *curr_gw;
2698 struct batadv_neigh_node *router;
2699 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
2700 int ret = -1;
2701
2702 router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
2703 if (!router)
2704 goto out;
2705
2706 router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
2707 if (!router_ifinfo)
2708 goto out;
2709
2710 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
2711
2712 seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n",
2713 (curr_gw == gw_node ? "=>" : " "),
2714 gw_node->orig_node->orig,
2715 router_ifinfo->bat_iv.tq_avg, router->addr,
2716 router->if_incoming->net_dev->name,
2717 gw_node->bandwidth_down / 10,
2718 gw_node->bandwidth_down % 10,
2719 gw_node->bandwidth_up / 10,
2720 gw_node->bandwidth_up % 10);
2721 ret = seq_has_overflowed(seq) ? -1 : 0;
2722
2723 if (curr_gw)
2724 batadv_gw_node_put(curr_gw);
2725out:
2726 if (router_ifinfo)
2727 batadv_neigh_ifinfo_put(router_ifinfo);
2728 if (router)
2729 batadv_neigh_node_put(router);
2730 return ret;
2731}
2732
2733static void batadv_iv_gw_print(struct batadv_priv *bat_priv,
2734 struct seq_file *seq)
2735{
2736 struct batadv_gw_node *gw_node;
2737 int gw_count = 0;
2738
2739 seq_puts(seq,
2740 " Gateway (#/255) Nexthop [outgoingIF]: advertised uplink bandwidth\n");
2741
2742 rcu_read_lock();
2743 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) {
2744 /* fails if orig_node has no router */
2745 if (batadv_iv_gw_write_buffer_text(bat_priv, seq, gw_node) < 0)
2746 continue;
2747
2748 gw_count++;
2749 }
2750 rcu_read_unlock();
2751
2752 if (gw_count == 0)
2753 seq_puts(seq, "No gateways in range ...\n");
2754}
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002755#endif
Antonio Quartulli34d99cf2016-07-03 12:46:33 +02002756
Andrew Lunnefb766a2016-07-03 13:31:43 +02002757/**
2758 * batadv_iv_gw_dump_entry - Dump a gateway into a message
2759 * @msg: Netlink message to dump into
2760 * @portid: Port making netlink request
2761 * @seq: Sequence number of netlink message
2762 * @bat_priv: The bat priv with all the soft interface information
2763 * @gw_node: Gateway to be dumped
2764 *
2765 * Return: Error code, or 0 on success
2766 */
2767static int batadv_iv_gw_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
2768 struct batadv_priv *bat_priv,
2769 struct batadv_gw_node *gw_node)
2770{
2771 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
2772 struct batadv_neigh_node *router;
Sven Eckelmannb5f6a0a2018-06-02 17:26:34 +02002773 struct batadv_gw_node *curr_gw = NULL;
Sven Eckelmann280a7b62018-02-19 14:08:52 +01002774 int ret = 0;
Andrew Lunnefb766a2016-07-03 13:31:43 +02002775 void *hdr;
2776
2777 router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
2778 if (!router)
2779 goto out;
2780
2781 router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
2782 if (!router_ifinfo)
2783 goto out;
2784
2785 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
2786
2787 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2788 NLM_F_MULTI, BATADV_CMD_GET_GATEWAYS);
2789 if (!hdr) {
2790 ret = -ENOBUFS;
2791 goto out;
2792 }
2793
2794 ret = -EMSGSIZE;
2795
2796 if (curr_gw == gw_node)
2797 if (nla_put_flag(msg, BATADV_ATTR_FLAG_BEST)) {
2798 genlmsg_cancel(msg, hdr);
2799 goto out;
2800 }
2801
2802 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
2803 gw_node->orig_node->orig) ||
2804 nla_put_u8(msg, BATADV_ATTR_TQ, router_ifinfo->bat_iv.tq_avg) ||
2805 nla_put(msg, BATADV_ATTR_ROUTER, ETH_ALEN,
2806 router->addr) ||
2807 nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
2808 router->if_incoming->net_dev->name) ||
2809 nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_DOWN,
2810 gw_node->bandwidth_down) ||
2811 nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_UP,
2812 gw_node->bandwidth_up)) {
2813 genlmsg_cancel(msg, hdr);
2814 goto out;
2815 }
2816
2817 genlmsg_end(msg, hdr);
2818 ret = 0;
2819
2820out:
Sven Eckelmannb5f6a0a2018-06-02 17:26:34 +02002821 if (curr_gw)
2822 batadv_gw_node_put(curr_gw);
Andrew Lunnefb766a2016-07-03 13:31:43 +02002823 if (router_ifinfo)
2824 batadv_neigh_ifinfo_put(router_ifinfo);
2825 if (router)
2826 batadv_neigh_node_put(router);
2827 return ret;
2828}
2829
2830/**
2831 * batadv_iv_gw_dump - Dump gateways into a message
2832 * @msg: Netlink message to dump into
2833 * @cb: Control block containing additional options
2834 * @bat_priv: The bat priv with all the soft interface information
2835 */
2836static void batadv_iv_gw_dump(struct sk_buff *msg, struct netlink_callback *cb,
2837 struct batadv_priv *bat_priv)
2838{
2839 int portid = NETLINK_CB(cb->skb).portid;
2840 struct batadv_gw_node *gw_node;
2841 int idx_skip = cb->args[0];
2842 int idx = 0;
2843
2844 rcu_read_lock();
2845 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) {
2846 if (idx++ < idx_skip)
2847 continue;
2848
2849 if (batadv_iv_gw_dump_entry(msg, portid, cb->nlh->nlmsg_seq,
2850 bat_priv, gw_node)) {
2851 idx_skip = idx - 1;
2852 goto unlock;
2853 }
2854 }
2855
2856 idx_skip = idx;
2857unlock:
2858 rcu_read_unlock();
2859
2860 cb->args[0] = idx_skip;
2861}
2862
Sven Eckelmann56303d32012-06-05 22:31:31 +02002863static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
Marek Lindner519d3492012-04-18 17:15:57 +08002864 .name = "BATMAN_IV",
Antonio Quartulli29824a52016-05-25 23:27:31 +08002865 .iface = {
Antonio Quartulli29824a52016-05-25 23:27:31 +08002866 .enable = batadv_iv_ogm_iface_enable,
Sven Eckelmann5f6dd572020-03-16 23:31:02 +01002867 .enabled = batadv_iv_iface_enabled,
Antonio Quartulli29824a52016-05-25 23:27:31 +08002868 .disable = batadv_iv_ogm_iface_disable,
2869 .update_mac = batadv_iv_ogm_iface_update_mac,
2870 .primary_set = batadv_iv_ogm_primary_iface_set,
2871 },
2872 .neigh = {
2873 .cmp = batadv_iv_ogm_neigh_cmp,
2874 .is_similar_or_better = batadv_iv_ogm_neigh_is_sob,
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002875#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Antonio Quartulli29824a52016-05-25 23:27:31 +08002876 .print = batadv_iv_neigh_print,
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002877#endif
Matthias Schiffer024f99c2016-07-03 13:31:40 +02002878 .dump = batadv_iv_ogm_neigh_dump,
Antonio Quartulli29824a52016-05-25 23:27:31 +08002879 },
2880 .orig = {
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002881#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Antonio Quartulli29824a52016-05-25 23:27:31 +08002882 .print = batadv_iv_ogm_orig_print,
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002883#endif
Matthias Schiffer024f99c2016-07-03 13:31:40 +02002884 .dump = batadv_iv_ogm_orig_dump,
Antonio Quartulli29824a52016-05-25 23:27:31 +08002885 .free = batadv_iv_ogm_orig_free,
2886 .add_if = batadv_iv_ogm_orig_add_if,
2887 .del_if = batadv_iv_ogm_orig_del_if,
2888 },
Antonio Quartulli34d99cf2016-07-03 12:46:33 +02002889 .gw = {
Sven Eckelmann4ae2c062020-03-16 23:30:44 +01002890 .init_sel_class = batadv_iv_init_sel_class,
Antonio Quartulli34d99cf2016-07-03 12:46:33 +02002891 .get_best_gw_node = batadv_iv_gw_get_best_gw_node,
2892 .is_eligible = batadv_iv_gw_is_eligible,
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002893#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Antonio Quartulli34d99cf2016-07-03 12:46:33 +02002894 .print = batadv_iv_gw_print,
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002895#endif
Andrew Lunnefb766a2016-07-03 13:31:43 +02002896 .dump = batadv_iv_gw_dump,
Antonio Quartulli34d99cf2016-07-03 12:46:33 +02002897 },
Marek Lindner1c280472011-11-28 17:40:17 +08002898};
2899
Sven Eckelmann81c524f2012-05-12 02:09:22 +02002900int __init batadv_iv_init(void)
Marek Lindner1c280472011-11-28 17:40:17 +08002901{
Marek Lindnerc3e29312012-03-04 16:56:25 +08002902 int ret;
2903
2904 /* batman originator packet */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002905 ret = batadv_recv_handler_register(BATADV_IV_OGM,
2906 batadv_iv_ogm_receive);
Marek Lindnerc3e29312012-03-04 16:56:25 +08002907 if (ret < 0)
2908 goto out;
2909
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02002910 ret = batadv_algo_register(&batadv_batman_iv);
Marek Lindnerc3e29312012-03-04 16:56:25 +08002911 if (ret < 0)
2912 goto handler_unregister;
2913
2914 goto out;
2915
2916handler_unregister:
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002917 batadv_recv_handler_unregister(BATADV_IV_OGM);
Marek Lindnerc3e29312012-03-04 16:56:25 +08002918out:
2919 return ret;
Marek Lindner1c280472011-11-28 17:40:17 +08002920}