GNU Linux-libre 4.14.328-gnu1
[releases.git] / net / batman-adv / bat_v_ogm.c
1 /* Copyright (C) 2013-2017  B.A.T.M.A.N. contributors:
2  *
3  * Antonio Quartulli
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, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "bat_v_ogm.h"
19 #include "main.h"
20
21 #include <linux/atomic.h>
22 #include <linux/byteorder/generic.h>
23 #include <linux/errno.h>
24 #include <linux/etherdevice.h>
25 #include <linux/fs.h>
26 #include <linux/if_ether.h>
27 #include <linux/jiffies.h>
28 #include <linux/kernel.h>
29 #include <linux/kref.h>
30 #include <linux/list.h>
31 #include <linux/lockdep.h>
32 #include <linux/mutex.h>
33 #include <linux/netdevice.h>
34 #include <linux/random.h>
35 #include <linux/rculist.h>
36 #include <linux/rcupdate.h>
37 #include <linux/skbuff.h>
38 #include <linux/slab.h>
39 #include <linux/stddef.h>
40 #include <linux/string.h>
41 #include <linux/types.h>
42 #include <linux/workqueue.h>
43
44 #include "bat_algo.h"
45 #include "hard-interface.h"
46 #include "hash.h"
47 #include "log.h"
48 #include "originator.h"
49 #include "packet.h"
50 #include "routing.h"
51 #include "send.h"
52 #include "translation-table.h"
53 #include "tvlv.h"
54
55 /**
56  * batadv_v_ogm_orig_get - retrieve and possibly create an originator node
57  * @bat_priv: the bat priv with all the soft interface information
58  * @addr: the address of the originator
59  *
60  * Return: the orig_node corresponding to the specified address. If such object
61  * does not exist it is allocated here. In case of allocation failure returns
62  * NULL.
63  */
64 struct batadv_orig_node *batadv_v_ogm_orig_get(struct batadv_priv *bat_priv,
65                                                const u8 *addr)
66 {
67         struct batadv_orig_node *orig_node;
68         int hash_added;
69
70         orig_node = batadv_orig_hash_find(bat_priv, addr);
71         if (orig_node)
72                 return orig_node;
73
74         orig_node = batadv_orig_node_new(bat_priv, addr);
75         if (!orig_node)
76                 return NULL;
77
78         kref_get(&orig_node->refcount);
79         hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
80                                      batadv_choose_orig, orig_node,
81                                      &orig_node->hash_entry);
82         if (hash_added != 0) {
83                 /* remove refcnt for newly created orig_node and hash entry */
84                 batadv_orig_node_put(orig_node);
85                 batadv_orig_node_put(orig_node);
86                 orig_node = NULL;
87         }
88
89         return orig_node;
90 }
91
92 /**
93  * batadv_v_ogm_start_timer - restart the OGM sending timer
94  * @bat_priv: the bat priv with all the soft interface information
95  */
96 static void batadv_v_ogm_start_timer(struct batadv_priv *bat_priv)
97 {
98         unsigned long msecs;
99         /* this function may be invoked in different contexts (ogm rescheduling
100          * or hard_iface activation), but the work timer should not be reset
101          */
102         if (delayed_work_pending(&bat_priv->bat_v.ogm_wq))
103                 return;
104
105         msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
106         msecs += prandom_u32() % (2 * BATADV_JITTER);
107         queue_delayed_work(batadv_event_workqueue, &bat_priv->bat_v.ogm_wq,
108                            msecs_to_jiffies(msecs));
109 }
110
111 /**
112  * batadv_v_ogm_send_to_if - send a batman ogm using a given interface
113  * @skb: the OGM to send
114  * @hard_iface: the interface to use to send the OGM
115  */
116 static void batadv_v_ogm_send_to_if(struct sk_buff *skb,
117                                     struct batadv_hard_iface *hard_iface)
118 {
119         struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
120
121         if (hard_iface->if_status != BATADV_IF_ACTIVE) {
122                 kfree_skb(skb);
123                 return;
124         }
125
126         batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
127         batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
128                            skb->len + ETH_HLEN);
129
130         batadv_send_broadcast_skb(skb, hard_iface);
131 }
132
133 /**
134  * batadv_v_ogm_send_softif() - periodic worker broadcasting the own OGM
135  *  @bat_priv: the bat priv with all the soft interface information
136  */
137 static void batadv_v_ogm_send_softif(struct batadv_priv *bat_priv)
138 {
139         struct batadv_hard_iface *hard_iface;
140         struct batadv_ogm2_packet *ogm_packet;
141         struct sk_buff *skb, *skb_tmp;
142         unsigned char *ogm_buff;
143         int ogm_buff_len;
144         u16 tvlv_len = 0;
145         int ret;
146
147         lockdep_assert_held(&bat_priv->bat_v.ogm_buff_mutex);
148
149         if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
150                 goto out;
151
152         ogm_buff = bat_priv->bat_v.ogm_buff;
153         ogm_buff_len = bat_priv->bat_v.ogm_buff_len;
154         /* tt changes have to be committed before the tvlv data is
155          * appended as it may alter the tt tvlv container
156          */
157         batadv_tt_local_commit_changes(bat_priv);
158         tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, &ogm_buff,
159                                                     &ogm_buff_len,
160                                                     BATADV_OGM2_HLEN);
161
162         bat_priv->bat_v.ogm_buff = ogm_buff;
163         bat_priv->bat_v.ogm_buff_len = ogm_buff_len;
164
165         skb = netdev_alloc_skb_ip_align(NULL, ETH_HLEN + ogm_buff_len);
166         if (!skb)
167                 goto reschedule;
168
169         skb_reserve(skb, ETH_HLEN);
170         skb_put_data(skb, ogm_buff, ogm_buff_len);
171
172         ogm_packet = (struct batadv_ogm2_packet *)skb->data;
173         ogm_packet->seqno = htonl(atomic_read(&bat_priv->bat_v.ogm_seqno));
174         atomic_inc(&bat_priv->bat_v.ogm_seqno);
175         ogm_packet->tvlv_len = htons(tvlv_len);
176
177         /* broadcast on every interface */
178         rcu_read_lock();
179         list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
180                 if (hard_iface->soft_iface != bat_priv->soft_iface)
181                         continue;
182
183                 if (!kref_get_unless_zero(&hard_iface->refcount))
184                         continue;
185
186                 ret = batadv_hardif_no_broadcast(hard_iface, NULL, NULL);
187                 if (ret) {
188                         char *type;
189
190                         switch (ret) {
191                         case BATADV_HARDIF_BCAST_NORECIPIENT:
192                                 type = "no neighbor";
193                                 break;
194                         case BATADV_HARDIF_BCAST_DUPFWD:
195                                 type = "single neighbor is source";
196                                 break;
197                         case BATADV_HARDIF_BCAST_DUPORIG:
198                                 type = "single neighbor is originator";
199                                 break;
200                         default:
201                                 type = "unknown";
202                         }
203
204                         batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "OGM2 from ourselves on %s suppressed: %s\n",
205                                    hard_iface->net_dev->name, type);
206
207                         batadv_hardif_put(hard_iface);
208                         continue;
209                 }
210
211                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
212                            "Sending own OGM2 packet (originator %pM, seqno %u, throughput %u, TTL %d) on interface %s [%pM]\n",
213                            ogm_packet->orig, ntohl(ogm_packet->seqno),
214                            ntohl(ogm_packet->throughput), ogm_packet->ttl,
215                            hard_iface->net_dev->name,
216                            hard_iface->net_dev->dev_addr);
217
218                 /* this skb gets consumed by batadv_v_ogm_send_to_if() */
219                 skb_tmp = skb_clone(skb, GFP_ATOMIC);
220                 if (!skb_tmp) {
221                         batadv_hardif_put(hard_iface);
222                         break;
223                 }
224
225                 batadv_v_ogm_send_to_if(skb_tmp, hard_iface);
226                 batadv_hardif_put(hard_iface);
227         }
228         rcu_read_unlock();
229
230         consume_skb(skb);
231
232 reschedule:
233         batadv_v_ogm_start_timer(bat_priv);
234 out:
235         return;
236 }
237
238 /**
239  * batadv_v_ogm_send() - periodic worker broadcasting the own OGM
240  * @work: work queue item
241  */
242 static void batadv_v_ogm_send(struct work_struct *work)
243 {
244         struct batadv_priv_bat_v *bat_v;
245         struct batadv_priv *bat_priv;
246
247         bat_v = container_of(work, struct batadv_priv_bat_v, ogm_wq.work);
248         bat_priv = container_of(bat_v, struct batadv_priv, bat_v);
249
250         mutex_lock(&bat_priv->bat_v.ogm_buff_mutex);
251         batadv_v_ogm_send_softif(bat_priv);
252         mutex_unlock(&bat_priv->bat_v.ogm_buff_mutex);
253 }
254
255 /**
256  * batadv_v_ogm_iface_enable - prepare an interface for B.A.T.M.A.N. V
257  * @hard_iface: the interface to prepare
258  *
259  * Takes care of scheduling own OGM sending routine for this interface.
260  *
261  * Return: 0 on success or a negative error code otherwise
262  */
263 int batadv_v_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
264 {
265         struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
266
267         batadv_v_ogm_start_timer(bat_priv);
268
269         return 0;
270 }
271
272 /**
273  * batadv_v_ogm_primary_iface_set - set a new primary interface
274  * @primary_iface: the new primary interface
275  */
276 void batadv_v_ogm_primary_iface_set(struct batadv_hard_iface *primary_iface)
277 {
278         struct batadv_priv *bat_priv = netdev_priv(primary_iface->soft_iface);
279         struct batadv_ogm2_packet *ogm_packet;
280
281         mutex_lock(&bat_priv->bat_v.ogm_buff_mutex);
282         if (!bat_priv->bat_v.ogm_buff)
283                 goto unlock;
284
285         ogm_packet = (struct batadv_ogm2_packet *)bat_priv->bat_v.ogm_buff;
286         ether_addr_copy(ogm_packet->orig, primary_iface->net_dev->dev_addr);
287
288 unlock:
289         mutex_unlock(&bat_priv->bat_v.ogm_buff_mutex);
290 }
291
292 /**
293  * batadv_v_forward_penalty - apply a penalty to the throughput metric forwarded
294  *  with B.A.T.M.A.N. V OGMs
295  * @bat_priv: the bat priv with all the soft interface information
296  * @if_incoming: the interface where the OGM has been received
297  * @if_outgoing: the interface where the OGM has to be forwarded to
298  * @throughput: the current throughput
299  *
300  * Apply a penalty on the current throughput metric value based on the
301  * characteristic of the interface where the OGM has been received. The return
302  * value is computed as follows:
303  * - throughput * 50%          if the incoming and outgoing interface are the
304  *                             same WiFi interface and the throughput is above
305  *                             1MBit/s
306  * - throughput                if the outgoing interface is the default
307  *                             interface (i.e. this OGM is processed for the
308  *                             internal table and not forwarded)
309  * - throughput * hop penalty  otherwise
310  *
311  * Return: the penalised throughput metric.
312  */
313 static u32 batadv_v_forward_penalty(struct batadv_priv *bat_priv,
314                                     struct batadv_hard_iface *if_incoming,
315                                     struct batadv_hard_iface *if_outgoing,
316                                     u32 throughput)
317 {
318         int hop_penalty = atomic_read(&bat_priv->hop_penalty);
319         int hop_penalty_max = BATADV_TQ_MAX_VALUE;
320
321         /* Don't apply hop penalty in default originator table. */
322         if (if_outgoing == BATADV_IF_DEFAULT)
323                 return throughput;
324
325         /* Forwarding on the same WiFi interface cuts the throughput in half
326          * due to the store & forward characteristics of WIFI.
327          * Very low throughput values are the exception.
328          */
329         if ((throughput > 10) &&
330             (if_incoming == if_outgoing) &&
331             !(if_incoming->bat_v.flags & BATADV_FULL_DUPLEX))
332                 return throughput / 2;
333
334         /* hop penalty of 255 equals 100% */
335         return throughput * (hop_penalty_max - hop_penalty) / hop_penalty_max;
336 }
337
338 /**
339  * batadv_v_ogm_forward - check conditions and forward an OGM to the given
340  *  outgoing interface
341  * @bat_priv: the bat priv with all the soft interface information
342  * @ogm_received: previously received OGM to be forwarded
343  * @orig_node: the originator which has been updated
344  * @neigh_node: the neigh_node through with the OGM has been received
345  * @if_incoming: the interface on which this OGM was received on
346  * @if_outgoing: the interface to which the OGM has to be forwarded to
347  *
348  * Forward an OGM to an interface after having altered the throughput metric and
349  * the TTL value contained in it. The original OGM isn't modified.
350  */
351 static void batadv_v_ogm_forward(struct batadv_priv *bat_priv,
352                                  const struct batadv_ogm2_packet *ogm_received,
353                                  struct batadv_orig_node *orig_node,
354                                  struct batadv_neigh_node *neigh_node,
355                                  struct batadv_hard_iface *if_incoming,
356                                  struct batadv_hard_iface *if_outgoing)
357 {
358         struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
359         struct batadv_orig_ifinfo *orig_ifinfo = NULL;
360         struct batadv_neigh_node *router = NULL;
361         struct batadv_ogm2_packet *ogm_forward;
362         unsigned char *skb_buff;
363         struct sk_buff *skb;
364         size_t packet_len;
365         u16 tvlv_len;
366
367         /* only forward for specific interfaces, not for the default one. */
368         if (if_outgoing == BATADV_IF_DEFAULT)
369                 goto out;
370
371         orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
372         if (!orig_ifinfo)
373                 goto out;
374
375         /* acquire possibly updated router */
376         router = batadv_orig_router_get(orig_node, if_outgoing);
377
378         /* strict rule: forward packets coming from the best next hop only */
379         if (neigh_node != router)
380                 goto out;
381
382         /* don't forward the same seqno twice on one interface */
383         if (orig_ifinfo->last_seqno_forwarded == ntohl(ogm_received->seqno))
384                 goto out;
385
386         orig_ifinfo->last_seqno_forwarded = ntohl(ogm_received->seqno);
387
388         if (ogm_received->ttl <= 1) {
389                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
390                 goto out;
391         }
392
393         neigh_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
394         if (!neigh_ifinfo)
395                 goto out;
396
397         tvlv_len = ntohs(ogm_received->tvlv_len);
398
399         packet_len = BATADV_OGM2_HLEN + tvlv_len;
400         skb = netdev_alloc_skb_ip_align(if_outgoing->net_dev,
401                                         ETH_HLEN + packet_len);
402         if (!skb)
403                 goto out;
404
405         skb_reserve(skb, ETH_HLEN);
406         skb_buff = skb_put_data(skb, ogm_received, packet_len);
407
408         /* apply forward penalty */
409         ogm_forward = (struct batadv_ogm2_packet *)skb_buff;
410         ogm_forward->throughput = htonl(neigh_ifinfo->bat_v.throughput);
411         ogm_forward->ttl--;
412
413         batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
414                    "Forwarding OGM2 packet on %s: throughput %u, ttl %u, received via %s\n",
415                    if_outgoing->net_dev->name, ntohl(ogm_forward->throughput),
416                    ogm_forward->ttl, if_incoming->net_dev->name);
417
418         batadv_v_ogm_send_to_if(skb, if_outgoing);
419
420 out:
421         if (orig_ifinfo)
422                 batadv_orig_ifinfo_put(orig_ifinfo);
423         if (router)
424                 batadv_neigh_node_put(router);
425         if (neigh_ifinfo)
426                 batadv_neigh_ifinfo_put(neigh_ifinfo);
427 }
428
429 /**
430  * batadv_v_ogm_metric_update - update route metric based on OGM
431  * @bat_priv: the bat priv with all the soft interface information
432  * @ogm2: OGM2 structure
433  * @orig_node: Originator structure for which the OGM has been received
434  * @neigh_node: the neigh_node through with the OGM has been received
435  * @if_incoming: the interface where this packet was received
436  * @if_outgoing: the interface for which the packet should be considered
437  *
438  * Return:
439  *  1  if the OGM is new,
440  *  0  if it is not new but valid,
441  *  <0 on error (e.g. old OGM)
442  */
443 static int batadv_v_ogm_metric_update(struct batadv_priv *bat_priv,
444                                       const struct batadv_ogm2_packet *ogm2,
445                                       struct batadv_orig_node *orig_node,
446                                       struct batadv_neigh_node *neigh_node,
447                                       struct batadv_hard_iface *if_incoming,
448                                       struct batadv_hard_iface *if_outgoing)
449 {
450         struct batadv_orig_ifinfo *orig_ifinfo;
451         struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
452         bool protection_started = false;
453         int ret = -EINVAL;
454         u32 path_throughput;
455         s32 seq_diff;
456
457         orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
458         if (!orig_ifinfo)
459                 goto out;
460
461         seq_diff = ntohl(ogm2->seqno) - orig_ifinfo->last_real_seqno;
462
463         if (!hlist_empty(&orig_node->neigh_list) &&
464             batadv_window_protected(bat_priv, seq_diff,
465                                     BATADV_OGM_MAX_AGE,
466                                     &orig_ifinfo->batman_seqno_reset,
467                                     &protection_started)) {
468                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
469                            "Drop packet: packet within window protection time from %pM\n",
470                            ogm2->orig);
471                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
472                            "Last reset: %ld, %ld\n",
473                            orig_ifinfo->batman_seqno_reset, jiffies);
474                 goto out;
475         }
476
477         /* drop packets with old seqnos, however accept the first packet after
478          * a host has been rebooted.
479          */
480         if ((seq_diff < 0) && !protection_started)
481                 goto out;
482
483         neigh_node->last_seen = jiffies;
484
485         orig_node->last_seen = jiffies;
486
487         orig_ifinfo->last_real_seqno = ntohl(ogm2->seqno);
488         orig_ifinfo->last_ttl = ogm2->ttl;
489
490         neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
491         if (!neigh_ifinfo)
492                 goto out;
493
494         path_throughput = batadv_v_forward_penalty(bat_priv, if_incoming,
495                                                    if_outgoing,
496                                                    ntohl(ogm2->throughput));
497         neigh_ifinfo->bat_v.throughput = path_throughput;
498         neigh_ifinfo->bat_v.last_seqno = ntohl(ogm2->seqno);
499         neigh_ifinfo->last_ttl = ogm2->ttl;
500
501         if (seq_diff > 0 || protection_started)
502                 ret = 1;
503         else
504                 ret = 0;
505 out:
506         if (orig_ifinfo)
507                 batadv_orig_ifinfo_put(orig_ifinfo);
508         if (neigh_ifinfo)
509                 batadv_neigh_ifinfo_put(neigh_ifinfo);
510
511         return ret;
512 }
513
514 /**
515  * batadv_v_ogm_route_update - update routes based on OGM
516  * @bat_priv: the bat priv with all the soft interface information
517  * @ethhdr: the Ethernet header of the OGM2
518  * @ogm2: OGM2 structure
519  * @orig_node: Originator structure for which the OGM has been received
520  * @neigh_node: the neigh_node through with the OGM has been received
521  * @if_incoming: the interface where this packet was received
522  * @if_outgoing: the interface for which the packet should be considered
523  *
524  * Return: true if the packet should be forwarded, false otherwise
525  */
526 static bool batadv_v_ogm_route_update(struct batadv_priv *bat_priv,
527                                       const struct ethhdr *ethhdr,
528                                       const struct batadv_ogm2_packet *ogm2,
529                                       struct batadv_orig_node *orig_node,
530                                       struct batadv_neigh_node *neigh_node,
531                                       struct batadv_hard_iface *if_incoming,
532                                       struct batadv_hard_iface *if_outgoing)
533 {
534         struct batadv_neigh_node *router = NULL;
535         struct batadv_orig_node *orig_neigh_node;
536         struct batadv_neigh_node *orig_neigh_router = NULL;
537         struct batadv_neigh_ifinfo *router_ifinfo = NULL, *neigh_ifinfo = NULL;
538         u32 router_throughput, neigh_throughput;
539         u32 router_last_seqno;
540         u32 neigh_last_seqno;
541         s32 neigh_seq_diff;
542         bool forward = false;
543
544         orig_neigh_node = batadv_v_ogm_orig_get(bat_priv, ethhdr->h_source);
545         if (!orig_neigh_node)
546                 goto out;
547
548         orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
549                                                    if_outgoing);
550
551         /* drop packet if sender is not a direct neighbor and if we
552          * don't route towards it
553          */
554         router = batadv_orig_router_get(orig_node, if_outgoing);
555         if (router && router->orig_node != orig_node && !orig_neigh_router) {
556                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
557                            "Drop packet: OGM via unknown neighbor!\n");
558                 goto out;
559         }
560
561         /* Mark the OGM to be considered for forwarding, and update routes
562          * if needed.
563          */
564         forward = true;
565
566         batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
567                    "Searching and updating originator entry of received packet\n");
568
569         /* if this neighbor already is our next hop there is nothing
570          * to change
571          */
572         if (router == neigh_node)
573                 goto out;
574
575         /* don't consider neighbours with worse throughput.
576          * also switch route if this seqno is BATADV_V_MAX_ORIGDIFF newer than
577          * the last received seqno from our best next hop.
578          */
579         if (router) {
580                 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
581                 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
582
583                 /* if these are not allocated, something is wrong. */
584                 if (!router_ifinfo || !neigh_ifinfo)
585                         goto out;
586
587                 neigh_last_seqno = neigh_ifinfo->bat_v.last_seqno;
588                 router_last_seqno = router_ifinfo->bat_v.last_seqno;
589                 neigh_seq_diff = neigh_last_seqno - router_last_seqno;
590                 router_throughput = router_ifinfo->bat_v.throughput;
591                 neigh_throughput = neigh_ifinfo->bat_v.throughput;
592
593                 if ((neigh_seq_diff < BATADV_OGM_MAX_ORIGDIFF) &&
594                     (router_throughput >= neigh_throughput))
595                         goto out;
596         }
597
598         batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
599 out:
600         if (router)
601                 batadv_neigh_node_put(router);
602         if (orig_neigh_router)
603                 batadv_neigh_node_put(orig_neigh_router);
604         if (orig_neigh_node)
605                 batadv_orig_node_put(orig_neigh_node);
606         if (router_ifinfo)
607                 batadv_neigh_ifinfo_put(router_ifinfo);
608         if (neigh_ifinfo)
609                 batadv_neigh_ifinfo_put(neigh_ifinfo);
610
611         return forward;
612 }
613
614 /**
615  * batadv_v_ogm_process_per_outif - process a batman v OGM for an outgoing if
616  * @bat_priv: the bat priv with all the soft interface information
617  * @ethhdr: the Ethernet header of the OGM2
618  * @ogm2: OGM2 structure
619  * @orig_node: Originator structure for which the OGM has been received
620  * @neigh_node: the neigh_node through with the OGM has been received
621  * @if_incoming: the interface where this packet was received
622  * @if_outgoing: the interface for which the packet should be considered
623  */
624 static void
625 batadv_v_ogm_process_per_outif(struct batadv_priv *bat_priv,
626                                const struct ethhdr *ethhdr,
627                                const struct batadv_ogm2_packet *ogm2,
628                                struct batadv_orig_node *orig_node,
629                                struct batadv_neigh_node *neigh_node,
630                                struct batadv_hard_iface *if_incoming,
631                                struct batadv_hard_iface *if_outgoing)
632 {
633         int seqno_age;
634         bool forward;
635
636         /* first, update the metric with according sanity checks */
637         seqno_age = batadv_v_ogm_metric_update(bat_priv, ogm2, orig_node,
638                                                neigh_node, if_incoming,
639                                                if_outgoing);
640
641         /* outdated sequence numbers are to be discarded */
642         if (seqno_age < 0)
643                 return;
644
645         /* only unknown & newer OGMs contain TVLVs we are interested in */
646         if ((seqno_age > 0) && (if_outgoing == BATADV_IF_DEFAULT))
647                 batadv_tvlv_containers_process(bat_priv, true, orig_node,
648                                                NULL, NULL,
649                                                (unsigned char *)(ogm2 + 1),
650                                                ntohs(ogm2->tvlv_len));
651
652         /* if the metric update went through, update routes if needed */
653         forward = batadv_v_ogm_route_update(bat_priv, ethhdr, ogm2, orig_node,
654                                             neigh_node, if_incoming,
655                                             if_outgoing);
656
657         /* if the routes have been processed correctly, check and forward */
658         if (forward)
659                 batadv_v_ogm_forward(bat_priv, ogm2, orig_node, neigh_node,
660                                      if_incoming, if_outgoing);
661 }
662
663 /**
664  * batadv_v_ogm_aggr_packet - checks if there is another OGM aggregated
665  * @buff_pos: current position in the skb
666  * @packet_len: total length of the skb
667  * @ogm2_packet: potential OGM2 in buffer
668  *
669  * Return: true if there is enough space for another OGM, false otherwise.
670  */
671 static bool
672 batadv_v_ogm_aggr_packet(int buff_pos, int packet_len,
673                          const struct batadv_ogm2_packet *ogm2_packet)
674 {
675         int next_buff_pos = 0;
676
677         /* check if there is enough space for the header */
678         next_buff_pos += buff_pos + sizeof(*ogm2_packet);
679         if (next_buff_pos > packet_len)
680                 return false;
681
682         /* check if there is enough space for the optional TVLV */
683         next_buff_pos += ntohs(ogm2_packet->tvlv_len);
684
685         return (next_buff_pos <= packet_len) &&
686                (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
687 }
688
689 /**
690  * batadv_v_ogm_process - process an incoming batman v OGM
691  * @skb: the skb containing the OGM
692  * @ogm_offset: offset to the OGM which should be processed (for aggregates)
693  * @if_incoming: the interface where this packet was receved
694  */
695 static void batadv_v_ogm_process(const struct sk_buff *skb, int ogm_offset,
696                                  struct batadv_hard_iface *if_incoming)
697 {
698         struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
699         struct ethhdr *ethhdr;
700         struct batadv_orig_node *orig_node = NULL;
701         struct batadv_hardif_neigh_node *hardif_neigh = NULL;
702         struct batadv_neigh_node *neigh_node = NULL;
703         struct batadv_hard_iface *hard_iface;
704         struct batadv_ogm2_packet *ogm_packet;
705         u32 ogm_throughput, link_throughput, path_throughput;
706         int ret;
707
708         ethhdr = eth_hdr(skb);
709         ogm_packet = (struct batadv_ogm2_packet *)(skb->data + ogm_offset);
710
711         ogm_throughput = ntohl(ogm_packet->throughput);
712
713         batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
714                    "Received OGM2 packet via NB: %pM, IF: %s [%pM] (from OG: %pM, seqno %u, throughput %u, TTL %u, V %u, tvlv_len %u)\n",
715                    ethhdr->h_source, if_incoming->net_dev->name,
716                    if_incoming->net_dev->dev_addr, ogm_packet->orig,
717                    ntohl(ogm_packet->seqno), ogm_throughput, ogm_packet->ttl,
718                    ogm_packet->version, ntohs(ogm_packet->tvlv_len));
719
720         if (batadv_is_my_mac(bat_priv, ogm_packet->orig)) {
721                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
722                            "Drop packet: originator packet from ourself\n");
723                 return;
724         }
725
726         /* If the throughput metric is 0, immediately drop the packet. No need
727          * to create orig_node / neigh_node for an unusable route.
728          */
729         if (ogm_throughput == 0) {
730                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
731                            "Drop packet: originator packet with throughput metric of 0\n");
732                 return;
733         }
734
735         /* require ELP packets be to received from this neighbor first */
736         hardif_neigh = batadv_hardif_neigh_get(if_incoming, ethhdr->h_source);
737         if (!hardif_neigh) {
738                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
739                            "Drop packet: OGM via unknown neighbor!\n");
740                 goto out;
741         }
742
743         orig_node = batadv_v_ogm_orig_get(bat_priv, ogm_packet->orig);
744         if (!orig_node)
745                 goto out;
746
747         neigh_node = batadv_neigh_node_get_or_create(orig_node, if_incoming,
748                                                      ethhdr->h_source);
749         if (!neigh_node)
750                 goto out;
751
752         /* Update the received throughput metric to match the link
753          * characteristic:
754          *  - If this OGM traveled one hop so far (emitted by single hop
755          *    neighbor) the path throughput metric equals the link throughput.
756          *  - For OGMs traversing more than hop the path throughput metric is
757          *    the smaller of the path throughput and the link throughput.
758          */
759         link_throughput = ewma_throughput_read(&hardif_neigh->bat_v.throughput);
760         path_throughput = min_t(u32, link_throughput, ogm_throughput);
761         ogm_packet->throughput = htonl(path_throughput);
762
763         batadv_v_ogm_process_per_outif(bat_priv, ethhdr, ogm_packet, orig_node,
764                                        neigh_node, if_incoming,
765                                        BATADV_IF_DEFAULT);
766
767         rcu_read_lock();
768         list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
769                 if (hard_iface->if_status != BATADV_IF_ACTIVE)
770                         continue;
771
772                 if (hard_iface->soft_iface != bat_priv->soft_iface)
773                         continue;
774
775                 if (!kref_get_unless_zero(&hard_iface->refcount))
776                         continue;
777
778                 ret = batadv_hardif_no_broadcast(hard_iface,
779                                                  ogm_packet->orig,
780                                                  hardif_neigh->orig);
781
782                 if (ret) {
783                         char *type;
784
785                         switch (ret) {
786                         case BATADV_HARDIF_BCAST_NORECIPIENT:
787                                 type = "no neighbor";
788                                 break;
789                         case BATADV_HARDIF_BCAST_DUPFWD:
790                                 type = "single neighbor is source";
791                                 break;
792                         case BATADV_HARDIF_BCAST_DUPORIG:
793                                 type = "single neighbor is originator";
794                                 break;
795                         default:
796                                 type = "unknown";
797                         }
798
799                         batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "OGM2 packet from %pM on %s suppressed: %s\n",
800                                    ogm_packet->orig, hard_iface->net_dev->name,
801                                    type);
802
803                         batadv_hardif_put(hard_iface);
804                         continue;
805                 }
806
807                 batadv_v_ogm_process_per_outif(bat_priv, ethhdr, ogm_packet,
808                                                orig_node, neigh_node,
809                                                if_incoming, hard_iface);
810
811                 batadv_hardif_put(hard_iface);
812         }
813         rcu_read_unlock();
814 out:
815         if (orig_node)
816                 batadv_orig_node_put(orig_node);
817         if (neigh_node)
818                 batadv_neigh_node_put(neigh_node);
819         if (hardif_neigh)
820                 batadv_hardif_neigh_put(hardif_neigh);
821 }
822
823 /**
824  * batadv_v_ogm_packet_recv - OGM2 receiving handler
825  * @skb: the received OGM
826  * @if_incoming: the interface where this OGM has been received
827  *
828  * Return: NET_RX_SUCCESS and consume the skb on success or returns NET_RX_DROP
829  * (without freeing the skb) on failure
830  */
831 int batadv_v_ogm_packet_recv(struct sk_buff *skb,
832                              struct batadv_hard_iface *if_incoming)
833 {
834         struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
835         struct batadv_ogm2_packet *ogm_packet;
836         struct ethhdr *ethhdr;
837         int ogm_offset;
838         u8 *packet_pos;
839         int ret = NET_RX_DROP;
840
841         /* did we receive a OGM2 packet on an interface that does not have
842          * B.A.T.M.A.N. V enabled ?
843          */
844         if (strcmp(bat_priv->algo_ops->name, "BATMAN_V") != 0)
845                 goto free_skb;
846
847         if (!batadv_check_management_packet(skb, if_incoming, BATADV_OGM2_HLEN))
848                 goto free_skb;
849
850         ethhdr = eth_hdr(skb);
851         if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
852                 goto free_skb;
853
854         batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
855         batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
856                            skb->len + ETH_HLEN);
857
858         ogm_offset = 0;
859         ogm_packet = (struct batadv_ogm2_packet *)skb->data;
860
861         while (batadv_v_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
862                                         ogm_packet)) {
863                 batadv_v_ogm_process(skb, ogm_offset, if_incoming);
864
865                 ogm_offset += BATADV_OGM2_HLEN;
866                 ogm_offset += ntohs(ogm_packet->tvlv_len);
867
868                 packet_pos = skb->data + ogm_offset;
869                 ogm_packet = (struct batadv_ogm2_packet *)packet_pos;
870         }
871
872         ret = NET_RX_SUCCESS;
873
874 free_skb:
875         if (ret == NET_RX_SUCCESS)
876                 consume_skb(skb);
877         else
878                 kfree_skb(skb);
879
880         return ret;
881 }
882
883 /**
884  * batadv_v_ogm_init - initialise the OGM2 engine
885  * @bat_priv: the bat priv with all the soft interface information
886  *
887  * Return: 0 on success or a negative error code in case of failure
888  */
889 int batadv_v_ogm_init(struct batadv_priv *bat_priv)
890 {
891         struct batadv_ogm2_packet *ogm_packet;
892         unsigned char *ogm_buff;
893         u32 random_seqno;
894
895         bat_priv->bat_v.ogm_buff_len = BATADV_OGM2_HLEN;
896         ogm_buff = kzalloc(bat_priv->bat_v.ogm_buff_len, GFP_ATOMIC);
897         if (!ogm_buff)
898                 return -ENOMEM;
899
900         bat_priv->bat_v.ogm_buff = ogm_buff;
901         ogm_packet = (struct batadv_ogm2_packet *)ogm_buff;
902         ogm_packet->packet_type = BATADV_OGM2;
903         ogm_packet->version = BATADV_COMPAT_VERSION;
904         ogm_packet->ttl = BATADV_TTL;
905         ogm_packet->flags = BATADV_NO_FLAGS;
906         ogm_packet->throughput = htonl(BATADV_THROUGHPUT_MAX_VALUE);
907
908         /* randomize initial seqno to avoid collision */
909         get_random_bytes(&random_seqno, sizeof(random_seqno));
910         atomic_set(&bat_priv->bat_v.ogm_seqno, random_seqno);
911         INIT_DELAYED_WORK(&bat_priv->bat_v.ogm_wq, batadv_v_ogm_send);
912
913         mutex_init(&bat_priv->bat_v.ogm_buff_mutex);
914
915         return 0;
916 }
917
918 /**
919  * batadv_v_ogm_free - free OGM private resources
920  * @bat_priv: the bat priv with all the soft interface information
921  */
922 void batadv_v_ogm_free(struct batadv_priv *bat_priv)
923 {
924         cancel_delayed_work_sync(&bat_priv->bat_v.ogm_wq);
925
926         mutex_lock(&bat_priv->bat_v.ogm_buff_mutex);
927
928         kfree(bat_priv->bat_v.ogm_buff);
929         bat_priv->bat_v.ogm_buff = NULL;
930         bat_priv->bat_v.ogm_buff_len = 0;
931
932         mutex_unlock(&bat_priv->bat_v.ogm_buff_mutex);
933 }