blob: a2b480a908723a37d47d14bd76e63628518e6561 [file] [log] [blame]
Antonio Quartulli0b873932013-01-04 03:05:31 +01001/* Copyright (C) 2007-2013 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
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
Marek Lindnerfc957272011-07-30 12:04:12 +020018 */
19
20#include "main.h"
Marek Lindnerfc957272011-07-30 12:04:12 +020021#include "translation-table.h"
Marek Lindnerfc957272011-07-30 12:04:12 +020022#include "originator.h"
23#include "routing.h"
24#include "gateway_common.h"
25#include "gateway_client.h"
26#include "hard-interface.h"
27#include "send.h"
Marek Lindner1c280472011-11-28 17:40:17 +080028#include "bat_algo.h"
Martin Hundebølld56b1702013-01-25 11:12:39 +010029#include "network-coding.h"
Marek Lindnerfc957272011-07-30 12:04:12 +020030
Antonio Quartulli791c2a22013-08-17 12:44:44 +020031
32/**
33 * batadv_dup_status - duplicate status
34 * @BATADV_NO_DUP: the packet is a duplicate
35 * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for the
36 * neighbor)
37 * @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor
38 * @BATADV_PROTECTED: originator is currently protected (after reboot)
39 */
40enum batadv_dup_status {
41 BATADV_NO_DUP = 0,
42 BATADV_ORIG_DUP,
43 BATADV_NEIGH_DUP,
44 BATADV_PROTECTED,
45};
46
Antonio Quartulli24a5dee2013-04-08 09:38:12 +020047/**
48 * batadv_ring_buffer_set - update the ring buffer with the given value
49 * @lq_recv: pointer to the ring buffer
50 * @lq_index: index to store the value at
51 * @value: value to store in the ring buffer
52 */
53static void batadv_ring_buffer_set(uint8_t lq_recv[], uint8_t *lq_index,
54 uint8_t value)
55{
56 lq_recv[*lq_index] = value;
57 *lq_index = (*lq_index + 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE;
58}
59
60/**
61 * batadv_ring_buffer_set - compute the average of all non-zero values stored
62 * in the given ring buffer
63 * @lq_recv: pointer to the ring buffer
64 *
65 * Returns computed average value.
66 */
67static uint8_t batadv_ring_buffer_avg(const uint8_t lq_recv[])
68{
69 const uint8_t *ptr;
70 uint16_t count = 0, i = 0, sum = 0;
71
72 ptr = lq_recv;
73
74 while (i < BATADV_TQ_GLOBAL_WINDOW_SIZE) {
75 if (*ptr != 0) {
76 count++;
77 sum += *ptr;
78 }
79
80 i++;
81 ptr++;
82 }
83
84 if (count == 0)
85 return 0;
86
87 return (uint8_t)(sum / count);
88}
David S. Millerd98cae64e2013-06-19 16:49:39 -070089
Antonio Quartullibbad0a52013-09-02 12:15:02 +020090/**
Antonio Quartullid0015fd2013-09-03 11:10:23 +020091 * batadv_iv_ogm_orig_free - free the private resources allocated for this
92 * orig_node
93 * @orig_node: the orig_node for which the resources have to be free'd
94 */
95static void batadv_iv_ogm_orig_free(struct batadv_orig_node *orig_node)
96{
97 kfree(orig_node->bat_iv.bcast_own);
98 kfree(orig_node->bat_iv.bcast_own_sum);
99}
100
101/**
102 * batadv_iv_ogm_orig_add_if - change the private structures of the orig_node to
103 * include the new hard-interface
104 * @orig_node: the orig_node that has to be changed
105 * @max_if_num: the current amount of interfaces
106 *
107 * Returns 0 on success, a negative error code otherwise.
108 */
109static int batadv_iv_ogm_orig_add_if(struct batadv_orig_node *orig_node,
110 int max_if_num)
111{
112 void *data_ptr;
113 size_t data_size, old_size;
114 int ret = -ENOMEM;
115
116 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
117
118 data_size = max_if_num * sizeof(unsigned long) * BATADV_NUM_WORDS;
119 old_size = (max_if_num - 1) * sizeof(unsigned long) * BATADV_NUM_WORDS;
120 data_ptr = kmalloc(data_size, GFP_ATOMIC);
121 if (!data_ptr)
122 goto unlock;
123
124 memcpy(data_ptr, orig_node->bat_iv.bcast_own, old_size);
125 kfree(orig_node->bat_iv.bcast_own);
126 orig_node->bat_iv.bcast_own = data_ptr;
127
128 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
129 if (!data_ptr) {
130 kfree(orig_node->bat_iv.bcast_own);
131 goto unlock;
132 }
133
134 memcpy(data_ptr, orig_node->bat_iv.bcast_own_sum,
135 (max_if_num - 1) * sizeof(uint8_t));
136 kfree(orig_node->bat_iv.bcast_own_sum);
137 orig_node->bat_iv.bcast_own_sum = data_ptr;
138
139 ret = 0;
140
141unlock:
142 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
143
144 return ret;
145}
146
147/**
148 * batadv_iv_ogm_orig_del_if - change the private structures of the orig_node to
149 * exclude the removed interface
150 * @orig_node: the orig_node that has to be changed
151 * @max_if_num: the current amount of interfaces
152 * @del_if_num: the index of the interface being removed
153 *
154 * Returns 0 on success, a negative error code otherwise.
155 */
156static int batadv_iv_ogm_orig_del_if(struct batadv_orig_node *orig_node,
157 int max_if_num, int del_if_num)
158{
159 int chunk_size, ret = -ENOMEM, if_offset;
160 void *data_ptr = NULL;
161
162 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
163
164 /* last interface was removed */
165 if (max_if_num == 0)
166 goto free_bcast_own;
167
168 chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS;
169 data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC);
170 if (!data_ptr)
171 goto unlock;
172
173 /* copy first part */
174 memcpy(data_ptr, orig_node->bat_iv.bcast_own, del_if_num * chunk_size);
175
176 /* copy second part */
177 memcpy((char *)data_ptr + del_if_num * chunk_size,
178 orig_node->bat_iv.bcast_own + ((del_if_num + 1) * chunk_size),
179 (max_if_num - del_if_num) * chunk_size);
180
181free_bcast_own:
182 kfree(orig_node->bat_iv.bcast_own);
183 orig_node->bat_iv.bcast_own = data_ptr;
184
185 if (max_if_num == 0)
186 goto free_own_sum;
187
188 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
189 if (!data_ptr) {
190 kfree(orig_node->bat_iv.bcast_own);
191 goto unlock;
192 }
193
194 memcpy(data_ptr, orig_node->bat_iv.bcast_own_sum,
195 del_if_num * sizeof(uint8_t));
196
197 if_offset = (del_if_num + 1) * sizeof(uint8_t);
198 memcpy((char *)data_ptr + del_if_num * sizeof(uint8_t),
199 orig_node->bat_iv.bcast_own_sum + if_offset,
200 (max_if_num - del_if_num) * sizeof(uint8_t));
201
202free_own_sum:
203 kfree(orig_node->bat_iv.bcast_own_sum);
204 orig_node->bat_iv.bcast_own_sum = data_ptr;
205
206 ret = 0;
207unlock:
208 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
209
210 return ret;
211}
212
213/**
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200214 * batadv_iv_ogm_orig_get - retrieve or create (if does not exist) an originator
215 * @bat_priv: the bat priv with all the soft interface information
216 * @addr: mac address of the originator
217 *
218 * Returns the originator object corresponding to the passed mac address or NULL
219 * on failure.
220 * If the object does not exists it is created an initialised.
221 */
222static struct batadv_orig_node *
223batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const uint8_t *addr)
224{
225 struct batadv_orig_node *orig_node;
226 int size, hash_added;
227
228 orig_node = batadv_orig_hash_find(bat_priv, addr);
229 if (orig_node)
230 return orig_node;
231
232 orig_node = batadv_orig_node_new(bat_priv, addr);
233 if (!orig_node)
234 return NULL;
235
236 spin_lock_init(&orig_node->bat_iv.ogm_cnt_lock);
237
238 size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS;
239 orig_node->bat_iv.bcast_own = kzalloc(size, GFP_ATOMIC);
240 if (!orig_node->bat_iv.bcast_own)
241 goto free_orig_node;
242
243 size = bat_priv->num_ifaces * sizeof(uint8_t);
244 orig_node->bat_iv.bcast_own_sum = kzalloc(size, GFP_ATOMIC);
245 if (!orig_node->bat_iv.bcast_own_sum)
246 goto free_bcast_own;
247
248 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
249 batadv_choose_orig, orig_node,
250 &orig_node->hash_entry);
251 if (hash_added != 0)
252 goto free_bcast_own;
253
254 return orig_node;
255
256free_bcast_own:
257 kfree(orig_node->bat_iv.bcast_own);
258free_orig_node:
259 batadv_orig_node_free_ref(orig_node);
260
261 return NULL;
262}
263
Sven Eckelmann56303d32012-06-05 22:31:31 +0200264static struct batadv_neigh_node *
265batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface,
266 const uint8_t *neigh_addr,
267 struct batadv_orig_node *orig_node,
Antonio Quartulli863dd7a2013-03-25 13:49:46 +0100268 struct batadv_orig_node *orig_neigh)
Marek Lindner7ae8b282012-03-01 15:35:21 +0800269{
Antonio Quartulli0538f752013-09-02 12:15:01 +0200270 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200271 struct batadv_neigh_node *neigh_node;
Marek Lindner7ae8b282012-03-01 15:35:21 +0800272
Antonio Quartulli0538f752013-09-02 12:15:01 +0200273 neigh_node = batadv_neigh_node_new(hard_iface, neigh_addr, orig_node);
Marek Lindner7ae8b282012-03-01 15:35:21 +0800274 if (!neigh_node)
275 goto out;
276
Antonio Quartulli0538f752013-09-02 12:15:01 +0200277 spin_lock_init(&neigh_node->bat_iv.lq_update_lock);
Marek Lindner7ae8b282012-03-01 15:35:21 +0800278
Antonio Quartulli0538f752013-09-02 12:15:01 +0200279 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
280 "Creating new neighbor %pM for orig_node %pM on interface %s\n",
281 neigh_addr, orig_node->orig, hard_iface->net_dev->name);
Marek Lindner7ae8b282012-03-01 15:35:21 +0800282
283 spin_lock_bh(&orig_node->neigh_list_lock);
284 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
285 spin_unlock_bh(&orig_node->neigh_list_lock);
286
287out:
288 return neigh_node;
289}
290
Sven Eckelmann56303d32012-06-05 22:31:31 +0200291static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200292{
Sven Eckelmann96412692012-06-05 22:31:30 +0200293 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindner14511512012-08-02 17:20:26 +0200294 unsigned char *ogm_buff;
Marek Lindnerd7d32ec2012-02-07 17:20:46 +0800295 uint32_t random_seqno;
Sven Eckelmann5346c352012-05-05 13:27:28 +0200296 int res = -ENOMEM;
Marek Lindnerd7d32ec2012-02-07 17:20:46 +0800297
298 /* randomize initial seqno to avoid collision */
299 get_random_bytes(&random_seqno, sizeof(random_seqno));
Marek Lindner14511512012-08-02 17:20:26 +0200300 atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno);
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200301
Marek Lindner14511512012-08-02 17:20:26 +0200302 hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN;
303 ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC);
304 if (!ogm_buff)
Marek Lindner77af7572012-02-07 17:20:48 +0800305 goto out;
306
Marek Lindner14511512012-08-02 17:20:26 +0200307 hard_iface->bat_iv.ogm_buff = ogm_buff;
308
309 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
Sven Eckelmann96412692012-06-05 22:31:30 +0200310 batadv_ogm_packet->header.packet_type = BATADV_IV_OGM;
311 batadv_ogm_packet->header.version = BATADV_COMPAT_VERSION;
312 batadv_ogm_packet->header.ttl = 2;
313 batadv_ogm_packet->flags = BATADV_NO_FLAGS;
Marek Lindner414254e2013-04-23 21:39:58 +0800314 batadv_ogm_packet->reserved = 0;
Sven Eckelmann96412692012-06-05 22:31:30 +0200315 batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE;
Marek Lindner77af7572012-02-07 17:20:48 +0800316
317 res = 0;
318
319out:
320 return res;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200321}
322
Sven Eckelmann56303d32012-06-05 22:31:31 +0200323static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
Marek Lindner00a50072012-02-07 17:20:47 +0800324{
Marek Lindner14511512012-08-02 17:20:26 +0200325 kfree(hard_iface->bat_iv.ogm_buff);
326 hard_iface->bat_iv.ogm_buff = NULL;
Marek Lindner00a50072012-02-07 17:20:47 +0800327}
328
Sven Eckelmann56303d32012-06-05 22:31:31 +0200329static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface)
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200330{
Sven Eckelmann96412692012-06-05 22:31:30 +0200331 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindner14511512012-08-02 17:20:26 +0200332 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200333
Marek Lindner14511512012-08-02 17:20:26 +0200334 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
Sven Eckelmann96412692012-06-05 22:31:30 +0200335 memcpy(batadv_ogm_packet->orig,
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200336 hard_iface->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmann96412692012-06-05 22:31:30 +0200337 memcpy(batadv_ogm_packet->prev_sender,
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200338 hard_iface->net_dev->dev_addr, ETH_ALEN);
339}
340
Sven Eckelmann56303d32012-06-05 22:31:31 +0200341static void
342batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface)
Marek Lindnerc3229392012-03-11 06:17:50 +0800343{
Sven Eckelmann96412692012-06-05 22:31:30 +0200344 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindner14511512012-08-02 17:20:26 +0200345 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
Marek Lindnerc3229392012-03-11 06:17:50 +0800346
Marek Lindner14511512012-08-02 17:20:26 +0200347 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
Sven Eckelmann96412692012-06-05 22:31:30 +0200348 batadv_ogm_packet->flags = BATADV_PRIMARIES_FIRST_HOP;
349 batadv_ogm_packet->header.ttl = BATADV_TTL;
Marek Lindnerc3229392012-03-11 06:17:50 +0800350}
351
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200352/* when do we schedule our own ogm to be sent */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200353static unsigned long
Sven Eckelmann56303d32012-06-05 22:31:31 +0200354batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200355{
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200356 unsigned int msecs;
357
358 msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
Akinobu Mitae76e4322012-12-24 11:14:07 +0900359 msecs += prandom_u32() % (2 * BATADV_JITTER);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200360
361 return jiffies + msecs_to_jiffies(msecs);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200362}
363
364/* when do we schedule a ogm packet to be sent */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200365static unsigned long batadv_iv_ogm_fwd_send_time(void)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200366{
Akinobu Mitae76e4322012-12-24 11:14:07 +0900367 return jiffies + msecs_to_jiffies(prandom_u32() % (BATADV_JITTER / 2));
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200368}
369
370/* apply hop penalty for a normal link */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200371static uint8_t batadv_hop_penalty(uint8_t tq,
372 const struct batadv_priv *bat_priv)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200373{
374 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200375 int new_tq;
376
377 new_tq = tq * (BATADV_TQ_MAX_VALUE - hop_penalty);
378 new_tq /= BATADV_TQ_MAX_VALUE;
379
380 return new_tq;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200381}
382
Marek Lindnerfc957272011-07-30 12:04:12 +0200383/* is there another aggregated packet here? */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200384static int batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len,
Marek Lindneref261572013-04-23 21:39:57 +0800385 __be16 tvlv_len)
Marek Lindnerfc957272011-07-30 12:04:12 +0200386{
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200387 int next_buff_pos = 0;
388
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200389 next_buff_pos += buff_pos + BATADV_OGM_HLEN;
Marek Lindneref261572013-04-23 21:39:57 +0800390 next_buff_pos += ntohs(tvlv_len);
Marek Lindnerfc957272011-07-30 12:04:12 +0200391
392 return (next_buff_pos <= packet_len) &&
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200393 (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
Marek Lindnerfc957272011-07-30 12:04:12 +0200394}
395
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200396/* send a batman ogm to a given interface */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200397static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet,
398 struct batadv_hard_iface *hard_iface)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200399{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200400 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200401 char *fwd_str;
402 uint8_t packet_num;
403 int16_t buff_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +0200404 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200405 struct sk_buff *skb;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200406 uint8_t *packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200407
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200408 if (hard_iface->if_status != BATADV_IF_ACTIVE)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200409 return;
410
411 packet_num = 0;
412 buff_pos = 0;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200413 packet_pos = forw_packet->skb->data;
414 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200415
416 /* adjust all flags and log packets */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200417 while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
Marek Lindneref261572013-04-23 21:39:57 +0800418 batadv_ogm_packet->tvlv_len)) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200419 /* we might have aggregated direct link packets with an
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200420 * ordinary base packet
421 */
Sven Eckelmann8de47de2012-07-08 16:32:09 +0200422 if (forw_packet->direct_link_flags & BIT(packet_num) &&
423 forw_packet->if_incoming == hard_iface)
Sven Eckelmann96412692012-06-05 22:31:30 +0200424 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200425 else
Sven Eckelmann96412692012-06-05 22:31:30 +0200426 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200427
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200428 if (packet_num > 0 || !forw_packet->own)
429 fwd_str = "Forwarding";
430 else
431 fwd_str = "Sending own";
432
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200433 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800434 "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200435 fwd_str, (packet_num > 0 ? "aggregated " : ""),
Sven Eckelmann96412692012-06-05 22:31:30 +0200436 batadv_ogm_packet->orig,
437 ntohl(batadv_ogm_packet->seqno),
438 batadv_ogm_packet->tq, batadv_ogm_packet->header.ttl,
439 (batadv_ogm_packet->flags & BATADV_DIRECTLINK ?
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200440 "on" : "off"),
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800441 hard_iface->net_dev->name,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200442 hard_iface->net_dev->dev_addr);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200443
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200444 buff_pos += BATADV_OGM_HLEN;
Marek Lindneref261572013-04-23 21:39:57 +0800445 buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200446 packet_num++;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200447 packet_pos = forw_packet->skb->data + buff_pos;
448 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200449 }
450
451 /* create clone because function is called more than once */
452 skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
Martin Hundebøllf8214862012-04-20 17:02:45 +0200453 if (skb) {
Sven Eckelmannd69909d2012-06-03 22:19:20 +0200454 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
455 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
Martin Hundebøllf8214862012-04-20 17:02:45 +0200456 skb->len + ETH_HLEN);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200457 batadv_send_skb_packet(skb, hard_iface, batadv_broadcast_addr);
Martin Hundebøllf8214862012-04-20 17:02:45 +0200458 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200459}
460
461/* send a batman ogm packet */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200462static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200463{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200464 struct batadv_hard_iface *hard_iface;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200465 struct net_device *soft_iface;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200466 struct batadv_priv *bat_priv;
467 struct batadv_hard_iface *primary_if = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200468 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200469 unsigned char directlink;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200470 uint8_t *packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200471
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200472 packet_pos = forw_packet->skb->data;
473 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +0200474 directlink = (batadv_ogm_packet->flags & BATADV_DIRECTLINK ? 1 : 0);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200475
476 if (!forw_packet->if_incoming) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100477 pr_err("Error - can't forward packet: incoming iface not specified\n");
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200478 goto out;
479 }
480
481 soft_iface = forw_packet->if_incoming->soft_iface;
482 bat_priv = netdev_priv(soft_iface);
483
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200484 if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200485 goto out;
486
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200487 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200488 if (!primary_if)
489 goto out;
490
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200491 /* multihomed peer assumed
492 * non-primary OGMs are only broadcasted on their interface
493 */
Sven Eckelmann96412692012-06-05 22:31:30 +0200494 if ((directlink && (batadv_ogm_packet->header.ttl == 1)) ||
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200495 (forw_packet->own && (forw_packet->if_incoming != primary_if))) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200496 /* FIXME: what about aggregated packets ? */
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200497 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200498 "%s packet (originator %pM, seqno %u, TTL %d) on interface %s [%pM]\n",
499 (forw_packet->own ? "Sending own" : "Forwarding"),
Sven Eckelmann96412692012-06-05 22:31:30 +0200500 batadv_ogm_packet->orig,
501 ntohl(batadv_ogm_packet->seqno),
502 batadv_ogm_packet->header.ttl,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200503 forw_packet->if_incoming->net_dev->name,
504 forw_packet->if_incoming->net_dev->dev_addr);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200505
506 /* skb is only used once and than forw_packet is free'd */
Sven Eckelmann9455e342012-05-12 02:09:37 +0200507 batadv_send_skb_packet(forw_packet->skb,
508 forw_packet->if_incoming,
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200509 batadv_broadcast_addr);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200510 forw_packet->skb = NULL;
511
512 goto out;
513 }
514
515 /* broadcast on every interface */
516 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200517 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200518 if (hard_iface->soft_iface != soft_iface)
519 continue;
520
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200521 batadv_iv_ogm_send_to_if(forw_packet, hard_iface);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200522 }
523 rcu_read_unlock();
524
525out:
526 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200527 batadv_hardif_free_ref(primary_if);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200528}
529
530/* return true if new_packet can be aggregated with forw_packet */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200531static bool
Sven Eckelmann96412692012-06-05 22:31:30 +0200532batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200533 struct batadv_priv *bat_priv,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200534 int packet_len, unsigned long send_time,
535 bool directlink,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200536 const struct batadv_hard_iface *if_incoming,
537 const struct batadv_forw_packet *forw_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200538{
Sven Eckelmann96412692012-06-05 22:31:30 +0200539 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200540 int aggregated_bytes = forw_packet->packet_len + packet_len;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200541 struct batadv_hard_iface *primary_if = NULL;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200542 bool res = false;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200543 unsigned long aggregation_end_time;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200544
Sven Eckelmann96412692012-06-05 22:31:30 +0200545 batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200546 aggregation_end_time = send_time;
547 aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200548
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200549 /* we can aggregate the current packet to this aggregated packet
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200550 * if:
551 *
552 * - the send time is within our MAX_AGGREGATION_MS time
553 * - the resulting packet wont be bigger than
554 * MAX_AGGREGATION_BYTES
555 */
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200556 if (time_before(send_time, forw_packet->send_time) &&
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200557 time_after_eq(aggregation_end_time, forw_packet->send_time) &&
558 (aggregated_bytes <= BATADV_MAX_AGGREGATION_BYTES)) {
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200559 /* check aggregation compatibility
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200560 * -> direct link packets are broadcasted on
561 * their interface only
562 * -> aggregate packet if the current packet is
563 * a "global" packet as well as the base
564 * packet
565 */
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200566 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200567 if (!primary_if)
568 goto out;
569
570 /* packets without direct link flag and high TTL
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200571 * are flooded through the net
572 */
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200573 if ((!directlink) &&
Sven Eckelmann96412692012-06-05 22:31:30 +0200574 (!(batadv_ogm_packet->flags & BATADV_DIRECTLINK)) &&
575 (batadv_ogm_packet->header.ttl != 1) &&
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200576
577 /* own packets originating non-primary
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200578 * interfaces leave only that interface
579 */
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200580 ((!forw_packet->own) ||
581 (forw_packet->if_incoming == primary_if))) {
582 res = true;
583 goto out;
584 }
585
586 /* if the incoming packet is sent via this one
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200587 * interface only - we still can aggregate
588 */
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200589 if ((directlink) &&
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200590 (new_bat_ogm_packet->header.ttl == 1) &&
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200591 (forw_packet->if_incoming == if_incoming) &&
592
593 /* packets from direct neighbors or
594 * own secondary interface packets
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200595 * (= secondary interface packets in general)
596 */
Sven Eckelmann96412692012-06-05 22:31:30 +0200597 (batadv_ogm_packet->flags & BATADV_DIRECTLINK ||
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200598 (forw_packet->own &&
599 forw_packet->if_incoming != primary_if))) {
600 res = true;
601 goto out;
602 }
603 }
604
605out:
606 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200607 batadv_hardif_free_ref(primary_if);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200608 return res;
609}
610
611/* create a new aggregated packet and add this packet to it */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200612static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,
613 int packet_len, unsigned long send_time,
614 bool direct_link,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200615 struct batadv_hard_iface *if_incoming,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200616 int own_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200617{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200618 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
619 struct batadv_forw_packet *forw_packet_aggr;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200620 unsigned char *skb_buff;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200621 unsigned int skb_size;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200622
623 if (!atomic_inc_not_zero(&if_incoming->refcount))
624 return;
625
626 /* own packet should always be scheduled */
627 if (!own_packet) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200628 if (!batadv_atomic_dec_not_zero(&bat_priv->batman_queue_left)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200629 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200630 "batman packet queue full\n");
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200631 goto out;
632 }
633 }
634
635 forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC);
636 if (!forw_packet_aggr) {
637 if (!own_packet)
638 atomic_inc(&bat_priv->batman_queue_left);
639 goto out;
640 }
641
642 if ((atomic_read(&bat_priv->aggregated_ogms)) &&
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200643 (packet_len < BATADV_MAX_AGGREGATION_BYTES))
Sven Eckelmann5b246572012-11-04 17:11:45 +0100644 skb_size = BATADV_MAX_AGGREGATION_BYTES;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200645 else
Sven Eckelmann5b246572012-11-04 17:11:45 +0100646 skb_size = packet_len;
647
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200648 skb_size += ETH_HLEN;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200649
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200650 forw_packet_aggr->skb = netdev_alloc_skb_ip_align(NULL, skb_size);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200651 if (!forw_packet_aggr->skb) {
652 if (!own_packet)
653 atomic_inc(&bat_priv->batman_queue_left);
654 kfree(forw_packet_aggr);
655 goto out;
656 }
Simon Wunderlichc54f38c2013-07-29 17:56:44 +0200657 forw_packet_aggr->skb->priority = TC_PRIO_CONTROL;
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200658 skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200659
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200660 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
661 forw_packet_aggr->packet_len = packet_len;
662 memcpy(skb_buff, packet_buff, packet_len);
663
664 forw_packet_aggr->own = own_packet;
665 forw_packet_aggr->if_incoming = if_incoming;
666 forw_packet_aggr->num_packets = 0;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200667 forw_packet_aggr->direct_link_flags = BATADV_NO_FLAGS;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200668 forw_packet_aggr->send_time = send_time;
669
670 /* save packet direct link flag status */
671 if (direct_link)
672 forw_packet_aggr->direct_link_flags |= 1;
673
674 /* add new packet to packet list */
675 spin_lock_bh(&bat_priv->forw_bat_list_lock);
676 hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list);
677 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
678
679 /* start timer for this packet */
680 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
Sven Eckelmann9455e342012-05-12 02:09:37 +0200681 batadv_send_outstanding_bat_ogm_packet);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200682 queue_delayed_work(batadv_event_workqueue,
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200683 &forw_packet_aggr->delayed_work,
684 send_time - jiffies);
685
686 return;
687out:
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200688 batadv_hardif_free_ref(if_incoming);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200689}
690
691/* aggregate a new packet into the existing ogm packet */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200692static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200693 const unsigned char *packet_buff,
694 int packet_len, bool direct_link)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200695{
696 unsigned char *skb_buff;
Sven Eckelmann8de47de2012-07-08 16:32:09 +0200697 unsigned long new_direct_link_flag;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200698
699 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
700 memcpy(skb_buff, packet_buff, packet_len);
701 forw_packet_aggr->packet_len += packet_len;
702 forw_packet_aggr->num_packets++;
703
704 /* save packet direct link flag status */
Sven Eckelmann8de47de2012-07-08 16:32:09 +0200705 if (direct_link) {
706 new_direct_link_flag = BIT(forw_packet_aggr->num_packets);
707 forw_packet_aggr->direct_link_flags |= new_direct_link_flag;
708 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200709}
710
Sven Eckelmann56303d32012-06-05 22:31:31 +0200711static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200712 unsigned char *packet_buff,
713 int packet_len,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200714 struct batadv_hard_iface *if_incoming,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200715 int own_packet, unsigned long send_time)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200716{
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200717 /* _aggr -> pointer to the packet we want to aggregate with
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200718 * _pos -> pointer to the position in the queue
719 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200720 struct batadv_forw_packet *forw_packet_aggr = NULL;
721 struct batadv_forw_packet *forw_packet_pos = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200722 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200723 bool direct_link;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200724 unsigned long max_aggregation_jiffies;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200725
Sven Eckelmann96412692012-06-05 22:31:30 +0200726 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
727 direct_link = batadv_ogm_packet->flags & BATADV_DIRECTLINK ? 1 : 0;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200728 max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200729
730 /* find position for the packet in the forward queue */
731 spin_lock_bh(&bat_priv->forw_bat_list_lock);
732 /* own packets are not to be aggregated */
733 if ((atomic_read(&bat_priv->aggregated_ogms)) && (!own_packet)) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800734 hlist_for_each_entry(forw_packet_pos,
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200735 &bat_priv->forw_bat_list, list) {
Sven Eckelmann96412692012-06-05 22:31:30 +0200736 if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200737 bat_priv, packet_len,
738 send_time, direct_link,
739 if_incoming,
740 forw_packet_pos)) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200741 forw_packet_aggr = forw_packet_pos;
742 break;
743 }
744 }
745 }
746
747 /* nothing to aggregate with - either aggregation disabled or no
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200748 * suitable aggregation packet found
749 */
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200750 if (!forw_packet_aggr) {
751 /* the following section can run without the lock */
752 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
753
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200754 /* if we could not aggregate this packet with one of the others
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200755 * we hold it back for a while, so that it might be aggregated
756 * later on
757 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200758 if (!own_packet && atomic_read(&bat_priv->aggregated_ogms))
759 send_time += max_aggregation_jiffies;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200760
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200761 batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
762 send_time, direct_link,
763 if_incoming, own_packet);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200764 } else {
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200765 batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
766 packet_len, direct_link);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200767 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
768 }
769}
770
Sven Eckelmann56303d32012-06-05 22:31:31 +0200771static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200772 const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +0200773 struct batadv_ogm_packet *batadv_ogm_packet,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200774 bool is_single_hop_neigh,
775 bool is_from_best_next_hop,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200776 struct batadv_hard_iface *if_incoming)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200777{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200778 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Marek Lindneref261572013-04-23 21:39:57 +0800779 uint16_t tvlv_len;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200780
Sven Eckelmann96412692012-06-05 22:31:30 +0200781 if (batadv_ogm_packet->header.ttl <= 1) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200782 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200783 return;
784 }
785
Marek Lindner13b25412012-03-11 06:17:53 +0800786 if (!is_from_best_next_hop) {
787 /* Mark the forwarded packet when it is not coming from our
788 * best next hop. We still need to forward the packet for our
789 * neighbor link quality detection to work in case the packet
790 * originated from a single hop neighbor. Otherwise we can
791 * simply drop the ogm.
792 */
793 if (is_single_hop_neigh)
Sven Eckelmann96412692012-06-05 22:31:30 +0200794 batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP;
Marek Lindner13b25412012-03-11 06:17:53 +0800795 else
796 return;
797 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200798
Marek Lindneref261572013-04-23 21:39:57 +0800799 tvlv_len = ntohs(batadv_ogm_packet->tvlv_len);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200800
Sven Eckelmann96412692012-06-05 22:31:30 +0200801 batadv_ogm_packet->header.ttl--;
802 memcpy(batadv_ogm_packet->prev_sender, ethhdr->h_source, ETH_ALEN);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200803
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200804 /* apply hop penalty */
Sven Eckelmann96412692012-06-05 22:31:30 +0200805 batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200806 bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200807
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200808 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200809 "Forwarding packet: tq: %i, ttl: %i\n",
Sven Eckelmann96412692012-06-05 22:31:30 +0200810 batadv_ogm_packet->tq, batadv_ogm_packet->header.ttl);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200811
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200812 /* switch of primaries first hop flag when forwarding */
Sven Eckelmann96412692012-06-05 22:31:30 +0200813 batadv_ogm_packet->flags &= ~BATADV_PRIMARIES_FIRST_HOP;
Marek Lindner75cd33f2012-03-01 15:35:16 +0800814 if (is_single_hop_neigh)
Sven Eckelmann96412692012-06-05 22:31:30 +0200815 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200816 else
Sven Eckelmann96412692012-06-05 22:31:30 +0200817 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200818
Sven Eckelmann96412692012-06-05 22:31:30 +0200819 batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
Marek Lindneref261572013-04-23 21:39:57 +0800820 BATADV_OGM_HLEN + tvlv_len,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200821 if_incoming, 0, batadv_iv_ogm_fwd_send_time());
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200822}
823
Antonio Quartullid9896612013-04-17 17:44:43 +0200824/**
825 * batadv_iv_ogm_slide_own_bcast_window - bitshift own OGM broadcast windows for
826 * the given interface
827 * @hard_iface: the interface for which the windows have to be shifted
828 */
829static void
830batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
831{
832 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
833 struct batadv_hashtable *hash = bat_priv->orig_hash;
834 struct hlist_head *head;
835 struct batadv_orig_node *orig_node;
836 unsigned long *word;
837 uint32_t i;
838 size_t word_index;
839 uint8_t *w;
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200840 int if_num;
Antonio Quartullid9896612013-04-17 17:44:43 +0200841
842 for (i = 0; i < hash->size; i++) {
843 head = &hash->table[i];
844
845 rcu_read_lock();
846 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200847 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Antonio Quartullid9896612013-04-17 17:44:43 +0200848 word_index = hard_iface->if_num * BATADV_NUM_WORDS;
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200849 word = &(orig_node->bat_iv.bcast_own[word_index]);
Antonio Quartullid9896612013-04-17 17:44:43 +0200850
851 batadv_bit_get_packet(bat_priv, word, 1, 0);
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200852 if_num = hard_iface->if_num;
853 w = &orig_node->bat_iv.bcast_own_sum[if_num];
Antonio Quartullid9896612013-04-17 17:44:43 +0200854 *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200855 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Antonio Quartullid9896612013-04-17 17:44:43 +0200856 }
857 rcu_read_unlock();
858 }
859}
860
Sven Eckelmann56303d32012-06-05 22:31:31 +0200861static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200862{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200863 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindner14511512012-08-02 17:20:26 +0200864 unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff;
Sven Eckelmann96412692012-06-05 22:31:30 +0200865 struct batadv_ogm_packet *batadv_ogm_packet;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200866 struct batadv_hard_iface *primary_if;
Marek Lindner14511512012-08-02 17:20:26 +0200867 int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len;
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200868 uint32_t seqno;
Marek Lindneref261572013-04-23 21:39:57 +0800869 uint16_t tvlv_len = 0;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200870
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200871 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200872
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800873 if (hard_iface == primary_if) {
874 /* tt changes have to be committed before the tvlv data is
875 * appended as it may alter the tt tvlv container
876 */
877 batadv_tt_local_commit_changes(bat_priv);
Marek Lindneref261572013-04-23 21:39:57 +0800878 tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff,
879 ogm_buff_len,
880 BATADV_OGM_HLEN);
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800881 }
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800882
Marek Lindner14511512012-08-02 17:20:26 +0200883 batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff);
Marek Lindneref261572013-04-23 21:39:57 +0800884 batadv_ogm_packet->tvlv_len = htons(tvlv_len);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200885
886 /* change sequence number to network order */
Marek Lindner14511512012-08-02 17:20:26 +0200887 seqno = (uint32_t)atomic_read(&hard_iface->bat_iv.ogm_seqno);
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200888 batadv_ogm_packet->seqno = htonl(seqno);
Marek Lindner14511512012-08-02 17:20:26 +0200889 atomic_inc(&hard_iface->bat_iv.ogm_seqno);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200890
Antonio Quartullid9896612013-04-17 17:44:43 +0200891 batadv_iv_ogm_slide_own_bcast_window(hard_iface);
Marek Lindner14511512012-08-02 17:20:26 +0200892 batadv_iv_ogm_queue_add(bat_priv, hard_iface->bat_iv.ogm_buff,
893 hard_iface->bat_iv.ogm_buff_len, hard_iface, 1,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200894 batadv_iv_ogm_emit_send_time(bat_priv));
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200895
896 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200897 batadv_hardif_free_ref(primary_if);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200898}
899
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200900static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200901batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
902 struct batadv_orig_node *orig_node,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200903 const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +0200904 const struct batadv_ogm_packet *batadv_ogm_packet,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200905 struct batadv_hard_iface *if_incoming,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200906 const unsigned char *tt_buff,
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +0200907 enum batadv_dup_status dup_status)
Marek Lindnerfc957272011-07-30 12:04:12 +0200908{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200909 struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
910 struct batadv_neigh_node *router = NULL;
911 struct batadv_orig_node *orig_node_tmp;
Linus LĂĽssing7caf69f2012-09-18 03:01:08 +0200912 int if_num;
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200913 uint8_t sum_orig, sum_neigh;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200914 uint8_t *neigh_addr;
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200915 uint8_t tq_avg;
Marek Lindnerfc957272011-07-30 12:04:12 +0200916
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200917 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200918 "update_originator(): Searching and updating originator entry of received packet\n");
Marek Lindnerfc957272011-07-30 12:04:12 +0200919
920 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800921 hlist_for_each_entry_rcu(tmp_neigh_node,
Marek Lindnerfc957272011-07-30 12:04:12 +0200922 &orig_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200923 neigh_addr = tmp_neigh_node->addr;
924 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
925 tmp_neigh_node->if_incoming == if_incoming &&
926 atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
Antonio Quartulli38dc40e2013-03-30 17:22:00 +0100927 if (WARN(neigh_node, "too many matching neigh_nodes"))
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200928 batadv_neigh_node_free_ref(neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +0200929 neigh_node = tmp_neigh_node;
930 continue;
931 }
932
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +0200933 if (dup_status != BATADV_NO_DUP)
Marek Lindnerfc957272011-07-30 12:04:12 +0200934 continue;
935
Antonio Quartulli0538f752013-09-02 12:15:01 +0200936 spin_lock_bh(&tmp_neigh_node->bat_iv.lq_update_lock);
937 batadv_ring_buffer_set(tmp_neigh_node->bat_iv.tq_recv,
938 &tmp_neigh_node->bat_iv.tq_index, 0);
939 tq_avg = batadv_ring_buffer_avg(tmp_neigh_node->bat_iv.tq_recv);
940 tmp_neigh_node->bat_iv.tq_avg = tq_avg;
941 spin_unlock_bh(&tmp_neigh_node->bat_iv.lq_update_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +0200942 }
943
944 if (!neigh_node) {
Sven Eckelmann56303d32012-06-05 22:31:31 +0200945 struct batadv_orig_node *orig_tmp;
Marek Lindnerfc957272011-07-30 12:04:12 +0200946
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200947 orig_tmp = batadv_iv_ogm_orig_get(bat_priv, ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +0200948 if (!orig_tmp)
949 goto unlock;
950
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200951 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
952 ethhdr->h_source,
Antonio Quartulli863dd7a2013-03-25 13:49:46 +0100953 orig_node, orig_tmp);
Marek Lindnerfc957272011-07-30 12:04:12 +0200954
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200955 batadv_orig_node_free_ref(orig_tmp);
Marek Lindnerfc957272011-07-30 12:04:12 +0200956 if (!neigh_node)
957 goto unlock;
958 } else
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200959 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200960 "Updating existing last-hop neighbor of originator\n");
Marek Lindnerfc957272011-07-30 12:04:12 +0200961
962 rcu_read_unlock();
963
Marek Lindnerd7b2a972012-03-01 15:35:19 +0800964 neigh_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +0200965
Antonio Quartulli0538f752013-09-02 12:15:01 +0200966 spin_lock_bh(&neigh_node->bat_iv.lq_update_lock);
967 batadv_ring_buffer_set(neigh_node->bat_iv.tq_recv,
968 &neigh_node->bat_iv.tq_index,
Sven Eckelmann96412692012-06-05 22:31:30 +0200969 batadv_ogm_packet->tq);
Antonio Quartulli0538f752013-09-02 12:15:01 +0200970 tq_avg = batadv_ring_buffer_avg(neigh_node->bat_iv.tq_recv);
971 neigh_node->bat_iv.tq_avg = tq_avg;
972 spin_unlock_bh(&neigh_node->bat_iv.lq_update_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +0200973
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +0200974 if (dup_status == BATADV_NO_DUP) {
Sven Eckelmann96412692012-06-05 22:31:30 +0200975 orig_node->last_ttl = batadv_ogm_packet->header.ttl;
976 neigh_node->last_ttl = batadv_ogm_packet->header.ttl;
Marek Lindnerfc957272011-07-30 12:04:12 +0200977 }
978
Antonio Quartulli6680a122013-09-02 12:15:06 +0200979 batadv_bonding_candidate_add(bat_priv, orig_node, neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +0200980
981 /* if this neighbor already is our next hop there is nothing
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200982 * to change
983 */
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200984 router = batadv_orig_node_get_router(orig_node);
Marek Lindnerfc957272011-07-30 12:04:12 +0200985 if (router == neigh_node)
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800986 goto out;
Marek Lindnerfc957272011-07-30 12:04:12 +0200987
988 /* if this neighbor does not offer a better TQ we won't consider it */
Antonio Quartulli0538f752013-09-02 12:15:01 +0200989 if (router && (router->bat_iv.tq_avg > neigh_node->bat_iv.tq_avg))
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800990 goto out;
Marek Lindnerfc957272011-07-30 12:04:12 +0200991
992 /* if the TQ is the same and the link not more symmetric we
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200993 * won't consider it either
994 */
Antonio Quartulli0538f752013-09-02 12:15:01 +0200995 if (router && (neigh_node->bat_iv.tq_avg == router->bat_iv.tq_avg)) {
Marek Lindnerfc957272011-07-30 12:04:12 +0200996 orig_node_tmp = router->orig_node;
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200997 spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
Linus LĂĽssing7caf69f2012-09-18 03:01:08 +0200998 if_num = router->if_incoming->if_num;
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200999 sum_orig = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
1000 spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001001
1002 orig_node_tmp = neigh_node->orig_node;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001003 spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
Linus LĂĽssing7caf69f2012-09-18 03:01:08 +02001004 if_num = neigh_node->if_incoming->if_num;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001005 sum_neigh = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
1006 spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001007
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001008 if (sum_orig >= sum_neigh)
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001009 goto out;
Marek Lindnerfc957272011-07-30 12:04:12 +02001010 }
1011
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001012 batadv_update_route(bat_priv, orig_node, neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001013 goto out;
1014
1015unlock:
1016 rcu_read_unlock();
1017out:
1018 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001019 batadv_neigh_node_free_ref(neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001020 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001021 batadv_neigh_node_free_ref(router);
Marek Lindnerfc957272011-07-30 12:04:12 +02001022}
1023
Sven Eckelmann56303d32012-06-05 22:31:31 +02001024static int batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
1025 struct batadv_orig_node *orig_neigh_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02001026 struct batadv_ogm_packet *batadv_ogm_packet,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001027 struct batadv_hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +02001028{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001029 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1030 struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
Marek Lindnerfc957272011-07-30 12:04:12 +02001031 uint8_t total_count;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001032 uint8_t orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
1033 unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001034 int tq_asym_penalty, inv_asym_penalty, if_num, ret = 0;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001035 unsigned int combined_tq;
Marek Lindnerfc957272011-07-30 12:04:12 +02001036
1037 /* find corresponding one hop neighbor */
1038 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001039 hlist_for_each_entry_rcu(tmp_neigh_node,
Marek Lindnerfc957272011-07-30 12:04:12 +02001040 &orig_neigh_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001041 if (!batadv_compare_eth(tmp_neigh_node->addr,
1042 orig_neigh_node->orig))
Marek Lindnerfc957272011-07-30 12:04:12 +02001043 continue;
1044
1045 if (tmp_neigh_node->if_incoming != if_incoming)
1046 continue;
1047
1048 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
1049 continue;
1050
1051 neigh_node = tmp_neigh_node;
1052 break;
1053 }
1054 rcu_read_unlock();
1055
1056 if (!neigh_node)
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001057 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1058 orig_neigh_node->orig,
1059 orig_neigh_node,
Antonio Quartulli863dd7a2013-03-25 13:49:46 +01001060 orig_neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001061
1062 if (!neigh_node)
1063 goto out;
1064
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001065 /* if orig_node is direct neighbor update neigh_node last_seen */
Marek Lindnerfc957272011-07-30 12:04:12 +02001066 if (orig_node == orig_neigh_node)
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001067 neigh_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +02001068
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001069 orig_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +02001070
1071 /* find packet count of corresponding one hop neighbor */
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001072 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1073 if_num = if_incoming->if_num;
1074 orig_eq_count = orig_neigh_node->bat_iv.bcast_own_sum[if_num];
Antonio Quartulli0538f752013-09-02 12:15:01 +02001075 neigh_rq_count = neigh_node->bat_iv.real_packet_count;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001076 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001077
1078 /* pay attention to not get a value bigger than 100 % */
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001079 if (orig_eq_count > neigh_rq_count)
1080 total_count = neigh_rq_count;
1081 else
1082 total_count = orig_eq_count;
Marek Lindnerfc957272011-07-30 12:04:12 +02001083
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001084 /* if we have too few packets (too less data) we set tq_own to zero
1085 * if we receive too few packets it is not considered bidirectional
1086 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001087 if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
1088 neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
Marek Lindnerfc957272011-07-30 12:04:12 +02001089 tq_own = 0;
1090 else
1091 /* neigh_node->real_packet_count is never zero as we
1092 * only purge old information when getting new
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001093 * information
1094 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001095 tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count;
Marek Lindnerfc957272011-07-30 12:04:12 +02001096
Sven Eckelmann21a12362012-03-07 09:07:46 +01001097 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
Marek Lindnerfc957272011-07-30 12:04:12 +02001098 * affect the nearly-symmetric links only a little, but
1099 * punishes asymmetric links more. This will give a value
1100 * between 0 and TQ_MAX_VALUE
1101 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001102 neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
1103 neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
1104 neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
1105 BATADV_TQ_LOCAL_WINDOW_SIZE *
1106 BATADV_TQ_LOCAL_WINDOW_SIZE;
1107 inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
1108 inv_asym_penalty /= neigh_rq_max_cube;
1109 tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;
Marek Lindnerfc957272011-07-30 12:04:12 +02001110
Sven Eckelmann96412692012-06-05 22:31:30 +02001111 combined_tq = batadv_ogm_packet->tq * tq_own * tq_asym_penalty;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001112 combined_tq /= BATADV_TQ_MAX_VALUE * BATADV_TQ_MAX_VALUE;
Sven Eckelmann96412692012-06-05 22:31:30 +02001113 batadv_ogm_packet->tq = combined_tq;
Marek Lindnerfc957272011-07-30 12:04:12 +02001114
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001115 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001116 "bidirectional: orig = %-15pM neigh = %-15pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, total tq: %3i\n",
1117 orig_node->orig, orig_neigh_node->orig, total_count,
1118 neigh_rq_count, tq_own,
Sven Eckelmann96412692012-06-05 22:31:30 +02001119 tq_asym_penalty, batadv_ogm_packet->tq);
Marek Lindnerfc957272011-07-30 12:04:12 +02001120
1121 /* if link has the minimum required transmission quality
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001122 * consider it bidirectional
1123 */
Sven Eckelmann96412692012-06-05 22:31:30 +02001124 if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
Marek Lindnerfc957272011-07-30 12:04:12 +02001125 ret = 1;
1126
1127out:
1128 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001129 batadv_neigh_node_free_ref(neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001130 return ret;
1131}
1132
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001133/**
1134 * batadv_iv_ogm_update_seqnos - process a batman packet for all interfaces,
1135 * adjust the sequence number and find out whether it is a duplicate
1136 * @ethhdr: ethernet header of the packet
1137 * @batadv_ogm_packet: OGM packet to be considered
1138 * @if_incoming: interface on which the OGM packet was received
1139 *
1140 * Returns duplicate status as enum batadv_dup_status
Marek Lindnerfc957272011-07-30 12:04:12 +02001141 */
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001142static enum batadv_dup_status
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001143batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +02001144 const struct batadv_ogm_packet *batadv_ogm_packet,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001145 const struct batadv_hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +02001146{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001147 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1148 struct batadv_orig_node *orig_node;
1149 struct batadv_neigh_node *tmp_neigh_node;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001150 int is_dup;
Marek Lindnerfc957272011-07-30 12:04:12 +02001151 int32_t seq_diff;
1152 int need_update = 0;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001153 int set_mark;
1154 enum batadv_dup_status ret = BATADV_NO_DUP;
Sven Eckelmann96412692012-06-05 22:31:30 +02001155 uint32_t seqno = ntohl(batadv_ogm_packet->seqno);
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001156 uint8_t *neigh_addr;
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001157 uint8_t packet_count;
Antonio Quartulli0538f752013-09-02 12:15:01 +02001158 unsigned long *bitmap;
Marek Lindnerfc957272011-07-30 12:04:12 +02001159
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001160 orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
Marek Lindnerfc957272011-07-30 12:04:12 +02001161 if (!orig_node)
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001162 return BATADV_NO_DUP;
Marek Lindnerfc957272011-07-30 12:04:12 +02001163
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001164 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Al Viroe0f52112012-04-22 07:46:29 +01001165 seq_diff = seqno - orig_node->last_real_seqno;
Marek Lindnerfc957272011-07-30 12:04:12 +02001166
1167 /* signalize caller that the packet is to be dropped. */
Antonio Quartulli1e5cc262012-02-26 15:39:42 +01001168 if (!hlist_empty(&orig_node->neigh_list) &&
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001169 batadv_window_protected(bat_priv, seq_diff,
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001170 &orig_node->batman_seqno_reset)) {
1171 ret = BATADV_PROTECTED;
Marek Lindnerfc957272011-07-30 12:04:12 +02001172 goto out;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001173 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001174
1175 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001176 hlist_for_each_entry_rcu(tmp_neigh_node,
Marek Lindnerfc957272011-07-30 12:04:12 +02001177 &orig_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001178 neigh_addr = tmp_neigh_node->addr;
Antonio Quartulli0538f752013-09-02 12:15:01 +02001179 is_dup = batadv_test_bit(tmp_neigh_node->bat_iv.real_bits,
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001180 orig_node->last_real_seqno,
1181 seqno);
1182
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001183 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001184 tmp_neigh_node->if_incoming == if_incoming) {
Marek Lindnerfc957272011-07-30 12:04:12 +02001185 set_mark = 1;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001186 if (is_dup)
1187 ret = BATADV_NEIGH_DUP;
1188 } else {
Marek Lindnerfc957272011-07-30 12:04:12 +02001189 set_mark = 0;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001190 if (is_dup && (ret != BATADV_NEIGH_DUP))
1191 ret = BATADV_ORIG_DUP;
1192 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001193
1194 /* if the window moved, set the update flag. */
Antonio Quartulli0538f752013-09-02 12:15:01 +02001195 bitmap = tmp_neigh_node->bat_iv.real_bits;
1196 need_update |= batadv_bit_get_packet(bat_priv, bitmap,
Sven Eckelmann0f5f9322012-05-12 02:09:25 +02001197 seq_diff, set_mark);
Marek Lindnerfc957272011-07-30 12:04:12 +02001198
Antonio Quartulli0538f752013-09-02 12:15:01 +02001199 packet_count = bitmap_weight(tmp_neigh_node->bat_iv.real_bits,
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001200 BATADV_TQ_LOCAL_WINDOW_SIZE);
Antonio Quartulli0538f752013-09-02 12:15:01 +02001201 tmp_neigh_node->bat_iv.real_packet_count = packet_count;
Marek Lindnerfc957272011-07-30 12:04:12 +02001202 }
1203 rcu_read_unlock();
1204
1205 if (need_update) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001206 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001207 "updating last_seqno: old %u, new %u\n",
1208 orig_node->last_real_seqno, seqno);
Al Viroe0f52112012-04-22 07:46:29 +01001209 orig_node->last_real_seqno = seqno;
Marek Lindnerfc957272011-07-30 12:04:12 +02001210 }
1211
Marek Lindnerfc957272011-07-30 12:04:12 +02001212out:
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001213 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001214 batadv_orig_node_free_ref(orig_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001215 return ret;
1216}
1217
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001218static void batadv_iv_ogm_process(const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +02001219 struct batadv_ogm_packet *batadv_ogm_packet,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001220 const unsigned char *tt_buff,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001221 struct batadv_hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +02001222{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001223 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1224 struct batadv_hard_iface *hard_iface;
Antonio Quartulli0538f752013-09-02 12:15:01 +02001225 struct batadv_orig_node *orig_neigh_node, *orig_node, *orig_node_tmp;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001226 struct batadv_neigh_node *router = NULL, *router_router = NULL;
1227 struct batadv_neigh_node *orig_neigh_router = NULL;
Marek Lindnerfc957272011-07-30 12:04:12 +02001228 int has_directlink_flag;
1229 int is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
Antonio Quartulli281581d2013-03-25 22:27:00 +01001230 int is_bidirect;
Marek Lindner75cd33f2012-03-01 15:35:16 +08001231 bool is_single_hop_neigh = false;
Marek Lindner13b25412012-03-11 06:17:53 +08001232 bool is_from_best_next_hop = false;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001233 int sameseq, similar_ttl;
1234 enum batadv_dup_status dup_status;
Marek Lindnerfc957272011-07-30 12:04:12 +02001235 uint32_t if_incoming_seqno;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001236 uint8_t *prev_sender;
Marek Lindnerfc957272011-07-30 12:04:12 +02001237
1238 /* Silently drop when the batman packet is actually not a
1239 * correct packet.
1240 *
1241 * This might happen if a packet is padded (e.g. Ethernet has a
1242 * minimum frame length of 64 byte) and the aggregation interprets
1243 * it as an additional length.
1244 *
1245 * TODO: A more sane solution would be to have a bit in the
Sven Eckelmann96412692012-06-05 22:31:30 +02001246 * batadv_ogm_packet to detect whether the packet is the last
Marek Lindnerfc957272011-07-30 12:04:12 +02001247 * packet in an aggregation. Here we expect that the padding
1248 * is always zero (or not 0x01)
1249 */
Sven Eckelmann96412692012-06-05 22:31:30 +02001250 if (batadv_ogm_packet->header.packet_type != BATADV_IV_OGM)
Marek Lindnerfc957272011-07-30 12:04:12 +02001251 return;
1252
1253 /* could be changed by schedule_own_packet() */
Marek Lindner14511512012-08-02 17:20:26 +02001254 if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno);
Marek Lindnerfc957272011-07-30 12:04:12 +02001255
Sven Eckelmann96412692012-06-05 22:31:30 +02001256 if (batadv_ogm_packet->flags & BATADV_DIRECTLINK)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001257 has_directlink_flag = 1;
1258 else
1259 has_directlink_flag = 0;
Marek Lindnerfc957272011-07-30 12:04:12 +02001260
Sven Eckelmann96412692012-06-05 22:31:30 +02001261 if (batadv_compare_eth(ethhdr->h_source, batadv_ogm_packet->orig))
Marek Lindner75cd33f2012-03-01 15:35:16 +08001262 is_single_hop_neigh = true;
Marek Lindnerfc957272011-07-30 12:04:12 +02001263
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001264 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001265 "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 +02001266 ethhdr->h_source, if_incoming->net_dev->name,
Sven Eckelmann96412692012-06-05 22:31:30 +02001267 if_incoming->net_dev->dev_addr, batadv_ogm_packet->orig,
1268 batadv_ogm_packet->prev_sender,
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001269 ntohl(batadv_ogm_packet->seqno), batadv_ogm_packet->tq,
Sven Eckelmann96412692012-06-05 22:31:30 +02001270 batadv_ogm_packet->header.ttl,
1271 batadv_ogm_packet->header.version, has_directlink_flag);
Marek Lindnerfc957272011-07-30 12:04:12 +02001272
1273 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001274 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001275 if (hard_iface->if_status != BATADV_IF_ACTIVE)
Marek Lindnerfc957272011-07-30 12:04:12 +02001276 continue;
1277
1278 if (hard_iface->soft_iface != if_incoming->soft_iface)
1279 continue;
1280
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001281 if (batadv_compare_eth(ethhdr->h_source,
1282 hard_iface->net_dev->dev_addr))
Marek Lindnerfc957272011-07-30 12:04:12 +02001283 is_my_addr = 1;
1284
Sven Eckelmann96412692012-06-05 22:31:30 +02001285 if (batadv_compare_eth(batadv_ogm_packet->orig,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001286 hard_iface->net_dev->dev_addr))
Marek Lindnerfc957272011-07-30 12:04:12 +02001287 is_my_orig = 1;
1288
Sven Eckelmann96412692012-06-05 22:31:30 +02001289 if (batadv_compare_eth(batadv_ogm_packet->prev_sender,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001290 hard_iface->net_dev->dev_addr))
Marek Lindnerfc957272011-07-30 12:04:12 +02001291 is_my_oldorig = 1;
Marek Lindnerfc957272011-07-30 12:04:12 +02001292 }
1293 rcu_read_unlock();
1294
Marek Lindnerfc957272011-07-30 12:04:12 +02001295 if (is_my_addr) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001296 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001297 "Drop packet: received my own broadcast (sender: %pM)\n",
1298 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001299 return;
1300 }
1301
Marek Lindnerfc957272011-07-30 12:04:12 +02001302 if (is_my_orig) {
1303 unsigned long *word;
1304 int offset;
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001305 int32_t bit_pos;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001306 int16_t if_num;
1307 uint8_t *weight;
Marek Lindnerfc957272011-07-30 12:04:12 +02001308
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001309 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1310 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001311 if (!orig_neigh_node)
1312 return;
1313
1314 /* neighbor has to indicate direct link and it has to
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001315 * come via the corresponding interface
1316 * save packet seqno for bidirectional check
1317 */
Marek Lindnerfc957272011-07-30 12:04:12 +02001318 if (has_directlink_flag &&
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001319 batadv_compare_eth(if_incoming->net_dev->dev_addr,
Sven Eckelmann96412692012-06-05 22:31:30 +02001320 batadv_ogm_packet->orig)) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001321 if_num = if_incoming->if_num;
1322 offset = if_num * BATADV_NUM_WORDS;
Marek Lindnerfc957272011-07-30 12:04:12 +02001323
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001324 spin_lock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
1325 word = &(orig_neigh_node->bat_iv.bcast_own[offset]);
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001326 bit_pos = if_incoming_seqno - 2;
Sven Eckelmann96412692012-06-05 22:31:30 +02001327 bit_pos -= ntohl(batadv_ogm_packet->seqno);
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001328 batadv_set_bit(word, bit_pos);
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001329 weight = &orig_neigh_node->bat_iv.bcast_own_sum[if_num];
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001330 *weight = bitmap_weight(word,
1331 BATADV_TQ_LOCAL_WINDOW_SIZE);
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001332 spin_unlock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001333 }
1334
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001335 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001336 "Drop packet: originator packet from myself (via neighbor)\n");
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001337 batadv_orig_node_free_ref(orig_neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001338 return;
1339 }
1340
1341 if (is_my_oldorig) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001342 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001343 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1344 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001345 return;
1346 }
1347
Sven Eckelmann96412692012-06-05 22:31:30 +02001348 if (batadv_ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001349 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001350 "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1351 ethhdr->h_source);
Marek Lindner13b25412012-03-11 06:17:53 +08001352 return;
1353 }
1354
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001355 orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
Marek Lindnerfc957272011-07-30 12:04:12 +02001356 if (!orig_node)
1357 return;
1358
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001359 dup_status = batadv_iv_ogm_update_seqnos(ethhdr, batadv_ogm_packet,
1360 if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001361
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001362 if (dup_status == BATADV_PROTECTED) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001363 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001364 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1365 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001366 goto out;
1367 }
1368
Sven Eckelmann96412692012-06-05 22:31:30 +02001369 if (batadv_ogm_packet->tq == 0) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001370 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001371 "Drop packet: originator packet with tq equal 0\n");
Marek Lindnerfc957272011-07-30 12:04:12 +02001372 goto out;
1373 }
1374
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001375 router = batadv_orig_node_get_router(orig_node);
Antonio Quartulli0538f752013-09-02 12:15:01 +02001376 if (router) {
1377 orig_node_tmp = router->orig_node;
1378 router_router = batadv_orig_node_get_router(orig_node_tmp);
1379 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001380
Antonio Quartulli0538f752013-09-02 12:15:01 +02001381 if ((router && router->bat_iv.tq_avg != 0) &&
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001382 (batadv_compare_eth(router->addr, ethhdr->h_source)))
Marek Lindner13b25412012-03-11 06:17:53 +08001383 is_from_best_next_hop = true;
1384
Sven Eckelmann96412692012-06-05 22:31:30 +02001385 prev_sender = batadv_ogm_packet->prev_sender;
Marek Lindnerfc957272011-07-30 12:04:12 +02001386 /* avoid temporary routing loops */
1387 if (router && router_router &&
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001388 (batadv_compare_eth(router->addr, prev_sender)) &&
Sven Eckelmann96412692012-06-05 22:31:30 +02001389 !(batadv_compare_eth(batadv_ogm_packet->orig, prev_sender)) &&
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001390 (batadv_compare_eth(router->addr, router_router->addr))) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001391 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001392 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1393 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001394 goto out;
1395 }
1396
Marek Lindneref261572013-04-23 21:39:57 +08001397 batadv_tvlv_ogm_receive(bat_priv, batadv_ogm_packet, orig_node);
1398
Marek Lindnerfc957272011-07-30 12:04:12 +02001399 /* if sender is a direct neighbor the sender mac equals
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001400 * originator mac
1401 */
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001402 if (is_single_hop_neigh)
1403 orig_neigh_node = orig_node;
1404 else
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001405 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1406 ethhdr->h_source);
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001407
Marek Lindnerfc957272011-07-30 12:04:12 +02001408 if (!orig_neigh_node)
1409 goto out;
1410
Martin Hundebølld56b1702013-01-25 11:12:39 +01001411 /* Update nc_nodes of the originator */
1412 batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node,
1413 batadv_ogm_packet, is_single_hop_neigh);
1414
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001415 orig_neigh_router = batadv_orig_node_get_router(orig_neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001416
1417 /* drop packet if sender is not a direct neighbor and if we
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001418 * don't route towards it
1419 */
Marek Lindnerfc957272011-07-30 12:04:12 +02001420 if (!is_single_hop_neigh && (!orig_neigh_router)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001421 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001422 "Drop packet: OGM via unknown neighbor!\n");
Marek Lindnerfc957272011-07-30 12:04:12 +02001423 goto out_neigh;
1424 }
1425
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001426 is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02001427 batadv_ogm_packet, if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001428
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001429 batadv_bonding_save_primary(orig_node, orig_neigh_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02001430 batadv_ogm_packet);
Marek Lindnerfc957272011-07-30 12:04:12 +02001431
1432 /* update ranking if it is not a duplicate or has the same
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001433 * seqno and similar ttl as the non-duplicate
1434 */
Sven Eckelmann96412692012-06-05 22:31:30 +02001435 sameseq = orig_node->last_real_seqno == ntohl(batadv_ogm_packet->seqno);
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001436 similar_ttl = orig_node->last_ttl - 3 <= batadv_ogm_packet->header.ttl;
1437 if (is_bidirect && ((dup_status == BATADV_NO_DUP) ||
1438 (sameseq && similar_ttl)))
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001439 batadv_iv_ogm_orig_update(bat_priv, orig_node, ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +02001440 batadv_ogm_packet, if_incoming,
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001441 tt_buff, dup_status);
Marek Lindnerfc957272011-07-30 12:04:12 +02001442
1443 /* is single hop (direct) neighbor */
1444 if (is_single_hop_neigh) {
Marek Lindnerfc957272011-07-30 12:04:12 +02001445 /* mark direct link on incoming interface */
Sven Eckelmann96412692012-06-05 22:31:30 +02001446 batadv_iv_ogm_forward(orig_node, ethhdr, batadv_ogm_packet,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001447 is_single_hop_neigh,
1448 is_from_best_next_hop, if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001449
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001450 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001451 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
Marek Lindnerfc957272011-07-30 12:04:12 +02001452 goto out_neigh;
1453 }
1454
1455 /* multihop originator */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001456 if (!is_bidirect) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001457 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001458 "Drop packet: not received via bidirectional link\n");
Marek Lindnerfc957272011-07-30 12:04:12 +02001459 goto out_neigh;
1460 }
1461
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001462 if (dup_status == BATADV_NEIGH_DUP) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001463 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001464 "Drop packet: duplicate packet received\n");
Marek Lindnerfc957272011-07-30 12:04:12 +02001465 goto out_neigh;
1466 }
1467
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001468 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001469 "Forwarding packet: rebroadcast originator packet\n");
Sven Eckelmann96412692012-06-05 22:31:30 +02001470 batadv_iv_ogm_forward(orig_node, ethhdr, batadv_ogm_packet,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001471 is_single_hop_neigh, is_from_best_next_hop,
1472 if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001473
1474out_neigh:
1475 if ((orig_neigh_node) && (!is_single_hop_neigh))
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001476 batadv_orig_node_free_ref(orig_neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001477out:
1478 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001479 batadv_neigh_node_free_ref(router);
Marek Lindnerfc957272011-07-30 12:04:12 +02001480 if (router_router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001481 batadv_neigh_node_free_ref(router_router);
Marek Lindnerfc957272011-07-30 12:04:12 +02001482 if (orig_neigh_router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001483 batadv_neigh_node_free_ref(orig_neigh_router);
Marek Lindnerfc957272011-07-30 12:04:12 +02001484
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001485 batadv_orig_node_free_ref(orig_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001486}
1487
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001488static int batadv_iv_ogm_receive(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001489 struct batadv_hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +02001490{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001491 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Sven Eckelmann96412692012-06-05 22:31:30 +02001492 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindner8780dad2011-12-05 04:01:51 +08001493 struct ethhdr *ethhdr;
1494 int buff_pos = 0, packet_len;
Marek Lindneref261572013-04-23 21:39:57 +08001495 unsigned char *tvlv_buff, *packet_buff;
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001496 uint8_t *packet_pos;
Marek Lindneref261572013-04-23 21:39:57 +08001497 bool ret;
Marek Lindnerc3e29312012-03-04 16:56:25 +08001498
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001499 ret = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
Marek Lindnerc3e29312012-03-04 16:56:25 +08001500 if (!ret)
1501 return NET_RX_DROP;
Marek Lindnerfc957272011-07-30 12:04:12 +02001502
Marek Lindneredbf12b2012-03-11 06:17:49 +08001503 /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
1504 * that does not have B.A.T.M.A.N. IV enabled ?
1505 */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001506 if (bat_priv->bat_algo_ops->bat_ogm_emit != batadv_iv_ogm_emit)
Marek Lindneredbf12b2012-03-11 06:17:49 +08001507 return NET_RX_DROP;
1508
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001509 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
1510 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
Martin Hundebøllf8214862012-04-20 17:02:45 +02001511 skb->len + ETH_HLEN);
1512
Marek Lindner8780dad2011-12-05 04:01:51 +08001513 packet_len = skb_headlen(skb);
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001514 ethhdr = eth_hdr(skb);
Marek Lindner8780dad2011-12-05 04:01:51 +08001515 packet_buff = skb->data;
Sven Eckelmann96412692012-06-05 22:31:30 +02001516 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
Marek Lindnerfc957272011-07-30 12:04:12 +02001517
1518 /* unpack the aggregated packets and process them one by one */
Marek Lindnerb47506d2013-03-04 10:39:49 +08001519 while (batadv_iv_ogm_aggr_packet(buff_pos, packet_len,
Marek Lindneref261572013-04-23 21:39:57 +08001520 batadv_ogm_packet->tvlv_len)) {
1521 tvlv_buff = packet_buff + buff_pos + BATADV_OGM_HLEN;
Marek Lindnerfc957272011-07-30 12:04:12 +02001522
Marek Lindneref261572013-04-23 21:39:57 +08001523 batadv_iv_ogm_process(ethhdr, batadv_ogm_packet,
1524 tvlv_buff, if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001525
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001526 buff_pos += BATADV_OGM_HLEN;
Marek Lindneref261572013-04-23 21:39:57 +08001527 buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
Marek Lindnerfc957272011-07-30 12:04:12 +02001528
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001529 packet_pos = packet_buff + buff_pos;
1530 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Marek Lindnerb47506d2013-03-04 10:39:49 +08001531 }
Marek Lindnerc3e29312012-03-04 16:56:25 +08001532
1533 kfree_skb(skb);
1534 return NET_RX_SUCCESS;
Marek Lindnerfc957272011-07-30 12:04:12 +02001535}
Marek Lindner1c280472011-11-28 17:40:17 +08001536
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001537/**
1538 * batadv_iv_ogm_orig_print - print the originator table
1539 * @bat_priv: the bat priv with all the soft interface information
1540 * @seq: debugfs table seq_file struct
1541 */
1542static void batadv_iv_ogm_orig_print(struct batadv_priv *bat_priv,
1543 struct seq_file *seq)
1544{
1545 struct batadv_neigh_node *neigh_node, *neigh_node_tmp;
1546 struct batadv_hashtable *hash = bat_priv->orig_hash;
1547 int last_seen_msecs, last_seen_secs;
1548 struct batadv_orig_node *orig_node;
1549 unsigned long last_seen_jiffies;
1550 struct hlist_head *head;
1551 int batman_count = 0;
1552 uint32_t i;
1553
1554 seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
1555 "Originator", "last-seen", "#", BATADV_TQ_MAX_VALUE,
1556 "Nexthop", "outgoingIF", "Potential nexthops");
1557
1558 for (i = 0; i < hash->size; i++) {
1559 head = &hash->table[i];
1560
1561 rcu_read_lock();
1562 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1563 neigh_node = batadv_orig_node_get_router(orig_node);
1564 if (!neigh_node)
1565 continue;
1566
1567 if (neigh_node->bat_iv.tq_avg == 0)
1568 goto next;
1569
1570 last_seen_jiffies = jiffies - orig_node->last_seen;
1571 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
1572 last_seen_secs = last_seen_msecs / 1000;
1573 last_seen_msecs = last_seen_msecs % 1000;
1574
1575 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
1576 orig_node->orig, last_seen_secs,
1577 last_seen_msecs, neigh_node->bat_iv.tq_avg,
1578 neigh_node->addr,
1579 neigh_node->if_incoming->net_dev->name);
1580
1581 hlist_for_each_entry_rcu(neigh_node_tmp,
1582 &orig_node->neigh_list, list) {
1583 seq_printf(seq, " %pM (%3i)",
1584 neigh_node_tmp->addr,
1585 neigh_node_tmp->bat_iv.tq_avg);
1586 }
1587
1588 seq_puts(seq, "\n");
1589 batman_count++;
1590
1591next:
1592 batadv_neigh_node_free_ref(neigh_node);
1593 }
1594 rcu_read_unlock();
1595 }
1596
1597 if (batman_count == 0)
1598 seq_puts(seq, "No batman nodes in range ...\n");
1599}
1600
Antonio Quartullia3285a82013-09-02 12:15:04 +02001601/**
1602 * batadv_iv_ogm_neigh_cmp - compare the metrics of two neighbors
1603 * @neigh1: the first neighbor object of the comparison
1604 * @neigh2: the second neighbor object of the comparison
1605 *
1606 * Returns a value less, equal to or greater than 0 if the metric via neigh1 is
1607 * lower, the same as or higher than the metric via neigh2
1608 */
1609static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1,
1610 struct batadv_neigh_node *neigh2)
1611{
1612 uint8_t tq1, tq2;
1613
1614 tq1 = neigh1->bat_iv.tq_avg;
1615 tq2 = neigh2->bat_iv.tq_avg;
1616
1617 return tq1 - tq2;
1618}
1619
Antonio Quartullic43c9812013-09-02 12:15:05 +02001620/**
1621 * batadv_iv_ogm_neigh_is_eob - check if neigh1 is equally good or better than
1622 * neigh2 from the metric prospective
1623 * @neigh1: the first neighbor object of the comparison
1624 * @neigh2: the second neighbor object of the comparison
1625 *
1626 * Returns true if the metric via neigh1 is equally good or better than the
1627 * metric via neigh2, false otherwise.
1628 */
1629static bool batadv_iv_ogm_neigh_is_eob(struct batadv_neigh_node *neigh1,
1630 struct batadv_neigh_node *neigh2)
1631{
1632 int diff = batadv_iv_ogm_neigh_cmp(neigh1, neigh2);
1633
1634 return diff > -BATADV_TQ_SIMILARITY_THRESHOLD;
1635}
1636
Sven Eckelmann56303d32012-06-05 22:31:31 +02001637static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
Marek Lindner519d3492012-04-18 17:15:57 +08001638 .name = "BATMAN_IV",
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001639 .bat_iface_enable = batadv_iv_ogm_iface_enable,
1640 .bat_iface_disable = batadv_iv_ogm_iface_disable,
1641 .bat_iface_update_mac = batadv_iv_ogm_iface_update_mac,
1642 .bat_primary_iface_set = batadv_iv_ogm_primary_iface_set,
1643 .bat_ogm_schedule = batadv_iv_ogm_schedule,
1644 .bat_ogm_emit = batadv_iv_ogm_emit,
Antonio Quartullia3285a82013-09-02 12:15:04 +02001645 .bat_neigh_cmp = batadv_iv_ogm_neigh_cmp,
Antonio Quartullic43c9812013-09-02 12:15:05 +02001646 .bat_neigh_is_equiv_or_better = batadv_iv_ogm_neigh_is_eob,
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001647 .bat_orig_print = batadv_iv_ogm_orig_print,
Antonio Quartullid0015fd2013-09-03 11:10:23 +02001648 .bat_orig_free = batadv_iv_ogm_orig_free,
1649 .bat_orig_add_if = batadv_iv_ogm_orig_add_if,
1650 .bat_orig_del_if = batadv_iv_ogm_orig_del_if,
Marek Lindner1c280472011-11-28 17:40:17 +08001651};
1652
Sven Eckelmann81c524f2012-05-12 02:09:22 +02001653int __init batadv_iv_init(void)
Marek Lindner1c280472011-11-28 17:40:17 +08001654{
Marek Lindnerc3e29312012-03-04 16:56:25 +08001655 int ret;
1656
1657 /* batman originator packet */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001658 ret = batadv_recv_handler_register(BATADV_IV_OGM,
1659 batadv_iv_ogm_receive);
Marek Lindnerc3e29312012-03-04 16:56:25 +08001660 if (ret < 0)
1661 goto out;
1662
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001663 ret = batadv_algo_register(&batadv_batman_iv);
Marek Lindnerc3e29312012-03-04 16:56:25 +08001664 if (ret < 0)
1665 goto handler_unregister;
1666
1667 goto out;
1668
1669handler_unregister:
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001670 batadv_recv_handler_unregister(BATADV_IV_OGM);
Marek Lindnerc3e29312012-03-04 16:56:25 +08001671out:
1672 return ret;
Marek Lindner1c280472011-11-28 17:40:17 +08001673}