blob: ec3542c3bf0ca026ba0b6673d1da44e198a5d40f [file] [log] [blame]
Marek Lindnerfc957272011-07-30 12:04:12 +02001/*
Sven Eckelmann567db7b2012-01-01 00:41:38 +01002 * Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
Marek Lindnerfc957272011-07-30 12:04:12 +02003 *
4 * Marek Lindner, Simon Wunderlich
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301, USA
19 *
20 */
21
22#include "main.h"
Marek Lindnerfc957272011-07-30 12:04:12 +020023#include "translation-table.h"
24#include "ring_buffer.h"
25#include "originator.h"
26#include "routing.h"
27#include "gateway_common.h"
28#include "gateway_client.h"
29#include "hard-interface.h"
30#include "send.h"
Marek Lindner1c280472011-11-28 17:40:17 +080031#include "bat_algo.h"
Marek Lindnerfc957272011-07-30 12:04:12 +020032
Marek Lindner7ae8b282012-03-01 15:35:21 +080033static struct neigh_node *bat_iv_ogm_neigh_new(struct hard_iface *hard_iface,
34 const uint8_t *neigh_addr,
35 struct orig_node *orig_node,
36 struct orig_node *orig_neigh,
Al Viroe0f52112012-04-22 07:46:29 +010037 __be32 seqno)
Marek Lindner7ae8b282012-03-01 15:35:21 +080038{
39 struct neigh_node *neigh_node;
40
Al Viroe0f52112012-04-22 07:46:29 +010041 neigh_node = batadv_neigh_node_new(hard_iface, neigh_addr,
42 ntohl(seqno));
Marek Lindner7ae8b282012-03-01 15:35:21 +080043 if (!neigh_node)
44 goto out;
45
46 INIT_LIST_HEAD(&neigh_node->bonding_list);
Marek Lindner7ae8b282012-03-01 15:35:21 +080047
48 neigh_node->orig_node = orig_neigh;
49 neigh_node->if_incoming = hard_iface;
50
51 spin_lock_bh(&orig_node->neigh_list_lock);
52 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
53 spin_unlock_bh(&orig_node->neigh_list_lock);
54
55out:
56 return neigh_node;
57}
58
Marek Lindner77af7572012-02-07 17:20:48 +080059static int bat_iv_ogm_iface_enable(struct hard_iface *hard_iface)
Marek Lindnerd0b9fd82011-07-30 12:33:33 +020060{
61 struct batman_ogm_packet *batman_ogm_packet;
Marek Lindnerd7d32ec2012-02-07 17:20:46 +080062 uint32_t random_seqno;
Sven Eckelmann5346c352012-05-05 13:27:28 +020063 int res = -ENOMEM;
Marek Lindnerd7d32ec2012-02-07 17:20:46 +080064
65 /* randomize initial seqno to avoid collision */
66 get_random_bytes(&random_seqno, sizeof(random_seqno));
67 atomic_set(&hard_iface->seqno, random_seqno);
Marek Lindnerd0b9fd82011-07-30 12:33:33 +020068
Marek Lindner76e3d7f2012-02-07 17:20:50 +080069 hard_iface->packet_len = BATMAN_OGM_HLEN;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +020070 hard_iface->packet_buff = kmalloc(hard_iface->packet_len, GFP_ATOMIC);
71
Marek Lindner77af7572012-02-07 17:20:48 +080072 if (!hard_iface->packet_buff)
73 goto out;
74
Marek Lindnerd0b9fd82011-07-30 12:33:33 +020075 batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff;
Marek Lindner1eeb4792012-02-07 17:20:51 +080076 batman_ogm_packet->header.packet_type = BAT_IV_OGM;
Sven Eckelmann76543d12011-11-20 15:47:38 +010077 batman_ogm_packet->header.version = COMPAT_VERSION;
78 batman_ogm_packet->header.ttl = 2;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +020079 batman_ogm_packet->flags = NO_FLAGS;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +020080 batman_ogm_packet->tq = TQ_MAX_VALUE;
81 batman_ogm_packet->tt_num_changes = 0;
82 batman_ogm_packet->ttvn = 0;
Marek Lindner77af7572012-02-07 17:20:48 +080083
84 res = 0;
85
86out:
87 return res;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +020088}
89
Marek Lindner00a50072012-02-07 17:20:47 +080090static void bat_iv_ogm_iface_disable(struct hard_iface *hard_iface)
91{
92 kfree(hard_iface->packet_buff);
93 hard_iface->packet_buff = NULL;
94}
95
Marek Lindnerc3229392012-03-11 06:17:50 +080096static void bat_iv_ogm_iface_update_mac(struct hard_iface *hard_iface)
Marek Lindnerd0b9fd82011-07-30 12:33:33 +020097{
98 struct batman_ogm_packet *batman_ogm_packet;
99
100 batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff;
101 memcpy(batman_ogm_packet->orig,
102 hard_iface->net_dev->dev_addr, ETH_ALEN);
103 memcpy(batman_ogm_packet->prev_sender,
104 hard_iface->net_dev->dev_addr, ETH_ALEN);
105}
106
Marek Lindnerc3229392012-03-11 06:17:50 +0800107static void bat_iv_ogm_primary_iface_set(struct hard_iface *hard_iface)
108{
109 struct batman_ogm_packet *batman_ogm_packet;
110
111 batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff;
112 batman_ogm_packet->flags = PRIMARIES_FIRST_HOP;
113 batman_ogm_packet->header.ttl = TTL;
114}
115
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200116/* when do we schedule our own ogm to be sent */
Marek Lindner01c42242011-11-28 21:31:55 +0800117static unsigned long bat_iv_ogm_emit_send_time(const struct bat_priv *bat_priv)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200118{
119 return jiffies + msecs_to_jiffies(
120 atomic_read(&bat_priv->orig_interval) -
121 JITTER + (random32() % 2*JITTER));
122}
123
124/* when do we schedule a ogm packet to be sent */
Marek Lindner01c42242011-11-28 21:31:55 +0800125static unsigned long bat_iv_ogm_fwd_send_time(void)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200126{
127 return jiffies + msecs_to_jiffies(random32() % (JITTER/2));
128}
129
130/* apply hop penalty for a normal link */
131static uint8_t hop_penalty(uint8_t tq, const struct bat_priv *bat_priv)
132{
133 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
134 return (tq * (TQ_MAX_VALUE - hop_penalty)) / (TQ_MAX_VALUE);
135}
136
Marek Lindnerfc957272011-07-30 12:04:12 +0200137/* is there another aggregated packet here? */
Marek Lindner01c42242011-11-28 21:31:55 +0800138static int bat_iv_ogm_aggr_packet(int buff_pos, int packet_len,
139 int tt_num_changes)
Marek Lindnerfc957272011-07-30 12:04:12 +0200140{
Marek Lindner76e3d7f2012-02-07 17:20:50 +0800141 int next_buff_pos = buff_pos + BATMAN_OGM_HLEN + tt_len(tt_num_changes);
Marek Lindnerfc957272011-07-30 12:04:12 +0200142
143 return (next_buff_pos <= packet_len) &&
144 (next_buff_pos <= MAX_AGGREGATION_BYTES);
145}
146
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200147/* send a batman ogm to a given interface */
Marek Lindner01c42242011-11-28 21:31:55 +0800148static void bat_iv_ogm_send_to_if(struct forw_packet *forw_packet,
149 struct hard_iface *hard_iface)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200150{
151 struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
152 char *fwd_str;
153 uint8_t packet_num;
154 int16_t buff_pos;
155 struct batman_ogm_packet *batman_ogm_packet;
156 struct sk_buff *skb;
157
158 if (hard_iface->if_status != IF_ACTIVE)
159 return;
160
161 packet_num = 0;
162 buff_pos = 0;
163 batman_ogm_packet = (struct batman_ogm_packet *)forw_packet->skb->data;
164
165 /* adjust all flags and log packets */
Marek Lindner01c42242011-11-28 21:31:55 +0800166 while (bat_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
167 batman_ogm_packet->tt_num_changes)) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200168
169 /* we might have aggregated direct link packets with an
170 * ordinary base packet */
171 if ((forw_packet->direct_link_flags & (1 << packet_num)) &&
172 (forw_packet->if_incoming == hard_iface))
173 batman_ogm_packet->flags |= DIRECTLINK;
174 else
175 batman_ogm_packet->flags &= ~DIRECTLINK;
176
177 fwd_str = (packet_num > 0 ? "Forwarding" : (forw_packet->own ?
178 "Sending own" :
179 "Forwarding"));
180 bat_dbg(DBG_BATMAN, bat_priv,
Antonio Quartullic97c72b2012-02-26 15:39:41 +0100181 "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s, ttvn %d) on interface %s [%pM]\n",
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200182 fwd_str, (packet_num > 0 ? "aggregated " : ""),
183 batman_ogm_packet->orig,
184 ntohl(batman_ogm_packet->seqno),
Sven Eckelmann76543d12011-11-20 15:47:38 +0100185 batman_ogm_packet->tq, batman_ogm_packet->header.ttl,
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200186 (batman_ogm_packet->flags & DIRECTLINK ?
187 "on" : "off"),
188 batman_ogm_packet->ttvn, hard_iface->net_dev->name,
189 hard_iface->net_dev->dev_addr);
190
Marek Lindner76e3d7f2012-02-07 17:20:50 +0800191 buff_pos += BATMAN_OGM_HLEN +
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200192 tt_len(batman_ogm_packet->tt_num_changes);
193 packet_num++;
194 batman_ogm_packet = (struct batman_ogm_packet *)
195 (forw_packet->skb->data + buff_pos);
196 }
197
198 /* create clone because function is called more than once */
199 skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
Martin Hundebøllf8214862012-04-20 17:02:45 +0200200 if (skb) {
201 batadv_inc_counter(bat_priv, BAT_CNT_MGMT_TX);
202 batadv_add_counter(bat_priv, BAT_CNT_MGMT_TX_BYTES,
203 skb->len + ETH_HLEN);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200204 send_skb_packet(skb, hard_iface, broadcast_addr);
Martin Hundebøllf8214862012-04-20 17:02:45 +0200205 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200206}
207
208/* send a batman ogm packet */
Marek Lindner01c42242011-11-28 21:31:55 +0800209static void bat_iv_ogm_emit(struct forw_packet *forw_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200210{
211 struct hard_iface *hard_iface;
212 struct net_device *soft_iface;
213 struct bat_priv *bat_priv;
214 struct hard_iface *primary_if = NULL;
215 struct batman_ogm_packet *batman_ogm_packet;
216 unsigned char directlink;
217
218 batman_ogm_packet = (struct batman_ogm_packet *)
219 (forw_packet->skb->data);
220 directlink = (batman_ogm_packet->flags & DIRECTLINK ? 1 : 0);
221
222 if (!forw_packet->if_incoming) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100223 pr_err("Error - can't forward packet: incoming iface not specified\n");
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200224 goto out;
225 }
226
227 soft_iface = forw_packet->if_incoming->soft_iface;
228 bat_priv = netdev_priv(soft_iface);
229
230 if (forw_packet->if_incoming->if_status != IF_ACTIVE)
231 goto out;
232
233 primary_if = primary_if_get_selected(bat_priv);
234 if (!primary_if)
235 goto out;
236
237 /* multihomed peer assumed */
238 /* non-primary OGMs are only broadcasted on their interface */
Sven Eckelmann76543d12011-11-20 15:47:38 +0100239 if ((directlink && (batman_ogm_packet->header.ttl == 1)) ||
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200240 (forw_packet->own && (forw_packet->if_incoming != primary_if))) {
241
242 /* FIXME: what about aggregated packets ? */
243 bat_dbg(DBG_BATMAN, bat_priv,
Antonio Quartullic97c72b2012-02-26 15:39:41 +0100244 "%s packet (originator %pM, seqno %u, TTL %d) on interface %s [%pM]\n",
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200245 (forw_packet->own ? "Sending own" : "Forwarding"),
246 batman_ogm_packet->orig,
247 ntohl(batman_ogm_packet->seqno),
Sven Eckelmann76543d12011-11-20 15:47:38 +0100248 batman_ogm_packet->header.ttl,
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200249 forw_packet->if_incoming->net_dev->name,
250 forw_packet->if_incoming->net_dev->dev_addr);
251
252 /* skb is only used once and than forw_packet is free'd */
253 send_skb_packet(forw_packet->skb, forw_packet->if_incoming,
254 broadcast_addr);
255 forw_packet->skb = NULL;
256
257 goto out;
258 }
259
260 /* broadcast on every interface */
261 rcu_read_lock();
262 list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
263 if (hard_iface->soft_iface != soft_iface)
264 continue;
265
Marek Lindner01c42242011-11-28 21:31:55 +0800266 bat_iv_ogm_send_to_if(forw_packet, hard_iface);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200267 }
268 rcu_read_unlock();
269
270out:
271 if (primary_if)
272 hardif_free_ref(primary_if);
273}
274
275/* return true if new_packet can be aggregated with forw_packet */
Marek Lindner01c42242011-11-28 21:31:55 +0800276static bool bat_iv_ogm_can_aggregate(const struct batman_ogm_packet
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200277 *new_batman_ogm_packet,
Marek Lindner01c42242011-11-28 21:31:55 +0800278 struct bat_priv *bat_priv,
279 int packet_len, unsigned long send_time,
280 bool directlink,
281 const struct hard_iface *if_incoming,
282 const struct forw_packet *forw_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200283{
284 struct batman_ogm_packet *batman_ogm_packet;
285 int aggregated_bytes = forw_packet->packet_len + packet_len;
286 struct hard_iface *primary_if = NULL;
287 bool res = false;
288
289 batman_ogm_packet = (struct batman_ogm_packet *)forw_packet->skb->data;
290
291 /**
292 * we can aggregate the current packet to this aggregated packet
293 * if:
294 *
295 * - the send time is within our MAX_AGGREGATION_MS time
296 * - the resulting packet wont be bigger than
297 * MAX_AGGREGATION_BYTES
298 */
299
300 if (time_before(send_time, forw_packet->send_time) &&
301 time_after_eq(send_time + msecs_to_jiffies(MAX_AGGREGATION_MS),
302 forw_packet->send_time) &&
303 (aggregated_bytes <= MAX_AGGREGATION_BYTES)) {
304
305 /**
306 * check aggregation compatibility
307 * -> direct link packets are broadcasted on
308 * their interface only
309 * -> aggregate packet if the current packet is
310 * a "global" packet as well as the base
311 * packet
312 */
313
314 primary_if = primary_if_get_selected(bat_priv);
315 if (!primary_if)
316 goto out;
317
318 /* packets without direct link flag and high TTL
319 * are flooded through the net */
320 if ((!directlink) &&
321 (!(batman_ogm_packet->flags & DIRECTLINK)) &&
Sven Eckelmann76543d12011-11-20 15:47:38 +0100322 (batman_ogm_packet->header.ttl != 1) &&
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200323
324 /* own packets originating non-primary
325 * interfaces leave only that interface */
326 ((!forw_packet->own) ||
327 (forw_packet->if_incoming == primary_if))) {
328 res = true;
329 goto out;
330 }
331
332 /* if the incoming packet is sent via this one
333 * interface only - we still can aggregate */
334 if ((directlink) &&
Sven Eckelmann76543d12011-11-20 15:47:38 +0100335 (new_batman_ogm_packet->header.ttl == 1) &&
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200336 (forw_packet->if_incoming == if_incoming) &&
337
338 /* packets from direct neighbors or
339 * own secondary interface packets
340 * (= secondary interface packets in general) */
341 (batman_ogm_packet->flags & DIRECTLINK ||
342 (forw_packet->own &&
343 forw_packet->if_incoming != primary_if))) {
344 res = true;
345 goto out;
346 }
347 }
348
349out:
350 if (primary_if)
351 hardif_free_ref(primary_if);
352 return res;
353}
354
355/* create a new aggregated packet and add this packet to it */
Marek Lindner01c42242011-11-28 21:31:55 +0800356static void bat_iv_ogm_aggregate_new(const unsigned char *packet_buff,
357 int packet_len, unsigned long send_time,
358 bool direct_link,
359 struct hard_iface *if_incoming,
360 int own_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200361{
362 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
363 struct forw_packet *forw_packet_aggr;
364 unsigned char *skb_buff;
365
366 if (!atomic_inc_not_zero(&if_incoming->refcount))
367 return;
368
369 /* own packet should always be scheduled */
370 if (!own_packet) {
371 if (!atomic_dec_not_zero(&bat_priv->batman_queue_left)) {
372 bat_dbg(DBG_BATMAN, bat_priv,
373 "batman packet queue full\n");
374 goto out;
375 }
376 }
377
378 forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC);
379 if (!forw_packet_aggr) {
380 if (!own_packet)
381 atomic_inc(&bat_priv->batman_queue_left);
382 goto out;
383 }
384
385 if ((atomic_read(&bat_priv->aggregated_ogms)) &&
386 (packet_len < MAX_AGGREGATION_BYTES))
387 forw_packet_aggr->skb = dev_alloc_skb(MAX_AGGREGATION_BYTES +
Antonio Quartulli0d125072012-02-18 11:27:34 +0100388 ETH_HLEN);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200389 else
Antonio Quartulli0d125072012-02-18 11:27:34 +0100390 forw_packet_aggr->skb = dev_alloc_skb(packet_len + ETH_HLEN);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200391
392 if (!forw_packet_aggr->skb) {
393 if (!own_packet)
394 atomic_inc(&bat_priv->batman_queue_left);
395 kfree(forw_packet_aggr);
396 goto out;
397 }
Antonio Quartulli0d125072012-02-18 11:27:34 +0100398 skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200399
400 INIT_HLIST_NODE(&forw_packet_aggr->list);
401
402 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
403 forw_packet_aggr->packet_len = packet_len;
404 memcpy(skb_buff, packet_buff, packet_len);
405
406 forw_packet_aggr->own = own_packet;
407 forw_packet_aggr->if_incoming = if_incoming;
408 forw_packet_aggr->num_packets = 0;
409 forw_packet_aggr->direct_link_flags = NO_FLAGS;
410 forw_packet_aggr->send_time = send_time;
411
412 /* save packet direct link flag status */
413 if (direct_link)
414 forw_packet_aggr->direct_link_flags |= 1;
415
416 /* add new packet to packet list */
417 spin_lock_bh(&bat_priv->forw_bat_list_lock);
418 hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list);
419 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
420
421 /* start timer for this packet */
422 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
423 send_outstanding_bat_ogm_packet);
424 queue_delayed_work(bat_event_workqueue,
425 &forw_packet_aggr->delayed_work,
426 send_time - jiffies);
427
428 return;
429out:
430 hardif_free_ref(if_incoming);
431}
432
433/* aggregate a new packet into the existing ogm packet */
Marek Lindner01c42242011-11-28 21:31:55 +0800434static void bat_iv_ogm_aggregate(struct forw_packet *forw_packet_aggr,
435 const unsigned char *packet_buff,
436 int packet_len, bool direct_link)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200437{
438 unsigned char *skb_buff;
439
440 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
441 memcpy(skb_buff, packet_buff, packet_len);
442 forw_packet_aggr->packet_len += packet_len;
443 forw_packet_aggr->num_packets++;
444
445 /* save packet direct link flag status */
446 if (direct_link)
447 forw_packet_aggr->direct_link_flags |=
448 (1 << forw_packet_aggr->num_packets);
449}
450
Marek Lindner01c42242011-11-28 21:31:55 +0800451static void bat_iv_ogm_queue_add(struct bat_priv *bat_priv,
452 unsigned char *packet_buff,
453 int packet_len, struct hard_iface *if_incoming,
454 int own_packet, unsigned long send_time)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200455{
456 /**
457 * _aggr -> pointer to the packet we want to aggregate with
458 * _pos -> pointer to the position in the queue
459 */
460 struct forw_packet *forw_packet_aggr = NULL, *forw_packet_pos = NULL;
461 struct hlist_node *tmp_node;
462 struct batman_ogm_packet *batman_ogm_packet;
463 bool direct_link;
464
465 batman_ogm_packet = (struct batman_ogm_packet *)packet_buff;
466 direct_link = batman_ogm_packet->flags & DIRECTLINK ? 1 : 0;
467
468 /* find position for the packet in the forward queue */
469 spin_lock_bh(&bat_priv->forw_bat_list_lock);
470 /* own packets are not to be aggregated */
471 if ((atomic_read(&bat_priv->aggregated_ogms)) && (!own_packet)) {
472 hlist_for_each_entry(forw_packet_pos, tmp_node,
473 &bat_priv->forw_bat_list, list) {
Marek Lindner01c42242011-11-28 21:31:55 +0800474 if (bat_iv_ogm_can_aggregate(batman_ogm_packet,
475 bat_priv, packet_len,
476 send_time, direct_link,
477 if_incoming,
478 forw_packet_pos)) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200479 forw_packet_aggr = forw_packet_pos;
480 break;
481 }
482 }
483 }
484
485 /* nothing to aggregate with - either aggregation disabled or no
486 * suitable aggregation packet found */
487 if (!forw_packet_aggr) {
488 /* the following section can run without the lock */
489 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
490
491 /**
492 * if we could not aggregate this packet with one of the others
493 * we hold it back for a while, so that it might be aggregated
494 * later on
495 */
496 if ((!own_packet) &&
497 (atomic_read(&bat_priv->aggregated_ogms)))
498 send_time += msecs_to_jiffies(MAX_AGGREGATION_MS);
499
Marek Lindner01c42242011-11-28 21:31:55 +0800500 bat_iv_ogm_aggregate_new(packet_buff, packet_len,
501 send_time, direct_link,
502 if_incoming, own_packet);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200503 } else {
Marek Lindner01c42242011-11-28 21:31:55 +0800504 bat_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
505 packet_len, direct_link);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200506 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
507 }
508}
509
Marek Lindner01c42242011-11-28 21:31:55 +0800510static void bat_iv_ogm_forward(struct orig_node *orig_node,
511 const struct ethhdr *ethhdr,
512 struct batman_ogm_packet *batman_ogm_packet,
Marek Lindner75cd33f2012-03-01 15:35:16 +0800513 bool is_single_hop_neigh,
Marek Lindner13b25412012-03-11 06:17:53 +0800514 bool is_from_best_next_hop,
Marek Lindner75cd33f2012-03-01 15:35:16 +0800515 struct hard_iface *if_incoming)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200516{
517 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200518 uint8_t tt_num_changes;
519
Sven Eckelmann76543d12011-11-20 15:47:38 +0100520 if (batman_ogm_packet->header.ttl <= 1) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200521 bat_dbg(DBG_BATMAN, bat_priv, "ttl exceeded\n");
522 return;
523 }
524
Marek Lindner13b25412012-03-11 06:17:53 +0800525 if (!is_from_best_next_hop) {
526 /* Mark the forwarded packet when it is not coming from our
527 * best next hop. We still need to forward the packet for our
528 * neighbor link quality detection to work in case the packet
529 * originated from a single hop neighbor. Otherwise we can
530 * simply drop the ogm.
531 */
532 if (is_single_hop_neigh)
533 batman_ogm_packet->flags |= NOT_BEST_NEXT_HOP;
534 else
535 return;
536 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200537
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200538 tt_num_changes = batman_ogm_packet->tt_num_changes;
539
Sven Eckelmann76543d12011-11-20 15:47:38 +0100540 batman_ogm_packet->header.ttl--;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200541 memcpy(batman_ogm_packet->prev_sender, ethhdr->h_source, ETH_ALEN);
542
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200543 /* apply hop penalty */
544 batman_ogm_packet->tq = hop_penalty(batman_ogm_packet->tq, bat_priv);
545
546 bat_dbg(DBG_BATMAN, bat_priv,
Marek Lindner13b25412012-03-11 06:17:53 +0800547 "Forwarding packet: tq: %i, ttl: %i\n",
548 batman_ogm_packet->tq, batman_ogm_packet->header.ttl);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200549
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200550 /* switch of primaries first hop flag when forwarding */
551 batman_ogm_packet->flags &= ~PRIMARIES_FIRST_HOP;
Marek Lindner75cd33f2012-03-01 15:35:16 +0800552 if (is_single_hop_neigh)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200553 batman_ogm_packet->flags |= DIRECTLINK;
554 else
555 batman_ogm_packet->flags &= ~DIRECTLINK;
556
Marek Lindner01c42242011-11-28 21:31:55 +0800557 bat_iv_ogm_queue_add(bat_priv, (unsigned char *)batman_ogm_packet,
Marek Lindner76e3d7f2012-02-07 17:20:50 +0800558 BATMAN_OGM_HLEN + tt_len(tt_num_changes),
Marek Lindner01c42242011-11-28 21:31:55 +0800559 if_incoming, 0, bat_iv_ogm_fwd_send_time());
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200560}
561
Marek Lindner01c42242011-11-28 21:31:55 +0800562static void bat_iv_ogm_schedule(struct hard_iface *hard_iface,
563 int tt_num_changes)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200564{
565 struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
566 struct batman_ogm_packet *batman_ogm_packet;
567 struct hard_iface *primary_if;
568 int vis_server;
569
570 vis_server = atomic_read(&bat_priv->vis_mode);
571 primary_if = primary_if_get_selected(bat_priv);
572
573 batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff;
574
575 /* change sequence number to network order */
576 batman_ogm_packet->seqno =
577 htonl((uint32_t)atomic_read(&hard_iface->seqno));
578
579 batman_ogm_packet->ttvn = atomic_read(&bat_priv->ttvn);
Antonio Quartulli66a1b2b2012-04-14 13:15:26 +0200580 batman_ogm_packet->tt_crc = htons(bat_priv->tt_crc);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200581 if (tt_num_changes >= 0)
582 batman_ogm_packet->tt_num_changes = tt_num_changes;
583
584 if (vis_server == VIS_TYPE_SERVER_SYNC)
585 batman_ogm_packet->flags |= VIS_SERVER;
586 else
587 batman_ogm_packet->flags &= ~VIS_SERVER;
588
589 if ((hard_iface == primary_if) &&
590 (atomic_read(&bat_priv->gw_mode) == GW_MODE_SERVER))
591 batman_ogm_packet->gw_flags =
592 (uint8_t)atomic_read(&bat_priv->gw_bandwidth);
593 else
594 batman_ogm_packet->gw_flags = NO_FLAGS;
595
596 atomic_inc(&hard_iface->seqno);
597
598 slide_own_bcast_window(hard_iface);
Marek Lindner01c42242011-11-28 21:31:55 +0800599 bat_iv_ogm_queue_add(bat_priv, hard_iface->packet_buff,
600 hard_iface->packet_len, hard_iface, 1,
601 bat_iv_ogm_emit_send_time(bat_priv));
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200602
603 if (primary_if)
604 hardif_free_ref(primary_if);
605}
606
Marek Lindner01c42242011-11-28 21:31:55 +0800607static void bat_iv_ogm_orig_update(struct bat_priv *bat_priv,
608 struct orig_node *orig_node,
609 const struct ethhdr *ethhdr,
610 const struct batman_ogm_packet
Marek Lindnerfc957272011-07-30 12:04:12 +0200611 *batman_ogm_packet,
Marek Lindner01c42242011-11-28 21:31:55 +0800612 struct hard_iface *if_incoming,
613 const unsigned char *tt_buff,
614 int is_duplicate)
Marek Lindnerfc957272011-07-30 12:04:12 +0200615{
616 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
617 struct neigh_node *router = NULL;
618 struct orig_node *orig_node_tmp;
619 struct hlist_node *node;
620 uint8_t bcast_own_sum_orig, bcast_own_sum_neigh;
621
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100622 bat_dbg(DBG_BATMAN, bat_priv,
623 "update_originator(): Searching and updating originator entry of received packet\n");
Marek Lindnerfc957272011-07-30 12:04:12 +0200624
625 rcu_read_lock();
626 hlist_for_each_entry_rcu(tmp_neigh_node, node,
627 &orig_node->neigh_list, list) {
628 if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
629 (tmp_neigh_node->if_incoming == if_incoming) &&
630 atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
631 if (neigh_node)
632 neigh_node_free_ref(neigh_node);
633 neigh_node = tmp_neigh_node;
634 continue;
635 }
636
637 if (is_duplicate)
638 continue;
639
Marek Lindnere3b0d0d2012-03-17 15:28:32 +0800640 spin_lock_bh(&tmp_neigh_node->lq_update_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +0200641 ring_buffer_set(tmp_neigh_node->tq_recv,
642 &tmp_neigh_node->tq_index, 0);
643 tmp_neigh_node->tq_avg =
644 ring_buffer_avg(tmp_neigh_node->tq_recv);
Marek Lindnere3b0d0d2012-03-17 15:28:32 +0800645 spin_unlock_bh(&tmp_neigh_node->lq_update_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +0200646 }
647
648 if (!neigh_node) {
649 struct orig_node *orig_tmp;
650
651 orig_tmp = get_orig_node(bat_priv, ethhdr->h_source);
652 if (!orig_tmp)
653 goto unlock;
654
Marek Lindner7ae8b282012-03-01 15:35:21 +0800655 neigh_node = bat_iv_ogm_neigh_new(if_incoming, ethhdr->h_source,
656 orig_node, orig_tmp,
657 batman_ogm_packet->seqno);
Marek Lindnerfc957272011-07-30 12:04:12 +0200658
659 orig_node_free_ref(orig_tmp);
660 if (!neigh_node)
661 goto unlock;
662 } else
663 bat_dbg(DBG_BATMAN, bat_priv,
664 "Updating existing last-hop neighbor of originator\n");
665
666 rcu_read_unlock();
667
668 orig_node->flags = batman_ogm_packet->flags;
Marek Lindnerd7b2a972012-03-01 15:35:19 +0800669 neigh_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +0200670
Marek Lindnere3b0d0d2012-03-17 15:28:32 +0800671 spin_lock_bh(&neigh_node->lq_update_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +0200672 ring_buffer_set(neigh_node->tq_recv,
673 &neigh_node->tq_index,
674 batman_ogm_packet->tq);
675 neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
Marek Lindnere3b0d0d2012-03-17 15:28:32 +0800676 spin_unlock_bh(&neigh_node->lq_update_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +0200677
678 if (!is_duplicate) {
Sven Eckelmann76543d12011-11-20 15:47:38 +0100679 orig_node->last_ttl = batman_ogm_packet->header.ttl;
680 neigh_node->last_ttl = batman_ogm_packet->header.ttl;
Marek Lindnerfc957272011-07-30 12:04:12 +0200681 }
682
683 bonding_candidate_add(orig_node, neigh_node);
684
685 /* if this neighbor already is our next hop there is nothing
686 * to change */
687 router = orig_node_get_router(orig_node);
688 if (router == neigh_node)
689 goto update_tt;
690
691 /* if this neighbor does not offer a better TQ we won't consider it */
692 if (router && (router->tq_avg > neigh_node->tq_avg))
693 goto update_tt;
694
695 /* if the TQ is the same and the link not more symmetric we
696 * won't consider it either */
697 if (router && (neigh_node->tq_avg == router->tq_avg)) {
698 orig_node_tmp = router->orig_node;
699 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
700 bcast_own_sum_orig =
701 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
702 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
703
704 orig_node_tmp = neigh_node->orig_node;
705 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
706 bcast_own_sum_neigh =
707 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
708 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
709
710 if (bcast_own_sum_orig >= bcast_own_sum_neigh)
711 goto update_tt;
712 }
713
714 update_route(bat_priv, orig_node, neigh_node);
715
716update_tt:
717 /* I have to check for transtable changes only if the OGM has been
718 * sent through a primary interface */
719 if (((batman_ogm_packet->orig != ethhdr->h_source) &&
Sven Eckelmann76543d12011-11-20 15:47:38 +0100720 (batman_ogm_packet->header.ttl > 2)) ||
Marek Lindnerfc957272011-07-30 12:04:12 +0200721 (batman_ogm_packet->flags & PRIMARIES_FIRST_HOP))
722 tt_update_orig(bat_priv, orig_node, tt_buff,
723 batman_ogm_packet->tt_num_changes,
724 batman_ogm_packet->ttvn,
Al Viro16a70342012-04-22 07:45:29 +0100725 ntohs(batman_ogm_packet->tt_crc));
Marek Lindnerfc957272011-07-30 12:04:12 +0200726
727 if (orig_node->gw_flags != batman_ogm_packet->gw_flags)
728 gw_node_update(bat_priv, orig_node,
729 batman_ogm_packet->gw_flags);
730
731 orig_node->gw_flags = batman_ogm_packet->gw_flags;
732
733 /* restart gateway selection if fast or late switching was enabled */
734 if ((orig_node->gw_flags) &&
735 (atomic_read(&bat_priv->gw_mode) == GW_MODE_CLIENT) &&
736 (atomic_read(&bat_priv->gw_sel_class) > 2))
737 gw_check_election(bat_priv, orig_node);
738
739 goto out;
740
741unlock:
742 rcu_read_unlock();
743out:
744 if (neigh_node)
745 neigh_node_free_ref(neigh_node);
746 if (router)
747 neigh_node_free_ref(router);
748}
749
Marek Lindner01c42242011-11-28 21:31:55 +0800750static int bat_iv_ogm_calc_tq(struct orig_node *orig_node,
751 struct orig_node *orig_neigh_node,
752 struct batman_ogm_packet *batman_ogm_packet,
753 struct hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +0200754{
755 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
756 struct neigh_node *neigh_node = NULL, *tmp_neigh_node;
757 struct hlist_node *node;
758 uint8_t total_count;
759 uint8_t orig_eq_count, neigh_rq_count, tq_own;
760 int tq_asym_penalty, ret = 0;
761
762 /* find corresponding one hop neighbor */
763 rcu_read_lock();
764 hlist_for_each_entry_rcu(tmp_neigh_node, node,
765 &orig_neigh_node->neigh_list, list) {
766
767 if (!compare_eth(tmp_neigh_node->addr, orig_neigh_node->orig))
768 continue;
769
770 if (tmp_neigh_node->if_incoming != if_incoming)
771 continue;
772
773 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
774 continue;
775
776 neigh_node = tmp_neigh_node;
777 break;
778 }
779 rcu_read_unlock();
780
781 if (!neigh_node)
Marek Lindner7ae8b282012-03-01 15:35:21 +0800782 neigh_node = bat_iv_ogm_neigh_new(if_incoming,
783 orig_neigh_node->orig,
784 orig_neigh_node,
785 orig_neigh_node,
786 batman_ogm_packet->seqno);
Marek Lindnerfc957272011-07-30 12:04:12 +0200787
788 if (!neigh_node)
789 goto out;
790
Marek Lindnerd7b2a972012-03-01 15:35:19 +0800791 /* if orig_node is direct neighbor update neigh_node last_seen */
Marek Lindnerfc957272011-07-30 12:04:12 +0200792 if (orig_node == orig_neigh_node)
Marek Lindnerd7b2a972012-03-01 15:35:19 +0800793 neigh_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +0200794
Marek Lindnerd7b2a972012-03-01 15:35:19 +0800795 orig_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +0200796
797 /* find packet count of corresponding one hop neighbor */
798 spin_lock_bh(&orig_node->ogm_cnt_lock);
799 orig_eq_count = orig_neigh_node->bcast_own_sum[if_incoming->if_num];
800 neigh_rq_count = neigh_node->real_packet_count;
801 spin_unlock_bh(&orig_node->ogm_cnt_lock);
802
803 /* pay attention to not get a value bigger than 100 % */
804 total_count = (orig_eq_count > neigh_rq_count ?
805 neigh_rq_count : orig_eq_count);
806
807 /* if we have too few packets (too less data) we set tq_own to zero */
808 /* if we receive too few packets it is not considered bidirectional */
809 if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
810 (neigh_rq_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
811 tq_own = 0;
812 else
813 /* neigh_node->real_packet_count is never zero as we
814 * only purge old information when getting new
815 * information */
816 tq_own = (TQ_MAX_VALUE * total_count) / neigh_rq_count;
817
Sven Eckelmann21a12362012-03-07 09:07:46 +0100818 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
Marek Lindnerfc957272011-07-30 12:04:12 +0200819 * affect the nearly-symmetric links only a little, but
820 * punishes asymmetric links more. This will give a value
821 * between 0 and TQ_MAX_VALUE
822 */
823 tq_asym_penalty = TQ_MAX_VALUE - (TQ_MAX_VALUE *
824 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
825 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
826 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count)) /
827 (TQ_LOCAL_WINDOW_SIZE *
828 TQ_LOCAL_WINDOW_SIZE *
829 TQ_LOCAL_WINDOW_SIZE);
830
831 batman_ogm_packet->tq = ((batman_ogm_packet->tq * tq_own
832 * tq_asym_penalty) /
833 (TQ_MAX_VALUE * TQ_MAX_VALUE));
834
835 bat_dbg(DBG_BATMAN, bat_priv,
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100836 "bidirectional: orig = %-15pM neigh = %-15pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, total tq: %3i\n",
Marek Lindnerfc957272011-07-30 12:04:12 +0200837 orig_node->orig, orig_neigh_node->orig, total_count,
838 neigh_rq_count, tq_own, tq_asym_penalty, batman_ogm_packet->tq);
839
840 /* if link has the minimum required transmission quality
841 * consider it bidirectional */
842 if (batman_ogm_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
843 ret = 1;
844
845out:
846 if (neigh_node)
847 neigh_node_free_ref(neigh_node);
848 return ret;
849}
850
851/* processes a batman packet for all interfaces, adjusts the sequence number and
852 * finds out whether it is a duplicate.
853 * returns:
854 * 1 the packet is a duplicate
855 * 0 the packet has not yet been received
856 * -1 the packet is old and has been received while the seqno window
857 * was protected. Caller should drop it.
858 */
Marek Lindner01c42242011-11-28 21:31:55 +0800859static int bat_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
860 const struct batman_ogm_packet
Marek Lindnerfc957272011-07-30 12:04:12 +0200861 *batman_ogm_packet,
Marek Lindner01c42242011-11-28 21:31:55 +0800862 const struct hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +0200863{
864 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
865 struct orig_node *orig_node;
866 struct neigh_node *tmp_neigh_node;
867 struct hlist_node *node;
868 int is_duplicate = 0;
869 int32_t seq_diff;
870 int need_update = 0;
871 int set_mark, ret = -1;
Al Viroe0f52112012-04-22 07:46:29 +0100872 uint32_t seqno = ntohl(batman_ogm_packet->seqno);
Marek Lindnerfc957272011-07-30 12:04:12 +0200873
874 orig_node = get_orig_node(bat_priv, batman_ogm_packet->orig);
875 if (!orig_node)
876 return 0;
877
878 spin_lock_bh(&orig_node->ogm_cnt_lock);
Al Viroe0f52112012-04-22 07:46:29 +0100879 seq_diff = seqno - orig_node->last_real_seqno;
Marek Lindnerfc957272011-07-30 12:04:12 +0200880
881 /* signalize caller that the packet is to be dropped. */
Antonio Quartulli1e5cc262012-02-26 15:39:42 +0100882 if (!hlist_empty(&orig_node->neigh_list) &&
883 window_protected(bat_priv, seq_diff,
Marek Lindnerfc957272011-07-30 12:04:12 +0200884 &orig_node->batman_seqno_reset))
885 goto out;
886
887 rcu_read_lock();
888 hlist_for_each_entry_rcu(tmp_neigh_node, node,
889 &orig_node->neigh_list, list) {
890
Sven Eckelmann0079d2c2012-02-04 17:34:52 +0100891 is_duplicate |= bat_test_bit(tmp_neigh_node->real_bits,
892 orig_node->last_real_seqno,
Al Viroe0f52112012-04-22 07:46:29 +0100893 seqno);
Marek Lindnerfc957272011-07-30 12:04:12 +0200894
895 if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
896 (tmp_neigh_node->if_incoming == if_incoming))
897 set_mark = 1;
898 else
899 set_mark = 0;
900
901 /* if the window moved, set the update flag. */
902 need_update |= bit_get_packet(bat_priv,
903 tmp_neigh_node->real_bits,
904 seq_diff, set_mark);
905
906 tmp_neigh_node->real_packet_count =
Sven Eckelmann0079d2c2012-02-04 17:34:52 +0100907 bitmap_weight(tmp_neigh_node->real_bits,
908 TQ_LOCAL_WINDOW_SIZE);
Marek Lindnerfc957272011-07-30 12:04:12 +0200909 }
910 rcu_read_unlock();
911
912 if (need_update) {
913 bat_dbg(DBG_BATMAN, bat_priv,
Antonio Quartullic97c72b2012-02-26 15:39:41 +0100914 "updating last_seqno: old %u, new %u\n",
Al Viroe0f52112012-04-22 07:46:29 +0100915 orig_node->last_real_seqno, seqno);
916 orig_node->last_real_seqno = seqno;
Marek Lindnerfc957272011-07-30 12:04:12 +0200917 }
918
919 ret = is_duplicate;
920
921out:
922 spin_unlock_bh(&orig_node->ogm_cnt_lock);
923 orig_node_free_ref(orig_node);
924 return ret;
925}
926
Marek Lindner01c42242011-11-28 21:31:55 +0800927static void bat_iv_ogm_process(const struct ethhdr *ethhdr,
928 struct batman_ogm_packet *batman_ogm_packet,
929 const unsigned char *tt_buff,
930 struct hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +0200931{
932 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
933 struct hard_iface *hard_iface;
934 struct orig_node *orig_neigh_node, *orig_node;
935 struct neigh_node *router = NULL, *router_router = NULL;
936 struct neigh_node *orig_neigh_router = NULL;
937 int has_directlink_flag;
938 int is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
Marek Lindner75cd33f2012-03-01 15:35:16 +0800939 int is_broadcast = 0, is_bidirectional;
940 bool is_single_hop_neigh = false;
Marek Lindner13b25412012-03-11 06:17:53 +0800941 bool is_from_best_next_hop = false;
Marek Lindnerfc957272011-07-30 12:04:12 +0200942 int is_duplicate;
943 uint32_t if_incoming_seqno;
944
945 /* Silently drop when the batman packet is actually not a
946 * correct packet.
947 *
948 * This might happen if a packet is padded (e.g. Ethernet has a
949 * minimum frame length of 64 byte) and the aggregation interprets
950 * it as an additional length.
951 *
952 * TODO: A more sane solution would be to have a bit in the
953 * batman_ogm_packet to detect whether the packet is the last
954 * packet in an aggregation. Here we expect that the padding
955 * is always zero (or not 0x01)
956 */
Marek Lindner1eeb4792012-02-07 17:20:51 +0800957 if (batman_ogm_packet->header.packet_type != BAT_IV_OGM)
Marek Lindnerfc957272011-07-30 12:04:12 +0200958 return;
959
960 /* could be changed by schedule_own_packet() */
961 if_incoming_seqno = atomic_read(&if_incoming->seqno);
962
963 has_directlink_flag = (batman_ogm_packet->flags & DIRECTLINK ? 1 : 0);
964
Marek Lindner75cd33f2012-03-01 15:35:16 +0800965 if (compare_eth(ethhdr->h_source, batman_ogm_packet->orig))
966 is_single_hop_neigh = true;
Marek Lindnerfc957272011-07-30 12:04:12 +0200967
968 bat_dbg(DBG_BATMAN, bat_priv,
Antonio Quartullic97c72b2012-02-26 15:39:41 +0100969 "Received BATMAN packet via NB: %pM, IF: %s [%pM] (from OG: %pM, via prev OG: %pM, seqno %u, ttvn %u, crc %u, changes %u, td %d, TTL %d, V %d, IDF %d)\n",
Marek Lindnerfc957272011-07-30 12:04:12 +0200970 ethhdr->h_source, if_incoming->net_dev->name,
971 if_incoming->net_dev->dev_addr, batman_ogm_packet->orig,
Al Viroe0f52112012-04-22 07:46:29 +0100972 batman_ogm_packet->prev_sender, ntohl(batman_ogm_packet->seqno),
Al Viro16a70342012-04-22 07:45:29 +0100973 batman_ogm_packet->ttvn, ntohs(batman_ogm_packet->tt_crc),
Marek Lindnerfc957272011-07-30 12:04:12 +0200974 batman_ogm_packet->tt_num_changes, batman_ogm_packet->tq,
Sven Eckelmann76543d12011-11-20 15:47:38 +0100975 batman_ogm_packet->header.ttl,
976 batman_ogm_packet->header.version, has_directlink_flag);
Marek Lindnerfc957272011-07-30 12:04:12 +0200977
978 rcu_read_lock();
979 list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
980 if (hard_iface->if_status != IF_ACTIVE)
981 continue;
982
983 if (hard_iface->soft_iface != if_incoming->soft_iface)
984 continue;
985
986 if (compare_eth(ethhdr->h_source,
987 hard_iface->net_dev->dev_addr))
988 is_my_addr = 1;
989
990 if (compare_eth(batman_ogm_packet->orig,
991 hard_iface->net_dev->dev_addr))
992 is_my_orig = 1;
993
994 if (compare_eth(batman_ogm_packet->prev_sender,
995 hard_iface->net_dev->dev_addr))
996 is_my_oldorig = 1;
997
998 if (is_broadcast_ether_addr(ethhdr->h_source))
999 is_broadcast = 1;
1000 }
1001 rcu_read_unlock();
1002
Sven Eckelmann76543d12011-11-20 15:47:38 +01001003 if (batman_ogm_packet->header.version != COMPAT_VERSION) {
Marek Lindnerfc957272011-07-30 12:04:12 +02001004 bat_dbg(DBG_BATMAN, bat_priv,
1005 "Drop packet: incompatible batman version (%i)\n",
Sven Eckelmann76543d12011-11-20 15:47:38 +01001006 batman_ogm_packet->header.version);
Marek Lindnerfc957272011-07-30 12:04:12 +02001007 return;
1008 }
1009
1010 if (is_my_addr) {
1011 bat_dbg(DBG_BATMAN, bat_priv,
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001012 "Drop packet: received my own broadcast (sender: %pM)\n",
Marek Lindnerfc957272011-07-30 12:04:12 +02001013 ethhdr->h_source);
1014 return;
1015 }
1016
1017 if (is_broadcast) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001018 bat_dbg(DBG_BATMAN, bat_priv,
1019 "Drop packet: ignoring all packets with broadcast source addr (sender: %pM)\n",
1020 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001021 return;
1022 }
1023
1024 if (is_my_orig) {
1025 unsigned long *word;
1026 int offset;
1027
1028 orig_neigh_node = get_orig_node(bat_priv, ethhdr->h_source);
1029 if (!orig_neigh_node)
1030 return;
1031
1032 /* neighbor has to indicate direct link and it has to
1033 * come via the corresponding interface */
1034 /* save packet seqno for bidirectional check */
1035 if (has_directlink_flag &&
1036 compare_eth(if_incoming->net_dev->dev_addr,
1037 batman_ogm_packet->orig)) {
1038 offset = if_incoming->if_num * NUM_WORDS;
1039
1040 spin_lock_bh(&orig_neigh_node->ogm_cnt_lock);
1041 word = &(orig_neigh_node->bcast_own[offset]);
Sven Eckelmann0079d2c2012-02-04 17:34:52 +01001042 bat_set_bit(word,
1043 if_incoming_seqno -
Al Viroe0f52112012-04-22 07:46:29 +01001044 ntohl(batman_ogm_packet->seqno) - 2);
Marek Lindnerfc957272011-07-30 12:04:12 +02001045 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
Sven Eckelmann0079d2c2012-02-04 17:34:52 +01001046 bitmap_weight(word, TQ_LOCAL_WINDOW_SIZE);
Marek Lindnerfc957272011-07-30 12:04:12 +02001047 spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock);
1048 }
1049
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001050 bat_dbg(DBG_BATMAN, bat_priv,
1051 "Drop packet: originator packet from myself (via neighbor)\n");
Marek Lindnerfc957272011-07-30 12:04:12 +02001052 orig_node_free_ref(orig_neigh_node);
1053 return;
1054 }
1055
1056 if (is_my_oldorig) {
1057 bat_dbg(DBG_BATMAN, bat_priv,
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001058 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1059 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001060 return;
1061 }
1062
Marek Lindner13b25412012-03-11 06:17:53 +08001063 if (batman_ogm_packet->flags & NOT_BEST_NEXT_HOP) {
1064 bat_dbg(DBG_BATMAN, bat_priv,
Marek Lindnerfefa5322012-03-17 15:28:34 +08001065 "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1066 ethhdr->h_source);
Marek Lindner13b25412012-03-11 06:17:53 +08001067 return;
1068 }
1069
Marek Lindnerfc957272011-07-30 12:04:12 +02001070 orig_node = get_orig_node(bat_priv, batman_ogm_packet->orig);
1071 if (!orig_node)
1072 return;
1073
Marek Lindner01c42242011-11-28 21:31:55 +08001074 is_duplicate = bat_iv_ogm_update_seqnos(ethhdr, batman_ogm_packet,
1075 if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001076
1077 if (is_duplicate == -1) {
1078 bat_dbg(DBG_BATMAN, bat_priv,
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001079 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1080 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001081 goto out;
1082 }
1083
1084 if (batman_ogm_packet->tq == 0) {
1085 bat_dbg(DBG_BATMAN, bat_priv,
1086 "Drop packet: originator packet with tq equal 0\n");
1087 goto out;
1088 }
1089
1090 router = orig_node_get_router(orig_node);
1091 if (router)
1092 router_router = orig_node_get_router(router->orig_node);
1093
Marek Lindner13b25412012-03-11 06:17:53 +08001094 if ((router && router->tq_avg != 0) &&
1095 (compare_eth(router->addr, ethhdr->h_source)))
1096 is_from_best_next_hop = true;
1097
Marek Lindnerfc957272011-07-30 12:04:12 +02001098 /* avoid temporary routing loops */
1099 if (router && router_router &&
1100 (compare_eth(router->addr, batman_ogm_packet->prev_sender)) &&
1101 !(compare_eth(batman_ogm_packet->orig,
1102 batman_ogm_packet->prev_sender)) &&
1103 (compare_eth(router->addr, router_router->addr))) {
1104 bat_dbg(DBG_BATMAN, bat_priv,
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001105 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1106 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001107 goto out;
1108 }
1109
1110 /* if sender is a direct neighbor the sender mac equals
1111 * originator mac */
1112 orig_neigh_node = (is_single_hop_neigh ?
1113 orig_node :
1114 get_orig_node(bat_priv, ethhdr->h_source));
1115 if (!orig_neigh_node)
1116 goto out;
1117
1118 orig_neigh_router = orig_node_get_router(orig_neigh_node);
1119
1120 /* drop packet if sender is not a direct neighbor and if we
1121 * don't route towards it */
1122 if (!is_single_hop_neigh && (!orig_neigh_router)) {
1123 bat_dbg(DBG_BATMAN, bat_priv,
1124 "Drop packet: OGM via unknown neighbor!\n");
1125 goto out_neigh;
1126 }
1127
Marek Lindner01c42242011-11-28 21:31:55 +08001128 is_bidirectional = bat_iv_ogm_calc_tq(orig_node, orig_neigh_node,
1129 batman_ogm_packet, if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001130
1131 bonding_save_primary(orig_node, orig_neigh_node, batman_ogm_packet);
1132
1133 /* update ranking if it is not a duplicate or has the same
1134 * seqno and similar ttl as the non-duplicate */
1135 if (is_bidirectional &&
1136 (!is_duplicate ||
Al Viroe0f52112012-04-22 07:46:29 +01001137 ((orig_node->last_real_seqno == ntohl(batman_ogm_packet->seqno)) &&
Sven Eckelmann76543d12011-11-20 15:47:38 +01001138 (orig_node->last_ttl - 3 <= batman_ogm_packet->header.ttl))))
Marek Lindner01c42242011-11-28 21:31:55 +08001139 bat_iv_ogm_orig_update(bat_priv, orig_node, ethhdr,
1140 batman_ogm_packet, if_incoming,
1141 tt_buff, is_duplicate);
Marek Lindnerfc957272011-07-30 12:04:12 +02001142
1143 /* is single hop (direct) neighbor */
1144 if (is_single_hop_neigh) {
1145
1146 /* mark direct link on incoming interface */
Marek Lindner01c42242011-11-28 21:31:55 +08001147 bat_iv_ogm_forward(orig_node, ethhdr, batman_ogm_packet,
Marek Lindner13b25412012-03-11 06:17:53 +08001148 is_single_hop_neigh, is_from_best_next_hop,
1149 if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001150
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001151 bat_dbg(DBG_BATMAN, bat_priv,
1152 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
Marek Lindnerfc957272011-07-30 12:04:12 +02001153 goto out_neigh;
1154 }
1155
1156 /* multihop originator */
1157 if (!is_bidirectional) {
1158 bat_dbg(DBG_BATMAN, bat_priv,
1159 "Drop packet: not received via bidirectional link\n");
1160 goto out_neigh;
1161 }
1162
1163 if (is_duplicate) {
1164 bat_dbg(DBG_BATMAN, bat_priv,
1165 "Drop packet: duplicate packet received\n");
1166 goto out_neigh;
1167 }
1168
1169 bat_dbg(DBG_BATMAN, bat_priv,
1170 "Forwarding packet: rebroadcast originator packet\n");
Marek Lindner01c42242011-11-28 21:31:55 +08001171 bat_iv_ogm_forward(orig_node, ethhdr, batman_ogm_packet,
Marek Lindner13b25412012-03-11 06:17:53 +08001172 is_single_hop_neigh, is_from_best_next_hop,
1173 if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001174
1175out_neigh:
1176 if ((orig_neigh_node) && (!is_single_hop_neigh))
1177 orig_node_free_ref(orig_neigh_node);
1178out:
1179 if (router)
1180 neigh_node_free_ref(router);
1181 if (router_router)
1182 neigh_node_free_ref(router_router);
1183 if (orig_neigh_router)
1184 neigh_node_free_ref(orig_neigh_router);
1185
1186 orig_node_free_ref(orig_node);
1187}
1188
Marek Lindnerc3e29312012-03-04 16:56:25 +08001189static int bat_iv_ogm_receive(struct sk_buff *skb,
1190 struct hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +02001191{
Marek Lindneredbf12b2012-03-11 06:17:49 +08001192 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Marek Lindnerfc957272011-07-30 12:04:12 +02001193 struct batman_ogm_packet *batman_ogm_packet;
Marek Lindner8780dad2011-12-05 04:01:51 +08001194 struct ethhdr *ethhdr;
1195 int buff_pos = 0, packet_len;
1196 unsigned char *tt_buff, *packet_buff;
Marek Lindnerc3e29312012-03-04 16:56:25 +08001197 bool ret;
1198
1199 ret = check_management_packet(skb, if_incoming, BATMAN_OGM_HLEN);
1200 if (!ret)
1201 return NET_RX_DROP;
Marek Lindnerfc957272011-07-30 12:04:12 +02001202
Marek Lindneredbf12b2012-03-11 06:17:49 +08001203 /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
1204 * that does not have B.A.T.M.A.N. IV enabled ?
1205 */
1206 if (bat_priv->bat_algo_ops->bat_ogm_emit != bat_iv_ogm_emit)
1207 return NET_RX_DROP;
1208
Martin Hundebøllf8214862012-04-20 17:02:45 +02001209 batadv_inc_counter(bat_priv, BAT_CNT_MGMT_RX);
1210 batadv_add_counter(bat_priv, BAT_CNT_MGMT_RX_BYTES,
1211 skb->len + ETH_HLEN);
1212
Marek Lindner8780dad2011-12-05 04:01:51 +08001213 packet_len = skb_headlen(skb);
1214 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1215 packet_buff = skb->data;
Marek Lindnerfc957272011-07-30 12:04:12 +02001216 batman_ogm_packet = (struct batman_ogm_packet *)packet_buff;
1217
1218 /* unpack the aggregated packets and process them one by one */
1219 do {
Marek Lindner76e3d7f2012-02-07 17:20:50 +08001220 tt_buff = packet_buff + buff_pos + BATMAN_OGM_HLEN;
Marek Lindnerfc957272011-07-30 12:04:12 +02001221
Marek Lindner01c42242011-11-28 21:31:55 +08001222 bat_iv_ogm_process(ethhdr, batman_ogm_packet,
1223 tt_buff, if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001224
Marek Lindner76e3d7f2012-02-07 17:20:50 +08001225 buff_pos += BATMAN_OGM_HLEN +
Marek Lindnerfc957272011-07-30 12:04:12 +02001226 tt_len(batman_ogm_packet->tt_num_changes);
1227
1228 batman_ogm_packet = (struct batman_ogm_packet *)
1229 (packet_buff + buff_pos);
Marek Lindner01c42242011-11-28 21:31:55 +08001230 } while (bat_iv_ogm_aggr_packet(buff_pos, packet_len,
1231 batman_ogm_packet->tt_num_changes));
Marek Lindnerc3e29312012-03-04 16:56:25 +08001232
1233 kfree_skb(skb);
1234 return NET_RX_SUCCESS;
Marek Lindnerfc957272011-07-30 12:04:12 +02001235}
Marek Lindner1c280472011-11-28 17:40:17 +08001236
1237static struct bat_algo_ops batman_iv __read_mostly = {
Marek Lindner519d3492012-04-18 17:15:57 +08001238 .name = "BATMAN_IV",
Marek Lindnerc2aca022012-02-07 17:20:45 +08001239 .bat_iface_enable = bat_iv_ogm_iface_enable,
Marek Lindner00a50072012-02-07 17:20:47 +08001240 .bat_iface_disable = bat_iv_ogm_iface_disable,
Marek Lindnerc3229392012-03-11 06:17:50 +08001241 .bat_iface_update_mac = bat_iv_ogm_iface_update_mac,
Marek Lindnercd8b78e2012-02-07 17:20:49 +08001242 .bat_primary_iface_set = bat_iv_ogm_primary_iface_set,
Marek Lindner01c42242011-11-28 21:31:55 +08001243 .bat_ogm_schedule = bat_iv_ogm_schedule,
1244 .bat_ogm_emit = bat_iv_ogm_emit,
Marek Lindner1c280472011-11-28 17:40:17 +08001245};
1246
1247int __init bat_iv_init(void)
1248{
Marek Lindnerc3e29312012-03-04 16:56:25 +08001249 int ret;
1250
1251 /* batman originator packet */
1252 ret = recv_handler_register(BAT_IV_OGM, bat_iv_ogm_receive);
1253 if (ret < 0)
1254 goto out;
1255
1256 ret = bat_algo_register(&batman_iv);
1257 if (ret < 0)
1258 goto handler_unregister;
1259
1260 goto out;
1261
1262handler_unregister:
1263 recv_handler_unregister(BAT_IV_OGM);
1264out:
1265 return ret;
Marek Lindner1c280472011-11-28 17:40:17 +08001266}