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