1 // SPDX-License-Identifier: GPL-2.0
3 * xfrm6_input.c: based on net/ipv4/xfrm4_input.c
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * YOSHIFUJI Hideaki @USAGI
13 #include <linux/module.h>
14 #include <linux/string.h>
15 #include <linux/netfilter.h>
16 #include <linux/netfilter_ipv6.h>
19 #include <net/protocol.h>
22 int xfrm6_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi,
25 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = t;
26 XFRM_SPI_SKB_CB(skb)->family = AF_INET6;
27 XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr);
28 return xfrm_input(skb, nexthdr, spi, 0);
30 EXPORT_SYMBOL(xfrm6_rcv_spi);
32 static int xfrm6_transport_finish2(struct net *net, struct sock *sk,
35 if (xfrm_trans_queue(skb, ip6_rcv_finish)) {
43 int xfrm6_transport_finish(struct sk_buff *skb, int async)
45 struct xfrm_offload *xo = xfrm_offload(skb);
46 int nhlen = skb->data - skb_network_header(skb);
48 skb_network_header(skb)[IP6CB(skb)->nhoff] =
49 XFRM_MODE_SKB_CB(skb)->protocol;
51 #ifndef CONFIG_NETFILTER
56 __skb_push(skb, nhlen);
57 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
58 skb_postpush_rcsum(skb, skb_network_header(skb), nhlen);
60 if (xo && (xo->flags & XFRM_GRO)) {
61 skb_mac_header_rebuild(skb);
62 skb_reset_transport_header(skb);
66 NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING,
67 dev_net(skb->dev), NULL, skb, skb->dev, NULL,
68 xfrm6_transport_finish2);
72 static int __xfrm6_udp_encap_rcv(struct sock *sk, struct sk_buff *skb, bool pull)
74 struct udp_sock *up = udp_sk(sk);
78 int ip6hlen = sizeof(struct ipv6hdr);
83 encap_type = READ_ONCE(up->encap_type);
84 /* if this is not encapsulated socket, then just return now */
88 /* If this is a paged skb, make sure we pull up
89 * whatever data we need to look at. */
90 len = skb->len - sizeof(struct udphdr);
91 if (!pskb_may_pull(skb, sizeof(struct udphdr) + min(len, 8)))
94 /* Now we can get the pointers */
96 udpdata = (__u8 *)uh + sizeof(struct udphdr);
97 udpdata32 = (__be32 *)udpdata;
101 case UDP_ENCAP_ESPINUDP:
102 /* Check if this is a keepalive packet. If so, eat it. */
103 if (len == 1 && udpdata[0] == 0xff) {
105 } else if (len > sizeof(struct ip_esp_hdr) && udpdata32[0] != 0) {
106 /* ESP Packet without Non-ESP header */
107 len = sizeof(struct udphdr);
109 /* Must be an IKE packet.. pass it through */
112 case UDP_ENCAP_ESPINUDP_NON_IKE:
113 /* Check if this is a keepalive packet. If so, eat it. */
114 if (len == 1 && udpdata[0] == 0xff) {
116 } else if (len > 2 * sizeof(u32) + sizeof(struct ip_esp_hdr) &&
117 udpdata32[0] == 0 && udpdata32[1] == 0) {
119 /* ESP Packet with Non-IKE marker */
120 len = sizeof(struct udphdr) + 2 * sizeof(u32);
122 /* Must be an IKE packet.. pass it through */
127 /* At this point we are sure that this is an ESPinUDP packet,
128 * so we need to remove 'len' bytes from the packet (the UDP
129 * header and optional ESP marker bytes) and then modify the
130 * protocol to ESP, and then call into the transform receiver.
132 if (skb_unclone(skb, GFP_ATOMIC))
135 /* Now we can update and verify the packet length... */
136 ip6h = ipv6_hdr(skb);
137 ip6h->payload_len = htons(ntohs(ip6h->payload_len) - len);
138 if (skb->len < ip6hlen + len) {
139 /* packet is too small!?! */
143 /* pull the data buffer up to the ESP header and set the
144 * transport header to point to ESP. Keep UDP on the stack
148 __skb_pull(skb, len);
149 skb_reset_transport_header(skb);
151 skb_set_transport_header(skb, len);
158 /* If it's a keepalive packet, then just eat it.
159 * If it's an encapsulated packet, then pass it to the
161 * Returns 0 if skb passed to xfrm or was dropped.
162 * Returns >0 if skb should be passed to UDP.
163 * Returns <0 if skb should be resubmitted (-ret is protocol)
165 int xfrm6_udp_encap_rcv(struct sock *sk, struct sk_buff *skb)
169 if (skb->protocol == htons(ETH_P_IP))
170 return xfrm4_udp_encap_rcv(sk, skb);
172 ret = __xfrm6_udp_encap_rcv(sk, skb, true);
174 return xfrm6_rcv_encap(skb, IPPROTO_ESP, 0,
175 udp_sk(sk)->encap_type);
185 struct sk_buff *xfrm6_gro_udp_encap_rcv(struct sock *sk, struct list_head *head,
188 int offset = skb_gro_offset(skb);
189 const struct net_offload *ops;
190 struct sk_buff *pp = NULL;
193 if (skb->protocol == htons(ETH_P_IP))
194 return xfrm4_gro_udp_encap_rcv(sk, head, skb);
196 offset = offset - sizeof(struct udphdr);
198 if (!pskb_pull(skb, offset))
202 ops = rcu_dereference(inet6_offloads[IPPROTO_ESP]);
203 if (!ops || !ops->callbacks.gro_receive)
206 ret = __xfrm6_udp_encap_rcv(sk, skb, false);
210 skb_push(skb, offset);
211 NAPI_GRO_CB(skb)->proto = IPPROTO_UDP;
213 pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
220 skb_push(skb, offset);
221 NAPI_GRO_CB(skb)->same_flow = 0;
222 NAPI_GRO_CB(skb)->flush = 1;
227 int xfrm6_rcv_tnl(struct sk_buff *skb, struct ip6_tnl *t)
229 return xfrm6_rcv_spi(skb, skb_network_header(skb)[IP6CB(skb)->nhoff],
232 EXPORT_SYMBOL(xfrm6_rcv_tnl);
234 int xfrm6_rcv(struct sk_buff *skb)
236 return xfrm6_rcv_tnl(skb, NULL);
238 EXPORT_SYMBOL(xfrm6_rcv);
239 int xfrm6_input_addr(struct sk_buff *skb, xfrm_address_t *daddr,
240 xfrm_address_t *saddr, u8 proto)
242 struct net *net = dev_net(skb->dev);
243 struct xfrm_state *x = NULL;
247 sp = secpath_set(skb);
249 XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
253 if (1 + sp->len == XFRM_MAX_DEPTH) {
254 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
258 for (i = 0; i < 3; i++) {
259 xfrm_address_t *dst, *src;
267 /* lookup state with wild-card source address */
269 src = (xfrm_address_t *)&in6addr_any;
272 /* lookup state with wild-card addresses */
273 dst = (xfrm_address_t *)&in6addr_any;
274 src = (xfrm_address_t *)&in6addr_any;
278 x = xfrm_state_lookup_byaddr(net, skb->mark, dst, src, proto, AF_INET6);
284 if ((!i || (x->props.flags & XFRM_STATE_WILDRECV)) &&
285 likely(x->km.state == XFRM_STATE_VALID) &&
286 !xfrm_state_check_expire(x)) {
287 spin_unlock(&x->lock);
288 if (x->type->input(x, skb) > 0) {
289 /* found a valid state */
293 spin_unlock(&x->lock);
300 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES);
301 xfrm_audit_state_notfound_simple(skb, AF_INET6);
305 sp->xvec[sp->len++] = x;
309 x->curlft.bytes += skb->len;
312 spin_unlock(&x->lock);
319 EXPORT_SYMBOL(xfrm6_input_addr);