xref: /linux/net/batman-adv/bat_v_ogm.c (revision 01d350d14712d1e8dbf2b00c82d2fc7c48d34e04)
10da00359SAntonio Quartulli /* Copyright (C) 2013-2016 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 
42*01d350d1SSven Eckelmann #include "bat_algo.h"
430da00359SAntonio Quartulli #include "hard-interface.h"
449323158eSAntonio Quartulli #include "hash.h"
459323158eSAntonio Quartulli #include "originator.h"
460da00359SAntonio Quartulli #include "packet.h"
470da00359SAntonio Quartulli #include "routing.h"
480da00359SAntonio Quartulli #include "send.h"
490da00359SAntonio Quartulli #include "translation-table.h"
501f8dce49SMarkus Pargmann #include "tvlv.h"
510da00359SAntonio Quartulli 
520da00359SAntonio Quartulli /**
539323158eSAntonio Quartulli  * batadv_v_ogm_orig_get - retrieve and possibly create an originator node
549323158eSAntonio Quartulli  * @bat_priv: the bat priv with all the soft interface information
559323158eSAntonio Quartulli  * @addr: the address of the originator
569323158eSAntonio Quartulli  *
579323158eSAntonio Quartulli  * Return: the orig_node corresponding to the specified address. If such object
589323158eSAntonio Quartulli  * does not exist it is allocated here. In case of allocation failure returns
599323158eSAntonio Quartulli  * NULL.
609323158eSAntonio Quartulli  */
619323158eSAntonio Quartulli struct batadv_orig_node *batadv_v_ogm_orig_get(struct batadv_priv *bat_priv,
629323158eSAntonio Quartulli 					       const u8 *addr)
639323158eSAntonio Quartulli {
649323158eSAntonio Quartulli 	struct batadv_orig_node *orig_node;
659323158eSAntonio Quartulli 	int hash_added;
669323158eSAntonio Quartulli 
679323158eSAntonio Quartulli 	orig_node = batadv_orig_hash_find(bat_priv, addr);
689323158eSAntonio Quartulli 	if (orig_node)
699323158eSAntonio Quartulli 		return orig_node;
709323158eSAntonio Quartulli 
719323158eSAntonio Quartulli 	orig_node = batadv_orig_node_new(bat_priv, addr);
729323158eSAntonio Quartulli 	if (!orig_node)
739323158eSAntonio Quartulli 		return NULL;
749323158eSAntonio Quartulli 
759323158eSAntonio Quartulli 	hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
769323158eSAntonio Quartulli 				     batadv_choose_orig, orig_node,
779323158eSAntonio Quartulli 				     &orig_node->hash_entry);
789323158eSAntonio Quartulli 	if (hash_added != 0) {
799323158eSAntonio Quartulli 		/* orig_node->refcounter is initialised to 2 by
809323158eSAntonio Quartulli 		 * batadv_orig_node_new()
819323158eSAntonio Quartulli 		 */
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;
1400da00359SAntonio Quartulli 	unsigned char *ogm_buff, *pkt_buff;
1410da00359SAntonio Quartulli 	int ogm_buff_len;
1420da00359SAntonio Quartulli 	u16 tvlv_len = 0;
1430da00359SAntonio Quartulli 
1440da00359SAntonio Quartulli 	bat_v = container_of(work, struct batadv_priv_bat_v, ogm_wq.work);
1450da00359SAntonio Quartulli 	bat_priv = container_of(bat_v, struct batadv_priv, bat_v);
1460da00359SAntonio Quartulli 
1470da00359SAntonio Quartulli 	if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
1480da00359SAntonio Quartulli 		goto out;
1490da00359SAntonio Quartulli 
1500da00359SAntonio Quartulli 	ogm_buff = bat_priv->bat_v.ogm_buff;
1510da00359SAntonio Quartulli 	ogm_buff_len = bat_priv->bat_v.ogm_buff_len;
1520da00359SAntonio Quartulli 	/* tt changes have to be committed before the tvlv data is
1530da00359SAntonio Quartulli 	 * appended as it may alter the tt tvlv container
1540da00359SAntonio Quartulli 	 */
1550da00359SAntonio Quartulli 	batadv_tt_local_commit_changes(bat_priv);
1560da00359SAntonio Quartulli 	tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, &ogm_buff,
1570da00359SAntonio Quartulli 						    &ogm_buff_len,
1580da00359SAntonio Quartulli 						    BATADV_OGM2_HLEN);
1590da00359SAntonio Quartulli 
1600da00359SAntonio Quartulli 	bat_priv->bat_v.ogm_buff = ogm_buff;
1610da00359SAntonio Quartulli 	bat_priv->bat_v.ogm_buff_len = ogm_buff_len;
1620da00359SAntonio Quartulli 
1630da00359SAntonio Quartulli 	skb = netdev_alloc_skb_ip_align(NULL, ETH_HLEN + ogm_buff_len);
1640da00359SAntonio Quartulli 	if (!skb)
1650da00359SAntonio Quartulli 		goto reschedule;
1660da00359SAntonio Quartulli 
1670da00359SAntonio Quartulli 	skb_reserve(skb, ETH_HLEN);
1680da00359SAntonio Quartulli 	pkt_buff = skb_put(skb, ogm_buff_len);
1690da00359SAntonio Quartulli 	memcpy(pkt_buff, 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 
1850da00359SAntonio Quartulli 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1860da00359SAntonio Quartulli 			   "Sending own OGM2 packet (originator %pM, seqno %u, throughput %u, TTL %d) on interface %s [%pM]\n",
1870da00359SAntonio Quartulli 			   ogm_packet->orig, ntohl(ogm_packet->seqno),
1880da00359SAntonio Quartulli 			   ntohl(ogm_packet->throughput), ogm_packet->ttl,
1890da00359SAntonio Quartulli 			   hard_iface->net_dev->name,
1900da00359SAntonio Quartulli 			   hard_iface->net_dev->dev_addr);
1910da00359SAntonio Quartulli 
1920da00359SAntonio Quartulli 		/* this skb gets consumed by batadv_v_ogm_send_to_if() */
1930da00359SAntonio Quartulli 		skb_tmp = skb_clone(skb, GFP_ATOMIC);
19427353446SSven Eckelmann 		if (!skb_tmp) {
19527353446SSven Eckelmann 			batadv_hardif_put(hard_iface);
1960da00359SAntonio Quartulli 			break;
19727353446SSven Eckelmann 		}
1980da00359SAntonio Quartulli 
1990da00359SAntonio Quartulli 		batadv_v_ogm_send_to_if(skb_tmp, hard_iface);
20027353446SSven Eckelmann 		batadv_hardif_put(hard_iface);
2010da00359SAntonio Quartulli 	}
2020da00359SAntonio Quartulli 	rcu_read_unlock();
2030da00359SAntonio Quartulli 
2040da00359SAntonio Quartulli 	consume_skb(skb);
2050da00359SAntonio Quartulli 
2060da00359SAntonio Quartulli reschedule:
2070da00359SAntonio Quartulli 	batadv_v_ogm_start_timer(bat_priv);
2080da00359SAntonio Quartulli out:
2090da00359SAntonio Quartulli 	return;
2100da00359SAntonio Quartulli }
2110da00359SAntonio Quartulli 
2120da00359SAntonio Quartulli /**
2130da00359SAntonio Quartulli  * batadv_v_ogm_iface_enable - prepare an interface for B.A.T.M.A.N. V
2140da00359SAntonio Quartulli  * @hard_iface: the interface to prepare
2150da00359SAntonio Quartulli  *
2160da00359SAntonio Quartulli  * Takes care of scheduling own OGM sending routine for this interface.
2170da00359SAntonio Quartulli  *
2180da00359SAntonio Quartulli  * Return: 0 on success or a negative error code otherwise
2190da00359SAntonio Quartulli  */
2200da00359SAntonio Quartulli int batadv_v_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
2210da00359SAntonio Quartulli {
2220da00359SAntonio Quartulli 	struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
2230da00359SAntonio Quartulli 
2240da00359SAntonio Quartulli 	batadv_v_ogm_start_timer(bat_priv);
2250da00359SAntonio Quartulli 
2260da00359SAntonio Quartulli 	return 0;
2270da00359SAntonio Quartulli }
2280da00359SAntonio Quartulli 
2290da00359SAntonio Quartulli /**
2300da00359SAntonio Quartulli  * batadv_v_ogm_primary_iface_set - set a new primary interface
2310da00359SAntonio Quartulli  * @primary_iface: the new primary interface
2320da00359SAntonio Quartulli  */
2330da00359SAntonio Quartulli void batadv_v_ogm_primary_iface_set(struct batadv_hard_iface *primary_iface)
2340da00359SAntonio Quartulli {
2350da00359SAntonio Quartulli 	struct batadv_priv *bat_priv = netdev_priv(primary_iface->soft_iface);
2360da00359SAntonio Quartulli 	struct batadv_ogm2_packet *ogm_packet;
2370da00359SAntonio Quartulli 
2380da00359SAntonio Quartulli 	if (!bat_priv->bat_v.ogm_buff)
2390da00359SAntonio Quartulli 		return;
2400da00359SAntonio Quartulli 
2410da00359SAntonio Quartulli 	ogm_packet = (struct batadv_ogm2_packet *)bat_priv->bat_v.ogm_buff;
2420da00359SAntonio Quartulli 	ether_addr_copy(ogm_packet->orig, primary_iface->net_dev->dev_addr);
2430da00359SAntonio Quartulli }
2440da00359SAntonio Quartulli 
2450da00359SAntonio Quartulli /**
2469323158eSAntonio Quartulli  * batadv_v_forward_penalty - apply a penalty to the throughput metric forwarded
2479323158eSAntonio Quartulli  *  with B.A.T.M.A.N. V OGMs
2489323158eSAntonio Quartulli  * @bat_priv: the bat priv with all the soft interface information
2499323158eSAntonio Quartulli  * @if_incoming: the interface where the OGM has been received
2509323158eSAntonio Quartulli  * @if_outgoing: the interface where the OGM has to be forwarded to
2519323158eSAntonio Quartulli  * @throughput: the current throughput
2529323158eSAntonio Quartulli  *
2539323158eSAntonio Quartulli  * Apply a penalty on the current throughput metric value based on the
2549323158eSAntonio Quartulli  * characteristic of the interface where the OGM has been received. The return
2559323158eSAntonio Quartulli  * value is computed as follows:
2569323158eSAntonio Quartulli  * - throughput * 50%          if the incoming and outgoing interface are the
2579323158eSAntonio Quartulli  *                             same WiFi interface and the throughput is above
2589323158eSAntonio Quartulli  *                             1MBit/s
2599323158eSAntonio Quartulli  * - throughput                if the outgoing interface is the default
2609323158eSAntonio Quartulli  *                             interface (i.e. this OGM is processed for the
2619323158eSAntonio Quartulli  *                             internal table and not forwarded)
2629323158eSAntonio Quartulli  * - throughput * hop penalty  otherwise
2639323158eSAntonio Quartulli  *
2649323158eSAntonio Quartulli  * Return: the penalised throughput metric.
2659323158eSAntonio Quartulli  */
2669323158eSAntonio Quartulli static u32 batadv_v_forward_penalty(struct batadv_priv *bat_priv,
2679323158eSAntonio Quartulli 				    struct batadv_hard_iface *if_incoming,
2689323158eSAntonio Quartulli 				    struct batadv_hard_iface *if_outgoing,
2699323158eSAntonio Quartulli 				    u32 throughput)
2709323158eSAntonio Quartulli {
2719323158eSAntonio Quartulli 	int hop_penalty = atomic_read(&bat_priv->hop_penalty);
2729323158eSAntonio Quartulli 	int hop_penalty_max = BATADV_TQ_MAX_VALUE;
2739323158eSAntonio Quartulli 
2749323158eSAntonio Quartulli 	/* Don't apply hop penalty in default originator table. */
2759323158eSAntonio Quartulli 	if (if_outgoing == BATADV_IF_DEFAULT)
2769323158eSAntonio Quartulli 		return throughput;
2779323158eSAntonio Quartulli 
2789323158eSAntonio Quartulli 	/* Forwarding on the same WiFi interface cuts the throughput in half
2799323158eSAntonio Quartulli 	 * due to the store & forward characteristics of WIFI.
2809323158eSAntonio Quartulli 	 * Very low throughput values are the exception.
2819323158eSAntonio Quartulli 	 */
2829323158eSAntonio Quartulli 	if ((throughput > 10) &&
2839323158eSAntonio Quartulli 	    (if_incoming == if_outgoing) &&
284c833484eSAntonio Quartulli 	    !(if_incoming->bat_v.flags & BATADV_FULL_DUPLEX))
2859323158eSAntonio Quartulli 		return throughput / 2;
2869323158eSAntonio Quartulli 
2879323158eSAntonio Quartulli 	/* hop penalty of 255 equals 100% */
2889323158eSAntonio Quartulli 	return throughput * (hop_penalty_max - hop_penalty) / hop_penalty_max;
2899323158eSAntonio Quartulli }
2909323158eSAntonio Quartulli 
2919323158eSAntonio Quartulli /**
292efcc9d30SSimon Wunderlich  * batadv_v_ogm_forward - check conditions and forward an OGM to the given
293efcc9d30SSimon Wunderlich  *  outgoing interface
2949323158eSAntonio Quartulli  * @bat_priv: the bat priv with all the soft interface information
2959323158eSAntonio Quartulli  * @ogm_received: previously received OGM to be forwarded
296efcc9d30SSimon Wunderlich  * @orig_node: the originator which has been updated
297efcc9d30SSimon Wunderlich  * @neigh_node: the neigh_node through with the OGM has been received
2989323158eSAntonio Quartulli  * @if_incoming: the interface on which this OGM was received on
2999323158eSAntonio Quartulli  * @if_outgoing: the interface to which the OGM has to be forwarded to
3009323158eSAntonio Quartulli  *
3019323158eSAntonio Quartulli  * Forward an OGM to an interface after having altered the throughput metric and
3029323158eSAntonio Quartulli  * the TTL value contained in it. The original OGM isn't modified.
3039323158eSAntonio Quartulli  */
3049323158eSAntonio Quartulli static void batadv_v_ogm_forward(struct batadv_priv *bat_priv,
3059323158eSAntonio Quartulli 				 const struct batadv_ogm2_packet *ogm_received,
306efcc9d30SSimon Wunderlich 				 struct batadv_orig_node *orig_node,
307efcc9d30SSimon Wunderlich 				 struct batadv_neigh_node *neigh_node,
3089323158eSAntonio Quartulli 				 struct batadv_hard_iface *if_incoming,
3099323158eSAntonio Quartulli 				 struct batadv_hard_iface *if_outgoing)
3109323158eSAntonio Quartulli {
311efcc9d30SSimon Wunderlich 	struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
312efcc9d30SSimon Wunderlich 	struct batadv_orig_ifinfo *orig_ifinfo = NULL;
313efcc9d30SSimon Wunderlich 	struct batadv_neigh_node *router = NULL;
3149323158eSAntonio Quartulli 	struct batadv_ogm2_packet *ogm_forward;
3159323158eSAntonio Quartulli 	unsigned char *skb_buff;
3169323158eSAntonio Quartulli 	struct sk_buff *skb;
3179323158eSAntonio Quartulli 	size_t packet_len;
3189323158eSAntonio Quartulli 	u16 tvlv_len;
3199323158eSAntonio Quartulli 
320efcc9d30SSimon Wunderlich 	/* only forward for specific interfaces, not for the default one. */
321efcc9d30SSimon Wunderlich 	if (if_outgoing == BATADV_IF_DEFAULT)
322efcc9d30SSimon Wunderlich 		goto out;
323efcc9d30SSimon Wunderlich 
324efcc9d30SSimon Wunderlich 	orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
325efcc9d30SSimon Wunderlich 	if (!orig_ifinfo)
326efcc9d30SSimon Wunderlich 		goto out;
327efcc9d30SSimon Wunderlich 
328efcc9d30SSimon Wunderlich 	/* acquire possibly updated router */
329efcc9d30SSimon Wunderlich 	router = batadv_orig_router_get(orig_node, if_outgoing);
330efcc9d30SSimon Wunderlich 
331efcc9d30SSimon Wunderlich 	/* strict rule: forward packets coming from the best next hop only */
332efcc9d30SSimon Wunderlich 	if (neigh_node != router)
333efcc9d30SSimon Wunderlich 		goto out;
334efcc9d30SSimon Wunderlich 
335efcc9d30SSimon Wunderlich 	/* don't forward the same seqno twice on one interface */
336efcc9d30SSimon Wunderlich 	if (orig_ifinfo->last_seqno_forwarded == ntohl(ogm_received->seqno))
337efcc9d30SSimon Wunderlich 		goto out;
338efcc9d30SSimon Wunderlich 
339efcc9d30SSimon Wunderlich 	orig_ifinfo->last_seqno_forwarded = ntohl(ogm_received->seqno);
340efcc9d30SSimon Wunderlich 
3419323158eSAntonio Quartulli 	if (ogm_received->ttl <= 1) {
3429323158eSAntonio Quartulli 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
343efcc9d30SSimon Wunderlich 		goto out;
3449323158eSAntonio Quartulli 	}
3459323158eSAntonio Quartulli 
346efcc9d30SSimon Wunderlich 	neigh_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
347efcc9d30SSimon Wunderlich 	if (!neigh_ifinfo)
348efcc9d30SSimon Wunderlich 		goto out;
349efcc9d30SSimon Wunderlich 
3509323158eSAntonio Quartulli 	tvlv_len = ntohs(ogm_received->tvlv_len);
3519323158eSAntonio Quartulli 
3529323158eSAntonio Quartulli 	packet_len = BATADV_OGM2_HLEN + tvlv_len;
3539323158eSAntonio Quartulli 	skb = netdev_alloc_skb_ip_align(if_outgoing->net_dev,
3549323158eSAntonio Quartulli 					ETH_HLEN + packet_len);
3559323158eSAntonio Quartulli 	if (!skb)
356efcc9d30SSimon Wunderlich 		goto out;
3579323158eSAntonio Quartulli 
3589323158eSAntonio Quartulli 	skb_reserve(skb, ETH_HLEN);
3599323158eSAntonio Quartulli 	skb_buff = skb_put(skb, packet_len);
3609323158eSAntonio Quartulli 	memcpy(skb_buff, ogm_received, packet_len);
3619323158eSAntonio Quartulli 
3629323158eSAntonio Quartulli 	/* apply forward penalty */
3639323158eSAntonio Quartulli 	ogm_forward = (struct batadv_ogm2_packet *)skb_buff;
364efcc9d30SSimon Wunderlich 	ogm_forward->throughput = htonl(neigh_ifinfo->bat_v.throughput);
3659323158eSAntonio Quartulli 	ogm_forward->ttl--;
3669323158eSAntonio Quartulli 
3679323158eSAntonio Quartulli 	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
3689323158eSAntonio Quartulli 		   "Forwarding OGM2 packet on %s: throughput %u, ttl %u, received via %s\n",
369efcc9d30SSimon Wunderlich 		   if_outgoing->net_dev->name, ntohl(ogm_forward->throughput),
370efcc9d30SSimon Wunderlich 		   ogm_forward->ttl, if_incoming->net_dev->name);
3719323158eSAntonio Quartulli 
3729323158eSAntonio Quartulli 	batadv_v_ogm_send_to_if(skb, if_outgoing);
373efcc9d30SSimon Wunderlich 
374efcc9d30SSimon Wunderlich out:
375efcc9d30SSimon Wunderlich 	if (orig_ifinfo)
376efcc9d30SSimon Wunderlich 		batadv_orig_ifinfo_put(orig_ifinfo);
377efcc9d30SSimon Wunderlich 	if (router)
378efcc9d30SSimon Wunderlich 		batadv_neigh_node_put(router);
379efcc9d30SSimon Wunderlich 	if (neigh_ifinfo)
380efcc9d30SSimon Wunderlich 		batadv_neigh_ifinfo_put(neigh_ifinfo);
3819323158eSAntonio Quartulli }
3829323158eSAntonio Quartulli 
3839323158eSAntonio Quartulli /**
3849323158eSAntonio Quartulli  * batadv_v_ogm_metric_update - update route metric based on OGM
3859323158eSAntonio Quartulli  * @bat_priv: the bat priv with all the soft interface information
3869323158eSAntonio Quartulli  * @ogm2: OGM2 structure
3879323158eSAntonio Quartulli  * @orig_node: Originator structure for which the OGM has been received
3889323158eSAntonio Quartulli  * @neigh_node: the neigh_node through with the OGM has been received
3899323158eSAntonio Quartulli  * @if_incoming: the interface where this packet was received
3909323158eSAntonio Quartulli  * @if_outgoing: the interface for which the packet should be considered
3919323158eSAntonio Quartulli  *
3929323158eSAntonio Quartulli  * Return:
3939323158eSAntonio Quartulli  *  1  if the OGM is new,
3949323158eSAntonio Quartulli  *  0  if it is not new but valid,
3959323158eSAntonio Quartulli  *  <0 on error (e.g. old OGM)
3969323158eSAntonio Quartulli  */
3979323158eSAntonio Quartulli static int batadv_v_ogm_metric_update(struct batadv_priv *bat_priv,
3989323158eSAntonio Quartulli 				      const struct batadv_ogm2_packet *ogm2,
3999323158eSAntonio Quartulli 				      struct batadv_orig_node *orig_node,
4009323158eSAntonio Quartulli 				      struct batadv_neigh_node *neigh_node,
4019323158eSAntonio Quartulli 				      struct batadv_hard_iface *if_incoming,
4029323158eSAntonio Quartulli 				      struct batadv_hard_iface *if_outgoing)
4039323158eSAntonio Quartulli {
4049323158eSAntonio Quartulli 	struct batadv_orig_ifinfo *orig_ifinfo = NULL;
4059323158eSAntonio Quartulli 	struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
4069323158eSAntonio Quartulli 	bool protection_started = false;
4079323158eSAntonio Quartulli 	int ret = -EINVAL;
4089323158eSAntonio Quartulli 	u32 path_throughput;
4099323158eSAntonio Quartulli 	s32 seq_diff;
4109323158eSAntonio Quartulli 
4119323158eSAntonio Quartulli 	orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
4129323158eSAntonio Quartulli 	if (!orig_ifinfo)
4139323158eSAntonio Quartulli 		goto out;
4149323158eSAntonio Quartulli 
4159323158eSAntonio Quartulli 	seq_diff = ntohl(ogm2->seqno) - orig_ifinfo->last_real_seqno;
4169323158eSAntonio Quartulli 
4179323158eSAntonio Quartulli 	if (!hlist_empty(&orig_node->neigh_list) &&
4189323158eSAntonio Quartulli 	    batadv_window_protected(bat_priv, seq_diff,
4199323158eSAntonio Quartulli 				    BATADV_OGM_MAX_AGE,
4209323158eSAntonio Quartulli 				    &orig_ifinfo->batman_seqno_reset,
4219323158eSAntonio Quartulli 				    &protection_started)) {
4229323158eSAntonio Quartulli 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
4239323158eSAntonio Quartulli 			   "Drop packet: packet within window protection time from %pM\n",
4249323158eSAntonio Quartulli 			   ogm2->orig);
4259323158eSAntonio Quartulli 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
4269323158eSAntonio Quartulli 			   "Last reset: %ld, %ld\n",
4279323158eSAntonio Quartulli 			   orig_ifinfo->batman_seqno_reset, jiffies);
4289323158eSAntonio Quartulli 		goto out;
4299323158eSAntonio Quartulli 	}
4309323158eSAntonio Quartulli 
4319323158eSAntonio Quartulli 	/* drop packets with old seqnos, however accept the first packet after
4329323158eSAntonio Quartulli 	 * a host has been rebooted.
4339323158eSAntonio Quartulli 	 */
4349323158eSAntonio Quartulli 	if ((seq_diff < 0) && !protection_started)
4359323158eSAntonio Quartulli 		goto out;
4369323158eSAntonio Quartulli 
4379323158eSAntonio Quartulli 	neigh_node->last_seen = jiffies;
4389323158eSAntonio Quartulli 
4399323158eSAntonio Quartulli 	orig_node->last_seen = jiffies;
4409323158eSAntonio Quartulli 
4419323158eSAntonio Quartulli 	orig_ifinfo->last_real_seqno = ntohl(ogm2->seqno);
4429323158eSAntonio Quartulli 	orig_ifinfo->last_ttl = ogm2->ttl;
4439323158eSAntonio Quartulli 
4449323158eSAntonio Quartulli 	neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
4459323158eSAntonio Quartulli 	if (!neigh_ifinfo)
4469323158eSAntonio Quartulli 		goto out;
4479323158eSAntonio Quartulli 
4489323158eSAntonio Quartulli 	path_throughput = batadv_v_forward_penalty(bat_priv, if_incoming,
4499323158eSAntonio Quartulli 						   if_outgoing,
4509323158eSAntonio Quartulli 						   ntohl(ogm2->throughput));
4519323158eSAntonio Quartulli 	neigh_ifinfo->bat_v.throughput = path_throughput;
4529323158eSAntonio Quartulli 	neigh_ifinfo->bat_v.last_seqno = ntohl(ogm2->seqno);
4539323158eSAntonio Quartulli 	neigh_ifinfo->last_ttl = ogm2->ttl;
4549323158eSAntonio Quartulli 
4559323158eSAntonio Quartulli 	if (seq_diff > 0 || protection_started)
4569323158eSAntonio Quartulli 		ret = 1;
4579323158eSAntonio Quartulli 	else
4589323158eSAntonio Quartulli 		ret = 0;
4599323158eSAntonio Quartulli out:
4609323158eSAntonio Quartulli 	if (orig_ifinfo)
4619323158eSAntonio Quartulli 		batadv_orig_ifinfo_put(orig_ifinfo);
4629323158eSAntonio Quartulli 	if (neigh_ifinfo)
4639323158eSAntonio Quartulli 		batadv_neigh_ifinfo_put(neigh_ifinfo);
4649323158eSAntonio Quartulli 
4659323158eSAntonio Quartulli 	return ret;
4669323158eSAntonio Quartulli }
4679323158eSAntonio Quartulli 
4689323158eSAntonio Quartulli /**
4699323158eSAntonio Quartulli  * batadv_v_ogm_route_update - update routes based on OGM
4709323158eSAntonio Quartulli  * @bat_priv: the bat priv with all the soft interface information
4719323158eSAntonio Quartulli  * @ethhdr: the Ethernet header of the OGM2
4729323158eSAntonio Quartulli  * @ogm2: OGM2 structure
4739323158eSAntonio Quartulli  * @orig_node: Originator structure for which the OGM has been received
4749323158eSAntonio Quartulli  * @neigh_node: the neigh_node through with the OGM has been received
4759323158eSAntonio Quartulli  * @if_incoming: the interface where this packet was received
4769323158eSAntonio Quartulli  * @if_outgoing: the interface for which the packet should be considered
477efcc9d30SSimon Wunderlich  *
478efcc9d30SSimon Wunderlich  * Return: true if the packet should be forwarded, false otherwise
4799323158eSAntonio Quartulli  */
480efcc9d30SSimon Wunderlich static bool batadv_v_ogm_route_update(struct batadv_priv *bat_priv,
4819323158eSAntonio Quartulli 				      const struct ethhdr *ethhdr,
4829323158eSAntonio Quartulli 				      const struct batadv_ogm2_packet *ogm2,
4839323158eSAntonio Quartulli 				      struct batadv_orig_node *orig_node,
4849323158eSAntonio Quartulli 				      struct batadv_neigh_node *neigh_node,
4859323158eSAntonio Quartulli 				      struct batadv_hard_iface *if_incoming,
4869323158eSAntonio Quartulli 				      struct batadv_hard_iface *if_outgoing)
4879323158eSAntonio Quartulli {
4889323158eSAntonio Quartulli 	struct batadv_neigh_node *router = NULL;
4899323158eSAntonio Quartulli 	struct batadv_orig_node *orig_neigh_node = NULL;
4909323158eSAntonio Quartulli 	struct batadv_neigh_node *orig_neigh_router = NULL;
49186de37c1SSimon Wunderlich 	struct batadv_neigh_ifinfo *router_ifinfo = NULL, *neigh_ifinfo = NULL;
49286de37c1SSimon Wunderlich 	u32 router_throughput, neigh_throughput;
49386de37c1SSimon Wunderlich 	u32 router_last_seqno;
49486de37c1SSimon Wunderlich 	u32 neigh_last_seqno;
49586de37c1SSimon Wunderlich 	s32 neigh_seq_diff;
496efcc9d30SSimon Wunderlich 	bool forward = false;
4979323158eSAntonio Quartulli 
4989323158eSAntonio Quartulli 	orig_neigh_node = batadv_v_ogm_orig_get(bat_priv, ethhdr->h_source);
4999323158eSAntonio Quartulli 	if (!orig_neigh_node)
5009323158eSAntonio Quartulli 		goto out;
5019323158eSAntonio Quartulli 
5029323158eSAntonio Quartulli 	orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
5039323158eSAntonio Quartulli 						   if_outgoing);
5049323158eSAntonio Quartulli 
5059323158eSAntonio Quartulli 	/* drop packet if sender is not a direct neighbor and if we
5069323158eSAntonio Quartulli 	 * don't route towards it
5079323158eSAntonio Quartulli 	 */
5089323158eSAntonio Quartulli 	router = batadv_orig_router_get(orig_node, if_outgoing);
5099323158eSAntonio Quartulli 	if (router && router->orig_node != orig_node && !orig_neigh_router) {
5109323158eSAntonio Quartulli 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
5119323158eSAntonio Quartulli 			   "Drop packet: OGM via unknown neighbor!\n");
5129323158eSAntonio Quartulli 		goto out;
5139323158eSAntonio Quartulli 	}
5149323158eSAntonio Quartulli 
51586de37c1SSimon Wunderlich 	/* Mark the OGM to be considered for forwarding, and update routes
51686de37c1SSimon Wunderlich 	 * if needed.
51786de37c1SSimon Wunderlich 	 */
518efcc9d30SSimon Wunderlich 	forward = true;
51986de37c1SSimon Wunderlich 
52086de37c1SSimon Wunderlich 	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
52186de37c1SSimon Wunderlich 		   "Searching and updating originator entry of received packet\n");
52286de37c1SSimon Wunderlich 
52386de37c1SSimon Wunderlich 	/* if this neighbor already is our next hop there is nothing
52486de37c1SSimon Wunderlich 	 * to change
52586de37c1SSimon Wunderlich 	 */
52686de37c1SSimon Wunderlich 	if (router == neigh_node)
52786de37c1SSimon Wunderlich 		goto out;
52886de37c1SSimon Wunderlich 
52986de37c1SSimon Wunderlich 	/* don't consider neighbours with worse throughput.
53086de37c1SSimon Wunderlich 	 * also switch route if this seqno is BATADV_V_MAX_ORIGDIFF newer than
53186de37c1SSimon Wunderlich 	 * the last received seqno from our best next hop.
53286de37c1SSimon Wunderlich 	 */
53386de37c1SSimon Wunderlich 	if (router) {
53486de37c1SSimon Wunderlich 		router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
53586de37c1SSimon Wunderlich 		neigh_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
53686de37c1SSimon Wunderlich 
53786de37c1SSimon Wunderlich 		/* if these are not allocated, something is wrong. */
53886de37c1SSimon Wunderlich 		if (!router_ifinfo || !neigh_ifinfo)
53986de37c1SSimon Wunderlich 			goto out;
54086de37c1SSimon Wunderlich 
54186de37c1SSimon Wunderlich 		neigh_last_seqno = neigh_ifinfo->bat_v.last_seqno;
54286de37c1SSimon Wunderlich 		router_last_seqno = router_ifinfo->bat_v.last_seqno;
54386de37c1SSimon Wunderlich 		neigh_seq_diff = neigh_last_seqno - router_last_seqno;
54486de37c1SSimon Wunderlich 		router_throughput = router_ifinfo->bat_v.throughput;
54586de37c1SSimon Wunderlich 		neigh_throughput = neigh_ifinfo->bat_v.throughput;
54686de37c1SSimon Wunderlich 
54786de37c1SSimon Wunderlich 		if ((neigh_seq_diff < BATADV_OGM_MAX_ORIGDIFF) &&
54886de37c1SSimon Wunderlich 		    (router_throughput >= neigh_throughput))
54986de37c1SSimon Wunderlich 			goto out;
55086de37c1SSimon Wunderlich 	}
55186de37c1SSimon Wunderlich 
55286de37c1SSimon Wunderlich 	batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
5539323158eSAntonio Quartulli out:
5549323158eSAntonio Quartulli 	if (router)
5559323158eSAntonio Quartulli 		batadv_neigh_node_put(router);
5569323158eSAntonio Quartulli 	if (orig_neigh_router)
5579323158eSAntonio Quartulli 		batadv_neigh_node_put(orig_neigh_router);
5589323158eSAntonio Quartulli 	if (orig_neigh_node)
5599323158eSAntonio Quartulli 		batadv_orig_node_put(orig_neigh_node);
56086de37c1SSimon Wunderlich 	if (router_ifinfo)
56186de37c1SSimon Wunderlich 		batadv_neigh_ifinfo_put(router_ifinfo);
56286de37c1SSimon Wunderlich 	if (neigh_ifinfo)
56386de37c1SSimon Wunderlich 		batadv_neigh_ifinfo_put(neigh_ifinfo);
564efcc9d30SSimon Wunderlich 
565efcc9d30SSimon Wunderlich 	return forward;
5669323158eSAntonio Quartulli }
5679323158eSAntonio Quartulli 
5689323158eSAntonio Quartulli /**
5699323158eSAntonio Quartulli  * batadv_v_ogm_process_per_outif - process a batman v OGM for an outgoing if
5709323158eSAntonio Quartulli  * @bat_priv: the bat priv with all the soft interface information
5719323158eSAntonio Quartulli  * @ethhdr: the Ethernet header of the OGM2
5729323158eSAntonio Quartulli  * @ogm2: OGM2 structure
5739323158eSAntonio Quartulli  * @orig_node: Originator structure for which the OGM has been received
5749323158eSAntonio Quartulli  * @neigh_node: the neigh_node through with the OGM has been received
5759323158eSAntonio Quartulli  * @if_incoming: the interface where this packet was received
5769323158eSAntonio Quartulli  * @if_outgoing: the interface for which the packet should be considered
5779323158eSAntonio Quartulli  */
5789323158eSAntonio Quartulli static void
5799323158eSAntonio Quartulli batadv_v_ogm_process_per_outif(struct batadv_priv *bat_priv,
5809323158eSAntonio Quartulli 			       const struct ethhdr *ethhdr,
5819323158eSAntonio Quartulli 			       const struct batadv_ogm2_packet *ogm2,
5829323158eSAntonio Quartulli 			       struct batadv_orig_node *orig_node,
5839323158eSAntonio Quartulli 			       struct batadv_neigh_node *neigh_node,
5849323158eSAntonio Quartulli 			       struct batadv_hard_iface *if_incoming,
5859323158eSAntonio Quartulli 			       struct batadv_hard_iface *if_outgoing)
5869323158eSAntonio Quartulli {
5879323158eSAntonio Quartulli 	int seqno_age;
588efcc9d30SSimon Wunderlich 	bool forward;
5899323158eSAntonio Quartulli 
5909323158eSAntonio Quartulli 	/* first, update the metric with according sanity checks */
5919323158eSAntonio Quartulli 	seqno_age = batadv_v_ogm_metric_update(bat_priv, ogm2, orig_node,
5929323158eSAntonio Quartulli 					       neigh_node, if_incoming,
5939323158eSAntonio Quartulli 					       if_outgoing);
5949323158eSAntonio Quartulli 
5959323158eSAntonio Quartulli 	/* outdated sequence numbers are to be discarded */
5969323158eSAntonio Quartulli 	if (seqno_age < 0)
5979323158eSAntonio Quartulli 		return;
5989323158eSAntonio Quartulli 
5999323158eSAntonio Quartulli 	/* only unknown & newer OGMs contain TVLVs we are interested in */
6009323158eSAntonio Quartulli 	if ((seqno_age > 0) && (if_outgoing == BATADV_IF_DEFAULT))
6019323158eSAntonio Quartulli 		batadv_tvlv_containers_process(bat_priv, true, orig_node,
6029323158eSAntonio Quartulli 					       NULL, NULL,
6039323158eSAntonio Quartulli 					       (unsigned char *)(ogm2 + 1),
6049323158eSAntonio Quartulli 					       ntohs(ogm2->tvlv_len));
6059323158eSAntonio Quartulli 
6069323158eSAntonio Quartulli 	/* if the metric update went through, update routes if needed */
607efcc9d30SSimon Wunderlich 	forward = batadv_v_ogm_route_update(bat_priv, ethhdr, ogm2, orig_node,
608efcc9d30SSimon Wunderlich 					    neigh_node, if_incoming,
609efcc9d30SSimon Wunderlich 					    if_outgoing);
610efcc9d30SSimon Wunderlich 
611efcc9d30SSimon Wunderlich 	/* if the routes have been processed correctly, check and forward */
612efcc9d30SSimon Wunderlich 	if (forward)
613efcc9d30SSimon Wunderlich 		batadv_v_ogm_forward(bat_priv, ogm2, orig_node, neigh_node,
614efcc9d30SSimon Wunderlich 				     if_incoming, if_outgoing);
6159323158eSAntonio Quartulli }
6169323158eSAntonio Quartulli 
6179323158eSAntonio Quartulli /**
6189323158eSAntonio Quartulli  * batadv_v_ogm_aggr_packet - checks if there is another OGM aggregated
6199323158eSAntonio Quartulli  * @buff_pos: current position in the skb
6209323158eSAntonio Quartulli  * @packet_len: total length of the skb
6219323158eSAntonio Quartulli  * @tvlv_len: tvlv length of the previously considered OGM
6229323158eSAntonio Quartulli  *
6239323158eSAntonio Quartulli  * Return: true if there is enough space for another OGM, false otherwise.
6249323158eSAntonio Quartulli  */
6259323158eSAntonio Quartulli static bool batadv_v_ogm_aggr_packet(int buff_pos, int packet_len,
6269323158eSAntonio Quartulli 				     __be16 tvlv_len)
6279323158eSAntonio Quartulli {
6289323158eSAntonio Quartulli 	int next_buff_pos = 0;
6299323158eSAntonio Quartulli 
6309323158eSAntonio Quartulli 	next_buff_pos += buff_pos + BATADV_OGM2_HLEN;
6319323158eSAntonio Quartulli 	next_buff_pos += ntohs(tvlv_len);
6329323158eSAntonio Quartulli 
6339323158eSAntonio Quartulli 	return (next_buff_pos <= packet_len) &&
6349323158eSAntonio Quartulli 	       (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
6359323158eSAntonio Quartulli }
6369323158eSAntonio Quartulli 
6379323158eSAntonio Quartulli /**
6389323158eSAntonio Quartulli  * batadv_v_ogm_process - process an incoming batman v OGM
6399323158eSAntonio Quartulli  * @skb: the skb containing the OGM
6409323158eSAntonio Quartulli  * @ogm_offset: offset to the OGM which should be processed (for aggregates)
6419323158eSAntonio Quartulli  * @if_incoming: the interface where this packet was receved
6429323158eSAntonio Quartulli  */
6439323158eSAntonio Quartulli static void batadv_v_ogm_process(const struct sk_buff *skb, int ogm_offset,
6449323158eSAntonio Quartulli 				 struct batadv_hard_iface *if_incoming)
6459323158eSAntonio Quartulli {
6469323158eSAntonio Quartulli 	struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
6479323158eSAntonio Quartulli 	struct ethhdr *ethhdr;
6489323158eSAntonio Quartulli 	struct batadv_orig_node *orig_node = NULL;
6499323158eSAntonio Quartulli 	struct batadv_hardif_neigh_node *hardif_neigh = NULL;
6509323158eSAntonio Quartulli 	struct batadv_neigh_node *neigh_node = NULL;
6519323158eSAntonio Quartulli 	struct batadv_hard_iface *hard_iface;
6529323158eSAntonio Quartulli 	struct batadv_ogm2_packet *ogm_packet;
6539323158eSAntonio Quartulli 	u32 ogm_throughput, link_throughput, path_throughput;
6549323158eSAntonio Quartulli 
6559323158eSAntonio Quartulli 	ethhdr = eth_hdr(skb);
6569323158eSAntonio Quartulli 	ogm_packet = (struct batadv_ogm2_packet *)(skb->data + ogm_offset);
6579323158eSAntonio Quartulli 
6589323158eSAntonio Quartulli 	ogm_throughput = ntohl(ogm_packet->throughput);
6599323158eSAntonio Quartulli 
6609323158eSAntonio Quartulli 	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
6619323158eSAntonio 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",
6629323158eSAntonio Quartulli 		   ethhdr->h_source, if_incoming->net_dev->name,
6639323158eSAntonio Quartulli 		   if_incoming->net_dev->dev_addr, ogm_packet->orig,
6649323158eSAntonio Quartulli 		   ntohl(ogm_packet->seqno), ogm_throughput, ogm_packet->ttl,
6659323158eSAntonio Quartulli 		   ogm_packet->version, ntohs(ogm_packet->tvlv_len));
6669323158eSAntonio Quartulli 
6679323158eSAntonio Quartulli 	/* If the troughput metric is 0, immediately drop the packet. No need to
6689323158eSAntonio Quartulli 	 * create orig_node / neigh_node for an unusable route.
6699323158eSAntonio Quartulli 	 */
6709323158eSAntonio Quartulli 	if (ogm_throughput == 0) {
6719323158eSAntonio Quartulli 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
6729323158eSAntonio Quartulli 			   "Drop packet: originator packet with troughput metric of 0\n");
6739323158eSAntonio Quartulli 		return;
6749323158eSAntonio Quartulli 	}
6759323158eSAntonio Quartulli 
6769323158eSAntonio Quartulli 	/* require ELP packets be to received from this neighbor first */
6779323158eSAntonio Quartulli 	hardif_neigh = batadv_hardif_neigh_get(if_incoming, ethhdr->h_source);
6789323158eSAntonio Quartulli 	if (!hardif_neigh) {
6799323158eSAntonio Quartulli 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
6809323158eSAntonio Quartulli 			   "Drop packet: OGM via unknown neighbor!\n");
6819323158eSAntonio Quartulli 		goto out;
6829323158eSAntonio Quartulli 	}
6839323158eSAntonio Quartulli 
6849323158eSAntonio Quartulli 	orig_node = batadv_v_ogm_orig_get(bat_priv, ogm_packet->orig);
6859323158eSAntonio Quartulli 	if (!orig_node)
6869323158eSAntonio Quartulli 		return;
6879323158eSAntonio Quartulli 
6886f0a6b5eSMarek Lindner 	neigh_node = batadv_neigh_node_get_or_create(orig_node, if_incoming,
6899323158eSAntonio Quartulli 						     ethhdr->h_source);
6909323158eSAntonio Quartulli 	if (!neigh_node)
6919323158eSAntonio Quartulli 		goto out;
6929323158eSAntonio Quartulli 
6939323158eSAntonio Quartulli 	/* Update the received throughput metric to match the link
6949323158eSAntonio Quartulli 	 * characteristic:
6959323158eSAntonio Quartulli 	 *  - If this OGM traveled one hop so far (emitted by single hop
6969323158eSAntonio Quartulli 	 *    neighbor) the path throughput metric equals the link throughput.
6979323158eSAntonio Quartulli 	 *  - For OGMs traversing more than hop the path throughput metric is
6989323158eSAntonio Quartulli 	 *    the smaller of the path throughput and the link throughput.
6999323158eSAntonio Quartulli 	 */
7009323158eSAntonio Quartulli 	link_throughput = ewma_throughput_read(&hardif_neigh->bat_v.throughput);
7019323158eSAntonio Quartulli 	path_throughput = min_t(u32, link_throughput, ogm_throughput);
7029323158eSAntonio Quartulli 	ogm_packet->throughput = htonl(path_throughput);
7039323158eSAntonio Quartulli 
7049323158eSAntonio Quartulli 	batadv_v_ogm_process_per_outif(bat_priv, ethhdr, ogm_packet, orig_node,
7059323158eSAntonio Quartulli 				       neigh_node, if_incoming,
7069323158eSAntonio Quartulli 				       BATADV_IF_DEFAULT);
7079323158eSAntonio Quartulli 
7089323158eSAntonio Quartulli 	rcu_read_lock();
7099323158eSAntonio Quartulli 	list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
7109323158eSAntonio Quartulli 		if (hard_iface->if_status != BATADV_IF_ACTIVE)
7119323158eSAntonio Quartulli 			continue;
7129323158eSAntonio Quartulli 
7139323158eSAntonio Quartulli 		if (hard_iface->soft_iface != bat_priv->soft_iface)
7149323158eSAntonio Quartulli 			continue;
7159323158eSAntonio Quartulli 
71627353446SSven Eckelmann 		if (!kref_get_unless_zero(&hard_iface->refcount))
71727353446SSven Eckelmann 			continue;
71827353446SSven Eckelmann 
7199323158eSAntonio Quartulli 		batadv_v_ogm_process_per_outif(bat_priv, ethhdr, ogm_packet,
7209323158eSAntonio Quartulli 					       orig_node, neigh_node,
7219323158eSAntonio Quartulli 					       if_incoming, hard_iface);
72227353446SSven Eckelmann 
72327353446SSven Eckelmann 		batadv_hardif_put(hard_iface);
7249323158eSAntonio Quartulli 	}
7259323158eSAntonio Quartulli 	rcu_read_unlock();
7269323158eSAntonio Quartulli out:
7279323158eSAntonio Quartulli 	if (orig_node)
7289323158eSAntonio Quartulli 		batadv_orig_node_put(orig_node);
7299323158eSAntonio Quartulli 	if (neigh_node)
7309323158eSAntonio Quartulli 		batadv_neigh_node_put(neigh_node);
7319323158eSAntonio Quartulli 	if (hardif_neigh)
7329323158eSAntonio Quartulli 		batadv_hardif_neigh_put(hardif_neigh);
7339323158eSAntonio Quartulli }
7349323158eSAntonio Quartulli 
7359323158eSAntonio Quartulli /**
7360da00359SAntonio Quartulli  * batadv_v_ogm_packet_recv - OGM2 receiving handler
7370da00359SAntonio Quartulli  * @skb: the received OGM
7380da00359SAntonio Quartulli  * @if_incoming: the interface where this OGM has been received
7390da00359SAntonio Quartulli  *
7400da00359SAntonio Quartulli  * Return: NET_RX_SUCCESS and consume the skb on success or returns NET_RX_DROP
7410da00359SAntonio Quartulli  * (without freeing the skb) on failure
7420da00359SAntonio Quartulli  */
7430da00359SAntonio Quartulli int batadv_v_ogm_packet_recv(struct sk_buff *skb,
7440da00359SAntonio Quartulli 			     struct batadv_hard_iface *if_incoming)
7450da00359SAntonio Quartulli {
7460da00359SAntonio Quartulli 	struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
7470da00359SAntonio Quartulli 	struct batadv_ogm2_packet *ogm_packet;
7480da00359SAntonio Quartulli 	struct ethhdr *ethhdr = eth_hdr(skb);
7499323158eSAntonio Quartulli 	int ogm_offset;
7509323158eSAntonio Quartulli 	u8 *packet_pos;
7519323158eSAntonio Quartulli 	int ret = NET_RX_DROP;
7520da00359SAntonio Quartulli 
7530da00359SAntonio Quartulli 	/* did we receive a OGM2 packet on an interface that does not have
7540da00359SAntonio Quartulli 	 * B.A.T.M.A.N. V enabled ?
7550da00359SAntonio Quartulli 	 */
7560da00359SAntonio Quartulli 	if (strcmp(bat_priv->bat_algo_ops->name, "BATMAN_V") != 0)
7570da00359SAntonio Quartulli 		return NET_RX_DROP;
7580da00359SAntonio Quartulli 
7590da00359SAntonio Quartulli 	if (!batadv_check_management_packet(skb, if_incoming, BATADV_OGM2_HLEN))
7600da00359SAntonio Quartulli 		return NET_RX_DROP;
7610da00359SAntonio Quartulli 
7620da00359SAntonio Quartulli 	if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
7630da00359SAntonio Quartulli 		return NET_RX_DROP;
7640da00359SAntonio Quartulli 
7650da00359SAntonio Quartulli 	ogm_packet = (struct batadv_ogm2_packet *)skb->data;
7660da00359SAntonio Quartulli 
7670da00359SAntonio Quartulli 	if (batadv_is_my_mac(bat_priv, ogm_packet->orig))
7680da00359SAntonio Quartulli 		return NET_RX_DROP;
7690da00359SAntonio Quartulli 
7700da00359SAntonio Quartulli 	batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
7710da00359SAntonio Quartulli 	batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
7720da00359SAntonio Quartulli 			   skb->len + ETH_HLEN);
7730da00359SAntonio Quartulli 
7749323158eSAntonio Quartulli 	ogm_offset = 0;
7759323158eSAntonio Quartulli 	ogm_packet = (struct batadv_ogm2_packet *)skb->data;
7769323158eSAntonio Quartulli 
7779323158eSAntonio Quartulli 	while (batadv_v_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
7789323158eSAntonio Quartulli 					ogm_packet->tvlv_len)) {
7799323158eSAntonio Quartulli 		batadv_v_ogm_process(skb, ogm_offset, if_incoming);
7809323158eSAntonio Quartulli 
7819323158eSAntonio Quartulli 		ogm_offset += BATADV_OGM2_HLEN;
7829323158eSAntonio Quartulli 		ogm_offset += ntohs(ogm_packet->tvlv_len);
7839323158eSAntonio Quartulli 
7849323158eSAntonio Quartulli 		packet_pos = skb->data + ogm_offset;
7859323158eSAntonio Quartulli 		ogm_packet = (struct batadv_ogm2_packet *)packet_pos;
7869323158eSAntonio Quartulli 	}
7879323158eSAntonio Quartulli 
7889323158eSAntonio Quartulli 	ret = NET_RX_SUCCESS;
7890da00359SAntonio Quartulli 	consume_skb(skb);
7909323158eSAntonio Quartulli 
7919323158eSAntonio Quartulli 	return ret;
7920da00359SAntonio Quartulli }
7930da00359SAntonio Quartulli 
7940da00359SAntonio Quartulli /**
7950da00359SAntonio Quartulli  * batadv_v_ogm_init - initialise the OGM2 engine
7960da00359SAntonio Quartulli  * @bat_priv: the bat priv with all the soft interface information
7970da00359SAntonio Quartulli  *
7980da00359SAntonio Quartulli  * Return: 0 on success or a negative error code in case of failure
7990da00359SAntonio Quartulli  */
8000da00359SAntonio Quartulli int batadv_v_ogm_init(struct batadv_priv *bat_priv)
8010da00359SAntonio Quartulli {
8020da00359SAntonio Quartulli 	struct batadv_ogm2_packet *ogm_packet;
8030da00359SAntonio Quartulli 	unsigned char *ogm_buff;
8040da00359SAntonio Quartulli 	u32 random_seqno;
8050da00359SAntonio Quartulli 
8060da00359SAntonio Quartulli 	bat_priv->bat_v.ogm_buff_len = BATADV_OGM2_HLEN;
8070da00359SAntonio Quartulli 	ogm_buff = kzalloc(bat_priv->bat_v.ogm_buff_len, GFP_ATOMIC);
8080da00359SAntonio Quartulli 	if (!ogm_buff)
8090da00359SAntonio Quartulli 		return -ENOMEM;
8100da00359SAntonio Quartulli 
8110da00359SAntonio Quartulli 	bat_priv->bat_v.ogm_buff = ogm_buff;
8120da00359SAntonio Quartulli 	ogm_packet = (struct batadv_ogm2_packet *)ogm_buff;
8130da00359SAntonio Quartulli 	ogm_packet->packet_type = BATADV_OGM2;
8140da00359SAntonio Quartulli 	ogm_packet->version = BATADV_COMPAT_VERSION;
8150da00359SAntonio Quartulli 	ogm_packet->ttl = BATADV_TTL;
8160da00359SAntonio Quartulli 	ogm_packet->flags = BATADV_NO_FLAGS;
8170da00359SAntonio Quartulli 	ogm_packet->throughput = htonl(BATADV_THROUGHPUT_MAX_VALUE);
8180da00359SAntonio Quartulli 
8190da00359SAntonio Quartulli 	/* randomize initial seqno to avoid collision */
8200da00359SAntonio Quartulli 	get_random_bytes(&random_seqno, sizeof(random_seqno));
8210da00359SAntonio Quartulli 	atomic_set(&bat_priv->bat_v.ogm_seqno, random_seqno);
8220da00359SAntonio Quartulli 	INIT_DELAYED_WORK(&bat_priv->bat_v.ogm_wq, batadv_v_ogm_send);
8230da00359SAntonio Quartulli 
8240da00359SAntonio Quartulli 	return 0;
8250da00359SAntonio Quartulli }
8260da00359SAntonio Quartulli 
8270da00359SAntonio Quartulli /**
8280da00359SAntonio Quartulli  * batadv_v_ogm_free - free OGM private resources
8290da00359SAntonio Quartulli  * @bat_priv: the bat priv with all the soft interface information
8300da00359SAntonio Quartulli  */
8310da00359SAntonio Quartulli void batadv_v_ogm_free(struct batadv_priv *bat_priv)
8320da00359SAntonio Quartulli {
8330da00359SAntonio Quartulli 	cancel_delayed_work_sync(&bat_priv->bat_v.ogm_wq);
8340da00359SAntonio Quartulli 
8350da00359SAntonio Quartulli 	kfree(bat_priv->bat_v.ogm_buff);
8360da00359SAntonio Quartulli 	bat_priv->bat_v.ogm_buff = NULL;
8370da00359SAntonio Quartulli 	bat_priv->bat_v.ogm_buff_len = 0;
8380da00359SAntonio Quartulli }
839