1 // SPDX-License-Identifier: GPL-2.0
2 /* xfrm_iptfs: IPTFS encapsulation support
3 *
4 * April 21 2022, Christian Hopps <chopps@labn.net>
5 *
6 * Copyright (c) 2022, LabN Consulting, L.L.C.
7 *
8 */
9
10 #include <linux/kernel.h>
11 #include <linux/icmpv6.h>
12 #include <linux/skbuff_ref.h>
13 #include <net/gro.h>
14 #include <net/icmp.h>
15 #include <net/ip6_route.h>
16 #include <net/inet_ecn.h>
17 #include <net/xfrm.h>
18
19 #include <crypto/aead.h>
20
21 #include "xfrm_inout.h"
22 #include "trace_iptfs.h"
23
24 /* IPTFS encap (header) values. */
25 #define IPTFS_SUBTYPE_BASIC 0
26 #define IPTFS_SUBTYPE_CC 1
27
28 /* ----------------------------------------------- */
29 /* IP-TFS default SA values (tunnel egress/dir-in) */
30 /* ----------------------------------------------- */
31
32 /**
33 * define IPTFS_DEFAULT_DROP_TIME_USECS - default drop time
34 *
35 * The default IPTFS drop time in microseconds. The drop time is the amount of
36 * time before a missing out-of-order IPTFS tunnel packet is considered lost.
37 * See also the reorder window.
38 *
39 * Default 1s.
40 */
41 #define IPTFS_DEFAULT_DROP_TIME_USECS 1000000
42
43 /**
44 * define IPTFS_DEFAULT_REORDER_WINDOW - default reorder window size
45 *
46 * The default IPTFS reorder window size. The reorder window size dictates the
47 * maximum number of IPTFS tunnel packets in a sequence that may arrive out of
48 * order.
49 *
50 * Default 3. (tcp folks suggested)
51 */
52 #define IPTFS_DEFAULT_REORDER_WINDOW 3
53
54 /* ------------------------------------------------ */
55 /* IPTFS default SA values (tunnel ingress/dir-out) */
56 /* ------------------------------------------------ */
57
58 /**
59 * define IPTFS_DEFAULT_INIT_DELAY_USECS - default initial output delay
60 *
61 * The initial output delay is the amount of time prior to servicing the output
62 * queue after queueing the first packet on said queue. This applies anytime the
63 * output queue was previously empty.
64 *
65 * Default 0.
66 */
67 #define IPTFS_DEFAULT_INIT_DELAY_USECS 0
68
69 /**
70 * define IPTFS_DEFAULT_MAX_QUEUE_SIZE - default max output queue size.
71 *
72 * The default IPTFS max output queue size in octets. The output queue is where
73 * received packets destined for output over an IPTFS tunnel are stored prior to
74 * being output in aggregated/fragmented form over the IPTFS tunnel.
75 *
76 * Default 1M.
77 */
78 #define IPTFS_DEFAULT_MAX_QUEUE_SIZE (1024 * 10240)
79
80 /* Assumed: skb->head is cache aligned.
81 *
82 * L2 Header resv: Arrange for cacheline to start at skb->data - 16 to keep the
83 * to-be-pushed L2 header in the same cacheline as resulting `skb->data` (i.e.,
84 * the L3 header). If cacheline size is > 64 then skb->data + pushed L2 will all
85 * be in a single cacheline if we simply reserve 64 bytes.
86 *
87 * L3 Header resv: For L3+L2 headers (i.e., skb->data points at the IPTFS payload)
88 * we want `skb->data` to be cacheline aligned and all pushed L2L3 headers will
89 * be in their own cacheline[s]. 128 works for cachelins up to 128 bytes, for
90 * any larger cacheline sizes the pushed headers will simply share the cacheline
91 * with the start of the IPTFS payload (skb->data).
92 */
93 #define XFRM_IPTFS_MIN_L3HEADROOM 128
94 #define XFRM_IPTFS_MIN_L2HEADROOM (L1_CACHE_BYTES > 64 ? 64 : 64 + 16)
95
96 /* Min to try to share outer iptfs skb data vs copying into new skb */
97 #define IPTFS_PKT_SHARE_MIN 129
98
99 #define NSECS_IN_USEC 1000
100
101 #define IPTFS_HRTIMER_MODE HRTIMER_MODE_REL_SOFT
102
103 /**
104 * struct xfrm_iptfs_config - configuration for the IPTFS tunnel.
105 * @pkt_size: size of the outer IP packet. 0 to use interface and MTU discovery,
106 * otherwise the user specified value.
107 * @max_queue_size: The maximum number of octets allowed to be queued to be sent
108 * over the IPTFS SA. The queue size is measured as the size of all the
109 * packets enqueued.
110 * @reorder_win_size: the number slots in the reorder window, thus the number of
111 * packets that may arrive out of order.
112 * @dont_frag: true to inhibit fragmenting across IPTFS outer packets.
113 */
114 struct xfrm_iptfs_config {
115 u32 pkt_size; /* outer_packet_size or 0 */
116 u32 max_queue_size; /* octets */
117 u16 reorder_win_size;
118 u8 dont_frag : 1;
119 };
120
121 struct skb_wseq {
122 struct sk_buff *skb;
123 u64 drop_time;
124 };
125
126 /**
127 * struct xfrm_iptfs_data - mode specific xfrm state.
128 * @cfg: IPTFS tunnel config.
129 * @x: owning SA (xfrm_state).
130 * @queue: queued user packets to send.
131 * @queue_size: number of octets on queue (sum of packet sizes).
132 * @ecn_queue_size: octets above with ECN mark.
133 * @init_delay_ns: nanoseconds to wait to send initial IPTFS packet.
134 * @iptfs_timer: output timer.
135 * @iptfs_settime: time the output timer was set.
136 * @payload_mtu: max payload size.
137 * @w_seq_set: true after first seq received.
138 * @w_wantseq: waiting for this seq number as next to process (in order).
139 * @w_saved: the saved buf array (reorder window).
140 * @w_savedlen: the saved len (not size).
141 * @drop_lock: lock to protect reorder queue.
142 * @drop_timer: timer for considering next packet lost.
143 * @drop_time_ns: timer intervan in nanoseconds.
144 * @ra_newskb: new pkt being reassembled.
145 * @ra_wantseq: expected next sequence for reassembly.
146 * @ra_runt: last pkt bytes from very end of last skb.
147 * @ra_runtlen: size of ra_runt.
148 */
149 struct xfrm_iptfs_data {
150 struct xfrm_iptfs_config cfg;
151
152 /* Ingress User Input */
153 struct xfrm_state *x; /* owning state */
154 struct sk_buff_head queue; /* output queue */
155
156 u32 queue_size; /* octets */
157 u32 ecn_queue_size; /* octets above which ECN mark */
158 u64 init_delay_ns; /* nanoseconds */
159 struct hrtimer iptfs_timer; /* output timer */
160 time64_t iptfs_settime; /* time timer was set */
161 u32 payload_mtu; /* max payload size */
162
163 /* Tunnel input reordering */
164 bool w_seq_set; /* true after first seq received */
165 u64 w_wantseq; /* expected next sequence */
166 struct skb_wseq *w_saved; /* the saved buf array */
167 u32 w_savedlen; /* the saved len (not size) */
168 spinlock_t drop_lock;
169 struct hrtimer drop_timer;
170 u64 drop_time_ns;
171
172 /* Tunnel input reassembly */
173 struct sk_buff *ra_newskb; /* new pkt being reassembled */
174 u64 ra_wantseq; /* expected next sequence */
175 u8 ra_runt[6]; /* last pkt bytes from last skb */
176 u8 ra_runtlen; /* count of ra_runt */
177 };
178
179 static u32 __iptfs_get_inner_mtu(struct xfrm_state *x, int outer_mtu);
180 static enum hrtimer_restart iptfs_delay_timer(struct hrtimer *me);
181 static enum hrtimer_restart iptfs_drop_timer(struct hrtimer *me);
182
183 /* ================= */
184 /* Utility Functions */
185 /* ================= */
186
187 #ifdef TRACEPOINTS_ENABLED
__trace_ip_proto(struct iphdr * iph)188 static u32 __trace_ip_proto(struct iphdr *iph)
189 {
190 if (iph->version == 4)
191 return iph->protocol;
192 return ((struct ipv6hdr *)iph)->nexthdr;
193 }
194
__trace_ip_proto_seq(struct iphdr * iph)195 static u32 __trace_ip_proto_seq(struct iphdr *iph)
196 {
197 void *nexthdr;
198 u32 protocol = 0;
199
200 if (iph->version == 4) {
201 nexthdr = (void *)(iph + 1);
202 protocol = iph->protocol;
203 } else if (iph->version == 6) {
204 nexthdr = (void *)(((struct ipv6hdr *)(iph)) + 1);
205 protocol = ((struct ipv6hdr *)(iph))->nexthdr;
206 }
207 switch (protocol) {
208 case IPPROTO_ICMP:
209 return ntohs(((struct icmphdr *)nexthdr)->un.echo.sequence);
210 case IPPROTO_ICMPV6:
211 return ntohs(((struct icmp6hdr *)nexthdr)->icmp6_sequence);
212 case IPPROTO_TCP:
213 return ntohl(((struct tcphdr *)nexthdr)->seq);
214 case IPPROTO_UDP:
215 return ntohs(((struct udphdr *)nexthdr)->source);
216 default:
217 return 0;
218 }
219 }
220 #endif /*TRACEPOINTS_ENABLED*/
221
__esp_seq(struct sk_buff * skb)222 static u64 __esp_seq(struct sk_buff *skb)
223 {
224 u64 seq = ntohl(XFRM_SKB_CB(skb)->seq.input.low);
225
226 return seq | (u64)ntohl(XFRM_SKB_CB(skb)->seq.input.hi) << 32;
227 }
228
229 /* ======================= */
230 /* IPTFS SK_BUFF Functions */
231 /* ======================= */
232
233 /**
234 * iptfs_alloc_skb() - Allocate a new `skb`.
235 * @tpl: the skb to copy required meta-data from.
236 * @len: the linear length of the head data, zero is fine.
237 * @l3resv: true if skb reserve needs to support pushing L3 headers
238 *
239 * A new `skb` is allocated and required meta-data is copied from `tpl`, the
240 * head data is sized to `len` + reserved space set according to the @l3resv
241 * boolean.
242 *
243 * When @l3resv is false, resv is XFRM_IPTFS_MIN_L2HEADROOM which arranges for
244 * `skb->data - 16` which is a good guess for good cache alignment (placing the
245 * to be pushed L2 header at the start of a cacheline.
246 *
247 * Otherwise, @l3resv is true and resv is set to the correct reserved space for
248 * dst->dev plus the calculated L3 overhead for the xfrm dst or
249 * XFRM_IPTFS_MIN_L3HEADROOM whichever is larger. This is then cache aligned so
250 * that all the headers will commonly fall in a cacheline when possible.
251 *
252 * l3resv=true is used on tunnel ingress (tx), because we need to reserve for
253 * the new IPTFS packet (i.e., L2+L3 headers). On tunnel egress (rx) the data
254 * being copied into the skb includes the user L3 headers already so we only
255 * need to reserve for L2.
256 *
257 * Return: the new skb or NULL.
258 */
iptfs_alloc_skb(struct sk_buff * tpl,u32 len,bool l3resv)259 static struct sk_buff *iptfs_alloc_skb(struct sk_buff *tpl, u32 len, bool l3resv)
260 {
261 struct sk_buff *skb;
262 u32 resv;
263
264 if (!l3resv) {
265 resv = XFRM_IPTFS_MIN_L2HEADROOM;
266 } else {
267 struct dst_entry *dst = skb_dst(tpl);
268
269 resv = LL_RESERVED_SPACE(dst->dev) + dst->header_len;
270 resv = max(resv, XFRM_IPTFS_MIN_L3HEADROOM);
271 resv = L1_CACHE_ALIGN(resv);
272 }
273
274 skb = alloc_skb(len + resv, GFP_ATOMIC | __GFP_NOWARN);
275 if (!skb)
276 return NULL;
277
278 skb_reserve(skb, resv);
279
280 if (!l3resv) {
281 /* xfrm_input resume needs dev and xfrm ext from tunnel pkt */
282 skb->dev = tpl->dev;
283 __skb_ext_copy(skb, tpl);
284 }
285
286 /* dropped by xfrm_input, used by xfrm_output */
287 skb_dst_copy(skb, tpl);
288
289 return skb;
290 }
291
292 /**
293 * iptfs_skb_head_to_frag() - initialize a skb_frag_t based on skb head data
294 * @skb: skb with the head data
295 * @frag: frag to initialize
296 */
iptfs_skb_head_to_frag(const struct sk_buff * skb,skb_frag_t * frag)297 static void iptfs_skb_head_to_frag(const struct sk_buff *skb, skb_frag_t *frag)
298 {
299 struct page *page = virt_to_head_page(skb->data);
300 unsigned char *addr = (unsigned char *)page_address(page);
301
302 skb_frag_fill_page_desc(frag, page, skb->data - addr, skb_headlen(skb));
303 }
304
305 /**
306 * struct iptfs_skb_frag_walk - use to track a walk through fragments
307 * @fragi: current fragment index
308 * @past: length of data in fragments before @fragi
309 * @total: length of data in all fragments
310 * @nr_frags: number of fragments present in array
311 * @initial_offset: the value passed in to skb_prepare_frag_walk()
312 * @frags: the page fragments inc. room for head page
313 * @pp_recycle: copy of skb->pp_recycle
314 */
315 struct iptfs_skb_frag_walk {
316 u32 fragi;
317 u32 past;
318 u32 total;
319 u32 nr_frags;
320 u32 initial_offset;
321 skb_frag_t frags[MAX_SKB_FRAGS + 1];
322 bool pp_recycle;
323 };
324
325 /**
326 * iptfs_skb_prepare_frag_walk() - initialize a frag walk over an skb.
327 * @skb: the skb to walk.
328 * @initial_offset: start the walk @initial_offset into the skb.
329 * @walk: the walk to initialize
330 *
331 * Future calls to skb_add_frags() will expect the @offset value to be at
332 * least @initial_offset large.
333 */
iptfs_skb_prepare_frag_walk(struct sk_buff * skb,u32 initial_offset,struct iptfs_skb_frag_walk * walk)334 static void iptfs_skb_prepare_frag_walk(struct sk_buff *skb, u32 initial_offset,
335 struct iptfs_skb_frag_walk *walk)
336 {
337 struct skb_shared_info *shinfo = skb_shinfo(skb);
338 skb_frag_t *frag, *from;
339 u32 i;
340
341 walk->initial_offset = initial_offset;
342 walk->fragi = 0;
343 walk->past = 0;
344 walk->total = 0;
345 walk->nr_frags = 0;
346 walk->pp_recycle = skb->pp_recycle;
347
348 if (skb->head_frag) {
349 if (initial_offset >= skb_headlen(skb)) {
350 initial_offset -= skb_headlen(skb);
351 } else {
352 frag = &walk->frags[walk->nr_frags++];
353 iptfs_skb_head_to_frag(skb, frag);
354 frag->offset += initial_offset;
355 frag->len -= initial_offset;
356 walk->total += frag->len;
357 initial_offset = 0;
358 }
359 } else {
360 initial_offset -= skb_headlen(skb);
361 }
362
363 for (i = 0; i < shinfo->nr_frags; i++) {
364 from = &shinfo->frags[i];
365 if (initial_offset >= from->len) {
366 initial_offset -= from->len;
367 continue;
368 }
369 frag = &walk->frags[walk->nr_frags++];
370 *frag = *from;
371 if (initial_offset) {
372 frag->offset += initial_offset;
373 frag->len -= initial_offset;
374 initial_offset = 0;
375 }
376 walk->total += frag->len;
377 }
378 }
379
iptfs_skb_reset_frag_walk(struct iptfs_skb_frag_walk * walk,u32 offset)380 static u32 iptfs_skb_reset_frag_walk(struct iptfs_skb_frag_walk *walk,
381 u32 offset)
382 {
383 /* Adjust offset to refer to internal walk values */
384 offset -= walk->initial_offset;
385
386 /* Get to the correct fragment for offset */
387 while (offset < walk->past) {
388 walk->past -= walk->frags[--walk->fragi].len;
389 if (offset >= walk->past)
390 break;
391 }
392 while (offset >= walk->past + walk->frags[walk->fragi].len)
393 walk->past += walk->frags[walk->fragi++].len;
394
395 /* offset now relative to this current frag */
396 offset -= walk->past;
397 return offset;
398 }
399
400 /**
401 * iptfs_skb_can_add_frags() - check if ok to add frags from walk to skb
402 * @skb: skb to check for adding frags to
403 * @walk: the walk that will be used as source for frags.
404 * @offset: offset from beginning of original skb to start from.
405 * @len: amount of data to add frag references to in @skb.
406 *
407 * Return: true if ok to add frags.
408 */
iptfs_skb_can_add_frags(const struct sk_buff * skb,struct iptfs_skb_frag_walk * walk,u32 offset,u32 len)409 static bool iptfs_skb_can_add_frags(const struct sk_buff *skb,
410 struct iptfs_skb_frag_walk *walk,
411 u32 offset, u32 len)
412 {
413 struct skb_shared_info *shinfo = skb_shinfo(skb);
414 u32 fragi, nr_frags, fraglen;
415
416 if (skb_has_frag_list(skb) || skb->pp_recycle != walk->pp_recycle)
417 return false;
418
419 /* Make offset relative to current frag after setting that */
420 offset = iptfs_skb_reset_frag_walk(walk, offset);
421
422 /* Verify we have array space for the fragments we need to add */
423 fragi = walk->fragi;
424 nr_frags = shinfo->nr_frags;
425 while (len && fragi < walk->nr_frags) {
426 skb_frag_t *frag = &walk->frags[fragi];
427
428 fraglen = frag->len;
429 if (offset) {
430 fraglen -= offset;
431 offset = 0;
432 }
433 if (++nr_frags > MAX_SKB_FRAGS)
434 return false;
435 if (len <= fraglen)
436 return true;
437 len -= fraglen;
438 fragi++;
439 }
440 /* We may not copy all @len but what we have will fit. */
441 return true;
442 }
443
444 /**
445 * iptfs_skb_add_frags() - add a range of fragment references into an skb
446 * @skb: skb to add references into
447 * @walk: the walk to add referenced fragments from.
448 * @offset: offset from beginning of original skb to start from.
449 * @len: amount of data to add frag references to in @skb.
450 *
451 * iptfs_skb_can_add_frags() should be called before this function to verify
452 * that the destination @skb is compatible with the walk and has space in the
453 * array for the to be added frag references.
454 *
455 * Return: The number of bytes not added to @skb b/c we reached the end of the
456 * walk before adding all of @len.
457 */
iptfs_skb_add_frags(struct sk_buff * skb,struct iptfs_skb_frag_walk * walk,u32 offset,u32 len)458 static int iptfs_skb_add_frags(struct sk_buff *skb,
459 struct iptfs_skb_frag_walk *walk, u32 offset,
460 u32 len)
461 {
462 struct skb_shared_info *shinfo = skb_shinfo(skb);
463 u32 fraglen;
464
465 if (!walk->nr_frags || offset >= walk->total + walk->initial_offset)
466 return len;
467
468 /* make offset relative to current frag after setting that */
469 offset = iptfs_skb_reset_frag_walk(walk, offset);
470
471 while (len && walk->fragi < walk->nr_frags) {
472 skb_frag_t *frag = &walk->frags[walk->fragi];
473 skb_frag_t *tofrag = &shinfo->frags[shinfo->nr_frags];
474
475 *tofrag = *frag;
476 if (offset) {
477 tofrag->offset += offset;
478 tofrag->len -= offset;
479 offset = 0;
480 }
481 __skb_frag_ref(tofrag);
482 shinfo->nr_frags++;
483 shinfo->flags |= SKBFL_SHARED_FRAG;
484
485 /* see if we are done */
486 fraglen = tofrag->len;
487 if (len < fraglen) {
488 tofrag->len = len;
489 skb->len += len;
490 skb->data_len += len;
491 return 0;
492 }
493 /* advance to next source fragment */
494 len -= fraglen; /* careful, use dst bv_len */
495 skb->len += fraglen; /* careful, " " " */
496 skb->data_len += fraglen; /* careful, " " " */
497 walk->past += frag->len; /* careful, use src bv_len */
498 walk->fragi++;
499 }
500 return len;
501 }
502
503 /* ================================== */
504 /* IPTFS Trace Event Definitions */
505 /* ================================== */
506
507 #define CREATE_TRACE_POINTS
508 #include "trace_iptfs.h"
509
510 /* ================================== */
511 /* IPTFS Receiving (egress) Functions */
512 /* ================================== */
513
514 /**
515 * iptfs_pskb_add_frags() - Create and add frags into a new sk_buff.
516 * @tpl: template to create new skb from.
517 * @walk: The source for fragments to add.
518 * @off: The offset into @walk to add frags from, also used with @st and
519 * @copy_len.
520 * @len: The length of data to add covering frags from @walk into @skb.
521 * This must be <= @skblen.
522 * @st: The sequence state to copy from into the new head skb.
523 * @copy_len: Copy @copy_len bytes from @st at offset @off into the new skb
524 * linear space.
525 *
526 * Create a new sk_buff `skb` using the template @tpl. Copy @copy_len bytes from
527 * @st into the new skb linear space, and then add shared fragments from the
528 * frag walk for the remaining @len of data (i.e., @len - @copy_len bytes).
529 *
530 * Return: The newly allocated sk_buff `skb` or NULL if an error occurs.
531 */
532 static struct sk_buff *
iptfs_pskb_add_frags(struct sk_buff * tpl,struct iptfs_skb_frag_walk * walk,u32 off,u32 len,struct skb_seq_state * st,u32 copy_len)533 iptfs_pskb_add_frags(struct sk_buff *tpl, struct iptfs_skb_frag_walk *walk,
534 u32 off, u32 len, struct skb_seq_state *st, u32 copy_len)
535 {
536 struct sk_buff *skb;
537
538 skb = iptfs_alloc_skb(tpl, copy_len, false);
539 if (!skb)
540 return NULL;
541
542 /* this should not normally be happening */
543 if (!iptfs_skb_can_add_frags(skb, walk, off + copy_len,
544 len - copy_len)) {
545 kfree_skb(skb);
546 return NULL;
547 }
548
549 if (copy_len &&
550 skb_copy_seq_read(st, off, skb_put(skb, copy_len), copy_len)) {
551 XFRM_INC_STATS(dev_net(st->root_skb->dev),
552 LINUX_MIB_XFRMINERROR);
553 kfree_skb(skb);
554 return NULL;
555 }
556
557 iptfs_skb_add_frags(skb, walk, off + copy_len, len - copy_len);
558 return skb;
559 }
560
561 /**
562 * iptfs_pskb_extract_seq() - Create and load data into a new sk_buff.
563 * @skblen: the total data size for `skb`.
564 * @st: The source for the rest of the data to copy into `skb`.
565 * @off: The offset into @st to copy data from.
566 * @len: The length of data to copy from @st into `skb`. This must be <=
567 * @skblen.
568 *
569 * Create a new sk_buff `skb` with @skblen of packet data space. If non-zero,
570 * copy @rlen bytes of @runt into `skb`. Then using seq functions copy @len
571 * bytes from @st into `skb` starting from @off.
572 *
573 * It is an error for @len to be greater than the amount of data left in @st.
574 *
575 * Return: The newly allocated sk_buff `skb` or NULL if an error occurs.
576 */
577 static struct sk_buff *
iptfs_pskb_extract_seq(u32 skblen,struct skb_seq_state * st,u32 off,int len)578 iptfs_pskb_extract_seq(u32 skblen, struct skb_seq_state *st, u32 off, int len)
579 {
580 struct sk_buff *skb = iptfs_alloc_skb(st->root_skb, skblen, false);
581
582 if (!skb)
583 return NULL;
584 if (skb_copy_seq_read(st, off, skb_put(skb, len), len)) {
585 XFRM_INC_STATS(dev_net(st->root_skb->dev), LINUX_MIB_XFRMINERROR);
586 kfree_skb(skb);
587 return NULL;
588 }
589 return skb;
590 }
591
592 /**
593 * iptfs_input_save_runt() - save data in xtfs runt space.
594 * @xtfs: xtfs state
595 * @seq: the current sequence
596 * @buf: packet data
597 * @len: length of packet data
598 *
599 * Save the small (`len`) start of a fragmented packet in `buf` in the xtfs data
600 * runt space.
601 */
iptfs_input_save_runt(struct xfrm_iptfs_data * xtfs,u64 seq,u8 * buf,int len)602 static void iptfs_input_save_runt(struct xfrm_iptfs_data *xtfs, u64 seq,
603 u8 *buf, int len)
604 {
605 memcpy(xtfs->ra_runt, buf, len);
606
607 xtfs->ra_runtlen = len;
608 xtfs->ra_wantseq = seq + 1;
609 }
610
611 /**
612 * __iptfs_iphlen() - return the v4/v6 header length using packet data.
613 * @data: pointer at octet with version nibble
614 *
615 * The version data has been checked to be valid (i.e., either 4 or 6).
616 *
617 * Return: the IP header size based on the IP version.
618 */
__iptfs_iphlen(u8 * data)619 static u32 __iptfs_iphlen(u8 *data)
620 {
621 struct iphdr *iph = (struct iphdr *)data;
622
623 if (iph->version == 0x4)
624 return sizeof(*iph);
625 return sizeof(struct ipv6hdr);
626 }
627
628 /**
629 * __iptfs_iplen() - return the v4/v6 length using packet data.
630 * @data: pointer to ip (v4/v6) packet header
631 *
632 * Grab the IPv4 or IPv6 length value in the start of the inner packet header
633 * pointed to by `data`. Assumes data len is enough for the length field only.
634 *
635 * The version data has been checked to be valid (i.e., either 4 or 6).
636 *
637 * Return: the length value.
638 */
__iptfs_iplen(u8 * data)639 static u32 __iptfs_iplen(u8 *data)
640 {
641 struct iphdr *iph = (struct iphdr *)data;
642
643 if (iph->version == 0x4)
644 return ntohs(iph->tot_len);
645 return ntohs(((struct ipv6hdr *)iph)->payload_len) +
646 sizeof(struct ipv6hdr);
647 }
648
649 /**
650 * iptfs_complete_inner_skb() - finish preparing the inner packet for gro recv.
651 * @x: xfrm state
652 * @skb: the inner packet
653 *
654 * Finish the standard xfrm processing on the inner packet prior to sending back
655 * through gro_cells_receive. We do this separately b/c we are building a list
656 * of packets in the hopes that one day a list will be taken by
657 * xfrm_input.
658 */
iptfs_complete_inner_skb(struct xfrm_state * x,struct sk_buff * skb)659 static void iptfs_complete_inner_skb(struct xfrm_state *x, struct sk_buff *skb)
660 {
661 skb_reset_network_header(skb);
662
663 /* The packet is going back through gro_cells_receive no need to
664 * set this.
665 */
666 skb_reset_transport_header(skb);
667
668 /* Packet already has checksum value set. */
669 skb->ip_summed = CHECKSUM_NONE;
670
671 /* Our skb will contain the header data copied when this outer packet
672 * which contained the start of this inner packet. This is true
673 * when we allocate a new skb as well as when we reuse the existing skb.
674 */
675 if (ip_hdr(skb)->version == 0x4) {
676 struct iphdr *iph = ip_hdr(skb);
677
678 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
679 ipv4_copy_dscp(XFRM_MODE_SKB_CB(skb)->tos, iph);
680 if (!(x->props.flags & XFRM_STATE_NOECN))
681 if (INET_ECN_is_ce(XFRM_MODE_SKB_CB(skb)->tos))
682 IP_ECN_set_ce(iph);
683
684 skb->protocol = htons(ETH_P_IP);
685 } else {
686 struct ipv6hdr *iph = ipv6_hdr(skb);
687
688 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
689 ipv6_copy_dscp(XFRM_MODE_SKB_CB(skb)->tos, iph);
690 if (!(x->props.flags & XFRM_STATE_NOECN))
691 if (INET_ECN_is_ce(XFRM_MODE_SKB_CB(skb)->tos))
692 IP6_ECN_set_ce(skb, iph);
693
694 skb->protocol = htons(ETH_P_IPV6);
695 }
696 }
697
__iptfs_reassem_done(struct xfrm_iptfs_data * xtfs,bool free)698 static void __iptfs_reassem_done(struct xfrm_iptfs_data *xtfs, bool free)
699 {
700 assert_spin_locked(&xtfs->drop_lock);
701
702 /* We don't care if it works locking takes care of things */
703 hrtimer_try_to_cancel(&xtfs->drop_timer);
704 if (free)
705 kfree_skb(xtfs->ra_newskb);
706 xtfs->ra_newskb = NULL;
707 }
708
709 /**
710 * iptfs_reassem_abort() - In-progress packet is aborted free the state.
711 * @xtfs: xtfs state
712 */
iptfs_reassem_abort(struct xfrm_iptfs_data * xtfs)713 static void iptfs_reassem_abort(struct xfrm_iptfs_data *xtfs)
714 {
715 __iptfs_reassem_done(xtfs, true);
716 }
717
718 /**
719 * iptfs_reassem_done() - In-progress packet is complete, clear the state.
720 * @xtfs: xtfs state
721 */
iptfs_reassem_done(struct xfrm_iptfs_data * xtfs)722 static void iptfs_reassem_done(struct xfrm_iptfs_data *xtfs)
723 {
724 __iptfs_reassem_done(xtfs, false);
725 }
726
727 /**
728 * iptfs_reassem_cont() - Continue the reassembly of an inner packets.
729 * @xtfs: xtfs state
730 * @seq: sequence of current packet
731 * @st: seq read stat for current packet
732 * @skb: current packet
733 * @data: offset into sequential packet data
734 * @blkoff: packet blkoff value
735 * @list: list of skbs to enqueue completed packet on
736 *
737 * Process an IPTFS payload that has a non-zero `blkoff` or when we are
738 * expecting the continuation b/c we have a runt or in-progress packet.
739 *
740 * Return: the new data offset to continue processing from.
741 */
iptfs_reassem_cont(struct xfrm_iptfs_data * xtfs,u64 seq,struct skb_seq_state * st,struct sk_buff * skb,u32 data,u32 blkoff,struct list_head * list)742 static u32 iptfs_reassem_cont(struct xfrm_iptfs_data *xtfs, u64 seq,
743 struct skb_seq_state *st, struct sk_buff *skb,
744 u32 data, u32 blkoff, struct list_head *list)
745 {
746 struct iptfs_skb_frag_walk _fragwalk;
747 struct iptfs_skb_frag_walk *fragwalk = NULL;
748 struct sk_buff *newskb = xtfs->ra_newskb;
749 u32 remaining = skb->len - data;
750 u32 runtlen = xtfs->ra_runtlen;
751 u32 copylen, fraglen, ipremain, iphlen, iphremain, rrem;
752
753 /* Handle packet fragment we aren't expecting */
754 if (!runtlen && !xtfs->ra_newskb)
755 return data + min(blkoff, remaining);
756
757 /* Important to remember that input to this function is an ordered
758 * packet stream (unless the user disabled the reorder window). Thus if
759 * we are waiting for, and expecting the next packet so we can continue
760 * assembly, a newer sequence number indicates older ones are not coming
761 * (or if they do should be ignored). Technically we can receive older
762 * ones when the reorder window is disabled; however, the user should
763 * have disabled fragmentation in this case, and regardless we don't
764 * deal with it.
765 *
766 * blkoff could be zero if the stream is messed up (or it's an all pad
767 * insertion) be careful to handle that case in each of the below
768 */
769
770 /* Too old case: This can happen when the reorder window is disabled so
771 * ordering isn't actually guaranteed.
772 */
773 if (seq < xtfs->ra_wantseq)
774 return data + remaining;
775
776 /* Too new case: We missed what we wanted cleanup. */
777 if (seq > xtfs->ra_wantseq) {
778 XFRM_INC_STATS(xs_net(xtfs->x), LINUX_MIB_XFRMINIPTFSERROR);
779 goto abandon;
780 }
781
782 if (blkoff == 0) {
783 if ((*skb->data & 0xF0) != 0) {
784 XFRM_INC_STATS(xs_net(xtfs->x),
785 LINUX_MIB_XFRMINIPTFSERROR);
786 goto abandon;
787 }
788 /* Handle all pad case, advance expected sequence number.
789 * (RFC 9347 S2.2.3)
790 */
791 xtfs->ra_wantseq++;
792 /* will end parsing */
793 return data + remaining;
794 }
795
796 if (runtlen) {
797 /* Regardless of what happens we're done with the runt */
798 xtfs->ra_runtlen = 0;
799
800 /* The start of this inner packet was at the very end of the last
801 * iptfs payload which didn't include enough for the ip header
802 * length field. We must have *at least* that now.
803 */
804 rrem = sizeof(xtfs->ra_runt) - runtlen;
805 if (remaining < rrem || blkoff < rrem) {
806 XFRM_INC_STATS(xs_net(xtfs->x),
807 LINUX_MIB_XFRMINIPTFSERROR);
808 goto abandon;
809 }
810
811 /* fill in the runt data */
812 if (skb_copy_seq_read(st, data, &xtfs->ra_runt[runtlen],
813 rrem)) {
814 XFRM_INC_STATS(xs_net(xtfs->x),
815 LINUX_MIB_XFRMINBUFFERERROR);
816 goto abandon;
817 }
818
819 /* We have enough data to get the ip length value now,
820 * allocate an in progress skb
821 */
822 ipremain = __iptfs_iplen(xtfs->ra_runt);
823 if (ipremain < sizeof(xtfs->ra_runt)) {
824 /* length has to be at least runtsize large */
825 XFRM_INC_STATS(xs_net(xtfs->x),
826 LINUX_MIB_XFRMINIPTFSERROR);
827 goto abandon;
828 }
829
830 /* For the runt case we don't attempt sharing currently. NOTE:
831 * Currently, this IPTFS implementation will not create runts.
832 */
833
834 newskb = iptfs_alloc_skb(skb, ipremain, false);
835 if (!newskb) {
836 XFRM_INC_STATS(xs_net(xtfs->x), LINUX_MIB_XFRMINERROR);
837 goto abandon;
838 }
839 xtfs->ra_newskb = newskb;
840
841 /* Copy the runt data into the buffer, but leave data
842 * pointers the same as normal non-runt case. The extra `rrem`
843 * recopied bytes are basically cacheline free. Allows using
844 * same logic below to complete.
845 */
846 memcpy(skb_put(newskb, runtlen), xtfs->ra_runt,
847 sizeof(xtfs->ra_runt));
848 }
849
850 /* Continue reassembling the packet */
851 ipremain = __iptfs_iplen(newskb->data);
852 iphlen = __iptfs_iphlen(newskb->data);
853
854 ipremain -= newskb->len;
855 if (blkoff < ipremain) {
856 /* Corrupt data, we don't have enough to complete the packet */
857 XFRM_INC_STATS(xs_net(xtfs->x), LINUX_MIB_XFRMINIPTFSERROR);
858 goto abandon;
859 }
860
861 /* We want the IP header in linear space */
862 if (newskb->len < iphlen) {
863 iphremain = iphlen - newskb->len;
864 if (blkoff < iphremain) {
865 XFRM_INC_STATS(xs_net(xtfs->x),
866 LINUX_MIB_XFRMINIPTFSERROR);
867 goto abandon;
868 }
869 fraglen = min(blkoff, remaining);
870 copylen = min(fraglen, iphremain);
871 if (skb_copy_seq_read(st, data, skb_put(newskb, copylen),
872 copylen)) {
873 XFRM_INC_STATS(xs_net(xtfs->x),
874 LINUX_MIB_XFRMINBUFFERERROR);
875 goto abandon;
876 }
877 /* this is a silly condition that might occur anyway */
878 if (copylen < iphremain) {
879 xtfs->ra_wantseq++;
880 return data + fraglen;
881 }
882 /* update data and things derived from it */
883 data += copylen;
884 blkoff -= copylen;
885 remaining -= copylen;
886 ipremain -= copylen;
887 }
888
889 fraglen = min(blkoff, remaining);
890 copylen = min(fraglen, ipremain);
891
892 /* If we may have the opportunity to share prepare a fragwalk. */
893 if (!skb_has_frag_list(skb) && !skb_has_frag_list(newskb) &&
894 (skb->head_frag || skb->len == skb->data_len) &&
895 skb->pp_recycle == newskb->pp_recycle) {
896 fragwalk = &_fragwalk;
897 iptfs_skb_prepare_frag_walk(skb, data, fragwalk);
898 }
899
900 /* Try share then copy. */
901 if (fragwalk &&
902 iptfs_skb_can_add_frags(newskb, fragwalk, data, copylen)) {
903 iptfs_skb_add_frags(newskb, fragwalk, data, copylen);
904 } else {
905 if (skb_linearize(newskb)) {
906 XFRM_INC_STATS(xs_net(xtfs->x),
907 LINUX_MIB_XFRMINBUFFERERROR);
908 goto abandon;
909 }
910
911 /* copy fragment data into newskb */
912 if (skb_copy_seq_read(st, data, skb_put(newskb, copylen),
913 copylen)) {
914 XFRM_INC_STATS(xs_net(xtfs->x),
915 LINUX_MIB_XFRMINBUFFERERROR);
916 goto abandon;
917 }
918 }
919
920 if (copylen < ipremain) {
921 xtfs->ra_wantseq++;
922 } else {
923 /* We are done with packet reassembly! */
924 iptfs_reassem_done(xtfs);
925 iptfs_complete_inner_skb(xtfs->x, newskb);
926 list_add_tail(&newskb->list, list);
927 }
928
929 /* will continue on to new data block or end */
930 return data + fraglen;
931
932 abandon:
933 if (xtfs->ra_newskb) {
934 iptfs_reassem_abort(xtfs);
935 } else {
936 xtfs->ra_runtlen = 0;
937 xtfs->ra_wantseq = 0;
938 }
939 /* skip past fragment, maybe to end */
940 return data + min(blkoff, remaining);
941 }
942
__input_process_payload(struct xfrm_state * x,u32 data,struct skb_seq_state * skbseq,struct list_head * sublist)943 static bool __input_process_payload(struct xfrm_state *x, u32 data,
944 struct skb_seq_state *skbseq,
945 struct list_head *sublist)
946 {
947 u8 hbytes[sizeof(struct ipv6hdr)];
948 struct iptfs_skb_frag_walk _fragwalk;
949 struct iptfs_skb_frag_walk *fragwalk = NULL;
950 struct sk_buff *defer, *first_skb, *next, *skb;
951 const unsigned char *old_mac;
952 struct xfrm_iptfs_data *xtfs;
953 struct iphdr *iph;
954 struct net *net;
955 u32 first_iplen, iphlen, iplen, remaining, tail;
956 u32 capturelen;
957 u64 seq;
958 bool first_skb_partial = false;
959
960 xtfs = x->mode_data;
961 net = xs_net(x);
962 skb = skbseq->root_skb;
963 first_skb = NULL;
964 defer = NULL;
965
966 seq = __esp_seq(skb);
967
968 /* Save the old mac header if set */
969 old_mac = skb_mac_header_was_set(skb) ? skb_mac_header(skb) : NULL;
970
971 /* New packets */
972
973 tail = skb->len;
974 while (data < tail) {
975 __be16 protocol = 0;
976
977 /* Gather information on the next data block.
978 * `data` points to the start of the data block.
979 */
980 remaining = tail - data;
981
982 /* try and copy enough bytes to read length from ipv4/ipv6 */
983 iphlen = min_t(u32, remaining, 6);
984 if (skb_copy_seq_read(skbseq, data, hbytes, iphlen)) {
985 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
986 goto done;
987 }
988
989 iph = (struct iphdr *)hbytes;
990 if (iph->version == 0x4) {
991 /* must have at least tot_len field present */
992 if (remaining < 4) {
993 /* save the bytes we have, advance data and exit */
994 iptfs_input_save_runt(xtfs, seq, hbytes,
995 remaining);
996 data += remaining;
997 break;
998 }
999
1000 iplen = be16_to_cpu(iph->tot_len);
1001 iphlen = iph->ihl << 2;
1002 if (iplen < iphlen || iphlen < sizeof(*iph)) {
1003 XFRM_INC_STATS(net,
1004 LINUX_MIB_XFRMINHDRERROR);
1005 goto done;
1006 }
1007 protocol = cpu_to_be16(ETH_P_IP);
1008 XFRM_MODE_SKB_CB(skbseq->root_skb)->tos = iph->tos;
1009 } else if (iph->version == 0x6) {
1010 /* must have at least payload_len field present */
1011 if (remaining < 6) {
1012 /* save the bytes we have, advance data and exit */
1013 iptfs_input_save_runt(xtfs, seq, hbytes,
1014 remaining);
1015 data += remaining;
1016 break;
1017 }
1018
1019 iplen = be16_to_cpu(((struct ipv6hdr *)hbytes)->payload_len);
1020 iplen += sizeof(struct ipv6hdr);
1021 iphlen = sizeof(struct ipv6hdr);
1022 protocol = cpu_to_be16(ETH_P_IPV6);
1023 XFRM_MODE_SKB_CB(skbseq->root_skb)->tos =
1024 ipv6_get_dsfield((struct ipv6hdr *)iph);
1025 } else if (iph->version == 0x0) {
1026 /* pad */
1027 data = tail;
1028 break;
1029 } else {
1030 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
1031 goto done;
1032 }
1033
1034 if (unlikely(skbseq->stepped_offset)) {
1035 /* We need to reset our seq read, it can't backup at
1036 * this point.
1037 */
1038 struct sk_buff *save = skbseq->root_skb;
1039
1040 skb_abort_seq_read(skbseq);
1041 skb_prepare_seq_read(save, data, tail, skbseq);
1042 }
1043
1044 if (first_skb) {
1045 skb = NULL;
1046 } else {
1047 first_skb = skb;
1048 first_iplen = iplen;
1049 fragwalk = NULL;
1050
1051 /* We are going to skip over `data` bytes to reach the
1052 * start of the IP header of `iphlen` len for `iplen`
1053 * inner packet.
1054 */
1055
1056 if (skb_has_frag_list(skb)) {
1057 defer = skb;
1058 skb = NULL;
1059 } else if (data + iphlen <= skb_headlen(skb) &&
1060 /* make sure our header is 32-bit aligned? */
1061 /* ((uintptr_t)(skb->data + data) & 0x3) == 0 && */
1062 skb_tailroom(skb) + tail - data >= iplen) {
1063 /* Reuse the received skb.
1064 *
1065 * We have enough headlen to pull past any
1066 * initial fragment data, leaving at least the
1067 * IP header in the linear buffer space.
1068 *
1069 * For linear buffer space we only require that
1070 * linear buffer space is large enough to
1071 * eventually hold the entire reassembled
1072 * packet (by including tailroom in the check).
1073 *
1074 * For non-linear tailroom is 0 and so we only
1075 * re-use if the entire packet is present
1076 * already.
1077 *
1078 * NOTE: there are many more options for
1079 * sharing, KISS for now. Also, this can produce
1080 * skb's with the IP header unaligned to 32
1081 * bits. If that ends up being a problem then a
1082 * check should be added to the conditional
1083 * above that the header lies on a 32-bit
1084 * boundary as well.
1085 */
1086 skb_pull(skb, data);
1087
1088 /* our range just changed */
1089 data = 0;
1090 tail = skb->len;
1091 remaining = skb->len;
1092
1093 skb->protocol = protocol;
1094 skb_mac_header_rebuild(skb);
1095 if (skb->mac_len)
1096 eth_hdr(skb)->h_proto = skb->protocol;
1097
1098 /* all pointers could be changed now reset walk */
1099 skb_abort_seq_read(skbseq);
1100 skb_prepare_seq_read(skb, data, tail, skbseq);
1101 } else if (skb->head_frag &&
1102 /* We have the IP header right now */
1103 remaining >= iphlen) {
1104 fragwalk = &_fragwalk;
1105 iptfs_skb_prepare_frag_walk(skb, data, fragwalk);
1106 defer = skb;
1107 skb = NULL;
1108 } else {
1109 /* We couldn't reuse the input skb so allocate a
1110 * new one.
1111 */
1112 defer = skb;
1113 skb = NULL;
1114 }
1115
1116 /* Don't trim `first_skb` until the end as we are
1117 * walking that data now.
1118 */
1119 }
1120
1121 capturelen = min(iplen, remaining);
1122 if (!skb) {
1123 if (!fragwalk ||
1124 /* Large enough to be worth sharing */
1125 iplen < IPTFS_PKT_SHARE_MIN ||
1126 /* Have IP header + some data to share. */
1127 capturelen <= iphlen ||
1128 /* Try creating skb and adding frags */
1129 !(skb = iptfs_pskb_add_frags(first_skb, fragwalk,
1130 data, capturelen,
1131 skbseq, iphlen))) {
1132 skb = iptfs_pskb_extract_seq(iplen, skbseq, data, capturelen);
1133 }
1134 if (!skb) {
1135 /* skip to next packet or done */
1136 data += capturelen;
1137 continue;
1138 }
1139
1140 skb->protocol = protocol;
1141 if (old_mac) {
1142 /* rebuild the mac header */
1143 skb_set_mac_header(skb, -first_skb->mac_len);
1144 memcpy(skb_mac_header(skb), old_mac, first_skb->mac_len);
1145 eth_hdr(skb)->h_proto = skb->protocol;
1146 }
1147 }
1148
1149 data += capturelen;
1150
1151 if (skb->len < iplen) {
1152 /* Start reassembly */
1153 spin_lock(&xtfs->drop_lock);
1154
1155 xtfs->ra_newskb = skb;
1156 xtfs->ra_wantseq = seq + 1;
1157 if (!hrtimer_is_queued(&xtfs->drop_timer)) {
1158 /* softirq blocked lest the timer fire and interrupt us */
1159 hrtimer_start(&xtfs->drop_timer,
1160 xtfs->drop_time_ns,
1161 IPTFS_HRTIMER_MODE);
1162 }
1163
1164 spin_unlock(&xtfs->drop_lock);
1165
1166 first_skb_partial = (first_skb == skb);
1167 break;
1168 }
1169
1170 iptfs_complete_inner_skb(x, skb);
1171 list_add_tail(&skb->list, sublist);
1172 }
1173
1174 if (data != tail)
1175 /* this should not happen from the above code */
1176 XFRM_INC_STATS(net, LINUX_MIB_XFRMINIPTFSERROR);
1177
1178 if (first_skb && first_iplen && !defer && !first_skb_partial) {
1179 /* first_skb is queued b/c !defer and not partial */
1180 if (pskb_trim(first_skb, first_iplen)) {
1181 /* error trimming */
1182 list_del(&first_skb->list);
1183 defer = first_skb;
1184 }
1185 first_skb->ip_summed = CHECKSUM_NONE;
1186 }
1187
1188 /* Send the packets! */
1189 list_for_each_entry_safe(skb, next, sublist, list) {
1190 skb_list_del_init(skb);
1191 if (xfrm_input(skb, 0, 0, -2))
1192 kfree_skb(skb);
1193 }
1194 done:
1195 skb = skbseq->root_skb;
1196 skb_abort_seq_read(skbseq);
1197
1198 if (defer) {
1199 consume_skb(defer);
1200 } else if (!first_skb) {
1201 /* skb is the original passed in skb, but we didn't get far
1202 * enough to process it as the first_skb, if we had it would
1203 * either be save in ra_newskb, trimmed and sent on as an skb or
1204 * placed in defer to be freed.
1205 */
1206 kfree_skb(skb);
1207 }
1208 return true;
1209 }
1210
1211 /**
1212 * iptfs_input_ordered() - handle next in order IPTFS payload.
1213 * @x: xfrm state
1214 * @skb: current packet
1215 *
1216 * Process the IPTFS payload in `skb` and consume it afterwards.
1217 */
iptfs_input_ordered(struct xfrm_state * x,struct sk_buff * skb)1218 static void iptfs_input_ordered(struct xfrm_state *x, struct sk_buff *skb)
1219 {
1220 struct ip_iptfs_cc_hdr iptcch;
1221 struct skb_seq_state skbseq;
1222 struct list_head sublist; /* rename this it's just a list */
1223 struct xfrm_iptfs_data *xtfs;
1224 struct ip_iptfs_hdr *ipth;
1225 struct net *net;
1226 u32 blkoff, data, remaining;
1227 bool consumed = false;
1228 u64 seq;
1229
1230 xtfs = x->mode_data;
1231 net = xs_net(x);
1232
1233 seq = __esp_seq(skb);
1234
1235 /* Large enough to hold both types of header */
1236 ipth = (struct ip_iptfs_hdr *)&iptcch;
1237
1238 skb_prepare_seq_read(skb, 0, skb->len, &skbseq);
1239
1240 /* Get the IPTFS header and validate it */
1241
1242 if (skb_copy_seq_read(&skbseq, 0, ipth, sizeof(*ipth))) {
1243 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
1244 goto done;
1245 }
1246 data = sizeof(*ipth);
1247
1248 trace_iptfs_egress_recv(skb, xtfs, be16_to_cpu(ipth->block_offset));
1249
1250 /* Set data past the basic header */
1251 if (ipth->subtype == IPTFS_SUBTYPE_CC) {
1252 /* Copy the rest of the CC header */
1253 remaining = sizeof(iptcch) - sizeof(*ipth);
1254 if (skb_copy_seq_read(&skbseq, data, ipth + 1, remaining)) {
1255 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
1256 goto done;
1257 }
1258 data += remaining;
1259 } else if (ipth->subtype != IPTFS_SUBTYPE_BASIC) {
1260 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
1261 goto done;
1262 }
1263
1264 if (ipth->flags != 0) {
1265 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
1266 goto done;
1267 }
1268
1269 INIT_LIST_HEAD(&sublist);
1270
1271 /* Handle fragment at start of payload, and/or waiting reassembly. */
1272
1273 blkoff = ntohs(ipth->block_offset);
1274 /* check before locking i.e., maybe */
1275 if (blkoff || xtfs->ra_runtlen || xtfs->ra_newskb) {
1276 spin_lock(&xtfs->drop_lock);
1277
1278 /* check again after lock */
1279 if (blkoff || xtfs->ra_runtlen || xtfs->ra_newskb) {
1280 data = iptfs_reassem_cont(xtfs, seq, &skbseq, skb, data,
1281 blkoff, &sublist);
1282 }
1283
1284 spin_unlock(&xtfs->drop_lock);
1285 }
1286
1287 /* New packets */
1288 consumed = __input_process_payload(x, data, &skbseq, &sublist);
1289 done:
1290 if (!consumed) {
1291 skb = skbseq.root_skb;
1292 skb_abort_seq_read(&skbseq);
1293 kfree_skb(skb);
1294 }
1295 }
1296
1297 /* ------------------------------- */
1298 /* Input (Egress) Re-ordering Code */
1299 /* ------------------------------- */
1300
__vec_shift(struct xfrm_iptfs_data * xtfs,u32 shift)1301 static void __vec_shift(struct xfrm_iptfs_data *xtfs, u32 shift)
1302 {
1303 u32 savedlen = xtfs->w_savedlen;
1304
1305 if (shift > savedlen)
1306 shift = savedlen;
1307 if (shift != savedlen)
1308 memcpy(xtfs->w_saved, xtfs->w_saved + shift,
1309 (savedlen - shift) * sizeof(*xtfs->w_saved));
1310 memset(xtfs->w_saved + savedlen - shift, 0,
1311 shift * sizeof(*xtfs->w_saved));
1312 xtfs->w_savedlen -= shift;
1313 }
1314
__reorder_past(struct xfrm_iptfs_data * xtfs,struct sk_buff * inskb,struct list_head * freelist)1315 static void __reorder_past(struct xfrm_iptfs_data *xtfs, struct sk_buff *inskb,
1316 struct list_head *freelist)
1317 {
1318 list_add_tail(&inskb->list, freelist);
1319 }
1320
__reorder_drop(struct xfrm_iptfs_data * xtfs,struct list_head * list)1321 static u32 __reorder_drop(struct xfrm_iptfs_data *xtfs, struct list_head *list)
1322
1323 {
1324 struct skb_wseq *s, *se;
1325 const u32 savedlen = xtfs->w_savedlen;
1326 time64_t now = ktime_get_raw_fast_ns();
1327 u32 count = 0;
1328 u32 scount = 0;
1329
1330 if (xtfs->w_saved[0].drop_time > now)
1331 goto set_timer;
1332
1333 ++xtfs->w_wantseq;
1334
1335 /* Keep flushing packets until we reach a drop time greater than now. */
1336 s = xtfs->w_saved;
1337 se = s + savedlen;
1338 do {
1339 /* Walking past empty slots until we reach a packet */
1340 for (; s < se && !s->skb; s++) {
1341 if (s->drop_time > now)
1342 goto outerdone;
1343 }
1344 /* Sending packets until we hit another empty slot. */
1345 for (; s < se && s->skb; scount++, s++)
1346 list_add_tail(&s->skb->list, list);
1347 } while (s < se);
1348 outerdone:
1349
1350 count = s - xtfs->w_saved;
1351 if (count) {
1352 xtfs->w_wantseq += count;
1353
1354 /* Shift handled slots plus final empty slot into slot 0. */
1355 __vec_shift(xtfs, count);
1356 }
1357
1358 if (xtfs->w_savedlen) {
1359 set_timer:
1360 /* Drifting is OK */
1361 hrtimer_start(&xtfs->drop_timer,
1362 xtfs->w_saved[0].drop_time - now,
1363 IPTFS_HRTIMER_MODE);
1364 }
1365 return scount;
1366 }
1367
__reorder_this(struct xfrm_iptfs_data * xtfs,struct sk_buff * inskb,struct list_head * list)1368 static void __reorder_this(struct xfrm_iptfs_data *xtfs, struct sk_buff *inskb,
1369 struct list_head *list)
1370 {
1371 struct skb_wseq *s, *se;
1372 const u32 savedlen = xtfs->w_savedlen;
1373 u32 count = 0;
1374
1375 /* Got what we wanted. */
1376 list_add_tail(&inskb->list, list);
1377 ++xtfs->w_wantseq;
1378 if (!savedlen)
1379 return;
1380
1381 /* Flush remaining consecutive packets. */
1382
1383 /* Keep sending until we hit another missed pkt. */
1384 for (s = xtfs->w_saved, se = s + savedlen; s < se && s->skb; s++)
1385 list_add_tail(&s->skb->list, list);
1386 count = s - xtfs->w_saved;
1387 if (count)
1388 xtfs->w_wantseq += count;
1389
1390 /* Shift handled slots plus final empty slot into slot 0. */
1391 __vec_shift(xtfs, count + 1);
1392 }
1393
1394 /* Set the slot's drop time and all the empty slots below it until reaching a
1395 * filled slot which will already be set.
1396 */
iptfs_set_window_drop_times(struct xfrm_iptfs_data * xtfs,int index)1397 static void iptfs_set_window_drop_times(struct xfrm_iptfs_data *xtfs, int index)
1398 {
1399 const u32 savedlen = xtfs->w_savedlen;
1400 struct skb_wseq *s = xtfs->w_saved;
1401 time64_t drop_time;
1402
1403 assert_spin_locked(&xtfs->drop_lock);
1404
1405 if (savedlen > index + 1) {
1406 /* we are below another, our drop time and the timer are already set */
1407 return;
1408 }
1409 /* we are the most future so get a new drop time. */
1410 drop_time = ktime_get_raw_fast_ns();
1411 drop_time += xtfs->drop_time_ns;
1412
1413 /* Walk back through the array setting drop times as we go */
1414 s[index].drop_time = drop_time;
1415 while (index-- > 0 && !s[index].skb)
1416 s[index].drop_time = drop_time;
1417
1418 /* If we walked all the way back, schedule the drop timer if needed */
1419 if (index == -1 && !hrtimer_is_queued(&xtfs->drop_timer))
1420 hrtimer_start(&xtfs->drop_timer, xtfs->drop_time_ns,
1421 IPTFS_HRTIMER_MODE);
1422 }
1423
__reorder_future_fits(struct xfrm_iptfs_data * xtfs,struct sk_buff * inskb,struct list_head * freelist)1424 static void __reorder_future_fits(struct xfrm_iptfs_data *xtfs,
1425 struct sk_buff *inskb,
1426 struct list_head *freelist)
1427 {
1428 const u64 inseq = __esp_seq(inskb);
1429 const u64 wantseq = xtfs->w_wantseq;
1430 const u64 distance = inseq - wantseq;
1431 const u32 savedlen = xtfs->w_savedlen;
1432 const u32 index = distance - 1;
1433
1434 /* Handle future sequence number received which fits in the window.
1435 *
1436 * We know we don't have the seq we want so we won't be able to flush
1437 * anything.
1438 */
1439
1440 /* slot count is 4, saved size is 3 savedlen is 2
1441 *
1442 * "window boundary" is based on the fixed window size
1443 * distance is also slot number
1444 * index is an array index (i.e., - 1 of slot)
1445 * : : - implicit NULL after array len
1446 *
1447 * +--------- used length (savedlen == 2)
1448 * | +----- array size (nslots - 1 == 3)
1449 * | | + window boundary (nslots == 4)
1450 * V V | V
1451 * |
1452 * 0 1 2 3 | slot number
1453 * --- 0 1 2 | array index
1454 * [-] [b] : :| array
1455 *
1456 * "2" "3" "4" *5*| seq numbers
1457 *
1458 * We receive seq number 5
1459 * distance == 3 [inseq(5) - w_wantseq(2)]
1460 * index == 2 [distance(6) - 1]
1461 */
1462
1463 if (xtfs->w_saved[index].skb) {
1464 /* a dup of a future */
1465 list_add_tail(&inskb->list, freelist);
1466 return;
1467 }
1468
1469 xtfs->w_saved[index].skb = inskb;
1470 xtfs->w_savedlen = max(savedlen, index + 1);
1471 iptfs_set_window_drop_times(xtfs, index);
1472 }
1473
__reorder_future_shifts(struct xfrm_iptfs_data * xtfs,struct sk_buff * inskb,struct list_head * list)1474 static void __reorder_future_shifts(struct xfrm_iptfs_data *xtfs,
1475 struct sk_buff *inskb,
1476 struct list_head *list)
1477 {
1478 const u32 nslots = xtfs->cfg.reorder_win_size + 1;
1479 const u64 inseq = __esp_seq(inskb);
1480 u32 savedlen = xtfs->w_savedlen;
1481 u64 wantseq = xtfs->w_wantseq;
1482 struct skb_wseq *wnext;
1483 struct sk_buff *slot0;
1484 u32 beyond, shifting, slot;
1485 u64 distance;
1486
1487 /* Handle future sequence number received.
1488 *
1489 * IMPORTANT: we are at least advancing w_wantseq (i.e., wantseq) by 1
1490 * b/c we are beyond the window boundary.
1491 *
1492 * We know we don't have the wantseq so that counts as a drop.
1493 */
1494
1495 /* example: slot count is 4, array size is 3 savedlen is 2, slot 0 is
1496 * the missing sequence number.
1497 *
1498 * the final slot at savedlen (index savedlen - 1) is always occupied.
1499 *
1500 * beyond is "beyond array size" not savedlen.
1501 *
1502 * +--------- array length (savedlen == 2)
1503 * | +----- array size (nslots - 1 == 3)
1504 * | | +- window boundary (nslots == 4)
1505 * V V |
1506 * |
1507 * 0 1 2 3 | slot number
1508 * --- 0 1 2 | array index
1509 * [b] [c] : :| array
1510 * |
1511 * "2" "3" "4" "5"|*6* seq numbers
1512 *
1513 * We receive seq number 6
1514 * distance == 4 [inseq(6) - w_wantseq(2)]
1515 * newslot == distance
1516 * index == 3 [distance(4) - 1]
1517 * beyond == 1 [newslot(4) - lastslot((nslots(4) - 1))]
1518 * shifting == 1 [min(savedlen(2), beyond(1)]
1519 * slot0_skb == [b], and should match w_wantseq
1520 *
1521 * +--- window boundary (nslots == 4)
1522 * 0 1 2 3 | 4 slot number
1523 * --- 0 1 2 | 3 array index
1524 * [b] : : : :| array
1525 * "2" "3" "4" "5" *6* seq numbers
1526 *
1527 * We receive seq number 6
1528 * distance == 4 [inseq(6) - w_wantseq(2)]
1529 * newslot == distance
1530 * index == 3 [distance(4) - 1]
1531 * beyond == 1 [newslot(4) - lastslot((nslots(4) - 1))]
1532 * shifting == 1 [min(savedlen(1), beyond(1)]
1533 * slot0_skb == [b] and should match w_wantseq
1534 *
1535 * +-- window boundary (nslots == 4)
1536 * 0 1 2 3 | 4 5 6 slot number
1537 * --- 0 1 2 | 3 4 5 array index
1538 * [-] [c] : :| array
1539 * "2" "3" "4" "5" "6" "7" *8* seq numbers
1540 *
1541 * savedlen = 2, beyond = 3
1542 * iter 1: slot0 == NULL, missed++, lastdrop = 2 (2+1-1), slot0 = [-]
1543 * iter 2: slot0 == NULL, missed++, lastdrop = 3 (2+2-1), slot0 = [c]
1544 * 2 < 3, extra = 1 (3-2), missed += extra, lastdrop = 4 (2+2+1-1)
1545 *
1546 * We receive seq number 8
1547 * distance == 6 [inseq(8) - w_wantseq(2)]
1548 * newslot == distance
1549 * index == 5 [distance(6) - 1]
1550 * beyond == 3 [newslot(6) - lastslot((nslots(4) - 1))]
1551 * shifting == 2 [min(savedlen(2), beyond(3)]
1552 *
1553 * slot0_skb == NULL changed from [b] when "savedlen < beyond" is true.
1554 */
1555
1556 /* Now send any packets that are being shifted out of saved, and account
1557 * for missing packets that are exiting the window as we shift it.
1558 */
1559
1560 distance = inseq - wantseq;
1561 beyond = distance - (nslots - 1);
1562
1563 /* If savedlen > beyond we are shifting some, else all. */
1564 shifting = min(savedlen, beyond);
1565
1566 /* slot0 is the buf that just shifted out and into slot0 */
1567 slot0 = NULL;
1568 wnext = xtfs->w_saved;
1569 for (slot = 1; slot <= shifting; slot++, wnext++) {
1570 /* handle what was in slot0 before we occupy it */
1571 if (slot0)
1572 list_add_tail(&slot0->list, list);
1573 slot0 = wnext->skb;
1574 wnext->skb = NULL;
1575 }
1576
1577 /* slot0 is now either NULL (in which case it's what we now are waiting
1578 * for, or a buf in which case we need to handle it like we received it;
1579 * however, we may be advancing past that buffer as well..
1580 */
1581
1582 /* Handle case where we need to shift more than we had saved, slot0 will
1583 * be NULL iff savedlen is 0, otherwise slot0 will always be
1584 * non-NULL b/c we shifted the final element, which is always set if
1585 * there is any saved, into slot0.
1586 */
1587 if (savedlen < beyond) {
1588 if (savedlen != 0)
1589 list_add_tail(&slot0->list, list);
1590 slot0 = NULL;
1591 /* slot0 has had an empty slot pushed into it */
1592 }
1593
1594 /* Remove the entries */
1595 __vec_shift(xtfs, beyond);
1596
1597 /* Advance want seq */
1598 xtfs->w_wantseq += beyond;
1599
1600 /* Process drops here when implementing congestion control */
1601
1602 /* We've shifted. plug the packet in at the end. */
1603 xtfs->w_savedlen = nslots - 1;
1604 xtfs->w_saved[xtfs->w_savedlen - 1].skb = inskb;
1605 iptfs_set_window_drop_times(xtfs, xtfs->w_savedlen - 1);
1606
1607 /* if we don't have a slot0 then we must wait for it */
1608 if (!slot0)
1609 return;
1610
1611 /* If slot0, seq must match new want seq */
1612
1613 /* slot0 is valid, treat like we received expected. */
1614 __reorder_this(xtfs, slot0, list);
1615 }
1616
1617 /* Receive a new packet into the reorder window. Return a list of ordered
1618 * packets from the window.
1619 */
iptfs_input_reorder(struct xfrm_iptfs_data * xtfs,struct sk_buff * inskb,struct list_head * list,struct list_head * freelist)1620 static void iptfs_input_reorder(struct xfrm_iptfs_data *xtfs,
1621 struct sk_buff *inskb, struct list_head *list,
1622 struct list_head *freelist)
1623 {
1624 const u32 nslots = xtfs->cfg.reorder_win_size + 1;
1625 u64 inseq = __esp_seq(inskb);
1626 u64 wantseq;
1627
1628 assert_spin_locked(&xtfs->drop_lock);
1629
1630 if (unlikely(!xtfs->w_seq_set)) {
1631 xtfs->w_seq_set = true;
1632 xtfs->w_wantseq = inseq;
1633 }
1634 wantseq = xtfs->w_wantseq;
1635
1636 if (likely(inseq == wantseq))
1637 __reorder_this(xtfs, inskb, list);
1638 else if (inseq < wantseq)
1639 __reorder_past(xtfs, inskb, freelist);
1640 else if ((inseq - wantseq) < nslots)
1641 __reorder_future_fits(xtfs, inskb, freelist);
1642 else
1643 __reorder_future_shifts(xtfs, inskb, list);
1644 }
1645
1646 /**
1647 * iptfs_drop_timer() - Handle drop timer expiry.
1648 * @me: the timer
1649 *
1650 * This is similar to our input function.
1651 *
1652 * The drop timer is set when we start an in progress reassembly, and also when
1653 * we save a future packet in the window saved array.
1654 *
1655 * NOTE packets in the save window are always newer WRT drop times as
1656 * they get further in the future. i.e. for:
1657 *
1658 * if slots (S0, S1, ... Sn) and `Dn` is the drop time for slot `Sn`,
1659 * then D(n-1) <= D(n).
1660 *
1661 * So, regardless of why the timer is firing we can always discard any inprogress
1662 * fragment; either it's the reassembly timer, or slot 0 is going to be
1663 * dropped as S0 must have the most recent drop time, and slot 0 holds the
1664 * continuation fragment of the in progress packet.
1665 *
1666 * Returns HRTIMER_NORESTART.
1667 */
iptfs_drop_timer(struct hrtimer * me)1668 static enum hrtimer_restart iptfs_drop_timer(struct hrtimer *me)
1669 {
1670 struct sk_buff *skb, *next;
1671 struct list_head list;
1672 struct xfrm_iptfs_data *xtfs;
1673 struct xfrm_state *x;
1674 u32 count;
1675
1676 xtfs = container_of(me, typeof(*xtfs), drop_timer);
1677 x = xtfs->x;
1678
1679 INIT_LIST_HEAD(&list);
1680
1681 spin_lock(&xtfs->drop_lock);
1682
1683 /* Drop any in progress packet */
1684 skb = xtfs->ra_newskb;
1685 xtfs->ra_newskb = NULL;
1686
1687 /* Now drop as many packets as we should from the reordering window
1688 * saved array
1689 */
1690 count = xtfs->w_savedlen ? __reorder_drop(xtfs, &list) : 0;
1691
1692 spin_unlock(&xtfs->drop_lock);
1693
1694 if (skb)
1695 kfree_skb_reason(skb, SKB_DROP_REASON_FRAG_REASM_TIMEOUT);
1696
1697 if (count) {
1698 list_for_each_entry_safe(skb, next, &list, list) {
1699 skb_list_del_init(skb);
1700 iptfs_input_ordered(x, skb);
1701 }
1702 }
1703
1704 return HRTIMER_NORESTART;
1705 }
1706
1707 /**
1708 * iptfs_input() - handle receipt of iptfs payload
1709 * @x: xfrm state
1710 * @skb: the packet
1711 *
1712 * We have an IPTFS payload order it if needed, then process newly in order
1713 * packets.
1714 *
1715 * Return: -EINPROGRESS to inform xfrm_input to stop processing the skb.
1716 */
iptfs_input(struct xfrm_state * x,struct sk_buff * skb)1717 static int iptfs_input(struct xfrm_state *x, struct sk_buff *skb)
1718 {
1719 struct list_head freelist, list;
1720 struct xfrm_iptfs_data *xtfs = x->mode_data;
1721 struct sk_buff *next;
1722
1723 /* Fast path for no reorder window. */
1724 if (xtfs->cfg.reorder_win_size == 0) {
1725 iptfs_input_ordered(x, skb);
1726 goto done;
1727 }
1728
1729 /* Fetch list of in-order packets from the reordering window as well as
1730 * a list of buffers we need to now free.
1731 */
1732 INIT_LIST_HEAD(&list);
1733 INIT_LIST_HEAD(&freelist);
1734
1735 spin_lock(&xtfs->drop_lock);
1736 iptfs_input_reorder(xtfs, skb, &list, &freelist);
1737 spin_unlock(&xtfs->drop_lock);
1738
1739 list_for_each_entry_safe(skb, next, &list, list) {
1740 skb_list_del_init(skb);
1741 iptfs_input_ordered(x, skb);
1742 }
1743
1744 list_for_each_entry_safe(skb, next, &freelist, list) {
1745 skb_list_del_init(skb);
1746 kfree_skb(skb);
1747 }
1748 done:
1749 /* We always have dealt with the input SKB, either we are re-using it,
1750 * or we have freed it. Return EINPROGRESS so that xfrm_input stops
1751 * processing it.
1752 */
1753 return -EINPROGRESS;
1754 }
1755
1756 /* ================================= */
1757 /* IPTFS Sending (ingress) Functions */
1758 /* ================================= */
1759
1760 /* ------------------------- */
1761 /* Enqueue to send functions */
1762 /* ------------------------- */
1763
1764 /**
1765 * iptfs_enqueue() - enqueue packet if ok to send.
1766 * @xtfs: xtfs state
1767 * @skb: the packet
1768 *
1769 * Return: true if packet enqueued.
1770 */
iptfs_enqueue(struct xfrm_iptfs_data * xtfs,struct sk_buff * skb)1771 static bool iptfs_enqueue(struct xfrm_iptfs_data *xtfs, struct sk_buff *skb)
1772 {
1773 u64 newsz = xtfs->queue_size + skb->len;
1774 struct iphdr *iph;
1775
1776 assert_spin_locked(&xtfs->x->lock);
1777
1778 if (newsz > xtfs->cfg.max_queue_size)
1779 return false;
1780
1781 /* Set ECN CE if we are above our ECN queue threshold */
1782 if (newsz > xtfs->ecn_queue_size) {
1783 iph = ip_hdr(skb);
1784 if (iph->version == 4)
1785 IP_ECN_set_ce(iph);
1786 else if (iph->version == 6)
1787 IP6_ECN_set_ce(skb, ipv6_hdr(skb));
1788 }
1789
1790 __skb_queue_tail(&xtfs->queue, skb);
1791 xtfs->queue_size += skb->len;
1792 return true;
1793 }
1794
iptfs_get_cur_pmtu(struct xfrm_state * x,struct xfrm_iptfs_data * xtfs,struct sk_buff * skb)1795 static int iptfs_get_cur_pmtu(struct xfrm_state *x, struct xfrm_iptfs_data *xtfs,
1796 struct sk_buff *skb)
1797 {
1798 struct xfrm_dst *xdst = (struct xfrm_dst *)skb_dst(skb);
1799 u32 payload_mtu = xtfs->payload_mtu;
1800 u32 pmtu = __iptfs_get_inner_mtu(x, xdst->child_mtu_cached);
1801
1802 if (payload_mtu && payload_mtu < pmtu)
1803 pmtu = payload_mtu;
1804
1805 return pmtu;
1806 }
1807
iptfs_is_too_big(struct sock * sk,struct sk_buff * skb,u32 pmtu)1808 static int iptfs_is_too_big(struct sock *sk, struct sk_buff *skb, u32 pmtu)
1809 {
1810 if (skb->len <= pmtu)
1811 return 0;
1812
1813 /* We only send ICMP too big if the user has configured us as
1814 * dont-fragment.
1815 */
1816 if (skb->dev)
1817 XFRM_INC_STATS(dev_net(skb->dev), LINUX_MIB_XFRMOUTERROR);
1818
1819 if (sk)
1820 xfrm_local_error(skb, pmtu);
1821 else if (ip_hdr(skb)->version == 4)
1822 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(pmtu));
1823 else
1824 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, pmtu);
1825
1826 return 1;
1827 }
1828
1829 /* IPv4/IPv6 packet ingress to IPTFS tunnel, arrange to send in IPTFS payload
1830 * (i.e., aggregating or fragmenting as appropriate).
1831 * This is set in dst->output for an SA.
1832 */
iptfs_output_collect(struct net * net,struct sock * sk,struct sk_buff * skb)1833 static int iptfs_output_collect(struct net *net, struct sock *sk, struct sk_buff *skb)
1834 {
1835 struct dst_entry *dst = skb_dst(skb);
1836 struct xfrm_state *x = dst->xfrm;
1837 struct xfrm_iptfs_data *xtfs = x->mode_data;
1838 struct sk_buff *segs, *nskb;
1839 u32 pmtu = 0;
1840 bool ok = true;
1841 bool was_gso;
1842
1843 /* We have hooked into dst_entry->output which means we have skipped the
1844 * protocol specific netfilter (see xfrm4_output, xfrm6_output).
1845 * when our timer runs we will end up calling xfrm_output directly on
1846 * the encapsulated traffic.
1847 *
1848 * For both cases this is the NF_INET_POST_ROUTING hook which allows
1849 * changing the skb->dst entry which then may not be xfrm based anymore
1850 * in which case a REROUTED flag is set. and dst_output is called.
1851 *
1852 * For IPv6 we are also skipping fragmentation handling for local
1853 * sockets, which may or may not be good depending on our tunnel DF
1854 * setting. Normally with fragmentation supported we want to skip this
1855 * fragmentation.
1856 */
1857
1858 if (xtfs->cfg.dont_frag)
1859 pmtu = iptfs_get_cur_pmtu(x, xtfs, skb);
1860
1861 /* Break apart GSO skbs. If the queue is nearing full then we want the
1862 * accounting and queuing to be based on the individual packets not on the
1863 * aggregate GSO buffer.
1864 */
1865 was_gso = skb_is_gso(skb);
1866 if (!was_gso) {
1867 segs = skb;
1868 } else {
1869 segs = skb_gso_segment(skb, 0);
1870 if (IS_ERR_OR_NULL(segs)) {
1871 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
1872 kfree_skb(skb);
1873 if (IS_ERR(segs))
1874 return PTR_ERR(segs);
1875 return -EINVAL;
1876 }
1877 consume_skb(skb);
1878 skb = NULL;
1879 }
1880
1881 /* We can be running on multiple cores and from the network softirq or
1882 * from user context depending on where the packet is coming from.
1883 */
1884 spin_lock_bh(&x->lock);
1885
1886 skb_list_walk_safe(segs, skb, nskb) {
1887 skb_mark_not_on_list(skb);
1888
1889 /* Once we drop due to no queue space we continue to drop the
1890 * rest of the packets from that GRO.
1891 */
1892 if (!ok) {
1893 nospace:
1894 trace_iptfs_no_queue_space(skb, xtfs, pmtu, was_gso);
1895 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOQSPACE);
1896 kfree_skb_reason(skb, SKB_DROP_REASON_FULL_RING);
1897 continue;
1898 }
1899
1900 /* If the user indicated no iptfs fragmenting check before
1901 * enqueue.
1902 */
1903 if (xtfs->cfg.dont_frag && iptfs_is_too_big(sk, skb, pmtu)) {
1904 trace_iptfs_too_big(skb, xtfs, pmtu, was_gso);
1905 kfree_skb_reason(skb, SKB_DROP_REASON_PKT_TOO_BIG);
1906 continue;
1907 }
1908
1909 /* Enqueue to send in tunnel */
1910 ok = iptfs_enqueue(xtfs, skb);
1911 if (!ok)
1912 goto nospace;
1913
1914 trace_iptfs_enqueue(skb, xtfs, pmtu, was_gso);
1915 }
1916
1917 /* Start a delay timer if we don't have one yet */
1918 if (!hrtimer_is_queued(&xtfs->iptfs_timer)) {
1919 hrtimer_start(&xtfs->iptfs_timer, xtfs->init_delay_ns, IPTFS_HRTIMER_MODE);
1920 xtfs->iptfs_settime = ktime_get_raw_fast_ns();
1921 trace_iptfs_timer_start(xtfs, xtfs->init_delay_ns);
1922 }
1923
1924 spin_unlock_bh(&x->lock);
1925 return 0;
1926 }
1927
1928 /* -------------------------- */
1929 /* Dequeue and send functions */
1930 /* -------------------------- */
1931
iptfs_output_prepare_skb(struct sk_buff * skb,u32 blkoff)1932 static void iptfs_output_prepare_skb(struct sk_buff *skb, u32 blkoff)
1933 {
1934 struct ip_iptfs_hdr *h;
1935 size_t hsz = sizeof(*h);
1936
1937 /* now reset values to be pointing at the rest of the packets */
1938 h = skb_push(skb, hsz);
1939 memset(h, 0, hsz);
1940 if (blkoff)
1941 h->block_offset = htons(blkoff);
1942
1943 /* network_header current points at the inner IP packet
1944 * move it to the iptfs header
1945 */
1946 skb->transport_header = skb->network_header;
1947 skb->network_header -= hsz;
1948
1949 IPCB(skb)->flags |= IPSKB_XFRM_TUNNEL_SIZE;
1950 }
1951
1952 /**
1953 * iptfs_copy_create_frag() - create an inner fragment skb.
1954 * @st: The source packet data.
1955 * @offset: offset in @st of the new fragment data.
1956 * @copy_len: the amount of data to copy from @st.
1957 *
1958 * Create a new skb holding a single IPTFS inner packet fragment. @copy_len must
1959 * not be greater than the max fragment size.
1960 *
1961 * Return: the new fragment skb or an ERR_PTR().
1962 */
iptfs_copy_create_frag(struct skb_seq_state * st,u32 offset,u32 copy_len)1963 static struct sk_buff *iptfs_copy_create_frag(struct skb_seq_state *st, u32 offset, u32 copy_len)
1964 {
1965 struct sk_buff *src = st->root_skb;
1966 struct sk_buff *skb;
1967 int err;
1968
1969 skb = iptfs_alloc_skb(src, copy_len, true);
1970 if (!skb)
1971 return ERR_PTR(-ENOMEM);
1972
1973 /* Now copy `copy_len` data from src */
1974 err = skb_copy_seq_read(st, offset, skb_put(skb, copy_len), copy_len);
1975 if (err) {
1976 kfree_skb(skb);
1977 return ERR_PTR(err);
1978 }
1979
1980 return skb;
1981 }
1982
1983 /**
1984 * iptfs_copy_create_frags() - create and send N-1 fragments of a larger skb.
1985 * @skbp: the source packet skb (IN), skb holding the last fragment in
1986 * the fragment stream (OUT).
1987 * @xtfs: IPTFS SA state.
1988 * @mtu: the max IPTFS fragment size.
1989 *
1990 * This function is responsible for fragmenting a larger inner packet into a
1991 * sequence of IPTFS payload packets. The last fragment is returned rather than
1992 * being sent so that the caller can append more inner packets (aggregation) if
1993 * there is room.
1994 *
1995 * Return: 0 on success or a negative error code on failure
1996 */
iptfs_copy_create_frags(struct sk_buff ** skbp,struct xfrm_iptfs_data * xtfs,u32 mtu)1997 static int iptfs_copy_create_frags(struct sk_buff **skbp, struct xfrm_iptfs_data *xtfs, u32 mtu)
1998 {
1999 struct skb_seq_state skbseq;
2000 struct list_head sublist;
2001 struct sk_buff *skb = *skbp;
2002 struct sk_buff *nskb = *skbp;
2003 u32 copy_len, offset;
2004 u32 to_copy = skb->len - mtu;
2005 u32 blkoff = 0;
2006 int err = 0;
2007
2008 INIT_LIST_HEAD(&sublist);
2009
2010 skb_prepare_seq_read(skb, 0, skb->len, &skbseq);
2011
2012 /* A trimmed `skb` will be sent as the first fragment, later. */
2013 offset = mtu;
2014 to_copy = skb->len - offset;
2015 while (to_copy) {
2016 /* Send all but last fragment to allow agg. append */
2017 trace_iptfs_first_fragmenting(nskb, mtu, to_copy, NULL);
2018 list_add_tail(&nskb->list, &sublist);
2019
2020 /* FUTURE: if the packet has an odd/non-aligning length we could
2021 * send less data in the penultimate fragment so that the last
2022 * fragment then ends on an aligned boundary.
2023 */
2024 copy_len = min(to_copy, mtu);
2025 nskb = iptfs_copy_create_frag(&skbseq, offset, copy_len);
2026 if (IS_ERR(nskb)) {
2027 XFRM_INC_STATS(xs_net(xtfs->x), LINUX_MIB_XFRMOUTERROR);
2028 skb_abort_seq_read(&skbseq);
2029 err = PTR_ERR(nskb);
2030 nskb = NULL;
2031 break;
2032 }
2033 iptfs_output_prepare_skb(nskb, to_copy);
2034 offset += copy_len;
2035 to_copy -= copy_len;
2036 blkoff = to_copy;
2037 }
2038 skb_abort_seq_read(&skbseq);
2039
2040 /* return last fragment that will be unsent (or NULL) */
2041 *skbp = nskb;
2042 if (nskb)
2043 trace_iptfs_first_final_fragment(nskb, mtu, blkoff, NULL);
2044
2045 /* trim the original skb to MTU */
2046 if (!err)
2047 err = pskb_trim(skb, mtu);
2048
2049 if (err) {
2050 /* Free all frags. Don't bother sending a partial packet we will
2051 * never complete.
2052 */
2053 kfree_skb(nskb);
2054 list_for_each_entry_safe(skb, nskb, &sublist, list) {
2055 skb_list_del_init(skb);
2056 kfree_skb(skb);
2057 }
2058 return err;
2059 }
2060
2061 /* prepare the initial fragment with an iptfs header */
2062 iptfs_output_prepare_skb(skb, 0);
2063
2064 /* Send all but last fragment, if we fail to send a fragment then free
2065 * the rest -- no point in sending a packet that can't be reassembled.
2066 */
2067 list_for_each_entry_safe(skb, nskb, &sublist, list) {
2068 skb_list_del_init(skb);
2069 if (!err)
2070 err = xfrm_output(NULL, skb);
2071 else
2072 kfree_skb(skb);
2073 }
2074 if (err)
2075 kfree_skb(*skbp);
2076 return err;
2077 }
2078
2079 /**
2080 * iptfs_first_skb() - handle the first dequeued inner packet for output
2081 * @skbp: the source packet skb (IN), skb holding the last fragment in
2082 * the fragment stream (OUT).
2083 * @xtfs: IPTFS SA state.
2084 * @mtu: the max IPTFS fragment size.
2085 *
2086 * This function is responsible for fragmenting a larger inner packet into a
2087 * sequence of IPTFS payload packets.
2088 *
2089 * The last fragment is returned rather than being sent so that the caller can
2090 * append more inner packets (aggregation) if there is room.
2091 *
2092 * Return: 0 on success or a negative error code on failure
2093 */
iptfs_first_skb(struct sk_buff ** skbp,struct xfrm_iptfs_data * xtfs,u32 mtu)2094 static int iptfs_first_skb(struct sk_buff **skbp, struct xfrm_iptfs_data *xtfs, u32 mtu)
2095 {
2096 struct sk_buff *skb = *skbp;
2097 int err;
2098
2099 /* Classic ESP skips the don't fragment ICMP error if DF is clear on
2100 * the inner packet or ignore_df is set. Otherwise it will send an ICMP
2101 * or local error if the inner packet won't fit it's MTU.
2102 *
2103 * With IPTFS we do not care about the inner packet DF bit. If the
2104 * tunnel is configured to "don't fragment" we error back if things
2105 * don't fit in our max packet size. Otherwise we iptfs-fragment as
2106 * normal.
2107 */
2108
2109 /* The opportunity for HW offload has ended */
2110 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2111 err = skb_checksum_help(skb);
2112 if (err)
2113 return err;
2114 }
2115
2116 /* We've split gso up before queuing */
2117
2118 trace_iptfs_first_dequeue(skb, mtu, 0, ip_hdr(skb));
2119
2120 /* Consider the buffer Tx'd and no longer owned */
2121 skb_orphan(skb);
2122
2123 /* Simple case -- it fits. `mtu` accounted for all the overhead
2124 * including the basic IPTFS header.
2125 */
2126 if (skb->len <= mtu) {
2127 iptfs_output_prepare_skb(skb, 0);
2128 return 0;
2129 }
2130
2131 return iptfs_copy_create_frags(skbp, xtfs, mtu);
2132 }
2133
iptfs_rehome_fraglist(struct sk_buff ** nextp,struct sk_buff * child)2134 static struct sk_buff **iptfs_rehome_fraglist(struct sk_buff **nextp, struct sk_buff *child)
2135 {
2136 u32 fllen = 0;
2137
2138 /* It might be possible to account for a frag list in addition to page
2139 * fragment if it's a valid state to be in. The page fragments size
2140 * should be kept as data_len so only the frag_list size is removed,
2141 * this must be done above as well.
2142 */
2143 *nextp = skb_shinfo(child)->frag_list;
2144 while (*nextp) {
2145 fllen += (*nextp)->len;
2146 nextp = &(*nextp)->next;
2147 }
2148 skb_frag_list_init(child);
2149 child->len -= fllen;
2150 child->data_len -= fllen;
2151
2152 return nextp;
2153 }
2154
iptfs_consume_frags(struct sk_buff * to,struct sk_buff * from)2155 static void iptfs_consume_frags(struct sk_buff *to, struct sk_buff *from)
2156 {
2157 struct skb_shared_info *fromi = skb_shinfo(from);
2158 struct skb_shared_info *toi = skb_shinfo(to);
2159 unsigned int new_truesize;
2160
2161 /* If we have data in a head page, grab it */
2162 if (!skb_headlen(from)) {
2163 new_truesize = SKB_TRUESIZE(skb_end_offset(from));
2164 } else {
2165 iptfs_skb_head_to_frag(from, &toi->frags[toi->nr_frags]);
2166 skb_frag_ref(to, toi->nr_frags++);
2167 new_truesize = SKB_DATA_ALIGN(sizeof(struct sk_buff));
2168 }
2169
2170 /* Move any other page fragments rather than copy */
2171 memcpy(&toi->frags[toi->nr_frags], fromi->frags,
2172 sizeof(fromi->frags[0]) * fromi->nr_frags);
2173 toi->nr_frags += fromi->nr_frags;
2174 if (fromi->nr_frags)
2175 toi->flags |= fromi->flags & SKBFL_SHARED_FRAG;
2176 fromi->nr_frags = 0;
2177 from->data_len = 0;
2178 from->len = 0;
2179 to->truesize += from->truesize - new_truesize;
2180 from->truesize = new_truesize;
2181
2182 /* We are done with this SKB */
2183 consume_skb(from);
2184 }
2185
iptfs_output_queued(struct xfrm_state * x,struct sk_buff_head * list)2186 static void iptfs_output_queued(struct xfrm_state *x, struct sk_buff_head *list)
2187 {
2188 struct xfrm_iptfs_data *xtfs = x->mode_data;
2189 struct sk_buff *skb, *skb2, **nextp;
2190 struct skb_shared_info *shi, *shi2;
2191
2192 /* If we are fragmenting due to a large inner packet we will output all
2193 * the outer IPTFS packets required to contain the fragments of the
2194 * single large inner packet. These outer packets need to be sent
2195 * consecutively (ESP seq-wise). Since this output function is always
2196 * running from a timer we do not need a lock to provide this guarantee.
2197 * We will output our packets consecutively before the timer is allowed
2198 * to run again on some other CPU.
2199 */
2200
2201 while ((skb = __skb_dequeue(list))) {
2202 u32 mtu = iptfs_get_cur_pmtu(x, xtfs, skb);
2203 bool share_ok = true;
2204 int remaining;
2205
2206 /* protocol comes to us cleared sometimes */
2207 skb->protocol = x->outer_mode.family == AF_INET ? htons(ETH_P_IP) :
2208 htons(ETH_P_IPV6);
2209
2210 if (skb->len > mtu && xtfs->cfg.dont_frag) {
2211 /* We handle this case before enqueueing so we are only
2212 * here b/c MTU changed after we enqueued before we
2213 * dequeued, just drop these.
2214 */
2215 XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTERROR);
2216
2217 trace_iptfs_first_toobig(skb, mtu, 0, ip_hdr(skb));
2218 kfree_skb_reason(skb, SKB_DROP_REASON_PKT_TOO_BIG);
2219 continue;
2220 }
2221
2222 /* Convert first inner packet into an outer IPTFS packet,
2223 * dealing with any fragmentation into multiple outer packets
2224 * if necessary.
2225 */
2226 if (iptfs_first_skb(&skb, xtfs, mtu))
2227 continue;
2228
2229 /* If fragmentation was required the returned skb is the last
2230 * IPTFS fragment in the chain, and it's IPTFS header blkoff has
2231 * been set just past the end of the fragment data.
2232 *
2233 * In either case the space remaining to send more inner packet
2234 * data is `mtu` - (skb->len - sizeof iptfs header). This is b/c
2235 * the `mtu` value has the basic IPTFS header len accounted for,
2236 * and we added that header to the skb so it is a part of
2237 * skb->len, thus we subtract it from the skb length.
2238 */
2239 remaining = mtu - (skb->len - sizeof(struct ip_iptfs_hdr));
2240
2241 /* Re-home (un-nest) nested fragment lists. We need to do this
2242 * b/c we will simply be appending any following aggregated
2243 * inner packets using the frag list.
2244 */
2245 shi = skb_shinfo(skb);
2246 nextp = &shi->frag_list;
2247 while (*nextp) {
2248 if (skb_has_frag_list(*nextp))
2249 nextp = iptfs_rehome_fraglist(&(*nextp)->next, *nextp);
2250 else
2251 nextp = &(*nextp)->next;
2252 }
2253
2254 if (shi->frag_list || skb_cloned(skb) || skb_shared(skb))
2255 share_ok = false;
2256
2257 /* See if we have enough space to simply append.
2258 *
2259 * NOTE: Maybe do not append if we will be mis-aligned,
2260 * SW-based endpoints will probably have to copy in this
2261 * case.
2262 */
2263 while ((skb2 = skb_peek(list))) {
2264 trace_iptfs_ingress_nth_peek(skb2, remaining);
2265 if (skb2->len > remaining)
2266 break;
2267
2268 __skb_unlink(skb2, list);
2269
2270 /* Consider the buffer Tx'd and no longer owned */
2271 skb_orphan(skb);
2272
2273 /* If we don't have a cksum in the packet we need to add
2274 * one before encapsulation.
2275 */
2276 if (skb2->ip_summed == CHECKSUM_PARTIAL) {
2277 if (skb_checksum_help(skb2)) {
2278 XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTERROR);
2279 kfree_skb(skb2);
2280 continue;
2281 }
2282 }
2283
2284 /* skb->pp_recycle is passed to __skb_flag_unref for all
2285 * frag pages so we can only share pages with skb's who
2286 * match ourselves.
2287 */
2288 shi2 = skb_shinfo(skb2);
2289 if (share_ok &&
2290 (shi2->frag_list ||
2291 (!skb2->head_frag && skb_headlen(skb)) ||
2292 skb->pp_recycle != skb2->pp_recycle ||
2293 skb_zcopy(skb2) ||
2294 (shi->nr_frags + shi2->nr_frags + 1 > MAX_SKB_FRAGS)))
2295 share_ok = false;
2296
2297 /* Do accounting */
2298 skb->data_len += skb2->len;
2299 skb->len += skb2->len;
2300 remaining -= skb2->len;
2301
2302 trace_iptfs_ingress_nth_add(skb2, share_ok);
2303
2304 if (share_ok) {
2305 iptfs_consume_frags(skb, skb2);
2306 } else {
2307 /* Append to the frag_list */
2308 *nextp = skb2;
2309 nextp = &skb2->next;
2310 if (skb_has_frag_list(skb2))
2311 nextp = iptfs_rehome_fraglist(nextp,
2312 skb2);
2313 skb->truesize += skb2->truesize;
2314 }
2315 }
2316
2317 xfrm_output(NULL, skb);
2318 }
2319 }
2320
iptfs_delay_timer(struct hrtimer * me)2321 static enum hrtimer_restart iptfs_delay_timer(struct hrtimer *me)
2322 {
2323 struct sk_buff_head list;
2324 struct xfrm_iptfs_data *xtfs;
2325 struct xfrm_state *x;
2326 time64_t settime;
2327
2328 xtfs = container_of(me, typeof(*xtfs), iptfs_timer);
2329 x = xtfs->x;
2330
2331 /* Process all the queued packets
2332 *
2333 * softirq execution order: timer > tasklet > hrtimer
2334 *
2335 * Network rx will have run before us giving one last chance to queue
2336 * ingress packets for us to process and transmit.
2337 */
2338
2339 spin_lock(&x->lock);
2340 __skb_queue_head_init(&list);
2341 skb_queue_splice_init(&xtfs->queue, &list);
2342 xtfs->queue_size = 0;
2343 settime = xtfs->iptfs_settime;
2344 spin_unlock(&x->lock);
2345
2346 /* After the above unlock, packets can begin queuing again, and the
2347 * timer can be set again, from another CPU either in softirq or user
2348 * context (not from this one since we are running at softirq level
2349 * already).
2350 */
2351
2352 trace_iptfs_timer_expire(xtfs, (unsigned long long)(ktime_get_raw_fast_ns() - settime));
2353
2354 iptfs_output_queued(x, &list);
2355
2356 return HRTIMER_NORESTART;
2357 }
2358
2359 /**
2360 * iptfs_encap_add_ipv4() - add outer encaps
2361 * @x: xfrm state
2362 * @skb: the packet
2363 *
2364 * This was originally taken from xfrm4_tunnel_encap_add. The reason for the
2365 * copy is that IP-TFS/AGGFRAG can have different functionality for how to set
2366 * the TOS/DSCP bits. Sets the protocol to a different value and doesn't do
2367 * anything with inner headers as they aren't pointing into a normal IP
2368 * singleton inner packet.
2369 *
2370 * Return: 0 on success or a negative error code on failure
2371 */
iptfs_encap_add_ipv4(struct xfrm_state * x,struct sk_buff * skb)2372 static int iptfs_encap_add_ipv4(struct xfrm_state *x, struct sk_buff *skb)
2373 {
2374 struct dst_entry *dst = skb_dst(skb);
2375 struct iphdr *top_iph;
2376
2377 skb_reset_inner_network_header(skb);
2378 skb_reset_inner_transport_header(skb);
2379
2380 skb_set_network_header(skb, -(x->props.header_len - x->props.enc_hdr_len));
2381 skb->mac_header = skb->network_header + offsetof(struct iphdr, protocol);
2382 skb->transport_header = skb->network_header + sizeof(*top_iph);
2383
2384 top_iph = ip_hdr(skb);
2385 top_iph->ihl = 5;
2386 top_iph->version = 4;
2387 top_iph->protocol = IPPROTO_AGGFRAG;
2388
2389 /* As we have 0, fractional, 1 or N inner packets there's no obviously
2390 * correct DSCP mapping to inherit. ECN should be cleared per RFC9347
2391 * 3.1.
2392 */
2393 top_iph->tos = 0;
2394
2395 top_iph->frag_off = htons(IP_DF);
2396 top_iph->ttl = ip4_dst_hoplimit(xfrm_dst_child(dst));
2397 top_iph->saddr = x->props.saddr.a4;
2398 top_iph->daddr = x->id.daddr.a4;
2399 ip_select_ident(dev_net(dst->dev), skb, NULL);
2400
2401 return 0;
2402 }
2403
2404 #if IS_ENABLED(CONFIG_IPV6)
2405 /**
2406 * iptfs_encap_add_ipv6() - add outer encaps
2407 * @x: xfrm state
2408 * @skb: the packet
2409 *
2410 * This was originally taken from xfrm6_tunnel_encap_add. The reason for the
2411 * copy is that IP-TFS/AGGFRAG can have different functionality for how to set
2412 * the flow label and TOS/DSCP bits. It also sets the protocol to a different
2413 * value and doesn't do anything with inner headers as they aren't pointing into
2414 * a normal IP singleton inner packet.
2415 *
2416 * Return: 0 on success or a negative error code on failure
2417 */
iptfs_encap_add_ipv6(struct xfrm_state * x,struct sk_buff * skb)2418 static int iptfs_encap_add_ipv6(struct xfrm_state *x, struct sk_buff *skb)
2419 {
2420 struct dst_entry *dst = skb_dst(skb);
2421 struct ipv6hdr *top_iph;
2422 int dsfield;
2423
2424 skb_reset_inner_network_header(skb);
2425 skb_reset_inner_transport_header(skb);
2426
2427 skb_set_network_header(skb, -x->props.header_len + x->props.enc_hdr_len);
2428 skb->mac_header = skb->network_header + offsetof(struct ipv6hdr, nexthdr);
2429 skb->transport_header = skb->network_header + sizeof(*top_iph);
2430
2431 top_iph = ipv6_hdr(skb);
2432 top_iph->version = 6;
2433 top_iph->priority = 0;
2434 memset(top_iph->flow_lbl, 0, sizeof(top_iph->flow_lbl));
2435 top_iph->nexthdr = IPPROTO_AGGFRAG;
2436
2437 /* As we have 0, fractional, 1 or N inner packets there's no obviously
2438 * correct DSCP mapping to inherit. ECN should be cleared per RFC9347
2439 * 3.1.
2440 */
2441 dsfield = 0;
2442 ipv6_change_dsfield(top_iph, 0, dsfield);
2443
2444 top_iph->hop_limit = ip6_dst_hoplimit(xfrm_dst_child(dst));
2445 top_iph->saddr = *(struct in6_addr *)&x->props.saddr;
2446 top_iph->daddr = *(struct in6_addr *)&x->id.daddr;
2447
2448 return 0;
2449 }
2450 #endif
2451
2452 /**
2453 * iptfs_prepare_output() - prepare the skb for output
2454 * @x: xfrm state
2455 * @skb: the packet
2456 *
2457 * Return: Error value, if 0 then skb values should be as follows:
2458 * - transport_header should point at ESP header
2459 * - network_header should point at Outer IP header
2460 * - mac_header should point at protocol/nexthdr of the outer IP
2461 */
iptfs_prepare_output(struct xfrm_state * x,struct sk_buff * skb)2462 static int iptfs_prepare_output(struct xfrm_state *x, struct sk_buff *skb)
2463 {
2464 if (x->outer_mode.family == AF_INET)
2465 return iptfs_encap_add_ipv4(x, skb);
2466 if (x->outer_mode.family == AF_INET6) {
2467 #if IS_ENABLED(CONFIG_IPV6)
2468 return iptfs_encap_add_ipv6(x, skb);
2469 #else
2470 return -EAFNOSUPPORT;
2471 #endif
2472 }
2473 return -EOPNOTSUPP;
2474 }
2475
2476 /* ========================== */
2477 /* State Management Functions */
2478 /* ========================== */
2479
2480 /**
2481 * __iptfs_get_inner_mtu() - return inner MTU with no fragmentation.
2482 * @x: xfrm state.
2483 * @outer_mtu: the outer mtu
2484 *
2485 * Return: Correct MTU taking in to account the encap overhead.
2486 */
__iptfs_get_inner_mtu(struct xfrm_state * x,int outer_mtu)2487 static u32 __iptfs_get_inner_mtu(struct xfrm_state *x, int outer_mtu)
2488 {
2489 struct crypto_aead *aead;
2490 u32 blksize;
2491
2492 aead = x->data;
2493 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
2494 return ((outer_mtu - x->props.header_len - crypto_aead_authsize(aead)) &
2495 ~(blksize - 1)) - 2;
2496 }
2497
2498 /**
2499 * iptfs_get_inner_mtu() - return the inner MTU for an IPTFS xfrm.
2500 * @x: xfrm state.
2501 * @outer_mtu: Outer MTU for the encapsulated packet.
2502 *
2503 * Return: Correct MTU taking in to account the encap overhead.
2504 */
iptfs_get_inner_mtu(struct xfrm_state * x,int outer_mtu)2505 static u32 iptfs_get_inner_mtu(struct xfrm_state *x, int outer_mtu)
2506 {
2507 struct xfrm_iptfs_data *xtfs = x->mode_data;
2508
2509 /* If not dont-frag we have no MTU */
2510 if (!xtfs->cfg.dont_frag)
2511 return x->outer_mode.family == AF_INET ? IP_MAX_MTU : IP6_MAX_MTU;
2512 return __iptfs_get_inner_mtu(x, outer_mtu);
2513 }
2514
2515 /**
2516 * iptfs_user_init() - initialize the SA with IPTFS options from netlink.
2517 * @net: the net data
2518 * @x: xfrm state
2519 * @attrs: netlink attributes
2520 * @extack: extack return data
2521 *
2522 * Return: 0 on success or a negative error code on failure
2523 */
iptfs_user_init(struct net * net,struct xfrm_state * x,struct nlattr ** attrs,struct netlink_ext_ack * extack)2524 static int iptfs_user_init(struct net *net, struct xfrm_state *x,
2525 struct nlattr **attrs,
2526 struct netlink_ext_ack *extack)
2527 {
2528 struct xfrm_iptfs_data *xtfs = x->mode_data;
2529 struct xfrm_iptfs_config *xc;
2530 u64 q;
2531
2532 xc = &xtfs->cfg;
2533 xc->max_queue_size = IPTFS_DEFAULT_MAX_QUEUE_SIZE;
2534 xc->reorder_win_size = IPTFS_DEFAULT_REORDER_WINDOW;
2535 xtfs->drop_time_ns = IPTFS_DEFAULT_DROP_TIME_USECS * NSECS_IN_USEC;
2536 xtfs->init_delay_ns = IPTFS_DEFAULT_INIT_DELAY_USECS * NSECS_IN_USEC;
2537
2538 if (attrs[XFRMA_IPTFS_DONT_FRAG])
2539 xc->dont_frag = true;
2540 if (attrs[XFRMA_IPTFS_REORDER_WINDOW])
2541 xc->reorder_win_size =
2542 nla_get_u16(attrs[XFRMA_IPTFS_REORDER_WINDOW]);
2543 /* saved array is for saving 1..N seq nums from wantseq */
2544 if (xc->reorder_win_size) {
2545 xtfs->w_saved = kzalloc_objs(*xtfs->w_saved,
2546 xc->reorder_win_size);
2547 if (!xtfs->w_saved) {
2548 NL_SET_ERR_MSG(extack, "Cannot alloc reorder window");
2549 return -ENOMEM;
2550 }
2551 }
2552 if (attrs[XFRMA_IPTFS_PKT_SIZE]) {
2553 xc->pkt_size = nla_get_u32(attrs[XFRMA_IPTFS_PKT_SIZE]);
2554 if (!xc->pkt_size) {
2555 xtfs->payload_mtu = 0;
2556 } else if (xc->pkt_size > x->props.header_len) {
2557 xtfs->payload_mtu = xc->pkt_size - x->props.header_len;
2558 } else {
2559 NL_SET_ERR_MSG(extack,
2560 "Packet size must be 0 or greater than IPTFS/ESP header length");
2561 return -EINVAL;
2562 }
2563 }
2564 if (attrs[XFRMA_IPTFS_MAX_QSIZE])
2565 xc->max_queue_size = nla_get_u32(attrs[XFRMA_IPTFS_MAX_QSIZE]);
2566 if (attrs[XFRMA_IPTFS_DROP_TIME])
2567 xtfs->drop_time_ns =
2568 (u64)nla_get_u32(attrs[XFRMA_IPTFS_DROP_TIME]) *
2569 NSECS_IN_USEC;
2570 if (attrs[XFRMA_IPTFS_INIT_DELAY])
2571 xtfs->init_delay_ns =
2572 (u64)nla_get_u32(attrs[XFRMA_IPTFS_INIT_DELAY]) * NSECS_IN_USEC;
2573
2574 q = (u64)xc->max_queue_size * 95;
2575 do_div(q, 100);
2576 xtfs->ecn_queue_size = (u32)q;
2577
2578 return 0;
2579 }
2580
iptfs_sa_len(const struct xfrm_state * x)2581 static unsigned int iptfs_sa_len(const struct xfrm_state *x)
2582 {
2583 struct xfrm_iptfs_data *xtfs = x->mode_data;
2584 struct xfrm_iptfs_config *xc = &xtfs->cfg;
2585 unsigned int l = 0;
2586
2587 if (x->dir == XFRM_SA_DIR_IN) {
2588 l += nla_total_size(sizeof(u32)); /* drop time usec */
2589 l += nla_total_size(sizeof(xc->reorder_win_size));
2590 } else {
2591 if (xc->dont_frag)
2592 l += nla_total_size(0); /* dont-frag flag */
2593 l += nla_total_size(sizeof(u32)); /* init delay usec */
2594 l += nla_total_size(sizeof(xc->max_queue_size));
2595 l += nla_total_size(sizeof(xc->pkt_size));
2596 }
2597
2598 return l;
2599 }
2600
iptfs_copy_to_user(struct xfrm_state * x,struct sk_buff * skb)2601 static int iptfs_copy_to_user(struct xfrm_state *x, struct sk_buff *skb)
2602 {
2603 struct xfrm_iptfs_data *xtfs = x->mode_data;
2604 struct xfrm_iptfs_config *xc = &xtfs->cfg;
2605 int ret = 0;
2606 u64 q;
2607
2608 if (x->dir == XFRM_SA_DIR_IN) {
2609 q = xtfs->drop_time_ns;
2610 do_div(q, NSECS_IN_USEC);
2611 ret = nla_put_u32(skb, XFRMA_IPTFS_DROP_TIME, q);
2612 if (ret)
2613 return ret;
2614
2615 ret = nla_put_u16(skb, XFRMA_IPTFS_REORDER_WINDOW,
2616 xc->reorder_win_size);
2617 } else {
2618 if (xc->dont_frag) {
2619 ret = nla_put_flag(skb, XFRMA_IPTFS_DONT_FRAG);
2620 if (ret)
2621 return ret;
2622 }
2623
2624 q = xtfs->init_delay_ns;
2625 do_div(q, NSECS_IN_USEC);
2626 ret = nla_put_u32(skb, XFRMA_IPTFS_INIT_DELAY, q);
2627 if (ret)
2628 return ret;
2629
2630 ret = nla_put_u32(skb, XFRMA_IPTFS_MAX_QSIZE, xc->max_queue_size);
2631 if (ret)
2632 return ret;
2633
2634 ret = nla_put_u32(skb, XFRMA_IPTFS_PKT_SIZE, xc->pkt_size);
2635 }
2636
2637 return ret;
2638 }
2639
__iptfs_init_state(struct xfrm_state * x,struct xfrm_iptfs_data * xtfs)2640 static void __iptfs_init_state(struct xfrm_state *x,
2641 struct xfrm_iptfs_data *xtfs)
2642 {
2643 __skb_queue_head_init(&xtfs->queue);
2644 hrtimer_setup(&xtfs->iptfs_timer, iptfs_delay_timer, CLOCK_MONOTONIC, IPTFS_HRTIMER_MODE);
2645
2646 spin_lock_init(&xtfs->drop_lock);
2647 hrtimer_setup(&xtfs->drop_timer, iptfs_drop_timer, CLOCK_MONOTONIC, IPTFS_HRTIMER_MODE);
2648
2649 /* Modify type (esp) adjustment values */
2650
2651 if (x->props.family == AF_INET)
2652 x->props.header_len += sizeof(struct iphdr) + sizeof(struct ip_iptfs_hdr);
2653 else if (x->props.family == AF_INET6)
2654 x->props.header_len += sizeof(struct ipv6hdr) + sizeof(struct ip_iptfs_hdr);
2655 x->props.enc_hdr_len = sizeof(struct ip_iptfs_hdr);
2656
2657 /* Always keep a module reference when x->mode_data is set */
2658 if (x->mode_data != xtfs)
2659 __module_get(x->mode_cbs->owner);
2660
2661 x->mode_data = xtfs;
2662 xtfs->x = x;
2663 }
2664
iptfs_clone_state(struct xfrm_state * x,struct xfrm_state * orig)2665 static int iptfs_clone_state(struct xfrm_state *x, struct xfrm_state *orig)
2666 {
2667 struct skb_wseq *w_saved = NULL;
2668 struct xfrm_iptfs_data *xtfs;
2669
2670 xtfs = kmemdup(orig->mode_data, sizeof(*xtfs), GFP_KERNEL);
2671 if (!xtfs)
2672 return -ENOMEM;
2673
2674 if (xtfs->cfg.reorder_win_size) {
2675 w_saved = kzalloc_objs(*w_saved, xtfs->cfg.reorder_win_size);
2676 if (!w_saved) {
2677 kfree_sensitive(xtfs);
2678 return -ENOMEM;
2679 }
2680 }
2681 xtfs->w_saved = w_saved;
2682
2683 __skb_queue_head_init(&xtfs->queue);
2684 xtfs->queue_size = 0;
2685 hrtimer_setup(&xtfs->iptfs_timer, iptfs_delay_timer, CLOCK_MONOTONIC,
2686 IPTFS_HRTIMER_MODE);
2687
2688 spin_lock_init(&xtfs->drop_lock);
2689 hrtimer_setup(&xtfs->drop_timer, iptfs_drop_timer, CLOCK_MONOTONIC,
2690 IPTFS_HRTIMER_MODE);
2691
2692 xtfs->w_seq_set = false;
2693 xtfs->w_wantseq = 0;
2694 xtfs->w_savedlen = 0;
2695 xtfs->ra_newskb = NULL;
2696 xtfs->ra_wantseq = 0;
2697 xtfs->ra_runtlen = 0;
2698
2699 __module_get(x->mode_cbs->owner);
2700 x->mode_data = xtfs;
2701 xtfs->x = x;
2702
2703 return 0;
2704 }
2705
iptfs_init_state(struct xfrm_state * x)2706 static int iptfs_init_state(struct xfrm_state *x)
2707 {
2708 struct xfrm_iptfs_data *xtfs;
2709
2710 if (x->mode_data) {
2711 /* We have arrived here from xfrm_state_clone() */
2712 xtfs = x->mode_data;
2713 } else {
2714 xtfs = kzalloc_obj(*xtfs);
2715 if (!xtfs)
2716 return -ENOMEM;
2717 }
2718
2719 __iptfs_init_state(x, xtfs);
2720
2721 return 0;
2722 }
2723
iptfs_destroy_state(struct xfrm_state * x)2724 static void iptfs_destroy_state(struct xfrm_state *x)
2725 {
2726 struct xfrm_iptfs_data *xtfs = x->mode_data;
2727 struct sk_buff_head list;
2728 struct skb_wseq *s, *se;
2729 struct sk_buff *skb;
2730
2731 if (!xtfs)
2732 return;
2733
2734 hrtimer_cancel(&xtfs->iptfs_timer);
2735
2736 spin_lock_bh(&xtfs->x->lock);
2737 __skb_queue_head_init(&list);
2738 skb_queue_splice_init(&xtfs->queue, &list);
2739 spin_unlock_bh(&xtfs->x->lock);
2740
2741 while ((skb = __skb_dequeue(&list)))
2742 kfree_skb(skb);
2743
2744 hrtimer_cancel(&xtfs->drop_timer);
2745
2746 if (xtfs->ra_newskb)
2747 kfree_skb(xtfs->ra_newskb);
2748
2749 for (s = xtfs->w_saved, se = s + xtfs->w_savedlen; s < se; s++) {
2750 if (s->skb)
2751 kfree_skb(s->skb);
2752 }
2753
2754 kfree_sensitive(xtfs->w_saved);
2755 kfree_sensitive(xtfs);
2756
2757 module_put(x->mode_cbs->owner);
2758 }
2759
2760 static const struct xfrm_mode_cbs iptfs_mode_cbs = {
2761 .owner = THIS_MODULE,
2762 .init_state = iptfs_init_state,
2763 .clone_state = iptfs_clone_state,
2764 .destroy_state = iptfs_destroy_state,
2765 .user_init = iptfs_user_init,
2766 .copy_to_user = iptfs_copy_to_user,
2767 .sa_len = iptfs_sa_len,
2768 .get_inner_mtu = iptfs_get_inner_mtu,
2769 .input = iptfs_input,
2770 .output = iptfs_output_collect,
2771 .prepare_output = iptfs_prepare_output,
2772 };
2773
xfrm_iptfs_init(void)2774 static int __init xfrm_iptfs_init(void)
2775 {
2776 int err;
2777
2778 pr_info("xfrm_iptfs: IPsec IP-TFS tunnel mode module\n");
2779
2780 err = xfrm_register_mode_cbs(XFRM_MODE_IPTFS, &iptfs_mode_cbs);
2781 if (err < 0)
2782 pr_info("%s: can't register IP-TFS\n", __func__);
2783
2784 return err;
2785 }
2786
xfrm_iptfs_fini(void)2787 static void __exit xfrm_iptfs_fini(void)
2788 {
2789 xfrm_unregister_mode_cbs(XFRM_MODE_IPTFS);
2790 }
2791
2792 module_init(xfrm_iptfs_init);
2793 module_exit(xfrm_iptfs_fini);
2794 MODULE_LICENSE("GPL");
2795 MODULE_DESCRIPTION("IP-TFS support for xfrm ipsec tunnels");
2796