blob: 1014210bb36594ce13e9c7eb7722a9aecf22ea57 [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 Lindner77af7572012-02-07 17:20:48 +080033static int bat_iv_ogm_iface_enable(struct hard_iface *hard_iface)
Marek Lindnerd0b9fd82011-07-30 12:33:33 +020034{
35 struct batman_ogm_packet *batman_ogm_packet;
Marek Lindnerd7d32ec2012-02-07 17:20:46 +080036 uint32_t random_seqno;
Marek Lindner77af7572012-02-07 17:20:48 +080037 int res = -1;
Marek Lindnerd7d32ec2012-02-07 17:20:46 +080038
39 /* randomize initial seqno to avoid collision */
40 get_random_bytes(&random_seqno, sizeof(random_seqno));
41 atomic_set(&hard_iface->seqno, random_seqno);
Marek Lindnerd0b9fd82011-07-30 12:33:33 +020042
Marek Lindner76e3d7f2012-02-07 17:20:50 +080043 hard_iface->packet_len = BATMAN_OGM_HLEN;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +020044 hard_iface->packet_buff = kmalloc(hard_iface->packet_len, GFP_ATOMIC);
45
Marek Lindner77af7572012-02-07 17:20:48 +080046 if (!hard_iface->packet_buff)
47 goto out;
48
Marek Lindnerd0b9fd82011-07-30 12:33:33 +020049 batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff;
Marek Lindner1eeb4792012-02-07 17:20:51 +080050 batman_ogm_packet->header.packet_type = BAT_IV_OGM;
Sven Eckelmann76543d12011-11-20 15:47:38 +010051 batman_ogm_packet->header.version = COMPAT_VERSION;
52 batman_ogm_packet->header.ttl = 2;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +020053 batman_ogm_packet->flags = NO_FLAGS;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +020054 batman_ogm_packet->tq = TQ_MAX_VALUE;
55 batman_ogm_packet->tt_num_changes = 0;
56 batman_ogm_packet->ttvn = 0;
Marek Lindner77af7572012-02-07 17:20:48 +080057
58 res = 0;
59
60out:
61 return res;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +020062}
63
Marek Lindner00a50072012-02-07 17:20:47 +080064static void bat_iv_ogm_iface_disable(struct hard_iface *hard_iface)
65{
66 kfree(hard_iface->packet_buff);
67 hard_iface->packet_buff = NULL;
68}
69
Marek Lindnercd8b78e2012-02-07 17:20:49 +080070static void bat_iv_ogm_primary_iface_set(struct hard_iface *hard_iface)
Marek Lindnerd0b9fd82011-07-30 12:33:33 +020071{
72 struct batman_ogm_packet *batman_ogm_packet;
73
74 batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff;
75 batman_ogm_packet->flags = PRIMARIES_FIRST_HOP;
Sven Eckelmann76543d12011-11-20 15:47:38 +010076 batman_ogm_packet->header.ttl = TTL;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +020077}
78
Marek Lindner01c42242011-11-28 21:31:55 +080079static void bat_iv_ogm_update_mac(struct hard_iface *hard_iface)
Marek Lindnerd0b9fd82011-07-30 12:33:33 +020080{
81 struct batman_ogm_packet *batman_ogm_packet;
82
83 batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff;
84 memcpy(batman_ogm_packet->orig,
85 hard_iface->net_dev->dev_addr, ETH_ALEN);
86 memcpy(batman_ogm_packet->prev_sender,
87 hard_iface->net_dev->dev_addr, ETH_ALEN);
88}
89
Marek Lindnerb9dacc52011-08-03 09:09:30 +020090/* when do we schedule our own ogm to be sent */
Marek Lindner01c42242011-11-28 21:31:55 +080091static unsigned long bat_iv_ogm_emit_send_time(const struct bat_priv *bat_priv)
Marek Lindnerb9dacc52011-08-03 09:09:30 +020092{
93 return jiffies + msecs_to_jiffies(
94 atomic_read(&bat_priv->orig_interval) -
95 JITTER + (random32() % 2*JITTER));
96}
97
98/* when do we schedule a ogm packet to be sent */
Marek Lindner01c42242011-11-28 21:31:55 +080099static unsigned long bat_iv_ogm_fwd_send_time(void)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200100{
101 return jiffies + msecs_to_jiffies(random32() % (JITTER/2));
102}
103
104/* apply hop penalty for a normal link */
105static uint8_t hop_penalty(uint8_t tq, const struct bat_priv *bat_priv)
106{
107 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
108 return (tq * (TQ_MAX_VALUE - hop_penalty)) / (TQ_MAX_VALUE);
109}
110
Marek Lindnerfc957272011-07-30 12:04:12 +0200111/* is there another aggregated packet here? */
Marek Lindner01c42242011-11-28 21:31:55 +0800112static int bat_iv_ogm_aggr_packet(int buff_pos, int packet_len,
113 int tt_num_changes)
Marek Lindnerfc957272011-07-30 12:04:12 +0200114{
Marek Lindner76e3d7f2012-02-07 17:20:50 +0800115 int next_buff_pos = buff_pos + BATMAN_OGM_HLEN + tt_len(tt_num_changes);
Marek Lindnerfc957272011-07-30 12:04:12 +0200116
117 return (next_buff_pos <= packet_len) &&
118 (next_buff_pos <= MAX_AGGREGATION_BYTES);
119}
120
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200121/* send a batman ogm to a given interface */
Marek Lindner01c42242011-11-28 21:31:55 +0800122static void bat_iv_ogm_send_to_if(struct forw_packet *forw_packet,
123 struct hard_iface *hard_iface)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200124{
125 struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
126 char *fwd_str;
127 uint8_t packet_num;
128 int16_t buff_pos;
129 struct batman_ogm_packet *batman_ogm_packet;
130 struct sk_buff *skb;
131
132 if (hard_iface->if_status != IF_ACTIVE)
133 return;
134
135 packet_num = 0;
136 buff_pos = 0;
137 batman_ogm_packet = (struct batman_ogm_packet *)forw_packet->skb->data;
138
139 /* adjust all flags and log packets */
Marek Lindner01c42242011-11-28 21:31:55 +0800140 while (bat_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
141 batman_ogm_packet->tt_num_changes)) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200142
143 /* we might have aggregated direct link packets with an
144 * ordinary base packet */
145 if ((forw_packet->direct_link_flags & (1 << packet_num)) &&
146 (forw_packet->if_incoming == hard_iface))
147 batman_ogm_packet->flags |= DIRECTLINK;
148 else
149 batman_ogm_packet->flags &= ~DIRECTLINK;
150
151 fwd_str = (packet_num > 0 ? "Forwarding" : (forw_packet->own ?
152 "Sending own" :
153 "Forwarding"));
154 bat_dbg(DBG_BATMAN, bat_priv,
Antonio Quartullic97c72b2012-02-26 15:39:41 +0100155 "%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 +0200156 fwd_str, (packet_num > 0 ? "aggregated " : ""),
157 batman_ogm_packet->orig,
158 ntohl(batman_ogm_packet->seqno),
Sven Eckelmann76543d12011-11-20 15:47:38 +0100159 batman_ogm_packet->tq, batman_ogm_packet->header.ttl,
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200160 (batman_ogm_packet->flags & DIRECTLINK ?
161 "on" : "off"),
162 batman_ogm_packet->ttvn, hard_iface->net_dev->name,
163 hard_iface->net_dev->dev_addr);
164
Marek Lindner76e3d7f2012-02-07 17:20:50 +0800165 buff_pos += BATMAN_OGM_HLEN +
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200166 tt_len(batman_ogm_packet->tt_num_changes);
167 packet_num++;
168 batman_ogm_packet = (struct batman_ogm_packet *)
169 (forw_packet->skb->data + buff_pos);
170 }
171
172 /* create clone because function is called more than once */
173 skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
174 if (skb)
175 send_skb_packet(skb, hard_iface, broadcast_addr);
176}
177
178/* send a batman ogm packet */
Marek Lindner01c42242011-11-28 21:31:55 +0800179static void bat_iv_ogm_emit(struct forw_packet *forw_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200180{
181 struct hard_iface *hard_iface;
182 struct net_device *soft_iface;
183 struct bat_priv *bat_priv;
184 struct hard_iface *primary_if = NULL;
185 struct batman_ogm_packet *batman_ogm_packet;
186 unsigned char directlink;
187
188 batman_ogm_packet = (struct batman_ogm_packet *)
189 (forw_packet->skb->data);
190 directlink = (batman_ogm_packet->flags & DIRECTLINK ? 1 : 0);
191
192 if (!forw_packet->if_incoming) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100193 pr_err("Error - can't forward packet: incoming iface not specified\n");
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200194 goto out;
195 }
196
197 soft_iface = forw_packet->if_incoming->soft_iface;
198 bat_priv = netdev_priv(soft_iface);
199
200 if (forw_packet->if_incoming->if_status != IF_ACTIVE)
201 goto out;
202
203 primary_if = primary_if_get_selected(bat_priv);
204 if (!primary_if)
205 goto out;
206
207 /* multihomed peer assumed */
208 /* non-primary OGMs are only broadcasted on their interface */
Sven Eckelmann76543d12011-11-20 15:47:38 +0100209 if ((directlink && (batman_ogm_packet->header.ttl == 1)) ||
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200210 (forw_packet->own && (forw_packet->if_incoming != primary_if))) {
211
212 /* FIXME: what about aggregated packets ? */
213 bat_dbg(DBG_BATMAN, bat_priv,
Antonio Quartullic97c72b2012-02-26 15:39:41 +0100214 "%s packet (originator %pM, seqno %u, TTL %d) on interface %s [%pM]\n",
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200215 (forw_packet->own ? "Sending own" : "Forwarding"),
216 batman_ogm_packet->orig,
217 ntohl(batman_ogm_packet->seqno),
Sven Eckelmann76543d12011-11-20 15:47:38 +0100218 batman_ogm_packet->header.ttl,
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200219 forw_packet->if_incoming->net_dev->name,
220 forw_packet->if_incoming->net_dev->dev_addr);
221
222 /* skb is only used once and than forw_packet is free'd */
223 send_skb_packet(forw_packet->skb, forw_packet->if_incoming,
224 broadcast_addr);
225 forw_packet->skb = NULL;
226
227 goto out;
228 }
229
230 /* broadcast on every interface */
231 rcu_read_lock();
232 list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
233 if (hard_iface->soft_iface != soft_iface)
234 continue;
235
Marek Lindner01c42242011-11-28 21:31:55 +0800236 bat_iv_ogm_send_to_if(forw_packet, hard_iface);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200237 }
238 rcu_read_unlock();
239
240out:
241 if (primary_if)
242 hardif_free_ref(primary_if);
243}
244
245/* return true if new_packet can be aggregated with forw_packet */
Marek Lindner01c42242011-11-28 21:31:55 +0800246static bool bat_iv_ogm_can_aggregate(const struct batman_ogm_packet
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200247 *new_batman_ogm_packet,
Marek Lindner01c42242011-11-28 21:31:55 +0800248 struct bat_priv *bat_priv,
249 int packet_len, unsigned long send_time,
250 bool directlink,
251 const struct hard_iface *if_incoming,
252 const struct forw_packet *forw_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200253{
254 struct batman_ogm_packet *batman_ogm_packet;
255 int aggregated_bytes = forw_packet->packet_len + packet_len;
256 struct hard_iface *primary_if = NULL;
257 bool res = false;
258
259 batman_ogm_packet = (struct batman_ogm_packet *)forw_packet->skb->data;
260
261 /**
262 * we can aggregate the current packet to this aggregated packet
263 * if:
264 *
265 * - the send time is within our MAX_AGGREGATION_MS time
266 * - the resulting packet wont be bigger than
267 * MAX_AGGREGATION_BYTES
268 */
269
270 if (time_before(send_time, forw_packet->send_time) &&
271 time_after_eq(send_time + msecs_to_jiffies(MAX_AGGREGATION_MS),
272 forw_packet->send_time) &&
273 (aggregated_bytes <= MAX_AGGREGATION_BYTES)) {
274
275 /**
276 * check aggregation compatibility
277 * -> direct link packets are broadcasted on
278 * their interface only
279 * -> aggregate packet if the current packet is
280 * a "global" packet as well as the base
281 * packet
282 */
283
284 primary_if = primary_if_get_selected(bat_priv);
285 if (!primary_if)
286 goto out;
287
288 /* packets without direct link flag and high TTL
289 * are flooded through the net */
290 if ((!directlink) &&
291 (!(batman_ogm_packet->flags & DIRECTLINK)) &&
Sven Eckelmann76543d12011-11-20 15:47:38 +0100292 (batman_ogm_packet->header.ttl != 1) &&
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200293
294 /* own packets originating non-primary
295 * interfaces leave only that interface */
296 ((!forw_packet->own) ||
297 (forw_packet->if_incoming == primary_if))) {
298 res = true;
299 goto out;
300 }
301
302 /* if the incoming packet is sent via this one
303 * interface only - we still can aggregate */
304 if ((directlink) &&
Sven Eckelmann76543d12011-11-20 15:47:38 +0100305 (new_batman_ogm_packet->header.ttl == 1) &&
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200306 (forw_packet->if_incoming == if_incoming) &&
307
308 /* packets from direct neighbors or
309 * own secondary interface packets
310 * (= secondary interface packets in general) */
311 (batman_ogm_packet->flags & DIRECTLINK ||
312 (forw_packet->own &&
313 forw_packet->if_incoming != primary_if))) {
314 res = true;
315 goto out;
316 }
317 }
318
319out:
320 if (primary_if)
321 hardif_free_ref(primary_if);
322 return res;
323}
324
325/* create a new aggregated packet and add this packet to it */
Marek Lindner01c42242011-11-28 21:31:55 +0800326static void bat_iv_ogm_aggregate_new(const unsigned char *packet_buff,
327 int packet_len, unsigned long send_time,
328 bool direct_link,
329 struct hard_iface *if_incoming,
330 int own_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200331{
332 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
333 struct forw_packet *forw_packet_aggr;
334 unsigned char *skb_buff;
335
336 if (!atomic_inc_not_zero(&if_incoming->refcount))
337 return;
338
339 /* own packet should always be scheduled */
340 if (!own_packet) {
341 if (!atomic_dec_not_zero(&bat_priv->batman_queue_left)) {
342 bat_dbg(DBG_BATMAN, bat_priv,
343 "batman packet queue full\n");
344 goto out;
345 }
346 }
347
348 forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC);
349 if (!forw_packet_aggr) {
350 if (!own_packet)
351 atomic_inc(&bat_priv->batman_queue_left);
352 goto out;
353 }
354
355 if ((atomic_read(&bat_priv->aggregated_ogms)) &&
356 (packet_len < MAX_AGGREGATION_BYTES))
357 forw_packet_aggr->skb = dev_alloc_skb(MAX_AGGREGATION_BYTES +
Antonio Quartulli0d125072012-02-18 11:27:34 +0100358 ETH_HLEN);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200359 else
Antonio Quartulli0d125072012-02-18 11:27:34 +0100360 forw_packet_aggr->skb = dev_alloc_skb(packet_len + ETH_HLEN);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200361
362 if (!forw_packet_aggr->skb) {
363 if (!own_packet)
364 atomic_inc(&bat_priv->batman_queue_left);
365 kfree(forw_packet_aggr);
366 goto out;
367 }
Antonio Quartulli0d125072012-02-18 11:27:34 +0100368 skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200369
370 INIT_HLIST_NODE(&forw_packet_aggr->list);
371
372 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
373 forw_packet_aggr->packet_len = packet_len;
374 memcpy(skb_buff, packet_buff, packet_len);
375
376 forw_packet_aggr->own = own_packet;
377 forw_packet_aggr->if_incoming = if_incoming;
378 forw_packet_aggr->num_packets = 0;
379 forw_packet_aggr->direct_link_flags = NO_FLAGS;
380 forw_packet_aggr->send_time = send_time;
381
382 /* save packet direct link flag status */
383 if (direct_link)
384 forw_packet_aggr->direct_link_flags |= 1;
385
386 /* add new packet to packet list */
387 spin_lock_bh(&bat_priv->forw_bat_list_lock);
388 hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list);
389 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
390
391 /* start timer for this packet */
392 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
393 send_outstanding_bat_ogm_packet);
394 queue_delayed_work(bat_event_workqueue,
395 &forw_packet_aggr->delayed_work,
396 send_time - jiffies);
397
398 return;
399out:
400 hardif_free_ref(if_incoming);
401}
402
403/* aggregate a new packet into the existing ogm packet */
Marek Lindner01c42242011-11-28 21:31:55 +0800404static void bat_iv_ogm_aggregate(struct forw_packet *forw_packet_aggr,
405 const unsigned char *packet_buff,
406 int packet_len, bool direct_link)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200407{
408 unsigned char *skb_buff;
409
410 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
411 memcpy(skb_buff, packet_buff, packet_len);
412 forw_packet_aggr->packet_len += packet_len;
413 forw_packet_aggr->num_packets++;
414
415 /* save packet direct link flag status */
416 if (direct_link)
417 forw_packet_aggr->direct_link_flags |=
418 (1 << forw_packet_aggr->num_packets);
419}
420
Marek Lindner01c42242011-11-28 21:31:55 +0800421static void bat_iv_ogm_queue_add(struct bat_priv *bat_priv,
422 unsigned char *packet_buff,
423 int packet_len, struct hard_iface *if_incoming,
424 int own_packet, unsigned long send_time)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200425{
426 /**
427 * _aggr -> pointer to the packet we want to aggregate with
428 * _pos -> pointer to the position in the queue
429 */
430 struct forw_packet *forw_packet_aggr = NULL, *forw_packet_pos = NULL;
431 struct hlist_node *tmp_node;
432 struct batman_ogm_packet *batman_ogm_packet;
433 bool direct_link;
434
435 batman_ogm_packet = (struct batman_ogm_packet *)packet_buff;
436 direct_link = batman_ogm_packet->flags & DIRECTLINK ? 1 : 0;
437
438 /* find position for the packet in the forward queue */
439 spin_lock_bh(&bat_priv->forw_bat_list_lock);
440 /* own packets are not to be aggregated */
441 if ((atomic_read(&bat_priv->aggregated_ogms)) && (!own_packet)) {
442 hlist_for_each_entry(forw_packet_pos, tmp_node,
443 &bat_priv->forw_bat_list, list) {
Marek Lindner01c42242011-11-28 21:31:55 +0800444 if (bat_iv_ogm_can_aggregate(batman_ogm_packet,
445 bat_priv, packet_len,
446 send_time, direct_link,
447 if_incoming,
448 forw_packet_pos)) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200449 forw_packet_aggr = forw_packet_pos;
450 break;
451 }
452 }
453 }
454
455 /* nothing to aggregate with - either aggregation disabled or no
456 * suitable aggregation packet found */
457 if (!forw_packet_aggr) {
458 /* the following section can run without the lock */
459 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
460
461 /**
462 * if we could not aggregate this packet with one of the others
463 * we hold it back for a while, so that it might be aggregated
464 * later on
465 */
466 if ((!own_packet) &&
467 (atomic_read(&bat_priv->aggregated_ogms)))
468 send_time += msecs_to_jiffies(MAX_AGGREGATION_MS);
469
Marek Lindner01c42242011-11-28 21:31:55 +0800470 bat_iv_ogm_aggregate_new(packet_buff, packet_len,
471 send_time, direct_link,
472 if_incoming, own_packet);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200473 } else {
Marek Lindner01c42242011-11-28 21:31:55 +0800474 bat_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
475 packet_len, direct_link);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200476 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
477 }
478}
479
Marek Lindner01c42242011-11-28 21:31:55 +0800480static void bat_iv_ogm_forward(struct orig_node *orig_node,
481 const struct ethhdr *ethhdr,
482 struct batman_ogm_packet *batman_ogm_packet,
483 int directlink, struct hard_iface *if_incoming)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200484{
485 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
486 struct neigh_node *router;
487 uint8_t in_tq, in_ttl, tq_avg = 0;
488 uint8_t tt_num_changes;
489
Sven Eckelmann76543d12011-11-20 15:47:38 +0100490 if (batman_ogm_packet->header.ttl <= 1) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200491 bat_dbg(DBG_BATMAN, bat_priv, "ttl exceeded\n");
492 return;
493 }
494
495 router = orig_node_get_router(orig_node);
496
497 in_tq = batman_ogm_packet->tq;
Sven Eckelmann76543d12011-11-20 15:47:38 +0100498 in_ttl = batman_ogm_packet->header.ttl;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200499 tt_num_changes = batman_ogm_packet->tt_num_changes;
500
Sven Eckelmann76543d12011-11-20 15:47:38 +0100501 batman_ogm_packet->header.ttl--;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200502 memcpy(batman_ogm_packet->prev_sender, ethhdr->h_source, ETH_ALEN);
503
504 /* rebroadcast tq of our best ranking neighbor to ensure the rebroadcast
505 * of our best tq value */
506 if (router && router->tq_avg != 0) {
507
508 /* rebroadcast ogm of best ranking neighbor as is */
509 if (!compare_eth(router->addr, ethhdr->h_source)) {
510 batman_ogm_packet->tq = router->tq_avg;
511
512 if (router->last_ttl)
Sven Eckelmann76543d12011-11-20 15:47:38 +0100513 batman_ogm_packet->header.ttl =
514 router->last_ttl - 1;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200515 }
516
517 tq_avg = router->tq_avg;
518 }
519
520 if (router)
521 neigh_node_free_ref(router);
522
523 /* apply hop penalty */
524 batman_ogm_packet->tq = hop_penalty(batman_ogm_packet->tq, bat_priv);
525
526 bat_dbg(DBG_BATMAN, bat_priv,
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100527 "Forwarding packet: tq_orig: %i, tq_avg: %i, tq_forw: %i, ttl_orig: %i, ttl_forw: %i\n",
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200528 in_tq, tq_avg, batman_ogm_packet->tq, in_ttl - 1,
Sven Eckelmann76543d12011-11-20 15:47:38 +0100529 batman_ogm_packet->header.ttl);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200530
531 batman_ogm_packet->seqno = htonl(batman_ogm_packet->seqno);
532 batman_ogm_packet->tt_crc = htons(batman_ogm_packet->tt_crc);
533
534 /* switch of primaries first hop flag when forwarding */
535 batman_ogm_packet->flags &= ~PRIMARIES_FIRST_HOP;
536 if (directlink)
537 batman_ogm_packet->flags |= DIRECTLINK;
538 else
539 batman_ogm_packet->flags &= ~DIRECTLINK;
540
Marek Lindner01c42242011-11-28 21:31:55 +0800541 bat_iv_ogm_queue_add(bat_priv, (unsigned char *)batman_ogm_packet,
Marek Lindner76e3d7f2012-02-07 17:20:50 +0800542 BATMAN_OGM_HLEN + tt_len(tt_num_changes),
Marek Lindner01c42242011-11-28 21:31:55 +0800543 if_incoming, 0, bat_iv_ogm_fwd_send_time());
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200544}
545
Marek Lindner01c42242011-11-28 21:31:55 +0800546static void bat_iv_ogm_schedule(struct hard_iface *hard_iface,
547 int tt_num_changes)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200548{
549 struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
550 struct batman_ogm_packet *batman_ogm_packet;
551 struct hard_iface *primary_if;
552 int vis_server;
553
554 vis_server = atomic_read(&bat_priv->vis_mode);
555 primary_if = primary_if_get_selected(bat_priv);
556
557 batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff;
558
559 /* change sequence number to network order */
560 batman_ogm_packet->seqno =
561 htonl((uint32_t)atomic_read(&hard_iface->seqno));
562
563 batman_ogm_packet->ttvn = atomic_read(&bat_priv->ttvn);
564 batman_ogm_packet->tt_crc = htons((uint16_t)
565 atomic_read(&bat_priv->tt_crc));
566 if (tt_num_changes >= 0)
567 batman_ogm_packet->tt_num_changes = tt_num_changes;
568
569 if (vis_server == VIS_TYPE_SERVER_SYNC)
570 batman_ogm_packet->flags |= VIS_SERVER;
571 else
572 batman_ogm_packet->flags &= ~VIS_SERVER;
573
574 if ((hard_iface == primary_if) &&
575 (atomic_read(&bat_priv->gw_mode) == GW_MODE_SERVER))
576 batman_ogm_packet->gw_flags =
577 (uint8_t)atomic_read(&bat_priv->gw_bandwidth);
578 else
579 batman_ogm_packet->gw_flags = NO_FLAGS;
580
581 atomic_inc(&hard_iface->seqno);
582
583 slide_own_bcast_window(hard_iface);
Marek Lindner01c42242011-11-28 21:31:55 +0800584 bat_iv_ogm_queue_add(bat_priv, hard_iface->packet_buff,
585 hard_iface->packet_len, hard_iface, 1,
586 bat_iv_ogm_emit_send_time(bat_priv));
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200587
588 if (primary_if)
589 hardif_free_ref(primary_if);
590}
591
Marek Lindner01c42242011-11-28 21:31:55 +0800592static void bat_iv_ogm_orig_update(struct bat_priv *bat_priv,
593 struct orig_node *orig_node,
594 const struct ethhdr *ethhdr,
595 const struct batman_ogm_packet
Marek Lindnerfc957272011-07-30 12:04:12 +0200596 *batman_ogm_packet,
Marek Lindner01c42242011-11-28 21:31:55 +0800597 struct hard_iface *if_incoming,
598 const unsigned char *tt_buff,
599 int is_duplicate)
Marek Lindnerfc957272011-07-30 12:04:12 +0200600{
601 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
602 struct neigh_node *router = NULL;
603 struct orig_node *orig_node_tmp;
604 struct hlist_node *node;
605 uint8_t bcast_own_sum_orig, bcast_own_sum_neigh;
606
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100607 bat_dbg(DBG_BATMAN, bat_priv,
608 "update_originator(): Searching and updating originator entry of received packet\n");
Marek Lindnerfc957272011-07-30 12:04:12 +0200609
610 rcu_read_lock();
611 hlist_for_each_entry_rcu(tmp_neigh_node, node,
612 &orig_node->neigh_list, list) {
613 if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
614 (tmp_neigh_node->if_incoming == if_incoming) &&
615 atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
616 if (neigh_node)
617 neigh_node_free_ref(neigh_node);
618 neigh_node = tmp_neigh_node;
619 continue;
620 }
621
622 if (is_duplicate)
623 continue;
624
625 spin_lock_bh(&tmp_neigh_node->tq_lock);
626 ring_buffer_set(tmp_neigh_node->tq_recv,
627 &tmp_neigh_node->tq_index, 0);
628 tmp_neigh_node->tq_avg =
629 ring_buffer_avg(tmp_neigh_node->tq_recv);
630 spin_unlock_bh(&tmp_neigh_node->tq_lock);
631 }
632
633 if (!neigh_node) {
634 struct orig_node *orig_tmp;
635
636 orig_tmp = get_orig_node(bat_priv, ethhdr->h_source);
637 if (!orig_tmp)
638 goto unlock;
639
640 neigh_node = create_neighbor(orig_node, orig_tmp,
641 ethhdr->h_source, if_incoming);
642
643 orig_node_free_ref(orig_tmp);
644 if (!neigh_node)
645 goto unlock;
646 } else
647 bat_dbg(DBG_BATMAN, bat_priv,
648 "Updating existing last-hop neighbor of originator\n");
649
650 rcu_read_unlock();
651
652 orig_node->flags = batman_ogm_packet->flags;
653 neigh_node->last_valid = jiffies;
654
655 spin_lock_bh(&neigh_node->tq_lock);
656 ring_buffer_set(neigh_node->tq_recv,
657 &neigh_node->tq_index,
658 batman_ogm_packet->tq);
659 neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
660 spin_unlock_bh(&neigh_node->tq_lock);
661
662 if (!is_duplicate) {
Sven Eckelmann76543d12011-11-20 15:47:38 +0100663 orig_node->last_ttl = batman_ogm_packet->header.ttl;
664 neigh_node->last_ttl = batman_ogm_packet->header.ttl;
Marek Lindnerfc957272011-07-30 12:04:12 +0200665 }
666
667 bonding_candidate_add(orig_node, neigh_node);
668
669 /* if this neighbor already is our next hop there is nothing
670 * to change */
671 router = orig_node_get_router(orig_node);
672 if (router == neigh_node)
673 goto update_tt;
674
675 /* if this neighbor does not offer a better TQ we won't consider it */
676 if (router && (router->tq_avg > neigh_node->tq_avg))
677 goto update_tt;
678
679 /* if the TQ is the same and the link not more symmetric we
680 * won't consider it either */
681 if (router && (neigh_node->tq_avg == router->tq_avg)) {
682 orig_node_tmp = router->orig_node;
683 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
684 bcast_own_sum_orig =
685 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
686 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
687
688 orig_node_tmp = neigh_node->orig_node;
689 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
690 bcast_own_sum_neigh =
691 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
692 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
693
694 if (bcast_own_sum_orig >= bcast_own_sum_neigh)
695 goto update_tt;
696 }
697
698 update_route(bat_priv, orig_node, neigh_node);
699
700update_tt:
701 /* I have to check for transtable changes only if the OGM has been
702 * sent through a primary interface */
703 if (((batman_ogm_packet->orig != ethhdr->h_source) &&
Sven Eckelmann76543d12011-11-20 15:47:38 +0100704 (batman_ogm_packet->header.ttl > 2)) ||
Marek Lindnerfc957272011-07-30 12:04:12 +0200705 (batman_ogm_packet->flags & PRIMARIES_FIRST_HOP))
706 tt_update_orig(bat_priv, orig_node, tt_buff,
707 batman_ogm_packet->tt_num_changes,
708 batman_ogm_packet->ttvn,
709 batman_ogm_packet->tt_crc);
710
711 if (orig_node->gw_flags != batman_ogm_packet->gw_flags)
712 gw_node_update(bat_priv, orig_node,
713 batman_ogm_packet->gw_flags);
714
715 orig_node->gw_flags = batman_ogm_packet->gw_flags;
716
717 /* restart gateway selection if fast or late switching was enabled */
718 if ((orig_node->gw_flags) &&
719 (atomic_read(&bat_priv->gw_mode) == GW_MODE_CLIENT) &&
720 (atomic_read(&bat_priv->gw_sel_class) > 2))
721 gw_check_election(bat_priv, orig_node);
722
723 goto out;
724
725unlock:
726 rcu_read_unlock();
727out:
728 if (neigh_node)
729 neigh_node_free_ref(neigh_node);
730 if (router)
731 neigh_node_free_ref(router);
732}
733
Marek Lindner01c42242011-11-28 21:31:55 +0800734static int bat_iv_ogm_calc_tq(struct orig_node *orig_node,
735 struct orig_node *orig_neigh_node,
736 struct batman_ogm_packet *batman_ogm_packet,
737 struct hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +0200738{
739 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
740 struct neigh_node *neigh_node = NULL, *tmp_neigh_node;
741 struct hlist_node *node;
742 uint8_t total_count;
743 uint8_t orig_eq_count, neigh_rq_count, tq_own;
744 int tq_asym_penalty, ret = 0;
745
746 /* find corresponding one hop neighbor */
747 rcu_read_lock();
748 hlist_for_each_entry_rcu(tmp_neigh_node, node,
749 &orig_neigh_node->neigh_list, list) {
750
751 if (!compare_eth(tmp_neigh_node->addr, orig_neigh_node->orig))
752 continue;
753
754 if (tmp_neigh_node->if_incoming != if_incoming)
755 continue;
756
757 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
758 continue;
759
760 neigh_node = tmp_neigh_node;
761 break;
762 }
763 rcu_read_unlock();
764
765 if (!neigh_node)
766 neigh_node = create_neighbor(orig_neigh_node,
767 orig_neigh_node,
768 orig_neigh_node->orig,
769 if_incoming);
770
771 if (!neigh_node)
772 goto out;
773
774 /* if orig_node is direct neighbor update neigh_node last_valid */
775 if (orig_node == orig_neigh_node)
776 neigh_node->last_valid = jiffies;
777
778 orig_node->last_valid = jiffies;
779
780 /* find packet count of corresponding one hop neighbor */
781 spin_lock_bh(&orig_node->ogm_cnt_lock);
782 orig_eq_count = orig_neigh_node->bcast_own_sum[if_incoming->if_num];
783 neigh_rq_count = neigh_node->real_packet_count;
784 spin_unlock_bh(&orig_node->ogm_cnt_lock);
785
786 /* pay attention to not get a value bigger than 100 % */
787 total_count = (orig_eq_count > neigh_rq_count ?
788 neigh_rq_count : orig_eq_count);
789
790 /* if we have too few packets (too less data) we set tq_own to zero */
791 /* if we receive too few packets it is not considered bidirectional */
792 if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
793 (neigh_rq_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
794 tq_own = 0;
795 else
796 /* neigh_node->real_packet_count is never zero as we
797 * only purge old information when getting new
798 * information */
799 tq_own = (TQ_MAX_VALUE * total_count) / neigh_rq_count;
800
Sven Eckelmann21a12362012-03-07 09:07:46 +0100801 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
Marek Lindnerfc957272011-07-30 12:04:12 +0200802 * affect the nearly-symmetric links only a little, but
803 * punishes asymmetric links more. This will give a value
804 * between 0 and TQ_MAX_VALUE
805 */
806 tq_asym_penalty = TQ_MAX_VALUE - (TQ_MAX_VALUE *
807 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
808 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
809 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count)) /
810 (TQ_LOCAL_WINDOW_SIZE *
811 TQ_LOCAL_WINDOW_SIZE *
812 TQ_LOCAL_WINDOW_SIZE);
813
814 batman_ogm_packet->tq = ((batman_ogm_packet->tq * tq_own
815 * tq_asym_penalty) /
816 (TQ_MAX_VALUE * TQ_MAX_VALUE));
817
818 bat_dbg(DBG_BATMAN, bat_priv,
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100819 "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 +0200820 orig_node->orig, orig_neigh_node->orig, total_count,
821 neigh_rq_count, tq_own, tq_asym_penalty, batman_ogm_packet->tq);
822
823 /* if link has the minimum required transmission quality
824 * consider it bidirectional */
825 if (batman_ogm_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
826 ret = 1;
827
828out:
829 if (neigh_node)
830 neigh_node_free_ref(neigh_node);
831 return ret;
832}
833
834/* processes a batman packet for all interfaces, adjusts the sequence number and
835 * finds out whether it is a duplicate.
836 * returns:
837 * 1 the packet is a duplicate
838 * 0 the packet has not yet been received
839 * -1 the packet is old and has been received while the seqno window
840 * was protected. Caller should drop it.
841 */
Marek Lindner01c42242011-11-28 21:31:55 +0800842static int bat_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
843 const struct batman_ogm_packet
Marek Lindnerfc957272011-07-30 12:04:12 +0200844 *batman_ogm_packet,
Marek Lindner01c42242011-11-28 21:31:55 +0800845 const struct hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +0200846{
847 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
848 struct orig_node *orig_node;
849 struct neigh_node *tmp_neigh_node;
850 struct hlist_node *node;
851 int is_duplicate = 0;
852 int32_t seq_diff;
853 int need_update = 0;
854 int set_mark, ret = -1;
855
856 orig_node = get_orig_node(bat_priv, batman_ogm_packet->orig);
857 if (!orig_node)
858 return 0;
859
860 spin_lock_bh(&orig_node->ogm_cnt_lock);
861 seq_diff = batman_ogm_packet->seqno - orig_node->last_real_seqno;
862
863 /* signalize caller that the packet is to be dropped. */
864 if (window_protected(bat_priv, seq_diff,
865 &orig_node->batman_seqno_reset))
866 goto out;
867
868 rcu_read_lock();
869 hlist_for_each_entry_rcu(tmp_neigh_node, node,
870 &orig_node->neigh_list, list) {
871
Sven Eckelmann0079d2c2012-02-04 17:34:52 +0100872 is_duplicate |= bat_test_bit(tmp_neigh_node->real_bits,
873 orig_node->last_real_seqno,
874 batman_ogm_packet->seqno);
Marek Lindnerfc957272011-07-30 12:04:12 +0200875
876 if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
877 (tmp_neigh_node->if_incoming == if_incoming))
878 set_mark = 1;
879 else
880 set_mark = 0;
881
882 /* if the window moved, set the update flag. */
883 need_update |= bit_get_packet(bat_priv,
884 tmp_neigh_node->real_bits,
885 seq_diff, set_mark);
886
887 tmp_neigh_node->real_packet_count =
Sven Eckelmann0079d2c2012-02-04 17:34:52 +0100888 bitmap_weight(tmp_neigh_node->real_bits,
889 TQ_LOCAL_WINDOW_SIZE);
Marek Lindnerfc957272011-07-30 12:04:12 +0200890 }
891 rcu_read_unlock();
892
893 if (need_update) {
894 bat_dbg(DBG_BATMAN, bat_priv,
Antonio Quartullic97c72b2012-02-26 15:39:41 +0100895 "updating last_seqno: old %u, new %u\n",
Marek Lindnerfc957272011-07-30 12:04:12 +0200896 orig_node->last_real_seqno, batman_ogm_packet->seqno);
897 orig_node->last_real_seqno = batman_ogm_packet->seqno;
898 }
899
900 ret = is_duplicate;
901
902out:
903 spin_unlock_bh(&orig_node->ogm_cnt_lock);
904 orig_node_free_ref(orig_node);
905 return ret;
906}
907
Marek Lindner01c42242011-11-28 21:31:55 +0800908static void bat_iv_ogm_process(const struct ethhdr *ethhdr,
909 struct batman_ogm_packet *batman_ogm_packet,
910 const unsigned char *tt_buff,
911 struct hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +0200912{
913 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
914 struct hard_iface *hard_iface;
915 struct orig_node *orig_neigh_node, *orig_node;
916 struct neigh_node *router = NULL, *router_router = NULL;
917 struct neigh_node *orig_neigh_router = NULL;
918 int has_directlink_flag;
919 int is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
920 int is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
921 int is_duplicate;
922 uint32_t if_incoming_seqno;
923
924 /* Silently drop when the batman packet is actually not a
925 * correct packet.
926 *
927 * This might happen if a packet is padded (e.g. Ethernet has a
928 * minimum frame length of 64 byte) and the aggregation interprets
929 * it as an additional length.
930 *
931 * TODO: A more sane solution would be to have a bit in the
932 * batman_ogm_packet to detect whether the packet is the last
933 * packet in an aggregation. Here we expect that the padding
934 * is always zero (or not 0x01)
935 */
Marek Lindner1eeb4792012-02-07 17:20:51 +0800936 if (batman_ogm_packet->header.packet_type != BAT_IV_OGM)
Marek Lindnerfc957272011-07-30 12:04:12 +0200937 return;
938
939 /* could be changed by schedule_own_packet() */
940 if_incoming_seqno = atomic_read(&if_incoming->seqno);
941
942 has_directlink_flag = (batman_ogm_packet->flags & DIRECTLINK ? 1 : 0);
943
944 is_single_hop_neigh = (compare_eth(ethhdr->h_source,
945 batman_ogm_packet->orig) ? 1 : 0);
946
947 bat_dbg(DBG_BATMAN, bat_priv,
Antonio Quartullic97c72b2012-02-26 15:39:41 +0100948 "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 +0200949 ethhdr->h_source, if_incoming->net_dev->name,
950 if_incoming->net_dev->dev_addr, batman_ogm_packet->orig,
951 batman_ogm_packet->prev_sender, batman_ogm_packet->seqno,
952 batman_ogm_packet->ttvn, batman_ogm_packet->tt_crc,
953 batman_ogm_packet->tt_num_changes, batman_ogm_packet->tq,
Sven Eckelmann76543d12011-11-20 15:47:38 +0100954 batman_ogm_packet->header.ttl,
955 batman_ogm_packet->header.version, has_directlink_flag);
Marek Lindnerfc957272011-07-30 12:04:12 +0200956
957 rcu_read_lock();
958 list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
959 if (hard_iface->if_status != IF_ACTIVE)
960 continue;
961
962 if (hard_iface->soft_iface != if_incoming->soft_iface)
963 continue;
964
965 if (compare_eth(ethhdr->h_source,
966 hard_iface->net_dev->dev_addr))
967 is_my_addr = 1;
968
969 if (compare_eth(batman_ogm_packet->orig,
970 hard_iface->net_dev->dev_addr))
971 is_my_orig = 1;
972
973 if (compare_eth(batman_ogm_packet->prev_sender,
974 hard_iface->net_dev->dev_addr))
975 is_my_oldorig = 1;
976
977 if (is_broadcast_ether_addr(ethhdr->h_source))
978 is_broadcast = 1;
979 }
980 rcu_read_unlock();
981
Sven Eckelmann76543d12011-11-20 15:47:38 +0100982 if (batman_ogm_packet->header.version != COMPAT_VERSION) {
Marek Lindnerfc957272011-07-30 12:04:12 +0200983 bat_dbg(DBG_BATMAN, bat_priv,
984 "Drop packet: incompatible batman version (%i)\n",
Sven Eckelmann76543d12011-11-20 15:47:38 +0100985 batman_ogm_packet->header.version);
Marek Lindnerfc957272011-07-30 12:04:12 +0200986 return;
987 }
988
989 if (is_my_addr) {
990 bat_dbg(DBG_BATMAN, bat_priv,
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100991 "Drop packet: received my own broadcast (sender: %pM)\n",
Marek Lindnerfc957272011-07-30 12:04:12 +0200992 ethhdr->h_source);
993 return;
994 }
995
996 if (is_broadcast) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100997 bat_dbg(DBG_BATMAN, bat_priv,
998 "Drop packet: ignoring all packets with broadcast source addr (sender: %pM)\n",
999 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001000 return;
1001 }
1002
1003 if (is_my_orig) {
1004 unsigned long *word;
1005 int offset;
1006
1007 orig_neigh_node = get_orig_node(bat_priv, ethhdr->h_source);
1008 if (!orig_neigh_node)
1009 return;
1010
1011 /* neighbor has to indicate direct link and it has to
1012 * come via the corresponding interface */
1013 /* save packet seqno for bidirectional check */
1014 if (has_directlink_flag &&
1015 compare_eth(if_incoming->net_dev->dev_addr,
1016 batman_ogm_packet->orig)) {
1017 offset = if_incoming->if_num * NUM_WORDS;
1018
1019 spin_lock_bh(&orig_neigh_node->ogm_cnt_lock);
1020 word = &(orig_neigh_node->bcast_own[offset]);
Sven Eckelmann0079d2c2012-02-04 17:34:52 +01001021 bat_set_bit(word,
1022 if_incoming_seqno -
Marek Lindnerfc957272011-07-30 12:04:12 +02001023 batman_ogm_packet->seqno - 2);
1024 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
Sven Eckelmann0079d2c2012-02-04 17:34:52 +01001025 bitmap_weight(word, TQ_LOCAL_WINDOW_SIZE);
Marek Lindnerfc957272011-07-30 12:04:12 +02001026 spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock);
1027 }
1028
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001029 bat_dbg(DBG_BATMAN, bat_priv,
1030 "Drop packet: originator packet from myself (via neighbor)\n");
Marek Lindnerfc957272011-07-30 12:04:12 +02001031 orig_node_free_ref(orig_neigh_node);
1032 return;
1033 }
1034
1035 if (is_my_oldorig) {
1036 bat_dbg(DBG_BATMAN, bat_priv,
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001037 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1038 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001039 return;
1040 }
1041
1042 orig_node = get_orig_node(bat_priv, batman_ogm_packet->orig);
1043 if (!orig_node)
1044 return;
1045
Marek Lindner01c42242011-11-28 21:31:55 +08001046 is_duplicate = bat_iv_ogm_update_seqnos(ethhdr, batman_ogm_packet,
1047 if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001048
1049 if (is_duplicate == -1) {
1050 bat_dbg(DBG_BATMAN, bat_priv,
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001051 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1052 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001053 goto out;
1054 }
1055
1056 if (batman_ogm_packet->tq == 0) {
1057 bat_dbg(DBG_BATMAN, bat_priv,
1058 "Drop packet: originator packet with tq equal 0\n");
1059 goto out;
1060 }
1061
1062 router = orig_node_get_router(orig_node);
1063 if (router)
1064 router_router = orig_node_get_router(router->orig_node);
1065
1066 /* avoid temporary routing loops */
1067 if (router && router_router &&
1068 (compare_eth(router->addr, batman_ogm_packet->prev_sender)) &&
1069 !(compare_eth(batman_ogm_packet->orig,
1070 batman_ogm_packet->prev_sender)) &&
1071 (compare_eth(router->addr, router_router->addr))) {
1072 bat_dbg(DBG_BATMAN, bat_priv,
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001073 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1074 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001075 goto out;
1076 }
1077
1078 /* if sender is a direct neighbor the sender mac equals
1079 * originator mac */
1080 orig_neigh_node = (is_single_hop_neigh ?
1081 orig_node :
1082 get_orig_node(bat_priv, ethhdr->h_source));
1083 if (!orig_neigh_node)
1084 goto out;
1085
1086 orig_neigh_router = orig_node_get_router(orig_neigh_node);
1087
1088 /* drop packet if sender is not a direct neighbor and if we
1089 * don't route towards it */
1090 if (!is_single_hop_neigh && (!orig_neigh_router)) {
1091 bat_dbg(DBG_BATMAN, bat_priv,
1092 "Drop packet: OGM via unknown neighbor!\n");
1093 goto out_neigh;
1094 }
1095
Marek Lindner01c42242011-11-28 21:31:55 +08001096 is_bidirectional = bat_iv_ogm_calc_tq(orig_node, orig_neigh_node,
1097 batman_ogm_packet, if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001098
1099 bonding_save_primary(orig_node, orig_neigh_node, batman_ogm_packet);
1100
1101 /* update ranking if it is not a duplicate or has the same
1102 * seqno and similar ttl as the non-duplicate */
1103 if (is_bidirectional &&
1104 (!is_duplicate ||
1105 ((orig_node->last_real_seqno == batman_ogm_packet->seqno) &&
Sven Eckelmann76543d12011-11-20 15:47:38 +01001106 (orig_node->last_ttl - 3 <= batman_ogm_packet->header.ttl))))
Marek Lindner01c42242011-11-28 21:31:55 +08001107 bat_iv_ogm_orig_update(bat_priv, orig_node, ethhdr,
1108 batman_ogm_packet, if_incoming,
1109 tt_buff, is_duplicate);
Marek Lindnerfc957272011-07-30 12:04:12 +02001110
1111 /* is single hop (direct) neighbor */
1112 if (is_single_hop_neigh) {
1113
1114 /* mark direct link on incoming interface */
Marek Lindner01c42242011-11-28 21:31:55 +08001115 bat_iv_ogm_forward(orig_node, ethhdr, batman_ogm_packet,
1116 1, if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001117
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001118 bat_dbg(DBG_BATMAN, bat_priv,
1119 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
Marek Lindnerfc957272011-07-30 12:04:12 +02001120 goto out_neigh;
1121 }
1122
1123 /* multihop originator */
1124 if (!is_bidirectional) {
1125 bat_dbg(DBG_BATMAN, bat_priv,
1126 "Drop packet: not received via bidirectional link\n");
1127 goto out_neigh;
1128 }
1129
1130 if (is_duplicate) {
1131 bat_dbg(DBG_BATMAN, bat_priv,
1132 "Drop packet: duplicate packet received\n");
1133 goto out_neigh;
1134 }
1135
1136 bat_dbg(DBG_BATMAN, bat_priv,
1137 "Forwarding packet: rebroadcast originator packet\n");
Marek Lindner01c42242011-11-28 21:31:55 +08001138 bat_iv_ogm_forward(orig_node, ethhdr, batman_ogm_packet,
1139 0, if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001140
1141out_neigh:
1142 if ((orig_neigh_node) && (!is_single_hop_neigh))
1143 orig_node_free_ref(orig_neigh_node);
1144out:
1145 if (router)
1146 neigh_node_free_ref(router);
1147 if (router_router)
1148 neigh_node_free_ref(router_router);
1149 if (orig_neigh_router)
1150 neigh_node_free_ref(orig_neigh_router);
1151
1152 orig_node_free_ref(orig_node);
1153}
1154
Marek Lindner01c42242011-11-28 21:31:55 +08001155static void bat_iv_ogm_receive(struct hard_iface *if_incoming,
1156 struct sk_buff *skb)
Marek Lindnerfc957272011-07-30 12:04:12 +02001157{
1158 struct batman_ogm_packet *batman_ogm_packet;
Marek Lindner8780dad2011-12-05 04:01:51 +08001159 struct ethhdr *ethhdr;
1160 int buff_pos = 0, packet_len;
1161 unsigned char *tt_buff, *packet_buff;
Marek Lindnerfc957272011-07-30 12:04:12 +02001162
Marek Lindner8780dad2011-12-05 04:01:51 +08001163 packet_len = skb_headlen(skb);
1164 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1165 packet_buff = skb->data;
Marek Lindnerfc957272011-07-30 12:04:12 +02001166 batman_ogm_packet = (struct batman_ogm_packet *)packet_buff;
1167
1168 /* unpack the aggregated packets and process them one by one */
1169 do {
1170 /* network to host order for our 32bit seqno and the
1171 orig_interval */
1172 batman_ogm_packet->seqno = ntohl(batman_ogm_packet->seqno);
1173 batman_ogm_packet->tt_crc = ntohs(batman_ogm_packet->tt_crc);
1174
Marek Lindner76e3d7f2012-02-07 17:20:50 +08001175 tt_buff = packet_buff + buff_pos + BATMAN_OGM_HLEN;
Marek Lindnerfc957272011-07-30 12:04:12 +02001176
Marek Lindner01c42242011-11-28 21:31:55 +08001177 bat_iv_ogm_process(ethhdr, batman_ogm_packet,
1178 tt_buff, if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001179
Marek Lindner76e3d7f2012-02-07 17:20:50 +08001180 buff_pos += BATMAN_OGM_HLEN +
Marek Lindnerfc957272011-07-30 12:04:12 +02001181 tt_len(batman_ogm_packet->tt_num_changes);
1182
1183 batman_ogm_packet = (struct batman_ogm_packet *)
1184 (packet_buff + buff_pos);
Marek Lindner01c42242011-11-28 21:31:55 +08001185 } while (bat_iv_ogm_aggr_packet(buff_pos, packet_len,
1186 batman_ogm_packet->tt_num_changes));
Marek Lindnerfc957272011-07-30 12:04:12 +02001187}
Marek Lindner1c280472011-11-28 17:40:17 +08001188
1189static struct bat_algo_ops batman_iv __read_mostly = {
1190 .name = "BATMAN IV",
Marek Lindnerc2aca022012-02-07 17:20:45 +08001191 .bat_iface_enable = bat_iv_ogm_iface_enable,
Marek Lindner00a50072012-02-07 17:20:47 +08001192 .bat_iface_disable = bat_iv_ogm_iface_disable,
Marek Lindnercd8b78e2012-02-07 17:20:49 +08001193 .bat_primary_iface_set = bat_iv_ogm_primary_iface_set,
Marek Lindner01c42242011-11-28 21:31:55 +08001194 .bat_ogm_update_mac = bat_iv_ogm_update_mac,
1195 .bat_ogm_schedule = bat_iv_ogm_schedule,
1196 .bat_ogm_emit = bat_iv_ogm_emit,
1197 .bat_ogm_receive = bat_iv_ogm_receive,
Marek Lindner1c280472011-11-28 17:40:17 +08001198};
1199
1200int __init bat_iv_init(void)
1201{
1202 return bat_algo_register(&batman_iv);
1203}