blob: 19b0abd6c640e26f11b26b7fd6f3eea82134e2ee [file] [log] [blame]
Sven Eckelmann0046b042016-01-01 00:01:03 +01001/* Copyright (C) 2007-2016 B.A.T.M.A.N. contributors:
Marek Lindnerfc957272011-07-30 12:04:12 +02002 *
3 * Marek Lindner, Simon Wunderlich
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
Antonio Quartulliebf38fb2013-11-03 20:40:48 +010015 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Marek Lindnerfc957272011-07-30 12:04:12 +020016 */
17
Sven Eckelmanna2d08162016-05-15 11:07:46 +020018#include "bat_iv_ogm.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020019#include "main.h"
20
21#include <linux/atomic.h>
22#include <linux/bitmap.h>
23#include <linux/bitops.h>
24#include <linux/bug.h>
25#include <linux/byteorder/generic.h>
26#include <linux/cache.h>
27#include <linux/errno.h>
28#include <linux/etherdevice.h>
29#include <linux/fs.h>
30#include <linux/if_ether.h>
31#include <linux/init.h>
32#include <linux/jiffies.h>
Antonio Quartullif0d97252016-05-03 01:45:34 +080033#include <linux/kernel.h>
Sven Eckelmann77ae32e2016-01-16 10:29:53 +010034#include <linux/kref.h>
Sven Eckelmannfcafa5e2016-05-15 11:07:42 +020035#include <linux/list.h>
Sven Eckelmann64ae74452016-02-22 22:56:34 +010036#include <linux/lockdep.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020037#include <linux/netdevice.h>
38#include <linux/pkt_sched.h>
39#include <linux/printk.h>
40#include <linux/random.h>
41#include <linux/rculist.h>
42#include <linux/rcupdate.h>
43#include <linux/seq_file.h>
44#include <linux/skbuff.h>
45#include <linux/slab.h>
46#include <linux/spinlock.h>
47#include <linux/stddef.h>
48#include <linux/string.h>
49#include <linux/types.h>
50#include <linux/workqueue.h>
51
Sven Eckelmanna2d08162016-05-15 11:07:46 +020052#include "bat_algo.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020053#include "bitarray.h"
54#include "hard-interface.h"
55#include "hash.h"
Sven Eckelmannba412082016-05-15 23:48:31 +020056#include "log.h"
Martin Hundebølld56b1702013-01-25 11:12:39 +010057#include "network-coding.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020058#include "originator.h"
59#include "packet.h"
60#include "routing.h"
61#include "send.h"
62#include "translation-table.h"
Markus Pargmann1f8dce42016-05-15 11:07:43 +020063#include "tvlv.h"
Marek Lindnerfc957272011-07-30 12:04:12 +020064
Antonio Quartullif0d97252016-05-03 01:45:34 +080065static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work);
66
Antonio Quartulli791c2a22013-08-17 12:44:44 +020067/**
Martin Hundebøll95298a92014-07-15 09:41:05 +020068 * enum batadv_dup_status - duplicate status
Markus Pargmann9f52ee12014-12-26 12:41:34 +010069 * @BATADV_NO_DUP: the packet is no duplicate
Antonio Quartulli791c2a22013-08-17 12:44:44 +020070 * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for the
71 * neighbor)
72 * @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor
73 * @BATADV_PROTECTED: originator is currently protected (after reboot)
74 */
75enum batadv_dup_status {
76 BATADV_NO_DUP = 0,
77 BATADV_ORIG_DUP,
78 BATADV_NEIGH_DUP,
79 BATADV_PROTECTED,
80};
81
Antonio Quartulli24a5dee2013-04-08 09:38:12 +020082/**
83 * batadv_ring_buffer_set - update the ring buffer with the given value
84 * @lq_recv: pointer to the ring buffer
85 * @lq_index: index to store the value at
86 * @value: value to store in the ring buffer
87 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020088static void batadv_ring_buffer_set(u8 lq_recv[], u8 *lq_index, u8 value)
Antonio Quartulli24a5dee2013-04-08 09:38:12 +020089{
90 lq_recv[*lq_index] = value;
91 *lq_index = (*lq_index + 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE;
92}
93
94/**
Markus Pargmannd491dbb2014-12-26 12:41:36 +010095 * batadv_ring_buffer_avg - compute the average of all non-zero values stored
Antonio Quartulli24a5dee2013-04-08 09:38:12 +020096 * in the given ring buffer
97 * @lq_recv: pointer to the ring buffer
98 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +020099 * Return: computed average value.
Antonio Quartulli24a5dee2013-04-08 09:38:12 +0200100 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200101static u8 batadv_ring_buffer_avg(const u8 lq_recv[])
Antonio Quartulli24a5dee2013-04-08 09:38:12 +0200102{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200103 const u8 *ptr;
104 u16 count = 0;
105 u16 i = 0;
106 u16 sum = 0;
Antonio Quartulli24a5dee2013-04-08 09:38:12 +0200107
108 ptr = lq_recv;
109
110 while (i < BATADV_TQ_GLOBAL_WINDOW_SIZE) {
111 if (*ptr != 0) {
112 count++;
113 sum += *ptr;
114 }
115
116 i++;
117 ptr++;
118 }
119
120 if (count == 0)
121 return 0;
122
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200123 return (u8)(sum / count);
Antonio Quartulli24a5dee2013-04-08 09:38:12 +0200124}
David S. Millerd98cae64e2013-06-19 16:49:39 -0700125
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200126/**
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200127 * batadv_iv_ogm_orig_free - free the private resources allocated for this
128 * orig_node
129 * @orig_node: the orig_node for which the resources have to be free'd
130 */
131static void batadv_iv_ogm_orig_free(struct batadv_orig_node *orig_node)
132{
133 kfree(orig_node->bat_iv.bcast_own);
134 kfree(orig_node->bat_iv.bcast_own_sum);
135}
136
137/**
138 * batadv_iv_ogm_orig_add_if - change the private structures of the orig_node to
139 * include the new hard-interface
140 * @orig_node: the orig_node that has to be changed
141 * @max_if_num: the current amount of interfaces
142 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200143 * Return: 0 on success, a negative error code otherwise.
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200144 */
145static int batadv_iv_ogm_orig_add_if(struct batadv_orig_node *orig_node,
146 int max_if_num)
147{
148 void *data_ptr;
Antonio Quartulli0185dda2014-05-24 06:43:47 +0200149 size_t old_size;
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200150 int ret = -ENOMEM;
151
152 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
153
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200154 old_size = (max_if_num - 1) * sizeof(unsigned long) * BATADV_NUM_WORDS;
Antonio Quartulli0185dda2014-05-24 06:43:47 +0200155 data_ptr = kmalloc_array(max_if_num,
156 BATADV_NUM_WORDS * sizeof(unsigned long),
157 GFP_ATOMIC);
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200158 if (!data_ptr)
159 goto unlock;
160
161 memcpy(data_ptr, orig_node->bat_iv.bcast_own, old_size);
162 kfree(orig_node->bat_iv.bcast_own);
163 orig_node->bat_iv.bcast_own = data_ptr;
164
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200165 data_ptr = kmalloc_array(max_if_num, sizeof(u8), GFP_ATOMIC);
Sven Eckelmannf7dcdf52016-02-22 22:56:33 +0100166 if (!data_ptr)
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200167 goto unlock;
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200168
169 memcpy(data_ptr, orig_node->bat_iv.bcast_own_sum,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200170 (max_if_num - 1) * sizeof(u8));
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200171 kfree(orig_node->bat_iv.bcast_own_sum);
172 orig_node->bat_iv.bcast_own_sum = data_ptr;
173
174 ret = 0;
175
176unlock:
177 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
178
179 return ret;
180}
181
182/**
Sven Eckelmann64ae74452016-02-22 22:56:34 +0100183 * batadv_iv_ogm_drop_bcast_own_entry - drop section of bcast_own
184 * @orig_node: the orig_node that has to be changed
185 * @max_if_num: the current amount of interfaces
186 * @del_if_num: the index of the interface being removed
187 */
188static void
189batadv_iv_ogm_drop_bcast_own_entry(struct batadv_orig_node *orig_node,
190 int max_if_num, int del_if_num)
191{
192 size_t chunk_size;
193 size_t if_offset;
194 void *data_ptr;
195
196 lockdep_assert_held(&orig_node->bat_iv.ogm_cnt_lock);
197
198 chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS;
199 data_ptr = kmalloc_array(max_if_num, chunk_size, GFP_ATOMIC);
200 if (!data_ptr)
201 /* use old buffer when new one could not be allocated */
202 data_ptr = orig_node->bat_iv.bcast_own;
203
204 /* copy first part */
205 memmove(data_ptr, orig_node->bat_iv.bcast_own, del_if_num * chunk_size);
206
207 /* copy second part */
208 if_offset = (del_if_num + 1) * chunk_size;
209 memmove((char *)data_ptr + del_if_num * chunk_size,
210 (uint8_t *)orig_node->bat_iv.bcast_own + if_offset,
211 (max_if_num - del_if_num) * chunk_size);
212
213 /* bcast_own was shrunk down in new buffer; free old one */
214 if (orig_node->bat_iv.bcast_own != data_ptr) {
215 kfree(orig_node->bat_iv.bcast_own);
216 orig_node->bat_iv.bcast_own = data_ptr;
217 }
218}
219
220/**
221 * batadv_iv_ogm_drop_bcast_own_sum_entry - drop section of bcast_own_sum
222 * @orig_node: the orig_node that has to be changed
223 * @max_if_num: the current amount of interfaces
224 * @del_if_num: the index of the interface being removed
225 */
226static void
227batadv_iv_ogm_drop_bcast_own_sum_entry(struct batadv_orig_node *orig_node,
228 int max_if_num, int del_if_num)
229{
230 size_t if_offset;
231 void *data_ptr;
232
233 lockdep_assert_held(&orig_node->bat_iv.ogm_cnt_lock);
234
235 data_ptr = kmalloc_array(max_if_num, sizeof(u8), GFP_ATOMIC);
236 if (!data_ptr)
237 /* use old buffer when new one could not be allocated */
238 data_ptr = orig_node->bat_iv.bcast_own_sum;
239
240 memmove(data_ptr, orig_node->bat_iv.bcast_own_sum,
241 del_if_num * sizeof(u8));
242
243 if_offset = (del_if_num + 1) * sizeof(u8);
244 memmove((char *)data_ptr + del_if_num * sizeof(u8),
245 orig_node->bat_iv.bcast_own_sum + if_offset,
246 (max_if_num - del_if_num) * sizeof(u8));
247
248 /* bcast_own_sum was shrunk down in new buffer; free old one */
249 if (orig_node->bat_iv.bcast_own_sum != data_ptr) {
250 kfree(orig_node->bat_iv.bcast_own_sum);
251 orig_node->bat_iv.bcast_own_sum = data_ptr;
252 }
253}
254
255/**
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200256 * batadv_iv_ogm_orig_del_if - change the private structures of the orig_node to
257 * exclude the removed interface
258 * @orig_node: the orig_node that has to be changed
259 * @max_if_num: the current amount of interfaces
260 * @del_if_num: the index of the interface being removed
261 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200262 * Return: 0 on success, a negative error code otherwise.
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200263 */
264static int batadv_iv_ogm_orig_del_if(struct batadv_orig_node *orig_node,
265 int max_if_num, int del_if_num)
266{
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200267 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
268
Sven Eckelmann64ae74452016-02-22 22:56:34 +0100269 if (max_if_num == 0) {
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200270 kfree(orig_node->bat_iv.bcast_own);
Sven Eckelmann64ae74452016-02-22 22:56:34 +0100271 kfree(orig_node->bat_iv.bcast_own_sum);
272 orig_node->bat_iv.bcast_own = NULL;
273 orig_node->bat_iv.bcast_own_sum = NULL;
274 } else {
275 batadv_iv_ogm_drop_bcast_own_entry(orig_node, max_if_num,
276 del_if_num);
277 batadv_iv_ogm_drop_bcast_own_sum_entry(orig_node, max_if_num,
278 del_if_num);
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200279 }
280
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200281 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
282
Sven Eckelmann64ae74452016-02-22 22:56:34 +0100283 return 0;
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200284}
285
286/**
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200287 * batadv_iv_ogm_orig_get - retrieve or create (if does not exist) an originator
288 * @bat_priv: the bat priv with all the soft interface information
289 * @addr: mac address of the originator
290 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200291 * Return: the originator object corresponding to the passed mac address or NULL
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200292 * on failure.
293 * If the object does not exists it is created an initialised.
294 */
295static struct batadv_orig_node *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200296batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const u8 *addr)
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200297{
298 struct batadv_orig_node *orig_node;
299 int size, hash_added;
300
301 orig_node = batadv_orig_hash_find(bat_priv, addr);
302 if (orig_node)
303 return orig_node;
304
305 orig_node = batadv_orig_node_new(bat_priv, addr);
306 if (!orig_node)
307 return NULL;
308
309 spin_lock_init(&orig_node->bat_iv.ogm_cnt_lock);
310
311 size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS;
312 orig_node->bat_iv.bcast_own = kzalloc(size, GFP_ATOMIC);
313 if (!orig_node->bat_iv.bcast_own)
314 goto free_orig_node;
315
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200316 size = bat_priv->num_ifaces * sizeof(u8);
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200317 orig_node->bat_iv.bcast_own_sum = kzalloc(size, GFP_ATOMIC);
318 if (!orig_node->bat_iv.bcast_own_sum)
Antonio Quartullia5a5cb82014-02-15 02:17:20 +0100319 goto free_orig_node;
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200320
321 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
322 batadv_choose_orig, orig_node,
323 &orig_node->hash_entry);
324 if (hash_added != 0)
Antonio Quartullia5a5cb82014-02-15 02:17:20 +0100325 goto free_orig_node;
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200326
327 return orig_node;
328
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200329free_orig_node:
Simon Wunderlichb2262df2014-02-08 16:45:06 +0100330 /* free twice, as batadv_orig_node_new sets refcount to 2 */
Sven Eckelmann5d967312016-01-17 11:01:09 +0100331 batadv_orig_node_put(orig_node);
332 batadv_orig_node_put(orig_node);
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200333
334 return NULL;
335}
336
Sven Eckelmann56303d32012-06-05 22:31:31 +0200337static struct batadv_neigh_node *
338batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200339 const u8 *neigh_addr,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200340 struct batadv_orig_node *orig_node,
Antonio Quartulli863dd7a2013-03-25 13:49:46 +0100341 struct batadv_orig_node *orig_neigh)
Marek Lindner7ae8b282012-03-01 15:35:21 +0800342{
Marek Lindner741aa062015-07-26 04:57:43 +0800343 struct batadv_neigh_node *neigh_node;
Marek Lindner7ae8b282012-03-01 15:35:21 +0800344
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800345 neigh_node = batadv_neigh_node_get_or_create(orig_node,
346 hard_iface, neigh_addr);
Marek Lindner7ae8b282012-03-01 15:35:21 +0800347 if (!neigh_node)
348 goto out;
349
Simon Wunderlich89652332013-11-13 19:14:46 +0100350 neigh_node->orig_node = orig_neigh;
Marek Lindner7ae8b282012-03-01 15:35:21 +0800351
Marek Lindner7ae8b282012-03-01 15:35:21 +0800352out:
353 return neigh_node;
354}
355
Sven Eckelmann56303d32012-06-05 22:31:31 +0200356static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200357{
Sven Eckelmann96412692012-06-05 22:31:30 +0200358 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindner14511512012-08-02 17:20:26 +0200359 unsigned char *ogm_buff;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200360 u32 random_seqno;
Marek Lindnerd7d32ec2012-02-07 17:20:46 +0800361
362 /* randomize initial seqno to avoid collision */
363 get_random_bytes(&random_seqno, sizeof(random_seqno));
Marek Lindner14511512012-08-02 17:20:26 +0200364 atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno);
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200365
Marek Lindner14511512012-08-02 17:20:26 +0200366 hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN;
367 ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC);
368 if (!ogm_buff)
Markus Pargmann42d9f2c2014-12-26 12:41:26 +0100369 return -ENOMEM;
Marek Lindner77af7572012-02-07 17:20:48 +0800370
Marek Lindner14511512012-08-02 17:20:26 +0200371 hard_iface->bat_iv.ogm_buff = ogm_buff;
372
373 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100374 batadv_ogm_packet->packet_type = BATADV_IV_OGM;
375 batadv_ogm_packet->version = BATADV_COMPAT_VERSION;
376 batadv_ogm_packet->ttl = 2;
Sven Eckelmann96412692012-06-05 22:31:30 +0200377 batadv_ogm_packet->flags = BATADV_NO_FLAGS;
Marek Lindner414254e2013-04-23 21:39:58 +0800378 batadv_ogm_packet->reserved = 0;
Sven Eckelmann96412692012-06-05 22:31:30 +0200379 batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE;
Marek Lindner77af7572012-02-07 17:20:48 +0800380
Markus Pargmann42d9f2c2014-12-26 12:41:26 +0100381 return 0;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200382}
383
Sven Eckelmann56303d32012-06-05 22:31:31 +0200384static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
Marek Lindner00a50072012-02-07 17:20:47 +0800385{
Marek Lindner14511512012-08-02 17:20:26 +0200386 kfree(hard_iface->bat_iv.ogm_buff);
387 hard_iface->bat_iv.ogm_buff = NULL;
Marek Lindner00a50072012-02-07 17:20:47 +0800388}
389
Sven Eckelmann56303d32012-06-05 22:31:31 +0200390static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface)
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200391{
Sven Eckelmann96412692012-06-05 22:31:30 +0200392 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindner14511512012-08-02 17:20:26 +0200393 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200394
Marek Lindner14511512012-08-02 17:20:26 +0200395 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100396 ether_addr_copy(batadv_ogm_packet->orig,
397 hard_iface->net_dev->dev_addr);
398 ether_addr_copy(batadv_ogm_packet->prev_sender,
399 hard_iface->net_dev->dev_addr);
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200400}
401
Sven Eckelmann56303d32012-06-05 22:31:31 +0200402static void
403batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface)
Marek Lindnerc3229392012-03-11 06:17:50 +0800404{
Sven Eckelmann96412692012-06-05 22:31:30 +0200405 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindner14511512012-08-02 17:20:26 +0200406 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
Marek Lindnerc3229392012-03-11 06:17:50 +0800407
Marek Lindner14511512012-08-02 17:20:26 +0200408 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100409 batadv_ogm_packet->ttl = BATADV_TTL;
Marek Lindnerc3229392012-03-11 06:17:50 +0800410}
411
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200412/* when do we schedule our own ogm to be sent */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200413static unsigned long
Sven Eckelmann56303d32012-06-05 22:31:31 +0200414batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200415{
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200416 unsigned int msecs;
417
418 msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
Akinobu Mitae76e4322012-12-24 11:14:07 +0900419 msecs += prandom_u32() % (2 * BATADV_JITTER);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200420
421 return jiffies + msecs_to_jiffies(msecs);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200422}
423
424/* when do we schedule a ogm packet to be sent */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200425static unsigned long batadv_iv_ogm_fwd_send_time(void)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200426{
Akinobu Mitae76e4322012-12-24 11:14:07 +0900427 return jiffies + msecs_to_jiffies(prandom_u32() % (BATADV_JITTER / 2));
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200428}
429
430/* apply hop penalty for a normal link */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200431static u8 batadv_hop_penalty(u8 tq, const struct batadv_priv *bat_priv)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200432{
433 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200434 int new_tq;
435
436 new_tq = tq * (BATADV_TQ_MAX_VALUE - hop_penalty);
437 new_tq /= BATADV_TQ_MAX_VALUE;
438
439 return new_tq;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200440}
441
Simon Wunderlich600184b2015-11-23 19:57:21 +0100442/**
443 * batadv_iv_ogm_aggr_packet - checks if there is another OGM attached
444 * @buff_pos: current position in the skb
445 * @packet_len: total length of the skb
446 * @tvlv_len: tvlv length of the previously considered OGM
447 *
448 * Return: true if there is enough space for another OGM, false otherwise.
449 */
Markus Pargmann9fd9b192014-12-26 12:41:27 +0100450static bool batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len,
451 __be16 tvlv_len)
Marek Lindnerfc957272011-07-30 12:04:12 +0200452{
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200453 int next_buff_pos = 0;
454
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200455 next_buff_pos += buff_pos + BATADV_OGM_HLEN;
Marek Lindneref261572013-04-23 21:39:57 +0800456 next_buff_pos += ntohs(tvlv_len);
Marek Lindnerfc957272011-07-30 12:04:12 +0200457
458 return (next_buff_pos <= packet_len) &&
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200459 (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
Marek Lindnerfc957272011-07-30 12:04:12 +0200460}
461
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200462/* send a batman ogm to a given interface */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200463static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet,
464 struct batadv_hard_iface *hard_iface)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200465{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200466 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Markus Pargmannde12bae2014-12-26 12:41:28 +0100467 const char *fwd_str;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200468 u8 packet_num;
469 s16 buff_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +0200470 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200471 struct sk_buff *skb;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200472 u8 *packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200473
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200474 if (hard_iface->if_status != BATADV_IF_ACTIVE)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200475 return;
476
477 packet_num = 0;
478 buff_pos = 0;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200479 packet_pos = forw_packet->skb->data;
480 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200481
482 /* adjust all flags and log packets */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200483 while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
Marek Lindneref261572013-04-23 21:39:57 +0800484 batadv_ogm_packet->tvlv_len)) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200485 /* we might have aggregated direct link packets with an
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200486 * ordinary base packet
487 */
Sven Eckelmann8de47de2012-07-08 16:32:09 +0200488 if (forw_packet->direct_link_flags & BIT(packet_num) &&
489 forw_packet->if_incoming == hard_iface)
Sven Eckelmann96412692012-06-05 22:31:30 +0200490 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200491 else
Sven Eckelmann96412692012-06-05 22:31:30 +0200492 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200493
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200494 if (packet_num > 0 || !forw_packet->own)
495 fwd_str = "Forwarding";
496 else
497 fwd_str = "Sending own";
498
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200499 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800500 "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200501 fwd_str, (packet_num > 0 ? "aggregated " : ""),
Sven Eckelmann96412692012-06-05 22:31:30 +0200502 batadv_ogm_packet->orig,
503 ntohl(batadv_ogm_packet->seqno),
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100504 batadv_ogm_packet->tq, batadv_ogm_packet->ttl,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +0200505 ((batadv_ogm_packet->flags & BATADV_DIRECTLINK) ?
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200506 "on" : "off"),
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800507 hard_iface->net_dev->name,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200508 hard_iface->net_dev->dev_addr);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200509
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200510 buff_pos += BATADV_OGM_HLEN;
Marek Lindneref261572013-04-23 21:39:57 +0800511 buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200512 packet_num++;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200513 packet_pos = forw_packet->skb->data + buff_pos;
514 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200515 }
516
517 /* create clone because function is called more than once */
518 skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
Martin Hundebøllf8214862012-04-20 17:02:45 +0200519 if (skb) {
Sven Eckelmannd69909d2012-06-03 22:19:20 +0200520 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
521 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
Martin Hundebøllf8214862012-04-20 17:02:45 +0200522 skb->len + ETH_HLEN);
Antonio Quartulli95d39272016-01-16 16:40:15 +0800523 batadv_send_broadcast_skb(skb, hard_iface);
Martin Hundebøllf8214862012-04-20 17:02:45 +0200524 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200525}
526
527/* send a batman ogm packet */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200528static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200529{
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200530 struct net_device *soft_iface;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200531 struct batadv_priv *bat_priv;
532 struct batadv_hard_iface *primary_if = NULL;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200533
534 if (!forw_packet->if_incoming) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100535 pr_err("Error - can't forward packet: incoming iface not specified\n");
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200536 goto out;
537 }
538
539 soft_iface = forw_packet->if_incoming->soft_iface;
540 bat_priv = netdev_priv(soft_iface);
541
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100542 if (WARN_ON(!forw_packet->if_outgoing))
543 goto out;
544
545 if (WARN_ON(forw_packet->if_outgoing->soft_iface != soft_iface))
546 goto out;
547
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200548 if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200549 goto out;
550
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200551 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200552 if (!primary_if)
553 goto out;
554
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100555 /* only for one specific outgoing interface */
556 batadv_iv_ogm_send_to_if(forw_packet, forw_packet->if_outgoing);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200557
558out:
559 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100560 batadv_hardif_put(primary_if);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200561}
562
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100563/**
564 * batadv_iv_ogm_can_aggregate - find out if an OGM can be aggregated on an
565 * existing forward packet
566 * @new_bat_ogm_packet: OGM packet to be aggregated
567 * @bat_priv: the bat priv with all the soft interface information
568 * @packet_len: (total) length of the OGM
569 * @send_time: timestamp (jiffies) when the packet is to be sent
Martin Hundebøll95298a92014-07-15 09:41:05 +0200570 * @directlink: true if this is a direct link packet
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100571 * @if_incoming: interface where the packet was received
572 * @if_outgoing: interface for which the retransmission should be considered
573 * @forw_packet: the forwarded packet which should be checked
574 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200575 * Return: true if new_packet can be aggregated with forw_packet
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100576 */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200577static bool
Sven Eckelmann96412692012-06-05 22:31:30 +0200578batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200579 struct batadv_priv *bat_priv,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200580 int packet_len, unsigned long send_time,
581 bool directlink,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200582 const struct batadv_hard_iface *if_incoming,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100583 const struct batadv_hard_iface *if_outgoing,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200584 const struct batadv_forw_packet *forw_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200585{
Sven Eckelmann96412692012-06-05 22:31:30 +0200586 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200587 int aggregated_bytes = forw_packet->packet_len + packet_len;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200588 struct batadv_hard_iface *primary_if = NULL;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200589 bool res = false;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200590 unsigned long aggregation_end_time;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200591
Sven Eckelmann96412692012-06-05 22:31:30 +0200592 batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200593 aggregation_end_time = send_time;
594 aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200595
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200596 /* we can aggregate the current packet to this aggregated packet
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200597 * if:
598 *
599 * - the send time is within our MAX_AGGREGATION_MS time
600 * - the resulting packet wont be bigger than
601 * MAX_AGGREGATION_BYTES
Markus Pargmann8f34b382014-12-26 12:41:29 +0100602 * otherwise aggregation is not possible
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200603 */
Markus Pargmann8f34b382014-12-26 12:41:29 +0100604 if (!time_before(send_time, forw_packet->send_time) ||
605 !time_after_eq(aggregation_end_time, forw_packet->send_time))
606 return false;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200607
Markus Pargmann8f34b382014-12-26 12:41:29 +0100608 if (aggregated_bytes > BATADV_MAX_AGGREGATION_BYTES)
609 return false;
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100610
Markus Pargmann8f34b382014-12-26 12:41:29 +0100611 /* packet is not leaving on the same interface. */
612 if (forw_packet->if_outgoing != if_outgoing)
613 return false;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200614
Markus Pargmann8f34b382014-12-26 12:41:29 +0100615 /* check aggregation compatibility
616 * -> direct link packets are broadcasted on
617 * their interface only
618 * -> aggregate packet if the current packet is
619 * a "global" packet as well as the base
620 * packet
621 */
622 primary_if = batadv_primary_if_get_selected(bat_priv);
623 if (!primary_if)
624 return false;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200625
Markus Pargmann8f34b382014-12-26 12:41:29 +0100626 /* packets without direct link flag and high TTL
627 * are flooded through the net
628 */
629 if (!directlink &&
630 !(batadv_ogm_packet->flags & BATADV_DIRECTLINK) &&
631 batadv_ogm_packet->ttl != 1 &&
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200632
Markus Pargmann8f34b382014-12-26 12:41:29 +0100633 /* own packets originating non-primary
634 * interfaces leave only that interface
635 */
636 (!forw_packet->own ||
637 forw_packet->if_incoming == primary_if)) {
638 res = true;
639 goto out;
640 }
641
642 /* if the incoming packet is sent via this one
643 * interface only - we still can aggregate
644 */
645 if (directlink &&
646 new_bat_ogm_packet->ttl == 1 &&
647 forw_packet->if_incoming == if_incoming &&
648
649 /* packets from direct neighbors or
650 * own secondary interface packets
651 * (= secondary interface packets in general)
652 */
653 (batadv_ogm_packet->flags & BATADV_DIRECTLINK ||
654 (forw_packet->own &&
655 forw_packet->if_incoming != primary_if))) {
656 res = true;
657 goto out;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200658 }
659
660out:
661 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100662 batadv_hardif_put(primary_if);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200663 return res;
664}
665
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100666/**
667 * batadv_iv_ogm_aggregate_new - create a new aggregated packet and add this
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100668 * packet to it.
669 * @packet_buff: pointer to the OGM
670 * @packet_len: (total) length of the OGM
671 * @send_time: timestamp (jiffies) when the packet is to be sent
672 * @direct_link: whether this OGM has direct link status
673 * @if_incoming: interface where the packet was received
674 * @if_outgoing: interface for which the retransmission should be considered
675 * @own_packet: true if it is a self-generated ogm
676 */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200677static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,
678 int packet_len, unsigned long send_time,
679 bool direct_link,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200680 struct batadv_hard_iface *if_incoming,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100681 struct batadv_hard_iface *if_outgoing,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200682 int own_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200683{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200684 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
685 struct batadv_forw_packet *forw_packet_aggr;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200686 unsigned char *skb_buff;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200687 unsigned int skb_size;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200688
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200689 /* own packet should always be scheduled */
690 if (!own_packet) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200691 if (!batadv_atomic_dec_not_zero(&bat_priv->batman_queue_left)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200692 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200693 "batman packet queue full\n");
Sven Eckelmann17a86912016-04-11 13:06:40 +0200694 return;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200695 }
696 }
697
698 forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC);
Markus Pargmann940d1562014-12-26 12:41:31 +0100699 if (!forw_packet_aggr)
700 goto out_nomem;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200701
Markus Pargmann940d1562014-12-26 12:41:31 +0100702 if (atomic_read(&bat_priv->aggregated_ogms) &&
703 packet_len < BATADV_MAX_AGGREGATION_BYTES)
Sven Eckelmann5b246572012-11-04 17:11:45 +0100704 skb_size = BATADV_MAX_AGGREGATION_BYTES;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200705 else
Sven Eckelmann5b246572012-11-04 17:11:45 +0100706 skb_size = packet_len;
707
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200708 skb_size += ETH_HLEN;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200709
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200710 forw_packet_aggr->skb = netdev_alloc_skb_ip_align(NULL, skb_size);
Markus Pargmann940d1562014-12-26 12:41:31 +0100711 if (!forw_packet_aggr->skb)
712 goto out_free_forw_packet;
Simon Wunderlichc54f38c2013-07-29 17:56:44 +0200713 forw_packet_aggr->skb->priority = TC_PRIO_CONTROL;
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200714 skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200715
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200716 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
717 forw_packet_aggr->packet_len = packet_len;
718 memcpy(skb_buff, packet_buff, packet_len);
719
Sven Eckelmann17a86912016-04-11 13:06:40 +0200720 kref_get(&if_incoming->refcount);
721 kref_get(&if_outgoing->refcount);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200722 forw_packet_aggr->own = own_packet;
723 forw_packet_aggr->if_incoming = if_incoming;
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100724 forw_packet_aggr->if_outgoing = if_outgoing;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200725 forw_packet_aggr->num_packets = 0;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200726 forw_packet_aggr->direct_link_flags = BATADV_NO_FLAGS;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200727 forw_packet_aggr->send_time = send_time;
728
729 /* save packet direct link flag status */
730 if (direct_link)
731 forw_packet_aggr->direct_link_flags |= 1;
732
733 /* add new packet to packet list */
734 spin_lock_bh(&bat_priv->forw_bat_list_lock);
735 hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list);
736 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
737
738 /* start timer for this packet */
739 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
Antonio Quartullif0d97252016-05-03 01:45:34 +0800740 batadv_iv_send_outstanding_bat_ogm_packet);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200741 queue_delayed_work(batadv_event_workqueue,
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200742 &forw_packet_aggr->delayed_work,
743 send_time - jiffies);
744
745 return;
Markus Pargmann940d1562014-12-26 12:41:31 +0100746out_free_forw_packet:
747 kfree(forw_packet_aggr);
748out_nomem:
749 if (!own_packet)
750 atomic_inc(&bat_priv->batman_queue_left);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200751}
752
753/* aggregate a new packet into the existing ogm packet */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200754static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200755 const unsigned char *packet_buff,
756 int packet_len, bool direct_link)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200757{
758 unsigned char *skb_buff;
Sven Eckelmann8de47de2012-07-08 16:32:09 +0200759 unsigned long new_direct_link_flag;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200760
761 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
762 memcpy(skb_buff, packet_buff, packet_len);
763 forw_packet_aggr->packet_len += packet_len;
764 forw_packet_aggr->num_packets++;
765
766 /* save packet direct link flag status */
Sven Eckelmann8de47de2012-07-08 16:32:09 +0200767 if (direct_link) {
768 new_direct_link_flag = BIT(forw_packet_aggr->num_packets);
769 forw_packet_aggr->direct_link_flags |= new_direct_link_flag;
770 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200771}
772
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100773/**
774 * batadv_iv_ogm_queue_add - queue up an OGM for transmission
775 * @bat_priv: the bat priv with all the soft interface information
776 * @packet_buff: pointer to the OGM
777 * @packet_len: (total) length of the OGM
778 * @if_incoming: interface where the packet was received
779 * @if_outgoing: interface for which the retransmission should be considered
780 * @own_packet: true if it is a self-generated ogm
781 * @send_time: timestamp (jiffies) when the packet is to be sent
782 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200783static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200784 unsigned char *packet_buff,
785 int packet_len,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200786 struct batadv_hard_iface *if_incoming,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100787 struct batadv_hard_iface *if_outgoing,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200788 int own_packet, unsigned long send_time)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200789{
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200790 /* _aggr -> pointer to the packet we want to aggregate with
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200791 * _pos -> pointer to the position in the queue
792 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200793 struct batadv_forw_packet *forw_packet_aggr = NULL;
794 struct batadv_forw_packet *forw_packet_pos = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200795 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200796 bool direct_link;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200797 unsigned long max_aggregation_jiffies;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200798
Sven Eckelmann96412692012-06-05 22:31:30 +0200799 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
Markus Pargmann56489152014-12-26 12:41:32 +0100800 direct_link = !!(batadv_ogm_packet->flags & BATADV_DIRECTLINK);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200801 max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200802
803 /* find position for the packet in the forward queue */
804 spin_lock_bh(&bat_priv->forw_bat_list_lock);
805 /* own packets are not to be aggregated */
Markus Pargmann56489152014-12-26 12:41:32 +0100806 if (atomic_read(&bat_priv->aggregated_ogms) && !own_packet) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800807 hlist_for_each_entry(forw_packet_pos,
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200808 &bat_priv->forw_bat_list, list) {
Sven Eckelmann96412692012-06-05 22:31:30 +0200809 if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200810 bat_priv, packet_len,
811 send_time, direct_link,
812 if_incoming,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100813 if_outgoing,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200814 forw_packet_pos)) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200815 forw_packet_aggr = forw_packet_pos;
816 break;
817 }
818 }
819 }
820
821 /* nothing to aggregate with - either aggregation disabled or no
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200822 * suitable aggregation packet found
823 */
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200824 if (!forw_packet_aggr) {
825 /* the following section can run without the lock */
826 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
827
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200828 /* if we could not aggregate this packet with one of the others
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200829 * we hold it back for a while, so that it might be aggregated
830 * later on
831 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200832 if (!own_packet && atomic_read(&bat_priv->aggregated_ogms))
833 send_time += max_aggregation_jiffies;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200834
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200835 batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
836 send_time, direct_link,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100837 if_incoming, if_outgoing,
838 own_packet);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200839 } else {
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200840 batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
841 packet_len, direct_link);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200842 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
843 }
844}
845
Sven Eckelmann56303d32012-06-05 22:31:31 +0200846static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200847 const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +0200848 struct batadv_ogm_packet *batadv_ogm_packet,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200849 bool is_single_hop_neigh,
850 bool is_from_best_next_hop,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100851 struct batadv_hard_iface *if_incoming,
852 struct batadv_hard_iface *if_outgoing)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200853{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200854 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200855 u16 tvlv_len;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200856
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100857 if (batadv_ogm_packet->ttl <= 1) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200858 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200859 return;
860 }
861
Marek Lindner13b25412012-03-11 06:17:53 +0800862 if (!is_from_best_next_hop) {
863 /* Mark the forwarded packet when it is not coming from our
864 * best next hop. We still need to forward the packet for our
865 * neighbor link quality detection to work in case the packet
866 * originated from a single hop neighbor. Otherwise we can
867 * simply drop the ogm.
868 */
869 if (is_single_hop_neigh)
Sven Eckelmann96412692012-06-05 22:31:30 +0200870 batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP;
Marek Lindner13b25412012-03-11 06:17:53 +0800871 else
872 return;
873 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200874
Marek Lindneref261572013-04-23 21:39:57 +0800875 tvlv_len = ntohs(batadv_ogm_packet->tvlv_len);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200876
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100877 batadv_ogm_packet->ttl--;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100878 ether_addr_copy(batadv_ogm_packet->prev_sender, ethhdr->h_source);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200879
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200880 /* apply hop penalty */
Sven Eckelmann96412692012-06-05 22:31:30 +0200881 batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200882 bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200883
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200884 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200885 "Forwarding packet: tq: %i, ttl: %i\n",
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100886 batadv_ogm_packet->tq, batadv_ogm_packet->ttl);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200887
Marek Lindner75cd33f2012-03-01 15:35:16 +0800888 if (is_single_hop_neigh)
Sven Eckelmann96412692012-06-05 22:31:30 +0200889 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200890 else
Sven Eckelmann96412692012-06-05 22:31:30 +0200891 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200892
Sven Eckelmann96412692012-06-05 22:31:30 +0200893 batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
Marek Lindneref261572013-04-23 21:39:57 +0800894 BATADV_OGM_HLEN + tvlv_len,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100895 if_incoming, if_outgoing, 0,
896 batadv_iv_ogm_fwd_send_time());
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200897}
898
Antonio Quartullid9896612013-04-17 17:44:43 +0200899/**
900 * batadv_iv_ogm_slide_own_bcast_window - bitshift own OGM broadcast windows for
901 * the given interface
902 * @hard_iface: the interface for which the windows have to be shifted
903 */
904static void
905batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
906{
907 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
908 struct batadv_hashtable *hash = bat_priv->orig_hash;
909 struct hlist_head *head;
910 struct batadv_orig_node *orig_node;
911 unsigned long *word;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200912 u32 i;
Antonio Quartullid9896612013-04-17 17:44:43 +0200913 size_t word_index;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200914 u8 *w;
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200915 int if_num;
Antonio Quartullid9896612013-04-17 17:44:43 +0200916
917 for (i = 0; i < hash->size; i++) {
918 head = &hash->table[i];
919
920 rcu_read_lock();
921 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200922 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Antonio Quartullid9896612013-04-17 17:44:43 +0200923 word_index = hard_iface->if_num * BATADV_NUM_WORDS;
Antonio Quartulli32db6aa2014-09-01 14:37:29 +0200924 word = &orig_node->bat_iv.bcast_own[word_index];
Antonio Quartullid9896612013-04-17 17:44:43 +0200925
926 batadv_bit_get_packet(bat_priv, word, 1, 0);
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200927 if_num = hard_iface->if_num;
928 w = &orig_node->bat_iv.bcast_own_sum[if_num];
Antonio Quartullid9896612013-04-17 17:44:43 +0200929 *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200930 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Antonio Quartullid9896612013-04-17 17:44:43 +0200931 }
932 rcu_read_unlock();
933 }
934}
935
Sven Eckelmann56303d32012-06-05 22:31:31 +0200936static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200937{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200938 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindner14511512012-08-02 17:20:26 +0200939 unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff;
Sven Eckelmann96412692012-06-05 22:31:30 +0200940 struct batadv_ogm_packet *batadv_ogm_packet;
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100941 struct batadv_hard_iface *primary_if, *tmp_hard_iface;
Marek Lindner14511512012-08-02 17:20:26 +0200942 int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200943 u32 seqno;
944 u16 tvlv_len = 0;
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100945 unsigned long send_time;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200946
Antonio Quartullif0d97252016-05-03 01:45:34 +0800947 if ((hard_iface->if_status == BATADV_IF_NOT_IN_USE) ||
948 (hard_iface->if_status == BATADV_IF_TO_BE_REMOVED))
949 return;
950
951 /* the interface gets activated here to avoid race conditions between
952 * the moment of activating the interface in
953 * hardif_activate_interface() where the originator mac is set and
954 * outdated packets (especially uninitialized mac addresses) in the
955 * packet queue
956 */
957 if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED)
958 hard_iface->if_status = BATADV_IF_ACTIVE;
959
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200960 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200961
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800962 if (hard_iface == primary_if) {
963 /* tt changes have to be committed before the tvlv data is
964 * appended as it may alter the tt tvlv container
965 */
966 batadv_tt_local_commit_changes(bat_priv);
Marek Lindneref261572013-04-23 21:39:57 +0800967 tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff,
968 ogm_buff_len,
969 BATADV_OGM_HLEN);
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800970 }
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800971
Marek Lindner14511512012-08-02 17:20:26 +0200972 batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff);
Marek Lindneref261572013-04-23 21:39:57 +0800973 batadv_ogm_packet->tvlv_len = htons(tvlv_len);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200974
975 /* change sequence number to network order */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200976 seqno = (u32)atomic_read(&hard_iface->bat_iv.ogm_seqno);
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200977 batadv_ogm_packet->seqno = htonl(seqno);
Marek Lindner14511512012-08-02 17:20:26 +0200978 atomic_inc(&hard_iface->bat_iv.ogm_seqno);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200979
Antonio Quartullid9896612013-04-17 17:44:43 +0200980 batadv_iv_ogm_slide_own_bcast_window(hard_iface);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200981
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100982 send_time = batadv_iv_ogm_emit_send_time(bat_priv);
983
984 if (hard_iface != primary_if) {
985 /* OGMs from secondary interfaces are only scheduled on their
986 * respective interfaces.
987 */
988 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff, *ogm_buff_len,
989 hard_iface, hard_iface, 1, send_time);
990 goto out;
991 }
992
993 /* OGMs from primary interfaces are scheduled on all
994 * interfaces.
995 */
996 rcu_read_lock();
997 list_for_each_entry_rcu(tmp_hard_iface, &batadv_hardif_list, list) {
998 if (tmp_hard_iface->soft_iface != hard_iface->soft_iface)
Sven Eckelmann87b40f52015-07-17 10:03:42 +0200999 continue;
Sven Eckelmann27353442016-03-05 16:09:16 +01001000
1001 if (!kref_get_unless_zero(&tmp_hard_iface->refcount))
1002 continue;
1003
Simon Wunderlichef0a9372013-11-13 19:14:49 +01001004 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff,
1005 *ogm_buff_len, hard_iface,
1006 tmp_hard_iface, 1, send_time);
Sven Eckelmann27353442016-03-05 16:09:16 +01001007
1008 batadv_hardif_put(tmp_hard_iface);
Simon Wunderlichef0a9372013-11-13 19:14:49 +01001009 }
1010 rcu_read_unlock();
1011
1012out:
Marek Lindnerb9dacc52011-08-03 09:09:30 +02001013 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001014 batadv_hardif_put(primary_if);
Marek Lindnerb9dacc52011-08-03 09:09:30 +02001015}
1016
Simon Wunderlich89652332013-11-13 19:14:46 +01001017/**
1018 * batadv_iv_ogm_orig_update - use OGM to update corresponding data in an
1019 * originator
1020 * @bat_priv: the bat priv with all the soft interface information
1021 * @orig_node: the orig node who originally emitted the ogm packet
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001022 * @orig_ifinfo: ifinfo for the outgoing interface of the orig_node
Simon Wunderlich89652332013-11-13 19:14:46 +01001023 * @ethhdr: Ethernet header of the OGM
1024 * @batadv_ogm_packet: the ogm packet
1025 * @if_incoming: interface where the packet was received
1026 * @if_outgoing: interface for which the retransmission should be considered
Simon Wunderlich89652332013-11-13 19:14:46 +01001027 * @dup_status: the duplicate status of this ogm packet.
1028 */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001029static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001030batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
1031 struct batadv_orig_node *orig_node,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001032 struct batadv_orig_ifinfo *orig_ifinfo,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001033 const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +02001034 const struct batadv_ogm_packet *batadv_ogm_packet,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001035 struct batadv_hard_iface *if_incoming,
Simon Wunderlich89652332013-11-13 19:14:46 +01001036 struct batadv_hard_iface *if_outgoing,
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001037 enum batadv_dup_status dup_status)
Marek Lindnerfc957272011-07-30 12:04:12 +02001038{
Simon Wunderlich89652332013-11-13 19:14:46 +01001039 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
1040 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
Sven Eckelmann4f248cf2015-06-09 20:50:49 +02001041 struct batadv_neigh_node *neigh_node = NULL;
1042 struct batadv_neigh_node *tmp_neigh_node = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001043 struct batadv_neigh_node *router = NULL;
1044 struct batadv_orig_node *orig_node_tmp;
Linus Lüssing7caf69f2012-09-18 03:01:08 +02001045 int if_num;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001046 u8 sum_orig, sum_neigh;
1047 u8 *neigh_addr;
1048 u8 tq_avg;
Marek Lindnerfc957272011-07-30 12:04:12 +02001049
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001050 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001051 "update_originator(): Searching and updating originator entry of received packet\n");
Marek Lindnerfc957272011-07-30 12:04:12 +02001052
1053 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001054 hlist_for_each_entry_rcu(tmp_neigh_node,
Marek Lindnerfc957272011-07-30 12:04:12 +02001055 &orig_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001056 neigh_addr = tmp_neigh_node->addr;
1057 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
1058 tmp_neigh_node->if_incoming == if_incoming &&
Sven Eckelmann77ae32e2016-01-16 10:29:53 +01001059 kref_get_unless_zero(&tmp_neigh_node->refcount)) {
Antonio Quartulli38dc40e2013-03-30 17:22:00 +01001060 if (WARN(neigh_node, "too many matching neigh_nodes"))
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001061 batadv_neigh_node_put(neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001062 neigh_node = tmp_neigh_node;
1063 continue;
1064 }
1065
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001066 if (dup_status != BATADV_NO_DUP)
Marek Lindnerfc957272011-07-30 12:04:12 +02001067 continue;
1068
Simon Wunderlich89652332013-11-13 19:14:46 +01001069 /* only update the entry for this outgoing interface */
1070 neigh_ifinfo = batadv_neigh_ifinfo_get(tmp_neigh_node,
1071 if_outgoing);
1072 if (!neigh_ifinfo)
1073 continue;
1074
1075 spin_lock_bh(&tmp_neigh_node->ifinfo_lock);
1076 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
1077 &neigh_ifinfo->bat_iv.tq_index, 0);
1078 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
1079 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
1080 spin_unlock_bh(&tmp_neigh_node->ifinfo_lock);
1081
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001082 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01001083 neigh_ifinfo = NULL;
Marek Lindnerfc957272011-07-30 12:04:12 +02001084 }
1085
1086 if (!neigh_node) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001087 struct batadv_orig_node *orig_tmp;
Marek Lindnerfc957272011-07-30 12:04:12 +02001088
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001089 orig_tmp = batadv_iv_ogm_orig_get(bat_priv, ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001090 if (!orig_tmp)
1091 goto unlock;
1092
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001093 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1094 ethhdr->h_source,
Antonio Quartulli863dd7a2013-03-25 13:49:46 +01001095 orig_node, orig_tmp);
Marek Lindnerfc957272011-07-30 12:04:12 +02001096
Sven Eckelmann5d967312016-01-17 11:01:09 +01001097 batadv_orig_node_put(orig_tmp);
Marek Lindnerfc957272011-07-30 12:04:12 +02001098 if (!neigh_node)
1099 goto unlock;
Markus Pargmann23badd62014-12-26 12:41:33 +01001100 } else {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001101 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001102 "Updating existing last-hop neighbor of originator\n");
Markus Pargmann23badd62014-12-26 12:41:33 +01001103 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001104
1105 rcu_read_unlock();
Simon Wunderlich89652332013-11-13 19:14:46 +01001106 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
1107 if (!neigh_ifinfo)
1108 goto out;
Marek Lindnerfc957272011-07-30 12:04:12 +02001109
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001110 neigh_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +02001111
Simon Wunderlich89652332013-11-13 19:14:46 +01001112 spin_lock_bh(&neigh_node->ifinfo_lock);
1113 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
1114 &neigh_ifinfo->bat_iv.tq_index,
Sven Eckelmann96412692012-06-05 22:31:30 +02001115 batadv_ogm_packet->tq);
Simon Wunderlich89652332013-11-13 19:14:46 +01001116 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
1117 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
1118 spin_unlock_bh(&neigh_node->ifinfo_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001119
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001120 if (dup_status == BATADV_NO_DUP) {
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001121 orig_ifinfo->last_ttl = batadv_ogm_packet->ttl;
Simon Wunderlich89652332013-11-13 19:14:46 +01001122 neigh_ifinfo->last_ttl = batadv_ogm_packet->ttl;
Marek Lindnerfc957272011-07-30 12:04:12 +02001123 }
1124
Marek Lindnerfc957272011-07-30 12:04:12 +02001125 /* if this neighbor already is our next hop there is nothing
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001126 * to change
1127 */
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001128 router = batadv_orig_router_get(orig_node, if_outgoing);
Marek Lindnerfc957272011-07-30 12:04:12 +02001129 if (router == neigh_node)
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001130 goto out;
Marek Lindnerfc957272011-07-30 12:04:12 +02001131
Simon Wunderlich89652332013-11-13 19:14:46 +01001132 if (router) {
1133 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1134 if (!router_ifinfo)
1135 goto out;
1136
1137 /* if this neighbor does not offer a better TQ we won't
1138 * consider it
1139 */
1140 if (router_ifinfo->bat_iv.tq_avg > neigh_ifinfo->bat_iv.tq_avg)
1141 goto out;
1142 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001143
1144 /* if the TQ is the same and the link not more symmetric we
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001145 * won't consider it either
1146 */
Simon Wunderlich89652332013-11-13 19:14:46 +01001147 if (router_ifinfo &&
Markus Pargmann01b97a32014-12-26 12:41:30 +01001148 neigh_ifinfo->bat_iv.tq_avg == router_ifinfo->bat_iv.tq_avg) {
Marek Lindnerfc957272011-07-30 12:04:12 +02001149 orig_node_tmp = router->orig_node;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001150 spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
Linus Lüssing7caf69f2012-09-18 03:01:08 +02001151 if_num = router->if_incoming->if_num;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001152 sum_orig = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
1153 spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001154
1155 orig_node_tmp = neigh_node->orig_node;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001156 spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
Linus Lüssing7caf69f2012-09-18 03:01:08 +02001157 if_num = neigh_node->if_incoming->if_num;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001158 sum_neigh = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
1159 spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001160
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001161 if (sum_orig >= sum_neigh)
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001162 goto out;
Marek Lindnerfc957272011-07-30 12:04:12 +02001163 }
1164
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001165 batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001166 goto out;
1167
1168unlock:
1169 rcu_read_unlock();
1170out:
1171 if (neigh_node)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001172 batadv_neigh_node_put(neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001173 if (router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001174 batadv_neigh_node_put(router);
Simon Wunderlich89652332013-11-13 19:14:46 +01001175 if (neigh_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001176 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01001177 if (router_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001178 batadv_neigh_ifinfo_put(router_ifinfo);
Marek Lindnerfc957272011-07-30 12:04:12 +02001179}
1180
Simon Wunderlich89652332013-11-13 19:14:46 +01001181/**
1182 * batadv_iv_ogm_calc_tq - calculate tq for current received ogm packet
1183 * @orig_node: the orig node who originally emitted the ogm packet
1184 * @orig_neigh_node: the orig node struct of the neighbor who sent the packet
1185 * @batadv_ogm_packet: the ogm packet
1186 * @if_incoming: interface where the packet was received
1187 * @if_outgoing: interface for which the retransmission should be considered
1188 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001189 * Return: true if the link can be considered bidirectional, false otherwise
Simon Wunderlich89652332013-11-13 19:14:46 +01001190 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001191static bool batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
1192 struct batadv_orig_node *orig_neigh_node,
1193 struct batadv_ogm_packet *batadv_ogm_packet,
1194 struct batadv_hard_iface *if_incoming,
1195 struct batadv_hard_iface *if_outgoing)
Marek Lindnerfc957272011-07-30 12:04:12 +02001196{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001197 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1198 struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
Simon Wunderlich89652332013-11-13 19:14:46 +01001199 struct batadv_neigh_ifinfo *neigh_ifinfo;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001200 u8 total_count;
1201 u8 orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001202 unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
Sven Eckelmannd285f522016-02-16 10:47:07 +01001203 int if_num;
1204 unsigned int tq_asym_penalty, inv_asym_penalty;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001205 unsigned int combined_tq;
Sven Eckelmannd285f522016-02-16 10:47:07 +01001206 unsigned int tq_iface_penalty;
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001207 bool ret = false;
Marek Lindnerfc957272011-07-30 12:04:12 +02001208
1209 /* find corresponding one hop neighbor */
1210 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001211 hlist_for_each_entry_rcu(tmp_neigh_node,
Marek Lindnerfc957272011-07-30 12:04:12 +02001212 &orig_neigh_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001213 if (!batadv_compare_eth(tmp_neigh_node->addr,
1214 orig_neigh_node->orig))
Marek Lindnerfc957272011-07-30 12:04:12 +02001215 continue;
1216
1217 if (tmp_neigh_node->if_incoming != if_incoming)
1218 continue;
1219
Sven Eckelmann77ae32e2016-01-16 10:29:53 +01001220 if (!kref_get_unless_zero(&tmp_neigh_node->refcount))
Marek Lindnerfc957272011-07-30 12:04:12 +02001221 continue;
1222
1223 neigh_node = tmp_neigh_node;
1224 break;
1225 }
1226 rcu_read_unlock();
1227
1228 if (!neigh_node)
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001229 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1230 orig_neigh_node->orig,
1231 orig_neigh_node,
Antonio Quartulli863dd7a2013-03-25 13:49:46 +01001232 orig_neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001233
1234 if (!neigh_node)
1235 goto out;
1236
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001237 /* if orig_node is direct neighbor update neigh_node last_seen */
Marek Lindnerfc957272011-07-30 12:04:12 +02001238 if (orig_node == orig_neigh_node)
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001239 neigh_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +02001240
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001241 orig_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +02001242
1243 /* find packet count of corresponding one hop neighbor */
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001244 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1245 if_num = if_incoming->if_num;
1246 orig_eq_count = orig_neigh_node->bat_iv.bcast_own_sum[if_num];
Simon Wunderlich89652332013-11-13 19:14:46 +01001247 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
1248 if (neigh_ifinfo) {
1249 neigh_rq_count = neigh_ifinfo->bat_iv.real_packet_count;
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001250 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01001251 } else {
1252 neigh_rq_count = 0;
1253 }
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001254 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001255
1256 /* pay attention to not get a value bigger than 100 % */
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001257 if (orig_eq_count > neigh_rq_count)
1258 total_count = neigh_rq_count;
1259 else
1260 total_count = orig_eq_count;
Marek Lindnerfc957272011-07-30 12:04:12 +02001261
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001262 /* if we have too few packets (too less data) we set tq_own to zero
1263 * if we receive too few packets it is not considered bidirectional
1264 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001265 if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
1266 neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
Marek Lindnerfc957272011-07-30 12:04:12 +02001267 tq_own = 0;
1268 else
1269 /* neigh_node->real_packet_count is never zero as we
1270 * only purge old information when getting new
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001271 * information
1272 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001273 tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count;
Marek Lindnerfc957272011-07-30 12:04:12 +02001274
Sven Eckelmann21a12362012-03-07 09:07:46 +01001275 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
Marek Lindnerfc957272011-07-30 12:04:12 +02001276 * affect the nearly-symmetric links only a little, but
1277 * punishes asymmetric links more. This will give a value
1278 * between 0 and TQ_MAX_VALUE
1279 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001280 neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
1281 neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
1282 neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
1283 BATADV_TQ_LOCAL_WINDOW_SIZE *
1284 BATADV_TQ_LOCAL_WINDOW_SIZE;
1285 inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
1286 inv_asym_penalty /= neigh_rq_max_cube;
1287 tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;
Marek Lindnerfc957272011-07-30 12:04:12 +02001288
Simon Wunderlichc0398762013-11-13 19:14:48 +01001289 /* penalize if the OGM is forwarded on the same interface. WiFi
1290 * interfaces and other half duplex devices suffer from throughput
1291 * drops as they can't send and receive at the same time.
1292 */
1293 tq_iface_penalty = BATADV_TQ_MAX_VALUE;
1294 if (if_outgoing && (if_incoming == if_outgoing) &&
1295 batadv_is_wifi_netdev(if_outgoing->net_dev))
1296 tq_iface_penalty = batadv_hop_penalty(BATADV_TQ_MAX_VALUE,
1297 bat_priv);
1298
1299 combined_tq = batadv_ogm_packet->tq *
1300 tq_own *
1301 tq_asym_penalty *
1302 tq_iface_penalty;
1303 combined_tq /= BATADV_TQ_MAX_VALUE *
1304 BATADV_TQ_MAX_VALUE *
1305 BATADV_TQ_MAX_VALUE;
Sven Eckelmann96412692012-06-05 22:31:30 +02001306 batadv_ogm_packet->tq = combined_tq;
Marek Lindnerfc957272011-07-30 12:04:12 +02001307
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001308 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Simon Wunderlichc0398762013-11-13 19:14:48 +01001309 "bidirectional: orig = %-15pM neigh = %-15pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, iface_penalty: %3i, total tq: %3i, if_incoming = %s, if_outgoing = %s\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001310 orig_node->orig, orig_neigh_node->orig, total_count,
Simon Wunderlichc0398762013-11-13 19:14:48 +01001311 neigh_rq_count, tq_own, tq_asym_penalty, tq_iface_penalty,
1312 batadv_ogm_packet->tq, if_incoming->net_dev->name,
1313 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT");
Marek Lindnerfc957272011-07-30 12:04:12 +02001314
1315 /* if link has the minimum required transmission quality
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001316 * consider it bidirectional
1317 */
Sven Eckelmann96412692012-06-05 22:31:30 +02001318 if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001319 ret = true;
Marek Lindnerfc957272011-07-30 12:04:12 +02001320
1321out:
1322 if (neigh_node)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001323 batadv_neigh_node_put(neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001324 return ret;
1325}
1326
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001327/**
1328 * batadv_iv_ogm_update_seqnos - process a batman packet for all interfaces,
1329 * adjust the sequence number and find out whether it is a duplicate
1330 * @ethhdr: ethernet header of the packet
1331 * @batadv_ogm_packet: OGM packet to be considered
1332 * @if_incoming: interface on which the OGM packet was received
Simon Wunderlich89652332013-11-13 19:14:46 +01001333 * @if_outgoing: interface for which the retransmission should be considered
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001334 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001335 * Return: duplicate status as enum batadv_dup_status
Marek Lindnerfc957272011-07-30 12:04:12 +02001336 */
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001337static enum batadv_dup_status
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001338batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +02001339 const struct batadv_ogm_packet *batadv_ogm_packet,
Simon Wunderlich89652332013-11-13 19:14:46 +01001340 const struct batadv_hard_iface *if_incoming,
1341 struct batadv_hard_iface *if_outgoing)
Marek Lindnerfc957272011-07-30 12:04:12 +02001342{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001343 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1344 struct batadv_orig_node *orig_node;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001345 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
Simon Wunderlich89652332013-11-13 19:14:46 +01001346 struct batadv_neigh_node *neigh_node;
1347 struct batadv_neigh_ifinfo *neigh_ifinfo;
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001348 bool is_dup;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001349 s32 seq_diff;
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001350 bool need_update = false;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001351 int set_mark;
1352 enum batadv_dup_status ret = BATADV_NO_DUP;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001353 u32 seqno = ntohl(batadv_ogm_packet->seqno);
1354 u8 *neigh_addr;
1355 u8 packet_count;
Antonio Quartulli0538f752013-09-02 12:15:01 +02001356 unsigned long *bitmap;
Marek Lindnerfc957272011-07-30 12:04:12 +02001357
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001358 orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
Marek Lindnerfc957272011-07-30 12:04:12 +02001359 if (!orig_node)
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001360 return BATADV_NO_DUP;
Marek Lindnerfc957272011-07-30 12:04:12 +02001361
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001362 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1363 if (WARN_ON(!orig_ifinfo)) {
Sven Eckelmann5d967312016-01-17 11:01:09 +01001364 batadv_orig_node_put(orig_node);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001365 return 0;
1366 }
1367
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001368 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001369 seq_diff = seqno - orig_ifinfo->last_real_seqno;
Marek Lindnerfc957272011-07-30 12:04:12 +02001370
1371 /* signalize caller that the packet is to be dropped. */
Antonio Quartulli1e5cc262012-02-26 15:39:42 +01001372 if (!hlist_empty(&orig_node->neigh_list) &&
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001373 batadv_window_protected(bat_priv, seq_diff,
Simon Wunderlich81f02682015-11-23 19:57:22 +01001374 BATADV_TQ_LOCAL_WINDOW_SIZE,
1375 &orig_ifinfo->batman_seqno_reset, NULL)) {
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001376 ret = BATADV_PROTECTED;
Marek Lindnerfc957272011-07-30 12:04:12 +02001377 goto out;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001378 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001379
1380 rcu_read_lock();
Simon Wunderlich89652332013-11-13 19:14:46 +01001381 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1382 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node,
1383 if_outgoing);
1384 if (!neigh_ifinfo)
1385 continue;
1386
1387 neigh_addr = neigh_node->addr;
1388 is_dup = batadv_test_bit(neigh_ifinfo->bat_iv.real_bits,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001389 orig_ifinfo->last_real_seqno,
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001390 seqno);
1391
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001392 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
Simon Wunderlich89652332013-11-13 19:14:46 +01001393 neigh_node->if_incoming == if_incoming) {
Marek Lindnerfc957272011-07-30 12:04:12 +02001394 set_mark = 1;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001395 if (is_dup)
1396 ret = BATADV_NEIGH_DUP;
1397 } else {
Marek Lindnerfc957272011-07-30 12:04:12 +02001398 set_mark = 0;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001399 if (is_dup && (ret != BATADV_NEIGH_DUP))
1400 ret = BATADV_ORIG_DUP;
1401 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001402
1403 /* if the window moved, set the update flag. */
Simon Wunderlich89652332013-11-13 19:14:46 +01001404 bitmap = neigh_ifinfo->bat_iv.real_bits;
Antonio Quartulli0538f752013-09-02 12:15:01 +02001405 need_update |= batadv_bit_get_packet(bat_priv, bitmap,
Sven Eckelmann0f5f9322012-05-12 02:09:25 +02001406 seq_diff, set_mark);
Marek Lindnerfc957272011-07-30 12:04:12 +02001407
Simon Wunderlich89652332013-11-13 19:14:46 +01001408 packet_count = bitmap_weight(bitmap,
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001409 BATADV_TQ_LOCAL_WINDOW_SIZE);
Simon Wunderlich89652332013-11-13 19:14:46 +01001410 neigh_ifinfo->bat_iv.real_packet_count = packet_count;
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001411 batadv_neigh_ifinfo_put(neigh_ifinfo);
Marek Lindnerfc957272011-07-30 12:04:12 +02001412 }
1413 rcu_read_unlock();
1414
1415 if (need_update) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001416 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001417 "%s updating last_seqno: old %u, new %u\n",
1418 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT",
1419 orig_ifinfo->last_real_seqno, seqno);
1420 orig_ifinfo->last_real_seqno = seqno;
Marek Lindnerfc957272011-07-30 12:04:12 +02001421 }
1422
Marek Lindnerfc957272011-07-30 12:04:12 +02001423out:
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001424 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Sven Eckelmann5d967312016-01-17 11:01:09 +01001425 batadv_orig_node_put(orig_node);
Sven Eckelmann35f94772016-01-17 11:01:13 +01001426 batadv_orig_ifinfo_put(orig_ifinfo);
Marek Lindnerfc957272011-07-30 12:04:12 +02001427 return ret;
1428}
1429
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001430/**
1431 * batadv_iv_ogm_process_per_outif - process a batman iv OGM for an outgoing if
1432 * @skb: the skb containing the OGM
Martin Hundebøll95298a92014-07-15 09:41:05 +02001433 * @ogm_offset: offset from skb->data to start of ogm header
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001434 * @orig_node: the (cached) orig node for the originator of this OGM
1435 * @if_incoming: the interface where this packet was received
1436 * @if_outgoing: the interface for which the packet should be considered
1437 */
1438static void
1439batadv_iv_ogm_process_per_outif(const struct sk_buff *skb, int ogm_offset,
1440 struct batadv_orig_node *orig_node,
1441 struct batadv_hard_iface *if_incoming,
1442 struct batadv_hard_iface *if_outgoing)
1443{
1444 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Marek Lindner4ff1e2a2015-08-04 21:09:58 +08001445 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
Sven Eckelmann4f248cf2015-06-09 20:50:49 +02001446 struct batadv_neigh_node *router = NULL;
1447 struct batadv_neigh_node *router_router = NULL;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001448 struct batadv_orig_node *orig_neigh_node;
1449 struct batadv_orig_ifinfo *orig_ifinfo;
1450 struct batadv_neigh_node *orig_neigh_router = NULL;
1451 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
1452 struct batadv_ogm_packet *ogm_packet;
1453 enum batadv_dup_status dup_status;
1454 bool is_from_best_next_hop = false;
1455 bool is_single_hop_neigh = false;
1456 bool sameseq, similar_ttl;
1457 struct sk_buff *skb_priv;
1458 struct ethhdr *ethhdr;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001459 u8 *prev_sender;
Sven Eckelmann4b426b12016-02-22 21:02:39 +01001460 bool is_bidirect;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001461
1462 /* create a private copy of the skb, as some functions change tq value
1463 * and/or flags.
1464 */
1465 skb_priv = skb_copy(skb, GFP_ATOMIC);
1466 if (!skb_priv)
1467 return;
1468
1469 ethhdr = eth_hdr(skb_priv);
1470 ogm_packet = (struct batadv_ogm_packet *)(skb_priv->data + ogm_offset);
1471
1472 dup_status = batadv_iv_ogm_update_seqnos(ethhdr, ogm_packet,
1473 if_incoming, if_outgoing);
1474 if (batadv_compare_eth(ethhdr->h_source, ogm_packet->orig))
1475 is_single_hop_neigh = true;
1476
1477 if (dup_status == BATADV_PROTECTED) {
1478 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1479 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1480 ethhdr->h_source);
1481 goto out;
1482 }
1483
1484 if (ogm_packet->tq == 0) {
1485 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1486 "Drop packet: originator packet with tq equal 0\n");
1487 goto out;
1488 }
1489
Marek Lindner4ff1e2a2015-08-04 21:09:58 +08001490 if (is_single_hop_neigh) {
1491 hardif_neigh = batadv_hardif_neigh_get(if_incoming,
1492 ethhdr->h_source);
1493 if (hardif_neigh)
1494 hardif_neigh->last_seen = jiffies;
1495 }
1496
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001497 router = batadv_orig_router_get(orig_node, if_outgoing);
1498 if (router) {
1499 router_router = batadv_orig_router_get(router->orig_node,
1500 if_outgoing);
1501 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1502 }
1503
1504 if ((router_ifinfo && router_ifinfo->bat_iv.tq_avg != 0) &&
1505 (batadv_compare_eth(router->addr, ethhdr->h_source)))
1506 is_from_best_next_hop = true;
1507
1508 prev_sender = ogm_packet->prev_sender;
1509 /* avoid temporary routing loops */
1510 if (router && router_router &&
1511 (batadv_compare_eth(router->addr, prev_sender)) &&
1512 !(batadv_compare_eth(ogm_packet->orig, prev_sender)) &&
1513 (batadv_compare_eth(router->addr, router_router->addr))) {
1514 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1515 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1516 ethhdr->h_source);
1517 goto out;
1518 }
1519
1520 if (if_outgoing == BATADV_IF_DEFAULT)
1521 batadv_tvlv_ogm_receive(bat_priv, ogm_packet, orig_node);
1522
1523 /* if sender is a direct neighbor the sender mac equals
1524 * originator mac
1525 */
1526 if (is_single_hop_neigh)
1527 orig_neigh_node = orig_node;
1528 else
1529 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1530 ethhdr->h_source);
1531
1532 if (!orig_neigh_node)
1533 goto out;
1534
1535 /* Update nc_nodes of the originator */
1536 batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node,
1537 ogm_packet, is_single_hop_neigh);
1538
1539 orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
1540 if_outgoing);
1541
1542 /* drop packet if sender is not a direct neighbor and if we
1543 * don't route towards it
1544 */
1545 if (!is_single_hop_neigh && (!orig_neigh_router)) {
1546 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1547 "Drop packet: OGM via unknown neighbor!\n");
1548 goto out_neigh;
1549 }
1550
1551 is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
1552 ogm_packet, if_incoming,
1553 if_outgoing);
1554
1555 /* update ranking if it is not a duplicate or has the same
1556 * seqno and similar ttl as the non-duplicate
1557 */
1558 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1559 if (!orig_ifinfo)
1560 goto out_neigh;
1561
1562 sameseq = orig_ifinfo->last_real_seqno == ntohl(ogm_packet->seqno);
1563 similar_ttl = (orig_ifinfo->last_ttl - 3) <= ogm_packet->ttl;
1564
1565 if (is_bidirect && ((dup_status == BATADV_NO_DUP) ||
1566 (sameseq && similar_ttl))) {
1567 batadv_iv_ogm_orig_update(bat_priv, orig_node,
1568 orig_ifinfo, ethhdr,
1569 ogm_packet, if_incoming,
1570 if_outgoing, dup_status);
1571 }
Sven Eckelmann35f94772016-01-17 11:01:13 +01001572 batadv_orig_ifinfo_put(orig_ifinfo);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001573
Simon Wunderlichef0a9372013-11-13 19:14:49 +01001574 /* only forward for specific interface, not for the default one. */
1575 if (if_outgoing == BATADV_IF_DEFAULT)
1576 goto out_neigh;
1577
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001578 /* is single hop (direct) neighbor */
1579 if (is_single_hop_neigh) {
1580 /* OGMs from secondary interfaces should only scheduled once
1581 * per interface where it has been received, not multiple times
1582 */
1583 if ((ogm_packet->ttl <= 2) &&
1584 (if_incoming != if_outgoing)) {
1585 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1586 "Drop packet: OGM from secondary interface and wrong outgoing interface\n");
1587 goto out_neigh;
1588 }
1589 /* mark direct link on incoming interface */
1590 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1591 is_single_hop_neigh,
Simon Wunderlichef0a9372013-11-13 19:14:49 +01001592 is_from_best_next_hop, if_incoming,
1593 if_outgoing);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001594
1595 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1596 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
1597 goto out_neigh;
1598 }
1599
1600 /* multihop originator */
1601 if (!is_bidirect) {
1602 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1603 "Drop packet: not received via bidirectional link\n");
1604 goto out_neigh;
1605 }
1606
1607 if (dup_status == BATADV_NEIGH_DUP) {
1608 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1609 "Drop packet: duplicate packet received\n");
1610 goto out_neigh;
1611 }
1612
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001613 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1614 "Forwarding packet: rebroadcast originator packet\n");
1615 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1616 is_single_hop_neigh, is_from_best_next_hop,
Simon Wunderlichef0a9372013-11-13 19:14:49 +01001617 if_incoming, if_outgoing);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001618
1619out_neigh:
1620 if ((orig_neigh_node) && (!is_single_hop_neigh))
Sven Eckelmann5d967312016-01-17 11:01:09 +01001621 batadv_orig_node_put(orig_neigh_node);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001622out:
Simon Wunderlichc1e517f2014-03-26 15:46:21 +01001623 if (router_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001624 batadv_neigh_ifinfo_put(router_ifinfo);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001625 if (router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001626 batadv_neigh_node_put(router);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001627 if (router_router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001628 batadv_neigh_node_put(router_router);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001629 if (orig_neigh_router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001630 batadv_neigh_node_put(orig_neigh_router);
Marek Lindner4ff1e2a2015-08-04 21:09:58 +08001631 if (hardif_neigh)
Sven Eckelmannaccadc32016-01-17 11:01:14 +01001632 batadv_hardif_neigh_put(hardif_neigh);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001633
1634 kfree_skb(skb_priv);
1635}
1636
1637/**
1638 * batadv_iv_ogm_process - process an incoming batman iv OGM
1639 * @skb: the skb containing the OGM
1640 * @ogm_offset: offset to the OGM which should be processed (for aggregates)
1641 * @if_incoming: the interface where this packet was receved
1642 */
1643static void batadv_iv_ogm_process(const struct sk_buff *skb, int ogm_offset,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001644 struct batadv_hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +02001645{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001646 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001647 struct batadv_orig_node *orig_neigh_node, *orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001648 struct batadv_hard_iface *hard_iface;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001649 struct batadv_ogm_packet *ogm_packet;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001650 u32 if_incoming_seqno;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001651 bool has_directlink_flag;
1652 struct ethhdr *ethhdr;
1653 bool is_my_oldorig = false;
1654 bool is_my_addr = false;
1655 bool is_my_orig = false;
1656
1657 ogm_packet = (struct batadv_ogm_packet *)(skb->data + ogm_offset);
1658 ethhdr = eth_hdr(skb);
Marek Lindnerfc957272011-07-30 12:04:12 +02001659
1660 /* Silently drop when the batman packet is actually not a
1661 * correct packet.
1662 *
1663 * This might happen if a packet is padded (e.g. Ethernet has a
1664 * minimum frame length of 64 byte) and the aggregation interprets
1665 * it as an additional length.
1666 *
1667 * TODO: A more sane solution would be to have a bit in the
Sven Eckelmann96412692012-06-05 22:31:30 +02001668 * batadv_ogm_packet to detect whether the packet is the last
Marek Lindnerfc957272011-07-30 12:04:12 +02001669 * packet in an aggregation. Here we expect that the padding
1670 * is always zero (or not 0x01)
1671 */
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001672 if (ogm_packet->packet_type != BATADV_IV_OGM)
Marek Lindnerfc957272011-07-30 12:04:12 +02001673 return;
1674
1675 /* could be changed by schedule_own_packet() */
Marek Lindner14511512012-08-02 17:20:26 +02001676 if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno);
Marek Lindnerfc957272011-07-30 12:04:12 +02001677
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001678 if (ogm_packet->flags & BATADV_DIRECTLINK)
1679 has_directlink_flag = true;
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001680 else
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001681 has_directlink_flag = false;
Marek Lindnerfc957272011-07-30 12:04:12 +02001682
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001683 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001684 "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 +02001685 ethhdr->h_source, if_incoming->net_dev->name,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001686 if_incoming->net_dev->dev_addr, ogm_packet->orig,
1687 ogm_packet->prev_sender, ntohl(ogm_packet->seqno),
1688 ogm_packet->tq, ogm_packet->ttl,
1689 ogm_packet->version, has_directlink_flag);
Marek Lindnerfc957272011-07-30 12:04:12 +02001690
1691 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001692 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001693 if (hard_iface->if_status != BATADV_IF_ACTIVE)
Marek Lindnerfc957272011-07-30 12:04:12 +02001694 continue;
1695
1696 if (hard_iface->soft_iface != if_incoming->soft_iface)
1697 continue;
1698
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001699 if (batadv_compare_eth(ethhdr->h_source,
1700 hard_iface->net_dev->dev_addr))
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001701 is_my_addr = true;
Marek Lindnerfc957272011-07-30 12:04:12 +02001702
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001703 if (batadv_compare_eth(ogm_packet->orig,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001704 hard_iface->net_dev->dev_addr))
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001705 is_my_orig = true;
Marek Lindnerfc957272011-07-30 12:04:12 +02001706
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001707 if (batadv_compare_eth(ogm_packet->prev_sender,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001708 hard_iface->net_dev->dev_addr))
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001709 is_my_oldorig = true;
Marek Lindnerfc957272011-07-30 12:04:12 +02001710 }
1711 rcu_read_unlock();
1712
Marek Lindnerfc957272011-07-30 12:04:12 +02001713 if (is_my_addr) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001714 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001715 "Drop packet: received my own broadcast (sender: %pM)\n",
1716 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001717 return;
1718 }
1719
Marek Lindnerfc957272011-07-30 12:04:12 +02001720 if (is_my_orig) {
1721 unsigned long *word;
1722 int offset;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001723 s32 bit_pos;
1724 s16 if_num;
1725 u8 *weight;
Marek Lindnerfc957272011-07-30 12:04:12 +02001726
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001727 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1728 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001729 if (!orig_neigh_node)
1730 return;
1731
1732 /* neighbor has to indicate direct link and it has to
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001733 * come via the corresponding interface
1734 * save packet seqno for bidirectional check
1735 */
Marek Lindnerfc957272011-07-30 12:04:12 +02001736 if (has_directlink_flag &&
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001737 batadv_compare_eth(if_incoming->net_dev->dev_addr,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001738 ogm_packet->orig)) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001739 if_num = if_incoming->if_num;
1740 offset = if_num * BATADV_NUM_WORDS;
Marek Lindnerfc957272011-07-30 12:04:12 +02001741
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001742 spin_lock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
Antonio Quartulli32db6aa2014-09-01 14:37:29 +02001743 word = &orig_neigh_node->bat_iv.bcast_own[offset];
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001744 bit_pos = if_incoming_seqno - 2;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001745 bit_pos -= ntohl(ogm_packet->seqno);
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001746 batadv_set_bit(word, bit_pos);
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001747 weight = &orig_neigh_node->bat_iv.bcast_own_sum[if_num];
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001748 *weight = bitmap_weight(word,
1749 BATADV_TQ_LOCAL_WINDOW_SIZE);
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001750 spin_unlock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001751 }
1752
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001753 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001754 "Drop packet: originator packet from myself (via neighbor)\n");
Sven Eckelmann5d967312016-01-17 11:01:09 +01001755 batadv_orig_node_put(orig_neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001756 return;
1757 }
1758
1759 if (is_my_oldorig) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001760 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001761 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1762 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001763 return;
1764 }
1765
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001766 if (ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001767 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001768 "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1769 ethhdr->h_source);
Marek Lindner13b25412012-03-11 06:17:53 +08001770 return;
1771 }
1772
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001773 orig_node = batadv_iv_ogm_orig_get(bat_priv, ogm_packet->orig);
Marek Lindnerfc957272011-07-30 12:04:12 +02001774 if (!orig_node)
1775 return;
1776
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001777 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1778 if_incoming, BATADV_IF_DEFAULT);
Marek Lindnerfc957272011-07-30 12:04:12 +02001779
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001780 rcu_read_lock();
1781 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1782 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1783 continue;
1784
1785 if (hard_iface->soft_iface != bat_priv->soft_iface)
1786 continue;
1787
Sven Eckelmann27353442016-03-05 16:09:16 +01001788 if (!kref_get_unless_zero(&hard_iface->refcount))
1789 continue;
1790
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001791 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1792 if_incoming, hard_iface);
Sven Eckelmann27353442016-03-05 16:09:16 +01001793
1794 batadv_hardif_put(hard_iface);
Marek Lindnerfc957272011-07-30 12:04:12 +02001795 }
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001796 rcu_read_unlock();
Marek Lindnerfc957272011-07-30 12:04:12 +02001797
Sven Eckelmann5d967312016-01-17 11:01:09 +01001798 batadv_orig_node_put(orig_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001799}
1800
Antonio Quartullif0d97252016-05-03 01:45:34 +08001801static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work)
1802{
1803 struct delayed_work *delayed_work;
1804 struct batadv_forw_packet *forw_packet;
1805 struct batadv_priv *bat_priv;
1806
1807 delayed_work = to_delayed_work(work);
1808 forw_packet = container_of(delayed_work, struct batadv_forw_packet,
1809 delayed_work);
1810 bat_priv = netdev_priv(forw_packet->if_incoming->soft_iface);
1811 spin_lock_bh(&bat_priv->forw_bat_list_lock);
1812 hlist_del(&forw_packet->list);
1813 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
1814
1815 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
1816 goto out;
1817
1818 batadv_iv_ogm_emit(forw_packet);
1819
1820 /* we have to have at least one packet in the queue to determine the
1821 * queues wake up time unless we are shutting down.
1822 *
1823 * only re-schedule if this is the "original" copy, e.g. the OGM of the
1824 * primary interface should only be rescheduled once per period, but
1825 * this function will be called for the forw_packet instances of the
1826 * other secondary interfaces as well.
1827 */
1828 if (forw_packet->own &&
1829 forw_packet->if_incoming == forw_packet->if_outgoing)
1830 batadv_iv_ogm_schedule(forw_packet->if_incoming);
1831
1832out:
1833 /* don't count own packet */
1834 if (!forw_packet->own)
1835 atomic_inc(&bat_priv->batman_queue_left);
1836
1837 batadv_forw_packet_free(forw_packet);
1838}
1839
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001840static int batadv_iv_ogm_receive(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001841 struct batadv_hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +02001842{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001843 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001844 struct batadv_ogm_packet *ogm_packet;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001845 u8 *packet_pos;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001846 int ogm_offset;
Marek Lindneref261572013-04-23 21:39:57 +08001847 bool ret;
Marek Lindnerc3e29312012-03-04 16:56:25 +08001848
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001849 ret = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
Marek Lindnerc3e29312012-03-04 16:56:25 +08001850 if (!ret)
1851 return NET_RX_DROP;
Marek Lindnerfc957272011-07-30 12:04:12 +02001852
Marek Lindneredbf12b2012-03-11 06:17:49 +08001853 /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
1854 * that does not have B.A.T.M.A.N. IV enabled ?
1855 */
Antonio Quartulli29824a52016-05-25 23:27:31 +08001856 if (bat_priv->algo_ops->iface.enable != batadv_iv_ogm_iface_enable)
Marek Lindneredbf12b2012-03-11 06:17:49 +08001857 return NET_RX_DROP;
1858
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001859 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
1860 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
Martin Hundebøllf8214862012-04-20 17:02:45 +02001861 skb->len + ETH_HLEN);
1862
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001863 ogm_offset = 0;
1864 ogm_packet = (struct batadv_ogm_packet *)skb->data;
Marek Lindnerfc957272011-07-30 12:04:12 +02001865
1866 /* unpack the aggregated packets and process them one by one */
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001867 while (batadv_iv_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
1868 ogm_packet->tvlv_len)) {
1869 batadv_iv_ogm_process(skb, ogm_offset, if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001870
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001871 ogm_offset += BATADV_OGM_HLEN;
1872 ogm_offset += ntohs(ogm_packet->tvlv_len);
Marek Lindnerfc957272011-07-30 12:04:12 +02001873
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001874 packet_pos = skb->data + ogm_offset;
1875 ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Marek Lindnerb47506d2013-03-04 10:39:49 +08001876 }
Marek Lindnerc3e29312012-03-04 16:56:25 +08001877
1878 kfree_skb(skb);
1879 return NET_RX_SUCCESS;
Marek Lindnerfc957272011-07-30 12:04:12 +02001880}
Marek Lindner1c280472011-11-28 17:40:17 +08001881
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001882/**
1883 * batadv_iv_ogm_orig_print_neigh - print neighbors for the originator table
Simon Wunderlich89652332013-11-13 19:14:46 +01001884 * @orig_node: the orig_node for which the neighbors are printed
1885 * @if_outgoing: outgoing interface for these entries
1886 * @seq: debugfs table seq_file struct
1887 *
1888 * Must be called while holding an rcu lock.
1889 */
1890static void
1891batadv_iv_ogm_orig_print_neigh(struct batadv_orig_node *orig_node,
1892 struct batadv_hard_iface *if_outgoing,
1893 struct seq_file *seq)
1894{
1895 struct batadv_neigh_node *neigh_node;
1896 struct batadv_neigh_ifinfo *n_ifinfo;
1897
1898 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1899 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
1900 if (!n_ifinfo)
1901 continue;
1902
1903 seq_printf(seq, " %pM (%3i)",
1904 neigh_node->addr,
1905 n_ifinfo->bat_iv.tq_avg);
1906
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001907 batadv_neigh_ifinfo_put(n_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01001908 }
1909}
1910
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001911/**
1912 * batadv_iv_ogm_orig_print - print the originator table
1913 * @bat_priv: the bat priv with all the soft interface information
1914 * @seq: debugfs table seq_file struct
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001915 * @if_outgoing: the outgoing interface for which this should be printed
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001916 */
1917static void batadv_iv_ogm_orig_print(struct batadv_priv *bat_priv,
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001918 struct seq_file *seq,
1919 struct batadv_hard_iface *if_outgoing)
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001920{
Simon Wunderlich89652332013-11-13 19:14:46 +01001921 struct batadv_neigh_node *neigh_node;
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001922 struct batadv_hashtable *hash = bat_priv->orig_hash;
1923 int last_seen_msecs, last_seen_secs;
1924 struct batadv_orig_node *orig_node;
Simon Wunderlich89652332013-11-13 19:14:46 +01001925 struct batadv_neigh_ifinfo *n_ifinfo;
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001926 unsigned long last_seen_jiffies;
1927 struct hlist_head *head;
1928 int batman_count = 0;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001929 u32 i;
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001930
Antonio Quartulli925a6f32016-03-12 10:30:18 +01001931 seq_puts(seq,
1932 " Originator last-seen (#/255) Nexthop [outgoingIF]: Potential nexthops ...\n");
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001933
1934 for (i = 0; i < hash->size; i++) {
1935 head = &hash->table[i];
1936
1937 rcu_read_lock();
1938 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001939 neigh_node = batadv_orig_router_get(orig_node,
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001940 if_outgoing);
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001941 if (!neigh_node)
1942 continue;
1943
Simon Wunderlich89652332013-11-13 19:14:46 +01001944 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node,
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001945 if_outgoing);
Simon Wunderlich89652332013-11-13 19:14:46 +01001946 if (!n_ifinfo)
1947 goto next;
1948
1949 if (n_ifinfo->bat_iv.tq_avg == 0)
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001950 goto next;
1951
1952 last_seen_jiffies = jiffies - orig_node->last_seen;
1953 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
1954 last_seen_secs = last_seen_msecs / 1000;
1955 last_seen_msecs = last_seen_msecs % 1000;
1956
1957 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
1958 orig_node->orig, last_seen_secs,
Simon Wunderlich89652332013-11-13 19:14:46 +01001959 last_seen_msecs, n_ifinfo->bat_iv.tq_avg,
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001960 neigh_node->addr,
1961 neigh_node->if_incoming->net_dev->name);
1962
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001963 batadv_iv_ogm_orig_print_neigh(orig_node, if_outgoing,
1964 seq);
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001965 seq_puts(seq, "\n");
1966 batman_count++;
1967
1968next:
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001969 batadv_neigh_node_put(neigh_node);
Simon Wunderlich89652332013-11-13 19:14:46 +01001970 if (n_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001971 batadv_neigh_ifinfo_put(n_ifinfo);
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001972 }
1973 rcu_read_unlock();
1974 }
1975
1976 if (batman_count == 0)
1977 seq_puts(seq, "No batman nodes in range ...\n");
1978}
1979
Antonio Quartullia3285a82013-09-02 12:15:04 +02001980/**
Marek Lindner75874052015-08-04 21:09:57 +08001981 * batadv_iv_hardif_neigh_print - print a single hop neighbour node
1982 * @seq: neighbour table seq_file struct
1983 * @hardif_neigh: hardif neighbour information
1984 */
1985static void
1986batadv_iv_hardif_neigh_print(struct seq_file *seq,
1987 struct batadv_hardif_neigh_node *hardif_neigh)
1988{
1989 int last_secs, last_msecs;
1990
1991 last_secs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) / 1000;
1992 last_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) % 1000;
1993
1994 seq_printf(seq, " %10s %pM %4i.%03is\n",
1995 hardif_neigh->if_incoming->net_dev->name,
1996 hardif_neigh->addr, last_secs, last_msecs);
1997}
1998
1999/**
2000 * batadv_iv_ogm_neigh_print - print the single hop neighbour list
2001 * @bat_priv: the bat priv with all the soft interface information
2002 * @seq: neighbour table seq_file struct
2003 */
2004static void batadv_iv_neigh_print(struct batadv_priv *bat_priv,
2005 struct seq_file *seq)
2006{
2007 struct net_device *net_dev = (struct net_device *)seq->private;
2008 struct batadv_hardif_neigh_node *hardif_neigh;
2009 struct batadv_hard_iface *hard_iface;
2010 int batman_count = 0;
2011
Antonio Quartulli925a6f32016-03-12 10:30:18 +01002012 seq_puts(seq, " IF Neighbor last-seen\n");
Marek Lindner75874052015-08-04 21:09:57 +08002013
2014 rcu_read_lock();
2015 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
2016 if (hard_iface->soft_iface != net_dev)
2017 continue;
2018
2019 hlist_for_each_entry_rcu(hardif_neigh,
2020 &hard_iface->neigh_list, list) {
2021 batadv_iv_hardif_neigh_print(seq, hardif_neigh);
2022 batman_count++;
2023 }
2024 }
2025 rcu_read_unlock();
2026
2027 if (batman_count == 0)
2028 seq_puts(seq, "No batman nodes in range ...\n");
2029}
2030
2031/**
Antonio Quartullia3285a82013-09-02 12:15:04 +02002032 * batadv_iv_ogm_neigh_cmp - compare the metrics of two neighbors
2033 * @neigh1: the first neighbor object of the comparison
Simon Wunderlich89652332013-11-13 19:14:46 +01002034 * @if_outgoing1: outgoing interface for the first neighbor
Antonio Quartullia3285a82013-09-02 12:15:04 +02002035 * @neigh2: the second neighbor object of the comparison
Simon Wunderlich89652332013-11-13 19:14:46 +01002036 * @if_outgoing2: outgoing interface for the second neighbor
Antonio Quartullia3285a82013-09-02 12:15:04 +02002037 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002038 * Return: a value less, equal to or greater than 0 if the metric via neigh1 is
Antonio Quartullia3285a82013-09-02 12:15:04 +02002039 * lower, the same as or higher than the metric via neigh2
2040 */
2041static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1,
Simon Wunderlich89652332013-11-13 19:14:46 +01002042 struct batadv_hard_iface *if_outgoing1,
2043 struct batadv_neigh_node *neigh2,
2044 struct batadv_hard_iface *if_outgoing2)
Antonio Quartullia3285a82013-09-02 12:15:04 +02002045{
Simon Wunderlich89652332013-11-13 19:14:46 +01002046 struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002047 u8 tq1, tq2;
Simon Wunderlich89652332013-11-13 19:14:46 +01002048 int diff;
Antonio Quartullia3285a82013-09-02 12:15:04 +02002049
Simon Wunderlich89652332013-11-13 19:14:46 +01002050 neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
2051 neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
Antonio Quartullia3285a82013-09-02 12:15:04 +02002052
Simon Wunderlich89652332013-11-13 19:14:46 +01002053 if (!neigh1_ifinfo || !neigh2_ifinfo) {
2054 diff = 0;
2055 goto out;
2056 }
2057
2058 tq1 = neigh1_ifinfo->bat_iv.tq_avg;
2059 tq2 = neigh2_ifinfo->bat_iv.tq_avg;
2060 diff = tq1 - tq2;
2061
2062out:
2063 if (neigh1_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01002064 batadv_neigh_ifinfo_put(neigh1_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01002065 if (neigh2_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01002066 batadv_neigh_ifinfo_put(neigh2_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01002067
2068 return diff;
Antonio Quartullia3285a82013-09-02 12:15:04 +02002069}
2070
Antonio Quartullic43c9812013-09-02 12:15:05 +02002071/**
Simon Wunderlich18165f62015-08-08 02:01:50 +02002072 * batadv_iv_ogm_neigh_is_sob - check if neigh1 is similarly good or better
2073 * than neigh2 from the metric prospective
Antonio Quartullic43c9812013-09-02 12:15:05 +02002074 * @neigh1: the first neighbor object of the comparison
Martin Hundebøll95298a92014-07-15 09:41:05 +02002075 * @if_outgoing1: outgoing interface for the first neighbor
Antonio Quartullic43c9812013-09-02 12:15:05 +02002076 * @neigh2: the second neighbor object of the comparison
Simon Wunderlich89652332013-11-13 19:14:46 +01002077 * @if_outgoing2: outgoing interface for the second neighbor
Martin Hundebøll95298a92014-07-15 09:41:05 +02002078 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002079 * Return: true if the metric via neigh1 is equally good or better than
Simon Wunderlich89652332013-11-13 19:14:46 +01002080 * the metric via neigh2, false otherwise.
2081 */
2082static bool
Simon Wunderlich18165f62015-08-08 02:01:50 +02002083batadv_iv_ogm_neigh_is_sob(struct batadv_neigh_node *neigh1,
Simon Wunderlich89652332013-11-13 19:14:46 +01002084 struct batadv_hard_iface *if_outgoing1,
2085 struct batadv_neigh_node *neigh2,
2086 struct batadv_hard_iface *if_outgoing2)
2087{
2088 struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002089 u8 tq1, tq2;
Simon Wunderlich89652332013-11-13 19:14:46 +01002090 bool ret;
2091
2092 neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
2093 neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
2094
2095 /* we can't say that the metric is better */
2096 if (!neigh1_ifinfo || !neigh2_ifinfo) {
2097 ret = false;
2098 goto out;
2099 }
2100
2101 tq1 = neigh1_ifinfo->bat_iv.tq_avg;
2102 tq2 = neigh2_ifinfo->bat_iv.tq_avg;
2103 ret = (tq1 - tq2) > -BATADV_TQ_SIMILARITY_THRESHOLD;
2104
2105out:
2106 if (neigh1_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01002107 batadv_neigh_ifinfo_put(neigh1_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01002108 if (neigh2_ifinfo)
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01002109 batadv_neigh_ifinfo_put(neigh2_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +01002110
2111 return ret;
Antonio Quartullic43c9812013-09-02 12:15:05 +02002112}
2113
Antonio Quartullif0d97252016-05-03 01:45:34 +08002114static void batadv_iv_iface_activate(struct batadv_hard_iface *hard_iface)
2115{
2116 /* begin scheduling originator messages on that interface */
2117 batadv_iv_ogm_schedule(hard_iface);
2118}
2119
Sven Eckelmann56303d32012-06-05 22:31:31 +02002120static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
Marek Lindner519d3492012-04-18 17:15:57 +08002121 .name = "BATMAN_IV",
Antonio Quartulli29824a52016-05-25 23:27:31 +08002122 .iface = {
2123 .activate = batadv_iv_iface_activate,
2124 .enable = batadv_iv_ogm_iface_enable,
2125 .disable = batadv_iv_ogm_iface_disable,
2126 .update_mac = batadv_iv_ogm_iface_update_mac,
2127 .primary_set = batadv_iv_ogm_primary_iface_set,
2128 },
2129 .neigh = {
2130 .cmp = batadv_iv_ogm_neigh_cmp,
2131 .is_similar_or_better = batadv_iv_ogm_neigh_is_sob,
2132 .print = batadv_iv_neigh_print,
2133 },
2134 .orig = {
2135 .print = batadv_iv_ogm_orig_print,
2136 .free = batadv_iv_ogm_orig_free,
2137 .add_if = batadv_iv_ogm_orig_add_if,
2138 .del_if = batadv_iv_ogm_orig_del_if,
2139 },
Marek Lindner1c280472011-11-28 17:40:17 +08002140};
2141
Sven Eckelmann81c524f2012-05-12 02:09:22 +02002142int __init batadv_iv_init(void)
Marek Lindner1c280472011-11-28 17:40:17 +08002143{
Marek Lindnerc3e29312012-03-04 16:56:25 +08002144 int ret;
2145
2146 /* batman originator packet */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002147 ret = batadv_recv_handler_register(BATADV_IV_OGM,
2148 batadv_iv_ogm_receive);
Marek Lindnerc3e29312012-03-04 16:56:25 +08002149 if (ret < 0)
2150 goto out;
2151
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02002152 ret = batadv_algo_register(&batadv_batman_iv);
Marek Lindnerc3e29312012-03-04 16:56:25 +08002153 if (ret < 0)
2154 goto handler_unregister;
2155
2156 goto out;
2157
2158handler_unregister:
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002159 batadv_recv_handler_unregister(BATADV_IV_OGM);
Marek Lindnerc3e29312012-03-04 16:56:25 +08002160out:
2161 return ret;
Marek Lindner1c280472011-11-28 17:40:17 +08002162}