^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * xfrm_output.c - Common IPsec encapsulation code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/netfilter.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <net/dst.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <net/icmp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <net/inet_ecn.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <net/xfrm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <net/ip6_route.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <net/ipv6_stubs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include "xfrm_inout.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static int xfrm_output2(struct net *net, struct sock *sk, struct sk_buff *skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static int xfrm_inner_extract_output(struct xfrm_state *x, struct sk_buff *skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static int xfrm_skb_check_space(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct dst_entry *dst = skb_dst(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) int nhead = dst->header_len + LL_RESERVED_SPACE(dst->dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) - skb_headroom(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) int ntail = dst->dev->needed_tailroom - skb_tailroom(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) if (nhead <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (ntail <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) nhead = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) } else if (ntail < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) ntail = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return pskb_expand_head(skb, nhead, ntail, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /* Children define the path of the packet through the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * Linux networking. Thus, destinations are stackable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static struct dst_entry *skb_dst_pop(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct dst_entry *child = dst_clone(xfrm_dst_child(skb_dst(skb)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) skb_dst_drop(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /* Add encapsulation header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * The IP header will be moved forward to make space for the encapsulation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static int xfrm4_transport_output(struct xfrm_state *x, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct iphdr *iph = ip_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) int ihl = iph->ihl * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) skb_set_inner_transport_header(skb, skb_transport_offset(skb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) skb_set_network_header(skb, -x->props.header_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) skb->mac_header = skb->network_header +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) offsetof(struct iphdr, protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) skb->transport_header = skb->network_header + ihl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) __skb_pull(skb, ihl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) memmove(skb_network_header(skb), iph, ihl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) /* Add encapsulation header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * The IP header and mutable extension headers will be moved forward to make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * space for the encapsulation header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static int xfrm6_transport_output(struct xfrm_state *x, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct ipv6hdr *iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) u8 *prevhdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) int hdr_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) iph = ipv6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) skb_set_inner_transport_header(skb, skb_transport_offset(skb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) hdr_len = x->type->hdr_offset(x, skb, &prevhdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (hdr_len < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return hdr_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) skb_set_mac_header(skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) (prevhdr - x->props.header_len) - skb->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) skb_set_network_header(skb, -x->props.header_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) skb->transport_header = skb->network_header + hdr_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) __skb_pull(skb, hdr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) memmove(ipv6_hdr(skb), iph, hdr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) WARN_ON_ONCE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return -EAFNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) /* Add route optimization header space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * The IP header and mutable extension headers will be moved forward to make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * space for the route optimization header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static int xfrm6_ro_output(struct xfrm_state *x, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) struct ipv6hdr *iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) u8 *prevhdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) int hdr_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) iph = ipv6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) hdr_len = x->type->hdr_offset(x, skb, &prevhdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (hdr_len < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return hdr_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) skb_set_mac_header(skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) (prevhdr - x->props.header_len) - skb->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) skb_set_network_header(skb, -x->props.header_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) skb->transport_header = skb->network_header + hdr_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) __skb_pull(skb, hdr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) memmove(ipv6_hdr(skb), iph, hdr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) x->lastused = ktime_get_real_seconds();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) WARN_ON_ONCE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return -EAFNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /* Add encapsulation header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * The top IP header will be constructed per draft-nikander-esp-beet-mode-06.txt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) static int xfrm4_beet_encap_add(struct xfrm_state *x, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) struct ip_beet_phdr *ph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) struct iphdr *top_iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) int hdrlen, optlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) hdrlen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) optlen = XFRM_MODE_SKB_CB(skb)->optlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (unlikely(optlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) hdrlen += IPV4_BEET_PHMAXLEN - (optlen & 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) skb_set_network_header(skb, -x->props.header_len - hdrlen +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) (XFRM_MODE_SKB_CB(skb)->ihl - sizeof(*top_iph)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (x->sel.family != AF_INET6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) skb->network_header += IPV4_BEET_PHMAXLEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) skb->mac_header = skb->network_header +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) offsetof(struct iphdr, protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) skb->transport_header = skb->network_header + sizeof(*top_iph);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) xfrm4_beet_make_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) ph = __skb_pull(skb, XFRM_MODE_SKB_CB(skb)->ihl - hdrlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) top_iph = ip_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (unlikely(optlen)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (WARN_ON(optlen < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) ph->padlen = 4 - (optlen & 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) ph->hdrlen = optlen / 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) ph->nexthdr = top_iph->protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (ph->padlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) memset(ph + 1, IPOPT_NOP, ph->padlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) top_iph->protocol = IPPROTO_BEETPH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) top_iph->ihl = sizeof(struct iphdr) / 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) top_iph->saddr = x->props.saddr.a4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) top_iph->daddr = x->id.daddr.a4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) /* Add encapsulation header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * The top IP header will be constructed per RFC 2401.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static int xfrm4_tunnel_encap_add(struct xfrm_state *x, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) struct dst_entry *dst = skb_dst(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) struct iphdr *top_iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) int flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) skb_set_inner_network_header(skb, skb_network_offset(skb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) skb_set_inner_transport_header(skb, skb_transport_offset(skb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) skb_set_network_header(skb, -x->props.header_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) skb->mac_header = skb->network_header +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) offsetof(struct iphdr, protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) skb->transport_header = skb->network_header + sizeof(*top_iph);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) top_iph = ip_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) top_iph->ihl = 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) top_iph->version = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) top_iph->protocol = xfrm_af2proto(skb_dst(skb)->ops->family);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) /* DS disclosing depends on XFRM_SA_XFLAG_DONT_ENCAP_DSCP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (x->props.extra_flags & XFRM_SA_XFLAG_DONT_ENCAP_DSCP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) top_iph->tos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) top_iph->tos = XFRM_MODE_SKB_CB(skb)->tos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) top_iph->tos = INET_ECN_encapsulate(top_iph->tos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) XFRM_MODE_SKB_CB(skb)->tos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) flags = x->props.flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (flags & XFRM_STATE_NOECN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) IP_ECN_clear(top_iph);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) top_iph->frag_off = (flags & XFRM_STATE_NOPMTUDISC) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 0 : (XFRM_MODE_SKB_CB(skb)->frag_off & htons(IP_DF));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) top_iph->ttl = ip4_dst_hoplimit(xfrm_dst_child(dst));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) top_iph->saddr = x->props.saddr.a4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) top_iph->daddr = x->id.daddr.a4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) ip_select_ident(dev_net(dst->dev), skb, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) static int xfrm6_tunnel_encap_add(struct xfrm_state *x, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) struct dst_entry *dst = skb_dst(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) struct ipv6hdr *top_iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) int dsfield;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) skb_set_inner_network_header(skb, skb_network_offset(skb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) skb_set_inner_transport_header(skb, skb_transport_offset(skb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) skb_set_network_header(skb, -x->props.header_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) skb->mac_header = skb->network_header +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) offsetof(struct ipv6hdr, nexthdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) skb->transport_header = skb->network_header + sizeof(*top_iph);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) top_iph = ipv6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) top_iph->version = 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) memcpy(top_iph->flow_lbl, XFRM_MODE_SKB_CB(skb)->flow_lbl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) sizeof(top_iph->flow_lbl));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) top_iph->nexthdr = xfrm_af2proto(skb_dst(skb)->ops->family);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) if (x->props.extra_flags & XFRM_SA_XFLAG_DONT_ENCAP_DSCP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) dsfield = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) dsfield = XFRM_MODE_SKB_CB(skb)->tos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) dsfield = INET_ECN_encapsulate(dsfield, XFRM_MODE_SKB_CB(skb)->tos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (x->props.flags & XFRM_STATE_NOECN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) dsfield &= ~INET_ECN_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) ipv6_change_dsfield(top_iph, 0, dsfield);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) top_iph->hop_limit = ip6_dst_hoplimit(xfrm_dst_child(dst));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) top_iph->saddr = *(struct in6_addr *)&x->props.saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) top_iph->daddr = *(struct in6_addr *)&x->id.daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) static int xfrm6_beet_encap_add(struct xfrm_state *x, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) struct ipv6hdr *top_iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) struct ip_beet_phdr *ph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) int optlen, hdr_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) hdr_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) optlen = XFRM_MODE_SKB_CB(skb)->optlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) if (unlikely(optlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) hdr_len += IPV4_BEET_PHMAXLEN - (optlen & 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) skb_set_network_header(skb, -x->props.header_len - hdr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) if (x->sel.family != AF_INET6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) skb->network_header += IPV4_BEET_PHMAXLEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) skb->mac_header = skb->network_header +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) offsetof(struct ipv6hdr, nexthdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) skb->transport_header = skb->network_header + sizeof(*top_iph);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) ph = __skb_pull(skb, XFRM_MODE_SKB_CB(skb)->ihl - hdr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) xfrm6_beet_make_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) top_iph = ipv6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) if (unlikely(optlen)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) if (WARN_ON(optlen < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) ph->padlen = 4 - (optlen & 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) ph->hdrlen = optlen / 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) ph->nexthdr = top_iph->nexthdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) if (ph->padlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) memset(ph + 1, IPOPT_NOP, ph->padlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) top_iph->nexthdr = IPPROTO_BEETPH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) top_iph->saddr = *(struct in6_addr *)&x->props.saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) top_iph->daddr = *(struct in6_addr *)&x->id.daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) /* Add encapsulation header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) * On exit, the transport header will be set to the start of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) * encapsulation header to be filled in by x->type->output and the mac
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) * header will be set to the nextheader (protocol for IPv4) field of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * extension header directly preceding the encapsulation header, or in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) * its absence, that of the top IP header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) * The value of the network header will always point to the top IP header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) * while skb->data will point to the payload.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) static int xfrm4_prepare_output(struct xfrm_state *x, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) err = xfrm_inner_extract_output(x, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) IPCB(skb)->flags |= IPSKB_XFRM_TUNNEL_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) skb->protocol = htons(ETH_P_IP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) switch (x->outer_mode.encap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) case XFRM_MODE_BEET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return xfrm4_beet_encap_add(x, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) case XFRM_MODE_TUNNEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) return xfrm4_tunnel_encap_add(x, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) WARN_ON_ONCE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) static int xfrm6_prepare_output(struct xfrm_state *x, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) err = xfrm_inner_extract_output(x, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) skb->ignore_df = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) skb->protocol = htons(ETH_P_IPV6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) switch (x->outer_mode.encap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) case XFRM_MODE_BEET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) return xfrm6_beet_encap_add(x, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) case XFRM_MODE_TUNNEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) return xfrm6_tunnel_encap_add(x, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) WARN_ON_ONCE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) WARN_ON_ONCE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) return -EAFNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) static int xfrm_outer_mode_output(struct xfrm_state *x, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) switch (x->outer_mode.encap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) case XFRM_MODE_BEET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) case XFRM_MODE_TUNNEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) if (x->outer_mode.family == AF_INET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) return xfrm4_prepare_output(x, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) if (x->outer_mode.family == AF_INET6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) return xfrm6_prepare_output(x, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) case XFRM_MODE_TRANSPORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) if (x->outer_mode.family == AF_INET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) return xfrm4_transport_output(x, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) if (x->outer_mode.family == AF_INET6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) return xfrm6_transport_output(x, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) case XFRM_MODE_ROUTEOPTIMIZATION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) if (x->outer_mode.family == AF_INET6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) return xfrm6_ro_output(x, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) WARN_ON_ONCE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) WARN_ON_ONCE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) int pktgen_xfrm_outer_mode_output(struct xfrm_state *x, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) return xfrm_outer_mode_output(x, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) EXPORT_SYMBOL_GPL(pktgen_xfrm_outer_mode_output);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) static int xfrm_output_one(struct sk_buff *skb, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) struct dst_entry *dst = skb_dst(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) struct xfrm_state *x = dst->xfrm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) struct net *net = xs_net(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) if (err <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) goto resume;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) err = xfrm_skb_check_space(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) goto error_nolock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) skb->mark = xfrm_smark_get(skb->mark, x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) err = xfrm_outer_mode_output(x, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEMODEERROR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) goto error_nolock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) spin_lock_bh(&x->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) if (unlikely(x->km.state != XFRM_STATE_VALID)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEINVALID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) err = xfrm_state_check_expire(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEEXPIRED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) err = x->repl->overflow(x, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATESEQERROR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) x->curlft.bytes += skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) x->curlft.packets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) spin_unlock_bh(&x->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) skb_dst_force(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) if (!skb_dst(skb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) err = -EHOSTUNREACH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) goto error_nolock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) if (xfrm_offload(skb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) x->type_offload->encap(x, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) /* Inner headers are invalid now. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) skb->encapsulation = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) err = x->type->output(x, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) if (err == -EINPROGRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) resume:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) goto error_nolock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) dst = skb_dst_pop(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) if (!dst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) err = -EHOSTUNREACH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) goto error_nolock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) skb_dst_set(skb, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) x = dst->xfrm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) } while (x && !(x->outer_mode.flags & XFRM_MODE_FLAG_TUNNEL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) spin_unlock_bh(&x->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) error_nolock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) int xfrm_output_resume(struct sk_buff *skb, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) struct net *net = xs_net(skb_dst(skb)->xfrm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) while (likely((err = xfrm_output_one(skb, err)) == 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) nf_reset_ct(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) err = skb_dst(skb)->ops->local_out(net, skb->sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) if (unlikely(err != 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) if (!skb_dst(skb)->xfrm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) return dst_output(net, skb->sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) err = nf_hook(skb_dst(skb)->ops->family,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) NF_INET_POST_ROUTING, net, skb->sk, skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) NULL, skb_dst(skb)->dev, xfrm_output2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) if (unlikely(err != 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) if (err == -EINPROGRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) EXPORT_SYMBOL_GPL(xfrm_output_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) static int xfrm_output2(struct net *net, struct sock *sk, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) return xfrm_output_resume(skb, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) static int xfrm_output_gso(struct net *net, struct sock *sk, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) struct sk_buff *segs, *nskb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) BUILD_BUG_ON(sizeof(*IPCB(skb)) > SKB_GSO_CB_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) BUILD_BUG_ON(sizeof(*IP6CB(skb)) > SKB_GSO_CB_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) segs = skb_gso_segment(skb, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) if (IS_ERR(segs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) return PTR_ERR(segs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) if (segs == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) skb_list_walk_safe(segs, segs, nskb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) skb_mark_not_on_list(segs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) err = xfrm_output2(net, sk, segs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) kfree_skb_list(nskb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) int xfrm_output(struct sock *sk, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) struct net *net = dev_net(skb_dst(skb)->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) struct xfrm_state *x = skb_dst(skb)->xfrm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) switch (x->outer_mode.family) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) case AF_INET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) IPCB(skb)->flags |= IPSKB_XFRM_TRANSFORMED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) case AF_INET6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) IP6CB(skb)->flags |= IP6SKB_XFRM_TRANSFORMED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) secpath_reset(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) if (xfrm_dev_offload_ok(skb, x)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) struct sec_path *sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) sp = secpath_set(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) if (!sp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) skb->encapsulation = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) sp->olen++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) sp->xvec[sp->len++] = x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) xfrm_state_hold(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) if (skb_is_gso(skb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) if (skb->inner_protocol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) return xfrm_output_gso(net, sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) skb_shinfo(skb)->gso_type |= SKB_GSO_ESP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) if (x->xso.dev && x->xso.dev->features & NETIF_F_HW_ESP_TX_CSUM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) if (skb_is_gso(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) return xfrm_output_gso(net, sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) if (skb->ip_summed == CHECKSUM_PARTIAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) err = skb_checksum_help(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) return xfrm_output2(net, sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) EXPORT_SYMBOL_GPL(xfrm_output);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) static int xfrm4_tunnel_check_size(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) int mtu, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) if (IPCB(skb)->flags & IPSKB_XFRM_TUNNEL_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) if (!(ip_hdr(skb)->frag_off & htons(IP_DF)) || skb->ignore_df)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) mtu = dst_mtu(skb_dst(skb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) if ((!skb_is_gso(skb) && skb->len > mtu) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) (skb_is_gso(skb) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) !skb_gso_validate_network_len(skb, ip_skb_dst_mtu(skb->sk, skb)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) skb->protocol = htons(ETH_P_IP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) if (skb->sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) xfrm_local_error(skb, mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) icmp_send(skb, ICMP_DEST_UNREACH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) ICMP_FRAG_NEEDED, htonl(mtu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) ret = -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) static int xfrm4_extract_output(struct xfrm_state *x, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) if (x->outer_mode.encap == XFRM_MODE_BEET &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) ip_is_fragment(ip_hdr(skb))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) net_warn_ratelimited("BEET mode doesn't support inner IPv4 fragments\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) return -EAFNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) err = xfrm4_tunnel_check_size(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) XFRM_MODE_SKB_CB(skb)->protocol = ip_hdr(skb)->protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) xfrm4_extract_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) static int xfrm6_tunnel_check_size(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) int mtu, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) struct dst_entry *dst = skb_dst(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) if (skb->ignore_df)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) mtu = dst_mtu(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) if (mtu < IPV6_MIN_MTU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) mtu = IPV6_MIN_MTU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) if ((!skb_is_gso(skb) && skb->len > mtu) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) (skb_is_gso(skb) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) !skb_gso_validate_network_len(skb, ip6_skb_dst_mtu(skb)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) skb->dev = dst->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) skb->protocol = htons(ETH_P_IPV6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) if (xfrm6_local_dontfrag(skb->sk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) ipv6_stub->xfrm6_local_rxpmtu(skb, mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) else if (skb->sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) xfrm_local_error(skb, mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) ret = -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) static int xfrm6_extract_output(struct xfrm_state *x, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) err = xfrm6_tunnel_check_size(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) XFRM_MODE_SKB_CB(skb)->protocol = ipv6_hdr(skb)->nexthdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) xfrm6_extract_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) WARN_ON_ONCE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) return -EAFNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) static int xfrm_inner_extract_output(struct xfrm_state *x, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) const struct xfrm_mode *inner_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) if (x->sel.family == AF_UNSPEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) inner_mode = xfrm_ip2inner_mode(x,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) xfrm_af2proto(skb_dst(skb)->ops->family));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) inner_mode = &x->inner_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) if (inner_mode == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) return -EAFNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) switch (inner_mode->family) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) case AF_INET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) return xfrm4_extract_output(x, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) case AF_INET6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) return xfrm6_extract_output(x, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) return -EAFNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) void xfrm_local_error(struct sk_buff *skb, int mtu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) unsigned int proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) struct xfrm_state_afinfo *afinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) if (skb->protocol == htons(ETH_P_IP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) proto = AF_INET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) else if (skb->protocol == htons(ETH_P_IPV6) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) skb->sk->sk_family == AF_INET6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) proto = AF_INET6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) afinfo = xfrm_state_get_afinfo(proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) if (afinfo) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) afinfo->local_error(skb, mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) EXPORT_SYMBOL_GPL(xfrm_local_error);