blob: 98f71827c7b3f605fa667f953578df6ed5e4d2b2 [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,
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100155 "%s %spacket (originator %pM, seqno %d, 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,
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100214 "%s packet (originator %pM, seqno %d, 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 +
358 sizeof(struct ethhdr));
359 else
360 forw_packet_aggr->skb = dev_alloc_skb(packet_len +
361 sizeof(struct ethhdr));
362
363 if (!forw_packet_aggr->skb) {
364 if (!own_packet)
365 atomic_inc(&bat_priv->batman_queue_left);
366 kfree(forw_packet_aggr);
367 goto out;
368 }
369 skb_reserve(forw_packet_aggr->skb, sizeof(struct ethhdr));
370
371 INIT_HLIST_NODE(&forw_packet_aggr->list);
372
373 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
374 forw_packet_aggr->packet_len = packet_len;
375 memcpy(skb_buff, packet_buff, packet_len);
376
377 forw_packet_aggr->own = own_packet;
378 forw_packet_aggr->if_incoming = if_incoming;
379 forw_packet_aggr->num_packets = 0;
380 forw_packet_aggr->direct_link_flags = NO_FLAGS;
381 forw_packet_aggr->send_time = send_time;
382
383 /* save packet direct link flag status */
384 if (direct_link)
385 forw_packet_aggr->direct_link_flags |= 1;
386
387 /* add new packet to packet list */
388 spin_lock_bh(&bat_priv->forw_bat_list_lock);
389 hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list);
390 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
391
392 /* start timer for this packet */
393 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
394 send_outstanding_bat_ogm_packet);
395 queue_delayed_work(bat_event_workqueue,
396 &forw_packet_aggr->delayed_work,
397 send_time - jiffies);
398
399 return;
400out:
401 hardif_free_ref(if_incoming);
402}
403
404/* aggregate a new packet into the existing ogm packet */
Marek Lindner01c42242011-11-28 21:31:55 +0800405static void bat_iv_ogm_aggregate(struct forw_packet *forw_packet_aggr,
406 const unsigned char *packet_buff,
407 int packet_len, bool direct_link)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200408{
409 unsigned char *skb_buff;
410
411 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
412 memcpy(skb_buff, packet_buff, packet_len);
413 forw_packet_aggr->packet_len += packet_len;
414 forw_packet_aggr->num_packets++;
415
416 /* save packet direct link flag status */
417 if (direct_link)
418 forw_packet_aggr->direct_link_flags |=
419 (1 << forw_packet_aggr->num_packets);
420}
421
Marek Lindner01c42242011-11-28 21:31:55 +0800422static void bat_iv_ogm_queue_add(struct bat_priv *bat_priv,
423 unsigned char *packet_buff,
424 int packet_len, struct hard_iface *if_incoming,
425 int own_packet, unsigned long send_time)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200426{
427 /**
428 * _aggr -> pointer to the packet we want to aggregate with
429 * _pos -> pointer to the position in the queue
430 */
431 struct forw_packet *forw_packet_aggr = NULL, *forw_packet_pos = NULL;
432 struct hlist_node *tmp_node;
433 struct batman_ogm_packet *batman_ogm_packet;
434 bool direct_link;
435
436 batman_ogm_packet = (struct batman_ogm_packet *)packet_buff;
437 direct_link = batman_ogm_packet->flags & DIRECTLINK ? 1 : 0;
438
439 /* find position for the packet in the forward queue */
440 spin_lock_bh(&bat_priv->forw_bat_list_lock);
441 /* own packets are not to be aggregated */
442 if ((atomic_read(&bat_priv->aggregated_ogms)) && (!own_packet)) {
443 hlist_for_each_entry(forw_packet_pos, tmp_node,
444 &bat_priv->forw_bat_list, list) {
Marek Lindner01c42242011-11-28 21:31:55 +0800445 if (bat_iv_ogm_can_aggregate(batman_ogm_packet,
446 bat_priv, packet_len,
447 send_time, direct_link,
448 if_incoming,
449 forw_packet_pos)) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200450 forw_packet_aggr = forw_packet_pos;
451 break;
452 }
453 }
454 }
455
456 /* nothing to aggregate with - either aggregation disabled or no
457 * suitable aggregation packet found */
458 if (!forw_packet_aggr) {
459 /* the following section can run without the lock */
460 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
461
462 /**
463 * if we could not aggregate this packet with one of the others
464 * we hold it back for a while, so that it might be aggregated
465 * later on
466 */
467 if ((!own_packet) &&
468 (atomic_read(&bat_priv->aggregated_ogms)))
469 send_time += msecs_to_jiffies(MAX_AGGREGATION_MS);
470
Marek Lindner01c42242011-11-28 21:31:55 +0800471 bat_iv_ogm_aggregate_new(packet_buff, packet_len,
472 send_time, direct_link,
473 if_incoming, own_packet);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200474 } else {
Marek Lindner01c42242011-11-28 21:31:55 +0800475 bat_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
476 packet_len, direct_link);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200477 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
478 }
479}
480
Marek Lindner01c42242011-11-28 21:31:55 +0800481static void bat_iv_ogm_forward(struct orig_node *orig_node,
482 const struct ethhdr *ethhdr,
483 struct batman_ogm_packet *batman_ogm_packet,
484 int directlink, struct hard_iface *if_incoming)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200485{
486 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
487 struct neigh_node *router;
488 uint8_t in_tq, in_ttl, tq_avg = 0;
489 uint8_t tt_num_changes;
490
Sven Eckelmann76543d12011-11-20 15:47:38 +0100491 if (batman_ogm_packet->header.ttl <= 1) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200492 bat_dbg(DBG_BATMAN, bat_priv, "ttl exceeded\n");
493 return;
494 }
495
496 router = orig_node_get_router(orig_node);
497
498 in_tq = batman_ogm_packet->tq;
Sven Eckelmann76543d12011-11-20 15:47:38 +0100499 in_ttl = batman_ogm_packet->header.ttl;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200500 tt_num_changes = batman_ogm_packet->tt_num_changes;
501
Sven Eckelmann76543d12011-11-20 15:47:38 +0100502 batman_ogm_packet->header.ttl--;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200503 memcpy(batman_ogm_packet->prev_sender, ethhdr->h_source, ETH_ALEN);
504
505 /* rebroadcast tq of our best ranking neighbor to ensure the rebroadcast
506 * of our best tq value */
507 if (router && router->tq_avg != 0) {
508
509 /* rebroadcast ogm of best ranking neighbor as is */
510 if (!compare_eth(router->addr, ethhdr->h_source)) {
511 batman_ogm_packet->tq = router->tq_avg;
512
513 if (router->last_ttl)
Sven Eckelmann76543d12011-11-20 15:47:38 +0100514 batman_ogm_packet->header.ttl =
515 router->last_ttl - 1;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200516 }
517
518 tq_avg = router->tq_avg;
519 }
520
521 if (router)
522 neigh_node_free_ref(router);
523
524 /* apply hop penalty */
525 batman_ogm_packet->tq = hop_penalty(batman_ogm_packet->tq, bat_priv);
526
527 bat_dbg(DBG_BATMAN, bat_priv,
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100528 "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 +0200529 in_tq, tq_avg, batman_ogm_packet->tq, in_ttl - 1,
Sven Eckelmann76543d12011-11-20 15:47:38 +0100530 batman_ogm_packet->header.ttl);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200531
532 batman_ogm_packet->seqno = htonl(batman_ogm_packet->seqno);
533 batman_ogm_packet->tt_crc = htons(batman_ogm_packet->tt_crc);
534
535 /* switch of primaries first hop flag when forwarding */
536 batman_ogm_packet->flags &= ~PRIMARIES_FIRST_HOP;
537 if (directlink)
538 batman_ogm_packet->flags |= DIRECTLINK;
539 else
540 batman_ogm_packet->flags &= ~DIRECTLINK;
541
Marek Lindner01c42242011-11-28 21:31:55 +0800542 bat_iv_ogm_queue_add(bat_priv, (unsigned char *)batman_ogm_packet,
Marek Lindner76e3d7f2012-02-07 17:20:50 +0800543 BATMAN_OGM_HLEN + tt_len(tt_num_changes),
Marek Lindner01c42242011-11-28 21:31:55 +0800544 if_incoming, 0, bat_iv_ogm_fwd_send_time());
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200545}
546
Marek Lindner01c42242011-11-28 21:31:55 +0800547static void bat_iv_ogm_schedule(struct hard_iface *hard_iface,
548 int tt_num_changes)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200549{
550 struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
551 struct batman_ogm_packet *batman_ogm_packet;
552 struct hard_iface *primary_if;
553 int vis_server;
554
555 vis_server = atomic_read(&bat_priv->vis_mode);
556 primary_if = primary_if_get_selected(bat_priv);
557
558 batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff;
559
560 /* change sequence number to network order */
561 batman_ogm_packet->seqno =
562 htonl((uint32_t)atomic_read(&hard_iface->seqno));
563
564 batman_ogm_packet->ttvn = atomic_read(&bat_priv->ttvn);
565 batman_ogm_packet->tt_crc = htons((uint16_t)
566 atomic_read(&bat_priv->tt_crc));
567 if (tt_num_changes >= 0)
568 batman_ogm_packet->tt_num_changes = tt_num_changes;
569
570 if (vis_server == VIS_TYPE_SERVER_SYNC)
571 batman_ogm_packet->flags |= VIS_SERVER;
572 else
573 batman_ogm_packet->flags &= ~VIS_SERVER;
574
575 if ((hard_iface == primary_if) &&
576 (atomic_read(&bat_priv->gw_mode) == GW_MODE_SERVER))
577 batman_ogm_packet->gw_flags =
578 (uint8_t)atomic_read(&bat_priv->gw_bandwidth);
579 else
580 batman_ogm_packet->gw_flags = NO_FLAGS;
581
582 atomic_inc(&hard_iface->seqno);
583
584 slide_own_bcast_window(hard_iface);
Marek Lindner01c42242011-11-28 21:31:55 +0800585 bat_iv_ogm_queue_add(bat_priv, hard_iface->packet_buff,
586 hard_iface->packet_len, hard_iface, 1,
587 bat_iv_ogm_emit_send_time(bat_priv));
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200588
589 if (primary_if)
590 hardif_free_ref(primary_if);
591}
592
Marek Lindner01c42242011-11-28 21:31:55 +0800593static void bat_iv_ogm_orig_update(struct bat_priv *bat_priv,
594 struct orig_node *orig_node,
595 const struct ethhdr *ethhdr,
596 const struct batman_ogm_packet
Marek Lindnerfc957272011-07-30 12:04:12 +0200597 *batman_ogm_packet,
Marek Lindner01c42242011-11-28 21:31:55 +0800598 struct hard_iface *if_incoming,
599 const unsigned char *tt_buff,
600 int is_duplicate)
Marek Lindnerfc957272011-07-30 12:04:12 +0200601{
602 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
603 struct neigh_node *router = NULL;
604 struct orig_node *orig_node_tmp;
605 struct hlist_node *node;
606 uint8_t bcast_own_sum_orig, bcast_own_sum_neigh;
607
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100608 bat_dbg(DBG_BATMAN, bat_priv,
609 "update_originator(): Searching and updating originator entry of received packet\n");
Marek Lindnerfc957272011-07-30 12:04:12 +0200610
611 rcu_read_lock();
612 hlist_for_each_entry_rcu(tmp_neigh_node, node,
613 &orig_node->neigh_list, list) {
614 if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
615 (tmp_neigh_node->if_incoming == if_incoming) &&
616 atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
617 if (neigh_node)
618 neigh_node_free_ref(neigh_node);
619 neigh_node = tmp_neigh_node;
620 continue;
621 }
622
623 if (is_duplicate)
624 continue;
625
626 spin_lock_bh(&tmp_neigh_node->tq_lock);
627 ring_buffer_set(tmp_neigh_node->tq_recv,
628 &tmp_neigh_node->tq_index, 0);
629 tmp_neigh_node->tq_avg =
630 ring_buffer_avg(tmp_neigh_node->tq_recv);
631 spin_unlock_bh(&tmp_neigh_node->tq_lock);
632 }
633
634 if (!neigh_node) {
635 struct orig_node *orig_tmp;
636
637 orig_tmp = get_orig_node(bat_priv, ethhdr->h_source);
638 if (!orig_tmp)
639 goto unlock;
640
641 neigh_node = create_neighbor(orig_node, orig_tmp,
642 ethhdr->h_source, if_incoming);
643
644 orig_node_free_ref(orig_tmp);
645 if (!neigh_node)
646 goto unlock;
647 } else
648 bat_dbg(DBG_BATMAN, bat_priv,
649 "Updating existing last-hop neighbor of originator\n");
650
651 rcu_read_unlock();
652
653 orig_node->flags = batman_ogm_packet->flags;
654 neigh_node->last_valid = jiffies;
655
656 spin_lock_bh(&neigh_node->tq_lock);
657 ring_buffer_set(neigh_node->tq_recv,
658 &neigh_node->tq_index,
659 batman_ogm_packet->tq);
660 neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
661 spin_unlock_bh(&neigh_node->tq_lock);
662
663 if (!is_duplicate) {
Sven Eckelmann76543d12011-11-20 15:47:38 +0100664 orig_node->last_ttl = batman_ogm_packet->header.ttl;
665 neigh_node->last_ttl = batman_ogm_packet->header.ttl;
Marek Lindnerfc957272011-07-30 12:04:12 +0200666 }
667
668 bonding_candidate_add(orig_node, neigh_node);
669
670 /* if this neighbor already is our next hop there is nothing
671 * to change */
672 router = orig_node_get_router(orig_node);
673 if (router == neigh_node)
674 goto update_tt;
675
676 /* if this neighbor does not offer a better TQ we won't consider it */
677 if (router && (router->tq_avg > neigh_node->tq_avg))
678 goto update_tt;
679
680 /* if the TQ is the same and the link not more symmetric we
681 * won't consider it either */
682 if (router && (neigh_node->tq_avg == router->tq_avg)) {
683 orig_node_tmp = router->orig_node;
684 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
685 bcast_own_sum_orig =
686 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
687 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
688
689 orig_node_tmp = neigh_node->orig_node;
690 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
691 bcast_own_sum_neigh =
692 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
693 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
694
695 if (bcast_own_sum_orig >= bcast_own_sum_neigh)
696 goto update_tt;
697 }
698
699 update_route(bat_priv, orig_node, neigh_node);
700
701update_tt:
702 /* I have to check for transtable changes only if the OGM has been
703 * sent through a primary interface */
704 if (((batman_ogm_packet->orig != ethhdr->h_source) &&
Sven Eckelmann76543d12011-11-20 15:47:38 +0100705 (batman_ogm_packet->header.ttl > 2)) ||
Marek Lindnerfc957272011-07-30 12:04:12 +0200706 (batman_ogm_packet->flags & PRIMARIES_FIRST_HOP))
707 tt_update_orig(bat_priv, orig_node, tt_buff,
708 batman_ogm_packet->tt_num_changes,
709 batman_ogm_packet->ttvn,
710 batman_ogm_packet->tt_crc);
711
712 if (orig_node->gw_flags != batman_ogm_packet->gw_flags)
713 gw_node_update(bat_priv, orig_node,
714 batman_ogm_packet->gw_flags);
715
716 orig_node->gw_flags = batman_ogm_packet->gw_flags;
717
718 /* restart gateway selection if fast or late switching was enabled */
719 if ((orig_node->gw_flags) &&
720 (atomic_read(&bat_priv->gw_mode) == GW_MODE_CLIENT) &&
721 (atomic_read(&bat_priv->gw_sel_class) > 2))
722 gw_check_election(bat_priv, orig_node);
723
724 goto out;
725
726unlock:
727 rcu_read_unlock();
728out:
729 if (neigh_node)
730 neigh_node_free_ref(neigh_node);
731 if (router)
732 neigh_node_free_ref(router);
733}
734
Marek Lindner01c42242011-11-28 21:31:55 +0800735static int bat_iv_ogm_calc_tq(struct orig_node *orig_node,
736 struct orig_node *orig_neigh_node,
737 struct batman_ogm_packet *batman_ogm_packet,
738 struct hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +0200739{
740 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
741 struct neigh_node *neigh_node = NULL, *tmp_neigh_node;
742 struct hlist_node *node;
743 uint8_t total_count;
744 uint8_t orig_eq_count, neigh_rq_count, tq_own;
745 int tq_asym_penalty, ret = 0;
746
747 /* find corresponding one hop neighbor */
748 rcu_read_lock();
749 hlist_for_each_entry_rcu(tmp_neigh_node, node,
750 &orig_neigh_node->neigh_list, list) {
751
752 if (!compare_eth(tmp_neigh_node->addr, orig_neigh_node->orig))
753 continue;
754
755 if (tmp_neigh_node->if_incoming != if_incoming)
756 continue;
757
758 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
759 continue;
760
761 neigh_node = tmp_neigh_node;
762 break;
763 }
764 rcu_read_unlock();
765
766 if (!neigh_node)
767 neigh_node = create_neighbor(orig_neigh_node,
768 orig_neigh_node,
769 orig_neigh_node->orig,
770 if_incoming);
771
772 if (!neigh_node)
773 goto out;
774
775 /* if orig_node is direct neighbor update neigh_node last_valid */
776 if (orig_node == orig_neigh_node)
777 neigh_node->last_valid = jiffies;
778
779 orig_node->last_valid = jiffies;
780
781 /* find packet count of corresponding one hop neighbor */
782 spin_lock_bh(&orig_node->ogm_cnt_lock);
783 orig_eq_count = orig_neigh_node->bcast_own_sum[if_incoming->if_num];
784 neigh_rq_count = neigh_node->real_packet_count;
785 spin_unlock_bh(&orig_node->ogm_cnt_lock);
786
787 /* pay attention to not get a value bigger than 100 % */
788 total_count = (orig_eq_count > neigh_rq_count ?
789 neigh_rq_count : orig_eq_count);
790
791 /* if we have too few packets (too less data) we set tq_own to zero */
792 /* if we receive too few packets it is not considered bidirectional */
793 if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
794 (neigh_rq_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
795 tq_own = 0;
796 else
797 /* neigh_node->real_packet_count is never zero as we
798 * only purge old information when getting new
799 * information */
800 tq_own = (TQ_MAX_VALUE * total_count) / neigh_rq_count;
801
Sven Eckelmann21a12362012-03-07 09:07:46 +0100802 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
Marek Lindnerfc957272011-07-30 12:04:12 +0200803 * affect the nearly-symmetric links only a little, but
804 * punishes asymmetric links more. This will give a value
805 * between 0 and TQ_MAX_VALUE
806 */
807 tq_asym_penalty = TQ_MAX_VALUE - (TQ_MAX_VALUE *
808 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
809 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
810 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count)) /
811 (TQ_LOCAL_WINDOW_SIZE *
812 TQ_LOCAL_WINDOW_SIZE *
813 TQ_LOCAL_WINDOW_SIZE);
814
815 batman_ogm_packet->tq = ((batman_ogm_packet->tq * tq_own
816 * tq_asym_penalty) /
817 (TQ_MAX_VALUE * TQ_MAX_VALUE));
818
819 bat_dbg(DBG_BATMAN, bat_priv,
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100820 "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 +0200821 orig_node->orig, orig_neigh_node->orig, total_count,
822 neigh_rq_count, tq_own, tq_asym_penalty, batman_ogm_packet->tq);
823
824 /* if link has the minimum required transmission quality
825 * consider it bidirectional */
826 if (batman_ogm_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
827 ret = 1;
828
829out:
830 if (neigh_node)
831 neigh_node_free_ref(neigh_node);
832 return ret;
833}
834
835/* processes a batman packet for all interfaces, adjusts the sequence number and
836 * finds out whether it is a duplicate.
837 * returns:
838 * 1 the packet is a duplicate
839 * 0 the packet has not yet been received
840 * -1 the packet is old and has been received while the seqno window
841 * was protected. Caller should drop it.
842 */
Marek Lindner01c42242011-11-28 21:31:55 +0800843static int bat_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
844 const struct batman_ogm_packet
Marek Lindnerfc957272011-07-30 12:04:12 +0200845 *batman_ogm_packet,
Marek Lindner01c42242011-11-28 21:31:55 +0800846 const struct hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +0200847{
848 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
849 struct orig_node *orig_node;
850 struct neigh_node *tmp_neigh_node;
851 struct hlist_node *node;
852 int is_duplicate = 0;
853 int32_t seq_diff;
854 int need_update = 0;
855 int set_mark, ret = -1;
856
857 orig_node = get_orig_node(bat_priv, batman_ogm_packet->orig);
858 if (!orig_node)
859 return 0;
860
861 spin_lock_bh(&orig_node->ogm_cnt_lock);
862 seq_diff = batman_ogm_packet->seqno - orig_node->last_real_seqno;
863
864 /* signalize caller that the packet is to be dropped. */
865 if (window_protected(bat_priv, seq_diff,
866 &orig_node->batman_seqno_reset))
867 goto out;
868
869 rcu_read_lock();
870 hlist_for_each_entry_rcu(tmp_neigh_node, node,
871 &orig_node->neigh_list, list) {
872
Sven Eckelmann0079d2c2012-02-04 17:34:52 +0100873 is_duplicate |= bat_test_bit(tmp_neigh_node->real_bits,
874 orig_node->last_real_seqno,
875 batman_ogm_packet->seqno);
Marek Lindnerfc957272011-07-30 12:04:12 +0200876
877 if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
878 (tmp_neigh_node->if_incoming == if_incoming))
879 set_mark = 1;
880 else
881 set_mark = 0;
882
883 /* if the window moved, set the update flag. */
884 need_update |= bit_get_packet(bat_priv,
885 tmp_neigh_node->real_bits,
886 seq_diff, set_mark);
887
888 tmp_neigh_node->real_packet_count =
Sven Eckelmann0079d2c2012-02-04 17:34:52 +0100889 bitmap_weight(tmp_neigh_node->real_bits,
890 TQ_LOCAL_WINDOW_SIZE);
Marek Lindnerfc957272011-07-30 12:04:12 +0200891 }
892 rcu_read_unlock();
893
894 if (need_update) {
895 bat_dbg(DBG_BATMAN, bat_priv,
896 "updating last_seqno: old %d, new %d\n",
897 orig_node->last_real_seqno, batman_ogm_packet->seqno);
898 orig_node->last_real_seqno = batman_ogm_packet->seqno;
899 }
900
901 ret = is_duplicate;
902
903out:
904 spin_unlock_bh(&orig_node->ogm_cnt_lock);
905 orig_node_free_ref(orig_node);
906 return ret;
907}
908
Marek Lindner01c42242011-11-28 21:31:55 +0800909static void bat_iv_ogm_process(const struct ethhdr *ethhdr,
910 struct batman_ogm_packet *batman_ogm_packet,
911 const unsigned char *tt_buff,
912 struct hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +0200913{
914 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
915 struct hard_iface *hard_iface;
916 struct orig_node *orig_neigh_node, *orig_node;
917 struct neigh_node *router = NULL, *router_router = NULL;
918 struct neigh_node *orig_neigh_router = NULL;
919 int has_directlink_flag;
920 int is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
921 int is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
922 int is_duplicate;
923 uint32_t if_incoming_seqno;
924
925 /* Silently drop when the batman packet is actually not a
926 * correct packet.
927 *
928 * This might happen if a packet is padded (e.g. Ethernet has a
929 * minimum frame length of 64 byte) and the aggregation interprets
930 * it as an additional length.
931 *
932 * TODO: A more sane solution would be to have a bit in the
933 * batman_ogm_packet to detect whether the packet is the last
934 * packet in an aggregation. Here we expect that the padding
935 * is always zero (or not 0x01)
936 */
Marek Lindner1eeb4792012-02-07 17:20:51 +0800937 if (batman_ogm_packet->header.packet_type != BAT_IV_OGM)
Marek Lindnerfc957272011-07-30 12:04:12 +0200938 return;
939
940 /* could be changed by schedule_own_packet() */
941 if_incoming_seqno = atomic_read(&if_incoming->seqno);
942
943 has_directlink_flag = (batman_ogm_packet->flags & DIRECTLINK ? 1 : 0);
944
945 is_single_hop_neigh = (compare_eth(ethhdr->h_source,
946 batman_ogm_packet->orig) ? 1 : 0);
947
948 bat_dbg(DBG_BATMAN, bat_priv,
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100949 "Received BATMAN packet via NB: %pM, IF: %s [%pM] (from OG: %pM, via prev OG: %pM, seqno %d, ttvn %u, crc %u, changes %u, td %d, TTL %d, V %d, IDF %d)\n",
Marek Lindnerfc957272011-07-30 12:04:12 +0200950 ethhdr->h_source, if_incoming->net_dev->name,
951 if_incoming->net_dev->dev_addr, batman_ogm_packet->orig,
952 batman_ogm_packet->prev_sender, batman_ogm_packet->seqno,
953 batman_ogm_packet->ttvn, batman_ogm_packet->tt_crc,
954 batman_ogm_packet->tt_num_changes, batman_ogm_packet->tq,
Sven Eckelmann76543d12011-11-20 15:47:38 +0100955 batman_ogm_packet->header.ttl,
956 batman_ogm_packet->header.version, has_directlink_flag);
Marek Lindnerfc957272011-07-30 12:04:12 +0200957
958 rcu_read_lock();
959 list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
960 if (hard_iface->if_status != IF_ACTIVE)
961 continue;
962
963 if (hard_iface->soft_iface != if_incoming->soft_iface)
964 continue;
965
966 if (compare_eth(ethhdr->h_source,
967 hard_iface->net_dev->dev_addr))
968 is_my_addr = 1;
969
970 if (compare_eth(batman_ogm_packet->orig,
971 hard_iface->net_dev->dev_addr))
972 is_my_orig = 1;
973
974 if (compare_eth(batman_ogm_packet->prev_sender,
975 hard_iface->net_dev->dev_addr))
976 is_my_oldorig = 1;
977
978 if (is_broadcast_ether_addr(ethhdr->h_source))
979 is_broadcast = 1;
980 }
981 rcu_read_unlock();
982
Sven Eckelmann76543d12011-11-20 15:47:38 +0100983 if (batman_ogm_packet->header.version != COMPAT_VERSION) {
Marek Lindnerfc957272011-07-30 12:04:12 +0200984 bat_dbg(DBG_BATMAN, bat_priv,
985 "Drop packet: incompatible batman version (%i)\n",
Sven Eckelmann76543d12011-11-20 15:47:38 +0100986 batman_ogm_packet->header.version);
Marek Lindnerfc957272011-07-30 12:04:12 +0200987 return;
988 }
989
990 if (is_my_addr) {
991 bat_dbg(DBG_BATMAN, bat_priv,
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100992 "Drop packet: received my own broadcast (sender: %pM)\n",
Marek Lindnerfc957272011-07-30 12:04:12 +0200993 ethhdr->h_source);
994 return;
995 }
996
997 if (is_broadcast) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100998 bat_dbg(DBG_BATMAN, bat_priv,
999 "Drop packet: ignoring all packets with broadcast source addr (sender: %pM)\n",
1000 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001001 return;
1002 }
1003
1004 if (is_my_orig) {
1005 unsigned long *word;
1006 int offset;
1007
1008 orig_neigh_node = get_orig_node(bat_priv, ethhdr->h_source);
1009 if (!orig_neigh_node)
1010 return;
1011
1012 /* neighbor has to indicate direct link and it has to
1013 * come via the corresponding interface */
1014 /* save packet seqno for bidirectional check */
1015 if (has_directlink_flag &&
1016 compare_eth(if_incoming->net_dev->dev_addr,
1017 batman_ogm_packet->orig)) {
1018 offset = if_incoming->if_num * NUM_WORDS;
1019
1020 spin_lock_bh(&orig_neigh_node->ogm_cnt_lock);
1021 word = &(orig_neigh_node->bcast_own[offset]);
Sven Eckelmann0079d2c2012-02-04 17:34:52 +01001022 bat_set_bit(word,
1023 if_incoming_seqno -
Marek Lindnerfc957272011-07-30 12:04:12 +02001024 batman_ogm_packet->seqno - 2);
1025 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
Sven Eckelmann0079d2c2012-02-04 17:34:52 +01001026 bitmap_weight(word, TQ_LOCAL_WINDOW_SIZE);
Marek Lindnerfc957272011-07-30 12:04:12 +02001027 spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock);
1028 }
1029
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001030 bat_dbg(DBG_BATMAN, bat_priv,
1031 "Drop packet: originator packet from myself (via neighbor)\n");
Marek Lindnerfc957272011-07-30 12:04:12 +02001032 orig_node_free_ref(orig_neigh_node);
1033 return;
1034 }
1035
1036 if (is_my_oldorig) {
1037 bat_dbg(DBG_BATMAN, bat_priv,
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001038 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1039 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001040 return;
1041 }
1042
1043 orig_node = get_orig_node(bat_priv, batman_ogm_packet->orig);
1044 if (!orig_node)
1045 return;
1046
Marek Lindner01c42242011-11-28 21:31:55 +08001047 is_duplicate = bat_iv_ogm_update_seqnos(ethhdr, batman_ogm_packet,
1048 if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001049
1050 if (is_duplicate == -1) {
1051 bat_dbg(DBG_BATMAN, bat_priv,
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001052 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1053 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001054 goto out;
1055 }
1056
1057 if (batman_ogm_packet->tq == 0) {
1058 bat_dbg(DBG_BATMAN, bat_priv,
1059 "Drop packet: originator packet with tq equal 0\n");
1060 goto out;
1061 }
1062
1063 router = orig_node_get_router(orig_node);
1064 if (router)
1065 router_router = orig_node_get_router(router->orig_node);
1066
1067 /* avoid temporary routing loops */
1068 if (router && router_router &&
1069 (compare_eth(router->addr, batman_ogm_packet->prev_sender)) &&
1070 !(compare_eth(batman_ogm_packet->orig,
1071 batman_ogm_packet->prev_sender)) &&
1072 (compare_eth(router->addr, router_router->addr))) {
1073 bat_dbg(DBG_BATMAN, bat_priv,
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001074 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1075 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001076 goto out;
1077 }
1078
1079 /* if sender is a direct neighbor the sender mac equals
1080 * originator mac */
1081 orig_neigh_node = (is_single_hop_neigh ?
1082 orig_node :
1083 get_orig_node(bat_priv, ethhdr->h_source));
1084 if (!orig_neigh_node)
1085 goto out;
1086
1087 orig_neigh_router = orig_node_get_router(orig_neigh_node);
1088
1089 /* drop packet if sender is not a direct neighbor and if we
1090 * don't route towards it */
1091 if (!is_single_hop_neigh && (!orig_neigh_router)) {
1092 bat_dbg(DBG_BATMAN, bat_priv,
1093 "Drop packet: OGM via unknown neighbor!\n");
1094 goto out_neigh;
1095 }
1096
Marek Lindner01c42242011-11-28 21:31:55 +08001097 is_bidirectional = bat_iv_ogm_calc_tq(orig_node, orig_neigh_node,
1098 batman_ogm_packet, if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001099
1100 bonding_save_primary(orig_node, orig_neigh_node, batman_ogm_packet);
1101
1102 /* update ranking if it is not a duplicate or has the same
1103 * seqno and similar ttl as the non-duplicate */
1104 if (is_bidirectional &&
1105 (!is_duplicate ||
1106 ((orig_node->last_real_seqno == batman_ogm_packet->seqno) &&
Sven Eckelmann76543d12011-11-20 15:47:38 +01001107 (orig_node->last_ttl - 3 <= batman_ogm_packet->header.ttl))))
Marek Lindner01c42242011-11-28 21:31:55 +08001108 bat_iv_ogm_orig_update(bat_priv, orig_node, ethhdr,
1109 batman_ogm_packet, if_incoming,
1110 tt_buff, is_duplicate);
Marek Lindnerfc957272011-07-30 12:04:12 +02001111
1112 /* is single hop (direct) neighbor */
1113 if (is_single_hop_neigh) {
1114
1115 /* mark direct link on incoming interface */
Marek Lindner01c42242011-11-28 21:31:55 +08001116 bat_iv_ogm_forward(orig_node, ethhdr, batman_ogm_packet,
1117 1, if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001118
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001119 bat_dbg(DBG_BATMAN, bat_priv,
1120 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
Marek Lindnerfc957272011-07-30 12:04:12 +02001121 goto out_neigh;
1122 }
1123
1124 /* multihop originator */
1125 if (!is_bidirectional) {
1126 bat_dbg(DBG_BATMAN, bat_priv,
1127 "Drop packet: not received via bidirectional link\n");
1128 goto out_neigh;
1129 }
1130
1131 if (is_duplicate) {
1132 bat_dbg(DBG_BATMAN, bat_priv,
1133 "Drop packet: duplicate packet received\n");
1134 goto out_neigh;
1135 }
1136
1137 bat_dbg(DBG_BATMAN, bat_priv,
1138 "Forwarding packet: rebroadcast originator packet\n");
Marek Lindner01c42242011-11-28 21:31:55 +08001139 bat_iv_ogm_forward(orig_node, ethhdr, batman_ogm_packet,
1140 0, if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001141
1142out_neigh:
1143 if ((orig_neigh_node) && (!is_single_hop_neigh))
1144 orig_node_free_ref(orig_neigh_node);
1145out:
1146 if (router)
1147 neigh_node_free_ref(router);
1148 if (router_router)
1149 neigh_node_free_ref(router_router);
1150 if (orig_neigh_router)
1151 neigh_node_free_ref(orig_neigh_router);
1152
1153 orig_node_free_ref(orig_node);
1154}
1155
Marek Lindner01c42242011-11-28 21:31:55 +08001156static void bat_iv_ogm_receive(struct hard_iface *if_incoming,
1157 struct sk_buff *skb)
Marek Lindnerfc957272011-07-30 12:04:12 +02001158{
1159 struct batman_ogm_packet *batman_ogm_packet;
Marek Lindner8780dad2011-12-05 04:01:51 +08001160 struct ethhdr *ethhdr;
1161 int buff_pos = 0, packet_len;
1162 unsigned char *tt_buff, *packet_buff;
Marek Lindnerfc957272011-07-30 12:04:12 +02001163
Marek Lindner8780dad2011-12-05 04:01:51 +08001164 packet_len = skb_headlen(skb);
1165 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1166 packet_buff = skb->data;
Marek Lindnerfc957272011-07-30 12:04:12 +02001167 batman_ogm_packet = (struct batman_ogm_packet *)packet_buff;
1168
1169 /* unpack the aggregated packets and process them one by one */
1170 do {
1171 /* network to host order for our 32bit seqno and the
1172 orig_interval */
1173 batman_ogm_packet->seqno = ntohl(batman_ogm_packet->seqno);
1174 batman_ogm_packet->tt_crc = ntohs(batman_ogm_packet->tt_crc);
1175
Marek Lindner76e3d7f2012-02-07 17:20:50 +08001176 tt_buff = packet_buff + buff_pos + BATMAN_OGM_HLEN;
Marek Lindnerfc957272011-07-30 12:04:12 +02001177
Marek Lindner01c42242011-11-28 21:31:55 +08001178 bat_iv_ogm_process(ethhdr, batman_ogm_packet,
1179 tt_buff, if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001180
Marek Lindner76e3d7f2012-02-07 17:20:50 +08001181 buff_pos += BATMAN_OGM_HLEN +
Marek Lindnerfc957272011-07-30 12:04:12 +02001182 tt_len(batman_ogm_packet->tt_num_changes);
1183
1184 batman_ogm_packet = (struct batman_ogm_packet *)
1185 (packet_buff + buff_pos);
Marek Lindner01c42242011-11-28 21:31:55 +08001186 } while (bat_iv_ogm_aggr_packet(buff_pos, packet_len,
1187 batman_ogm_packet->tt_num_changes));
Marek Lindnerfc957272011-07-30 12:04:12 +02001188}
Marek Lindner1c280472011-11-28 17:40:17 +08001189
1190static struct bat_algo_ops batman_iv __read_mostly = {
1191 .name = "BATMAN IV",
Marek Lindnerc2aca022012-02-07 17:20:45 +08001192 .bat_iface_enable = bat_iv_ogm_iface_enable,
Marek Lindner00a50072012-02-07 17:20:47 +08001193 .bat_iface_disable = bat_iv_ogm_iface_disable,
Marek Lindnercd8b78e2012-02-07 17:20:49 +08001194 .bat_primary_iface_set = bat_iv_ogm_primary_iface_set,
Marek Lindner01c42242011-11-28 21:31:55 +08001195 .bat_ogm_update_mac = bat_iv_ogm_update_mac,
1196 .bat_ogm_schedule = bat_iv_ogm_schedule,
1197 .bat_ogm_emit = bat_iv_ogm_emit,
1198 .bat_ogm_receive = bat_iv_ogm_receive,
Marek Lindner1c280472011-11-28 17:40:17 +08001199};
1200
1201int __init bat_iv_init(void)
1202{
1203 return bat_algo_register(&batman_iv);
1204}