xref: /openbmc/linux/net/batman-adv/fragmentation.c (revision 83e8b87721f21b26b843633caca8ef453e943623)
19f6446c7SSven Eckelmann /* Copyright (C) 2013-2015 B.A.T.M.A.N. contributors:
2610bfc6bSMartin Hundebøll  *
3610bfc6bSMartin Hundebøll  * Martin Hundebøll <martin@hundeboll.net>
4610bfc6bSMartin Hundebøll  *
5610bfc6bSMartin Hundebøll  * This program is free software; you can redistribute it and/or
6610bfc6bSMartin Hundebøll  * modify it under the terms of version 2 of the GNU General Public
7610bfc6bSMartin Hundebøll  * License as published by the Free Software Foundation.
8610bfc6bSMartin Hundebøll  *
9610bfc6bSMartin Hundebøll  * This program is distributed in the hope that it will be useful, but
10610bfc6bSMartin Hundebøll  * WITHOUT ANY WARRANTY; without even the implied warranty of
11610bfc6bSMartin Hundebøll  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12610bfc6bSMartin Hundebøll  * General Public License for more details.
13610bfc6bSMartin Hundebøll  *
14610bfc6bSMartin Hundebøll  * You should have received a copy of the GNU General Public License
15ebf38fb7SAntonio Quartulli  * along with this program; if not, see <http://www.gnu.org/licenses/>.
16610bfc6bSMartin Hundebøll  */
17610bfc6bSMartin Hundebøll 
18610bfc6bSMartin Hundebøll #include "main.h"
19610bfc6bSMartin Hundebøll #include "fragmentation.h"
20610bfc6bSMartin Hundebøll #include "send.h"
21610bfc6bSMartin Hundebøll #include "originator.h"
22610bfc6bSMartin Hundebøll #include "routing.h"
23610bfc6bSMartin Hundebøll #include "hard-interface.h"
24610bfc6bSMartin Hundebøll #include "soft-interface.h"
25610bfc6bSMartin Hundebøll 
26610bfc6bSMartin Hundebøll /**
27610bfc6bSMartin Hundebøll  * batadv_frag_clear_chain - delete entries in the fragment buffer chain
28610bfc6bSMartin Hundebøll  * @head: head of chain with entries.
29610bfc6bSMartin Hundebøll  *
30610bfc6bSMartin Hundebøll  * Free fragments in the passed hlist. Should be called with appropriate lock.
31610bfc6bSMartin Hundebøll  */
32610bfc6bSMartin Hundebøll static void batadv_frag_clear_chain(struct hlist_head *head)
33610bfc6bSMartin Hundebøll {
34610bfc6bSMartin Hundebøll 	struct batadv_frag_list_entry *entry;
35610bfc6bSMartin Hundebøll 	struct hlist_node *node;
36610bfc6bSMartin Hundebøll 
37610bfc6bSMartin Hundebøll 	hlist_for_each_entry_safe(entry, node, head, list) {
38610bfc6bSMartin Hundebøll 		hlist_del(&entry->list);
39610bfc6bSMartin Hundebøll 		kfree_skb(entry->skb);
40610bfc6bSMartin Hundebøll 		kfree(entry);
41610bfc6bSMartin Hundebøll 	}
42610bfc6bSMartin Hundebøll }
43610bfc6bSMartin Hundebøll 
44610bfc6bSMartin Hundebøll /**
45610bfc6bSMartin Hundebøll  * batadv_frag_purge_orig - free fragments associated to an orig
46610bfc6bSMartin Hundebøll  * @orig_node: originator to free fragments from
47610bfc6bSMartin Hundebøll  * @check_cb: optional function to tell if an entry should be purged
48610bfc6bSMartin Hundebøll  */
49610bfc6bSMartin Hundebøll void batadv_frag_purge_orig(struct batadv_orig_node *orig_node,
50610bfc6bSMartin Hundebøll 			    bool (*check_cb)(struct batadv_frag_table_entry *))
51610bfc6bSMartin Hundebøll {
52610bfc6bSMartin Hundebøll 	struct batadv_frag_table_entry *chain;
53610bfc6bSMartin Hundebøll 	uint8_t i;
54610bfc6bSMartin Hundebøll 
55610bfc6bSMartin Hundebøll 	for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
56610bfc6bSMartin Hundebøll 		chain = &orig_node->fragments[i];
57610bfc6bSMartin Hundebøll 		spin_lock_bh(&orig_node->fragments[i].lock);
58610bfc6bSMartin Hundebøll 
59610bfc6bSMartin Hundebøll 		if (!check_cb || check_cb(chain)) {
60610bfc6bSMartin Hundebøll 			batadv_frag_clear_chain(&orig_node->fragments[i].head);
61610bfc6bSMartin Hundebøll 			orig_node->fragments[i].size = 0;
62610bfc6bSMartin Hundebøll 		}
63610bfc6bSMartin Hundebøll 
64610bfc6bSMartin Hundebøll 		spin_unlock_bh(&orig_node->fragments[i].lock);
65610bfc6bSMartin Hundebøll 	}
66610bfc6bSMartin Hundebøll }
67610bfc6bSMartin Hundebøll 
68610bfc6bSMartin Hundebøll /**
69610bfc6bSMartin Hundebøll  * batadv_frag_size_limit - maximum possible size of packet to be fragmented
70610bfc6bSMartin Hundebøll  *
71610bfc6bSMartin Hundebøll  * Returns the maximum size of payload that can be fragmented.
72610bfc6bSMartin Hundebøll  */
73610bfc6bSMartin Hundebøll static int batadv_frag_size_limit(void)
74610bfc6bSMartin Hundebøll {
75610bfc6bSMartin Hundebøll 	int limit = BATADV_FRAG_MAX_FRAG_SIZE;
76610bfc6bSMartin Hundebøll 
77610bfc6bSMartin Hundebøll 	limit -= sizeof(struct batadv_frag_packet);
78610bfc6bSMartin Hundebøll 	limit *= BATADV_FRAG_MAX_FRAGMENTS;
79610bfc6bSMartin Hundebøll 
80610bfc6bSMartin Hundebøll 	return limit;
81610bfc6bSMartin Hundebøll }
82610bfc6bSMartin Hundebøll 
83610bfc6bSMartin Hundebøll /**
84610bfc6bSMartin Hundebøll  * batadv_frag_init_chain - check and prepare fragment chain for new fragment
85610bfc6bSMartin Hundebøll  * @chain: chain in fragments table to init
86610bfc6bSMartin Hundebøll  * @seqno: sequence number of the received fragment
87610bfc6bSMartin Hundebøll  *
88610bfc6bSMartin Hundebøll  * Make chain ready for a fragment with sequence number "seqno". Delete existing
89610bfc6bSMartin Hundebøll  * entries if they have an "old" sequence number.
90610bfc6bSMartin Hundebøll  *
91610bfc6bSMartin Hundebøll  * Caller must hold chain->lock.
92610bfc6bSMartin Hundebøll  *
93610bfc6bSMartin Hundebøll  * Returns true if chain is empty and caller can just insert the new fragment
94610bfc6bSMartin Hundebøll  * without searching for the right position.
95610bfc6bSMartin Hundebøll  */
96610bfc6bSMartin Hundebøll static bool batadv_frag_init_chain(struct batadv_frag_table_entry *chain,
97610bfc6bSMartin Hundebøll 				   uint16_t seqno)
98610bfc6bSMartin Hundebøll {
99610bfc6bSMartin Hundebøll 	if (chain->seqno == seqno)
100610bfc6bSMartin Hundebøll 		return false;
101610bfc6bSMartin Hundebøll 
102610bfc6bSMartin Hundebøll 	if (!hlist_empty(&chain->head))
103610bfc6bSMartin Hundebøll 		batadv_frag_clear_chain(&chain->head);
104610bfc6bSMartin Hundebøll 
105610bfc6bSMartin Hundebøll 	chain->size = 0;
106610bfc6bSMartin Hundebøll 	chain->seqno = seqno;
107610bfc6bSMartin Hundebøll 
108610bfc6bSMartin Hundebøll 	return true;
109610bfc6bSMartin Hundebøll }
110610bfc6bSMartin Hundebøll 
111610bfc6bSMartin Hundebøll /**
112610bfc6bSMartin Hundebøll  * batadv_frag_insert_packet - insert a fragment into a fragment chain
113610bfc6bSMartin Hundebøll  * @orig_node: originator that the fragment was received from
114610bfc6bSMartin Hundebøll  * @skb: skb to insert
115610bfc6bSMartin Hundebøll  * @chain_out: list head to attach complete chains of fragments to
116610bfc6bSMartin Hundebøll  *
117610bfc6bSMartin Hundebøll  * Insert a new fragment into the reverse ordered chain in the right table
118610bfc6bSMartin Hundebøll  * entry. The hash table entry is cleared if "old" fragments exist in it.
119610bfc6bSMartin Hundebøll  *
120610bfc6bSMartin Hundebøll  * Returns true if skb is buffered, false on error. If the chain has all the
121610bfc6bSMartin Hundebøll  * fragments needed to merge the packet, the chain is moved to the passed head
122610bfc6bSMartin Hundebøll  * to avoid locking the chain in the table.
123610bfc6bSMartin Hundebøll  */
124610bfc6bSMartin Hundebøll static bool batadv_frag_insert_packet(struct batadv_orig_node *orig_node,
125610bfc6bSMartin Hundebøll 				      struct sk_buff *skb,
126610bfc6bSMartin Hundebøll 				      struct hlist_head *chain_out)
127610bfc6bSMartin Hundebøll {
128610bfc6bSMartin Hundebøll 	struct batadv_frag_table_entry *chain;
129610bfc6bSMartin Hundebøll 	struct batadv_frag_list_entry *frag_entry_new = NULL, *frag_entry_curr;
130d9124268SSven Eckelmann 	struct batadv_frag_list_entry *frag_entry_last = NULL;
131610bfc6bSMartin Hundebøll 	struct batadv_frag_packet *frag_packet;
132610bfc6bSMartin Hundebøll 	uint8_t bucket;
133610bfc6bSMartin Hundebøll 	uint16_t seqno, hdr_size = sizeof(struct batadv_frag_packet);
134610bfc6bSMartin Hundebøll 	bool ret = false;
135610bfc6bSMartin Hundebøll 
136610bfc6bSMartin Hundebøll 	/* Linearize packet to avoid linearizing 16 packets in a row when doing
137610bfc6bSMartin Hundebøll 	 * the later merge. Non-linear merge should be added to remove this
138610bfc6bSMartin Hundebøll 	 * linearization.
139610bfc6bSMartin Hundebøll 	 */
140610bfc6bSMartin Hundebøll 	if (skb_linearize(skb) < 0)
141610bfc6bSMartin Hundebøll 		goto err;
142610bfc6bSMartin Hundebøll 
143610bfc6bSMartin Hundebøll 	frag_packet = (struct batadv_frag_packet *)skb->data;
144610bfc6bSMartin Hundebøll 	seqno = ntohs(frag_packet->seqno);
145610bfc6bSMartin Hundebøll 	bucket = seqno % BATADV_FRAG_BUFFER_COUNT;
146610bfc6bSMartin Hundebøll 
147610bfc6bSMartin Hundebøll 	frag_entry_new = kmalloc(sizeof(*frag_entry_new), GFP_ATOMIC);
148610bfc6bSMartin Hundebøll 	if (!frag_entry_new)
149610bfc6bSMartin Hundebøll 		goto err;
150610bfc6bSMartin Hundebøll 
151610bfc6bSMartin Hundebøll 	frag_entry_new->skb = skb;
152610bfc6bSMartin Hundebøll 	frag_entry_new->no = frag_packet->no;
153610bfc6bSMartin Hundebøll 
154610bfc6bSMartin Hundebøll 	/* Select entry in the "chain table" and delete any prior fragments
155610bfc6bSMartin Hundebøll 	 * with another sequence number. batadv_frag_init_chain() returns true,
156610bfc6bSMartin Hundebøll 	 * if the list is empty at return.
157610bfc6bSMartin Hundebøll 	 */
158610bfc6bSMartin Hundebøll 	chain = &orig_node->fragments[bucket];
159610bfc6bSMartin Hundebøll 	spin_lock_bh(&chain->lock);
160610bfc6bSMartin Hundebøll 	if (batadv_frag_init_chain(chain, seqno)) {
161610bfc6bSMartin Hundebøll 		hlist_add_head(&frag_entry_new->list, &chain->head);
162610bfc6bSMartin Hundebøll 		chain->size = skb->len - hdr_size;
163610bfc6bSMartin Hundebøll 		chain->timestamp = jiffies;
16453e77145SSven Eckelmann 		chain->total_size = ntohs(frag_packet->total_size);
165610bfc6bSMartin Hundebøll 		ret = true;
166610bfc6bSMartin Hundebøll 		goto out;
167610bfc6bSMartin Hundebøll 	}
168610bfc6bSMartin Hundebøll 
169610bfc6bSMartin Hundebøll 	/* Find the position for the new fragment. */
170610bfc6bSMartin Hundebøll 	hlist_for_each_entry(frag_entry_curr, &chain->head, list) {
171610bfc6bSMartin Hundebøll 		/* Drop packet if fragment already exists. */
172610bfc6bSMartin Hundebøll 		if (frag_entry_curr->no == frag_entry_new->no)
173610bfc6bSMartin Hundebøll 			goto err_unlock;
174610bfc6bSMartin Hundebøll 
175610bfc6bSMartin Hundebøll 		/* Order fragments from highest to lowest. */
176610bfc6bSMartin Hundebøll 		if (frag_entry_curr->no < frag_entry_new->no) {
177610bfc6bSMartin Hundebøll 			hlist_add_before(&frag_entry_new->list,
178610bfc6bSMartin Hundebøll 					 &frag_entry_curr->list);
179610bfc6bSMartin Hundebøll 			chain->size += skb->len - hdr_size;
180610bfc6bSMartin Hundebøll 			chain->timestamp = jiffies;
181610bfc6bSMartin Hundebøll 			ret = true;
182610bfc6bSMartin Hundebøll 			goto out;
183610bfc6bSMartin Hundebøll 		}
184d9124268SSven Eckelmann 
185d9124268SSven Eckelmann 		/* store current entry because it could be the last in list */
186d9124268SSven Eckelmann 		frag_entry_last = frag_entry_curr;
187610bfc6bSMartin Hundebøll 	}
188610bfc6bSMartin Hundebøll 
189d9124268SSven Eckelmann 	/* Reached the end of the list, so insert after 'frag_entry_last'. */
190d9124268SSven Eckelmann 	if (likely(frag_entry_last)) {
191e050dbebSSven Eckelmann 		hlist_add_behind(&frag_entry_new->list, &frag_entry_last->list);
192610bfc6bSMartin Hundebøll 		chain->size += skb->len - hdr_size;
193610bfc6bSMartin Hundebøll 		chain->timestamp = jiffies;
194610bfc6bSMartin Hundebøll 		ret = true;
195610bfc6bSMartin Hundebøll 	}
196610bfc6bSMartin Hundebøll 
197610bfc6bSMartin Hundebøll out:
198610bfc6bSMartin Hundebøll 	if (chain->size > batadv_frag_size_limit() ||
19953e77145SSven Eckelmann 	    chain->total_size != ntohs(frag_packet->total_size) ||
20053e77145SSven Eckelmann 	    chain->total_size > batadv_frag_size_limit()) {
201610bfc6bSMartin Hundebøll 		/* Clear chain if total size of either the list or the packet
20253e77145SSven Eckelmann 		 * exceeds the maximum size of one merged packet. Don't allow
20353e77145SSven Eckelmann 		 * packets to have different total_size.
204610bfc6bSMartin Hundebøll 		 */
205610bfc6bSMartin Hundebøll 		batadv_frag_clear_chain(&chain->head);
206610bfc6bSMartin Hundebøll 		chain->size = 0;
207610bfc6bSMartin Hundebøll 	} else if (ntohs(frag_packet->total_size) == chain->size) {
208610bfc6bSMartin Hundebøll 		/* All fragments received. Hand over chain to caller. */
209610bfc6bSMartin Hundebøll 		hlist_move_list(&chain->head, chain_out);
210610bfc6bSMartin Hundebøll 		chain->size = 0;
211610bfc6bSMartin Hundebøll 	}
212610bfc6bSMartin Hundebøll 
213610bfc6bSMartin Hundebøll err_unlock:
214610bfc6bSMartin Hundebøll 	spin_unlock_bh(&chain->lock);
215610bfc6bSMartin Hundebøll 
216610bfc6bSMartin Hundebøll err:
217610bfc6bSMartin Hundebøll 	if (!ret)
218610bfc6bSMartin Hundebøll 		kfree(frag_entry_new);
219610bfc6bSMartin Hundebøll 
220610bfc6bSMartin Hundebøll 	return ret;
221610bfc6bSMartin Hundebøll }
222610bfc6bSMartin Hundebøll 
223610bfc6bSMartin Hundebøll /**
224610bfc6bSMartin Hundebøll  * batadv_frag_merge_packets - merge a chain of fragments
225610bfc6bSMartin Hundebøll  * @chain: head of chain with fragments
226610bfc6bSMartin Hundebøll  * @skb: packet with total size of skb after merging
227610bfc6bSMartin Hundebøll  *
228610bfc6bSMartin Hundebøll  * Expand the first skb in the chain and copy the content of the remaining
229610bfc6bSMartin Hundebøll  * skb's into the expanded one. After doing so, clear the chain.
230610bfc6bSMartin Hundebøll  *
231610bfc6bSMartin Hundebøll  * Returns the merged skb or NULL on error.
232610bfc6bSMartin Hundebøll  */
233610bfc6bSMartin Hundebøll static struct sk_buff *
234*83e8b877SSven Eckelmann batadv_frag_merge_packets(struct hlist_head *chain)
235610bfc6bSMartin Hundebøll {
236610bfc6bSMartin Hundebøll 	struct batadv_frag_packet *packet;
237610bfc6bSMartin Hundebøll 	struct batadv_frag_list_entry *entry;
238610bfc6bSMartin Hundebøll 	struct sk_buff *skb_out = NULL;
239610bfc6bSMartin Hundebøll 	int size, hdr_size = sizeof(struct batadv_frag_packet);
240610bfc6bSMartin Hundebøll 
241610bfc6bSMartin Hundebøll 	/* Remove first entry, as this is the destination for the rest of the
242610bfc6bSMartin Hundebøll 	 * fragments.
243610bfc6bSMartin Hundebøll 	 */
244610bfc6bSMartin Hundebøll 	entry = hlist_entry(chain->first, struct batadv_frag_list_entry, list);
245610bfc6bSMartin Hundebøll 	hlist_del(&entry->list);
246610bfc6bSMartin Hundebøll 	skb_out = entry->skb;
247610bfc6bSMartin Hundebøll 	kfree(entry);
248610bfc6bSMartin Hundebøll 
249*83e8b877SSven Eckelmann 	packet = (struct batadv_frag_packet *)skb_out->data;
250*83e8b877SSven Eckelmann 	size = ntohs(packet->total_size);
251*83e8b877SSven Eckelmann 
252610bfc6bSMartin Hundebøll 	/* Make room for the rest of the fragments. */
2535b6698b0SSven Eckelmann 	if (pskb_expand_head(skb_out, 0, size - skb_out->len, GFP_ATOMIC) < 0) {
254610bfc6bSMartin Hundebøll 		kfree_skb(skb_out);
255610bfc6bSMartin Hundebøll 		skb_out = NULL;
256610bfc6bSMartin Hundebøll 		goto free;
257610bfc6bSMartin Hundebøll 	}
258610bfc6bSMartin Hundebøll 
259610bfc6bSMartin Hundebøll 	/* Move the existing MAC header to just before the payload. (Override
260610bfc6bSMartin Hundebøll 	 * the fragment header.)
261610bfc6bSMartin Hundebøll 	 */
262610bfc6bSMartin Hundebøll 	skb_pull_rcsum(skb_out, hdr_size);
263610bfc6bSMartin Hundebøll 	memmove(skb_out->data - ETH_HLEN, skb_mac_header(skb_out), ETH_HLEN);
264610bfc6bSMartin Hundebøll 	skb_set_mac_header(skb_out, -ETH_HLEN);
265610bfc6bSMartin Hundebøll 	skb_reset_network_header(skb_out);
266610bfc6bSMartin Hundebøll 	skb_reset_transport_header(skb_out);
267610bfc6bSMartin Hundebøll 
268610bfc6bSMartin Hundebøll 	/* Copy the payload of the each fragment into the last skb */
269610bfc6bSMartin Hundebøll 	hlist_for_each_entry(entry, chain, list) {
270610bfc6bSMartin Hundebøll 		size = entry->skb->len - hdr_size;
271610bfc6bSMartin Hundebøll 		memcpy(skb_put(skb_out, size), entry->skb->data + hdr_size,
272610bfc6bSMartin Hundebøll 		       size);
273610bfc6bSMartin Hundebøll 	}
274610bfc6bSMartin Hundebøll 
275610bfc6bSMartin Hundebøll free:
276610bfc6bSMartin Hundebøll 	/* Locking is not needed, because 'chain' is not part of any orig. */
277610bfc6bSMartin Hundebøll 	batadv_frag_clear_chain(chain);
278610bfc6bSMartin Hundebøll 	return skb_out;
279610bfc6bSMartin Hundebøll }
280610bfc6bSMartin Hundebøll 
281610bfc6bSMartin Hundebøll /**
282610bfc6bSMartin Hundebøll  * batadv_frag_skb_buffer - buffer fragment for later merge
283610bfc6bSMartin Hundebøll  * @skb: skb to buffer
284610bfc6bSMartin Hundebøll  * @orig_node_src: originator that the skb is received from
285610bfc6bSMartin Hundebøll  *
286610bfc6bSMartin Hundebøll  * Add fragment to buffer and merge fragments if possible.
287610bfc6bSMartin Hundebøll  *
288610bfc6bSMartin Hundebøll  * There are three possible outcomes: 1) Packet is merged: Return true and
289610bfc6bSMartin Hundebøll  * set *skb to merged packet; 2) Packet is buffered: Return true and set *skb
290610bfc6bSMartin Hundebøll  * to NULL; 3) Error: Return false and leave skb as is.
291610bfc6bSMartin Hundebøll  */
292610bfc6bSMartin Hundebøll bool batadv_frag_skb_buffer(struct sk_buff **skb,
293610bfc6bSMartin Hundebøll 			    struct batadv_orig_node *orig_node_src)
294610bfc6bSMartin Hundebøll {
295610bfc6bSMartin Hundebøll 	struct sk_buff *skb_out = NULL;
296610bfc6bSMartin Hundebøll 	struct hlist_head head = HLIST_HEAD_INIT;
297610bfc6bSMartin Hundebøll 	bool ret = false;
298610bfc6bSMartin Hundebøll 
299610bfc6bSMartin Hundebøll 	/* Add packet to buffer and table entry if merge is possible. */
300610bfc6bSMartin Hundebøll 	if (!batadv_frag_insert_packet(orig_node_src, *skb, &head))
301610bfc6bSMartin Hundebøll 		goto out_err;
302610bfc6bSMartin Hundebøll 
303610bfc6bSMartin Hundebøll 	/* Leave if more fragments are needed to merge. */
304610bfc6bSMartin Hundebøll 	if (hlist_empty(&head))
305610bfc6bSMartin Hundebøll 		goto out;
306610bfc6bSMartin Hundebøll 
307*83e8b877SSven Eckelmann 	skb_out = batadv_frag_merge_packets(&head);
308610bfc6bSMartin Hundebøll 	if (!skb_out)
309610bfc6bSMartin Hundebøll 		goto out_err;
310610bfc6bSMartin Hundebøll 
311610bfc6bSMartin Hundebøll out:
312610bfc6bSMartin Hundebøll 	*skb = skb_out;
313610bfc6bSMartin Hundebøll 	ret = true;
314610bfc6bSMartin Hundebøll out_err:
315610bfc6bSMartin Hundebøll 	return ret;
316610bfc6bSMartin Hundebøll }
317610bfc6bSMartin Hundebøll 
318610bfc6bSMartin Hundebøll /**
319610bfc6bSMartin Hundebøll  * batadv_frag_skb_fwd - forward fragments that would exceed MTU when merged
320610bfc6bSMartin Hundebøll  * @skb: skb to forward
321610bfc6bSMartin Hundebøll  * @recv_if: interface that the skb is received on
322610bfc6bSMartin Hundebøll  * @orig_node_src: originator that the skb is received from
323610bfc6bSMartin Hundebøll  *
324610bfc6bSMartin Hundebøll  * Look up the next-hop of the fragments payload and check if the merged packet
325610bfc6bSMartin Hundebøll  * will exceed the MTU towards the next-hop. If so, the fragment is forwarded
326610bfc6bSMartin Hundebøll  * without merging it.
327610bfc6bSMartin Hundebøll  *
328610bfc6bSMartin Hundebøll  * Returns true if the fragment is consumed/forwarded, false otherwise.
329610bfc6bSMartin Hundebøll  */
330610bfc6bSMartin Hundebøll bool batadv_frag_skb_fwd(struct sk_buff *skb,
331610bfc6bSMartin Hundebøll 			 struct batadv_hard_iface *recv_if,
332610bfc6bSMartin Hundebøll 			 struct batadv_orig_node *orig_node_src)
333610bfc6bSMartin Hundebøll {
334610bfc6bSMartin Hundebøll 	struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
335610bfc6bSMartin Hundebøll 	struct batadv_orig_node *orig_node_dst = NULL;
336610bfc6bSMartin Hundebøll 	struct batadv_neigh_node *neigh_node = NULL;
337610bfc6bSMartin Hundebøll 	struct batadv_frag_packet *packet;
338610bfc6bSMartin Hundebøll 	uint16_t total_size;
339610bfc6bSMartin Hundebøll 	bool ret = false;
340610bfc6bSMartin Hundebøll 
341610bfc6bSMartin Hundebøll 	packet = (struct batadv_frag_packet *)skb->data;
342610bfc6bSMartin Hundebøll 	orig_node_dst = batadv_orig_hash_find(bat_priv, packet->dest);
343610bfc6bSMartin Hundebøll 	if (!orig_node_dst)
344610bfc6bSMartin Hundebøll 		goto out;
345610bfc6bSMartin Hundebøll 
346610bfc6bSMartin Hundebøll 	neigh_node = batadv_find_router(bat_priv, orig_node_dst, recv_if);
347610bfc6bSMartin Hundebøll 	if (!neigh_node)
348610bfc6bSMartin Hundebøll 		goto out;
349610bfc6bSMartin Hundebøll 
350610bfc6bSMartin Hundebøll 	/* Forward the fragment, if the merged packet would be too big to
351610bfc6bSMartin Hundebøll 	 * be assembled.
352610bfc6bSMartin Hundebøll 	 */
353610bfc6bSMartin Hundebøll 	total_size = ntohs(packet->total_size);
354610bfc6bSMartin Hundebøll 	if (total_size > neigh_node->if_incoming->net_dev->mtu) {
355610bfc6bSMartin Hundebøll 		batadv_inc_counter(bat_priv, BATADV_CNT_FRAG_FWD);
356610bfc6bSMartin Hundebøll 		batadv_add_counter(bat_priv, BATADV_CNT_FRAG_FWD_BYTES,
357610bfc6bSMartin Hundebøll 				   skb->len + ETH_HLEN);
358610bfc6bSMartin Hundebøll 
359a40d9b07SSimon Wunderlich 		packet->ttl--;
360610bfc6bSMartin Hundebøll 		batadv_send_skb_packet(skb, neigh_node->if_incoming,
361610bfc6bSMartin Hundebøll 				       neigh_node->addr);
362610bfc6bSMartin Hundebøll 		ret = true;
363610bfc6bSMartin Hundebøll 	}
364610bfc6bSMartin Hundebøll 
365610bfc6bSMartin Hundebøll out:
366610bfc6bSMartin Hundebøll 	if (orig_node_dst)
367610bfc6bSMartin Hundebøll 		batadv_orig_node_free_ref(orig_node_dst);
368610bfc6bSMartin Hundebøll 	if (neigh_node)
369610bfc6bSMartin Hundebøll 		batadv_neigh_node_free_ref(neigh_node);
370610bfc6bSMartin Hundebøll 	return ret;
371610bfc6bSMartin Hundebøll }
372ee75ed88SMartin Hundebøll 
373ee75ed88SMartin Hundebøll /**
374ee75ed88SMartin Hundebøll  * batadv_frag_create - create a fragment from skb
375ee75ed88SMartin Hundebøll  * @skb: skb to create fragment from
376ee75ed88SMartin Hundebøll  * @frag_head: header to use in new fragment
377ee75ed88SMartin Hundebøll  * @mtu: size of new fragment
378ee75ed88SMartin Hundebøll  *
379ee75ed88SMartin Hundebøll  * Split the passed skb into two fragments: A new one with size matching the
380ee75ed88SMartin Hundebøll  * passed mtu and the old one with the rest. The new skb contains data from the
381ee75ed88SMartin Hundebøll  * tail of the old skb.
382ee75ed88SMartin Hundebøll  *
383ee75ed88SMartin Hundebøll  * Returns the new fragment, NULL on error.
384ee75ed88SMartin Hundebøll  */
385ee75ed88SMartin Hundebøll static struct sk_buff *batadv_frag_create(struct sk_buff *skb,
386ee75ed88SMartin Hundebøll 					  struct batadv_frag_packet *frag_head,
387ee75ed88SMartin Hundebøll 					  unsigned int mtu)
388ee75ed88SMartin Hundebøll {
389ee75ed88SMartin Hundebøll 	struct sk_buff *skb_fragment;
390ee75ed88SMartin Hundebøll 	unsigned header_size = sizeof(*frag_head);
391ee75ed88SMartin Hundebøll 	unsigned fragment_size = mtu - header_size;
392ee75ed88SMartin Hundebøll 
393ee75ed88SMartin Hundebøll 	skb_fragment = netdev_alloc_skb(NULL, mtu + ETH_HLEN);
394ee75ed88SMartin Hundebøll 	if (!skb_fragment)
395ee75ed88SMartin Hundebøll 		goto err;
396ee75ed88SMartin Hundebøll 
397ee75ed88SMartin Hundebøll 	skb->priority = TC_PRIO_CONTROL;
398ee75ed88SMartin Hundebøll 
399ee75ed88SMartin Hundebøll 	/* Eat the last mtu-bytes of the skb */
400ee75ed88SMartin Hundebøll 	skb_reserve(skb_fragment, header_size + ETH_HLEN);
401ee75ed88SMartin Hundebøll 	skb_split(skb, skb_fragment, skb->len - fragment_size);
402ee75ed88SMartin Hundebøll 
403ee75ed88SMartin Hundebøll 	/* Add the header */
404ee75ed88SMartin Hundebøll 	skb_push(skb_fragment, header_size);
405ee75ed88SMartin Hundebøll 	memcpy(skb_fragment->data, frag_head, header_size);
406ee75ed88SMartin Hundebøll 
407ee75ed88SMartin Hundebøll err:
408ee75ed88SMartin Hundebøll 	return skb_fragment;
409ee75ed88SMartin Hundebøll }
410ee75ed88SMartin Hundebøll 
411ee75ed88SMartin Hundebøll /**
412ee75ed88SMartin Hundebøll  * batadv_frag_send_packet - create up to 16 fragments from the passed skb
413ee75ed88SMartin Hundebøll  * @skb: skb to create fragments from
414ee75ed88SMartin Hundebøll  * @orig_node: final destination of the created fragments
415ee75ed88SMartin Hundebøll  * @neigh_node: next-hop of the created fragments
416ee75ed88SMartin Hundebøll  *
417ee75ed88SMartin Hundebøll  * Returns true on success, false otherwise.
418ee75ed88SMartin Hundebøll  */
419ee75ed88SMartin Hundebøll bool batadv_frag_send_packet(struct sk_buff *skb,
420ee75ed88SMartin Hundebøll 			     struct batadv_orig_node *orig_node,
421ee75ed88SMartin Hundebøll 			     struct batadv_neigh_node *neigh_node)
422ee75ed88SMartin Hundebøll {
423ee75ed88SMartin Hundebøll 	struct batadv_priv *bat_priv;
424be181015SAntonio Quartulli 	struct batadv_hard_iface *primary_if = NULL;
425ee75ed88SMartin Hundebøll 	struct batadv_frag_packet frag_header;
426ee75ed88SMartin Hundebøll 	struct sk_buff *skb_fragment;
427ee75ed88SMartin Hundebøll 	unsigned mtu = neigh_node->if_incoming->net_dev->mtu;
428ee75ed88SMartin Hundebøll 	unsigned header_size = sizeof(frag_header);
429ee75ed88SMartin Hundebøll 	unsigned max_fragment_size, max_packet_size;
430be181015SAntonio Quartulli 	bool ret = false;
431ee75ed88SMartin Hundebøll 
432ee75ed88SMartin Hundebøll 	/* To avoid merge and refragmentation at next-hops we never send
433ee75ed88SMartin Hundebøll 	 * fragments larger than BATADV_FRAG_MAX_FRAG_SIZE
434ee75ed88SMartin Hundebøll 	 */
435ee75ed88SMartin Hundebøll 	mtu = min_t(unsigned, mtu, BATADV_FRAG_MAX_FRAG_SIZE);
4360402e444SSven Eckelmann 	max_fragment_size = mtu - header_size;
437ee75ed88SMartin Hundebøll 	max_packet_size = max_fragment_size * BATADV_FRAG_MAX_FRAGMENTS;
438ee75ed88SMartin Hundebøll 
439ee75ed88SMartin Hundebøll 	/* Don't even try to fragment, if we need more than 16 fragments */
440ee75ed88SMartin Hundebøll 	if (skb->len > max_packet_size)
441ee75ed88SMartin Hundebøll 		goto out_err;
442ee75ed88SMartin Hundebøll 
443ee75ed88SMartin Hundebøll 	bat_priv = orig_node->bat_priv;
444ee75ed88SMartin Hundebøll 	primary_if = batadv_primary_if_get_selected(bat_priv);
445ee75ed88SMartin Hundebøll 	if (!primary_if)
446ee75ed88SMartin Hundebøll 		goto out_err;
447ee75ed88SMartin Hundebøll 
448ee75ed88SMartin Hundebøll 	/* Create one header to be copied to all fragments */
449a40d9b07SSimon Wunderlich 	frag_header.packet_type = BATADV_UNICAST_FRAG;
450a40d9b07SSimon Wunderlich 	frag_header.version = BATADV_COMPAT_VERSION;
451a40d9b07SSimon Wunderlich 	frag_header.ttl = BATADV_TTL;
452ee75ed88SMartin Hundebøll 	frag_header.seqno = htons(atomic_inc_return(&bat_priv->frag_seqno));
453ee75ed88SMartin Hundebøll 	frag_header.reserved = 0;
454ee75ed88SMartin Hundebøll 	frag_header.no = 0;
455ee75ed88SMartin Hundebøll 	frag_header.total_size = htons(skb->len);
4568fdd0153SAntonio Quartulli 	ether_addr_copy(frag_header.orig, primary_if->net_dev->dev_addr);
4578fdd0153SAntonio Quartulli 	ether_addr_copy(frag_header.dest, orig_node->orig);
458ee75ed88SMartin Hundebøll 
459ee75ed88SMartin Hundebøll 	/* Eat and send fragments from the tail of skb */
460ee75ed88SMartin Hundebøll 	while (skb->len > max_fragment_size) {
461ee75ed88SMartin Hundebøll 		skb_fragment = batadv_frag_create(skb, &frag_header, mtu);
462ee75ed88SMartin Hundebøll 		if (!skb_fragment)
463ee75ed88SMartin Hundebøll 			goto out_err;
464ee75ed88SMartin Hundebøll 
465ee75ed88SMartin Hundebøll 		batadv_inc_counter(bat_priv, BATADV_CNT_FRAG_TX);
466ee75ed88SMartin Hundebøll 		batadv_add_counter(bat_priv, BATADV_CNT_FRAG_TX_BYTES,
467ee75ed88SMartin Hundebøll 				   skb_fragment->len + ETH_HLEN);
468ee75ed88SMartin Hundebøll 		batadv_send_skb_packet(skb_fragment, neigh_node->if_incoming,
469ee75ed88SMartin Hundebøll 				       neigh_node->addr);
470ee75ed88SMartin Hundebøll 		frag_header.no++;
471ee75ed88SMartin Hundebøll 
472ee75ed88SMartin Hundebøll 		/* The initial check in this function should cover this case */
473ee75ed88SMartin Hundebøll 		if (frag_header.no == BATADV_FRAG_MAX_FRAGMENTS - 1)
474ee75ed88SMartin Hundebøll 			goto out_err;
475ee75ed88SMartin Hundebøll 	}
476ee75ed88SMartin Hundebøll 
477ee75ed88SMartin Hundebøll 	/* Make room for the fragment header. */
478ee75ed88SMartin Hundebøll 	if (batadv_skb_head_push(skb, header_size) < 0 ||
479ee75ed88SMartin Hundebøll 	    pskb_expand_head(skb, header_size + ETH_HLEN, 0, GFP_ATOMIC) < 0)
480ee75ed88SMartin Hundebøll 		goto out_err;
481ee75ed88SMartin Hundebøll 
482ee75ed88SMartin Hundebøll 	memcpy(skb->data, &frag_header, header_size);
483ee75ed88SMartin Hundebøll 
484ee75ed88SMartin Hundebøll 	/* Send the last fragment */
485ee75ed88SMartin Hundebøll 	batadv_inc_counter(bat_priv, BATADV_CNT_FRAG_TX);
486ee75ed88SMartin Hundebøll 	batadv_add_counter(bat_priv, BATADV_CNT_FRAG_TX_BYTES,
487ee75ed88SMartin Hundebøll 			   skb->len + ETH_HLEN);
488ee75ed88SMartin Hundebøll 	batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
489ee75ed88SMartin Hundebøll 
490be181015SAntonio Quartulli 	ret = true;
491be181015SAntonio Quartulli 
492ee75ed88SMartin Hundebøll out_err:
493be181015SAntonio Quartulli 	if (primary_if)
494be181015SAntonio Quartulli 		batadv_hardif_free_ref(primary_if);
495be181015SAntonio Quartulli 
496be181015SAntonio Quartulli 	return ret;
497ee75ed88SMartin Hundebøll }
498