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