blob: 1c483a5dd8082ee5fb3cb49f4903f83147ceb5dd [file] [log] [blame]
Marek Lindnerfc957272011-07-30 12:04:12 +02001/*
2 * Copyright (C) 2007-2011 B.A.T.M.A.N. contributors:
3 *
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 Lindner01c42242011-11-28 21:31:55 +080033static void bat_iv_ogm_init(struct hard_iface *hard_iface)
Marek Lindnerd0b9fd82011-07-30 12:33:33 +020034{
35 struct batman_ogm_packet *batman_ogm_packet;
36
37 hard_iface->packet_len = BATMAN_OGM_LEN;
38 hard_iface->packet_buff = kmalloc(hard_iface->packet_len, GFP_ATOMIC);
39
40 batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff;
Sven Eckelmann76543d12011-11-20 15:47:38 +010041 batman_ogm_packet->header.packet_type = BAT_OGM;
42 batman_ogm_packet->header.version = COMPAT_VERSION;
43 batman_ogm_packet->header.ttl = 2;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +020044 batman_ogm_packet->flags = NO_FLAGS;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +020045 batman_ogm_packet->tq = TQ_MAX_VALUE;
46 batman_ogm_packet->tt_num_changes = 0;
47 batman_ogm_packet->ttvn = 0;
48}
49
Marek Lindner01c42242011-11-28 21:31:55 +080050static void bat_iv_ogm_init_primary(struct hard_iface *hard_iface)
Marek Lindnerd0b9fd82011-07-30 12:33:33 +020051{
52 struct batman_ogm_packet *batman_ogm_packet;
53
54 batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff;
55 batman_ogm_packet->flags = PRIMARIES_FIRST_HOP;
Sven Eckelmann76543d12011-11-20 15:47:38 +010056 batman_ogm_packet->header.ttl = TTL;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +020057}
58
Marek Lindner01c42242011-11-28 21:31:55 +080059static void bat_iv_ogm_update_mac(struct hard_iface *hard_iface)
Marek Lindnerd0b9fd82011-07-30 12:33:33 +020060{
61 struct batman_ogm_packet *batman_ogm_packet;
62
63 batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff;
64 memcpy(batman_ogm_packet->orig,
65 hard_iface->net_dev->dev_addr, ETH_ALEN);
66 memcpy(batman_ogm_packet->prev_sender,
67 hard_iface->net_dev->dev_addr, ETH_ALEN);
68}
69
Marek Lindnerb9dacc52011-08-03 09:09:30 +020070/* when do we schedule our own ogm to be sent */
Marek Lindner01c42242011-11-28 21:31:55 +080071static unsigned long bat_iv_ogm_emit_send_time(const struct bat_priv *bat_priv)
Marek Lindnerb9dacc52011-08-03 09:09:30 +020072{
73 return jiffies + msecs_to_jiffies(
74 atomic_read(&bat_priv->orig_interval) -
75 JITTER + (random32() % 2*JITTER));
76}
77
78/* when do we schedule a ogm packet to be sent */
Marek Lindner01c42242011-11-28 21:31:55 +080079static unsigned long bat_iv_ogm_fwd_send_time(void)
Marek Lindnerb9dacc52011-08-03 09:09:30 +020080{
81 return jiffies + msecs_to_jiffies(random32() % (JITTER/2));
82}
83
84/* apply hop penalty for a normal link */
85static uint8_t hop_penalty(uint8_t tq, const struct bat_priv *bat_priv)
86{
87 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
88 return (tq * (TQ_MAX_VALUE - hop_penalty)) / (TQ_MAX_VALUE);
89}
90
Marek Lindnerfc957272011-07-30 12:04:12 +020091/* is there another aggregated packet here? */
Marek Lindner01c42242011-11-28 21:31:55 +080092static int bat_iv_ogm_aggr_packet(int buff_pos, int packet_len,
93 int tt_num_changes)
Marek Lindnerfc957272011-07-30 12:04:12 +020094{
95 int next_buff_pos = buff_pos + BATMAN_OGM_LEN + tt_len(tt_num_changes);
96
97 return (next_buff_pos <= packet_len) &&
98 (next_buff_pos <= MAX_AGGREGATION_BYTES);
99}
100
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200101/* send a batman ogm to a given interface */
Marek Lindner01c42242011-11-28 21:31:55 +0800102static void bat_iv_ogm_send_to_if(struct forw_packet *forw_packet,
103 struct hard_iface *hard_iface)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200104{
105 struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
106 char *fwd_str;
107 uint8_t packet_num;
108 int16_t buff_pos;
109 struct batman_ogm_packet *batman_ogm_packet;
110 struct sk_buff *skb;
111
112 if (hard_iface->if_status != IF_ACTIVE)
113 return;
114
115 packet_num = 0;
116 buff_pos = 0;
117 batman_ogm_packet = (struct batman_ogm_packet *)forw_packet->skb->data;
118
119 /* adjust all flags and log packets */
Marek Lindner01c42242011-11-28 21:31:55 +0800120 while (bat_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
121 batman_ogm_packet->tt_num_changes)) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200122
123 /* we might have aggregated direct link packets with an
124 * ordinary base packet */
125 if ((forw_packet->direct_link_flags & (1 << packet_num)) &&
126 (forw_packet->if_incoming == hard_iface))
127 batman_ogm_packet->flags |= DIRECTLINK;
128 else
129 batman_ogm_packet->flags &= ~DIRECTLINK;
130
131 fwd_str = (packet_num > 0 ? "Forwarding" : (forw_packet->own ?
132 "Sending own" :
133 "Forwarding"));
134 bat_dbg(DBG_BATMAN, bat_priv,
135 "%s %spacket (originator %pM, seqno %d, TQ %d, TTL %d,"
136 " IDF %s, ttvn %d) on interface %s [%pM]\n",
137 fwd_str, (packet_num > 0 ? "aggregated " : ""),
138 batman_ogm_packet->orig,
139 ntohl(batman_ogm_packet->seqno),
Sven Eckelmann76543d12011-11-20 15:47:38 +0100140 batman_ogm_packet->tq, batman_ogm_packet->header.ttl,
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200141 (batman_ogm_packet->flags & DIRECTLINK ?
142 "on" : "off"),
143 batman_ogm_packet->ttvn, hard_iface->net_dev->name,
144 hard_iface->net_dev->dev_addr);
145
146 buff_pos += BATMAN_OGM_LEN +
147 tt_len(batman_ogm_packet->tt_num_changes);
148 packet_num++;
149 batman_ogm_packet = (struct batman_ogm_packet *)
150 (forw_packet->skb->data + buff_pos);
151 }
152
153 /* create clone because function is called more than once */
154 skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
155 if (skb)
156 send_skb_packet(skb, hard_iface, broadcast_addr);
157}
158
159/* send a batman ogm packet */
Marek Lindner01c42242011-11-28 21:31:55 +0800160static void bat_iv_ogm_emit(struct forw_packet *forw_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200161{
162 struct hard_iface *hard_iface;
163 struct net_device *soft_iface;
164 struct bat_priv *bat_priv;
165 struct hard_iface *primary_if = NULL;
166 struct batman_ogm_packet *batman_ogm_packet;
167 unsigned char directlink;
168
169 batman_ogm_packet = (struct batman_ogm_packet *)
170 (forw_packet->skb->data);
171 directlink = (batman_ogm_packet->flags & DIRECTLINK ? 1 : 0);
172
173 if (!forw_packet->if_incoming) {
174 pr_err("Error - can't forward packet: incoming iface not "
175 "specified\n");
176 goto out;
177 }
178
179 soft_iface = forw_packet->if_incoming->soft_iface;
180 bat_priv = netdev_priv(soft_iface);
181
182 if (forw_packet->if_incoming->if_status != IF_ACTIVE)
183 goto out;
184
185 primary_if = primary_if_get_selected(bat_priv);
186 if (!primary_if)
187 goto out;
188
189 /* multihomed peer assumed */
190 /* non-primary OGMs are only broadcasted on their interface */
Sven Eckelmann76543d12011-11-20 15:47:38 +0100191 if ((directlink && (batman_ogm_packet->header.ttl == 1)) ||
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200192 (forw_packet->own && (forw_packet->if_incoming != primary_if))) {
193
194 /* FIXME: what about aggregated packets ? */
195 bat_dbg(DBG_BATMAN, bat_priv,
196 "%s packet (originator %pM, seqno %d, TTL %d) "
197 "on interface %s [%pM]\n",
198 (forw_packet->own ? "Sending own" : "Forwarding"),
199 batman_ogm_packet->orig,
200 ntohl(batman_ogm_packet->seqno),
Sven Eckelmann76543d12011-11-20 15:47:38 +0100201 batman_ogm_packet->header.ttl,
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200202 forw_packet->if_incoming->net_dev->name,
203 forw_packet->if_incoming->net_dev->dev_addr);
204
205 /* skb is only used once and than forw_packet is free'd */
206 send_skb_packet(forw_packet->skb, forw_packet->if_incoming,
207 broadcast_addr);
208 forw_packet->skb = NULL;
209
210 goto out;
211 }
212
213 /* broadcast on every interface */
214 rcu_read_lock();
215 list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
216 if (hard_iface->soft_iface != soft_iface)
217 continue;
218
Marek Lindner01c42242011-11-28 21:31:55 +0800219 bat_iv_ogm_send_to_if(forw_packet, hard_iface);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200220 }
221 rcu_read_unlock();
222
223out:
224 if (primary_if)
225 hardif_free_ref(primary_if);
226}
227
228/* return true if new_packet can be aggregated with forw_packet */
Marek Lindner01c42242011-11-28 21:31:55 +0800229static bool bat_iv_ogm_can_aggregate(const struct batman_ogm_packet
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200230 *new_batman_ogm_packet,
Marek Lindner01c42242011-11-28 21:31:55 +0800231 struct bat_priv *bat_priv,
232 int packet_len, unsigned long send_time,
233 bool directlink,
234 const struct hard_iface *if_incoming,
235 const struct forw_packet *forw_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200236{
237 struct batman_ogm_packet *batman_ogm_packet;
238 int aggregated_bytes = forw_packet->packet_len + packet_len;
239 struct hard_iface *primary_if = NULL;
240 bool res = false;
241
242 batman_ogm_packet = (struct batman_ogm_packet *)forw_packet->skb->data;
243
244 /**
245 * we can aggregate the current packet to this aggregated packet
246 * if:
247 *
248 * - the send time is within our MAX_AGGREGATION_MS time
249 * - the resulting packet wont be bigger than
250 * MAX_AGGREGATION_BYTES
251 */
252
253 if (time_before(send_time, forw_packet->send_time) &&
254 time_after_eq(send_time + msecs_to_jiffies(MAX_AGGREGATION_MS),
255 forw_packet->send_time) &&
256 (aggregated_bytes <= MAX_AGGREGATION_BYTES)) {
257
258 /**
259 * check aggregation compatibility
260 * -> direct link packets are broadcasted on
261 * their interface only
262 * -> aggregate packet if the current packet is
263 * a "global" packet as well as the base
264 * packet
265 */
266
267 primary_if = primary_if_get_selected(bat_priv);
268 if (!primary_if)
269 goto out;
270
271 /* packets without direct link flag and high TTL
272 * are flooded through the net */
273 if ((!directlink) &&
274 (!(batman_ogm_packet->flags & DIRECTLINK)) &&
Sven Eckelmann76543d12011-11-20 15:47:38 +0100275 (batman_ogm_packet->header.ttl != 1) &&
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200276
277 /* own packets originating non-primary
278 * interfaces leave only that interface */
279 ((!forw_packet->own) ||
280 (forw_packet->if_incoming == primary_if))) {
281 res = true;
282 goto out;
283 }
284
285 /* if the incoming packet is sent via this one
286 * interface only - we still can aggregate */
287 if ((directlink) &&
Sven Eckelmann76543d12011-11-20 15:47:38 +0100288 (new_batman_ogm_packet->header.ttl == 1) &&
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200289 (forw_packet->if_incoming == if_incoming) &&
290
291 /* packets from direct neighbors or
292 * own secondary interface packets
293 * (= secondary interface packets in general) */
294 (batman_ogm_packet->flags & DIRECTLINK ||
295 (forw_packet->own &&
296 forw_packet->if_incoming != primary_if))) {
297 res = true;
298 goto out;
299 }
300 }
301
302out:
303 if (primary_if)
304 hardif_free_ref(primary_if);
305 return res;
306}
307
308/* create a new aggregated packet and add this packet to it */
Marek Lindner01c42242011-11-28 21:31:55 +0800309static void bat_iv_ogm_aggregate_new(const unsigned char *packet_buff,
310 int packet_len, unsigned long send_time,
311 bool direct_link,
312 struct hard_iface *if_incoming,
313 int own_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200314{
315 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
316 struct forw_packet *forw_packet_aggr;
317 unsigned char *skb_buff;
318
319 if (!atomic_inc_not_zero(&if_incoming->refcount))
320 return;
321
322 /* own packet should always be scheduled */
323 if (!own_packet) {
324 if (!atomic_dec_not_zero(&bat_priv->batman_queue_left)) {
325 bat_dbg(DBG_BATMAN, bat_priv,
326 "batman packet queue full\n");
327 goto out;
328 }
329 }
330
331 forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC);
332 if (!forw_packet_aggr) {
333 if (!own_packet)
334 atomic_inc(&bat_priv->batman_queue_left);
335 goto out;
336 }
337
338 if ((atomic_read(&bat_priv->aggregated_ogms)) &&
339 (packet_len < MAX_AGGREGATION_BYTES))
340 forw_packet_aggr->skb = dev_alloc_skb(MAX_AGGREGATION_BYTES +
341 sizeof(struct ethhdr));
342 else
343 forw_packet_aggr->skb = dev_alloc_skb(packet_len +
344 sizeof(struct ethhdr));
345
346 if (!forw_packet_aggr->skb) {
347 if (!own_packet)
348 atomic_inc(&bat_priv->batman_queue_left);
349 kfree(forw_packet_aggr);
350 goto out;
351 }
352 skb_reserve(forw_packet_aggr->skb, sizeof(struct ethhdr));
353
354 INIT_HLIST_NODE(&forw_packet_aggr->list);
355
356 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
357 forw_packet_aggr->packet_len = packet_len;
358 memcpy(skb_buff, packet_buff, packet_len);
359
360 forw_packet_aggr->own = own_packet;
361 forw_packet_aggr->if_incoming = if_incoming;
362 forw_packet_aggr->num_packets = 0;
363 forw_packet_aggr->direct_link_flags = NO_FLAGS;
364 forw_packet_aggr->send_time = send_time;
365
366 /* save packet direct link flag status */
367 if (direct_link)
368 forw_packet_aggr->direct_link_flags |= 1;
369
370 /* add new packet to packet list */
371 spin_lock_bh(&bat_priv->forw_bat_list_lock);
372 hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list);
373 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
374
375 /* start timer for this packet */
376 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
377 send_outstanding_bat_ogm_packet);
378 queue_delayed_work(bat_event_workqueue,
379 &forw_packet_aggr->delayed_work,
380 send_time - jiffies);
381
382 return;
383out:
384 hardif_free_ref(if_incoming);
385}
386
387/* aggregate a new packet into the existing ogm packet */
Marek Lindner01c42242011-11-28 21:31:55 +0800388static void bat_iv_ogm_aggregate(struct forw_packet *forw_packet_aggr,
389 const unsigned char *packet_buff,
390 int packet_len, bool direct_link)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200391{
392 unsigned char *skb_buff;
393
394 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
395 memcpy(skb_buff, packet_buff, packet_len);
396 forw_packet_aggr->packet_len += packet_len;
397 forw_packet_aggr->num_packets++;
398
399 /* save packet direct link flag status */
400 if (direct_link)
401 forw_packet_aggr->direct_link_flags |=
402 (1 << forw_packet_aggr->num_packets);
403}
404
Marek Lindner01c42242011-11-28 21:31:55 +0800405static void bat_iv_ogm_queue_add(struct bat_priv *bat_priv,
406 unsigned char *packet_buff,
407 int packet_len, struct hard_iface *if_incoming,
408 int own_packet, unsigned long send_time)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200409{
410 /**
411 * _aggr -> pointer to the packet we want to aggregate with
412 * _pos -> pointer to the position in the queue
413 */
414 struct forw_packet *forw_packet_aggr = NULL, *forw_packet_pos = NULL;
415 struct hlist_node *tmp_node;
416 struct batman_ogm_packet *batman_ogm_packet;
417 bool direct_link;
418
419 batman_ogm_packet = (struct batman_ogm_packet *)packet_buff;
420 direct_link = batman_ogm_packet->flags & DIRECTLINK ? 1 : 0;
421
422 /* find position for the packet in the forward queue */
423 spin_lock_bh(&bat_priv->forw_bat_list_lock);
424 /* own packets are not to be aggregated */
425 if ((atomic_read(&bat_priv->aggregated_ogms)) && (!own_packet)) {
426 hlist_for_each_entry(forw_packet_pos, tmp_node,
427 &bat_priv->forw_bat_list, list) {
Marek Lindner01c42242011-11-28 21:31:55 +0800428 if (bat_iv_ogm_can_aggregate(batman_ogm_packet,
429 bat_priv, packet_len,
430 send_time, direct_link,
431 if_incoming,
432 forw_packet_pos)) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200433 forw_packet_aggr = forw_packet_pos;
434 break;
435 }
436 }
437 }
438
439 /* nothing to aggregate with - either aggregation disabled or no
440 * suitable aggregation packet found */
441 if (!forw_packet_aggr) {
442 /* the following section can run without the lock */
443 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
444
445 /**
446 * if we could not aggregate this packet with one of the others
447 * we hold it back for a while, so that it might be aggregated
448 * later on
449 */
450 if ((!own_packet) &&
451 (atomic_read(&bat_priv->aggregated_ogms)))
452 send_time += msecs_to_jiffies(MAX_AGGREGATION_MS);
453
Marek Lindner01c42242011-11-28 21:31:55 +0800454 bat_iv_ogm_aggregate_new(packet_buff, packet_len,
455 send_time, direct_link,
456 if_incoming, own_packet);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200457 } else {
Marek Lindner01c42242011-11-28 21:31:55 +0800458 bat_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
459 packet_len, direct_link);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200460 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
461 }
462}
463
Marek Lindner01c42242011-11-28 21:31:55 +0800464static void bat_iv_ogm_forward(struct orig_node *orig_node,
465 const struct ethhdr *ethhdr,
466 struct batman_ogm_packet *batman_ogm_packet,
467 int directlink, struct hard_iface *if_incoming)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200468{
469 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
470 struct neigh_node *router;
471 uint8_t in_tq, in_ttl, tq_avg = 0;
472 uint8_t tt_num_changes;
473
Sven Eckelmann76543d12011-11-20 15:47:38 +0100474 if (batman_ogm_packet->header.ttl <= 1) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200475 bat_dbg(DBG_BATMAN, bat_priv, "ttl exceeded\n");
476 return;
477 }
478
479 router = orig_node_get_router(orig_node);
480
481 in_tq = batman_ogm_packet->tq;
Sven Eckelmann76543d12011-11-20 15:47:38 +0100482 in_ttl = batman_ogm_packet->header.ttl;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200483 tt_num_changes = batman_ogm_packet->tt_num_changes;
484
Sven Eckelmann76543d12011-11-20 15:47:38 +0100485 batman_ogm_packet->header.ttl--;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200486 memcpy(batman_ogm_packet->prev_sender, ethhdr->h_source, ETH_ALEN);
487
488 /* rebroadcast tq of our best ranking neighbor to ensure the rebroadcast
489 * of our best tq value */
490 if (router && router->tq_avg != 0) {
491
492 /* rebroadcast ogm of best ranking neighbor as is */
493 if (!compare_eth(router->addr, ethhdr->h_source)) {
494 batman_ogm_packet->tq = router->tq_avg;
495
496 if (router->last_ttl)
Sven Eckelmann76543d12011-11-20 15:47:38 +0100497 batman_ogm_packet->header.ttl =
498 router->last_ttl - 1;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200499 }
500
501 tq_avg = router->tq_avg;
502 }
503
504 if (router)
505 neigh_node_free_ref(router);
506
507 /* apply hop penalty */
508 batman_ogm_packet->tq = hop_penalty(batman_ogm_packet->tq, bat_priv);
509
510 bat_dbg(DBG_BATMAN, bat_priv,
511 "Forwarding packet: tq_orig: %i, tq_avg: %i, "
512 "tq_forw: %i, ttl_orig: %i, ttl_forw: %i\n",
513 in_tq, tq_avg, batman_ogm_packet->tq, in_ttl - 1,
Sven Eckelmann76543d12011-11-20 15:47:38 +0100514 batman_ogm_packet->header.ttl);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200515
516 batman_ogm_packet->seqno = htonl(batman_ogm_packet->seqno);
517 batman_ogm_packet->tt_crc = htons(batman_ogm_packet->tt_crc);
518
519 /* switch of primaries first hop flag when forwarding */
520 batman_ogm_packet->flags &= ~PRIMARIES_FIRST_HOP;
521 if (directlink)
522 batman_ogm_packet->flags |= DIRECTLINK;
523 else
524 batman_ogm_packet->flags &= ~DIRECTLINK;
525
Marek Lindner01c42242011-11-28 21:31:55 +0800526 bat_iv_ogm_queue_add(bat_priv, (unsigned char *)batman_ogm_packet,
527 BATMAN_OGM_LEN + tt_len(tt_num_changes),
528 if_incoming, 0, bat_iv_ogm_fwd_send_time());
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200529}
530
Marek Lindner01c42242011-11-28 21:31:55 +0800531static void bat_iv_ogm_schedule(struct hard_iface *hard_iface,
532 int tt_num_changes)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200533{
534 struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
535 struct batman_ogm_packet *batman_ogm_packet;
536 struct hard_iface *primary_if;
537 int vis_server;
538
539 vis_server = atomic_read(&bat_priv->vis_mode);
540 primary_if = primary_if_get_selected(bat_priv);
541
542 batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff;
543
544 /* change sequence number to network order */
545 batman_ogm_packet->seqno =
546 htonl((uint32_t)atomic_read(&hard_iface->seqno));
547
548 batman_ogm_packet->ttvn = atomic_read(&bat_priv->ttvn);
549 batman_ogm_packet->tt_crc = htons((uint16_t)
550 atomic_read(&bat_priv->tt_crc));
551 if (tt_num_changes >= 0)
552 batman_ogm_packet->tt_num_changes = tt_num_changes;
553
554 if (vis_server == VIS_TYPE_SERVER_SYNC)
555 batman_ogm_packet->flags |= VIS_SERVER;
556 else
557 batman_ogm_packet->flags &= ~VIS_SERVER;
558
559 if ((hard_iface == primary_if) &&
560 (atomic_read(&bat_priv->gw_mode) == GW_MODE_SERVER))
561 batman_ogm_packet->gw_flags =
562 (uint8_t)atomic_read(&bat_priv->gw_bandwidth);
563 else
564 batman_ogm_packet->gw_flags = NO_FLAGS;
565
566 atomic_inc(&hard_iface->seqno);
567
568 slide_own_bcast_window(hard_iface);
Marek Lindner01c42242011-11-28 21:31:55 +0800569 bat_iv_ogm_queue_add(bat_priv, hard_iface->packet_buff,
570 hard_iface->packet_len, hard_iface, 1,
571 bat_iv_ogm_emit_send_time(bat_priv));
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200572
573 if (primary_if)
574 hardif_free_ref(primary_if);
575}
576
Marek Lindner01c42242011-11-28 21:31:55 +0800577static void bat_iv_ogm_orig_update(struct bat_priv *bat_priv,
578 struct orig_node *orig_node,
579 const struct ethhdr *ethhdr,
580 const struct batman_ogm_packet
Marek Lindnerfc957272011-07-30 12:04:12 +0200581 *batman_ogm_packet,
Marek Lindner01c42242011-11-28 21:31:55 +0800582 struct hard_iface *if_incoming,
583 const unsigned char *tt_buff,
584 int is_duplicate)
Marek Lindnerfc957272011-07-30 12:04:12 +0200585{
586 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
587 struct neigh_node *router = NULL;
588 struct orig_node *orig_node_tmp;
589 struct hlist_node *node;
590 uint8_t bcast_own_sum_orig, bcast_own_sum_neigh;
591
592 bat_dbg(DBG_BATMAN, bat_priv, "update_originator(): "
593 "Searching and updating originator entry of received packet\n");
594
595 rcu_read_lock();
596 hlist_for_each_entry_rcu(tmp_neigh_node, node,
597 &orig_node->neigh_list, list) {
598 if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
599 (tmp_neigh_node->if_incoming == if_incoming) &&
600 atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
601 if (neigh_node)
602 neigh_node_free_ref(neigh_node);
603 neigh_node = tmp_neigh_node;
604 continue;
605 }
606
607 if (is_duplicate)
608 continue;
609
610 spin_lock_bh(&tmp_neigh_node->tq_lock);
611 ring_buffer_set(tmp_neigh_node->tq_recv,
612 &tmp_neigh_node->tq_index, 0);
613 tmp_neigh_node->tq_avg =
614 ring_buffer_avg(tmp_neigh_node->tq_recv);
615 spin_unlock_bh(&tmp_neigh_node->tq_lock);
616 }
617
618 if (!neigh_node) {
619 struct orig_node *orig_tmp;
620
621 orig_tmp = get_orig_node(bat_priv, ethhdr->h_source);
622 if (!orig_tmp)
623 goto unlock;
624
625 neigh_node = create_neighbor(orig_node, orig_tmp,
626 ethhdr->h_source, if_incoming);
627
628 orig_node_free_ref(orig_tmp);
629 if (!neigh_node)
630 goto unlock;
631 } else
632 bat_dbg(DBG_BATMAN, bat_priv,
633 "Updating existing last-hop neighbor of originator\n");
634
635 rcu_read_unlock();
636
637 orig_node->flags = batman_ogm_packet->flags;
638 neigh_node->last_valid = jiffies;
639
640 spin_lock_bh(&neigh_node->tq_lock);
641 ring_buffer_set(neigh_node->tq_recv,
642 &neigh_node->tq_index,
643 batman_ogm_packet->tq);
644 neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
645 spin_unlock_bh(&neigh_node->tq_lock);
646
647 if (!is_duplicate) {
Sven Eckelmann76543d12011-11-20 15:47:38 +0100648 orig_node->last_ttl = batman_ogm_packet->header.ttl;
649 neigh_node->last_ttl = batman_ogm_packet->header.ttl;
Marek Lindnerfc957272011-07-30 12:04:12 +0200650 }
651
652 bonding_candidate_add(orig_node, neigh_node);
653
654 /* if this neighbor already is our next hop there is nothing
655 * to change */
656 router = orig_node_get_router(orig_node);
657 if (router == neigh_node)
658 goto update_tt;
659
660 /* if this neighbor does not offer a better TQ we won't consider it */
661 if (router && (router->tq_avg > neigh_node->tq_avg))
662 goto update_tt;
663
664 /* if the TQ is the same and the link not more symmetric we
665 * won't consider it either */
666 if (router && (neigh_node->tq_avg == router->tq_avg)) {
667 orig_node_tmp = router->orig_node;
668 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
669 bcast_own_sum_orig =
670 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
671 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
672
673 orig_node_tmp = neigh_node->orig_node;
674 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
675 bcast_own_sum_neigh =
676 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
677 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
678
679 if (bcast_own_sum_orig >= bcast_own_sum_neigh)
680 goto update_tt;
681 }
682
683 update_route(bat_priv, orig_node, neigh_node);
684
685update_tt:
686 /* I have to check for transtable changes only if the OGM has been
687 * sent through a primary interface */
688 if (((batman_ogm_packet->orig != ethhdr->h_source) &&
Sven Eckelmann76543d12011-11-20 15:47:38 +0100689 (batman_ogm_packet->header.ttl > 2)) ||
Marek Lindnerfc957272011-07-30 12:04:12 +0200690 (batman_ogm_packet->flags & PRIMARIES_FIRST_HOP))
691 tt_update_orig(bat_priv, orig_node, tt_buff,
692 batman_ogm_packet->tt_num_changes,
693 batman_ogm_packet->ttvn,
694 batman_ogm_packet->tt_crc);
695
696 if (orig_node->gw_flags != batman_ogm_packet->gw_flags)
697 gw_node_update(bat_priv, orig_node,
698 batman_ogm_packet->gw_flags);
699
700 orig_node->gw_flags = batman_ogm_packet->gw_flags;
701
702 /* restart gateway selection if fast or late switching was enabled */
703 if ((orig_node->gw_flags) &&
704 (atomic_read(&bat_priv->gw_mode) == GW_MODE_CLIENT) &&
705 (atomic_read(&bat_priv->gw_sel_class) > 2))
706 gw_check_election(bat_priv, orig_node);
707
708 goto out;
709
710unlock:
711 rcu_read_unlock();
712out:
713 if (neigh_node)
714 neigh_node_free_ref(neigh_node);
715 if (router)
716 neigh_node_free_ref(router);
717}
718
Marek Lindner01c42242011-11-28 21:31:55 +0800719static int bat_iv_ogm_calc_tq(struct orig_node *orig_node,
720 struct orig_node *orig_neigh_node,
721 struct batman_ogm_packet *batman_ogm_packet,
722 struct hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +0200723{
724 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
725 struct neigh_node *neigh_node = NULL, *tmp_neigh_node;
726 struct hlist_node *node;
727 uint8_t total_count;
728 uint8_t orig_eq_count, neigh_rq_count, tq_own;
729 int tq_asym_penalty, ret = 0;
730
731 /* find corresponding one hop neighbor */
732 rcu_read_lock();
733 hlist_for_each_entry_rcu(tmp_neigh_node, node,
734 &orig_neigh_node->neigh_list, list) {
735
736 if (!compare_eth(tmp_neigh_node->addr, orig_neigh_node->orig))
737 continue;
738
739 if (tmp_neigh_node->if_incoming != if_incoming)
740 continue;
741
742 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
743 continue;
744
745 neigh_node = tmp_neigh_node;
746 break;
747 }
748 rcu_read_unlock();
749
750 if (!neigh_node)
751 neigh_node = create_neighbor(orig_neigh_node,
752 orig_neigh_node,
753 orig_neigh_node->orig,
754 if_incoming);
755
756 if (!neigh_node)
757 goto out;
758
759 /* if orig_node is direct neighbor update neigh_node last_valid */
760 if (orig_node == orig_neigh_node)
761 neigh_node->last_valid = jiffies;
762
763 orig_node->last_valid = jiffies;
764
765 /* find packet count of corresponding one hop neighbor */
766 spin_lock_bh(&orig_node->ogm_cnt_lock);
767 orig_eq_count = orig_neigh_node->bcast_own_sum[if_incoming->if_num];
768 neigh_rq_count = neigh_node->real_packet_count;
769 spin_unlock_bh(&orig_node->ogm_cnt_lock);
770
771 /* pay attention to not get a value bigger than 100 % */
772 total_count = (orig_eq_count > neigh_rq_count ?
773 neigh_rq_count : orig_eq_count);
774
775 /* if we have too few packets (too less data) we set tq_own to zero */
776 /* if we receive too few packets it is not considered bidirectional */
777 if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
778 (neigh_rq_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
779 tq_own = 0;
780 else
781 /* neigh_node->real_packet_count is never zero as we
782 * only purge old information when getting new
783 * information */
784 tq_own = (TQ_MAX_VALUE * total_count) / neigh_rq_count;
785
786 /*
787 * 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
788 * affect the nearly-symmetric links only a little, but
789 * punishes asymmetric links more. This will give a value
790 * between 0 and TQ_MAX_VALUE
791 */
792 tq_asym_penalty = TQ_MAX_VALUE - (TQ_MAX_VALUE *
793 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
794 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
795 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count)) /
796 (TQ_LOCAL_WINDOW_SIZE *
797 TQ_LOCAL_WINDOW_SIZE *
798 TQ_LOCAL_WINDOW_SIZE);
799
800 batman_ogm_packet->tq = ((batman_ogm_packet->tq * tq_own
801 * tq_asym_penalty) /
802 (TQ_MAX_VALUE * TQ_MAX_VALUE));
803
804 bat_dbg(DBG_BATMAN, bat_priv,
805 "bidirectional: "
806 "orig = %-15pM neigh = %-15pM => own_bcast = %2i, "
807 "real recv = %2i, local tq: %3i, asym_penalty: %3i, "
808 "total tq: %3i\n",
809 orig_node->orig, orig_neigh_node->orig, total_count,
810 neigh_rq_count, tq_own, tq_asym_penalty, batman_ogm_packet->tq);
811
812 /* if link has the minimum required transmission quality
813 * consider it bidirectional */
814 if (batman_ogm_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
815 ret = 1;
816
817out:
818 if (neigh_node)
819 neigh_node_free_ref(neigh_node);
820 return ret;
821}
822
823/* processes a batman packet for all interfaces, adjusts the sequence number and
824 * finds out whether it is a duplicate.
825 * returns:
826 * 1 the packet is a duplicate
827 * 0 the packet has not yet been received
828 * -1 the packet is old and has been received while the seqno window
829 * was protected. Caller should drop it.
830 */
Marek Lindner01c42242011-11-28 21:31:55 +0800831static int bat_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
832 const struct batman_ogm_packet
Marek Lindnerfc957272011-07-30 12:04:12 +0200833 *batman_ogm_packet,
Marek Lindner01c42242011-11-28 21:31:55 +0800834 const struct hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +0200835{
836 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
837 struct orig_node *orig_node;
838 struct neigh_node *tmp_neigh_node;
839 struct hlist_node *node;
840 int is_duplicate = 0;
841 int32_t seq_diff;
842 int need_update = 0;
843 int set_mark, ret = -1;
844
845 orig_node = get_orig_node(bat_priv, batman_ogm_packet->orig);
846 if (!orig_node)
847 return 0;
848
849 spin_lock_bh(&orig_node->ogm_cnt_lock);
850 seq_diff = batman_ogm_packet->seqno - orig_node->last_real_seqno;
851
852 /* signalize caller that the packet is to be dropped. */
853 if (window_protected(bat_priv, seq_diff,
854 &orig_node->batman_seqno_reset))
855 goto out;
856
857 rcu_read_lock();
858 hlist_for_each_entry_rcu(tmp_neigh_node, node,
859 &orig_node->neigh_list, list) {
860
861 is_duplicate |= get_bit_status(tmp_neigh_node->real_bits,
862 orig_node->last_real_seqno,
863 batman_ogm_packet->seqno);
864
865 if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
866 (tmp_neigh_node->if_incoming == if_incoming))
867 set_mark = 1;
868 else
869 set_mark = 0;
870
871 /* if the window moved, set the update flag. */
872 need_update |= bit_get_packet(bat_priv,
873 tmp_neigh_node->real_bits,
874 seq_diff, set_mark);
875
876 tmp_neigh_node->real_packet_count =
877 bit_packet_count(tmp_neigh_node->real_bits);
878 }
879 rcu_read_unlock();
880
881 if (need_update) {
882 bat_dbg(DBG_BATMAN, bat_priv,
883 "updating last_seqno: old %d, new %d\n",
884 orig_node->last_real_seqno, batman_ogm_packet->seqno);
885 orig_node->last_real_seqno = batman_ogm_packet->seqno;
886 }
887
888 ret = is_duplicate;
889
890out:
891 spin_unlock_bh(&orig_node->ogm_cnt_lock);
892 orig_node_free_ref(orig_node);
893 return ret;
894}
895
Marek Lindner01c42242011-11-28 21:31:55 +0800896static void bat_iv_ogm_process(const struct ethhdr *ethhdr,
897 struct batman_ogm_packet *batman_ogm_packet,
898 const unsigned char *tt_buff,
899 struct hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +0200900{
901 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
902 struct hard_iface *hard_iface;
903 struct orig_node *orig_neigh_node, *orig_node;
904 struct neigh_node *router = NULL, *router_router = NULL;
905 struct neigh_node *orig_neigh_router = NULL;
906 int has_directlink_flag;
907 int is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
908 int is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
909 int is_duplicate;
910 uint32_t if_incoming_seqno;
911
912 /* Silently drop when the batman packet is actually not a
913 * correct packet.
914 *
915 * This might happen if a packet is padded (e.g. Ethernet has a
916 * minimum frame length of 64 byte) and the aggregation interprets
917 * it as an additional length.
918 *
919 * TODO: A more sane solution would be to have a bit in the
920 * batman_ogm_packet to detect whether the packet is the last
921 * packet in an aggregation. Here we expect that the padding
922 * is always zero (or not 0x01)
923 */
Sven Eckelmann76543d12011-11-20 15:47:38 +0100924 if (batman_ogm_packet->header.packet_type != BAT_OGM)
Marek Lindnerfc957272011-07-30 12:04:12 +0200925 return;
926
927 /* could be changed by schedule_own_packet() */
928 if_incoming_seqno = atomic_read(&if_incoming->seqno);
929
930 has_directlink_flag = (batman_ogm_packet->flags & DIRECTLINK ? 1 : 0);
931
932 is_single_hop_neigh = (compare_eth(ethhdr->h_source,
933 batman_ogm_packet->orig) ? 1 : 0);
934
935 bat_dbg(DBG_BATMAN, bat_priv,
936 "Received BATMAN packet via NB: %pM, IF: %s [%pM] "
937 "(from OG: %pM, via prev OG: %pM, seqno %d, ttvn %u, "
938 "crc %u, changes %u, td %d, TTL %d, V %d, IDF %d)\n",
939 ethhdr->h_source, if_incoming->net_dev->name,
940 if_incoming->net_dev->dev_addr, batman_ogm_packet->orig,
941 batman_ogm_packet->prev_sender, batman_ogm_packet->seqno,
942 batman_ogm_packet->ttvn, batman_ogm_packet->tt_crc,
943 batman_ogm_packet->tt_num_changes, batman_ogm_packet->tq,
Sven Eckelmann76543d12011-11-20 15:47:38 +0100944 batman_ogm_packet->header.ttl,
945 batman_ogm_packet->header.version, has_directlink_flag);
Marek Lindnerfc957272011-07-30 12:04:12 +0200946
947 rcu_read_lock();
948 list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
949 if (hard_iface->if_status != IF_ACTIVE)
950 continue;
951
952 if (hard_iface->soft_iface != if_incoming->soft_iface)
953 continue;
954
955 if (compare_eth(ethhdr->h_source,
956 hard_iface->net_dev->dev_addr))
957 is_my_addr = 1;
958
959 if (compare_eth(batman_ogm_packet->orig,
960 hard_iface->net_dev->dev_addr))
961 is_my_orig = 1;
962
963 if (compare_eth(batman_ogm_packet->prev_sender,
964 hard_iface->net_dev->dev_addr))
965 is_my_oldorig = 1;
966
967 if (is_broadcast_ether_addr(ethhdr->h_source))
968 is_broadcast = 1;
969 }
970 rcu_read_unlock();
971
Sven Eckelmann76543d12011-11-20 15:47:38 +0100972 if (batman_ogm_packet->header.version != COMPAT_VERSION) {
Marek Lindnerfc957272011-07-30 12:04:12 +0200973 bat_dbg(DBG_BATMAN, bat_priv,
974 "Drop packet: incompatible batman version (%i)\n",
Sven Eckelmann76543d12011-11-20 15:47:38 +0100975 batman_ogm_packet->header.version);
Marek Lindnerfc957272011-07-30 12:04:12 +0200976 return;
977 }
978
979 if (is_my_addr) {
980 bat_dbg(DBG_BATMAN, bat_priv,
981 "Drop packet: received my own broadcast (sender: %pM"
982 ")\n",
983 ethhdr->h_source);
984 return;
985 }
986
987 if (is_broadcast) {
988 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
989 "ignoring all packets with broadcast source addr (sender: %pM"
990 ")\n", ethhdr->h_source);
991 return;
992 }
993
994 if (is_my_orig) {
995 unsigned long *word;
996 int offset;
997
998 orig_neigh_node = get_orig_node(bat_priv, ethhdr->h_source);
999 if (!orig_neigh_node)
1000 return;
1001
1002 /* neighbor has to indicate direct link and it has to
1003 * come via the corresponding interface */
1004 /* save packet seqno for bidirectional check */
1005 if (has_directlink_flag &&
1006 compare_eth(if_incoming->net_dev->dev_addr,
1007 batman_ogm_packet->orig)) {
1008 offset = if_incoming->if_num * NUM_WORDS;
1009
1010 spin_lock_bh(&orig_neigh_node->ogm_cnt_lock);
1011 word = &(orig_neigh_node->bcast_own[offset]);
1012 bit_mark(word,
1013 if_incoming_seqno -
1014 batman_ogm_packet->seqno - 2);
1015 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
1016 bit_packet_count(word);
1017 spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock);
1018 }
1019
1020 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
1021 "originator packet from myself (via neighbor)\n");
1022 orig_node_free_ref(orig_neigh_node);
1023 return;
1024 }
1025
1026 if (is_my_oldorig) {
1027 bat_dbg(DBG_BATMAN, bat_priv,
1028 "Drop packet: ignoring all rebroadcast echos (sender: "
1029 "%pM)\n", ethhdr->h_source);
1030 return;
1031 }
1032
1033 orig_node = get_orig_node(bat_priv, batman_ogm_packet->orig);
1034 if (!orig_node)
1035 return;
1036
Marek Lindner01c42242011-11-28 21:31:55 +08001037 is_duplicate = bat_iv_ogm_update_seqnos(ethhdr, batman_ogm_packet,
1038 if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001039
1040 if (is_duplicate == -1) {
1041 bat_dbg(DBG_BATMAN, bat_priv,
1042 "Drop packet: packet within seqno protection time "
1043 "(sender: %pM)\n", ethhdr->h_source);
1044 goto out;
1045 }
1046
1047 if (batman_ogm_packet->tq == 0) {
1048 bat_dbg(DBG_BATMAN, bat_priv,
1049 "Drop packet: originator packet with tq equal 0\n");
1050 goto out;
1051 }
1052
1053 router = orig_node_get_router(orig_node);
1054 if (router)
1055 router_router = orig_node_get_router(router->orig_node);
1056
1057 /* avoid temporary routing loops */
1058 if (router && router_router &&
1059 (compare_eth(router->addr, batman_ogm_packet->prev_sender)) &&
1060 !(compare_eth(batman_ogm_packet->orig,
1061 batman_ogm_packet->prev_sender)) &&
1062 (compare_eth(router->addr, router_router->addr))) {
1063 bat_dbg(DBG_BATMAN, bat_priv,
1064 "Drop packet: ignoring all rebroadcast packets that "
1065 "may make me loop (sender: %pM)\n", ethhdr->h_source);
1066 goto out;
1067 }
1068
1069 /* if sender is a direct neighbor the sender mac equals
1070 * originator mac */
1071 orig_neigh_node = (is_single_hop_neigh ?
1072 orig_node :
1073 get_orig_node(bat_priv, ethhdr->h_source));
1074 if (!orig_neigh_node)
1075 goto out;
1076
1077 orig_neigh_router = orig_node_get_router(orig_neigh_node);
1078
1079 /* drop packet if sender is not a direct neighbor and if we
1080 * don't route towards it */
1081 if (!is_single_hop_neigh && (!orig_neigh_router)) {
1082 bat_dbg(DBG_BATMAN, bat_priv,
1083 "Drop packet: OGM via unknown neighbor!\n");
1084 goto out_neigh;
1085 }
1086
Marek Lindner01c42242011-11-28 21:31:55 +08001087 is_bidirectional = bat_iv_ogm_calc_tq(orig_node, orig_neigh_node,
1088 batman_ogm_packet, if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001089
1090 bonding_save_primary(orig_node, orig_neigh_node, batman_ogm_packet);
1091
1092 /* update ranking if it is not a duplicate or has the same
1093 * seqno and similar ttl as the non-duplicate */
1094 if (is_bidirectional &&
1095 (!is_duplicate ||
1096 ((orig_node->last_real_seqno == batman_ogm_packet->seqno) &&
Sven Eckelmann76543d12011-11-20 15:47:38 +01001097 (orig_node->last_ttl - 3 <= batman_ogm_packet->header.ttl))))
Marek Lindner01c42242011-11-28 21:31:55 +08001098 bat_iv_ogm_orig_update(bat_priv, orig_node, ethhdr,
1099 batman_ogm_packet, if_incoming,
1100 tt_buff, is_duplicate);
Marek Lindnerfc957272011-07-30 12:04:12 +02001101
1102 /* is single hop (direct) neighbor */
1103 if (is_single_hop_neigh) {
1104
1105 /* mark direct link on incoming interface */
Marek Lindner01c42242011-11-28 21:31:55 +08001106 bat_iv_ogm_forward(orig_node, ethhdr, batman_ogm_packet,
1107 1, if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001108
1109 bat_dbg(DBG_BATMAN, bat_priv, "Forwarding packet: "
1110 "rebroadcast neighbor packet with direct link flag\n");
1111 goto out_neigh;
1112 }
1113
1114 /* multihop originator */
1115 if (!is_bidirectional) {
1116 bat_dbg(DBG_BATMAN, bat_priv,
1117 "Drop packet: not received via bidirectional link\n");
1118 goto out_neigh;
1119 }
1120
1121 if (is_duplicate) {
1122 bat_dbg(DBG_BATMAN, bat_priv,
1123 "Drop packet: duplicate packet received\n");
1124 goto out_neigh;
1125 }
1126
1127 bat_dbg(DBG_BATMAN, bat_priv,
1128 "Forwarding packet: rebroadcast originator packet\n");
Marek Lindner01c42242011-11-28 21:31:55 +08001129 bat_iv_ogm_forward(orig_node, ethhdr, batman_ogm_packet,
1130 0, if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001131
1132out_neigh:
1133 if ((orig_neigh_node) && (!is_single_hop_neigh))
1134 orig_node_free_ref(orig_neigh_node);
1135out:
1136 if (router)
1137 neigh_node_free_ref(router);
1138 if (router_router)
1139 neigh_node_free_ref(router_router);
1140 if (orig_neigh_router)
1141 neigh_node_free_ref(orig_neigh_router);
1142
1143 orig_node_free_ref(orig_node);
1144}
1145
Marek Lindner01c42242011-11-28 21:31:55 +08001146static void bat_iv_ogm_receive(struct hard_iface *if_incoming,
1147 struct sk_buff *skb)
Marek Lindnerfc957272011-07-30 12:04:12 +02001148{
1149 struct batman_ogm_packet *batman_ogm_packet;
Marek Lindner8780dad2011-12-05 04:01:51 +08001150 struct ethhdr *ethhdr;
1151 int buff_pos = 0, packet_len;
1152 unsigned char *tt_buff, *packet_buff;
Marek Lindnerfc957272011-07-30 12:04:12 +02001153
Marek Lindner8780dad2011-12-05 04:01:51 +08001154 packet_len = skb_headlen(skb);
1155 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1156 packet_buff = skb->data;
Marek Lindnerfc957272011-07-30 12:04:12 +02001157 batman_ogm_packet = (struct batman_ogm_packet *)packet_buff;
1158
1159 /* unpack the aggregated packets and process them one by one */
1160 do {
1161 /* network to host order for our 32bit seqno and the
1162 orig_interval */
1163 batman_ogm_packet->seqno = ntohl(batman_ogm_packet->seqno);
1164 batman_ogm_packet->tt_crc = ntohs(batman_ogm_packet->tt_crc);
1165
1166 tt_buff = packet_buff + buff_pos + BATMAN_OGM_LEN;
1167
Marek Lindner01c42242011-11-28 21:31:55 +08001168 bat_iv_ogm_process(ethhdr, batman_ogm_packet,
1169 tt_buff, if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001170
1171 buff_pos += BATMAN_OGM_LEN +
1172 tt_len(batman_ogm_packet->tt_num_changes);
1173
1174 batman_ogm_packet = (struct batman_ogm_packet *)
1175 (packet_buff + buff_pos);
Marek Lindner01c42242011-11-28 21:31:55 +08001176 } while (bat_iv_ogm_aggr_packet(buff_pos, packet_len,
1177 batman_ogm_packet->tt_num_changes));
Marek Lindnerfc957272011-07-30 12:04:12 +02001178}
Marek Lindner1c280472011-11-28 17:40:17 +08001179
1180static struct bat_algo_ops batman_iv __read_mostly = {
1181 .name = "BATMAN IV",
Marek Lindner01c42242011-11-28 21:31:55 +08001182 .bat_ogm_init = bat_iv_ogm_init,
1183 .bat_ogm_init_primary = bat_iv_ogm_init_primary,
1184 .bat_ogm_update_mac = bat_iv_ogm_update_mac,
1185 .bat_ogm_schedule = bat_iv_ogm_schedule,
1186 .bat_ogm_emit = bat_iv_ogm_emit,
1187 .bat_ogm_receive = bat_iv_ogm_receive,
Marek Lindner1c280472011-11-28 17:40:17 +08001188};
1189
1190int __init bat_iv_init(void)
1191{
1192 return bat_algo_register(&batman_iv);
1193}