xref: /linux/net/batman-adv/bat_v_ogm.c (revision e04de4861cdebdfaa13507ad3d40e6542aeab857)
1ac79cbb9SSven Eckelmann /* Copyright (C) 2013-2017  B.A.T.M.A.N. contributors:
20da00359SAntonio Quartulli  *
30da00359SAntonio Quartulli  * Antonio Quartulli
40da00359SAntonio Quartulli  *
50da00359SAntonio Quartulli  * This program is free software; you can redistribute it and/or
60da00359SAntonio Quartulli  * modify it under the terms of version 2 of the GNU General Public
70da00359SAntonio Quartulli  * License as published by the Free Software Foundation.
80da00359SAntonio Quartulli  *
90da00359SAntonio Quartulli  * This program is distributed in the hope that it will be useful, but
100da00359SAntonio Quartulli  * WITHOUT ANY WARRANTY; without even the implied warranty of
110da00359SAntonio Quartulli  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
120da00359SAntonio Quartulli  * General Public License for more details.
130da00359SAntonio Quartulli  *
140da00359SAntonio Quartulli  * You should have received a copy of the GNU General Public License
150da00359SAntonio Quartulli  * along with this program; if not, see <http://www.gnu.org/licenses/>.
160da00359SAntonio Quartulli  */
170da00359SAntonio Quartulli 
180da00359SAntonio Quartulli #include "bat_v_ogm.h"
190da00359SAntonio Quartulli #include "main.h"
200da00359SAntonio Quartulli 
210da00359SAntonio Quartulli #include <linux/atomic.h>
220da00359SAntonio Quartulli #include <linux/byteorder/generic.h>
230da00359SAntonio Quartulli #include <linux/errno.h>
240da00359SAntonio Quartulli #include <linux/etherdevice.h>
250da00359SAntonio Quartulli #include <linux/fs.h>
260da00359SAntonio Quartulli #include <linux/if_ether.h>
270da00359SAntonio Quartulli #include <linux/jiffies.h>
280da00359SAntonio Quartulli #include <linux/kernel.h>
2927353446SSven Eckelmann #include <linux/kref.h>
309323158eSAntonio Quartulli #include <linux/list.h>
310da00359SAntonio Quartulli #include <linux/netdevice.h>
320da00359SAntonio Quartulli #include <linux/random.h>
330da00359SAntonio Quartulli #include <linux/rculist.h>
340da00359SAntonio Quartulli #include <linux/rcupdate.h>
350da00359SAntonio Quartulli #include <linux/skbuff.h>
360da00359SAntonio Quartulli #include <linux/slab.h>
370da00359SAntonio Quartulli #include <linux/stddef.h>
380da00359SAntonio Quartulli #include <linux/string.h>
390da00359SAntonio Quartulli #include <linux/types.h>
400da00359SAntonio Quartulli #include <linux/workqueue.h>
410da00359SAntonio Quartulli 
4201d350d1SSven Eckelmann #include "bat_algo.h"
430da00359SAntonio Quartulli #include "hard-interface.h"
449323158eSAntonio Quartulli #include "hash.h"
45ba412080SSven Eckelmann #include "log.h"
469323158eSAntonio Quartulli #include "originator.h"
470da00359SAntonio Quartulli #include "packet.h"
480da00359SAntonio Quartulli #include "routing.h"
490da00359SAntonio Quartulli #include "send.h"
500da00359SAntonio Quartulli #include "translation-table.h"
511f8dce49SMarkus Pargmann #include "tvlv.h"
520da00359SAntonio Quartulli 
530da00359SAntonio Quartulli /**
549323158eSAntonio Quartulli  * batadv_v_ogm_orig_get - retrieve and possibly create an originator node
559323158eSAntonio Quartulli  * @bat_priv: the bat priv with all the soft interface information
569323158eSAntonio Quartulli  * @addr: the address of the originator
579323158eSAntonio Quartulli  *
589323158eSAntonio Quartulli  * Return: the orig_node corresponding to the specified address. If such object
599323158eSAntonio Quartulli  * does not exist it is allocated here. In case of allocation failure returns
609323158eSAntonio Quartulli  * NULL.
619323158eSAntonio Quartulli  */
629323158eSAntonio Quartulli struct batadv_orig_node *batadv_v_ogm_orig_get(struct batadv_priv *bat_priv,
639323158eSAntonio Quartulli 					       const u8 *addr)
649323158eSAntonio Quartulli {
659323158eSAntonio Quartulli 	struct batadv_orig_node *orig_node;
669323158eSAntonio Quartulli 	int hash_added;
679323158eSAntonio Quartulli 
689323158eSAntonio Quartulli 	orig_node = batadv_orig_hash_find(bat_priv, addr);
699323158eSAntonio Quartulli 	if (orig_node)
709323158eSAntonio Quartulli 		return orig_node;
719323158eSAntonio Quartulli 
729323158eSAntonio Quartulli 	orig_node = batadv_orig_node_new(bat_priv, addr);
739323158eSAntonio Quartulli 	if (!orig_node)
749323158eSAntonio Quartulli 		return NULL;
759323158eSAntonio Quartulli 
7655db2d59SSven Eckelmann 	kref_get(&orig_node->refcount);
779323158eSAntonio Quartulli 	hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
789323158eSAntonio Quartulli 				     batadv_choose_orig, orig_node,
799323158eSAntonio Quartulli 				     &orig_node->hash_entry);
809323158eSAntonio Quartulli 	if (hash_added != 0) {
8155db2d59SSven Eckelmann 		/* remove refcnt for newly created orig_node and hash entry */
829323158eSAntonio Quartulli 		batadv_orig_node_put(orig_node);
839323158eSAntonio Quartulli 		batadv_orig_node_put(orig_node);
849323158eSAntonio Quartulli 		orig_node = NULL;
859323158eSAntonio Quartulli 	}
869323158eSAntonio Quartulli 
879323158eSAntonio Quartulli 	return orig_node;
889323158eSAntonio Quartulli }
899323158eSAntonio Quartulli 
909323158eSAntonio Quartulli /**
910da00359SAntonio Quartulli  * batadv_v_ogm_start_timer - restart the OGM sending timer
920da00359SAntonio Quartulli  * @bat_priv: the bat priv with all the soft interface information
930da00359SAntonio Quartulli  */
940da00359SAntonio Quartulli static void batadv_v_ogm_start_timer(struct batadv_priv *bat_priv)
950da00359SAntonio Quartulli {
960da00359SAntonio Quartulli 	unsigned long msecs;
970da00359SAntonio Quartulli 	/* this function may be invoked in different contexts (ogm rescheduling
980da00359SAntonio Quartulli 	 * or hard_iface activation), but the work timer should not be reset
990da00359SAntonio Quartulli 	 */
1000da00359SAntonio Quartulli 	if (delayed_work_pending(&bat_priv->bat_v.ogm_wq))
1010da00359SAntonio Quartulli 		return;
1020da00359SAntonio Quartulli 
1030da00359SAntonio Quartulli 	msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
1040da00359SAntonio Quartulli 	msecs += prandom_u32() % (2 * BATADV_JITTER);
1050da00359SAntonio Quartulli 	queue_delayed_work(batadv_event_workqueue, &bat_priv->bat_v.ogm_wq,
1060da00359SAntonio Quartulli 			   msecs_to_jiffies(msecs));
1070da00359SAntonio Quartulli }
1080da00359SAntonio Quartulli 
1090da00359SAntonio Quartulli /**
1100da00359SAntonio Quartulli  * batadv_v_ogm_send_to_if - send a batman ogm using a given interface
1110da00359SAntonio Quartulli  * @skb: the OGM to send
1120da00359SAntonio Quartulli  * @hard_iface: the interface to use to send the OGM
1130da00359SAntonio Quartulli  */
1140da00359SAntonio Quartulli static void batadv_v_ogm_send_to_if(struct sk_buff *skb,
1150da00359SAntonio Quartulli 				    struct batadv_hard_iface *hard_iface)
1160da00359SAntonio Quartulli {
1170da00359SAntonio Quartulli 	struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
1180da00359SAntonio Quartulli 
1190da00359SAntonio Quartulli 	if (hard_iface->if_status != BATADV_IF_ACTIVE)
1200da00359SAntonio Quartulli 		return;
1210da00359SAntonio Quartulli 
1220da00359SAntonio Quartulli 	batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
1230da00359SAntonio Quartulli 	batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
1240da00359SAntonio Quartulli 			   skb->len + ETH_HLEN);
1250da00359SAntonio Quartulli 
12695d39278SAntonio Quartulli 	batadv_send_broadcast_skb(skb, hard_iface);
1270da00359SAntonio Quartulli }
1280da00359SAntonio Quartulli 
1290da00359SAntonio Quartulli /**
1300da00359SAntonio Quartulli  * batadv_v_ogm_send - periodic worker broadcasting the own OGM
1310da00359SAntonio Quartulli  * @work: work queue item
1320da00359SAntonio Quartulli  */
1330da00359SAntonio Quartulli static void batadv_v_ogm_send(struct work_struct *work)
1340da00359SAntonio Quartulli {
1350da00359SAntonio Quartulli 	struct batadv_hard_iface *hard_iface;
1360da00359SAntonio Quartulli 	struct batadv_priv_bat_v *bat_v;
1370da00359SAntonio Quartulli 	struct batadv_priv *bat_priv;
1380da00359SAntonio Quartulli 	struct batadv_ogm2_packet *ogm_packet;
1390da00359SAntonio Quartulli 	struct sk_buff *skb, *skb_tmp;
140*e04de486SSven Eckelmann 	unsigned char *ogm_buff;
1410da00359SAntonio Quartulli 	int ogm_buff_len;
1420da00359SAntonio Quartulli 	u16 tvlv_len = 0;
1433111beedSLinus Lüssing 	int ret;
1440da00359SAntonio Quartulli 
1450da00359SAntonio Quartulli 	bat_v = container_of(work, struct batadv_priv_bat_v, ogm_wq.work);
1460da00359SAntonio Quartulli 	bat_priv = container_of(bat_v, struct batadv_priv, bat_v);
1470da00359SAntonio Quartulli 
1480da00359SAntonio Quartulli 	if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
1490da00359SAntonio Quartulli 		goto out;
1500da00359SAntonio Quartulli 
1510da00359SAntonio Quartulli 	ogm_buff = bat_priv->bat_v.ogm_buff;
1520da00359SAntonio Quartulli 	ogm_buff_len = bat_priv->bat_v.ogm_buff_len;
1530da00359SAntonio Quartulli 	/* tt changes have to be committed before the tvlv data is
1540da00359SAntonio Quartulli 	 * appended as it may alter the tt tvlv container
1550da00359SAntonio Quartulli 	 */
1560da00359SAntonio Quartulli 	batadv_tt_local_commit_changes(bat_priv);
1570da00359SAntonio Quartulli 	tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, &ogm_buff,
1580da00359SAntonio Quartulli 						    &ogm_buff_len,
1590da00359SAntonio Quartulli 						    BATADV_OGM2_HLEN);
1600da00359SAntonio Quartulli 
1610da00359SAntonio Quartulli 	bat_priv->bat_v.ogm_buff = ogm_buff;
1620da00359SAntonio Quartulli 	bat_priv->bat_v.ogm_buff_len = ogm_buff_len;
1630da00359SAntonio Quartulli 
1640da00359SAntonio Quartulli 	skb = netdev_alloc_skb_ip_align(NULL, ETH_HLEN + ogm_buff_len);
1650da00359SAntonio Quartulli 	if (!skb)
1660da00359SAntonio Quartulli 		goto reschedule;
1670da00359SAntonio Quartulli 
1680da00359SAntonio Quartulli 	skb_reserve(skb, ETH_HLEN);
169*e04de486SSven Eckelmann 	skb_put_data(skb, ogm_buff, ogm_buff_len);
1700da00359SAntonio Quartulli 
1710da00359SAntonio Quartulli 	ogm_packet = (struct batadv_ogm2_packet *)skb->data;
1720da00359SAntonio Quartulli 	ogm_packet->seqno = htonl(atomic_read(&bat_priv->bat_v.ogm_seqno));
1730da00359SAntonio Quartulli 	atomic_inc(&bat_priv->bat_v.ogm_seqno);
1740da00359SAntonio Quartulli 	ogm_packet->tvlv_len = htons(tvlv_len);
1750da00359SAntonio Quartulli 
1760da00359SAntonio Quartulli 	/* broadcast on every interface */
1770da00359SAntonio Quartulli 	rcu_read_lock();
1780da00359SAntonio Quartulli 	list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1790da00359SAntonio Quartulli 		if (hard_iface->soft_iface != bat_priv->soft_iface)
1800da00359SAntonio Quartulli 			continue;
1810da00359SAntonio Quartulli 
18227353446SSven Eckelmann 		if (!kref_get_unless_zero(&hard_iface->refcount))
18327353446SSven Eckelmann 			continue;
18427353446SSven Eckelmann 
1853111beedSLinus Lüssing 		ret = batadv_hardif_no_broadcast(hard_iface, NULL, NULL);
1863111beedSLinus Lüssing 		if (ret) {
1873111beedSLinus Lüssing 			char *type;
1883111beedSLinus Lüssing 
1893111beedSLinus Lüssing 			switch (ret) {
1903111beedSLinus Lüssing 			case BATADV_HARDIF_BCAST_NORECIPIENT:
1913111beedSLinus Lüssing 				type = "no neighbor";
1923111beedSLinus Lüssing 				break;
1933111beedSLinus Lüssing 			case BATADV_HARDIF_BCAST_DUPFWD:
1943111beedSLinus Lüssing 				type = "single neighbor is source";
1953111beedSLinus Lüssing 				break;
1963111beedSLinus Lüssing 			case BATADV_HARDIF_BCAST_DUPORIG:
1973111beedSLinus Lüssing 				type = "single neighbor is originator";
1983111beedSLinus Lüssing 				break;
1993111beedSLinus Lüssing 			default:
2003111beedSLinus Lüssing 				type = "unknown";
2013111beedSLinus Lüssing 			}
2023111beedSLinus Lüssing 
2033111beedSLinus Lüssing 			batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "OGM2 from ourselve on %s surpressed: %s\n",
2043111beedSLinus Lüssing 				   hard_iface->net_dev->name, type);
2053111beedSLinus Lüssing 
2063111beedSLinus Lüssing 			batadv_hardif_put(hard_iface);
2073111beedSLinus Lüssing 			continue;
2083111beedSLinus Lüssing 		}
2093111beedSLinus Lüssing 
2100da00359SAntonio Quartulli 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
2110da00359SAntonio Quartulli 			   "Sending own OGM2 packet (originator %pM, seqno %u, throughput %u, TTL %d) on interface %s [%pM]\n",
2120da00359SAntonio Quartulli 			   ogm_packet->orig, ntohl(ogm_packet->seqno),
2130da00359SAntonio Quartulli 			   ntohl(ogm_packet->throughput), ogm_packet->ttl,
2140da00359SAntonio Quartulli 			   hard_iface->net_dev->name,
2150da00359SAntonio Quartulli 			   hard_iface->net_dev->dev_addr);
2160da00359SAntonio Quartulli 
2170da00359SAntonio Quartulli 		/* this skb gets consumed by batadv_v_ogm_send_to_if() */
2180da00359SAntonio Quartulli 		skb_tmp = skb_clone(skb, GFP_ATOMIC);
21927353446SSven Eckelmann 		if (!skb_tmp) {
22027353446SSven Eckelmann 			batadv_hardif_put(hard_iface);
2210da00359SAntonio Quartulli 			break;
22227353446SSven Eckelmann 		}
2230da00359SAntonio Quartulli 
2240da00359SAntonio Quartulli 		batadv_v_ogm_send_to_if(skb_tmp, hard_iface);
22527353446SSven Eckelmann 		batadv_hardif_put(hard_iface);
2260da00359SAntonio Quartulli 	}
2270da00359SAntonio Quartulli 	rcu_read_unlock();
2280da00359SAntonio Quartulli 
2290da00359SAntonio Quartulli 	consume_skb(skb);
2300da00359SAntonio Quartulli 
2310da00359SAntonio Quartulli reschedule:
2320da00359SAntonio Quartulli 	batadv_v_ogm_start_timer(bat_priv);
2330da00359SAntonio Quartulli out:
2340da00359SAntonio Quartulli 	return;
2350da00359SAntonio Quartulli }
2360da00359SAntonio Quartulli 
2370da00359SAntonio Quartulli /**
2380da00359SAntonio Quartulli  * batadv_v_ogm_iface_enable - prepare an interface for B.A.T.M.A.N. V
2390da00359SAntonio Quartulli  * @hard_iface: the interface to prepare
2400da00359SAntonio Quartulli  *
2410da00359SAntonio Quartulli  * Takes care of scheduling own OGM sending routine for this interface.
2420da00359SAntonio Quartulli  *
2430da00359SAntonio Quartulli  * Return: 0 on success or a negative error code otherwise
2440da00359SAntonio Quartulli  */
2450da00359SAntonio Quartulli int batadv_v_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
2460da00359SAntonio Quartulli {
2470da00359SAntonio Quartulli 	struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
2480da00359SAntonio Quartulli 
2490da00359SAntonio Quartulli 	batadv_v_ogm_start_timer(bat_priv);
2500da00359SAntonio Quartulli 
2510da00359SAntonio Quartulli 	return 0;
2520da00359SAntonio Quartulli }
2530da00359SAntonio Quartulli 
2540da00359SAntonio Quartulli /**
2550da00359SAntonio Quartulli  * batadv_v_ogm_primary_iface_set - set a new primary interface
2560da00359SAntonio Quartulli  * @primary_iface: the new primary interface
2570da00359SAntonio Quartulli  */
2580da00359SAntonio Quartulli void batadv_v_ogm_primary_iface_set(struct batadv_hard_iface *primary_iface)
2590da00359SAntonio Quartulli {
2600da00359SAntonio Quartulli 	struct batadv_priv *bat_priv = netdev_priv(primary_iface->soft_iface);
2610da00359SAntonio Quartulli 	struct batadv_ogm2_packet *ogm_packet;
2620da00359SAntonio Quartulli 
2630da00359SAntonio Quartulli 	if (!bat_priv->bat_v.ogm_buff)
2640da00359SAntonio Quartulli 		return;
2650da00359SAntonio Quartulli 
2660da00359SAntonio Quartulli 	ogm_packet = (struct batadv_ogm2_packet *)bat_priv->bat_v.ogm_buff;
2670da00359SAntonio Quartulli 	ether_addr_copy(ogm_packet->orig, primary_iface->net_dev->dev_addr);
2680da00359SAntonio Quartulli }
2690da00359SAntonio Quartulli 
2700da00359SAntonio Quartulli /**
2719323158eSAntonio Quartulli  * batadv_v_forward_penalty - apply a penalty to the throughput metric forwarded
2729323158eSAntonio Quartulli  *  with B.A.T.M.A.N. V OGMs
2739323158eSAntonio Quartulli  * @bat_priv: the bat priv with all the soft interface information
2749323158eSAntonio Quartulli  * @if_incoming: the interface where the OGM has been received
2759323158eSAntonio Quartulli  * @if_outgoing: the interface where the OGM has to be forwarded to
2769323158eSAntonio Quartulli  * @throughput: the current throughput
2779323158eSAntonio Quartulli  *
2789323158eSAntonio Quartulli  * Apply a penalty on the current throughput metric value based on the
2799323158eSAntonio Quartulli  * characteristic of the interface where the OGM has been received. The return
2809323158eSAntonio Quartulli  * value is computed as follows:
2819323158eSAntonio Quartulli  * - throughput * 50%          if the incoming and outgoing interface are the
2829323158eSAntonio Quartulli  *                             same WiFi interface and the throughput is above
2839323158eSAntonio Quartulli  *                             1MBit/s
2849323158eSAntonio Quartulli  * - throughput                if the outgoing interface is the default
2859323158eSAntonio Quartulli  *                             interface (i.e. this OGM is processed for the
2869323158eSAntonio Quartulli  *                             internal table and not forwarded)
2879323158eSAntonio Quartulli  * - throughput * hop penalty  otherwise
2889323158eSAntonio Quartulli  *
2899323158eSAntonio Quartulli  * Return: the penalised throughput metric.
2909323158eSAntonio Quartulli  */
2919323158eSAntonio Quartulli static u32 batadv_v_forward_penalty(struct batadv_priv *bat_priv,
2929323158eSAntonio Quartulli 				    struct batadv_hard_iface *if_incoming,
2939323158eSAntonio Quartulli 				    struct batadv_hard_iface *if_outgoing,
2949323158eSAntonio Quartulli 				    u32 throughput)
2959323158eSAntonio Quartulli {
2969323158eSAntonio Quartulli 	int hop_penalty = atomic_read(&bat_priv->hop_penalty);
2979323158eSAntonio Quartulli 	int hop_penalty_max = BATADV_TQ_MAX_VALUE;
2989323158eSAntonio Quartulli 
2999323158eSAntonio Quartulli 	/* Don't apply hop penalty in default originator table. */
3009323158eSAntonio Quartulli 	if (if_outgoing == BATADV_IF_DEFAULT)
3019323158eSAntonio Quartulli 		return throughput;
3029323158eSAntonio Quartulli 
3039323158eSAntonio Quartulli 	/* Forwarding on the same WiFi interface cuts the throughput in half
3049323158eSAntonio Quartulli 	 * due to the store & forward characteristics of WIFI.
3059323158eSAntonio Quartulli 	 * Very low throughput values are the exception.
3069323158eSAntonio Quartulli 	 */
3079323158eSAntonio Quartulli 	if ((throughput > 10) &&
3089323158eSAntonio Quartulli 	    (if_incoming == if_outgoing) &&
309c833484eSAntonio Quartulli 	    !(if_incoming->bat_v.flags & BATADV_FULL_DUPLEX))
3109323158eSAntonio Quartulli 		return throughput / 2;
3119323158eSAntonio Quartulli 
3129323158eSAntonio Quartulli 	/* hop penalty of 255 equals 100% */
3139323158eSAntonio Quartulli 	return throughput * (hop_penalty_max - hop_penalty) / hop_penalty_max;
3149323158eSAntonio Quartulli }
3159323158eSAntonio Quartulli 
3169323158eSAntonio Quartulli /**
317efcc9d30SSimon Wunderlich  * batadv_v_ogm_forward - check conditions and forward an OGM to the given
318efcc9d30SSimon Wunderlich  *  outgoing interface
3199323158eSAntonio Quartulli  * @bat_priv: the bat priv with all the soft interface information
3209323158eSAntonio Quartulli  * @ogm_received: previously received OGM to be forwarded
321efcc9d30SSimon Wunderlich  * @orig_node: the originator which has been updated
322efcc9d30SSimon Wunderlich  * @neigh_node: the neigh_node through with the OGM has been received
3239323158eSAntonio Quartulli  * @if_incoming: the interface on which this OGM was received on
3249323158eSAntonio Quartulli  * @if_outgoing: the interface to which the OGM has to be forwarded to
3259323158eSAntonio Quartulli  *
3269323158eSAntonio Quartulli  * Forward an OGM to an interface after having altered the throughput metric and
3279323158eSAntonio Quartulli  * the TTL value contained in it. The original OGM isn't modified.
3289323158eSAntonio Quartulli  */
3299323158eSAntonio Quartulli static void batadv_v_ogm_forward(struct batadv_priv *bat_priv,
3309323158eSAntonio Quartulli 				 const struct batadv_ogm2_packet *ogm_received,
331efcc9d30SSimon Wunderlich 				 struct batadv_orig_node *orig_node,
332efcc9d30SSimon Wunderlich 				 struct batadv_neigh_node *neigh_node,
3339323158eSAntonio Quartulli 				 struct batadv_hard_iface *if_incoming,
3349323158eSAntonio Quartulli 				 struct batadv_hard_iface *if_outgoing)
3359323158eSAntonio Quartulli {
336efcc9d30SSimon Wunderlich 	struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
337efcc9d30SSimon Wunderlich 	struct batadv_orig_ifinfo *orig_ifinfo = NULL;
338efcc9d30SSimon Wunderlich 	struct batadv_neigh_node *router = NULL;
3399323158eSAntonio Quartulli 	struct batadv_ogm2_packet *ogm_forward;
3409323158eSAntonio Quartulli 	unsigned char *skb_buff;
3419323158eSAntonio Quartulli 	struct sk_buff *skb;
3429323158eSAntonio Quartulli 	size_t packet_len;
3439323158eSAntonio Quartulli 	u16 tvlv_len;
3449323158eSAntonio Quartulli 
345efcc9d30SSimon Wunderlich 	/* only forward for specific interfaces, not for the default one. */
346efcc9d30SSimon Wunderlich 	if (if_outgoing == BATADV_IF_DEFAULT)
347efcc9d30SSimon Wunderlich 		goto out;
348efcc9d30SSimon Wunderlich 
349efcc9d30SSimon Wunderlich 	orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
350efcc9d30SSimon Wunderlich 	if (!orig_ifinfo)
351efcc9d30SSimon Wunderlich 		goto out;
352efcc9d30SSimon Wunderlich 
353efcc9d30SSimon Wunderlich 	/* acquire possibly updated router */
354efcc9d30SSimon Wunderlich 	router = batadv_orig_router_get(orig_node, if_outgoing);
355efcc9d30SSimon Wunderlich 
356efcc9d30SSimon Wunderlich 	/* strict rule: forward packets coming from the best next hop only */
357efcc9d30SSimon Wunderlich 	if (neigh_node != router)
358efcc9d30SSimon Wunderlich 		goto out;
359efcc9d30SSimon Wunderlich 
360efcc9d30SSimon Wunderlich 	/* don't forward the same seqno twice on one interface */
361efcc9d30SSimon Wunderlich 	if (orig_ifinfo->last_seqno_forwarded == ntohl(ogm_received->seqno))
362efcc9d30SSimon Wunderlich 		goto out;
363efcc9d30SSimon Wunderlich 
364efcc9d30SSimon Wunderlich 	orig_ifinfo->last_seqno_forwarded = ntohl(ogm_received->seqno);
365efcc9d30SSimon Wunderlich 
3669323158eSAntonio Quartulli 	if (ogm_received->ttl <= 1) {
3679323158eSAntonio Quartulli 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
368efcc9d30SSimon Wunderlich 		goto out;
3699323158eSAntonio Quartulli 	}
3709323158eSAntonio Quartulli 
371efcc9d30SSimon Wunderlich 	neigh_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
372efcc9d30SSimon Wunderlich 	if (!neigh_ifinfo)
373efcc9d30SSimon Wunderlich 		goto out;
374efcc9d30SSimon Wunderlich 
3759323158eSAntonio Quartulli 	tvlv_len = ntohs(ogm_received->tvlv_len);
3769323158eSAntonio Quartulli 
3779323158eSAntonio Quartulli 	packet_len = BATADV_OGM2_HLEN + tvlv_len;
3789323158eSAntonio Quartulli 	skb = netdev_alloc_skb_ip_align(if_outgoing->net_dev,
3799323158eSAntonio Quartulli 					ETH_HLEN + packet_len);
3809323158eSAntonio Quartulli 	if (!skb)
381efcc9d30SSimon Wunderlich 		goto out;
3829323158eSAntonio Quartulli 
3839323158eSAntonio Quartulli 	skb_reserve(skb, ETH_HLEN);
38459ae1d12SJohannes Berg 	skb_buff = skb_put_data(skb, ogm_received, packet_len);
3859323158eSAntonio Quartulli 
3869323158eSAntonio Quartulli 	/* apply forward penalty */
3879323158eSAntonio Quartulli 	ogm_forward = (struct batadv_ogm2_packet *)skb_buff;
388efcc9d30SSimon Wunderlich 	ogm_forward->throughput = htonl(neigh_ifinfo->bat_v.throughput);
3899323158eSAntonio Quartulli 	ogm_forward->ttl--;
3909323158eSAntonio Quartulli 
3919323158eSAntonio Quartulli 	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
3929323158eSAntonio Quartulli 		   "Forwarding OGM2 packet on %s: throughput %u, ttl %u, received via %s\n",
393efcc9d30SSimon Wunderlich 		   if_outgoing->net_dev->name, ntohl(ogm_forward->throughput),
394efcc9d30SSimon Wunderlich 		   ogm_forward->ttl, if_incoming->net_dev->name);
3959323158eSAntonio Quartulli 
3969323158eSAntonio Quartulli 	batadv_v_ogm_send_to_if(skb, if_outgoing);
397efcc9d30SSimon Wunderlich 
398efcc9d30SSimon Wunderlich out:
399efcc9d30SSimon Wunderlich 	if (orig_ifinfo)
400efcc9d30SSimon Wunderlich 		batadv_orig_ifinfo_put(orig_ifinfo);
401efcc9d30SSimon Wunderlich 	if (router)
402efcc9d30SSimon Wunderlich 		batadv_neigh_node_put(router);
403efcc9d30SSimon Wunderlich 	if (neigh_ifinfo)
404efcc9d30SSimon Wunderlich 		batadv_neigh_ifinfo_put(neigh_ifinfo);
4059323158eSAntonio Quartulli }
4069323158eSAntonio Quartulli 
4079323158eSAntonio Quartulli /**
4089323158eSAntonio Quartulli  * batadv_v_ogm_metric_update - update route metric based on OGM
4099323158eSAntonio Quartulli  * @bat_priv: the bat priv with all the soft interface information
4109323158eSAntonio Quartulli  * @ogm2: OGM2 structure
4119323158eSAntonio Quartulli  * @orig_node: Originator structure for which the OGM has been received
4129323158eSAntonio Quartulli  * @neigh_node: the neigh_node through with the OGM has been received
4139323158eSAntonio Quartulli  * @if_incoming: the interface where this packet was received
4149323158eSAntonio Quartulli  * @if_outgoing: the interface for which the packet should be considered
4159323158eSAntonio Quartulli  *
4169323158eSAntonio Quartulli  * Return:
4179323158eSAntonio Quartulli  *  1  if the OGM is new,
4189323158eSAntonio Quartulli  *  0  if it is not new but valid,
4199323158eSAntonio Quartulli  *  <0 on error (e.g. old OGM)
4209323158eSAntonio Quartulli  */
4219323158eSAntonio Quartulli static int batadv_v_ogm_metric_update(struct batadv_priv *bat_priv,
4229323158eSAntonio Quartulli 				      const struct batadv_ogm2_packet *ogm2,
4239323158eSAntonio Quartulli 				      struct batadv_orig_node *orig_node,
4249323158eSAntonio Quartulli 				      struct batadv_neigh_node *neigh_node,
4259323158eSAntonio Quartulli 				      struct batadv_hard_iface *if_incoming,
4269323158eSAntonio Quartulli 				      struct batadv_hard_iface *if_outgoing)
4279323158eSAntonio Quartulli {
428422d2f77SSven Eckelmann 	struct batadv_orig_ifinfo *orig_ifinfo;
4299323158eSAntonio Quartulli 	struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
4309323158eSAntonio Quartulli 	bool protection_started = false;
4319323158eSAntonio Quartulli 	int ret = -EINVAL;
4329323158eSAntonio Quartulli 	u32 path_throughput;
4339323158eSAntonio Quartulli 	s32 seq_diff;
4349323158eSAntonio Quartulli 
4359323158eSAntonio Quartulli 	orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
4369323158eSAntonio Quartulli 	if (!orig_ifinfo)
4379323158eSAntonio Quartulli 		goto out;
4389323158eSAntonio Quartulli 
4399323158eSAntonio Quartulli 	seq_diff = ntohl(ogm2->seqno) - orig_ifinfo->last_real_seqno;
4409323158eSAntonio Quartulli 
4419323158eSAntonio Quartulli 	if (!hlist_empty(&orig_node->neigh_list) &&
4429323158eSAntonio Quartulli 	    batadv_window_protected(bat_priv, seq_diff,
4439323158eSAntonio Quartulli 				    BATADV_OGM_MAX_AGE,
4449323158eSAntonio Quartulli 				    &orig_ifinfo->batman_seqno_reset,
4459323158eSAntonio Quartulli 				    &protection_started)) {
4469323158eSAntonio Quartulli 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
4479323158eSAntonio Quartulli 			   "Drop packet: packet within window protection time from %pM\n",
4489323158eSAntonio Quartulli 			   ogm2->orig);
4499323158eSAntonio Quartulli 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
4509323158eSAntonio Quartulli 			   "Last reset: %ld, %ld\n",
4519323158eSAntonio Quartulli 			   orig_ifinfo->batman_seqno_reset, jiffies);
4529323158eSAntonio Quartulli 		goto out;
4539323158eSAntonio Quartulli 	}
4549323158eSAntonio Quartulli 
4559323158eSAntonio Quartulli 	/* drop packets with old seqnos, however accept the first packet after
4569323158eSAntonio Quartulli 	 * a host has been rebooted.
4579323158eSAntonio Quartulli 	 */
4589323158eSAntonio Quartulli 	if ((seq_diff < 0) && !protection_started)
4599323158eSAntonio Quartulli 		goto out;
4609323158eSAntonio Quartulli 
4619323158eSAntonio Quartulli 	neigh_node->last_seen = jiffies;
4629323158eSAntonio Quartulli 
4639323158eSAntonio Quartulli 	orig_node->last_seen = jiffies;
4649323158eSAntonio Quartulli 
4659323158eSAntonio Quartulli 	orig_ifinfo->last_real_seqno = ntohl(ogm2->seqno);
4669323158eSAntonio Quartulli 	orig_ifinfo->last_ttl = ogm2->ttl;
4679323158eSAntonio Quartulli 
4689323158eSAntonio Quartulli 	neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
4699323158eSAntonio Quartulli 	if (!neigh_ifinfo)
4709323158eSAntonio Quartulli 		goto out;
4719323158eSAntonio Quartulli 
4729323158eSAntonio Quartulli 	path_throughput = batadv_v_forward_penalty(bat_priv, if_incoming,
4739323158eSAntonio Quartulli 						   if_outgoing,
4749323158eSAntonio Quartulli 						   ntohl(ogm2->throughput));
4759323158eSAntonio Quartulli 	neigh_ifinfo->bat_v.throughput = path_throughput;
4769323158eSAntonio Quartulli 	neigh_ifinfo->bat_v.last_seqno = ntohl(ogm2->seqno);
4779323158eSAntonio Quartulli 	neigh_ifinfo->last_ttl = ogm2->ttl;
4789323158eSAntonio Quartulli 
4799323158eSAntonio Quartulli 	if (seq_diff > 0 || protection_started)
4809323158eSAntonio Quartulli 		ret = 1;
4819323158eSAntonio Quartulli 	else
4829323158eSAntonio Quartulli 		ret = 0;
4839323158eSAntonio Quartulli out:
4849323158eSAntonio Quartulli 	if (orig_ifinfo)
4859323158eSAntonio Quartulli 		batadv_orig_ifinfo_put(orig_ifinfo);
4869323158eSAntonio Quartulli 	if (neigh_ifinfo)
4879323158eSAntonio Quartulli 		batadv_neigh_ifinfo_put(neigh_ifinfo);
4889323158eSAntonio Quartulli 
4899323158eSAntonio Quartulli 	return ret;
4909323158eSAntonio Quartulli }
4919323158eSAntonio Quartulli 
4929323158eSAntonio Quartulli /**
4939323158eSAntonio Quartulli  * batadv_v_ogm_route_update - update routes based on OGM
4949323158eSAntonio Quartulli  * @bat_priv: the bat priv with all the soft interface information
4959323158eSAntonio Quartulli  * @ethhdr: the Ethernet header of the OGM2
4969323158eSAntonio Quartulli  * @ogm2: OGM2 structure
4979323158eSAntonio Quartulli  * @orig_node: Originator structure for which the OGM has been received
4989323158eSAntonio Quartulli  * @neigh_node: the neigh_node through with the OGM has been received
4999323158eSAntonio Quartulli  * @if_incoming: the interface where this packet was received
5009323158eSAntonio Quartulli  * @if_outgoing: the interface for which the packet should be considered
501efcc9d30SSimon Wunderlich  *
502efcc9d30SSimon Wunderlich  * Return: true if the packet should be forwarded, false otherwise
5039323158eSAntonio Quartulli  */
504efcc9d30SSimon Wunderlich static bool batadv_v_ogm_route_update(struct batadv_priv *bat_priv,
5059323158eSAntonio Quartulli 				      const struct ethhdr *ethhdr,
5069323158eSAntonio Quartulli 				      const struct batadv_ogm2_packet *ogm2,
5079323158eSAntonio Quartulli 				      struct batadv_orig_node *orig_node,
5089323158eSAntonio Quartulli 				      struct batadv_neigh_node *neigh_node,
5099323158eSAntonio Quartulli 				      struct batadv_hard_iface *if_incoming,
5109323158eSAntonio Quartulli 				      struct batadv_hard_iface *if_outgoing)
5119323158eSAntonio Quartulli {
5129323158eSAntonio Quartulli 	struct batadv_neigh_node *router = NULL;
513422d2f77SSven Eckelmann 	struct batadv_orig_node *orig_neigh_node;
5149323158eSAntonio Quartulli 	struct batadv_neigh_node *orig_neigh_router = NULL;
51586de37c1SSimon Wunderlich 	struct batadv_neigh_ifinfo *router_ifinfo = NULL, *neigh_ifinfo = NULL;
51686de37c1SSimon Wunderlich 	u32 router_throughput, neigh_throughput;
51786de37c1SSimon Wunderlich 	u32 router_last_seqno;
51886de37c1SSimon Wunderlich 	u32 neigh_last_seqno;
51986de37c1SSimon Wunderlich 	s32 neigh_seq_diff;
520efcc9d30SSimon Wunderlich 	bool forward = false;
5219323158eSAntonio Quartulli 
5229323158eSAntonio Quartulli 	orig_neigh_node = batadv_v_ogm_orig_get(bat_priv, ethhdr->h_source);
5239323158eSAntonio Quartulli 	if (!orig_neigh_node)
5249323158eSAntonio Quartulli 		goto out;
5259323158eSAntonio Quartulli 
5269323158eSAntonio Quartulli 	orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
5279323158eSAntonio Quartulli 						   if_outgoing);
5289323158eSAntonio Quartulli 
5299323158eSAntonio Quartulli 	/* drop packet if sender is not a direct neighbor and if we
5309323158eSAntonio Quartulli 	 * don't route towards it
5319323158eSAntonio Quartulli 	 */
5329323158eSAntonio Quartulli 	router = batadv_orig_router_get(orig_node, if_outgoing);
5339323158eSAntonio Quartulli 	if (router && router->orig_node != orig_node && !orig_neigh_router) {
5349323158eSAntonio Quartulli 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
5359323158eSAntonio Quartulli 			   "Drop packet: OGM via unknown neighbor!\n");
5369323158eSAntonio Quartulli 		goto out;
5379323158eSAntonio Quartulli 	}
5389323158eSAntonio Quartulli 
53986de37c1SSimon Wunderlich 	/* Mark the OGM to be considered for forwarding, and update routes
54086de37c1SSimon Wunderlich 	 * if needed.
54186de37c1SSimon Wunderlich 	 */
542efcc9d30SSimon Wunderlich 	forward = true;
54386de37c1SSimon Wunderlich 
54486de37c1SSimon Wunderlich 	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
54586de37c1SSimon Wunderlich 		   "Searching and updating originator entry of received packet\n");
54686de37c1SSimon Wunderlich 
54786de37c1SSimon Wunderlich 	/* if this neighbor already is our next hop there is nothing
54886de37c1SSimon Wunderlich 	 * to change
54986de37c1SSimon Wunderlich 	 */
55086de37c1SSimon Wunderlich 	if (router == neigh_node)
55186de37c1SSimon Wunderlich 		goto out;
55286de37c1SSimon Wunderlich 
55386de37c1SSimon Wunderlich 	/* don't consider neighbours with worse throughput.
55486de37c1SSimon Wunderlich 	 * also switch route if this seqno is BATADV_V_MAX_ORIGDIFF newer than
55586de37c1SSimon Wunderlich 	 * the last received seqno from our best next hop.
55686de37c1SSimon Wunderlich 	 */
55786de37c1SSimon Wunderlich 	if (router) {
55886de37c1SSimon Wunderlich 		router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
55986de37c1SSimon Wunderlich 		neigh_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
56086de37c1SSimon Wunderlich 
56186de37c1SSimon Wunderlich 		/* if these are not allocated, something is wrong. */
56286de37c1SSimon Wunderlich 		if (!router_ifinfo || !neigh_ifinfo)
56386de37c1SSimon Wunderlich 			goto out;
56486de37c1SSimon Wunderlich 
56586de37c1SSimon Wunderlich 		neigh_last_seqno = neigh_ifinfo->bat_v.last_seqno;
56686de37c1SSimon Wunderlich 		router_last_seqno = router_ifinfo->bat_v.last_seqno;
56786de37c1SSimon Wunderlich 		neigh_seq_diff = neigh_last_seqno - router_last_seqno;
56886de37c1SSimon Wunderlich 		router_throughput = router_ifinfo->bat_v.throughput;
56986de37c1SSimon Wunderlich 		neigh_throughput = neigh_ifinfo->bat_v.throughput;
57086de37c1SSimon Wunderlich 
57186de37c1SSimon Wunderlich 		if ((neigh_seq_diff < BATADV_OGM_MAX_ORIGDIFF) &&
57286de37c1SSimon Wunderlich 		    (router_throughput >= neigh_throughput))
57386de37c1SSimon Wunderlich 			goto out;
57486de37c1SSimon Wunderlich 	}
57586de37c1SSimon Wunderlich 
57686de37c1SSimon Wunderlich 	batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
5779323158eSAntonio Quartulli out:
5789323158eSAntonio Quartulli 	if (router)
5799323158eSAntonio Quartulli 		batadv_neigh_node_put(router);
5809323158eSAntonio Quartulli 	if (orig_neigh_router)
5819323158eSAntonio Quartulli 		batadv_neigh_node_put(orig_neigh_router);
5829323158eSAntonio Quartulli 	if (orig_neigh_node)
5839323158eSAntonio Quartulli 		batadv_orig_node_put(orig_neigh_node);
58486de37c1SSimon Wunderlich 	if (router_ifinfo)
58586de37c1SSimon Wunderlich 		batadv_neigh_ifinfo_put(router_ifinfo);
58686de37c1SSimon Wunderlich 	if (neigh_ifinfo)
58786de37c1SSimon Wunderlich 		batadv_neigh_ifinfo_put(neigh_ifinfo);
588efcc9d30SSimon Wunderlich 
589efcc9d30SSimon Wunderlich 	return forward;
5909323158eSAntonio Quartulli }
5919323158eSAntonio Quartulli 
5929323158eSAntonio Quartulli /**
5939323158eSAntonio Quartulli  * batadv_v_ogm_process_per_outif - process a batman v OGM for an outgoing if
5949323158eSAntonio Quartulli  * @bat_priv: the bat priv with all the soft interface information
5959323158eSAntonio Quartulli  * @ethhdr: the Ethernet header of the OGM2
5969323158eSAntonio Quartulli  * @ogm2: OGM2 structure
5979323158eSAntonio Quartulli  * @orig_node: Originator structure for which the OGM has been received
5989323158eSAntonio Quartulli  * @neigh_node: the neigh_node through with the OGM has been received
5999323158eSAntonio Quartulli  * @if_incoming: the interface where this packet was received
6009323158eSAntonio Quartulli  * @if_outgoing: the interface for which the packet should be considered
6019323158eSAntonio Quartulli  */
6029323158eSAntonio Quartulli static void
6039323158eSAntonio Quartulli batadv_v_ogm_process_per_outif(struct batadv_priv *bat_priv,
6049323158eSAntonio Quartulli 			       const struct ethhdr *ethhdr,
6059323158eSAntonio Quartulli 			       const struct batadv_ogm2_packet *ogm2,
6069323158eSAntonio Quartulli 			       struct batadv_orig_node *orig_node,
6079323158eSAntonio Quartulli 			       struct batadv_neigh_node *neigh_node,
6089323158eSAntonio Quartulli 			       struct batadv_hard_iface *if_incoming,
6099323158eSAntonio Quartulli 			       struct batadv_hard_iface *if_outgoing)
6109323158eSAntonio Quartulli {
6119323158eSAntonio Quartulli 	int seqno_age;
612efcc9d30SSimon Wunderlich 	bool forward;
6139323158eSAntonio Quartulli 
6149323158eSAntonio Quartulli 	/* first, update the metric with according sanity checks */
6159323158eSAntonio Quartulli 	seqno_age = batadv_v_ogm_metric_update(bat_priv, ogm2, orig_node,
6169323158eSAntonio Quartulli 					       neigh_node, if_incoming,
6179323158eSAntonio Quartulli 					       if_outgoing);
6189323158eSAntonio Quartulli 
6199323158eSAntonio Quartulli 	/* outdated sequence numbers are to be discarded */
6209323158eSAntonio Quartulli 	if (seqno_age < 0)
6219323158eSAntonio Quartulli 		return;
6229323158eSAntonio Quartulli 
6239323158eSAntonio Quartulli 	/* only unknown & newer OGMs contain TVLVs we are interested in */
6249323158eSAntonio Quartulli 	if ((seqno_age > 0) && (if_outgoing == BATADV_IF_DEFAULT))
6259323158eSAntonio Quartulli 		batadv_tvlv_containers_process(bat_priv, true, orig_node,
6269323158eSAntonio Quartulli 					       NULL, NULL,
6279323158eSAntonio Quartulli 					       (unsigned char *)(ogm2 + 1),
6289323158eSAntonio Quartulli 					       ntohs(ogm2->tvlv_len));
6299323158eSAntonio Quartulli 
6309323158eSAntonio Quartulli 	/* if the metric update went through, update routes if needed */
631efcc9d30SSimon Wunderlich 	forward = batadv_v_ogm_route_update(bat_priv, ethhdr, ogm2, orig_node,
632efcc9d30SSimon Wunderlich 					    neigh_node, if_incoming,
633efcc9d30SSimon Wunderlich 					    if_outgoing);
634efcc9d30SSimon Wunderlich 
635efcc9d30SSimon Wunderlich 	/* if the routes have been processed correctly, check and forward */
636efcc9d30SSimon Wunderlich 	if (forward)
637efcc9d30SSimon Wunderlich 		batadv_v_ogm_forward(bat_priv, ogm2, orig_node, neigh_node,
638efcc9d30SSimon Wunderlich 				     if_incoming, if_outgoing);
6399323158eSAntonio Quartulli }
6409323158eSAntonio Quartulli 
6419323158eSAntonio Quartulli /**
6429323158eSAntonio Quartulli  * batadv_v_ogm_aggr_packet - checks if there is another OGM aggregated
6439323158eSAntonio Quartulli  * @buff_pos: current position in the skb
6449323158eSAntonio Quartulli  * @packet_len: total length of the skb
6459323158eSAntonio Quartulli  * @tvlv_len: tvlv length of the previously considered OGM
6469323158eSAntonio Quartulli  *
6479323158eSAntonio Quartulli  * Return: true if there is enough space for another OGM, false otherwise.
6489323158eSAntonio Quartulli  */
6499323158eSAntonio Quartulli static bool batadv_v_ogm_aggr_packet(int buff_pos, int packet_len,
6509323158eSAntonio Quartulli 				     __be16 tvlv_len)
6519323158eSAntonio Quartulli {
6529323158eSAntonio Quartulli 	int next_buff_pos = 0;
6539323158eSAntonio Quartulli 
6549323158eSAntonio Quartulli 	next_buff_pos += buff_pos + BATADV_OGM2_HLEN;
6559323158eSAntonio Quartulli 	next_buff_pos += ntohs(tvlv_len);
6569323158eSAntonio Quartulli 
6579323158eSAntonio Quartulli 	return (next_buff_pos <= packet_len) &&
6589323158eSAntonio Quartulli 	       (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
6599323158eSAntonio Quartulli }
6609323158eSAntonio Quartulli 
6619323158eSAntonio Quartulli /**
6629323158eSAntonio Quartulli  * batadv_v_ogm_process - process an incoming batman v OGM
6639323158eSAntonio Quartulli  * @skb: the skb containing the OGM
6649323158eSAntonio Quartulli  * @ogm_offset: offset to the OGM which should be processed (for aggregates)
6659323158eSAntonio Quartulli  * @if_incoming: the interface where this packet was receved
6669323158eSAntonio Quartulli  */
6679323158eSAntonio Quartulli static void batadv_v_ogm_process(const struct sk_buff *skb, int ogm_offset,
6689323158eSAntonio Quartulli 				 struct batadv_hard_iface *if_incoming)
6699323158eSAntonio Quartulli {
6709323158eSAntonio Quartulli 	struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
6719323158eSAntonio Quartulli 	struct ethhdr *ethhdr;
6729323158eSAntonio Quartulli 	struct batadv_orig_node *orig_node = NULL;
6739323158eSAntonio Quartulli 	struct batadv_hardif_neigh_node *hardif_neigh = NULL;
6749323158eSAntonio Quartulli 	struct batadv_neigh_node *neigh_node = NULL;
6759323158eSAntonio Quartulli 	struct batadv_hard_iface *hard_iface;
6769323158eSAntonio Quartulli 	struct batadv_ogm2_packet *ogm_packet;
6779323158eSAntonio Quartulli 	u32 ogm_throughput, link_throughput, path_throughput;
6783111beedSLinus Lüssing 	int ret;
6799323158eSAntonio Quartulli 
6809323158eSAntonio Quartulli 	ethhdr = eth_hdr(skb);
6819323158eSAntonio Quartulli 	ogm_packet = (struct batadv_ogm2_packet *)(skb->data + ogm_offset);
6829323158eSAntonio Quartulli 
6839323158eSAntonio Quartulli 	ogm_throughput = ntohl(ogm_packet->throughput);
6849323158eSAntonio Quartulli 
6859323158eSAntonio Quartulli 	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
6869323158eSAntonio Quartulli 		   "Received OGM2 packet via NB: %pM, IF: %s [%pM] (from OG: %pM, seqno %u, troughput %u, TTL %u, V %u, tvlv_len %u)\n",
6879323158eSAntonio Quartulli 		   ethhdr->h_source, if_incoming->net_dev->name,
6889323158eSAntonio Quartulli 		   if_incoming->net_dev->dev_addr, ogm_packet->orig,
6899323158eSAntonio Quartulli 		   ntohl(ogm_packet->seqno), ogm_throughput, ogm_packet->ttl,
6909323158eSAntonio Quartulli 		   ogm_packet->version, ntohs(ogm_packet->tvlv_len));
6919323158eSAntonio Quartulli 
6929323158eSAntonio Quartulli 	/* If the troughput metric is 0, immediately drop the packet. No need to
6939323158eSAntonio Quartulli 	 * create orig_node / neigh_node for an unusable route.
6949323158eSAntonio Quartulli 	 */
6959323158eSAntonio Quartulli 	if (ogm_throughput == 0) {
6969323158eSAntonio Quartulli 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
6979323158eSAntonio Quartulli 			   "Drop packet: originator packet with troughput metric of 0\n");
6989323158eSAntonio Quartulli 		return;
6999323158eSAntonio Quartulli 	}
7009323158eSAntonio Quartulli 
7019323158eSAntonio Quartulli 	/* require ELP packets be to received from this neighbor first */
7029323158eSAntonio Quartulli 	hardif_neigh = batadv_hardif_neigh_get(if_incoming, ethhdr->h_source);
7039323158eSAntonio Quartulli 	if (!hardif_neigh) {
7049323158eSAntonio Quartulli 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
7059323158eSAntonio Quartulli 			   "Drop packet: OGM via unknown neighbor!\n");
7069323158eSAntonio Quartulli 		goto out;
7079323158eSAntonio Quartulli 	}
7089323158eSAntonio Quartulli 
7099323158eSAntonio Quartulli 	orig_node = batadv_v_ogm_orig_get(bat_priv, ogm_packet->orig);
7109323158eSAntonio Quartulli 	if (!orig_node)
7119323158eSAntonio Quartulli 		return;
7129323158eSAntonio Quartulli 
7136f0a6b5eSMarek Lindner 	neigh_node = batadv_neigh_node_get_or_create(orig_node, if_incoming,
7149323158eSAntonio Quartulli 						     ethhdr->h_source);
7159323158eSAntonio Quartulli 	if (!neigh_node)
7169323158eSAntonio Quartulli 		goto out;
7179323158eSAntonio Quartulli 
7189323158eSAntonio Quartulli 	/* Update the received throughput metric to match the link
7199323158eSAntonio Quartulli 	 * characteristic:
7209323158eSAntonio Quartulli 	 *  - If this OGM traveled one hop so far (emitted by single hop
7219323158eSAntonio Quartulli 	 *    neighbor) the path throughput metric equals the link throughput.
7229323158eSAntonio Quartulli 	 *  - For OGMs traversing more than hop the path throughput metric is
7239323158eSAntonio Quartulli 	 *    the smaller of the path throughput and the link throughput.
7249323158eSAntonio Quartulli 	 */
7259323158eSAntonio Quartulli 	link_throughput = ewma_throughput_read(&hardif_neigh->bat_v.throughput);
7269323158eSAntonio Quartulli 	path_throughput = min_t(u32, link_throughput, ogm_throughput);
7279323158eSAntonio Quartulli 	ogm_packet->throughput = htonl(path_throughput);
7289323158eSAntonio Quartulli 
7299323158eSAntonio Quartulli 	batadv_v_ogm_process_per_outif(bat_priv, ethhdr, ogm_packet, orig_node,
7309323158eSAntonio Quartulli 				       neigh_node, if_incoming,
7319323158eSAntonio Quartulli 				       BATADV_IF_DEFAULT);
7329323158eSAntonio Quartulli 
7339323158eSAntonio Quartulli 	rcu_read_lock();
7349323158eSAntonio Quartulli 	list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
7359323158eSAntonio Quartulli 		if (hard_iface->if_status != BATADV_IF_ACTIVE)
7369323158eSAntonio Quartulli 			continue;
7379323158eSAntonio Quartulli 
7389323158eSAntonio Quartulli 		if (hard_iface->soft_iface != bat_priv->soft_iface)
7399323158eSAntonio Quartulli 			continue;
7409323158eSAntonio Quartulli 
74127353446SSven Eckelmann 		if (!kref_get_unless_zero(&hard_iface->refcount))
74227353446SSven Eckelmann 			continue;
74327353446SSven Eckelmann 
7443111beedSLinus Lüssing 		ret = batadv_hardif_no_broadcast(hard_iface,
7453111beedSLinus Lüssing 						 ogm_packet->orig,
7463111beedSLinus Lüssing 						 hardif_neigh->orig);
7473111beedSLinus Lüssing 
7483111beedSLinus Lüssing 		if (ret) {
7493111beedSLinus Lüssing 			char *type;
7503111beedSLinus Lüssing 
7513111beedSLinus Lüssing 			switch (ret) {
7523111beedSLinus Lüssing 			case BATADV_HARDIF_BCAST_NORECIPIENT:
7533111beedSLinus Lüssing 				type = "no neighbor";
7543111beedSLinus Lüssing 				break;
7553111beedSLinus Lüssing 			case BATADV_HARDIF_BCAST_DUPFWD:
7563111beedSLinus Lüssing 				type = "single neighbor is source";
7573111beedSLinus Lüssing 				break;
7583111beedSLinus Lüssing 			case BATADV_HARDIF_BCAST_DUPORIG:
7593111beedSLinus Lüssing 				type = "single neighbor is originator";
7603111beedSLinus Lüssing 				break;
7613111beedSLinus Lüssing 			default:
7623111beedSLinus Lüssing 				type = "unknown";
7633111beedSLinus Lüssing 			}
7643111beedSLinus Lüssing 
7653111beedSLinus Lüssing 			batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "OGM2 packet from %pM on %s surpressed: %s\n",
7663111beedSLinus Lüssing 				   ogm_packet->orig, hard_iface->net_dev->name,
7673111beedSLinus Lüssing 				   type);
7683111beedSLinus Lüssing 
7693111beedSLinus Lüssing 			batadv_hardif_put(hard_iface);
7703111beedSLinus Lüssing 			continue;
7713111beedSLinus Lüssing 		}
7723111beedSLinus Lüssing 
7739323158eSAntonio Quartulli 		batadv_v_ogm_process_per_outif(bat_priv, ethhdr, ogm_packet,
7749323158eSAntonio Quartulli 					       orig_node, neigh_node,
7759323158eSAntonio Quartulli 					       if_incoming, hard_iface);
77627353446SSven Eckelmann 
77727353446SSven Eckelmann 		batadv_hardif_put(hard_iface);
7789323158eSAntonio Quartulli 	}
7799323158eSAntonio Quartulli 	rcu_read_unlock();
7809323158eSAntonio Quartulli out:
7819323158eSAntonio Quartulli 	if (orig_node)
7829323158eSAntonio Quartulli 		batadv_orig_node_put(orig_node);
7839323158eSAntonio Quartulli 	if (neigh_node)
7849323158eSAntonio Quartulli 		batadv_neigh_node_put(neigh_node);
7859323158eSAntonio Quartulli 	if (hardif_neigh)
7869323158eSAntonio Quartulli 		batadv_hardif_neigh_put(hardif_neigh);
7879323158eSAntonio Quartulli }
7889323158eSAntonio Quartulli 
7899323158eSAntonio Quartulli /**
7900da00359SAntonio Quartulli  * batadv_v_ogm_packet_recv - OGM2 receiving handler
7910da00359SAntonio Quartulli  * @skb: the received OGM
7920da00359SAntonio Quartulli  * @if_incoming: the interface where this OGM has been received
7930da00359SAntonio Quartulli  *
7940da00359SAntonio Quartulli  * Return: NET_RX_SUCCESS and consume the skb on success or returns NET_RX_DROP
7950da00359SAntonio Quartulli  * (without freeing the skb) on failure
7960da00359SAntonio Quartulli  */
7970da00359SAntonio Quartulli int batadv_v_ogm_packet_recv(struct sk_buff *skb,
7980da00359SAntonio Quartulli 			     struct batadv_hard_iface *if_incoming)
7990da00359SAntonio Quartulli {
8000da00359SAntonio Quartulli 	struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
8010da00359SAntonio Quartulli 	struct batadv_ogm2_packet *ogm_packet;
8020da00359SAntonio Quartulli 	struct ethhdr *ethhdr = eth_hdr(skb);
8039323158eSAntonio Quartulli 	int ogm_offset;
8049323158eSAntonio Quartulli 	u8 *packet_pos;
8059323158eSAntonio Quartulli 	int ret = NET_RX_DROP;
8060da00359SAntonio Quartulli 
8070da00359SAntonio Quartulli 	/* did we receive a OGM2 packet on an interface that does not have
8080da00359SAntonio Quartulli 	 * B.A.T.M.A.N. V enabled ?
8090da00359SAntonio Quartulli 	 */
81029824a55SAntonio Quartulli 	if (strcmp(bat_priv->algo_ops->name, "BATMAN_V") != 0)
811b91a2543SSven Eckelmann 		goto free_skb;
8120da00359SAntonio Quartulli 
8130da00359SAntonio Quartulli 	if (!batadv_check_management_packet(skb, if_incoming, BATADV_OGM2_HLEN))
814b91a2543SSven Eckelmann 		goto free_skb;
8150da00359SAntonio Quartulli 
8160da00359SAntonio Quartulli 	if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
817b91a2543SSven Eckelmann 		goto free_skb;
8180da00359SAntonio Quartulli 
8190da00359SAntonio Quartulli 	ogm_packet = (struct batadv_ogm2_packet *)skb->data;
8200da00359SAntonio Quartulli 
8210da00359SAntonio Quartulli 	if (batadv_is_my_mac(bat_priv, ogm_packet->orig))
822b91a2543SSven Eckelmann 		goto free_skb;
8230da00359SAntonio Quartulli 
8240da00359SAntonio Quartulli 	batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
8250da00359SAntonio Quartulli 	batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
8260da00359SAntonio Quartulli 			   skb->len + ETH_HLEN);
8270da00359SAntonio Quartulli 
8289323158eSAntonio Quartulli 	ogm_offset = 0;
8299323158eSAntonio Quartulli 	ogm_packet = (struct batadv_ogm2_packet *)skb->data;
8309323158eSAntonio Quartulli 
8319323158eSAntonio Quartulli 	while (batadv_v_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
8329323158eSAntonio Quartulli 					ogm_packet->tvlv_len)) {
8339323158eSAntonio Quartulli 		batadv_v_ogm_process(skb, ogm_offset, if_incoming);
8349323158eSAntonio Quartulli 
8359323158eSAntonio Quartulli 		ogm_offset += BATADV_OGM2_HLEN;
8369323158eSAntonio Quartulli 		ogm_offset += ntohs(ogm_packet->tvlv_len);
8379323158eSAntonio Quartulli 
8389323158eSAntonio Quartulli 		packet_pos = skb->data + ogm_offset;
8399323158eSAntonio Quartulli 		ogm_packet = (struct batadv_ogm2_packet *)packet_pos;
8409323158eSAntonio Quartulli 	}
8419323158eSAntonio Quartulli 
8429323158eSAntonio Quartulli 	ret = NET_RX_SUCCESS;
843b91a2543SSven Eckelmann 
844b91a2543SSven Eckelmann free_skb:
845b91a2543SSven Eckelmann 	if (ret == NET_RX_SUCCESS)
8460da00359SAntonio Quartulli 		consume_skb(skb);
847b91a2543SSven Eckelmann 	else
848b91a2543SSven Eckelmann 		kfree_skb(skb);
8499323158eSAntonio Quartulli 
8509323158eSAntonio Quartulli 	return ret;
8510da00359SAntonio Quartulli }
8520da00359SAntonio Quartulli 
8530da00359SAntonio Quartulli /**
8540da00359SAntonio Quartulli  * batadv_v_ogm_init - initialise the OGM2 engine
8550da00359SAntonio Quartulli  * @bat_priv: the bat priv with all the soft interface information
8560da00359SAntonio Quartulli  *
8570da00359SAntonio Quartulli  * Return: 0 on success or a negative error code in case of failure
8580da00359SAntonio Quartulli  */
8590da00359SAntonio Quartulli int batadv_v_ogm_init(struct batadv_priv *bat_priv)
8600da00359SAntonio Quartulli {
8610da00359SAntonio Quartulli 	struct batadv_ogm2_packet *ogm_packet;
8620da00359SAntonio Quartulli 	unsigned char *ogm_buff;
8630da00359SAntonio Quartulli 	u32 random_seqno;
8640da00359SAntonio Quartulli 
8650da00359SAntonio Quartulli 	bat_priv->bat_v.ogm_buff_len = BATADV_OGM2_HLEN;
8660da00359SAntonio Quartulli 	ogm_buff = kzalloc(bat_priv->bat_v.ogm_buff_len, GFP_ATOMIC);
8670da00359SAntonio Quartulli 	if (!ogm_buff)
8680da00359SAntonio Quartulli 		return -ENOMEM;
8690da00359SAntonio Quartulli 
8700da00359SAntonio Quartulli 	bat_priv->bat_v.ogm_buff = ogm_buff;
8710da00359SAntonio Quartulli 	ogm_packet = (struct batadv_ogm2_packet *)ogm_buff;
8720da00359SAntonio Quartulli 	ogm_packet->packet_type = BATADV_OGM2;
8730da00359SAntonio Quartulli 	ogm_packet->version = BATADV_COMPAT_VERSION;
8740da00359SAntonio Quartulli 	ogm_packet->ttl = BATADV_TTL;
8750da00359SAntonio Quartulli 	ogm_packet->flags = BATADV_NO_FLAGS;
8760da00359SAntonio Quartulli 	ogm_packet->throughput = htonl(BATADV_THROUGHPUT_MAX_VALUE);
8770da00359SAntonio Quartulli 
8780da00359SAntonio Quartulli 	/* randomize initial seqno to avoid collision */
8790da00359SAntonio Quartulli 	get_random_bytes(&random_seqno, sizeof(random_seqno));
8800da00359SAntonio Quartulli 	atomic_set(&bat_priv->bat_v.ogm_seqno, random_seqno);
8810da00359SAntonio Quartulli 	INIT_DELAYED_WORK(&bat_priv->bat_v.ogm_wq, batadv_v_ogm_send);
8820da00359SAntonio Quartulli 
8830da00359SAntonio Quartulli 	return 0;
8840da00359SAntonio Quartulli }
8850da00359SAntonio Quartulli 
8860da00359SAntonio Quartulli /**
8870da00359SAntonio Quartulli  * batadv_v_ogm_free - free OGM private resources
8880da00359SAntonio Quartulli  * @bat_priv: the bat priv with all the soft interface information
8890da00359SAntonio Quartulli  */
8900da00359SAntonio Quartulli void batadv_v_ogm_free(struct batadv_priv *bat_priv)
8910da00359SAntonio Quartulli {
8920da00359SAntonio Quartulli 	cancel_delayed_work_sync(&bat_priv->bat_v.ogm_wq);
8930da00359SAntonio Quartulli 
8940da00359SAntonio Quartulli 	kfree(bat_priv->bat_v.ogm_buff);
8950da00359SAntonio Quartulli 	bat_priv->bat_v.ogm_buff = NULL;
8960da00359SAntonio Quartulli 	bat_priv->bat_v.ogm_buff_len = 0;
8970da00359SAntonio Quartulli }
898