blob: a7c8dd7ae51314f3fd729484300b0796238a87c8 [file] [log] [blame]
Sven Eckelmann7db7d9f2017-11-19 15:05:11 +01001// SPDX-License-Identifier: GPL-2.0
Sven Eckelmann68e039f2020-01-01 00:00:01 +01002/* Copyright (C) 2007-2020 B.A.T.M.A.N. contributors:
Marek Lindnerfc957272011-07-30 12:04:12 +02003 *
4 * Marek Lindner, Simon Wunderlich
Marek Lindnerfc957272011-07-30 12:04:12 +02005 */
6
Sven Eckelmanna2d08162016-05-15 11:07:46 +02007#include "bat_iv_ogm.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +02008#include "main.h"
9
10#include <linux/atomic.h>
11#include <linux/bitmap.h>
12#include <linux/bitops.h>
13#include <linux/bug.h>
14#include <linux/byteorder/generic.h>
15#include <linux/cache.h>
16#include <linux/errno.h>
17#include <linux/etherdevice.h>
Sven Eckelmannb92b94a2017-11-19 17:12:02 +010018#include <linux/gfp.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020019#include <linux/if_ether.h>
20#include <linux/init.h>
21#include <linux/jiffies.h>
Antonio Quartullif0d97252016-05-03 01:45:34 +080022#include <linux/kernel.h>
Sven Eckelmann77ae32e2016-01-16 10:29:53 +010023#include <linux/kref.h>
Sven Eckelmannfcafa5e2016-05-15 11:07:42 +020024#include <linux/list.h>
Sven Eckelmann40e220b2019-10-03 17:02:01 +020025#include <linux/lockdep.h>
26#include <linux/mutex.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020027#include <linux/netdevice.h>
Matthias Schiffer024f99c2016-07-03 13:31:40 +020028#include <linux/netlink.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020029#include <linux/pkt_sched.h>
30#include <linux/printk.h>
31#include <linux/random.h>
32#include <linux/rculist.h>
33#include <linux/rcupdate.h>
34#include <linux/seq_file.h>
35#include <linux/skbuff.h>
36#include <linux/slab.h>
37#include <linux/spinlock.h>
38#include <linux/stddef.h>
39#include <linux/string.h>
40#include <linux/types.h>
41#include <linux/workqueue.h>
Matthias Schiffer024f99c2016-07-03 13:31:40 +020042#include <net/genetlink.h>
43#include <net/netlink.h>
Sven Eckelmannfec149f2017-12-21 10:17:41 +010044#include <uapi/linux/batadv_packet.h>
Matthias Schiffer024f99c2016-07-03 13:31:40 +020045#include <uapi/linux/batman_adv.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020046
Sven Eckelmanna2d08162016-05-15 11:07:46 +020047#include "bat_algo.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020048#include "bitarray.h"
Antonio Quartulli34d99cf2016-07-03 12:46:33 +020049#include "gateway_client.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020050#include "hard-interface.h"
51#include "hash.h"
Sven Eckelmannba412082016-05-15 23:48:31 +020052#include "log.h"
Matthias Schiffer024f99c2016-07-03 13:31:40 +020053#include "netlink.h"
Martin Hundebølld56b1702013-01-25 11:12:39 +010054#include "network-coding.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020055#include "originator.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020056#include "routing.h"
57#include "send.h"
58#include "translation-table.h"
Markus Pargmann1f8dce42016-05-15 11:07:43 +020059#include "tvlv.h"
Marek Lindnerfc957272011-07-30 12:04:12 +020060
Antonio Quartullif0d97252016-05-03 01:45:34 +080061static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work);
62
Antonio Quartulli791c2a22013-08-17 12:44:44 +020063/**
Martin Hundebøll95298a92014-07-15 09:41:05 +020064 * enum batadv_dup_status - duplicate status
Antonio Quartulli791c2a22013-08-17 12:44:44 +020065 */
66enum batadv_dup_status {
Sven Eckelmann8b84cc42017-12-02 19:51:48 +010067 /** @BATADV_NO_DUP: the packet is no duplicate */
Antonio Quartulli791c2a22013-08-17 12:44:44 +020068 BATADV_NO_DUP = 0,
Sven Eckelmann8b84cc42017-12-02 19:51:48 +010069
70 /**
71 * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for
72 * the neighbor)
73 */
Antonio Quartulli791c2a22013-08-17 12:44:44 +020074 BATADV_ORIG_DUP,
Sven Eckelmann8b84cc42017-12-02 19:51:48 +010075
76 /** @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor */
Antonio Quartulli791c2a22013-08-17 12:44:44 +020077 BATADV_NEIGH_DUP,
Sven Eckelmann8b84cc42017-12-02 19:51:48 +010078
79 /**
80 * @BATADV_PROTECTED: originator is currently protected (after reboot)
81 */
Antonio Quartulli791c2a22013-08-17 12:44:44 +020082 BATADV_PROTECTED,
83};
84
Antonio Quartulli24a5dee2013-04-08 09:38:12 +020085/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +010086 * batadv_ring_buffer_set() - update the ring buffer with the given value
Antonio Quartulli24a5dee2013-04-08 09:38:12 +020087 * @lq_recv: pointer to the ring buffer
88 * @lq_index: index to store the value at
89 * @value: value to store in the ring buffer
90 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020091static void batadv_ring_buffer_set(u8 lq_recv[], u8 *lq_index, u8 value)
Antonio Quartulli24a5dee2013-04-08 09:38:12 +020092{
93 lq_recv[*lq_index] = value;
94 *lq_index = (*lq_index + 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE;
95}
96
97/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +010098 * batadv_ring_buffer_avg() - compute the average of all non-zero values stored
Antonio Quartulli24a5dee2013-04-08 09:38:12 +020099 * in the given ring buffer
100 * @lq_recv: pointer to the ring buffer
101 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200102 * Return: computed average value.
Antonio Quartulli24a5dee2013-04-08 09:38:12 +0200103 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200104static u8 batadv_ring_buffer_avg(const u8 lq_recv[])
Antonio Quartulli24a5dee2013-04-08 09:38:12 +0200105{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200106 const u8 *ptr;
107 u16 count = 0;
108 u16 i = 0;
109 u16 sum = 0;
Antonio Quartulli24a5dee2013-04-08 09:38:12 +0200110
111 ptr = lq_recv;
112
113 while (i < BATADV_TQ_GLOBAL_WINDOW_SIZE) {
114 if (*ptr != 0) {
115 count++;
116 sum += *ptr;
117 }
118
119 i++;
120 ptr++;
121 }
122
123 if (count == 0)
124 return 0;
125
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200126 return (u8)(sum / count);
Antonio Quartulli24a5dee2013-04-08 09:38:12 +0200127}
David S. Millerd98cae64e2013-06-19 16:49:39 -0700128
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200129/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100130 * batadv_iv_ogm_orig_get() - retrieve or create (if does not exist) an
131 * originator
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200132 * @bat_priv: the bat priv with all the soft interface information
133 * @addr: mac address of the originator
134 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200135 * Return: the originator object corresponding to the passed mac address or NULL
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200136 * on failure.
137 * If the object does not exists it is created an initialised.
138 */
139static struct batadv_orig_node *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200140batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const u8 *addr)
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200141{
142 struct batadv_orig_node *orig_node;
Sven Eckelmannf22e0892017-12-26 15:14:01 +0100143 int hash_added;
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200144
145 orig_node = batadv_orig_hash_find(bat_priv, addr);
146 if (orig_node)
147 return orig_node;
148
149 orig_node = batadv_orig_node_new(bat_priv, addr);
150 if (!orig_node)
151 return NULL;
152
153 spin_lock_init(&orig_node->bat_iv.ogm_cnt_lock);
154
Sven Eckelmann55db2d52016-07-15 17:39:21 +0200155 kref_get(&orig_node->refcount);
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200156 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
157 batadv_choose_orig, orig_node,
158 &orig_node->hash_entry);
159 if (hash_added != 0)
Sven Eckelmann55db2d52016-07-15 17:39:21 +0200160 goto free_orig_node_hash;
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200161
162 return orig_node;
163
Sven Eckelmann55db2d52016-07-15 17:39:21 +0200164free_orig_node_hash:
Sven Eckelmanndee222c72018-08-16 16:54:45 +0200165 /* reference for batadv_hash_add */
Sven Eckelmann5d967312016-01-17 11:01:09 +0100166 batadv_orig_node_put(orig_node);
Sven Eckelmanndee222c72018-08-16 16:54:45 +0200167 /* reference from batadv_orig_node_new */
Sven Eckelmann5d967312016-01-17 11:01:09 +0100168 batadv_orig_node_put(orig_node);
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200169
170 return NULL;
171}
172
Sven Eckelmann56303d32012-06-05 22:31:31 +0200173static struct batadv_neigh_node *
174batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200175 const u8 *neigh_addr,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200176 struct batadv_orig_node *orig_node,
Antonio Quartulli863dd7a2013-03-25 13:49:46 +0100177 struct batadv_orig_node *orig_neigh)
Marek Lindner7ae8b282012-03-01 15:35:21 +0800178{
Marek Lindner741aa062015-07-26 04:57:43 +0800179 struct batadv_neigh_node *neigh_node;
Marek Lindner7ae8b282012-03-01 15:35:21 +0800180
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800181 neigh_node = batadv_neigh_node_get_or_create(orig_node,
182 hard_iface, neigh_addr);
Marek Lindner7ae8b282012-03-01 15:35:21 +0800183 if (!neigh_node)
184 goto out;
185
Simon Wunderlich89652332013-11-13 19:14:46 +0100186 neigh_node->orig_node = orig_neigh;
Marek Lindner7ae8b282012-03-01 15:35:21 +0800187
Marek Lindner7ae8b282012-03-01 15:35:21 +0800188out:
189 return neigh_node;
190}
191
Sven Eckelmann56303d32012-06-05 22:31:31 +0200192static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200193{
Sven Eckelmann96412692012-06-05 22:31:30 +0200194 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindner14511512012-08-02 17:20:26 +0200195 unsigned char *ogm_buff;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200196 u32 random_seqno;
Marek Lindnerd7d32ec2012-02-07 17:20:46 +0800197
Sven Eckelmann40e220b2019-10-03 17:02:01 +0200198 mutex_lock(&hard_iface->bat_iv.ogm_buff_mutex);
199
Marek Lindnerd7d32ec2012-02-07 17:20:46 +0800200 /* randomize initial seqno to avoid collision */
201 get_random_bytes(&random_seqno, sizeof(random_seqno));
Marek Lindner14511512012-08-02 17:20:26 +0200202 atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno);
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200203
Marek Lindner14511512012-08-02 17:20:26 +0200204 hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN;
205 ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC);
Sven Eckelmann40e220b2019-10-03 17:02:01 +0200206 if (!ogm_buff) {
207 mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex);
Markus Pargmann42d9f2c2014-12-26 12:41:26 +0100208 return -ENOMEM;
Sven Eckelmann40e220b2019-10-03 17:02:01 +0200209 }
Marek Lindner77af7572012-02-07 17:20:48 +0800210
Marek Lindner14511512012-08-02 17:20:26 +0200211 hard_iface->bat_iv.ogm_buff = ogm_buff;
212
213 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100214 batadv_ogm_packet->packet_type = BATADV_IV_OGM;
215 batadv_ogm_packet->version = BATADV_COMPAT_VERSION;
216 batadv_ogm_packet->ttl = 2;
Sven Eckelmann96412692012-06-05 22:31:30 +0200217 batadv_ogm_packet->flags = BATADV_NO_FLAGS;
Marek Lindner414254e2013-04-23 21:39:58 +0800218 batadv_ogm_packet->reserved = 0;
Sven Eckelmann96412692012-06-05 22:31:30 +0200219 batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE;
Marek Lindner77af7572012-02-07 17:20:48 +0800220
Sven Eckelmann40e220b2019-10-03 17:02:01 +0200221 mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex);
222
Markus Pargmann42d9f2c2014-12-26 12:41:26 +0100223 return 0;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200224}
225
Sven Eckelmann56303d32012-06-05 22:31:31 +0200226static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
Marek Lindner00a50072012-02-07 17:20:47 +0800227{
Sven Eckelmann40e220b2019-10-03 17:02:01 +0200228 mutex_lock(&hard_iface->bat_iv.ogm_buff_mutex);
229
Marek Lindner14511512012-08-02 17:20:26 +0200230 kfree(hard_iface->bat_iv.ogm_buff);
231 hard_iface->bat_iv.ogm_buff = NULL;
Sven Eckelmann40e220b2019-10-03 17:02:01 +0200232
233 mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex);
Marek Lindner00a50072012-02-07 17:20:47 +0800234}
235
Sven Eckelmann56303d32012-06-05 22:31:31 +0200236static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface)
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200237{
Sven Eckelmann96412692012-06-05 22:31:30 +0200238 struct batadv_ogm_packet *batadv_ogm_packet;
Sven Eckelmann40e220b2019-10-03 17:02:01 +0200239 void *ogm_buff;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200240
Sven Eckelmann40e220b2019-10-03 17:02:01 +0200241 mutex_lock(&hard_iface->bat_iv.ogm_buff_mutex);
242
243 ogm_buff = hard_iface->bat_iv.ogm_buff;
244 if (!ogm_buff)
245 goto unlock;
246
247 batadv_ogm_packet = ogm_buff;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100248 ether_addr_copy(batadv_ogm_packet->orig,
249 hard_iface->net_dev->dev_addr);
250 ether_addr_copy(batadv_ogm_packet->prev_sender,
251 hard_iface->net_dev->dev_addr);
Sven Eckelmann40e220b2019-10-03 17:02:01 +0200252
253unlock:
254 mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex);
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200255}
256
Sven Eckelmann56303d32012-06-05 22:31:31 +0200257static void
258batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface)
Marek Lindnerc3229392012-03-11 06:17:50 +0800259{
Sven Eckelmann96412692012-06-05 22:31:30 +0200260 struct batadv_ogm_packet *batadv_ogm_packet;
Sven Eckelmann40e220b2019-10-03 17:02:01 +0200261 void *ogm_buff;
Marek Lindnerc3229392012-03-11 06:17:50 +0800262
Sven Eckelmann40e220b2019-10-03 17:02:01 +0200263 mutex_lock(&hard_iface->bat_iv.ogm_buff_mutex);
264
265 ogm_buff = hard_iface->bat_iv.ogm_buff;
266 if (!ogm_buff)
267 goto unlock;
268
269 batadv_ogm_packet = ogm_buff;
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100270 batadv_ogm_packet->ttl = BATADV_TTL;
Sven Eckelmann40e220b2019-10-03 17:02:01 +0200271
272unlock:
273 mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex);
Marek Lindnerc3229392012-03-11 06:17:50 +0800274}
275
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200276/* when do we schedule our own ogm to be sent */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200277static unsigned long
Sven Eckelmann56303d32012-06-05 22:31:31 +0200278batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200279{
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200280 unsigned int msecs;
281
282 msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
Akinobu Mitae76e4322012-12-24 11:14:07 +0900283 msecs += prandom_u32() % (2 * BATADV_JITTER);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200284
285 return jiffies + msecs_to_jiffies(msecs);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200286}
287
288/* when do we schedule a ogm packet to be sent */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200289static unsigned long batadv_iv_ogm_fwd_send_time(void)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200290{
Akinobu Mitae76e4322012-12-24 11:14:07 +0900291 return jiffies + msecs_to_jiffies(prandom_u32() % (BATADV_JITTER / 2));
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200292}
293
294/* apply hop penalty for a normal link */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200295static u8 batadv_hop_penalty(u8 tq, const struct batadv_priv *bat_priv)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200296{
297 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200298 int new_tq;
299
300 new_tq = tq * (BATADV_TQ_MAX_VALUE - hop_penalty);
301 new_tq /= BATADV_TQ_MAX_VALUE;
302
303 return new_tq;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200304}
305
Simon Wunderlich600184b2015-11-23 19:57:21 +0100306/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100307 * batadv_iv_ogm_aggr_packet() - checks if there is another OGM attached
Simon Wunderlich600184b2015-11-23 19:57:21 +0100308 * @buff_pos: current position in the skb
309 * @packet_len: total length of the skb
Sven Eckelmanna15d56a2019-08-22 08:55:36 +0200310 * @ogm_packet: potential OGM in buffer
Simon Wunderlich600184b2015-11-23 19:57:21 +0100311 *
312 * Return: true if there is enough space for another OGM, false otherwise.
313 */
Sven Eckelmanna15d56a2019-08-22 08:55:36 +0200314static bool
315batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len,
316 const struct batadv_ogm_packet *ogm_packet)
Marek Lindnerfc957272011-07-30 12:04:12 +0200317{
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200318 int next_buff_pos = 0;
319
Sven Eckelmanna15d56a2019-08-22 08:55:36 +0200320 /* check if there is enough space for the header */
321 next_buff_pos += buff_pos + sizeof(*ogm_packet);
322 if (next_buff_pos > packet_len)
323 return false;
324
325 /* check if there is enough space for the optional TVLV */
326 next_buff_pos += ntohs(ogm_packet->tvlv_len);
Marek Lindnerfc957272011-07-30 12:04:12 +0200327
328 return (next_buff_pos <= packet_len) &&
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200329 (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
Marek Lindnerfc957272011-07-30 12:04:12 +0200330}
331
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200332/* send a batman ogm to a given interface */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200333static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet,
334 struct batadv_hard_iface *hard_iface)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200335{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200336 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Markus Pargmannde12bae2014-12-26 12:41:28 +0100337 const char *fwd_str;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200338 u8 packet_num;
339 s16 buff_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +0200340 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200341 struct sk_buff *skb;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200342 u8 *packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200343
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200344 if (hard_iface->if_status != BATADV_IF_ACTIVE)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200345 return;
346
347 packet_num = 0;
348 buff_pos = 0;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200349 packet_pos = forw_packet->skb->data;
350 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200351
352 /* adjust all flags and log packets */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200353 while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
Sven Eckelmanna15d56a2019-08-22 08:55:36 +0200354 batadv_ogm_packet)) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200355 /* we might have aggregated direct link packets with an
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200356 * ordinary base packet
357 */
Sven Eckelmann8de47de2012-07-08 16:32:09 +0200358 if (forw_packet->direct_link_flags & BIT(packet_num) &&
359 forw_packet->if_incoming == hard_iface)
Sven Eckelmann96412692012-06-05 22:31:30 +0200360 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200361 else
Sven Eckelmann96412692012-06-05 22:31:30 +0200362 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200363
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200364 if (packet_num > 0 || !forw_packet->own)
365 fwd_str = "Forwarding";
366 else
367 fwd_str = "Sending own";
368
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200369 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800370 "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200371 fwd_str, (packet_num > 0 ? "aggregated " : ""),
Sven Eckelmann96412692012-06-05 22:31:30 +0200372 batadv_ogm_packet->orig,
373 ntohl(batadv_ogm_packet->seqno),
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100374 batadv_ogm_packet->tq, batadv_ogm_packet->ttl,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +0200375 ((batadv_ogm_packet->flags & BATADV_DIRECTLINK) ?
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200376 "on" : "off"),
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800377 hard_iface->net_dev->name,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200378 hard_iface->net_dev->dev_addr);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200379
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200380 buff_pos += BATADV_OGM_HLEN;
Marek Lindneref261572013-04-23 21:39:57 +0800381 buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200382 packet_num++;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200383 packet_pos = forw_packet->skb->data + buff_pos;
384 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200385 }
386
387 /* create clone because function is called more than once */
388 skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
Martin Hundebøllf8214862012-04-20 17:02:45 +0200389 if (skb) {
Sven Eckelmannd69909d2012-06-03 22:19:20 +0200390 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
391 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
Martin Hundebøllf8214862012-04-20 17:02:45 +0200392 skb->len + ETH_HLEN);
Antonio Quartulli95d39272016-01-16 16:40:15 +0800393 batadv_send_broadcast_skb(skb, hard_iface);
Martin Hundebøllf8214862012-04-20 17:02:45 +0200394 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200395}
396
397/* send a batman ogm packet */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200398static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200399{
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200400 struct net_device *soft_iface;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200401
402 if (!forw_packet->if_incoming) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100403 pr_err("Error - can't forward packet: incoming iface not specified\n");
Linus LĂĽssingf55a2e82016-06-14 22:56:50 +0200404 return;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200405 }
406
407 soft_iface = forw_packet->if_incoming->soft_iface;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200408
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100409 if (WARN_ON(!forw_packet->if_outgoing))
Linus LĂĽssingf55a2e82016-06-14 22:56:50 +0200410 return;
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100411
412 if (WARN_ON(forw_packet->if_outgoing->soft_iface != soft_iface))
Linus LĂĽssingf55a2e82016-06-14 22:56:50 +0200413 return;
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100414
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200415 if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE)
Linus LĂĽssingf55a2e82016-06-14 22:56:50 +0200416 return;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200417
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100418 /* only for one specific outgoing interface */
419 batadv_iv_ogm_send_to_if(forw_packet, forw_packet->if_outgoing);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200420}
421
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100422/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100423 * batadv_iv_ogm_can_aggregate() - find out if an OGM can be aggregated on an
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100424 * existing forward packet
425 * @new_bat_ogm_packet: OGM packet to be aggregated
426 * @bat_priv: the bat priv with all the soft interface information
427 * @packet_len: (total) length of the OGM
428 * @send_time: timestamp (jiffies) when the packet is to be sent
Martin Hundebøll95298a92014-07-15 09:41:05 +0200429 * @directlink: true if this is a direct link packet
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100430 * @if_incoming: interface where the packet was received
431 * @if_outgoing: interface for which the retransmission should be considered
432 * @forw_packet: the forwarded packet which should be checked
433 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200434 * Return: true if new_packet can be aggregated with forw_packet
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100435 */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200436static bool
Sven Eckelmann96412692012-06-05 22:31:30 +0200437batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200438 struct batadv_priv *bat_priv,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200439 int packet_len, unsigned long send_time,
440 bool directlink,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200441 const struct batadv_hard_iface *if_incoming,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100442 const struct batadv_hard_iface *if_outgoing,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200443 const struct batadv_forw_packet *forw_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200444{
Sven Eckelmann96412692012-06-05 22:31:30 +0200445 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200446 int aggregated_bytes = forw_packet->packet_len + packet_len;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200447 struct batadv_hard_iface *primary_if = NULL;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200448 bool res = false;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200449 unsigned long aggregation_end_time;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200450
Sven Eckelmann96412692012-06-05 22:31:30 +0200451 batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200452 aggregation_end_time = send_time;
453 aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200454
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200455 /* we can aggregate the current packet to this aggregated packet
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200456 * if:
457 *
458 * - the send time is within our MAX_AGGREGATION_MS time
459 * - the resulting packet wont be bigger than
460 * MAX_AGGREGATION_BYTES
Markus Pargmann8f34b382014-12-26 12:41:29 +0100461 * otherwise aggregation is not possible
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200462 */
Markus Pargmann8f34b382014-12-26 12:41:29 +0100463 if (!time_before(send_time, forw_packet->send_time) ||
464 !time_after_eq(aggregation_end_time, forw_packet->send_time))
465 return false;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200466
Markus Pargmann8f34b382014-12-26 12:41:29 +0100467 if (aggregated_bytes > BATADV_MAX_AGGREGATION_BYTES)
468 return false;
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100469
Markus Pargmann8f34b382014-12-26 12:41:29 +0100470 /* packet is not leaving on the same interface. */
471 if (forw_packet->if_outgoing != if_outgoing)
472 return false;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200473
Markus Pargmann8f34b382014-12-26 12:41:29 +0100474 /* check aggregation compatibility
475 * -> direct link packets are broadcasted on
476 * their interface only
477 * -> aggregate packet if the current packet is
478 * a "global" packet as well as the base
479 * packet
480 */
481 primary_if = batadv_primary_if_get_selected(bat_priv);
482 if (!primary_if)
483 return false;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200484
Markus Pargmann8f34b382014-12-26 12:41:29 +0100485 /* packets without direct link flag and high TTL
486 * are flooded through the net
487 */
488 if (!directlink &&
489 !(batadv_ogm_packet->flags & BATADV_DIRECTLINK) &&
490 batadv_ogm_packet->ttl != 1 &&
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200491
Markus Pargmann8f34b382014-12-26 12:41:29 +0100492 /* own packets originating non-primary
493 * interfaces leave only that interface
494 */
495 (!forw_packet->own ||
496 forw_packet->if_incoming == primary_if)) {
497 res = true;
498 goto out;
499 }
500
501 /* if the incoming packet is sent via this one
502 * interface only - we still can aggregate
503 */
504 if (directlink &&
505 new_bat_ogm_packet->ttl == 1 &&
506 forw_packet->if_incoming == if_incoming &&
507
508 /* packets from direct neighbors or
509 * own secondary interface packets
510 * (= secondary interface packets in general)
511 */
512 (batadv_ogm_packet->flags & BATADV_DIRECTLINK ||
513 (forw_packet->own &&
514 forw_packet->if_incoming != primary_if))) {
515 res = true;
516 goto out;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200517 }
518
519out:
520 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100521 batadv_hardif_put(primary_if);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200522 return res;
523}
524
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100525/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100526 * batadv_iv_ogm_aggregate_new() - create a new aggregated packet and add this
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100527 * packet to it.
528 * @packet_buff: pointer to the OGM
529 * @packet_len: (total) length of the OGM
530 * @send_time: timestamp (jiffies) when the packet is to be sent
531 * @direct_link: whether this OGM has direct link status
532 * @if_incoming: interface where the packet was received
533 * @if_outgoing: interface for which the retransmission should be considered
534 * @own_packet: true if it is a self-generated ogm
535 */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200536static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,
537 int packet_len, unsigned long send_time,
538 bool direct_link,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200539 struct batadv_hard_iface *if_incoming,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100540 struct batadv_hard_iface *if_outgoing,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200541 int own_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200542{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200543 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
544 struct batadv_forw_packet *forw_packet_aggr;
Linus LĂĽssing99ba18e2017-02-17 11:17:06 +0100545 struct sk_buff *skb;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200546 unsigned char *skb_buff;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200547 unsigned int skb_size;
Linus LĂĽssinga65e5482016-06-20 21:39:54 +0200548 atomic_t *queue_left = own_packet ? NULL : &bat_priv->batman_queue_left;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200549
Markus Pargmann940d1562014-12-26 12:41:31 +0100550 if (atomic_read(&bat_priv->aggregated_ogms) &&
551 packet_len < BATADV_MAX_AGGREGATION_BYTES)
Sven Eckelmann5b246572012-11-04 17:11:45 +0100552 skb_size = BATADV_MAX_AGGREGATION_BYTES;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200553 else
Sven Eckelmann5b246572012-11-04 17:11:45 +0100554 skb_size = packet_len;
555
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200556 skb_size += ETH_HLEN;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200557
Linus LĂĽssing99ba18e2017-02-17 11:17:06 +0100558 skb = netdev_alloc_skb_ip_align(NULL, skb_size);
559 if (!skb)
560 return;
561
562 forw_packet_aggr = batadv_forw_packet_alloc(if_incoming, if_outgoing,
563 queue_left, bat_priv, skb);
564 if (!forw_packet_aggr) {
565 kfree_skb(skb);
Linus LĂĽssinga65e5482016-06-20 21:39:54 +0200566 return;
567 }
568
Simon Wunderlichc54f38c2013-07-29 17:56:44 +0200569 forw_packet_aggr->skb->priority = TC_PRIO_CONTROL;
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200570 skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200571
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200572 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
573 forw_packet_aggr->packet_len = packet_len;
574 memcpy(skb_buff, packet_buff, packet_len);
575
576 forw_packet_aggr->own = own_packet;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200577 forw_packet_aggr->direct_link_flags = BATADV_NO_FLAGS;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200578 forw_packet_aggr->send_time = send_time;
579
580 /* save packet direct link flag status */
581 if (direct_link)
582 forw_packet_aggr->direct_link_flags |= 1;
583
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200584 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
Antonio Quartullif0d97252016-05-03 01:45:34 +0800585 batadv_iv_send_outstanding_bat_ogm_packet);
Linus LĂĽssing9b4aec62016-11-01 09:44:44 +0100586
587 batadv_forw_packet_ogmv1_queue(bat_priv, forw_packet_aggr, send_time);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200588}
589
590/* aggregate a new packet into the existing ogm packet */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200591static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200592 const unsigned char *packet_buff,
593 int packet_len, bool direct_link)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200594{
Sven Eckelmann8de47de2012-07-08 16:32:09 +0200595 unsigned long new_direct_link_flag;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200596
Sven Eckelmanne04de482017-06-18 09:59:28 +0200597 skb_put_data(forw_packet_aggr->skb, packet_buff, packet_len);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200598 forw_packet_aggr->packet_len += packet_len;
599 forw_packet_aggr->num_packets++;
600
601 /* save packet direct link flag status */
Sven Eckelmann8de47de2012-07-08 16:32:09 +0200602 if (direct_link) {
603 new_direct_link_flag = BIT(forw_packet_aggr->num_packets);
604 forw_packet_aggr->direct_link_flags |= new_direct_link_flag;
605 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200606}
607
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100608/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100609 * batadv_iv_ogm_queue_add() - queue up an OGM for transmission
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100610 * @bat_priv: the bat priv with all the soft interface information
611 * @packet_buff: pointer to the OGM
612 * @packet_len: (total) length of the OGM
613 * @if_incoming: interface where the packet was received
614 * @if_outgoing: interface for which the retransmission should be considered
615 * @own_packet: true if it is a self-generated ogm
616 * @send_time: timestamp (jiffies) when the packet is to be sent
617 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200618static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200619 unsigned char *packet_buff,
620 int packet_len,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200621 struct batadv_hard_iface *if_incoming,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100622 struct batadv_hard_iface *if_outgoing,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200623 int own_packet, unsigned long send_time)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200624{
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200625 /* _aggr -> pointer to the packet we want to aggregate with
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200626 * _pos -> pointer to the position in the queue
627 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200628 struct batadv_forw_packet *forw_packet_aggr = NULL;
629 struct batadv_forw_packet *forw_packet_pos = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200630 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200631 bool direct_link;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200632 unsigned long max_aggregation_jiffies;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200633
Sven Eckelmann96412692012-06-05 22:31:30 +0200634 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
Markus Pargmann56489152014-12-26 12:41:32 +0100635 direct_link = !!(batadv_ogm_packet->flags & BATADV_DIRECTLINK);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200636 max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200637
638 /* find position for the packet in the forward queue */
639 spin_lock_bh(&bat_priv->forw_bat_list_lock);
640 /* own packets are not to be aggregated */
Markus Pargmann56489152014-12-26 12:41:32 +0100641 if (atomic_read(&bat_priv->aggregated_ogms) && !own_packet) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800642 hlist_for_each_entry(forw_packet_pos,
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200643 &bat_priv->forw_bat_list, list) {
Sven Eckelmann96412692012-06-05 22:31:30 +0200644 if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200645 bat_priv, packet_len,
646 send_time, direct_link,
647 if_incoming,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100648 if_outgoing,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200649 forw_packet_pos)) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200650 forw_packet_aggr = forw_packet_pos;
651 break;
652 }
653 }
654 }
655
656 /* nothing to aggregate with - either aggregation disabled or no
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200657 * suitable aggregation packet found
658 */
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200659 if (!forw_packet_aggr) {
660 /* the following section can run without the lock */
661 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
662
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200663 /* if we could not aggregate this packet with one of the others
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200664 * we hold it back for a while, so that it might be aggregated
665 * later on
666 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200667 if (!own_packet && atomic_read(&bat_priv->aggregated_ogms))
668 send_time += max_aggregation_jiffies;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200669
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200670 batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
671 send_time, direct_link,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100672 if_incoming, if_outgoing,
673 own_packet);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200674 } else {
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200675 batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
676 packet_len, direct_link);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200677 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
678 }
679}
680
Sven Eckelmann56303d32012-06-05 22:31:31 +0200681static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200682 const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +0200683 struct batadv_ogm_packet *batadv_ogm_packet,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200684 bool is_single_hop_neigh,
685 bool is_from_best_next_hop,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100686 struct batadv_hard_iface *if_incoming,
687 struct batadv_hard_iface *if_outgoing)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200688{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200689 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200690 u16 tvlv_len;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200691
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100692 if (batadv_ogm_packet->ttl <= 1) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200693 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200694 return;
695 }
696
Marek Lindner13b25412012-03-11 06:17:53 +0800697 if (!is_from_best_next_hop) {
698 /* Mark the forwarded packet when it is not coming from our
699 * best next hop. We still need to forward the packet for our
700 * neighbor link quality detection to work in case the packet
701 * originated from a single hop neighbor. Otherwise we can
702 * simply drop the ogm.
703 */
704 if (is_single_hop_neigh)
Sven Eckelmann96412692012-06-05 22:31:30 +0200705 batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP;
Marek Lindner13b25412012-03-11 06:17:53 +0800706 else
707 return;
708 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200709
Marek Lindneref261572013-04-23 21:39:57 +0800710 tvlv_len = ntohs(batadv_ogm_packet->tvlv_len);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200711
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100712 batadv_ogm_packet->ttl--;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100713 ether_addr_copy(batadv_ogm_packet->prev_sender, ethhdr->h_source);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200714
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200715 /* apply hop penalty */
Sven Eckelmann96412692012-06-05 22:31:30 +0200716 batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200717 bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200718
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200719 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200720 "Forwarding packet: tq: %i, ttl: %i\n",
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100721 batadv_ogm_packet->tq, batadv_ogm_packet->ttl);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200722
Marek Lindner75cd33f2012-03-01 15:35:16 +0800723 if (is_single_hop_neigh)
Sven Eckelmann96412692012-06-05 22:31:30 +0200724 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200725 else
Sven Eckelmann96412692012-06-05 22:31:30 +0200726 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200727
Sven Eckelmann96412692012-06-05 22:31:30 +0200728 batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
Marek Lindneref261572013-04-23 21:39:57 +0800729 BATADV_OGM_HLEN + tvlv_len,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100730 if_incoming, if_outgoing, 0,
731 batadv_iv_ogm_fwd_send_time());
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200732}
733
Antonio Quartullid9896612013-04-17 17:44:43 +0200734/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100735 * batadv_iv_ogm_slide_own_bcast_window() - bitshift own OGM broadcast windows
736 * for the given interface
Antonio Quartullid9896612013-04-17 17:44:43 +0200737 * @hard_iface: the interface for which the windows have to be shifted
738 */
739static void
740batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
741{
742 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
743 struct batadv_hashtable *hash = bat_priv->orig_hash;
744 struct hlist_head *head;
745 struct batadv_orig_node *orig_node;
Sven Eckelmanndee222c72018-08-16 16:54:45 +0200746 struct batadv_orig_ifinfo *orig_ifinfo;
Antonio Quartullid9896612013-04-17 17:44:43 +0200747 unsigned long *word;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200748 u32 i;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200749 u8 *w;
Antonio Quartullid9896612013-04-17 17:44:43 +0200750
751 for (i = 0; i < hash->size; i++) {
752 head = &hash->table[i];
753
754 rcu_read_lock();
755 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Sven Eckelmanndee222c72018-08-16 16:54:45 +0200756 hlist_for_each_entry_rcu(orig_ifinfo,
757 &orig_node->ifinfo_list,
758 list) {
759 if (orig_ifinfo->if_outgoing != hard_iface)
760 continue;
Antonio Quartullid9896612013-04-17 17:44:43 +0200761
Sven Eckelmanndee222c72018-08-16 16:54:45 +0200762 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
763 word = orig_ifinfo->bat_iv.bcast_own;
764 batadv_bit_get_packet(bat_priv, word, 1, 0);
765 w = &orig_ifinfo->bat_iv.bcast_own_sum;
766 *w = bitmap_weight(word,
767 BATADV_TQ_LOCAL_WINDOW_SIZE);
768 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
769 }
Antonio Quartullid9896612013-04-17 17:44:43 +0200770 }
771 rcu_read_unlock();
772 }
773}
774
Sven Eckelmann40e220b2019-10-03 17:02:01 +0200775/**
776 * batadv_iv_ogm_schedule_buff() - schedule submission of hardif ogm buffer
777 * @hard_iface: interface whose ogm buffer should be transmitted
778 */
779static void batadv_iv_ogm_schedule_buff(struct batadv_hard_iface *hard_iface)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200780{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200781 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindner14511512012-08-02 17:20:26 +0200782 unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff;
Sven Eckelmann96412692012-06-05 22:31:30 +0200783 struct batadv_ogm_packet *batadv_ogm_packet;
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100784 struct batadv_hard_iface *primary_if, *tmp_hard_iface;
Marek Lindner14511512012-08-02 17:20:26 +0200785 int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200786 u32 seqno;
787 u16 tvlv_len = 0;
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100788 unsigned long send_time;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200789
Sven Eckelmann40e220b2019-10-03 17:02:01 +0200790 lockdep_assert_held(&hard_iface->bat_iv.ogm_buff_mutex);
Antonio Quartullif0d97252016-05-03 01:45:34 +0800791
Sven Eckelmann8e8ce082020-02-16 13:02:06 +0100792 /* interface already disabled by batadv_iv_ogm_iface_disable */
793 if (!*ogm_buff)
794 return;
795
Antonio Quartullif0d97252016-05-03 01:45:34 +0800796 /* the interface gets activated here to avoid race conditions between
797 * the moment of activating the interface in
798 * hardif_activate_interface() where the originator mac is set and
799 * outdated packets (especially uninitialized mac addresses) in the
800 * packet queue
801 */
802 if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED)
803 hard_iface->if_status = BATADV_IF_ACTIVE;
804
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200805 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200806
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800807 if (hard_iface == primary_if) {
808 /* tt changes have to be committed before the tvlv data is
809 * appended as it may alter the tt tvlv container
810 */
811 batadv_tt_local_commit_changes(bat_priv);
Marek Lindneref261572013-04-23 21:39:57 +0800812 tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff,
813 ogm_buff_len,
814 BATADV_OGM_HLEN);
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800815 }
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800816
Marek Lindner14511512012-08-02 17:20:26 +0200817 batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff);
Marek Lindneref261572013-04-23 21:39:57 +0800818 batadv_ogm_packet->tvlv_len = htons(tvlv_len);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200819
820 /* change sequence number to network order */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200821 seqno = (u32)atomic_read(&hard_iface->bat_iv.ogm_seqno);
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200822 batadv_ogm_packet->seqno = htonl(seqno);
Marek Lindner14511512012-08-02 17:20:26 +0200823 atomic_inc(&hard_iface->bat_iv.ogm_seqno);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200824
Antonio Quartullid9896612013-04-17 17:44:43 +0200825 batadv_iv_ogm_slide_own_bcast_window(hard_iface);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200826
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100827 send_time = batadv_iv_ogm_emit_send_time(bat_priv);
828
829 if (hard_iface != primary_if) {
830 /* OGMs from secondary interfaces are only scheduled on their
831 * respective interfaces.
832 */
833 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff, *ogm_buff_len,
834 hard_iface, hard_iface, 1, send_time);
835 goto out;
836 }
837
838 /* OGMs from primary interfaces are scheduled on all
839 * interfaces.
840 */
841 rcu_read_lock();
842 list_for_each_entry_rcu(tmp_hard_iface, &batadv_hardif_list, list) {
843 if (tmp_hard_iface->soft_iface != hard_iface->soft_iface)
Sven Eckelmann87b40f52015-07-17 10:03:42 +0200844 continue;
Sven Eckelmann27353442016-03-05 16:09:16 +0100845
846 if (!kref_get_unless_zero(&tmp_hard_iface->refcount))
847 continue;
848
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100849 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff,
850 *ogm_buff_len, hard_iface,
851 tmp_hard_iface, 1, send_time);
Sven Eckelmann27353442016-03-05 16:09:16 +0100852
853 batadv_hardif_put(tmp_hard_iface);
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100854 }
855 rcu_read_unlock();
856
857out:
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200858 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100859 batadv_hardif_put(primary_if);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200860}
861
Sven Eckelmann40e220b2019-10-03 17:02:01 +0200862static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
863{
864 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE ||
865 hard_iface->if_status == BATADV_IF_TO_BE_REMOVED)
866 return;
867
868 mutex_lock(&hard_iface->bat_iv.ogm_buff_mutex);
869 batadv_iv_ogm_schedule_buff(hard_iface);
870 mutex_unlock(&hard_iface->bat_iv.ogm_buff_mutex);
871}
872
Simon Wunderlich89652332013-11-13 19:14:46 +0100873/**
Sven Eckelmanndee222c72018-08-16 16:54:45 +0200874 * batadv_iv_orig_ifinfo_sum() - Get bcast_own sum for originator over iterface
875 * @orig_node: originator which reproadcasted the OGMs directly
876 * @if_outgoing: interface which transmitted the original OGM and received the
877 * direct rebroadcast
878 *
879 * Return: Number of replied (rebroadcasted) OGMs which were transmitted by
880 * an originator and directly (without intermediate hop) received by a specific
881 * interface
882 */
883static u8 batadv_iv_orig_ifinfo_sum(struct batadv_orig_node *orig_node,
884 struct batadv_hard_iface *if_outgoing)
885{
886 struct batadv_orig_ifinfo *orig_ifinfo;
887 u8 sum;
888
889 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing);
890 if (!orig_ifinfo)
891 return 0;
892
893 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
894 sum = orig_ifinfo->bat_iv.bcast_own_sum;
895 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
896
897 batadv_orig_ifinfo_put(orig_ifinfo);
898
899 return sum;
900}
901
902/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100903 * batadv_iv_ogm_orig_update() - use OGM to update corresponding data in an
Simon Wunderlich89652332013-11-13 19:14:46 +0100904 * originator
905 * @bat_priv: the bat priv with all the soft interface information
906 * @orig_node: the orig node who originally emitted the ogm packet
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100907 * @orig_ifinfo: ifinfo for the outgoing interface of the orig_node
Simon Wunderlich89652332013-11-13 19:14:46 +0100908 * @ethhdr: Ethernet header of the OGM
909 * @batadv_ogm_packet: the ogm packet
910 * @if_incoming: interface where the packet was received
911 * @if_outgoing: interface for which the retransmission should be considered
Simon Wunderlich89652332013-11-13 19:14:46 +0100912 * @dup_status: the duplicate status of this ogm packet.
913 */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200914static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200915batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
916 struct batadv_orig_node *orig_node,
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100917 struct batadv_orig_ifinfo *orig_ifinfo,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200918 const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +0200919 const struct batadv_ogm_packet *batadv_ogm_packet,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200920 struct batadv_hard_iface *if_incoming,
Simon Wunderlich89652332013-11-13 19:14:46 +0100921 struct batadv_hard_iface *if_outgoing,
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +0200922 enum batadv_dup_status dup_status)
Marek Lindnerfc957272011-07-30 12:04:12 +0200923{
Simon Wunderlich89652332013-11-13 19:14:46 +0100924 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
925 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200926 struct batadv_neigh_node *neigh_node = NULL;
927 struct batadv_neigh_node *tmp_neigh_node = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200928 struct batadv_neigh_node *router = NULL;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200929 u8 sum_orig, sum_neigh;
930 u8 *neigh_addr;
931 u8 tq_avg;
Marek Lindnerfc957272011-07-30 12:04:12 +0200932
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200933 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann22f05022017-05-19 13:02:00 +0200934 "%s(): Searching and updating originator entry of received packet\n",
935 __func__);
Marek Lindnerfc957272011-07-30 12:04:12 +0200936
937 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800938 hlist_for_each_entry_rcu(tmp_neigh_node,
Marek Lindnerfc957272011-07-30 12:04:12 +0200939 &orig_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200940 neigh_addr = tmp_neigh_node->addr;
941 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
942 tmp_neigh_node->if_incoming == if_incoming &&
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100943 kref_get_unless_zero(&tmp_neigh_node->refcount)) {
Antonio Quartulli38dc40e2013-03-30 17:22:00 +0100944 if (WARN(neigh_node, "too many matching neigh_nodes"))
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100945 batadv_neigh_node_put(neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +0200946 neigh_node = tmp_neigh_node;
947 continue;
948 }
949
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +0200950 if (dup_status != BATADV_NO_DUP)
Marek Lindnerfc957272011-07-30 12:04:12 +0200951 continue;
952
Simon Wunderlich89652332013-11-13 19:14:46 +0100953 /* only update the entry for this outgoing interface */
954 neigh_ifinfo = batadv_neigh_ifinfo_get(tmp_neigh_node,
955 if_outgoing);
956 if (!neigh_ifinfo)
957 continue;
958
959 spin_lock_bh(&tmp_neigh_node->ifinfo_lock);
960 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
961 &neigh_ifinfo->bat_iv.tq_index, 0);
962 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
963 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
964 spin_unlock_bh(&tmp_neigh_node->ifinfo_lock);
965
Sven Eckelmann044fa3a2016-01-17 11:01:12 +0100966 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +0100967 neigh_ifinfo = NULL;
Marek Lindnerfc957272011-07-30 12:04:12 +0200968 }
969
970 if (!neigh_node) {
Sven Eckelmann56303d32012-06-05 22:31:31 +0200971 struct batadv_orig_node *orig_tmp;
Marek Lindnerfc957272011-07-30 12:04:12 +0200972
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200973 orig_tmp = batadv_iv_ogm_orig_get(bat_priv, ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +0200974 if (!orig_tmp)
975 goto unlock;
976
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200977 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
978 ethhdr->h_source,
Antonio Quartulli863dd7a2013-03-25 13:49:46 +0100979 orig_node, orig_tmp);
Marek Lindnerfc957272011-07-30 12:04:12 +0200980
Sven Eckelmann5d967312016-01-17 11:01:09 +0100981 batadv_orig_node_put(orig_tmp);
Marek Lindnerfc957272011-07-30 12:04:12 +0200982 if (!neigh_node)
983 goto unlock;
Markus Pargmann23badd62014-12-26 12:41:33 +0100984 } else {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200985 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200986 "Updating existing last-hop neighbor of originator\n");
Markus Pargmann23badd62014-12-26 12:41:33 +0100987 }
Marek Lindnerfc957272011-07-30 12:04:12 +0200988
989 rcu_read_unlock();
Simon Wunderlich89652332013-11-13 19:14:46 +0100990 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
991 if (!neigh_ifinfo)
992 goto out;
Marek Lindnerfc957272011-07-30 12:04:12 +0200993
Marek Lindnerd7b2a972012-03-01 15:35:19 +0800994 neigh_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +0200995
Simon Wunderlich89652332013-11-13 19:14:46 +0100996 spin_lock_bh(&neigh_node->ifinfo_lock);
997 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
998 &neigh_ifinfo->bat_iv.tq_index,
Sven Eckelmann96412692012-06-05 22:31:30 +0200999 batadv_ogm_packet->tq);
Simon Wunderlich89652332013-11-13 19:14:46 +01001000 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
1001 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
1002 spin_unlock_bh(&neigh_node->ifinfo_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001003
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001004 if (dup_status == BATADV_NO_DUP) {
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001005 orig_ifinfo->last_ttl = batadv_ogm_packet->ttl;
Simon Wunderlich89652332013-11-13 19:14:46 +01001006 neigh_ifinfo->last_ttl = batadv_ogm_packet->ttl;
Marek Lindnerfc957272011-07-30 12:04:12 +02001007 }
1008
Marek Lindnerfc957272011-07-30 12:04:12 +02001009 /* if this neighbor already is our next hop there is nothing
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001010 * to change
1011 */
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001012 router = batadv_orig_router_get(orig_node, if_outgoing);
Marek Lindnerfc957272011-07-30 12:04:12 +02001013 if (router == neigh_node)
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001014 goto out;
Marek Lindnerfc957272011-07-30 12:04:12 +02001015
Simon Wunderlich89652332013-11-13 19:14:46 +01001016 if (router) {
1017 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1018 if (!router_ifinfo)
1019 goto out;
1020
1021 /* if this neighbor does not offer a better TQ we won't
1022 * consider it
1023 */
1024 if (router_ifinfo->bat_iv.tq_avg > neigh_ifinfo->bat_iv.tq_avg)
1025 goto out;
1026 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001027
1028 /* if the TQ is the same and the link not more symmetric we
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001029 * won't consider it either
1030 */
Simon Wunderlich89652332013-11-13 19:14:46 +01001031 if (router_ifinfo &&
Markus Pargmann01b97a32014-12-26 12:41:30 +01001032 neigh_ifinfo->bat_iv.tq_avg == router_ifinfo->bat_iv.tq_avg) {
Sven Eckelmanndee222c72018-08-16 16:54:45 +02001033 sum_orig = batadv_iv_orig_ifinfo_sum(router->orig_node,
1034 router->if_incoming);
1035 sum_neigh = batadv_iv_orig_ifinfo_sum(neigh_node->orig_node,
1036 neigh_node->if_incoming);
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001037 if (sum_orig >= sum_neigh)
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001038 goto out;
Marek Lindnerfc957272011-07-30 12:04:12 +02001039 }
1040
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001041 batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001042 goto out;
1043
1044unlock:
1045 rcu_read_unlock();
1046out:
1047 if (neigh_node)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001048 batadv_neigh_node_put(neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001049 if (router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001050 batadv_neigh_node_put(router);
Simon Wunderlich89652332013-11-13 19:14:46 +01001051 if (neigh_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001052 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01001053 if (router_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001054 batadv_neigh_ifinfo_put(router_ifinfo);
Marek Lindnerfc957272011-07-30 12:04:12 +02001055}
1056
Simon Wunderlich89652332013-11-13 19:14:46 +01001057/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001058 * batadv_iv_ogm_calc_tq() - calculate tq for current received ogm packet
Simon Wunderlich89652332013-11-13 19:14:46 +01001059 * @orig_node: the orig node who originally emitted the ogm packet
1060 * @orig_neigh_node: the orig node struct of the neighbor who sent the packet
1061 * @batadv_ogm_packet: the ogm packet
1062 * @if_incoming: interface where the packet was received
1063 * @if_outgoing: interface for which the retransmission should be considered
1064 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001065 * Return: true if the link can be considered bidirectional, false otherwise
Simon Wunderlich89652332013-11-13 19:14:46 +01001066 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001067static bool batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
1068 struct batadv_orig_node *orig_neigh_node,
1069 struct batadv_ogm_packet *batadv_ogm_packet,
1070 struct batadv_hard_iface *if_incoming,
1071 struct batadv_hard_iface *if_outgoing)
Marek Lindnerfc957272011-07-30 12:04:12 +02001072{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001073 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1074 struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
Simon Wunderlich89652332013-11-13 19:14:46 +01001075 struct batadv_neigh_ifinfo *neigh_ifinfo;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001076 u8 total_count;
1077 u8 orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001078 unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
Sven Eckelmannd285f522016-02-16 10:47:07 +01001079 unsigned int tq_asym_penalty, inv_asym_penalty;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001080 unsigned int combined_tq;
Sven Eckelmannd285f522016-02-16 10:47:07 +01001081 unsigned int tq_iface_penalty;
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001082 bool ret = false;
Marek Lindnerfc957272011-07-30 12:04:12 +02001083
1084 /* find corresponding one hop neighbor */
1085 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001086 hlist_for_each_entry_rcu(tmp_neigh_node,
Marek Lindnerfc957272011-07-30 12:04:12 +02001087 &orig_neigh_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001088 if (!batadv_compare_eth(tmp_neigh_node->addr,
1089 orig_neigh_node->orig))
Marek Lindnerfc957272011-07-30 12:04:12 +02001090 continue;
1091
1092 if (tmp_neigh_node->if_incoming != if_incoming)
1093 continue;
1094
Sven Eckelmann77ae32e2016-01-16 10:29:53 +01001095 if (!kref_get_unless_zero(&tmp_neigh_node->refcount))
Marek Lindnerfc957272011-07-30 12:04:12 +02001096 continue;
1097
1098 neigh_node = tmp_neigh_node;
1099 break;
1100 }
1101 rcu_read_unlock();
1102
1103 if (!neigh_node)
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001104 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1105 orig_neigh_node->orig,
1106 orig_neigh_node,
Antonio Quartulli863dd7a2013-03-25 13:49:46 +01001107 orig_neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001108
1109 if (!neigh_node)
1110 goto out;
1111
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001112 /* if orig_node is direct neighbor update neigh_node last_seen */
Marek Lindnerfc957272011-07-30 12:04:12 +02001113 if (orig_node == orig_neigh_node)
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001114 neigh_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +02001115
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001116 orig_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +02001117
1118 /* find packet count of corresponding one hop neighbor */
Sven Eckelmanndee222c72018-08-16 16:54:45 +02001119 orig_eq_count = batadv_iv_orig_ifinfo_sum(orig_neigh_node, if_incoming);
Simon Wunderlich89652332013-11-13 19:14:46 +01001120 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
1121 if (neigh_ifinfo) {
1122 neigh_rq_count = neigh_ifinfo->bat_iv.real_packet_count;
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001123 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01001124 } else {
1125 neigh_rq_count = 0;
1126 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001127
1128 /* pay attention to not get a value bigger than 100 % */
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001129 if (orig_eq_count > neigh_rq_count)
1130 total_count = neigh_rq_count;
1131 else
1132 total_count = orig_eq_count;
Marek Lindnerfc957272011-07-30 12:04:12 +02001133
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001134 /* if we have too few packets (too less data) we set tq_own to zero
1135 * if we receive too few packets it is not considered bidirectional
1136 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001137 if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
1138 neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
Marek Lindnerfc957272011-07-30 12:04:12 +02001139 tq_own = 0;
1140 else
1141 /* neigh_node->real_packet_count is never zero as we
1142 * only purge old information when getting new
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001143 * information
1144 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001145 tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count;
Marek Lindnerfc957272011-07-30 12:04:12 +02001146
Sven Eckelmann21a12362012-03-07 09:07:46 +01001147 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
Marek Lindnerfc957272011-07-30 12:04:12 +02001148 * affect the nearly-symmetric links only a little, but
1149 * punishes asymmetric links more. This will give a value
1150 * between 0 and TQ_MAX_VALUE
1151 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001152 neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
1153 neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
1154 neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
1155 BATADV_TQ_LOCAL_WINDOW_SIZE *
1156 BATADV_TQ_LOCAL_WINDOW_SIZE;
1157 inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
1158 inv_asym_penalty /= neigh_rq_max_cube;
1159 tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;
Marek Lindnerfc957272011-07-30 12:04:12 +02001160
Simon Wunderlichc0398762013-11-13 19:14:48 +01001161 /* penalize if the OGM is forwarded on the same interface. WiFi
1162 * interfaces and other half duplex devices suffer from throughput
1163 * drops as they can't send and receive at the same time.
1164 */
1165 tq_iface_penalty = BATADV_TQ_MAX_VALUE;
Sven Eckelmann825ffe12017-08-23 21:52:13 +02001166 if (if_outgoing && if_incoming == if_outgoing &&
Sven Eckelmann10b1bbb2016-09-30 15:21:03 +02001167 batadv_is_wifi_hardif(if_outgoing))
Simon Wunderlichc0398762013-11-13 19:14:48 +01001168 tq_iface_penalty = batadv_hop_penalty(BATADV_TQ_MAX_VALUE,
1169 bat_priv);
1170
1171 combined_tq = batadv_ogm_packet->tq *
1172 tq_own *
1173 tq_asym_penalty *
1174 tq_iface_penalty;
1175 combined_tq /= BATADV_TQ_MAX_VALUE *
1176 BATADV_TQ_MAX_VALUE *
1177 BATADV_TQ_MAX_VALUE;
Sven Eckelmann96412692012-06-05 22:31:30 +02001178 batadv_ogm_packet->tq = combined_tq;
Marek Lindnerfc957272011-07-30 12:04:12 +02001179
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001180 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann6a04be82017-06-18 10:12:30 +02001181 "bidirectional: orig = %pM neigh = %pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, iface_penalty: %3i, total tq: %3i, if_incoming = %s, if_outgoing = %s\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001182 orig_node->orig, orig_neigh_node->orig, total_count,
Simon Wunderlichc0398762013-11-13 19:14:48 +01001183 neigh_rq_count, tq_own, tq_asym_penalty, tq_iface_penalty,
1184 batadv_ogm_packet->tq, if_incoming->net_dev->name,
1185 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT");
Marek Lindnerfc957272011-07-30 12:04:12 +02001186
1187 /* if link has the minimum required transmission quality
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001188 * consider it bidirectional
1189 */
Sven Eckelmann96412692012-06-05 22:31:30 +02001190 if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001191 ret = true;
Marek Lindnerfc957272011-07-30 12:04:12 +02001192
1193out:
1194 if (neigh_node)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001195 batadv_neigh_node_put(neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001196 return ret;
1197}
1198
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001199/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001200 * batadv_iv_ogm_update_seqnos() - process a batman packet for all interfaces,
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001201 * adjust the sequence number and find out whether it is a duplicate
1202 * @ethhdr: ethernet header of the packet
1203 * @batadv_ogm_packet: OGM packet to be considered
1204 * @if_incoming: interface on which the OGM packet was received
Simon Wunderlich89652332013-11-13 19:14:46 +01001205 * @if_outgoing: interface for which the retransmission should be considered
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001206 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001207 * Return: duplicate status as enum batadv_dup_status
Marek Lindnerfc957272011-07-30 12:04:12 +02001208 */
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001209static enum batadv_dup_status
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001210batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +02001211 const struct batadv_ogm_packet *batadv_ogm_packet,
Simon Wunderlich89652332013-11-13 19:14:46 +01001212 const struct batadv_hard_iface *if_incoming,
1213 struct batadv_hard_iface *if_outgoing)
Marek Lindnerfc957272011-07-30 12:04:12 +02001214{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001215 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1216 struct batadv_orig_node *orig_node;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001217 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
Simon Wunderlich89652332013-11-13 19:14:46 +01001218 struct batadv_neigh_node *neigh_node;
1219 struct batadv_neigh_ifinfo *neigh_ifinfo;
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001220 bool is_dup;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001221 s32 seq_diff;
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001222 bool need_update = false;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001223 int set_mark;
1224 enum batadv_dup_status ret = BATADV_NO_DUP;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001225 u32 seqno = ntohl(batadv_ogm_packet->seqno);
1226 u8 *neigh_addr;
1227 u8 packet_count;
Antonio Quartulli0538f7592013-09-02 12:15:01 +02001228 unsigned long *bitmap;
Marek Lindnerfc957272011-07-30 12:04:12 +02001229
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001230 orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
Marek Lindnerfc957272011-07-30 12:04:12 +02001231 if (!orig_node)
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001232 return BATADV_NO_DUP;
Marek Lindnerfc957272011-07-30 12:04:12 +02001233
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001234 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1235 if (WARN_ON(!orig_ifinfo)) {
Sven Eckelmann5d967312016-01-17 11:01:09 +01001236 batadv_orig_node_put(orig_node);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001237 return 0;
1238 }
1239
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001240 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001241 seq_diff = seqno - orig_ifinfo->last_real_seqno;
Marek Lindnerfc957272011-07-30 12:04:12 +02001242
1243 /* signalize caller that the packet is to be dropped. */
Antonio Quartulli1e5cc262012-02-26 15:39:42 +01001244 if (!hlist_empty(&orig_node->neigh_list) &&
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001245 batadv_window_protected(bat_priv, seq_diff,
Simon Wunderlich81f02682015-11-23 19:57:22 +01001246 BATADV_TQ_LOCAL_WINDOW_SIZE,
1247 &orig_ifinfo->batman_seqno_reset, NULL)) {
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001248 ret = BATADV_PROTECTED;
Marek Lindnerfc957272011-07-30 12:04:12 +02001249 goto out;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001250 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001251
1252 rcu_read_lock();
Simon Wunderlich89652332013-11-13 19:14:46 +01001253 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1254 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node,
1255 if_outgoing);
1256 if (!neigh_ifinfo)
1257 continue;
1258
1259 neigh_addr = neigh_node->addr;
1260 is_dup = batadv_test_bit(neigh_ifinfo->bat_iv.real_bits,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001261 orig_ifinfo->last_real_seqno,
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001262 seqno);
1263
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001264 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
Simon Wunderlich89652332013-11-13 19:14:46 +01001265 neigh_node->if_incoming == if_incoming) {
Marek Lindnerfc957272011-07-30 12:04:12 +02001266 set_mark = 1;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001267 if (is_dup)
1268 ret = BATADV_NEIGH_DUP;
1269 } else {
Marek Lindnerfc957272011-07-30 12:04:12 +02001270 set_mark = 0;
Sven Eckelmann825ffe12017-08-23 21:52:13 +02001271 if (is_dup && ret != BATADV_NEIGH_DUP)
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001272 ret = BATADV_ORIG_DUP;
1273 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001274
1275 /* if the window moved, set the update flag. */
Simon Wunderlich89652332013-11-13 19:14:46 +01001276 bitmap = neigh_ifinfo->bat_iv.real_bits;
Antonio Quartulli0538f7592013-09-02 12:15:01 +02001277 need_update |= batadv_bit_get_packet(bat_priv, bitmap,
Sven Eckelmann0f5f93222012-05-12 02:09:25 +02001278 seq_diff, set_mark);
Marek Lindnerfc957272011-07-30 12:04:12 +02001279
Simon Wunderlich89652332013-11-13 19:14:46 +01001280 packet_count = bitmap_weight(bitmap,
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001281 BATADV_TQ_LOCAL_WINDOW_SIZE);
Simon Wunderlich89652332013-11-13 19:14:46 +01001282 neigh_ifinfo->bat_iv.real_packet_count = packet_count;
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001283 batadv_neigh_ifinfo_put(neigh_ifinfo);
Marek Lindnerfc957272011-07-30 12:04:12 +02001284 }
1285 rcu_read_unlock();
1286
1287 if (need_update) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001288 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001289 "%s updating last_seqno: old %u, new %u\n",
1290 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT",
1291 orig_ifinfo->last_real_seqno, seqno);
1292 orig_ifinfo->last_real_seqno = seqno;
Marek Lindnerfc957272011-07-30 12:04:12 +02001293 }
1294
Marek Lindnerfc957272011-07-30 12:04:12 +02001295out:
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001296 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Sven Eckelmann5d967312016-01-17 11:01:09 +01001297 batadv_orig_node_put(orig_node);
Sven Eckelmann35f94772016-01-17 11:01:13 +01001298 batadv_orig_ifinfo_put(orig_ifinfo);
Marek Lindnerfc957272011-07-30 12:04:12 +02001299 return ret;
1300}
1301
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001302/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001303 * batadv_iv_ogm_process_per_outif() - process a batman iv OGM for an outgoing
1304 * interface
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001305 * @skb: the skb containing the OGM
Martin Hundebøll95298a92014-07-15 09:41:05 +02001306 * @ogm_offset: offset from skb->data to start of ogm header
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001307 * @orig_node: the (cached) orig node for the originator of this OGM
1308 * @if_incoming: the interface where this packet was received
1309 * @if_outgoing: the interface for which the packet should be considered
1310 */
1311static void
1312batadv_iv_ogm_process_per_outif(const struct sk_buff *skb, int ogm_offset,
1313 struct batadv_orig_node *orig_node,
1314 struct batadv_hard_iface *if_incoming,
1315 struct batadv_hard_iface *if_outgoing)
1316{
1317 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Marek Lindner4ff1e2a2015-08-04 21:09:58 +08001318 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
Sven Eckelmann4f248cf2015-06-09 20:50:49 +02001319 struct batadv_neigh_node *router = NULL;
1320 struct batadv_neigh_node *router_router = NULL;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001321 struct batadv_orig_node *orig_neigh_node;
1322 struct batadv_orig_ifinfo *orig_ifinfo;
1323 struct batadv_neigh_node *orig_neigh_router = NULL;
1324 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
1325 struct batadv_ogm_packet *ogm_packet;
1326 enum batadv_dup_status dup_status;
1327 bool is_from_best_next_hop = false;
1328 bool is_single_hop_neigh = false;
1329 bool sameseq, similar_ttl;
1330 struct sk_buff *skb_priv;
1331 struct ethhdr *ethhdr;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001332 u8 *prev_sender;
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001333 bool is_bidirect;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001334
1335 /* create a private copy of the skb, as some functions change tq value
1336 * and/or flags.
1337 */
1338 skb_priv = skb_copy(skb, GFP_ATOMIC);
1339 if (!skb_priv)
1340 return;
1341
1342 ethhdr = eth_hdr(skb_priv);
1343 ogm_packet = (struct batadv_ogm_packet *)(skb_priv->data + ogm_offset);
1344
1345 dup_status = batadv_iv_ogm_update_seqnos(ethhdr, ogm_packet,
1346 if_incoming, if_outgoing);
1347 if (batadv_compare_eth(ethhdr->h_source, ogm_packet->orig))
1348 is_single_hop_neigh = true;
1349
1350 if (dup_status == BATADV_PROTECTED) {
1351 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1352 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1353 ethhdr->h_source);
1354 goto out;
1355 }
1356
1357 if (ogm_packet->tq == 0) {
1358 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1359 "Drop packet: originator packet with tq equal 0\n");
1360 goto out;
1361 }
1362
Marek Lindner4ff1e2a2015-08-04 21:09:58 +08001363 if (is_single_hop_neigh) {
1364 hardif_neigh = batadv_hardif_neigh_get(if_incoming,
1365 ethhdr->h_source);
1366 if (hardif_neigh)
1367 hardif_neigh->last_seen = jiffies;
1368 }
1369
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001370 router = batadv_orig_router_get(orig_node, if_outgoing);
1371 if (router) {
1372 router_router = batadv_orig_router_get(router->orig_node,
1373 if_outgoing);
1374 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1375 }
1376
1377 if ((router_ifinfo && router_ifinfo->bat_iv.tq_avg != 0) &&
1378 (batadv_compare_eth(router->addr, ethhdr->h_source)))
1379 is_from_best_next_hop = true;
1380
1381 prev_sender = ogm_packet->prev_sender;
1382 /* avoid temporary routing loops */
1383 if (router && router_router &&
1384 (batadv_compare_eth(router->addr, prev_sender)) &&
1385 !(batadv_compare_eth(ogm_packet->orig, prev_sender)) &&
1386 (batadv_compare_eth(router->addr, router_router->addr))) {
1387 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1388 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1389 ethhdr->h_source);
1390 goto out;
1391 }
1392
1393 if (if_outgoing == BATADV_IF_DEFAULT)
1394 batadv_tvlv_ogm_receive(bat_priv, ogm_packet, orig_node);
1395
1396 /* if sender is a direct neighbor the sender mac equals
1397 * originator mac
1398 */
1399 if (is_single_hop_neigh)
1400 orig_neigh_node = orig_node;
1401 else
1402 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1403 ethhdr->h_source);
1404
1405 if (!orig_neigh_node)
1406 goto out;
1407
1408 /* Update nc_nodes of the originator */
1409 batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node,
1410 ogm_packet, is_single_hop_neigh);
1411
1412 orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
1413 if_outgoing);
1414
1415 /* drop packet if sender is not a direct neighbor and if we
1416 * don't route towards it
1417 */
Sven Eckelmann825ffe12017-08-23 21:52:13 +02001418 if (!is_single_hop_neigh && !orig_neigh_router) {
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001419 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1420 "Drop packet: OGM via unknown neighbor!\n");
1421 goto out_neigh;
1422 }
1423
1424 is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
1425 ogm_packet, if_incoming,
1426 if_outgoing);
1427
1428 /* update ranking if it is not a duplicate or has the same
1429 * seqno and similar ttl as the non-duplicate
1430 */
1431 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1432 if (!orig_ifinfo)
1433 goto out_neigh;
1434
1435 sameseq = orig_ifinfo->last_real_seqno == ntohl(ogm_packet->seqno);
1436 similar_ttl = (orig_ifinfo->last_ttl - 3) <= ogm_packet->ttl;
1437
Sven Eckelmann825ffe12017-08-23 21:52:13 +02001438 if (is_bidirect && (dup_status == BATADV_NO_DUP ||
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001439 (sameseq && similar_ttl))) {
1440 batadv_iv_ogm_orig_update(bat_priv, orig_node,
1441 orig_ifinfo, ethhdr,
1442 ogm_packet, if_incoming,
1443 if_outgoing, dup_status);
1444 }
Sven Eckelmann35f94772016-01-17 11:01:13 +01001445 batadv_orig_ifinfo_put(orig_ifinfo);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001446
Simon Wunderlichef0a9372013-11-13 19:14:49 +01001447 /* only forward for specific interface, not for the default one. */
1448 if (if_outgoing == BATADV_IF_DEFAULT)
1449 goto out_neigh;
1450
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001451 /* is single hop (direct) neighbor */
1452 if (is_single_hop_neigh) {
1453 /* OGMs from secondary interfaces should only scheduled once
1454 * per interface where it has been received, not multiple times
1455 */
Sven Eckelmann825ffe12017-08-23 21:52:13 +02001456 if (ogm_packet->ttl <= 2 &&
1457 if_incoming != if_outgoing) {
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001458 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1459 "Drop packet: OGM from secondary interface and wrong outgoing interface\n");
1460 goto out_neigh;
1461 }
1462 /* mark direct link on incoming interface */
1463 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1464 is_single_hop_neigh,
Simon Wunderlichef0a9372013-11-13 19:14:49 +01001465 is_from_best_next_hop, if_incoming,
1466 if_outgoing);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001467
1468 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1469 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
1470 goto out_neigh;
1471 }
1472
1473 /* multihop originator */
1474 if (!is_bidirect) {
1475 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1476 "Drop packet: not received via bidirectional link\n");
1477 goto out_neigh;
1478 }
1479
1480 if (dup_status == BATADV_NEIGH_DUP) {
1481 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1482 "Drop packet: duplicate packet received\n");
1483 goto out_neigh;
1484 }
1485
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001486 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1487 "Forwarding packet: rebroadcast originator packet\n");
1488 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1489 is_single_hop_neigh, is_from_best_next_hop,
Simon Wunderlichef0a9372013-11-13 19:14:49 +01001490 if_incoming, if_outgoing);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001491
1492out_neigh:
Sven Eckelmann825ffe12017-08-23 21:52:13 +02001493 if (orig_neigh_node && !is_single_hop_neigh)
Sven Eckelmann5d967312016-01-17 11:01:09 +01001494 batadv_orig_node_put(orig_neigh_node);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001495out:
Simon Wunderlichc1e517f2014-03-26 15:46:21 +01001496 if (router_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001497 batadv_neigh_ifinfo_put(router_ifinfo);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001498 if (router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001499 batadv_neigh_node_put(router);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001500 if (router_router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001501 batadv_neigh_node_put(router_router);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001502 if (orig_neigh_router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001503 batadv_neigh_node_put(orig_neigh_router);
Marek Lindner4ff1e2a2015-08-04 21:09:58 +08001504 if (hardif_neigh)
Sven Eckelmannaccadc32016-01-17 11:01:14 +01001505 batadv_hardif_neigh_put(hardif_neigh);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001506
Sven Eckelmannbd687fe2016-07-17 21:04:00 +02001507 consume_skb(skb_priv);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001508}
1509
1510/**
Sven Eckelmanndee222c72018-08-16 16:54:45 +02001511 * batadv_iv_ogm_process_reply() - Check OGM for direct reply and process it
1512 * @ogm_packet: rebroadcast OGM packet to process
1513 * @if_incoming: the interface where this packet was received
1514 * @orig_node: originator which reproadcasted the OGMs
1515 * @if_incoming_seqno: OGM sequence number when rebroadcast was received
1516 */
1517static void batadv_iv_ogm_process_reply(struct batadv_ogm_packet *ogm_packet,
1518 struct batadv_hard_iface *if_incoming,
1519 struct batadv_orig_node *orig_node,
1520 u32 if_incoming_seqno)
1521{
1522 struct batadv_orig_ifinfo *orig_ifinfo;
1523 s32 bit_pos;
1524 u8 *weight;
1525
1526 /* neighbor has to indicate direct link and it has to
1527 * come via the corresponding interface
1528 */
1529 if (!(ogm_packet->flags & BATADV_DIRECTLINK))
1530 return;
1531
1532 if (!batadv_compare_eth(if_incoming->net_dev->dev_addr,
1533 ogm_packet->orig))
1534 return;
1535
1536 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_incoming);
1537 if (!orig_ifinfo)
1538 return;
1539
1540 /* save packet seqno for bidirectional check */
1541 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1542 bit_pos = if_incoming_seqno - 2;
1543 bit_pos -= ntohl(ogm_packet->seqno);
1544 batadv_set_bit(orig_ifinfo->bat_iv.bcast_own, bit_pos);
1545 weight = &orig_ifinfo->bat_iv.bcast_own_sum;
1546 *weight = bitmap_weight(orig_ifinfo->bat_iv.bcast_own,
1547 BATADV_TQ_LOCAL_WINDOW_SIZE);
1548 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1549
1550 batadv_orig_ifinfo_put(orig_ifinfo);
1551}
1552
1553/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001554 * batadv_iv_ogm_process() - process an incoming batman iv OGM
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001555 * @skb: the skb containing the OGM
1556 * @ogm_offset: offset to the OGM which should be processed (for aggregates)
1557 * @if_incoming: the interface where this packet was receved
1558 */
1559static void batadv_iv_ogm_process(const struct sk_buff *skb, int ogm_offset,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001560 struct batadv_hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +02001561{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001562 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001563 struct batadv_orig_node *orig_neigh_node, *orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001564 struct batadv_hard_iface *hard_iface;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001565 struct batadv_ogm_packet *ogm_packet;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001566 u32 if_incoming_seqno;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001567 bool has_directlink_flag;
1568 struct ethhdr *ethhdr;
1569 bool is_my_oldorig = false;
1570 bool is_my_addr = false;
1571 bool is_my_orig = false;
1572
1573 ogm_packet = (struct batadv_ogm_packet *)(skb->data + ogm_offset);
1574 ethhdr = eth_hdr(skb);
Marek Lindnerfc957272011-07-30 12:04:12 +02001575
1576 /* Silently drop when the batman packet is actually not a
1577 * correct packet.
1578 *
1579 * This might happen if a packet is padded (e.g. Ethernet has a
1580 * minimum frame length of 64 byte) and the aggregation interprets
1581 * it as an additional length.
1582 *
1583 * TODO: A more sane solution would be to have a bit in the
Sven Eckelmann96412692012-06-05 22:31:30 +02001584 * batadv_ogm_packet to detect whether the packet is the last
Marek Lindnerfc957272011-07-30 12:04:12 +02001585 * packet in an aggregation. Here we expect that the padding
1586 * is always zero (or not 0x01)
1587 */
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001588 if (ogm_packet->packet_type != BATADV_IV_OGM)
Marek Lindnerfc957272011-07-30 12:04:12 +02001589 return;
1590
1591 /* could be changed by schedule_own_packet() */
Marek Lindner14511512012-08-02 17:20:26 +02001592 if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno);
Marek Lindnerfc957272011-07-30 12:04:12 +02001593
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001594 if (ogm_packet->flags & BATADV_DIRECTLINK)
1595 has_directlink_flag = true;
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001596 else
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001597 has_directlink_flag = false;
Marek Lindnerfc957272011-07-30 12:04:12 +02001598
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001599 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001600 "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 +02001601 ethhdr->h_source, if_incoming->net_dev->name,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001602 if_incoming->net_dev->dev_addr, ogm_packet->orig,
1603 ogm_packet->prev_sender, ntohl(ogm_packet->seqno),
1604 ogm_packet->tq, ogm_packet->ttl,
1605 ogm_packet->version, has_directlink_flag);
Marek Lindnerfc957272011-07-30 12:04:12 +02001606
1607 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001608 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001609 if (hard_iface->if_status != BATADV_IF_ACTIVE)
Marek Lindnerfc957272011-07-30 12:04:12 +02001610 continue;
1611
1612 if (hard_iface->soft_iface != if_incoming->soft_iface)
1613 continue;
1614
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001615 if (batadv_compare_eth(ethhdr->h_source,
1616 hard_iface->net_dev->dev_addr))
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001617 is_my_addr = true;
Marek Lindnerfc957272011-07-30 12:04:12 +02001618
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001619 if (batadv_compare_eth(ogm_packet->orig,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001620 hard_iface->net_dev->dev_addr))
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001621 is_my_orig = true;
Marek Lindnerfc957272011-07-30 12:04:12 +02001622
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001623 if (batadv_compare_eth(ogm_packet->prev_sender,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001624 hard_iface->net_dev->dev_addr))
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001625 is_my_oldorig = true;
Marek Lindnerfc957272011-07-30 12:04:12 +02001626 }
1627 rcu_read_unlock();
1628
Marek Lindnerfc957272011-07-30 12:04:12 +02001629 if (is_my_addr) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001630 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001631 "Drop packet: received my own broadcast (sender: %pM)\n",
1632 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001633 return;
1634 }
1635
Marek Lindnerfc957272011-07-30 12:04:12 +02001636 if (is_my_orig) {
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001637 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1638 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001639 if (!orig_neigh_node)
1640 return;
1641
Sven Eckelmanndee222c72018-08-16 16:54:45 +02001642 batadv_iv_ogm_process_reply(ogm_packet, if_incoming,
1643 orig_neigh_node, if_incoming_seqno);
Marek Lindnerfc957272011-07-30 12:04:12 +02001644
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001645 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001646 "Drop packet: originator packet from myself (via neighbor)\n");
Sven Eckelmann5d967312016-01-17 11:01:09 +01001647 batadv_orig_node_put(orig_neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001648 return;
1649 }
1650
1651 if (is_my_oldorig) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001652 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001653 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1654 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001655 return;
1656 }
1657
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001658 if (ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001659 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001660 "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1661 ethhdr->h_source);
Marek Lindner13b25412012-03-11 06:17:53 +08001662 return;
1663 }
1664
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001665 orig_node = batadv_iv_ogm_orig_get(bat_priv, ogm_packet->orig);
Marek Lindnerfc957272011-07-30 12:04:12 +02001666 if (!orig_node)
1667 return;
1668
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001669 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1670 if_incoming, BATADV_IF_DEFAULT);
Marek Lindnerfc957272011-07-30 12:04:12 +02001671
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001672 rcu_read_lock();
1673 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1674 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1675 continue;
1676
1677 if (hard_iface->soft_iface != bat_priv->soft_iface)
1678 continue;
1679
Sven Eckelmann27353442016-03-05 16:09:16 +01001680 if (!kref_get_unless_zero(&hard_iface->refcount))
1681 continue;
1682
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001683 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1684 if_incoming, hard_iface);
Sven Eckelmann27353442016-03-05 16:09:16 +01001685
1686 batadv_hardif_put(hard_iface);
Marek Lindnerfc957272011-07-30 12:04:12 +02001687 }
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001688 rcu_read_unlock();
Marek Lindnerfc957272011-07-30 12:04:12 +02001689
Sven Eckelmann5d967312016-01-17 11:01:09 +01001690 batadv_orig_node_put(orig_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001691}
1692
Antonio Quartullif0d97252016-05-03 01:45:34 +08001693static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work)
1694{
1695 struct delayed_work *delayed_work;
1696 struct batadv_forw_packet *forw_packet;
1697 struct batadv_priv *bat_priv;
Sven Eckelmannbd687fe2016-07-17 21:04:00 +02001698 bool dropped = false;
Antonio Quartullif0d97252016-05-03 01:45:34 +08001699
1700 delayed_work = to_delayed_work(work);
1701 forw_packet = container_of(delayed_work, struct batadv_forw_packet,
1702 delayed_work);
1703 bat_priv = netdev_priv(forw_packet->if_incoming->soft_iface);
Antonio Quartullif0d97252016-05-03 01:45:34 +08001704
Sven Eckelmannbd687fe2016-07-17 21:04:00 +02001705 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING) {
1706 dropped = true;
Antonio Quartullif0d97252016-05-03 01:45:34 +08001707 goto out;
Sven Eckelmannbd687fe2016-07-17 21:04:00 +02001708 }
Antonio Quartullif0d97252016-05-03 01:45:34 +08001709
1710 batadv_iv_ogm_emit(forw_packet);
1711
1712 /* we have to have at least one packet in the queue to determine the
1713 * queues wake up time unless we are shutting down.
1714 *
1715 * only re-schedule if this is the "original" copy, e.g. the OGM of the
1716 * primary interface should only be rescheduled once per period, but
1717 * this function will be called for the forw_packet instances of the
1718 * other secondary interfaces as well.
1719 */
1720 if (forw_packet->own &&
1721 forw_packet->if_incoming == forw_packet->if_outgoing)
1722 batadv_iv_ogm_schedule(forw_packet->if_incoming);
1723
1724out:
Linus LĂĽssing9b4aec62016-11-01 09:44:44 +01001725 /* do we get something for free()? */
1726 if (batadv_forw_packet_steal(forw_packet,
1727 &bat_priv->forw_bat_list_lock))
1728 batadv_forw_packet_free(forw_packet, dropped);
Antonio Quartullif0d97252016-05-03 01:45:34 +08001729}
1730
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001731static int batadv_iv_ogm_receive(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001732 struct batadv_hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +02001733{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001734 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001735 struct batadv_ogm_packet *ogm_packet;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001736 u8 *packet_pos;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001737 int ogm_offset;
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001738 bool res;
1739 int ret = NET_RX_DROP;
Marek Lindnerc3e29312012-03-04 16:56:25 +08001740
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001741 res = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
1742 if (!res)
1743 goto free_skb;
Marek Lindnerfc957272011-07-30 12:04:12 +02001744
Marek Lindneredbf12b2012-03-11 06:17:49 +08001745 /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
1746 * that does not have B.A.T.M.A.N. IV enabled ?
1747 */
Antonio Quartulli29824a52016-05-25 23:27:31 +08001748 if (bat_priv->algo_ops->iface.enable != batadv_iv_ogm_iface_enable)
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001749 goto free_skb;
Marek Lindneredbf12b2012-03-11 06:17:49 +08001750
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001751 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
1752 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
Martin Hundebøllf8214862012-04-20 17:02:45 +02001753 skb->len + ETH_HLEN);
1754
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001755 ogm_offset = 0;
1756 ogm_packet = (struct batadv_ogm_packet *)skb->data;
Marek Lindnerfc957272011-07-30 12:04:12 +02001757
1758 /* unpack the aggregated packets and process them one by one */
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001759 while (batadv_iv_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
Sven Eckelmanna15d56a2019-08-22 08:55:36 +02001760 ogm_packet)) {
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001761 batadv_iv_ogm_process(skb, ogm_offset, if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001762
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001763 ogm_offset += BATADV_OGM_HLEN;
1764 ogm_offset += ntohs(ogm_packet->tvlv_len);
Marek Lindnerfc957272011-07-30 12:04:12 +02001765
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001766 packet_pos = skb->data + ogm_offset;
1767 ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Marek Lindnerb47506d2013-03-04 10:39:49 +08001768 }
Marek Lindnerc3e29312012-03-04 16:56:25 +08001769
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001770 ret = NET_RX_SUCCESS;
1771
1772free_skb:
1773 if (ret == NET_RX_SUCCESS)
1774 consume_skb(skb);
1775 else
1776 kfree_skb(skb);
1777
1778 return ret;
Marek Lindnerfc957272011-07-30 12:04:12 +02001779}
Marek Lindner1c280472011-11-28 17:40:17 +08001780
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02001781#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001782/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001783 * batadv_iv_ogm_orig_print_neigh() - print neighbors for the originator table
Simon Wunderlich89652332013-11-13 19:14:46 +01001784 * @orig_node: the orig_node for which the neighbors are printed
1785 * @if_outgoing: outgoing interface for these entries
1786 * @seq: debugfs table seq_file struct
1787 *
1788 * Must be called while holding an rcu lock.
1789 */
1790static void
1791batadv_iv_ogm_orig_print_neigh(struct batadv_orig_node *orig_node,
1792 struct batadv_hard_iface *if_outgoing,
1793 struct seq_file *seq)
1794{
1795 struct batadv_neigh_node *neigh_node;
1796 struct batadv_neigh_ifinfo *n_ifinfo;
1797
1798 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1799 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
1800 if (!n_ifinfo)
1801 continue;
1802
1803 seq_printf(seq, " %pM (%3i)",
1804 neigh_node->addr,
1805 n_ifinfo->bat_iv.tq_avg);
1806
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001807 batadv_neigh_ifinfo_put(n_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01001808 }
1809}
1810
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001811/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001812 * batadv_iv_ogm_orig_print() - print the originator table
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001813 * @bat_priv: the bat priv with all the soft interface information
1814 * @seq: debugfs table seq_file struct
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001815 * @if_outgoing: the outgoing interface for which this should be printed
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001816 */
1817static void batadv_iv_ogm_orig_print(struct batadv_priv *bat_priv,
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001818 struct seq_file *seq,
1819 struct batadv_hard_iface *if_outgoing)
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001820{
Simon Wunderlich89652332013-11-13 19:14:46 +01001821 struct batadv_neigh_node *neigh_node;
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001822 struct batadv_hashtable *hash = bat_priv->orig_hash;
1823 int last_seen_msecs, last_seen_secs;
1824 struct batadv_orig_node *orig_node;
Simon Wunderlich89652332013-11-13 19:14:46 +01001825 struct batadv_neigh_ifinfo *n_ifinfo;
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001826 unsigned long last_seen_jiffies;
1827 struct hlist_head *head;
1828 int batman_count = 0;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001829 u32 i;
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001830
Antonio Quartulli925a6f32016-03-12 10:30:18 +01001831 seq_puts(seq,
1832 " Originator last-seen (#/255) Nexthop [outgoingIF]: Potential nexthops ...\n");
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001833
1834 for (i = 0; i < hash->size; i++) {
1835 head = &hash->table[i];
1836
1837 rcu_read_lock();
1838 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001839 neigh_node = batadv_orig_router_get(orig_node,
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001840 if_outgoing);
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001841 if (!neigh_node)
1842 continue;
1843
Simon Wunderlich89652332013-11-13 19:14:46 +01001844 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node,
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001845 if_outgoing);
Simon Wunderlich89652332013-11-13 19:14:46 +01001846 if (!n_ifinfo)
1847 goto next;
1848
1849 if (n_ifinfo->bat_iv.tq_avg == 0)
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001850 goto next;
1851
1852 last_seen_jiffies = jiffies - orig_node->last_seen;
1853 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
1854 last_seen_secs = last_seen_msecs / 1000;
1855 last_seen_msecs = last_seen_msecs % 1000;
1856
1857 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
1858 orig_node->orig, last_seen_secs,
Simon Wunderlich89652332013-11-13 19:14:46 +01001859 last_seen_msecs, n_ifinfo->bat_iv.tq_avg,
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001860 neigh_node->addr,
1861 neigh_node->if_incoming->net_dev->name);
1862
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001863 batadv_iv_ogm_orig_print_neigh(orig_node, if_outgoing,
1864 seq);
Markus Elfring626caae2017-05-06 17:50:13 +02001865 seq_putc(seq, '\n');
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001866 batman_count++;
1867
1868next:
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001869 batadv_neigh_node_put(neigh_node);
Simon Wunderlich89652332013-11-13 19:14:46 +01001870 if (n_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001871 batadv_neigh_ifinfo_put(n_ifinfo);
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001872 }
1873 rcu_read_unlock();
1874 }
1875
1876 if (batman_count == 0)
1877 seq_puts(seq, "No batman nodes in range ...\n");
1878}
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02001879#endif
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001880
Antonio Quartullia3285a82013-09-02 12:15:04 +02001881/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001882 * batadv_iv_ogm_neigh_get_tq_avg() - Get the TQ average for a neighbour on a
Matthias Schiffer024f99c2016-07-03 13:31:40 +02001883 * given outgoing interface.
1884 * @neigh_node: Neighbour of interest
1885 * @if_outgoing: Outgoing interface of interest
1886 * @tq_avg: Pointer of where to store the TQ average
1887 *
1888 * Return: False if no average TQ available, otherwise true.
1889 */
1890static bool
1891batadv_iv_ogm_neigh_get_tq_avg(struct batadv_neigh_node *neigh_node,
1892 struct batadv_hard_iface *if_outgoing,
1893 u8 *tq_avg)
1894{
1895 struct batadv_neigh_ifinfo *n_ifinfo;
1896
1897 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
1898 if (!n_ifinfo)
1899 return false;
1900
1901 *tq_avg = n_ifinfo->bat_iv.tq_avg;
1902 batadv_neigh_ifinfo_put(n_ifinfo);
1903
1904 return true;
1905}
1906
1907/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001908 * batadv_iv_ogm_orig_dump_subentry() - Dump an originator subentry into a
Matthias Schiffer024f99c2016-07-03 13:31:40 +02001909 * message
1910 * @msg: Netlink message to dump into
1911 * @portid: Port making netlink request
1912 * @seq: Sequence number of netlink message
1913 * @bat_priv: The bat priv with all the soft interface information
1914 * @if_outgoing: Limit dump to entries with this outgoing interface
1915 * @orig_node: Originator to dump
1916 * @neigh_node: Single hops neighbour
1917 * @best: Is the best originator
1918 *
1919 * Return: Error code, or 0 on success
1920 */
1921static int
1922batadv_iv_ogm_orig_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
1923 struct batadv_priv *bat_priv,
1924 struct batadv_hard_iface *if_outgoing,
1925 struct batadv_orig_node *orig_node,
1926 struct batadv_neigh_node *neigh_node,
1927 bool best)
1928{
1929 void *hdr;
1930 u8 tq_avg;
1931 unsigned int last_seen_msecs;
1932
1933 last_seen_msecs = jiffies_to_msecs(jiffies - orig_node->last_seen);
1934
1935 if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node, if_outgoing, &tq_avg))
1936 return 0;
1937
1938 if (if_outgoing != BATADV_IF_DEFAULT &&
1939 if_outgoing != neigh_node->if_incoming)
1940 return 0;
1941
1942 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
1943 NLM_F_MULTI, BATADV_CMD_GET_ORIGINATORS);
1944 if (!hdr)
1945 return -ENOBUFS;
1946
1947 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
1948 orig_node->orig) ||
1949 nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
1950 neigh_node->addr) ||
1951 nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
1952 neigh_node->if_incoming->net_dev->ifindex) ||
1953 nla_put_u8(msg, BATADV_ATTR_TQ, tq_avg) ||
1954 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
1955 last_seen_msecs))
1956 goto nla_put_failure;
1957
1958 if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
1959 goto nla_put_failure;
1960
1961 genlmsg_end(msg, hdr);
1962 return 0;
1963
1964 nla_put_failure:
1965 genlmsg_cancel(msg, hdr);
1966 return -EMSGSIZE;
1967}
1968
1969/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001970 * batadv_iv_ogm_orig_dump_entry() - Dump an originator entry into a message
Matthias Schiffer024f99c2016-07-03 13:31:40 +02001971 * @msg: Netlink message to dump into
1972 * @portid: Port making netlink request
1973 * @seq: Sequence number of netlink message
1974 * @bat_priv: The bat priv with all the soft interface information
1975 * @if_outgoing: Limit dump to entries with this outgoing interface
1976 * @orig_node: Originator to dump
1977 * @sub_s: Number of sub entries to skip
1978 *
1979 * This function assumes the caller holds rcu_read_lock().
1980 *
1981 * Return: Error code, or 0 on success
1982 */
1983static int
1984batadv_iv_ogm_orig_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
1985 struct batadv_priv *bat_priv,
1986 struct batadv_hard_iface *if_outgoing,
1987 struct batadv_orig_node *orig_node, int *sub_s)
1988{
1989 struct batadv_neigh_node *neigh_node_best;
1990 struct batadv_neigh_node *neigh_node;
1991 int sub = 0;
1992 bool best;
1993 u8 tq_avg_best;
1994
1995 neigh_node_best = batadv_orig_router_get(orig_node, if_outgoing);
1996 if (!neigh_node_best)
1997 goto out;
1998
1999 if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node_best, if_outgoing,
2000 &tq_avg_best))
2001 goto out;
2002
2003 if (tq_avg_best == 0)
2004 goto out;
2005
2006 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
2007 if (sub++ < *sub_s)
2008 continue;
2009
2010 best = (neigh_node == neigh_node_best);
2011
2012 if (batadv_iv_ogm_orig_dump_subentry(msg, portid, seq,
2013 bat_priv, if_outgoing,
2014 orig_node, neigh_node,
2015 best)) {
2016 batadv_neigh_node_put(neigh_node_best);
2017
2018 *sub_s = sub - 1;
2019 return -EMSGSIZE;
2020 }
2021 }
2022
2023 out:
2024 if (neigh_node_best)
2025 batadv_neigh_node_put(neigh_node_best);
2026
2027 *sub_s = 0;
2028 return 0;
2029}
2030
2031/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002032 * batadv_iv_ogm_orig_dump_bucket() - Dump an originator bucket into a
Matthias Schiffer024f99c2016-07-03 13:31:40 +02002033 * message
2034 * @msg: Netlink message to dump into
2035 * @portid: Port making netlink request
2036 * @seq: Sequence number of netlink message
2037 * @bat_priv: The bat priv with all the soft interface information
2038 * @if_outgoing: Limit dump to entries with this outgoing interface
2039 * @head: Bucket to be dumped
2040 * @idx_s: Number of entries to be skipped
2041 * @sub: Number of sub entries to be skipped
2042 *
2043 * Return: Error code, or 0 on success
2044 */
2045static int
2046batadv_iv_ogm_orig_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
2047 struct batadv_priv *bat_priv,
2048 struct batadv_hard_iface *if_outgoing,
2049 struct hlist_head *head, int *idx_s, int *sub)
2050{
2051 struct batadv_orig_node *orig_node;
2052 int idx = 0;
2053
2054 rcu_read_lock();
2055 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
2056 if (idx++ < *idx_s)
2057 continue;
2058
2059 if (batadv_iv_ogm_orig_dump_entry(msg, portid, seq, bat_priv,
2060 if_outgoing, orig_node,
2061 sub)) {
2062 rcu_read_unlock();
2063 *idx_s = idx - 1;
2064 return -EMSGSIZE;
2065 }
2066 }
2067 rcu_read_unlock();
2068
2069 *idx_s = 0;
2070 *sub = 0;
2071 return 0;
2072}
2073
2074/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002075 * batadv_iv_ogm_orig_dump() - Dump the originators into a message
Matthias Schiffer024f99c2016-07-03 13:31:40 +02002076 * @msg: Netlink message to dump into
2077 * @cb: Control block containing additional options
2078 * @bat_priv: The bat priv with all the soft interface information
2079 * @if_outgoing: Limit dump to entries with this outgoing interface
2080 */
2081static void
2082batadv_iv_ogm_orig_dump(struct sk_buff *msg, struct netlink_callback *cb,
2083 struct batadv_priv *bat_priv,
2084 struct batadv_hard_iface *if_outgoing)
2085{
2086 struct batadv_hashtable *hash = bat_priv->orig_hash;
2087 struct hlist_head *head;
2088 int bucket = cb->args[0];
2089 int idx = cb->args[1];
2090 int sub = cb->args[2];
2091 int portid = NETLINK_CB(cb->skb).portid;
2092
2093 while (bucket < hash->size) {
2094 head = &hash->table[bucket];
2095
2096 if (batadv_iv_ogm_orig_dump_bucket(msg, portid,
2097 cb->nlh->nlmsg_seq,
2098 bat_priv, if_outgoing, head,
2099 &idx, &sub))
2100 break;
2101
2102 bucket++;
2103 }
2104
2105 cb->args[0] = bucket;
2106 cb->args[1] = idx;
2107 cb->args[2] = sub;
2108}
2109
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002110#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Matthias Schiffer024f99c2016-07-03 13:31:40 +02002111/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002112 * batadv_iv_hardif_neigh_print() - print a single hop neighbour node
Marek Lindner75874052015-08-04 21:09:57 +08002113 * @seq: neighbour table seq_file struct
2114 * @hardif_neigh: hardif neighbour information
2115 */
2116static void
2117batadv_iv_hardif_neigh_print(struct seq_file *seq,
2118 struct batadv_hardif_neigh_node *hardif_neigh)
2119{
2120 int last_secs, last_msecs;
2121
2122 last_secs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) / 1000;
2123 last_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) % 1000;
2124
2125 seq_printf(seq, " %10s %pM %4i.%03is\n",
2126 hardif_neigh->if_incoming->net_dev->name,
2127 hardif_neigh->addr, last_secs, last_msecs);
2128}
2129
2130/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002131 * batadv_iv_ogm_neigh_print() - print the single hop neighbour list
Marek Lindner75874052015-08-04 21:09:57 +08002132 * @bat_priv: the bat priv with all the soft interface information
2133 * @seq: neighbour table seq_file struct
2134 */
2135static void batadv_iv_neigh_print(struct batadv_priv *bat_priv,
2136 struct seq_file *seq)
2137{
2138 struct net_device *net_dev = (struct net_device *)seq->private;
2139 struct batadv_hardif_neigh_node *hardif_neigh;
2140 struct batadv_hard_iface *hard_iface;
2141 int batman_count = 0;
2142
Antonio Quartulli925a6f32016-03-12 10:30:18 +01002143 seq_puts(seq, " IF Neighbor last-seen\n");
Marek Lindner75874052015-08-04 21:09:57 +08002144
2145 rcu_read_lock();
2146 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
2147 if (hard_iface->soft_iface != net_dev)
2148 continue;
2149
2150 hlist_for_each_entry_rcu(hardif_neigh,
2151 &hard_iface->neigh_list, list) {
2152 batadv_iv_hardif_neigh_print(seq, hardif_neigh);
2153 batman_count++;
2154 }
2155 }
2156 rcu_read_unlock();
2157
2158 if (batman_count == 0)
2159 seq_puts(seq, "No batman nodes in range ...\n");
2160}
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002161#endif
Marek Lindner75874052015-08-04 21:09:57 +08002162
2163/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002164 * batadv_iv_ogm_neigh_diff() - calculate tq difference of two neighbors
Markus Pargmann57b12502016-07-03 11:07:14 +02002165 * @neigh1: the first neighbor object of the comparison
2166 * @if_outgoing1: outgoing interface for the first neighbor
2167 * @neigh2: the second neighbor object of the comparison
2168 * @if_outgoing2: outgoing interface for the second neighbor
2169 * @diff: pointer to integer receiving the calculated difference
2170 *
2171 * The content of *@diff is only valid when this function returns true.
2172 * It is less, equal to or greater than 0 if the metric via neigh1 is lower,
2173 * the same as or higher than the metric via neigh2
2174 *
2175 * Return: true when the difference could be calculated, false otherwise
2176 */
2177static bool batadv_iv_ogm_neigh_diff(struct batadv_neigh_node *neigh1,
2178 struct batadv_hard_iface *if_outgoing1,
2179 struct batadv_neigh_node *neigh2,
2180 struct batadv_hard_iface *if_outgoing2,
2181 int *diff)
2182{
2183 struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
2184 u8 tq1, tq2;
2185 bool ret = true;
2186
2187 neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
2188 neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
2189
2190 if (!neigh1_ifinfo || !neigh2_ifinfo) {
2191 ret = false;
2192 goto out;
2193 }
2194
2195 tq1 = neigh1_ifinfo->bat_iv.tq_avg;
2196 tq2 = neigh2_ifinfo->bat_iv.tq_avg;
2197 *diff = (int)tq1 - (int)tq2;
2198
2199out:
2200 if (neigh1_ifinfo)
2201 batadv_neigh_ifinfo_put(neigh1_ifinfo);
2202 if (neigh2_ifinfo)
2203 batadv_neigh_ifinfo_put(neigh2_ifinfo);
2204
2205 return ret;
2206}
2207
2208/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002209 * batadv_iv_ogm_neigh_dump_neigh() - Dump a neighbour into a netlink message
Matthias Schiffer024f99c2016-07-03 13:31:40 +02002210 * @msg: Netlink message to dump into
2211 * @portid: Port making netlink request
2212 * @seq: Sequence number of netlink message
2213 * @hardif_neigh: Neighbour to be dumped
2214 *
2215 * Return: Error code, or 0 on success
2216 */
2217static int
2218batadv_iv_ogm_neigh_dump_neigh(struct sk_buff *msg, u32 portid, u32 seq,
2219 struct batadv_hardif_neigh_node *hardif_neigh)
2220{
2221 void *hdr;
2222 unsigned int last_seen_msecs;
2223
2224 last_seen_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen);
2225
2226 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2227 NLM_F_MULTI, BATADV_CMD_GET_NEIGHBORS);
2228 if (!hdr)
2229 return -ENOBUFS;
2230
2231 if (nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
2232 hardif_neigh->addr) ||
2233 nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
2234 hardif_neigh->if_incoming->net_dev->ifindex) ||
2235 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
2236 last_seen_msecs))
2237 goto nla_put_failure;
2238
2239 genlmsg_end(msg, hdr);
2240 return 0;
2241
2242 nla_put_failure:
2243 genlmsg_cancel(msg, hdr);
2244 return -EMSGSIZE;
2245}
2246
2247/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002248 * batadv_iv_ogm_neigh_dump_hardif() - Dump the neighbours of a hard interface
Matthias Schiffer024f99c2016-07-03 13:31:40 +02002249 * into a message
2250 * @msg: Netlink message to dump into
2251 * @portid: Port making netlink request
2252 * @seq: Sequence number of netlink message
2253 * @bat_priv: The bat priv with all the soft interface information
2254 * @hard_iface: Hard interface to dump the neighbours for
2255 * @idx_s: Number of entries to skip
2256 *
2257 * This function assumes the caller holds rcu_read_lock().
2258 *
2259 * Return: Error code, or 0 on success
2260 */
2261static int
2262batadv_iv_ogm_neigh_dump_hardif(struct sk_buff *msg, u32 portid, u32 seq,
2263 struct batadv_priv *bat_priv,
2264 struct batadv_hard_iface *hard_iface,
2265 int *idx_s)
2266{
2267 struct batadv_hardif_neigh_node *hardif_neigh;
2268 int idx = 0;
2269
2270 hlist_for_each_entry_rcu(hardif_neigh,
2271 &hard_iface->neigh_list, list) {
2272 if (idx++ < *idx_s)
2273 continue;
2274
2275 if (batadv_iv_ogm_neigh_dump_neigh(msg, portid, seq,
2276 hardif_neigh)) {
2277 *idx_s = idx - 1;
2278 return -EMSGSIZE;
2279 }
2280 }
2281
2282 *idx_s = 0;
2283 return 0;
2284}
2285
2286/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002287 * batadv_iv_ogm_neigh_dump() - Dump the neighbours into a message
Matthias Schiffer024f99c2016-07-03 13:31:40 +02002288 * @msg: Netlink message to dump into
2289 * @cb: Control block containing additional options
2290 * @bat_priv: The bat priv with all the soft interface information
2291 * @single_hardif: Limit dump to this hard interfaace
2292 */
2293static void
2294batadv_iv_ogm_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb,
2295 struct batadv_priv *bat_priv,
2296 struct batadv_hard_iface *single_hardif)
2297{
2298 struct batadv_hard_iface *hard_iface;
2299 int i_hardif = 0;
2300 int i_hardif_s = cb->args[0];
2301 int idx = cb->args[1];
2302 int portid = NETLINK_CB(cb->skb).portid;
2303
2304 rcu_read_lock();
2305 if (single_hardif) {
2306 if (i_hardif_s == 0) {
2307 if (batadv_iv_ogm_neigh_dump_hardif(msg, portid,
2308 cb->nlh->nlmsg_seq,
2309 bat_priv,
2310 single_hardif,
2311 &idx) == 0)
2312 i_hardif++;
2313 }
2314 } else {
2315 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list,
2316 list) {
2317 if (hard_iface->soft_iface != bat_priv->soft_iface)
2318 continue;
2319
2320 if (i_hardif++ < i_hardif_s)
2321 continue;
2322
2323 if (batadv_iv_ogm_neigh_dump_hardif(msg, portid,
2324 cb->nlh->nlmsg_seq,
2325 bat_priv,
2326 hard_iface, &idx)) {
2327 i_hardif--;
2328 break;
2329 }
2330 }
2331 }
2332 rcu_read_unlock();
2333
2334 cb->args[0] = i_hardif;
2335 cb->args[1] = idx;
2336}
2337
2338/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002339 * batadv_iv_ogm_neigh_cmp() - compare the metrics of two neighbors
Antonio Quartullia3285a82013-09-02 12:15:04 +02002340 * @neigh1: the first neighbor object of the comparison
Simon Wunderlich89652332013-11-13 19:14:46 +01002341 * @if_outgoing1: outgoing interface for the first neighbor
Antonio Quartullia3285a82013-09-02 12:15:04 +02002342 * @neigh2: the second neighbor object of the comparison
Simon Wunderlich89652332013-11-13 19:14:46 +01002343 * @if_outgoing2: outgoing interface for the second neighbor
Antonio Quartullia3285a82013-09-02 12:15:04 +02002344 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002345 * Return: a value less, equal to or greater than 0 if the metric via neigh1 is
Antonio Quartullia3285a82013-09-02 12:15:04 +02002346 * lower, the same as or higher than the metric via neigh2
2347 */
2348static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1,
Simon Wunderlich89652332013-11-13 19:14:46 +01002349 struct batadv_hard_iface *if_outgoing1,
2350 struct batadv_neigh_node *neigh2,
2351 struct batadv_hard_iface *if_outgoing2)
Antonio Quartullia3285a82013-09-02 12:15:04 +02002352{
Markus Pargmann57b12502016-07-03 11:07:14 +02002353 bool ret;
Simon Wunderlich89652332013-11-13 19:14:46 +01002354 int diff;
Antonio Quartullia3285a82013-09-02 12:15:04 +02002355
Markus Pargmann57b12502016-07-03 11:07:14 +02002356 ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
2357 if_outgoing2, &diff);
2358 if (!ret)
2359 return 0;
Simon Wunderlich89652332013-11-13 19:14:46 +01002360
2361 return diff;
Antonio Quartullia3285a82013-09-02 12:15:04 +02002362}
2363
Antonio Quartullic43c9812013-09-02 12:15:05 +02002364/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002365 * batadv_iv_ogm_neigh_is_sob() - check if neigh1 is similarly good or better
Simon Wunderlich18165f62015-08-08 02:01:50 +02002366 * than neigh2 from the metric prospective
Antonio Quartullic43c9812013-09-02 12:15:05 +02002367 * @neigh1: the first neighbor object of the comparison
Martin Hundebøll95298a92014-07-15 09:41:05 +02002368 * @if_outgoing1: outgoing interface for the first neighbor
Antonio Quartullic43c9812013-09-02 12:15:05 +02002369 * @neigh2: the second neighbor object of the comparison
Simon Wunderlich89652332013-11-13 19:14:46 +01002370 * @if_outgoing2: outgoing interface for the second neighbor
Martin Hundebøll95298a92014-07-15 09:41:05 +02002371 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002372 * Return: true if the metric via neigh1 is equally good or better than
Simon Wunderlich89652332013-11-13 19:14:46 +01002373 * the metric via neigh2, false otherwise.
2374 */
2375static bool
Simon Wunderlich18165f62015-08-08 02:01:50 +02002376batadv_iv_ogm_neigh_is_sob(struct batadv_neigh_node *neigh1,
Simon Wunderlich89652332013-11-13 19:14:46 +01002377 struct batadv_hard_iface *if_outgoing1,
2378 struct batadv_neigh_node *neigh2,
2379 struct batadv_hard_iface *if_outgoing2)
2380{
Simon Wunderlich89652332013-11-13 19:14:46 +01002381 bool ret;
Markus Pargmann57b12502016-07-03 11:07:14 +02002382 int diff;
Simon Wunderlich89652332013-11-13 19:14:46 +01002383
Markus Pargmann57b12502016-07-03 11:07:14 +02002384 ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
2385 if_outgoing2, &diff);
2386 if (!ret)
2387 return false;
Simon Wunderlich89652332013-11-13 19:14:46 +01002388
Markus Pargmann57b12502016-07-03 11:07:14 +02002389 ret = diff > -BATADV_TQ_SIMILARITY_THRESHOLD;
Simon Wunderlich89652332013-11-13 19:14:46 +01002390 return ret;
Antonio Quartullic43c9812013-09-02 12:15:05 +02002391}
2392
Sven Eckelmann9e6b5642019-06-02 10:57:31 +02002393static void batadv_iv_iface_enabled(struct batadv_hard_iface *hard_iface)
Antonio Quartullif0d97252016-05-03 01:45:34 +08002394{
2395 /* begin scheduling originator messages on that interface */
2396 batadv_iv_ogm_schedule(hard_iface);
2397}
2398
Sven Eckelmann1a9070e2017-03-04 15:48:50 +01002399/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002400 * batadv_iv_init_sel_class() - initialize GW selection class
Sven Eckelmann1a9070e2017-03-04 15:48:50 +01002401 * @bat_priv: the bat priv with all the soft interface information
2402 */
2403static void batadv_iv_init_sel_class(struct batadv_priv *bat_priv)
2404{
2405 /* set default TQ difference threshold to 20 */
2406 atomic_set(&bat_priv->gw.sel_class, 20);
2407}
2408
Antonio Quartulli34d99cf2016-07-03 12:46:33 +02002409static struct batadv_gw_node *
2410batadv_iv_gw_get_best_gw_node(struct batadv_priv *bat_priv)
2411{
2412 struct batadv_neigh_node *router;
2413 struct batadv_neigh_ifinfo *router_ifinfo;
2414 struct batadv_gw_node *gw_node, *curr_gw = NULL;
2415 u64 max_gw_factor = 0;
2416 u64 tmp_gw_factor = 0;
2417 u8 max_tq = 0;
2418 u8 tq_avg;
2419 struct batadv_orig_node *orig_node;
2420
2421 rcu_read_lock();
Sven Eckelmann70ea5ce2016-07-27 12:31:08 +02002422 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
Antonio Quartulli34d99cf2016-07-03 12:46:33 +02002423 orig_node = gw_node->orig_node;
2424 router = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
2425 if (!router)
2426 continue;
2427
2428 router_ifinfo = batadv_neigh_ifinfo_get(router,
2429 BATADV_IF_DEFAULT);
2430 if (!router_ifinfo)
2431 goto next;
2432
2433 if (!kref_get_unless_zero(&gw_node->refcount))
2434 goto next;
2435
2436 tq_avg = router_ifinfo->bat_iv.tq_avg;
2437
2438 switch (atomic_read(&bat_priv->gw.sel_class)) {
2439 case 1: /* fast connection */
2440 tmp_gw_factor = tq_avg * tq_avg;
2441 tmp_gw_factor *= gw_node->bandwidth_down;
2442 tmp_gw_factor *= 100 * 100;
2443 tmp_gw_factor >>= 18;
2444
Sven Eckelmann825ffe12017-08-23 21:52:13 +02002445 if (tmp_gw_factor > max_gw_factor ||
2446 (tmp_gw_factor == max_gw_factor &&
2447 tq_avg > max_tq)) {
Antonio Quartulli34d99cf2016-07-03 12:46:33 +02002448 if (curr_gw)
2449 batadv_gw_node_put(curr_gw);
2450 curr_gw = gw_node;
2451 kref_get(&curr_gw->refcount);
2452 }
2453 break;
2454
2455 default: /* 2: stable connection (use best statistic)
2456 * 3: fast-switch (use best statistic but change as
2457 * soon as a better gateway appears)
2458 * XX: late-switch (use best statistic but change as
2459 * soon as a better gateway appears which has
2460 * $routing_class more tq points)
2461 */
2462 if (tq_avg > max_tq) {
2463 if (curr_gw)
2464 batadv_gw_node_put(curr_gw);
2465 curr_gw = gw_node;
2466 kref_get(&curr_gw->refcount);
2467 }
2468 break;
2469 }
2470
2471 if (tq_avg > max_tq)
2472 max_tq = tq_avg;
2473
2474 if (tmp_gw_factor > max_gw_factor)
2475 max_gw_factor = tmp_gw_factor;
2476
2477 batadv_gw_node_put(gw_node);
2478
2479next:
2480 batadv_neigh_node_put(router);
2481 if (router_ifinfo)
2482 batadv_neigh_ifinfo_put(router_ifinfo);
2483 }
2484 rcu_read_unlock();
2485
2486 return curr_gw;
2487}
2488
2489static bool batadv_iv_gw_is_eligible(struct batadv_priv *bat_priv,
2490 struct batadv_orig_node *curr_gw_orig,
2491 struct batadv_orig_node *orig_node)
2492{
2493 struct batadv_neigh_ifinfo *router_orig_ifinfo = NULL;
2494 struct batadv_neigh_ifinfo *router_gw_ifinfo = NULL;
2495 struct batadv_neigh_node *router_gw = NULL;
2496 struct batadv_neigh_node *router_orig = NULL;
2497 u8 gw_tq_avg, orig_tq_avg;
2498 bool ret = false;
2499
2500 /* dynamic re-election is performed only on fast or late switch */
2501 if (atomic_read(&bat_priv->gw.sel_class) <= 2)
2502 return false;
2503
2504 router_gw = batadv_orig_router_get(curr_gw_orig, BATADV_IF_DEFAULT);
2505 if (!router_gw) {
2506 ret = true;
2507 goto out;
2508 }
2509
2510 router_gw_ifinfo = batadv_neigh_ifinfo_get(router_gw,
2511 BATADV_IF_DEFAULT);
2512 if (!router_gw_ifinfo) {
2513 ret = true;
2514 goto out;
2515 }
2516
2517 router_orig = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
2518 if (!router_orig)
2519 goto out;
2520
2521 router_orig_ifinfo = batadv_neigh_ifinfo_get(router_orig,
2522 BATADV_IF_DEFAULT);
2523 if (!router_orig_ifinfo)
2524 goto out;
2525
2526 gw_tq_avg = router_gw_ifinfo->bat_iv.tq_avg;
2527 orig_tq_avg = router_orig_ifinfo->bat_iv.tq_avg;
2528
2529 /* the TQ value has to be better */
2530 if (orig_tq_avg < gw_tq_avg)
2531 goto out;
2532
2533 /* if the routing class is greater than 3 the value tells us how much
2534 * greater the TQ value of the new gateway must be
2535 */
2536 if ((atomic_read(&bat_priv->gw.sel_class) > 3) &&
2537 (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw.sel_class)))
2538 goto out;
2539
2540 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
2541 "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n",
2542 gw_tq_avg, orig_tq_avg);
2543
2544 ret = true;
2545out:
2546 if (router_gw_ifinfo)
2547 batadv_neigh_ifinfo_put(router_gw_ifinfo);
2548 if (router_orig_ifinfo)
2549 batadv_neigh_ifinfo_put(router_orig_ifinfo);
2550 if (router_gw)
2551 batadv_neigh_node_put(router_gw);
2552 if (router_orig)
2553 batadv_neigh_node_put(router_orig);
2554
2555 return ret;
2556}
2557
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002558#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Antonio Quartulli34d99cf2016-07-03 12:46:33 +02002559/* fails if orig_node has no router */
2560static int batadv_iv_gw_write_buffer_text(struct batadv_priv *bat_priv,
2561 struct seq_file *seq,
2562 const struct batadv_gw_node *gw_node)
2563{
2564 struct batadv_gw_node *curr_gw;
2565 struct batadv_neigh_node *router;
2566 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
2567 int ret = -1;
2568
2569 router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
2570 if (!router)
2571 goto out;
2572
2573 router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
2574 if (!router_ifinfo)
2575 goto out;
2576
2577 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
2578
2579 seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n",
2580 (curr_gw == gw_node ? "=>" : " "),
2581 gw_node->orig_node->orig,
2582 router_ifinfo->bat_iv.tq_avg, router->addr,
2583 router->if_incoming->net_dev->name,
2584 gw_node->bandwidth_down / 10,
2585 gw_node->bandwidth_down % 10,
2586 gw_node->bandwidth_up / 10,
2587 gw_node->bandwidth_up % 10);
2588 ret = seq_has_overflowed(seq) ? -1 : 0;
2589
2590 if (curr_gw)
2591 batadv_gw_node_put(curr_gw);
2592out:
2593 if (router_ifinfo)
2594 batadv_neigh_ifinfo_put(router_ifinfo);
2595 if (router)
2596 batadv_neigh_node_put(router);
2597 return ret;
2598}
2599
2600static void batadv_iv_gw_print(struct batadv_priv *bat_priv,
2601 struct seq_file *seq)
2602{
2603 struct batadv_gw_node *gw_node;
2604 int gw_count = 0;
2605
2606 seq_puts(seq,
2607 " Gateway (#/255) Nexthop [outgoingIF]: advertised uplink bandwidth\n");
2608
2609 rcu_read_lock();
Sven Eckelmann70ea5ce2016-07-27 12:31:08 +02002610 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
Antonio Quartulli34d99cf2016-07-03 12:46:33 +02002611 /* fails if orig_node has no router */
2612 if (batadv_iv_gw_write_buffer_text(bat_priv, seq, gw_node) < 0)
2613 continue;
2614
2615 gw_count++;
2616 }
2617 rcu_read_unlock();
2618
2619 if (gw_count == 0)
2620 seq_puts(seq, "No gateways in range ...\n");
2621}
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002622#endif
Antonio Quartulli34d99cf2016-07-03 12:46:33 +02002623
Andrew Lunnefb766a2016-07-03 13:31:43 +02002624/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002625 * batadv_iv_gw_dump_entry() - Dump a gateway into a message
Andrew Lunnefb766a2016-07-03 13:31:43 +02002626 * @msg: Netlink message to dump into
2627 * @portid: Port making netlink request
Sven Eckelmann9264c852018-10-30 22:01:23 +01002628 * @cb: Control block containing additional options
Andrew Lunnefb766a2016-07-03 13:31:43 +02002629 * @bat_priv: The bat priv with all the soft interface information
2630 * @gw_node: Gateway to be dumped
2631 *
2632 * Return: Error code, or 0 on success
2633 */
Sven Eckelmann9264c852018-10-30 22:01:23 +01002634static int batadv_iv_gw_dump_entry(struct sk_buff *msg, u32 portid,
2635 struct netlink_callback *cb,
Andrew Lunnefb766a2016-07-03 13:31:43 +02002636 struct batadv_priv *bat_priv,
2637 struct batadv_gw_node *gw_node)
2638{
2639 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
2640 struct batadv_neigh_node *router;
Sven Eckelmannb5685d22018-06-02 17:26:34 +02002641 struct batadv_gw_node *curr_gw = NULL;
Sven Eckelmann10d57022018-02-19 14:08:52 +01002642 int ret = 0;
Andrew Lunnefb766a2016-07-03 13:31:43 +02002643 void *hdr;
2644
2645 router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
2646 if (!router)
2647 goto out;
2648
2649 router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
2650 if (!router_ifinfo)
2651 goto out;
2652
2653 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
2654
Sven Eckelmann9264c852018-10-30 22:01:23 +01002655 hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
2656 &batadv_netlink_family, NLM_F_MULTI,
2657 BATADV_CMD_GET_GATEWAYS);
Andrew Lunnefb766a2016-07-03 13:31:43 +02002658 if (!hdr) {
2659 ret = -ENOBUFS;
2660 goto out;
2661 }
2662
Sven Eckelmann9264c852018-10-30 22:01:23 +01002663 genl_dump_check_consistent(cb, hdr);
2664
Andrew Lunnefb766a2016-07-03 13:31:43 +02002665 ret = -EMSGSIZE;
2666
2667 if (curr_gw == gw_node)
2668 if (nla_put_flag(msg, BATADV_ATTR_FLAG_BEST)) {
2669 genlmsg_cancel(msg, hdr);
2670 goto out;
2671 }
2672
2673 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
2674 gw_node->orig_node->orig) ||
2675 nla_put_u8(msg, BATADV_ATTR_TQ, router_ifinfo->bat_iv.tq_avg) ||
2676 nla_put(msg, BATADV_ATTR_ROUTER, ETH_ALEN,
2677 router->addr) ||
2678 nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
2679 router->if_incoming->net_dev->name) ||
2680 nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_DOWN,
2681 gw_node->bandwidth_down) ||
2682 nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_UP,
2683 gw_node->bandwidth_up)) {
2684 genlmsg_cancel(msg, hdr);
2685 goto out;
2686 }
2687
2688 genlmsg_end(msg, hdr);
2689 ret = 0;
2690
2691out:
Sven Eckelmannb5685d22018-06-02 17:26:34 +02002692 if (curr_gw)
2693 batadv_gw_node_put(curr_gw);
Andrew Lunnefb766a2016-07-03 13:31:43 +02002694 if (router_ifinfo)
2695 batadv_neigh_ifinfo_put(router_ifinfo);
2696 if (router)
2697 batadv_neigh_node_put(router);
2698 return ret;
2699}
2700
2701/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002702 * batadv_iv_gw_dump() - Dump gateways into a message
Andrew Lunnefb766a2016-07-03 13:31:43 +02002703 * @msg: Netlink message to dump into
2704 * @cb: Control block containing additional options
2705 * @bat_priv: The bat priv with all the soft interface information
2706 */
2707static void batadv_iv_gw_dump(struct sk_buff *msg, struct netlink_callback *cb,
2708 struct batadv_priv *bat_priv)
2709{
2710 int portid = NETLINK_CB(cb->skb).portid;
2711 struct batadv_gw_node *gw_node;
2712 int idx_skip = cb->args[0];
2713 int idx = 0;
2714
Sven Eckelmann9264c852018-10-30 22:01:23 +01002715 spin_lock_bh(&bat_priv->gw.list_lock);
2716 cb->seq = bat_priv->gw.generation << 1 | 1;
2717
2718 hlist_for_each_entry(gw_node, &bat_priv->gw.gateway_list, list) {
Andrew Lunnefb766a2016-07-03 13:31:43 +02002719 if (idx++ < idx_skip)
2720 continue;
2721
Sven Eckelmann9264c852018-10-30 22:01:23 +01002722 if (batadv_iv_gw_dump_entry(msg, portid, cb, bat_priv,
2723 gw_node)) {
Andrew Lunnefb766a2016-07-03 13:31:43 +02002724 idx_skip = idx - 1;
2725 goto unlock;
2726 }
2727 }
2728
2729 idx_skip = idx;
2730unlock:
Sven Eckelmann9264c852018-10-30 22:01:23 +01002731 spin_unlock_bh(&bat_priv->gw.list_lock);
Andrew Lunnefb766a2016-07-03 13:31:43 +02002732
2733 cb->args[0] = idx_skip;
2734}
2735
Sven Eckelmann56303d32012-06-05 22:31:31 +02002736static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
Marek Lindner519d3492012-04-18 17:15:57 +08002737 .name = "BATMAN_IV",
Antonio Quartulli29824a52016-05-25 23:27:31 +08002738 .iface = {
Antonio Quartulli29824a52016-05-25 23:27:31 +08002739 .enable = batadv_iv_ogm_iface_enable,
Sven Eckelmann9e6b5642019-06-02 10:57:31 +02002740 .enabled = batadv_iv_iface_enabled,
Antonio Quartulli29824a52016-05-25 23:27:31 +08002741 .disable = batadv_iv_ogm_iface_disable,
2742 .update_mac = batadv_iv_ogm_iface_update_mac,
2743 .primary_set = batadv_iv_ogm_primary_iface_set,
2744 },
2745 .neigh = {
2746 .cmp = batadv_iv_ogm_neigh_cmp,
2747 .is_similar_or_better = batadv_iv_ogm_neigh_is_sob,
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002748#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Antonio Quartulli29824a52016-05-25 23:27:31 +08002749 .print = batadv_iv_neigh_print,
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002750#endif
Matthias Schiffer024f99c2016-07-03 13:31:40 +02002751 .dump = batadv_iv_ogm_neigh_dump,
Antonio Quartulli29824a52016-05-25 23:27:31 +08002752 },
2753 .orig = {
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002754#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Antonio Quartulli29824a52016-05-25 23:27:31 +08002755 .print = batadv_iv_ogm_orig_print,
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002756#endif
Matthias Schiffer024f99c2016-07-03 13:31:40 +02002757 .dump = batadv_iv_ogm_orig_dump,
Antonio Quartulli29824a52016-05-25 23:27:31 +08002758 },
Antonio Quartulli34d99cf2016-07-03 12:46:33 +02002759 .gw = {
Sven Eckelmann1a9070e2017-03-04 15:48:50 +01002760 .init_sel_class = batadv_iv_init_sel_class,
Antonio Quartulli34d99cf2016-07-03 12:46:33 +02002761 .get_best_gw_node = batadv_iv_gw_get_best_gw_node,
2762 .is_eligible = batadv_iv_gw_is_eligible,
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002763#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Antonio Quartulli34d99cf2016-07-03 12:46:33 +02002764 .print = batadv_iv_gw_print,
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002765#endif
Andrew Lunnefb766a2016-07-03 13:31:43 +02002766 .dump = batadv_iv_gw_dump,
Antonio Quartulli34d99cf2016-07-03 12:46:33 +02002767 },
Marek Lindner1c280472011-11-28 17:40:17 +08002768};
2769
Sven Eckelmannff15c272017-12-02 19:51:53 +01002770/**
2771 * batadv_iv_init() - B.A.T.M.A.N. IV initialization function
2772 *
2773 * Return: 0 on success or negative error number in case of failure
2774 */
Sven Eckelmann81c524f2012-05-12 02:09:22 +02002775int __init batadv_iv_init(void)
Marek Lindner1c280472011-11-28 17:40:17 +08002776{
Marek Lindnerc3e29312012-03-04 16:56:25 +08002777 int ret;
2778
2779 /* batman originator packet */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002780 ret = batadv_recv_handler_register(BATADV_IV_OGM,
2781 batadv_iv_ogm_receive);
Marek Lindnerc3e29312012-03-04 16:56:25 +08002782 if (ret < 0)
2783 goto out;
2784
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02002785 ret = batadv_algo_register(&batadv_batman_iv);
Marek Lindnerc3e29312012-03-04 16:56:25 +08002786 if (ret < 0)
2787 goto handler_unregister;
2788
2789 goto out;
2790
2791handler_unregister:
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002792 batadv_recv_handler_unregister(BATADV_IV_OGM);
Marek Lindnerc3e29312012-03-04 16:56:25 +08002793out:
2794 return ret;
Marek Lindner1c280472011-11-28 17:40:17 +08002795}