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