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