xref: /openbmc/linux/net/batman-adv/fragmentation.c (revision fcd193e1dfa6842e2783b04d98345767fe99cf31)
17db7d9f3SSven Eckelmann // SPDX-License-Identifier: GPL-2.0
268e039f9SSven Eckelmann /* Copyright (C) 2013-2020  B.A.T.M.A.N. contributors:
3610bfc6bSMartin Hundebøll  *
4610bfc6bSMartin Hundebøll  * Martin Hundebøll <martin@hundeboll.net>
5610bfc6bSMartin Hundebøll  */
6610bfc6bSMartin Hundebøll 
7610bfc6bSMartin Hundebøll #include "fragmentation.h"
81e2c2a4fSSven Eckelmann #include "main.h"
91e2c2a4fSSven Eckelmann 
101e2c2a4fSSven Eckelmann #include <linux/atomic.h>
111e2c2a4fSSven Eckelmann #include <linux/byteorder/generic.h>
128def0be8SSven Eckelmann #include <linux/errno.h>
131e2c2a4fSSven Eckelmann #include <linux/etherdevice.h>
14b92b94acSSven Eckelmann #include <linux/gfp.h>
151e2c2a4fSSven Eckelmann #include <linux/if_ether.h>
161e2c2a4fSSven Eckelmann #include <linux/jiffies.h>
175274cd68SSven Eckelmann #include <linux/lockdep.h>
18*fcd193e1SSven Eckelmann #include <linux/minmax.h>
191e2c2a4fSSven Eckelmann #include <linux/netdevice.h>
201e2c2a4fSSven Eckelmann #include <linux/skbuff.h>
211e2c2a4fSSven Eckelmann #include <linux/slab.h>
221e2c2a4fSSven Eckelmann #include <linux/spinlock.h>
231e2c2a4fSSven Eckelmann #include <linux/string.h>
24fec149f5SSven Eckelmann #include <uapi/linux/batadv_packet.h>
251e2c2a4fSSven Eckelmann 
26610bfc6bSMartin Hundebøll #include "hard-interface.h"
271e2c2a4fSSven Eckelmann #include "originator.h"
281e2c2a4fSSven Eckelmann #include "routing.h"
291e2c2a4fSSven Eckelmann #include "send.h"
30610bfc6bSMartin Hundebøll #include "soft-interface.h"
31610bfc6bSMartin Hundebøll 
32610bfc6bSMartin Hundebøll /**
337e9a8c2cSSven Eckelmann  * batadv_frag_clear_chain() - delete entries in the fragment buffer chain
34610bfc6bSMartin Hundebøll  * @head: head of chain with entries.
35bd687fe4SSven Eckelmann  * @dropped: whether the chain is cleared because all fragments are dropped
36610bfc6bSMartin Hundebøll  *
37610bfc6bSMartin Hundebøll  * Free fragments in the passed hlist. Should be called with appropriate lock.
38610bfc6bSMartin Hundebøll  */
39bd687fe4SSven Eckelmann static void batadv_frag_clear_chain(struct hlist_head *head, bool dropped)
40610bfc6bSMartin Hundebøll {
41610bfc6bSMartin Hundebøll 	struct batadv_frag_list_entry *entry;
42610bfc6bSMartin Hundebøll 	struct hlist_node *node;
43610bfc6bSMartin Hundebøll 
44610bfc6bSMartin Hundebøll 	hlist_for_each_entry_safe(entry, node, head, list) {
45610bfc6bSMartin Hundebøll 		hlist_del(&entry->list);
46bd687fe4SSven Eckelmann 
47bd687fe4SSven Eckelmann 		if (dropped)
48610bfc6bSMartin Hundebøll 			kfree_skb(entry->skb);
49bd687fe4SSven Eckelmann 		else
50bd687fe4SSven Eckelmann 			consume_skb(entry->skb);
51bd687fe4SSven Eckelmann 
52610bfc6bSMartin Hundebøll 		kfree(entry);
53610bfc6bSMartin Hundebøll 	}
54610bfc6bSMartin Hundebøll }
55610bfc6bSMartin Hundebøll 
56610bfc6bSMartin Hundebøll /**
577e9a8c2cSSven Eckelmann  * batadv_frag_purge_orig() - free fragments associated to an orig
58610bfc6bSMartin Hundebøll  * @orig_node: originator to free fragments from
59610bfc6bSMartin Hundebøll  * @check_cb: optional function to tell if an entry should be purged
60610bfc6bSMartin Hundebøll  */
61610bfc6bSMartin Hundebøll void batadv_frag_purge_orig(struct batadv_orig_node *orig_node,
62610bfc6bSMartin Hundebøll 			    bool (*check_cb)(struct batadv_frag_table_entry *))
63610bfc6bSMartin Hundebøll {
64610bfc6bSMartin Hundebøll 	struct batadv_frag_table_entry *chain;
656b5e971aSSven Eckelmann 	u8 i;
66610bfc6bSMartin Hundebøll 
67610bfc6bSMartin Hundebøll 	for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
68610bfc6bSMartin Hundebøll 		chain = &orig_node->fragments[i];
6901f6b5c7SSven Eckelmann 		spin_lock_bh(&chain->lock);
70610bfc6bSMartin Hundebøll 
71610bfc6bSMartin Hundebøll 		if (!check_cb || check_cb(chain)) {
72bd687fe4SSven Eckelmann 			batadv_frag_clear_chain(&chain->fragment_list, true);
7301f6b5c7SSven Eckelmann 			chain->size = 0;
74610bfc6bSMartin Hundebøll 		}
75610bfc6bSMartin Hundebøll 
7601f6b5c7SSven Eckelmann 		spin_unlock_bh(&chain->lock);
77610bfc6bSMartin Hundebøll 	}
78610bfc6bSMartin Hundebøll }
79610bfc6bSMartin Hundebøll 
80610bfc6bSMartin Hundebøll /**
817e9a8c2cSSven Eckelmann  * batadv_frag_size_limit() - maximum possible size of packet to be fragmented
82610bfc6bSMartin Hundebøll  *
8362fe710fSSven Eckelmann  * Return: the maximum size of payload that can be fragmented.
84610bfc6bSMartin Hundebøll  */
85610bfc6bSMartin Hundebøll static int batadv_frag_size_limit(void)
86610bfc6bSMartin Hundebøll {
87610bfc6bSMartin Hundebøll 	int limit = BATADV_FRAG_MAX_FRAG_SIZE;
88610bfc6bSMartin Hundebøll 
89610bfc6bSMartin Hundebøll 	limit -= sizeof(struct batadv_frag_packet);
90610bfc6bSMartin Hundebøll 	limit *= BATADV_FRAG_MAX_FRAGMENTS;
91610bfc6bSMartin Hundebøll 
92610bfc6bSMartin Hundebøll 	return limit;
93610bfc6bSMartin Hundebøll }
94610bfc6bSMartin Hundebøll 
95610bfc6bSMartin Hundebøll /**
967e9a8c2cSSven Eckelmann  * batadv_frag_init_chain() - check and prepare fragment chain for new fragment
97610bfc6bSMartin Hundebøll  * @chain: chain in fragments table to init
98610bfc6bSMartin Hundebøll  * @seqno: sequence number of the received fragment
99610bfc6bSMartin Hundebøll  *
100610bfc6bSMartin Hundebøll  * Make chain ready for a fragment with sequence number "seqno". Delete existing
101610bfc6bSMartin Hundebøll  * entries if they have an "old" sequence number.
102610bfc6bSMartin Hundebøll  *
103610bfc6bSMartin Hundebøll  * Caller must hold chain->lock.
104610bfc6bSMartin Hundebøll  *
105bccb48c8SSven Eckelmann  * Return: true if chain is empty and the caller can just insert the new
106bccb48c8SSven Eckelmann  * fragment without searching for the right position.
107610bfc6bSMartin Hundebøll  */
108610bfc6bSMartin Hundebøll static bool batadv_frag_init_chain(struct batadv_frag_table_entry *chain,
1096b5e971aSSven Eckelmann 				   u16 seqno)
110610bfc6bSMartin Hundebøll {
1115274cd68SSven Eckelmann 	lockdep_assert_held(&chain->lock);
1125274cd68SSven Eckelmann 
113610bfc6bSMartin Hundebøll 	if (chain->seqno == seqno)
114610bfc6bSMartin Hundebøll 		return false;
115610bfc6bSMartin Hundebøll 
116176e5b77SSven Eckelmann 	if (!hlist_empty(&chain->fragment_list))
117bd687fe4SSven Eckelmann 		batadv_frag_clear_chain(&chain->fragment_list, true);
118610bfc6bSMartin Hundebøll 
119610bfc6bSMartin Hundebøll 	chain->size = 0;
120610bfc6bSMartin Hundebøll 	chain->seqno = seqno;
121610bfc6bSMartin Hundebøll 
122610bfc6bSMartin Hundebøll 	return true;
123610bfc6bSMartin Hundebøll }
124610bfc6bSMartin Hundebøll 
125610bfc6bSMartin Hundebøll /**
1267e9a8c2cSSven Eckelmann  * batadv_frag_insert_packet() - insert a fragment into a fragment chain
127610bfc6bSMartin Hundebøll  * @orig_node: originator that the fragment was received from
128610bfc6bSMartin Hundebøll  * @skb: skb to insert
129610bfc6bSMartin Hundebøll  * @chain_out: list head to attach complete chains of fragments to
130610bfc6bSMartin Hundebøll  *
131610bfc6bSMartin Hundebøll  * Insert a new fragment into the reverse ordered chain in the right table
132610bfc6bSMartin Hundebøll  * entry. The hash table entry is cleared if "old" fragments exist in it.
133610bfc6bSMartin Hundebøll  *
13462fe710fSSven Eckelmann  * Return: true if skb is buffered, false on error. If the chain has all the
135610bfc6bSMartin Hundebøll  * fragments needed to merge the packet, the chain is moved to the passed head
136610bfc6bSMartin Hundebøll  * to avoid locking the chain in the table.
137610bfc6bSMartin Hundebøll  */
138610bfc6bSMartin Hundebøll static bool batadv_frag_insert_packet(struct batadv_orig_node *orig_node,
139610bfc6bSMartin Hundebøll 				      struct sk_buff *skb,
140610bfc6bSMartin Hundebøll 				      struct hlist_head *chain_out)
141610bfc6bSMartin Hundebøll {
142610bfc6bSMartin Hundebøll 	struct batadv_frag_table_entry *chain;
143610bfc6bSMartin Hundebøll 	struct batadv_frag_list_entry *frag_entry_new = NULL, *frag_entry_curr;
144d9124268SSven Eckelmann 	struct batadv_frag_list_entry *frag_entry_last = NULL;
145610bfc6bSMartin Hundebøll 	struct batadv_frag_packet *frag_packet;
1466b5e971aSSven Eckelmann 	u8 bucket;
1476b5e971aSSven Eckelmann 	u16 seqno, hdr_size = sizeof(struct batadv_frag_packet);
148610bfc6bSMartin Hundebøll 	bool ret = false;
149610bfc6bSMartin Hundebøll 
150610bfc6bSMartin Hundebøll 	/* Linearize packet to avoid linearizing 16 packets in a row when doing
151610bfc6bSMartin Hundebøll 	 * the later merge. Non-linear merge should be added to remove this
152610bfc6bSMartin Hundebøll 	 * linearization.
153610bfc6bSMartin Hundebøll 	 */
154610bfc6bSMartin Hundebøll 	if (skb_linearize(skb) < 0)
155610bfc6bSMartin Hundebøll 		goto err;
156610bfc6bSMartin Hundebøll 
157610bfc6bSMartin Hundebøll 	frag_packet = (struct batadv_frag_packet *)skb->data;
158610bfc6bSMartin Hundebøll 	seqno = ntohs(frag_packet->seqno);
159610bfc6bSMartin Hundebøll 	bucket = seqno % BATADV_FRAG_BUFFER_COUNT;
160610bfc6bSMartin Hundebøll 
161610bfc6bSMartin Hundebøll 	frag_entry_new = kmalloc(sizeof(*frag_entry_new), GFP_ATOMIC);
162610bfc6bSMartin Hundebøll 	if (!frag_entry_new)
163610bfc6bSMartin Hundebøll 		goto err;
164610bfc6bSMartin Hundebøll 
165610bfc6bSMartin Hundebøll 	frag_entry_new->skb = skb;
166610bfc6bSMartin Hundebøll 	frag_entry_new->no = frag_packet->no;
167610bfc6bSMartin Hundebøll 
168610bfc6bSMartin Hundebøll 	/* Select entry in the "chain table" and delete any prior fragments
169610bfc6bSMartin Hundebøll 	 * with another sequence number. batadv_frag_init_chain() returns true,
170610bfc6bSMartin Hundebøll 	 * if the list is empty at return.
171610bfc6bSMartin Hundebøll 	 */
172610bfc6bSMartin Hundebøll 	chain = &orig_node->fragments[bucket];
173610bfc6bSMartin Hundebøll 	spin_lock_bh(&chain->lock);
174610bfc6bSMartin Hundebøll 	if (batadv_frag_init_chain(chain, seqno)) {
175176e5b77SSven Eckelmann 		hlist_add_head(&frag_entry_new->list, &chain->fragment_list);
176610bfc6bSMartin Hundebøll 		chain->size = skb->len - hdr_size;
177610bfc6bSMartin Hundebøll 		chain->timestamp = jiffies;
17853e77145SSven Eckelmann 		chain->total_size = ntohs(frag_packet->total_size);
179610bfc6bSMartin Hundebøll 		ret = true;
180610bfc6bSMartin Hundebøll 		goto out;
181610bfc6bSMartin Hundebøll 	}
182610bfc6bSMartin Hundebøll 
183610bfc6bSMartin Hundebøll 	/* Find the position for the new fragment. */
184176e5b77SSven Eckelmann 	hlist_for_each_entry(frag_entry_curr, &chain->fragment_list, list) {
185610bfc6bSMartin Hundebøll 		/* Drop packet if fragment already exists. */
186610bfc6bSMartin Hundebøll 		if (frag_entry_curr->no == frag_entry_new->no)
187610bfc6bSMartin Hundebøll 			goto err_unlock;
188610bfc6bSMartin Hundebøll 
189610bfc6bSMartin Hundebøll 		/* Order fragments from highest to lowest. */
190610bfc6bSMartin Hundebøll 		if (frag_entry_curr->no < frag_entry_new->no) {
191610bfc6bSMartin Hundebøll 			hlist_add_before(&frag_entry_new->list,
192610bfc6bSMartin Hundebøll 					 &frag_entry_curr->list);
193610bfc6bSMartin Hundebøll 			chain->size += skb->len - hdr_size;
194610bfc6bSMartin Hundebøll 			chain->timestamp = jiffies;
195610bfc6bSMartin Hundebøll 			ret = true;
196610bfc6bSMartin Hundebøll 			goto out;
197610bfc6bSMartin Hundebøll 		}
198d9124268SSven Eckelmann 
199d9124268SSven Eckelmann 		/* store current entry because it could be the last in list */
200d9124268SSven Eckelmann 		frag_entry_last = frag_entry_curr;
201610bfc6bSMartin Hundebøll 	}
202610bfc6bSMartin Hundebøll 
203d9124268SSven Eckelmann 	/* Reached the end of the list, so insert after 'frag_entry_last'. */
204d9124268SSven Eckelmann 	if (likely(frag_entry_last)) {
205e050dbebSSven Eckelmann 		hlist_add_behind(&frag_entry_new->list, &frag_entry_last->list);
206610bfc6bSMartin Hundebøll 		chain->size += skb->len - hdr_size;
207610bfc6bSMartin Hundebøll 		chain->timestamp = jiffies;
208610bfc6bSMartin Hundebøll 		ret = true;
209610bfc6bSMartin Hundebøll 	}
210610bfc6bSMartin Hundebøll 
211610bfc6bSMartin Hundebøll out:
212610bfc6bSMartin Hundebøll 	if (chain->size > batadv_frag_size_limit() ||
21353e77145SSven Eckelmann 	    chain->total_size != ntohs(frag_packet->total_size) ||
21453e77145SSven Eckelmann 	    chain->total_size > batadv_frag_size_limit()) {
215610bfc6bSMartin Hundebøll 		/* Clear chain if total size of either the list or the packet
21653e77145SSven Eckelmann 		 * exceeds the maximum size of one merged packet. Don't allow
21753e77145SSven Eckelmann 		 * packets to have different total_size.
218610bfc6bSMartin Hundebøll 		 */
219bd687fe4SSven Eckelmann 		batadv_frag_clear_chain(&chain->fragment_list, true);
220610bfc6bSMartin Hundebøll 		chain->size = 0;
221610bfc6bSMartin Hundebøll 	} else if (ntohs(frag_packet->total_size) == chain->size) {
222610bfc6bSMartin Hundebøll 		/* All fragments received. Hand over chain to caller. */
223176e5b77SSven Eckelmann 		hlist_move_list(&chain->fragment_list, chain_out);
224610bfc6bSMartin Hundebøll 		chain->size = 0;
225610bfc6bSMartin Hundebøll 	}
226610bfc6bSMartin Hundebøll 
227610bfc6bSMartin Hundebøll err_unlock:
228610bfc6bSMartin Hundebøll 	spin_unlock_bh(&chain->lock);
229610bfc6bSMartin Hundebøll 
230610bfc6bSMartin Hundebøll err:
231248e23b5SSven Eckelmann 	if (!ret) {
232610bfc6bSMartin Hundebøll 		kfree(frag_entry_new);
233248e23b5SSven Eckelmann 		kfree_skb(skb);
234248e23b5SSven Eckelmann 	}
235610bfc6bSMartin Hundebøll 
236610bfc6bSMartin Hundebøll 	return ret;
237610bfc6bSMartin Hundebøll }
238610bfc6bSMartin Hundebøll 
239610bfc6bSMartin Hundebøll /**
2407e9a8c2cSSven Eckelmann  * batadv_frag_merge_packets() - merge a chain of fragments
241610bfc6bSMartin Hundebøll  * @chain: head of chain with fragments
242610bfc6bSMartin Hundebøll  *
243610bfc6bSMartin Hundebøll  * Expand the first skb in the chain and copy the content of the remaining
244610bfc6bSMartin Hundebøll  * skb's into the expanded one. After doing so, clear the chain.
245610bfc6bSMartin Hundebøll  *
24662fe710fSSven Eckelmann  * Return: the merged skb or NULL on error.
247610bfc6bSMartin Hundebøll  */
248610bfc6bSMartin Hundebøll static struct sk_buff *
24983e8b877SSven Eckelmann batadv_frag_merge_packets(struct hlist_head *chain)
250610bfc6bSMartin Hundebøll {
251610bfc6bSMartin Hundebøll 	struct batadv_frag_packet *packet;
252610bfc6bSMartin Hundebøll 	struct batadv_frag_list_entry *entry;
253422d2f77SSven Eckelmann 	struct sk_buff *skb_out;
254610bfc6bSMartin Hundebøll 	int size, hdr_size = sizeof(struct batadv_frag_packet);
255bd687fe4SSven Eckelmann 	bool dropped = false;
256610bfc6bSMartin Hundebøll 
257610bfc6bSMartin Hundebøll 	/* Remove first entry, as this is the destination for the rest of the
258610bfc6bSMartin Hundebøll 	 * fragments.
259610bfc6bSMartin Hundebøll 	 */
260610bfc6bSMartin Hundebøll 	entry = hlist_entry(chain->first, struct batadv_frag_list_entry, list);
261610bfc6bSMartin Hundebøll 	hlist_del(&entry->list);
262610bfc6bSMartin Hundebøll 	skb_out = entry->skb;
263610bfc6bSMartin Hundebøll 	kfree(entry);
264610bfc6bSMartin Hundebøll 
26583e8b877SSven Eckelmann 	packet = (struct batadv_frag_packet *)skb_out->data;
266d7d8bbb4SSven Eckelmann 	size = ntohs(packet->total_size) + hdr_size;
26783e8b877SSven Eckelmann 
268610bfc6bSMartin Hundebøll 	/* Make room for the rest of the fragments. */
2695b6698b0SSven Eckelmann 	if (pskb_expand_head(skb_out, 0, size - skb_out->len, GFP_ATOMIC) < 0) {
270610bfc6bSMartin Hundebøll 		kfree_skb(skb_out);
271610bfc6bSMartin Hundebøll 		skb_out = NULL;
272bd687fe4SSven Eckelmann 		dropped = true;
273610bfc6bSMartin Hundebøll 		goto free;
274610bfc6bSMartin Hundebøll 	}
275610bfc6bSMartin Hundebøll 
276610bfc6bSMartin Hundebøll 	/* Move the existing MAC header to just before the payload. (Override
277610bfc6bSMartin Hundebøll 	 * the fragment header.)
278610bfc6bSMartin Hundebøll 	 */
2793bf2a09dSMatthias Schiffer 	skb_pull(skb_out, hdr_size);
2803bf2a09dSMatthias Schiffer 	skb_out->ip_summed = CHECKSUM_NONE;
281610bfc6bSMartin Hundebøll 	memmove(skb_out->data - ETH_HLEN, skb_mac_header(skb_out), ETH_HLEN);
282610bfc6bSMartin Hundebøll 	skb_set_mac_header(skb_out, -ETH_HLEN);
283610bfc6bSMartin Hundebøll 	skb_reset_network_header(skb_out);
284610bfc6bSMartin Hundebøll 	skb_reset_transport_header(skb_out);
285610bfc6bSMartin Hundebøll 
286610bfc6bSMartin Hundebøll 	/* Copy the payload of the each fragment into the last skb */
287610bfc6bSMartin Hundebøll 	hlist_for_each_entry(entry, chain, list) {
288610bfc6bSMartin Hundebøll 		size = entry->skb->len - hdr_size;
28959ae1d12SJohannes Berg 		skb_put_data(skb_out, entry->skb->data + hdr_size, size);
290610bfc6bSMartin Hundebøll 	}
291610bfc6bSMartin Hundebøll 
292610bfc6bSMartin Hundebøll free:
293610bfc6bSMartin Hundebøll 	/* Locking is not needed, because 'chain' is not part of any orig. */
294bd687fe4SSven Eckelmann 	batadv_frag_clear_chain(chain, dropped);
295610bfc6bSMartin Hundebøll 	return skb_out;
296610bfc6bSMartin Hundebøll }
297610bfc6bSMartin Hundebøll 
298610bfc6bSMartin Hundebøll /**
2997e9a8c2cSSven Eckelmann  * batadv_frag_skb_buffer() - buffer fragment for later merge
300610bfc6bSMartin Hundebøll  * @skb: skb to buffer
301610bfc6bSMartin Hundebøll  * @orig_node_src: originator that the skb is received from
302610bfc6bSMartin Hundebøll  *
303610bfc6bSMartin Hundebøll  * Add fragment to buffer and merge fragments if possible.
304610bfc6bSMartin Hundebøll  *
305610bfc6bSMartin Hundebøll  * There are three possible outcomes: 1) Packet is merged: Return true and
306610bfc6bSMartin Hundebøll  * set *skb to merged packet; 2) Packet is buffered: Return true and set *skb
307248e23b5SSven Eckelmann  * to NULL; 3) Error: Return false and free skb.
30862fe710fSSven Eckelmann  *
30921ba5ab2SSven Eckelmann  * Return: true when the packet is merged or buffered, false when skb is not
31062fe710fSSven Eckelmann  * used.
311610bfc6bSMartin Hundebøll  */
312610bfc6bSMartin Hundebøll bool batadv_frag_skb_buffer(struct sk_buff **skb,
313610bfc6bSMartin Hundebøll 			    struct batadv_orig_node *orig_node_src)
314610bfc6bSMartin Hundebøll {
315610bfc6bSMartin Hundebøll 	struct sk_buff *skb_out = NULL;
316610bfc6bSMartin Hundebøll 	struct hlist_head head = HLIST_HEAD_INIT;
317610bfc6bSMartin Hundebøll 	bool ret = false;
318610bfc6bSMartin Hundebøll 
319610bfc6bSMartin Hundebøll 	/* Add packet to buffer and table entry if merge is possible. */
320610bfc6bSMartin Hundebøll 	if (!batadv_frag_insert_packet(orig_node_src, *skb, &head))
321610bfc6bSMartin Hundebøll 		goto out_err;
322610bfc6bSMartin Hundebøll 
323610bfc6bSMartin Hundebøll 	/* Leave if more fragments are needed to merge. */
324610bfc6bSMartin Hundebøll 	if (hlist_empty(&head))
325610bfc6bSMartin Hundebøll 		goto out;
326610bfc6bSMartin Hundebøll 
32783e8b877SSven Eckelmann 	skb_out = batadv_frag_merge_packets(&head);
328610bfc6bSMartin Hundebøll 	if (!skb_out)
329610bfc6bSMartin Hundebøll 		goto out_err;
330610bfc6bSMartin Hundebøll 
331610bfc6bSMartin Hundebøll out:
332610bfc6bSMartin Hundebøll 	ret = true;
333610bfc6bSMartin Hundebøll out_err:
334248e23b5SSven Eckelmann 	*skb = skb_out;
335610bfc6bSMartin Hundebøll 	return ret;
336610bfc6bSMartin Hundebøll }
337610bfc6bSMartin Hundebøll 
338610bfc6bSMartin Hundebøll /**
3397e9a8c2cSSven Eckelmann  * batadv_frag_skb_fwd() - forward fragments that would exceed MTU when merged
340610bfc6bSMartin Hundebøll  * @skb: skb to forward
341610bfc6bSMartin Hundebøll  * @recv_if: interface that the skb is received on
342610bfc6bSMartin Hundebøll  * @orig_node_src: originator that the skb is received from
343610bfc6bSMartin Hundebøll  *
344610bfc6bSMartin Hundebøll  * Look up the next-hop of the fragments payload and check if the merged packet
345610bfc6bSMartin Hundebøll  * will exceed the MTU towards the next-hop. If so, the fragment is forwarded
346610bfc6bSMartin Hundebøll  * without merging it.
347610bfc6bSMartin Hundebøll  *
34862fe710fSSven Eckelmann  * Return: true if the fragment is consumed/forwarded, false otherwise.
349610bfc6bSMartin Hundebøll  */
350610bfc6bSMartin Hundebøll bool batadv_frag_skb_fwd(struct sk_buff *skb,
351610bfc6bSMartin Hundebøll 			 struct batadv_hard_iface *recv_if,
352610bfc6bSMartin Hundebøll 			 struct batadv_orig_node *orig_node_src)
353610bfc6bSMartin Hundebøll {
354610bfc6bSMartin Hundebøll 	struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
355422d2f77SSven Eckelmann 	struct batadv_orig_node *orig_node_dst;
356610bfc6bSMartin Hundebøll 	struct batadv_neigh_node *neigh_node = NULL;
357610bfc6bSMartin Hundebøll 	struct batadv_frag_packet *packet;
3586b5e971aSSven Eckelmann 	u16 total_size;
359610bfc6bSMartin Hundebøll 	bool ret = false;
360610bfc6bSMartin Hundebøll 
361610bfc6bSMartin Hundebøll 	packet = (struct batadv_frag_packet *)skb->data;
362610bfc6bSMartin Hundebøll 	orig_node_dst = batadv_orig_hash_find(bat_priv, packet->dest);
363610bfc6bSMartin Hundebøll 	if (!orig_node_dst)
364610bfc6bSMartin Hundebøll 		goto out;
365610bfc6bSMartin Hundebøll 
366610bfc6bSMartin Hundebøll 	neigh_node = batadv_find_router(bat_priv, orig_node_dst, recv_if);
367610bfc6bSMartin Hundebøll 	if (!neigh_node)
368610bfc6bSMartin Hundebøll 		goto out;
369610bfc6bSMartin Hundebøll 
370610bfc6bSMartin Hundebøll 	/* Forward the fragment, if the merged packet would be too big to
371610bfc6bSMartin Hundebøll 	 * be assembled.
372610bfc6bSMartin Hundebøll 	 */
373610bfc6bSMartin Hundebøll 	total_size = ntohs(packet->total_size);
374610bfc6bSMartin Hundebøll 	if (total_size > neigh_node->if_incoming->net_dev->mtu) {
375610bfc6bSMartin Hundebøll 		batadv_inc_counter(bat_priv, BATADV_CNT_FRAG_FWD);
376610bfc6bSMartin Hundebøll 		batadv_add_counter(bat_priv, BATADV_CNT_FRAG_FWD_BYTES,
377610bfc6bSMartin Hundebøll 				   skb->len + ETH_HLEN);
378610bfc6bSMartin Hundebøll 
379a40d9b07SSimon Wunderlich 		packet->ttl--;
38095d39278SAntonio Quartulli 		batadv_send_unicast_skb(skb, neigh_node);
381610bfc6bSMartin Hundebøll 		ret = true;
382610bfc6bSMartin Hundebøll 	}
383610bfc6bSMartin Hundebøll 
384610bfc6bSMartin Hundebøll out:
385610bfc6bSMartin Hundebøll 	if (orig_node_dst)
3865d967310SSven Eckelmann 		batadv_orig_node_put(orig_node_dst);
387610bfc6bSMartin Hundebøll 	if (neigh_node)
38825bb2509SSven Eckelmann 		batadv_neigh_node_put(neigh_node);
389610bfc6bSMartin Hundebøll 	return ret;
390610bfc6bSMartin Hundebøll }
391ee75ed88SMartin Hundebøll 
392ee75ed88SMartin Hundebøll /**
3937e9a8c2cSSven Eckelmann  * batadv_frag_create() - create a fragment from skb
394c5cbfc87SSven Eckelmann  * @net_dev: outgoing device for fragment
395ee75ed88SMartin Hundebøll  * @skb: skb to create fragment from
396ee75ed88SMartin Hundebøll  * @frag_head: header to use in new fragment
3971c2bcc76SSven Eckelmann  * @fragment_size: size of new fragment
398ee75ed88SMartin Hundebøll  *
399ee75ed88SMartin Hundebøll  * Split the passed skb into two fragments: A new one with size matching the
400ee75ed88SMartin Hundebøll  * passed mtu and the old one with the rest. The new skb contains data from the
401ee75ed88SMartin Hundebøll  * tail of the old skb.
402ee75ed88SMartin Hundebøll  *
40362fe710fSSven Eckelmann  * Return: the new fragment, NULL on error.
404ee75ed88SMartin Hundebøll  */
405c5cbfc87SSven Eckelmann static struct sk_buff *batadv_frag_create(struct net_device *net_dev,
406c5cbfc87SSven Eckelmann 					  struct sk_buff *skb,
407ee75ed88SMartin Hundebøll 					  struct batadv_frag_packet *frag_head,
4081c2bcc76SSven Eckelmann 					  unsigned int fragment_size)
409ee75ed88SMartin Hundebøll {
410c5cbfc87SSven Eckelmann 	unsigned int ll_reserved = LL_RESERVED_SPACE(net_dev);
411c5cbfc87SSven Eckelmann 	unsigned int tailroom = net_dev->needed_tailroom;
412ee75ed88SMartin Hundebøll 	struct sk_buff *skb_fragment;
413d3abce78SSven Eckelmann 	unsigned int header_size = sizeof(*frag_head);
4141c2bcc76SSven Eckelmann 	unsigned int mtu = fragment_size + header_size;
415ee75ed88SMartin Hundebøll 
416c5cbfc87SSven Eckelmann 	skb_fragment = dev_alloc_skb(ll_reserved + mtu + tailroom);
417ee75ed88SMartin Hundebøll 	if (!skb_fragment)
418ee75ed88SMartin Hundebøll 		goto err;
419ee75ed88SMartin Hundebøll 
4201914848eSAndrew Lunn 	skb_fragment->priority = skb->priority;
421ee75ed88SMartin Hundebøll 
422ee75ed88SMartin Hundebøll 	/* Eat the last mtu-bytes of the skb */
423c5cbfc87SSven Eckelmann 	skb_reserve(skb_fragment, ll_reserved + header_size);
424ee75ed88SMartin Hundebøll 	skb_split(skb, skb_fragment, skb->len - fragment_size);
425ee75ed88SMartin Hundebøll 
426ee75ed88SMartin Hundebøll 	/* Add the header */
427ee75ed88SMartin Hundebøll 	skb_push(skb_fragment, header_size);
428ee75ed88SMartin Hundebøll 	memcpy(skb_fragment->data, frag_head, header_size);
429ee75ed88SMartin Hundebøll 
430ee75ed88SMartin Hundebøll err:
431ee75ed88SMartin Hundebøll 	return skb_fragment;
432ee75ed88SMartin Hundebøll }
433ee75ed88SMartin Hundebøll 
434ee75ed88SMartin Hundebøll /**
4357e9a8c2cSSven Eckelmann  * batadv_frag_send_packet() - create up to 16 fragments from the passed skb
436ee75ed88SMartin Hundebøll  * @skb: skb to create fragments from
437ee75ed88SMartin Hundebøll  * @orig_node: final destination of the created fragments
438ee75ed88SMartin Hundebøll  * @neigh_node: next-hop of the created fragments
439ee75ed88SMartin Hundebøll  *
4408def0be8SSven Eckelmann  * Return: the netdev tx status or a negative errno code on a failure
441ee75ed88SMartin Hundebøll  */
442f50ca95aSAntonio Quartulli int batadv_frag_send_packet(struct sk_buff *skb,
443ee75ed88SMartin Hundebøll 			    struct batadv_orig_node *orig_node,
444ee75ed88SMartin Hundebøll 			    struct batadv_neigh_node *neigh_node)
445ee75ed88SMartin Hundebøll {
446c5cbfc87SSven Eckelmann 	struct net_device *net_dev = neigh_node->if_incoming->net_dev;
447ee75ed88SMartin Hundebøll 	struct batadv_priv *bat_priv;
448be181015SAntonio Quartulli 	struct batadv_hard_iface *primary_if = NULL;
449ee75ed88SMartin Hundebøll 	struct batadv_frag_packet frag_header;
450ee75ed88SMartin Hundebøll 	struct sk_buff *skb_fragment;
451c5cbfc87SSven Eckelmann 	unsigned int mtu = net_dev->mtu;
452d3abce78SSven Eckelmann 	unsigned int header_size = sizeof(frag_header);
4531c2bcc76SSven Eckelmann 	unsigned int max_fragment_size, num_fragments;
4548def0be8SSven Eckelmann 	int ret;
455ee75ed88SMartin Hundebøll 
456ee75ed88SMartin Hundebøll 	/* To avoid merge and refragmentation at next-hops we never send
457ee75ed88SMartin Hundebøll 	 * fragments larger than BATADV_FRAG_MAX_FRAG_SIZE
458ee75ed88SMartin Hundebøll 	 */
459d3abce78SSven Eckelmann 	mtu = min_t(unsigned int, mtu, BATADV_FRAG_MAX_FRAG_SIZE);
4600402e444SSven Eckelmann 	max_fragment_size = mtu - header_size;
4611c2bcc76SSven Eckelmann 
4621c2bcc76SSven Eckelmann 	if (skb->len == 0 || max_fragment_size == 0)
4631c2bcc76SSven Eckelmann 		return -EINVAL;
4641c2bcc76SSven Eckelmann 
4651c2bcc76SSven Eckelmann 	num_fragments = (skb->len - 1) / max_fragment_size + 1;
4661c2bcc76SSven Eckelmann 	max_fragment_size = (skb->len - 1) / num_fragments + 1;
467ee75ed88SMartin Hundebøll 
468ee75ed88SMartin Hundebøll 	/* Don't even try to fragment, if we need more than 16 fragments */
4691c2bcc76SSven Eckelmann 	if (num_fragments > BATADV_FRAG_MAX_FRAGMENTS) {
4708def0be8SSven Eckelmann 		ret = -EAGAIN;
4718def0be8SSven Eckelmann 		goto free_skb;
4728def0be8SSven Eckelmann 	}
473ee75ed88SMartin Hundebøll 
474ee75ed88SMartin Hundebøll 	bat_priv = orig_node->bat_priv;
475ee75ed88SMartin Hundebøll 	primary_if = batadv_primary_if_get_selected(bat_priv);
4768def0be8SSven Eckelmann 	if (!primary_if) {
4778def0be8SSven Eckelmann 		ret = -EINVAL;
4784ea33ef0SSven Eckelmann 		goto free_skb;
4798def0be8SSven Eckelmann 	}
480ee75ed88SMartin Hundebøll 
481ee75ed88SMartin Hundebøll 	/* Create one header to be copied to all fragments */
482a40d9b07SSimon Wunderlich 	frag_header.packet_type = BATADV_UNICAST_FRAG;
483a40d9b07SSimon Wunderlich 	frag_header.version = BATADV_COMPAT_VERSION;
484a40d9b07SSimon Wunderlich 	frag_header.ttl = BATADV_TTL;
485ee75ed88SMartin Hundebøll 	frag_header.seqno = htons(atomic_inc_return(&bat_priv->frag_seqno));
486ee75ed88SMartin Hundebøll 	frag_header.reserved = 0;
487ee75ed88SMartin Hundebøll 	frag_header.no = 0;
488ee75ed88SMartin Hundebøll 	frag_header.total_size = htons(skb->len);
489c0f25c80SAndrew Lunn 
490c0f25c80SAndrew Lunn 	/* skb->priority values from 256->263 are magic values to
491c0f25c80SAndrew Lunn 	 * directly indicate a specific 802.1d priority.  This is used
492c0f25c80SAndrew Lunn 	 * to allow 802.1d priority to be passed directly in from VLAN
493c0f25c80SAndrew Lunn 	 * tags, etc.
494c0f25c80SAndrew Lunn 	 */
495c0f25c80SAndrew Lunn 	if (skb->priority >= 256 && skb->priority <= 263)
496c0f25c80SAndrew Lunn 		frag_header.priority = skb->priority - 256;
497fe77d825SSven Eckelmann 	else
498fe77d825SSven Eckelmann 		frag_header.priority = 0;
499c0f25c80SAndrew Lunn 
5008fdd0153SAntonio Quartulli 	ether_addr_copy(frag_header.orig, primary_if->net_dev->dev_addr);
5018fdd0153SAntonio Quartulli 	ether_addr_copy(frag_header.dest, orig_node->orig);
502ee75ed88SMartin Hundebøll 
503ee75ed88SMartin Hundebøll 	/* Eat and send fragments from the tail of skb */
504ee75ed88SMartin Hundebøll 	while (skb->len > max_fragment_size) {
50551c6b429SLinus Lüssing 		/* The initial check in this function should cover this case */
50651c6b429SLinus Lüssing 		if (unlikely(frag_header.no == BATADV_FRAG_MAX_FRAGMENTS - 1)) {
50751c6b429SLinus Lüssing 			ret = -EINVAL;
50851c6b429SLinus Lüssing 			goto put_primary_if;
50951c6b429SLinus Lüssing 		}
51051c6b429SLinus Lüssing 
511c5cbfc87SSven Eckelmann 		skb_fragment = batadv_frag_create(net_dev, skb, &frag_header,
5121c2bcc76SSven Eckelmann 						  max_fragment_size);
5138def0be8SSven Eckelmann 		if (!skb_fragment) {
5148def0be8SSven Eckelmann 			ret = -ENOMEM;
5154ea33ef0SSven Eckelmann 			goto put_primary_if;
5168def0be8SSven Eckelmann 		}
517ee75ed88SMartin Hundebøll 
518ee75ed88SMartin Hundebøll 		batadv_inc_counter(bat_priv, BATADV_CNT_FRAG_TX);
519ee75ed88SMartin Hundebøll 		batadv_add_counter(bat_priv, BATADV_CNT_FRAG_TX_BYTES,
520ee75ed88SMartin Hundebøll 				   skb_fragment->len + ETH_HLEN);
521f50ca95aSAntonio Quartulli 		ret = batadv_send_unicast_skb(skb_fragment, neigh_node);
522f50ca95aSAntonio Quartulli 		if (ret != NET_XMIT_SUCCESS) {
5238def0be8SSven Eckelmann 			ret = NET_XMIT_DROP;
5244ea33ef0SSven Eckelmann 			goto put_primary_if;
525f50ca95aSAntonio Quartulli 		}
526f50ca95aSAntonio Quartulli 
527ee75ed88SMartin Hundebøll 		frag_header.no++;
528ee75ed88SMartin Hundebøll 	}
529ee75ed88SMartin Hundebøll 
530992b03b8SSven Eckelmann 	/* make sure that there is at least enough head for the fragmentation
531992b03b8SSven Eckelmann 	 * and ethernet headers
532992b03b8SSven Eckelmann 	 */
533992b03b8SSven Eckelmann 	ret = skb_cow_head(skb, ETH_HLEN + header_size);
534992b03b8SSven Eckelmann 	if (ret < 0)
5354ea33ef0SSven Eckelmann 		goto put_primary_if;
536ee75ed88SMartin Hundebøll 
537992b03b8SSven Eckelmann 	skb_push(skb, header_size);
538ee75ed88SMartin Hundebøll 	memcpy(skb->data, &frag_header, header_size);
539ee75ed88SMartin Hundebøll 
540ee75ed88SMartin Hundebøll 	/* Send the last fragment */
541ee75ed88SMartin Hundebøll 	batadv_inc_counter(bat_priv, BATADV_CNT_FRAG_TX);
542ee75ed88SMartin Hundebøll 	batadv_add_counter(bat_priv, BATADV_CNT_FRAG_TX_BYTES,
543ee75ed88SMartin Hundebøll 			   skb->len + ETH_HLEN);
544f50ca95aSAntonio Quartulli 	ret = batadv_send_unicast_skb(skb, neigh_node);
5458def0be8SSven Eckelmann 	/* skb was consumed */
5468def0be8SSven Eckelmann 	skb = NULL;
547ee75ed88SMartin Hundebøll 
5488def0be8SSven Eckelmann put_primary_if:
54982047ad7SSven Eckelmann 	batadv_hardif_put(primary_if);
5508def0be8SSven Eckelmann free_skb:
5518def0be8SSven Eckelmann 	kfree_skb(skb);
552be181015SAntonio Quartulli 
553be181015SAntonio Quartulli 	return ret;
554ee75ed88SMartin Hundebøll }
555