Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /* Copyright (C) 2012-2020  B.A.T.M.A.N. contributors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  * Martin Hundebøll, Jeppe Ledet-Pedersen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) #include "network-coding.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include "main.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/atomic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/byteorder/generic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/etherdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/gfp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/if_ether.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/if_packet.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/jhash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <linux/kref.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include <linux/lockdep.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include <linux/net.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <linux/prandom.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include <linux/printk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <linux/rculist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include <linux/rcupdate.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #include <linux/stddef.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #include <uapi/linux/batadv_packet.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) #include "hard-interface.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) #include "hash.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) #include "log.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) #include "originator.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) #include "routing.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) #include "send.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) #include "tvlv.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) static struct lock_class_key batadv_nc_coding_hash_lock_class_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) static struct lock_class_key batadv_nc_decoding_hash_lock_class_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) static void batadv_nc_worker(struct work_struct *work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) static int batadv_nc_recv_coded_packet(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 				       struct batadv_hard_iface *recv_if);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58)  * batadv_nc_init() - one-time initialization for network coding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60)  * Return: 0 on success or negative error number in case of failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) int __init batadv_nc_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 	/* Register our packet type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 	ret = batadv_recv_handler_register(BATADV_CODED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 					   batadv_nc_recv_coded_packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74)  * batadv_nc_start_timer() - initialise the nc periodic worker
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75)  * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) static void batadv_nc_start_timer(struct batadv_priv *bat_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 	queue_delayed_work(batadv_event_workqueue, &bat_priv->nc.work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 			   msecs_to_jiffies(10));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84)  * batadv_nc_tvlv_container_update() - update the network coding tvlv container
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85)  *  after network coding setting change
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86)  * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) static void batadv_nc_tvlv_container_update(struct batadv_priv *bat_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	char nc_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	nc_mode = atomic_read(&bat_priv->network_coding);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	switch (nc_mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 		batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_NC, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 		batadv_tvlv_container_register(bat_priv, BATADV_TVLV_NC, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 					       NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106)  * batadv_nc_status_update() - update the network coding tvlv container after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107)  *  network coding setting change
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108)  * @net_dev: the soft interface net device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) void batadv_nc_status_update(struct net_device *net_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	struct batadv_priv *bat_priv = netdev_priv(net_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	batadv_nc_tvlv_container_update(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118)  * batadv_nc_tvlv_ogm_handler_v1() - process incoming nc tvlv container
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119)  * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120)  * @orig: the orig_node of the ogm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121)  * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122)  * @tvlv_value: tvlv buffer containing the gateway data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123)  * @tvlv_value_len: tvlv buffer length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) static void batadv_nc_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 					  struct batadv_orig_node *orig,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 					  u8 flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 					  void *tvlv_value, u16 tvlv_value_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 		clear_bit(BATADV_ORIG_CAPA_HAS_NC, &orig->capabilities);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 		set_bit(BATADV_ORIG_CAPA_HAS_NC, &orig->capabilities);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137)  * batadv_nc_mesh_init() - initialise coding hash table and start housekeeping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138)  * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140)  * Return: 0 on success or negative error number in case of failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) int batadv_nc_mesh_init(struct batadv_priv *bat_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	bat_priv->nc.timestamp_fwd_flush = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	bat_priv->nc.timestamp_sniffed_purge = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	if (bat_priv->nc.coding_hash || bat_priv->nc.decoding_hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	bat_priv->nc.coding_hash = batadv_hash_new(128);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	if (!bat_priv->nc.coding_hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	batadv_hash_set_lock_class(bat_priv->nc.coding_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 				   &batadv_nc_coding_hash_lock_class_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	bat_priv->nc.decoding_hash = batadv_hash_new(128);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	if (!bat_priv->nc.decoding_hash) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 		batadv_hash_destroy(bat_priv->nc.coding_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	batadv_hash_set_lock_class(bat_priv->nc.decoding_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 				   &batadv_nc_decoding_hash_lock_class_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	INIT_DELAYED_WORK(&bat_priv->nc.work, batadv_nc_worker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	batadv_nc_start_timer(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	batadv_tvlv_handler_register(bat_priv, batadv_nc_tvlv_ogm_handler_v1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 				     NULL, BATADV_TVLV_NC, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 				     BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 	batadv_nc_tvlv_container_update(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180)  * batadv_nc_init_bat_priv() - initialise the nc specific bat_priv variables
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181)  * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) void batadv_nc_init_bat_priv(struct batadv_priv *bat_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	atomic_set(&bat_priv->network_coding, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	bat_priv->nc.min_tq = 200;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	bat_priv->nc.max_fwd_delay = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	bat_priv->nc.max_buffer_time = 200;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192)  * batadv_nc_init_orig() - initialise the nc fields of an orig_node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193)  * @orig_node: the orig_node which is going to be initialised
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) void batadv_nc_init_orig(struct batadv_orig_node *orig_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	INIT_LIST_HEAD(&orig_node->in_coding_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	INIT_LIST_HEAD(&orig_node->out_coding_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	spin_lock_init(&orig_node->in_coding_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	spin_lock_init(&orig_node->out_coding_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204)  * batadv_nc_node_release() - release nc_node from lists and queue for free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205)  *  after rcu grace period
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206)  * @ref: kref pointer of the nc_node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) static void batadv_nc_node_release(struct kref *ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	struct batadv_nc_node *nc_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 	nc_node = container_of(ref, struct batadv_nc_node, refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	batadv_orig_node_put(nc_node->orig_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	kfree_rcu(nc_node, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219)  * batadv_nc_node_put() - decrement the nc_node refcounter and possibly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220)  *  release it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221)  * @nc_node: nc_node to be free'd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) static void batadv_nc_node_put(struct batadv_nc_node *nc_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	if (!nc_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	kref_put(&nc_node->refcount, batadv_nc_node_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232)  * batadv_nc_path_release() - release nc_path from lists and queue for free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233)  *  after rcu grace period
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234)  * @ref: kref pointer of the nc_path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) static void batadv_nc_path_release(struct kref *ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	struct batadv_nc_path *nc_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	nc_path = container_of(ref, struct batadv_nc_path, refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	kfree_rcu(nc_path, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246)  * batadv_nc_path_put() - decrement the nc_path refcounter and possibly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247)  *  release it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248)  * @nc_path: nc_path to be free'd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) static void batadv_nc_path_put(struct batadv_nc_path *nc_path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	if (!nc_path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	kref_put(&nc_path->refcount, batadv_nc_path_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259)  * batadv_nc_packet_free() - frees nc packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260)  * @nc_packet: the nc packet to free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261)  * @dropped: whether the packet is freed because is dropped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) static void batadv_nc_packet_free(struct batadv_nc_packet *nc_packet,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 				  bool dropped)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	if (dropped)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 		kfree_skb(nc_packet->skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 		consume_skb(nc_packet->skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	batadv_nc_path_put(nc_packet->nc_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	kfree(nc_packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276)  * batadv_nc_to_purge_nc_node() - checks whether an nc node has to be purged
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277)  * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278)  * @nc_node: the nc node to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280)  * Return: true if the entry has to be purged now, false otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) static bool batadv_nc_to_purge_nc_node(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 				       struct batadv_nc_node *nc_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	return batadv_has_timed_out(nc_node->last_seen, BATADV_NC_NODE_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292)  * batadv_nc_to_purge_nc_path_coding() - checks whether an nc path has timed out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293)  * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294)  * @nc_path: the nc path to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296)  * Return: true if the entry has to be purged now, false otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) static bool batadv_nc_to_purge_nc_path_coding(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 					      struct batadv_nc_path *nc_path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	/* purge the path when no packets has been added for 10 times the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	 * max_fwd_delay time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	return batadv_has_timed_out(nc_path->last_valid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 				    bat_priv->nc.max_fwd_delay * 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312)  * batadv_nc_to_purge_nc_path_decoding() - checks whether an nc path has timed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313)  *  out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314)  * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315)  * @nc_path: the nc path to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317)  * Return: true if the entry has to be purged now, false otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) static bool batadv_nc_to_purge_nc_path_decoding(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 						struct batadv_nc_path *nc_path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	/* purge the path when no packets has been added for 10 times the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	 * max_buffer time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	return batadv_has_timed_out(nc_path->last_valid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 				    bat_priv->nc.max_buffer_time * 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333)  * batadv_nc_purge_orig_nc_nodes() - go through list of nc nodes and purge stale
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334)  *  entries
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335)  * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336)  * @list: list of nc nodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337)  * @lock: nc node list lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338)  * @to_purge: function in charge to decide whether an entry has to be purged or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339)  *	      not. This function takes the nc node as argument and has to return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340)  *	      a boolean value: true if the entry has to be deleted, false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341)  *	      otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) batadv_nc_purge_orig_nc_nodes(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 			      struct list_head *list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 			      spinlock_t *lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 			      bool (*to_purge)(struct batadv_priv *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 					       struct batadv_nc_node *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	struct batadv_nc_node *nc_node, *nc_node_tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	/* For each nc_node in list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	spin_lock_bh(lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	list_for_each_entry_safe(nc_node, nc_node_tmp, list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 		/* if an helper function has been passed as parameter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 		 * ask it if the entry has to be purged or not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 		if (to_purge && !to_purge(bat_priv, nc_node))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 		batadv_dbg(BATADV_DBG_NC, bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 			   "Removing nc_node %pM -> %pM\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 			   nc_node->addr, nc_node->orig_node->orig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 		list_del_rcu(&nc_node->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 		batadv_nc_node_put(nc_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	spin_unlock_bh(lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371)  * batadv_nc_purge_orig() - purges all nc node data attached of the given
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372)  *  originator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373)  * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374)  * @orig_node: orig_node with the nc node entries to be purged
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375)  * @to_purge: function in charge to decide whether an entry has to be purged or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376)  *	      not. This function takes the nc node as argument and has to return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377)  *	      a boolean value: true is the entry has to be deleted, false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378)  *	      otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) void batadv_nc_purge_orig(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 			  struct batadv_orig_node *orig_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 			  bool (*to_purge)(struct batadv_priv *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 					   struct batadv_nc_node *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	/* Check ingoing nc_node's of this orig_node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	batadv_nc_purge_orig_nc_nodes(bat_priv, &orig_node->in_coding_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 				      &orig_node->in_coding_list_lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 				      to_purge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	/* Check outgoing nc_node's of this orig_node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	batadv_nc_purge_orig_nc_nodes(bat_priv, &orig_node->out_coding_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 				      &orig_node->out_coding_list_lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 				      to_purge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397)  * batadv_nc_purge_orig_hash() - traverse entire originator hash to check if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398)  *  they have timed out nc nodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399)  * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) static void batadv_nc_purge_orig_hash(struct batadv_priv *bat_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	struct batadv_hashtable *hash = bat_priv->orig_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	struct hlist_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	struct batadv_orig_node *orig_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	u32 i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	if (!hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	/* For each orig_node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	for (i = 0; i < hash->size; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 		head = &hash->table[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 		rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 		hlist_for_each_entry_rcu(orig_node, head, hash_entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 			batadv_nc_purge_orig(bat_priv, orig_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 					     batadv_nc_to_purge_nc_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 		rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424)  * batadv_nc_purge_paths() - traverse all nc paths part of the hash and remove
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425)  *  unused ones
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426)  * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427)  * @hash: hash table containing the nc paths to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428)  * @to_purge: function in charge to decide whether an entry has to be purged or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429)  *	      not. This function takes the nc node as argument and has to return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430)  *	      a boolean value: true is the entry has to be deleted, false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431)  *	      otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) static void batadv_nc_purge_paths(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 				  struct batadv_hashtable *hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 				  bool (*to_purge)(struct batadv_priv *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 						   struct batadv_nc_path *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	struct hlist_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	struct hlist_node *node_tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	struct batadv_nc_path *nc_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	spinlock_t *lock; /* Protects lists in hash */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	u32 i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	for (i = 0; i < hash->size; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 		head = &hash->table[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 		lock = &hash->list_locks[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 		/* For each nc_path in this bin */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 		spin_lock_bh(lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 		hlist_for_each_entry_safe(nc_path, node_tmp, head, hash_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 			/* if an helper function has been passed as parameter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 			 * ask it if the entry has to be purged or not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 			if (to_purge && !to_purge(bat_priv, nc_path))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 			/* purging an non-empty nc_path should never happen, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 			 * is observed under high CPU load. Delay the purging
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 			 * until next iteration to allow the packet_list to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 			 * emptied first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 			if (!unlikely(list_empty(&nc_path->packet_list))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 				net_ratelimited_function(printk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 							 KERN_WARNING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 							 "Skipping free of non-empty nc_path (%pM -> %pM)!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 							 nc_path->prev_hop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 							 nc_path->next_hop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 			/* nc_path is unused, so remove it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 			batadv_dbg(BATADV_DBG_NC, bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 				   "Remove nc_path %pM -> %pM\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 				   nc_path->prev_hop, nc_path->next_hop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 			hlist_del_rcu(&nc_path->hash_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 			batadv_nc_path_put(nc_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 		spin_unlock_bh(lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483)  * batadv_nc_hash_key_gen() - computes the nc_path hash key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484)  * @key: buffer to hold the final hash key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485)  * @src: source ethernet mac address going into the hash key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486)  * @dst: destination ethernet mac address going into the hash key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) static void batadv_nc_hash_key_gen(struct batadv_nc_path *key, const char *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 				   const char *dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	memcpy(key->prev_hop, src, sizeof(key->prev_hop));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	memcpy(key->next_hop, dst, sizeof(key->next_hop));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496)  * batadv_nc_hash_choose() - compute the hash value for an nc path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497)  * @data: data to hash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498)  * @size: size of the hash table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500)  * Return: the selected index in the hash table for the given data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) static u32 batadv_nc_hash_choose(const void *data, u32 size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	const struct batadv_nc_path *nc_path = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	u32 hash = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	hash = jhash(&nc_path->prev_hop, sizeof(nc_path->prev_hop), hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	hash = jhash(&nc_path->next_hop, sizeof(nc_path->next_hop), hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	return hash % size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514)  * batadv_nc_hash_compare() - comparing function used in the network coding hash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515)  *  tables
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516)  * @node: node in the local table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517)  * @data2: second object to compare the node to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519)  * Return: true if the two entry are the same, false otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) static bool batadv_nc_hash_compare(const struct hlist_node *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 				   const void *data2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	const struct batadv_nc_path *nc_path1, *nc_path2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	nc_path1 = container_of(node, struct batadv_nc_path, hash_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 	nc_path2 = data2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	/* Return 1 if the two keys are identical */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	if (!batadv_compare_eth(nc_path1->prev_hop, nc_path2->prev_hop))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	if (!batadv_compare_eth(nc_path1->next_hop, nc_path2->next_hop))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540)  * batadv_nc_hash_find() - search for an existing nc path and return it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541)  * @hash: hash table containing the nc path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542)  * @data: search key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544)  * Return: the nc_path if found, NULL otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) static struct batadv_nc_path *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) batadv_nc_hash_find(struct batadv_hashtable *hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 		    void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	struct hlist_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	struct batadv_nc_path *nc_path, *nc_path_tmp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	int index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	if (!hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	index = batadv_nc_hash_choose(data, hash->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	head = &hash->table[index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	hlist_for_each_entry_rcu(nc_path, head, hash_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 		if (!batadv_nc_hash_compare(&nc_path->hash_entry, data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 		if (!kref_get_unless_zero(&nc_path->refcount))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 		nc_path_tmp = nc_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	return nc_path_tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577)  * batadv_nc_send_packet() - send non-coded packet and free nc_packet struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578)  * @nc_packet: the nc packet to send
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) static void batadv_nc_send_packet(struct batadv_nc_packet *nc_packet)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	batadv_send_unicast_skb(nc_packet->skb, nc_packet->neigh_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	nc_packet->skb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	batadv_nc_packet_free(nc_packet, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588)  * batadv_nc_sniffed_purge() - Checks timestamp of given sniffed nc_packet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589)  * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590)  * @nc_path: the nc path the packet belongs to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591)  * @nc_packet: the nc packet to be checked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593)  * Checks whether the given sniffed (overheard) nc_packet has hit its buffering
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594)  * timeout. If so, the packet is no longer kept and the entry deleted from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595)  * queue. Has to be called with the appropriate locks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597)  * Return: false as soon as the entry in the fifo queue has not been timed out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598)  * yet and true otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) static bool batadv_nc_sniffed_purge(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 				    struct batadv_nc_path *nc_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 				    struct batadv_nc_packet *nc_packet)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	unsigned long timeout = bat_priv->nc.max_buffer_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	bool res = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	lockdep_assert_held(&nc_path->packet_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	/* Packets are added to tail, so the remaining packets did not time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	 * out and we can stop processing the current queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	    !batadv_has_timed_out(nc_packet->timestamp, timeout))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	/* purge nc packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	list_del(&nc_packet->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	batadv_nc_packet_free(nc_packet, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	res = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627)  * batadv_nc_fwd_flush() - Checks the timestamp of the given nc packet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628)  * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629)  * @nc_path: the nc path the packet belongs to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630)  * @nc_packet: the nc packet to be checked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632)  * Checks whether the given nc packet has hit its forward timeout. If so, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633)  * packet is no longer delayed, immediately sent and the entry deleted from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634)  * queue. Has to be called with the appropriate locks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636)  * Return: false as soon as the entry in the fifo queue has not been timed out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637)  * yet and true otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) static bool batadv_nc_fwd_flush(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 				struct batadv_nc_path *nc_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 				struct batadv_nc_packet *nc_packet)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	unsigned long timeout = bat_priv->nc.max_fwd_delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	lockdep_assert_held(&nc_path->packet_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	/* Packets are added to tail, so the remaining packets did not time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 	 * out and we can stop processing the current queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	    !batadv_has_timed_out(nc_packet->timestamp, timeout))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	/* Send packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 			   nc_packet->skb->len + ETH_HLEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	list_del(&nc_packet->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	batadv_nc_send_packet(nc_packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665)  * batadv_nc_process_nc_paths() - traverse given nc packet pool and free timed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666)  *  out nc packets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667)  * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668)  * @hash: to be processed hash table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669)  * @process_fn: Function called to process given nc packet. Should return true
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670)  *	        to encourage this function to proceed with the next packet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671)  *	        Otherwise the rest of the current queue is skipped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) batadv_nc_process_nc_paths(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 			   struct batadv_hashtable *hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 			   bool (*process_fn)(struct batadv_priv *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 					      struct batadv_nc_path *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 					      struct batadv_nc_packet *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	struct hlist_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	struct batadv_nc_packet *nc_packet, *nc_packet_tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	struct batadv_nc_path *nc_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	bool ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	if (!hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	/* Loop hash table bins */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	for (i = 0; i < hash->size; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 		head = &hash->table[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 		/* Loop coding paths */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 		rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 		hlist_for_each_entry_rcu(nc_path, head, hash_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 			/* Loop packets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 			spin_lock_bh(&nc_path->packet_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 			list_for_each_entry_safe(nc_packet, nc_packet_tmp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 						 &nc_path->packet_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 				ret = process_fn(bat_priv, nc_path, nc_packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 				if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 			spin_unlock_bh(&nc_path->packet_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 		rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711)  * batadv_nc_worker() - periodic task for housekeeping related to network
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712)  *  coding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713)  * @work: kernel work struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) static void batadv_nc_worker(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	struct delayed_work *delayed_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	struct batadv_priv_nc *priv_nc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	struct batadv_priv *bat_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	unsigned long timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	delayed_work = to_delayed_work(work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	priv_nc = container_of(delayed_work, struct batadv_priv_nc, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	bat_priv = container_of(priv_nc, struct batadv_priv, nc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	batadv_nc_purge_orig_hash(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	batadv_nc_purge_paths(bat_priv, bat_priv->nc.coding_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 			      batadv_nc_to_purge_nc_path_coding);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	batadv_nc_purge_paths(bat_priv, bat_priv->nc.decoding_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 			      batadv_nc_to_purge_nc_path_decoding);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	timeout = bat_priv->nc.max_fwd_delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	if (batadv_has_timed_out(bat_priv->nc.timestamp_fwd_flush, timeout)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 		batadv_nc_process_nc_paths(bat_priv, bat_priv->nc.coding_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 					   batadv_nc_fwd_flush);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 		bat_priv->nc.timestamp_fwd_flush = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	if (batadv_has_timed_out(bat_priv->nc.timestamp_sniffed_purge,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 				 bat_priv->nc.max_buffer_time)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 		batadv_nc_process_nc_paths(bat_priv, bat_priv->nc.decoding_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 					   batadv_nc_sniffed_purge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 		bat_priv->nc.timestamp_sniffed_purge = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	/* Schedule a new check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	batadv_nc_start_timer(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752)  * batadv_can_nc_with_orig() - checks whether the given orig node is suitable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753)  *  for coding or not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754)  * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755)  * @orig_node: neighboring orig node which may be used as nc candidate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756)  * @ogm_packet: incoming ogm packet also used for the checks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758)  * Return: true if:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759)  *  1) The OGM must have the most recent sequence number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760)  *  2) The TTL must be decremented by one and only one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761)  *  3) The OGM must be received from the first hop from orig_node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762)  *  4) The TQ value of the OGM must be above bat_priv->nc.min_tq.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) static bool batadv_can_nc_with_orig(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 				    struct batadv_orig_node *orig_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 				    struct batadv_ogm_packet *ogm_packet)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	struct batadv_orig_ifinfo *orig_ifinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	u32 last_real_seqno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	u8 last_ttl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	orig_ifinfo = batadv_orig_ifinfo_get(orig_node, BATADV_IF_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	if (!orig_ifinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	last_ttl = orig_ifinfo->last_ttl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	last_real_seqno = orig_ifinfo->last_real_seqno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	batadv_orig_ifinfo_put(orig_ifinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	if (last_real_seqno != ntohl(ogm_packet->seqno))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	if (last_ttl != ogm_packet->ttl + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 	if (!batadv_compare_eth(ogm_packet->orig, ogm_packet->prev_sender))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	if (ogm_packet->tq < bat_priv->nc.min_tq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793)  * batadv_nc_find_nc_node() - search for an existing nc node and return it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794)  * @orig_node: orig node originating the ogm packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795)  * @orig_neigh_node: neighboring orig node from which we received the ogm packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796)  *  (can be equal to orig_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797)  * @in_coding: traverse incoming or outgoing network coding list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799)  * Return: the nc_node if found, NULL otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) static struct batadv_nc_node *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) batadv_nc_find_nc_node(struct batadv_orig_node *orig_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 		       struct batadv_orig_node *orig_neigh_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 		       bool in_coding)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 	struct batadv_nc_node *nc_node, *nc_node_out = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	struct list_head *list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	if (in_coding)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 		list = &orig_neigh_node->in_coding_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 		list = &orig_neigh_node->out_coding_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	/* Traverse list of nc_nodes to orig_node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	list_for_each_entry_rcu(nc_node, list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 		if (!batadv_compare_eth(nc_node->addr, orig_node->orig))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 		if (!kref_get_unless_zero(&nc_node->refcount))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 		/* Found a match */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 		nc_node_out = nc_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	return nc_node_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833)  * batadv_nc_get_nc_node() - retrieves an nc node or creates the entry if it was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834)  *  not found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835)  * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836)  * @orig_node: orig node originating the ogm packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837)  * @orig_neigh_node: neighboring orig node from which we received the ogm packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838)  *  (can be equal to orig_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839)  * @in_coding: traverse incoming or outgoing network coding list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841)  * Return: the nc_node if found or created, NULL in case of an error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) static struct batadv_nc_node *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) batadv_nc_get_nc_node(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 		      struct batadv_orig_node *orig_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 		      struct batadv_orig_node *orig_neigh_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 		      bool in_coding)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	struct batadv_nc_node *nc_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	spinlock_t *lock; /* Used to lock list selected by "int in_coding" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	struct list_head *list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	/* Select ingoing or outgoing coding node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	if (in_coding) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 		lock = &orig_neigh_node->in_coding_list_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 		list = &orig_neigh_node->in_coding_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 		lock = &orig_neigh_node->out_coding_list_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 		list = &orig_neigh_node->out_coding_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	spin_lock_bh(lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	/* Check if nc_node is already added */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	nc_node = batadv_nc_find_nc_node(orig_node, orig_neigh_node, in_coding);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	/* Node found */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	if (nc_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	nc_node = kzalloc(sizeof(*nc_node), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	if (!nc_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	/* Initialize nc_node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	INIT_LIST_HEAD(&nc_node->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	kref_init(&nc_node->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	ether_addr_copy(nc_node->addr, orig_node->orig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	kref_get(&orig_neigh_node->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	nc_node->orig_node = orig_neigh_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	batadv_dbg(BATADV_DBG_NC, bat_priv, "Adding nc_node %pM -> %pM\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 		   nc_node->addr, nc_node->orig_node->orig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	/* Add nc_node to orig_node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	kref_get(&nc_node->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	list_add_tail_rcu(&nc_node->list, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	spin_unlock_bh(lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	return nc_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896)  * batadv_nc_update_nc_node() - updates stored incoming and outgoing nc node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897)  *  structs (best called on incoming OGMs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898)  * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899)  * @orig_node: orig node originating the ogm packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900)  * @orig_neigh_node: neighboring orig node from which we received the ogm packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901)  *  (can be equal to orig_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902)  * @ogm_packet: incoming ogm packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903)  * @is_single_hop_neigh: orig_node is a single hop neighbor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) void batadv_nc_update_nc_node(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 			      struct batadv_orig_node *orig_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 			      struct batadv_orig_node *orig_neigh_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 			      struct batadv_ogm_packet *ogm_packet,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 			      int is_single_hop_neigh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	struct batadv_nc_node *in_nc_node = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	struct batadv_nc_node *out_nc_node = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	/* Check if network coding is enabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	if (!atomic_read(&bat_priv->network_coding))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 	/* check if orig node is network coding enabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	if (!test_bit(BATADV_ORIG_CAPA_HAS_NC, &orig_node->capabilities))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	/* accept ogms from 'good' neighbors and single hop neighbors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	if (!batadv_can_nc_with_orig(bat_priv, orig_node, ogm_packet) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	    !is_single_hop_neigh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 	/* Add orig_node as in_nc_node on hop */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	in_nc_node = batadv_nc_get_nc_node(bat_priv, orig_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 					   orig_neigh_node, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	if (!in_nc_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	in_nc_node->last_seen = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	/* Add hop as out_nc_node on orig_node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	out_nc_node = batadv_nc_get_nc_node(bat_priv, orig_neigh_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 					    orig_node, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	if (!out_nc_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	out_nc_node->last_seen = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	if (in_nc_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 		batadv_nc_node_put(in_nc_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	if (out_nc_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 		batadv_nc_node_put(out_nc_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951)  * batadv_nc_get_path() - get existing nc_path or allocate a new one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952)  * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953)  * @hash: hash table containing the nc path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954)  * @src: ethernet source address - first half of the nc path search key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955)  * @dst: ethernet destination address - second half of the nc path search key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957)  * Return: pointer to nc_path if the path was found or created, returns NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958)  * on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) static struct batadv_nc_path *batadv_nc_get_path(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 						 struct batadv_hashtable *hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 						 u8 *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 						 u8 *dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	int hash_added;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	struct batadv_nc_path *nc_path, nc_path_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	batadv_nc_hash_key_gen(&nc_path_key, src, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	/* Search for existing nc_path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 	nc_path = batadv_nc_hash_find(hash, (void *)&nc_path_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	if (nc_path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 		/* Set timestamp to delay removal of nc_path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 		nc_path->last_valid = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		return nc_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	/* No existing nc_path was found; create a new */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	nc_path = kzalloc(sizeof(*nc_path), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	if (!nc_path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	/* Initialize nc_path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	INIT_LIST_HEAD(&nc_path->packet_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	spin_lock_init(&nc_path->packet_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	kref_init(&nc_path->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	nc_path->last_valid = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	ether_addr_copy(nc_path->next_hop, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	ether_addr_copy(nc_path->prev_hop, src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	batadv_dbg(BATADV_DBG_NC, bat_priv, "Adding nc_path %pM -> %pM\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 		   nc_path->prev_hop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 		   nc_path->next_hop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	/* Add nc_path to hash table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	kref_get(&nc_path->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	hash_added = batadv_hash_add(hash, batadv_nc_hash_compare,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 				     batadv_nc_hash_choose, &nc_path_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 				     &nc_path->hash_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	if (hash_added < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 		kfree(nc_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	return nc_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012)  * batadv_nc_random_weight_tq() - scale the receivers TQ-value to avoid unfair
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013)  *  selection of a receiver with slightly lower TQ than the other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014)  * @tq: to be weighted tq value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016)  * Return: scaled tq value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) static u8 batadv_nc_random_weight_tq(u8 tq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	/* randomize the estimated packet loss (max TQ - estimated TQ) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	u8 rand_tq = prandom_u32_max(BATADV_TQ_MAX_VALUE + 1 - tq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	/* convert to (randomized) estimated tq again */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	return BATADV_TQ_MAX_VALUE - rand_tq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028)  * batadv_nc_memxor() - XOR destination with source
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029)  * @dst: byte array to XOR into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030)  * @src: byte array to XOR from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031)  * @len: length of destination array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) static void batadv_nc_memxor(char *dst, const char *src, unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	for (i = 0; i < len; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 		dst[i] ^= src[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042)  * batadv_nc_code_packets() - code a received unicast_packet with an nc packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043)  *  into a coded_packet and send it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044)  * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045)  * @skb: data skb to forward
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046)  * @ethhdr: pointer to the ethernet header inside the skb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047)  * @nc_packet: structure containing the packet to the skb can be coded with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048)  * @neigh_node: next hop to forward packet to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050)  * Return: true if both packets are consumed, false otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) static bool batadv_nc_code_packets(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 				   struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 				   struct ethhdr *ethhdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 				   struct batadv_nc_packet *nc_packet,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 				   struct batadv_neigh_node *neigh_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	u8 tq_weighted_neigh, tq_weighted_coding, tq_tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	struct sk_buff *skb_dest, *skb_src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	struct batadv_unicast_packet *packet1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	struct batadv_unicast_packet *packet2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 	struct batadv_coded_packet *coded_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	struct batadv_neigh_node *neigh_tmp, *router_neigh, *first_dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 	struct batadv_neigh_node *router_coding = NULL, *second_dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	struct batadv_neigh_ifinfo *router_neigh_ifinfo = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	struct batadv_neigh_ifinfo *router_coding_ifinfo = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 	u8 *first_source, *second_source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 	__be32 packet_id1, packet_id2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	size_t count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	bool res = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	int coding_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 	int unicast_size = sizeof(*packet1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 	int coded_size = sizeof(*coded_packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	int header_add = coded_size - unicast_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 	/* TODO: do we need to consider the outgoing interface for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	 * coded packets?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 	router_neigh = batadv_orig_router_get(neigh_node->orig_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 					      BATADV_IF_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	if (!router_neigh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	router_neigh_ifinfo = batadv_neigh_ifinfo_get(router_neigh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 						      BATADV_IF_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	if (!router_neigh_ifinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	neigh_tmp = nc_packet->neigh_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	router_coding = batadv_orig_router_get(neigh_tmp->orig_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 					       BATADV_IF_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	if (!router_coding)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	router_coding_ifinfo = batadv_neigh_ifinfo_get(router_coding,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 						       BATADV_IF_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 	if (!router_coding_ifinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	tq_tmp = router_neigh_ifinfo->bat_iv.tq_avg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	tq_weighted_neigh = batadv_nc_random_weight_tq(tq_tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 	tq_tmp = router_coding_ifinfo->bat_iv.tq_avg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 	tq_weighted_coding = batadv_nc_random_weight_tq(tq_tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 	/* Select one destination for the MAC-header dst-field based on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	 * weighted TQ-values.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	if (tq_weighted_neigh >= tq_weighted_coding) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 		/* Destination from nc_packet is selected for MAC-header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 		first_dest = nc_packet->neigh_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 		first_source = nc_packet->nc_path->prev_hop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 		second_dest = neigh_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 		second_source = ethhdr->h_source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 		packet1 = (struct batadv_unicast_packet *)nc_packet->skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 		packet2 = (struct batadv_unicast_packet *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 		packet_id1 = nc_packet->packet_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 		packet_id2 = batadv_skb_crc32(skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 					      skb->data + sizeof(*packet2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 		/* Destination for skb is selected for MAC-header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 		first_dest = neigh_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 		first_source = ethhdr->h_source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 		second_dest = nc_packet->neigh_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 		second_source = nc_packet->nc_path->prev_hop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 		packet1 = (struct batadv_unicast_packet *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 		packet2 = (struct batadv_unicast_packet *)nc_packet->skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 		packet_id1 = batadv_skb_crc32(skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 					      skb->data + sizeof(*packet1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 		packet_id2 = nc_packet->packet_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	/* Instead of zero padding the smallest data buffer, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	 * code into the largest.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	if (skb->len <= nc_packet->skb->len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 		skb_dest = nc_packet->skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 		skb_src = skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 		skb_dest = skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 		skb_src = nc_packet->skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	/* coding_len is used when decoding the packet shorter packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	coding_len = skb_src->len - unicast_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	if (skb_linearize(skb_dest) < 0 || skb_linearize(skb_src) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	skb_push(skb_dest, header_add);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	coded_packet = (struct batadv_coded_packet *)skb_dest->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	skb_reset_mac_header(skb_dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	coded_packet->packet_type = BATADV_CODED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	coded_packet->version = BATADV_COMPAT_VERSION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	coded_packet->ttl = packet1->ttl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	/* Info about first unicast packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 	ether_addr_copy(coded_packet->first_source, first_source);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	ether_addr_copy(coded_packet->first_orig_dest, packet1->dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	coded_packet->first_crc = packet_id1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	coded_packet->first_ttvn = packet1->ttvn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	/* Info about second unicast packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 	ether_addr_copy(coded_packet->second_dest, second_dest->addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	ether_addr_copy(coded_packet->second_source, second_source);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	ether_addr_copy(coded_packet->second_orig_dest, packet2->dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 	coded_packet->second_crc = packet_id2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 	coded_packet->second_ttl = packet2->ttl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	coded_packet->second_ttvn = packet2->ttvn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 	coded_packet->coded_len = htons(coding_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	/* This is where the magic happens: Code skb_src into skb_dest */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	batadv_nc_memxor(skb_dest->data + coded_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 			 skb_src->data + unicast_size, coding_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 	/* Update counters accordingly */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 	if (BATADV_SKB_CB(skb_src)->decoded &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 	    BATADV_SKB_CB(skb_dest)->decoded) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 		/* Both packets are recoded */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 		count = skb_src->len + ETH_HLEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 		count += skb_dest->len + ETH_HLEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 		batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 		batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	} else if (!BATADV_SKB_CB(skb_src)->decoded &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 		   !BATADV_SKB_CB(skb_dest)->decoded) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 		/* Both packets are newly coded */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 		count = skb_src->len + ETH_HLEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 		count += skb_dest->len + ETH_HLEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 		batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 		batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 	} else if (BATADV_SKB_CB(skb_src)->decoded &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 		   !BATADV_SKB_CB(skb_dest)->decoded) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 		/* skb_src recoded and skb_dest is newly coded */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 		batadv_inc_counter(bat_priv, BATADV_CNT_NC_RECODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 		batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 				   skb_src->len + ETH_HLEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 		batadv_inc_counter(bat_priv, BATADV_CNT_NC_CODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 		batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 				   skb_dest->len + ETH_HLEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	} else if (!BATADV_SKB_CB(skb_src)->decoded &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 		   BATADV_SKB_CB(skb_dest)->decoded) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 		/* skb_src is newly coded and skb_dest is recoded */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 		batadv_inc_counter(bat_priv, BATADV_CNT_NC_CODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 		batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 				   skb_src->len + ETH_HLEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 		batadv_inc_counter(bat_priv, BATADV_CNT_NC_RECODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 		batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 				   skb_dest->len + ETH_HLEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 	/* skb_src is now coded into skb_dest, so free it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 	consume_skb(skb_src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 	/* avoid duplicate free of skb from nc_packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 	nc_packet->skb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	batadv_nc_packet_free(nc_packet, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 	/* Send the coded packet and return true */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	batadv_send_unicast_skb(skb_dest, first_dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 	res = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	if (router_neigh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 		batadv_neigh_node_put(router_neigh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	if (router_coding)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 		batadv_neigh_node_put(router_coding);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 	if (router_neigh_ifinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 		batadv_neigh_ifinfo_put(router_neigh_ifinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	if (router_coding_ifinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 		batadv_neigh_ifinfo_put(router_coding_ifinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235)  * batadv_nc_skb_coding_possible() - true if a decoded skb is available at dst.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236)  * @skb: data skb to forward
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237)  * @dst: destination mac address of the other skb to code with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238)  * @src: source mac address of skb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240)  * Whenever we network code a packet we have to check whether we received it in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241)  * a network coded form. If so, we may not be able to use it for coding because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242)  * some neighbors may also have received (overheard) the packet in the network
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243)  * coded form without being able to decode it. It is hard to know which of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244)  * neighboring nodes was able to decode the packet, therefore we can only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245)  * re-code the packet if the source of the previous encoded packet is involved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246)  * Since the source encoded the packet we can be certain it has all necessary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247)  * decode information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249)  * Return: true if coding of a decoded packet is allowed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) static bool batadv_nc_skb_coding_possible(struct sk_buff *skb, u8 *dst, u8 *src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	if (BATADV_SKB_CB(skb)->decoded && !batadv_compare_eth(dst, src))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259)  * batadv_nc_path_search() - Find the coding path matching in_nc_node and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260)  *  out_nc_node to retrieve a buffered packet that can be used for coding.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261)  * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262)  * @in_nc_node: pointer to skb next hop's neighbor nc node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263)  * @out_nc_node: pointer to skb source's neighbor nc node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264)  * @skb: data skb to forward
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265)  * @eth_dst: next hop mac address of skb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267)  * Return: true if coding of a decoded skb is allowed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) static struct batadv_nc_packet *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) batadv_nc_path_search(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 		      struct batadv_nc_node *in_nc_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 		      struct batadv_nc_node *out_nc_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 		      struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 		      u8 *eth_dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 	struct batadv_nc_path *nc_path, nc_path_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 	struct batadv_nc_packet *nc_packet_out = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 	struct batadv_nc_packet *nc_packet, *nc_packet_tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 	struct batadv_hashtable *hash = bat_priv->nc.coding_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 	int idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 	if (!hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 	/* Create almost path key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 	batadv_nc_hash_key_gen(&nc_path_key, in_nc_node->addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 			       out_nc_node->addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 	idx = batadv_nc_hash_choose(&nc_path_key, hash->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 	/* Check for coding opportunities in this nc_path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 	hlist_for_each_entry_rcu(nc_path, &hash->table[idx], hash_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 		if (!batadv_compare_eth(nc_path->prev_hop, in_nc_node->addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 		if (!batadv_compare_eth(nc_path->next_hop, out_nc_node->addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 		spin_lock_bh(&nc_path->packet_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 		if (list_empty(&nc_path->packet_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 			spin_unlock_bh(&nc_path->packet_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 		list_for_each_entry_safe(nc_packet, nc_packet_tmp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 					 &nc_path->packet_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 			if (!batadv_nc_skb_coding_possible(nc_packet->skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 							   eth_dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 							   in_nc_node->addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 			/* Coding opportunity is found! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 			list_del(&nc_packet->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 			nc_packet_out = nc_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 		spin_unlock_bh(&nc_path->packet_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 	return nc_packet_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327)  * batadv_nc_skb_src_search() - Loops through the list of neighboring nodes of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328)  *  the skb's sender (may be equal to the originator).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329)  * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330)  * @skb: data skb to forward
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331)  * @eth_dst: next hop mac address of skb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332)  * @eth_src: source mac address of skb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333)  * @in_nc_node: pointer to skb next hop's neighbor nc node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335)  * Return: an nc packet if a suitable coding packet was found, NULL otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) static struct batadv_nc_packet *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) batadv_nc_skb_src_search(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 			 struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 			 u8 *eth_dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 			 u8 *eth_src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 			 struct batadv_nc_node *in_nc_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 	struct batadv_orig_node *orig_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 	struct batadv_nc_node *out_nc_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 	struct batadv_nc_packet *nc_packet = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 	orig_node = batadv_orig_hash_find(bat_priv, eth_src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 	if (!orig_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 	list_for_each_entry_rcu(out_nc_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 				&orig_node->out_coding_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 		/* Check if the skb is decoded and if recoding is possible */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 		if (!batadv_nc_skb_coding_possible(skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 						   out_nc_node->addr, eth_src))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 		/* Search for an opportunity in this nc_path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 		nc_packet = batadv_nc_path_search(bat_priv, in_nc_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 						  out_nc_node, skb, eth_dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 		if (nc_packet)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 	batadv_orig_node_put(orig_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 	return nc_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373)  * batadv_nc_skb_store_before_coding() - set the ethernet src and dst of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374)  *  unicast skb before it is stored for use in later decoding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375)  * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376)  * @skb: data skb to store
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377)  * @eth_dst_new: new destination mac address of skb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) static void batadv_nc_skb_store_before_coding(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 					      struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 					      u8 *eth_dst_new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 	struct ethhdr *ethhdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 	/* Copy skb header to change the mac header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 	skb = pskb_copy_for_clone(skb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 	/* Set the mac header as if we actually sent the packet uncoded */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 	ethhdr = eth_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 	ether_addr_copy(ethhdr->h_source, ethhdr->h_dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 	ether_addr_copy(ethhdr->h_dest, eth_dst_new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 	/* Set data pointer to MAC header to mimic packets from our tx path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	skb_push(skb, ETH_HLEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 	/* Add the packet to the decoding packet pool */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 	batadv_nc_skb_store_for_decoding(bat_priv, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 	/* batadv_nc_skb_store_for_decoding() clones the skb, so we must free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 	 * our ref
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 	consume_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408)  * batadv_nc_skb_dst_search() - Loops through list of neighboring nodes to dst.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409)  * @skb: data skb to forward
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410)  * @neigh_node: next hop to forward packet to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411)  * @ethhdr: pointer to the ethernet header inside the skb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413)  * Loops through the list of neighboring nodes the next hop has a good
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414)  * connection to (receives OGMs with a sufficient quality). We need to find a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415)  * neighbor of our next hop that potentially sent a packet which our next hop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416)  * also received (overheard) and has stored for later decoding.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418)  * Return: true if the skb was consumed (encoded packet sent) or false otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) static bool batadv_nc_skb_dst_search(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 				     struct batadv_neigh_node *neigh_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 				     struct ethhdr *ethhdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 	struct net_device *netdev = neigh_node->if_incoming->soft_iface;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 	struct batadv_priv *bat_priv = netdev_priv(netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 	struct batadv_orig_node *orig_node = neigh_node->orig_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 	struct batadv_nc_node *nc_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 	struct batadv_nc_packet *nc_packet = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 	list_for_each_entry_rcu(nc_node, &orig_node->in_coding_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 		/* Search for coding opportunity with this in_nc_node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 		nc_packet = batadv_nc_skb_src_search(bat_priv, skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 						     neigh_node->addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 						     ethhdr->h_source, nc_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 		/* Opportunity was found, so stop searching */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 		if (nc_packet)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 	if (!nc_packet)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 	/* Save packets for later decoding */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 	batadv_nc_skb_store_before_coding(bat_priv, skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 					  neigh_node->addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 	batadv_nc_skb_store_before_coding(bat_priv, nc_packet->skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 					  nc_packet->neigh_node->addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 	/* Code and send packets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 	if (batadv_nc_code_packets(bat_priv, skb, ethhdr, nc_packet,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 				   neigh_node))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 	/* out of mem ? Coding failed - we have to free the buffered packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	 * to avoid memleaks. The skb passed as argument will be dealt with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	 * by the calling function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	batadv_nc_send_packet(nc_packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466)  * batadv_nc_skb_add_to_path() - buffer skb for later encoding / decoding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467)  * @skb: skb to add to path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468)  * @nc_path: path to add skb to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469)  * @neigh_node: next hop to forward packet to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470)  * @packet_id: checksum to identify packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472)  * Return: true if the packet was buffered or false in case of an error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) static bool batadv_nc_skb_add_to_path(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 				      struct batadv_nc_path *nc_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 				      struct batadv_neigh_node *neigh_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 				      __be32 packet_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 	struct batadv_nc_packet *nc_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 	nc_packet = kzalloc(sizeof(*nc_packet), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 	if (!nc_packet)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 	/* Initialize nc_packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 	nc_packet->timestamp = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 	nc_packet->packet_id = packet_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 	nc_packet->skb = skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 	nc_packet->neigh_node = neigh_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 	nc_packet->nc_path = nc_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 	/* Add coding packet to list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 	spin_lock_bh(&nc_path->packet_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 	list_add_tail(&nc_packet->list, &nc_path->packet_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 	spin_unlock_bh(&nc_path->packet_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501)  * batadv_nc_skb_forward() - try to code a packet or add it to the coding packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502)  *  buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503)  * @skb: data skb to forward
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504)  * @neigh_node: next hop to forward packet to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506)  * Return: true if the skb was consumed (encoded packet sent) or false otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) bool batadv_nc_skb_forward(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 			   struct batadv_neigh_node *neigh_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 	const struct net_device *netdev = neigh_node->if_incoming->soft_iface;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 	struct batadv_priv *bat_priv = netdev_priv(netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 	struct batadv_unicast_packet *packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	struct batadv_nc_path *nc_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 	struct ethhdr *ethhdr = eth_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 	__be32 packet_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 	u8 *payload;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 	/* Check if network coding is enabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 	if (!atomic_read(&bat_priv->network_coding))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 	/* We only handle unicast packets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 	payload = skb_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 	packet = (struct batadv_unicast_packet *)payload;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 	if (packet->packet_type != BATADV_UNICAST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 	/* Try to find a coding opportunity and send the skb if one is found */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 	if (batadv_nc_skb_dst_search(skb, neigh_node, ethhdr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 	/* Find or create a nc_path for this src-dst pair */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	nc_path = batadv_nc_get_path(bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 				     bat_priv->nc.coding_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 				     ethhdr->h_source,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 				     neigh_node->addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 	if (!nc_path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 	/* Add skb to nc_path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 	packet_id = batadv_skb_crc32(skb, payload + sizeof(*packet));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 	if (!batadv_nc_skb_add_to_path(skb, nc_path, neigh_node, packet_id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 		goto free_nc_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 	/* Packet is consumed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) free_nc_path:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 	batadv_nc_path_put(nc_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 	/* Packet is not consumed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558)  * batadv_nc_skb_store_for_decoding() - save a clone of the skb which can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559)  *  used when decoding coded packets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560)  * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561)  * @skb: data skb to store
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) void batadv_nc_skb_store_for_decoding(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 				      struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 	struct batadv_unicast_packet *packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 	struct batadv_nc_path *nc_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 	struct ethhdr *ethhdr = eth_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 	__be32 packet_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 	u8 *payload;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 	/* Check if network coding is enabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 	if (!atomic_read(&bat_priv->network_coding))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 	/* Check for supported packet type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 	payload = skb_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 	packet = (struct batadv_unicast_packet *)payload;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 	if (packet->packet_type != BATADV_UNICAST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 	/* Find existing nc_path or create a new */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 	nc_path = batadv_nc_get_path(bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 				     bat_priv->nc.decoding_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 				     ethhdr->h_source,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 				     ethhdr->h_dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 	if (!nc_path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 	/* Clone skb and adjust skb->data to point at batman header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 	skb = skb_clone(skb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 	if (unlikely(!skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 		goto free_nc_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 	if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 		goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 	if (unlikely(!skb_pull_rcsum(skb, ETH_HLEN)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 		goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 	/* Add skb to nc_path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 	packet_id = batadv_skb_crc32(skb, payload + sizeof(*packet));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 	if (!batadv_nc_skb_add_to_path(skb, nc_path, NULL, packet_id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 		goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 	batadv_inc_counter(bat_priv, BATADV_CNT_NC_BUFFER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) free_skb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 	kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) free_nc_path:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 	batadv_nc_path_put(nc_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619)  * batadv_nc_skb_store_sniffed_unicast() - check if a received unicast packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620)  *  should be saved in the decoding buffer and, if so, store it there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621)  * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622)  * @skb: unicast skb to store
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) void batadv_nc_skb_store_sniffed_unicast(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 					 struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 	struct ethhdr *ethhdr = eth_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 	if (batadv_is_my_mac(bat_priv, ethhdr->h_dest))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 	/* Set data pointer to MAC header to mimic packets from our tx path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 	skb_push(skb, ETH_HLEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 	batadv_nc_skb_store_for_decoding(bat_priv, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639)  * batadv_nc_skb_decode_packet() - decode given skb using the decode data stored
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640)  *  in nc_packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641)  * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642)  * @skb: unicast skb to decode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643)  * @nc_packet: decode data needed to decode the skb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645)  * Return: pointer to decoded unicast packet if the packet was decoded or NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646)  * in case of an error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) static struct batadv_unicast_packet *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) batadv_nc_skb_decode_packet(struct batadv_priv *bat_priv, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 			    struct batadv_nc_packet *nc_packet)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 	const int h_size = sizeof(struct batadv_unicast_packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 	const int h_diff = sizeof(struct batadv_coded_packet) - h_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 	struct batadv_unicast_packet *unicast_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 	struct batadv_coded_packet coded_packet_tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 	struct ethhdr *ethhdr, ethhdr_tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 	u8 *orig_dest, ttl, ttvn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 	unsigned int coding_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 	/* Save headers temporarily */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 	memcpy(&coded_packet_tmp, skb->data, sizeof(coded_packet_tmp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 	memcpy(&ethhdr_tmp, skb_mac_header(skb), sizeof(ethhdr_tmp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 	if (skb_cow(skb, 0) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 	if (unlikely(!skb_pull_rcsum(skb, h_diff)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 	/* Data points to batman header, so set mac header 14 bytes before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 	 * and network to data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 	skb_set_mac_header(skb, -ETH_HLEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 	skb_reset_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 	/* Reconstruct original mac header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 	ethhdr = eth_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 	*ethhdr = ethhdr_tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 	/* Select the correct unicast header information based on the location
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 	 * of our mac address in the coded_packet header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 	if (batadv_is_my_mac(bat_priv, coded_packet_tmp.second_dest)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 		/* If we are the second destination the packet was overheard,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 		 * so the Ethernet address must be copied to h_dest and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 		 * pkt_type changed from PACKET_OTHERHOST to PACKET_HOST
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 		ether_addr_copy(ethhdr->h_dest, coded_packet_tmp.second_dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 		skb->pkt_type = PACKET_HOST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 		orig_dest = coded_packet_tmp.second_orig_dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 		ttl = coded_packet_tmp.second_ttl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 		ttvn = coded_packet_tmp.second_ttvn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 		orig_dest = coded_packet_tmp.first_orig_dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 		ttl = coded_packet_tmp.ttl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 		ttvn = coded_packet_tmp.first_ttvn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 	coding_len = ntohs(coded_packet_tmp.coded_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 	if (coding_len > skb->len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 	/* Here the magic is reversed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 	 *   extract the missing packet from the received coded packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 	batadv_nc_memxor(skb->data + h_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 			 nc_packet->skb->data + h_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 			 coding_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 	/* Resize decoded skb if decoded with larger packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 	if (nc_packet->skb->len > coding_len + h_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 		err = pskb_trim_rcsum(skb, coding_len + h_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 	/* Create decoded unicast packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 	unicast_packet = (struct batadv_unicast_packet *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 	unicast_packet->packet_type = BATADV_UNICAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 	unicast_packet->version = BATADV_COMPAT_VERSION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 	unicast_packet->ttl = ttl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 	ether_addr_copy(unicast_packet->dest, orig_dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 	unicast_packet->ttvn = ttvn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 	batadv_nc_packet_free(nc_packet, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 	return unicast_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733)  * batadv_nc_find_decoding_packet() - search through buffered decoding data to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734)  *  find the data needed to decode the coded packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735)  * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736)  * @ethhdr: pointer to the ethernet header inside the coded packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737)  * @coded: coded packet we try to find decode data for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739)  * Return: pointer to nc packet if the needed data was found or NULL otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) static struct batadv_nc_packet *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) batadv_nc_find_decoding_packet(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 			       struct ethhdr *ethhdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 			       struct batadv_coded_packet *coded)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 	struct batadv_hashtable *hash = bat_priv->nc.decoding_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 	struct batadv_nc_packet *tmp_nc_packet, *nc_packet = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 	struct batadv_nc_path *nc_path, nc_path_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 	u8 *dest, *source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 	__be32 packet_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 	int index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 	if (!hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 	/* Select the correct packet id based on the location of our mac-addr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 	dest = ethhdr->h_source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 	if (!batadv_is_my_mac(bat_priv, coded->second_dest)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 		source = coded->second_source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 		packet_id = coded->second_crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 		source = coded->first_source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 		packet_id = coded->first_crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 	batadv_nc_hash_key_gen(&nc_path_key, source, dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 	index = batadv_nc_hash_choose(&nc_path_key, hash->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 	/* Search for matching coding path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 	hlist_for_each_entry_rcu(nc_path, &hash->table[index], hash_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 		/* Find matching nc_packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 		spin_lock_bh(&nc_path->packet_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 		list_for_each_entry(tmp_nc_packet,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 				    &nc_path->packet_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 			if (packet_id == tmp_nc_packet->packet_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 				list_del(&tmp_nc_packet->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 				nc_packet = tmp_nc_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 		spin_unlock_bh(&nc_path->packet_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 		if (nc_packet)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 	if (!nc_packet)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 		batadv_dbg(BATADV_DBG_NC, bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 			   "No decoding packet found for %u\n", packet_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 	return nc_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798)  * batadv_nc_recv_coded_packet() - try to decode coded packet and enqueue the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799)  *  resulting unicast packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800)  * @skb: incoming coded packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801)  * @recv_if: pointer to interface this packet was received on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803)  * Return: NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804)  * otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) static int batadv_nc_recv_coded_packet(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 				       struct batadv_hard_iface *recv_if)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 	struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 	struct batadv_unicast_packet *unicast_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 	struct batadv_coded_packet *coded_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 	struct batadv_nc_packet *nc_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 	struct ethhdr *ethhdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 	int hdr_size = sizeof(*coded_packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 	/* Check if network coding is enabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 	if (!atomic_read(&bat_priv->network_coding))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 		goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 	/* Make sure we can access (and remove) header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 	if (unlikely(!pskb_may_pull(skb, hdr_size)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 		goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 	coded_packet = (struct batadv_coded_packet *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 	ethhdr = eth_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 	/* Verify frame is destined for us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 	if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 	    !batadv_is_my_mac(bat_priv, coded_packet->second_dest))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 		goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 	/* Update stat counter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 	if (batadv_is_my_mac(bat_priv, coded_packet->second_dest))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 		batadv_inc_counter(bat_priv, BATADV_CNT_NC_SNIFFED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 	nc_packet = batadv_nc_find_decoding_packet(bat_priv, ethhdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 						   coded_packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 	if (!nc_packet) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 		batadv_inc_counter(bat_priv, BATADV_CNT_NC_DECODE_FAILED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 		goto free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 	/* Make skb's linear, because decoding accesses the entire buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 	if (skb_linearize(skb) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 		goto free_nc_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 	if (skb_linearize(nc_packet->skb) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 		goto free_nc_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 	/* Decode the packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 	unicast_packet = batadv_nc_skb_decode_packet(bat_priv, skb, nc_packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 	if (!unicast_packet) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 		batadv_inc_counter(bat_priv, BATADV_CNT_NC_DECODE_FAILED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 		goto free_nc_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 	/* Mark packet as decoded to do correct recoding when forwarding */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 	BATADV_SKB_CB(skb)->decoded = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 	batadv_inc_counter(bat_priv, BATADV_CNT_NC_DECODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 	batadv_add_counter(bat_priv, BATADV_CNT_NC_DECODE_BYTES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 			   skb->len + ETH_HLEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 	return batadv_recv_unicast_packet(skb, recv_if);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) free_nc_packet:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 	batadv_nc_packet_free(nc_packet, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) free_skb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 	kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 	return NET_RX_DROP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873)  * batadv_nc_mesh_free() - clean up network coding memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874)  * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) void batadv_nc_mesh_free(struct batadv_priv *bat_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 	batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_NC, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 	batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_NC, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 	cancel_delayed_work_sync(&bat_priv->nc.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 	batadv_nc_purge_paths(bat_priv, bat_priv->nc.coding_hash, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 	batadv_hash_destroy(bat_priv->nc.coding_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 	batadv_nc_purge_paths(bat_priv, bat_priv->nc.decoding_hash, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 	batadv_hash_destroy(bat_priv->nc.decoding_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) #ifdef CONFIG_BATMAN_ADV_DEBUGFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890)  * batadv_nc_nodes_seq_print_text() - print the nc node information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891)  * @seq: seq file to print on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892)  * @offset: not used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894)  * Return: always 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) int batadv_nc_nodes_seq_print_text(struct seq_file *seq, void *offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 	struct net_device *net_dev = (struct net_device *)seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 	struct batadv_priv *bat_priv = netdev_priv(net_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 	struct batadv_hashtable *hash = bat_priv->orig_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 	struct batadv_hard_iface *primary_if;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 	struct hlist_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 	struct batadv_orig_node *orig_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 	struct batadv_nc_node *nc_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 	primary_if = batadv_seq_print_text_primary_if_get(seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 	if (!primary_if)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 	/* Traverse list of originators */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 	for (i = 0; i < hash->size; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 		head = &hash->table[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 		/* For each orig_node in this bin */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 		rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 		hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 			/* no need to print the orig node if it does not have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 			 * network coding neighbors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 			if (list_empty(&orig_node->in_coding_list) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 			    list_empty(&orig_node->out_coding_list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 			seq_printf(seq, "Node:      %pM\n", orig_node->orig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) 			seq_puts(seq, " Ingoing:  ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 			/* For each in_nc_node to this orig_node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 			list_for_each_entry_rcu(nc_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 						&orig_node->in_coding_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 						list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 				seq_printf(seq, "%pM ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 					   nc_node->addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 			seq_puts(seq, "\n Outgoing: ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 			/* For out_nc_node to this orig_node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 			list_for_each_entry_rcu(nc_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 						&orig_node->out_coding_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 						list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 				seq_printf(seq, "%pM ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 					   nc_node->addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 			seq_puts(seq, "\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 		rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 	if (primary_if)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 		batadv_hardif_put(primary_if);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953)  * batadv_nc_init_debugfs() - create nc folder and related files in debugfs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954)  * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) void batadv_nc_init_debugfs(struct batadv_priv *bat_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 	struct dentry *nc_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 	nc_dir = debugfs_create_dir("nc", bat_priv->debug_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 	debugfs_create_u8("min_tq", 0644, nc_dir, &bat_priv->nc.min_tq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 	debugfs_create_u32("max_fwd_delay", 0644, nc_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 			   &bat_priv->nc.max_fwd_delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 	debugfs_create_u32("max_buffer_time", 0644, nc_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 			   &bat_priv->nc.max_buffer_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) #endif