blob: fa8d6b475c063b2ce39cfba4ed7c762aeb30c3e4 [file] [log] [blame]
Sven Eckelmannac79cbb2017-01-01 00:00:00 +01001/* Copyright (C) 2007-2017 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 Eckelmann1e2c2a42015-04-17 19:40:28 +020037#include <linux/netdevice.h>
Matthias Schiffer024f99c2016-07-03 13:31:40 +020038#include <linux/netlink.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020039#include <linux/pkt_sched.h>
40#include <linux/printk.h>
41#include <linux/random.h>
42#include <linux/rculist.h>
43#include <linux/rcupdate.h>
44#include <linux/seq_file.h>
45#include <linux/skbuff.h>
46#include <linux/slab.h>
47#include <linux/spinlock.h>
48#include <linux/stddef.h>
49#include <linux/string.h>
50#include <linux/types.h>
51#include <linux/workqueue.h>
Matthias Schiffer024f99c2016-07-03 13:31:40 +020052#include <net/genetlink.h>
53#include <net/netlink.h>
54#include <uapi/linux/batman_adv.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020055
Sven Eckelmanna2d08162016-05-15 11:07:46 +020056#include "bat_algo.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020057#include "bitarray.h"
Antonio Quartulli34d99cf2016-07-03 12:46:33 +020058#include "gateway_client.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020059#include "hard-interface.h"
60#include "hash.h"
Sven Eckelmannba412082016-05-15 23:48:31 +020061#include "log.h"
Matthias Schiffer024f99c2016-07-03 13:31:40 +020062#include "netlink.h"
Martin Hundebølld56b1702013-01-25 11:12:39 +010063#include "network-coding.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020064#include "originator.h"
65#include "packet.h"
66#include "routing.h"
67#include "send.h"
68#include "translation-table.h"
Markus Pargmann1f8dce42016-05-15 11:07:43 +020069#include "tvlv.h"
Marek Lindnerfc957272011-07-30 12:04:12 +020070
Antonio Quartullif0d97252016-05-03 01:45:34 +080071static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work);
72
Antonio Quartulli791c2a22013-08-17 12:44:44 +020073/**
Martin Hundebøll95298a92014-07-15 09:41:05 +020074 * enum batadv_dup_status - duplicate status
Markus Pargmann9f52ee12014-12-26 12:41:34 +010075 * @BATADV_NO_DUP: the packet is no duplicate
Antonio Quartulli791c2a22013-08-17 12:44:44 +020076 * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for the
77 * neighbor)
78 * @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor
79 * @BATADV_PROTECTED: originator is currently protected (after reboot)
80 */
81enum batadv_dup_status {
82 BATADV_NO_DUP = 0,
83 BATADV_ORIG_DUP,
84 BATADV_NEIGH_DUP,
85 BATADV_PROTECTED,
86};
87
Antonio Quartulli24a5dee2013-04-08 09:38:12 +020088/**
89 * batadv_ring_buffer_set - update the ring buffer with the given value
90 * @lq_recv: pointer to the ring buffer
91 * @lq_index: index to store the value at
92 * @value: value to store in the ring buffer
93 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020094static void batadv_ring_buffer_set(u8 lq_recv[], u8 *lq_index, u8 value)
Antonio Quartulli24a5dee2013-04-08 09:38:12 +020095{
96 lq_recv[*lq_index] = value;
97 *lq_index = (*lq_index + 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE;
98}
99
100/**
Markus Pargmannd491dbb2014-12-26 12:41:36 +0100101 * batadv_ring_buffer_avg - compute the average of all non-zero values stored
Antonio Quartulli24a5dee2013-04-08 09:38:12 +0200102 * in the given ring buffer
103 * @lq_recv: pointer to the ring buffer
104 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200105 * Return: computed average value.
Antonio Quartulli24a5dee2013-04-08 09:38:12 +0200106 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200107static u8 batadv_ring_buffer_avg(const u8 lq_recv[])
Antonio Quartulli24a5dee2013-04-08 09:38:12 +0200108{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200109 const u8 *ptr;
110 u16 count = 0;
111 u16 i = 0;
112 u16 sum = 0;
Antonio Quartulli24a5dee2013-04-08 09:38:12 +0200113
114 ptr = lq_recv;
115
116 while (i < BATADV_TQ_GLOBAL_WINDOW_SIZE) {
117 if (*ptr != 0) {
118 count++;
119 sum += *ptr;
120 }
121
122 i++;
123 ptr++;
124 }
125
126 if (count == 0)
127 return 0;
128
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200129 return (u8)(sum / count);
Antonio Quartulli24a5dee2013-04-08 09:38:12 +0200130}
David S. Millerd98cae64e2013-06-19 16:49:39 -0700131
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200132/**
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200133 * batadv_iv_ogm_orig_free - free the private resources allocated for this
134 * orig_node
135 * @orig_node: the orig_node for which the resources have to be free'd
136 */
137static void batadv_iv_ogm_orig_free(struct batadv_orig_node *orig_node)
138{
139 kfree(orig_node->bat_iv.bcast_own);
140 kfree(orig_node->bat_iv.bcast_own_sum);
141}
142
143/**
144 * batadv_iv_ogm_orig_add_if - change the private structures of the orig_node to
145 * include the new hard-interface
146 * @orig_node: the orig_node that has to be changed
147 * @max_if_num: the current amount of interfaces
148 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200149 * Return: 0 on success, a negative error code otherwise.
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200150 */
151static int batadv_iv_ogm_orig_add_if(struct batadv_orig_node *orig_node,
152 int max_if_num)
153{
154 void *data_ptr;
Antonio Quartulli0185dda2014-05-24 06:43:47 +0200155 size_t old_size;
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200156 int ret = -ENOMEM;
157
158 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
159
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200160 old_size = (max_if_num - 1) * sizeof(unsigned long) * BATADV_NUM_WORDS;
Antonio Quartulli0185dda2014-05-24 06:43:47 +0200161 data_ptr = kmalloc_array(max_if_num,
162 BATADV_NUM_WORDS * sizeof(unsigned long),
163 GFP_ATOMIC);
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200164 if (!data_ptr)
165 goto unlock;
166
167 memcpy(data_ptr, orig_node->bat_iv.bcast_own, old_size);
168 kfree(orig_node->bat_iv.bcast_own);
169 orig_node->bat_iv.bcast_own = data_ptr;
170
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200171 data_ptr = kmalloc_array(max_if_num, sizeof(u8), GFP_ATOMIC);
Sven Eckelmannf7dcdf52016-02-22 22:56:33 +0100172 if (!data_ptr)
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200173 goto unlock;
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200174
175 memcpy(data_ptr, orig_node->bat_iv.bcast_own_sum,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200176 (max_if_num - 1) * sizeof(u8));
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200177 kfree(orig_node->bat_iv.bcast_own_sum);
178 orig_node->bat_iv.bcast_own_sum = data_ptr;
179
180 ret = 0;
181
182unlock:
183 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
184
185 return ret;
186}
187
188/**
Sven Eckelmann64ae74452016-02-22 22:56:34 +0100189 * batadv_iv_ogm_drop_bcast_own_entry - drop section of bcast_own
190 * @orig_node: the orig_node that has to be changed
191 * @max_if_num: the current amount of interfaces
192 * @del_if_num: the index of the interface being removed
193 */
194static void
195batadv_iv_ogm_drop_bcast_own_entry(struct batadv_orig_node *orig_node,
196 int max_if_num, int del_if_num)
197{
198 size_t chunk_size;
199 size_t if_offset;
200 void *data_ptr;
201
202 lockdep_assert_held(&orig_node->bat_iv.ogm_cnt_lock);
203
204 chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS;
205 data_ptr = kmalloc_array(max_if_num, chunk_size, GFP_ATOMIC);
206 if (!data_ptr)
207 /* use old buffer when new one could not be allocated */
208 data_ptr = orig_node->bat_iv.bcast_own;
209
210 /* copy first part */
211 memmove(data_ptr, orig_node->bat_iv.bcast_own, del_if_num * chunk_size);
212
213 /* copy second part */
214 if_offset = (del_if_num + 1) * chunk_size;
215 memmove((char *)data_ptr + del_if_num * chunk_size,
216 (uint8_t *)orig_node->bat_iv.bcast_own + if_offset,
217 (max_if_num - del_if_num) * chunk_size);
218
219 /* bcast_own was shrunk down in new buffer; free old one */
220 if (orig_node->bat_iv.bcast_own != data_ptr) {
221 kfree(orig_node->bat_iv.bcast_own);
222 orig_node->bat_iv.bcast_own = data_ptr;
223 }
224}
225
226/**
227 * batadv_iv_ogm_drop_bcast_own_sum_entry - drop section of bcast_own_sum
228 * @orig_node: the orig_node that has to be changed
229 * @max_if_num: the current amount of interfaces
230 * @del_if_num: the index of the interface being removed
231 */
232static void
233batadv_iv_ogm_drop_bcast_own_sum_entry(struct batadv_orig_node *orig_node,
234 int max_if_num, int del_if_num)
235{
236 size_t if_offset;
237 void *data_ptr;
238
239 lockdep_assert_held(&orig_node->bat_iv.ogm_cnt_lock);
240
241 data_ptr = kmalloc_array(max_if_num, sizeof(u8), GFP_ATOMIC);
242 if (!data_ptr)
243 /* use old buffer when new one could not be allocated */
244 data_ptr = orig_node->bat_iv.bcast_own_sum;
245
246 memmove(data_ptr, orig_node->bat_iv.bcast_own_sum,
247 del_if_num * sizeof(u8));
248
249 if_offset = (del_if_num + 1) * sizeof(u8);
250 memmove((char *)data_ptr + del_if_num * sizeof(u8),
251 orig_node->bat_iv.bcast_own_sum + if_offset,
252 (max_if_num - del_if_num) * sizeof(u8));
253
254 /* bcast_own_sum was shrunk down in new buffer; free old one */
255 if (orig_node->bat_iv.bcast_own_sum != data_ptr) {
256 kfree(orig_node->bat_iv.bcast_own_sum);
257 orig_node->bat_iv.bcast_own_sum = data_ptr;
258 }
259}
260
261/**
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200262 * batadv_iv_ogm_orig_del_if - change the private structures of the orig_node to
263 * exclude the removed interface
264 * @orig_node: the orig_node that has to be changed
265 * @max_if_num: the current amount of interfaces
266 * @del_if_num: the index of the interface being removed
267 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200268 * Return: 0 on success, a negative error code otherwise.
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200269 */
270static int batadv_iv_ogm_orig_del_if(struct batadv_orig_node *orig_node,
271 int max_if_num, int del_if_num)
272{
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200273 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
274
Sven Eckelmann64ae74452016-02-22 22:56:34 +0100275 if (max_if_num == 0) {
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200276 kfree(orig_node->bat_iv.bcast_own);
Sven Eckelmann64ae74452016-02-22 22:56:34 +0100277 kfree(orig_node->bat_iv.bcast_own_sum);
278 orig_node->bat_iv.bcast_own = NULL;
279 orig_node->bat_iv.bcast_own_sum = NULL;
280 } else {
281 batadv_iv_ogm_drop_bcast_own_entry(orig_node, max_if_num,
282 del_if_num);
283 batadv_iv_ogm_drop_bcast_own_sum_entry(orig_node, max_if_num,
284 del_if_num);
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200285 }
286
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200287 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
288
Sven Eckelmann64ae74452016-02-22 22:56:34 +0100289 return 0;
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200290}
291
292/**
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200293 * batadv_iv_ogm_orig_get - retrieve or create (if does not exist) an originator
294 * @bat_priv: the bat priv with all the soft interface information
295 * @addr: mac address of the originator
296 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200297 * Return: the originator object corresponding to the passed mac address or NULL
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200298 * on failure.
299 * If the object does not exists it is created an initialised.
300 */
301static struct batadv_orig_node *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200302batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const u8 *addr)
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200303{
304 struct batadv_orig_node *orig_node;
305 int size, hash_added;
306
307 orig_node = batadv_orig_hash_find(bat_priv, addr);
308 if (orig_node)
309 return orig_node;
310
311 orig_node = batadv_orig_node_new(bat_priv, addr);
312 if (!orig_node)
313 return NULL;
314
315 spin_lock_init(&orig_node->bat_iv.ogm_cnt_lock);
316
317 size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS;
318 orig_node->bat_iv.bcast_own = kzalloc(size, GFP_ATOMIC);
319 if (!orig_node->bat_iv.bcast_own)
320 goto free_orig_node;
321
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200322 size = bat_priv->num_ifaces * sizeof(u8);
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200323 orig_node->bat_iv.bcast_own_sum = kzalloc(size, GFP_ATOMIC);
324 if (!orig_node->bat_iv.bcast_own_sum)
Antonio Quartullia5a5cb82014-02-15 02:17:20 +0100325 goto free_orig_node;
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200326
Sven Eckelmann55db2d52016-07-15 17:39:21 +0200327 kref_get(&orig_node->refcount);
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200328 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
329 batadv_choose_orig, orig_node,
330 &orig_node->hash_entry);
331 if (hash_added != 0)
Sven Eckelmann55db2d52016-07-15 17:39:21 +0200332 goto free_orig_node_hash;
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200333
334 return orig_node;
335
Sven Eckelmann55db2d52016-07-15 17:39:21 +0200336free_orig_node_hash:
Sven Eckelmann5d967312016-01-17 11:01:09 +0100337 batadv_orig_node_put(orig_node);
Sven Eckelmann55db2d52016-07-15 17:39:21 +0200338free_orig_node:
Sven Eckelmann5d967312016-01-17 11:01:09 +0100339 batadv_orig_node_put(orig_node);
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200340
341 return NULL;
342}
343
Sven Eckelmann56303d32012-06-05 22:31:31 +0200344static struct batadv_neigh_node *
345batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200346 const u8 *neigh_addr,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200347 struct batadv_orig_node *orig_node,
Antonio Quartulli863dd7a2013-03-25 13:49:46 +0100348 struct batadv_orig_node *orig_neigh)
Marek Lindner7ae8b282012-03-01 15:35:21 +0800349{
Marek Lindner741aa062015-07-26 04:57:43 +0800350 struct batadv_neigh_node *neigh_node;
Marek Lindner7ae8b282012-03-01 15:35:21 +0800351
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800352 neigh_node = batadv_neigh_node_get_or_create(orig_node,
353 hard_iface, neigh_addr);
Marek Lindner7ae8b282012-03-01 15:35:21 +0800354 if (!neigh_node)
355 goto out;
356
Simon Wunderlich89652332013-11-13 19:14:46 +0100357 neigh_node->orig_node = orig_neigh;
Marek Lindner7ae8b282012-03-01 15:35:21 +0800358
Marek Lindner7ae8b282012-03-01 15:35:21 +0800359out:
360 return neigh_node;
361}
362
Sven Eckelmann56303d32012-06-05 22:31:31 +0200363static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200364{
Sven Eckelmann96412692012-06-05 22:31:30 +0200365 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindner14511512012-08-02 17:20:26 +0200366 unsigned char *ogm_buff;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200367 u32 random_seqno;
Marek Lindnerd7d32ec2012-02-07 17:20:46 +0800368
369 /* randomize initial seqno to avoid collision */
370 get_random_bytes(&random_seqno, sizeof(random_seqno));
Marek Lindner14511512012-08-02 17:20:26 +0200371 atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno);
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200372
Marek Lindner14511512012-08-02 17:20:26 +0200373 hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN;
374 ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC);
375 if (!ogm_buff)
Markus Pargmann42d9f2c2014-12-26 12:41:26 +0100376 return -ENOMEM;
Marek Lindner77af7572012-02-07 17:20:48 +0800377
Marek Lindner14511512012-08-02 17:20:26 +0200378 hard_iface->bat_iv.ogm_buff = ogm_buff;
379
380 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100381 batadv_ogm_packet->packet_type = BATADV_IV_OGM;
382 batadv_ogm_packet->version = BATADV_COMPAT_VERSION;
383 batadv_ogm_packet->ttl = 2;
Sven Eckelmann96412692012-06-05 22:31:30 +0200384 batadv_ogm_packet->flags = BATADV_NO_FLAGS;
Marek Lindner414254e2013-04-23 21:39:58 +0800385 batadv_ogm_packet->reserved = 0;
Sven Eckelmann96412692012-06-05 22:31:30 +0200386 batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE;
Marek Lindner77af7572012-02-07 17:20:48 +0800387
Markus Pargmann42d9f2c2014-12-26 12:41:26 +0100388 return 0;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200389}
390
Sven Eckelmann56303d32012-06-05 22:31:31 +0200391static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
Marek Lindner00a50072012-02-07 17:20:47 +0800392{
Marek Lindner14511512012-08-02 17:20:26 +0200393 kfree(hard_iface->bat_iv.ogm_buff);
394 hard_iface->bat_iv.ogm_buff = NULL;
Marek Lindner00a50072012-02-07 17:20:47 +0800395}
396
Sven Eckelmann56303d32012-06-05 22:31:31 +0200397static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface)
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200398{
Sven Eckelmann96412692012-06-05 22:31:30 +0200399 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindner14511512012-08-02 17:20:26 +0200400 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200401
Marek Lindner14511512012-08-02 17:20:26 +0200402 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100403 ether_addr_copy(batadv_ogm_packet->orig,
404 hard_iface->net_dev->dev_addr);
405 ether_addr_copy(batadv_ogm_packet->prev_sender,
406 hard_iface->net_dev->dev_addr);
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200407}
408
Sven Eckelmann56303d32012-06-05 22:31:31 +0200409static void
410batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface)
Marek Lindnerc3229392012-03-11 06:17:50 +0800411{
Sven Eckelmann96412692012-06-05 22:31:30 +0200412 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindner14511512012-08-02 17:20:26 +0200413 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
Marek Lindnerc3229392012-03-11 06:17:50 +0800414
Marek Lindner14511512012-08-02 17:20:26 +0200415 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100416 batadv_ogm_packet->ttl = BATADV_TTL;
Marek Lindnerc3229392012-03-11 06:17:50 +0800417}
418
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200419/* when do we schedule our own ogm to be sent */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200420static unsigned long
Sven Eckelmann56303d32012-06-05 22:31:31 +0200421batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200422{
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200423 unsigned int msecs;
424
425 msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
Akinobu Mitae76e4322012-12-24 11:14:07 +0900426 msecs += prandom_u32() % (2 * BATADV_JITTER);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200427
428 return jiffies + msecs_to_jiffies(msecs);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200429}
430
431/* when do we schedule a ogm packet to be sent */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200432static unsigned long batadv_iv_ogm_fwd_send_time(void)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200433{
Akinobu Mitae76e4322012-12-24 11:14:07 +0900434 return jiffies + msecs_to_jiffies(prandom_u32() % (BATADV_JITTER / 2));
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200435}
436
437/* apply hop penalty for a normal link */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200438static u8 batadv_hop_penalty(u8 tq, const struct batadv_priv *bat_priv)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200439{
440 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200441 int new_tq;
442
443 new_tq = tq * (BATADV_TQ_MAX_VALUE - hop_penalty);
444 new_tq /= BATADV_TQ_MAX_VALUE;
445
446 return new_tq;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200447}
448
Simon Wunderlich600184b2015-11-23 19:57:21 +0100449/**
450 * batadv_iv_ogm_aggr_packet - checks if there is another OGM attached
451 * @buff_pos: current position in the skb
452 * @packet_len: total length of the skb
453 * @tvlv_len: tvlv length of the previously considered OGM
454 *
455 * Return: true if there is enough space for another OGM, false otherwise.
456 */
Markus Pargmann9fd9b192014-12-26 12:41:27 +0100457static bool batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len,
458 __be16 tvlv_len)
Marek Lindnerfc957272011-07-30 12:04:12 +0200459{
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200460 int next_buff_pos = 0;
461
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200462 next_buff_pos += buff_pos + BATADV_OGM_HLEN;
Marek Lindneref261572013-04-23 21:39:57 +0800463 next_buff_pos += ntohs(tvlv_len);
Marek Lindnerfc957272011-07-30 12:04:12 +0200464
465 return (next_buff_pos <= packet_len) &&
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200466 (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
Marek Lindnerfc957272011-07-30 12:04:12 +0200467}
468
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200469/* send a batman ogm to a given interface */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200470static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet,
471 struct batadv_hard_iface *hard_iface)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200472{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200473 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Markus Pargmannde12bae2014-12-26 12:41:28 +0100474 const char *fwd_str;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200475 u8 packet_num;
476 s16 buff_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +0200477 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200478 struct sk_buff *skb;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200479 u8 *packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200480
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200481 if (hard_iface->if_status != BATADV_IF_ACTIVE)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200482 return;
483
484 packet_num = 0;
485 buff_pos = 0;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200486 packet_pos = forw_packet->skb->data;
487 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200488
489 /* adjust all flags and log packets */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200490 while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
Marek Lindneref261572013-04-23 21:39:57 +0800491 batadv_ogm_packet->tvlv_len)) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200492 /* we might have aggregated direct link packets with an
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200493 * ordinary base packet
494 */
Sven Eckelmann8de47de2012-07-08 16:32:09 +0200495 if (forw_packet->direct_link_flags & BIT(packet_num) &&
496 forw_packet->if_incoming == hard_iface)
Sven Eckelmann96412692012-06-05 22:31:30 +0200497 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200498 else
Sven Eckelmann96412692012-06-05 22:31:30 +0200499 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200500
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200501 if (packet_num > 0 || !forw_packet->own)
502 fwd_str = "Forwarding";
503 else
504 fwd_str = "Sending own";
505
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200506 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800507 "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200508 fwd_str, (packet_num > 0 ? "aggregated " : ""),
Sven Eckelmann96412692012-06-05 22:31:30 +0200509 batadv_ogm_packet->orig,
510 ntohl(batadv_ogm_packet->seqno),
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100511 batadv_ogm_packet->tq, batadv_ogm_packet->ttl,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +0200512 ((batadv_ogm_packet->flags & BATADV_DIRECTLINK) ?
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200513 "on" : "off"),
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800514 hard_iface->net_dev->name,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200515 hard_iface->net_dev->dev_addr);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200516
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200517 buff_pos += BATADV_OGM_HLEN;
Marek Lindneref261572013-04-23 21:39:57 +0800518 buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200519 packet_num++;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200520 packet_pos = forw_packet->skb->data + buff_pos;
521 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200522 }
523
524 /* create clone because function is called more than once */
525 skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
Martin Hundebøllf8214862012-04-20 17:02:45 +0200526 if (skb) {
Sven Eckelmannd69909d2012-06-03 22:19:20 +0200527 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
528 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
Martin Hundebøllf8214862012-04-20 17:02:45 +0200529 skb->len + ETH_HLEN);
Antonio Quartulli95d39272016-01-16 16:40:15 +0800530 batadv_send_broadcast_skb(skb, hard_iface);
Martin Hundebøllf8214862012-04-20 17:02:45 +0200531 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200532}
533
534/* send a batman ogm packet */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200535static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200536{
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200537 struct net_device *soft_iface;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200538
539 if (!forw_packet->if_incoming) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100540 pr_err("Error - can't forward packet: incoming iface not specified\n");
Linus LĂĽssingf55a2e82016-06-14 22:56:50 +0200541 return;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200542 }
543
544 soft_iface = forw_packet->if_incoming->soft_iface;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200545
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100546 if (WARN_ON(!forw_packet->if_outgoing))
Linus LĂĽssingf55a2e82016-06-14 22:56:50 +0200547 return;
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100548
549 if (WARN_ON(forw_packet->if_outgoing->soft_iface != soft_iface))
Linus LĂĽssingf55a2e82016-06-14 22:56:50 +0200550 return;
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100551
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200552 if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE)
Linus LĂĽssingf55a2e82016-06-14 22:56:50 +0200553 return;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200554
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100555 /* only for one specific outgoing interface */
556 batadv_iv_ogm_send_to_if(forw_packet, forw_packet->if_outgoing);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200557}
558
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100559/**
560 * batadv_iv_ogm_can_aggregate - find out if an OGM can be aggregated on an
561 * existing forward packet
562 * @new_bat_ogm_packet: OGM packet to be aggregated
563 * @bat_priv: the bat priv with all the soft interface information
564 * @packet_len: (total) length of the OGM
565 * @send_time: timestamp (jiffies) when the packet is to be sent
Martin Hundebøll95298a92014-07-15 09:41:05 +0200566 * @directlink: true if this is a direct link packet
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100567 * @if_incoming: interface where the packet was received
568 * @if_outgoing: interface for which the retransmission should be considered
569 * @forw_packet: the forwarded packet which should be checked
570 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200571 * Return: true if new_packet can be aggregated with forw_packet
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100572 */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200573static bool
Sven Eckelmann96412692012-06-05 22:31:30 +0200574batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200575 struct batadv_priv *bat_priv,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200576 int packet_len, unsigned long send_time,
577 bool directlink,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200578 const struct batadv_hard_iface *if_incoming,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100579 const struct batadv_hard_iface *if_outgoing,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200580 const struct batadv_forw_packet *forw_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200581{
Sven Eckelmann96412692012-06-05 22:31:30 +0200582 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200583 int aggregated_bytes = forw_packet->packet_len + packet_len;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200584 struct batadv_hard_iface *primary_if = NULL;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200585 bool res = false;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200586 unsigned long aggregation_end_time;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200587
Sven Eckelmann96412692012-06-05 22:31:30 +0200588 batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200589 aggregation_end_time = send_time;
590 aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200591
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200592 /* we can aggregate the current packet to this aggregated packet
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200593 * if:
594 *
595 * - the send time is within our MAX_AGGREGATION_MS time
596 * - the resulting packet wont be bigger than
597 * MAX_AGGREGATION_BYTES
Markus Pargmann8f34b382014-12-26 12:41:29 +0100598 * otherwise aggregation is not possible
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200599 */
Markus Pargmann8f34b382014-12-26 12:41:29 +0100600 if (!time_before(send_time, forw_packet->send_time) ||
601 !time_after_eq(aggregation_end_time, forw_packet->send_time))
602 return false;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200603
Markus Pargmann8f34b382014-12-26 12:41:29 +0100604 if (aggregated_bytes > BATADV_MAX_AGGREGATION_BYTES)
605 return false;
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100606
Markus Pargmann8f34b382014-12-26 12:41:29 +0100607 /* packet is not leaving on the same interface. */
608 if (forw_packet->if_outgoing != if_outgoing)
609 return false;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200610
Markus Pargmann8f34b382014-12-26 12:41:29 +0100611 /* check aggregation compatibility
612 * -> direct link packets are broadcasted on
613 * their interface only
614 * -> aggregate packet if the current packet is
615 * a "global" packet as well as the base
616 * packet
617 */
618 primary_if = batadv_primary_if_get_selected(bat_priv);
619 if (!primary_if)
620 return false;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200621
Markus Pargmann8f34b382014-12-26 12:41:29 +0100622 /* packets without direct link flag and high TTL
623 * are flooded through the net
624 */
625 if (!directlink &&
626 !(batadv_ogm_packet->flags & BATADV_DIRECTLINK) &&
627 batadv_ogm_packet->ttl != 1 &&
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200628
Markus Pargmann8f34b382014-12-26 12:41:29 +0100629 /* own packets originating non-primary
630 * interfaces leave only that interface
631 */
632 (!forw_packet->own ||
633 forw_packet->if_incoming == primary_if)) {
634 res = true;
635 goto out;
636 }
637
638 /* if the incoming packet is sent via this one
639 * interface only - we still can aggregate
640 */
641 if (directlink &&
642 new_bat_ogm_packet->ttl == 1 &&
643 forw_packet->if_incoming == if_incoming &&
644
645 /* packets from direct neighbors or
646 * own secondary interface packets
647 * (= secondary interface packets in general)
648 */
649 (batadv_ogm_packet->flags & BATADV_DIRECTLINK ||
650 (forw_packet->own &&
651 forw_packet->if_incoming != primary_if))) {
652 res = true;
653 goto out;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200654 }
655
656out:
657 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100658 batadv_hardif_put(primary_if);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200659 return res;
660}
661
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100662/**
663 * batadv_iv_ogm_aggregate_new - create a new aggregated packet and add this
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100664 * packet to it.
665 * @packet_buff: pointer to the OGM
666 * @packet_len: (total) length of the OGM
667 * @send_time: timestamp (jiffies) when the packet is to be sent
668 * @direct_link: whether this OGM has direct link status
669 * @if_incoming: interface where the packet was received
670 * @if_outgoing: interface for which the retransmission should be considered
671 * @own_packet: true if it is a self-generated ogm
672 */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200673static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,
674 int packet_len, unsigned long send_time,
675 bool direct_link,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200676 struct batadv_hard_iface *if_incoming,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100677 struct batadv_hard_iface *if_outgoing,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200678 int own_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200679{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200680 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
681 struct batadv_forw_packet *forw_packet_aggr;
Linus LĂĽssing99ba18e2017-02-17 11:17:06 +0100682 struct sk_buff *skb;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200683 unsigned char *skb_buff;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200684 unsigned int skb_size;
Linus LĂĽssinga65e5482016-06-20 21:39:54 +0200685 atomic_t *queue_left = own_packet ? NULL : &bat_priv->batman_queue_left;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200686
Markus Pargmann940d1562014-12-26 12:41:31 +0100687 if (atomic_read(&bat_priv->aggregated_ogms) &&
688 packet_len < BATADV_MAX_AGGREGATION_BYTES)
Sven Eckelmann5b246572012-11-04 17:11:45 +0100689 skb_size = BATADV_MAX_AGGREGATION_BYTES;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200690 else
Sven Eckelmann5b246572012-11-04 17:11:45 +0100691 skb_size = packet_len;
692
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200693 skb_size += ETH_HLEN;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200694
Linus LĂĽssing99ba18e2017-02-17 11:17:06 +0100695 skb = netdev_alloc_skb_ip_align(NULL, skb_size);
696 if (!skb)
697 return;
698
699 forw_packet_aggr = batadv_forw_packet_alloc(if_incoming, if_outgoing,
700 queue_left, bat_priv, skb);
701 if (!forw_packet_aggr) {
702 kfree_skb(skb);
Linus LĂĽssinga65e5482016-06-20 21:39:54 +0200703 return;
704 }
705
Simon Wunderlichc54f38c92013-07-29 17:56:44 +0200706 forw_packet_aggr->skb->priority = TC_PRIO_CONTROL;
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200707 skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200708
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200709 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
710 forw_packet_aggr->packet_len = packet_len;
711 memcpy(skb_buff, packet_buff, packet_len);
712
713 forw_packet_aggr->own = own_packet;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200714 forw_packet_aggr->direct_link_flags = BATADV_NO_FLAGS;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200715 forw_packet_aggr->send_time = send_time;
716
717 /* save packet direct link flag status */
718 if (direct_link)
719 forw_packet_aggr->direct_link_flags |= 1;
720
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200721 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
Antonio Quartullif0d97252016-05-03 01:45:34 +0800722 batadv_iv_send_outstanding_bat_ogm_packet);
Linus LĂĽssing9b4aec62016-11-01 09:44:44 +0100723
724 batadv_forw_packet_ogmv1_queue(bat_priv, forw_packet_aggr, send_time);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200725}
726
727/* aggregate a new packet into the existing ogm packet */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200728static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200729 const unsigned char *packet_buff,
730 int packet_len, bool direct_link)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200731{
732 unsigned char *skb_buff;
Sven Eckelmann8de47de2012-07-08 16:32:09 +0200733 unsigned long new_direct_link_flag;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200734
735 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
736 memcpy(skb_buff, packet_buff, packet_len);
737 forw_packet_aggr->packet_len += packet_len;
738 forw_packet_aggr->num_packets++;
739
740 /* save packet direct link flag status */
Sven Eckelmann8de47de2012-07-08 16:32:09 +0200741 if (direct_link) {
742 new_direct_link_flag = BIT(forw_packet_aggr->num_packets);
743 forw_packet_aggr->direct_link_flags |= new_direct_link_flag;
744 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200745}
746
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100747/**
748 * batadv_iv_ogm_queue_add - queue up an OGM for transmission
749 * @bat_priv: the bat priv with all the soft interface information
750 * @packet_buff: pointer to the OGM
751 * @packet_len: (total) length of the OGM
752 * @if_incoming: interface where the packet was received
753 * @if_outgoing: interface for which the retransmission should be considered
754 * @own_packet: true if it is a self-generated ogm
755 * @send_time: timestamp (jiffies) when the packet is to be sent
756 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200757static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200758 unsigned char *packet_buff,
759 int packet_len,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200760 struct batadv_hard_iface *if_incoming,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100761 struct batadv_hard_iface *if_outgoing,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200762 int own_packet, unsigned long send_time)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200763{
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200764 /* _aggr -> pointer to the packet we want to aggregate with
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200765 * _pos -> pointer to the position in the queue
766 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200767 struct batadv_forw_packet *forw_packet_aggr = NULL;
768 struct batadv_forw_packet *forw_packet_pos = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200769 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200770 bool direct_link;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200771 unsigned long max_aggregation_jiffies;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200772
Sven Eckelmann96412692012-06-05 22:31:30 +0200773 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
Markus Pargmann56489152014-12-26 12:41:32 +0100774 direct_link = !!(batadv_ogm_packet->flags & BATADV_DIRECTLINK);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200775 max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200776
777 /* find position for the packet in the forward queue */
778 spin_lock_bh(&bat_priv->forw_bat_list_lock);
779 /* own packets are not to be aggregated */
Markus Pargmann56489152014-12-26 12:41:32 +0100780 if (atomic_read(&bat_priv->aggregated_ogms) && !own_packet) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800781 hlist_for_each_entry(forw_packet_pos,
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200782 &bat_priv->forw_bat_list, list) {
Sven Eckelmann96412692012-06-05 22:31:30 +0200783 if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200784 bat_priv, packet_len,
785 send_time, direct_link,
786 if_incoming,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100787 if_outgoing,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200788 forw_packet_pos)) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200789 forw_packet_aggr = forw_packet_pos;
790 break;
791 }
792 }
793 }
794
795 /* nothing to aggregate with - either aggregation disabled or no
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200796 * suitable aggregation packet found
797 */
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200798 if (!forw_packet_aggr) {
799 /* the following section can run without the lock */
800 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
801
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200802 /* if we could not aggregate this packet with one of the others
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200803 * we hold it back for a while, so that it might be aggregated
804 * later on
805 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200806 if (!own_packet && atomic_read(&bat_priv->aggregated_ogms))
807 send_time += max_aggregation_jiffies;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200808
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200809 batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
810 send_time, direct_link,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100811 if_incoming, if_outgoing,
812 own_packet);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200813 } else {
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200814 batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
815 packet_len, direct_link);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200816 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
817 }
818}
819
Sven Eckelmann56303d32012-06-05 22:31:31 +0200820static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200821 const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +0200822 struct batadv_ogm_packet *batadv_ogm_packet,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200823 bool is_single_hop_neigh,
824 bool is_from_best_next_hop,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100825 struct batadv_hard_iface *if_incoming,
826 struct batadv_hard_iface *if_outgoing)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200827{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200828 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200829 u16 tvlv_len;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200830
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100831 if (batadv_ogm_packet->ttl <= 1) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200832 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200833 return;
834 }
835
Marek Lindner13b25412012-03-11 06:17:53 +0800836 if (!is_from_best_next_hop) {
837 /* Mark the forwarded packet when it is not coming from our
838 * best next hop. We still need to forward the packet for our
839 * neighbor link quality detection to work in case the packet
840 * originated from a single hop neighbor. Otherwise we can
841 * simply drop the ogm.
842 */
843 if (is_single_hop_neigh)
Sven Eckelmann96412692012-06-05 22:31:30 +0200844 batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP;
Marek Lindner13b25412012-03-11 06:17:53 +0800845 else
846 return;
847 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200848
Marek Lindneref261572013-04-23 21:39:57 +0800849 tvlv_len = ntohs(batadv_ogm_packet->tvlv_len);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200850
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100851 batadv_ogm_packet->ttl--;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100852 ether_addr_copy(batadv_ogm_packet->prev_sender, ethhdr->h_source);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200853
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200854 /* apply hop penalty */
Sven Eckelmann96412692012-06-05 22:31:30 +0200855 batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200856 bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200857
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200858 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200859 "Forwarding packet: tq: %i, ttl: %i\n",
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100860 batadv_ogm_packet->tq, batadv_ogm_packet->ttl);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200861
Marek Lindner75cd33f2012-03-01 15:35:16 +0800862 if (is_single_hop_neigh)
Sven Eckelmann96412692012-06-05 22:31:30 +0200863 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200864 else
Sven Eckelmann96412692012-06-05 22:31:30 +0200865 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200866
Sven Eckelmann96412692012-06-05 22:31:30 +0200867 batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
Marek Lindneref261572013-04-23 21:39:57 +0800868 BATADV_OGM_HLEN + tvlv_len,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100869 if_incoming, if_outgoing, 0,
870 batadv_iv_ogm_fwd_send_time());
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200871}
872
Antonio Quartullid9896612013-04-17 17:44:43 +0200873/**
874 * batadv_iv_ogm_slide_own_bcast_window - bitshift own OGM broadcast windows for
875 * the given interface
876 * @hard_iface: the interface for which the windows have to be shifted
877 */
878static void
879batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
880{
881 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
882 struct batadv_hashtable *hash = bat_priv->orig_hash;
883 struct hlist_head *head;
884 struct batadv_orig_node *orig_node;
885 unsigned long *word;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200886 u32 i;
Antonio Quartullid9896612013-04-17 17:44:43 +0200887 size_t word_index;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200888 u8 *w;
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200889 int if_num;
Antonio Quartullid9896612013-04-17 17:44:43 +0200890
891 for (i = 0; i < hash->size; i++) {
892 head = &hash->table[i];
893
894 rcu_read_lock();
895 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200896 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Antonio Quartullid9896612013-04-17 17:44:43 +0200897 word_index = hard_iface->if_num * BATADV_NUM_WORDS;
Antonio Quartulli32db6aa2014-09-01 14:37:29 +0200898 word = &orig_node->bat_iv.bcast_own[word_index];
Antonio Quartullid9896612013-04-17 17:44:43 +0200899
900 batadv_bit_get_packet(bat_priv, word, 1, 0);
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200901 if_num = hard_iface->if_num;
902 w = &orig_node->bat_iv.bcast_own_sum[if_num];
Antonio Quartullid9896612013-04-17 17:44:43 +0200903 *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200904 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Antonio Quartullid9896612013-04-17 17:44:43 +0200905 }
906 rcu_read_unlock();
907 }
908}
909
Sven Eckelmann56303d32012-06-05 22:31:31 +0200910static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200911{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200912 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindner14511512012-08-02 17:20:26 +0200913 unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff;
Sven Eckelmann96412692012-06-05 22:31:30 +0200914 struct batadv_ogm_packet *batadv_ogm_packet;
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100915 struct batadv_hard_iface *primary_if, *tmp_hard_iface;
Marek Lindner14511512012-08-02 17:20:26 +0200916 int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200917 u32 seqno;
918 u16 tvlv_len = 0;
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100919 unsigned long send_time;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200920
Antonio Quartullif0d97252016-05-03 01:45:34 +0800921 if ((hard_iface->if_status == BATADV_IF_NOT_IN_USE) ||
922 (hard_iface->if_status == BATADV_IF_TO_BE_REMOVED))
923 return;
924
925 /* the interface gets activated here to avoid race conditions between
926 * the moment of activating the interface in
927 * hardif_activate_interface() where the originator mac is set and
928 * outdated packets (especially uninitialized mac addresses) in the
929 * packet queue
930 */
931 if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED)
932 hard_iface->if_status = BATADV_IF_ACTIVE;
933
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200934 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200935
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800936 if (hard_iface == primary_if) {
937 /* tt changes have to be committed before the tvlv data is
938 * appended as it may alter the tt tvlv container
939 */
940 batadv_tt_local_commit_changes(bat_priv);
Marek Lindneref261572013-04-23 21:39:57 +0800941 tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff,
942 ogm_buff_len,
943 BATADV_OGM_HLEN);
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800944 }
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800945
Marek Lindner14511512012-08-02 17:20:26 +0200946 batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff);
Marek Lindneref261572013-04-23 21:39:57 +0800947 batadv_ogm_packet->tvlv_len = htons(tvlv_len);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200948
949 /* change sequence number to network order */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200950 seqno = (u32)atomic_read(&hard_iface->bat_iv.ogm_seqno);
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200951 batadv_ogm_packet->seqno = htonl(seqno);
Marek Lindner14511512012-08-02 17:20:26 +0200952 atomic_inc(&hard_iface->bat_iv.ogm_seqno);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200953
Antonio Quartullid9896612013-04-17 17:44:43 +0200954 batadv_iv_ogm_slide_own_bcast_window(hard_iface);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200955
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100956 send_time = batadv_iv_ogm_emit_send_time(bat_priv);
957
958 if (hard_iface != primary_if) {
959 /* OGMs from secondary interfaces are only scheduled on their
960 * respective interfaces.
961 */
962 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff, *ogm_buff_len,
963 hard_iface, hard_iface, 1, send_time);
964 goto out;
965 }
966
967 /* OGMs from primary interfaces are scheduled on all
968 * interfaces.
969 */
970 rcu_read_lock();
971 list_for_each_entry_rcu(tmp_hard_iface, &batadv_hardif_list, list) {
972 if (tmp_hard_iface->soft_iface != hard_iface->soft_iface)
Sven Eckelmann87b40f52015-07-17 10:03:42 +0200973 continue;
Sven Eckelmann27353442016-03-05 16:09:16 +0100974
975 if (!kref_get_unless_zero(&tmp_hard_iface->refcount))
976 continue;
977
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100978 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff,
979 *ogm_buff_len, hard_iface,
980 tmp_hard_iface, 1, send_time);
Sven Eckelmann27353442016-03-05 16:09:16 +0100981
982 batadv_hardif_put(tmp_hard_iface);
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100983 }
984 rcu_read_unlock();
985
986out:
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200987 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100988 batadv_hardif_put(primary_if);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200989}
990
Simon Wunderlich89652332013-11-13 19:14:46 +0100991/**
992 * batadv_iv_ogm_orig_update - use OGM to update corresponding data in an
993 * originator
994 * @bat_priv: the bat priv with all the soft interface information
995 * @orig_node: the orig node who originally emitted the ogm packet
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100996 * @orig_ifinfo: ifinfo for the outgoing interface of the orig_node
Simon Wunderlich89652332013-11-13 19:14:46 +0100997 * @ethhdr: Ethernet header of the OGM
998 * @batadv_ogm_packet: the ogm packet
999 * @if_incoming: interface where the packet was received
1000 * @if_outgoing: interface for which the retransmission should be considered
Simon Wunderlich89652332013-11-13 19:14:46 +01001001 * @dup_status: the duplicate status of this ogm packet.
1002 */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001003static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001004batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
1005 struct batadv_orig_node *orig_node,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001006 struct batadv_orig_ifinfo *orig_ifinfo,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001007 const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +02001008 const struct batadv_ogm_packet *batadv_ogm_packet,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001009 struct batadv_hard_iface *if_incoming,
Simon Wunderlich89652332013-11-13 19:14:46 +01001010 struct batadv_hard_iface *if_outgoing,
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001011 enum batadv_dup_status dup_status)
Marek Lindnerfc957272011-07-30 12:04:12 +02001012{
Simon Wunderlich89652332013-11-13 19:14:46 +01001013 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
1014 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
Sven Eckelmann4f248cf2015-06-09 20:50:49 +02001015 struct batadv_neigh_node *neigh_node = NULL;
1016 struct batadv_neigh_node *tmp_neigh_node = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001017 struct batadv_neigh_node *router = NULL;
1018 struct batadv_orig_node *orig_node_tmp;
Linus LĂĽssing7caf69f2012-09-18 03:01:08 +02001019 int if_num;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001020 u8 sum_orig, sum_neigh;
1021 u8 *neigh_addr;
1022 u8 tq_avg;
Marek Lindnerfc957272011-07-30 12:04:12 +02001023
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001024 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann22f05022017-05-19 13:02:00 +02001025 "%s(): Searching and updating originator entry of received packet\n",
1026 __func__);
Marek Lindnerfc957272011-07-30 12:04:12 +02001027
1028 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001029 hlist_for_each_entry_rcu(tmp_neigh_node,
Marek Lindnerfc957272011-07-30 12:04:12 +02001030 &orig_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001031 neigh_addr = tmp_neigh_node->addr;
1032 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
1033 tmp_neigh_node->if_incoming == if_incoming &&
Sven Eckelmann77ae32e2016-01-16 10:29:53 +01001034 kref_get_unless_zero(&tmp_neigh_node->refcount)) {
Antonio Quartulli38dc40e2013-03-30 17:22:00 +01001035 if (WARN(neigh_node, "too many matching neigh_nodes"))
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001036 batadv_neigh_node_put(neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001037 neigh_node = tmp_neigh_node;
1038 continue;
1039 }
1040
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001041 if (dup_status != BATADV_NO_DUP)
Marek Lindnerfc957272011-07-30 12:04:12 +02001042 continue;
1043
Simon Wunderlich89652332013-11-13 19:14:46 +01001044 /* only update the entry for this outgoing interface */
1045 neigh_ifinfo = batadv_neigh_ifinfo_get(tmp_neigh_node,
1046 if_outgoing);
1047 if (!neigh_ifinfo)
1048 continue;
1049
1050 spin_lock_bh(&tmp_neigh_node->ifinfo_lock);
1051 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
1052 &neigh_ifinfo->bat_iv.tq_index, 0);
1053 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
1054 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
1055 spin_unlock_bh(&tmp_neigh_node->ifinfo_lock);
1056
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001057 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01001058 neigh_ifinfo = NULL;
Marek Lindnerfc957272011-07-30 12:04:12 +02001059 }
1060
1061 if (!neigh_node) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001062 struct batadv_orig_node *orig_tmp;
Marek Lindnerfc957272011-07-30 12:04:12 +02001063
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001064 orig_tmp = batadv_iv_ogm_orig_get(bat_priv, ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001065 if (!orig_tmp)
1066 goto unlock;
1067
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001068 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1069 ethhdr->h_source,
Antonio Quartulli863dd7a2013-03-25 13:49:46 +01001070 orig_node, orig_tmp);
Marek Lindnerfc957272011-07-30 12:04:12 +02001071
Sven Eckelmann5d967312016-01-17 11:01:09 +01001072 batadv_orig_node_put(orig_tmp);
Marek Lindnerfc957272011-07-30 12:04:12 +02001073 if (!neigh_node)
1074 goto unlock;
Markus Pargmann23badd62014-12-26 12:41:33 +01001075 } else {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001076 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001077 "Updating existing last-hop neighbor of originator\n");
Markus Pargmann23badd62014-12-26 12:41:33 +01001078 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001079
1080 rcu_read_unlock();
Simon Wunderlich89652332013-11-13 19:14:46 +01001081 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
1082 if (!neigh_ifinfo)
1083 goto out;
Marek Lindnerfc957272011-07-30 12:04:12 +02001084
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001085 neigh_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +02001086
Simon Wunderlich89652332013-11-13 19:14:46 +01001087 spin_lock_bh(&neigh_node->ifinfo_lock);
1088 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
1089 &neigh_ifinfo->bat_iv.tq_index,
Sven Eckelmann96412692012-06-05 22:31:30 +02001090 batadv_ogm_packet->tq);
Simon Wunderlich89652332013-11-13 19:14:46 +01001091 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
1092 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
1093 spin_unlock_bh(&neigh_node->ifinfo_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001094
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001095 if (dup_status == BATADV_NO_DUP) {
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001096 orig_ifinfo->last_ttl = batadv_ogm_packet->ttl;
Simon Wunderlich89652332013-11-13 19:14:46 +01001097 neigh_ifinfo->last_ttl = batadv_ogm_packet->ttl;
Marek Lindnerfc957272011-07-30 12:04:12 +02001098 }
1099
Marek Lindnerfc957272011-07-30 12:04:12 +02001100 /* if this neighbor already is our next hop there is nothing
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001101 * to change
1102 */
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001103 router = batadv_orig_router_get(orig_node, if_outgoing);
Marek Lindnerfc957272011-07-30 12:04:12 +02001104 if (router == neigh_node)
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001105 goto out;
Marek Lindnerfc957272011-07-30 12:04:12 +02001106
Simon Wunderlich89652332013-11-13 19:14:46 +01001107 if (router) {
1108 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1109 if (!router_ifinfo)
1110 goto out;
1111
1112 /* if this neighbor does not offer a better TQ we won't
1113 * consider it
1114 */
1115 if (router_ifinfo->bat_iv.tq_avg > neigh_ifinfo->bat_iv.tq_avg)
1116 goto out;
1117 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001118
1119 /* if the TQ is the same and the link not more symmetric we
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001120 * won't consider it either
1121 */
Simon Wunderlich89652332013-11-13 19:14:46 +01001122 if (router_ifinfo &&
Markus Pargmann01b97a32014-12-26 12:41:30 +01001123 neigh_ifinfo->bat_iv.tq_avg == router_ifinfo->bat_iv.tq_avg) {
Marek Lindnerfc957272011-07-30 12:04:12 +02001124 orig_node_tmp = router->orig_node;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001125 spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
Linus LĂĽssing7caf69f2012-09-18 03:01:08 +02001126 if_num = router->if_incoming->if_num;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001127 sum_orig = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
1128 spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001129
1130 orig_node_tmp = neigh_node->orig_node;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001131 spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
Linus LĂĽssing7caf69f2012-09-18 03:01:08 +02001132 if_num = neigh_node->if_incoming->if_num;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001133 sum_neigh = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
1134 spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001135
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001136 if (sum_orig >= sum_neigh)
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001137 goto out;
Marek Lindnerfc957272011-07-30 12:04:12 +02001138 }
1139
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001140 batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001141 goto out;
1142
1143unlock:
1144 rcu_read_unlock();
1145out:
1146 if (neigh_node)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001147 batadv_neigh_node_put(neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001148 if (router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001149 batadv_neigh_node_put(router);
Simon Wunderlich89652332013-11-13 19:14:46 +01001150 if (neigh_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001151 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01001152 if (router_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001153 batadv_neigh_ifinfo_put(router_ifinfo);
Marek Lindnerfc957272011-07-30 12:04:12 +02001154}
1155
Simon Wunderlich89652332013-11-13 19:14:46 +01001156/**
1157 * batadv_iv_ogm_calc_tq - calculate tq for current received ogm packet
1158 * @orig_node: the orig node who originally emitted the ogm packet
1159 * @orig_neigh_node: the orig node struct of the neighbor who sent the packet
1160 * @batadv_ogm_packet: the ogm packet
1161 * @if_incoming: interface where the packet was received
1162 * @if_outgoing: interface for which the retransmission should be considered
1163 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001164 * Return: true if the link can be considered bidirectional, false otherwise
Simon Wunderlich89652332013-11-13 19:14:46 +01001165 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001166static bool batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
1167 struct batadv_orig_node *orig_neigh_node,
1168 struct batadv_ogm_packet *batadv_ogm_packet,
1169 struct batadv_hard_iface *if_incoming,
1170 struct batadv_hard_iface *if_outgoing)
Marek Lindnerfc957272011-07-30 12:04:12 +02001171{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001172 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1173 struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
Simon Wunderlich89652332013-11-13 19:14:46 +01001174 struct batadv_neigh_ifinfo *neigh_ifinfo;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001175 u8 total_count;
1176 u8 orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001177 unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
Sven Eckelmannd285f522016-02-16 10:47:07 +01001178 int if_num;
1179 unsigned int tq_asym_penalty, inv_asym_penalty;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001180 unsigned int combined_tq;
Sven Eckelmannd285f522016-02-16 10:47:07 +01001181 unsigned int tq_iface_penalty;
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001182 bool ret = false;
Marek Lindnerfc957272011-07-30 12:04:12 +02001183
1184 /* find corresponding one hop neighbor */
1185 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001186 hlist_for_each_entry_rcu(tmp_neigh_node,
Marek Lindnerfc957272011-07-30 12:04:12 +02001187 &orig_neigh_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001188 if (!batadv_compare_eth(tmp_neigh_node->addr,
1189 orig_neigh_node->orig))
Marek Lindnerfc957272011-07-30 12:04:12 +02001190 continue;
1191
1192 if (tmp_neigh_node->if_incoming != if_incoming)
1193 continue;
1194
Sven Eckelmann77ae32e2016-01-16 10:29:53 +01001195 if (!kref_get_unless_zero(&tmp_neigh_node->refcount))
Marek Lindnerfc957272011-07-30 12:04:12 +02001196 continue;
1197
1198 neigh_node = tmp_neigh_node;
1199 break;
1200 }
1201 rcu_read_unlock();
1202
1203 if (!neigh_node)
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001204 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1205 orig_neigh_node->orig,
1206 orig_neigh_node,
Antonio Quartulli863dd7a2013-03-25 13:49:46 +01001207 orig_neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001208
1209 if (!neigh_node)
1210 goto out;
1211
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001212 /* if orig_node is direct neighbor update neigh_node last_seen */
Marek Lindnerfc957272011-07-30 12:04:12 +02001213 if (orig_node == orig_neigh_node)
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001214 neigh_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +02001215
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001216 orig_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +02001217
1218 /* find packet count of corresponding one hop neighbor */
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001219 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1220 if_num = if_incoming->if_num;
1221 orig_eq_count = orig_neigh_node->bat_iv.bcast_own_sum[if_num];
Simon Wunderlich89652332013-11-13 19:14:46 +01001222 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
1223 if (neigh_ifinfo) {
1224 neigh_rq_count = neigh_ifinfo->bat_iv.real_packet_count;
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001225 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01001226 } else {
1227 neigh_rq_count = 0;
1228 }
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001229 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001230
1231 /* pay attention to not get a value bigger than 100 % */
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001232 if (orig_eq_count > neigh_rq_count)
1233 total_count = neigh_rq_count;
1234 else
1235 total_count = orig_eq_count;
Marek Lindnerfc957272011-07-30 12:04:12 +02001236
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001237 /* if we have too few packets (too less data) we set tq_own to zero
1238 * if we receive too few packets it is not considered bidirectional
1239 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001240 if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
1241 neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
Marek Lindnerfc957272011-07-30 12:04:12 +02001242 tq_own = 0;
1243 else
1244 /* neigh_node->real_packet_count is never zero as we
1245 * only purge old information when getting new
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001246 * information
1247 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001248 tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count;
Marek Lindnerfc957272011-07-30 12:04:12 +02001249
Sven Eckelmann21a12362012-03-07 09:07:46 +01001250 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
Marek Lindnerfc957272011-07-30 12:04:12 +02001251 * affect the nearly-symmetric links only a little, but
1252 * punishes asymmetric links more. This will give a value
1253 * between 0 and TQ_MAX_VALUE
1254 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001255 neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
1256 neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
1257 neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
1258 BATADV_TQ_LOCAL_WINDOW_SIZE *
1259 BATADV_TQ_LOCAL_WINDOW_SIZE;
1260 inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
1261 inv_asym_penalty /= neigh_rq_max_cube;
1262 tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;
Marek Lindnerfc957272011-07-30 12:04:12 +02001263
Simon Wunderlichc0398762013-11-13 19:14:48 +01001264 /* penalize if the OGM is forwarded on the same interface. WiFi
1265 * interfaces and other half duplex devices suffer from throughput
1266 * drops as they can't send and receive at the same time.
1267 */
1268 tq_iface_penalty = BATADV_TQ_MAX_VALUE;
1269 if (if_outgoing && (if_incoming == if_outgoing) &&
Sven Eckelmann10b1bbb2016-09-30 15:21:03 +02001270 batadv_is_wifi_hardif(if_outgoing))
Simon Wunderlichc0398762013-11-13 19:14:48 +01001271 tq_iface_penalty = batadv_hop_penalty(BATADV_TQ_MAX_VALUE,
1272 bat_priv);
1273
1274 combined_tq = batadv_ogm_packet->tq *
1275 tq_own *
1276 tq_asym_penalty *
1277 tq_iface_penalty;
1278 combined_tq /= BATADV_TQ_MAX_VALUE *
1279 BATADV_TQ_MAX_VALUE *
1280 BATADV_TQ_MAX_VALUE;
Sven Eckelmann96412692012-06-05 22:31:30 +02001281 batadv_ogm_packet->tq = combined_tq;
Marek Lindnerfc957272011-07-30 12:04:12 +02001282
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001283 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Simon Wunderlichc0398762013-11-13 19:14:48 +01001284 "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 +02001285 orig_node->orig, orig_neigh_node->orig, total_count,
Simon Wunderlichc0398762013-11-13 19:14:48 +01001286 neigh_rq_count, tq_own, tq_asym_penalty, tq_iface_penalty,
1287 batadv_ogm_packet->tq, if_incoming->net_dev->name,
1288 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT");
Marek Lindnerfc957272011-07-30 12:04:12 +02001289
1290 /* if link has the minimum required transmission quality
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001291 * consider it bidirectional
1292 */
Sven Eckelmann96412692012-06-05 22:31:30 +02001293 if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001294 ret = true;
Marek Lindnerfc957272011-07-30 12:04:12 +02001295
1296out:
1297 if (neigh_node)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001298 batadv_neigh_node_put(neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001299 return ret;
1300}
1301
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001302/**
1303 * batadv_iv_ogm_update_seqnos - process a batman packet for all interfaces,
1304 * adjust the sequence number and find out whether it is a duplicate
1305 * @ethhdr: ethernet header of the packet
1306 * @batadv_ogm_packet: OGM packet to be considered
1307 * @if_incoming: interface on which the OGM packet was received
Simon Wunderlich89652332013-11-13 19:14:46 +01001308 * @if_outgoing: interface for which the retransmission should be considered
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001309 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001310 * Return: duplicate status as enum batadv_dup_status
Marek Lindnerfc957272011-07-30 12:04:12 +02001311 */
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001312static enum batadv_dup_status
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001313batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +02001314 const struct batadv_ogm_packet *batadv_ogm_packet,
Simon Wunderlich89652332013-11-13 19:14:46 +01001315 const struct batadv_hard_iface *if_incoming,
1316 struct batadv_hard_iface *if_outgoing)
Marek Lindnerfc957272011-07-30 12:04:12 +02001317{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001318 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1319 struct batadv_orig_node *orig_node;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001320 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
Simon Wunderlich89652332013-11-13 19:14:46 +01001321 struct batadv_neigh_node *neigh_node;
1322 struct batadv_neigh_ifinfo *neigh_ifinfo;
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001323 bool is_dup;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001324 s32 seq_diff;
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001325 bool need_update = false;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001326 int set_mark;
1327 enum batadv_dup_status ret = BATADV_NO_DUP;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001328 u32 seqno = ntohl(batadv_ogm_packet->seqno);
1329 u8 *neigh_addr;
1330 u8 packet_count;
Antonio Quartulli0538f752013-09-02 12:15:01 +02001331 unsigned long *bitmap;
Marek Lindnerfc957272011-07-30 12:04:12 +02001332
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001333 orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
Marek Lindnerfc957272011-07-30 12:04:12 +02001334 if (!orig_node)
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001335 return BATADV_NO_DUP;
Marek Lindnerfc957272011-07-30 12:04:12 +02001336
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001337 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1338 if (WARN_ON(!orig_ifinfo)) {
Sven Eckelmann5d967312016-01-17 11:01:09 +01001339 batadv_orig_node_put(orig_node);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001340 return 0;
1341 }
1342
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001343 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001344 seq_diff = seqno - orig_ifinfo->last_real_seqno;
Marek Lindnerfc957272011-07-30 12:04:12 +02001345
1346 /* signalize caller that the packet is to be dropped. */
Antonio Quartulli1e5cc262012-02-26 15:39:42 +01001347 if (!hlist_empty(&orig_node->neigh_list) &&
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001348 batadv_window_protected(bat_priv, seq_diff,
Simon Wunderlich81f02682015-11-23 19:57:22 +01001349 BATADV_TQ_LOCAL_WINDOW_SIZE,
1350 &orig_ifinfo->batman_seqno_reset, NULL)) {
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001351 ret = BATADV_PROTECTED;
Marek Lindnerfc957272011-07-30 12:04:12 +02001352 goto out;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001353 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001354
1355 rcu_read_lock();
Simon Wunderlich89652332013-11-13 19:14:46 +01001356 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1357 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node,
1358 if_outgoing);
1359 if (!neigh_ifinfo)
1360 continue;
1361
1362 neigh_addr = neigh_node->addr;
1363 is_dup = batadv_test_bit(neigh_ifinfo->bat_iv.real_bits,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001364 orig_ifinfo->last_real_seqno,
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001365 seqno);
1366
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001367 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
Simon Wunderlich89652332013-11-13 19:14:46 +01001368 neigh_node->if_incoming == if_incoming) {
Marek Lindnerfc957272011-07-30 12:04:12 +02001369 set_mark = 1;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001370 if (is_dup)
1371 ret = BATADV_NEIGH_DUP;
1372 } else {
Marek Lindnerfc957272011-07-30 12:04:12 +02001373 set_mark = 0;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001374 if (is_dup && (ret != BATADV_NEIGH_DUP))
1375 ret = BATADV_ORIG_DUP;
1376 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001377
1378 /* if the window moved, set the update flag. */
Simon Wunderlich89652332013-11-13 19:14:46 +01001379 bitmap = neigh_ifinfo->bat_iv.real_bits;
Antonio Quartulli0538f752013-09-02 12:15:01 +02001380 need_update |= batadv_bit_get_packet(bat_priv, bitmap,
Sven Eckelmann0f5f93222012-05-12 02:09:25 +02001381 seq_diff, set_mark);
Marek Lindnerfc957272011-07-30 12:04:12 +02001382
Simon Wunderlich89652332013-11-13 19:14:46 +01001383 packet_count = bitmap_weight(bitmap,
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001384 BATADV_TQ_LOCAL_WINDOW_SIZE);
Simon Wunderlich89652332013-11-13 19:14:46 +01001385 neigh_ifinfo->bat_iv.real_packet_count = packet_count;
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001386 batadv_neigh_ifinfo_put(neigh_ifinfo);
Marek Lindnerfc957272011-07-30 12:04:12 +02001387 }
1388 rcu_read_unlock();
1389
1390 if (need_update) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001391 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001392 "%s updating last_seqno: old %u, new %u\n",
1393 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT",
1394 orig_ifinfo->last_real_seqno, seqno);
1395 orig_ifinfo->last_real_seqno = seqno;
Marek Lindnerfc957272011-07-30 12:04:12 +02001396 }
1397
Marek Lindnerfc957272011-07-30 12:04:12 +02001398out:
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001399 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Sven Eckelmann5d967312016-01-17 11:01:09 +01001400 batadv_orig_node_put(orig_node);
Sven Eckelmann35f94772016-01-17 11:01:13 +01001401 batadv_orig_ifinfo_put(orig_ifinfo);
Marek Lindnerfc957272011-07-30 12:04:12 +02001402 return ret;
1403}
1404
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001405/**
1406 * batadv_iv_ogm_process_per_outif - process a batman iv OGM for an outgoing if
1407 * @skb: the skb containing the OGM
Martin Hundebøll95298a92014-07-15 09:41:05 +02001408 * @ogm_offset: offset from skb->data to start of ogm header
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001409 * @orig_node: the (cached) orig node for the originator of this OGM
1410 * @if_incoming: the interface where this packet was received
1411 * @if_outgoing: the interface for which the packet should be considered
1412 */
1413static void
1414batadv_iv_ogm_process_per_outif(const struct sk_buff *skb, int ogm_offset,
1415 struct batadv_orig_node *orig_node,
1416 struct batadv_hard_iface *if_incoming,
1417 struct batadv_hard_iface *if_outgoing)
1418{
1419 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Marek Lindner4ff1e2a2015-08-04 21:09:58 +08001420 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
Sven Eckelmann4f248cf2015-06-09 20:50:49 +02001421 struct batadv_neigh_node *router = NULL;
1422 struct batadv_neigh_node *router_router = NULL;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001423 struct batadv_orig_node *orig_neigh_node;
1424 struct batadv_orig_ifinfo *orig_ifinfo;
1425 struct batadv_neigh_node *orig_neigh_router = NULL;
1426 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
1427 struct batadv_ogm_packet *ogm_packet;
1428 enum batadv_dup_status dup_status;
1429 bool is_from_best_next_hop = false;
1430 bool is_single_hop_neigh = false;
1431 bool sameseq, similar_ttl;
1432 struct sk_buff *skb_priv;
1433 struct ethhdr *ethhdr;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001434 u8 *prev_sender;
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001435 bool is_bidirect;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001436
1437 /* create a private copy of the skb, as some functions change tq value
1438 * and/or flags.
1439 */
1440 skb_priv = skb_copy(skb, GFP_ATOMIC);
1441 if (!skb_priv)
1442 return;
1443
1444 ethhdr = eth_hdr(skb_priv);
1445 ogm_packet = (struct batadv_ogm_packet *)(skb_priv->data + ogm_offset);
1446
1447 dup_status = batadv_iv_ogm_update_seqnos(ethhdr, ogm_packet,
1448 if_incoming, if_outgoing);
1449 if (batadv_compare_eth(ethhdr->h_source, ogm_packet->orig))
1450 is_single_hop_neigh = true;
1451
1452 if (dup_status == BATADV_PROTECTED) {
1453 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1454 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1455 ethhdr->h_source);
1456 goto out;
1457 }
1458
1459 if (ogm_packet->tq == 0) {
1460 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1461 "Drop packet: originator packet with tq equal 0\n");
1462 goto out;
1463 }
1464
Marek Lindner4ff1e2a2015-08-04 21:09:58 +08001465 if (is_single_hop_neigh) {
1466 hardif_neigh = batadv_hardif_neigh_get(if_incoming,
1467 ethhdr->h_source);
1468 if (hardif_neigh)
1469 hardif_neigh->last_seen = jiffies;
1470 }
1471
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001472 router = batadv_orig_router_get(orig_node, if_outgoing);
1473 if (router) {
1474 router_router = batadv_orig_router_get(router->orig_node,
1475 if_outgoing);
1476 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1477 }
1478
1479 if ((router_ifinfo && router_ifinfo->bat_iv.tq_avg != 0) &&
1480 (batadv_compare_eth(router->addr, ethhdr->h_source)))
1481 is_from_best_next_hop = true;
1482
1483 prev_sender = ogm_packet->prev_sender;
1484 /* avoid temporary routing loops */
1485 if (router && router_router &&
1486 (batadv_compare_eth(router->addr, prev_sender)) &&
1487 !(batadv_compare_eth(ogm_packet->orig, prev_sender)) &&
1488 (batadv_compare_eth(router->addr, router_router->addr))) {
1489 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1490 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1491 ethhdr->h_source);
1492 goto out;
1493 }
1494
1495 if (if_outgoing == BATADV_IF_DEFAULT)
1496 batadv_tvlv_ogm_receive(bat_priv, ogm_packet, orig_node);
1497
1498 /* if sender is a direct neighbor the sender mac equals
1499 * originator mac
1500 */
1501 if (is_single_hop_neigh)
1502 orig_neigh_node = orig_node;
1503 else
1504 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1505 ethhdr->h_source);
1506
1507 if (!orig_neigh_node)
1508 goto out;
1509
1510 /* Update nc_nodes of the originator */
1511 batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node,
1512 ogm_packet, is_single_hop_neigh);
1513
1514 orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
1515 if_outgoing);
1516
1517 /* drop packet if sender is not a direct neighbor and if we
1518 * don't route towards it
1519 */
1520 if (!is_single_hop_neigh && (!orig_neigh_router)) {
1521 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1522 "Drop packet: OGM via unknown neighbor!\n");
1523 goto out_neigh;
1524 }
1525
1526 is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
1527 ogm_packet, if_incoming,
1528 if_outgoing);
1529
1530 /* update ranking if it is not a duplicate or has the same
1531 * seqno and similar ttl as the non-duplicate
1532 */
1533 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1534 if (!orig_ifinfo)
1535 goto out_neigh;
1536
1537 sameseq = orig_ifinfo->last_real_seqno == ntohl(ogm_packet->seqno);
1538 similar_ttl = (orig_ifinfo->last_ttl - 3) <= ogm_packet->ttl;
1539
1540 if (is_bidirect && ((dup_status == BATADV_NO_DUP) ||
1541 (sameseq && similar_ttl))) {
1542 batadv_iv_ogm_orig_update(bat_priv, orig_node,
1543 orig_ifinfo, ethhdr,
1544 ogm_packet, if_incoming,
1545 if_outgoing, dup_status);
1546 }
Sven Eckelmann35f94772016-01-17 11:01:13 +01001547 batadv_orig_ifinfo_put(orig_ifinfo);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001548
Simon Wunderlichef0a9372013-11-13 19:14:49 +01001549 /* only forward for specific interface, not for the default one. */
1550 if (if_outgoing == BATADV_IF_DEFAULT)
1551 goto out_neigh;
1552
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001553 /* is single hop (direct) neighbor */
1554 if (is_single_hop_neigh) {
1555 /* OGMs from secondary interfaces should only scheduled once
1556 * per interface where it has been received, not multiple times
1557 */
1558 if ((ogm_packet->ttl <= 2) &&
1559 (if_incoming != if_outgoing)) {
1560 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1561 "Drop packet: OGM from secondary interface and wrong outgoing interface\n");
1562 goto out_neigh;
1563 }
1564 /* mark direct link on incoming interface */
1565 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1566 is_single_hop_neigh,
Simon Wunderlichef0a9372013-11-13 19:14:49 +01001567 is_from_best_next_hop, if_incoming,
1568 if_outgoing);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001569
1570 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1571 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
1572 goto out_neigh;
1573 }
1574
1575 /* multihop originator */
1576 if (!is_bidirect) {
1577 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1578 "Drop packet: not received via bidirectional link\n");
1579 goto out_neigh;
1580 }
1581
1582 if (dup_status == BATADV_NEIGH_DUP) {
1583 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1584 "Drop packet: duplicate packet received\n");
1585 goto out_neigh;
1586 }
1587
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001588 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1589 "Forwarding packet: rebroadcast originator packet\n");
1590 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1591 is_single_hop_neigh, is_from_best_next_hop,
Simon Wunderlichef0a9372013-11-13 19:14:49 +01001592 if_incoming, if_outgoing);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001593
1594out_neigh:
1595 if ((orig_neigh_node) && (!is_single_hop_neigh))
Sven Eckelmann5d967312016-01-17 11:01:09 +01001596 batadv_orig_node_put(orig_neigh_node);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001597out:
Simon Wunderlichc1e517f2014-03-26 15:46:21 +01001598 if (router_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001599 batadv_neigh_ifinfo_put(router_ifinfo);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001600 if (router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001601 batadv_neigh_node_put(router);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001602 if (router_router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001603 batadv_neigh_node_put(router_router);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001604 if (orig_neigh_router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001605 batadv_neigh_node_put(orig_neigh_router);
Marek Lindner4ff1e2a2015-08-04 21:09:58 +08001606 if (hardif_neigh)
Sven Eckelmannaccadc32016-01-17 11:01:14 +01001607 batadv_hardif_neigh_put(hardif_neigh);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001608
Sven Eckelmannbd687fe2016-07-17 21:04:00 +02001609 consume_skb(skb_priv);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001610}
1611
1612/**
1613 * batadv_iv_ogm_process - process an incoming batman iv OGM
1614 * @skb: the skb containing the OGM
1615 * @ogm_offset: offset to the OGM which should be processed (for aggregates)
1616 * @if_incoming: the interface where this packet was receved
1617 */
1618static void batadv_iv_ogm_process(const struct sk_buff *skb, int ogm_offset,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001619 struct batadv_hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +02001620{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001621 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001622 struct batadv_orig_node *orig_neigh_node, *orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001623 struct batadv_hard_iface *hard_iface;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001624 struct batadv_ogm_packet *ogm_packet;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001625 u32 if_incoming_seqno;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001626 bool has_directlink_flag;
1627 struct ethhdr *ethhdr;
1628 bool is_my_oldorig = false;
1629 bool is_my_addr = false;
1630 bool is_my_orig = false;
1631
1632 ogm_packet = (struct batadv_ogm_packet *)(skb->data + ogm_offset);
1633 ethhdr = eth_hdr(skb);
Marek Lindnerfc957272011-07-30 12:04:12 +02001634
1635 /* Silently drop when the batman packet is actually not a
1636 * correct packet.
1637 *
1638 * This might happen if a packet is padded (e.g. Ethernet has a
1639 * minimum frame length of 64 byte) and the aggregation interprets
1640 * it as an additional length.
1641 *
1642 * TODO: A more sane solution would be to have a bit in the
Sven Eckelmann96412692012-06-05 22:31:30 +02001643 * batadv_ogm_packet to detect whether the packet is the last
Marek Lindnerfc957272011-07-30 12:04:12 +02001644 * packet in an aggregation. Here we expect that the padding
1645 * is always zero (or not 0x01)
1646 */
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001647 if (ogm_packet->packet_type != BATADV_IV_OGM)
Marek Lindnerfc957272011-07-30 12:04:12 +02001648 return;
1649
1650 /* could be changed by schedule_own_packet() */
Marek Lindner14511512012-08-02 17:20:26 +02001651 if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno);
Marek Lindnerfc957272011-07-30 12:04:12 +02001652
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001653 if (ogm_packet->flags & BATADV_DIRECTLINK)
1654 has_directlink_flag = true;
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001655 else
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001656 has_directlink_flag = false;
Marek Lindnerfc957272011-07-30 12:04:12 +02001657
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001658 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001659 "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 +02001660 ethhdr->h_source, if_incoming->net_dev->name,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001661 if_incoming->net_dev->dev_addr, ogm_packet->orig,
1662 ogm_packet->prev_sender, ntohl(ogm_packet->seqno),
1663 ogm_packet->tq, ogm_packet->ttl,
1664 ogm_packet->version, has_directlink_flag);
Marek Lindnerfc957272011-07-30 12:04:12 +02001665
1666 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001667 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001668 if (hard_iface->if_status != BATADV_IF_ACTIVE)
Marek Lindnerfc957272011-07-30 12:04:12 +02001669 continue;
1670
1671 if (hard_iface->soft_iface != if_incoming->soft_iface)
1672 continue;
1673
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001674 if (batadv_compare_eth(ethhdr->h_source,
1675 hard_iface->net_dev->dev_addr))
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001676 is_my_addr = true;
Marek Lindnerfc957272011-07-30 12:04:12 +02001677
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001678 if (batadv_compare_eth(ogm_packet->orig,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001679 hard_iface->net_dev->dev_addr))
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001680 is_my_orig = true;
Marek Lindnerfc957272011-07-30 12:04:12 +02001681
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001682 if (batadv_compare_eth(ogm_packet->prev_sender,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001683 hard_iface->net_dev->dev_addr))
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001684 is_my_oldorig = true;
Marek Lindnerfc957272011-07-30 12:04:12 +02001685 }
1686 rcu_read_unlock();
1687
Marek Lindnerfc957272011-07-30 12:04:12 +02001688 if (is_my_addr) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001689 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001690 "Drop packet: received my own broadcast (sender: %pM)\n",
1691 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001692 return;
1693 }
1694
Marek Lindnerfc957272011-07-30 12:04:12 +02001695 if (is_my_orig) {
1696 unsigned long *word;
1697 int offset;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001698 s32 bit_pos;
1699 s16 if_num;
1700 u8 *weight;
Marek Lindnerfc957272011-07-30 12:04:12 +02001701
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001702 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1703 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001704 if (!orig_neigh_node)
1705 return;
1706
1707 /* neighbor has to indicate direct link and it has to
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001708 * come via the corresponding interface
1709 * save packet seqno for bidirectional check
1710 */
Marek Lindnerfc957272011-07-30 12:04:12 +02001711 if (has_directlink_flag &&
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001712 batadv_compare_eth(if_incoming->net_dev->dev_addr,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001713 ogm_packet->orig)) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001714 if_num = if_incoming->if_num;
1715 offset = if_num * BATADV_NUM_WORDS;
Marek Lindnerfc957272011-07-30 12:04:12 +02001716
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001717 spin_lock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
Antonio Quartulli32db6aa2014-09-01 14:37:29 +02001718 word = &orig_neigh_node->bat_iv.bcast_own[offset];
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001719 bit_pos = if_incoming_seqno - 2;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001720 bit_pos -= ntohl(ogm_packet->seqno);
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001721 batadv_set_bit(word, bit_pos);
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001722 weight = &orig_neigh_node->bat_iv.bcast_own_sum[if_num];
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001723 *weight = bitmap_weight(word,
1724 BATADV_TQ_LOCAL_WINDOW_SIZE);
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001725 spin_unlock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001726 }
1727
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001728 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001729 "Drop packet: originator packet from myself (via neighbor)\n");
Sven Eckelmann5d967312016-01-17 11:01:09 +01001730 batadv_orig_node_put(orig_neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001731 return;
1732 }
1733
1734 if (is_my_oldorig) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001735 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001736 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1737 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001738 return;
1739 }
1740
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001741 if (ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001742 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001743 "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1744 ethhdr->h_source);
Marek Lindner13b25412012-03-11 06:17:53 +08001745 return;
1746 }
1747
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001748 orig_node = batadv_iv_ogm_orig_get(bat_priv, ogm_packet->orig);
Marek Lindnerfc957272011-07-30 12:04:12 +02001749 if (!orig_node)
1750 return;
1751
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001752 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1753 if_incoming, BATADV_IF_DEFAULT);
Marek Lindnerfc957272011-07-30 12:04:12 +02001754
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001755 rcu_read_lock();
1756 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1757 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1758 continue;
1759
1760 if (hard_iface->soft_iface != bat_priv->soft_iface)
1761 continue;
1762
Sven Eckelmann27353442016-03-05 16:09:16 +01001763 if (!kref_get_unless_zero(&hard_iface->refcount))
1764 continue;
1765
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001766 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1767 if_incoming, hard_iface);
Sven Eckelmann27353442016-03-05 16:09:16 +01001768
1769 batadv_hardif_put(hard_iface);
Marek Lindnerfc957272011-07-30 12:04:12 +02001770 }
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001771 rcu_read_unlock();
Marek Lindnerfc957272011-07-30 12:04:12 +02001772
Sven Eckelmann5d967312016-01-17 11:01:09 +01001773 batadv_orig_node_put(orig_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001774}
1775
Antonio Quartullif0d97252016-05-03 01:45:34 +08001776static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work)
1777{
1778 struct delayed_work *delayed_work;
1779 struct batadv_forw_packet *forw_packet;
1780 struct batadv_priv *bat_priv;
Sven Eckelmannbd687fe2016-07-17 21:04:00 +02001781 bool dropped = false;
Antonio Quartullif0d97252016-05-03 01:45:34 +08001782
1783 delayed_work = to_delayed_work(work);
1784 forw_packet = container_of(delayed_work, struct batadv_forw_packet,
1785 delayed_work);
1786 bat_priv = netdev_priv(forw_packet->if_incoming->soft_iface);
Antonio Quartullif0d97252016-05-03 01:45:34 +08001787
Sven Eckelmannbd687fe2016-07-17 21:04:00 +02001788 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING) {
1789 dropped = true;
Antonio Quartullif0d97252016-05-03 01:45:34 +08001790 goto out;
Sven Eckelmannbd687fe2016-07-17 21:04:00 +02001791 }
Antonio Quartullif0d97252016-05-03 01:45:34 +08001792
1793 batadv_iv_ogm_emit(forw_packet);
1794
1795 /* we have to have at least one packet in the queue to determine the
1796 * queues wake up time unless we are shutting down.
1797 *
1798 * only re-schedule if this is the "original" copy, e.g. the OGM of the
1799 * primary interface should only be rescheduled once per period, but
1800 * this function will be called for the forw_packet instances of the
1801 * other secondary interfaces as well.
1802 */
1803 if (forw_packet->own &&
1804 forw_packet->if_incoming == forw_packet->if_outgoing)
1805 batadv_iv_ogm_schedule(forw_packet->if_incoming);
1806
1807out:
Linus LĂĽssing9b4aec62016-11-01 09:44:44 +01001808 /* do we get something for free()? */
1809 if (batadv_forw_packet_steal(forw_packet,
1810 &bat_priv->forw_bat_list_lock))
1811 batadv_forw_packet_free(forw_packet, dropped);
Antonio Quartullif0d97252016-05-03 01:45:34 +08001812}
1813
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001814static int batadv_iv_ogm_receive(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001815 struct batadv_hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +02001816{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001817 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001818 struct batadv_ogm_packet *ogm_packet;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001819 u8 *packet_pos;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001820 int ogm_offset;
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001821 bool res;
1822 int ret = NET_RX_DROP;
Marek Lindnerc3e29312012-03-04 16:56:25 +08001823
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001824 res = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
1825 if (!res)
1826 goto free_skb;
Marek Lindnerfc957272011-07-30 12:04:12 +02001827
Marek Lindneredbf12b2012-03-11 06:17:49 +08001828 /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
1829 * that does not have B.A.T.M.A.N. IV enabled ?
1830 */
Antonio Quartulli29824a52016-05-25 23:27:31 +08001831 if (bat_priv->algo_ops->iface.enable != batadv_iv_ogm_iface_enable)
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001832 goto free_skb;
Marek Lindneredbf12b2012-03-11 06:17:49 +08001833
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001834 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
1835 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
Martin Hundebøllf8214862012-04-20 17:02:45 +02001836 skb->len + ETH_HLEN);
1837
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001838 ogm_offset = 0;
1839 ogm_packet = (struct batadv_ogm_packet *)skb->data;
Marek Lindnerfc957272011-07-30 12:04:12 +02001840
1841 /* unpack the aggregated packets and process them one by one */
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001842 while (batadv_iv_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
1843 ogm_packet->tvlv_len)) {
1844 batadv_iv_ogm_process(skb, ogm_offset, if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001845
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001846 ogm_offset += BATADV_OGM_HLEN;
1847 ogm_offset += ntohs(ogm_packet->tvlv_len);
Marek Lindnerfc957272011-07-30 12:04:12 +02001848
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001849 packet_pos = skb->data + ogm_offset;
1850 ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Marek Lindnerb47506d2013-03-04 10:39:49 +08001851 }
Marek Lindnerc3e29312012-03-04 16:56:25 +08001852
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001853 ret = NET_RX_SUCCESS;
1854
1855free_skb:
1856 if (ret == NET_RX_SUCCESS)
1857 consume_skb(skb);
1858 else
1859 kfree_skb(skb);
1860
1861 return ret;
Marek Lindnerfc957272011-07-30 12:04:12 +02001862}
Marek Lindner1c280472011-11-28 17:40:17 +08001863
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02001864#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001865/**
1866 * batadv_iv_ogm_orig_print_neigh - print neighbors for the originator table
Simon Wunderlich89652332013-11-13 19:14:46 +01001867 * @orig_node: the orig_node for which the neighbors are printed
1868 * @if_outgoing: outgoing interface for these entries
1869 * @seq: debugfs table seq_file struct
1870 *
1871 * Must be called while holding an rcu lock.
1872 */
1873static void
1874batadv_iv_ogm_orig_print_neigh(struct batadv_orig_node *orig_node,
1875 struct batadv_hard_iface *if_outgoing,
1876 struct seq_file *seq)
1877{
1878 struct batadv_neigh_node *neigh_node;
1879 struct batadv_neigh_ifinfo *n_ifinfo;
1880
1881 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1882 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
1883 if (!n_ifinfo)
1884 continue;
1885
1886 seq_printf(seq, " %pM (%3i)",
1887 neigh_node->addr,
1888 n_ifinfo->bat_iv.tq_avg);
1889
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001890 batadv_neigh_ifinfo_put(n_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01001891 }
1892}
1893
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001894/**
1895 * batadv_iv_ogm_orig_print - print the originator table
1896 * @bat_priv: the bat priv with all the soft interface information
1897 * @seq: debugfs table seq_file struct
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001898 * @if_outgoing: the outgoing interface for which this should be printed
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001899 */
1900static void batadv_iv_ogm_orig_print(struct batadv_priv *bat_priv,
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001901 struct seq_file *seq,
1902 struct batadv_hard_iface *if_outgoing)
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001903{
Simon Wunderlich89652332013-11-13 19:14:46 +01001904 struct batadv_neigh_node *neigh_node;
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001905 struct batadv_hashtable *hash = bat_priv->orig_hash;
1906 int last_seen_msecs, last_seen_secs;
1907 struct batadv_orig_node *orig_node;
Simon Wunderlich89652332013-11-13 19:14:46 +01001908 struct batadv_neigh_ifinfo *n_ifinfo;
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001909 unsigned long last_seen_jiffies;
1910 struct hlist_head *head;
1911 int batman_count = 0;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001912 u32 i;
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001913
Antonio Quartulli925a6f32016-03-12 10:30:18 +01001914 seq_puts(seq,
1915 " Originator last-seen (#/255) Nexthop [outgoingIF]: Potential nexthops ...\n");
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001916
1917 for (i = 0; i < hash->size; i++) {
1918 head = &hash->table[i];
1919
1920 rcu_read_lock();
1921 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001922 neigh_node = batadv_orig_router_get(orig_node,
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001923 if_outgoing);
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001924 if (!neigh_node)
1925 continue;
1926
Simon Wunderlich89652332013-11-13 19:14:46 +01001927 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node,
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001928 if_outgoing);
Simon Wunderlich89652332013-11-13 19:14:46 +01001929 if (!n_ifinfo)
1930 goto next;
1931
1932 if (n_ifinfo->bat_iv.tq_avg == 0)
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001933 goto next;
1934
1935 last_seen_jiffies = jiffies - orig_node->last_seen;
1936 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
1937 last_seen_secs = last_seen_msecs / 1000;
1938 last_seen_msecs = last_seen_msecs % 1000;
1939
1940 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
1941 orig_node->orig, last_seen_secs,
Simon Wunderlich89652332013-11-13 19:14:46 +01001942 last_seen_msecs, n_ifinfo->bat_iv.tq_avg,
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001943 neigh_node->addr,
1944 neigh_node->if_incoming->net_dev->name);
1945
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001946 batadv_iv_ogm_orig_print_neigh(orig_node, if_outgoing,
1947 seq);
Markus Elfring626caae2017-05-06 17:50:13 +02001948 seq_putc(seq, '\n');
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001949 batman_count++;
1950
1951next:
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001952 batadv_neigh_node_put(neigh_node);
Simon Wunderlich89652332013-11-13 19:14:46 +01001953 if (n_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001954 batadv_neigh_ifinfo_put(n_ifinfo);
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001955 }
1956 rcu_read_unlock();
1957 }
1958
1959 if (batman_count == 0)
1960 seq_puts(seq, "No batman nodes in range ...\n");
1961}
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02001962#endif
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001963
Antonio Quartullia3285a82013-09-02 12:15:04 +02001964/**
Matthias Schiffer024f99c2016-07-03 13:31:40 +02001965 * batadv_iv_ogm_neigh_get_tq_avg - Get the TQ average for a neighbour on a
1966 * given outgoing interface.
1967 * @neigh_node: Neighbour of interest
1968 * @if_outgoing: Outgoing interface of interest
1969 * @tq_avg: Pointer of where to store the TQ average
1970 *
1971 * Return: False if no average TQ available, otherwise true.
1972 */
1973static bool
1974batadv_iv_ogm_neigh_get_tq_avg(struct batadv_neigh_node *neigh_node,
1975 struct batadv_hard_iface *if_outgoing,
1976 u8 *tq_avg)
1977{
1978 struct batadv_neigh_ifinfo *n_ifinfo;
1979
1980 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
1981 if (!n_ifinfo)
1982 return false;
1983
1984 *tq_avg = n_ifinfo->bat_iv.tq_avg;
1985 batadv_neigh_ifinfo_put(n_ifinfo);
1986
1987 return true;
1988}
1989
1990/**
1991 * batadv_iv_ogm_orig_dump_subentry - Dump an originator subentry into a
1992 * message
1993 * @msg: Netlink message to dump into
1994 * @portid: Port making netlink request
1995 * @seq: Sequence number of netlink message
1996 * @bat_priv: The bat priv with all the soft interface information
1997 * @if_outgoing: Limit dump to entries with this outgoing interface
1998 * @orig_node: Originator to dump
1999 * @neigh_node: Single hops neighbour
2000 * @best: Is the best originator
2001 *
2002 * Return: Error code, or 0 on success
2003 */
2004static int
2005batadv_iv_ogm_orig_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
2006 struct batadv_priv *bat_priv,
2007 struct batadv_hard_iface *if_outgoing,
2008 struct batadv_orig_node *orig_node,
2009 struct batadv_neigh_node *neigh_node,
2010 bool best)
2011{
2012 void *hdr;
2013 u8 tq_avg;
2014 unsigned int last_seen_msecs;
2015
2016 last_seen_msecs = jiffies_to_msecs(jiffies - orig_node->last_seen);
2017
2018 if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node, if_outgoing, &tq_avg))
2019 return 0;
2020
2021 if (if_outgoing != BATADV_IF_DEFAULT &&
2022 if_outgoing != neigh_node->if_incoming)
2023 return 0;
2024
2025 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2026 NLM_F_MULTI, BATADV_CMD_GET_ORIGINATORS);
2027 if (!hdr)
2028 return -ENOBUFS;
2029
2030 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
2031 orig_node->orig) ||
2032 nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
2033 neigh_node->addr) ||
2034 nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
2035 neigh_node->if_incoming->net_dev->ifindex) ||
2036 nla_put_u8(msg, BATADV_ATTR_TQ, tq_avg) ||
2037 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
2038 last_seen_msecs))
2039 goto nla_put_failure;
2040
2041 if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
2042 goto nla_put_failure;
2043
2044 genlmsg_end(msg, hdr);
2045 return 0;
2046
2047 nla_put_failure:
2048 genlmsg_cancel(msg, hdr);
2049 return -EMSGSIZE;
2050}
2051
2052/**
2053 * batadv_iv_ogm_orig_dump_entry - Dump an originator entry into a message
2054 * @msg: Netlink message to dump into
2055 * @portid: Port making netlink request
2056 * @seq: Sequence number of netlink message
2057 * @bat_priv: The bat priv with all the soft interface information
2058 * @if_outgoing: Limit dump to entries with this outgoing interface
2059 * @orig_node: Originator to dump
2060 * @sub_s: Number of sub entries to skip
2061 *
2062 * This function assumes the caller holds rcu_read_lock().
2063 *
2064 * Return: Error code, or 0 on success
2065 */
2066static int
2067batadv_iv_ogm_orig_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
2068 struct batadv_priv *bat_priv,
2069 struct batadv_hard_iface *if_outgoing,
2070 struct batadv_orig_node *orig_node, int *sub_s)
2071{
2072 struct batadv_neigh_node *neigh_node_best;
2073 struct batadv_neigh_node *neigh_node;
2074 int sub = 0;
2075 bool best;
2076 u8 tq_avg_best;
2077
2078 neigh_node_best = batadv_orig_router_get(orig_node, if_outgoing);
2079 if (!neigh_node_best)
2080 goto out;
2081
2082 if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node_best, if_outgoing,
2083 &tq_avg_best))
2084 goto out;
2085
2086 if (tq_avg_best == 0)
2087 goto out;
2088
2089 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
2090 if (sub++ < *sub_s)
2091 continue;
2092
2093 best = (neigh_node == neigh_node_best);
2094
2095 if (batadv_iv_ogm_orig_dump_subentry(msg, portid, seq,
2096 bat_priv, if_outgoing,
2097 orig_node, neigh_node,
2098 best)) {
2099 batadv_neigh_node_put(neigh_node_best);
2100
2101 *sub_s = sub - 1;
2102 return -EMSGSIZE;
2103 }
2104 }
2105
2106 out:
2107 if (neigh_node_best)
2108 batadv_neigh_node_put(neigh_node_best);
2109
2110 *sub_s = 0;
2111 return 0;
2112}
2113
2114/**
2115 * batadv_iv_ogm_orig_dump_bucket - Dump an originator bucket into a
2116 * message
2117 * @msg: Netlink message to dump into
2118 * @portid: Port making netlink request
2119 * @seq: Sequence number of netlink message
2120 * @bat_priv: The bat priv with all the soft interface information
2121 * @if_outgoing: Limit dump to entries with this outgoing interface
2122 * @head: Bucket to be dumped
2123 * @idx_s: Number of entries to be skipped
2124 * @sub: Number of sub entries to be skipped
2125 *
2126 * Return: Error code, or 0 on success
2127 */
2128static int
2129batadv_iv_ogm_orig_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
2130 struct batadv_priv *bat_priv,
2131 struct batadv_hard_iface *if_outgoing,
2132 struct hlist_head *head, int *idx_s, int *sub)
2133{
2134 struct batadv_orig_node *orig_node;
2135 int idx = 0;
2136
2137 rcu_read_lock();
2138 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
2139 if (idx++ < *idx_s)
2140 continue;
2141
2142 if (batadv_iv_ogm_orig_dump_entry(msg, portid, seq, bat_priv,
2143 if_outgoing, orig_node,
2144 sub)) {
2145 rcu_read_unlock();
2146 *idx_s = idx - 1;
2147 return -EMSGSIZE;
2148 }
2149 }
2150 rcu_read_unlock();
2151
2152 *idx_s = 0;
2153 *sub = 0;
2154 return 0;
2155}
2156
2157/**
2158 * batadv_iv_ogm_orig_dump - Dump the originators into a message
2159 * @msg: Netlink message to dump into
2160 * @cb: Control block containing additional options
2161 * @bat_priv: The bat priv with all the soft interface information
2162 * @if_outgoing: Limit dump to entries with this outgoing interface
2163 */
2164static void
2165batadv_iv_ogm_orig_dump(struct sk_buff *msg, struct netlink_callback *cb,
2166 struct batadv_priv *bat_priv,
2167 struct batadv_hard_iface *if_outgoing)
2168{
2169 struct batadv_hashtable *hash = bat_priv->orig_hash;
2170 struct hlist_head *head;
2171 int bucket = cb->args[0];
2172 int idx = cb->args[1];
2173 int sub = cb->args[2];
2174 int portid = NETLINK_CB(cb->skb).portid;
2175
2176 while (bucket < hash->size) {
2177 head = &hash->table[bucket];
2178
2179 if (batadv_iv_ogm_orig_dump_bucket(msg, portid,
2180 cb->nlh->nlmsg_seq,
2181 bat_priv, if_outgoing, head,
2182 &idx, &sub))
2183 break;
2184
2185 bucket++;
2186 }
2187
2188 cb->args[0] = bucket;
2189 cb->args[1] = idx;
2190 cb->args[2] = sub;
2191}
2192
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002193#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Matthias Schiffer024f99c2016-07-03 13:31:40 +02002194/**
Marek Lindner75874052015-08-04 21:09:57 +08002195 * batadv_iv_hardif_neigh_print - print a single hop neighbour node
2196 * @seq: neighbour table seq_file struct
2197 * @hardif_neigh: hardif neighbour information
2198 */
2199static void
2200batadv_iv_hardif_neigh_print(struct seq_file *seq,
2201 struct batadv_hardif_neigh_node *hardif_neigh)
2202{
2203 int last_secs, last_msecs;
2204
2205 last_secs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) / 1000;
2206 last_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) % 1000;
2207
2208 seq_printf(seq, " %10s %pM %4i.%03is\n",
2209 hardif_neigh->if_incoming->net_dev->name,
2210 hardif_neigh->addr, last_secs, last_msecs);
2211}
2212
2213/**
2214 * batadv_iv_ogm_neigh_print - print the single hop neighbour list
2215 * @bat_priv: the bat priv with all the soft interface information
2216 * @seq: neighbour table seq_file struct
2217 */
2218static void batadv_iv_neigh_print(struct batadv_priv *bat_priv,
2219 struct seq_file *seq)
2220{
2221 struct net_device *net_dev = (struct net_device *)seq->private;
2222 struct batadv_hardif_neigh_node *hardif_neigh;
2223 struct batadv_hard_iface *hard_iface;
2224 int batman_count = 0;
2225
Antonio Quartulli925a6f32016-03-12 10:30:18 +01002226 seq_puts(seq, " IF Neighbor last-seen\n");
Marek Lindner75874052015-08-04 21:09:57 +08002227
2228 rcu_read_lock();
2229 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
2230 if (hard_iface->soft_iface != net_dev)
2231 continue;
2232
2233 hlist_for_each_entry_rcu(hardif_neigh,
2234 &hard_iface->neigh_list, list) {
2235 batadv_iv_hardif_neigh_print(seq, hardif_neigh);
2236 batman_count++;
2237 }
2238 }
2239 rcu_read_unlock();
2240
2241 if (batman_count == 0)
2242 seq_puts(seq, "No batman nodes in range ...\n");
2243}
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002244#endif
Marek Lindner75874052015-08-04 21:09:57 +08002245
2246/**
Markus Pargmann57b12502016-07-03 11:07:14 +02002247 * batadv_iv_ogm_neigh_diff - calculate tq difference of two neighbors
2248 * @neigh1: the first neighbor object of the comparison
2249 * @if_outgoing1: outgoing interface for the first neighbor
2250 * @neigh2: the second neighbor object of the comparison
2251 * @if_outgoing2: outgoing interface for the second neighbor
2252 * @diff: pointer to integer receiving the calculated difference
2253 *
2254 * The content of *@diff is only valid when this function returns true.
2255 * It is less, equal to or greater than 0 if the metric via neigh1 is lower,
2256 * the same as or higher than the metric via neigh2
2257 *
2258 * Return: true when the difference could be calculated, false otherwise
2259 */
2260static bool batadv_iv_ogm_neigh_diff(struct batadv_neigh_node *neigh1,
2261 struct batadv_hard_iface *if_outgoing1,
2262 struct batadv_neigh_node *neigh2,
2263 struct batadv_hard_iface *if_outgoing2,
2264 int *diff)
2265{
2266 struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
2267 u8 tq1, tq2;
2268 bool ret = true;
2269
2270 neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
2271 neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
2272
2273 if (!neigh1_ifinfo || !neigh2_ifinfo) {
2274 ret = false;
2275 goto out;
2276 }
2277
2278 tq1 = neigh1_ifinfo->bat_iv.tq_avg;
2279 tq2 = neigh2_ifinfo->bat_iv.tq_avg;
2280 *diff = (int)tq1 - (int)tq2;
2281
2282out:
2283 if (neigh1_ifinfo)
2284 batadv_neigh_ifinfo_put(neigh1_ifinfo);
2285 if (neigh2_ifinfo)
2286 batadv_neigh_ifinfo_put(neigh2_ifinfo);
2287
2288 return ret;
2289}
2290
2291/**
Matthias Schiffer024f99c2016-07-03 13:31:40 +02002292 * batadv_iv_ogm_neigh_dump_neigh - Dump a neighbour into a netlink message
2293 * @msg: Netlink message to dump into
2294 * @portid: Port making netlink request
2295 * @seq: Sequence number of netlink message
2296 * @hardif_neigh: Neighbour to be dumped
2297 *
2298 * Return: Error code, or 0 on success
2299 */
2300static int
2301batadv_iv_ogm_neigh_dump_neigh(struct sk_buff *msg, u32 portid, u32 seq,
2302 struct batadv_hardif_neigh_node *hardif_neigh)
2303{
2304 void *hdr;
2305 unsigned int last_seen_msecs;
2306
2307 last_seen_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen);
2308
2309 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2310 NLM_F_MULTI, BATADV_CMD_GET_NEIGHBORS);
2311 if (!hdr)
2312 return -ENOBUFS;
2313
2314 if (nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
2315 hardif_neigh->addr) ||
2316 nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
2317 hardif_neigh->if_incoming->net_dev->ifindex) ||
2318 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
2319 last_seen_msecs))
2320 goto nla_put_failure;
2321
2322 genlmsg_end(msg, hdr);
2323 return 0;
2324
2325 nla_put_failure:
2326 genlmsg_cancel(msg, hdr);
2327 return -EMSGSIZE;
2328}
2329
2330/**
2331 * batadv_iv_ogm_neigh_dump_hardif - Dump the neighbours of a hard interface
2332 * into a message
2333 * @msg: Netlink message to dump into
2334 * @portid: Port making netlink request
2335 * @seq: Sequence number of netlink message
2336 * @bat_priv: The bat priv with all the soft interface information
2337 * @hard_iface: Hard interface to dump the neighbours for
2338 * @idx_s: Number of entries to skip
2339 *
2340 * This function assumes the caller holds rcu_read_lock().
2341 *
2342 * Return: Error code, or 0 on success
2343 */
2344static int
2345batadv_iv_ogm_neigh_dump_hardif(struct sk_buff *msg, u32 portid, u32 seq,
2346 struct batadv_priv *bat_priv,
2347 struct batadv_hard_iface *hard_iface,
2348 int *idx_s)
2349{
2350 struct batadv_hardif_neigh_node *hardif_neigh;
2351 int idx = 0;
2352
2353 hlist_for_each_entry_rcu(hardif_neigh,
2354 &hard_iface->neigh_list, list) {
2355 if (idx++ < *idx_s)
2356 continue;
2357
2358 if (batadv_iv_ogm_neigh_dump_neigh(msg, portid, seq,
2359 hardif_neigh)) {
2360 *idx_s = idx - 1;
2361 return -EMSGSIZE;
2362 }
2363 }
2364
2365 *idx_s = 0;
2366 return 0;
2367}
2368
2369/**
2370 * batadv_iv_ogm_neigh_dump - Dump the neighbours into a message
2371 * @msg: Netlink message to dump into
2372 * @cb: Control block containing additional options
2373 * @bat_priv: The bat priv with all the soft interface information
2374 * @single_hardif: Limit dump to this hard interfaace
2375 */
2376static void
2377batadv_iv_ogm_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb,
2378 struct batadv_priv *bat_priv,
2379 struct batadv_hard_iface *single_hardif)
2380{
2381 struct batadv_hard_iface *hard_iface;
2382 int i_hardif = 0;
2383 int i_hardif_s = cb->args[0];
2384 int idx = cb->args[1];
2385 int portid = NETLINK_CB(cb->skb).portid;
2386
2387 rcu_read_lock();
2388 if (single_hardif) {
2389 if (i_hardif_s == 0) {
2390 if (batadv_iv_ogm_neigh_dump_hardif(msg, portid,
2391 cb->nlh->nlmsg_seq,
2392 bat_priv,
2393 single_hardif,
2394 &idx) == 0)
2395 i_hardif++;
2396 }
2397 } else {
2398 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list,
2399 list) {
2400 if (hard_iface->soft_iface != bat_priv->soft_iface)
2401 continue;
2402
2403 if (i_hardif++ < i_hardif_s)
2404 continue;
2405
2406 if (batadv_iv_ogm_neigh_dump_hardif(msg, portid,
2407 cb->nlh->nlmsg_seq,
2408 bat_priv,
2409 hard_iface, &idx)) {
2410 i_hardif--;
2411 break;
2412 }
2413 }
2414 }
2415 rcu_read_unlock();
2416
2417 cb->args[0] = i_hardif;
2418 cb->args[1] = idx;
2419}
2420
2421/**
Antonio Quartullia3285a82013-09-02 12:15:04 +02002422 * batadv_iv_ogm_neigh_cmp - compare the metrics of two neighbors
2423 * @neigh1: the first neighbor object of the comparison
Simon Wunderlich89652332013-11-13 19:14:46 +01002424 * @if_outgoing1: outgoing interface for the first neighbor
Antonio Quartullia3285a82013-09-02 12:15:04 +02002425 * @neigh2: the second neighbor object of the comparison
Simon Wunderlich89652332013-11-13 19:14:46 +01002426 * @if_outgoing2: outgoing interface for the second neighbor
Antonio Quartullia3285a82013-09-02 12:15:04 +02002427 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002428 * Return: a value less, equal to or greater than 0 if the metric via neigh1 is
Antonio Quartullia3285a82013-09-02 12:15:04 +02002429 * lower, the same as or higher than the metric via neigh2
2430 */
2431static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1,
Simon Wunderlich89652332013-11-13 19:14:46 +01002432 struct batadv_hard_iface *if_outgoing1,
2433 struct batadv_neigh_node *neigh2,
2434 struct batadv_hard_iface *if_outgoing2)
Antonio Quartullia3285a82013-09-02 12:15:04 +02002435{
Markus Pargmann57b12502016-07-03 11:07:14 +02002436 bool ret;
Simon Wunderlich89652332013-11-13 19:14:46 +01002437 int diff;
Antonio Quartullia3285a82013-09-02 12:15:04 +02002438
Markus Pargmann57b12502016-07-03 11:07:14 +02002439 ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
2440 if_outgoing2, &diff);
2441 if (!ret)
2442 return 0;
Simon Wunderlich89652332013-11-13 19:14:46 +01002443
2444 return diff;
Antonio Quartullia3285a82013-09-02 12:15:04 +02002445}
2446
Antonio Quartullic43c9812013-09-02 12:15:05 +02002447/**
Simon Wunderlich18165f62015-08-08 02:01:50 +02002448 * batadv_iv_ogm_neigh_is_sob - check if neigh1 is similarly good or better
2449 * than neigh2 from the metric prospective
Antonio Quartullic43c9812013-09-02 12:15:05 +02002450 * @neigh1: the first neighbor object of the comparison
Martin Hundebøll95298a92014-07-15 09:41:05 +02002451 * @if_outgoing1: outgoing interface for the first neighbor
Antonio Quartullic43c9812013-09-02 12:15:05 +02002452 * @neigh2: the second neighbor object of the comparison
Simon Wunderlich89652332013-11-13 19:14:46 +01002453 * @if_outgoing2: outgoing interface for the second neighbor
Martin Hundebøll95298a92014-07-15 09:41:05 +02002454 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002455 * Return: true if the metric via neigh1 is equally good or better than
Simon Wunderlich89652332013-11-13 19:14:46 +01002456 * the metric via neigh2, false otherwise.
2457 */
2458static bool
Simon Wunderlich18165f62015-08-08 02:01:50 +02002459batadv_iv_ogm_neigh_is_sob(struct batadv_neigh_node *neigh1,
Simon Wunderlich89652332013-11-13 19:14:46 +01002460 struct batadv_hard_iface *if_outgoing1,
2461 struct batadv_neigh_node *neigh2,
2462 struct batadv_hard_iface *if_outgoing2)
2463{
Simon Wunderlich89652332013-11-13 19:14:46 +01002464 bool ret;
Markus Pargmann57b12502016-07-03 11:07:14 +02002465 int diff;
Simon Wunderlich89652332013-11-13 19:14:46 +01002466
Markus Pargmann57b12502016-07-03 11:07:14 +02002467 ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
2468 if_outgoing2, &diff);
2469 if (!ret)
2470 return false;
Simon Wunderlich89652332013-11-13 19:14:46 +01002471
Markus Pargmann57b12502016-07-03 11:07:14 +02002472 ret = diff > -BATADV_TQ_SIMILARITY_THRESHOLD;
Simon Wunderlich89652332013-11-13 19:14:46 +01002473 return ret;
Antonio Quartullic43c9812013-09-02 12:15:05 +02002474}
2475
Antonio Quartullif0d97252016-05-03 01:45:34 +08002476static void batadv_iv_iface_activate(struct batadv_hard_iface *hard_iface)
2477{
2478 /* begin scheduling originator messages on that interface */
2479 batadv_iv_ogm_schedule(hard_iface);
2480}
2481
Sven Eckelmann1a9070e2017-03-04 15:48:50 +01002482/**
2483 * batadv_iv_init_sel_class - initialize GW selection class
2484 * @bat_priv: the bat priv with all the soft interface information
2485 */
2486static void batadv_iv_init_sel_class(struct batadv_priv *bat_priv)
2487{
2488 /* set default TQ difference threshold to 20 */
2489 atomic_set(&bat_priv->gw.sel_class, 20);
2490}
2491
Antonio Quartulli34d99cf2016-07-03 12:46:33 +02002492static struct batadv_gw_node *
2493batadv_iv_gw_get_best_gw_node(struct batadv_priv *bat_priv)
2494{
2495 struct batadv_neigh_node *router;
2496 struct batadv_neigh_ifinfo *router_ifinfo;
2497 struct batadv_gw_node *gw_node, *curr_gw = NULL;
2498 u64 max_gw_factor = 0;
2499 u64 tmp_gw_factor = 0;
2500 u8 max_tq = 0;
2501 u8 tq_avg;
2502 struct batadv_orig_node *orig_node;
2503
2504 rcu_read_lock();
Sven Eckelmann70ea5ce2016-07-27 12:31:08 +02002505 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
Antonio Quartulli34d99cf2016-07-03 12:46:33 +02002506 orig_node = gw_node->orig_node;
2507 router = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
2508 if (!router)
2509 continue;
2510
2511 router_ifinfo = batadv_neigh_ifinfo_get(router,
2512 BATADV_IF_DEFAULT);
2513 if (!router_ifinfo)
2514 goto next;
2515
2516 if (!kref_get_unless_zero(&gw_node->refcount))
2517 goto next;
2518
2519 tq_avg = router_ifinfo->bat_iv.tq_avg;
2520
2521 switch (atomic_read(&bat_priv->gw.sel_class)) {
2522 case 1: /* fast connection */
2523 tmp_gw_factor = tq_avg * tq_avg;
2524 tmp_gw_factor *= gw_node->bandwidth_down;
2525 tmp_gw_factor *= 100 * 100;
2526 tmp_gw_factor >>= 18;
2527
2528 if ((tmp_gw_factor > max_gw_factor) ||
2529 ((tmp_gw_factor == max_gw_factor) &&
2530 (tq_avg > max_tq))) {
2531 if (curr_gw)
2532 batadv_gw_node_put(curr_gw);
2533 curr_gw = gw_node;
2534 kref_get(&curr_gw->refcount);
2535 }
2536 break;
2537
2538 default: /* 2: stable connection (use best statistic)
2539 * 3: fast-switch (use best statistic but change as
2540 * soon as a better gateway appears)
2541 * XX: late-switch (use best statistic but change as
2542 * soon as a better gateway appears which has
2543 * $routing_class more tq points)
2544 */
2545 if (tq_avg > max_tq) {
2546 if (curr_gw)
2547 batadv_gw_node_put(curr_gw);
2548 curr_gw = gw_node;
2549 kref_get(&curr_gw->refcount);
2550 }
2551 break;
2552 }
2553
2554 if (tq_avg > max_tq)
2555 max_tq = tq_avg;
2556
2557 if (tmp_gw_factor > max_gw_factor)
2558 max_gw_factor = tmp_gw_factor;
2559
2560 batadv_gw_node_put(gw_node);
2561
2562next:
2563 batadv_neigh_node_put(router);
2564 if (router_ifinfo)
2565 batadv_neigh_ifinfo_put(router_ifinfo);
2566 }
2567 rcu_read_unlock();
2568
2569 return curr_gw;
2570}
2571
2572static bool batadv_iv_gw_is_eligible(struct batadv_priv *bat_priv,
2573 struct batadv_orig_node *curr_gw_orig,
2574 struct batadv_orig_node *orig_node)
2575{
2576 struct batadv_neigh_ifinfo *router_orig_ifinfo = NULL;
2577 struct batadv_neigh_ifinfo *router_gw_ifinfo = NULL;
2578 struct batadv_neigh_node *router_gw = NULL;
2579 struct batadv_neigh_node *router_orig = NULL;
2580 u8 gw_tq_avg, orig_tq_avg;
2581 bool ret = false;
2582
2583 /* dynamic re-election is performed only on fast or late switch */
2584 if (atomic_read(&bat_priv->gw.sel_class) <= 2)
2585 return false;
2586
2587 router_gw = batadv_orig_router_get(curr_gw_orig, BATADV_IF_DEFAULT);
2588 if (!router_gw) {
2589 ret = true;
2590 goto out;
2591 }
2592
2593 router_gw_ifinfo = batadv_neigh_ifinfo_get(router_gw,
2594 BATADV_IF_DEFAULT);
2595 if (!router_gw_ifinfo) {
2596 ret = true;
2597 goto out;
2598 }
2599
2600 router_orig = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
2601 if (!router_orig)
2602 goto out;
2603
2604 router_orig_ifinfo = batadv_neigh_ifinfo_get(router_orig,
2605 BATADV_IF_DEFAULT);
2606 if (!router_orig_ifinfo)
2607 goto out;
2608
2609 gw_tq_avg = router_gw_ifinfo->bat_iv.tq_avg;
2610 orig_tq_avg = router_orig_ifinfo->bat_iv.tq_avg;
2611
2612 /* the TQ value has to be better */
2613 if (orig_tq_avg < gw_tq_avg)
2614 goto out;
2615
2616 /* if the routing class is greater than 3 the value tells us how much
2617 * greater the TQ value of the new gateway must be
2618 */
2619 if ((atomic_read(&bat_priv->gw.sel_class) > 3) &&
2620 (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw.sel_class)))
2621 goto out;
2622
2623 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
2624 "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n",
2625 gw_tq_avg, orig_tq_avg);
2626
2627 ret = true;
2628out:
2629 if (router_gw_ifinfo)
2630 batadv_neigh_ifinfo_put(router_gw_ifinfo);
2631 if (router_orig_ifinfo)
2632 batadv_neigh_ifinfo_put(router_orig_ifinfo);
2633 if (router_gw)
2634 batadv_neigh_node_put(router_gw);
2635 if (router_orig)
2636 batadv_neigh_node_put(router_orig);
2637
2638 return ret;
2639}
2640
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002641#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Antonio Quartulli34d99cf2016-07-03 12:46:33 +02002642/* fails if orig_node has no router */
2643static int batadv_iv_gw_write_buffer_text(struct batadv_priv *bat_priv,
2644 struct seq_file *seq,
2645 const struct batadv_gw_node *gw_node)
2646{
2647 struct batadv_gw_node *curr_gw;
2648 struct batadv_neigh_node *router;
2649 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
2650 int ret = -1;
2651
2652 router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
2653 if (!router)
2654 goto out;
2655
2656 router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
2657 if (!router_ifinfo)
2658 goto out;
2659
2660 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
2661
2662 seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n",
2663 (curr_gw == gw_node ? "=>" : " "),
2664 gw_node->orig_node->orig,
2665 router_ifinfo->bat_iv.tq_avg, router->addr,
2666 router->if_incoming->net_dev->name,
2667 gw_node->bandwidth_down / 10,
2668 gw_node->bandwidth_down % 10,
2669 gw_node->bandwidth_up / 10,
2670 gw_node->bandwidth_up % 10);
2671 ret = seq_has_overflowed(seq) ? -1 : 0;
2672
2673 if (curr_gw)
2674 batadv_gw_node_put(curr_gw);
2675out:
2676 if (router_ifinfo)
2677 batadv_neigh_ifinfo_put(router_ifinfo);
2678 if (router)
2679 batadv_neigh_node_put(router);
2680 return ret;
2681}
2682
2683static void batadv_iv_gw_print(struct batadv_priv *bat_priv,
2684 struct seq_file *seq)
2685{
2686 struct batadv_gw_node *gw_node;
2687 int gw_count = 0;
2688
2689 seq_puts(seq,
2690 " Gateway (#/255) Nexthop [outgoingIF]: advertised uplink bandwidth\n");
2691
2692 rcu_read_lock();
Sven Eckelmann70ea5ce2016-07-27 12:31:08 +02002693 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
Antonio Quartulli34d99cf2016-07-03 12:46:33 +02002694 /* fails if orig_node has no router */
2695 if (batadv_iv_gw_write_buffer_text(bat_priv, seq, gw_node) < 0)
2696 continue;
2697
2698 gw_count++;
2699 }
2700 rcu_read_unlock();
2701
2702 if (gw_count == 0)
2703 seq_puts(seq, "No gateways in range ...\n");
2704}
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002705#endif
Antonio Quartulli34d99cf2016-07-03 12:46:33 +02002706
Andrew Lunnefb766a2016-07-03 13:31:43 +02002707/**
2708 * batadv_iv_gw_dump_entry - Dump a gateway into a message
2709 * @msg: Netlink message to dump into
2710 * @portid: Port making netlink request
2711 * @seq: Sequence number of netlink message
2712 * @bat_priv: The bat priv with all the soft interface information
2713 * @gw_node: Gateway to be dumped
2714 *
2715 * Return: Error code, or 0 on success
2716 */
2717static int batadv_iv_gw_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
2718 struct batadv_priv *bat_priv,
2719 struct batadv_gw_node *gw_node)
2720{
2721 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
2722 struct batadv_neigh_node *router;
2723 struct batadv_gw_node *curr_gw;
2724 int ret = -EINVAL;
2725 void *hdr;
2726
2727 router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
2728 if (!router)
2729 goto out;
2730
2731 router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
2732 if (!router_ifinfo)
2733 goto out;
2734
2735 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
2736
2737 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2738 NLM_F_MULTI, BATADV_CMD_GET_GATEWAYS);
2739 if (!hdr) {
2740 ret = -ENOBUFS;
2741 goto out;
2742 }
2743
2744 ret = -EMSGSIZE;
2745
2746 if (curr_gw == gw_node)
2747 if (nla_put_flag(msg, BATADV_ATTR_FLAG_BEST)) {
2748 genlmsg_cancel(msg, hdr);
2749 goto out;
2750 }
2751
2752 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
2753 gw_node->orig_node->orig) ||
2754 nla_put_u8(msg, BATADV_ATTR_TQ, router_ifinfo->bat_iv.tq_avg) ||
2755 nla_put(msg, BATADV_ATTR_ROUTER, ETH_ALEN,
2756 router->addr) ||
2757 nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
2758 router->if_incoming->net_dev->name) ||
2759 nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_DOWN,
2760 gw_node->bandwidth_down) ||
2761 nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_UP,
2762 gw_node->bandwidth_up)) {
2763 genlmsg_cancel(msg, hdr);
2764 goto out;
2765 }
2766
2767 genlmsg_end(msg, hdr);
2768 ret = 0;
2769
2770out:
2771 if (router_ifinfo)
2772 batadv_neigh_ifinfo_put(router_ifinfo);
2773 if (router)
2774 batadv_neigh_node_put(router);
2775 return ret;
2776}
2777
2778/**
2779 * batadv_iv_gw_dump - Dump gateways into a message
2780 * @msg: Netlink message to dump into
2781 * @cb: Control block containing additional options
2782 * @bat_priv: The bat priv with all the soft interface information
2783 */
2784static void batadv_iv_gw_dump(struct sk_buff *msg, struct netlink_callback *cb,
2785 struct batadv_priv *bat_priv)
2786{
2787 int portid = NETLINK_CB(cb->skb).portid;
2788 struct batadv_gw_node *gw_node;
2789 int idx_skip = cb->args[0];
2790 int idx = 0;
2791
2792 rcu_read_lock();
Sven Eckelmann70ea5ce2016-07-27 12:31:08 +02002793 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
Andrew Lunnefb766a2016-07-03 13:31:43 +02002794 if (idx++ < idx_skip)
2795 continue;
2796
2797 if (batadv_iv_gw_dump_entry(msg, portid, cb->nlh->nlmsg_seq,
2798 bat_priv, gw_node)) {
2799 idx_skip = idx - 1;
2800 goto unlock;
2801 }
2802 }
2803
2804 idx_skip = idx;
2805unlock:
2806 rcu_read_unlock();
2807
2808 cb->args[0] = idx_skip;
2809}
2810
Sven Eckelmann56303d32012-06-05 22:31:31 +02002811static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
Marek Lindner519d3492012-04-18 17:15:57 +08002812 .name = "BATMAN_IV",
Antonio Quartulli29824a52016-05-25 23:27:31 +08002813 .iface = {
2814 .activate = batadv_iv_iface_activate,
2815 .enable = batadv_iv_ogm_iface_enable,
2816 .disable = batadv_iv_ogm_iface_disable,
2817 .update_mac = batadv_iv_ogm_iface_update_mac,
2818 .primary_set = batadv_iv_ogm_primary_iface_set,
2819 },
2820 .neigh = {
2821 .cmp = batadv_iv_ogm_neigh_cmp,
2822 .is_similar_or_better = batadv_iv_ogm_neigh_is_sob,
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002823#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Antonio Quartulli29824a52016-05-25 23:27:31 +08002824 .print = batadv_iv_neigh_print,
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002825#endif
Matthias Schiffer024f99c2016-07-03 13:31:40 +02002826 .dump = batadv_iv_ogm_neigh_dump,
Antonio Quartulli29824a52016-05-25 23:27:31 +08002827 },
2828 .orig = {
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002829#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Antonio Quartulli29824a52016-05-25 23:27:31 +08002830 .print = batadv_iv_ogm_orig_print,
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002831#endif
Matthias Schiffer024f99c2016-07-03 13:31:40 +02002832 .dump = batadv_iv_ogm_orig_dump,
Antonio Quartulli29824a52016-05-25 23:27:31 +08002833 .free = batadv_iv_ogm_orig_free,
2834 .add_if = batadv_iv_ogm_orig_add_if,
2835 .del_if = batadv_iv_ogm_orig_del_if,
2836 },
Antonio Quartulli34d99cf2016-07-03 12:46:33 +02002837 .gw = {
Sven Eckelmann1a9070e2017-03-04 15:48:50 +01002838 .init_sel_class = batadv_iv_init_sel_class,
Antonio Quartulli34d99cf2016-07-03 12:46:33 +02002839 .get_best_gw_node = batadv_iv_gw_get_best_gw_node,
2840 .is_eligible = batadv_iv_gw_is_eligible,
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002841#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Antonio Quartulli34d99cf2016-07-03 12:46:33 +02002842 .print = batadv_iv_gw_print,
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002843#endif
Andrew Lunnefb766a2016-07-03 13:31:43 +02002844 .dump = batadv_iv_gw_dump,
Antonio Quartulli34d99cf2016-07-03 12:46:33 +02002845 },
Marek Lindner1c280472011-11-28 17:40:17 +08002846};
2847
Sven Eckelmann81c524f2012-05-12 02:09:22 +02002848int __init batadv_iv_init(void)
Marek Lindner1c280472011-11-28 17:40:17 +08002849{
Marek Lindnerc3e29312012-03-04 16:56:25 +08002850 int ret;
2851
2852 /* batman originator packet */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002853 ret = batadv_recv_handler_register(BATADV_IV_OGM,
2854 batadv_iv_ogm_receive);
Marek Lindnerc3e29312012-03-04 16:56:25 +08002855 if (ret < 0)
2856 goto out;
2857
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02002858 ret = batadv_algo_register(&batadv_batman_iv);
Marek Lindnerc3e29312012-03-04 16:56:25 +08002859 if (ret < 0)
2860 goto handler_unregister;
2861
2862 goto out;
2863
2864handler_unregister:
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002865 batadv_recv_handler_unregister(BATADV_IV_OGM);
Marek Lindnerc3e29312012-03-04 16:56:25 +08002866out:
2867 return ret;
Marek Lindner1c280472011-11-28 17:40:17 +08002868}