blob: 512159bf607f0d4a3b09eae9af101775a8b56441 [file] [log] [blame]
Simon Wunderliche19f9752014-01-04 18:04:25 +01001/* Copyright (C) 2007-2014 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
18#include "main.h"
Marek Lindnerfc957272011-07-30 12:04:12 +020019#include "translation-table.h"
Marek Lindnerfc957272011-07-30 12:04:12 +020020#include "originator.h"
21#include "routing.h"
22#include "gateway_common.h"
23#include "gateway_client.h"
24#include "hard-interface.h"
25#include "send.h"
Marek Lindner1c280472011-11-28 17:40:17 +080026#include "bat_algo.h"
Martin Hundebølld56b1702013-01-25 11:12:39 +010027#include "network-coding.h"
Marek Lindnerfc957272011-07-30 12:04:12 +020028
Antonio Quartulli791c2a22013-08-17 12:44:44 +020029
30/**
31 * batadv_dup_status - duplicate status
32 * @BATADV_NO_DUP: the packet is a duplicate
33 * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for the
34 * neighbor)
35 * @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor
36 * @BATADV_PROTECTED: originator is currently protected (after reboot)
37 */
38enum batadv_dup_status {
39 BATADV_NO_DUP = 0,
40 BATADV_ORIG_DUP,
41 BATADV_NEIGH_DUP,
42 BATADV_PROTECTED,
43};
44
Antonio Quartulli24a5dee2013-04-08 09:38:12 +020045/**
46 * batadv_ring_buffer_set - update the ring buffer with the given value
47 * @lq_recv: pointer to the ring buffer
48 * @lq_index: index to store the value at
49 * @value: value to store in the ring buffer
50 */
51static void batadv_ring_buffer_set(uint8_t lq_recv[], uint8_t *lq_index,
52 uint8_t value)
53{
54 lq_recv[*lq_index] = value;
55 *lq_index = (*lq_index + 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE;
56}
57
58/**
59 * batadv_ring_buffer_set - compute the average of all non-zero values stored
60 * in the given ring buffer
61 * @lq_recv: pointer to the ring buffer
62 *
63 * Returns computed average value.
64 */
65static uint8_t batadv_ring_buffer_avg(const uint8_t lq_recv[])
66{
67 const uint8_t *ptr;
68 uint16_t count = 0, i = 0, sum = 0;
69
70 ptr = lq_recv;
71
72 while (i < BATADV_TQ_GLOBAL_WINDOW_SIZE) {
73 if (*ptr != 0) {
74 count++;
75 sum += *ptr;
76 }
77
78 i++;
79 ptr++;
80 }
81
82 if (count == 0)
83 return 0;
84
85 return (uint8_t)(sum / count);
86}
David S. Millerd98cae64e2013-06-19 16:49:39 -070087
Antonio Quartullibbad0a52013-09-02 12:15:02 +020088/**
Antonio Quartullid0015fd2013-09-03 11:10:23 +020089 * batadv_iv_ogm_orig_free - free the private resources allocated for this
90 * orig_node
91 * @orig_node: the orig_node for which the resources have to be free'd
92 */
93static void batadv_iv_ogm_orig_free(struct batadv_orig_node *orig_node)
94{
95 kfree(orig_node->bat_iv.bcast_own);
96 kfree(orig_node->bat_iv.bcast_own_sum);
97}
98
99/**
100 * batadv_iv_ogm_orig_add_if - change the private structures of the orig_node to
101 * include the new hard-interface
102 * @orig_node: the orig_node that has to be changed
103 * @max_if_num: the current amount of interfaces
104 *
105 * Returns 0 on success, a negative error code otherwise.
106 */
107static int batadv_iv_ogm_orig_add_if(struct batadv_orig_node *orig_node,
108 int max_if_num)
109{
110 void *data_ptr;
111 size_t data_size, old_size;
112 int ret = -ENOMEM;
113
114 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
115
116 data_size = max_if_num * sizeof(unsigned long) * BATADV_NUM_WORDS;
117 old_size = (max_if_num - 1) * sizeof(unsigned long) * BATADV_NUM_WORDS;
118 data_ptr = kmalloc(data_size, GFP_ATOMIC);
119 if (!data_ptr)
120 goto unlock;
121
122 memcpy(data_ptr, orig_node->bat_iv.bcast_own, old_size);
123 kfree(orig_node->bat_iv.bcast_own);
124 orig_node->bat_iv.bcast_own = data_ptr;
125
126 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
127 if (!data_ptr) {
128 kfree(orig_node->bat_iv.bcast_own);
129 goto unlock;
130 }
131
132 memcpy(data_ptr, orig_node->bat_iv.bcast_own_sum,
133 (max_if_num - 1) * sizeof(uint8_t));
134 kfree(orig_node->bat_iv.bcast_own_sum);
135 orig_node->bat_iv.bcast_own_sum = data_ptr;
136
137 ret = 0;
138
139unlock:
140 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
141
142 return ret;
143}
144
145/**
146 * batadv_iv_ogm_orig_del_if - change the private structures of the orig_node to
147 * exclude the removed interface
148 * @orig_node: the orig_node that has to be changed
149 * @max_if_num: the current amount of interfaces
150 * @del_if_num: the index of the interface being removed
151 *
152 * Returns 0 on success, a negative error code otherwise.
153 */
154static int batadv_iv_ogm_orig_del_if(struct batadv_orig_node *orig_node,
155 int max_if_num, int del_if_num)
156{
157 int chunk_size, ret = -ENOMEM, if_offset;
158 void *data_ptr = NULL;
159
160 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
161
162 /* last interface was removed */
163 if (max_if_num == 0)
164 goto free_bcast_own;
165
166 chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS;
167 data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC);
168 if (!data_ptr)
169 goto unlock;
170
171 /* copy first part */
172 memcpy(data_ptr, orig_node->bat_iv.bcast_own, del_if_num * chunk_size);
173
174 /* copy second part */
175 memcpy((char *)data_ptr + del_if_num * chunk_size,
176 orig_node->bat_iv.bcast_own + ((del_if_num + 1) * chunk_size),
177 (max_if_num - del_if_num) * chunk_size);
178
179free_bcast_own:
180 kfree(orig_node->bat_iv.bcast_own);
181 orig_node->bat_iv.bcast_own = data_ptr;
182
183 if (max_if_num == 0)
184 goto free_own_sum;
185
186 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
187 if (!data_ptr) {
188 kfree(orig_node->bat_iv.bcast_own);
189 goto unlock;
190 }
191
192 memcpy(data_ptr, orig_node->bat_iv.bcast_own_sum,
193 del_if_num * sizeof(uint8_t));
194
195 if_offset = (del_if_num + 1) * sizeof(uint8_t);
196 memcpy((char *)data_ptr + del_if_num * sizeof(uint8_t),
197 orig_node->bat_iv.bcast_own_sum + if_offset,
198 (max_if_num - del_if_num) * sizeof(uint8_t));
199
200free_own_sum:
201 kfree(orig_node->bat_iv.bcast_own_sum);
202 orig_node->bat_iv.bcast_own_sum = data_ptr;
203
204 ret = 0;
205unlock:
206 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
207
208 return ret;
209}
210
211/**
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200212 * batadv_iv_ogm_orig_get - retrieve or create (if does not exist) an originator
213 * @bat_priv: the bat priv with all the soft interface information
214 * @addr: mac address of the originator
215 *
216 * Returns the originator object corresponding to the passed mac address or NULL
217 * on failure.
218 * If the object does not exists it is created an initialised.
219 */
220static struct batadv_orig_node *
221batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const uint8_t *addr)
222{
223 struct batadv_orig_node *orig_node;
224 int size, hash_added;
225
226 orig_node = batadv_orig_hash_find(bat_priv, addr);
227 if (orig_node)
228 return orig_node;
229
230 orig_node = batadv_orig_node_new(bat_priv, addr);
231 if (!orig_node)
232 return NULL;
233
234 spin_lock_init(&orig_node->bat_iv.ogm_cnt_lock);
235
236 size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS;
237 orig_node->bat_iv.bcast_own = kzalloc(size, GFP_ATOMIC);
238 if (!orig_node->bat_iv.bcast_own)
239 goto free_orig_node;
240
241 size = bat_priv->num_ifaces * sizeof(uint8_t);
242 orig_node->bat_iv.bcast_own_sum = kzalloc(size, GFP_ATOMIC);
243 if (!orig_node->bat_iv.bcast_own_sum)
244 goto free_bcast_own;
245
246 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
247 batadv_choose_orig, orig_node,
248 &orig_node->hash_entry);
249 if (hash_added != 0)
250 goto free_bcast_own;
251
252 return orig_node;
253
254free_bcast_own:
255 kfree(orig_node->bat_iv.bcast_own);
256free_orig_node:
257 batadv_orig_node_free_ref(orig_node);
258
259 return NULL;
260}
261
Sven Eckelmann56303d32012-06-05 22:31:31 +0200262static struct batadv_neigh_node *
263batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface,
264 const uint8_t *neigh_addr,
265 struct batadv_orig_node *orig_node,
Antonio Quartulli863dd7a2013-03-25 13:49:46 +0100266 struct batadv_orig_node *orig_neigh)
Marek Lindner7ae8b282012-03-01 15:35:21 +0800267{
Antonio Quartulli0538f752013-09-02 12:15:01 +0200268 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200269 struct batadv_neigh_node *neigh_node;
Marek Lindner7ae8b282012-03-01 15:35:21 +0800270
Antonio Quartulli0538f752013-09-02 12:15:01 +0200271 neigh_node = batadv_neigh_node_new(hard_iface, neigh_addr, orig_node);
Marek Lindner7ae8b282012-03-01 15:35:21 +0800272 if (!neigh_node)
273 goto out;
274
Simon Wunderlich89652332013-11-13 19:14:46 +0100275 if (!atomic_inc_not_zero(&hard_iface->refcount)) {
276 kfree(neigh_node);
277 neigh_node = NULL;
278 goto out;
279 }
280
281 neigh_node->orig_node = orig_neigh;
282 neigh_node->if_incoming = hard_iface;
Marek Lindner7ae8b282012-03-01 15:35:21 +0800283
Antonio Quartulli0538f752013-09-02 12:15:01 +0200284 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
285 "Creating new neighbor %pM for orig_node %pM on interface %s\n",
286 neigh_addr, orig_node->orig, hard_iface->net_dev->name);
Marek Lindner7ae8b282012-03-01 15:35:21 +0800287
288 spin_lock_bh(&orig_node->neigh_list_lock);
289 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
290 spin_unlock_bh(&orig_node->neigh_list_lock);
291
292out:
293 return neigh_node;
294}
295
Sven Eckelmann56303d32012-06-05 22:31:31 +0200296static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200297{
Sven Eckelmann96412692012-06-05 22:31:30 +0200298 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindner14511512012-08-02 17:20:26 +0200299 unsigned char *ogm_buff;
Marek Lindnerd7d32ec2012-02-07 17:20:46 +0800300 uint32_t random_seqno;
Sven Eckelmann5346c352012-05-05 13:27:28 +0200301 int res = -ENOMEM;
Marek Lindnerd7d32ec2012-02-07 17:20:46 +0800302
303 /* randomize initial seqno to avoid collision */
304 get_random_bytes(&random_seqno, sizeof(random_seqno));
Marek Lindner14511512012-08-02 17:20:26 +0200305 atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno);
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200306
Marek Lindner14511512012-08-02 17:20:26 +0200307 hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN;
308 ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC);
309 if (!ogm_buff)
Marek Lindner77af7572012-02-07 17:20:48 +0800310 goto out;
311
Marek Lindner14511512012-08-02 17:20:26 +0200312 hard_iface->bat_iv.ogm_buff = ogm_buff;
313
314 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100315 batadv_ogm_packet->packet_type = BATADV_IV_OGM;
316 batadv_ogm_packet->version = BATADV_COMPAT_VERSION;
317 batadv_ogm_packet->ttl = 2;
Sven Eckelmann96412692012-06-05 22:31:30 +0200318 batadv_ogm_packet->flags = BATADV_NO_FLAGS;
Marek Lindner414254e2013-04-23 21:39:58 +0800319 batadv_ogm_packet->reserved = 0;
Sven Eckelmann96412692012-06-05 22:31:30 +0200320 batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE;
Marek Lindner77af7572012-02-07 17:20:48 +0800321
322 res = 0;
323
324out:
325 return res;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200326}
327
Sven Eckelmann56303d32012-06-05 22:31:31 +0200328static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
Marek Lindner00a50072012-02-07 17:20:47 +0800329{
Marek Lindner14511512012-08-02 17:20:26 +0200330 kfree(hard_iface->bat_iv.ogm_buff);
331 hard_iface->bat_iv.ogm_buff = NULL;
Marek Lindner00a50072012-02-07 17:20:47 +0800332}
333
Sven Eckelmann56303d32012-06-05 22:31:31 +0200334static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface)
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200335{
Sven Eckelmann96412692012-06-05 22:31:30 +0200336 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindner14511512012-08-02 17:20:26 +0200337 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200338
Marek Lindner14511512012-08-02 17:20:26 +0200339 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
Sven Eckelmann96412692012-06-05 22:31:30 +0200340 memcpy(batadv_ogm_packet->orig,
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200341 hard_iface->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmann96412692012-06-05 22:31:30 +0200342 memcpy(batadv_ogm_packet->prev_sender,
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200343 hard_iface->net_dev->dev_addr, ETH_ALEN);
344}
345
Sven Eckelmann56303d32012-06-05 22:31:31 +0200346static void
347batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface)
Marek Lindnerc3229392012-03-11 06:17:50 +0800348{
Sven Eckelmann96412692012-06-05 22:31:30 +0200349 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindner14511512012-08-02 17:20:26 +0200350 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
Marek Lindnerc3229392012-03-11 06:17:50 +0800351
Marek Lindner14511512012-08-02 17:20:26 +0200352 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
Sven Eckelmann96412692012-06-05 22:31:30 +0200353 batadv_ogm_packet->flags = BATADV_PRIMARIES_FIRST_HOP;
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100354 batadv_ogm_packet->ttl = BATADV_TTL;
Marek Lindnerc3229392012-03-11 06:17:50 +0800355}
356
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200357/* when do we schedule our own ogm to be sent */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200358static unsigned long
Sven Eckelmann56303d32012-06-05 22:31:31 +0200359batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200360{
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200361 unsigned int msecs;
362
363 msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
Akinobu Mitae76e4322012-12-24 11:14:07 +0900364 msecs += prandom_u32() % (2 * BATADV_JITTER);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200365
366 return jiffies + msecs_to_jiffies(msecs);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200367}
368
369/* when do we schedule a ogm packet to be sent */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200370static unsigned long batadv_iv_ogm_fwd_send_time(void)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200371{
Akinobu Mitae76e4322012-12-24 11:14:07 +0900372 return jiffies + msecs_to_jiffies(prandom_u32() % (BATADV_JITTER / 2));
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200373}
374
375/* apply hop penalty for a normal link */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200376static uint8_t batadv_hop_penalty(uint8_t tq,
377 const struct batadv_priv *bat_priv)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200378{
379 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200380 int new_tq;
381
382 new_tq = tq * (BATADV_TQ_MAX_VALUE - hop_penalty);
383 new_tq /= BATADV_TQ_MAX_VALUE;
384
385 return new_tq;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200386}
387
Marek Lindnerfc957272011-07-30 12:04:12 +0200388/* is there another aggregated packet here? */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200389static int batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len,
Marek Lindneref261572013-04-23 21:39:57 +0800390 __be16 tvlv_len)
Marek Lindnerfc957272011-07-30 12:04:12 +0200391{
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200392 int next_buff_pos = 0;
393
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200394 next_buff_pos += buff_pos + BATADV_OGM_HLEN;
Marek Lindneref261572013-04-23 21:39:57 +0800395 next_buff_pos += ntohs(tvlv_len);
Marek Lindnerfc957272011-07-30 12:04:12 +0200396
397 return (next_buff_pos <= packet_len) &&
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200398 (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
Marek Lindnerfc957272011-07-30 12:04:12 +0200399}
400
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200401/* send a batman ogm to a given interface */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200402static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet,
403 struct batadv_hard_iface *hard_iface)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200404{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200405 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200406 char *fwd_str;
407 uint8_t packet_num;
408 int16_t buff_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +0200409 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200410 struct sk_buff *skb;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200411 uint8_t *packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200412
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200413 if (hard_iface->if_status != BATADV_IF_ACTIVE)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200414 return;
415
416 packet_num = 0;
417 buff_pos = 0;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200418 packet_pos = forw_packet->skb->data;
419 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200420
421 /* adjust all flags and log packets */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200422 while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
Marek Lindneref261572013-04-23 21:39:57 +0800423 batadv_ogm_packet->tvlv_len)) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200424 /* we might have aggregated direct link packets with an
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200425 * ordinary base packet
426 */
Sven Eckelmann8de47de2012-07-08 16:32:09 +0200427 if (forw_packet->direct_link_flags & BIT(packet_num) &&
428 forw_packet->if_incoming == hard_iface)
Sven Eckelmann96412692012-06-05 22:31:30 +0200429 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200430 else
Sven Eckelmann96412692012-06-05 22:31:30 +0200431 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200432
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200433 if (packet_num > 0 || !forw_packet->own)
434 fwd_str = "Forwarding";
435 else
436 fwd_str = "Sending own";
437
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200438 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800439 "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200440 fwd_str, (packet_num > 0 ? "aggregated " : ""),
Sven Eckelmann96412692012-06-05 22:31:30 +0200441 batadv_ogm_packet->orig,
442 ntohl(batadv_ogm_packet->seqno),
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100443 batadv_ogm_packet->tq, batadv_ogm_packet->ttl,
Sven Eckelmann96412692012-06-05 22:31:30 +0200444 (batadv_ogm_packet->flags & BATADV_DIRECTLINK ?
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200445 "on" : "off"),
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800446 hard_iface->net_dev->name,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200447 hard_iface->net_dev->dev_addr);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200448
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200449 buff_pos += BATADV_OGM_HLEN;
Marek Lindneref261572013-04-23 21:39:57 +0800450 buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200451 packet_num++;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200452 packet_pos = forw_packet->skb->data + buff_pos;
453 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200454 }
455
456 /* create clone because function is called more than once */
457 skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
Martin Hundebøllf8214862012-04-20 17:02:45 +0200458 if (skb) {
Sven Eckelmannd69909d2012-06-03 22:19:20 +0200459 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
460 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
Martin Hundebøllf8214862012-04-20 17:02:45 +0200461 skb->len + ETH_HLEN);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200462 batadv_send_skb_packet(skb, hard_iface, batadv_broadcast_addr);
Martin Hundebøllf8214862012-04-20 17:02:45 +0200463 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200464}
465
466/* send a batman ogm packet */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200467static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200468{
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200469 struct net_device *soft_iface;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200470 struct batadv_priv *bat_priv;
471 struct batadv_hard_iface *primary_if = NULL;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200472
473 if (!forw_packet->if_incoming) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100474 pr_err("Error - can't forward packet: incoming iface not specified\n");
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200475 goto out;
476 }
477
478 soft_iface = forw_packet->if_incoming->soft_iface;
479 bat_priv = netdev_priv(soft_iface);
480
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100481 if (WARN_ON(!forw_packet->if_outgoing))
482 goto out;
483
484 if (WARN_ON(forw_packet->if_outgoing->soft_iface != soft_iface))
485 goto out;
486
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200487 if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200488 goto out;
489
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200490 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200491 if (!primary_if)
492 goto out;
493
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100494 /* only for one specific outgoing interface */
495 batadv_iv_ogm_send_to_if(forw_packet, forw_packet->if_outgoing);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200496
497out:
498 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200499 batadv_hardif_free_ref(primary_if);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200500}
501
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100502/**
503 * batadv_iv_ogm_can_aggregate - find out if an OGM can be aggregated on an
504 * existing forward packet
505 * @new_bat_ogm_packet: OGM packet to be aggregated
506 * @bat_priv: the bat priv with all the soft interface information
507 * @packet_len: (total) length of the OGM
508 * @send_time: timestamp (jiffies) when the packet is to be sent
509 * @direktlink: true if this is a direct link packet
510 * @if_incoming: interface where the packet was received
511 * @if_outgoing: interface for which the retransmission should be considered
512 * @forw_packet: the forwarded packet which should be checked
513 *
514 * Returns true if new_packet can be aggregated with forw_packet
515 */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200516static bool
Sven Eckelmann96412692012-06-05 22:31:30 +0200517batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200518 struct batadv_priv *bat_priv,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200519 int packet_len, unsigned long send_time,
520 bool directlink,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200521 const struct batadv_hard_iface *if_incoming,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100522 const struct batadv_hard_iface *if_outgoing,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200523 const struct batadv_forw_packet *forw_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200524{
Sven Eckelmann96412692012-06-05 22:31:30 +0200525 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200526 int aggregated_bytes = forw_packet->packet_len + packet_len;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200527 struct batadv_hard_iface *primary_if = NULL;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200528 bool res = false;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200529 unsigned long aggregation_end_time;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200530
Sven Eckelmann96412692012-06-05 22:31:30 +0200531 batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200532 aggregation_end_time = send_time;
533 aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200534
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200535 /* we can aggregate the current packet to this aggregated packet
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200536 * if:
537 *
538 * - the send time is within our MAX_AGGREGATION_MS time
539 * - the resulting packet wont be bigger than
540 * MAX_AGGREGATION_BYTES
541 */
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200542 if (time_before(send_time, forw_packet->send_time) &&
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200543 time_after_eq(aggregation_end_time, forw_packet->send_time) &&
544 (aggregated_bytes <= BATADV_MAX_AGGREGATION_BYTES)) {
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200545 /* check aggregation compatibility
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200546 * -> direct link packets are broadcasted on
547 * their interface only
548 * -> aggregate packet if the current packet is
549 * a "global" packet as well as the base
550 * packet
551 */
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200552 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200553 if (!primary_if)
554 goto out;
555
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100556 /* packet is not leaving on the same interface. */
557 if (forw_packet->if_outgoing != if_outgoing)
558 goto out;
559
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200560 /* packets without direct link flag and high TTL
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200561 * are flooded through the net
562 */
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200563 if ((!directlink) &&
Sven Eckelmann96412692012-06-05 22:31:30 +0200564 (!(batadv_ogm_packet->flags & BATADV_DIRECTLINK)) &&
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100565 (batadv_ogm_packet->ttl != 1) &&
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200566
567 /* own packets originating non-primary
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200568 * interfaces leave only that interface
569 */
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200570 ((!forw_packet->own) ||
571 (forw_packet->if_incoming == primary_if))) {
572 res = true;
573 goto out;
574 }
575
576 /* if the incoming packet is sent via this one
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200577 * interface only - we still can aggregate
578 */
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200579 if ((directlink) &&
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100580 (new_bat_ogm_packet->ttl == 1) &&
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200581 (forw_packet->if_incoming == if_incoming) &&
582
583 /* packets from direct neighbors or
584 * own secondary interface packets
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200585 * (= secondary interface packets in general)
586 */
Sven Eckelmann96412692012-06-05 22:31:30 +0200587 (batadv_ogm_packet->flags & BATADV_DIRECTLINK ||
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200588 (forw_packet->own &&
589 forw_packet->if_incoming != primary_if))) {
590 res = true;
591 goto out;
592 }
593 }
594
595out:
596 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200597 batadv_hardif_free_ref(primary_if);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200598 return res;
599}
600
Simon Wunderlich1b371d12014-01-15 21:17:54 +0100601/**
602 * batadv_iv_ogm_aggregate_new - create a new aggregated packet and add this
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100603 * packet to it.
604 * @packet_buff: pointer to the OGM
605 * @packet_len: (total) length of the OGM
606 * @send_time: timestamp (jiffies) when the packet is to be sent
607 * @direct_link: whether this OGM has direct link status
608 * @if_incoming: interface where the packet was received
609 * @if_outgoing: interface for which the retransmission should be considered
610 * @own_packet: true if it is a self-generated ogm
611 */
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,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100616 struct batadv_hard_iface *if_outgoing,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200617 int own_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200618{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200619 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
620 struct batadv_forw_packet *forw_packet_aggr;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200621 unsigned char *skb_buff;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200622 unsigned int skb_size;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200623
624 if (!atomic_inc_not_zero(&if_incoming->refcount))
625 return;
626
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100627 if (!atomic_inc_not_zero(&if_outgoing->refcount))
628 goto out_free_incoming;
629
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200630 /* own packet should always be scheduled */
631 if (!own_packet) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200632 if (!batadv_atomic_dec_not_zero(&bat_priv->batman_queue_left)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200633 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200634 "batman packet queue full\n");
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200635 goto out;
636 }
637 }
638
639 forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC);
640 if (!forw_packet_aggr) {
641 if (!own_packet)
642 atomic_inc(&bat_priv->batman_queue_left);
643 goto out;
644 }
645
646 if ((atomic_read(&bat_priv->aggregated_ogms)) &&
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200647 (packet_len < BATADV_MAX_AGGREGATION_BYTES))
Sven Eckelmann5b246572012-11-04 17:11:45 +0100648 skb_size = BATADV_MAX_AGGREGATION_BYTES;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200649 else
Sven Eckelmann5b246572012-11-04 17:11:45 +0100650 skb_size = packet_len;
651
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200652 skb_size += ETH_HLEN;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200653
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200654 forw_packet_aggr->skb = netdev_alloc_skb_ip_align(NULL, skb_size);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200655 if (!forw_packet_aggr->skb) {
656 if (!own_packet)
657 atomic_inc(&bat_priv->batman_queue_left);
658 kfree(forw_packet_aggr);
659 goto out;
660 }
Simon Wunderlichc54f38c2013-07-29 17:56:44 +0200661 forw_packet_aggr->skb->priority = TC_PRIO_CONTROL;
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200662 skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200663
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200664 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
665 forw_packet_aggr->packet_len = packet_len;
666 memcpy(skb_buff, packet_buff, packet_len);
667
668 forw_packet_aggr->own = own_packet;
669 forw_packet_aggr->if_incoming = if_incoming;
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100670 forw_packet_aggr->if_outgoing = if_outgoing;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200671 forw_packet_aggr->num_packets = 0;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200672 forw_packet_aggr->direct_link_flags = BATADV_NO_FLAGS;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200673 forw_packet_aggr->send_time = send_time;
674
675 /* save packet direct link flag status */
676 if (direct_link)
677 forw_packet_aggr->direct_link_flags |= 1;
678
679 /* add new packet to packet list */
680 spin_lock_bh(&bat_priv->forw_bat_list_lock);
681 hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list);
682 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
683
684 /* start timer for this packet */
685 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
Sven Eckelmann9455e342012-05-12 02:09:37 +0200686 batadv_send_outstanding_bat_ogm_packet);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200687 queue_delayed_work(batadv_event_workqueue,
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200688 &forw_packet_aggr->delayed_work,
689 send_time - jiffies);
690
691 return;
692out:
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100693 batadv_hardif_free_ref(if_outgoing);
694out_free_incoming:
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200695 batadv_hardif_free_ref(if_incoming);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200696}
697
698/* aggregate a new packet into the existing ogm packet */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200699static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200700 const unsigned char *packet_buff,
701 int packet_len, bool direct_link)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200702{
703 unsigned char *skb_buff;
Sven Eckelmann8de47de2012-07-08 16:32:09 +0200704 unsigned long new_direct_link_flag;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200705
706 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
707 memcpy(skb_buff, packet_buff, packet_len);
708 forw_packet_aggr->packet_len += packet_len;
709 forw_packet_aggr->num_packets++;
710
711 /* save packet direct link flag status */
Sven Eckelmann8de47de2012-07-08 16:32:09 +0200712 if (direct_link) {
713 new_direct_link_flag = BIT(forw_packet_aggr->num_packets);
714 forw_packet_aggr->direct_link_flags |= new_direct_link_flag;
715 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200716}
717
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100718/**
719 * batadv_iv_ogm_queue_add - queue up an OGM for transmission
720 * @bat_priv: the bat priv with all the soft interface information
721 * @packet_buff: pointer to the OGM
722 * @packet_len: (total) length of the OGM
723 * @if_incoming: interface where the packet was received
724 * @if_outgoing: interface for which the retransmission should be considered
725 * @own_packet: true if it is a self-generated ogm
726 * @send_time: timestamp (jiffies) when the packet is to be sent
727 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200728static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200729 unsigned char *packet_buff,
730 int packet_len,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200731 struct batadv_hard_iface *if_incoming,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100732 struct batadv_hard_iface *if_outgoing,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200733 int own_packet, unsigned long send_time)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200734{
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200735 /* _aggr -> pointer to the packet we want to aggregate with
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200736 * _pos -> pointer to the position in the queue
737 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200738 struct batadv_forw_packet *forw_packet_aggr = NULL;
739 struct batadv_forw_packet *forw_packet_pos = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200740 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200741 bool direct_link;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200742 unsigned long max_aggregation_jiffies;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200743
Sven Eckelmann96412692012-06-05 22:31:30 +0200744 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
745 direct_link = batadv_ogm_packet->flags & BATADV_DIRECTLINK ? 1 : 0;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200746 max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200747
748 /* find position for the packet in the forward queue */
749 spin_lock_bh(&bat_priv->forw_bat_list_lock);
750 /* own packets are not to be aggregated */
751 if ((atomic_read(&bat_priv->aggregated_ogms)) && (!own_packet)) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800752 hlist_for_each_entry(forw_packet_pos,
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200753 &bat_priv->forw_bat_list, list) {
Sven Eckelmann96412692012-06-05 22:31:30 +0200754 if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200755 bat_priv, packet_len,
756 send_time, direct_link,
757 if_incoming,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100758 if_outgoing,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200759 forw_packet_pos)) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200760 forw_packet_aggr = forw_packet_pos;
761 break;
762 }
763 }
764 }
765
766 /* nothing to aggregate with - either aggregation disabled or no
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200767 * suitable aggregation packet found
768 */
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200769 if (!forw_packet_aggr) {
770 /* the following section can run without the lock */
771 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
772
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200773 /* if we could not aggregate this packet with one of the others
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200774 * we hold it back for a while, so that it might be aggregated
775 * later on
776 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200777 if (!own_packet && atomic_read(&bat_priv->aggregated_ogms))
778 send_time += max_aggregation_jiffies;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200779
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200780 batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
781 send_time, direct_link,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100782 if_incoming, if_outgoing,
783 own_packet);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200784 } else {
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200785 batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
786 packet_len, direct_link);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200787 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
788 }
789}
790
Sven Eckelmann56303d32012-06-05 22:31:31 +0200791static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200792 const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +0200793 struct batadv_ogm_packet *batadv_ogm_packet,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200794 bool is_single_hop_neigh,
795 bool is_from_best_next_hop,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100796 struct batadv_hard_iface *if_incoming,
797 struct batadv_hard_iface *if_outgoing)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200798{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200799 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Marek Lindneref261572013-04-23 21:39:57 +0800800 uint16_t tvlv_len;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200801
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100802 if (batadv_ogm_packet->ttl <= 1) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200803 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200804 return;
805 }
806
Marek Lindner13b25412012-03-11 06:17:53 +0800807 if (!is_from_best_next_hop) {
808 /* Mark the forwarded packet when it is not coming from our
809 * best next hop. We still need to forward the packet for our
810 * neighbor link quality detection to work in case the packet
811 * originated from a single hop neighbor. Otherwise we can
812 * simply drop the ogm.
813 */
814 if (is_single_hop_neigh)
Sven Eckelmann96412692012-06-05 22:31:30 +0200815 batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP;
Marek Lindner13b25412012-03-11 06:17:53 +0800816 else
817 return;
818 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200819
Marek Lindneref261572013-04-23 21:39:57 +0800820 tvlv_len = ntohs(batadv_ogm_packet->tvlv_len);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200821
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100822 batadv_ogm_packet->ttl--;
Sven Eckelmann96412692012-06-05 22:31:30 +0200823 memcpy(batadv_ogm_packet->prev_sender, ethhdr->h_source, ETH_ALEN);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200824
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200825 /* apply hop penalty */
Sven Eckelmann96412692012-06-05 22:31:30 +0200826 batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200827 bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200828
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200829 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200830 "Forwarding packet: tq: %i, ttl: %i\n",
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100831 batadv_ogm_packet->tq, batadv_ogm_packet->ttl);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200832
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200833 /* switch of primaries first hop flag when forwarding */
Sven Eckelmann96412692012-06-05 22:31:30 +0200834 batadv_ogm_packet->flags &= ~BATADV_PRIMARIES_FIRST_HOP;
Marek Lindner75cd33f2012-03-01 15:35:16 +0800835 if (is_single_hop_neigh)
Sven Eckelmann96412692012-06-05 22:31:30 +0200836 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200837 else
Sven Eckelmann96412692012-06-05 22:31:30 +0200838 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200839
Sven Eckelmann96412692012-06-05 22:31:30 +0200840 batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
Marek Lindneref261572013-04-23 21:39:57 +0800841 BATADV_OGM_HLEN + tvlv_len,
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100842 if_incoming, if_outgoing, 0,
843 batadv_iv_ogm_fwd_send_time());
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200844}
845
Antonio Quartullid9896612013-04-17 17:44:43 +0200846/**
847 * batadv_iv_ogm_slide_own_bcast_window - bitshift own OGM broadcast windows for
848 * the given interface
849 * @hard_iface: the interface for which the windows have to be shifted
850 */
851static void
852batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
853{
854 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
855 struct batadv_hashtable *hash = bat_priv->orig_hash;
856 struct hlist_head *head;
857 struct batadv_orig_node *orig_node;
858 unsigned long *word;
859 uint32_t i;
860 size_t word_index;
861 uint8_t *w;
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200862 int if_num;
Antonio Quartullid9896612013-04-17 17:44:43 +0200863
864 for (i = 0; i < hash->size; i++) {
865 head = &hash->table[i];
866
867 rcu_read_lock();
868 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200869 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Antonio Quartullid9896612013-04-17 17:44:43 +0200870 word_index = hard_iface->if_num * BATADV_NUM_WORDS;
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200871 word = &(orig_node->bat_iv.bcast_own[word_index]);
Antonio Quartullid9896612013-04-17 17:44:43 +0200872
873 batadv_bit_get_packet(bat_priv, word, 1, 0);
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200874 if_num = hard_iface->if_num;
875 w = &orig_node->bat_iv.bcast_own_sum[if_num];
Antonio Quartullid9896612013-04-17 17:44:43 +0200876 *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200877 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Antonio Quartullid9896612013-04-17 17:44:43 +0200878 }
879 rcu_read_unlock();
880 }
881}
882
Sven Eckelmann56303d32012-06-05 22:31:31 +0200883static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200884{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200885 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindner14511512012-08-02 17:20:26 +0200886 unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff;
Sven Eckelmann96412692012-06-05 22:31:30 +0200887 struct batadv_ogm_packet *batadv_ogm_packet;
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100888 struct batadv_hard_iface *primary_if, *tmp_hard_iface;
Marek Lindner14511512012-08-02 17:20:26 +0200889 int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len;
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200890 uint32_t seqno;
Marek Lindneref261572013-04-23 21:39:57 +0800891 uint16_t tvlv_len = 0;
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100892 unsigned long send_time;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200893
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200894 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200895
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800896 if (hard_iface == primary_if) {
897 /* tt changes have to be committed before the tvlv data is
898 * appended as it may alter the tt tvlv container
899 */
900 batadv_tt_local_commit_changes(bat_priv);
Marek Lindneref261572013-04-23 21:39:57 +0800901 tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff,
902 ogm_buff_len,
903 BATADV_OGM_HLEN);
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800904 }
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800905
Marek Lindner14511512012-08-02 17:20:26 +0200906 batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff);
Marek Lindneref261572013-04-23 21:39:57 +0800907 batadv_ogm_packet->tvlv_len = htons(tvlv_len);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200908
909 /* change sequence number to network order */
Marek Lindner14511512012-08-02 17:20:26 +0200910 seqno = (uint32_t)atomic_read(&hard_iface->bat_iv.ogm_seqno);
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200911 batadv_ogm_packet->seqno = htonl(seqno);
Marek Lindner14511512012-08-02 17:20:26 +0200912 atomic_inc(&hard_iface->bat_iv.ogm_seqno);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200913
Antonio Quartullid9896612013-04-17 17:44:43 +0200914 batadv_iv_ogm_slide_own_bcast_window(hard_iface);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200915
Simon Wunderlichef0a9372013-11-13 19:14:49 +0100916 send_time = batadv_iv_ogm_emit_send_time(bat_priv);
917
918 if (hard_iface != primary_if) {
919 /* OGMs from secondary interfaces are only scheduled on their
920 * respective interfaces.
921 */
922 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff, *ogm_buff_len,
923 hard_iface, hard_iface, 1, send_time);
924 goto out;
925 }
926
927 /* OGMs from primary interfaces are scheduled on all
928 * interfaces.
929 */
930 rcu_read_lock();
931 list_for_each_entry_rcu(tmp_hard_iface, &batadv_hardif_list, list) {
932 if (tmp_hard_iface->soft_iface != hard_iface->soft_iface)
933 continue;
934 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff,
935 *ogm_buff_len, hard_iface,
936 tmp_hard_iface, 1, send_time);
937 }
938 rcu_read_unlock();
939
940out:
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200941 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200942 batadv_hardif_free_ref(primary_if);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200943}
944
Simon Wunderlich89652332013-11-13 19:14:46 +0100945/**
946 * batadv_iv_ogm_orig_update - use OGM to update corresponding data in an
947 * originator
948 * @bat_priv: the bat priv with all the soft interface information
949 * @orig_node: the orig node who originally emitted the ogm packet
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100950 * @orig_ifinfo: ifinfo for the outgoing interface of the orig_node
Simon Wunderlich89652332013-11-13 19:14:46 +0100951 * @ethhdr: Ethernet header of the OGM
952 * @batadv_ogm_packet: the ogm packet
953 * @if_incoming: interface where the packet was received
954 * @if_outgoing: interface for which the retransmission should be considered
Simon Wunderlich89652332013-11-13 19:14:46 +0100955 * @dup_status: the duplicate status of this ogm packet.
956 */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200957static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200958batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
959 struct batadv_orig_node *orig_node,
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100960 struct batadv_orig_ifinfo *orig_ifinfo,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200961 const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +0200962 const struct batadv_ogm_packet *batadv_ogm_packet,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200963 struct batadv_hard_iface *if_incoming,
Simon Wunderlich89652332013-11-13 19:14:46 +0100964 struct batadv_hard_iface *if_outgoing,
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +0200965 enum batadv_dup_status dup_status)
Marek Lindnerfc957272011-07-30 12:04:12 +0200966{
Simon Wunderlich89652332013-11-13 19:14:46 +0100967 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
968 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200969 struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
970 struct batadv_neigh_node *router = NULL;
971 struct batadv_orig_node *orig_node_tmp;
Linus LĂĽssing7caf69f2012-09-18 03:01:08 +0200972 int if_num;
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200973 uint8_t sum_orig, sum_neigh;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200974 uint8_t *neigh_addr;
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200975 uint8_t tq_avg;
Marek Lindnerfc957272011-07-30 12:04:12 +0200976
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200977 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200978 "update_originator(): Searching and updating originator entry of received packet\n");
Marek Lindnerfc957272011-07-30 12:04:12 +0200979
980 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800981 hlist_for_each_entry_rcu(tmp_neigh_node,
Marek Lindnerfc957272011-07-30 12:04:12 +0200982 &orig_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200983 neigh_addr = tmp_neigh_node->addr;
984 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
985 tmp_neigh_node->if_incoming == if_incoming &&
986 atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
Antonio Quartulli38dc40e2013-03-30 17:22:00 +0100987 if (WARN(neigh_node, "too many matching neigh_nodes"))
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200988 batadv_neigh_node_free_ref(neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +0200989 neigh_node = tmp_neigh_node;
990 continue;
991 }
992
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +0200993 if (dup_status != BATADV_NO_DUP)
Marek Lindnerfc957272011-07-30 12:04:12 +0200994 continue;
995
Simon Wunderlich89652332013-11-13 19:14:46 +0100996 /* only update the entry for this outgoing interface */
997 neigh_ifinfo = batadv_neigh_ifinfo_get(tmp_neigh_node,
998 if_outgoing);
999 if (!neigh_ifinfo)
1000 continue;
1001
1002 spin_lock_bh(&tmp_neigh_node->ifinfo_lock);
1003 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
1004 &neigh_ifinfo->bat_iv.tq_index, 0);
1005 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
1006 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
1007 spin_unlock_bh(&tmp_neigh_node->ifinfo_lock);
1008
1009 batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
1010 neigh_ifinfo = NULL;
Marek Lindnerfc957272011-07-30 12:04:12 +02001011 }
1012
1013 if (!neigh_node) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001014 struct batadv_orig_node *orig_tmp;
Marek Lindnerfc957272011-07-30 12:04:12 +02001015
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001016 orig_tmp = batadv_iv_ogm_orig_get(bat_priv, ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001017 if (!orig_tmp)
1018 goto unlock;
1019
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001020 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1021 ethhdr->h_source,
Antonio Quartulli863dd7a2013-03-25 13:49:46 +01001022 orig_node, orig_tmp);
Marek Lindnerfc957272011-07-30 12:04:12 +02001023
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001024 batadv_orig_node_free_ref(orig_tmp);
Marek Lindnerfc957272011-07-30 12:04:12 +02001025 if (!neigh_node)
1026 goto unlock;
1027 } else
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001028 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001029 "Updating existing last-hop neighbor of originator\n");
Marek Lindnerfc957272011-07-30 12:04:12 +02001030
1031 rcu_read_unlock();
Simon Wunderlich89652332013-11-13 19:14:46 +01001032 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
1033 if (!neigh_ifinfo)
1034 goto out;
Marek Lindnerfc957272011-07-30 12:04:12 +02001035
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001036 neigh_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +02001037
Simon Wunderlich89652332013-11-13 19:14:46 +01001038 spin_lock_bh(&neigh_node->ifinfo_lock);
1039 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
1040 &neigh_ifinfo->bat_iv.tq_index,
Sven Eckelmann96412692012-06-05 22:31:30 +02001041 batadv_ogm_packet->tq);
Simon Wunderlich89652332013-11-13 19:14:46 +01001042 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
1043 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
1044 spin_unlock_bh(&neigh_node->ifinfo_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001045
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001046 if (dup_status == BATADV_NO_DUP) {
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001047 orig_ifinfo->last_ttl = batadv_ogm_packet->ttl;
Simon Wunderlich89652332013-11-13 19:14:46 +01001048 neigh_ifinfo->last_ttl = batadv_ogm_packet->ttl;
Marek Lindnerfc957272011-07-30 12:04:12 +02001049 }
1050
Marek Lindnerfc957272011-07-30 12:04:12 +02001051 /* if this neighbor already is our next hop there is nothing
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001052 * to change
1053 */
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001054 router = batadv_orig_router_get(orig_node, if_outgoing);
Marek Lindnerfc957272011-07-30 12:04:12 +02001055 if (router == neigh_node)
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001056 goto out;
Marek Lindnerfc957272011-07-30 12:04:12 +02001057
Simon Wunderlich89652332013-11-13 19:14:46 +01001058 if (router) {
1059 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1060 if (!router_ifinfo)
1061 goto out;
1062
1063 /* if this neighbor does not offer a better TQ we won't
1064 * consider it
1065 */
1066 if (router_ifinfo->bat_iv.tq_avg > neigh_ifinfo->bat_iv.tq_avg)
1067 goto out;
1068 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001069
1070 /* if the TQ is the same and the link not more symmetric we
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001071 * won't consider it either
1072 */
Simon Wunderlich89652332013-11-13 19:14:46 +01001073 if (router_ifinfo &&
1074 (neigh_ifinfo->bat_iv.tq_avg == router_ifinfo->bat_iv.tq_avg)) {
Marek Lindnerfc957272011-07-30 12:04:12 +02001075 orig_node_tmp = router->orig_node;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001076 spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
Linus LĂĽssing7caf69f2012-09-18 03:01:08 +02001077 if_num = router->if_incoming->if_num;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001078 sum_orig = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
1079 spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001080
1081 orig_node_tmp = neigh_node->orig_node;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001082 spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
Linus LĂĽssing7caf69f2012-09-18 03:01:08 +02001083 if_num = neigh_node->if_incoming->if_num;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001084 sum_neigh = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
1085 spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001086
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001087 if (sum_orig >= sum_neigh)
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001088 goto out;
Marek Lindnerfc957272011-07-30 12:04:12 +02001089 }
1090
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001091 batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001092 goto out;
1093
1094unlock:
1095 rcu_read_unlock();
1096out:
1097 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001098 batadv_neigh_node_free_ref(neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001099 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001100 batadv_neigh_node_free_ref(router);
Simon Wunderlich89652332013-11-13 19:14:46 +01001101 if (neigh_ifinfo)
1102 batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
1103 if (router_ifinfo)
1104 batadv_neigh_ifinfo_free_ref(router_ifinfo);
Marek Lindnerfc957272011-07-30 12:04:12 +02001105}
1106
Simon Wunderlich89652332013-11-13 19:14:46 +01001107/**
1108 * batadv_iv_ogm_calc_tq - calculate tq for current received ogm packet
1109 * @orig_node: the orig node who originally emitted the ogm packet
1110 * @orig_neigh_node: the orig node struct of the neighbor who sent the packet
1111 * @batadv_ogm_packet: the ogm packet
1112 * @if_incoming: interface where the packet was received
1113 * @if_outgoing: interface for which the retransmission should be considered
1114 *
1115 * Returns 1 if the link can be considered bidirectional, 0 otherwise
1116 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001117static int batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
1118 struct batadv_orig_node *orig_neigh_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02001119 struct batadv_ogm_packet *batadv_ogm_packet,
Simon Wunderlich89652332013-11-13 19:14:46 +01001120 struct batadv_hard_iface *if_incoming,
1121 struct batadv_hard_iface *if_outgoing)
Marek Lindnerfc957272011-07-30 12:04:12 +02001122{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001123 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1124 struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
Simon Wunderlich89652332013-11-13 19:14:46 +01001125 struct batadv_neigh_ifinfo *neigh_ifinfo;
Marek Lindnerfc957272011-07-30 12:04:12 +02001126 uint8_t total_count;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001127 uint8_t orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
1128 unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001129 int tq_asym_penalty, inv_asym_penalty, if_num, ret = 0;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001130 unsigned int combined_tq;
Simon Wunderlichc0398762013-11-13 19:14:48 +01001131 int tq_iface_penalty;
Marek Lindnerfc957272011-07-30 12:04:12 +02001132
1133 /* find corresponding one hop neighbor */
1134 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001135 hlist_for_each_entry_rcu(tmp_neigh_node,
Marek Lindnerfc957272011-07-30 12:04:12 +02001136 &orig_neigh_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001137 if (!batadv_compare_eth(tmp_neigh_node->addr,
1138 orig_neigh_node->orig))
Marek Lindnerfc957272011-07-30 12:04:12 +02001139 continue;
1140
1141 if (tmp_neigh_node->if_incoming != if_incoming)
1142 continue;
1143
1144 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
1145 continue;
1146
1147 neigh_node = tmp_neigh_node;
1148 break;
1149 }
1150 rcu_read_unlock();
1151
1152 if (!neigh_node)
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001153 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1154 orig_neigh_node->orig,
1155 orig_neigh_node,
Antonio Quartulli863dd7a2013-03-25 13:49:46 +01001156 orig_neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001157
1158 if (!neigh_node)
1159 goto out;
1160
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001161 /* if orig_node is direct neighbor update neigh_node last_seen */
Marek Lindnerfc957272011-07-30 12:04:12 +02001162 if (orig_node == orig_neigh_node)
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001163 neigh_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +02001164
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001165 orig_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +02001166
1167 /* find packet count of corresponding one hop neighbor */
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001168 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1169 if_num = if_incoming->if_num;
1170 orig_eq_count = orig_neigh_node->bat_iv.bcast_own_sum[if_num];
Simon Wunderlich89652332013-11-13 19:14:46 +01001171 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
1172 if (neigh_ifinfo) {
1173 neigh_rq_count = neigh_ifinfo->bat_iv.real_packet_count;
1174 batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
1175 } else {
1176 neigh_rq_count = 0;
1177 }
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001178 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001179
1180 /* pay attention to not get a value bigger than 100 % */
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001181 if (orig_eq_count > neigh_rq_count)
1182 total_count = neigh_rq_count;
1183 else
1184 total_count = orig_eq_count;
Marek Lindnerfc957272011-07-30 12:04:12 +02001185
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001186 /* if we have too few packets (too less data) we set tq_own to zero
1187 * if we receive too few packets it is not considered bidirectional
1188 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001189 if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
1190 neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
Marek Lindnerfc957272011-07-30 12:04:12 +02001191 tq_own = 0;
1192 else
1193 /* neigh_node->real_packet_count is never zero as we
1194 * only purge old information when getting new
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001195 * information
1196 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001197 tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count;
Marek Lindnerfc957272011-07-30 12:04:12 +02001198
Sven Eckelmann21a12362012-03-07 09:07:46 +01001199 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
Marek Lindnerfc957272011-07-30 12:04:12 +02001200 * affect the nearly-symmetric links only a little, but
1201 * punishes asymmetric links more. This will give a value
1202 * between 0 and TQ_MAX_VALUE
1203 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001204 neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
1205 neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
1206 neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
1207 BATADV_TQ_LOCAL_WINDOW_SIZE *
1208 BATADV_TQ_LOCAL_WINDOW_SIZE;
1209 inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
1210 inv_asym_penalty /= neigh_rq_max_cube;
1211 tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;
Marek Lindnerfc957272011-07-30 12:04:12 +02001212
Simon Wunderlichc0398762013-11-13 19:14:48 +01001213 /* penalize if the OGM is forwarded on the same interface. WiFi
1214 * interfaces and other half duplex devices suffer from throughput
1215 * drops as they can't send and receive at the same time.
1216 */
1217 tq_iface_penalty = BATADV_TQ_MAX_VALUE;
1218 if (if_outgoing && (if_incoming == if_outgoing) &&
1219 batadv_is_wifi_netdev(if_outgoing->net_dev))
1220 tq_iface_penalty = batadv_hop_penalty(BATADV_TQ_MAX_VALUE,
1221 bat_priv);
1222
1223 combined_tq = batadv_ogm_packet->tq *
1224 tq_own *
1225 tq_asym_penalty *
1226 tq_iface_penalty;
1227 combined_tq /= BATADV_TQ_MAX_VALUE *
1228 BATADV_TQ_MAX_VALUE *
1229 BATADV_TQ_MAX_VALUE;
Sven Eckelmann96412692012-06-05 22:31:30 +02001230 batadv_ogm_packet->tq = combined_tq;
Marek Lindnerfc957272011-07-30 12:04:12 +02001231
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001232 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Simon Wunderlichc0398762013-11-13 19:14:48 +01001233 "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 +02001234 orig_node->orig, orig_neigh_node->orig, total_count,
Simon Wunderlichc0398762013-11-13 19:14:48 +01001235 neigh_rq_count, tq_own, tq_asym_penalty, tq_iface_penalty,
1236 batadv_ogm_packet->tq, if_incoming->net_dev->name,
1237 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT");
Marek Lindnerfc957272011-07-30 12:04:12 +02001238
1239 /* if link has the minimum required transmission quality
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001240 * consider it bidirectional
1241 */
Sven Eckelmann96412692012-06-05 22:31:30 +02001242 if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
Marek Lindnerfc957272011-07-30 12:04:12 +02001243 ret = 1;
1244
1245out:
1246 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001247 batadv_neigh_node_free_ref(neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001248 return ret;
1249}
1250
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001251/**
1252 * batadv_iv_ogm_update_seqnos - process a batman packet for all interfaces,
1253 * adjust the sequence number and find out whether it is a duplicate
1254 * @ethhdr: ethernet header of the packet
1255 * @batadv_ogm_packet: OGM packet to be considered
1256 * @if_incoming: interface on which the OGM packet was received
Simon Wunderlich89652332013-11-13 19:14:46 +01001257 * @if_outgoing: interface for which the retransmission should be considered
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001258 *
1259 * Returns duplicate status as enum batadv_dup_status
Marek Lindnerfc957272011-07-30 12:04:12 +02001260 */
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001261static enum batadv_dup_status
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001262batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +02001263 const struct batadv_ogm_packet *batadv_ogm_packet,
Simon Wunderlich89652332013-11-13 19:14:46 +01001264 const struct batadv_hard_iface *if_incoming,
1265 struct batadv_hard_iface *if_outgoing)
Marek Lindnerfc957272011-07-30 12:04:12 +02001266{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001267 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1268 struct batadv_orig_node *orig_node;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001269 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
Simon Wunderlich89652332013-11-13 19:14:46 +01001270 struct batadv_neigh_node *neigh_node;
1271 struct batadv_neigh_ifinfo *neigh_ifinfo;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001272 int is_dup;
Marek Lindnerfc957272011-07-30 12:04:12 +02001273 int32_t seq_diff;
1274 int need_update = 0;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001275 int set_mark;
1276 enum batadv_dup_status ret = BATADV_NO_DUP;
Sven Eckelmann96412692012-06-05 22:31:30 +02001277 uint32_t seqno = ntohl(batadv_ogm_packet->seqno);
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001278 uint8_t *neigh_addr;
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001279 uint8_t packet_count;
Antonio Quartulli0538f752013-09-02 12:15:01 +02001280 unsigned long *bitmap;
Marek Lindnerfc957272011-07-30 12:04:12 +02001281
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001282 orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
Marek Lindnerfc957272011-07-30 12:04:12 +02001283 if (!orig_node)
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001284 return BATADV_NO_DUP;
Marek Lindnerfc957272011-07-30 12:04:12 +02001285
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001286 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1287 if (WARN_ON(!orig_ifinfo)) {
1288 batadv_orig_node_free_ref(orig_node);
1289 return 0;
1290 }
1291
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001292 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001293 seq_diff = seqno - orig_ifinfo->last_real_seqno;
Marek Lindnerfc957272011-07-30 12:04:12 +02001294
1295 /* signalize caller that the packet is to be dropped. */
Antonio Quartulli1e5cc262012-02-26 15:39:42 +01001296 if (!hlist_empty(&orig_node->neigh_list) &&
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001297 batadv_window_protected(bat_priv, seq_diff,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001298 &orig_ifinfo->batman_seqno_reset)) {
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001299 ret = BATADV_PROTECTED;
Marek Lindnerfc957272011-07-30 12:04:12 +02001300 goto out;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001301 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001302
1303 rcu_read_lock();
Simon Wunderlich89652332013-11-13 19:14:46 +01001304 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1305 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node,
1306 if_outgoing);
1307 if (!neigh_ifinfo)
1308 continue;
1309
1310 neigh_addr = neigh_node->addr;
1311 is_dup = batadv_test_bit(neigh_ifinfo->bat_iv.real_bits,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001312 orig_ifinfo->last_real_seqno,
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001313 seqno);
1314
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001315 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
Simon Wunderlich89652332013-11-13 19:14:46 +01001316 neigh_node->if_incoming == if_incoming) {
Marek Lindnerfc957272011-07-30 12:04:12 +02001317 set_mark = 1;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001318 if (is_dup)
1319 ret = BATADV_NEIGH_DUP;
1320 } else {
Marek Lindnerfc957272011-07-30 12:04:12 +02001321 set_mark = 0;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001322 if (is_dup && (ret != BATADV_NEIGH_DUP))
1323 ret = BATADV_ORIG_DUP;
1324 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001325
1326 /* if the window moved, set the update flag. */
Simon Wunderlich89652332013-11-13 19:14:46 +01001327 bitmap = neigh_ifinfo->bat_iv.real_bits;
Antonio Quartulli0538f752013-09-02 12:15:01 +02001328 need_update |= batadv_bit_get_packet(bat_priv, bitmap,
Sven Eckelmann0f5f9322012-05-12 02:09:25 +02001329 seq_diff, set_mark);
Marek Lindnerfc957272011-07-30 12:04:12 +02001330
Simon Wunderlich89652332013-11-13 19:14:46 +01001331 packet_count = bitmap_weight(bitmap,
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001332 BATADV_TQ_LOCAL_WINDOW_SIZE);
Simon Wunderlich89652332013-11-13 19:14:46 +01001333 neigh_ifinfo->bat_iv.real_packet_count = packet_count;
1334 batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
Marek Lindnerfc957272011-07-30 12:04:12 +02001335 }
1336 rcu_read_unlock();
1337
1338 if (need_update) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001339 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001340 "%s updating last_seqno: old %u, new %u\n",
1341 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT",
1342 orig_ifinfo->last_real_seqno, seqno);
1343 orig_ifinfo->last_real_seqno = seqno;
Marek Lindnerfc957272011-07-30 12:04:12 +02001344 }
1345
Marek Lindnerfc957272011-07-30 12:04:12 +02001346out:
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001347 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001348 batadv_orig_node_free_ref(orig_node);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001349 if (orig_ifinfo)
1350 batadv_orig_ifinfo_free_ref(orig_ifinfo);
Marek Lindnerfc957272011-07-30 12:04:12 +02001351 return ret;
1352}
1353
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001354
1355/**
1356 * batadv_iv_ogm_process_per_outif - process a batman iv OGM for an outgoing if
1357 * @skb: the skb containing the OGM
1358 * @orig_node: the (cached) orig node for the originator of this OGM
1359 * @if_incoming: the interface where this packet was received
1360 * @if_outgoing: the interface for which the packet should be considered
1361 */
1362static void
1363batadv_iv_ogm_process_per_outif(const struct sk_buff *skb, int ogm_offset,
1364 struct batadv_orig_node *orig_node,
1365 struct batadv_hard_iface *if_incoming,
1366 struct batadv_hard_iface *if_outgoing)
1367{
1368 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1369 struct batadv_neigh_node *router = NULL, *router_router = NULL;
1370 struct batadv_orig_node *orig_neigh_node;
1371 struct batadv_orig_ifinfo *orig_ifinfo;
1372 struct batadv_neigh_node *orig_neigh_router = NULL;
1373 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
1374 struct batadv_ogm_packet *ogm_packet;
1375 enum batadv_dup_status dup_status;
1376 bool is_from_best_next_hop = false;
1377 bool is_single_hop_neigh = false;
1378 bool sameseq, similar_ttl;
1379 struct sk_buff *skb_priv;
1380 struct ethhdr *ethhdr;
1381 uint8_t *prev_sender;
1382 int is_bidirect;
1383
1384 /* create a private copy of the skb, as some functions change tq value
1385 * and/or flags.
1386 */
1387 skb_priv = skb_copy(skb, GFP_ATOMIC);
1388 if (!skb_priv)
1389 return;
1390
1391 ethhdr = eth_hdr(skb_priv);
1392 ogm_packet = (struct batadv_ogm_packet *)(skb_priv->data + ogm_offset);
1393
1394 dup_status = batadv_iv_ogm_update_seqnos(ethhdr, ogm_packet,
1395 if_incoming, if_outgoing);
1396 if (batadv_compare_eth(ethhdr->h_source, ogm_packet->orig))
1397 is_single_hop_neigh = true;
1398
1399 if (dup_status == BATADV_PROTECTED) {
1400 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1401 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1402 ethhdr->h_source);
1403 goto out;
1404 }
1405
1406 if (ogm_packet->tq == 0) {
1407 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1408 "Drop packet: originator packet with tq equal 0\n");
1409 goto out;
1410 }
1411
1412 router = batadv_orig_router_get(orig_node, if_outgoing);
1413 if (router) {
1414 router_router = batadv_orig_router_get(router->orig_node,
1415 if_outgoing);
1416 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1417 }
1418
1419 if ((router_ifinfo && router_ifinfo->bat_iv.tq_avg != 0) &&
1420 (batadv_compare_eth(router->addr, ethhdr->h_source)))
1421 is_from_best_next_hop = true;
1422
1423 prev_sender = ogm_packet->prev_sender;
1424 /* avoid temporary routing loops */
1425 if (router && router_router &&
1426 (batadv_compare_eth(router->addr, prev_sender)) &&
1427 !(batadv_compare_eth(ogm_packet->orig, prev_sender)) &&
1428 (batadv_compare_eth(router->addr, router_router->addr))) {
1429 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1430 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1431 ethhdr->h_source);
1432 goto out;
1433 }
1434
1435 if (if_outgoing == BATADV_IF_DEFAULT)
1436 batadv_tvlv_ogm_receive(bat_priv, ogm_packet, orig_node);
1437
1438 /* if sender is a direct neighbor the sender mac equals
1439 * originator mac
1440 */
1441 if (is_single_hop_neigh)
1442 orig_neigh_node = orig_node;
1443 else
1444 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1445 ethhdr->h_source);
1446
1447 if (!orig_neigh_node)
1448 goto out;
1449
1450 /* Update nc_nodes of the originator */
1451 batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node,
1452 ogm_packet, is_single_hop_neigh);
1453
1454 orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
1455 if_outgoing);
1456
1457 /* drop packet if sender is not a direct neighbor and if we
1458 * don't route towards it
1459 */
1460 if (!is_single_hop_neigh && (!orig_neigh_router)) {
1461 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1462 "Drop packet: OGM via unknown neighbor!\n");
1463 goto out_neigh;
1464 }
1465
1466 is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
1467 ogm_packet, if_incoming,
1468 if_outgoing);
1469
1470 /* update ranking if it is not a duplicate or has the same
1471 * seqno and similar ttl as the non-duplicate
1472 */
1473 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1474 if (!orig_ifinfo)
1475 goto out_neigh;
1476
1477 sameseq = orig_ifinfo->last_real_seqno == ntohl(ogm_packet->seqno);
1478 similar_ttl = (orig_ifinfo->last_ttl - 3) <= ogm_packet->ttl;
1479
1480 if (is_bidirect && ((dup_status == BATADV_NO_DUP) ||
1481 (sameseq && similar_ttl))) {
1482 batadv_iv_ogm_orig_update(bat_priv, orig_node,
1483 orig_ifinfo, ethhdr,
1484 ogm_packet, if_incoming,
1485 if_outgoing, dup_status);
1486 }
1487 batadv_orig_ifinfo_free_ref(orig_ifinfo);
1488
Simon Wunderlichef0a9372013-11-13 19:14:49 +01001489 /* only forward for specific interface, not for the default one. */
1490 if (if_outgoing == BATADV_IF_DEFAULT)
1491 goto out_neigh;
1492
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001493 /* is single hop (direct) neighbor */
1494 if (is_single_hop_neigh) {
1495 /* OGMs from secondary interfaces should only scheduled once
1496 * per interface where it has been received, not multiple times
1497 */
1498 if ((ogm_packet->ttl <= 2) &&
1499 (if_incoming != if_outgoing)) {
1500 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1501 "Drop packet: OGM from secondary interface and wrong outgoing interface\n");
1502 goto out_neigh;
1503 }
1504 /* mark direct link on incoming interface */
1505 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1506 is_single_hop_neigh,
Simon Wunderlichef0a9372013-11-13 19:14:49 +01001507 is_from_best_next_hop, if_incoming,
1508 if_outgoing);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001509
1510 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1511 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
1512 goto out_neigh;
1513 }
1514
1515 /* multihop originator */
1516 if (!is_bidirect) {
1517 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1518 "Drop packet: not received via bidirectional link\n");
1519 goto out_neigh;
1520 }
1521
1522 if (dup_status == BATADV_NEIGH_DUP) {
1523 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1524 "Drop packet: duplicate packet received\n");
1525 goto out_neigh;
1526 }
1527
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001528 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1529 "Forwarding packet: rebroadcast originator packet\n");
1530 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1531 is_single_hop_neigh, is_from_best_next_hop,
Simon Wunderlichef0a9372013-11-13 19:14:49 +01001532 if_incoming, if_outgoing);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001533
1534out_neigh:
1535 if ((orig_neigh_node) && (!is_single_hop_neigh))
1536 batadv_orig_node_free_ref(orig_neigh_node);
1537out:
1538 if (router)
1539 batadv_neigh_node_free_ref(router);
1540 if (router_router)
1541 batadv_neigh_node_free_ref(router_router);
1542 if (orig_neigh_router)
1543 batadv_neigh_node_free_ref(orig_neigh_router);
1544
1545 kfree_skb(skb_priv);
1546}
1547
1548/**
1549 * batadv_iv_ogm_process - process an incoming batman iv OGM
1550 * @skb: the skb containing the OGM
1551 * @ogm_offset: offset to the OGM which should be processed (for aggregates)
1552 * @if_incoming: the interface where this packet was receved
1553 */
1554static void batadv_iv_ogm_process(const struct sk_buff *skb, int ogm_offset,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001555 struct batadv_hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +02001556{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001557 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001558 struct batadv_orig_node *orig_neigh_node, *orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001559 struct batadv_hard_iface *hard_iface;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001560 struct batadv_ogm_packet *ogm_packet;
Marek Lindnerfc957272011-07-30 12:04:12 +02001561 uint32_t if_incoming_seqno;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001562 bool has_directlink_flag;
1563 struct ethhdr *ethhdr;
1564 bool is_my_oldorig = false;
1565 bool is_my_addr = false;
1566 bool is_my_orig = false;
1567
1568 ogm_packet = (struct batadv_ogm_packet *)(skb->data + ogm_offset);
1569 ethhdr = eth_hdr(skb);
Marek Lindnerfc957272011-07-30 12:04:12 +02001570
1571 /* Silently drop when the batman packet is actually not a
1572 * correct packet.
1573 *
1574 * This might happen if a packet is padded (e.g. Ethernet has a
1575 * minimum frame length of 64 byte) and the aggregation interprets
1576 * it as an additional length.
1577 *
1578 * TODO: A more sane solution would be to have a bit in the
Sven Eckelmann96412692012-06-05 22:31:30 +02001579 * batadv_ogm_packet to detect whether the packet is the last
Marek Lindnerfc957272011-07-30 12:04:12 +02001580 * packet in an aggregation. Here we expect that the padding
1581 * is always zero (or not 0x01)
1582 */
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001583 if (ogm_packet->packet_type != BATADV_IV_OGM)
Marek Lindnerfc957272011-07-30 12:04:12 +02001584 return;
1585
1586 /* could be changed by schedule_own_packet() */
Marek Lindner14511512012-08-02 17:20:26 +02001587 if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno);
Marek Lindnerfc957272011-07-30 12:04:12 +02001588
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001589 if (ogm_packet->flags & BATADV_DIRECTLINK)
1590 has_directlink_flag = true;
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001591 else
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001592 has_directlink_flag = false;
Marek Lindnerfc957272011-07-30 12:04:12 +02001593
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001594 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001595 "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 +02001596 ethhdr->h_source, if_incoming->net_dev->name,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001597 if_incoming->net_dev->dev_addr, ogm_packet->orig,
1598 ogm_packet->prev_sender, ntohl(ogm_packet->seqno),
1599 ogm_packet->tq, ogm_packet->ttl,
1600 ogm_packet->version, has_directlink_flag);
Marek Lindnerfc957272011-07-30 12:04:12 +02001601
1602 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001603 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001604 if (hard_iface->if_status != BATADV_IF_ACTIVE)
Marek Lindnerfc957272011-07-30 12:04:12 +02001605 continue;
1606
1607 if (hard_iface->soft_iface != if_incoming->soft_iface)
1608 continue;
1609
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001610 if (batadv_compare_eth(ethhdr->h_source,
1611 hard_iface->net_dev->dev_addr))
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001612 is_my_addr = true;
Marek Lindnerfc957272011-07-30 12:04:12 +02001613
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001614 if (batadv_compare_eth(ogm_packet->orig,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001615 hard_iface->net_dev->dev_addr))
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001616 is_my_orig = true;
Marek Lindnerfc957272011-07-30 12:04:12 +02001617
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001618 if (batadv_compare_eth(ogm_packet->prev_sender,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001619 hard_iface->net_dev->dev_addr))
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001620 is_my_oldorig = true;
Marek Lindnerfc957272011-07-30 12:04:12 +02001621 }
1622 rcu_read_unlock();
1623
Marek Lindnerfc957272011-07-30 12:04:12 +02001624 if (is_my_addr) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001625 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001626 "Drop packet: received my own broadcast (sender: %pM)\n",
1627 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001628 return;
1629 }
1630
Marek Lindnerfc957272011-07-30 12:04:12 +02001631 if (is_my_orig) {
1632 unsigned long *word;
1633 int offset;
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001634 int32_t bit_pos;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001635 int16_t if_num;
1636 uint8_t *weight;
Marek Lindnerfc957272011-07-30 12:04:12 +02001637
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001638 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1639 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001640 if (!orig_neigh_node)
1641 return;
1642
1643 /* neighbor has to indicate direct link and it has to
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001644 * come via the corresponding interface
1645 * save packet seqno for bidirectional check
1646 */
Marek Lindnerfc957272011-07-30 12:04:12 +02001647 if (has_directlink_flag &&
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001648 batadv_compare_eth(if_incoming->net_dev->dev_addr,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001649 ogm_packet->orig)) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001650 if_num = if_incoming->if_num;
1651 offset = if_num * BATADV_NUM_WORDS;
Marek Lindnerfc957272011-07-30 12:04:12 +02001652
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001653 spin_lock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
1654 word = &(orig_neigh_node->bat_iv.bcast_own[offset]);
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001655 bit_pos = if_incoming_seqno - 2;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001656 bit_pos -= ntohl(ogm_packet->seqno);
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001657 batadv_set_bit(word, bit_pos);
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001658 weight = &orig_neigh_node->bat_iv.bcast_own_sum[if_num];
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001659 *weight = bitmap_weight(word,
1660 BATADV_TQ_LOCAL_WINDOW_SIZE);
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001661 spin_unlock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001662 }
1663
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001664 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001665 "Drop packet: originator packet from myself (via neighbor)\n");
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001666 batadv_orig_node_free_ref(orig_neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001667 return;
1668 }
1669
1670 if (is_my_oldorig) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001671 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001672 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1673 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001674 return;
1675 }
1676
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001677 if (ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001678 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001679 "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1680 ethhdr->h_source);
Marek Lindner13b25412012-03-11 06:17:53 +08001681 return;
1682 }
1683
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001684 orig_node = batadv_iv_ogm_orig_get(bat_priv, ogm_packet->orig);
Marek Lindnerfc957272011-07-30 12:04:12 +02001685 if (!orig_node)
1686 return;
1687
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001688 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1689 if_incoming, BATADV_IF_DEFAULT);
Marek Lindnerfc957272011-07-30 12:04:12 +02001690
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001691 rcu_read_lock();
1692 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1693 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1694 continue;
1695
1696 if (hard_iface->soft_iface != bat_priv->soft_iface)
1697 continue;
1698
1699 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1700 if_incoming, hard_iface);
Marek Lindnerfc957272011-07-30 12:04:12 +02001701 }
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001702 rcu_read_unlock();
Marek Lindnerfc957272011-07-30 12:04:12 +02001703
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001704 batadv_orig_node_free_ref(orig_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001705}
1706
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001707static int batadv_iv_ogm_receive(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001708 struct batadv_hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +02001709{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001710 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001711 struct batadv_ogm_packet *ogm_packet;
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001712 uint8_t *packet_pos;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001713 int ogm_offset;
Marek Lindneref261572013-04-23 21:39:57 +08001714 bool ret;
Marek Lindnerc3e29312012-03-04 16:56:25 +08001715
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001716 ret = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
Marek Lindnerc3e29312012-03-04 16:56:25 +08001717 if (!ret)
1718 return NET_RX_DROP;
Marek Lindnerfc957272011-07-30 12:04:12 +02001719
Marek Lindneredbf12b2012-03-11 06:17:49 +08001720 /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
1721 * that does not have B.A.T.M.A.N. IV enabled ?
1722 */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001723 if (bat_priv->bat_algo_ops->bat_ogm_emit != batadv_iv_ogm_emit)
Marek Lindneredbf12b2012-03-11 06:17:49 +08001724 return NET_RX_DROP;
1725
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001726 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
1727 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
Martin Hundebøllf8214862012-04-20 17:02:45 +02001728 skb->len + ETH_HLEN);
1729
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001730 ogm_offset = 0;
1731 ogm_packet = (struct batadv_ogm_packet *)skb->data;
Marek Lindnerfc957272011-07-30 12:04:12 +02001732
1733 /* unpack the aggregated packets and process them one by one */
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001734 while (batadv_iv_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
1735 ogm_packet->tvlv_len)) {
1736 batadv_iv_ogm_process(skb, ogm_offset, if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001737
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001738 ogm_offset += BATADV_OGM_HLEN;
1739 ogm_offset += ntohs(ogm_packet->tvlv_len);
Marek Lindnerfc957272011-07-30 12:04:12 +02001740
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001741 packet_pos = skb->data + ogm_offset;
1742 ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Marek Lindnerb47506d2013-03-04 10:39:49 +08001743 }
Marek Lindnerc3e29312012-03-04 16:56:25 +08001744
1745 kfree_skb(skb);
1746 return NET_RX_SUCCESS;
Marek Lindnerfc957272011-07-30 12:04:12 +02001747}
Marek Lindner1c280472011-11-28 17:40:17 +08001748
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001749/**
1750 * batadv_iv_ogm_orig_print_neigh - print neighbors for the originator table
Simon Wunderlich89652332013-11-13 19:14:46 +01001751 * @orig_node: the orig_node for which the neighbors are printed
1752 * @if_outgoing: outgoing interface for these entries
1753 * @seq: debugfs table seq_file struct
1754 *
1755 * Must be called while holding an rcu lock.
1756 */
1757static void
1758batadv_iv_ogm_orig_print_neigh(struct batadv_orig_node *orig_node,
1759 struct batadv_hard_iface *if_outgoing,
1760 struct seq_file *seq)
1761{
1762 struct batadv_neigh_node *neigh_node;
1763 struct batadv_neigh_ifinfo *n_ifinfo;
1764
1765 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1766 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
1767 if (!n_ifinfo)
1768 continue;
1769
1770 seq_printf(seq, " %pM (%3i)",
1771 neigh_node->addr,
1772 n_ifinfo->bat_iv.tq_avg);
1773
1774 batadv_neigh_ifinfo_free_ref(n_ifinfo);
1775 }
1776}
1777
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001778/**
1779 * batadv_iv_ogm_orig_print - print the originator table
1780 * @bat_priv: the bat priv with all the soft interface information
1781 * @seq: debugfs table seq_file struct
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001782 * @if_outgoing: the outgoing interface for which this should be printed
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001783 */
1784static void batadv_iv_ogm_orig_print(struct batadv_priv *bat_priv,
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001785 struct seq_file *seq,
1786 struct batadv_hard_iface *if_outgoing)
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001787{
Simon Wunderlich89652332013-11-13 19:14:46 +01001788 struct batadv_neigh_node *neigh_node;
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001789 struct batadv_hashtable *hash = bat_priv->orig_hash;
1790 int last_seen_msecs, last_seen_secs;
1791 struct batadv_orig_node *orig_node;
Simon Wunderlich89652332013-11-13 19:14:46 +01001792 struct batadv_neigh_ifinfo *n_ifinfo;
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001793 unsigned long last_seen_jiffies;
1794 struct hlist_head *head;
1795 int batman_count = 0;
1796 uint32_t i;
1797
1798 seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
1799 "Originator", "last-seen", "#", BATADV_TQ_MAX_VALUE,
1800 "Nexthop", "outgoingIF", "Potential nexthops");
1801
1802 for (i = 0; i < hash->size; i++) {
1803 head = &hash->table[i];
1804
1805 rcu_read_lock();
1806 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001807 neigh_node = batadv_orig_router_get(orig_node,
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001808 if_outgoing);
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001809 if (!neigh_node)
1810 continue;
1811
Simon Wunderlich89652332013-11-13 19:14:46 +01001812 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node,
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001813 if_outgoing);
Simon Wunderlich89652332013-11-13 19:14:46 +01001814 if (!n_ifinfo)
1815 goto next;
1816
1817 if (n_ifinfo->bat_iv.tq_avg == 0)
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001818 goto next;
1819
1820 last_seen_jiffies = jiffies - orig_node->last_seen;
1821 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
1822 last_seen_secs = last_seen_msecs / 1000;
1823 last_seen_msecs = last_seen_msecs % 1000;
1824
1825 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
1826 orig_node->orig, last_seen_secs,
Simon Wunderlich89652332013-11-13 19:14:46 +01001827 last_seen_msecs, n_ifinfo->bat_iv.tq_avg,
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001828 neigh_node->addr,
1829 neigh_node->if_incoming->net_dev->name);
1830
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001831 batadv_iv_ogm_orig_print_neigh(orig_node, if_outgoing,
1832 seq);
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001833 seq_puts(seq, "\n");
1834 batman_count++;
1835
1836next:
1837 batadv_neigh_node_free_ref(neigh_node);
Simon Wunderlich89652332013-11-13 19:14:46 +01001838 if (n_ifinfo)
1839 batadv_neigh_ifinfo_free_ref(n_ifinfo);
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001840 }
1841 rcu_read_unlock();
1842 }
1843
1844 if (batman_count == 0)
1845 seq_puts(seq, "No batman nodes in range ...\n");
1846}
1847
Antonio Quartullia3285a82013-09-02 12:15:04 +02001848/**
1849 * batadv_iv_ogm_neigh_cmp - compare the metrics of two neighbors
1850 * @neigh1: the first neighbor object of the comparison
Simon Wunderlich89652332013-11-13 19:14:46 +01001851 * @if_outgoing1: outgoing interface for the first neighbor
Antonio Quartullia3285a82013-09-02 12:15:04 +02001852 * @neigh2: the second neighbor object of the comparison
Simon Wunderlich89652332013-11-13 19:14:46 +01001853 * @if_outgoing2: outgoing interface for the second neighbor
Antonio Quartullia3285a82013-09-02 12:15:04 +02001854 *
1855 * Returns a value less, equal to or greater than 0 if the metric via neigh1 is
1856 * lower, the same as or higher than the metric via neigh2
1857 */
1858static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1,
Simon Wunderlich89652332013-11-13 19:14:46 +01001859 struct batadv_hard_iface *if_outgoing1,
1860 struct batadv_neigh_node *neigh2,
1861 struct batadv_hard_iface *if_outgoing2)
Antonio Quartullia3285a82013-09-02 12:15:04 +02001862{
Simon Wunderlich89652332013-11-13 19:14:46 +01001863 struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
Antonio Quartullia3285a82013-09-02 12:15:04 +02001864 uint8_t tq1, tq2;
Simon Wunderlich89652332013-11-13 19:14:46 +01001865 int diff;
Antonio Quartullia3285a82013-09-02 12:15:04 +02001866
Simon Wunderlich89652332013-11-13 19:14:46 +01001867 neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
1868 neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
Antonio Quartullia3285a82013-09-02 12:15:04 +02001869
Simon Wunderlich89652332013-11-13 19:14:46 +01001870 if (!neigh1_ifinfo || !neigh2_ifinfo) {
1871 diff = 0;
1872 goto out;
1873 }
1874
1875 tq1 = neigh1_ifinfo->bat_iv.tq_avg;
1876 tq2 = neigh2_ifinfo->bat_iv.tq_avg;
1877 diff = tq1 - tq2;
1878
1879out:
1880 if (neigh1_ifinfo)
1881 batadv_neigh_ifinfo_free_ref(neigh1_ifinfo);
1882 if (neigh2_ifinfo)
1883 batadv_neigh_ifinfo_free_ref(neigh2_ifinfo);
1884
1885 return diff;
Antonio Quartullia3285a82013-09-02 12:15:04 +02001886}
1887
Antonio Quartullic43c9812013-09-02 12:15:05 +02001888/**
1889 * batadv_iv_ogm_neigh_is_eob - check if neigh1 is equally good or better than
1890 * neigh2 from the metric prospective
1891 * @neigh1: the first neighbor object of the comparison
Simon Wunderlich89652332013-11-13 19:14:46 +01001892 * @if_outgoing: outgoing interface for the first neighbor
Antonio Quartullic43c9812013-09-02 12:15:05 +02001893 * @neigh2: the second neighbor object of the comparison
Simon Wunderlich89652332013-11-13 19:14:46 +01001894 * @if_outgoing2: outgoing interface for the second neighbor
Antonio Quartullic43c9812013-09-02 12:15:05 +02001895
Simon Wunderlich89652332013-11-13 19:14:46 +01001896 * Returns true if the metric via neigh1 is equally good or better than
1897 * the metric via neigh2, false otherwise.
1898 */
1899static bool
1900batadv_iv_ogm_neigh_is_eob(struct batadv_neigh_node *neigh1,
1901 struct batadv_hard_iface *if_outgoing1,
1902 struct batadv_neigh_node *neigh2,
1903 struct batadv_hard_iface *if_outgoing2)
1904{
1905 struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
1906 uint8_t tq1, tq2;
1907 bool ret;
1908
1909 neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
1910 neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
1911
1912 /* we can't say that the metric is better */
1913 if (!neigh1_ifinfo || !neigh2_ifinfo) {
1914 ret = false;
1915 goto out;
1916 }
1917
1918 tq1 = neigh1_ifinfo->bat_iv.tq_avg;
1919 tq2 = neigh2_ifinfo->bat_iv.tq_avg;
1920 ret = (tq1 - tq2) > -BATADV_TQ_SIMILARITY_THRESHOLD;
1921
1922out:
1923 if (neigh1_ifinfo)
1924 batadv_neigh_ifinfo_free_ref(neigh1_ifinfo);
1925 if (neigh2_ifinfo)
1926 batadv_neigh_ifinfo_free_ref(neigh2_ifinfo);
1927
1928 return ret;
Antonio Quartullic43c9812013-09-02 12:15:05 +02001929}
1930
Sven Eckelmann56303d32012-06-05 22:31:31 +02001931static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
Marek Lindner519d3492012-04-18 17:15:57 +08001932 .name = "BATMAN_IV",
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001933 .bat_iface_enable = batadv_iv_ogm_iface_enable,
1934 .bat_iface_disable = batadv_iv_ogm_iface_disable,
1935 .bat_iface_update_mac = batadv_iv_ogm_iface_update_mac,
1936 .bat_primary_iface_set = batadv_iv_ogm_primary_iface_set,
1937 .bat_ogm_schedule = batadv_iv_ogm_schedule,
1938 .bat_ogm_emit = batadv_iv_ogm_emit,
Antonio Quartullia3285a82013-09-02 12:15:04 +02001939 .bat_neigh_cmp = batadv_iv_ogm_neigh_cmp,
Antonio Quartullic43c9812013-09-02 12:15:05 +02001940 .bat_neigh_is_equiv_or_better = batadv_iv_ogm_neigh_is_eob,
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001941 .bat_orig_print = batadv_iv_ogm_orig_print,
Antonio Quartullid0015fd2013-09-03 11:10:23 +02001942 .bat_orig_free = batadv_iv_ogm_orig_free,
1943 .bat_orig_add_if = batadv_iv_ogm_orig_add_if,
1944 .bat_orig_del_if = batadv_iv_ogm_orig_del_if,
Marek Lindner1c280472011-11-28 17:40:17 +08001945};
1946
Sven Eckelmann81c524f2012-05-12 02:09:22 +02001947int __init batadv_iv_init(void)
Marek Lindner1c280472011-11-28 17:40:17 +08001948{
Marek Lindnerc3e29312012-03-04 16:56:25 +08001949 int ret;
1950
1951 /* batman originator packet */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001952 ret = batadv_recv_handler_register(BATADV_IV_OGM,
1953 batadv_iv_ogm_receive);
Marek Lindnerc3e29312012-03-04 16:56:25 +08001954 if (ret < 0)
1955 goto out;
1956
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001957 ret = batadv_algo_register(&batadv_batman_iv);
Marek Lindnerc3e29312012-03-04 16:56:25 +08001958 if (ret < 0)
1959 goto handler_unregister;
1960
1961 goto out;
1962
1963handler_unregister:
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001964 batadv_recv_handler_unregister(BATADV_IV_OGM);
Marek Lindnerc3e29312012-03-04 16:56:25 +08001965out:
1966 return ret;
Marek Lindner1c280472011-11-28 17:40:17 +08001967}