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