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 <martin@hundeboll.net>
562306a36Sopenharmony_ci */
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci#include "fragmentation.h"
862306a36Sopenharmony_ci#include "main.h"
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include <linux/atomic.h>
1162306a36Sopenharmony_ci#include <linux/byteorder/generic.h>
1262306a36Sopenharmony_ci#include <linux/errno.h>
1362306a36Sopenharmony_ci#include <linux/etherdevice.h>
1462306a36Sopenharmony_ci#include <linux/gfp.h>
1562306a36Sopenharmony_ci#include <linux/if_ether.h>
1662306a36Sopenharmony_ci#include <linux/jiffies.h>
1762306a36Sopenharmony_ci#include <linux/lockdep.h>
1862306a36Sopenharmony_ci#include <linux/minmax.h>
1962306a36Sopenharmony_ci#include <linux/netdevice.h>
2062306a36Sopenharmony_ci#include <linux/skbuff.h>
2162306a36Sopenharmony_ci#include <linux/slab.h>
2262306a36Sopenharmony_ci#include <linux/spinlock.h>
2362306a36Sopenharmony_ci#include <linux/string.h>
2462306a36Sopenharmony_ci#include <uapi/linux/batadv_packet.h>
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci#include "hard-interface.h"
2762306a36Sopenharmony_ci#include "originator.h"
2862306a36Sopenharmony_ci#include "routing.h"
2962306a36Sopenharmony_ci#include "send.h"
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci/**
3262306a36Sopenharmony_ci * batadv_frag_clear_chain() - delete entries in the fragment buffer chain
3362306a36Sopenharmony_ci * @head: head of chain with entries.
3462306a36Sopenharmony_ci * @dropped: whether the chain is cleared because all fragments are dropped
3562306a36Sopenharmony_ci *
3662306a36Sopenharmony_ci * Free fragments in the passed hlist. Should be called with appropriate lock.
3762306a36Sopenharmony_ci */
3862306a36Sopenharmony_cistatic void batadv_frag_clear_chain(struct hlist_head *head, bool dropped)
3962306a36Sopenharmony_ci{
4062306a36Sopenharmony_ci	struct batadv_frag_list_entry *entry;
4162306a36Sopenharmony_ci	struct hlist_node *node;
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci	hlist_for_each_entry_safe(entry, node, head, list) {
4462306a36Sopenharmony_ci		hlist_del(&entry->list);
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci		if (dropped)
4762306a36Sopenharmony_ci			kfree_skb(entry->skb);
4862306a36Sopenharmony_ci		else
4962306a36Sopenharmony_ci			consume_skb(entry->skb);
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci		kfree(entry);
5262306a36Sopenharmony_ci	}
5362306a36Sopenharmony_ci}
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci/**
5662306a36Sopenharmony_ci * batadv_frag_purge_orig() - free fragments associated to an orig
5762306a36Sopenharmony_ci * @orig_node: originator to free fragments from
5862306a36Sopenharmony_ci * @check_cb: optional function to tell if an entry should be purged
5962306a36Sopenharmony_ci */
6062306a36Sopenharmony_civoid batadv_frag_purge_orig(struct batadv_orig_node *orig_node,
6162306a36Sopenharmony_ci			    bool (*check_cb)(struct batadv_frag_table_entry *))
6262306a36Sopenharmony_ci{
6362306a36Sopenharmony_ci	struct batadv_frag_table_entry *chain;
6462306a36Sopenharmony_ci	u8 i;
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci	for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
6762306a36Sopenharmony_ci		chain = &orig_node->fragments[i];
6862306a36Sopenharmony_ci		spin_lock_bh(&chain->lock);
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci		if (!check_cb || check_cb(chain)) {
7162306a36Sopenharmony_ci			batadv_frag_clear_chain(&chain->fragment_list, true);
7262306a36Sopenharmony_ci			chain->size = 0;
7362306a36Sopenharmony_ci		}
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci		spin_unlock_bh(&chain->lock);
7662306a36Sopenharmony_ci	}
7762306a36Sopenharmony_ci}
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci/**
8062306a36Sopenharmony_ci * batadv_frag_size_limit() - maximum possible size of packet to be fragmented
8162306a36Sopenharmony_ci *
8262306a36Sopenharmony_ci * Return: the maximum size of payload that can be fragmented.
8362306a36Sopenharmony_ci */
8462306a36Sopenharmony_cistatic int batadv_frag_size_limit(void)
8562306a36Sopenharmony_ci{
8662306a36Sopenharmony_ci	int limit = BATADV_FRAG_MAX_FRAG_SIZE;
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci	limit -= sizeof(struct batadv_frag_packet);
8962306a36Sopenharmony_ci	limit *= BATADV_FRAG_MAX_FRAGMENTS;
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci	return limit;
9262306a36Sopenharmony_ci}
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci/**
9562306a36Sopenharmony_ci * batadv_frag_init_chain() - check and prepare fragment chain for new fragment
9662306a36Sopenharmony_ci * @chain: chain in fragments table to init
9762306a36Sopenharmony_ci * @seqno: sequence number of the received fragment
9862306a36Sopenharmony_ci *
9962306a36Sopenharmony_ci * Make chain ready for a fragment with sequence number "seqno". Delete existing
10062306a36Sopenharmony_ci * entries if they have an "old" sequence number.
10162306a36Sopenharmony_ci *
10262306a36Sopenharmony_ci * Caller must hold chain->lock.
10362306a36Sopenharmony_ci *
10462306a36Sopenharmony_ci * Return: true if chain is empty and the caller can just insert the new
10562306a36Sopenharmony_ci * fragment without searching for the right position.
10662306a36Sopenharmony_ci */
10762306a36Sopenharmony_cistatic bool batadv_frag_init_chain(struct batadv_frag_table_entry *chain,
10862306a36Sopenharmony_ci				   u16 seqno)
10962306a36Sopenharmony_ci{
11062306a36Sopenharmony_ci	lockdep_assert_held(&chain->lock);
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci	if (chain->seqno == seqno)
11362306a36Sopenharmony_ci		return false;
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci	if (!hlist_empty(&chain->fragment_list))
11662306a36Sopenharmony_ci		batadv_frag_clear_chain(&chain->fragment_list, true);
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci	chain->size = 0;
11962306a36Sopenharmony_ci	chain->seqno = seqno;
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci	return true;
12262306a36Sopenharmony_ci}
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci/**
12562306a36Sopenharmony_ci * batadv_frag_insert_packet() - insert a fragment into a fragment chain
12662306a36Sopenharmony_ci * @orig_node: originator that the fragment was received from
12762306a36Sopenharmony_ci * @skb: skb to insert
12862306a36Sopenharmony_ci * @chain_out: list head to attach complete chains of fragments to
12962306a36Sopenharmony_ci *
13062306a36Sopenharmony_ci * Insert a new fragment into the reverse ordered chain in the right table
13162306a36Sopenharmony_ci * entry. The hash table entry is cleared if "old" fragments exist in it.
13262306a36Sopenharmony_ci *
13362306a36Sopenharmony_ci * Return: true if skb is buffered, false on error. If the chain has all the
13462306a36Sopenharmony_ci * fragments needed to merge the packet, the chain is moved to the passed head
13562306a36Sopenharmony_ci * to avoid locking the chain in the table.
13662306a36Sopenharmony_ci */
13762306a36Sopenharmony_cistatic bool batadv_frag_insert_packet(struct batadv_orig_node *orig_node,
13862306a36Sopenharmony_ci				      struct sk_buff *skb,
13962306a36Sopenharmony_ci				      struct hlist_head *chain_out)
14062306a36Sopenharmony_ci{
14162306a36Sopenharmony_ci	struct batadv_frag_table_entry *chain;
14262306a36Sopenharmony_ci	struct batadv_frag_list_entry *frag_entry_new = NULL, *frag_entry_curr;
14362306a36Sopenharmony_ci	struct batadv_frag_list_entry *frag_entry_last = NULL;
14462306a36Sopenharmony_ci	struct batadv_frag_packet *frag_packet;
14562306a36Sopenharmony_ci	u8 bucket;
14662306a36Sopenharmony_ci	u16 seqno, hdr_size = sizeof(struct batadv_frag_packet);
14762306a36Sopenharmony_ci	bool ret = false;
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci	/* Linearize packet to avoid linearizing 16 packets in a row when doing
15062306a36Sopenharmony_ci	 * the later merge. Non-linear merge should be added to remove this
15162306a36Sopenharmony_ci	 * linearization.
15262306a36Sopenharmony_ci	 */
15362306a36Sopenharmony_ci	if (skb_linearize(skb) < 0)
15462306a36Sopenharmony_ci		goto err;
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci	frag_packet = (struct batadv_frag_packet *)skb->data;
15762306a36Sopenharmony_ci	seqno = ntohs(frag_packet->seqno);
15862306a36Sopenharmony_ci	bucket = seqno % BATADV_FRAG_BUFFER_COUNT;
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci	frag_entry_new = kmalloc(sizeof(*frag_entry_new), GFP_ATOMIC);
16162306a36Sopenharmony_ci	if (!frag_entry_new)
16262306a36Sopenharmony_ci		goto err;
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci	frag_entry_new->skb = skb;
16562306a36Sopenharmony_ci	frag_entry_new->no = frag_packet->no;
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci	/* Select entry in the "chain table" and delete any prior fragments
16862306a36Sopenharmony_ci	 * with another sequence number. batadv_frag_init_chain() returns true,
16962306a36Sopenharmony_ci	 * if the list is empty at return.
17062306a36Sopenharmony_ci	 */
17162306a36Sopenharmony_ci	chain = &orig_node->fragments[bucket];
17262306a36Sopenharmony_ci	spin_lock_bh(&chain->lock);
17362306a36Sopenharmony_ci	if (batadv_frag_init_chain(chain, seqno)) {
17462306a36Sopenharmony_ci		hlist_add_head(&frag_entry_new->list, &chain->fragment_list);
17562306a36Sopenharmony_ci		chain->size = skb->len - hdr_size;
17662306a36Sopenharmony_ci		chain->timestamp = jiffies;
17762306a36Sopenharmony_ci		chain->total_size = ntohs(frag_packet->total_size);
17862306a36Sopenharmony_ci		ret = true;
17962306a36Sopenharmony_ci		goto out;
18062306a36Sopenharmony_ci	}
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci	/* Find the position for the new fragment. */
18362306a36Sopenharmony_ci	hlist_for_each_entry(frag_entry_curr, &chain->fragment_list, list) {
18462306a36Sopenharmony_ci		/* Drop packet if fragment already exists. */
18562306a36Sopenharmony_ci		if (frag_entry_curr->no == frag_entry_new->no)
18662306a36Sopenharmony_ci			goto err_unlock;
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ci		/* Order fragments from highest to lowest. */
18962306a36Sopenharmony_ci		if (frag_entry_curr->no < frag_entry_new->no) {
19062306a36Sopenharmony_ci			hlist_add_before(&frag_entry_new->list,
19162306a36Sopenharmony_ci					 &frag_entry_curr->list);
19262306a36Sopenharmony_ci			chain->size += skb->len - hdr_size;
19362306a36Sopenharmony_ci			chain->timestamp = jiffies;
19462306a36Sopenharmony_ci			ret = true;
19562306a36Sopenharmony_ci			goto out;
19662306a36Sopenharmony_ci		}
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci		/* store current entry because it could be the last in list */
19962306a36Sopenharmony_ci		frag_entry_last = frag_entry_curr;
20062306a36Sopenharmony_ci	}
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_ci	/* Reached the end of the list, so insert after 'frag_entry_last'. */
20362306a36Sopenharmony_ci	if (likely(frag_entry_last)) {
20462306a36Sopenharmony_ci		hlist_add_behind(&frag_entry_new->list, &frag_entry_last->list);
20562306a36Sopenharmony_ci		chain->size += skb->len - hdr_size;
20662306a36Sopenharmony_ci		chain->timestamp = jiffies;
20762306a36Sopenharmony_ci		ret = true;
20862306a36Sopenharmony_ci	}
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ciout:
21162306a36Sopenharmony_ci	if (chain->size > batadv_frag_size_limit() ||
21262306a36Sopenharmony_ci	    chain->total_size != ntohs(frag_packet->total_size) ||
21362306a36Sopenharmony_ci	    chain->total_size > batadv_frag_size_limit()) {
21462306a36Sopenharmony_ci		/* Clear chain if total size of either the list or the packet
21562306a36Sopenharmony_ci		 * exceeds the maximum size of one merged packet. Don't allow
21662306a36Sopenharmony_ci		 * packets to have different total_size.
21762306a36Sopenharmony_ci		 */
21862306a36Sopenharmony_ci		batadv_frag_clear_chain(&chain->fragment_list, true);
21962306a36Sopenharmony_ci		chain->size = 0;
22062306a36Sopenharmony_ci	} else if (ntohs(frag_packet->total_size) == chain->size) {
22162306a36Sopenharmony_ci		/* All fragments received. Hand over chain to caller. */
22262306a36Sopenharmony_ci		hlist_move_list(&chain->fragment_list, chain_out);
22362306a36Sopenharmony_ci		chain->size = 0;
22462306a36Sopenharmony_ci	}
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_cierr_unlock:
22762306a36Sopenharmony_ci	spin_unlock_bh(&chain->lock);
22862306a36Sopenharmony_ci
22962306a36Sopenharmony_cierr:
23062306a36Sopenharmony_ci	if (!ret) {
23162306a36Sopenharmony_ci		kfree(frag_entry_new);
23262306a36Sopenharmony_ci		kfree_skb(skb);
23362306a36Sopenharmony_ci	}
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ci	return ret;
23662306a36Sopenharmony_ci}
23762306a36Sopenharmony_ci
23862306a36Sopenharmony_ci/**
23962306a36Sopenharmony_ci * batadv_frag_merge_packets() - merge a chain of fragments
24062306a36Sopenharmony_ci * @chain: head of chain with fragments
24162306a36Sopenharmony_ci *
24262306a36Sopenharmony_ci * Expand the first skb in the chain and copy the content of the remaining
24362306a36Sopenharmony_ci * skb's into the expanded one. After doing so, clear the chain.
24462306a36Sopenharmony_ci *
24562306a36Sopenharmony_ci * Return: the merged skb or NULL on error.
24662306a36Sopenharmony_ci */
24762306a36Sopenharmony_cistatic struct sk_buff *
24862306a36Sopenharmony_cibatadv_frag_merge_packets(struct hlist_head *chain)
24962306a36Sopenharmony_ci{
25062306a36Sopenharmony_ci	struct batadv_frag_packet *packet;
25162306a36Sopenharmony_ci	struct batadv_frag_list_entry *entry;
25262306a36Sopenharmony_ci	struct sk_buff *skb_out;
25362306a36Sopenharmony_ci	int size, hdr_size = sizeof(struct batadv_frag_packet);
25462306a36Sopenharmony_ci	bool dropped = false;
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_ci	/* Remove first entry, as this is the destination for the rest of the
25762306a36Sopenharmony_ci	 * fragments.
25862306a36Sopenharmony_ci	 */
25962306a36Sopenharmony_ci	entry = hlist_entry(chain->first, struct batadv_frag_list_entry, list);
26062306a36Sopenharmony_ci	hlist_del(&entry->list);
26162306a36Sopenharmony_ci	skb_out = entry->skb;
26262306a36Sopenharmony_ci	kfree(entry);
26362306a36Sopenharmony_ci
26462306a36Sopenharmony_ci	packet = (struct batadv_frag_packet *)skb_out->data;
26562306a36Sopenharmony_ci	size = ntohs(packet->total_size) + hdr_size;
26662306a36Sopenharmony_ci
26762306a36Sopenharmony_ci	/* Make room for the rest of the fragments. */
26862306a36Sopenharmony_ci	if (pskb_expand_head(skb_out, 0, size - skb_out->len, GFP_ATOMIC) < 0) {
26962306a36Sopenharmony_ci		kfree_skb(skb_out);
27062306a36Sopenharmony_ci		skb_out = NULL;
27162306a36Sopenharmony_ci		dropped = true;
27262306a36Sopenharmony_ci		goto free;
27362306a36Sopenharmony_ci	}
27462306a36Sopenharmony_ci
27562306a36Sopenharmony_ci	/* Move the existing MAC header to just before the payload. (Override
27662306a36Sopenharmony_ci	 * the fragment header.)
27762306a36Sopenharmony_ci	 */
27862306a36Sopenharmony_ci	skb_pull(skb_out, hdr_size);
27962306a36Sopenharmony_ci	skb_out->ip_summed = CHECKSUM_NONE;
28062306a36Sopenharmony_ci	memmove(skb_out->data - ETH_HLEN, skb_mac_header(skb_out), ETH_HLEN);
28162306a36Sopenharmony_ci	skb_set_mac_header(skb_out, -ETH_HLEN);
28262306a36Sopenharmony_ci	skb_reset_network_header(skb_out);
28362306a36Sopenharmony_ci	skb_reset_transport_header(skb_out);
28462306a36Sopenharmony_ci
28562306a36Sopenharmony_ci	/* Copy the payload of the each fragment into the last skb */
28662306a36Sopenharmony_ci	hlist_for_each_entry(entry, chain, list) {
28762306a36Sopenharmony_ci		size = entry->skb->len - hdr_size;
28862306a36Sopenharmony_ci		skb_put_data(skb_out, entry->skb->data + hdr_size, size);
28962306a36Sopenharmony_ci	}
29062306a36Sopenharmony_ci
29162306a36Sopenharmony_cifree:
29262306a36Sopenharmony_ci	/* Locking is not needed, because 'chain' is not part of any orig. */
29362306a36Sopenharmony_ci	batadv_frag_clear_chain(chain, dropped);
29462306a36Sopenharmony_ci	return skb_out;
29562306a36Sopenharmony_ci}
29662306a36Sopenharmony_ci
29762306a36Sopenharmony_ci/**
29862306a36Sopenharmony_ci * batadv_frag_skb_buffer() - buffer fragment for later merge
29962306a36Sopenharmony_ci * @skb: skb to buffer
30062306a36Sopenharmony_ci * @orig_node_src: originator that the skb is received from
30162306a36Sopenharmony_ci *
30262306a36Sopenharmony_ci * Add fragment to buffer and merge fragments if possible.
30362306a36Sopenharmony_ci *
30462306a36Sopenharmony_ci * There are three possible outcomes: 1) Packet is merged: Return true and
30562306a36Sopenharmony_ci * set *skb to merged packet; 2) Packet is buffered: Return true and set *skb
30662306a36Sopenharmony_ci * to NULL; 3) Error: Return false and free skb.
30762306a36Sopenharmony_ci *
30862306a36Sopenharmony_ci * Return: true when the packet is merged or buffered, false when skb is not
30962306a36Sopenharmony_ci * used.
31062306a36Sopenharmony_ci */
31162306a36Sopenharmony_cibool batadv_frag_skb_buffer(struct sk_buff **skb,
31262306a36Sopenharmony_ci			    struct batadv_orig_node *orig_node_src)
31362306a36Sopenharmony_ci{
31462306a36Sopenharmony_ci	struct sk_buff *skb_out = NULL;
31562306a36Sopenharmony_ci	struct hlist_head head = HLIST_HEAD_INIT;
31662306a36Sopenharmony_ci	bool ret = false;
31762306a36Sopenharmony_ci
31862306a36Sopenharmony_ci	/* Add packet to buffer and table entry if merge is possible. */
31962306a36Sopenharmony_ci	if (!batadv_frag_insert_packet(orig_node_src, *skb, &head))
32062306a36Sopenharmony_ci		goto out_err;
32162306a36Sopenharmony_ci
32262306a36Sopenharmony_ci	/* Leave if more fragments are needed to merge. */
32362306a36Sopenharmony_ci	if (hlist_empty(&head))
32462306a36Sopenharmony_ci		goto out;
32562306a36Sopenharmony_ci
32662306a36Sopenharmony_ci	skb_out = batadv_frag_merge_packets(&head);
32762306a36Sopenharmony_ci	if (!skb_out)
32862306a36Sopenharmony_ci		goto out_err;
32962306a36Sopenharmony_ci
33062306a36Sopenharmony_ciout:
33162306a36Sopenharmony_ci	ret = true;
33262306a36Sopenharmony_ciout_err:
33362306a36Sopenharmony_ci	*skb = skb_out;
33462306a36Sopenharmony_ci	return ret;
33562306a36Sopenharmony_ci}
33662306a36Sopenharmony_ci
33762306a36Sopenharmony_ci/**
33862306a36Sopenharmony_ci * batadv_frag_skb_fwd() - forward fragments that would exceed MTU when merged
33962306a36Sopenharmony_ci * @skb: skb to forward
34062306a36Sopenharmony_ci * @recv_if: interface that the skb is received on
34162306a36Sopenharmony_ci * @orig_node_src: originator that the skb is received from
34262306a36Sopenharmony_ci *
34362306a36Sopenharmony_ci * Look up the next-hop of the fragments payload and check if the merged packet
34462306a36Sopenharmony_ci * will exceed the MTU towards the next-hop. If so, the fragment is forwarded
34562306a36Sopenharmony_ci * without merging it.
34662306a36Sopenharmony_ci *
34762306a36Sopenharmony_ci * Return: true if the fragment is consumed/forwarded, false otherwise.
34862306a36Sopenharmony_ci */
34962306a36Sopenharmony_cibool batadv_frag_skb_fwd(struct sk_buff *skb,
35062306a36Sopenharmony_ci			 struct batadv_hard_iface *recv_if,
35162306a36Sopenharmony_ci			 struct batadv_orig_node *orig_node_src)
35262306a36Sopenharmony_ci{
35362306a36Sopenharmony_ci	struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
35462306a36Sopenharmony_ci	struct batadv_orig_node *orig_node_dst;
35562306a36Sopenharmony_ci	struct batadv_neigh_node *neigh_node = NULL;
35662306a36Sopenharmony_ci	struct batadv_frag_packet *packet;
35762306a36Sopenharmony_ci	u16 total_size;
35862306a36Sopenharmony_ci	bool ret = false;
35962306a36Sopenharmony_ci
36062306a36Sopenharmony_ci	packet = (struct batadv_frag_packet *)skb->data;
36162306a36Sopenharmony_ci	orig_node_dst = batadv_orig_hash_find(bat_priv, packet->dest);
36262306a36Sopenharmony_ci	if (!orig_node_dst)
36362306a36Sopenharmony_ci		goto out;
36462306a36Sopenharmony_ci
36562306a36Sopenharmony_ci	neigh_node = batadv_find_router(bat_priv, orig_node_dst, recv_if);
36662306a36Sopenharmony_ci	if (!neigh_node)
36762306a36Sopenharmony_ci		goto out;
36862306a36Sopenharmony_ci
36962306a36Sopenharmony_ci	/* Forward the fragment, if the merged packet would be too big to
37062306a36Sopenharmony_ci	 * be assembled.
37162306a36Sopenharmony_ci	 */
37262306a36Sopenharmony_ci	total_size = ntohs(packet->total_size);
37362306a36Sopenharmony_ci	if (total_size > neigh_node->if_incoming->net_dev->mtu) {
37462306a36Sopenharmony_ci		batadv_inc_counter(bat_priv, BATADV_CNT_FRAG_FWD);
37562306a36Sopenharmony_ci		batadv_add_counter(bat_priv, BATADV_CNT_FRAG_FWD_BYTES,
37662306a36Sopenharmony_ci				   skb->len + ETH_HLEN);
37762306a36Sopenharmony_ci
37862306a36Sopenharmony_ci		packet->ttl--;
37962306a36Sopenharmony_ci		batadv_send_unicast_skb(skb, neigh_node);
38062306a36Sopenharmony_ci		ret = true;
38162306a36Sopenharmony_ci	}
38262306a36Sopenharmony_ci
38362306a36Sopenharmony_ciout:
38462306a36Sopenharmony_ci	batadv_orig_node_put(orig_node_dst);
38562306a36Sopenharmony_ci	batadv_neigh_node_put(neigh_node);
38662306a36Sopenharmony_ci	return ret;
38762306a36Sopenharmony_ci}
38862306a36Sopenharmony_ci
38962306a36Sopenharmony_ci/**
39062306a36Sopenharmony_ci * batadv_frag_create() - create a fragment from skb
39162306a36Sopenharmony_ci * @net_dev: outgoing device for fragment
39262306a36Sopenharmony_ci * @skb: skb to create fragment from
39362306a36Sopenharmony_ci * @frag_head: header to use in new fragment
39462306a36Sopenharmony_ci * @fragment_size: size of new fragment
39562306a36Sopenharmony_ci *
39662306a36Sopenharmony_ci * Split the passed skb into two fragments: A new one with size matching the
39762306a36Sopenharmony_ci * passed mtu and the old one with the rest. The new skb contains data from the
39862306a36Sopenharmony_ci * tail of the old skb.
39962306a36Sopenharmony_ci *
40062306a36Sopenharmony_ci * Return: the new fragment, NULL on error.
40162306a36Sopenharmony_ci */
40262306a36Sopenharmony_cistatic struct sk_buff *batadv_frag_create(struct net_device *net_dev,
40362306a36Sopenharmony_ci					  struct sk_buff *skb,
40462306a36Sopenharmony_ci					  struct batadv_frag_packet *frag_head,
40562306a36Sopenharmony_ci					  unsigned int fragment_size)
40662306a36Sopenharmony_ci{
40762306a36Sopenharmony_ci	unsigned int ll_reserved = LL_RESERVED_SPACE(net_dev);
40862306a36Sopenharmony_ci	unsigned int tailroom = net_dev->needed_tailroom;
40962306a36Sopenharmony_ci	struct sk_buff *skb_fragment;
41062306a36Sopenharmony_ci	unsigned int header_size = sizeof(*frag_head);
41162306a36Sopenharmony_ci	unsigned int mtu = fragment_size + header_size;
41262306a36Sopenharmony_ci
41362306a36Sopenharmony_ci	skb_fragment = dev_alloc_skb(ll_reserved + mtu + tailroom);
41462306a36Sopenharmony_ci	if (!skb_fragment)
41562306a36Sopenharmony_ci		goto err;
41662306a36Sopenharmony_ci
41762306a36Sopenharmony_ci	skb_fragment->priority = skb->priority;
41862306a36Sopenharmony_ci
41962306a36Sopenharmony_ci	/* Eat the last mtu-bytes of the skb */
42062306a36Sopenharmony_ci	skb_reserve(skb_fragment, ll_reserved + header_size);
42162306a36Sopenharmony_ci	skb_split(skb, skb_fragment, skb->len - fragment_size);
42262306a36Sopenharmony_ci
42362306a36Sopenharmony_ci	/* Add the header */
42462306a36Sopenharmony_ci	skb_push(skb_fragment, header_size);
42562306a36Sopenharmony_ci	memcpy(skb_fragment->data, frag_head, header_size);
42662306a36Sopenharmony_ci
42762306a36Sopenharmony_cierr:
42862306a36Sopenharmony_ci	return skb_fragment;
42962306a36Sopenharmony_ci}
43062306a36Sopenharmony_ci
43162306a36Sopenharmony_ci/**
43262306a36Sopenharmony_ci * batadv_frag_send_packet() - create up to 16 fragments from the passed skb
43362306a36Sopenharmony_ci * @skb: skb to create fragments from
43462306a36Sopenharmony_ci * @orig_node: final destination of the created fragments
43562306a36Sopenharmony_ci * @neigh_node: next-hop of the created fragments
43662306a36Sopenharmony_ci *
43762306a36Sopenharmony_ci * Return: the netdev tx status or a negative errno code on a failure
43862306a36Sopenharmony_ci */
43962306a36Sopenharmony_ciint batadv_frag_send_packet(struct sk_buff *skb,
44062306a36Sopenharmony_ci			    struct batadv_orig_node *orig_node,
44162306a36Sopenharmony_ci			    struct batadv_neigh_node *neigh_node)
44262306a36Sopenharmony_ci{
44362306a36Sopenharmony_ci	struct net_device *net_dev = neigh_node->if_incoming->net_dev;
44462306a36Sopenharmony_ci	struct batadv_priv *bat_priv;
44562306a36Sopenharmony_ci	struct batadv_hard_iface *primary_if = NULL;
44662306a36Sopenharmony_ci	struct batadv_frag_packet frag_header;
44762306a36Sopenharmony_ci	struct sk_buff *skb_fragment;
44862306a36Sopenharmony_ci	unsigned int mtu = net_dev->mtu;
44962306a36Sopenharmony_ci	unsigned int header_size = sizeof(frag_header);
45062306a36Sopenharmony_ci	unsigned int max_fragment_size, num_fragments;
45162306a36Sopenharmony_ci	int ret;
45262306a36Sopenharmony_ci
45362306a36Sopenharmony_ci	/* To avoid merge and refragmentation at next-hops we never send
45462306a36Sopenharmony_ci	 * fragments larger than BATADV_FRAG_MAX_FRAG_SIZE
45562306a36Sopenharmony_ci	 */
45662306a36Sopenharmony_ci	mtu = min_t(unsigned int, mtu, BATADV_FRAG_MAX_FRAG_SIZE);
45762306a36Sopenharmony_ci	max_fragment_size = mtu - header_size;
45862306a36Sopenharmony_ci
45962306a36Sopenharmony_ci	if (skb->len == 0 || max_fragment_size == 0)
46062306a36Sopenharmony_ci		return -EINVAL;
46162306a36Sopenharmony_ci
46262306a36Sopenharmony_ci	num_fragments = (skb->len - 1) / max_fragment_size + 1;
46362306a36Sopenharmony_ci	max_fragment_size = (skb->len - 1) / num_fragments + 1;
46462306a36Sopenharmony_ci
46562306a36Sopenharmony_ci	/* Don't even try to fragment, if we need more than 16 fragments */
46662306a36Sopenharmony_ci	if (num_fragments > BATADV_FRAG_MAX_FRAGMENTS) {
46762306a36Sopenharmony_ci		ret = -EAGAIN;
46862306a36Sopenharmony_ci		goto free_skb;
46962306a36Sopenharmony_ci	}
47062306a36Sopenharmony_ci
47162306a36Sopenharmony_ci	bat_priv = orig_node->bat_priv;
47262306a36Sopenharmony_ci	primary_if = batadv_primary_if_get_selected(bat_priv);
47362306a36Sopenharmony_ci	if (!primary_if) {
47462306a36Sopenharmony_ci		ret = -EINVAL;
47562306a36Sopenharmony_ci		goto free_skb;
47662306a36Sopenharmony_ci	}
47762306a36Sopenharmony_ci
47862306a36Sopenharmony_ci	/* GRO might have added fragments to the fragment list instead of
47962306a36Sopenharmony_ci	 * frags[]. But this is not handled by skb_split and must be
48062306a36Sopenharmony_ci	 * linearized to avoid incorrect length information after all
48162306a36Sopenharmony_ci	 * batman-adv fragments were created and submitted to the
48262306a36Sopenharmony_ci	 * hard-interface
48362306a36Sopenharmony_ci	 */
48462306a36Sopenharmony_ci	if (skb_has_frag_list(skb) && __skb_linearize(skb)) {
48562306a36Sopenharmony_ci		ret = -ENOMEM;
48662306a36Sopenharmony_ci		goto free_skb;
48762306a36Sopenharmony_ci	}
48862306a36Sopenharmony_ci
48962306a36Sopenharmony_ci	/* Create one header to be copied to all fragments */
49062306a36Sopenharmony_ci	frag_header.packet_type = BATADV_UNICAST_FRAG;
49162306a36Sopenharmony_ci	frag_header.version = BATADV_COMPAT_VERSION;
49262306a36Sopenharmony_ci	frag_header.ttl = BATADV_TTL;
49362306a36Sopenharmony_ci	frag_header.seqno = htons(atomic_inc_return(&bat_priv->frag_seqno));
49462306a36Sopenharmony_ci	frag_header.reserved = 0;
49562306a36Sopenharmony_ci	frag_header.no = 0;
49662306a36Sopenharmony_ci	frag_header.total_size = htons(skb->len);
49762306a36Sopenharmony_ci
49862306a36Sopenharmony_ci	/* skb->priority values from 256->263 are magic values to
49962306a36Sopenharmony_ci	 * directly indicate a specific 802.1d priority.  This is used
50062306a36Sopenharmony_ci	 * to allow 802.1d priority to be passed directly in from VLAN
50162306a36Sopenharmony_ci	 * tags, etc.
50262306a36Sopenharmony_ci	 */
50362306a36Sopenharmony_ci	if (skb->priority >= 256 && skb->priority <= 263)
50462306a36Sopenharmony_ci		frag_header.priority = skb->priority - 256;
50562306a36Sopenharmony_ci	else
50662306a36Sopenharmony_ci		frag_header.priority = 0;
50762306a36Sopenharmony_ci
50862306a36Sopenharmony_ci	ether_addr_copy(frag_header.orig, primary_if->net_dev->dev_addr);
50962306a36Sopenharmony_ci	ether_addr_copy(frag_header.dest, orig_node->orig);
51062306a36Sopenharmony_ci
51162306a36Sopenharmony_ci	/* Eat and send fragments from the tail of skb */
51262306a36Sopenharmony_ci	while (skb->len > max_fragment_size) {
51362306a36Sopenharmony_ci		/* The initial check in this function should cover this case */
51462306a36Sopenharmony_ci		if (unlikely(frag_header.no == BATADV_FRAG_MAX_FRAGMENTS - 1)) {
51562306a36Sopenharmony_ci			ret = -EINVAL;
51662306a36Sopenharmony_ci			goto put_primary_if;
51762306a36Sopenharmony_ci		}
51862306a36Sopenharmony_ci
51962306a36Sopenharmony_ci		skb_fragment = batadv_frag_create(net_dev, skb, &frag_header,
52062306a36Sopenharmony_ci						  max_fragment_size);
52162306a36Sopenharmony_ci		if (!skb_fragment) {
52262306a36Sopenharmony_ci			ret = -ENOMEM;
52362306a36Sopenharmony_ci			goto put_primary_if;
52462306a36Sopenharmony_ci		}
52562306a36Sopenharmony_ci
52662306a36Sopenharmony_ci		batadv_inc_counter(bat_priv, BATADV_CNT_FRAG_TX);
52762306a36Sopenharmony_ci		batadv_add_counter(bat_priv, BATADV_CNT_FRAG_TX_BYTES,
52862306a36Sopenharmony_ci				   skb_fragment->len + ETH_HLEN);
52962306a36Sopenharmony_ci		ret = batadv_send_unicast_skb(skb_fragment, neigh_node);
53062306a36Sopenharmony_ci		if (ret != NET_XMIT_SUCCESS) {
53162306a36Sopenharmony_ci			ret = NET_XMIT_DROP;
53262306a36Sopenharmony_ci			goto put_primary_if;
53362306a36Sopenharmony_ci		}
53462306a36Sopenharmony_ci
53562306a36Sopenharmony_ci		frag_header.no++;
53662306a36Sopenharmony_ci	}
53762306a36Sopenharmony_ci
53862306a36Sopenharmony_ci	/* make sure that there is at least enough head for the fragmentation
53962306a36Sopenharmony_ci	 * and ethernet headers
54062306a36Sopenharmony_ci	 */
54162306a36Sopenharmony_ci	ret = skb_cow_head(skb, ETH_HLEN + header_size);
54262306a36Sopenharmony_ci	if (ret < 0)
54362306a36Sopenharmony_ci		goto put_primary_if;
54462306a36Sopenharmony_ci
54562306a36Sopenharmony_ci	skb_push(skb, header_size);
54662306a36Sopenharmony_ci	memcpy(skb->data, &frag_header, header_size);
54762306a36Sopenharmony_ci
54862306a36Sopenharmony_ci	/* Send the last fragment */
54962306a36Sopenharmony_ci	batadv_inc_counter(bat_priv, BATADV_CNT_FRAG_TX);
55062306a36Sopenharmony_ci	batadv_add_counter(bat_priv, BATADV_CNT_FRAG_TX_BYTES,
55162306a36Sopenharmony_ci			   skb->len + ETH_HLEN);
55262306a36Sopenharmony_ci	ret = batadv_send_unicast_skb(skb, neigh_node);
55362306a36Sopenharmony_ci	/* skb was consumed */
55462306a36Sopenharmony_ci	skb = NULL;
55562306a36Sopenharmony_ci
55662306a36Sopenharmony_ciput_primary_if:
55762306a36Sopenharmony_ci	batadv_hardif_put(primary_if);
55862306a36Sopenharmony_cifree_skb:
55962306a36Sopenharmony_ci	kfree_skb(skb);
56062306a36Sopenharmony_ci
56162306a36Sopenharmony_ci	return ret;
56262306a36Sopenharmony_ci}
563