17db7d9f3SSven Eckelmann // SPDX-License-Identifier: GPL-2.0
2cfa55c6dSSven Eckelmann /* Copyright (C) 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>
18fcd193e1SSven 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
31610bfc6bSMartin Hundebøll /**
327e9a8c2cSSven Eckelmann * batadv_frag_clear_chain() - delete entries in the fragment buffer chain
33610bfc6bSMartin Hundebøll * @head: head of chain with entries.
34bd687fe4SSven Eckelmann * @dropped: whether the chain is cleared because all fragments are dropped
35610bfc6bSMartin Hundebøll *
36610bfc6bSMartin Hundebøll * Free fragments in the passed hlist. Should be called with appropriate lock.
37610bfc6bSMartin Hundebøll */
batadv_frag_clear_chain(struct hlist_head * head,bool dropped)38bd687fe4SSven Eckelmann static void batadv_frag_clear_chain(struct hlist_head *head, bool dropped)
39610bfc6bSMartin Hundebøll {
40610bfc6bSMartin Hundebøll struct batadv_frag_list_entry *entry;
41610bfc6bSMartin Hundebøll struct hlist_node *node;
42610bfc6bSMartin Hundebøll
43610bfc6bSMartin Hundebøll hlist_for_each_entry_safe(entry, node, head, list) {
44610bfc6bSMartin Hundebøll hlist_del(&entry->list);
45bd687fe4SSven Eckelmann
46bd687fe4SSven Eckelmann if (dropped)
47610bfc6bSMartin Hundebøll kfree_skb(entry->skb);
48bd687fe4SSven Eckelmann else
49bd687fe4SSven Eckelmann consume_skb(entry->skb);
50bd687fe4SSven Eckelmann
51610bfc6bSMartin Hundebøll kfree(entry);
52610bfc6bSMartin Hundebøll }
53610bfc6bSMartin Hundebøll }
54610bfc6bSMartin Hundebøll
55610bfc6bSMartin Hundebøll /**
567e9a8c2cSSven Eckelmann * batadv_frag_purge_orig() - free fragments associated to an orig
57610bfc6bSMartin Hundebøll * @orig_node: originator to free fragments from
58610bfc6bSMartin Hundebøll * @check_cb: optional function to tell if an entry should be purged
59610bfc6bSMartin Hundebøll */
batadv_frag_purge_orig(struct batadv_orig_node * orig_node,bool (* check_cb)(struct batadv_frag_table_entry *))60610bfc6bSMartin Hundebøll void batadv_frag_purge_orig(struct batadv_orig_node *orig_node,
61610bfc6bSMartin Hundebøll bool (*check_cb)(struct batadv_frag_table_entry *))
62610bfc6bSMartin Hundebøll {
63610bfc6bSMartin Hundebøll struct batadv_frag_table_entry *chain;
646b5e971aSSven Eckelmann u8 i;
65610bfc6bSMartin Hundebøll
66610bfc6bSMartin Hundebøll for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
67610bfc6bSMartin Hundebøll chain = &orig_node->fragments[i];
6801f6b5c7SSven Eckelmann spin_lock_bh(&chain->lock);
69610bfc6bSMartin Hundebøll
70610bfc6bSMartin Hundebøll if (!check_cb || check_cb(chain)) {
71bd687fe4SSven Eckelmann batadv_frag_clear_chain(&chain->fragment_list, true);
7201f6b5c7SSven Eckelmann chain->size = 0;
73610bfc6bSMartin Hundebøll }
74610bfc6bSMartin Hundebøll
7501f6b5c7SSven Eckelmann spin_unlock_bh(&chain->lock);
76610bfc6bSMartin Hundebøll }
77610bfc6bSMartin Hundebøll }
78610bfc6bSMartin Hundebøll
79610bfc6bSMartin Hundebøll /**
807e9a8c2cSSven Eckelmann * batadv_frag_size_limit() - maximum possible size of packet to be fragmented
81610bfc6bSMartin Hundebøll *
8262fe710fSSven Eckelmann * Return: the maximum size of payload that can be fragmented.
83610bfc6bSMartin Hundebøll */
batadv_frag_size_limit(void)84610bfc6bSMartin Hundebøll static int batadv_frag_size_limit(void)
85610bfc6bSMartin Hundebøll {
86610bfc6bSMartin Hundebøll int limit = BATADV_FRAG_MAX_FRAG_SIZE;
87610bfc6bSMartin Hundebøll
88610bfc6bSMartin Hundebøll limit -= sizeof(struct batadv_frag_packet);
89610bfc6bSMartin Hundebøll limit *= BATADV_FRAG_MAX_FRAGMENTS;
90610bfc6bSMartin Hundebøll
91610bfc6bSMartin Hundebøll return limit;
92610bfc6bSMartin Hundebøll }
93610bfc6bSMartin Hundebøll
94610bfc6bSMartin Hundebøll /**
957e9a8c2cSSven Eckelmann * batadv_frag_init_chain() - check and prepare fragment chain for new fragment
96610bfc6bSMartin Hundebøll * @chain: chain in fragments table to init
97610bfc6bSMartin Hundebøll * @seqno: sequence number of the received fragment
98610bfc6bSMartin Hundebøll *
99610bfc6bSMartin Hundebøll * Make chain ready for a fragment with sequence number "seqno". Delete existing
100610bfc6bSMartin Hundebøll * entries if they have an "old" sequence number.
101610bfc6bSMartin Hundebøll *
102610bfc6bSMartin Hundebøll * Caller must hold chain->lock.
103610bfc6bSMartin Hundebøll *
104bccb48c8SSven Eckelmann * Return: true if chain is empty and the caller can just insert the new
105bccb48c8SSven Eckelmann * fragment without searching for the right position.
106610bfc6bSMartin Hundebøll */
batadv_frag_init_chain(struct batadv_frag_table_entry * chain,u16 seqno)107610bfc6bSMartin Hundebøll static bool batadv_frag_init_chain(struct batadv_frag_table_entry *chain,
1086b5e971aSSven Eckelmann u16 seqno)
109610bfc6bSMartin Hundebøll {
1105274cd68SSven Eckelmann lockdep_assert_held(&chain->lock);
1115274cd68SSven Eckelmann
112610bfc6bSMartin Hundebøll if (chain->seqno == seqno)
113610bfc6bSMartin Hundebøll return false;
114610bfc6bSMartin Hundebøll
115176e5b77SSven Eckelmann if (!hlist_empty(&chain->fragment_list))
116bd687fe4SSven Eckelmann batadv_frag_clear_chain(&chain->fragment_list, true);
117610bfc6bSMartin Hundebøll
118610bfc6bSMartin Hundebøll chain->size = 0;
119610bfc6bSMartin Hundebøll chain->seqno = seqno;
120610bfc6bSMartin Hundebøll
121610bfc6bSMartin Hundebøll return true;
122610bfc6bSMartin Hundebøll }
123610bfc6bSMartin Hundebøll
124610bfc6bSMartin Hundebøll /**
1257e9a8c2cSSven Eckelmann * batadv_frag_insert_packet() - insert a fragment into a fragment chain
126610bfc6bSMartin Hundebøll * @orig_node: originator that the fragment was received from
127610bfc6bSMartin Hundebøll * @skb: skb to insert
128610bfc6bSMartin Hundebøll * @chain_out: list head to attach complete chains of fragments to
129610bfc6bSMartin Hundebøll *
130610bfc6bSMartin Hundebøll * Insert a new fragment into the reverse ordered chain in the right table
131610bfc6bSMartin Hundebøll * entry. The hash table entry is cleared if "old" fragments exist in it.
132610bfc6bSMartin Hundebøll *
13362fe710fSSven Eckelmann * Return: true if skb is buffered, false on error. If the chain has all the
134610bfc6bSMartin Hundebøll * fragments needed to merge the packet, the chain is moved to the passed head
135610bfc6bSMartin Hundebøll * to avoid locking the chain in the table.
136610bfc6bSMartin Hundebøll */
batadv_frag_insert_packet(struct batadv_orig_node * orig_node,struct sk_buff * skb,struct hlist_head * chain_out)137610bfc6bSMartin Hundebøll static bool batadv_frag_insert_packet(struct batadv_orig_node *orig_node,
138610bfc6bSMartin Hundebøll struct sk_buff *skb,
139610bfc6bSMartin Hundebøll struct hlist_head *chain_out)
140610bfc6bSMartin Hundebøll {
141610bfc6bSMartin Hundebøll struct batadv_frag_table_entry *chain;
142610bfc6bSMartin Hundebøll struct batadv_frag_list_entry *frag_entry_new = NULL, *frag_entry_curr;
143d9124268SSven Eckelmann struct batadv_frag_list_entry *frag_entry_last = NULL;
144610bfc6bSMartin Hundebøll struct batadv_frag_packet *frag_packet;
1456b5e971aSSven Eckelmann u8 bucket;
1466b5e971aSSven Eckelmann u16 seqno, hdr_size = sizeof(struct batadv_frag_packet);
147610bfc6bSMartin Hundebøll bool ret = false;
148610bfc6bSMartin Hundebøll
149610bfc6bSMartin Hundebøll /* Linearize packet to avoid linearizing 16 packets in a row when doing
150610bfc6bSMartin Hundebøll * the later merge. Non-linear merge should be added to remove this
151610bfc6bSMartin Hundebøll * linearization.
152610bfc6bSMartin Hundebøll */
153610bfc6bSMartin Hundebøll if (skb_linearize(skb) < 0)
154610bfc6bSMartin Hundebøll goto err;
155610bfc6bSMartin Hundebøll
156610bfc6bSMartin Hundebøll frag_packet = (struct batadv_frag_packet *)skb->data;
157610bfc6bSMartin Hundebøll seqno = ntohs(frag_packet->seqno);
158610bfc6bSMartin Hundebøll bucket = seqno % BATADV_FRAG_BUFFER_COUNT;
159610bfc6bSMartin Hundebøll
160610bfc6bSMartin Hundebøll frag_entry_new = kmalloc(sizeof(*frag_entry_new), GFP_ATOMIC);
161610bfc6bSMartin Hundebøll if (!frag_entry_new)
162610bfc6bSMartin Hundebøll goto err;
163610bfc6bSMartin Hundebøll
164610bfc6bSMartin Hundebøll frag_entry_new->skb = skb;
165610bfc6bSMartin Hundebøll frag_entry_new->no = frag_packet->no;
166610bfc6bSMartin Hundebøll
167610bfc6bSMartin Hundebøll /* Select entry in the "chain table" and delete any prior fragments
168610bfc6bSMartin Hundebøll * with another sequence number. batadv_frag_init_chain() returns true,
169610bfc6bSMartin Hundebøll * if the list is empty at return.
170610bfc6bSMartin Hundebøll */
171610bfc6bSMartin Hundebøll chain = &orig_node->fragments[bucket];
172610bfc6bSMartin Hundebøll spin_lock_bh(&chain->lock);
173610bfc6bSMartin Hundebøll if (batadv_frag_init_chain(chain, seqno)) {
174176e5b77SSven Eckelmann hlist_add_head(&frag_entry_new->list, &chain->fragment_list);
175610bfc6bSMartin Hundebøll chain->size = skb->len - hdr_size;
176610bfc6bSMartin Hundebøll chain->timestamp = jiffies;
17753e77145SSven Eckelmann chain->total_size = ntohs(frag_packet->total_size);
178610bfc6bSMartin Hundebøll ret = true;
179610bfc6bSMartin Hundebøll goto out;
180610bfc6bSMartin Hundebøll }
181610bfc6bSMartin Hundebøll
182610bfc6bSMartin Hundebøll /* Find the position for the new fragment. */
183176e5b77SSven Eckelmann hlist_for_each_entry(frag_entry_curr, &chain->fragment_list, list) {
184610bfc6bSMartin Hundebøll /* Drop packet if fragment already exists. */
185610bfc6bSMartin Hundebøll if (frag_entry_curr->no == frag_entry_new->no)
186610bfc6bSMartin Hundebøll goto err_unlock;
187610bfc6bSMartin Hundebøll
188610bfc6bSMartin Hundebøll /* Order fragments from highest to lowest. */
189610bfc6bSMartin Hundebøll if (frag_entry_curr->no < frag_entry_new->no) {
190610bfc6bSMartin Hundebøll hlist_add_before(&frag_entry_new->list,
191610bfc6bSMartin Hundebøll &frag_entry_curr->list);
192610bfc6bSMartin Hundebøll chain->size += skb->len - hdr_size;
193610bfc6bSMartin Hundebøll chain->timestamp = jiffies;
194610bfc6bSMartin Hundebøll ret = true;
195610bfc6bSMartin Hundebøll goto out;
196610bfc6bSMartin Hundebøll }
197d9124268SSven Eckelmann
198d9124268SSven Eckelmann /* store current entry because it could be the last in list */
199d9124268SSven Eckelmann frag_entry_last = frag_entry_curr;
200610bfc6bSMartin Hundebøll }
201610bfc6bSMartin Hundebøll
202d9124268SSven Eckelmann /* Reached the end of the list, so insert after 'frag_entry_last'. */
203d9124268SSven Eckelmann if (likely(frag_entry_last)) {
204e050dbebSSven Eckelmann hlist_add_behind(&frag_entry_new->list, &frag_entry_last->list);
205610bfc6bSMartin Hundebøll chain->size += skb->len - hdr_size;
206610bfc6bSMartin Hundebøll chain->timestamp = jiffies;
207610bfc6bSMartin Hundebøll ret = true;
208610bfc6bSMartin Hundebøll }
209610bfc6bSMartin Hundebøll
210610bfc6bSMartin Hundebøll out:
211610bfc6bSMartin Hundebøll if (chain->size > batadv_frag_size_limit() ||
21253e77145SSven Eckelmann chain->total_size != ntohs(frag_packet->total_size) ||
21353e77145SSven Eckelmann chain->total_size > batadv_frag_size_limit()) {
214610bfc6bSMartin Hundebøll /* Clear chain if total size of either the list or the packet
21553e77145SSven Eckelmann * exceeds the maximum size of one merged packet. Don't allow
21653e77145SSven Eckelmann * packets to have different total_size.
217610bfc6bSMartin Hundebøll */
218bd687fe4SSven Eckelmann batadv_frag_clear_chain(&chain->fragment_list, true);
219610bfc6bSMartin Hundebøll chain->size = 0;
220610bfc6bSMartin Hundebøll } else if (ntohs(frag_packet->total_size) == chain->size) {
221610bfc6bSMartin Hundebøll /* All fragments received. Hand over chain to caller. */
222176e5b77SSven Eckelmann hlist_move_list(&chain->fragment_list, chain_out);
223610bfc6bSMartin Hundebøll chain->size = 0;
224610bfc6bSMartin Hundebøll }
225610bfc6bSMartin Hundebøll
226610bfc6bSMartin Hundebøll err_unlock:
227610bfc6bSMartin Hundebøll spin_unlock_bh(&chain->lock);
228610bfc6bSMartin Hundebøll
229610bfc6bSMartin Hundebøll err:
230248e23b5SSven Eckelmann if (!ret) {
231610bfc6bSMartin Hundebøll kfree(frag_entry_new);
232248e23b5SSven Eckelmann kfree_skb(skb);
233248e23b5SSven Eckelmann }
234610bfc6bSMartin Hundebøll
235610bfc6bSMartin Hundebøll return ret;
236610bfc6bSMartin Hundebøll }
237610bfc6bSMartin Hundebøll
238610bfc6bSMartin Hundebøll /**
2397e9a8c2cSSven Eckelmann * batadv_frag_merge_packets() - merge a chain of fragments
240610bfc6bSMartin Hundebøll * @chain: head of chain with fragments
241610bfc6bSMartin Hundebøll *
242610bfc6bSMartin Hundebøll * Expand the first skb in the chain and copy the content of the remaining
243610bfc6bSMartin Hundebøll * skb's into the expanded one. After doing so, clear the chain.
244610bfc6bSMartin Hundebøll *
24562fe710fSSven Eckelmann * Return: the merged skb or NULL on error.
246610bfc6bSMartin Hundebøll */
247610bfc6bSMartin Hundebøll static struct sk_buff *
batadv_frag_merge_packets(struct hlist_head * chain)24883e8b877SSven Eckelmann batadv_frag_merge_packets(struct hlist_head *chain)
249610bfc6bSMartin Hundebøll {
250610bfc6bSMartin Hundebøll struct batadv_frag_packet *packet;
251610bfc6bSMartin Hundebøll struct batadv_frag_list_entry *entry;
252422d2f77SSven Eckelmann struct sk_buff *skb_out;
253610bfc6bSMartin Hundebøll int size, hdr_size = sizeof(struct batadv_frag_packet);
254bd687fe4SSven Eckelmann bool dropped = false;
255610bfc6bSMartin Hundebøll
256610bfc6bSMartin Hundebøll /* Remove first entry, as this is the destination for the rest of the
257610bfc6bSMartin Hundebøll * fragments.
258610bfc6bSMartin Hundebøll */
259610bfc6bSMartin Hundebøll entry = hlist_entry(chain->first, struct batadv_frag_list_entry, list);
260610bfc6bSMartin Hundebøll hlist_del(&entry->list);
261610bfc6bSMartin Hundebøll skb_out = entry->skb;
262610bfc6bSMartin Hundebøll kfree(entry);
263610bfc6bSMartin Hundebøll
26483e8b877SSven Eckelmann packet = (struct batadv_frag_packet *)skb_out->data;
265d7d8bbb4SSven Eckelmann size = ntohs(packet->total_size) + hdr_size;
26683e8b877SSven Eckelmann
267610bfc6bSMartin Hundebøll /* Make room for the rest of the fragments. */
2685b6698b0SSven Eckelmann if (pskb_expand_head(skb_out, 0, size - skb_out->len, GFP_ATOMIC) < 0) {
269610bfc6bSMartin Hundebøll kfree_skb(skb_out);
270610bfc6bSMartin Hundebøll skb_out = NULL;
271bd687fe4SSven Eckelmann dropped = true;
272610bfc6bSMartin Hundebøll goto free;
273610bfc6bSMartin Hundebøll }
274610bfc6bSMartin Hundebøll
275610bfc6bSMartin Hundebøll /* Move the existing MAC header to just before the payload. (Override
276610bfc6bSMartin Hundebøll * the fragment header.)
277610bfc6bSMartin Hundebøll */
2783bf2a09dSMatthias Schiffer skb_pull(skb_out, hdr_size);
2793bf2a09dSMatthias Schiffer skb_out->ip_summed = CHECKSUM_NONE;
280610bfc6bSMartin Hundebøll memmove(skb_out->data - ETH_HLEN, skb_mac_header(skb_out), ETH_HLEN);
281610bfc6bSMartin Hundebøll skb_set_mac_header(skb_out, -ETH_HLEN);
282610bfc6bSMartin Hundebøll skb_reset_network_header(skb_out);
283610bfc6bSMartin Hundebøll skb_reset_transport_header(skb_out);
284610bfc6bSMartin Hundebøll
285610bfc6bSMartin Hundebøll /* Copy the payload of the each fragment into the last skb */
286610bfc6bSMartin Hundebøll hlist_for_each_entry(entry, chain, list) {
287610bfc6bSMartin Hundebøll size = entry->skb->len - hdr_size;
28859ae1d12SJohannes Berg skb_put_data(skb_out, entry->skb->data + hdr_size, size);
289610bfc6bSMartin Hundebøll }
290610bfc6bSMartin Hundebøll
291610bfc6bSMartin Hundebøll free:
292610bfc6bSMartin Hundebøll /* Locking is not needed, because 'chain' is not part of any orig. */
293bd687fe4SSven Eckelmann batadv_frag_clear_chain(chain, dropped);
294610bfc6bSMartin Hundebøll return skb_out;
295610bfc6bSMartin Hundebøll }
296610bfc6bSMartin Hundebøll
297610bfc6bSMartin Hundebøll /**
2987e9a8c2cSSven Eckelmann * batadv_frag_skb_buffer() - buffer fragment for later merge
299610bfc6bSMartin Hundebøll * @skb: skb to buffer
300610bfc6bSMartin Hundebøll * @orig_node_src: originator that the skb is received from
301610bfc6bSMartin Hundebøll *
302610bfc6bSMartin Hundebøll * Add fragment to buffer and merge fragments if possible.
303610bfc6bSMartin Hundebøll *
304610bfc6bSMartin Hundebøll * There are three possible outcomes: 1) Packet is merged: Return true and
305610bfc6bSMartin Hundebøll * set *skb to merged packet; 2) Packet is buffered: Return true and set *skb
306248e23b5SSven Eckelmann * to NULL; 3) Error: Return false and free skb.
30762fe710fSSven Eckelmann *
30821ba5ab2SSven Eckelmann * Return: true when the packet is merged or buffered, false when skb is not
30962fe710fSSven Eckelmann * used.
310610bfc6bSMartin Hundebøll */
batadv_frag_skb_buffer(struct sk_buff ** skb,struct batadv_orig_node * orig_node_src)311610bfc6bSMartin Hundebøll bool batadv_frag_skb_buffer(struct sk_buff **skb,
312610bfc6bSMartin Hundebøll struct batadv_orig_node *orig_node_src)
313610bfc6bSMartin Hundebøll {
314610bfc6bSMartin Hundebøll struct sk_buff *skb_out = NULL;
315610bfc6bSMartin Hundebøll struct hlist_head head = HLIST_HEAD_INIT;
316610bfc6bSMartin Hundebøll bool ret = false;
317610bfc6bSMartin Hundebøll
318610bfc6bSMartin Hundebøll /* Add packet to buffer and table entry if merge is possible. */
319610bfc6bSMartin Hundebøll if (!batadv_frag_insert_packet(orig_node_src, *skb, &head))
320610bfc6bSMartin Hundebøll goto out_err;
321610bfc6bSMartin Hundebøll
322610bfc6bSMartin Hundebøll /* Leave if more fragments are needed to merge. */
323610bfc6bSMartin Hundebøll if (hlist_empty(&head))
324610bfc6bSMartin Hundebøll goto out;
325610bfc6bSMartin Hundebøll
32683e8b877SSven Eckelmann skb_out = batadv_frag_merge_packets(&head);
327610bfc6bSMartin Hundebøll if (!skb_out)
328610bfc6bSMartin Hundebøll goto out_err;
329610bfc6bSMartin Hundebøll
330610bfc6bSMartin Hundebøll out:
331610bfc6bSMartin Hundebøll ret = true;
332610bfc6bSMartin Hundebøll out_err:
333248e23b5SSven Eckelmann *skb = skb_out;
334610bfc6bSMartin Hundebøll return ret;
335610bfc6bSMartin Hundebøll }
336610bfc6bSMartin Hundebøll
337610bfc6bSMartin Hundebøll /**
3387e9a8c2cSSven Eckelmann * batadv_frag_skb_fwd() - forward fragments that would exceed MTU when merged
339610bfc6bSMartin Hundebøll * @skb: skb to forward
340610bfc6bSMartin Hundebøll * @recv_if: interface that the skb is received on
341610bfc6bSMartin Hundebøll * @orig_node_src: originator that the skb is received from
342610bfc6bSMartin Hundebøll *
343610bfc6bSMartin Hundebøll * Look up the next-hop of the fragments payload and check if the merged packet
344610bfc6bSMartin Hundebøll * will exceed the MTU towards the next-hop. If so, the fragment is forwarded
345610bfc6bSMartin Hundebøll * without merging it.
346610bfc6bSMartin Hundebøll *
34762fe710fSSven Eckelmann * Return: true if the fragment is consumed/forwarded, false otherwise.
348610bfc6bSMartin Hundebøll */
batadv_frag_skb_fwd(struct sk_buff * skb,struct batadv_hard_iface * recv_if,struct batadv_orig_node * orig_node_src)349610bfc6bSMartin Hundebøll bool batadv_frag_skb_fwd(struct sk_buff *skb,
350610bfc6bSMartin Hundebøll struct batadv_hard_iface *recv_if,
351610bfc6bSMartin Hundebøll struct batadv_orig_node *orig_node_src)
352610bfc6bSMartin Hundebøll {
353610bfc6bSMartin Hundebøll struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
354422d2f77SSven Eckelmann struct batadv_orig_node *orig_node_dst;
355610bfc6bSMartin Hundebøll struct batadv_neigh_node *neigh_node = NULL;
356610bfc6bSMartin Hundebøll struct batadv_frag_packet *packet;
3576b5e971aSSven Eckelmann u16 total_size;
358610bfc6bSMartin Hundebøll bool ret = false;
359610bfc6bSMartin Hundebøll
360610bfc6bSMartin Hundebøll packet = (struct batadv_frag_packet *)skb->data;
361610bfc6bSMartin Hundebøll orig_node_dst = batadv_orig_hash_find(bat_priv, packet->dest);
362610bfc6bSMartin Hundebøll if (!orig_node_dst)
363610bfc6bSMartin Hundebøll goto out;
364610bfc6bSMartin Hundebøll
365610bfc6bSMartin Hundebøll neigh_node = batadv_find_router(bat_priv, orig_node_dst, recv_if);
366610bfc6bSMartin Hundebøll if (!neigh_node)
367610bfc6bSMartin Hundebøll goto out;
368610bfc6bSMartin Hundebøll
369610bfc6bSMartin Hundebøll /* Forward the fragment, if the merged packet would be too big to
370610bfc6bSMartin Hundebøll * be assembled.
371610bfc6bSMartin Hundebøll */
372610bfc6bSMartin Hundebøll total_size = ntohs(packet->total_size);
373610bfc6bSMartin Hundebøll if (total_size > neigh_node->if_incoming->net_dev->mtu) {
374610bfc6bSMartin Hundebøll batadv_inc_counter(bat_priv, BATADV_CNT_FRAG_FWD);
375610bfc6bSMartin Hundebøll batadv_add_counter(bat_priv, BATADV_CNT_FRAG_FWD_BYTES,
376610bfc6bSMartin Hundebøll skb->len + ETH_HLEN);
377610bfc6bSMartin Hundebøll
378a40d9b07SSimon Wunderlich packet->ttl--;
37995d39278SAntonio Quartulli batadv_send_unicast_skb(skb, neigh_node);
380610bfc6bSMartin Hundebøll ret = true;
381610bfc6bSMartin Hundebøll }
382610bfc6bSMartin Hundebøll
383610bfc6bSMartin Hundebøll out:
3845d967310SSven Eckelmann batadv_orig_node_put(orig_node_dst);
38525bb2509SSven Eckelmann batadv_neigh_node_put(neigh_node);
386610bfc6bSMartin Hundebøll return ret;
387610bfc6bSMartin Hundebøll }
388ee75ed88SMartin Hundebøll
389ee75ed88SMartin Hundebøll /**
3907e9a8c2cSSven Eckelmann * batadv_frag_create() - create a fragment from skb
391c5cbfc87SSven Eckelmann * @net_dev: outgoing device for fragment
392ee75ed88SMartin Hundebøll * @skb: skb to create fragment from
393ee75ed88SMartin Hundebøll * @frag_head: header to use in new fragment
3941c2bcc76SSven Eckelmann * @fragment_size: size of new fragment
395ee75ed88SMartin Hundebøll *
396ee75ed88SMartin Hundebøll * Split the passed skb into two fragments: A new one with size matching the
397ee75ed88SMartin Hundebøll * passed mtu and the old one with the rest. The new skb contains data from the
398ee75ed88SMartin Hundebøll * tail of the old skb.
399ee75ed88SMartin Hundebøll *
40062fe710fSSven Eckelmann * Return: the new fragment, NULL on error.
401ee75ed88SMartin Hundebøll */
batadv_frag_create(struct net_device * net_dev,struct sk_buff * skb,struct batadv_frag_packet * frag_head,unsigned int fragment_size)402c5cbfc87SSven Eckelmann static struct sk_buff *batadv_frag_create(struct net_device *net_dev,
403c5cbfc87SSven Eckelmann struct sk_buff *skb,
404ee75ed88SMartin Hundebøll struct batadv_frag_packet *frag_head,
4051c2bcc76SSven Eckelmann unsigned int fragment_size)
406ee75ed88SMartin Hundebøll {
407c5cbfc87SSven Eckelmann unsigned int ll_reserved = LL_RESERVED_SPACE(net_dev);
408c5cbfc87SSven Eckelmann unsigned int tailroom = net_dev->needed_tailroom;
409ee75ed88SMartin Hundebøll struct sk_buff *skb_fragment;
410d3abce78SSven Eckelmann unsigned int header_size = sizeof(*frag_head);
4111c2bcc76SSven Eckelmann unsigned int mtu = fragment_size + header_size;
412ee75ed88SMartin Hundebøll
413c5cbfc87SSven Eckelmann skb_fragment = dev_alloc_skb(ll_reserved + mtu + tailroom);
414ee75ed88SMartin Hundebøll if (!skb_fragment)
415ee75ed88SMartin Hundebøll goto err;
416ee75ed88SMartin Hundebøll
4171914848eSAndrew Lunn skb_fragment->priority = skb->priority;
418ee75ed88SMartin Hundebøll
419ee75ed88SMartin Hundebøll /* Eat the last mtu-bytes of the skb */
420c5cbfc87SSven Eckelmann skb_reserve(skb_fragment, ll_reserved + header_size);
421ee75ed88SMartin Hundebøll skb_split(skb, skb_fragment, skb->len - fragment_size);
422ee75ed88SMartin Hundebøll
423ee75ed88SMartin Hundebøll /* Add the header */
424ee75ed88SMartin Hundebøll skb_push(skb_fragment, header_size);
425ee75ed88SMartin Hundebøll memcpy(skb_fragment->data, frag_head, header_size);
426ee75ed88SMartin Hundebøll
427ee75ed88SMartin Hundebøll err:
428ee75ed88SMartin Hundebøll return skb_fragment;
429ee75ed88SMartin Hundebøll }
430ee75ed88SMartin Hundebøll
431ee75ed88SMartin Hundebøll /**
4327e9a8c2cSSven Eckelmann * batadv_frag_send_packet() - create up to 16 fragments from the passed skb
433ee75ed88SMartin Hundebøll * @skb: skb to create fragments from
434ee75ed88SMartin Hundebøll * @orig_node: final destination of the created fragments
435ee75ed88SMartin Hundebøll * @neigh_node: next-hop of the created fragments
436ee75ed88SMartin Hundebøll *
4378def0be8SSven Eckelmann * Return: the netdev tx status or a negative errno code on a failure
438ee75ed88SMartin Hundebøll */
batadv_frag_send_packet(struct sk_buff * skb,struct batadv_orig_node * orig_node,struct batadv_neigh_node * neigh_node)439f50ca95aSAntonio Quartulli int batadv_frag_send_packet(struct sk_buff *skb,
440ee75ed88SMartin Hundebøll struct batadv_orig_node *orig_node,
441ee75ed88SMartin Hundebøll struct batadv_neigh_node *neigh_node)
442ee75ed88SMartin Hundebøll {
443c5cbfc87SSven Eckelmann struct net_device *net_dev = neigh_node->if_incoming->net_dev;
444ee75ed88SMartin Hundebøll struct batadv_priv *bat_priv;
445be181015SAntonio Quartulli struct batadv_hard_iface *primary_if = NULL;
446ee75ed88SMartin Hundebøll struct batadv_frag_packet frag_header;
447ee75ed88SMartin Hundebøll struct sk_buff *skb_fragment;
448c5cbfc87SSven Eckelmann unsigned int mtu = net_dev->mtu;
449d3abce78SSven Eckelmann unsigned int header_size = sizeof(frag_header);
4501c2bcc76SSven Eckelmann unsigned int max_fragment_size, num_fragments;
4518def0be8SSven Eckelmann int ret;
452ee75ed88SMartin Hundebøll
453ee75ed88SMartin Hundebøll /* To avoid merge and refragmentation at next-hops we never send
454ee75ed88SMartin Hundebøll * fragments larger than BATADV_FRAG_MAX_FRAG_SIZE
455ee75ed88SMartin Hundebøll */
456d3abce78SSven Eckelmann mtu = min_t(unsigned int, mtu, BATADV_FRAG_MAX_FRAG_SIZE);
4570402e444SSven Eckelmann max_fragment_size = mtu - header_size;
4581c2bcc76SSven Eckelmann
4591c2bcc76SSven Eckelmann if (skb->len == 0 || max_fragment_size == 0)
4601c2bcc76SSven Eckelmann return -EINVAL;
4611c2bcc76SSven Eckelmann
4621c2bcc76SSven Eckelmann num_fragments = (skb->len - 1) / max_fragment_size + 1;
4631c2bcc76SSven Eckelmann max_fragment_size = (skb->len - 1) / num_fragments + 1;
464ee75ed88SMartin Hundebøll
465ee75ed88SMartin Hundebøll /* Don't even try to fragment, if we need more than 16 fragments */
4661c2bcc76SSven Eckelmann if (num_fragments > BATADV_FRAG_MAX_FRAGMENTS) {
4678def0be8SSven Eckelmann ret = -EAGAIN;
4688def0be8SSven Eckelmann goto free_skb;
4698def0be8SSven Eckelmann }
470ee75ed88SMartin Hundebøll
471ee75ed88SMartin Hundebøll bat_priv = orig_node->bat_priv;
472ee75ed88SMartin Hundebøll primary_if = batadv_primary_if_get_selected(bat_priv);
4738def0be8SSven Eckelmann if (!primary_if) {
4748def0be8SSven Eckelmann ret = -EINVAL;
4754ea33ef0SSven Eckelmann goto free_skb;
4768def0be8SSven Eckelmann }
477ee75ed88SMartin Hundebøll
478*a063f2fbSSven Eckelmann /* GRO might have added fragments to the fragment list instead of
479*a063f2fbSSven Eckelmann * frags[]. But this is not handled by skb_split and must be
480*a063f2fbSSven Eckelmann * linearized to avoid incorrect length information after all
481*a063f2fbSSven Eckelmann * batman-adv fragments were created and submitted to the
482*a063f2fbSSven Eckelmann * hard-interface
483*a063f2fbSSven Eckelmann */
484*a063f2fbSSven Eckelmann if (skb_has_frag_list(skb) && __skb_linearize(skb)) {
485*a063f2fbSSven Eckelmann ret = -ENOMEM;
486*a063f2fbSSven Eckelmann goto free_skb;
487*a063f2fbSSven Eckelmann }
488*a063f2fbSSven Eckelmann
489ee75ed88SMartin Hundebøll /* Create one header to be copied to all fragments */
490a40d9b07SSimon Wunderlich frag_header.packet_type = BATADV_UNICAST_FRAG;
491a40d9b07SSimon Wunderlich frag_header.version = BATADV_COMPAT_VERSION;
492a40d9b07SSimon Wunderlich frag_header.ttl = BATADV_TTL;
493ee75ed88SMartin Hundebøll frag_header.seqno = htons(atomic_inc_return(&bat_priv->frag_seqno));
494ee75ed88SMartin Hundebøll frag_header.reserved = 0;
495ee75ed88SMartin Hundebøll frag_header.no = 0;
496ee75ed88SMartin Hundebøll frag_header.total_size = htons(skb->len);
497c0f25c80SAndrew Lunn
498c0f25c80SAndrew Lunn /* skb->priority values from 256->263 are magic values to
499c0f25c80SAndrew Lunn * directly indicate a specific 802.1d priority. This is used
500c0f25c80SAndrew Lunn * to allow 802.1d priority to be passed directly in from VLAN
501c0f25c80SAndrew Lunn * tags, etc.
502c0f25c80SAndrew Lunn */
503c0f25c80SAndrew Lunn if (skb->priority >= 256 && skb->priority <= 263)
504c0f25c80SAndrew Lunn frag_header.priority = skb->priority - 256;
505fe77d825SSven Eckelmann else
506fe77d825SSven Eckelmann frag_header.priority = 0;
507c0f25c80SAndrew Lunn
5088fdd0153SAntonio Quartulli ether_addr_copy(frag_header.orig, primary_if->net_dev->dev_addr);
5098fdd0153SAntonio Quartulli ether_addr_copy(frag_header.dest, orig_node->orig);
510ee75ed88SMartin Hundebøll
511ee75ed88SMartin Hundebøll /* Eat and send fragments from the tail of skb */
512ee75ed88SMartin Hundebøll while (skb->len > max_fragment_size) {
51351c6b429SLinus Lüssing /* The initial check in this function should cover this case */
51451c6b429SLinus Lüssing if (unlikely(frag_header.no == BATADV_FRAG_MAX_FRAGMENTS - 1)) {
51551c6b429SLinus Lüssing ret = -EINVAL;
51651c6b429SLinus Lüssing goto put_primary_if;
51751c6b429SLinus Lüssing }
51851c6b429SLinus Lüssing
519c5cbfc87SSven Eckelmann skb_fragment = batadv_frag_create(net_dev, skb, &frag_header,
5201c2bcc76SSven Eckelmann max_fragment_size);
5218def0be8SSven Eckelmann if (!skb_fragment) {
5228def0be8SSven Eckelmann ret = -ENOMEM;
5234ea33ef0SSven Eckelmann goto put_primary_if;
5248def0be8SSven Eckelmann }
525ee75ed88SMartin Hundebøll
526ee75ed88SMartin Hundebøll batadv_inc_counter(bat_priv, BATADV_CNT_FRAG_TX);
527ee75ed88SMartin Hundebøll batadv_add_counter(bat_priv, BATADV_CNT_FRAG_TX_BYTES,
528ee75ed88SMartin Hundebøll skb_fragment->len + ETH_HLEN);
529f50ca95aSAntonio Quartulli ret = batadv_send_unicast_skb(skb_fragment, neigh_node);
530f50ca95aSAntonio Quartulli if (ret != NET_XMIT_SUCCESS) {
5318def0be8SSven Eckelmann ret = NET_XMIT_DROP;
5324ea33ef0SSven Eckelmann goto put_primary_if;
533f50ca95aSAntonio Quartulli }
534f50ca95aSAntonio Quartulli
535ee75ed88SMartin Hundebøll frag_header.no++;
536ee75ed88SMartin Hundebøll }
537ee75ed88SMartin Hundebøll
538992b03b8SSven Eckelmann /* make sure that there is at least enough head for the fragmentation
539992b03b8SSven Eckelmann * and ethernet headers
540992b03b8SSven Eckelmann */
541992b03b8SSven Eckelmann ret = skb_cow_head(skb, ETH_HLEN + header_size);
542992b03b8SSven Eckelmann if (ret < 0)
5434ea33ef0SSven Eckelmann goto put_primary_if;
544ee75ed88SMartin Hundebøll
545992b03b8SSven Eckelmann skb_push(skb, header_size);
546ee75ed88SMartin Hundebøll memcpy(skb->data, &frag_header, header_size);
547ee75ed88SMartin Hundebøll
548ee75ed88SMartin Hundebøll /* Send the last fragment */
549ee75ed88SMartin Hundebøll batadv_inc_counter(bat_priv, BATADV_CNT_FRAG_TX);
550ee75ed88SMartin Hundebøll batadv_add_counter(bat_priv, BATADV_CNT_FRAG_TX_BYTES,
551ee75ed88SMartin Hundebøll skb->len + ETH_HLEN);
552f50ca95aSAntonio Quartulli ret = batadv_send_unicast_skb(skb, neigh_node);
5538def0be8SSven Eckelmann /* skb was consumed */
5548def0be8SSven Eckelmann skb = NULL;
555ee75ed88SMartin Hundebøll
5568def0be8SSven Eckelmann put_primary_if:
55782047ad7SSven Eckelmann batadv_hardif_put(primary_if);
5588def0be8SSven Eckelmann free_skb:
5598def0be8SSven Eckelmann kfree_skb(skb);
560be181015SAntonio Quartulli
561be181015SAntonio Quartulli return ret;
562ee75ed88SMartin Hundebøll }
563