162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/* Copyright (C) B.A.T.M.A.N. contributors:
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Martin Hundebøll, Jeppe Ledet-Pedersen
562306a36Sopenharmony_ci */
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci#include "network-coding.h"
862306a36Sopenharmony_ci#include "main.h"
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include <linux/atomic.h>
1162306a36Sopenharmony_ci#include <linux/bitops.h>
1262306a36Sopenharmony_ci#include <linux/byteorder/generic.h>
1362306a36Sopenharmony_ci#include <linux/compiler.h>
1462306a36Sopenharmony_ci#include <linux/container_of.h>
1562306a36Sopenharmony_ci#include <linux/errno.h>
1662306a36Sopenharmony_ci#include <linux/etherdevice.h>
1762306a36Sopenharmony_ci#include <linux/gfp.h>
1862306a36Sopenharmony_ci#include <linux/if_ether.h>
1962306a36Sopenharmony_ci#include <linux/if_packet.h>
2062306a36Sopenharmony_ci#include <linux/init.h>
2162306a36Sopenharmony_ci#include <linux/jhash.h>
2262306a36Sopenharmony_ci#include <linux/jiffies.h>
2362306a36Sopenharmony_ci#include <linux/kref.h>
2462306a36Sopenharmony_ci#include <linux/list.h>
2562306a36Sopenharmony_ci#include <linux/lockdep.h>
2662306a36Sopenharmony_ci#include <linux/net.h>
2762306a36Sopenharmony_ci#include <linux/netdevice.h>
2862306a36Sopenharmony_ci#include <linux/printk.h>
2962306a36Sopenharmony_ci#include <linux/random.h>
3062306a36Sopenharmony_ci#include <linux/rculist.h>
3162306a36Sopenharmony_ci#include <linux/rcupdate.h>
3262306a36Sopenharmony_ci#include <linux/skbuff.h>
3362306a36Sopenharmony_ci#include <linux/slab.h>
3462306a36Sopenharmony_ci#include <linux/spinlock.h>
3562306a36Sopenharmony_ci#include <linux/stddef.h>
3662306a36Sopenharmony_ci#include <linux/string.h>
3762306a36Sopenharmony_ci#include <linux/workqueue.h>
3862306a36Sopenharmony_ci#include <uapi/linux/batadv_packet.h>
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci#include "hash.h"
4162306a36Sopenharmony_ci#include "log.h"
4262306a36Sopenharmony_ci#include "originator.h"
4362306a36Sopenharmony_ci#include "routing.h"
4462306a36Sopenharmony_ci#include "send.h"
4562306a36Sopenharmony_ci#include "tvlv.h"
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_cistatic struct lock_class_key batadv_nc_coding_hash_lock_class_key;
4862306a36Sopenharmony_cistatic struct lock_class_key batadv_nc_decoding_hash_lock_class_key;
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_cistatic void batadv_nc_worker(struct work_struct *work);
5162306a36Sopenharmony_cistatic int batadv_nc_recv_coded_packet(struct sk_buff *skb,
5262306a36Sopenharmony_ci				       struct batadv_hard_iface *recv_if);
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci/**
5562306a36Sopenharmony_ci * batadv_nc_init() - one-time initialization for network coding
5662306a36Sopenharmony_ci *
5762306a36Sopenharmony_ci * Return: 0 on success or negative error number in case of failure
5862306a36Sopenharmony_ci */
5962306a36Sopenharmony_ciint __init batadv_nc_init(void)
6062306a36Sopenharmony_ci{
6162306a36Sopenharmony_ci	/* Register our packet type */
6262306a36Sopenharmony_ci	return batadv_recv_handler_register(BATADV_CODED,
6362306a36Sopenharmony_ci					    batadv_nc_recv_coded_packet);
6462306a36Sopenharmony_ci}
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci/**
6762306a36Sopenharmony_ci * batadv_nc_start_timer() - initialise the nc periodic worker
6862306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
6962306a36Sopenharmony_ci */
7062306a36Sopenharmony_cistatic void batadv_nc_start_timer(struct batadv_priv *bat_priv)
7162306a36Sopenharmony_ci{
7262306a36Sopenharmony_ci	queue_delayed_work(batadv_event_workqueue, &bat_priv->nc.work,
7362306a36Sopenharmony_ci			   msecs_to_jiffies(10));
7462306a36Sopenharmony_ci}
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci/**
7762306a36Sopenharmony_ci * batadv_nc_tvlv_container_update() - update the network coding tvlv container
7862306a36Sopenharmony_ci *  after network coding setting change
7962306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
8062306a36Sopenharmony_ci */
8162306a36Sopenharmony_cistatic void batadv_nc_tvlv_container_update(struct batadv_priv *bat_priv)
8262306a36Sopenharmony_ci{
8362306a36Sopenharmony_ci	char nc_mode;
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci	nc_mode = atomic_read(&bat_priv->network_coding);
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci	switch (nc_mode) {
8862306a36Sopenharmony_ci	case 0:
8962306a36Sopenharmony_ci		batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_NC, 1);
9062306a36Sopenharmony_ci		break;
9162306a36Sopenharmony_ci	case 1:
9262306a36Sopenharmony_ci		batadv_tvlv_container_register(bat_priv, BATADV_TVLV_NC, 1,
9362306a36Sopenharmony_ci					       NULL, 0);
9462306a36Sopenharmony_ci		break;
9562306a36Sopenharmony_ci	}
9662306a36Sopenharmony_ci}
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci/**
9962306a36Sopenharmony_ci * batadv_nc_status_update() - update the network coding tvlv container after
10062306a36Sopenharmony_ci *  network coding setting change
10162306a36Sopenharmony_ci * @net_dev: the soft interface net device
10262306a36Sopenharmony_ci */
10362306a36Sopenharmony_civoid batadv_nc_status_update(struct net_device *net_dev)
10462306a36Sopenharmony_ci{
10562306a36Sopenharmony_ci	struct batadv_priv *bat_priv = netdev_priv(net_dev);
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci	batadv_nc_tvlv_container_update(bat_priv);
10862306a36Sopenharmony_ci}
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci/**
11162306a36Sopenharmony_ci * batadv_nc_tvlv_ogm_handler_v1() - process incoming nc tvlv container
11262306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
11362306a36Sopenharmony_ci * @orig: the orig_node of the ogm
11462306a36Sopenharmony_ci * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
11562306a36Sopenharmony_ci * @tvlv_value: tvlv buffer containing the gateway data
11662306a36Sopenharmony_ci * @tvlv_value_len: tvlv buffer length
11762306a36Sopenharmony_ci */
11862306a36Sopenharmony_cistatic void batadv_nc_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
11962306a36Sopenharmony_ci					  struct batadv_orig_node *orig,
12062306a36Sopenharmony_ci					  u8 flags,
12162306a36Sopenharmony_ci					  void *tvlv_value, u16 tvlv_value_len)
12262306a36Sopenharmony_ci{
12362306a36Sopenharmony_ci	if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND)
12462306a36Sopenharmony_ci		clear_bit(BATADV_ORIG_CAPA_HAS_NC, &orig->capabilities);
12562306a36Sopenharmony_ci	else
12662306a36Sopenharmony_ci		set_bit(BATADV_ORIG_CAPA_HAS_NC, &orig->capabilities);
12762306a36Sopenharmony_ci}
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci/**
13062306a36Sopenharmony_ci * batadv_nc_mesh_init() - initialise coding hash table and start housekeeping
13162306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
13262306a36Sopenharmony_ci *
13362306a36Sopenharmony_ci * Return: 0 on success or negative error number in case of failure
13462306a36Sopenharmony_ci */
13562306a36Sopenharmony_ciint batadv_nc_mesh_init(struct batadv_priv *bat_priv)
13662306a36Sopenharmony_ci{
13762306a36Sopenharmony_ci	bat_priv->nc.timestamp_fwd_flush = jiffies;
13862306a36Sopenharmony_ci	bat_priv->nc.timestamp_sniffed_purge = jiffies;
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci	if (bat_priv->nc.coding_hash || bat_priv->nc.decoding_hash)
14162306a36Sopenharmony_ci		return 0;
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci	bat_priv->nc.coding_hash = batadv_hash_new(128);
14462306a36Sopenharmony_ci	if (!bat_priv->nc.coding_hash)
14562306a36Sopenharmony_ci		goto err;
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci	batadv_hash_set_lock_class(bat_priv->nc.coding_hash,
14862306a36Sopenharmony_ci				   &batadv_nc_coding_hash_lock_class_key);
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci	bat_priv->nc.decoding_hash = batadv_hash_new(128);
15162306a36Sopenharmony_ci	if (!bat_priv->nc.decoding_hash) {
15262306a36Sopenharmony_ci		batadv_hash_destroy(bat_priv->nc.coding_hash);
15362306a36Sopenharmony_ci		goto err;
15462306a36Sopenharmony_ci	}
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci	batadv_hash_set_lock_class(bat_priv->nc.decoding_hash,
15762306a36Sopenharmony_ci				   &batadv_nc_decoding_hash_lock_class_key);
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci	INIT_DELAYED_WORK(&bat_priv->nc.work, batadv_nc_worker);
16062306a36Sopenharmony_ci	batadv_nc_start_timer(bat_priv);
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci	batadv_tvlv_handler_register(bat_priv, batadv_nc_tvlv_ogm_handler_v1,
16362306a36Sopenharmony_ci				     NULL, NULL, BATADV_TVLV_NC, 1,
16462306a36Sopenharmony_ci				     BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
16562306a36Sopenharmony_ci	batadv_nc_tvlv_container_update(bat_priv);
16662306a36Sopenharmony_ci	return 0;
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_cierr:
16962306a36Sopenharmony_ci	return -ENOMEM;
17062306a36Sopenharmony_ci}
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci/**
17362306a36Sopenharmony_ci * batadv_nc_init_bat_priv() - initialise the nc specific bat_priv variables
17462306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
17562306a36Sopenharmony_ci */
17662306a36Sopenharmony_civoid batadv_nc_init_bat_priv(struct batadv_priv *bat_priv)
17762306a36Sopenharmony_ci{
17862306a36Sopenharmony_ci	atomic_set(&bat_priv->network_coding, 0);
17962306a36Sopenharmony_ci	bat_priv->nc.min_tq = 200;
18062306a36Sopenharmony_ci	bat_priv->nc.max_fwd_delay = 10;
18162306a36Sopenharmony_ci	bat_priv->nc.max_buffer_time = 200;
18262306a36Sopenharmony_ci}
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ci/**
18562306a36Sopenharmony_ci * batadv_nc_init_orig() - initialise the nc fields of an orig_node
18662306a36Sopenharmony_ci * @orig_node: the orig_node which is going to be initialised
18762306a36Sopenharmony_ci */
18862306a36Sopenharmony_civoid batadv_nc_init_orig(struct batadv_orig_node *orig_node)
18962306a36Sopenharmony_ci{
19062306a36Sopenharmony_ci	INIT_LIST_HEAD(&orig_node->in_coding_list);
19162306a36Sopenharmony_ci	INIT_LIST_HEAD(&orig_node->out_coding_list);
19262306a36Sopenharmony_ci	spin_lock_init(&orig_node->in_coding_list_lock);
19362306a36Sopenharmony_ci	spin_lock_init(&orig_node->out_coding_list_lock);
19462306a36Sopenharmony_ci}
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci/**
19762306a36Sopenharmony_ci * batadv_nc_node_release() - release nc_node from lists and queue for free
19862306a36Sopenharmony_ci *  after rcu grace period
19962306a36Sopenharmony_ci * @ref: kref pointer of the nc_node
20062306a36Sopenharmony_ci */
20162306a36Sopenharmony_cistatic void batadv_nc_node_release(struct kref *ref)
20262306a36Sopenharmony_ci{
20362306a36Sopenharmony_ci	struct batadv_nc_node *nc_node;
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ci	nc_node = container_of(ref, struct batadv_nc_node, refcount);
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ci	batadv_orig_node_put(nc_node->orig_node);
20862306a36Sopenharmony_ci	kfree_rcu(nc_node, rcu);
20962306a36Sopenharmony_ci}
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_ci/**
21262306a36Sopenharmony_ci * batadv_nc_node_put() - decrement the nc_node refcounter and possibly
21362306a36Sopenharmony_ci *  release it
21462306a36Sopenharmony_ci * @nc_node: nc_node to be free'd
21562306a36Sopenharmony_ci */
21662306a36Sopenharmony_cistatic void batadv_nc_node_put(struct batadv_nc_node *nc_node)
21762306a36Sopenharmony_ci{
21862306a36Sopenharmony_ci	if (!nc_node)
21962306a36Sopenharmony_ci		return;
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_ci	kref_put(&nc_node->refcount, batadv_nc_node_release);
22262306a36Sopenharmony_ci}
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_ci/**
22562306a36Sopenharmony_ci * batadv_nc_path_release() - release nc_path from lists and queue for free
22662306a36Sopenharmony_ci *  after rcu grace period
22762306a36Sopenharmony_ci * @ref: kref pointer of the nc_path
22862306a36Sopenharmony_ci */
22962306a36Sopenharmony_cistatic void batadv_nc_path_release(struct kref *ref)
23062306a36Sopenharmony_ci{
23162306a36Sopenharmony_ci	struct batadv_nc_path *nc_path;
23262306a36Sopenharmony_ci
23362306a36Sopenharmony_ci	nc_path = container_of(ref, struct batadv_nc_path, refcount);
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ci	kfree_rcu(nc_path, rcu);
23662306a36Sopenharmony_ci}
23762306a36Sopenharmony_ci
23862306a36Sopenharmony_ci/**
23962306a36Sopenharmony_ci * batadv_nc_path_put() - decrement the nc_path refcounter and possibly
24062306a36Sopenharmony_ci *  release it
24162306a36Sopenharmony_ci * @nc_path: nc_path to be free'd
24262306a36Sopenharmony_ci */
24362306a36Sopenharmony_cistatic void batadv_nc_path_put(struct batadv_nc_path *nc_path)
24462306a36Sopenharmony_ci{
24562306a36Sopenharmony_ci	if (!nc_path)
24662306a36Sopenharmony_ci		return;
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_ci	kref_put(&nc_path->refcount, batadv_nc_path_release);
24962306a36Sopenharmony_ci}
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_ci/**
25262306a36Sopenharmony_ci * batadv_nc_packet_free() - frees nc packet
25362306a36Sopenharmony_ci * @nc_packet: the nc packet to free
25462306a36Sopenharmony_ci * @dropped: whether the packet is freed because is dropped
25562306a36Sopenharmony_ci */
25662306a36Sopenharmony_cistatic void batadv_nc_packet_free(struct batadv_nc_packet *nc_packet,
25762306a36Sopenharmony_ci				  bool dropped)
25862306a36Sopenharmony_ci{
25962306a36Sopenharmony_ci	if (dropped)
26062306a36Sopenharmony_ci		kfree_skb(nc_packet->skb);
26162306a36Sopenharmony_ci	else
26262306a36Sopenharmony_ci		consume_skb(nc_packet->skb);
26362306a36Sopenharmony_ci
26462306a36Sopenharmony_ci	batadv_nc_path_put(nc_packet->nc_path);
26562306a36Sopenharmony_ci	kfree(nc_packet);
26662306a36Sopenharmony_ci}
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_ci/**
26962306a36Sopenharmony_ci * batadv_nc_to_purge_nc_node() - checks whether an nc node has to be purged
27062306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
27162306a36Sopenharmony_ci * @nc_node: the nc node to check
27262306a36Sopenharmony_ci *
27362306a36Sopenharmony_ci * Return: true if the entry has to be purged now, false otherwise
27462306a36Sopenharmony_ci */
27562306a36Sopenharmony_cistatic bool batadv_nc_to_purge_nc_node(struct batadv_priv *bat_priv,
27662306a36Sopenharmony_ci				       struct batadv_nc_node *nc_node)
27762306a36Sopenharmony_ci{
27862306a36Sopenharmony_ci	if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
27962306a36Sopenharmony_ci		return true;
28062306a36Sopenharmony_ci
28162306a36Sopenharmony_ci	return batadv_has_timed_out(nc_node->last_seen, BATADV_NC_NODE_TIMEOUT);
28262306a36Sopenharmony_ci}
28362306a36Sopenharmony_ci
28462306a36Sopenharmony_ci/**
28562306a36Sopenharmony_ci * batadv_nc_to_purge_nc_path_coding() - checks whether an nc path has timed out
28662306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
28762306a36Sopenharmony_ci * @nc_path: the nc path to check
28862306a36Sopenharmony_ci *
28962306a36Sopenharmony_ci * Return: true if the entry has to be purged now, false otherwise
29062306a36Sopenharmony_ci */
29162306a36Sopenharmony_cistatic bool batadv_nc_to_purge_nc_path_coding(struct batadv_priv *bat_priv,
29262306a36Sopenharmony_ci					      struct batadv_nc_path *nc_path)
29362306a36Sopenharmony_ci{
29462306a36Sopenharmony_ci	if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
29562306a36Sopenharmony_ci		return true;
29662306a36Sopenharmony_ci
29762306a36Sopenharmony_ci	/* purge the path when no packets has been added for 10 times the
29862306a36Sopenharmony_ci	 * max_fwd_delay time
29962306a36Sopenharmony_ci	 */
30062306a36Sopenharmony_ci	return batadv_has_timed_out(nc_path->last_valid,
30162306a36Sopenharmony_ci				    bat_priv->nc.max_fwd_delay * 10);
30262306a36Sopenharmony_ci}
30362306a36Sopenharmony_ci
30462306a36Sopenharmony_ci/**
30562306a36Sopenharmony_ci * batadv_nc_to_purge_nc_path_decoding() - checks whether an nc path has timed
30662306a36Sopenharmony_ci *  out
30762306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
30862306a36Sopenharmony_ci * @nc_path: the nc path to check
30962306a36Sopenharmony_ci *
31062306a36Sopenharmony_ci * Return: true if the entry has to be purged now, false otherwise
31162306a36Sopenharmony_ci */
31262306a36Sopenharmony_cistatic bool batadv_nc_to_purge_nc_path_decoding(struct batadv_priv *bat_priv,
31362306a36Sopenharmony_ci						struct batadv_nc_path *nc_path)
31462306a36Sopenharmony_ci{
31562306a36Sopenharmony_ci	if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
31662306a36Sopenharmony_ci		return true;
31762306a36Sopenharmony_ci
31862306a36Sopenharmony_ci	/* purge the path when no packets has been added for 10 times the
31962306a36Sopenharmony_ci	 * max_buffer time
32062306a36Sopenharmony_ci	 */
32162306a36Sopenharmony_ci	return batadv_has_timed_out(nc_path->last_valid,
32262306a36Sopenharmony_ci				    bat_priv->nc.max_buffer_time * 10);
32362306a36Sopenharmony_ci}
32462306a36Sopenharmony_ci
32562306a36Sopenharmony_ci/**
32662306a36Sopenharmony_ci * batadv_nc_purge_orig_nc_nodes() - go through list of nc nodes and purge stale
32762306a36Sopenharmony_ci *  entries
32862306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
32962306a36Sopenharmony_ci * @list: list of nc nodes
33062306a36Sopenharmony_ci * @lock: nc node list lock
33162306a36Sopenharmony_ci * @to_purge: function in charge to decide whether an entry has to be purged or
33262306a36Sopenharmony_ci *	      not. This function takes the nc node as argument and has to return
33362306a36Sopenharmony_ci *	      a boolean value: true if the entry has to be deleted, false
33462306a36Sopenharmony_ci *	      otherwise
33562306a36Sopenharmony_ci */
33662306a36Sopenharmony_cistatic void
33762306a36Sopenharmony_cibatadv_nc_purge_orig_nc_nodes(struct batadv_priv *bat_priv,
33862306a36Sopenharmony_ci			      struct list_head *list,
33962306a36Sopenharmony_ci			      spinlock_t *lock,
34062306a36Sopenharmony_ci			      bool (*to_purge)(struct batadv_priv *,
34162306a36Sopenharmony_ci					       struct batadv_nc_node *))
34262306a36Sopenharmony_ci{
34362306a36Sopenharmony_ci	struct batadv_nc_node *nc_node, *nc_node_tmp;
34462306a36Sopenharmony_ci
34562306a36Sopenharmony_ci	/* For each nc_node in list */
34662306a36Sopenharmony_ci	spin_lock_bh(lock);
34762306a36Sopenharmony_ci	list_for_each_entry_safe(nc_node, nc_node_tmp, list, list) {
34862306a36Sopenharmony_ci		/* if an helper function has been passed as parameter,
34962306a36Sopenharmony_ci		 * ask it if the entry has to be purged or not
35062306a36Sopenharmony_ci		 */
35162306a36Sopenharmony_ci		if (to_purge && !to_purge(bat_priv, nc_node))
35262306a36Sopenharmony_ci			continue;
35362306a36Sopenharmony_ci
35462306a36Sopenharmony_ci		batadv_dbg(BATADV_DBG_NC, bat_priv,
35562306a36Sopenharmony_ci			   "Removing nc_node %pM -> %pM\n",
35662306a36Sopenharmony_ci			   nc_node->addr, nc_node->orig_node->orig);
35762306a36Sopenharmony_ci		list_del_rcu(&nc_node->list);
35862306a36Sopenharmony_ci		batadv_nc_node_put(nc_node);
35962306a36Sopenharmony_ci	}
36062306a36Sopenharmony_ci	spin_unlock_bh(lock);
36162306a36Sopenharmony_ci}
36262306a36Sopenharmony_ci
36362306a36Sopenharmony_ci/**
36462306a36Sopenharmony_ci * batadv_nc_purge_orig() - purges all nc node data attached of the given
36562306a36Sopenharmony_ci *  originator
36662306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
36762306a36Sopenharmony_ci * @orig_node: orig_node with the nc node entries to be purged
36862306a36Sopenharmony_ci * @to_purge: function in charge to decide whether an entry has to be purged or
36962306a36Sopenharmony_ci *	      not. This function takes the nc node as argument and has to return
37062306a36Sopenharmony_ci *	      a boolean value: true is the entry has to be deleted, false
37162306a36Sopenharmony_ci *	      otherwise
37262306a36Sopenharmony_ci */
37362306a36Sopenharmony_civoid batadv_nc_purge_orig(struct batadv_priv *bat_priv,
37462306a36Sopenharmony_ci			  struct batadv_orig_node *orig_node,
37562306a36Sopenharmony_ci			  bool (*to_purge)(struct batadv_priv *,
37662306a36Sopenharmony_ci					   struct batadv_nc_node *))
37762306a36Sopenharmony_ci{
37862306a36Sopenharmony_ci	/* Check ingoing nc_node's of this orig_node */
37962306a36Sopenharmony_ci	batadv_nc_purge_orig_nc_nodes(bat_priv, &orig_node->in_coding_list,
38062306a36Sopenharmony_ci				      &orig_node->in_coding_list_lock,
38162306a36Sopenharmony_ci				      to_purge);
38262306a36Sopenharmony_ci
38362306a36Sopenharmony_ci	/* Check outgoing nc_node's of this orig_node */
38462306a36Sopenharmony_ci	batadv_nc_purge_orig_nc_nodes(bat_priv, &orig_node->out_coding_list,
38562306a36Sopenharmony_ci				      &orig_node->out_coding_list_lock,
38662306a36Sopenharmony_ci				      to_purge);
38762306a36Sopenharmony_ci}
38862306a36Sopenharmony_ci
38962306a36Sopenharmony_ci/**
39062306a36Sopenharmony_ci * batadv_nc_purge_orig_hash() - traverse entire originator hash to check if
39162306a36Sopenharmony_ci *  they have timed out nc nodes
39262306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
39362306a36Sopenharmony_ci */
39462306a36Sopenharmony_cistatic void batadv_nc_purge_orig_hash(struct batadv_priv *bat_priv)
39562306a36Sopenharmony_ci{
39662306a36Sopenharmony_ci	struct batadv_hashtable *hash = bat_priv->orig_hash;
39762306a36Sopenharmony_ci	struct hlist_head *head;
39862306a36Sopenharmony_ci	struct batadv_orig_node *orig_node;
39962306a36Sopenharmony_ci	u32 i;
40062306a36Sopenharmony_ci
40162306a36Sopenharmony_ci	if (!hash)
40262306a36Sopenharmony_ci		return;
40362306a36Sopenharmony_ci
40462306a36Sopenharmony_ci	/* For each orig_node */
40562306a36Sopenharmony_ci	for (i = 0; i < hash->size; i++) {
40662306a36Sopenharmony_ci		head = &hash->table[i];
40762306a36Sopenharmony_ci
40862306a36Sopenharmony_ci		rcu_read_lock();
40962306a36Sopenharmony_ci		hlist_for_each_entry_rcu(orig_node, head, hash_entry)
41062306a36Sopenharmony_ci			batadv_nc_purge_orig(bat_priv, orig_node,
41162306a36Sopenharmony_ci					     batadv_nc_to_purge_nc_node);
41262306a36Sopenharmony_ci		rcu_read_unlock();
41362306a36Sopenharmony_ci	}
41462306a36Sopenharmony_ci}
41562306a36Sopenharmony_ci
41662306a36Sopenharmony_ci/**
41762306a36Sopenharmony_ci * batadv_nc_purge_paths() - traverse all nc paths part of the hash and remove
41862306a36Sopenharmony_ci *  unused ones
41962306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
42062306a36Sopenharmony_ci * @hash: hash table containing the nc paths to check
42162306a36Sopenharmony_ci * @to_purge: function in charge to decide whether an entry has to be purged or
42262306a36Sopenharmony_ci *	      not. This function takes the nc node as argument and has to return
42362306a36Sopenharmony_ci *	      a boolean value: true is the entry has to be deleted, false
42462306a36Sopenharmony_ci *	      otherwise
42562306a36Sopenharmony_ci */
42662306a36Sopenharmony_cistatic void batadv_nc_purge_paths(struct batadv_priv *bat_priv,
42762306a36Sopenharmony_ci				  struct batadv_hashtable *hash,
42862306a36Sopenharmony_ci				  bool (*to_purge)(struct batadv_priv *,
42962306a36Sopenharmony_ci						   struct batadv_nc_path *))
43062306a36Sopenharmony_ci{
43162306a36Sopenharmony_ci	struct hlist_head *head;
43262306a36Sopenharmony_ci	struct hlist_node *node_tmp;
43362306a36Sopenharmony_ci	struct batadv_nc_path *nc_path;
43462306a36Sopenharmony_ci	spinlock_t *lock; /* Protects lists in hash */
43562306a36Sopenharmony_ci	u32 i;
43662306a36Sopenharmony_ci
43762306a36Sopenharmony_ci	for (i = 0; i < hash->size; i++) {
43862306a36Sopenharmony_ci		head = &hash->table[i];
43962306a36Sopenharmony_ci		lock = &hash->list_locks[i];
44062306a36Sopenharmony_ci
44162306a36Sopenharmony_ci		/* For each nc_path in this bin */
44262306a36Sopenharmony_ci		spin_lock_bh(lock);
44362306a36Sopenharmony_ci		hlist_for_each_entry_safe(nc_path, node_tmp, head, hash_entry) {
44462306a36Sopenharmony_ci			/* if an helper function has been passed as parameter,
44562306a36Sopenharmony_ci			 * ask it if the entry has to be purged or not
44662306a36Sopenharmony_ci			 */
44762306a36Sopenharmony_ci			if (to_purge && !to_purge(bat_priv, nc_path))
44862306a36Sopenharmony_ci				continue;
44962306a36Sopenharmony_ci
45062306a36Sopenharmony_ci			/* purging an non-empty nc_path should never happen, but
45162306a36Sopenharmony_ci			 * is observed under high CPU load. Delay the purging
45262306a36Sopenharmony_ci			 * until next iteration to allow the packet_list to be
45362306a36Sopenharmony_ci			 * emptied first.
45462306a36Sopenharmony_ci			 */
45562306a36Sopenharmony_ci			if (!unlikely(list_empty(&nc_path->packet_list))) {
45662306a36Sopenharmony_ci				net_ratelimited_function(printk,
45762306a36Sopenharmony_ci							 KERN_WARNING
45862306a36Sopenharmony_ci							 "Skipping free of non-empty nc_path (%pM -> %pM)!\n",
45962306a36Sopenharmony_ci							 nc_path->prev_hop,
46062306a36Sopenharmony_ci							 nc_path->next_hop);
46162306a36Sopenharmony_ci				continue;
46262306a36Sopenharmony_ci			}
46362306a36Sopenharmony_ci
46462306a36Sopenharmony_ci			/* nc_path is unused, so remove it */
46562306a36Sopenharmony_ci			batadv_dbg(BATADV_DBG_NC, bat_priv,
46662306a36Sopenharmony_ci				   "Remove nc_path %pM -> %pM\n",
46762306a36Sopenharmony_ci				   nc_path->prev_hop, nc_path->next_hop);
46862306a36Sopenharmony_ci			hlist_del_rcu(&nc_path->hash_entry);
46962306a36Sopenharmony_ci			batadv_nc_path_put(nc_path);
47062306a36Sopenharmony_ci		}
47162306a36Sopenharmony_ci		spin_unlock_bh(lock);
47262306a36Sopenharmony_ci	}
47362306a36Sopenharmony_ci}
47462306a36Sopenharmony_ci
47562306a36Sopenharmony_ci/**
47662306a36Sopenharmony_ci * batadv_nc_hash_key_gen() - computes the nc_path hash key
47762306a36Sopenharmony_ci * @key: buffer to hold the final hash key
47862306a36Sopenharmony_ci * @src: source ethernet mac address going into the hash key
47962306a36Sopenharmony_ci * @dst: destination ethernet mac address going into the hash key
48062306a36Sopenharmony_ci */
48162306a36Sopenharmony_cistatic void batadv_nc_hash_key_gen(struct batadv_nc_path *key, const char *src,
48262306a36Sopenharmony_ci				   const char *dst)
48362306a36Sopenharmony_ci{
48462306a36Sopenharmony_ci	memcpy(key->prev_hop, src, sizeof(key->prev_hop));
48562306a36Sopenharmony_ci	memcpy(key->next_hop, dst, sizeof(key->next_hop));
48662306a36Sopenharmony_ci}
48762306a36Sopenharmony_ci
48862306a36Sopenharmony_ci/**
48962306a36Sopenharmony_ci * batadv_nc_hash_choose() - compute the hash value for an nc path
49062306a36Sopenharmony_ci * @data: data to hash
49162306a36Sopenharmony_ci * @size: size of the hash table
49262306a36Sopenharmony_ci *
49362306a36Sopenharmony_ci * Return: the selected index in the hash table for the given data.
49462306a36Sopenharmony_ci */
49562306a36Sopenharmony_cistatic u32 batadv_nc_hash_choose(const void *data, u32 size)
49662306a36Sopenharmony_ci{
49762306a36Sopenharmony_ci	const struct batadv_nc_path *nc_path = data;
49862306a36Sopenharmony_ci	u32 hash = 0;
49962306a36Sopenharmony_ci
50062306a36Sopenharmony_ci	hash = jhash(&nc_path->prev_hop, sizeof(nc_path->prev_hop), hash);
50162306a36Sopenharmony_ci	hash = jhash(&nc_path->next_hop, sizeof(nc_path->next_hop), hash);
50262306a36Sopenharmony_ci
50362306a36Sopenharmony_ci	return hash % size;
50462306a36Sopenharmony_ci}
50562306a36Sopenharmony_ci
50662306a36Sopenharmony_ci/**
50762306a36Sopenharmony_ci * batadv_nc_hash_compare() - comparing function used in the network coding hash
50862306a36Sopenharmony_ci *  tables
50962306a36Sopenharmony_ci * @node: node in the local table
51062306a36Sopenharmony_ci * @data2: second object to compare the node to
51162306a36Sopenharmony_ci *
51262306a36Sopenharmony_ci * Return: true if the two entry are the same, false otherwise
51362306a36Sopenharmony_ci */
51462306a36Sopenharmony_cistatic bool batadv_nc_hash_compare(const struct hlist_node *node,
51562306a36Sopenharmony_ci				   const void *data2)
51662306a36Sopenharmony_ci{
51762306a36Sopenharmony_ci	const struct batadv_nc_path *nc_path1, *nc_path2;
51862306a36Sopenharmony_ci
51962306a36Sopenharmony_ci	nc_path1 = container_of(node, struct batadv_nc_path, hash_entry);
52062306a36Sopenharmony_ci	nc_path2 = data2;
52162306a36Sopenharmony_ci
52262306a36Sopenharmony_ci	/* Return 1 if the two keys are identical */
52362306a36Sopenharmony_ci	if (!batadv_compare_eth(nc_path1->prev_hop, nc_path2->prev_hop))
52462306a36Sopenharmony_ci		return false;
52562306a36Sopenharmony_ci
52662306a36Sopenharmony_ci	if (!batadv_compare_eth(nc_path1->next_hop, nc_path2->next_hop))
52762306a36Sopenharmony_ci		return false;
52862306a36Sopenharmony_ci
52962306a36Sopenharmony_ci	return true;
53062306a36Sopenharmony_ci}
53162306a36Sopenharmony_ci
53262306a36Sopenharmony_ci/**
53362306a36Sopenharmony_ci * batadv_nc_hash_find() - search for an existing nc path and return it
53462306a36Sopenharmony_ci * @hash: hash table containing the nc path
53562306a36Sopenharmony_ci * @data: search key
53662306a36Sopenharmony_ci *
53762306a36Sopenharmony_ci * Return: the nc_path if found, NULL otherwise.
53862306a36Sopenharmony_ci */
53962306a36Sopenharmony_cistatic struct batadv_nc_path *
54062306a36Sopenharmony_cibatadv_nc_hash_find(struct batadv_hashtable *hash,
54162306a36Sopenharmony_ci		    void *data)
54262306a36Sopenharmony_ci{
54362306a36Sopenharmony_ci	struct hlist_head *head;
54462306a36Sopenharmony_ci	struct batadv_nc_path *nc_path, *nc_path_tmp = NULL;
54562306a36Sopenharmony_ci	int index;
54662306a36Sopenharmony_ci
54762306a36Sopenharmony_ci	if (!hash)
54862306a36Sopenharmony_ci		return NULL;
54962306a36Sopenharmony_ci
55062306a36Sopenharmony_ci	index = batadv_nc_hash_choose(data, hash->size);
55162306a36Sopenharmony_ci	head = &hash->table[index];
55262306a36Sopenharmony_ci
55362306a36Sopenharmony_ci	rcu_read_lock();
55462306a36Sopenharmony_ci	hlist_for_each_entry_rcu(nc_path, head, hash_entry) {
55562306a36Sopenharmony_ci		if (!batadv_nc_hash_compare(&nc_path->hash_entry, data))
55662306a36Sopenharmony_ci			continue;
55762306a36Sopenharmony_ci
55862306a36Sopenharmony_ci		if (!kref_get_unless_zero(&nc_path->refcount))
55962306a36Sopenharmony_ci			continue;
56062306a36Sopenharmony_ci
56162306a36Sopenharmony_ci		nc_path_tmp = nc_path;
56262306a36Sopenharmony_ci		break;
56362306a36Sopenharmony_ci	}
56462306a36Sopenharmony_ci	rcu_read_unlock();
56562306a36Sopenharmony_ci
56662306a36Sopenharmony_ci	return nc_path_tmp;
56762306a36Sopenharmony_ci}
56862306a36Sopenharmony_ci
56962306a36Sopenharmony_ci/**
57062306a36Sopenharmony_ci * batadv_nc_send_packet() - send non-coded packet and free nc_packet struct
57162306a36Sopenharmony_ci * @nc_packet: the nc packet to send
57262306a36Sopenharmony_ci */
57362306a36Sopenharmony_cistatic void batadv_nc_send_packet(struct batadv_nc_packet *nc_packet)
57462306a36Sopenharmony_ci{
57562306a36Sopenharmony_ci	batadv_send_unicast_skb(nc_packet->skb, nc_packet->neigh_node);
57662306a36Sopenharmony_ci	nc_packet->skb = NULL;
57762306a36Sopenharmony_ci	batadv_nc_packet_free(nc_packet, false);
57862306a36Sopenharmony_ci}
57962306a36Sopenharmony_ci
58062306a36Sopenharmony_ci/**
58162306a36Sopenharmony_ci * batadv_nc_sniffed_purge() - Checks timestamp of given sniffed nc_packet.
58262306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
58362306a36Sopenharmony_ci * @nc_path: the nc path the packet belongs to
58462306a36Sopenharmony_ci * @nc_packet: the nc packet to be checked
58562306a36Sopenharmony_ci *
58662306a36Sopenharmony_ci * Checks whether the given sniffed (overheard) nc_packet has hit its buffering
58762306a36Sopenharmony_ci * timeout. If so, the packet is no longer kept and the entry deleted from the
58862306a36Sopenharmony_ci * queue. Has to be called with the appropriate locks.
58962306a36Sopenharmony_ci *
59062306a36Sopenharmony_ci * Return: false as soon as the entry in the fifo queue has not been timed out
59162306a36Sopenharmony_ci * yet and true otherwise.
59262306a36Sopenharmony_ci */
59362306a36Sopenharmony_cistatic bool batadv_nc_sniffed_purge(struct batadv_priv *bat_priv,
59462306a36Sopenharmony_ci				    struct batadv_nc_path *nc_path,
59562306a36Sopenharmony_ci				    struct batadv_nc_packet *nc_packet)
59662306a36Sopenharmony_ci{
59762306a36Sopenharmony_ci	unsigned long timeout = bat_priv->nc.max_buffer_time;
59862306a36Sopenharmony_ci	bool res = false;
59962306a36Sopenharmony_ci
60062306a36Sopenharmony_ci	lockdep_assert_held(&nc_path->packet_list_lock);
60162306a36Sopenharmony_ci
60262306a36Sopenharmony_ci	/* Packets are added to tail, so the remaining packets did not time
60362306a36Sopenharmony_ci	 * out and we can stop processing the current queue
60462306a36Sopenharmony_ci	 */
60562306a36Sopenharmony_ci	if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE &&
60662306a36Sopenharmony_ci	    !batadv_has_timed_out(nc_packet->timestamp, timeout))
60762306a36Sopenharmony_ci		goto out;
60862306a36Sopenharmony_ci
60962306a36Sopenharmony_ci	/* purge nc packet */
61062306a36Sopenharmony_ci	list_del(&nc_packet->list);
61162306a36Sopenharmony_ci	batadv_nc_packet_free(nc_packet, true);
61262306a36Sopenharmony_ci
61362306a36Sopenharmony_ci	res = true;
61462306a36Sopenharmony_ci
61562306a36Sopenharmony_ciout:
61662306a36Sopenharmony_ci	return res;
61762306a36Sopenharmony_ci}
61862306a36Sopenharmony_ci
61962306a36Sopenharmony_ci/**
62062306a36Sopenharmony_ci * batadv_nc_fwd_flush() - Checks the timestamp of the given nc packet.
62162306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
62262306a36Sopenharmony_ci * @nc_path: the nc path the packet belongs to
62362306a36Sopenharmony_ci * @nc_packet: the nc packet to be checked
62462306a36Sopenharmony_ci *
62562306a36Sopenharmony_ci * Checks whether the given nc packet has hit its forward timeout. If so, the
62662306a36Sopenharmony_ci * packet is no longer delayed, immediately sent and the entry deleted from the
62762306a36Sopenharmony_ci * queue. Has to be called with the appropriate locks.
62862306a36Sopenharmony_ci *
62962306a36Sopenharmony_ci * Return: false as soon as the entry in the fifo queue has not been timed out
63062306a36Sopenharmony_ci * yet and true otherwise.
63162306a36Sopenharmony_ci */
63262306a36Sopenharmony_cistatic bool batadv_nc_fwd_flush(struct batadv_priv *bat_priv,
63362306a36Sopenharmony_ci				struct batadv_nc_path *nc_path,
63462306a36Sopenharmony_ci				struct batadv_nc_packet *nc_packet)
63562306a36Sopenharmony_ci{
63662306a36Sopenharmony_ci	unsigned long timeout = bat_priv->nc.max_fwd_delay;
63762306a36Sopenharmony_ci
63862306a36Sopenharmony_ci	lockdep_assert_held(&nc_path->packet_list_lock);
63962306a36Sopenharmony_ci
64062306a36Sopenharmony_ci	/* Packets are added to tail, so the remaining packets did not time
64162306a36Sopenharmony_ci	 * out and we can stop processing the current queue
64262306a36Sopenharmony_ci	 */
64362306a36Sopenharmony_ci	if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE &&
64462306a36Sopenharmony_ci	    !batadv_has_timed_out(nc_packet->timestamp, timeout))
64562306a36Sopenharmony_ci		return false;
64662306a36Sopenharmony_ci
64762306a36Sopenharmony_ci	/* Send packet */
64862306a36Sopenharmony_ci	batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
64962306a36Sopenharmony_ci	batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
65062306a36Sopenharmony_ci			   nc_packet->skb->len + ETH_HLEN);
65162306a36Sopenharmony_ci	list_del(&nc_packet->list);
65262306a36Sopenharmony_ci	batadv_nc_send_packet(nc_packet);
65362306a36Sopenharmony_ci
65462306a36Sopenharmony_ci	return true;
65562306a36Sopenharmony_ci}
65662306a36Sopenharmony_ci
65762306a36Sopenharmony_ci/**
65862306a36Sopenharmony_ci * batadv_nc_process_nc_paths() - traverse given nc packet pool and free timed
65962306a36Sopenharmony_ci *  out nc packets
66062306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
66162306a36Sopenharmony_ci * @hash: to be processed hash table
66262306a36Sopenharmony_ci * @process_fn: Function called to process given nc packet. Should return true
66362306a36Sopenharmony_ci *	        to encourage this function to proceed with the next packet.
66462306a36Sopenharmony_ci *	        Otherwise the rest of the current queue is skipped.
66562306a36Sopenharmony_ci */
66662306a36Sopenharmony_cistatic void
66762306a36Sopenharmony_cibatadv_nc_process_nc_paths(struct batadv_priv *bat_priv,
66862306a36Sopenharmony_ci			   struct batadv_hashtable *hash,
66962306a36Sopenharmony_ci			   bool (*process_fn)(struct batadv_priv *,
67062306a36Sopenharmony_ci					      struct batadv_nc_path *,
67162306a36Sopenharmony_ci					      struct batadv_nc_packet *))
67262306a36Sopenharmony_ci{
67362306a36Sopenharmony_ci	struct hlist_head *head;
67462306a36Sopenharmony_ci	struct batadv_nc_packet *nc_packet, *nc_packet_tmp;
67562306a36Sopenharmony_ci	struct batadv_nc_path *nc_path;
67662306a36Sopenharmony_ci	bool ret;
67762306a36Sopenharmony_ci	int i;
67862306a36Sopenharmony_ci
67962306a36Sopenharmony_ci	if (!hash)
68062306a36Sopenharmony_ci		return;
68162306a36Sopenharmony_ci
68262306a36Sopenharmony_ci	/* Loop hash table bins */
68362306a36Sopenharmony_ci	for (i = 0; i < hash->size; i++) {
68462306a36Sopenharmony_ci		head = &hash->table[i];
68562306a36Sopenharmony_ci
68662306a36Sopenharmony_ci		/* Loop coding paths */
68762306a36Sopenharmony_ci		rcu_read_lock();
68862306a36Sopenharmony_ci		hlist_for_each_entry_rcu(nc_path, head, hash_entry) {
68962306a36Sopenharmony_ci			/* Loop packets */
69062306a36Sopenharmony_ci			spin_lock_bh(&nc_path->packet_list_lock);
69162306a36Sopenharmony_ci			list_for_each_entry_safe(nc_packet, nc_packet_tmp,
69262306a36Sopenharmony_ci						 &nc_path->packet_list, list) {
69362306a36Sopenharmony_ci				ret = process_fn(bat_priv, nc_path, nc_packet);
69462306a36Sopenharmony_ci				if (!ret)
69562306a36Sopenharmony_ci					break;
69662306a36Sopenharmony_ci			}
69762306a36Sopenharmony_ci			spin_unlock_bh(&nc_path->packet_list_lock);
69862306a36Sopenharmony_ci		}
69962306a36Sopenharmony_ci		rcu_read_unlock();
70062306a36Sopenharmony_ci	}
70162306a36Sopenharmony_ci}
70262306a36Sopenharmony_ci
70362306a36Sopenharmony_ci/**
70462306a36Sopenharmony_ci * batadv_nc_worker() - periodic task for housekeeping related to network
70562306a36Sopenharmony_ci *  coding
70662306a36Sopenharmony_ci * @work: kernel work struct
70762306a36Sopenharmony_ci */
70862306a36Sopenharmony_cistatic void batadv_nc_worker(struct work_struct *work)
70962306a36Sopenharmony_ci{
71062306a36Sopenharmony_ci	struct delayed_work *delayed_work;
71162306a36Sopenharmony_ci	struct batadv_priv_nc *priv_nc;
71262306a36Sopenharmony_ci	struct batadv_priv *bat_priv;
71362306a36Sopenharmony_ci	unsigned long timeout;
71462306a36Sopenharmony_ci
71562306a36Sopenharmony_ci	delayed_work = to_delayed_work(work);
71662306a36Sopenharmony_ci	priv_nc = container_of(delayed_work, struct batadv_priv_nc, work);
71762306a36Sopenharmony_ci	bat_priv = container_of(priv_nc, struct batadv_priv, nc);
71862306a36Sopenharmony_ci
71962306a36Sopenharmony_ci	batadv_nc_purge_orig_hash(bat_priv);
72062306a36Sopenharmony_ci	batadv_nc_purge_paths(bat_priv, bat_priv->nc.coding_hash,
72162306a36Sopenharmony_ci			      batadv_nc_to_purge_nc_path_coding);
72262306a36Sopenharmony_ci	batadv_nc_purge_paths(bat_priv, bat_priv->nc.decoding_hash,
72362306a36Sopenharmony_ci			      batadv_nc_to_purge_nc_path_decoding);
72462306a36Sopenharmony_ci
72562306a36Sopenharmony_ci	timeout = bat_priv->nc.max_fwd_delay;
72662306a36Sopenharmony_ci
72762306a36Sopenharmony_ci	if (batadv_has_timed_out(bat_priv->nc.timestamp_fwd_flush, timeout)) {
72862306a36Sopenharmony_ci		batadv_nc_process_nc_paths(bat_priv, bat_priv->nc.coding_hash,
72962306a36Sopenharmony_ci					   batadv_nc_fwd_flush);
73062306a36Sopenharmony_ci		bat_priv->nc.timestamp_fwd_flush = jiffies;
73162306a36Sopenharmony_ci	}
73262306a36Sopenharmony_ci
73362306a36Sopenharmony_ci	if (batadv_has_timed_out(bat_priv->nc.timestamp_sniffed_purge,
73462306a36Sopenharmony_ci				 bat_priv->nc.max_buffer_time)) {
73562306a36Sopenharmony_ci		batadv_nc_process_nc_paths(bat_priv, bat_priv->nc.decoding_hash,
73662306a36Sopenharmony_ci					   batadv_nc_sniffed_purge);
73762306a36Sopenharmony_ci		bat_priv->nc.timestamp_sniffed_purge = jiffies;
73862306a36Sopenharmony_ci	}
73962306a36Sopenharmony_ci
74062306a36Sopenharmony_ci	/* Schedule a new check */
74162306a36Sopenharmony_ci	batadv_nc_start_timer(bat_priv);
74262306a36Sopenharmony_ci}
74362306a36Sopenharmony_ci
74462306a36Sopenharmony_ci/**
74562306a36Sopenharmony_ci * batadv_can_nc_with_orig() - checks whether the given orig node is suitable
74662306a36Sopenharmony_ci *  for coding or not
74762306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
74862306a36Sopenharmony_ci * @orig_node: neighboring orig node which may be used as nc candidate
74962306a36Sopenharmony_ci * @ogm_packet: incoming ogm packet also used for the checks
75062306a36Sopenharmony_ci *
75162306a36Sopenharmony_ci * Return: true if:
75262306a36Sopenharmony_ci *  1) The OGM must have the most recent sequence number.
75362306a36Sopenharmony_ci *  2) The TTL must be decremented by one and only one.
75462306a36Sopenharmony_ci *  3) The OGM must be received from the first hop from orig_node.
75562306a36Sopenharmony_ci *  4) The TQ value of the OGM must be above bat_priv->nc.min_tq.
75662306a36Sopenharmony_ci */
75762306a36Sopenharmony_cistatic bool batadv_can_nc_with_orig(struct batadv_priv *bat_priv,
75862306a36Sopenharmony_ci				    struct batadv_orig_node *orig_node,
75962306a36Sopenharmony_ci				    struct batadv_ogm_packet *ogm_packet)
76062306a36Sopenharmony_ci{
76162306a36Sopenharmony_ci	struct batadv_orig_ifinfo *orig_ifinfo;
76262306a36Sopenharmony_ci	u32 last_real_seqno;
76362306a36Sopenharmony_ci	u8 last_ttl;
76462306a36Sopenharmony_ci
76562306a36Sopenharmony_ci	orig_ifinfo = batadv_orig_ifinfo_get(orig_node, BATADV_IF_DEFAULT);
76662306a36Sopenharmony_ci	if (!orig_ifinfo)
76762306a36Sopenharmony_ci		return false;
76862306a36Sopenharmony_ci
76962306a36Sopenharmony_ci	last_ttl = orig_ifinfo->last_ttl;
77062306a36Sopenharmony_ci	last_real_seqno = orig_ifinfo->last_real_seqno;
77162306a36Sopenharmony_ci	batadv_orig_ifinfo_put(orig_ifinfo);
77262306a36Sopenharmony_ci
77362306a36Sopenharmony_ci	if (last_real_seqno != ntohl(ogm_packet->seqno))
77462306a36Sopenharmony_ci		return false;
77562306a36Sopenharmony_ci	if (last_ttl != ogm_packet->ttl + 1)
77662306a36Sopenharmony_ci		return false;
77762306a36Sopenharmony_ci	if (!batadv_compare_eth(ogm_packet->orig, ogm_packet->prev_sender))
77862306a36Sopenharmony_ci		return false;
77962306a36Sopenharmony_ci	if (ogm_packet->tq < bat_priv->nc.min_tq)
78062306a36Sopenharmony_ci		return false;
78162306a36Sopenharmony_ci
78262306a36Sopenharmony_ci	return true;
78362306a36Sopenharmony_ci}
78462306a36Sopenharmony_ci
78562306a36Sopenharmony_ci/**
78662306a36Sopenharmony_ci * batadv_nc_find_nc_node() - search for an existing nc node and return it
78762306a36Sopenharmony_ci * @orig_node: orig node originating the ogm packet
78862306a36Sopenharmony_ci * @orig_neigh_node: neighboring orig node from which we received the ogm packet
78962306a36Sopenharmony_ci *  (can be equal to orig_node)
79062306a36Sopenharmony_ci * @in_coding: traverse incoming or outgoing network coding list
79162306a36Sopenharmony_ci *
79262306a36Sopenharmony_ci * Return: the nc_node if found, NULL otherwise.
79362306a36Sopenharmony_ci */
79462306a36Sopenharmony_cistatic struct batadv_nc_node *
79562306a36Sopenharmony_cibatadv_nc_find_nc_node(struct batadv_orig_node *orig_node,
79662306a36Sopenharmony_ci		       struct batadv_orig_node *orig_neigh_node,
79762306a36Sopenharmony_ci		       bool in_coding)
79862306a36Sopenharmony_ci{
79962306a36Sopenharmony_ci	struct batadv_nc_node *nc_node, *nc_node_out = NULL;
80062306a36Sopenharmony_ci	struct list_head *list;
80162306a36Sopenharmony_ci
80262306a36Sopenharmony_ci	if (in_coding)
80362306a36Sopenharmony_ci		list = &orig_neigh_node->in_coding_list;
80462306a36Sopenharmony_ci	else
80562306a36Sopenharmony_ci		list = &orig_neigh_node->out_coding_list;
80662306a36Sopenharmony_ci
80762306a36Sopenharmony_ci	/* Traverse list of nc_nodes to orig_node */
80862306a36Sopenharmony_ci	rcu_read_lock();
80962306a36Sopenharmony_ci	list_for_each_entry_rcu(nc_node, list, list) {
81062306a36Sopenharmony_ci		if (!batadv_compare_eth(nc_node->addr, orig_node->orig))
81162306a36Sopenharmony_ci			continue;
81262306a36Sopenharmony_ci
81362306a36Sopenharmony_ci		if (!kref_get_unless_zero(&nc_node->refcount))
81462306a36Sopenharmony_ci			continue;
81562306a36Sopenharmony_ci
81662306a36Sopenharmony_ci		/* Found a match */
81762306a36Sopenharmony_ci		nc_node_out = nc_node;
81862306a36Sopenharmony_ci		break;
81962306a36Sopenharmony_ci	}
82062306a36Sopenharmony_ci	rcu_read_unlock();
82162306a36Sopenharmony_ci
82262306a36Sopenharmony_ci	return nc_node_out;
82362306a36Sopenharmony_ci}
82462306a36Sopenharmony_ci
82562306a36Sopenharmony_ci/**
82662306a36Sopenharmony_ci * batadv_nc_get_nc_node() - retrieves an nc node or creates the entry if it was
82762306a36Sopenharmony_ci *  not found
82862306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
82962306a36Sopenharmony_ci * @orig_node: orig node originating the ogm packet
83062306a36Sopenharmony_ci * @orig_neigh_node: neighboring orig node from which we received the ogm packet
83162306a36Sopenharmony_ci *  (can be equal to orig_node)
83262306a36Sopenharmony_ci * @in_coding: traverse incoming or outgoing network coding list
83362306a36Sopenharmony_ci *
83462306a36Sopenharmony_ci * Return: the nc_node if found or created, NULL in case of an error.
83562306a36Sopenharmony_ci */
83662306a36Sopenharmony_cistatic struct batadv_nc_node *
83762306a36Sopenharmony_cibatadv_nc_get_nc_node(struct batadv_priv *bat_priv,
83862306a36Sopenharmony_ci		      struct batadv_orig_node *orig_node,
83962306a36Sopenharmony_ci		      struct batadv_orig_node *orig_neigh_node,
84062306a36Sopenharmony_ci		      bool in_coding)
84162306a36Sopenharmony_ci{
84262306a36Sopenharmony_ci	struct batadv_nc_node *nc_node;
84362306a36Sopenharmony_ci	spinlock_t *lock; /* Used to lock list selected by "int in_coding" */
84462306a36Sopenharmony_ci	struct list_head *list;
84562306a36Sopenharmony_ci
84662306a36Sopenharmony_ci	/* Select ingoing or outgoing coding node */
84762306a36Sopenharmony_ci	if (in_coding) {
84862306a36Sopenharmony_ci		lock = &orig_neigh_node->in_coding_list_lock;
84962306a36Sopenharmony_ci		list = &orig_neigh_node->in_coding_list;
85062306a36Sopenharmony_ci	} else {
85162306a36Sopenharmony_ci		lock = &orig_neigh_node->out_coding_list_lock;
85262306a36Sopenharmony_ci		list = &orig_neigh_node->out_coding_list;
85362306a36Sopenharmony_ci	}
85462306a36Sopenharmony_ci
85562306a36Sopenharmony_ci	spin_lock_bh(lock);
85662306a36Sopenharmony_ci
85762306a36Sopenharmony_ci	/* Check if nc_node is already added */
85862306a36Sopenharmony_ci	nc_node = batadv_nc_find_nc_node(orig_node, orig_neigh_node, in_coding);
85962306a36Sopenharmony_ci
86062306a36Sopenharmony_ci	/* Node found */
86162306a36Sopenharmony_ci	if (nc_node)
86262306a36Sopenharmony_ci		goto unlock;
86362306a36Sopenharmony_ci
86462306a36Sopenharmony_ci	nc_node = kzalloc(sizeof(*nc_node), GFP_ATOMIC);
86562306a36Sopenharmony_ci	if (!nc_node)
86662306a36Sopenharmony_ci		goto unlock;
86762306a36Sopenharmony_ci
86862306a36Sopenharmony_ci	/* Initialize nc_node */
86962306a36Sopenharmony_ci	INIT_LIST_HEAD(&nc_node->list);
87062306a36Sopenharmony_ci	kref_init(&nc_node->refcount);
87162306a36Sopenharmony_ci	ether_addr_copy(nc_node->addr, orig_node->orig);
87262306a36Sopenharmony_ci	kref_get(&orig_neigh_node->refcount);
87362306a36Sopenharmony_ci	nc_node->orig_node = orig_neigh_node;
87462306a36Sopenharmony_ci
87562306a36Sopenharmony_ci	batadv_dbg(BATADV_DBG_NC, bat_priv, "Adding nc_node %pM -> %pM\n",
87662306a36Sopenharmony_ci		   nc_node->addr, nc_node->orig_node->orig);
87762306a36Sopenharmony_ci
87862306a36Sopenharmony_ci	/* Add nc_node to orig_node */
87962306a36Sopenharmony_ci	kref_get(&nc_node->refcount);
88062306a36Sopenharmony_ci	list_add_tail_rcu(&nc_node->list, list);
88162306a36Sopenharmony_ci
88262306a36Sopenharmony_ciunlock:
88362306a36Sopenharmony_ci	spin_unlock_bh(lock);
88462306a36Sopenharmony_ci
88562306a36Sopenharmony_ci	return nc_node;
88662306a36Sopenharmony_ci}
88762306a36Sopenharmony_ci
88862306a36Sopenharmony_ci/**
88962306a36Sopenharmony_ci * batadv_nc_update_nc_node() - updates stored incoming and outgoing nc node
89062306a36Sopenharmony_ci *  structs (best called on incoming OGMs)
89162306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
89262306a36Sopenharmony_ci * @orig_node: orig node originating the ogm packet
89362306a36Sopenharmony_ci * @orig_neigh_node: neighboring orig node from which we received the ogm packet
89462306a36Sopenharmony_ci *  (can be equal to orig_node)
89562306a36Sopenharmony_ci * @ogm_packet: incoming ogm packet
89662306a36Sopenharmony_ci * @is_single_hop_neigh: orig_node is a single hop neighbor
89762306a36Sopenharmony_ci */
89862306a36Sopenharmony_civoid batadv_nc_update_nc_node(struct batadv_priv *bat_priv,
89962306a36Sopenharmony_ci			      struct batadv_orig_node *orig_node,
90062306a36Sopenharmony_ci			      struct batadv_orig_node *orig_neigh_node,
90162306a36Sopenharmony_ci			      struct batadv_ogm_packet *ogm_packet,
90262306a36Sopenharmony_ci			      int is_single_hop_neigh)
90362306a36Sopenharmony_ci{
90462306a36Sopenharmony_ci	struct batadv_nc_node *in_nc_node = NULL;
90562306a36Sopenharmony_ci	struct batadv_nc_node *out_nc_node = NULL;
90662306a36Sopenharmony_ci
90762306a36Sopenharmony_ci	/* Check if network coding is enabled */
90862306a36Sopenharmony_ci	if (!atomic_read(&bat_priv->network_coding))
90962306a36Sopenharmony_ci		goto out;
91062306a36Sopenharmony_ci
91162306a36Sopenharmony_ci	/* check if orig node is network coding enabled */
91262306a36Sopenharmony_ci	if (!test_bit(BATADV_ORIG_CAPA_HAS_NC, &orig_node->capabilities))
91362306a36Sopenharmony_ci		goto out;
91462306a36Sopenharmony_ci
91562306a36Sopenharmony_ci	/* accept ogms from 'good' neighbors and single hop neighbors */
91662306a36Sopenharmony_ci	if (!batadv_can_nc_with_orig(bat_priv, orig_node, ogm_packet) &&
91762306a36Sopenharmony_ci	    !is_single_hop_neigh)
91862306a36Sopenharmony_ci		goto out;
91962306a36Sopenharmony_ci
92062306a36Sopenharmony_ci	/* Add orig_node as in_nc_node on hop */
92162306a36Sopenharmony_ci	in_nc_node = batadv_nc_get_nc_node(bat_priv, orig_node,
92262306a36Sopenharmony_ci					   orig_neigh_node, true);
92362306a36Sopenharmony_ci	if (!in_nc_node)
92462306a36Sopenharmony_ci		goto out;
92562306a36Sopenharmony_ci
92662306a36Sopenharmony_ci	in_nc_node->last_seen = jiffies;
92762306a36Sopenharmony_ci
92862306a36Sopenharmony_ci	/* Add hop as out_nc_node on orig_node */
92962306a36Sopenharmony_ci	out_nc_node = batadv_nc_get_nc_node(bat_priv, orig_neigh_node,
93062306a36Sopenharmony_ci					    orig_node, false);
93162306a36Sopenharmony_ci	if (!out_nc_node)
93262306a36Sopenharmony_ci		goto out;
93362306a36Sopenharmony_ci
93462306a36Sopenharmony_ci	out_nc_node->last_seen = jiffies;
93562306a36Sopenharmony_ci
93662306a36Sopenharmony_ciout:
93762306a36Sopenharmony_ci	batadv_nc_node_put(in_nc_node);
93862306a36Sopenharmony_ci	batadv_nc_node_put(out_nc_node);
93962306a36Sopenharmony_ci}
94062306a36Sopenharmony_ci
94162306a36Sopenharmony_ci/**
94262306a36Sopenharmony_ci * batadv_nc_get_path() - get existing nc_path or allocate a new one
94362306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
94462306a36Sopenharmony_ci * @hash: hash table containing the nc path
94562306a36Sopenharmony_ci * @src: ethernet source address - first half of the nc path search key
94662306a36Sopenharmony_ci * @dst: ethernet destination address - second half of the nc path search key
94762306a36Sopenharmony_ci *
94862306a36Sopenharmony_ci * Return: pointer to nc_path if the path was found or created, returns NULL
94962306a36Sopenharmony_ci * on error.
95062306a36Sopenharmony_ci */
95162306a36Sopenharmony_cistatic struct batadv_nc_path *batadv_nc_get_path(struct batadv_priv *bat_priv,
95262306a36Sopenharmony_ci						 struct batadv_hashtable *hash,
95362306a36Sopenharmony_ci						 u8 *src,
95462306a36Sopenharmony_ci						 u8 *dst)
95562306a36Sopenharmony_ci{
95662306a36Sopenharmony_ci	int hash_added;
95762306a36Sopenharmony_ci	struct batadv_nc_path *nc_path, nc_path_key;
95862306a36Sopenharmony_ci
95962306a36Sopenharmony_ci	batadv_nc_hash_key_gen(&nc_path_key, src, dst);
96062306a36Sopenharmony_ci
96162306a36Sopenharmony_ci	/* Search for existing nc_path */
96262306a36Sopenharmony_ci	nc_path = batadv_nc_hash_find(hash, (void *)&nc_path_key);
96362306a36Sopenharmony_ci
96462306a36Sopenharmony_ci	if (nc_path) {
96562306a36Sopenharmony_ci		/* Set timestamp to delay removal of nc_path */
96662306a36Sopenharmony_ci		nc_path->last_valid = jiffies;
96762306a36Sopenharmony_ci		return nc_path;
96862306a36Sopenharmony_ci	}
96962306a36Sopenharmony_ci
97062306a36Sopenharmony_ci	/* No existing nc_path was found; create a new */
97162306a36Sopenharmony_ci	nc_path = kzalloc(sizeof(*nc_path), GFP_ATOMIC);
97262306a36Sopenharmony_ci
97362306a36Sopenharmony_ci	if (!nc_path)
97462306a36Sopenharmony_ci		return NULL;
97562306a36Sopenharmony_ci
97662306a36Sopenharmony_ci	/* Initialize nc_path */
97762306a36Sopenharmony_ci	INIT_LIST_HEAD(&nc_path->packet_list);
97862306a36Sopenharmony_ci	spin_lock_init(&nc_path->packet_list_lock);
97962306a36Sopenharmony_ci	kref_init(&nc_path->refcount);
98062306a36Sopenharmony_ci	nc_path->last_valid = jiffies;
98162306a36Sopenharmony_ci	ether_addr_copy(nc_path->next_hop, dst);
98262306a36Sopenharmony_ci	ether_addr_copy(nc_path->prev_hop, src);
98362306a36Sopenharmony_ci
98462306a36Sopenharmony_ci	batadv_dbg(BATADV_DBG_NC, bat_priv, "Adding nc_path %pM -> %pM\n",
98562306a36Sopenharmony_ci		   nc_path->prev_hop,
98662306a36Sopenharmony_ci		   nc_path->next_hop);
98762306a36Sopenharmony_ci
98862306a36Sopenharmony_ci	/* Add nc_path to hash table */
98962306a36Sopenharmony_ci	kref_get(&nc_path->refcount);
99062306a36Sopenharmony_ci	hash_added = batadv_hash_add(hash, batadv_nc_hash_compare,
99162306a36Sopenharmony_ci				     batadv_nc_hash_choose, &nc_path_key,
99262306a36Sopenharmony_ci				     &nc_path->hash_entry);
99362306a36Sopenharmony_ci
99462306a36Sopenharmony_ci	if (hash_added < 0) {
99562306a36Sopenharmony_ci		kfree(nc_path);
99662306a36Sopenharmony_ci		return NULL;
99762306a36Sopenharmony_ci	}
99862306a36Sopenharmony_ci
99962306a36Sopenharmony_ci	return nc_path;
100062306a36Sopenharmony_ci}
100162306a36Sopenharmony_ci
100262306a36Sopenharmony_ci/**
100362306a36Sopenharmony_ci * batadv_nc_random_weight_tq() - scale the receivers TQ-value to avoid unfair
100462306a36Sopenharmony_ci *  selection of a receiver with slightly lower TQ than the other
100562306a36Sopenharmony_ci * @tq: to be weighted tq value
100662306a36Sopenharmony_ci *
100762306a36Sopenharmony_ci * Return: scaled tq value
100862306a36Sopenharmony_ci */
100962306a36Sopenharmony_cistatic u8 batadv_nc_random_weight_tq(u8 tq)
101062306a36Sopenharmony_ci{
101162306a36Sopenharmony_ci	/* randomize the estimated packet loss (max TQ - estimated TQ) */
101262306a36Sopenharmony_ci	u8 rand_tq = get_random_u32_below(BATADV_TQ_MAX_VALUE + 1 - tq);
101362306a36Sopenharmony_ci
101462306a36Sopenharmony_ci	/* convert to (randomized) estimated tq again */
101562306a36Sopenharmony_ci	return BATADV_TQ_MAX_VALUE - rand_tq;
101662306a36Sopenharmony_ci}
101762306a36Sopenharmony_ci
101862306a36Sopenharmony_ci/**
101962306a36Sopenharmony_ci * batadv_nc_memxor() - XOR destination with source
102062306a36Sopenharmony_ci * @dst: byte array to XOR into
102162306a36Sopenharmony_ci * @src: byte array to XOR from
102262306a36Sopenharmony_ci * @len: length of destination array
102362306a36Sopenharmony_ci */
102462306a36Sopenharmony_cistatic void batadv_nc_memxor(char *dst, const char *src, unsigned int len)
102562306a36Sopenharmony_ci{
102662306a36Sopenharmony_ci	unsigned int i;
102762306a36Sopenharmony_ci
102862306a36Sopenharmony_ci	for (i = 0; i < len; ++i)
102962306a36Sopenharmony_ci		dst[i] ^= src[i];
103062306a36Sopenharmony_ci}
103162306a36Sopenharmony_ci
103262306a36Sopenharmony_ci/**
103362306a36Sopenharmony_ci * batadv_nc_code_packets() - code a received unicast_packet with an nc packet
103462306a36Sopenharmony_ci *  into a coded_packet and send it
103562306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
103662306a36Sopenharmony_ci * @skb: data skb to forward
103762306a36Sopenharmony_ci * @ethhdr: pointer to the ethernet header inside the skb
103862306a36Sopenharmony_ci * @nc_packet: structure containing the packet to the skb can be coded with
103962306a36Sopenharmony_ci * @neigh_node: next hop to forward packet to
104062306a36Sopenharmony_ci *
104162306a36Sopenharmony_ci * Return: true if both packets are consumed, false otherwise.
104262306a36Sopenharmony_ci */
104362306a36Sopenharmony_cistatic bool batadv_nc_code_packets(struct batadv_priv *bat_priv,
104462306a36Sopenharmony_ci				   struct sk_buff *skb,
104562306a36Sopenharmony_ci				   struct ethhdr *ethhdr,
104662306a36Sopenharmony_ci				   struct batadv_nc_packet *nc_packet,
104762306a36Sopenharmony_ci				   struct batadv_neigh_node *neigh_node)
104862306a36Sopenharmony_ci{
104962306a36Sopenharmony_ci	u8 tq_weighted_neigh, tq_weighted_coding, tq_tmp;
105062306a36Sopenharmony_ci	struct sk_buff *skb_dest, *skb_src;
105162306a36Sopenharmony_ci	struct batadv_unicast_packet *packet1;
105262306a36Sopenharmony_ci	struct batadv_unicast_packet *packet2;
105362306a36Sopenharmony_ci	struct batadv_coded_packet *coded_packet;
105462306a36Sopenharmony_ci	struct batadv_neigh_node *neigh_tmp, *router_neigh, *first_dest;
105562306a36Sopenharmony_ci	struct batadv_neigh_node *router_coding = NULL, *second_dest;
105662306a36Sopenharmony_ci	struct batadv_neigh_ifinfo *router_neigh_ifinfo = NULL;
105762306a36Sopenharmony_ci	struct batadv_neigh_ifinfo *router_coding_ifinfo = NULL;
105862306a36Sopenharmony_ci	u8 *first_source, *second_source;
105962306a36Sopenharmony_ci	__be32 packet_id1, packet_id2;
106062306a36Sopenharmony_ci	size_t count;
106162306a36Sopenharmony_ci	bool res = false;
106262306a36Sopenharmony_ci	int coding_len;
106362306a36Sopenharmony_ci	int unicast_size = sizeof(*packet1);
106462306a36Sopenharmony_ci	int coded_size = sizeof(*coded_packet);
106562306a36Sopenharmony_ci	int header_add = coded_size - unicast_size;
106662306a36Sopenharmony_ci
106762306a36Sopenharmony_ci	/* TODO: do we need to consider the outgoing interface for
106862306a36Sopenharmony_ci	 * coded packets?
106962306a36Sopenharmony_ci	 */
107062306a36Sopenharmony_ci	router_neigh = batadv_orig_router_get(neigh_node->orig_node,
107162306a36Sopenharmony_ci					      BATADV_IF_DEFAULT);
107262306a36Sopenharmony_ci	if (!router_neigh)
107362306a36Sopenharmony_ci		goto out;
107462306a36Sopenharmony_ci
107562306a36Sopenharmony_ci	router_neigh_ifinfo = batadv_neigh_ifinfo_get(router_neigh,
107662306a36Sopenharmony_ci						      BATADV_IF_DEFAULT);
107762306a36Sopenharmony_ci	if (!router_neigh_ifinfo)
107862306a36Sopenharmony_ci		goto out;
107962306a36Sopenharmony_ci
108062306a36Sopenharmony_ci	neigh_tmp = nc_packet->neigh_node;
108162306a36Sopenharmony_ci	router_coding = batadv_orig_router_get(neigh_tmp->orig_node,
108262306a36Sopenharmony_ci					       BATADV_IF_DEFAULT);
108362306a36Sopenharmony_ci	if (!router_coding)
108462306a36Sopenharmony_ci		goto out;
108562306a36Sopenharmony_ci
108662306a36Sopenharmony_ci	router_coding_ifinfo = batadv_neigh_ifinfo_get(router_coding,
108762306a36Sopenharmony_ci						       BATADV_IF_DEFAULT);
108862306a36Sopenharmony_ci	if (!router_coding_ifinfo)
108962306a36Sopenharmony_ci		goto out;
109062306a36Sopenharmony_ci
109162306a36Sopenharmony_ci	tq_tmp = router_neigh_ifinfo->bat_iv.tq_avg;
109262306a36Sopenharmony_ci	tq_weighted_neigh = batadv_nc_random_weight_tq(tq_tmp);
109362306a36Sopenharmony_ci	tq_tmp = router_coding_ifinfo->bat_iv.tq_avg;
109462306a36Sopenharmony_ci	tq_weighted_coding = batadv_nc_random_weight_tq(tq_tmp);
109562306a36Sopenharmony_ci
109662306a36Sopenharmony_ci	/* Select one destination for the MAC-header dst-field based on
109762306a36Sopenharmony_ci	 * weighted TQ-values.
109862306a36Sopenharmony_ci	 */
109962306a36Sopenharmony_ci	if (tq_weighted_neigh >= tq_weighted_coding) {
110062306a36Sopenharmony_ci		/* Destination from nc_packet is selected for MAC-header */
110162306a36Sopenharmony_ci		first_dest = nc_packet->neigh_node;
110262306a36Sopenharmony_ci		first_source = nc_packet->nc_path->prev_hop;
110362306a36Sopenharmony_ci		second_dest = neigh_node;
110462306a36Sopenharmony_ci		second_source = ethhdr->h_source;
110562306a36Sopenharmony_ci		packet1 = (struct batadv_unicast_packet *)nc_packet->skb->data;
110662306a36Sopenharmony_ci		packet2 = (struct batadv_unicast_packet *)skb->data;
110762306a36Sopenharmony_ci		packet_id1 = nc_packet->packet_id;
110862306a36Sopenharmony_ci		packet_id2 = batadv_skb_crc32(skb,
110962306a36Sopenharmony_ci					      skb->data + sizeof(*packet2));
111062306a36Sopenharmony_ci	} else {
111162306a36Sopenharmony_ci		/* Destination for skb is selected for MAC-header */
111262306a36Sopenharmony_ci		first_dest = neigh_node;
111362306a36Sopenharmony_ci		first_source = ethhdr->h_source;
111462306a36Sopenharmony_ci		second_dest = nc_packet->neigh_node;
111562306a36Sopenharmony_ci		second_source = nc_packet->nc_path->prev_hop;
111662306a36Sopenharmony_ci		packet1 = (struct batadv_unicast_packet *)skb->data;
111762306a36Sopenharmony_ci		packet2 = (struct batadv_unicast_packet *)nc_packet->skb->data;
111862306a36Sopenharmony_ci		packet_id1 = batadv_skb_crc32(skb,
111962306a36Sopenharmony_ci					      skb->data + sizeof(*packet1));
112062306a36Sopenharmony_ci		packet_id2 = nc_packet->packet_id;
112162306a36Sopenharmony_ci	}
112262306a36Sopenharmony_ci
112362306a36Sopenharmony_ci	/* Instead of zero padding the smallest data buffer, we
112462306a36Sopenharmony_ci	 * code into the largest.
112562306a36Sopenharmony_ci	 */
112662306a36Sopenharmony_ci	if (skb->len <= nc_packet->skb->len) {
112762306a36Sopenharmony_ci		skb_dest = nc_packet->skb;
112862306a36Sopenharmony_ci		skb_src = skb;
112962306a36Sopenharmony_ci	} else {
113062306a36Sopenharmony_ci		skb_dest = skb;
113162306a36Sopenharmony_ci		skb_src = nc_packet->skb;
113262306a36Sopenharmony_ci	}
113362306a36Sopenharmony_ci
113462306a36Sopenharmony_ci	/* coding_len is used when decoding the packet shorter packet */
113562306a36Sopenharmony_ci	coding_len = skb_src->len - unicast_size;
113662306a36Sopenharmony_ci
113762306a36Sopenharmony_ci	if (skb_linearize(skb_dest) < 0 || skb_linearize(skb_src) < 0)
113862306a36Sopenharmony_ci		goto out;
113962306a36Sopenharmony_ci
114062306a36Sopenharmony_ci	skb_push(skb_dest, header_add);
114162306a36Sopenharmony_ci
114262306a36Sopenharmony_ci	coded_packet = (struct batadv_coded_packet *)skb_dest->data;
114362306a36Sopenharmony_ci	skb_reset_mac_header(skb_dest);
114462306a36Sopenharmony_ci
114562306a36Sopenharmony_ci	coded_packet->packet_type = BATADV_CODED;
114662306a36Sopenharmony_ci	coded_packet->version = BATADV_COMPAT_VERSION;
114762306a36Sopenharmony_ci	coded_packet->ttl = packet1->ttl;
114862306a36Sopenharmony_ci
114962306a36Sopenharmony_ci	/* Info about first unicast packet */
115062306a36Sopenharmony_ci	ether_addr_copy(coded_packet->first_source, first_source);
115162306a36Sopenharmony_ci	ether_addr_copy(coded_packet->first_orig_dest, packet1->dest);
115262306a36Sopenharmony_ci	coded_packet->first_crc = packet_id1;
115362306a36Sopenharmony_ci	coded_packet->first_ttvn = packet1->ttvn;
115462306a36Sopenharmony_ci
115562306a36Sopenharmony_ci	/* Info about second unicast packet */
115662306a36Sopenharmony_ci	ether_addr_copy(coded_packet->second_dest, second_dest->addr);
115762306a36Sopenharmony_ci	ether_addr_copy(coded_packet->second_source, second_source);
115862306a36Sopenharmony_ci	ether_addr_copy(coded_packet->second_orig_dest, packet2->dest);
115962306a36Sopenharmony_ci	coded_packet->second_crc = packet_id2;
116062306a36Sopenharmony_ci	coded_packet->second_ttl = packet2->ttl;
116162306a36Sopenharmony_ci	coded_packet->second_ttvn = packet2->ttvn;
116262306a36Sopenharmony_ci	coded_packet->coded_len = htons(coding_len);
116362306a36Sopenharmony_ci
116462306a36Sopenharmony_ci	/* This is where the magic happens: Code skb_src into skb_dest */
116562306a36Sopenharmony_ci	batadv_nc_memxor(skb_dest->data + coded_size,
116662306a36Sopenharmony_ci			 skb_src->data + unicast_size, coding_len);
116762306a36Sopenharmony_ci
116862306a36Sopenharmony_ci	/* Update counters accordingly */
116962306a36Sopenharmony_ci	if (BATADV_SKB_CB(skb_src)->decoded &&
117062306a36Sopenharmony_ci	    BATADV_SKB_CB(skb_dest)->decoded) {
117162306a36Sopenharmony_ci		/* Both packets are recoded */
117262306a36Sopenharmony_ci		count = skb_src->len + ETH_HLEN;
117362306a36Sopenharmony_ci		count += skb_dest->len + ETH_HLEN;
117462306a36Sopenharmony_ci		batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE, 2);
117562306a36Sopenharmony_ci		batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES, count);
117662306a36Sopenharmony_ci	} else if (!BATADV_SKB_CB(skb_src)->decoded &&
117762306a36Sopenharmony_ci		   !BATADV_SKB_CB(skb_dest)->decoded) {
117862306a36Sopenharmony_ci		/* Both packets are newly coded */
117962306a36Sopenharmony_ci		count = skb_src->len + ETH_HLEN;
118062306a36Sopenharmony_ci		count += skb_dest->len + ETH_HLEN;
118162306a36Sopenharmony_ci		batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE, 2);
118262306a36Sopenharmony_ci		batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES, count);
118362306a36Sopenharmony_ci	} else if (BATADV_SKB_CB(skb_src)->decoded &&
118462306a36Sopenharmony_ci		   !BATADV_SKB_CB(skb_dest)->decoded) {
118562306a36Sopenharmony_ci		/* skb_src recoded and skb_dest is newly coded */
118662306a36Sopenharmony_ci		batadv_inc_counter(bat_priv, BATADV_CNT_NC_RECODE);
118762306a36Sopenharmony_ci		batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES,
118862306a36Sopenharmony_ci				   skb_src->len + ETH_HLEN);
118962306a36Sopenharmony_ci		batadv_inc_counter(bat_priv, BATADV_CNT_NC_CODE);
119062306a36Sopenharmony_ci		batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES,
119162306a36Sopenharmony_ci				   skb_dest->len + ETH_HLEN);
119262306a36Sopenharmony_ci	} else if (!BATADV_SKB_CB(skb_src)->decoded &&
119362306a36Sopenharmony_ci		   BATADV_SKB_CB(skb_dest)->decoded) {
119462306a36Sopenharmony_ci		/* skb_src is newly coded and skb_dest is recoded */
119562306a36Sopenharmony_ci		batadv_inc_counter(bat_priv, BATADV_CNT_NC_CODE);
119662306a36Sopenharmony_ci		batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES,
119762306a36Sopenharmony_ci				   skb_src->len + ETH_HLEN);
119862306a36Sopenharmony_ci		batadv_inc_counter(bat_priv, BATADV_CNT_NC_RECODE);
119962306a36Sopenharmony_ci		batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES,
120062306a36Sopenharmony_ci				   skb_dest->len + ETH_HLEN);
120162306a36Sopenharmony_ci	}
120262306a36Sopenharmony_ci
120362306a36Sopenharmony_ci	/* skb_src is now coded into skb_dest, so free it */
120462306a36Sopenharmony_ci	consume_skb(skb_src);
120562306a36Sopenharmony_ci
120662306a36Sopenharmony_ci	/* avoid duplicate free of skb from nc_packet */
120762306a36Sopenharmony_ci	nc_packet->skb = NULL;
120862306a36Sopenharmony_ci	batadv_nc_packet_free(nc_packet, false);
120962306a36Sopenharmony_ci
121062306a36Sopenharmony_ci	/* Send the coded packet and return true */
121162306a36Sopenharmony_ci	batadv_send_unicast_skb(skb_dest, first_dest);
121262306a36Sopenharmony_ci	res = true;
121362306a36Sopenharmony_ciout:
121462306a36Sopenharmony_ci	batadv_neigh_node_put(router_neigh);
121562306a36Sopenharmony_ci	batadv_neigh_node_put(router_coding);
121662306a36Sopenharmony_ci	batadv_neigh_ifinfo_put(router_neigh_ifinfo);
121762306a36Sopenharmony_ci	batadv_neigh_ifinfo_put(router_coding_ifinfo);
121862306a36Sopenharmony_ci	return res;
121962306a36Sopenharmony_ci}
122062306a36Sopenharmony_ci
122162306a36Sopenharmony_ci/**
122262306a36Sopenharmony_ci * batadv_nc_skb_coding_possible() - true if a decoded skb is available at dst.
122362306a36Sopenharmony_ci * @skb: data skb to forward
122462306a36Sopenharmony_ci * @dst: destination mac address of the other skb to code with
122562306a36Sopenharmony_ci * @src: source mac address of skb
122662306a36Sopenharmony_ci *
122762306a36Sopenharmony_ci * Whenever we network code a packet we have to check whether we received it in
122862306a36Sopenharmony_ci * a network coded form. If so, we may not be able to use it for coding because
122962306a36Sopenharmony_ci * some neighbors may also have received (overheard) the packet in the network
123062306a36Sopenharmony_ci * coded form without being able to decode it. It is hard to know which of the
123162306a36Sopenharmony_ci * neighboring nodes was able to decode the packet, therefore we can only
123262306a36Sopenharmony_ci * re-code the packet if the source of the previous encoded packet is involved.
123362306a36Sopenharmony_ci * Since the source encoded the packet we can be certain it has all necessary
123462306a36Sopenharmony_ci * decode information.
123562306a36Sopenharmony_ci *
123662306a36Sopenharmony_ci * Return: true if coding of a decoded packet is allowed.
123762306a36Sopenharmony_ci */
123862306a36Sopenharmony_cistatic bool batadv_nc_skb_coding_possible(struct sk_buff *skb, u8 *dst, u8 *src)
123962306a36Sopenharmony_ci{
124062306a36Sopenharmony_ci	if (BATADV_SKB_CB(skb)->decoded && !batadv_compare_eth(dst, src))
124162306a36Sopenharmony_ci		return false;
124262306a36Sopenharmony_ci	return true;
124362306a36Sopenharmony_ci}
124462306a36Sopenharmony_ci
124562306a36Sopenharmony_ci/**
124662306a36Sopenharmony_ci * batadv_nc_path_search() - Find the coding path matching in_nc_node and
124762306a36Sopenharmony_ci *  out_nc_node to retrieve a buffered packet that can be used for coding.
124862306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
124962306a36Sopenharmony_ci * @in_nc_node: pointer to skb next hop's neighbor nc node
125062306a36Sopenharmony_ci * @out_nc_node: pointer to skb source's neighbor nc node
125162306a36Sopenharmony_ci * @skb: data skb to forward
125262306a36Sopenharmony_ci * @eth_dst: next hop mac address of skb
125362306a36Sopenharmony_ci *
125462306a36Sopenharmony_ci * Return: true if coding of a decoded skb is allowed.
125562306a36Sopenharmony_ci */
125662306a36Sopenharmony_cistatic struct batadv_nc_packet *
125762306a36Sopenharmony_cibatadv_nc_path_search(struct batadv_priv *bat_priv,
125862306a36Sopenharmony_ci		      struct batadv_nc_node *in_nc_node,
125962306a36Sopenharmony_ci		      struct batadv_nc_node *out_nc_node,
126062306a36Sopenharmony_ci		      struct sk_buff *skb,
126162306a36Sopenharmony_ci		      u8 *eth_dst)
126262306a36Sopenharmony_ci{
126362306a36Sopenharmony_ci	struct batadv_nc_path *nc_path, nc_path_key;
126462306a36Sopenharmony_ci	struct batadv_nc_packet *nc_packet_out = NULL;
126562306a36Sopenharmony_ci	struct batadv_nc_packet *nc_packet, *nc_packet_tmp;
126662306a36Sopenharmony_ci	struct batadv_hashtable *hash = bat_priv->nc.coding_hash;
126762306a36Sopenharmony_ci	int idx;
126862306a36Sopenharmony_ci
126962306a36Sopenharmony_ci	if (!hash)
127062306a36Sopenharmony_ci		return NULL;
127162306a36Sopenharmony_ci
127262306a36Sopenharmony_ci	/* Create almost path key */
127362306a36Sopenharmony_ci	batadv_nc_hash_key_gen(&nc_path_key, in_nc_node->addr,
127462306a36Sopenharmony_ci			       out_nc_node->addr);
127562306a36Sopenharmony_ci	idx = batadv_nc_hash_choose(&nc_path_key, hash->size);
127662306a36Sopenharmony_ci
127762306a36Sopenharmony_ci	/* Check for coding opportunities in this nc_path */
127862306a36Sopenharmony_ci	rcu_read_lock();
127962306a36Sopenharmony_ci	hlist_for_each_entry_rcu(nc_path, &hash->table[idx], hash_entry) {
128062306a36Sopenharmony_ci		if (!batadv_compare_eth(nc_path->prev_hop, in_nc_node->addr))
128162306a36Sopenharmony_ci			continue;
128262306a36Sopenharmony_ci
128362306a36Sopenharmony_ci		if (!batadv_compare_eth(nc_path->next_hop, out_nc_node->addr))
128462306a36Sopenharmony_ci			continue;
128562306a36Sopenharmony_ci
128662306a36Sopenharmony_ci		spin_lock_bh(&nc_path->packet_list_lock);
128762306a36Sopenharmony_ci		if (list_empty(&nc_path->packet_list)) {
128862306a36Sopenharmony_ci			spin_unlock_bh(&nc_path->packet_list_lock);
128962306a36Sopenharmony_ci			continue;
129062306a36Sopenharmony_ci		}
129162306a36Sopenharmony_ci
129262306a36Sopenharmony_ci		list_for_each_entry_safe(nc_packet, nc_packet_tmp,
129362306a36Sopenharmony_ci					 &nc_path->packet_list, list) {
129462306a36Sopenharmony_ci			if (!batadv_nc_skb_coding_possible(nc_packet->skb,
129562306a36Sopenharmony_ci							   eth_dst,
129662306a36Sopenharmony_ci							   in_nc_node->addr))
129762306a36Sopenharmony_ci				continue;
129862306a36Sopenharmony_ci
129962306a36Sopenharmony_ci			/* Coding opportunity is found! */
130062306a36Sopenharmony_ci			list_del(&nc_packet->list);
130162306a36Sopenharmony_ci			nc_packet_out = nc_packet;
130262306a36Sopenharmony_ci			break;
130362306a36Sopenharmony_ci		}
130462306a36Sopenharmony_ci
130562306a36Sopenharmony_ci		spin_unlock_bh(&nc_path->packet_list_lock);
130662306a36Sopenharmony_ci		break;
130762306a36Sopenharmony_ci	}
130862306a36Sopenharmony_ci	rcu_read_unlock();
130962306a36Sopenharmony_ci
131062306a36Sopenharmony_ci	return nc_packet_out;
131162306a36Sopenharmony_ci}
131262306a36Sopenharmony_ci
131362306a36Sopenharmony_ci/**
131462306a36Sopenharmony_ci * batadv_nc_skb_src_search() - Loops through the list of neighboring nodes of
131562306a36Sopenharmony_ci *  the skb's sender (may be equal to the originator).
131662306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
131762306a36Sopenharmony_ci * @skb: data skb to forward
131862306a36Sopenharmony_ci * @eth_dst: next hop mac address of skb
131962306a36Sopenharmony_ci * @eth_src: source mac address of skb
132062306a36Sopenharmony_ci * @in_nc_node: pointer to skb next hop's neighbor nc node
132162306a36Sopenharmony_ci *
132262306a36Sopenharmony_ci * Return: an nc packet if a suitable coding packet was found, NULL otherwise.
132362306a36Sopenharmony_ci */
132462306a36Sopenharmony_cistatic struct batadv_nc_packet *
132562306a36Sopenharmony_cibatadv_nc_skb_src_search(struct batadv_priv *bat_priv,
132662306a36Sopenharmony_ci			 struct sk_buff *skb,
132762306a36Sopenharmony_ci			 u8 *eth_dst,
132862306a36Sopenharmony_ci			 u8 *eth_src,
132962306a36Sopenharmony_ci			 struct batadv_nc_node *in_nc_node)
133062306a36Sopenharmony_ci{
133162306a36Sopenharmony_ci	struct batadv_orig_node *orig_node;
133262306a36Sopenharmony_ci	struct batadv_nc_node *out_nc_node;
133362306a36Sopenharmony_ci	struct batadv_nc_packet *nc_packet = NULL;
133462306a36Sopenharmony_ci
133562306a36Sopenharmony_ci	orig_node = batadv_orig_hash_find(bat_priv, eth_src);
133662306a36Sopenharmony_ci	if (!orig_node)
133762306a36Sopenharmony_ci		return NULL;
133862306a36Sopenharmony_ci
133962306a36Sopenharmony_ci	rcu_read_lock();
134062306a36Sopenharmony_ci	list_for_each_entry_rcu(out_nc_node,
134162306a36Sopenharmony_ci				&orig_node->out_coding_list, list) {
134262306a36Sopenharmony_ci		/* Check if the skb is decoded and if recoding is possible */
134362306a36Sopenharmony_ci		if (!batadv_nc_skb_coding_possible(skb,
134462306a36Sopenharmony_ci						   out_nc_node->addr, eth_src))
134562306a36Sopenharmony_ci			continue;
134662306a36Sopenharmony_ci
134762306a36Sopenharmony_ci		/* Search for an opportunity in this nc_path */
134862306a36Sopenharmony_ci		nc_packet = batadv_nc_path_search(bat_priv, in_nc_node,
134962306a36Sopenharmony_ci						  out_nc_node, skb, eth_dst);
135062306a36Sopenharmony_ci		if (nc_packet)
135162306a36Sopenharmony_ci			break;
135262306a36Sopenharmony_ci	}
135362306a36Sopenharmony_ci	rcu_read_unlock();
135462306a36Sopenharmony_ci
135562306a36Sopenharmony_ci	batadv_orig_node_put(orig_node);
135662306a36Sopenharmony_ci	return nc_packet;
135762306a36Sopenharmony_ci}
135862306a36Sopenharmony_ci
135962306a36Sopenharmony_ci/**
136062306a36Sopenharmony_ci * batadv_nc_skb_store_before_coding() - set the ethernet src and dst of the
136162306a36Sopenharmony_ci *  unicast skb before it is stored for use in later decoding
136262306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
136362306a36Sopenharmony_ci * @skb: data skb to store
136462306a36Sopenharmony_ci * @eth_dst_new: new destination mac address of skb
136562306a36Sopenharmony_ci */
136662306a36Sopenharmony_cistatic void batadv_nc_skb_store_before_coding(struct batadv_priv *bat_priv,
136762306a36Sopenharmony_ci					      struct sk_buff *skb,
136862306a36Sopenharmony_ci					      u8 *eth_dst_new)
136962306a36Sopenharmony_ci{
137062306a36Sopenharmony_ci	struct ethhdr *ethhdr;
137162306a36Sopenharmony_ci
137262306a36Sopenharmony_ci	/* Copy skb header to change the mac header */
137362306a36Sopenharmony_ci	skb = pskb_copy_for_clone(skb, GFP_ATOMIC);
137462306a36Sopenharmony_ci	if (!skb)
137562306a36Sopenharmony_ci		return;
137662306a36Sopenharmony_ci
137762306a36Sopenharmony_ci	/* Set the mac header as if we actually sent the packet uncoded */
137862306a36Sopenharmony_ci	ethhdr = eth_hdr(skb);
137962306a36Sopenharmony_ci	ether_addr_copy(ethhdr->h_source, ethhdr->h_dest);
138062306a36Sopenharmony_ci	ether_addr_copy(ethhdr->h_dest, eth_dst_new);
138162306a36Sopenharmony_ci
138262306a36Sopenharmony_ci	/* Set data pointer to MAC header to mimic packets from our tx path */
138362306a36Sopenharmony_ci	skb_push(skb, ETH_HLEN);
138462306a36Sopenharmony_ci
138562306a36Sopenharmony_ci	/* Add the packet to the decoding packet pool */
138662306a36Sopenharmony_ci	batadv_nc_skb_store_for_decoding(bat_priv, skb);
138762306a36Sopenharmony_ci
138862306a36Sopenharmony_ci	/* batadv_nc_skb_store_for_decoding() clones the skb, so we must free
138962306a36Sopenharmony_ci	 * our ref
139062306a36Sopenharmony_ci	 */
139162306a36Sopenharmony_ci	consume_skb(skb);
139262306a36Sopenharmony_ci}
139362306a36Sopenharmony_ci
139462306a36Sopenharmony_ci/**
139562306a36Sopenharmony_ci * batadv_nc_skb_dst_search() - Loops through list of neighboring nodes to dst.
139662306a36Sopenharmony_ci * @skb: data skb to forward
139762306a36Sopenharmony_ci * @neigh_node: next hop to forward packet to
139862306a36Sopenharmony_ci * @ethhdr: pointer to the ethernet header inside the skb
139962306a36Sopenharmony_ci *
140062306a36Sopenharmony_ci * Loops through the list of neighboring nodes the next hop has a good
140162306a36Sopenharmony_ci * connection to (receives OGMs with a sufficient quality). We need to find a
140262306a36Sopenharmony_ci * neighbor of our next hop that potentially sent a packet which our next hop
140362306a36Sopenharmony_ci * also received (overheard) and has stored for later decoding.
140462306a36Sopenharmony_ci *
140562306a36Sopenharmony_ci * Return: true if the skb was consumed (encoded packet sent) or false otherwise
140662306a36Sopenharmony_ci */
140762306a36Sopenharmony_cistatic bool batadv_nc_skb_dst_search(struct sk_buff *skb,
140862306a36Sopenharmony_ci				     struct batadv_neigh_node *neigh_node,
140962306a36Sopenharmony_ci				     struct ethhdr *ethhdr)
141062306a36Sopenharmony_ci{
141162306a36Sopenharmony_ci	struct net_device *netdev = neigh_node->if_incoming->soft_iface;
141262306a36Sopenharmony_ci	struct batadv_priv *bat_priv = netdev_priv(netdev);
141362306a36Sopenharmony_ci	struct batadv_orig_node *orig_node = neigh_node->orig_node;
141462306a36Sopenharmony_ci	struct batadv_nc_node *nc_node;
141562306a36Sopenharmony_ci	struct batadv_nc_packet *nc_packet = NULL;
141662306a36Sopenharmony_ci
141762306a36Sopenharmony_ci	rcu_read_lock();
141862306a36Sopenharmony_ci	list_for_each_entry_rcu(nc_node, &orig_node->in_coding_list, list) {
141962306a36Sopenharmony_ci		/* Search for coding opportunity with this in_nc_node */
142062306a36Sopenharmony_ci		nc_packet = batadv_nc_skb_src_search(bat_priv, skb,
142162306a36Sopenharmony_ci						     neigh_node->addr,
142262306a36Sopenharmony_ci						     ethhdr->h_source, nc_node);
142362306a36Sopenharmony_ci
142462306a36Sopenharmony_ci		/* Opportunity was found, so stop searching */
142562306a36Sopenharmony_ci		if (nc_packet)
142662306a36Sopenharmony_ci			break;
142762306a36Sopenharmony_ci	}
142862306a36Sopenharmony_ci	rcu_read_unlock();
142962306a36Sopenharmony_ci
143062306a36Sopenharmony_ci	if (!nc_packet)
143162306a36Sopenharmony_ci		return false;
143262306a36Sopenharmony_ci
143362306a36Sopenharmony_ci	/* Save packets for later decoding */
143462306a36Sopenharmony_ci	batadv_nc_skb_store_before_coding(bat_priv, skb,
143562306a36Sopenharmony_ci					  neigh_node->addr);
143662306a36Sopenharmony_ci	batadv_nc_skb_store_before_coding(bat_priv, nc_packet->skb,
143762306a36Sopenharmony_ci					  nc_packet->neigh_node->addr);
143862306a36Sopenharmony_ci
143962306a36Sopenharmony_ci	/* Code and send packets */
144062306a36Sopenharmony_ci	if (batadv_nc_code_packets(bat_priv, skb, ethhdr, nc_packet,
144162306a36Sopenharmony_ci				   neigh_node))
144262306a36Sopenharmony_ci		return true;
144362306a36Sopenharmony_ci
144462306a36Sopenharmony_ci	/* out of mem ? Coding failed - we have to free the buffered packet
144562306a36Sopenharmony_ci	 * to avoid memleaks. The skb passed as argument will be dealt with
144662306a36Sopenharmony_ci	 * by the calling function.
144762306a36Sopenharmony_ci	 */
144862306a36Sopenharmony_ci	batadv_nc_send_packet(nc_packet);
144962306a36Sopenharmony_ci	return false;
145062306a36Sopenharmony_ci}
145162306a36Sopenharmony_ci
145262306a36Sopenharmony_ci/**
145362306a36Sopenharmony_ci * batadv_nc_skb_add_to_path() - buffer skb for later encoding / decoding
145462306a36Sopenharmony_ci * @skb: skb to add to path
145562306a36Sopenharmony_ci * @nc_path: path to add skb to
145662306a36Sopenharmony_ci * @neigh_node: next hop to forward packet to
145762306a36Sopenharmony_ci * @packet_id: checksum to identify packet
145862306a36Sopenharmony_ci *
145962306a36Sopenharmony_ci * Return: true if the packet was buffered or false in case of an error.
146062306a36Sopenharmony_ci */
146162306a36Sopenharmony_cistatic bool batadv_nc_skb_add_to_path(struct sk_buff *skb,
146262306a36Sopenharmony_ci				      struct batadv_nc_path *nc_path,
146362306a36Sopenharmony_ci				      struct batadv_neigh_node *neigh_node,
146462306a36Sopenharmony_ci				      __be32 packet_id)
146562306a36Sopenharmony_ci{
146662306a36Sopenharmony_ci	struct batadv_nc_packet *nc_packet;
146762306a36Sopenharmony_ci
146862306a36Sopenharmony_ci	nc_packet = kzalloc(sizeof(*nc_packet), GFP_ATOMIC);
146962306a36Sopenharmony_ci	if (!nc_packet)
147062306a36Sopenharmony_ci		return false;
147162306a36Sopenharmony_ci
147262306a36Sopenharmony_ci	/* Initialize nc_packet */
147362306a36Sopenharmony_ci	nc_packet->timestamp = jiffies;
147462306a36Sopenharmony_ci	nc_packet->packet_id = packet_id;
147562306a36Sopenharmony_ci	nc_packet->skb = skb;
147662306a36Sopenharmony_ci	nc_packet->neigh_node = neigh_node;
147762306a36Sopenharmony_ci	nc_packet->nc_path = nc_path;
147862306a36Sopenharmony_ci
147962306a36Sopenharmony_ci	/* Add coding packet to list */
148062306a36Sopenharmony_ci	spin_lock_bh(&nc_path->packet_list_lock);
148162306a36Sopenharmony_ci	list_add_tail(&nc_packet->list, &nc_path->packet_list);
148262306a36Sopenharmony_ci	spin_unlock_bh(&nc_path->packet_list_lock);
148362306a36Sopenharmony_ci
148462306a36Sopenharmony_ci	return true;
148562306a36Sopenharmony_ci}
148662306a36Sopenharmony_ci
148762306a36Sopenharmony_ci/**
148862306a36Sopenharmony_ci * batadv_nc_skb_forward() - try to code a packet or add it to the coding packet
148962306a36Sopenharmony_ci *  buffer
149062306a36Sopenharmony_ci * @skb: data skb to forward
149162306a36Sopenharmony_ci * @neigh_node: next hop to forward packet to
149262306a36Sopenharmony_ci *
149362306a36Sopenharmony_ci * Return: true if the skb was consumed (encoded packet sent) or false otherwise
149462306a36Sopenharmony_ci */
149562306a36Sopenharmony_cibool batadv_nc_skb_forward(struct sk_buff *skb,
149662306a36Sopenharmony_ci			   struct batadv_neigh_node *neigh_node)
149762306a36Sopenharmony_ci{
149862306a36Sopenharmony_ci	const struct net_device *netdev = neigh_node->if_incoming->soft_iface;
149962306a36Sopenharmony_ci	struct batadv_priv *bat_priv = netdev_priv(netdev);
150062306a36Sopenharmony_ci	struct batadv_unicast_packet *packet;
150162306a36Sopenharmony_ci	struct batadv_nc_path *nc_path;
150262306a36Sopenharmony_ci	struct ethhdr *ethhdr = eth_hdr(skb);
150362306a36Sopenharmony_ci	__be32 packet_id;
150462306a36Sopenharmony_ci	u8 *payload;
150562306a36Sopenharmony_ci
150662306a36Sopenharmony_ci	/* Check if network coding is enabled */
150762306a36Sopenharmony_ci	if (!atomic_read(&bat_priv->network_coding))
150862306a36Sopenharmony_ci		goto out;
150962306a36Sopenharmony_ci
151062306a36Sopenharmony_ci	/* We only handle unicast packets */
151162306a36Sopenharmony_ci	payload = skb_network_header(skb);
151262306a36Sopenharmony_ci	packet = (struct batadv_unicast_packet *)payload;
151362306a36Sopenharmony_ci	if (packet->packet_type != BATADV_UNICAST)
151462306a36Sopenharmony_ci		goto out;
151562306a36Sopenharmony_ci
151662306a36Sopenharmony_ci	/* Try to find a coding opportunity and send the skb if one is found */
151762306a36Sopenharmony_ci	if (batadv_nc_skb_dst_search(skb, neigh_node, ethhdr))
151862306a36Sopenharmony_ci		return true;
151962306a36Sopenharmony_ci
152062306a36Sopenharmony_ci	/* Find or create a nc_path for this src-dst pair */
152162306a36Sopenharmony_ci	nc_path = batadv_nc_get_path(bat_priv,
152262306a36Sopenharmony_ci				     bat_priv->nc.coding_hash,
152362306a36Sopenharmony_ci				     ethhdr->h_source,
152462306a36Sopenharmony_ci				     neigh_node->addr);
152562306a36Sopenharmony_ci
152662306a36Sopenharmony_ci	if (!nc_path)
152762306a36Sopenharmony_ci		goto out;
152862306a36Sopenharmony_ci
152962306a36Sopenharmony_ci	/* Add skb to nc_path */
153062306a36Sopenharmony_ci	packet_id = batadv_skb_crc32(skb, payload + sizeof(*packet));
153162306a36Sopenharmony_ci	if (!batadv_nc_skb_add_to_path(skb, nc_path, neigh_node, packet_id))
153262306a36Sopenharmony_ci		goto free_nc_path;
153362306a36Sopenharmony_ci
153462306a36Sopenharmony_ci	/* Packet is consumed */
153562306a36Sopenharmony_ci	return true;
153662306a36Sopenharmony_ci
153762306a36Sopenharmony_cifree_nc_path:
153862306a36Sopenharmony_ci	batadv_nc_path_put(nc_path);
153962306a36Sopenharmony_ciout:
154062306a36Sopenharmony_ci	/* Packet is not consumed */
154162306a36Sopenharmony_ci	return false;
154262306a36Sopenharmony_ci}
154362306a36Sopenharmony_ci
154462306a36Sopenharmony_ci/**
154562306a36Sopenharmony_ci * batadv_nc_skb_store_for_decoding() - save a clone of the skb which can be
154662306a36Sopenharmony_ci *  used when decoding coded packets
154762306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
154862306a36Sopenharmony_ci * @skb: data skb to store
154962306a36Sopenharmony_ci */
155062306a36Sopenharmony_civoid batadv_nc_skb_store_for_decoding(struct batadv_priv *bat_priv,
155162306a36Sopenharmony_ci				      struct sk_buff *skb)
155262306a36Sopenharmony_ci{
155362306a36Sopenharmony_ci	struct batadv_unicast_packet *packet;
155462306a36Sopenharmony_ci	struct batadv_nc_path *nc_path;
155562306a36Sopenharmony_ci	struct ethhdr *ethhdr = eth_hdr(skb);
155662306a36Sopenharmony_ci	__be32 packet_id;
155762306a36Sopenharmony_ci	u8 *payload;
155862306a36Sopenharmony_ci
155962306a36Sopenharmony_ci	/* Check if network coding is enabled */
156062306a36Sopenharmony_ci	if (!atomic_read(&bat_priv->network_coding))
156162306a36Sopenharmony_ci		goto out;
156262306a36Sopenharmony_ci
156362306a36Sopenharmony_ci	/* Check for supported packet type */
156462306a36Sopenharmony_ci	payload = skb_network_header(skb);
156562306a36Sopenharmony_ci	packet = (struct batadv_unicast_packet *)payload;
156662306a36Sopenharmony_ci	if (packet->packet_type != BATADV_UNICAST)
156762306a36Sopenharmony_ci		goto out;
156862306a36Sopenharmony_ci
156962306a36Sopenharmony_ci	/* Find existing nc_path or create a new */
157062306a36Sopenharmony_ci	nc_path = batadv_nc_get_path(bat_priv,
157162306a36Sopenharmony_ci				     bat_priv->nc.decoding_hash,
157262306a36Sopenharmony_ci				     ethhdr->h_source,
157362306a36Sopenharmony_ci				     ethhdr->h_dest);
157462306a36Sopenharmony_ci
157562306a36Sopenharmony_ci	if (!nc_path)
157662306a36Sopenharmony_ci		goto out;
157762306a36Sopenharmony_ci
157862306a36Sopenharmony_ci	/* Clone skb and adjust skb->data to point at batman header */
157962306a36Sopenharmony_ci	skb = skb_clone(skb, GFP_ATOMIC);
158062306a36Sopenharmony_ci	if (unlikely(!skb))
158162306a36Sopenharmony_ci		goto free_nc_path;
158262306a36Sopenharmony_ci
158362306a36Sopenharmony_ci	if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
158462306a36Sopenharmony_ci		goto free_skb;
158562306a36Sopenharmony_ci
158662306a36Sopenharmony_ci	if (unlikely(!skb_pull_rcsum(skb, ETH_HLEN)))
158762306a36Sopenharmony_ci		goto free_skb;
158862306a36Sopenharmony_ci
158962306a36Sopenharmony_ci	/* Add skb to nc_path */
159062306a36Sopenharmony_ci	packet_id = batadv_skb_crc32(skb, payload + sizeof(*packet));
159162306a36Sopenharmony_ci	if (!batadv_nc_skb_add_to_path(skb, nc_path, NULL, packet_id))
159262306a36Sopenharmony_ci		goto free_skb;
159362306a36Sopenharmony_ci
159462306a36Sopenharmony_ci	batadv_inc_counter(bat_priv, BATADV_CNT_NC_BUFFER);
159562306a36Sopenharmony_ci	return;
159662306a36Sopenharmony_ci
159762306a36Sopenharmony_cifree_skb:
159862306a36Sopenharmony_ci	kfree_skb(skb);
159962306a36Sopenharmony_cifree_nc_path:
160062306a36Sopenharmony_ci	batadv_nc_path_put(nc_path);
160162306a36Sopenharmony_ciout:
160262306a36Sopenharmony_ci	return;
160362306a36Sopenharmony_ci}
160462306a36Sopenharmony_ci
160562306a36Sopenharmony_ci/**
160662306a36Sopenharmony_ci * batadv_nc_skb_store_sniffed_unicast() - check if a received unicast packet
160762306a36Sopenharmony_ci *  should be saved in the decoding buffer and, if so, store it there
160862306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
160962306a36Sopenharmony_ci * @skb: unicast skb to store
161062306a36Sopenharmony_ci */
161162306a36Sopenharmony_civoid batadv_nc_skb_store_sniffed_unicast(struct batadv_priv *bat_priv,
161262306a36Sopenharmony_ci					 struct sk_buff *skb)
161362306a36Sopenharmony_ci{
161462306a36Sopenharmony_ci	struct ethhdr *ethhdr = eth_hdr(skb);
161562306a36Sopenharmony_ci
161662306a36Sopenharmony_ci	if (batadv_is_my_mac(bat_priv, ethhdr->h_dest))
161762306a36Sopenharmony_ci		return;
161862306a36Sopenharmony_ci
161962306a36Sopenharmony_ci	/* Set data pointer to MAC header to mimic packets from our tx path */
162062306a36Sopenharmony_ci	skb_push(skb, ETH_HLEN);
162162306a36Sopenharmony_ci
162262306a36Sopenharmony_ci	batadv_nc_skb_store_for_decoding(bat_priv, skb);
162362306a36Sopenharmony_ci}
162462306a36Sopenharmony_ci
162562306a36Sopenharmony_ci/**
162662306a36Sopenharmony_ci * batadv_nc_skb_decode_packet() - decode given skb using the decode data stored
162762306a36Sopenharmony_ci *  in nc_packet
162862306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
162962306a36Sopenharmony_ci * @skb: unicast skb to decode
163062306a36Sopenharmony_ci * @nc_packet: decode data needed to decode the skb
163162306a36Sopenharmony_ci *
163262306a36Sopenharmony_ci * Return: pointer to decoded unicast packet if the packet was decoded or NULL
163362306a36Sopenharmony_ci * in case of an error.
163462306a36Sopenharmony_ci */
163562306a36Sopenharmony_cistatic struct batadv_unicast_packet *
163662306a36Sopenharmony_cibatadv_nc_skb_decode_packet(struct batadv_priv *bat_priv, struct sk_buff *skb,
163762306a36Sopenharmony_ci			    struct batadv_nc_packet *nc_packet)
163862306a36Sopenharmony_ci{
163962306a36Sopenharmony_ci	const int h_size = sizeof(struct batadv_unicast_packet);
164062306a36Sopenharmony_ci	const int h_diff = sizeof(struct batadv_coded_packet) - h_size;
164162306a36Sopenharmony_ci	struct batadv_unicast_packet *unicast_packet;
164262306a36Sopenharmony_ci	struct batadv_coded_packet coded_packet_tmp;
164362306a36Sopenharmony_ci	struct ethhdr *ethhdr, ethhdr_tmp;
164462306a36Sopenharmony_ci	u8 *orig_dest, ttl, ttvn;
164562306a36Sopenharmony_ci	unsigned int coding_len;
164662306a36Sopenharmony_ci	int err;
164762306a36Sopenharmony_ci
164862306a36Sopenharmony_ci	/* Save headers temporarily */
164962306a36Sopenharmony_ci	memcpy(&coded_packet_tmp, skb->data, sizeof(coded_packet_tmp));
165062306a36Sopenharmony_ci	memcpy(&ethhdr_tmp, skb_mac_header(skb), sizeof(ethhdr_tmp));
165162306a36Sopenharmony_ci
165262306a36Sopenharmony_ci	if (skb_cow(skb, 0) < 0)
165362306a36Sopenharmony_ci		return NULL;
165462306a36Sopenharmony_ci
165562306a36Sopenharmony_ci	if (unlikely(!skb_pull_rcsum(skb, h_diff)))
165662306a36Sopenharmony_ci		return NULL;
165762306a36Sopenharmony_ci
165862306a36Sopenharmony_ci	/* Data points to batman header, so set mac header 14 bytes before
165962306a36Sopenharmony_ci	 * and network to data
166062306a36Sopenharmony_ci	 */
166162306a36Sopenharmony_ci	skb_set_mac_header(skb, -ETH_HLEN);
166262306a36Sopenharmony_ci	skb_reset_network_header(skb);
166362306a36Sopenharmony_ci
166462306a36Sopenharmony_ci	/* Reconstruct original mac header */
166562306a36Sopenharmony_ci	ethhdr = eth_hdr(skb);
166662306a36Sopenharmony_ci	*ethhdr = ethhdr_tmp;
166762306a36Sopenharmony_ci
166862306a36Sopenharmony_ci	/* Select the correct unicast header information based on the location
166962306a36Sopenharmony_ci	 * of our mac address in the coded_packet header
167062306a36Sopenharmony_ci	 */
167162306a36Sopenharmony_ci	if (batadv_is_my_mac(bat_priv, coded_packet_tmp.second_dest)) {
167262306a36Sopenharmony_ci		/* If we are the second destination the packet was overheard,
167362306a36Sopenharmony_ci		 * so the Ethernet address must be copied to h_dest and
167462306a36Sopenharmony_ci		 * pkt_type changed from PACKET_OTHERHOST to PACKET_HOST
167562306a36Sopenharmony_ci		 */
167662306a36Sopenharmony_ci		ether_addr_copy(ethhdr->h_dest, coded_packet_tmp.second_dest);
167762306a36Sopenharmony_ci		skb->pkt_type = PACKET_HOST;
167862306a36Sopenharmony_ci
167962306a36Sopenharmony_ci		orig_dest = coded_packet_tmp.second_orig_dest;
168062306a36Sopenharmony_ci		ttl = coded_packet_tmp.second_ttl;
168162306a36Sopenharmony_ci		ttvn = coded_packet_tmp.second_ttvn;
168262306a36Sopenharmony_ci	} else {
168362306a36Sopenharmony_ci		orig_dest = coded_packet_tmp.first_orig_dest;
168462306a36Sopenharmony_ci		ttl = coded_packet_tmp.ttl;
168562306a36Sopenharmony_ci		ttvn = coded_packet_tmp.first_ttvn;
168662306a36Sopenharmony_ci	}
168762306a36Sopenharmony_ci
168862306a36Sopenharmony_ci	coding_len = ntohs(coded_packet_tmp.coded_len);
168962306a36Sopenharmony_ci
169062306a36Sopenharmony_ci	if (coding_len > skb->len)
169162306a36Sopenharmony_ci		return NULL;
169262306a36Sopenharmony_ci
169362306a36Sopenharmony_ci	/* Here the magic is reversed:
169462306a36Sopenharmony_ci	 *   extract the missing packet from the received coded packet
169562306a36Sopenharmony_ci	 */
169662306a36Sopenharmony_ci	batadv_nc_memxor(skb->data + h_size,
169762306a36Sopenharmony_ci			 nc_packet->skb->data + h_size,
169862306a36Sopenharmony_ci			 coding_len);
169962306a36Sopenharmony_ci
170062306a36Sopenharmony_ci	/* Resize decoded skb if decoded with larger packet */
170162306a36Sopenharmony_ci	if (nc_packet->skb->len > coding_len + h_size) {
170262306a36Sopenharmony_ci		err = pskb_trim_rcsum(skb, coding_len + h_size);
170362306a36Sopenharmony_ci		if (err)
170462306a36Sopenharmony_ci			return NULL;
170562306a36Sopenharmony_ci	}
170662306a36Sopenharmony_ci
170762306a36Sopenharmony_ci	/* Create decoded unicast packet */
170862306a36Sopenharmony_ci	unicast_packet = (struct batadv_unicast_packet *)skb->data;
170962306a36Sopenharmony_ci	unicast_packet->packet_type = BATADV_UNICAST;
171062306a36Sopenharmony_ci	unicast_packet->version = BATADV_COMPAT_VERSION;
171162306a36Sopenharmony_ci	unicast_packet->ttl = ttl;
171262306a36Sopenharmony_ci	ether_addr_copy(unicast_packet->dest, orig_dest);
171362306a36Sopenharmony_ci	unicast_packet->ttvn = ttvn;
171462306a36Sopenharmony_ci
171562306a36Sopenharmony_ci	batadv_nc_packet_free(nc_packet, false);
171662306a36Sopenharmony_ci	return unicast_packet;
171762306a36Sopenharmony_ci}
171862306a36Sopenharmony_ci
171962306a36Sopenharmony_ci/**
172062306a36Sopenharmony_ci * batadv_nc_find_decoding_packet() - search through buffered decoding data to
172162306a36Sopenharmony_ci *  find the data needed to decode the coded packet
172262306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
172362306a36Sopenharmony_ci * @ethhdr: pointer to the ethernet header inside the coded packet
172462306a36Sopenharmony_ci * @coded: coded packet we try to find decode data for
172562306a36Sopenharmony_ci *
172662306a36Sopenharmony_ci * Return: pointer to nc packet if the needed data was found or NULL otherwise.
172762306a36Sopenharmony_ci */
172862306a36Sopenharmony_cistatic struct batadv_nc_packet *
172962306a36Sopenharmony_cibatadv_nc_find_decoding_packet(struct batadv_priv *bat_priv,
173062306a36Sopenharmony_ci			       struct ethhdr *ethhdr,
173162306a36Sopenharmony_ci			       struct batadv_coded_packet *coded)
173262306a36Sopenharmony_ci{
173362306a36Sopenharmony_ci	struct batadv_hashtable *hash = bat_priv->nc.decoding_hash;
173462306a36Sopenharmony_ci	struct batadv_nc_packet *tmp_nc_packet, *nc_packet = NULL;
173562306a36Sopenharmony_ci	struct batadv_nc_path *nc_path, nc_path_key;
173662306a36Sopenharmony_ci	u8 *dest, *source;
173762306a36Sopenharmony_ci	__be32 packet_id;
173862306a36Sopenharmony_ci	int index;
173962306a36Sopenharmony_ci
174062306a36Sopenharmony_ci	if (!hash)
174162306a36Sopenharmony_ci		return NULL;
174262306a36Sopenharmony_ci
174362306a36Sopenharmony_ci	/* Select the correct packet id based on the location of our mac-addr */
174462306a36Sopenharmony_ci	dest = ethhdr->h_source;
174562306a36Sopenharmony_ci	if (!batadv_is_my_mac(bat_priv, coded->second_dest)) {
174662306a36Sopenharmony_ci		source = coded->second_source;
174762306a36Sopenharmony_ci		packet_id = coded->second_crc;
174862306a36Sopenharmony_ci	} else {
174962306a36Sopenharmony_ci		source = coded->first_source;
175062306a36Sopenharmony_ci		packet_id = coded->first_crc;
175162306a36Sopenharmony_ci	}
175262306a36Sopenharmony_ci
175362306a36Sopenharmony_ci	batadv_nc_hash_key_gen(&nc_path_key, source, dest);
175462306a36Sopenharmony_ci	index = batadv_nc_hash_choose(&nc_path_key, hash->size);
175562306a36Sopenharmony_ci
175662306a36Sopenharmony_ci	/* Search for matching coding path */
175762306a36Sopenharmony_ci	rcu_read_lock();
175862306a36Sopenharmony_ci	hlist_for_each_entry_rcu(nc_path, &hash->table[index], hash_entry) {
175962306a36Sopenharmony_ci		/* Find matching nc_packet */
176062306a36Sopenharmony_ci		spin_lock_bh(&nc_path->packet_list_lock);
176162306a36Sopenharmony_ci		list_for_each_entry(tmp_nc_packet,
176262306a36Sopenharmony_ci				    &nc_path->packet_list, list) {
176362306a36Sopenharmony_ci			if (packet_id == tmp_nc_packet->packet_id) {
176462306a36Sopenharmony_ci				list_del(&tmp_nc_packet->list);
176562306a36Sopenharmony_ci
176662306a36Sopenharmony_ci				nc_packet = tmp_nc_packet;
176762306a36Sopenharmony_ci				break;
176862306a36Sopenharmony_ci			}
176962306a36Sopenharmony_ci		}
177062306a36Sopenharmony_ci		spin_unlock_bh(&nc_path->packet_list_lock);
177162306a36Sopenharmony_ci
177262306a36Sopenharmony_ci		if (nc_packet)
177362306a36Sopenharmony_ci			break;
177462306a36Sopenharmony_ci	}
177562306a36Sopenharmony_ci	rcu_read_unlock();
177662306a36Sopenharmony_ci
177762306a36Sopenharmony_ci	if (!nc_packet)
177862306a36Sopenharmony_ci		batadv_dbg(BATADV_DBG_NC, bat_priv,
177962306a36Sopenharmony_ci			   "No decoding packet found for %u\n", packet_id);
178062306a36Sopenharmony_ci
178162306a36Sopenharmony_ci	return nc_packet;
178262306a36Sopenharmony_ci}
178362306a36Sopenharmony_ci
178462306a36Sopenharmony_ci/**
178562306a36Sopenharmony_ci * batadv_nc_recv_coded_packet() - try to decode coded packet and enqueue the
178662306a36Sopenharmony_ci *  resulting unicast packet
178762306a36Sopenharmony_ci * @skb: incoming coded packet
178862306a36Sopenharmony_ci * @recv_if: pointer to interface this packet was received on
178962306a36Sopenharmony_ci *
179062306a36Sopenharmony_ci * Return: NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
179162306a36Sopenharmony_ci * otherwise.
179262306a36Sopenharmony_ci */
179362306a36Sopenharmony_cistatic int batadv_nc_recv_coded_packet(struct sk_buff *skb,
179462306a36Sopenharmony_ci				       struct batadv_hard_iface *recv_if)
179562306a36Sopenharmony_ci{
179662306a36Sopenharmony_ci	struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
179762306a36Sopenharmony_ci	struct batadv_unicast_packet *unicast_packet;
179862306a36Sopenharmony_ci	struct batadv_coded_packet *coded_packet;
179962306a36Sopenharmony_ci	struct batadv_nc_packet *nc_packet;
180062306a36Sopenharmony_ci	struct ethhdr *ethhdr;
180162306a36Sopenharmony_ci	int hdr_size = sizeof(*coded_packet);
180262306a36Sopenharmony_ci
180362306a36Sopenharmony_ci	/* Check if network coding is enabled */
180462306a36Sopenharmony_ci	if (!atomic_read(&bat_priv->network_coding))
180562306a36Sopenharmony_ci		goto free_skb;
180662306a36Sopenharmony_ci
180762306a36Sopenharmony_ci	/* Make sure we can access (and remove) header */
180862306a36Sopenharmony_ci	if (unlikely(!pskb_may_pull(skb, hdr_size)))
180962306a36Sopenharmony_ci		goto free_skb;
181062306a36Sopenharmony_ci
181162306a36Sopenharmony_ci	coded_packet = (struct batadv_coded_packet *)skb->data;
181262306a36Sopenharmony_ci	ethhdr = eth_hdr(skb);
181362306a36Sopenharmony_ci
181462306a36Sopenharmony_ci	/* Verify frame is destined for us */
181562306a36Sopenharmony_ci	if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest) &&
181662306a36Sopenharmony_ci	    !batadv_is_my_mac(bat_priv, coded_packet->second_dest))
181762306a36Sopenharmony_ci		goto free_skb;
181862306a36Sopenharmony_ci
181962306a36Sopenharmony_ci	/* Update stat counter */
182062306a36Sopenharmony_ci	if (batadv_is_my_mac(bat_priv, coded_packet->second_dest))
182162306a36Sopenharmony_ci		batadv_inc_counter(bat_priv, BATADV_CNT_NC_SNIFFED);
182262306a36Sopenharmony_ci
182362306a36Sopenharmony_ci	nc_packet = batadv_nc_find_decoding_packet(bat_priv, ethhdr,
182462306a36Sopenharmony_ci						   coded_packet);
182562306a36Sopenharmony_ci	if (!nc_packet) {
182662306a36Sopenharmony_ci		batadv_inc_counter(bat_priv, BATADV_CNT_NC_DECODE_FAILED);
182762306a36Sopenharmony_ci		goto free_skb;
182862306a36Sopenharmony_ci	}
182962306a36Sopenharmony_ci
183062306a36Sopenharmony_ci	/* Make skb's linear, because decoding accesses the entire buffer */
183162306a36Sopenharmony_ci	if (skb_linearize(skb) < 0)
183262306a36Sopenharmony_ci		goto free_nc_packet;
183362306a36Sopenharmony_ci
183462306a36Sopenharmony_ci	if (skb_linearize(nc_packet->skb) < 0)
183562306a36Sopenharmony_ci		goto free_nc_packet;
183662306a36Sopenharmony_ci
183762306a36Sopenharmony_ci	/* Decode the packet */
183862306a36Sopenharmony_ci	unicast_packet = batadv_nc_skb_decode_packet(bat_priv, skb, nc_packet);
183962306a36Sopenharmony_ci	if (!unicast_packet) {
184062306a36Sopenharmony_ci		batadv_inc_counter(bat_priv, BATADV_CNT_NC_DECODE_FAILED);
184162306a36Sopenharmony_ci		goto free_nc_packet;
184262306a36Sopenharmony_ci	}
184362306a36Sopenharmony_ci
184462306a36Sopenharmony_ci	/* Mark packet as decoded to do correct recoding when forwarding */
184562306a36Sopenharmony_ci	BATADV_SKB_CB(skb)->decoded = true;
184662306a36Sopenharmony_ci	batadv_inc_counter(bat_priv, BATADV_CNT_NC_DECODE);
184762306a36Sopenharmony_ci	batadv_add_counter(bat_priv, BATADV_CNT_NC_DECODE_BYTES,
184862306a36Sopenharmony_ci			   skb->len + ETH_HLEN);
184962306a36Sopenharmony_ci	return batadv_recv_unicast_packet(skb, recv_if);
185062306a36Sopenharmony_ci
185162306a36Sopenharmony_cifree_nc_packet:
185262306a36Sopenharmony_ci	batadv_nc_packet_free(nc_packet, true);
185362306a36Sopenharmony_cifree_skb:
185462306a36Sopenharmony_ci	kfree_skb(skb);
185562306a36Sopenharmony_ci
185662306a36Sopenharmony_ci	return NET_RX_DROP;
185762306a36Sopenharmony_ci}
185862306a36Sopenharmony_ci
185962306a36Sopenharmony_ci/**
186062306a36Sopenharmony_ci * batadv_nc_mesh_free() - clean up network coding memory
186162306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
186262306a36Sopenharmony_ci */
186362306a36Sopenharmony_civoid batadv_nc_mesh_free(struct batadv_priv *bat_priv)
186462306a36Sopenharmony_ci{
186562306a36Sopenharmony_ci	batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_NC, 1);
186662306a36Sopenharmony_ci	batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_NC, 1);
186762306a36Sopenharmony_ci	cancel_delayed_work_sync(&bat_priv->nc.work);
186862306a36Sopenharmony_ci
186962306a36Sopenharmony_ci	batadv_nc_purge_paths(bat_priv, bat_priv->nc.coding_hash, NULL);
187062306a36Sopenharmony_ci	batadv_hash_destroy(bat_priv->nc.coding_hash);
187162306a36Sopenharmony_ci	batadv_nc_purge_paths(bat_priv, bat_priv->nc.decoding_hash, NULL);
187262306a36Sopenharmony_ci	batadv_hash_destroy(bat_priv->nc.decoding_hash);
187362306a36Sopenharmony_ci}
1874