1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * xfrm_input.c 4 * 5 * Changes: 6 * YOSHIFUJI Hideaki @USAGI 7 * Split up af-specific portion 8 * 9 */ 10 11 #include <linux/bottom_half.h> 12 #include <linux/cache.h> 13 #include <linux/interrupt.h> 14 #include <linux/slab.h> 15 #include <linux/module.h> 16 #include <linux/netdevice.h> 17 #include <linux/percpu.h> 18 #include <net/dst.h> 19 #include <net/ip.h> 20 #include <net/xfrm.h> 21 #include <net/ip_tunnels.h> 22 #include <net/ip6_tunnel.h> 23 #include <net/dst_metadata.h> 24 #include <net/hotdata.h> 25 26 #include "xfrm_inout.h" 27 28 struct xfrm_trans_tasklet { 29 struct work_struct work; 30 spinlock_t queue_lock; 31 struct sk_buff_head queue; 32 }; 33 34 struct xfrm_trans_cb { 35 union { 36 struct inet_skb_parm h4; 37 #if IS_ENABLED(CONFIG_IPV6) 38 struct inet6_skb_parm h6; 39 #endif 40 } header; 41 int (*finish)(struct net *net, struct sock *sk, struct sk_buff *skb); 42 struct net *net; 43 }; 44 45 #define XFRM_TRANS_SKB_CB(__skb) ((struct xfrm_trans_cb *)&((__skb)->cb[0])) 46 47 static DEFINE_SPINLOCK(xfrm_input_afinfo_lock); 48 static struct xfrm_input_afinfo const __rcu *xfrm_input_afinfo[2][AF_INET6 + 1]; 49 50 static struct gro_cells gro_cells; 51 static struct net_device *xfrm_napi_dev; 52 53 static DEFINE_PER_CPU(struct xfrm_trans_tasklet, xfrm_trans_tasklet); 54 55 int xfrm_input_register_afinfo(const struct xfrm_input_afinfo *afinfo) 56 { 57 int err = 0; 58 59 if (WARN_ON(afinfo->family > AF_INET6)) 60 return -EAFNOSUPPORT; 61 62 spin_lock_bh(&xfrm_input_afinfo_lock); 63 if (unlikely(xfrm_input_afinfo[afinfo->is_ipip][afinfo->family])) 64 err = -EEXIST; 65 else 66 rcu_assign_pointer(xfrm_input_afinfo[afinfo->is_ipip][afinfo->family], afinfo); 67 spin_unlock_bh(&xfrm_input_afinfo_lock); 68 return err; 69 } 70 EXPORT_SYMBOL(xfrm_input_register_afinfo); 71 72 int xfrm_input_unregister_afinfo(const struct xfrm_input_afinfo *afinfo) 73 { 74 int err = 0; 75 76 spin_lock_bh(&xfrm_input_afinfo_lock); 77 if (likely(xfrm_input_afinfo[afinfo->is_ipip][afinfo->family])) { 78 const struct xfrm_input_afinfo *cur; 79 80 cur = rcu_access_pointer(xfrm_input_afinfo[afinfo->is_ipip][afinfo->family]); 81 if (unlikely(cur != afinfo)) 82 err = -EINVAL; 83 else 84 RCU_INIT_POINTER(xfrm_input_afinfo[afinfo->is_ipip][afinfo->family], NULL); 85 } 86 spin_unlock_bh(&xfrm_input_afinfo_lock); 87 synchronize_rcu(); 88 return err; 89 } 90 EXPORT_SYMBOL(xfrm_input_unregister_afinfo); 91 92 static const struct xfrm_input_afinfo *xfrm_input_get_afinfo(u8 family, bool is_ipip) 93 { 94 const struct xfrm_input_afinfo *afinfo; 95 96 if (WARN_ON_ONCE(family > AF_INET6)) 97 return NULL; 98 99 rcu_read_lock(); 100 afinfo = rcu_dereference(xfrm_input_afinfo[is_ipip][family]); 101 if (unlikely(!afinfo)) 102 rcu_read_unlock(); 103 return afinfo; 104 } 105 106 static int xfrm_rcv_cb(struct sk_buff *skb, unsigned int family, u8 protocol, 107 int err) 108 { 109 bool is_ipip = (protocol == IPPROTO_IPIP || protocol == IPPROTO_IPV6); 110 const struct xfrm_input_afinfo *afinfo; 111 int ret; 112 113 afinfo = xfrm_input_get_afinfo(family, is_ipip); 114 if (!afinfo) 115 return -EAFNOSUPPORT; 116 117 ret = afinfo->callback(skb, protocol, err); 118 rcu_read_unlock(); 119 120 return ret; 121 } 122 123 struct sec_path *secpath_set(struct sk_buff *skb) 124 { 125 struct sec_path *sp, *tmp = skb_ext_find(skb, SKB_EXT_SEC_PATH); 126 127 sp = skb_ext_add(skb, SKB_EXT_SEC_PATH); 128 if (!sp) 129 return NULL; 130 131 if (tmp) /* reused existing one (was COW'd if needed) */ 132 return sp; 133 134 /* allocated new secpath */ 135 memset(sp->ovec, 0, sizeof(sp->ovec)); 136 sp->olen = 0; 137 sp->len = 0; 138 sp->verified_cnt = 0; 139 140 return sp; 141 } 142 EXPORT_SYMBOL(secpath_set); 143 144 /* Fetch spi and seq from ipsec header */ 145 146 int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq) 147 { 148 int offset, offset_seq; 149 int hlen; 150 151 switch (nexthdr) { 152 case IPPROTO_AH: 153 hlen = sizeof(struct ip_auth_hdr); 154 offset = offsetof(struct ip_auth_hdr, spi); 155 offset_seq = offsetof(struct ip_auth_hdr, seq_no); 156 break; 157 case IPPROTO_ESP: 158 hlen = sizeof(struct ip_esp_hdr); 159 offset = offsetof(struct ip_esp_hdr, spi); 160 offset_seq = offsetof(struct ip_esp_hdr, seq_no); 161 break; 162 case IPPROTO_COMP: 163 if (!pskb_may_pull(skb, sizeof(struct ip_comp_hdr))) 164 return -EINVAL; 165 *spi = htonl(ntohs(*(__be16 *)(skb_transport_header(skb) + 2))); 166 *seq = 0; 167 return 0; 168 default: 169 return 1; 170 } 171 172 if (!pskb_may_pull(skb, hlen)) 173 return -EINVAL; 174 175 *spi = *(__be32 *)(skb_transport_header(skb) + offset); 176 *seq = *(__be32 *)(skb_transport_header(skb) + offset_seq); 177 return 0; 178 } 179 EXPORT_SYMBOL(xfrm_parse_spi); 180 181 static int xfrm4_remove_beet_encap(struct xfrm_state *x, struct sk_buff *skb) 182 { 183 struct iphdr *iph; 184 int optlen = 0; 185 int err = -EINVAL; 186 187 skb->protocol = htons(ETH_P_IP); 188 189 if (unlikely(XFRM_MODE_SKB_CB(skb)->protocol == IPPROTO_BEETPH)) { 190 struct ip_beet_phdr *ph; 191 int phlen; 192 193 if (!pskb_may_pull(skb, sizeof(*ph))) 194 goto out; 195 196 ph = (struct ip_beet_phdr *)skb->data; 197 198 phlen = sizeof(*ph) + ph->padlen; 199 optlen = ph->hdrlen * 8 + (IPV4_BEET_PHMAXLEN - phlen); 200 if (optlen < 0 || optlen & 3 || optlen > 250) 201 goto out; 202 203 XFRM_MODE_SKB_CB(skb)->protocol = ph->nexthdr; 204 205 if (!pskb_may_pull(skb, phlen)) 206 goto out; 207 __skb_pull(skb, phlen); 208 } 209 210 skb_push(skb, sizeof(*iph)); 211 skb_reset_network_header(skb); 212 skb_mac_header_rebuild(skb); 213 214 xfrm4_beet_make_header(skb); 215 216 iph = ip_hdr(skb); 217 218 iph->ihl += optlen / 4; 219 iph->tot_len = htons(skb->len); 220 iph->daddr = x->sel.daddr.a4; 221 iph->saddr = x->sel.saddr.a4; 222 iph->check = 0; 223 iph->check = ip_fast_csum(skb_network_header(skb), iph->ihl); 224 err = 0; 225 out: 226 return err; 227 } 228 229 static void ipip_ecn_decapsulate(struct sk_buff *skb) 230 { 231 struct iphdr *inner_iph = ipip_hdr(skb); 232 233 if (INET_ECN_is_ce(XFRM_MODE_SKB_CB(skb)->tos)) 234 IP_ECN_set_ce(inner_iph); 235 } 236 237 static int xfrm4_remove_tunnel_encap(struct xfrm_state *x, struct sk_buff *skb) 238 { 239 int err = -EINVAL; 240 241 skb->protocol = htons(ETH_P_IP); 242 243 if (!pskb_may_pull(skb, sizeof(struct iphdr))) 244 goto out; 245 246 err = skb_unclone(skb, GFP_ATOMIC); 247 if (err) 248 goto out; 249 250 if (x->props.flags & XFRM_STATE_DECAP_DSCP) 251 ipv4_copy_dscp(XFRM_MODE_SKB_CB(skb)->tos, ipip_hdr(skb)); 252 if (!(x->props.flags & XFRM_STATE_NOECN)) 253 ipip_ecn_decapsulate(skb); 254 255 skb_reset_network_header(skb); 256 skb_mac_header_rebuild(skb); 257 if (skb->mac_len) 258 eth_hdr(skb)->h_proto = skb->protocol; 259 260 err = 0; 261 262 out: 263 return err; 264 } 265 266 static void ipip6_ecn_decapsulate(struct sk_buff *skb) 267 { 268 struct ipv6hdr *inner_iph = ipipv6_hdr(skb); 269 270 if (INET_ECN_is_ce(XFRM_MODE_SKB_CB(skb)->tos)) 271 IP6_ECN_set_ce(skb, inner_iph); 272 } 273 274 static int xfrm6_remove_tunnel_encap(struct xfrm_state *x, struct sk_buff *skb) 275 { 276 int err = -EINVAL; 277 278 skb->protocol = htons(ETH_P_IPV6); 279 280 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr))) 281 goto out; 282 283 err = skb_unclone(skb, GFP_ATOMIC); 284 if (err) 285 goto out; 286 287 if (x->props.flags & XFRM_STATE_DECAP_DSCP) 288 ipv6_copy_dscp(XFRM_MODE_SKB_CB(skb)->tos, ipipv6_hdr(skb)); 289 if (!(x->props.flags & XFRM_STATE_NOECN)) 290 ipip6_ecn_decapsulate(skb); 291 292 skb_reset_network_header(skb); 293 skb_mac_header_rebuild(skb); 294 if (skb->mac_len) 295 eth_hdr(skb)->h_proto = skb->protocol; 296 297 err = 0; 298 299 out: 300 return err; 301 } 302 303 static int xfrm6_remove_beet_encap(struct xfrm_state *x, struct sk_buff *skb) 304 { 305 struct ipv6hdr *ip6h; 306 int size = sizeof(struct ipv6hdr); 307 int err; 308 309 skb->protocol = htons(ETH_P_IPV6); 310 311 err = skb_cow_head(skb, size + skb->mac_len); 312 if (err) 313 goto out; 314 315 __skb_push(skb, size); 316 skb_reset_network_header(skb); 317 skb_mac_header_rebuild(skb); 318 319 xfrm6_beet_make_header(skb); 320 321 ip6h = ipv6_hdr(skb); 322 ip6h->payload_len = htons(skb->len - size); 323 ip6h->daddr = x->sel.daddr.in6; 324 ip6h->saddr = x->sel.saddr.in6; 325 err = 0; 326 out: 327 return err; 328 } 329 330 /* Remove encapsulation header. 331 * 332 * The IP header will be moved over the top of the encapsulation 333 * header. 334 * 335 * On entry, the transport header shall point to where the IP header 336 * should be and the network header shall be set to where the IP 337 * header currently is. skb->data shall point to the start of the 338 * payload. 339 */ 340 static int 341 xfrm_inner_mode_encap_remove(struct xfrm_state *x, 342 struct sk_buff *skb) 343 { 344 switch (x->props.mode) { 345 case XFRM_MODE_BEET: 346 switch (x->sel.family) { 347 case AF_INET: 348 return xfrm4_remove_beet_encap(x, skb); 349 case AF_INET6: 350 return xfrm6_remove_beet_encap(x, skb); 351 } 352 break; 353 case XFRM_MODE_TUNNEL: 354 switch (XFRM_MODE_SKB_CB(skb)->protocol) { 355 case IPPROTO_IPIP: 356 return xfrm4_remove_tunnel_encap(x, skb); 357 case IPPROTO_IPV6: 358 return xfrm6_remove_tunnel_encap(x, skb); 359 break; 360 } 361 return -EINVAL; 362 } 363 364 WARN_ON_ONCE(1); 365 return -EOPNOTSUPP; 366 } 367 368 static int xfrm_prepare_input(struct xfrm_state *x, struct sk_buff *skb) 369 { 370 switch (x->props.family) { 371 case AF_INET: 372 xfrm4_extract_header(skb); 373 break; 374 case AF_INET6: 375 xfrm6_extract_header(skb); 376 break; 377 default: 378 WARN_ON_ONCE(1); 379 return -EAFNOSUPPORT; 380 } 381 382 return xfrm_inner_mode_encap_remove(x, skb); 383 } 384 385 /* Remove encapsulation header. 386 * 387 * The IP header will be moved over the top of the encapsulation header. 388 * 389 * On entry, skb_transport_header() shall point to where the IP header 390 * should be and skb_network_header() shall be set to where the IP header 391 * currently is. skb->data shall point to the start of the payload. 392 */ 393 static int xfrm4_transport_input(struct xfrm_state *x, struct sk_buff *skb) 394 { 395 struct xfrm_offload *xo = xfrm_offload(skb); 396 int ihl = skb->data - skb_transport_header(skb); 397 398 if (skb->transport_header != skb->network_header) { 399 memmove(skb_transport_header(skb), 400 skb_network_header(skb), ihl); 401 if (xo) 402 xo->orig_mac_len = 403 skb_mac_header_was_set(skb) ? skb_mac_header_len(skb) : 0; 404 skb->network_header = skb->transport_header; 405 } 406 ip_hdr(skb)->tot_len = htons(skb->len + ihl); 407 skb_reset_transport_header(skb); 408 return 0; 409 } 410 411 static int xfrm6_transport_input(struct xfrm_state *x, struct sk_buff *skb) 412 { 413 #if IS_ENABLED(CONFIG_IPV6) 414 struct xfrm_offload *xo = xfrm_offload(skb); 415 int ihl = skb->data - skb_transport_header(skb); 416 417 if (skb->transport_header != skb->network_header) { 418 memmove(skb_transport_header(skb), 419 skb_network_header(skb), ihl); 420 if (xo) 421 xo->orig_mac_len = 422 skb_mac_header_was_set(skb) ? skb_mac_header_len(skb) : 0; 423 skb->network_header = skb->transport_header; 424 } 425 ipv6_hdr(skb)->payload_len = htons(skb->len + ihl - 426 sizeof(struct ipv6hdr)); 427 skb_reset_transport_header(skb); 428 return 0; 429 #else 430 WARN_ON_ONCE(1); 431 return -EAFNOSUPPORT; 432 #endif 433 } 434 435 static int xfrm_inner_mode_input(struct xfrm_state *x, 436 struct sk_buff *skb) 437 { 438 switch (x->props.mode) { 439 case XFRM_MODE_BEET: 440 case XFRM_MODE_TUNNEL: 441 return xfrm_prepare_input(x, skb); 442 case XFRM_MODE_TRANSPORT: 443 if (x->props.family == AF_INET) 444 return xfrm4_transport_input(x, skb); 445 if (x->props.family == AF_INET6) 446 return xfrm6_transport_input(x, skb); 447 break; 448 case XFRM_MODE_ROUTEOPTIMIZATION: 449 WARN_ON_ONCE(1); 450 break; 451 default: 452 if (x->mode_cbs && x->mode_cbs->input) 453 return x->mode_cbs->input(x, skb); 454 455 WARN_ON_ONCE(1); 456 break; 457 } 458 459 return -EOPNOTSUPP; 460 } 461 462 /* NOTE: encap_type - In addition to the normal (non-negative) values for 463 * encap_type, a negative value of -1 or -2 can be used to resume/restart this 464 * function after a previous invocation early terminated for async operation. 465 */ 466 int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type) 467 { 468 const struct xfrm_state_afinfo *afinfo; 469 struct net *net = dev_net(skb->dev); 470 int err; 471 __be32 seq; 472 __be32 seq_hi; 473 struct xfrm_state *x = NULL; 474 xfrm_address_t *daddr; 475 u32 mark = skb->mark; 476 unsigned int family = AF_UNSPEC; 477 int decaps = 0; 478 int async = 0; 479 bool xfrm_gro = false; 480 bool crypto_done = false; 481 struct xfrm_offload *xo = xfrm_offload(skb); 482 struct sec_path *sp; 483 484 if (encap_type < 0 || (xo && (xo->flags & XFRM_GRO || encap_type == 0 || 485 encap_type == UDP_ENCAP_ESPINUDP))) { 486 x = xfrm_input_state(skb); 487 488 if (unlikely(x->km.state != XFRM_STATE_VALID)) { 489 if (x->km.state == XFRM_STATE_ACQ) 490 XFRM_INC_STATS(net, LINUX_MIB_XFRMACQUIREERROR); 491 else 492 XFRM_INC_STATS(net, 493 LINUX_MIB_XFRMINSTATEINVALID); 494 495 if (encap_type == -1) 496 dev_put(skb->dev); 497 goto drop; 498 } 499 500 family = x->props.family; 501 502 /* An encap_type of -2 indicates reconstructed inner packet */ 503 if (encap_type == -2) 504 goto resume_decapped; 505 506 /* An encap_type of -1 indicates async resumption. */ 507 if (encap_type == -1) { 508 async = 1; 509 dev_put(skb->dev); 510 seq = XFRM_SKB_CB(skb)->seq.input.low; 511 spin_lock(&x->lock); 512 goto resume; 513 } 514 /* GRO call */ 515 seq = XFRM_SPI_SKB_CB(skb)->seq; 516 517 if (xo && (xo->flags & CRYPTO_DONE)) { 518 crypto_done = true; 519 family = XFRM_SPI_SKB_CB(skb)->family; 520 521 if (!(xo->status & CRYPTO_SUCCESS)) { 522 if (xo->status & 523 (CRYPTO_TRANSPORT_AH_AUTH_FAILED | 524 CRYPTO_TRANSPORT_ESP_AUTH_FAILED | 525 CRYPTO_TUNNEL_AH_AUTH_FAILED | 526 CRYPTO_TUNNEL_ESP_AUTH_FAILED)) { 527 528 xfrm_audit_state_icvfail(x, skb, 529 x->type->proto); 530 x->stats.integrity_failed++; 531 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR); 532 goto drop; 533 } 534 535 if (xo->status & CRYPTO_INVALID_PROTOCOL) { 536 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR); 537 goto drop; 538 } 539 540 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR); 541 goto drop; 542 } 543 544 if (xfrm_parse_spi(skb, nexthdr, &spi, &seq)) { 545 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR); 546 goto drop; 547 } 548 549 nexthdr = x->type_offload->input_tail(x, skb); 550 } 551 552 goto process; 553 } 554 555 family = XFRM_SPI_SKB_CB(skb)->family; 556 557 /* if tunnel is present override skb->mark value with tunnel i_key */ 558 switch (family) { 559 case AF_INET: 560 if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4) 561 mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4->parms.i_key); 562 break; 563 case AF_INET6: 564 if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6) 565 mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6->parms.i_key); 566 break; 567 } 568 569 sp = secpath_set(skb); 570 if (!sp) { 571 XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR); 572 goto drop; 573 } 574 575 seq = 0; 576 if (!spi && xfrm_parse_spi(skb, nexthdr, &spi, &seq)) { 577 secpath_reset(skb); 578 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR); 579 goto drop; 580 } 581 582 daddr = (xfrm_address_t *)(skb_network_header(skb) + 583 XFRM_SPI_SKB_CB(skb)->daddroff); 584 do { 585 sp = skb_sec_path(skb); 586 587 if (sp->len == XFRM_MAX_DEPTH) { 588 secpath_reset(skb); 589 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR); 590 goto drop; 591 } 592 593 x = xfrm_input_state_lookup(net, mark, daddr, spi, nexthdr, family); 594 if (x == NULL) { 595 secpath_reset(skb); 596 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES); 597 xfrm_audit_state_notfound(skb, family, spi, seq); 598 goto drop; 599 } 600 601 if (unlikely(x->dir && x->dir != XFRM_SA_DIR_IN)) { 602 secpath_reset(skb); 603 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEDIRERROR); 604 xfrm_audit_state_notfound(skb, family, spi, seq); 605 xfrm_state_put(x); 606 x = NULL; 607 goto drop; 608 } 609 610 skb->mark = xfrm_smark_get(skb->mark, x); 611 612 sp->xvec[sp->len++] = x; 613 614 skb_dst_force(skb); 615 if (!skb_dst(skb)) { 616 XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR); 617 goto drop; 618 } 619 620 process: 621 seq_hi = htonl(xfrm_replay_seqhi(x, seq)); 622 623 XFRM_SKB_CB(skb)->seq.input.low = seq; 624 XFRM_SKB_CB(skb)->seq.input.hi = seq_hi; 625 626 spin_lock(&x->lock); 627 628 if (unlikely(x->km.state != XFRM_STATE_VALID)) { 629 if (x->km.state == XFRM_STATE_ACQ) 630 XFRM_INC_STATS(net, LINUX_MIB_XFRMACQUIREERROR); 631 else 632 XFRM_INC_STATS(net, 633 LINUX_MIB_XFRMINSTATEINVALID); 634 goto drop_unlock; 635 } 636 637 if ((x->encap ? x->encap->encap_type : 0) != encap_type) { 638 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH); 639 goto drop_unlock; 640 } 641 642 if (xfrm_replay_check(x, skb, seq)) { 643 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR); 644 goto drop_unlock; 645 } 646 647 if (xfrm_state_check_expire(x)) { 648 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEEXPIRED); 649 goto drop_unlock; 650 } 651 652 if (xfrm_tunnel_check(skb, x, family)) { 653 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR); 654 goto drop_unlock; 655 } 656 657 if (!crypto_done) { 658 spin_unlock(&x->lock); 659 dev_hold(skb->dev); 660 661 nexthdr = x->type->input(x, skb); 662 if (nexthdr == -EINPROGRESS) 663 return 0; 664 665 dev_put(skb->dev); 666 spin_lock(&x->lock); 667 } 668 resume: 669 if (nexthdr < 0) { 670 if (nexthdr == -EBADMSG) { 671 xfrm_audit_state_icvfail(x, skb, 672 x->type->proto); 673 x->stats.integrity_failed++; 674 } 675 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR); 676 goto drop_unlock; 677 } 678 679 /* only the first xfrm gets the encap type */ 680 encap_type = 0; 681 682 if (!crypto_done && xfrm_replay_recheck(x, skb, seq)) { 683 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR); 684 goto drop_unlock; 685 } 686 687 xfrm_replay_advance(x, seq); 688 689 x->curlft.bytes += skb->len; 690 x->curlft.packets++; 691 x->lastused = ktime_get_real_seconds(); 692 693 spin_unlock(&x->lock); 694 695 XFRM_MODE_SKB_CB(skb)->protocol = nexthdr; 696 697 err = xfrm_inner_mode_input(x, skb); 698 if (err == -EINPROGRESS) 699 return 0; 700 else if (err) { 701 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR); 702 goto drop; 703 } 704 resume_decapped: 705 if (x->outer_mode.flags & XFRM_MODE_FLAG_TUNNEL) { 706 decaps = 1; 707 break; 708 } 709 710 /* 711 * We need the inner address. However, we only get here for 712 * transport mode so the outer address is identical. 713 */ 714 daddr = &x->id.daddr; 715 family = x->props.family; 716 717 err = xfrm_parse_spi(skb, nexthdr, &spi, &seq); 718 if (err < 0) { 719 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR); 720 goto drop; 721 } 722 crypto_done = false; 723 } while (!err); 724 725 err = xfrm_rcv_cb(skb, family, x->type->proto, 0); 726 if (err) 727 goto drop; 728 729 nf_reset_ct(skb); 730 731 if (decaps) { 732 sp = skb_sec_path(skb); 733 if (sp) 734 sp->olen = 0; 735 if (skb_valid_dst(skb)) 736 skb_dst_drop(skb); 737 gro_cells_receive(&gro_cells, skb); 738 return 0; 739 } else { 740 xo = xfrm_offload(skb); 741 if (xo) 742 xfrm_gro = xo->flags & XFRM_GRO; 743 744 err = -EAFNOSUPPORT; 745 rcu_read_lock(); 746 afinfo = xfrm_state_afinfo_get_rcu(x->props.family); 747 if (likely(afinfo)) 748 err = afinfo->transport_finish(skb, xfrm_gro || async); 749 rcu_read_unlock(); 750 if (xfrm_gro) { 751 sp = skb_sec_path(skb); 752 if (sp) 753 sp->olen = 0; 754 if (skb_valid_dst(skb)) 755 skb_dst_drop(skb); 756 gro_cells_receive(&gro_cells, skb); 757 return err; 758 } 759 760 return err; 761 } 762 763 drop_unlock: 764 spin_unlock(&x->lock); 765 drop: 766 xfrm_rcv_cb(skb, family, x && x->type ? x->type->proto : nexthdr, -1); 767 kfree_skb(skb); 768 return 0; 769 } 770 EXPORT_SYMBOL(xfrm_input); 771 772 int xfrm_input_resume(struct sk_buff *skb, int nexthdr) 773 { 774 return xfrm_input(skb, nexthdr, 0, -1); 775 } 776 EXPORT_SYMBOL(xfrm_input_resume); 777 778 static void xfrm_trans_reinject(struct work_struct *work) 779 { 780 struct xfrm_trans_tasklet *trans = container_of(work, struct xfrm_trans_tasklet, work); 781 struct sk_buff_head queue; 782 struct sk_buff *skb; 783 784 __skb_queue_head_init(&queue); 785 spin_lock_bh(&trans->queue_lock); 786 skb_queue_splice_init(&trans->queue, &queue); 787 spin_unlock_bh(&trans->queue_lock); 788 789 local_bh_disable(); 790 while ((skb = __skb_dequeue(&queue))) 791 XFRM_TRANS_SKB_CB(skb)->finish(XFRM_TRANS_SKB_CB(skb)->net, 792 NULL, skb); 793 local_bh_enable(); 794 } 795 796 int xfrm_trans_queue_net(struct net *net, struct sk_buff *skb, 797 int (*finish)(struct net *, struct sock *, 798 struct sk_buff *)) 799 { 800 struct xfrm_trans_tasklet *trans; 801 802 trans = this_cpu_ptr(&xfrm_trans_tasklet); 803 804 if (skb_queue_len(&trans->queue) >= READ_ONCE(net_hotdata.max_backlog)) 805 return -ENOBUFS; 806 807 BUILD_BUG_ON(sizeof(struct xfrm_trans_cb) > sizeof(skb->cb)); 808 809 XFRM_TRANS_SKB_CB(skb)->finish = finish; 810 XFRM_TRANS_SKB_CB(skb)->net = net; 811 spin_lock_bh(&trans->queue_lock); 812 __skb_queue_tail(&trans->queue, skb); 813 spin_unlock_bh(&trans->queue_lock); 814 schedule_work(&trans->work); 815 return 0; 816 } 817 EXPORT_SYMBOL(xfrm_trans_queue_net); 818 819 int xfrm_trans_queue(struct sk_buff *skb, 820 int (*finish)(struct net *, struct sock *, 821 struct sk_buff *)) 822 { 823 return xfrm_trans_queue_net(dev_net(skb->dev), skb, finish); 824 } 825 EXPORT_SYMBOL(xfrm_trans_queue); 826 827 void __init xfrm_input_init(void) 828 { 829 int err; 830 int i; 831 832 xfrm_napi_dev = alloc_netdev_dummy(0); 833 if (!xfrm_napi_dev) 834 panic("Failed to allocate XFRM dummy netdev\n"); 835 836 err = gro_cells_init(&gro_cells, xfrm_napi_dev); 837 if (err) 838 gro_cells.cells = NULL; 839 840 for_each_possible_cpu(i) { 841 struct xfrm_trans_tasklet *trans; 842 843 trans = &per_cpu(xfrm_trans_tasklet, i); 844 spin_lock_init(&trans->queue_lock); 845 __skb_queue_head_init(&trans->queue); 846 INIT_WORK(&trans->work, xfrm_trans_reinject); 847 } 848 } 849