]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - net/batman-adv/bat_iv_ogm.c
Merge remote-tracking branch 'driver-core/driver-core-next'
[karo-tx-linux.git] / net / batman-adv / bat_iv_ogm.c
1 /* Copyright (C) 2007-2013 B.A.T.M.A.N. contributors:
2  *
3  * Marek Lindner, Simon Wunderlich
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 2 of the GNU General Public
7  * License as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA
18  */
19
20 #include "main.h"
21 #include "translation-table.h"
22 #include "originator.h"
23 #include "routing.h"
24 #include "gateway_common.h"
25 #include "gateway_client.h"
26 #include "hard-interface.h"
27 #include "send.h"
28 #include "bat_algo.h"
29 #include "network-coding.h"
30
31
32 /**
33  * batadv_dup_status - duplicate status
34  * @BATADV_NO_DUP: the packet is a duplicate
35  * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for the
36  *  neighbor)
37  * @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor
38  * @BATADV_PROTECTED: originator is currently protected (after reboot)
39  */
40 enum batadv_dup_status {
41         BATADV_NO_DUP = 0,
42         BATADV_ORIG_DUP,
43         BATADV_NEIGH_DUP,
44         BATADV_PROTECTED,
45 };
46
47 /**
48  * batadv_ring_buffer_set - update the ring buffer with the given value
49  * @lq_recv: pointer to the ring buffer
50  * @lq_index: index to store the value at
51  * @value: value to store in the ring buffer
52  */
53 static void batadv_ring_buffer_set(uint8_t lq_recv[], uint8_t *lq_index,
54                                    uint8_t value)
55 {
56         lq_recv[*lq_index] = value;
57         *lq_index = (*lq_index + 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE;
58 }
59
60 /**
61  * batadv_ring_buffer_set - compute the average of all non-zero values stored
62  * in the given ring buffer
63  * @lq_recv: pointer to the ring buffer
64  *
65  * Returns computed average value.
66  */
67 static uint8_t batadv_ring_buffer_avg(const uint8_t lq_recv[])
68 {
69         const uint8_t *ptr;
70         uint16_t count = 0, i = 0, sum = 0;
71
72         ptr = lq_recv;
73
74         while (i < BATADV_TQ_GLOBAL_WINDOW_SIZE) {
75                 if (*ptr != 0) {
76                         count++;
77                         sum += *ptr;
78                 }
79
80                 i++;
81                 ptr++;
82         }
83
84         if (count == 0)
85                 return 0;
86
87         return (uint8_t)(sum / count);
88 }
89
90 static struct batadv_neigh_node *
91 batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface,
92                         const uint8_t *neigh_addr,
93                         struct batadv_orig_node *orig_node,
94                         struct batadv_orig_node *orig_neigh)
95 {
96         struct batadv_neigh_node *neigh_node;
97
98         neigh_node = batadv_neigh_node_new(hard_iface, neigh_addr);
99         if (!neigh_node)
100                 goto out;
101
102         INIT_LIST_HEAD(&neigh_node->bonding_list);
103
104         neigh_node->orig_node = orig_neigh;
105         neigh_node->if_incoming = hard_iface;
106
107         spin_lock_bh(&orig_node->neigh_list_lock);
108         hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
109         spin_unlock_bh(&orig_node->neigh_list_lock);
110
111 out:
112         return neigh_node;
113 }
114
115 static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
116 {
117         struct batadv_ogm_packet *batadv_ogm_packet;
118         unsigned char *ogm_buff;
119         uint32_t random_seqno;
120         int res = -ENOMEM;
121
122         /* randomize initial seqno to avoid collision */
123         get_random_bytes(&random_seqno, sizeof(random_seqno));
124         atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno);
125
126         hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN;
127         ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC);
128         if (!ogm_buff)
129                 goto out;
130
131         hard_iface->bat_iv.ogm_buff = ogm_buff;
132
133         batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
134         batadv_ogm_packet->header.packet_type = BATADV_IV_OGM;
135         batadv_ogm_packet->header.version = BATADV_COMPAT_VERSION;
136         batadv_ogm_packet->header.ttl = 2;
137         batadv_ogm_packet->flags = BATADV_NO_FLAGS;
138         batadv_ogm_packet->reserved = 0;
139         batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE;
140
141         res = 0;
142
143 out:
144         return res;
145 }
146
147 static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
148 {
149         kfree(hard_iface->bat_iv.ogm_buff);
150         hard_iface->bat_iv.ogm_buff = NULL;
151 }
152
153 static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface)
154 {
155         struct batadv_ogm_packet *batadv_ogm_packet;
156         unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
157
158         batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
159         memcpy(batadv_ogm_packet->orig,
160                hard_iface->net_dev->dev_addr, ETH_ALEN);
161         memcpy(batadv_ogm_packet->prev_sender,
162                hard_iface->net_dev->dev_addr, ETH_ALEN);
163 }
164
165 static void
166 batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface)
167 {
168         struct batadv_ogm_packet *batadv_ogm_packet;
169         unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
170
171         batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
172         batadv_ogm_packet->flags = BATADV_PRIMARIES_FIRST_HOP;
173         batadv_ogm_packet->header.ttl = BATADV_TTL;
174 }
175
176 /* when do we schedule our own ogm to be sent */
177 static unsigned long
178 batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv)
179 {
180         unsigned int msecs;
181
182         msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
183         msecs += prandom_u32() % (2 * BATADV_JITTER);
184
185         return jiffies + msecs_to_jiffies(msecs);
186 }
187
188 /* when do we schedule a ogm packet to be sent */
189 static unsigned long batadv_iv_ogm_fwd_send_time(void)
190 {
191         return jiffies + msecs_to_jiffies(prandom_u32() % (BATADV_JITTER / 2));
192 }
193
194 /* apply hop penalty for a normal link */
195 static uint8_t batadv_hop_penalty(uint8_t tq,
196                                   const struct batadv_priv *bat_priv)
197 {
198         int hop_penalty = atomic_read(&bat_priv->hop_penalty);
199         int new_tq;
200
201         new_tq = tq * (BATADV_TQ_MAX_VALUE - hop_penalty);
202         new_tq /= BATADV_TQ_MAX_VALUE;
203
204         return new_tq;
205 }
206
207 /* is there another aggregated packet here? */
208 static int batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len,
209                                      __be16 tvlv_len)
210 {
211         int next_buff_pos = 0;
212
213         next_buff_pos += buff_pos + BATADV_OGM_HLEN;
214         next_buff_pos += ntohs(tvlv_len);
215
216         return (next_buff_pos <= packet_len) &&
217                (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
218 }
219
220 /* send a batman ogm to a given interface */
221 static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet,
222                                      struct batadv_hard_iface *hard_iface)
223 {
224         struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
225         char *fwd_str;
226         uint8_t packet_num;
227         int16_t buff_pos;
228         struct batadv_ogm_packet *batadv_ogm_packet;
229         struct sk_buff *skb;
230         uint8_t *packet_pos;
231
232         if (hard_iface->if_status != BATADV_IF_ACTIVE)
233                 return;
234
235         packet_num = 0;
236         buff_pos = 0;
237         packet_pos = forw_packet->skb->data;
238         batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
239
240         /* adjust all flags and log packets */
241         while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
242                                          batadv_ogm_packet->tvlv_len)) {
243                 /* we might have aggregated direct link packets with an
244                  * ordinary base packet
245                  */
246                 if (forw_packet->direct_link_flags & BIT(packet_num) &&
247                     forw_packet->if_incoming == hard_iface)
248                         batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
249                 else
250                         batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
251
252                 if (packet_num > 0 || !forw_packet->own)
253                         fwd_str = "Forwarding";
254                 else
255                         fwd_str = "Sending own";
256
257                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
258                            "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n",
259                            fwd_str, (packet_num > 0 ? "aggregated " : ""),
260                            batadv_ogm_packet->orig,
261                            ntohl(batadv_ogm_packet->seqno),
262                            batadv_ogm_packet->tq, batadv_ogm_packet->header.ttl,
263                            (batadv_ogm_packet->flags & BATADV_DIRECTLINK ?
264                             "on" : "off"),
265                            hard_iface->net_dev->name,
266                            hard_iface->net_dev->dev_addr);
267
268                 buff_pos += BATADV_OGM_HLEN;
269                 buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
270                 packet_num++;
271                 packet_pos = forw_packet->skb->data + buff_pos;
272                 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
273         }
274
275         /* create clone because function is called more than once */
276         skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
277         if (skb) {
278                 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
279                 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
280                                    skb->len + ETH_HLEN);
281                 batadv_send_skb_packet(skb, hard_iface, batadv_broadcast_addr);
282         }
283 }
284
285 /* send a batman ogm packet */
286 static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet)
287 {
288         struct batadv_hard_iface *hard_iface;
289         struct net_device *soft_iface;
290         struct batadv_priv *bat_priv;
291         struct batadv_hard_iface *primary_if = NULL;
292         struct batadv_ogm_packet *batadv_ogm_packet;
293         unsigned char directlink;
294         uint8_t *packet_pos;
295
296         packet_pos = forw_packet->skb->data;
297         batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
298         directlink = (batadv_ogm_packet->flags & BATADV_DIRECTLINK ? 1 : 0);
299
300         if (!forw_packet->if_incoming) {
301                 pr_err("Error - can't forward packet: incoming iface not specified\n");
302                 goto out;
303         }
304
305         soft_iface = forw_packet->if_incoming->soft_iface;
306         bat_priv = netdev_priv(soft_iface);
307
308         if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE)
309                 goto out;
310
311         primary_if = batadv_primary_if_get_selected(bat_priv);
312         if (!primary_if)
313                 goto out;
314
315         /* multihomed peer assumed
316          * non-primary OGMs are only broadcasted on their interface
317          */
318         if ((directlink && (batadv_ogm_packet->header.ttl == 1)) ||
319             (forw_packet->own && (forw_packet->if_incoming != primary_if))) {
320                 /* FIXME: what about aggregated packets ? */
321                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
322                            "%s packet (originator %pM, seqno %u, TTL %d) on interface %s [%pM]\n",
323                            (forw_packet->own ? "Sending own" : "Forwarding"),
324                            batadv_ogm_packet->orig,
325                            ntohl(batadv_ogm_packet->seqno),
326                            batadv_ogm_packet->header.ttl,
327                            forw_packet->if_incoming->net_dev->name,
328                            forw_packet->if_incoming->net_dev->dev_addr);
329
330                 /* skb is only used once and than forw_packet is free'd */
331                 batadv_send_skb_packet(forw_packet->skb,
332                                        forw_packet->if_incoming,
333                                        batadv_broadcast_addr);
334                 forw_packet->skb = NULL;
335
336                 goto out;
337         }
338
339         /* broadcast on every interface */
340         rcu_read_lock();
341         list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
342                 if (hard_iface->soft_iface != soft_iface)
343                         continue;
344
345                 batadv_iv_ogm_send_to_if(forw_packet, hard_iface);
346         }
347         rcu_read_unlock();
348
349 out:
350         if (primary_if)
351                 batadv_hardif_free_ref(primary_if);
352 }
353
354 /* return true if new_packet can be aggregated with forw_packet */
355 static bool
356 batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
357                             struct batadv_priv *bat_priv,
358                             int packet_len, unsigned long send_time,
359                             bool directlink,
360                             const struct batadv_hard_iface *if_incoming,
361                             const struct batadv_forw_packet *forw_packet)
362 {
363         struct batadv_ogm_packet *batadv_ogm_packet;
364         int aggregated_bytes = forw_packet->packet_len + packet_len;
365         struct batadv_hard_iface *primary_if = NULL;
366         bool res = false;
367         unsigned long aggregation_end_time;
368
369         batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data;
370         aggregation_end_time = send_time;
371         aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
372
373         /* we can aggregate the current packet to this aggregated packet
374          * if:
375          *
376          * - the send time is within our MAX_AGGREGATION_MS time
377          * - the resulting packet wont be bigger than
378          *   MAX_AGGREGATION_BYTES
379          */
380         if (time_before(send_time, forw_packet->send_time) &&
381             time_after_eq(aggregation_end_time, forw_packet->send_time) &&
382             (aggregated_bytes <= BATADV_MAX_AGGREGATION_BYTES)) {
383                 /* check aggregation compatibility
384                  * -> direct link packets are broadcasted on
385                  *    their interface only
386                  * -> aggregate packet if the current packet is
387                  *    a "global" packet as well as the base
388                  *    packet
389                  */
390                 primary_if = batadv_primary_if_get_selected(bat_priv);
391                 if (!primary_if)
392                         goto out;
393
394                 /* packets without direct link flag and high TTL
395                  * are flooded through the net
396                  */
397                 if ((!directlink) &&
398                     (!(batadv_ogm_packet->flags & BATADV_DIRECTLINK)) &&
399                     (batadv_ogm_packet->header.ttl != 1) &&
400
401                     /* own packets originating non-primary
402                      * interfaces leave only that interface
403                      */
404                     ((!forw_packet->own) ||
405                      (forw_packet->if_incoming == primary_if))) {
406                         res = true;
407                         goto out;
408                 }
409
410                 /* if the incoming packet is sent via this one
411                  * interface only - we still can aggregate
412                  */
413                 if ((directlink) &&
414                     (new_bat_ogm_packet->header.ttl == 1) &&
415                     (forw_packet->if_incoming == if_incoming) &&
416
417                     /* packets from direct neighbors or
418                      * own secondary interface packets
419                      * (= secondary interface packets in general)
420                      */
421                     (batadv_ogm_packet->flags & BATADV_DIRECTLINK ||
422                      (forw_packet->own &&
423                       forw_packet->if_incoming != primary_if))) {
424                         res = true;
425                         goto out;
426                 }
427         }
428
429 out:
430         if (primary_if)
431                 batadv_hardif_free_ref(primary_if);
432         return res;
433 }
434
435 /* create a new aggregated packet and add this packet to it */
436 static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,
437                                         int packet_len, unsigned long send_time,
438                                         bool direct_link,
439                                         struct batadv_hard_iface *if_incoming,
440                                         int own_packet)
441 {
442         struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
443         struct batadv_forw_packet *forw_packet_aggr;
444         unsigned char *skb_buff;
445         unsigned int skb_size;
446
447         if (!atomic_inc_not_zero(&if_incoming->refcount))
448                 return;
449
450         /* own packet should always be scheduled */
451         if (!own_packet) {
452                 if (!batadv_atomic_dec_not_zero(&bat_priv->batman_queue_left)) {
453                         batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
454                                    "batman packet queue full\n");
455                         goto out;
456                 }
457         }
458
459         forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC);
460         if (!forw_packet_aggr) {
461                 if (!own_packet)
462                         atomic_inc(&bat_priv->batman_queue_left);
463                 goto out;
464         }
465
466         if ((atomic_read(&bat_priv->aggregated_ogms)) &&
467             (packet_len < BATADV_MAX_AGGREGATION_BYTES))
468                 skb_size = BATADV_MAX_AGGREGATION_BYTES;
469         else
470                 skb_size = packet_len;
471
472         skb_size += ETH_HLEN;
473
474         forw_packet_aggr->skb = netdev_alloc_skb_ip_align(NULL, skb_size);
475         if (!forw_packet_aggr->skb) {
476                 if (!own_packet)
477                         atomic_inc(&bat_priv->batman_queue_left);
478                 kfree(forw_packet_aggr);
479                 goto out;
480         }
481         forw_packet_aggr->skb->priority = TC_PRIO_CONTROL;
482         skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
483
484         skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
485         forw_packet_aggr->packet_len = packet_len;
486         memcpy(skb_buff, packet_buff, packet_len);
487
488         forw_packet_aggr->own = own_packet;
489         forw_packet_aggr->if_incoming = if_incoming;
490         forw_packet_aggr->num_packets = 0;
491         forw_packet_aggr->direct_link_flags = BATADV_NO_FLAGS;
492         forw_packet_aggr->send_time = send_time;
493
494         /* save packet direct link flag status */
495         if (direct_link)
496                 forw_packet_aggr->direct_link_flags |= 1;
497
498         /* add new packet to packet list */
499         spin_lock_bh(&bat_priv->forw_bat_list_lock);
500         hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list);
501         spin_unlock_bh(&bat_priv->forw_bat_list_lock);
502
503         /* start timer for this packet */
504         INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
505                           batadv_send_outstanding_bat_ogm_packet);
506         queue_delayed_work(batadv_event_workqueue,
507                            &forw_packet_aggr->delayed_work,
508                            send_time - jiffies);
509
510         return;
511 out:
512         batadv_hardif_free_ref(if_incoming);
513 }
514
515 /* aggregate a new packet into the existing ogm packet */
516 static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr,
517                                     const unsigned char *packet_buff,
518                                     int packet_len, bool direct_link)
519 {
520         unsigned char *skb_buff;
521         unsigned long new_direct_link_flag;
522
523         skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
524         memcpy(skb_buff, packet_buff, packet_len);
525         forw_packet_aggr->packet_len += packet_len;
526         forw_packet_aggr->num_packets++;
527
528         /* save packet direct link flag status */
529         if (direct_link) {
530                 new_direct_link_flag = BIT(forw_packet_aggr->num_packets);
531                 forw_packet_aggr->direct_link_flags |= new_direct_link_flag;
532         }
533 }
534
535 static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv,
536                                     unsigned char *packet_buff,
537                                     int packet_len,
538                                     struct batadv_hard_iface *if_incoming,
539                                     int own_packet, unsigned long send_time)
540 {
541         /* _aggr -> pointer to the packet we want to aggregate with
542          * _pos -> pointer to the position in the queue
543          */
544         struct batadv_forw_packet *forw_packet_aggr = NULL;
545         struct batadv_forw_packet *forw_packet_pos = NULL;
546         struct batadv_ogm_packet *batadv_ogm_packet;
547         bool direct_link;
548         unsigned long max_aggregation_jiffies;
549
550         batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
551         direct_link = batadv_ogm_packet->flags & BATADV_DIRECTLINK ? 1 : 0;
552         max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
553
554         /* find position for the packet in the forward queue */
555         spin_lock_bh(&bat_priv->forw_bat_list_lock);
556         /* own packets are not to be aggregated */
557         if ((atomic_read(&bat_priv->aggregated_ogms)) && (!own_packet)) {
558                 hlist_for_each_entry(forw_packet_pos,
559                                      &bat_priv->forw_bat_list, list) {
560                         if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
561                                                         bat_priv, packet_len,
562                                                         send_time, direct_link,
563                                                         if_incoming,
564                                                         forw_packet_pos)) {
565                                 forw_packet_aggr = forw_packet_pos;
566                                 break;
567                         }
568                 }
569         }
570
571         /* nothing to aggregate with - either aggregation disabled or no
572          * suitable aggregation packet found
573          */
574         if (!forw_packet_aggr) {
575                 /* the following section can run without the lock */
576                 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
577
578                 /* if we could not aggregate this packet with one of the others
579                  * we hold it back for a while, so that it might be aggregated
580                  * later on
581                  */
582                 if (!own_packet && atomic_read(&bat_priv->aggregated_ogms))
583                         send_time += max_aggregation_jiffies;
584
585                 batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
586                                             send_time, direct_link,
587                                             if_incoming, own_packet);
588         } else {
589                 batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
590                                         packet_len, direct_link);
591                 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
592         }
593 }
594
595 static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
596                                   const struct ethhdr *ethhdr,
597                                   struct batadv_ogm_packet *batadv_ogm_packet,
598                                   bool is_single_hop_neigh,
599                                   bool is_from_best_next_hop,
600                                   struct batadv_hard_iface *if_incoming)
601 {
602         struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
603         uint16_t tvlv_len;
604
605         if (batadv_ogm_packet->header.ttl <= 1) {
606                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
607                 return;
608         }
609
610         if (!is_from_best_next_hop) {
611                 /* Mark the forwarded packet when it is not coming from our
612                  * best next hop. We still need to forward the packet for our
613                  * neighbor link quality detection to work in case the packet
614                  * originated from a single hop neighbor. Otherwise we can
615                  * simply drop the ogm.
616                  */
617                 if (is_single_hop_neigh)
618                         batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP;
619                 else
620                         return;
621         }
622
623         tvlv_len = ntohs(batadv_ogm_packet->tvlv_len);
624
625         batadv_ogm_packet->header.ttl--;
626         memcpy(batadv_ogm_packet->prev_sender, ethhdr->h_source, ETH_ALEN);
627
628         /* apply hop penalty */
629         batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq,
630                                                    bat_priv);
631
632         batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
633                    "Forwarding packet: tq: %i, ttl: %i\n",
634                    batadv_ogm_packet->tq, batadv_ogm_packet->header.ttl);
635
636         /* switch of primaries first hop flag when forwarding */
637         batadv_ogm_packet->flags &= ~BATADV_PRIMARIES_FIRST_HOP;
638         if (is_single_hop_neigh)
639                 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
640         else
641                 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
642
643         batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
644                                 BATADV_OGM_HLEN + tvlv_len,
645                                 if_incoming, 0, batadv_iv_ogm_fwd_send_time());
646 }
647
648 /**
649  * batadv_iv_ogm_slide_own_bcast_window - bitshift own OGM broadcast windows for
650  * the given interface
651  * @hard_iface: the interface for which the windows have to be shifted
652  */
653 static void
654 batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
655 {
656         struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
657         struct batadv_hashtable *hash = bat_priv->orig_hash;
658         struct hlist_head *head;
659         struct batadv_orig_node *orig_node;
660         unsigned long *word;
661         uint32_t i;
662         size_t word_index;
663         uint8_t *w;
664
665         for (i = 0; i < hash->size; i++) {
666                 head = &hash->table[i];
667
668                 rcu_read_lock();
669                 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
670                         spin_lock_bh(&orig_node->ogm_cnt_lock);
671                         word_index = hard_iface->if_num * BATADV_NUM_WORDS;
672                         word = &(orig_node->bcast_own[word_index]);
673
674                         batadv_bit_get_packet(bat_priv, word, 1, 0);
675                         w = &orig_node->bcast_own_sum[hard_iface->if_num];
676                         *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
677                         spin_unlock_bh(&orig_node->ogm_cnt_lock);
678                 }
679                 rcu_read_unlock();
680         }
681 }
682
683 static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
684 {
685         struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
686         unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff;
687         struct batadv_ogm_packet *batadv_ogm_packet;
688         struct batadv_hard_iface *primary_if;
689         int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len;
690         uint32_t seqno;
691         uint16_t tvlv_len = 0;
692
693         primary_if = batadv_primary_if_get_selected(bat_priv);
694
695         if (hard_iface == primary_if) {
696                 /* tt changes have to be committed before the tvlv data is
697                  * appended as it may alter the tt tvlv container
698                  */
699                 batadv_tt_local_commit_changes(bat_priv);
700                 tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff,
701                                                             ogm_buff_len,
702                                                             BATADV_OGM_HLEN);
703         }
704
705         batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff);
706         batadv_ogm_packet->tvlv_len = htons(tvlv_len);
707
708         /* change sequence number to network order */
709         seqno = (uint32_t)atomic_read(&hard_iface->bat_iv.ogm_seqno);
710         batadv_ogm_packet->seqno = htonl(seqno);
711         atomic_inc(&hard_iface->bat_iv.ogm_seqno);
712
713         batadv_iv_ogm_slide_own_bcast_window(hard_iface);
714         batadv_iv_ogm_queue_add(bat_priv, hard_iface->bat_iv.ogm_buff,
715                                 hard_iface->bat_iv.ogm_buff_len, hard_iface, 1,
716                                 batadv_iv_ogm_emit_send_time(bat_priv));
717
718         if (primary_if)
719                 batadv_hardif_free_ref(primary_if);
720 }
721
722 static void
723 batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
724                           struct batadv_orig_node *orig_node,
725                           const struct ethhdr *ethhdr,
726                           const struct batadv_ogm_packet *batadv_ogm_packet,
727                           struct batadv_hard_iface *if_incoming,
728                           const unsigned char *tt_buff,
729                           enum batadv_dup_status dup_status)
730 {
731         struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
732         struct batadv_neigh_node *router = NULL;
733         struct batadv_orig_node *orig_node_tmp;
734         int if_num;
735         uint8_t sum_orig, sum_neigh;
736         uint8_t *neigh_addr;
737         uint8_t tq_avg;
738
739         batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
740                    "update_originator(): Searching and updating originator entry of received packet\n");
741
742         rcu_read_lock();
743         hlist_for_each_entry_rcu(tmp_neigh_node,
744                                  &orig_node->neigh_list, list) {
745                 neigh_addr = tmp_neigh_node->addr;
746                 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
747                     tmp_neigh_node->if_incoming == if_incoming &&
748                     atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
749                         if (WARN(neigh_node, "too many matching neigh_nodes"))
750                                 batadv_neigh_node_free_ref(neigh_node);
751                         neigh_node = tmp_neigh_node;
752                         continue;
753                 }
754
755                 if (dup_status != BATADV_NO_DUP)
756                         continue;
757
758                 spin_lock_bh(&tmp_neigh_node->lq_update_lock);
759                 batadv_ring_buffer_set(tmp_neigh_node->tq_recv,
760                                        &tmp_neigh_node->tq_index, 0);
761                 tq_avg = batadv_ring_buffer_avg(tmp_neigh_node->tq_recv);
762                 tmp_neigh_node->tq_avg = tq_avg;
763                 spin_unlock_bh(&tmp_neigh_node->lq_update_lock);
764         }
765
766         if (!neigh_node) {
767                 struct batadv_orig_node *orig_tmp;
768
769                 orig_tmp = batadv_get_orig_node(bat_priv, ethhdr->h_source);
770                 if (!orig_tmp)
771                         goto unlock;
772
773                 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
774                                                      ethhdr->h_source,
775                                                      orig_node, orig_tmp);
776
777                 batadv_orig_node_free_ref(orig_tmp);
778                 if (!neigh_node)
779                         goto unlock;
780         } else
781                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
782                            "Updating existing last-hop neighbor of originator\n");
783
784         rcu_read_unlock();
785
786         neigh_node->last_seen = jiffies;
787
788         spin_lock_bh(&neigh_node->lq_update_lock);
789         batadv_ring_buffer_set(neigh_node->tq_recv,
790                                &neigh_node->tq_index,
791                                batadv_ogm_packet->tq);
792         neigh_node->tq_avg = batadv_ring_buffer_avg(neigh_node->tq_recv);
793         spin_unlock_bh(&neigh_node->lq_update_lock);
794
795         if (dup_status == BATADV_NO_DUP) {
796                 orig_node->last_ttl = batadv_ogm_packet->header.ttl;
797                 neigh_node->last_ttl = batadv_ogm_packet->header.ttl;
798         }
799
800         batadv_bonding_candidate_add(orig_node, neigh_node);
801
802         /* if this neighbor already is our next hop there is nothing
803          * to change
804          */
805         router = batadv_orig_node_get_router(orig_node);
806         if (router == neigh_node)
807                 goto out;
808
809         /* if this neighbor does not offer a better TQ we won't consider it */
810         if (router && (router->tq_avg > neigh_node->tq_avg))
811                 goto out;
812
813         /* if the TQ is the same and the link not more symmetric we
814          * won't consider it either
815          */
816         if (router && (neigh_node->tq_avg == router->tq_avg)) {
817                 orig_node_tmp = router->orig_node;
818                 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
819                 if_num = router->if_incoming->if_num;
820                 sum_orig = orig_node_tmp->bcast_own_sum[if_num];
821                 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
822
823                 orig_node_tmp = neigh_node->orig_node;
824                 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
825                 if_num = neigh_node->if_incoming->if_num;
826                 sum_neigh = orig_node_tmp->bcast_own_sum[if_num];
827                 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
828
829                 if (sum_orig >= sum_neigh)
830                         goto out;
831         }
832
833         batadv_update_route(bat_priv, orig_node, neigh_node);
834         goto out;
835
836 unlock:
837         rcu_read_unlock();
838 out:
839         if (neigh_node)
840                 batadv_neigh_node_free_ref(neigh_node);
841         if (router)
842                 batadv_neigh_node_free_ref(router);
843 }
844
845 static int batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
846                                  struct batadv_orig_node *orig_neigh_node,
847                                  struct batadv_ogm_packet *batadv_ogm_packet,
848                                  struct batadv_hard_iface *if_incoming)
849 {
850         struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
851         struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
852         uint8_t total_count;
853         uint8_t orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
854         unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
855         int tq_asym_penalty, inv_asym_penalty, ret = 0;
856         unsigned int combined_tq;
857
858         /* find corresponding one hop neighbor */
859         rcu_read_lock();
860         hlist_for_each_entry_rcu(tmp_neigh_node,
861                                  &orig_neigh_node->neigh_list, list) {
862                 if (!batadv_compare_eth(tmp_neigh_node->addr,
863                                         orig_neigh_node->orig))
864                         continue;
865
866                 if (tmp_neigh_node->if_incoming != if_incoming)
867                         continue;
868
869                 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
870                         continue;
871
872                 neigh_node = tmp_neigh_node;
873                 break;
874         }
875         rcu_read_unlock();
876
877         if (!neigh_node)
878                 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
879                                                      orig_neigh_node->orig,
880                                                      orig_neigh_node,
881                                                      orig_neigh_node);
882
883         if (!neigh_node)
884                 goto out;
885
886         /* if orig_node is direct neighbor update neigh_node last_seen */
887         if (orig_node == orig_neigh_node)
888                 neigh_node->last_seen = jiffies;
889
890         orig_node->last_seen = jiffies;
891
892         /* find packet count of corresponding one hop neighbor */
893         spin_lock_bh(&orig_node->ogm_cnt_lock);
894         orig_eq_count = orig_neigh_node->bcast_own_sum[if_incoming->if_num];
895         neigh_rq_count = neigh_node->real_packet_count;
896         spin_unlock_bh(&orig_node->ogm_cnt_lock);
897
898         /* pay attention to not get a value bigger than 100 % */
899         if (orig_eq_count > neigh_rq_count)
900                 total_count = neigh_rq_count;
901         else
902                 total_count = orig_eq_count;
903
904         /* if we have too few packets (too less data) we set tq_own to zero
905          * if we receive too few packets it is not considered bidirectional
906          */
907         if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
908             neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
909                 tq_own = 0;
910         else
911                 /* neigh_node->real_packet_count is never zero as we
912                  * only purge old information when getting new
913                  * information
914                  */
915                 tq_own = (BATADV_TQ_MAX_VALUE * total_count) /  neigh_rq_count;
916
917         /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
918          * affect the nearly-symmetric links only a little, but
919          * punishes asymmetric links more.  This will give a value
920          * between 0 and TQ_MAX_VALUE
921          */
922         neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
923         neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
924         neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
925                             BATADV_TQ_LOCAL_WINDOW_SIZE *
926                             BATADV_TQ_LOCAL_WINDOW_SIZE;
927         inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
928         inv_asym_penalty /= neigh_rq_max_cube;
929         tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;
930
931         combined_tq = batadv_ogm_packet->tq * tq_own * tq_asym_penalty;
932         combined_tq /= BATADV_TQ_MAX_VALUE * BATADV_TQ_MAX_VALUE;
933         batadv_ogm_packet->tq = combined_tq;
934
935         batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
936                    "bidirectional: orig = %-15pM neigh = %-15pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, total tq: %3i\n",
937                    orig_node->orig, orig_neigh_node->orig, total_count,
938                    neigh_rq_count, tq_own,
939                    tq_asym_penalty, batadv_ogm_packet->tq);
940
941         /* if link has the minimum required transmission quality
942          * consider it bidirectional
943          */
944         if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
945                 ret = 1;
946
947 out:
948         if (neigh_node)
949                 batadv_neigh_node_free_ref(neigh_node);
950         return ret;
951 }
952
953 /**
954  * batadv_iv_ogm_update_seqnos -  process a batman packet for all interfaces,
955  *  adjust the sequence number and find out whether it is a duplicate
956  * @ethhdr: ethernet header of the packet
957  * @batadv_ogm_packet: OGM packet to be considered
958  * @if_incoming: interface on which the OGM packet was received
959  *
960  * Returns duplicate status as enum batadv_dup_status
961  */
962 static enum batadv_dup_status
963 batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
964                             const struct batadv_ogm_packet *batadv_ogm_packet,
965                             const struct batadv_hard_iface *if_incoming)
966 {
967         struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
968         struct batadv_orig_node *orig_node;
969         struct batadv_neigh_node *tmp_neigh_node;
970         int is_dup;
971         int32_t seq_diff;
972         int need_update = 0;
973         int set_mark;
974         enum batadv_dup_status ret = BATADV_NO_DUP;
975         uint32_t seqno = ntohl(batadv_ogm_packet->seqno);
976         uint8_t *neigh_addr;
977         uint8_t packet_count;
978
979         orig_node = batadv_get_orig_node(bat_priv, batadv_ogm_packet->orig);
980         if (!orig_node)
981                 return BATADV_NO_DUP;
982
983         spin_lock_bh(&orig_node->ogm_cnt_lock);
984         seq_diff = seqno - orig_node->last_real_seqno;
985
986         /* signalize caller that the packet is to be dropped. */
987         if (!hlist_empty(&orig_node->neigh_list) &&
988             batadv_window_protected(bat_priv, seq_diff,
989                                     &orig_node->batman_seqno_reset)) {
990                 ret = BATADV_PROTECTED;
991                 goto out;
992         }
993
994         rcu_read_lock();
995         hlist_for_each_entry_rcu(tmp_neigh_node,
996                                  &orig_node->neigh_list, list) {
997                 neigh_addr = tmp_neigh_node->addr;
998                 is_dup = batadv_test_bit(tmp_neigh_node->real_bits,
999                                          orig_node->last_real_seqno,
1000                                          seqno);
1001
1002                 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
1003                     tmp_neigh_node->if_incoming == if_incoming) {
1004                         set_mark = 1;
1005                         if (is_dup)
1006                                 ret = BATADV_NEIGH_DUP;
1007                 } else {
1008                         set_mark = 0;
1009                         if (is_dup && (ret != BATADV_NEIGH_DUP))
1010                                 ret = BATADV_ORIG_DUP;
1011                 }
1012
1013                 /* if the window moved, set the update flag. */
1014                 need_update |= batadv_bit_get_packet(bat_priv,
1015                                                      tmp_neigh_node->real_bits,
1016                                                      seq_diff, set_mark);
1017
1018                 packet_count = bitmap_weight(tmp_neigh_node->real_bits,
1019                                              BATADV_TQ_LOCAL_WINDOW_SIZE);
1020                 tmp_neigh_node->real_packet_count = packet_count;
1021         }
1022         rcu_read_unlock();
1023
1024         if (need_update) {
1025                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1026                            "updating last_seqno: old %u, new %u\n",
1027                            orig_node->last_real_seqno, seqno);
1028                 orig_node->last_real_seqno = seqno;
1029         }
1030
1031 out:
1032         spin_unlock_bh(&orig_node->ogm_cnt_lock);
1033         batadv_orig_node_free_ref(orig_node);
1034         return ret;
1035 }
1036
1037 static void batadv_iv_ogm_process(const struct ethhdr *ethhdr,
1038                                   struct batadv_ogm_packet *batadv_ogm_packet,
1039                                   const unsigned char *tt_buff,
1040                                   struct batadv_hard_iface *if_incoming)
1041 {
1042         struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1043         struct batadv_hard_iface *hard_iface;
1044         struct batadv_orig_node *orig_neigh_node, *orig_node;
1045         struct batadv_neigh_node *router = NULL, *router_router = NULL;
1046         struct batadv_neigh_node *orig_neigh_router = NULL;
1047         int has_directlink_flag;
1048         int is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
1049         int is_bidirect;
1050         bool is_single_hop_neigh = false;
1051         bool is_from_best_next_hop = false;
1052         int sameseq, similar_ttl;
1053         enum batadv_dup_status dup_status;
1054         uint32_t if_incoming_seqno;
1055         uint8_t *prev_sender;
1056
1057         /* Silently drop when the batman packet is actually not a
1058          * correct packet.
1059          *
1060          * This might happen if a packet is padded (e.g. Ethernet has a
1061          * minimum frame length of 64 byte) and the aggregation interprets
1062          * it as an additional length.
1063          *
1064          * TODO: A more sane solution would be to have a bit in the
1065          * batadv_ogm_packet to detect whether the packet is the last
1066          * packet in an aggregation.  Here we expect that the padding
1067          * is always zero (or not 0x01)
1068          */
1069         if (batadv_ogm_packet->header.packet_type != BATADV_IV_OGM)
1070                 return;
1071
1072         /* could be changed by schedule_own_packet() */
1073         if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno);
1074
1075         if (batadv_ogm_packet->flags & BATADV_DIRECTLINK)
1076                 has_directlink_flag = 1;
1077         else
1078                 has_directlink_flag = 0;
1079
1080         if (batadv_compare_eth(ethhdr->h_source, batadv_ogm_packet->orig))
1081                 is_single_hop_neigh = true;
1082
1083         batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1084                    "Received BATMAN packet via NB: %pM, IF: %s [%pM] (from OG: %pM, via prev OG: %pM, seqno %u, tq %d, TTL %d, V %d, IDF %d)\n",
1085                    ethhdr->h_source, if_incoming->net_dev->name,
1086                    if_incoming->net_dev->dev_addr, batadv_ogm_packet->orig,
1087                    batadv_ogm_packet->prev_sender,
1088                    ntohl(batadv_ogm_packet->seqno), batadv_ogm_packet->tq,
1089                    batadv_ogm_packet->header.ttl,
1090                    batadv_ogm_packet->header.version, has_directlink_flag);
1091
1092         rcu_read_lock();
1093         list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1094                 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1095                         continue;
1096
1097                 if (hard_iface->soft_iface != if_incoming->soft_iface)
1098                         continue;
1099
1100                 if (batadv_compare_eth(ethhdr->h_source,
1101                                        hard_iface->net_dev->dev_addr))
1102                         is_my_addr = 1;
1103
1104                 if (batadv_compare_eth(batadv_ogm_packet->orig,
1105                                        hard_iface->net_dev->dev_addr))
1106                         is_my_orig = 1;
1107
1108                 if (batadv_compare_eth(batadv_ogm_packet->prev_sender,
1109                                        hard_iface->net_dev->dev_addr))
1110                         is_my_oldorig = 1;
1111         }
1112         rcu_read_unlock();
1113
1114         if (is_my_addr) {
1115                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1116                            "Drop packet: received my own broadcast (sender: %pM)\n",
1117                            ethhdr->h_source);
1118                 return;
1119         }
1120
1121         if (is_my_orig) {
1122                 unsigned long *word;
1123                 int offset;
1124                 int32_t bit_pos;
1125                 int16_t if_num;
1126                 uint8_t *weight;
1127
1128                 orig_neigh_node = batadv_get_orig_node(bat_priv,
1129                                                        ethhdr->h_source);
1130                 if (!orig_neigh_node)
1131                         return;
1132
1133                 /* neighbor has to indicate direct link and it has to
1134                  * come via the corresponding interface
1135                  * save packet seqno for bidirectional check
1136                  */
1137                 if (has_directlink_flag &&
1138                     batadv_compare_eth(if_incoming->net_dev->dev_addr,
1139                                        batadv_ogm_packet->orig)) {
1140                         if_num = if_incoming->if_num;
1141                         offset = if_num * BATADV_NUM_WORDS;
1142
1143                         spin_lock_bh(&orig_neigh_node->ogm_cnt_lock);
1144                         word = &(orig_neigh_node->bcast_own[offset]);
1145                         bit_pos = if_incoming_seqno - 2;
1146                         bit_pos -= ntohl(batadv_ogm_packet->seqno);
1147                         batadv_set_bit(word, bit_pos);
1148                         weight = &orig_neigh_node->bcast_own_sum[if_num];
1149                         *weight = bitmap_weight(word,
1150                                                 BATADV_TQ_LOCAL_WINDOW_SIZE);
1151                         spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock);
1152                 }
1153
1154                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1155                            "Drop packet: originator packet from myself (via neighbor)\n");
1156                 batadv_orig_node_free_ref(orig_neigh_node);
1157                 return;
1158         }
1159
1160         if (is_my_oldorig) {
1161                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1162                            "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1163                            ethhdr->h_source);
1164                 return;
1165         }
1166
1167         if (batadv_ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
1168                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1169                            "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1170                            ethhdr->h_source);
1171                 return;
1172         }
1173
1174         orig_node = batadv_get_orig_node(bat_priv, batadv_ogm_packet->orig);
1175         if (!orig_node)
1176                 return;
1177
1178         dup_status = batadv_iv_ogm_update_seqnos(ethhdr, batadv_ogm_packet,
1179                                                  if_incoming);
1180
1181         if (dup_status == BATADV_PROTECTED) {
1182                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1183                            "Drop packet: packet within seqno protection time (sender: %pM)\n",
1184                            ethhdr->h_source);
1185                 goto out;
1186         }
1187
1188         if (batadv_ogm_packet->tq == 0) {
1189                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1190                            "Drop packet: originator packet with tq equal 0\n");
1191                 goto out;
1192         }
1193
1194         router = batadv_orig_node_get_router(orig_node);
1195         if (router)
1196                 router_router = batadv_orig_node_get_router(router->orig_node);
1197
1198         if ((router && router->tq_avg != 0) &&
1199             (batadv_compare_eth(router->addr, ethhdr->h_source)))
1200                 is_from_best_next_hop = true;
1201
1202         prev_sender = batadv_ogm_packet->prev_sender;
1203         /* avoid temporary routing loops */
1204         if (router && router_router &&
1205             (batadv_compare_eth(router->addr, prev_sender)) &&
1206             !(batadv_compare_eth(batadv_ogm_packet->orig, prev_sender)) &&
1207             (batadv_compare_eth(router->addr, router_router->addr))) {
1208                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1209                            "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1210                            ethhdr->h_source);
1211                 goto out;
1212         }
1213
1214         batadv_tvlv_ogm_receive(bat_priv, batadv_ogm_packet, orig_node);
1215
1216         /* if sender is a direct neighbor the sender mac equals
1217          * originator mac
1218          */
1219         if (is_single_hop_neigh)
1220                 orig_neigh_node = orig_node;
1221         else
1222                 orig_neigh_node = batadv_get_orig_node(bat_priv,
1223                                                        ethhdr->h_source);
1224
1225         if (!orig_neigh_node)
1226                 goto out;
1227
1228         /* Update nc_nodes of the originator */
1229         batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node,
1230                                  batadv_ogm_packet, is_single_hop_neigh);
1231
1232         orig_neigh_router = batadv_orig_node_get_router(orig_neigh_node);
1233
1234         /* drop packet if sender is not a direct neighbor and if we
1235          * don't route towards it
1236          */
1237         if (!is_single_hop_neigh && (!orig_neigh_router)) {
1238                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1239                            "Drop packet: OGM via unknown neighbor!\n");
1240                 goto out_neigh;
1241         }
1242
1243         is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
1244                                             batadv_ogm_packet, if_incoming);
1245
1246         batadv_bonding_save_primary(orig_node, orig_neigh_node,
1247                                     batadv_ogm_packet);
1248
1249         /* update ranking if it is not a duplicate or has the same
1250          * seqno and similar ttl as the non-duplicate
1251          */
1252         sameseq = orig_node->last_real_seqno == ntohl(batadv_ogm_packet->seqno);
1253         similar_ttl = orig_node->last_ttl - 3 <= batadv_ogm_packet->header.ttl;
1254         if (is_bidirect && ((dup_status == BATADV_NO_DUP) ||
1255                             (sameseq && similar_ttl)))
1256                 batadv_iv_ogm_orig_update(bat_priv, orig_node, ethhdr,
1257                                           batadv_ogm_packet, if_incoming,
1258                                           tt_buff, dup_status);
1259
1260         /* is single hop (direct) neighbor */
1261         if (is_single_hop_neigh) {
1262                 /* mark direct link on incoming interface */
1263                 batadv_iv_ogm_forward(orig_node, ethhdr, batadv_ogm_packet,
1264                                       is_single_hop_neigh,
1265                                       is_from_best_next_hop, if_incoming);
1266
1267                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1268                            "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
1269                 goto out_neigh;
1270         }
1271
1272         /* multihop originator */
1273         if (!is_bidirect) {
1274                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1275                            "Drop packet: not received via bidirectional link\n");
1276                 goto out_neigh;
1277         }
1278
1279         if (dup_status == BATADV_NEIGH_DUP) {
1280                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1281                            "Drop packet: duplicate packet received\n");
1282                 goto out_neigh;
1283         }
1284
1285         batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1286                    "Forwarding packet: rebroadcast originator packet\n");
1287         batadv_iv_ogm_forward(orig_node, ethhdr, batadv_ogm_packet,
1288                               is_single_hop_neigh, is_from_best_next_hop,
1289                               if_incoming);
1290
1291 out_neigh:
1292         if ((orig_neigh_node) && (!is_single_hop_neigh))
1293                 batadv_orig_node_free_ref(orig_neigh_node);
1294 out:
1295         if (router)
1296                 batadv_neigh_node_free_ref(router);
1297         if (router_router)
1298                 batadv_neigh_node_free_ref(router_router);
1299         if (orig_neigh_router)
1300                 batadv_neigh_node_free_ref(orig_neigh_router);
1301
1302         batadv_orig_node_free_ref(orig_node);
1303 }
1304
1305 static int batadv_iv_ogm_receive(struct sk_buff *skb,
1306                                  struct batadv_hard_iface *if_incoming)
1307 {
1308         struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1309         struct batadv_ogm_packet *batadv_ogm_packet;
1310         struct ethhdr *ethhdr;
1311         int buff_pos = 0, packet_len;
1312         unsigned char *tvlv_buff, *packet_buff;
1313         uint8_t *packet_pos;
1314         bool ret;
1315
1316         ret = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
1317         if (!ret)
1318                 return NET_RX_DROP;
1319
1320         /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
1321          * that does not have B.A.T.M.A.N. IV enabled ?
1322          */
1323         if (bat_priv->bat_algo_ops->bat_ogm_emit != batadv_iv_ogm_emit)
1324                 return NET_RX_DROP;
1325
1326         batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
1327         batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
1328                            skb->len + ETH_HLEN);
1329
1330         packet_len = skb_headlen(skb);
1331         ethhdr = eth_hdr(skb);
1332         packet_buff = skb->data;
1333         batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
1334
1335         /* unpack the aggregated packets and process them one by one */
1336         while (batadv_iv_ogm_aggr_packet(buff_pos, packet_len,
1337                                          batadv_ogm_packet->tvlv_len)) {
1338                 tvlv_buff = packet_buff + buff_pos + BATADV_OGM_HLEN;
1339
1340                 batadv_iv_ogm_process(ethhdr, batadv_ogm_packet,
1341                                       tvlv_buff, if_incoming);
1342
1343                 buff_pos += BATADV_OGM_HLEN;
1344                 buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
1345
1346                 packet_pos = packet_buff + buff_pos;
1347                 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
1348         }
1349
1350         kfree_skb(skb);
1351         return NET_RX_SUCCESS;
1352 }
1353
1354 static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
1355         .name = "BATMAN_IV",
1356         .bat_iface_enable = batadv_iv_ogm_iface_enable,
1357         .bat_iface_disable = batadv_iv_ogm_iface_disable,
1358         .bat_iface_update_mac = batadv_iv_ogm_iface_update_mac,
1359         .bat_primary_iface_set = batadv_iv_ogm_primary_iface_set,
1360         .bat_ogm_schedule = batadv_iv_ogm_schedule,
1361         .bat_ogm_emit = batadv_iv_ogm_emit,
1362 };
1363
1364 int __init batadv_iv_init(void)
1365 {
1366         int ret;
1367
1368         /* batman originator packet */
1369         ret = batadv_recv_handler_register(BATADV_IV_OGM,
1370                                            batadv_iv_ogm_receive);
1371         if (ret < 0)
1372                 goto out;
1373
1374         ret = batadv_algo_register(&batadv_batman_iv);
1375         if (ret < 0)
1376                 goto handler_unregister;
1377
1378         goto out;
1379
1380 handler_unregister:
1381         batadv_recv_handler_unregister(BATADV_IV_OGM);
1382 out:
1383         return ret;
1384 }