blob: 1f9fe4270454957f2bad5581ae64629e38d73720 [file] [log] [blame]
Antonio Quartulli0b873932013-01-04 03:05:31 +01001/* Copyright (C) 2007-2013 B.A.T.M.A.N. contributors:
Marek Lindnerfc957272011-07-30 12:04:12 +02002 *
3 * Marek Lindner, Simon Wunderlich
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
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{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200469 struct batadv_hard_iface *hard_iface;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200470 struct net_device *soft_iface;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200471 struct batadv_priv *bat_priv;
472 struct batadv_hard_iface *primary_if = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200473 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200474 unsigned char directlink;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200475 uint8_t *packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200476
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200477 packet_pos = forw_packet->skb->data;
478 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +0200479 directlink = (batadv_ogm_packet->flags & BATADV_DIRECTLINK ? 1 : 0);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200480
481 if (!forw_packet->if_incoming) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100482 pr_err("Error - can't forward packet: incoming iface not specified\n");
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200483 goto out;
484 }
485
486 soft_iface = forw_packet->if_incoming->soft_iface;
487 bat_priv = netdev_priv(soft_iface);
488
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200489 if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200490 goto out;
491
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200492 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200493 if (!primary_if)
494 goto out;
495
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200496 /* multihomed peer assumed
497 * non-primary OGMs are only broadcasted on their interface
498 */
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100499 if ((directlink && (batadv_ogm_packet->ttl == 1)) ||
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200500 (forw_packet->own && (forw_packet->if_incoming != primary_if))) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200501 /* FIXME: what about aggregated packets ? */
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200502 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200503 "%s packet (originator %pM, seqno %u, TTL %d) on interface %s [%pM]\n",
504 (forw_packet->own ? "Sending own" : "Forwarding"),
Sven Eckelmann96412692012-06-05 22:31:30 +0200505 batadv_ogm_packet->orig,
506 ntohl(batadv_ogm_packet->seqno),
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100507 batadv_ogm_packet->ttl,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200508 forw_packet->if_incoming->net_dev->name,
509 forw_packet->if_incoming->net_dev->dev_addr);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200510
511 /* skb is only used once and than forw_packet is free'd */
Sven Eckelmann9455e342012-05-12 02:09:37 +0200512 batadv_send_skb_packet(forw_packet->skb,
513 forw_packet->if_incoming,
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200514 batadv_broadcast_addr);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200515 forw_packet->skb = NULL;
516
517 goto out;
518 }
519
520 /* broadcast on every interface */
521 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200522 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200523 if (hard_iface->soft_iface != soft_iface)
524 continue;
525
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200526 batadv_iv_ogm_send_to_if(forw_packet, hard_iface);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200527 }
528 rcu_read_unlock();
529
530out:
531 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200532 batadv_hardif_free_ref(primary_if);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200533}
534
535/* return true if new_packet can be aggregated with forw_packet */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200536static bool
Sven Eckelmann96412692012-06-05 22:31:30 +0200537batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200538 struct batadv_priv *bat_priv,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200539 int packet_len, unsigned long send_time,
540 bool directlink,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200541 const struct batadv_hard_iface *if_incoming,
542 const struct batadv_forw_packet *forw_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200543{
Sven Eckelmann96412692012-06-05 22:31:30 +0200544 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200545 int aggregated_bytes = forw_packet->packet_len + packet_len;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200546 struct batadv_hard_iface *primary_if = NULL;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200547 bool res = false;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200548 unsigned long aggregation_end_time;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200549
Sven Eckelmann96412692012-06-05 22:31:30 +0200550 batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200551 aggregation_end_time = send_time;
552 aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200553
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200554 /* we can aggregate the current packet to this aggregated packet
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200555 * if:
556 *
557 * - the send time is within our MAX_AGGREGATION_MS time
558 * - the resulting packet wont be bigger than
559 * MAX_AGGREGATION_BYTES
560 */
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200561 if (time_before(send_time, forw_packet->send_time) &&
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200562 time_after_eq(aggregation_end_time, forw_packet->send_time) &&
563 (aggregated_bytes <= BATADV_MAX_AGGREGATION_BYTES)) {
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200564 /* check aggregation compatibility
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200565 * -> direct link packets are broadcasted on
566 * their interface only
567 * -> aggregate packet if the current packet is
568 * a "global" packet as well as the base
569 * packet
570 */
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200571 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200572 if (!primary_if)
573 goto out;
574
575 /* packets without direct link flag and high TTL
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200576 * are flooded through the net
577 */
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200578 if ((!directlink) &&
Sven Eckelmann96412692012-06-05 22:31:30 +0200579 (!(batadv_ogm_packet->flags & BATADV_DIRECTLINK)) &&
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100580 (batadv_ogm_packet->ttl != 1) &&
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200581
582 /* own packets originating non-primary
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200583 * interfaces leave only that interface
584 */
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200585 ((!forw_packet->own) ||
586 (forw_packet->if_incoming == primary_if))) {
587 res = true;
588 goto out;
589 }
590
591 /* if the incoming packet is sent via this one
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200592 * interface only - we still can aggregate
593 */
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200594 if ((directlink) &&
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100595 (new_bat_ogm_packet->ttl == 1) &&
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200596 (forw_packet->if_incoming == if_incoming) &&
597
598 /* packets from direct neighbors or
599 * own secondary interface packets
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200600 * (= secondary interface packets in general)
601 */
Sven Eckelmann96412692012-06-05 22:31:30 +0200602 (batadv_ogm_packet->flags & BATADV_DIRECTLINK ||
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200603 (forw_packet->own &&
604 forw_packet->if_incoming != primary_if))) {
605 res = true;
606 goto out;
607 }
608 }
609
610out:
611 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200612 batadv_hardif_free_ref(primary_if);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200613 return res;
614}
615
616/* create a new aggregated packet and add this packet to it */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200617static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,
618 int packet_len, unsigned long send_time,
619 bool direct_link,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200620 struct batadv_hard_iface *if_incoming,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200621 int own_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200622{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200623 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
624 struct batadv_forw_packet *forw_packet_aggr;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200625 unsigned char *skb_buff;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200626 unsigned int skb_size;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200627
628 if (!atomic_inc_not_zero(&if_incoming->refcount))
629 return;
630
631 /* own packet should always be scheduled */
632 if (!own_packet) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200633 if (!batadv_atomic_dec_not_zero(&bat_priv->batman_queue_left)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200634 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200635 "batman packet queue full\n");
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200636 goto out;
637 }
638 }
639
640 forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC);
641 if (!forw_packet_aggr) {
642 if (!own_packet)
643 atomic_inc(&bat_priv->batman_queue_left);
644 goto out;
645 }
646
647 if ((atomic_read(&bat_priv->aggregated_ogms)) &&
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200648 (packet_len < BATADV_MAX_AGGREGATION_BYTES))
Sven Eckelmann5b246572012-11-04 17:11:45 +0100649 skb_size = BATADV_MAX_AGGREGATION_BYTES;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200650 else
Sven Eckelmann5b246572012-11-04 17:11:45 +0100651 skb_size = packet_len;
652
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200653 skb_size += ETH_HLEN;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200654
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200655 forw_packet_aggr->skb = netdev_alloc_skb_ip_align(NULL, skb_size);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200656 if (!forw_packet_aggr->skb) {
657 if (!own_packet)
658 atomic_inc(&bat_priv->batman_queue_left);
659 kfree(forw_packet_aggr);
660 goto out;
661 }
Simon Wunderlichc54f38c2013-07-29 17:56:44 +0200662 forw_packet_aggr->skb->priority = TC_PRIO_CONTROL;
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200663 skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200664
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200665 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
666 forw_packet_aggr->packet_len = packet_len;
667 memcpy(skb_buff, packet_buff, packet_len);
668
669 forw_packet_aggr->own = own_packet;
670 forw_packet_aggr->if_incoming = if_incoming;
671 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:
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200693 batadv_hardif_free_ref(if_incoming);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200694}
695
696/* aggregate a new packet into the existing ogm packet */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200697static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200698 const unsigned char *packet_buff,
699 int packet_len, bool direct_link)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200700{
701 unsigned char *skb_buff;
Sven Eckelmann8de47de2012-07-08 16:32:09 +0200702 unsigned long new_direct_link_flag;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200703
704 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
705 memcpy(skb_buff, packet_buff, packet_len);
706 forw_packet_aggr->packet_len += packet_len;
707 forw_packet_aggr->num_packets++;
708
709 /* save packet direct link flag status */
Sven Eckelmann8de47de2012-07-08 16:32:09 +0200710 if (direct_link) {
711 new_direct_link_flag = BIT(forw_packet_aggr->num_packets);
712 forw_packet_aggr->direct_link_flags |= new_direct_link_flag;
713 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200714}
715
Sven Eckelmann56303d32012-06-05 22:31:31 +0200716static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200717 unsigned char *packet_buff,
718 int packet_len,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200719 struct batadv_hard_iface *if_incoming,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200720 int own_packet, unsigned long send_time)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200721{
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200722 /* _aggr -> pointer to the packet we want to aggregate with
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200723 * _pos -> pointer to the position in the queue
724 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200725 struct batadv_forw_packet *forw_packet_aggr = NULL;
726 struct batadv_forw_packet *forw_packet_pos = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200727 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200728 bool direct_link;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200729 unsigned long max_aggregation_jiffies;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200730
Sven Eckelmann96412692012-06-05 22:31:30 +0200731 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
732 direct_link = batadv_ogm_packet->flags & BATADV_DIRECTLINK ? 1 : 0;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200733 max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200734
735 /* find position for the packet in the forward queue */
736 spin_lock_bh(&bat_priv->forw_bat_list_lock);
737 /* own packets are not to be aggregated */
738 if ((atomic_read(&bat_priv->aggregated_ogms)) && (!own_packet)) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800739 hlist_for_each_entry(forw_packet_pos,
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200740 &bat_priv->forw_bat_list, list) {
Sven Eckelmann96412692012-06-05 22:31:30 +0200741 if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200742 bat_priv, packet_len,
743 send_time, direct_link,
744 if_incoming,
745 forw_packet_pos)) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200746 forw_packet_aggr = forw_packet_pos;
747 break;
748 }
749 }
750 }
751
752 /* nothing to aggregate with - either aggregation disabled or no
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200753 * suitable aggregation packet found
754 */
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200755 if (!forw_packet_aggr) {
756 /* the following section can run without the lock */
757 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
758
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200759 /* if we could not aggregate this packet with one of the others
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200760 * we hold it back for a while, so that it might be aggregated
761 * later on
762 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200763 if (!own_packet && atomic_read(&bat_priv->aggregated_ogms))
764 send_time += max_aggregation_jiffies;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200765
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200766 batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
767 send_time, direct_link,
768 if_incoming, own_packet);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200769 } else {
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200770 batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
771 packet_len, direct_link);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200772 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
773 }
774}
775
Sven Eckelmann56303d32012-06-05 22:31:31 +0200776static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200777 const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +0200778 struct batadv_ogm_packet *batadv_ogm_packet,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200779 bool is_single_hop_neigh,
780 bool is_from_best_next_hop,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200781 struct batadv_hard_iface *if_incoming)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200782{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200783 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Marek Lindneref261572013-04-23 21:39:57 +0800784 uint16_t tvlv_len;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200785
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100786 if (batadv_ogm_packet->ttl <= 1) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200787 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200788 return;
789 }
790
Marek Lindner13b25412012-03-11 06:17:53 +0800791 if (!is_from_best_next_hop) {
792 /* Mark the forwarded packet when it is not coming from our
793 * best next hop. We still need to forward the packet for our
794 * neighbor link quality detection to work in case the packet
795 * originated from a single hop neighbor. Otherwise we can
796 * simply drop the ogm.
797 */
798 if (is_single_hop_neigh)
Sven Eckelmann96412692012-06-05 22:31:30 +0200799 batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP;
Marek Lindner13b25412012-03-11 06:17:53 +0800800 else
801 return;
802 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200803
Marek Lindneref261572013-04-23 21:39:57 +0800804 tvlv_len = ntohs(batadv_ogm_packet->tvlv_len);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200805
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100806 batadv_ogm_packet->ttl--;
Sven Eckelmann96412692012-06-05 22:31:30 +0200807 memcpy(batadv_ogm_packet->prev_sender, ethhdr->h_source, ETH_ALEN);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200808
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200809 /* apply hop penalty */
Sven Eckelmann96412692012-06-05 22:31:30 +0200810 batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200811 bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200812
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200813 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200814 "Forwarding packet: tq: %i, ttl: %i\n",
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100815 batadv_ogm_packet->tq, batadv_ogm_packet->ttl);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200816
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200817 /* switch of primaries first hop flag when forwarding */
Sven Eckelmann96412692012-06-05 22:31:30 +0200818 batadv_ogm_packet->flags &= ~BATADV_PRIMARIES_FIRST_HOP;
Marek Lindner75cd33f2012-03-01 15:35:16 +0800819 if (is_single_hop_neigh)
Sven Eckelmann96412692012-06-05 22:31:30 +0200820 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200821 else
Sven Eckelmann96412692012-06-05 22:31:30 +0200822 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200823
Sven Eckelmann96412692012-06-05 22:31:30 +0200824 batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
Marek Lindneref261572013-04-23 21:39:57 +0800825 BATADV_OGM_HLEN + tvlv_len,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200826 if_incoming, 0, batadv_iv_ogm_fwd_send_time());
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200827}
828
Antonio Quartullid9896612013-04-17 17:44:43 +0200829/**
830 * batadv_iv_ogm_slide_own_bcast_window - bitshift own OGM broadcast windows for
831 * the given interface
832 * @hard_iface: the interface for which the windows have to be shifted
833 */
834static void
835batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
836{
837 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
838 struct batadv_hashtable *hash = bat_priv->orig_hash;
839 struct hlist_head *head;
840 struct batadv_orig_node *orig_node;
841 unsigned long *word;
842 uint32_t i;
843 size_t word_index;
844 uint8_t *w;
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200845 int if_num;
Antonio Quartullid9896612013-04-17 17:44:43 +0200846
847 for (i = 0; i < hash->size; i++) {
848 head = &hash->table[i];
849
850 rcu_read_lock();
851 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200852 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Antonio Quartullid9896612013-04-17 17:44:43 +0200853 word_index = hard_iface->if_num * BATADV_NUM_WORDS;
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200854 word = &(orig_node->bat_iv.bcast_own[word_index]);
Antonio Quartullid9896612013-04-17 17:44:43 +0200855
856 batadv_bit_get_packet(bat_priv, word, 1, 0);
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200857 if_num = hard_iface->if_num;
858 w = &orig_node->bat_iv.bcast_own_sum[if_num];
Antonio Quartullid9896612013-04-17 17:44:43 +0200859 *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200860 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Antonio Quartullid9896612013-04-17 17:44:43 +0200861 }
862 rcu_read_unlock();
863 }
864}
865
Sven Eckelmann56303d32012-06-05 22:31:31 +0200866static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200867{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200868 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindner14511512012-08-02 17:20:26 +0200869 unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff;
Sven Eckelmann96412692012-06-05 22:31:30 +0200870 struct batadv_ogm_packet *batadv_ogm_packet;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200871 struct batadv_hard_iface *primary_if;
Marek Lindner14511512012-08-02 17:20:26 +0200872 int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len;
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200873 uint32_t seqno;
Marek Lindneref261572013-04-23 21:39:57 +0800874 uint16_t tvlv_len = 0;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200875
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200876 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200877
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800878 if (hard_iface == primary_if) {
879 /* tt changes have to be committed before the tvlv data is
880 * appended as it may alter the tt tvlv container
881 */
882 batadv_tt_local_commit_changes(bat_priv);
Marek Lindneref261572013-04-23 21:39:57 +0800883 tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff,
884 ogm_buff_len,
885 BATADV_OGM_HLEN);
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800886 }
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800887
Marek Lindner14511512012-08-02 17:20:26 +0200888 batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff);
Marek Lindneref261572013-04-23 21:39:57 +0800889 batadv_ogm_packet->tvlv_len = htons(tvlv_len);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200890
891 /* change sequence number to network order */
Marek Lindner14511512012-08-02 17:20:26 +0200892 seqno = (uint32_t)atomic_read(&hard_iface->bat_iv.ogm_seqno);
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200893 batadv_ogm_packet->seqno = htonl(seqno);
Marek Lindner14511512012-08-02 17:20:26 +0200894 atomic_inc(&hard_iface->bat_iv.ogm_seqno);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200895
Antonio Quartullid9896612013-04-17 17:44:43 +0200896 batadv_iv_ogm_slide_own_bcast_window(hard_iface);
Marek Lindner14511512012-08-02 17:20:26 +0200897 batadv_iv_ogm_queue_add(bat_priv, hard_iface->bat_iv.ogm_buff,
898 hard_iface->bat_iv.ogm_buff_len, hard_iface, 1,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200899 batadv_iv_ogm_emit_send_time(bat_priv));
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200900
901 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200902 batadv_hardif_free_ref(primary_if);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200903}
904
Simon Wunderlich89652332013-11-13 19:14:46 +0100905/**
906 * batadv_iv_ogm_orig_update - use OGM to update corresponding data in an
907 * originator
908 * @bat_priv: the bat priv with all the soft interface information
909 * @orig_node: the orig node who originally emitted the ogm packet
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100910 * @orig_ifinfo: ifinfo for the outgoing interface of the orig_node
Simon Wunderlich89652332013-11-13 19:14:46 +0100911 * @ethhdr: Ethernet header of the OGM
912 * @batadv_ogm_packet: the ogm packet
913 * @if_incoming: interface where the packet was received
914 * @if_outgoing: interface for which the retransmission should be considered
Simon Wunderlich89652332013-11-13 19:14:46 +0100915 * @dup_status: the duplicate status of this ogm packet.
916 */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200917static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200918batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
919 struct batadv_orig_node *orig_node,
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100920 struct batadv_orig_ifinfo *orig_ifinfo,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200921 const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +0200922 const struct batadv_ogm_packet *batadv_ogm_packet,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200923 struct batadv_hard_iface *if_incoming,
Simon Wunderlich89652332013-11-13 19:14:46 +0100924 struct batadv_hard_iface *if_outgoing,
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +0200925 enum batadv_dup_status dup_status)
Marek Lindnerfc957272011-07-30 12:04:12 +0200926{
Simon Wunderlich89652332013-11-13 19:14:46 +0100927 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
928 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200929 struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
930 struct batadv_neigh_node *router = NULL;
931 struct batadv_orig_node *orig_node_tmp;
Linus LĂĽssing7caf69f2012-09-18 03:01:08 +0200932 int if_num;
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200933 uint8_t sum_orig, sum_neigh;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200934 uint8_t *neigh_addr;
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200935 uint8_t tq_avg;
Marek Lindnerfc957272011-07-30 12:04:12 +0200936
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200937 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200938 "update_originator(): Searching and updating originator entry of received packet\n");
Marek Lindnerfc957272011-07-30 12:04:12 +0200939
940 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800941 hlist_for_each_entry_rcu(tmp_neigh_node,
Marek Lindnerfc957272011-07-30 12:04:12 +0200942 &orig_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200943 neigh_addr = tmp_neigh_node->addr;
944 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
945 tmp_neigh_node->if_incoming == if_incoming &&
946 atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
Antonio Quartulli38dc40e2013-03-30 17:22:00 +0100947 if (WARN(neigh_node, "too many matching neigh_nodes"))
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200948 batadv_neigh_node_free_ref(neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +0200949 neigh_node = tmp_neigh_node;
950 continue;
951 }
952
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +0200953 if (dup_status != BATADV_NO_DUP)
Marek Lindnerfc957272011-07-30 12:04:12 +0200954 continue;
955
Simon Wunderlich89652332013-11-13 19:14:46 +0100956 /* only update the entry for this outgoing interface */
957 neigh_ifinfo = batadv_neigh_ifinfo_get(tmp_neigh_node,
958 if_outgoing);
959 if (!neigh_ifinfo)
960 continue;
961
962 spin_lock_bh(&tmp_neigh_node->ifinfo_lock);
963 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
964 &neigh_ifinfo->bat_iv.tq_index, 0);
965 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
966 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
967 spin_unlock_bh(&tmp_neigh_node->ifinfo_lock);
968
969 batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
970 neigh_ifinfo = NULL;
Marek Lindnerfc957272011-07-30 12:04:12 +0200971 }
972
973 if (!neigh_node) {
Sven Eckelmann56303d32012-06-05 22:31:31 +0200974 struct batadv_orig_node *orig_tmp;
Marek Lindnerfc957272011-07-30 12:04:12 +0200975
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200976 orig_tmp = batadv_iv_ogm_orig_get(bat_priv, ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +0200977 if (!orig_tmp)
978 goto unlock;
979
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200980 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
981 ethhdr->h_source,
Antonio Quartulli863dd7a2013-03-25 13:49:46 +0100982 orig_node, orig_tmp);
Marek Lindnerfc957272011-07-30 12:04:12 +0200983
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200984 batadv_orig_node_free_ref(orig_tmp);
Marek Lindnerfc957272011-07-30 12:04:12 +0200985 if (!neigh_node)
986 goto unlock;
987 } else
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200988 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200989 "Updating existing last-hop neighbor of originator\n");
Marek Lindnerfc957272011-07-30 12:04:12 +0200990
991 rcu_read_unlock();
Simon Wunderlich89652332013-11-13 19:14:46 +0100992 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
993 if (!neigh_ifinfo)
994 goto out;
Marek Lindnerfc957272011-07-30 12:04:12 +0200995
Marek Lindnerd7b2a972012-03-01 15:35:19 +0800996 neigh_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +0200997
Simon Wunderlich89652332013-11-13 19:14:46 +0100998 spin_lock_bh(&neigh_node->ifinfo_lock);
999 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
1000 &neigh_ifinfo->bat_iv.tq_index,
Sven Eckelmann96412692012-06-05 22:31:30 +02001001 batadv_ogm_packet->tq);
Simon Wunderlich89652332013-11-13 19:14:46 +01001002 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
1003 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
1004 spin_unlock_bh(&neigh_node->ifinfo_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001005
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001006 if (dup_status == BATADV_NO_DUP) {
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001007 orig_ifinfo->last_ttl = batadv_ogm_packet->ttl;
Simon Wunderlich89652332013-11-13 19:14:46 +01001008 neigh_ifinfo->last_ttl = batadv_ogm_packet->ttl;
Marek Lindnerfc957272011-07-30 12:04:12 +02001009 }
1010
Marek Lindnerfc957272011-07-30 12:04:12 +02001011 /* if this neighbor already is our next hop there is nothing
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001012 * to change
1013 */
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001014 router = batadv_orig_router_get(orig_node, if_outgoing);
Marek Lindnerfc957272011-07-30 12:04:12 +02001015 if (router == neigh_node)
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001016 goto out;
Marek Lindnerfc957272011-07-30 12:04:12 +02001017
Simon Wunderlich89652332013-11-13 19:14:46 +01001018 if (router) {
1019 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1020 if (!router_ifinfo)
1021 goto out;
1022
1023 /* if this neighbor does not offer a better TQ we won't
1024 * consider it
1025 */
1026 if (router_ifinfo->bat_iv.tq_avg > neigh_ifinfo->bat_iv.tq_avg)
1027 goto out;
1028 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001029
1030 /* if the TQ is the same and the link not more symmetric we
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001031 * won't consider it either
1032 */
Simon Wunderlich89652332013-11-13 19:14:46 +01001033 if (router_ifinfo &&
1034 (neigh_ifinfo->bat_iv.tq_avg == router_ifinfo->bat_iv.tq_avg)) {
Marek Lindnerfc957272011-07-30 12:04:12 +02001035 orig_node_tmp = router->orig_node;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001036 spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
Linus LĂĽssing7caf69f2012-09-18 03:01:08 +02001037 if_num = router->if_incoming->if_num;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001038 sum_orig = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
1039 spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001040
1041 orig_node_tmp = neigh_node->orig_node;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001042 spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
Linus LĂĽssing7caf69f2012-09-18 03:01:08 +02001043 if_num = neigh_node->if_incoming->if_num;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001044 sum_neigh = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
1045 spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001046
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001047 if (sum_orig >= sum_neigh)
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001048 goto out;
Marek Lindnerfc957272011-07-30 12:04:12 +02001049 }
1050
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001051 batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001052 goto out;
1053
1054unlock:
1055 rcu_read_unlock();
1056out:
1057 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001058 batadv_neigh_node_free_ref(neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001059 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001060 batadv_neigh_node_free_ref(router);
Simon Wunderlich89652332013-11-13 19:14:46 +01001061 if (neigh_ifinfo)
1062 batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
1063 if (router_ifinfo)
1064 batadv_neigh_ifinfo_free_ref(router_ifinfo);
Marek Lindnerfc957272011-07-30 12:04:12 +02001065}
1066
Simon Wunderlich89652332013-11-13 19:14:46 +01001067/**
1068 * batadv_iv_ogm_calc_tq - calculate tq for current received ogm packet
1069 * @orig_node: the orig node who originally emitted the ogm packet
1070 * @orig_neigh_node: the orig node struct of the neighbor who sent the packet
1071 * @batadv_ogm_packet: the ogm packet
1072 * @if_incoming: interface where the packet was received
1073 * @if_outgoing: interface for which the retransmission should be considered
1074 *
1075 * Returns 1 if the link can be considered bidirectional, 0 otherwise
1076 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001077static int batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
1078 struct batadv_orig_node *orig_neigh_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02001079 struct batadv_ogm_packet *batadv_ogm_packet,
Simon Wunderlich89652332013-11-13 19:14:46 +01001080 struct batadv_hard_iface *if_incoming,
1081 struct batadv_hard_iface *if_outgoing)
Marek Lindnerfc957272011-07-30 12:04:12 +02001082{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001083 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1084 struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
Simon Wunderlich89652332013-11-13 19:14:46 +01001085 struct batadv_neigh_ifinfo *neigh_ifinfo;
Marek Lindnerfc957272011-07-30 12:04:12 +02001086 uint8_t total_count;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001087 uint8_t orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
1088 unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001089 int tq_asym_penalty, inv_asym_penalty, if_num, ret = 0;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001090 unsigned int combined_tq;
Marek Lindnerfc957272011-07-30 12:04:12 +02001091
1092 /* find corresponding one hop neighbor */
1093 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001094 hlist_for_each_entry_rcu(tmp_neigh_node,
Marek Lindnerfc957272011-07-30 12:04:12 +02001095 &orig_neigh_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001096 if (!batadv_compare_eth(tmp_neigh_node->addr,
1097 orig_neigh_node->orig))
Marek Lindnerfc957272011-07-30 12:04:12 +02001098 continue;
1099
1100 if (tmp_neigh_node->if_incoming != if_incoming)
1101 continue;
1102
1103 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
1104 continue;
1105
1106 neigh_node = tmp_neigh_node;
1107 break;
1108 }
1109 rcu_read_unlock();
1110
1111 if (!neigh_node)
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001112 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1113 orig_neigh_node->orig,
1114 orig_neigh_node,
Antonio Quartulli863dd7a2013-03-25 13:49:46 +01001115 orig_neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001116
1117 if (!neigh_node)
1118 goto out;
1119
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001120 /* if orig_node is direct neighbor update neigh_node last_seen */
Marek Lindnerfc957272011-07-30 12:04:12 +02001121 if (orig_node == orig_neigh_node)
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001122 neigh_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +02001123
Marek Lindnerd7b2a972012-03-01 15:35:19 +08001124 orig_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +02001125
1126 /* find packet count of corresponding one hop neighbor */
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001127 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1128 if_num = if_incoming->if_num;
1129 orig_eq_count = orig_neigh_node->bat_iv.bcast_own_sum[if_num];
Simon Wunderlich89652332013-11-13 19:14:46 +01001130 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
1131 if (neigh_ifinfo) {
1132 neigh_rq_count = neigh_ifinfo->bat_iv.real_packet_count;
1133 batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
1134 } else {
1135 neigh_rq_count = 0;
1136 }
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001137 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001138
1139 /* pay attention to not get a value bigger than 100 % */
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001140 if (orig_eq_count > neigh_rq_count)
1141 total_count = neigh_rq_count;
1142 else
1143 total_count = orig_eq_count;
Marek Lindnerfc957272011-07-30 12:04:12 +02001144
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001145 /* if we have too few packets (too less data) we set tq_own to zero
1146 * if we receive too few packets it is not considered bidirectional
1147 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001148 if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
1149 neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
Marek Lindnerfc957272011-07-30 12:04:12 +02001150 tq_own = 0;
1151 else
1152 /* neigh_node->real_packet_count is never zero as we
1153 * only purge old information when getting new
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001154 * information
1155 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001156 tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count;
Marek Lindnerfc957272011-07-30 12:04:12 +02001157
Sven Eckelmann21a12362012-03-07 09:07:46 +01001158 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
Marek Lindnerfc957272011-07-30 12:04:12 +02001159 * affect the nearly-symmetric links only a little, but
1160 * punishes asymmetric links more. This will give a value
1161 * between 0 and TQ_MAX_VALUE
1162 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001163 neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
1164 neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
1165 neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
1166 BATADV_TQ_LOCAL_WINDOW_SIZE *
1167 BATADV_TQ_LOCAL_WINDOW_SIZE;
1168 inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
1169 inv_asym_penalty /= neigh_rq_max_cube;
1170 tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;
Marek Lindnerfc957272011-07-30 12:04:12 +02001171
Sven Eckelmann96412692012-06-05 22:31:30 +02001172 combined_tq = batadv_ogm_packet->tq * tq_own * tq_asym_penalty;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001173 combined_tq /= BATADV_TQ_MAX_VALUE * BATADV_TQ_MAX_VALUE;
Sven Eckelmann96412692012-06-05 22:31:30 +02001174 batadv_ogm_packet->tq = combined_tq;
Marek Lindnerfc957272011-07-30 12:04:12 +02001175
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001176 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001177 "bidirectional: orig = %-15pM neigh = %-15pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, total tq: %3i\n",
1178 orig_node->orig, orig_neigh_node->orig, total_count,
1179 neigh_rq_count, tq_own,
Sven Eckelmann96412692012-06-05 22:31:30 +02001180 tq_asym_penalty, batadv_ogm_packet->tq);
Marek Lindnerfc957272011-07-30 12:04:12 +02001181
1182 /* if link has the minimum required transmission quality
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001183 * consider it bidirectional
1184 */
Sven Eckelmann96412692012-06-05 22:31:30 +02001185 if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
Marek Lindnerfc957272011-07-30 12:04:12 +02001186 ret = 1;
1187
1188out:
1189 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001190 batadv_neigh_node_free_ref(neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001191 return ret;
1192}
1193
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001194/**
1195 * batadv_iv_ogm_update_seqnos - process a batman packet for all interfaces,
1196 * adjust the sequence number and find out whether it is a duplicate
1197 * @ethhdr: ethernet header of the packet
1198 * @batadv_ogm_packet: OGM packet to be considered
1199 * @if_incoming: interface on which the OGM packet was received
Simon Wunderlich89652332013-11-13 19:14:46 +01001200 * @if_outgoing: interface for which the retransmission should be considered
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001201 *
1202 * Returns duplicate status as enum batadv_dup_status
Marek Lindnerfc957272011-07-30 12:04:12 +02001203 */
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001204static enum batadv_dup_status
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001205batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +02001206 const struct batadv_ogm_packet *batadv_ogm_packet,
Simon Wunderlich89652332013-11-13 19:14:46 +01001207 const struct batadv_hard_iface *if_incoming,
1208 struct batadv_hard_iface *if_outgoing)
Marek Lindnerfc957272011-07-30 12:04:12 +02001209{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001210 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1211 struct batadv_orig_node *orig_node;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001212 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
Simon Wunderlich89652332013-11-13 19:14:46 +01001213 struct batadv_neigh_node *neigh_node;
1214 struct batadv_neigh_ifinfo *neigh_ifinfo;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001215 int is_dup;
Marek Lindnerfc957272011-07-30 12:04:12 +02001216 int32_t seq_diff;
1217 int need_update = 0;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001218 int set_mark;
1219 enum batadv_dup_status ret = BATADV_NO_DUP;
Sven Eckelmann96412692012-06-05 22:31:30 +02001220 uint32_t seqno = ntohl(batadv_ogm_packet->seqno);
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001221 uint8_t *neigh_addr;
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001222 uint8_t packet_count;
Antonio Quartulli0538f752013-09-02 12:15:01 +02001223 unsigned long *bitmap;
Marek Lindnerfc957272011-07-30 12:04:12 +02001224
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001225 orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
Marek Lindnerfc957272011-07-30 12:04:12 +02001226 if (!orig_node)
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001227 return BATADV_NO_DUP;
Marek Lindnerfc957272011-07-30 12:04:12 +02001228
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001229 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1230 if (WARN_ON(!orig_ifinfo)) {
1231 batadv_orig_node_free_ref(orig_node);
1232 return 0;
1233 }
1234
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001235 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001236 seq_diff = seqno - orig_ifinfo->last_real_seqno;
Marek Lindnerfc957272011-07-30 12:04:12 +02001237
1238 /* signalize caller that the packet is to be dropped. */
Antonio Quartulli1e5cc262012-02-26 15:39:42 +01001239 if (!hlist_empty(&orig_node->neigh_list) &&
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001240 batadv_window_protected(bat_priv, seq_diff,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001241 &orig_ifinfo->batman_seqno_reset)) {
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001242 ret = BATADV_PROTECTED;
Marek Lindnerfc957272011-07-30 12:04:12 +02001243 goto out;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001244 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001245
1246 rcu_read_lock();
Simon Wunderlich89652332013-11-13 19:14:46 +01001247 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1248 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node,
1249 if_outgoing);
1250 if (!neigh_ifinfo)
1251 continue;
1252
1253 neigh_addr = neigh_node->addr;
1254 is_dup = batadv_test_bit(neigh_ifinfo->bat_iv.real_bits,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001255 orig_ifinfo->last_real_seqno,
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001256 seqno);
1257
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001258 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
Simon Wunderlich89652332013-11-13 19:14:46 +01001259 neigh_node->if_incoming == if_incoming) {
Marek Lindnerfc957272011-07-30 12:04:12 +02001260 set_mark = 1;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001261 if (is_dup)
1262 ret = BATADV_NEIGH_DUP;
1263 } else {
Marek Lindnerfc957272011-07-30 12:04:12 +02001264 set_mark = 0;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001265 if (is_dup && (ret != BATADV_NEIGH_DUP))
1266 ret = BATADV_ORIG_DUP;
1267 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001268
1269 /* if the window moved, set the update flag. */
Simon Wunderlich89652332013-11-13 19:14:46 +01001270 bitmap = neigh_ifinfo->bat_iv.real_bits;
Antonio Quartulli0538f752013-09-02 12:15:01 +02001271 need_update |= batadv_bit_get_packet(bat_priv, bitmap,
Sven Eckelmann0f5f9322012-05-12 02:09:25 +02001272 seq_diff, set_mark);
Marek Lindnerfc957272011-07-30 12:04:12 +02001273
Simon Wunderlich89652332013-11-13 19:14:46 +01001274 packet_count = bitmap_weight(bitmap,
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001275 BATADV_TQ_LOCAL_WINDOW_SIZE);
Simon Wunderlich89652332013-11-13 19:14:46 +01001276 neigh_ifinfo->bat_iv.real_packet_count = packet_count;
1277 batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
Marek Lindnerfc957272011-07-30 12:04:12 +02001278 }
1279 rcu_read_unlock();
1280
1281 if (need_update) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001282 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001283 "%s updating last_seqno: old %u, new %u\n",
1284 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT",
1285 orig_ifinfo->last_real_seqno, seqno);
1286 orig_ifinfo->last_real_seqno = seqno;
Marek Lindnerfc957272011-07-30 12:04:12 +02001287 }
1288
Marek Lindnerfc957272011-07-30 12:04:12 +02001289out:
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001290 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001291 batadv_orig_node_free_ref(orig_node);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001292 if (orig_ifinfo)
1293 batadv_orig_ifinfo_free_ref(orig_ifinfo);
Marek Lindnerfc957272011-07-30 12:04:12 +02001294 return ret;
1295}
1296
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001297
1298/**
1299 * batadv_iv_ogm_process_per_outif - process a batman iv OGM for an outgoing if
1300 * @skb: the skb containing the OGM
1301 * @orig_node: the (cached) orig node for the originator of this OGM
1302 * @if_incoming: the interface where this packet was received
1303 * @if_outgoing: the interface for which the packet should be considered
1304 */
1305static void
1306batadv_iv_ogm_process_per_outif(const struct sk_buff *skb, int ogm_offset,
1307 struct batadv_orig_node *orig_node,
1308 struct batadv_hard_iface *if_incoming,
1309 struct batadv_hard_iface *if_outgoing)
1310{
1311 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1312 struct batadv_neigh_node *router = NULL, *router_router = NULL;
1313 struct batadv_orig_node *orig_neigh_node;
1314 struct batadv_orig_ifinfo *orig_ifinfo;
1315 struct batadv_neigh_node *orig_neigh_router = NULL;
1316 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
1317 struct batadv_ogm_packet *ogm_packet;
1318 enum batadv_dup_status dup_status;
1319 bool is_from_best_next_hop = false;
1320 bool is_single_hop_neigh = false;
1321 bool sameseq, similar_ttl;
1322 struct sk_buff *skb_priv;
1323 struct ethhdr *ethhdr;
1324 uint8_t *prev_sender;
1325 int is_bidirect;
1326
1327 /* create a private copy of the skb, as some functions change tq value
1328 * and/or flags.
1329 */
1330 skb_priv = skb_copy(skb, GFP_ATOMIC);
1331 if (!skb_priv)
1332 return;
1333
1334 ethhdr = eth_hdr(skb_priv);
1335 ogm_packet = (struct batadv_ogm_packet *)(skb_priv->data + ogm_offset);
1336
1337 dup_status = batadv_iv_ogm_update_seqnos(ethhdr, ogm_packet,
1338 if_incoming, if_outgoing);
1339 if (batadv_compare_eth(ethhdr->h_source, ogm_packet->orig))
1340 is_single_hop_neigh = true;
1341
1342 if (dup_status == BATADV_PROTECTED) {
1343 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1344 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1345 ethhdr->h_source);
1346 goto out;
1347 }
1348
1349 if (ogm_packet->tq == 0) {
1350 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1351 "Drop packet: originator packet with tq equal 0\n");
1352 goto out;
1353 }
1354
1355 router = batadv_orig_router_get(orig_node, if_outgoing);
1356 if (router) {
1357 router_router = batadv_orig_router_get(router->orig_node,
1358 if_outgoing);
1359 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1360 }
1361
1362 if ((router_ifinfo && router_ifinfo->bat_iv.tq_avg != 0) &&
1363 (batadv_compare_eth(router->addr, ethhdr->h_source)))
1364 is_from_best_next_hop = true;
1365
1366 prev_sender = ogm_packet->prev_sender;
1367 /* avoid temporary routing loops */
1368 if (router && router_router &&
1369 (batadv_compare_eth(router->addr, prev_sender)) &&
1370 !(batadv_compare_eth(ogm_packet->orig, prev_sender)) &&
1371 (batadv_compare_eth(router->addr, router_router->addr))) {
1372 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1373 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1374 ethhdr->h_source);
1375 goto out;
1376 }
1377
1378 if (if_outgoing == BATADV_IF_DEFAULT)
1379 batadv_tvlv_ogm_receive(bat_priv, ogm_packet, orig_node);
1380
1381 /* if sender is a direct neighbor the sender mac equals
1382 * originator mac
1383 */
1384 if (is_single_hop_neigh)
1385 orig_neigh_node = orig_node;
1386 else
1387 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1388 ethhdr->h_source);
1389
1390 if (!orig_neigh_node)
1391 goto out;
1392
1393 /* Update nc_nodes of the originator */
1394 batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node,
1395 ogm_packet, is_single_hop_neigh);
1396
1397 orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
1398 if_outgoing);
1399
1400 /* drop packet if sender is not a direct neighbor and if we
1401 * don't route towards it
1402 */
1403 if (!is_single_hop_neigh && (!orig_neigh_router)) {
1404 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1405 "Drop packet: OGM via unknown neighbor!\n");
1406 goto out_neigh;
1407 }
1408
1409 is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
1410 ogm_packet, if_incoming,
1411 if_outgoing);
1412
1413 /* update ranking if it is not a duplicate or has the same
1414 * seqno and similar ttl as the non-duplicate
1415 */
1416 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1417 if (!orig_ifinfo)
1418 goto out_neigh;
1419
1420 sameseq = orig_ifinfo->last_real_seqno == ntohl(ogm_packet->seqno);
1421 similar_ttl = (orig_ifinfo->last_ttl - 3) <= ogm_packet->ttl;
1422
1423 if (is_bidirect && ((dup_status == BATADV_NO_DUP) ||
1424 (sameseq && similar_ttl))) {
1425 batadv_iv_ogm_orig_update(bat_priv, orig_node,
1426 orig_ifinfo, ethhdr,
1427 ogm_packet, if_incoming,
1428 if_outgoing, dup_status);
1429 }
1430 batadv_orig_ifinfo_free_ref(orig_ifinfo);
1431
1432 /* is single hop (direct) neighbor */
1433 if (is_single_hop_neigh) {
1434 /* OGMs from secondary interfaces should only scheduled once
1435 * per interface where it has been received, not multiple times
1436 */
1437 if ((ogm_packet->ttl <= 2) &&
1438 (if_incoming != if_outgoing)) {
1439 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1440 "Drop packet: OGM from secondary interface and wrong outgoing interface\n");
1441 goto out_neigh;
1442 }
1443 /* mark direct link on incoming interface */
1444 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1445 is_single_hop_neigh,
1446 is_from_best_next_hop, if_incoming);
1447
1448 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1449 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
1450 goto out_neigh;
1451 }
1452
1453 /* multihop originator */
1454 if (!is_bidirect) {
1455 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1456 "Drop packet: not received via bidirectional link\n");
1457 goto out_neigh;
1458 }
1459
1460 if (dup_status == BATADV_NEIGH_DUP) {
1461 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1462 "Drop packet: duplicate packet received\n");
1463 goto out_neigh;
1464 }
1465
1466 /* only forward the packet on the default interface until the
1467 * OGM forwarding has been reworked to send on specific interfaces.
1468 */
1469 if (if_outgoing != BATADV_IF_DEFAULT)
1470 goto out_neigh;
1471 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1472 "Forwarding packet: rebroadcast originator packet\n");
1473 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1474 is_single_hop_neigh, is_from_best_next_hop,
1475 if_incoming);
1476
1477out_neigh:
1478 if ((orig_neigh_node) && (!is_single_hop_neigh))
1479 batadv_orig_node_free_ref(orig_neigh_node);
1480out:
1481 if (router)
1482 batadv_neigh_node_free_ref(router);
1483 if (router_router)
1484 batadv_neigh_node_free_ref(router_router);
1485 if (orig_neigh_router)
1486 batadv_neigh_node_free_ref(orig_neigh_router);
1487
1488 kfree_skb(skb_priv);
1489}
1490
1491/**
1492 * batadv_iv_ogm_process - process an incoming batman iv OGM
1493 * @skb: the skb containing the OGM
1494 * @ogm_offset: offset to the OGM which should be processed (for aggregates)
1495 * @if_incoming: the interface where this packet was receved
1496 */
1497static void batadv_iv_ogm_process(const struct sk_buff *skb, int ogm_offset,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001498 struct batadv_hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +02001499{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001500 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001501 struct batadv_orig_node *orig_neigh_node, *orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001502 struct batadv_hard_iface *hard_iface;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001503 struct batadv_ogm_packet *ogm_packet;
Marek Lindnerfc957272011-07-30 12:04:12 +02001504 uint32_t if_incoming_seqno;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001505 bool has_directlink_flag;
1506 struct ethhdr *ethhdr;
1507 bool is_my_oldorig = false;
1508 bool is_my_addr = false;
1509 bool is_my_orig = false;
1510
1511 ogm_packet = (struct batadv_ogm_packet *)(skb->data + ogm_offset);
1512 ethhdr = eth_hdr(skb);
Marek Lindnerfc957272011-07-30 12:04:12 +02001513
1514 /* Silently drop when the batman packet is actually not a
1515 * correct packet.
1516 *
1517 * This might happen if a packet is padded (e.g. Ethernet has a
1518 * minimum frame length of 64 byte) and the aggregation interprets
1519 * it as an additional length.
1520 *
1521 * TODO: A more sane solution would be to have a bit in the
Sven Eckelmann96412692012-06-05 22:31:30 +02001522 * batadv_ogm_packet to detect whether the packet is the last
Marek Lindnerfc957272011-07-30 12:04:12 +02001523 * packet in an aggregation. Here we expect that the padding
1524 * is always zero (or not 0x01)
1525 */
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001526 if (ogm_packet->packet_type != BATADV_IV_OGM)
Marek Lindnerfc957272011-07-30 12:04:12 +02001527 return;
1528
1529 /* could be changed by schedule_own_packet() */
Marek Lindner14511512012-08-02 17:20:26 +02001530 if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno);
Marek Lindnerfc957272011-07-30 12:04:12 +02001531
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001532 if (ogm_packet->flags & BATADV_DIRECTLINK)
1533 has_directlink_flag = true;
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001534 else
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001535 has_directlink_flag = false;
Marek Lindnerfc957272011-07-30 12:04:12 +02001536
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001537 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001538 "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 +02001539 ethhdr->h_source, if_incoming->net_dev->name,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001540 if_incoming->net_dev->dev_addr, ogm_packet->orig,
1541 ogm_packet->prev_sender, ntohl(ogm_packet->seqno),
1542 ogm_packet->tq, ogm_packet->ttl,
1543 ogm_packet->version, has_directlink_flag);
Marek Lindnerfc957272011-07-30 12:04:12 +02001544
1545 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001546 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001547 if (hard_iface->if_status != BATADV_IF_ACTIVE)
Marek Lindnerfc957272011-07-30 12:04:12 +02001548 continue;
1549
1550 if (hard_iface->soft_iface != if_incoming->soft_iface)
1551 continue;
1552
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001553 if (batadv_compare_eth(ethhdr->h_source,
1554 hard_iface->net_dev->dev_addr))
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001555 is_my_addr = true;
Marek Lindnerfc957272011-07-30 12:04:12 +02001556
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001557 if (batadv_compare_eth(ogm_packet->orig,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001558 hard_iface->net_dev->dev_addr))
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001559 is_my_orig = true;
Marek Lindnerfc957272011-07-30 12:04:12 +02001560
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001561 if (batadv_compare_eth(ogm_packet->prev_sender,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001562 hard_iface->net_dev->dev_addr))
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001563 is_my_oldorig = true;
Marek Lindnerfc957272011-07-30 12:04:12 +02001564 }
1565 rcu_read_unlock();
1566
Marek Lindnerfc957272011-07-30 12:04:12 +02001567 if (is_my_addr) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001568 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001569 "Drop packet: received my own broadcast (sender: %pM)\n",
1570 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001571 return;
1572 }
1573
Marek Lindnerfc957272011-07-30 12:04:12 +02001574 if (is_my_orig) {
1575 unsigned long *word;
1576 int offset;
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001577 int32_t bit_pos;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001578 int16_t if_num;
1579 uint8_t *weight;
Marek Lindnerfc957272011-07-30 12:04:12 +02001580
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001581 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1582 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001583 if (!orig_neigh_node)
1584 return;
1585
1586 /* neighbor has to indicate direct link and it has to
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001587 * come via the corresponding interface
1588 * save packet seqno for bidirectional check
1589 */
Marek Lindnerfc957272011-07-30 12:04:12 +02001590 if (has_directlink_flag &&
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001591 batadv_compare_eth(if_incoming->net_dev->dev_addr,
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001592 ogm_packet->orig)) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001593 if_num = if_incoming->if_num;
1594 offset = if_num * BATADV_NUM_WORDS;
Marek Lindnerfc957272011-07-30 12:04:12 +02001595
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001596 spin_lock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
1597 word = &(orig_neigh_node->bat_iv.bcast_own[offset]);
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001598 bit_pos = if_incoming_seqno - 2;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001599 bit_pos -= ntohl(ogm_packet->seqno);
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001600 batadv_set_bit(word, bit_pos);
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001601 weight = &orig_neigh_node->bat_iv.bcast_own_sum[if_num];
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001602 *weight = bitmap_weight(word,
1603 BATADV_TQ_LOCAL_WINDOW_SIZE);
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001604 spin_unlock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +02001605 }
1606
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001607 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001608 "Drop packet: originator packet from myself (via neighbor)\n");
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001609 batadv_orig_node_free_ref(orig_neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001610 return;
1611 }
1612
1613 if (is_my_oldorig) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001614 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001615 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1616 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001617 return;
1618 }
1619
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001620 if (ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001621 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001622 "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1623 ethhdr->h_source);
Marek Lindner13b25412012-03-11 06:17:53 +08001624 return;
1625 }
1626
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001627 orig_node = batadv_iv_ogm_orig_get(bat_priv, ogm_packet->orig);
Marek Lindnerfc957272011-07-30 12:04:12 +02001628 if (!orig_node)
1629 return;
1630
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001631 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1632 if_incoming, BATADV_IF_DEFAULT);
Marek Lindnerfc957272011-07-30 12:04:12 +02001633
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001634 rcu_read_lock();
1635 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1636 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1637 continue;
1638
1639 if (hard_iface->soft_iface != bat_priv->soft_iface)
1640 continue;
1641
1642 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1643 if_incoming, hard_iface);
Marek Lindnerfc957272011-07-30 12:04:12 +02001644 }
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001645 rcu_read_unlock();
Marek Lindnerfc957272011-07-30 12:04:12 +02001646
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001647 batadv_orig_node_free_ref(orig_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001648}
1649
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001650static int batadv_iv_ogm_receive(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001651 struct batadv_hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +02001652{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001653 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001654 struct batadv_ogm_packet *ogm_packet;
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001655 uint8_t *packet_pos;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001656 int ogm_offset;
Marek Lindneref261572013-04-23 21:39:57 +08001657 bool ret;
Marek Lindnerc3e29312012-03-04 16:56:25 +08001658
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001659 ret = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
Marek Lindnerc3e29312012-03-04 16:56:25 +08001660 if (!ret)
1661 return NET_RX_DROP;
Marek Lindnerfc957272011-07-30 12:04:12 +02001662
Marek Lindneredbf12b2012-03-11 06:17:49 +08001663 /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
1664 * that does not have B.A.T.M.A.N. IV enabled ?
1665 */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001666 if (bat_priv->bat_algo_ops->bat_ogm_emit != batadv_iv_ogm_emit)
Marek Lindneredbf12b2012-03-11 06:17:49 +08001667 return NET_RX_DROP;
1668
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001669 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
1670 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
Martin Hundebøllf8214862012-04-20 17:02:45 +02001671 skb->len + ETH_HLEN);
1672
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001673 ogm_offset = 0;
1674 ogm_packet = (struct batadv_ogm_packet *)skb->data;
Marek Lindnerfc957272011-07-30 12:04:12 +02001675
1676 /* unpack the aggregated packets and process them one by one */
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001677 while (batadv_iv_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
1678 ogm_packet->tvlv_len)) {
1679 batadv_iv_ogm_process(skb, ogm_offset, if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001680
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001681 ogm_offset += BATADV_OGM_HLEN;
1682 ogm_offset += ntohs(ogm_packet->tvlv_len);
Marek Lindnerfc957272011-07-30 12:04:12 +02001683
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001684 packet_pos = skb->data + ogm_offset;
1685 ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Marek Lindnerb47506d2013-03-04 10:39:49 +08001686 }
Marek Lindnerc3e29312012-03-04 16:56:25 +08001687
1688 kfree_skb(skb);
1689 return NET_RX_SUCCESS;
Marek Lindnerfc957272011-07-30 12:04:12 +02001690}
Marek Lindner1c280472011-11-28 17:40:17 +08001691
Simon Wunderlich89652332013-11-13 19:14:46 +01001692/* batadv_iv_ogm_orig_print_neigh - print neighbors for the originator table
1693 * @orig_node: the orig_node for which the neighbors are printed
1694 * @if_outgoing: outgoing interface for these entries
1695 * @seq: debugfs table seq_file struct
1696 *
1697 * Must be called while holding an rcu lock.
1698 */
1699static void
1700batadv_iv_ogm_orig_print_neigh(struct batadv_orig_node *orig_node,
1701 struct batadv_hard_iface *if_outgoing,
1702 struct seq_file *seq)
1703{
1704 struct batadv_neigh_node *neigh_node;
1705 struct batadv_neigh_ifinfo *n_ifinfo;
1706
1707 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1708 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
1709 if (!n_ifinfo)
1710 continue;
1711
1712 seq_printf(seq, " %pM (%3i)",
1713 neigh_node->addr,
1714 n_ifinfo->bat_iv.tq_avg);
1715
1716 batadv_neigh_ifinfo_free_ref(n_ifinfo);
1717 }
1718}
1719
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001720/**
1721 * batadv_iv_ogm_orig_print - print the originator table
1722 * @bat_priv: the bat priv with all the soft interface information
1723 * @seq: debugfs table seq_file struct
1724 */
1725static void batadv_iv_ogm_orig_print(struct batadv_priv *bat_priv,
1726 struct seq_file *seq)
1727{
Simon Wunderlich89652332013-11-13 19:14:46 +01001728 struct batadv_neigh_node *neigh_node;
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001729 struct batadv_hashtable *hash = bat_priv->orig_hash;
1730 int last_seen_msecs, last_seen_secs;
1731 struct batadv_orig_node *orig_node;
Simon Wunderlich89652332013-11-13 19:14:46 +01001732 struct batadv_neigh_ifinfo *n_ifinfo;
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001733 unsigned long last_seen_jiffies;
1734 struct hlist_head *head;
1735 int batman_count = 0;
1736 uint32_t i;
1737
1738 seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
1739 "Originator", "last-seen", "#", BATADV_TQ_MAX_VALUE,
1740 "Nexthop", "outgoingIF", "Potential nexthops");
1741
1742 for (i = 0; i < hash->size; i++) {
1743 head = &hash->table[i];
1744
1745 rcu_read_lock();
1746 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001747 neigh_node = batadv_orig_router_get(orig_node,
1748 BATADV_IF_DEFAULT);
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001749 if (!neigh_node)
1750 continue;
1751
Simon Wunderlich89652332013-11-13 19:14:46 +01001752 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node,
1753 BATADV_IF_DEFAULT);
1754 if (!n_ifinfo)
1755 goto next;
1756
1757 if (n_ifinfo->bat_iv.tq_avg == 0)
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001758 goto next;
1759
1760 last_seen_jiffies = jiffies - orig_node->last_seen;
1761 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
1762 last_seen_secs = last_seen_msecs / 1000;
1763 last_seen_msecs = last_seen_msecs % 1000;
1764
1765 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
1766 orig_node->orig, last_seen_secs,
Simon Wunderlich89652332013-11-13 19:14:46 +01001767 last_seen_msecs, n_ifinfo->bat_iv.tq_avg,
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001768 neigh_node->addr,
1769 neigh_node->if_incoming->net_dev->name);
1770
Simon Wunderlich89652332013-11-13 19:14:46 +01001771 batadv_iv_ogm_orig_print_neigh(orig_node,
1772 BATADV_IF_DEFAULT, seq);
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001773 seq_puts(seq, "\n");
1774 batman_count++;
1775
1776next:
1777 batadv_neigh_node_free_ref(neigh_node);
Simon Wunderlich89652332013-11-13 19:14:46 +01001778 if (n_ifinfo)
1779 batadv_neigh_ifinfo_free_ref(n_ifinfo);
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001780 }
1781 rcu_read_unlock();
1782 }
1783
1784 if (batman_count == 0)
1785 seq_puts(seq, "No batman nodes in range ...\n");
1786}
1787
Antonio Quartullia3285a82013-09-02 12:15:04 +02001788/**
1789 * batadv_iv_ogm_neigh_cmp - compare the metrics of two neighbors
1790 * @neigh1: the first neighbor object of the comparison
Simon Wunderlich89652332013-11-13 19:14:46 +01001791 * @if_outgoing1: outgoing interface for the first neighbor
Antonio Quartullia3285a82013-09-02 12:15:04 +02001792 * @neigh2: the second neighbor object of the comparison
Simon Wunderlich89652332013-11-13 19:14:46 +01001793 * @if_outgoing2: outgoing interface for the second neighbor
Antonio Quartullia3285a82013-09-02 12:15:04 +02001794 *
1795 * Returns a value less, equal to or greater than 0 if the metric via neigh1 is
1796 * lower, the same as or higher than the metric via neigh2
1797 */
1798static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1,
Simon Wunderlich89652332013-11-13 19:14:46 +01001799 struct batadv_hard_iface *if_outgoing1,
1800 struct batadv_neigh_node *neigh2,
1801 struct batadv_hard_iface *if_outgoing2)
Antonio Quartullia3285a82013-09-02 12:15:04 +02001802{
Simon Wunderlich89652332013-11-13 19:14:46 +01001803 struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
Antonio Quartullia3285a82013-09-02 12:15:04 +02001804 uint8_t tq1, tq2;
Simon Wunderlich89652332013-11-13 19:14:46 +01001805 int diff;
Antonio Quartullia3285a82013-09-02 12:15:04 +02001806
Simon Wunderlich89652332013-11-13 19:14:46 +01001807 neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
1808 neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
Antonio Quartullia3285a82013-09-02 12:15:04 +02001809
Simon Wunderlich89652332013-11-13 19:14:46 +01001810 if (!neigh1_ifinfo || !neigh2_ifinfo) {
1811 diff = 0;
1812 goto out;
1813 }
1814
1815 tq1 = neigh1_ifinfo->bat_iv.tq_avg;
1816 tq2 = neigh2_ifinfo->bat_iv.tq_avg;
1817 diff = tq1 - tq2;
1818
1819out:
1820 if (neigh1_ifinfo)
1821 batadv_neigh_ifinfo_free_ref(neigh1_ifinfo);
1822 if (neigh2_ifinfo)
1823 batadv_neigh_ifinfo_free_ref(neigh2_ifinfo);
1824
1825 return diff;
Antonio Quartullia3285a82013-09-02 12:15:04 +02001826}
1827
Antonio Quartullic43c9812013-09-02 12:15:05 +02001828/**
1829 * batadv_iv_ogm_neigh_is_eob - check if neigh1 is equally good or better than
1830 * neigh2 from the metric prospective
1831 * @neigh1: the first neighbor object of the comparison
Simon Wunderlich89652332013-11-13 19:14:46 +01001832 * @if_outgoing: outgoing interface for the first neighbor
Antonio Quartullic43c9812013-09-02 12:15:05 +02001833 * @neigh2: the second neighbor object of the comparison
Simon Wunderlich89652332013-11-13 19:14:46 +01001834 * @if_outgoing2: outgoing interface for the second neighbor
Antonio Quartullic43c9812013-09-02 12:15:05 +02001835
Simon Wunderlich89652332013-11-13 19:14:46 +01001836 * Returns true if the metric via neigh1 is equally good or better than
1837 * the metric via neigh2, false otherwise.
1838 */
1839static bool
1840batadv_iv_ogm_neigh_is_eob(struct batadv_neigh_node *neigh1,
1841 struct batadv_hard_iface *if_outgoing1,
1842 struct batadv_neigh_node *neigh2,
1843 struct batadv_hard_iface *if_outgoing2)
1844{
1845 struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
1846 uint8_t tq1, tq2;
1847 bool ret;
1848
1849 neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
1850 neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
1851
1852 /* we can't say that the metric is better */
1853 if (!neigh1_ifinfo || !neigh2_ifinfo) {
1854 ret = false;
1855 goto out;
1856 }
1857
1858 tq1 = neigh1_ifinfo->bat_iv.tq_avg;
1859 tq2 = neigh2_ifinfo->bat_iv.tq_avg;
1860 ret = (tq1 - tq2) > -BATADV_TQ_SIMILARITY_THRESHOLD;
1861
1862out:
1863 if (neigh1_ifinfo)
1864 batadv_neigh_ifinfo_free_ref(neigh1_ifinfo);
1865 if (neigh2_ifinfo)
1866 batadv_neigh_ifinfo_free_ref(neigh2_ifinfo);
1867
1868 return ret;
Antonio Quartullic43c9812013-09-02 12:15:05 +02001869}
1870
Sven Eckelmann56303d32012-06-05 22:31:31 +02001871static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
Marek Lindner519d3492012-04-18 17:15:57 +08001872 .name = "BATMAN_IV",
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001873 .bat_iface_enable = batadv_iv_ogm_iface_enable,
1874 .bat_iface_disable = batadv_iv_ogm_iface_disable,
1875 .bat_iface_update_mac = batadv_iv_ogm_iface_update_mac,
1876 .bat_primary_iface_set = batadv_iv_ogm_primary_iface_set,
1877 .bat_ogm_schedule = batadv_iv_ogm_schedule,
1878 .bat_ogm_emit = batadv_iv_ogm_emit,
Antonio Quartullia3285a82013-09-02 12:15:04 +02001879 .bat_neigh_cmp = batadv_iv_ogm_neigh_cmp,
Antonio Quartullic43c9812013-09-02 12:15:05 +02001880 .bat_neigh_is_equiv_or_better = batadv_iv_ogm_neigh_is_eob,
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001881 .bat_orig_print = batadv_iv_ogm_orig_print,
Antonio Quartullid0015fd2013-09-03 11:10:23 +02001882 .bat_orig_free = batadv_iv_ogm_orig_free,
1883 .bat_orig_add_if = batadv_iv_ogm_orig_add_if,
1884 .bat_orig_del_if = batadv_iv_ogm_orig_del_if,
Marek Lindner1c280472011-11-28 17:40:17 +08001885};
1886
Sven Eckelmann81c524f2012-05-12 02:09:22 +02001887int __init batadv_iv_init(void)
Marek Lindner1c280472011-11-28 17:40:17 +08001888{
Marek Lindnerc3e29312012-03-04 16:56:25 +08001889 int ret;
1890
1891 /* batman originator packet */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001892 ret = batadv_recv_handler_register(BATADV_IV_OGM,
1893 batadv_iv_ogm_receive);
Marek Lindnerc3e29312012-03-04 16:56:25 +08001894 if (ret < 0)
1895 goto out;
1896
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001897 ret = batadv_algo_register(&batadv_batman_iv);
Marek Lindnerc3e29312012-03-04 16:56:25 +08001898 if (ret < 0)
1899 goto handler_unregister;
1900
1901 goto out;
1902
1903handler_unregister:
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001904 batadv_recv_handler_unregister(BATADV_IV_OGM);
Marek Lindnerc3e29312012-03-04 16:56:25 +08001905out:
1906 return ret;
Marek Lindner1c280472011-11-28 17:40:17 +08001907}