blob: ec351199c652aa3f8eaaa4b4086031fd9b005349 [file] [log] [blame]
Marek Lindnerfc957272011-07-30 12:04:12 +02001/*
Sven Eckelmann567db7b2012-01-01 00:41:38 +01002 * Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
Marek Lindnerfc957272011-07-30 12:04:12 +02003 *
4 * Marek Lindner, Simon Wunderlich
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301, USA
19 *
20 */
21
22#include "main.h"
Marek Lindnerfc957272011-07-30 12:04:12 +020023#include "translation-table.h"
24#include "ring_buffer.h"
25#include "originator.h"
26#include "routing.h"
27#include "gateway_common.h"
28#include "gateway_client.h"
29#include "hard-interface.h"
30#include "send.h"
Marek Lindner1c280472011-11-28 17:40:17 +080031#include "bat_algo.h"
Marek Lindnerfc957272011-07-30 12:04:12 +020032
Marek Lindner7ae8b282012-03-01 15:35:21 +080033static struct neigh_node *bat_iv_ogm_neigh_new(struct hard_iface *hard_iface,
34 const uint8_t *neigh_addr,
35 struct orig_node *orig_node,
36 struct orig_node *orig_neigh,
37 uint32_t seqno)
38{
39 struct neigh_node *neigh_node;
40
41 neigh_node = batadv_neigh_node_new(hard_iface, neigh_addr, seqno);
42 if (!neigh_node)
43 goto out;
44
45 INIT_LIST_HEAD(&neigh_node->bonding_list);
Marek Lindner7ae8b282012-03-01 15:35:21 +080046
47 neigh_node->orig_node = orig_neigh;
48 neigh_node->if_incoming = hard_iface;
49
50 spin_lock_bh(&orig_node->neigh_list_lock);
51 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
52 spin_unlock_bh(&orig_node->neigh_list_lock);
53
54out:
55 return neigh_node;
56}
57
Marek Lindner77af7572012-02-07 17:20:48 +080058static int bat_iv_ogm_iface_enable(struct hard_iface *hard_iface)
Marek Lindnerd0b9fd82011-07-30 12:33:33 +020059{
60 struct batman_ogm_packet *batman_ogm_packet;
Marek Lindnerd7d32ec2012-02-07 17:20:46 +080061 uint32_t random_seqno;
Marek Lindner77af7572012-02-07 17:20:48 +080062 int res = -1;
Marek Lindnerd7d32ec2012-02-07 17:20:46 +080063
64 /* randomize initial seqno to avoid collision */
65 get_random_bytes(&random_seqno, sizeof(random_seqno));
66 atomic_set(&hard_iface->seqno, random_seqno);
Marek Lindnerd0b9fd82011-07-30 12:33:33 +020067
Marek Lindner76e3d7fc2012-02-07 17:20:50 +080068 hard_iface->packet_len = BATMAN_OGM_HLEN;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +020069 hard_iface->packet_buff = kmalloc(hard_iface->packet_len, GFP_ATOMIC);
70
Marek Lindner77af7572012-02-07 17:20:48 +080071 if (!hard_iface->packet_buff)
72 goto out;
73
Marek Lindnerd0b9fd82011-07-30 12:33:33 +020074 batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff;
Marek Lindner1eeb4792012-02-07 17:20:51 +080075 batman_ogm_packet->header.packet_type = BAT_IV_OGM;
Sven Eckelmann76543d12011-11-20 15:47:38 +010076 batman_ogm_packet->header.version = COMPAT_VERSION;
77 batman_ogm_packet->header.ttl = 2;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +020078 batman_ogm_packet->flags = NO_FLAGS;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +020079 batman_ogm_packet->tq = TQ_MAX_VALUE;
80 batman_ogm_packet->tt_num_changes = 0;
81 batman_ogm_packet->ttvn = 0;
Marek Lindner77af7572012-02-07 17:20:48 +080082
83 res = 0;
84
85out:
86 return res;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +020087}
88
Marek Lindner00a50072012-02-07 17:20:47 +080089static void bat_iv_ogm_iface_disable(struct hard_iface *hard_iface)
90{
91 kfree(hard_iface->packet_buff);
92 hard_iface->packet_buff = NULL;
93}
94
Marek Lindnerc3229392012-03-11 06:17:50 +080095static void bat_iv_ogm_iface_update_mac(struct hard_iface *hard_iface)
Marek Lindnerd0b9fd82011-07-30 12:33:33 +020096{
97 struct batman_ogm_packet *batman_ogm_packet;
98
99 batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff;
100 memcpy(batman_ogm_packet->orig,
101 hard_iface->net_dev->dev_addr, ETH_ALEN);
102 memcpy(batman_ogm_packet->prev_sender,
103 hard_iface->net_dev->dev_addr, ETH_ALEN);
104}
105
Marek Lindnerc3229392012-03-11 06:17:50 +0800106static void bat_iv_ogm_primary_iface_set(struct hard_iface *hard_iface)
107{
108 struct batman_ogm_packet *batman_ogm_packet;
109
110 batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff;
111 batman_ogm_packet->flags = PRIMARIES_FIRST_HOP;
112 batman_ogm_packet->header.ttl = TTL;
113}
114
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200115/* when do we schedule our own ogm to be sent */
Marek Lindner01c42242011-11-28 21:31:55 +0800116static unsigned long bat_iv_ogm_emit_send_time(const struct bat_priv *bat_priv)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200117{
118 return jiffies + msecs_to_jiffies(
119 atomic_read(&bat_priv->orig_interval) -
120 JITTER + (random32() % 2*JITTER));
121}
122
123/* when do we schedule a ogm packet to be sent */
Marek Lindner01c42242011-11-28 21:31:55 +0800124static unsigned long bat_iv_ogm_fwd_send_time(void)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200125{
126 return jiffies + msecs_to_jiffies(random32() % (JITTER/2));
127}
128
129/* apply hop penalty for a normal link */
130static uint8_t hop_penalty(uint8_t tq, const struct bat_priv *bat_priv)
131{
132 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
133 return (tq * (TQ_MAX_VALUE - hop_penalty)) / (TQ_MAX_VALUE);
134}
135
Marek Lindnerfc957272011-07-30 12:04:12 +0200136/* is there another aggregated packet here? */
Marek Lindner01c42242011-11-28 21:31:55 +0800137static int bat_iv_ogm_aggr_packet(int buff_pos, int packet_len,
138 int tt_num_changes)
Marek Lindnerfc957272011-07-30 12:04:12 +0200139{
Marek Lindner76e3d7fc2012-02-07 17:20:50 +0800140 int next_buff_pos = buff_pos + BATMAN_OGM_HLEN + tt_len(tt_num_changes);
Marek Lindnerfc957272011-07-30 12:04:12 +0200141
142 return (next_buff_pos <= packet_len) &&
143 (next_buff_pos <= MAX_AGGREGATION_BYTES);
144}
145
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200146/* send a batman ogm to a given interface */
Marek Lindner01c42242011-11-28 21:31:55 +0800147static void bat_iv_ogm_send_to_if(struct forw_packet *forw_packet,
148 struct hard_iface *hard_iface)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200149{
150 struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
151 char *fwd_str;
152 uint8_t packet_num;
153 int16_t buff_pos;
154 struct batman_ogm_packet *batman_ogm_packet;
155 struct sk_buff *skb;
156
157 if (hard_iface->if_status != IF_ACTIVE)
158 return;
159
160 packet_num = 0;
161 buff_pos = 0;
162 batman_ogm_packet = (struct batman_ogm_packet *)forw_packet->skb->data;
163
164 /* adjust all flags and log packets */
Marek Lindner01c42242011-11-28 21:31:55 +0800165 while (bat_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
166 batman_ogm_packet->tt_num_changes)) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200167
168 /* we might have aggregated direct link packets with an
169 * ordinary base packet */
170 if ((forw_packet->direct_link_flags & (1 << packet_num)) &&
171 (forw_packet->if_incoming == hard_iface))
172 batman_ogm_packet->flags |= DIRECTLINK;
173 else
174 batman_ogm_packet->flags &= ~DIRECTLINK;
175
176 fwd_str = (packet_num > 0 ? "Forwarding" : (forw_packet->own ?
177 "Sending own" :
178 "Forwarding"));
179 bat_dbg(DBG_BATMAN, bat_priv,
Antonio Quartullic97c72b2012-02-26 15:39:41 +0100180 "%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 +0200181 fwd_str, (packet_num > 0 ? "aggregated " : ""),
182 batman_ogm_packet->orig,
183 ntohl(batman_ogm_packet->seqno),
Sven Eckelmann76543d12011-11-20 15:47:38 +0100184 batman_ogm_packet->tq, batman_ogm_packet->header.ttl,
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200185 (batman_ogm_packet->flags & DIRECTLINK ?
186 "on" : "off"),
187 batman_ogm_packet->ttvn, hard_iface->net_dev->name,
188 hard_iface->net_dev->dev_addr);
189
Marek Lindner76e3d7fc2012-02-07 17:20:50 +0800190 buff_pos += BATMAN_OGM_HLEN +
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200191 tt_len(batman_ogm_packet->tt_num_changes);
192 packet_num++;
193 batman_ogm_packet = (struct batman_ogm_packet *)
194 (forw_packet->skb->data + buff_pos);
195 }
196
197 /* create clone because function is called more than once */
198 skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
199 if (skb)
200 send_skb_packet(skb, hard_iface, broadcast_addr);
201}
202
203/* send a batman ogm packet */
Marek Lindner01c42242011-11-28 21:31:55 +0800204static void bat_iv_ogm_emit(struct forw_packet *forw_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200205{
206 struct hard_iface *hard_iface;
207 struct net_device *soft_iface;
208 struct bat_priv *bat_priv;
209 struct hard_iface *primary_if = NULL;
210 struct batman_ogm_packet *batman_ogm_packet;
211 unsigned char directlink;
212
213 batman_ogm_packet = (struct batman_ogm_packet *)
214 (forw_packet->skb->data);
215 directlink = (batman_ogm_packet->flags & DIRECTLINK ? 1 : 0);
216
217 if (!forw_packet->if_incoming) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100218 pr_err("Error - can't forward packet: incoming iface not specified\n");
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200219 goto out;
220 }
221
222 soft_iface = forw_packet->if_incoming->soft_iface;
223 bat_priv = netdev_priv(soft_iface);
224
225 if (forw_packet->if_incoming->if_status != IF_ACTIVE)
226 goto out;
227
228 primary_if = primary_if_get_selected(bat_priv);
229 if (!primary_if)
230 goto out;
231
232 /* multihomed peer assumed */
233 /* non-primary OGMs are only broadcasted on their interface */
Sven Eckelmann76543d12011-11-20 15:47:38 +0100234 if ((directlink && (batman_ogm_packet->header.ttl == 1)) ||
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200235 (forw_packet->own && (forw_packet->if_incoming != primary_if))) {
236
237 /* FIXME: what about aggregated packets ? */
238 bat_dbg(DBG_BATMAN, bat_priv,
Antonio Quartullic97c72b2012-02-26 15:39:41 +0100239 "%s packet (originator %pM, seqno %u, TTL %d) on interface %s [%pM]\n",
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200240 (forw_packet->own ? "Sending own" : "Forwarding"),
241 batman_ogm_packet->orig,
242 ntohl(batman_ogm_packet->seqno),
Sven Eckelmann76543d12011-11-20 15:47:38 +0100243 batman_ogm_packet->header.ttl,
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200244 forw_packet->if_incoming->net_dev->name,
245 forw_packet->if_incoming->net_dev->dev_addr);
246
247 /* skb is only used once and than forw_packet is free'd */
248 send_skb_packet(forw_packet->skb, forw_packet->if_incoming,
249 broadcast_addr);
250 forw_packet->skb = NULL;
251
252 goto out;
253 }
254
255 /* broadcast on every interface */
256 rcu_read_lock();
257 list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
258 if (hard_iface->soft_iface != soft_iface)
259 continue;
260
Marek Lindner01c42242011-11-28 21:31:55 +0800261 bat_iv_ogm_send_to_if(forw_packet, hard_iface);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200262 }
263 rcu_read_unlock();
264
265out:
266 if (primary_if)
267 hardif_free_ref(primary_if);
268}
269
270/* return true if new_packet can be aggregated with forw_packet */
Marek Lindner01c42242011-11-28 21:31:55 +0800271static bool bat_iv_ogm_can_aggregate(const struct batman_ogm_packet
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200272 *new_batman_ogm_packet,
Marek Lindner01c42242011-11-28 21:31:55 +0800273 struct bat_priv *bat_priv,
274 int packet_len, unsigned long send_time,
275 bool directlink,
276 const struct hard_iface *if_incoming,
277 const struct forw_packet *forw_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200278{
279 struct batman_ogm_packet *batman_ogm_packet;
280 int aggregated_bytes = forw_packet->packet_len + packet_len;
281 struct hard_iface *primary_if = NULL;
282 bool res = false;
283
284 batman_ogm_packet = (struct batman_ogm_packet *)forw_packet->skb->data;
285
286 /**
287 * we can aggregate the current packet to this aggregated packet
288 * if:
289 *
290 * - the send time is within our MAX_AGGREGATION_MS time
291 * - the resulting packet wont be bigger than
292 * MAX_AGGREGATION_BYTES
293 */
294
295 if (time_before(send_time, forw_packet->send_time) &&
296 time_after_eq(send_time + msecs_to_jiffies(MAX_AGGREGATION_MS),
297 forw_packet->send_time) &&
298 (aggregated_bytes <= MAX_AGGREGATION_BYTES)) {
299
300 /**
301 * check aggregation compatibility
302 * -> direct link packets are broadcasted on
303 * their interface only
304 * -> aggregate packet if the current packet is
305 * a "global" packet as well as the base
306 * packet
307 */
308
309 primary_if = primary_if_get_selected(bat_priv);
310 if (!primary_if)
311 goto out;
312
313 /* packets without direct link flag and high TTL
314 * are flooded through the net */
315 if ((!directlink) &&
316 (!(batman_ogm_packet->flags & DIRECTLINK)) &&
Sven Eckelmann76543d12011-11-20 15:47:38 +0100317 (batman_ogm_packet->header.ttl != 1) &&
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200318
319 /* own packets originating non-primary
320 * interfaces leave only that interface */
321 ((!forw_packet->own) ||
322 (forw_packet->if_incoming == primary_if))) {
323 res = true;
324 goto out;
325 }
326
327 /* if the incoming packet is sent via this one
328 * interface only - we still can aggregate */
329 if ((directlink) &&
Sven Eckelmann76543d12011-11-20 15:47:38 +0100330 (new_batman_ogm_packet->header.ttl == 1) &&
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200331 (forw_packet->if_incoming == if_incoming) &&
332
333 /* packets from direct neighbors or
334 * own secondary interface packets
335 * (= secondary interface packets in general) */
336 (batman_ogm_packet->flags & DIRECTLINK ||
337 (forw_packet->own &&
338 forw_packet->if_incoming != primary_if))) {
339 res = true;
340 goto out;
341 }
342 }
343
344out:
345 if (primary_if)
346 hardif_free_ref(primary_if);
347 return res;
348}
349
350/* create a new aggregated packet and add this packet to it */
Marek Lindner01c42242011-11-28 21:31:55 +0800351static void bat_iv_ogm_aggregate_new(const unsigned char *packet_buff,
352 int packet_len, unsigned long send_time,
353 bool direct_link,
354 struct hard_iface *if_incoming,
355 int own_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200356{
357 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
358 struct forw_packet *forw_packet_aggr;
359 unsigned char *skb_buff;
360
361 if (!atomic_inc_not_zero(&if_incoming->refcount))
362 return;
363
364 /* own packet should always be scheduled */
365 if (!own_packet) {
366 if (!atomic_dec_not_zero(&bat_priv->batman_queue_left)) {
367 bat_dbg(DBG_BATMAN, bat_priv,
368 "batman packet queue full\n");
369 goto out;
370 }
371 }
372
373 forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC);
374 if (!forw_packet_aggr) {
375 if (!own_packet)
376 atomic_inc(&bat_priv->batman_queue_left);
377 goto out;
378 }
379
380 if ((atomic_read(&bat_priv->aggregated_ogms)) &&
381 (packet_len < MAX_AGGREGATION_BYTES))
382 forw_packet_aggr->skb = dev_alloc_skb(MAX_AGGREGATION_BYTES +
Antonio Quartulli0d125072012-02-18 11:27:34 +0100383 ETH_HLEN);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200384 else
Antonio Quartulli0d125072012-02-18 11:27:34 +0100385 forw_packet_aggr->skb = dev_alloc_skb(packet_len + ETH_HLEN);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200386
387 if (!forw_packet_aggr->skb) {
388 if (!own_packet)
389 atomic_inc(&bat_priv->batman_queue_left);
390 kfree(forw_packet_aggr);
391 goto out;
392 }
Antonio Quartulli0d125072012-02-18 11:27:34 +0100393 skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200394
395 INIT_HLIST_NODE(&forw_packet_aggr->list);
396
397 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
398 forw_packet_aggr->packet_len = packet_len;
399 memcpy(skb_buff, packet_buff, packet_len);
400
401 forw_packet_aggr->own = own_packet;
402 forw_packet_aggr->if_incoming = if_incoming;
403 forw_packet_aggr->num_packets = 0;
404 forw_packet_aggr->direct_link_flags = NO_FLAGS;
405 forw_packet_aggr->send_time = send_time;
406
407 /* save packet direct link flag status */
408 if (direct_link)
409 forw_packet_aggr->direct_link_flags |= 1;
410
411 /* add new packet to packet list */
412 spin_lock_bh(&bat_priv->forw_bat_list_lock);
413 hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list);
414 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
415
416 /* start timer for this packet */
417 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
418 send_outstanding_bat_ogm_packet);
419 queue_delayed_work(bat_event_workqueue,
420 &forw_packet_aggr->delayed_work,
421 send_time - jiffies);
422
423 return;
424out:
425 hardif_free_ref(if_incoming);
426}
427
428/* aggregate a new packet into the existing ogm packet */
Marek Lindner01c42242011-11-28 21:31:55 +0800429static void bat_iv_ogm_aggregate(struct forw_packet *forw_packet_aggr,
430 const unsigned char *packet_buff,
431 int packet_len, bool direct_link)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200432{
433 unsigned char *skb_buff;
434
435 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
436 memcpy(skb_buff, packet_buff, packet_len);
437 forw_packet_aggr->packet_len += packet_len;
438 forw_packet_aggr->num_packets++;
439
440 /* save packet direct link flag status */
441 if (direct_link)
442 forw_packet_aggr->direct_link_flags |=
443 (1 << forw_packet_aggr->num_packets);
444}
445
Marek Lindner01c42242011-11-28 21:31:55 +0800446static void bat_iv_ogm_queue_add(struct bat_priv *bat_priv,
447 unsigned char *packet_buff,
448 int packet_len, struct hard_iface *if_incoming,
449 int own_packet, unsigned long send_time)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200450{
451 /**
452 * _aggr -> pointer to the packet we want to aggregate with
453 * _pos -> pointer to the position in the queue
454 */
455 struct forw_packet *forw_packet_aggr = NULL, *forw_packet_pos = NULL;
456 struct hlist_node *tmp_node;
457 struct batman_ogm_packet *batman_ogm_packet;
458 bool direct_link;
459
460 batman_ogm_packet = (struct batman_ogm_packet *)packet_buff;
461 direct_link = batman_ogm_packet->flags & DIRECTLINK ? 1 : 0;
462
463 /* find position for the packet in the forward queue */
464 spin_lock_bh(&bat_priv->forw_bat_list_lock);
465 /* own packets are not to be aggregated */
466 if ((atomic_read(&bat_priv->aggregated_ogms)) && (!own_packet)) {
467 hlist_for_each_entry(forw_packet_pos, tmp_node,
468 &bat_priv->forw_bat_list, list) {
Marek Lindner01c42242011-11-28 21:31:55 +0800469 if (bat_iv_ogm_can_aggregate(batman_ogm_packet,
470 bat_priv, packet_len,
471 send_time, direct_link,
472 if_incoming,
473 forw_packet_pos)) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200474 forw_packet_aggr = forw_packet_pos;
475 break;
476 }
477 }
478 }
479
480 /* nothing to aggregate with - either aggregation disabled or no
481 * suitable aggregation packet found */
482 if (!forw_packet_aggr) {
483 /* the following section can run without the lock */
484 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
485
486 /**
487 * if we could not aggregate this packet with one of the others
488 * we hold it back for a while, so that it might be aggregated
489 * later on
490 */
491 if ((!own_packet) &&
492 (atomic_read(&bat_priv->aggregated_ogms)))
493 send_time += msecs_to_jiffies(MAX_AGGREGATION_MS);
494
Marek Lindner01c42242011-11-28 21:31:55 +0800495 bat_iv_ogm_aggregate_new(packet_buff, packet_len,
496 send_time, direct_link,
497 if_incoming, own_packet);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200498 } else {
Marek Lindner01c42242011-11-28 21:31:55 +0800499 bat_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
500 packet_len, direct_link);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200501 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
502 }
503}
504
Marek Lindner01c42242011-11-28 21:31:55 +0800505static void bat_iv_ogm_forward(struct orig_node *orig_node,
506 const struct ethhdr *ethhdr,
507 struct batman_ogm_packet *batman_ogm_packet,
Marek Lindner75cd33f2012-03-01 15:35:16 +0800508 bool is_single_hop_neigh,
Marek Lindner13b25412012-03-11 06:17:53 +0800509 bool is_from_best_next_hop,
Marek Lindner75cd33f2012-03-01 15:35:16 +0800510 struct hard_iface *if_incoming)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200511{
512 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200513 uint8_t tt_num_changes;
514
Sven Eckelmann76543d12011-11-20 15:47:38 +0100515 if (batman_ogm_packet->header.ttl <= 1) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200516 bat_dbg(DBG_BATMAN, bat_priv, "ttl exceeded\n");
517 return;
518 }
519
Marek Lindner13b25412012-03-11 06:17:53 +0800520 if (!is_from_best_next_hop) {
521 /* Mark the forwarded packet when it is not coming from our
522 * best next hop. We still need to forward the packet for our
523 * neighbor link quality detection to work in case the packet
524 * originated from a single hop neighbor. Otherwise we can
525 * simply drop the ogm.
526 */
527 if (is_single_hop_neigh)
528 batman_ogm_packet->flags |= NOT_BEST_NEXT_HOP;
529 else
530 return;
531 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200532
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200533 tt_num_changes = batman_ogm_packet->tt_num_changes;
534
Sven Eckelmann76543d12011-11-20 15:47:38 +0100535 batman_ogm_packet->header.ttl--;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200536 memcpy(batman_ogm_packet->prev_sender, ethhdr->h_source, ETH_ALEN);
537
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200538 /* apply hop penalty */
539 batman_ogm_packet->tq = hop_penalty(batman_ogm_packet->tq, bat_priv);
540
541 bat_dbg(DBG_BATMAN, bat_priv,
Marek Lindner13b25412012-03-11 06:17:53 +0800542 "Forwarding packet: tq: %i, ttl: %i\n",
543 batman_ogm_packet->tq, batman_ogm_packet->header.ttl);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200544
545 batman_ogm_packet->seqno = htonl(batman_ogm_packet->seqno);
546 batman_ogm_packet->tt_crc = htons(batman_ogm_packet->tt_crc);
547
548 /* switch of primaries first hop flag when forwarding */
549 batman_ogm_packet->flags &= ~PRIMARIES_FIRST_HOP;
Marek Lindner75cd33f2012-03-01 15:35:16 +0800550 if (is_single_hop_neigh)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200551 batman_ogm_packet->flags |= DIRECTLINK;
552 else
553 batman_ogm_packet->flags &= ~DIRECTLINK;
554
Marek Lindner01c42242011-11-28 21:31:55 +0800555 bat_iv_ogm_queue_add(bat_priv, (unsigned char *)batman_ogm_packet,
Marek Lindner76e3d7fc2012-02-07 17:20:50 +0800556 BATMAN_OGM_HLEN + tt_len(tt_num_changes),
Marek Lindner01c42242011-11-28 21:31:55 +0800557 if_incoming, 0, bat_iv_ogm_fwd_send_time());
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200558}
559
Marek Lindner01c42242011-11-28 21:31:55 +0800560static void bat_iv_ogm_schedule(struct hard_iface *hard_iface,
561 int tt_num_changes)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200562{
563 struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
564 struct batman_ogm_packet *batman_ogm_packet;
565 struct hard_iface *primary_if;
566 int vis_server;
567
568 vis_server = atomic_read(&bat_priv->vis_mode);
569 primary_if = primary_if_get_selected(bat_priv);
570
571 batman_ogm_packet = (struct batman_ogm_packet *)hard_iface->packet_buff;
572
573 /* change sequence number to network order */
574 batman_ogm_packet->seqno =
575 htonl((uint32_t)atomic_read(&hard_iface->seqno));
576
577 batman_ogm_packet->ttvn = atomic_read(&bat_priv->ttvn);
Antonio Quartulli66a1b2b2012-04-14 13:15:26 +0200578 batman_ogm_packet->tt_crc = htons(bat_priv->tt_crc);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200579 if (tt_num_changes >= 0)
580 batman_ogm_packet->tt_num_changes = tt_num_changes;
581
582 if (vis_server == VIS_TYPE_SERVER_SYNC)
583 batman_ogm_packet->flags |= VIS_SERVER;
584 else
585 batman_ogm_packet->flags &= ~VIS_SERVER;
586
587 if ((hard_iface == primary_if) &&
588 (atomic_read(&bat_priv->gw_mode) == GW_MODE_SERVER))
589 batman_ogm_packet->gw_flags =
590 (uint8_t)atomic_read(&bat_priv->gw_bandwidth);
591 else
592 batman_ogm_packet->gw_flags = NO_FLAGS;
593
594 atomic_inc(&hard_iface->seqno);
595
596 slide_own_bcast_window(hard_iface);
Marek Lindner01c42242011-11-28 21:31:55 +0800597 bat_iv_ogm_queue_add(bat_priv, hard_iface->packet_buff,
598 hard_iface->packet_len, hard_iface, 1,
599 bat_iv_ogm_emit_send_time(bat_priv));
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200600
601 if (primary_if)
602 hardif_free_ref(primary_if);
603}
604
Marek Lindner01c42242011-11-28 21:31:55 +0800605static void bat_iv_ogm_orig_update(struct bat_priv *bat_priv,
606 struct orig_node *orig_node,
607 const struct ethhdr *ethhdr,
608 const struct batman_ogm_packet
Marek Lindnerfc957272011-07-30 12:04:12 +0200609 *batman_ogm_packet,
Marek Lindner01c42242011-11-28 21:31:55 +0800610 struct hard_iface *if_incoming,
611 const unsigned char *tt_buff,
612 int is_duplicate)
Marek Lindnerfc957272011-07-30 12:04:12 +0200613{
614 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
615 struct neigh_node *router = NULL;
616 struct orig_node *orig_node_tmp;
617 struct hlist_node *node;
618 uint8_t bcast_own_sum_orig, bcast_own_sum_neigh;
619
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100620 bat_dbg(DBG_BATMAN, bat_priv,
621 "update_originator(): Searching and updating originator entry of received packet\n");
Marek Lindnerfc957272011-07-30 12:04:12 +0200622
623 rcu_read_lock();
624 hlist_for_each_entry_rcu(tmp_neigh_node, node,
625 &orig_node->neigh_list, list) {
626 if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
627 (tmp_neigh_node->if_incoming == if_incoming) &&
628 atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
629 if (neigh_node)
630 neigh_node_free_ref(neigh_node);
631 neigh_node = tmp_neigh_node;
632 continue;
633 }
634
635 if (is_duplicate)
636 continue;
637
Marek Lindnere3b0d0d2012-03-17 15:28:32 +0800638 spin_lock_bh(&tmp_neigh_node->lq_update_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +0200639 ring_buffer_set(tmp_neigh_node->tq_recv,
640 &tmp_neigh_node->tq_index, 0);
641 tmp_neigh_node->tq_avg =
642 ring_buffer_avg(tmp_neigh_node->tq_recv);
Marek Lindnere3b0d0d2012-03-17 15:28:32 +0800643 spin_unlock_bh(&tmp_neigh_node->lq_update_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +0200644 }
645
646 if (!neigh_node) {
647 struct orig_node *orig_tmp;
648
649 orig_tmp = get_orig_node(bat_priv, ethhdr->h_source);
650 if (!orig_tmp)
651 goto unlock;
652
Marek Lindner7ae8b282012-03-01 15:35:21 +0800653 neigh_node = bat_iv_ogm_neigh_new(if_incoming, ethhdr->h_source,
654 orig_node, orig_tmp,
655 batman_ogm_packet->seqno);
Marek Lindnerfc957272011-07-30 12:04:12 +0200656
657 orig_node_free_ref(orig_tmp);
658 if (!neigh_node)
659 goto unlock;
660 } else
661 bat_dbg(DBG_BATMAN, bat_priv,
662 "Updating existing last-hop neighbor of originator\n");
663
664 rcu_read_unlock();
665
666 orig_node->flags = batman_ogm_packet->flags;
Marek Lindnerd7b2a972012-03-01 15:35:19 +0800667 neigh_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +0200668
Marek Lindnere3b0d0d2012-03-17 15:28:32 +0800669 spin_lock_bh(&neigh_node->lq_update_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +0200670 ring_buffer_set(neigh_node->tq_recv,
671 &neigh_node->tq_index,
672 batman_ogm_packet->tq);
673 neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
Marek Lindnere3b0d0d2012-03-17 15:28:32 +0800674 spin_unlock_bh(&neigh_node->lq_update_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +0200675
676 if (!is_duplicate) {
Sven Eckelmann76543d12011-11-20 15:47:38 +0100677 orig_node->last_ttl = batman_ogm_packet->header.ttl;
678 neigh_node->last_ttl = batman_ogm_packet->header.ttl;
Marek Lindnerfc957272011-07-30 12:04:12 +0200679 }
680
681 bonding_candidate_add(orig_node, neigh_node);
682
683 /* if this neighbor already is our next hop there is nothing
684 * to change */
685 router = orig_node_get_router(orig_node);
686 if (router == neigh_node)
687 goto update_tt;
688
689 /* if this neighbor does not offer a better TQ we won't consider it */
690 if (router && (router->tq_avg > neigh_node->tq_avg))
691 goto update_tt;
692
693 /* if the TQ is the same and the link not more symmetric we
694 * won't consider it either */
695 if (router && (neigh_node->tq_avg == router->tq_avg)) {
696 orig_node_tmp = router->orig_node;
697 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
698 bcast_own_sum_orig =
699 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
700 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
701
702 orig_node_tmp = neigh_node->orig_node;
703 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
704 bcast_own_sum_neigh =
705 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
706 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
707
708 if (bcast_own_sum_orig >= bcast_own_sum_neigh)
709 goto update_tt;
710 }
711
712 update_route(bat_priv, orig_node, neigh_node);
713
714update_tt:
715 /* I have to check for transtable changes only if the OGM has been
716 * sent through a primary interface */
717 if (((batman_ogm_packet->orig != ethhdr->h_source) &&
Sven Eckelmann76543d12011-11-20 15:47:38 +0100718 (batman_ogm_packet->header.ttl > 2)) ||
Marek Lindnerfc957272011-07-30 12:04:12 +0200719 (batman_ogm_packet->flags & PRIMARIES_FIRST_HOP))
720 tt_update_orig(bat_priv, orig_node, tt_buff,
721 batman_ogm_packet->tt_num_changes,
722 batman_ogm_packet->ttvn,
723 batman_ogm_packet->tt_crc);
724
725 if (orig_node->gw_flags != batman_ogm_packet->gw_flags)
726 gw_node_update(bat_priv, orig_node,
727 batman_ogm_packet->gw_flags);
728
729 orig_node->gw_flags = batman_ogm_packet->gw_flags;
730
731 /* restart gateway selection if fast or late switching was enabled */
732 if ((orig_node->gw_flags) &&
733 (atomic_read(&bat_priv->gw_mode) == GW_MODE_CLIENT) &&
734 (atomic_read(&bat_priv->gw_sel_class) > 2))
735 gw_check_election(bat_priv, orig_node);
736
737 goto out;
738
739unlock:
740 rcu_read_unlock();
741out:
742 if (neigh_node)
743 neigh_node_free_ref(neigh_node);
744 if (router)
745 neigh_node_free_ref(router);
746}
747
Marek Lindner01c42242011-11-28 21:31:55 +0800748static int bat_iv_ogm_calc_tq(struct orig_node *orig_node,
749 struct orig_node *orig_neigh_node,
750 struct batman_ogm_packet *batman_ogm_packet,
751 struct hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +0200752{
753 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
754 struct neigh_node *neigh_node = NULL, *tmp_neigh_node;
755 struct hlist_node *node;
756 uint8_t total_count;
757 uint8_t orig_eq_count, neigh_rq_count, tq_own;
758 int tq_asym_penalty, ret = 0;
759
760 /* find corresponding one hop neighbor */
761 rcu_read_lock();
762 hlist_for_each_entry_rcu(tmp_neigh_node, node,
763 &orig_neigh_node->neigh_list, list) {
764
765 if (!compare_eth(tmp_neigh_node->addr, orig_neigh_node->orig))
766 continue;
767
768 if (tmp_neigh_node->if_incoming != if_incoming)
769 continue;
770
771 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
772 continue;
773
774 neigh_node = tmp_neigh_node;
775 break;
776 }
777 rcu_read_unlock();
778
779 if (!neigh_node)
Marek Lindner7ae8b282012-03-01 15:35:21 +0800780 neigh_node = bat_iv_ogm_neigh_new(if_incoming,
781 orig_neigh_node->orig,
782 orig_neigh_node,
783 orig_neigh_node,
784 batman_ogm_packet->seqno);
Marek Lindnerfc957272011-07-30 12:04:12 +0200785
786 if (!neigh_node)
787 goto out;
788
Marek Lindnerd7b2a972012-03-01 15:35:19 +0800789 /* if orig_node is direct neighbor update neigh_node last_seen */
Marek Lindnerfc957272011-07-30 12:04:12 +0200790 if (orig_node == orig_neigh_node)
Marek Lindnerd7b2a972012-03-01 15:35:19 +0800791 neigh_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +0200792
Marek Lindnerd7b2a972012-03-01 15:35:19 +0800793 orig_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +0200794
795 /* find packet count of corresponding one hop neighbor */
796 spin_lock_bh(&orig_node->ogm_cnt_lock);
797 orig_eq_count = orig_neigh_node->bcast_own_sum[if_incoming->if_num];
798 neigh_rq_count = neigh_node->real_packet_count;
799 spin_unlock_bh(&orig_node->ogm_cnt_lock);
800
801 /* pay attention to not get a value bigger than 100 % */
802 total_count = (orig_eq_count > neigh_rq_count ?
803 neigh_rq_count : orig_eq_count);
804
805 /* if we have too few packets (too less data) we set tq_own to zero */
806 /* if we receive too few packets it is not considered bidirectional */
807 if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
808 (neigh_rq_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
809 tq_own = 0;
810 else
811 /* neigh_node->real_packet_count is never zero as we
812 * only purge old information when getting new
813 * information */
814 tq_own = (TQ_MAX_VALUE * total_count) / neigh_rq_count;
815
Sven Eckelmann21a12362012-03-07 09:07:46 +0100816 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
Marek Lindnerfc957272011-07-30 12:04:12 +0200817 * affect the nearly-symmetric links only a little, but
818 * punishes asymmetric links more. This will give a value
819 * between 0 and TQ_MAX_VALUE
820 */
821 tq_asym_penalty = TQ_MAX_VALUE - (TQ_MAX_VALUE *
822 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
823 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
824 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count)) /
825 (TQ_LOCAL_WINDOW_SIZE *
826 TQ_LOCAL_WINDOW_SIZE *
827 TQ_LOCAL_WINDOW_SIZE);
828
829 batman_ogm_packet->tq = ((batman_ogm_packet->tq * tq_own
830 * tq_asym_penalty) /
831 (TQ_MAX_VALUE * TQ_MAX_VALUE));
832
833 bat_dbg(DBG_BATMAN, bat_priv,
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100834 "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 +0200835 orig_node->orig, orig_neigh_node->orig, total_count,
836 neigh_rq_count, tq_own, tq_asym_penalty, batman_ogm_packet->tq);
837
838 /* if link has the minimum required transmission quality
839 * consider it bidirectional */
840 if (batman_ogm_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
841 ret = 1;
842
843out:
844 if (neigh_node)
845 neigh_node_free_ref(neigh_node);
846 return ret;
847}
848
849/* processes a batman packet for all interfaces, adjusts the sequence number and
850 * finds out whether it is a duplicate.
851 * returns:
852 * 1 the packet is a duplicate
853 * 0 the packet has not yet been received
854 * -1 the packet is old and has been received while the seqno window
855 * was protected. Caller should drop it.
856 */
Marek Lindner01c42242011-11-28 21:31:55 +0800857static int bat_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
858 const struct batman_ogm_packet
Marek Lindnerfc957272011-07-30 12:04:12 +0200859 *batman_ogm_packet,
Marek Lindner01c42242011-11-28 21:31:55 +0800860 const struct hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +0200861{
862 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
863 struct orig_node *orig_node;
864 struct neigh_node *tmp_neigh_node;
865 struct hlist_node *node;
866 int is_duplicate = 0;
867 int32_t seq_diff;
868 int need_update = 0;
869 int set_mark, ret = -1;
870
871 orig_node = get_orig_node(bat_priv, batman_ogm_packet->orig);
872 if (!orig_node)
873 return 0;
874
875 spin_lock_bh(&orig_node->ogm_cnt_lock);
876 seq_diff = batman_ogm_packet->seqno - orig_node->last_real_seqno;
877
878 /* signalize caller that the packet is to be dropped. */
Antonio Quartulli1e5cc262012-02-26 15:39:42 +0100879 if (!hlist_empty(&orig_node->neigh_list) &&
880 window_protected(bat_priv, seq_diff,
Marek Lindnerfc957272011-07-30 12:04:12 +0200881 &orig_node->batman_seqno_reset))
882 goto out;
883
884 rcu_read_lock();
885 hlist_for_each_entry_rcu(tmp_neigh_node, node,
886 &orig_node->neigh_list, list) {
887
Sven Eckelmann0079d2c2012-02-04 17:34:52 +0100888 is_duplicate |= bat_test_bit(tmp_neigh_node->real_bits,
889 orig_node->last_real_seqno,
890 batman_ogm_packet->seqno);
Marek Lindnerfc957272011-07-30 12:04:12 +0200891
892 if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
893 (tmp_neigh_node->if_incoming == if_incoming))
894 set_mark = 1;
895 else
896 set_mark = 0;
897
898 /* if the window moved, set the update flag. */
899 need_update |= bit_get_packet(bat_priv,
900 tmp_neigh_node->real_bits,
901 seq_diff, set_mark);
902
903 tmp_neigh_node->real_packet_count =
Sven Eckelmann0079d2c2012-02-04 17:34:52 +0100904 bitmap_weight(tmp_neigh_node->real_bits,
905 TQ_LOCAL_WINDOW_SIZE);
Marek Lindnerfc957272011-07-30 12:04:12 +0200906 }
907 rcu_read_unlock();
908
909 if (need_update) {
910 bat_dbg(DBG_BATMAN, bat_priv,
Antonio Quartullic97c72b2012-02-26 15:39:41 +0100911 "updating last_seqno: old %u, new %u\n",
Marek Lindnerfc957272011-07-30 12:04:12 +0200912 orig_node->last_real_seqno, batman_ogm_packet->seqno);
913 orig_node->last_real_seqno = batman_ogm_packet->seqno;
914 }
915
916 ret = is_duplicate;
917
918out:
919 spin_unlock_bh(&orig_node->ogm_cnt_lock);
920 orig_node_free_ref(orig_node);
921 return ret;
922}
923
Marek Lindner01c42242011-11-28 21:31:55 +0800924static void bat_iv_ogm_process(const struct ethhdr *ethhdr,
925 struct batman_ogm_packet *batman_ogm_packet,
926 const unsigned char *tt_buff,
927 struct hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +0200928{
929 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
930 struct hard_iface *hard_iface;
931 struct orig_node *orig_neigh_node, *orig_node;
932 struct neigh_node *router = NULL, *router_router = NULL;
933 struct neigh_node *orig_neigh_router = NULL;
934 int has_directlink_flag;
935 int is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
Marek Lindner75cd33f2012-03-01 15:35:16 +0800936 int is_broadcast = 0, is_bidirectional;
937 bool is_single_hop_neigh = false;
Marek Lindner13b25412012-03-11 06:17:53 +0800938 bool is_from_best_next_hop = false;
Marek Lindnerfc957272011-07-30 12:04:12 +0200939 int is_duplicate;
940 uint32_t if_incoming_seqno;
941
942 /* Silently drop when the batman packet is actually not a
943 * correct packet.
944 *
945 * This might happen if a packet is padded (e.g. Ethernet has a
946 * minimum frame length of 64 byte) and the aggregation interprets
947 * it as an additional length.
948 *
949 * TODO: A more sane solution would be to have a bit in the
950 * batman_ogm_packet to detect whether the packet is the last
951 * packet in an aggregation. Here we expect that the padding
952 * is always zero (or not 0x01)
953 */
Marek Lindner1eeb4792012-02-07 17:20:51 +0800954 if (batman_ogm_packet->header.packet_type != BAT_IV_OGM)
Marek Lindnerfc957272011-07-30 12:04:12 +0200955 return;
956
957 /* could be changed by schedule_own_packet() */
958 if_incoming_seqno = atomic_read(&if_incoming->seqno);
959
960 has_directlink_flag = (batman_ogm_packet->flags & DIRECTLINK ? 1 : 0);
961
Marek Lindner75cd33f2012-03-01 15:35:16 +0800962 if (compare_eth(ethhdr->h_source, batman_ogm_packet->orig))
963 is_single_hop_neigh = true;
Marek Lindnerfc957272011-07-30 12:04:12 +0200964
965 bat_dbg(DBG_BATMAN, bat_priv,
Antonio Quartullic97c72b2012-02-26 15:39:41 +0100966 "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 +0200967 ethhdr->h_source, if_incoming->net_dev->name,
968 if_incoming->net_dev->dev_addr, batman_ogm_packet->orig,
969 batman_ogm_packet->prev_sender, batman_ogm_packet->seqno,
970 batman_ogm_packet->ttvn, batman_ogm_packet->tt_crc,
971 batman_ogm_packet->tt_num_changes, batman_ogm_packet->tq,
Sven Eckelmann76543d12011-11-20 15:47:38 +0100972 batman_ogm_packet->header.ttl,
973 batman_ogm_packet->header.version, has_directlink_flag);
Marek Lindnerfc957272011-07-30 12:04:12 +0200974
975 rcu_read_lock();
976 list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
977 if (hard_iface->if_status != IF_ACTIVE)
978 continue;
979
980 if (hard_iface->soft_iface != if_incoming->soft_iface)
981 continue;
982
983 if (compare_eth(ethhdr->h_source,
984 hard_iface->net_dev->dev_addr))
985 is_my_addr = 1;
986
987 if (compare_eth(batman_ogm_packet->orig,
988 hard_iface->net_dev->dev_addr))
989 is_my_orig = 1;
990
991 if (compare_eth(batman_ogm_packet->prev_sender,
992 hard_iface->net_dev->dev_addr))
993 is_my_oldorig = 1;
994
995 if (is_broadcast_ether_addr(ethhdr->h_source))
996 is_broadcast = 1;
997 }
998 rcu_read_unlock();
999
Sven Eckelmann76543d12011-11-20 15:47:38 +01001000 if (batman_ogm_packet->header.version != COMPAT_VERSION) {
Marek Lindnerfc957272011-07-30 12:04:12 +02001001 bat_dbg(DBG_BATMAN, bat_priv,
1002 "Drop packet: incompatible batman version (%i)\n",
Sven Eckelmann76543d12011-11-20 15:47:38 +01001003 batman_ogm_packet->header.version);
Marek Lindnerfc957272011-07-30 12:04:12 +02001004 return;
1005 }
1006
1007 if (is_my_addr) {
1008 bat_dbg(DBG_BATMAN, bat_priv,
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001009 "Drop packet: received my own broadcast (sender: %pM)\n",
Marek Lindnerfc957272011-07-30 12:04:12 +02001010 ethhdr->h_source);
1011 return;
1012 }
1013
1014 if (is_broadcast) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001015 bat_dbg(DBG_BATMAN, bat_priv,
1016 "Drop packet: ignoring all packets with broadcast source addr (sender: %pM)\n",
1017 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001018 return;
1019 }
1020
1021 if (is_my_orig) {
1022 unsigned long *word;
1023 int offset;
1024
1025 orig_neigh_node = get_orig_node(bat_priv, ethhdr->h_source);
1026 if (!orig_neigh_node)
1027 return;
1028
1029 /* neighbor has to indicate direct link and it has to
1030 * come via the corresponding interface */
1031 /* save packet seqno for bidirectional check */
1032 if (has_directlink_flag &&
1033 compare_eth(if_incoming->net_dev->dev_addr,
1034 batman_ogm_packet->orig)) {
1035 offset = if_incoming->if_num * NUM_WORDS;
1036
1037 spin_lock_bh(&orig_neigh_node->ogm_cnt_lock);
1038 word = &(orig_neigh_node->bcast_own[offset]);
Sven Eckelmann0079d2c2012-02-04 17:34:52 +01001039 bat_set_bit(word,
1040 if_incoming_seqno -
Marek Lindnerfc957272011-07-30 12:04:12 +02001041 batman_ogm_packet->seqno - 2);
1042 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
Sven Eckelmann0079d2c2012-02-04 17:34:52 +01001043 bitmap_weight(word, TQ_LOCAL_WINDOW_SIZE);
Marek Lindnerfc957272011-07-30 12:04:12 +02001044 spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock);
1045 }
1046
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001047 bat_dbg(DBG_BATMAN, bat_priv,
1048 "Drop packet: originator packet from myself (via neighbor)\n");
Marek Lindnerfc957272011-07-30 12:04:12 +02001049 orig_node_free_ref(orig_neigh_node);
1050 return;
1051 }
1052
1053 if (is_my_oldorig) {
1054 bat_dbg(DBG_BATMAN, bat_priv,
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001055 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1056 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001057 return;
1058 }
1059
Marek Lindner13b25412012-03-11 06:17:53 +08001060 if (batman_ogm_packet->flags & NOT_BEST_NEXT_HOP) {
1061 bat_dbg(DBG_BATMAN, bat_priv,
Marek Lindnerfefa5322012-03-17 15:28:34 +08001062 "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1063 ethhdr->h_source);
Marek Lindner13b25412012-03-11 06:17:53 +08001064 return;
1065 }
1066
Marek Lindnerfc957272011-07-30 12:04:12 +02001067 orig_node = get_orig_node(bat_priv, batman_ogm_packet->orig);
1068 if (!orig_node)
1069 return;
1070
Marek Lindner01c42242011-11-28 21:31:55 +08001071 is_duplicate = bat_iv_ogm_update_seqnos(ethhdr, batman_ogm_packet,
1072 if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001073
1074 if (is_duplicate == -1) {
1075 bat_dbg(DBG_BATMAN, bat_priv,
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001076 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1077 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001078 goto out;
1079 }
1080
1081 if (batman_ogm_packet->tq == 0) {
1082 bat_dbg(DBG_BATMAN, bat_priv,
1083 "Drop packet: originator packet with tq equal 0\n");
1084 goto out;
1085 }
1086
1087 router = orig_node_get_router(orig_node);
1088 if (router)
1089 router_router = orig_node_get_router(router->orig_node);
1090
Marek Lindner13b25412012-03-11 06:17:53 +08001091 if ((router && router->tq_avg != 0) &&
1092 (compare_eth(router->addr, ethhdr->h_source)))
1093 is_from_best_next_hop = true;
1094
Marek Lindnerfc957272011-07-30 12:04:12 +02001095 /* avoid temporary routing loops */
1096 if (router && router_router &&
1097 (compare_eth(router->addr, batman_ogm_packet->prev_sender)) &&
1098 !(compare_eth(batman_ogm_packet->orig,
1099 batman_ogm_packet->prev_sender)) &&
1100 (compare_eth(router->addr, router_router->addr))) {
1101 bat_dbg(DBG_BATMAN, bat_priv,
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001102 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1103 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001104 goto out;
1105 }
1106
1107 /* if sender is a direct neighbor the sender mac equals
1108 * originator mac */
1109 orig_neigh_node = (is_single_hop_neigh ?
1110 orig_node :
1111 get_orig_node(bat_priv, ethhdr->h_source));
1112 if (!orig_neigh_node)
1113 goto out;
1114
1115 orig_neigh_router = orig_node_get_router(orig_neigh_node);
1116
1117 /* drop packet if sender is not a direct neighbor and if we
1118 * don't route towards it */
1119 if (!is_single_hop_neigh && (!orig_neigh_router)) {
1120 bat_dbg(DBG_BATMAN, bat_priv,
1121 "Drop packet: OGM via unknown neighbor!\n");
1122 goto out_neigh;
1123 }
1124
Marek Lindner01c42242011-11-28 21:31:55 +08001125 is_bidirectional = bat_iv_ogm_calc_tq(orig_node, orig_neigh_node,
1126 batman_ogm_packet, if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001127
1128 bonding_save_primary(orig_node, orig_neigh_node, batman_ogm_packet);
1129
1130 /* update ranking if it is not a duplicate or has the same
1131 * seqno and similar ttl as the non-duplicate */
1132 if (is_bidirectional &&
1133 (!is_duplicate ||
1134 ((orig_node->last_real_seqno == batman_ogm_packet->seqno) &&
Sven Eckelmann76543d12011-11-20 15:47:38 +01001135 (orig_node->last_ttl - 3 <= batman_ogm_packet->header.ttl))))
Marek Lindner01c42242011-11-28 21:31:55 +08001136 bat_iv_ogm_orig_update(bat_priv, orig_node, ethhdr,
1137 batman_ogm_packet, if_incoming,
1138 tt_buff, is_duplicate);
Marek Lindnerfc957272011-07-30 12:04:12 +02001139
1140 /* is single hop (direct) neighbor */
1141 if (is_single_hop_neigh) {
1142
1143 /* mark direct link on incoming interface */
Marek Lindner01c42242011-11-28 21:31:55 +08001144 bat_iv_ogm_forward(orig_node, ethhdr, batman_ogm_packet,
Marek Lindner13b25412012-03-11 06:17:53 +08001145 is_single_hop_neigh, is_from_best_next_hop,
1146 if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001147
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001148 bat_dbg(DBG_BATMAN, bat_priv,
1149 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
Marek Lindnerfc957272011-07-30 12:04:12 +02001150 goto out_neigh;
1151 }
1152
1153 /* multihop originator */
1154 if (!is_bidirectional) {
1155 bat_dbg(DBG_BATMAN, bat_priv,
1156 "Drop packet: not received via bidirectional link\n");
1157 goto out_neigh;
1158 }
1159
1160 if (is_duplicate) {
1161 bat_dbg(DBG_BATMAN, bat_priv,
1162 "Drop packet: duplicate packet received\n");
1163 goto out_neigh;
1164 }
1165
1166 bat_dbg(DBG_BATMAN, bat_priv,
1167 "Forwarding packet: rebroadcast originator packet\n");
Marek Lindner01c42242011-11-28 21:31:55 +08001168 bat_iv_ogm_forward(orig_node, ethhdr, batman_ogm_packet,
Marek Lindner13b25412012-03-11 06:17:53 +08001169 is_single_hop_neigh, is_from_best_next_hop,
1170 if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001171
1172out_neigh:
1173 if ((orig_neigh_node) && (!is_single_hop_neigh))
1174 orig_node_free_ref(orig_neigh_node);
1175out:
1176 if (router)
1177 neigh_node_free_ref(router);
1178 if (router_router)
1179 neigh_node_free_ref(router_router);
1180 if (orig_neigh_router)
1181 neigh_node_free_ref(orig_neigh_router);
1182
1183 orig_node_free_ref(orig_node);
1184}
1185
Marek Lindnerc3e29312012-03-04 16:56:25 +08001186static int bat_iv_ogm_receive(struct sk_buff *skb,
1187 struct hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +02001188{
Marek Lindneredbf12b2012-03-11 06:17:49 +08001189 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Marek Lindnerfc957272011-07-30 12:04:12 +02001190 struct batman_ogm_packet *batman_ogm_packet;
Marek Lindner8780dad2011-12-05 04:01:51 +08001191 struct ethhdr *ethhdr;
1192 int buff_pos = 0, packet_len;
1193 unsigned char *tt_buff, *packet_buff;
Marek Lindnerc3e29312012-03-04 16:56:25 +08001194 bool ret;
1195
1196 ret = check_management_packet(skb, if_incoming, BATMAN_OGM_HLEN);
1197 if (!ret)
1198 return NET_RX_DROP;
Marek Lindnerfc957272011-07-30 12:04:12 +02001199
Marek Lindneredbf12b2012-03-11 06:17:49 +08001200 /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
1201 * that does not have B.A.T.M.A.N. IV enabled ?
1202 */
1203 if (bat_priv->bat_algo_ops->bat_ogm_emit != bat_iv_ogm_emit)
1204 return NET_RX_DROP;
1205
Marek Lindner8780dad2011-12-05 04:01:51 +08001206 packet_len = skb_headlen(skb);
1207 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1208 packet_buff = skb->data;
Marek Lindnerfc957272011-07-30 12:04:12 +02001209 batman_ogm_packet = (struct batman_ogm_packet *)packet_buff;
1210
1211 /* unpack the aggregated packets and process them one by one */
1212 do {
1213 /* network to host order for our 32bit seqno and the
1214 orig_interval */
1215 batman_ogm_packet->seqno = ntohl(batman_ogm_packet->seqno);
1216 batman_ogm_packet->tt_crc = ntohs(batman_ogm_packet->tt_crc);
1217
Marek Lindner76e3d7fc2012-02-07 17:20:50 +08001218 tt_buff = packet_buff + buff_pos + BATMAN_OGM_HLEN;
Marek Lindnerfc957272011-07-30 12:04:12 +02001219
Marek Lindner01c42242011-11-28 21:31:55 +08001220 bat_iv_ogm_process(ethhdr, batman_ogm_packet,
1221 tt_buff, if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001222
Marek Lindner76e3d7fc2012-02-07 17:20:50 +08001223 buff_pos += BATMAN_OGM_HLEN +
Marek Lindnerfc957272011-07-30 12:04:12 +02001224 tt_len(batman_ogm_packet->tt_num_changes);
1225
1226 batman_ogm_packet = (struct batman_ogm_packet *)
1227 (packet_buff + buff_pos);
Marek Lindner01c42242011-11-28 21:31:55 +08001228 } while (bat_iv_ogm_aggr_packet(buff_pos, packet_len,
1229 batman_ogm_packet->tt_num_changes));
Marek Lindnerc3e29312012-03-04 16:56:25 +08001230
1231 kfree_skb(skb);
1232 return NET_RX_SUCCESS;
Marek Lindnerfc957272011-07-30 12:04:12 +02001233}
Marek Lindner1c280472011-11-28 17:40:17 +08001234
1235static struct bat_algo_ops batman_iv __read_mostly = {
1236 .name = "BATMAN IV",
Marek Lindnerc2aca022012-02-07 17:20:45 +08001237 .bat_iface_enable = bat_iv_ogm_iface_enable,
Marek Lindner00a50072012-02-07 17:20:47 +08001238 .bat_iface_disable = bat_iv_ogm_iface_disable,
Marek Lindnerc3229392012-03-11 06:17:50 +08001239 .bat_iface_update_mac = bat_iv_ogm_iface_update_mac,
Marek Lindnercd8b78e2012-02-07 17:20:49 +08001240 .bat_primary_iface_set = bat_iv_ogm_primary_iface_set,
Marek Lindner01c42242011-11-28 21:31:55 +08001241 .bat_ogm_schedule = bat_iv_ogm_schedule,
1242 .bat_ogm_emit = bat_iv_ogm_emit,
Marek Lindner1c280472011-11-28 17:40:17 +08001243};
1244
1245int __init bat_iv_init(void)
1246{
Marek Lindnerc3e29312012-03-04 16:56:25 +08001247 int ret;
1248
1249 /* batman originator packet */
1250 ret = recv_handler_register(BAT_IV_OGM, bat_iv_ogm_receive);
1251 if (ret < 0)
1252 goto out;
1253
1254 ret = bat_algo_register(&batman_iv);
1255 if (ret < 0)
1256 goto handler_unregister;
1257
1258 goto out;
1259
1260handler_unregister:
1261 recv_handler_unregister(BAT_IV_OGM);
1262out:
1263 return ret;
Marek Lindner1c280472011-11-28 17:40:17 +08001264}