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>
20 int xfrm6_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi,
23 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = t;
24 XFRM_SPI_SKB_CB(skb)->family = AF_INET6;
25 XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr);
26 return xfrm_input(skb, nexthdr, spi, 0);
28 EXPORT_SYMBOL(xfrm6_rcv_spi);
30 static int xfrm6_transport_finish2(struct net *net, struct sock *sk,
33 if (xfrm_trans_queue(skb, ip6_rcv_finish)) {
41 int xfrm6_transport_finish(struct sk_buff *skb, int async)
43 struct xfrm_offload *xo = xfrm_offload(skb);
44 int nhlen = skb->data - skb_network_header(skb);
46 skb_network_header(skb)[IP6CB(skb)->nhoff] =
47 XFRM_MODE_SKB_CB(skb)->protocol;
49 #ifndef CONFIG_NETFILTER
54 __skb_push(skb, nhlen);
55 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
56 skb_postpush_rcsum(skb, skb_network_header(skb), nhlen);
58 if (xo && (xo->flags & XFRM_GRO)) {
59 skb_mac_header_rebuild(skb);
60 skb_reset_transport_header(skb);
64 NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING,
65 dev_net(skb->dev), NULL, skb, skb->dev, NULL,
66 xfrm6_transport_finish2);
70 /* If it's a keepalive packet, then just eat it.
71 * If it's an encapsulated packet, then pass it to the
73 * Returns 0 if skb passed to xfrm or was dropped.
74 * Returns >0 if skb should be passed to UDP.
75 * Returns <0 if skb should be resubmitted (-ret is protocol)
77 int xfrm6_udp_encap_rcv(struct sock *sk, struct sk_buff *skb)
79 struct udp_sock *up = udp_sk(sk);
83 int ip6hlen = sizeof(struct ipv6hdr);
87 __u16 encap_type = up->encap_type;
89 if (skb->protocol == htons(ETH_P_IP))
90 return xfrm4_udp_encap_rcv(sk, skb);
92 /* if this is not encapsulated socket, then just return now */
96 /* If this is a paged skb, make sure we pull up
97 * whatever data we need to look at. */
98 len = skb->len - sizeof(struct udphdr);
99 if (!pskb_may_pull(skb, sizeof(struct udphdr) + min(len, 8)))
102 /* Now we can get the pointers */
104 udpdata = (__u8 *)uh + sizeof(struct udphdr);
105 udpdata32 = (__be32 *)udpdata;
107 switch (encap_type) {
109 case UDP_ENCAP_ESPINUDP:
110 /* Check if this is a keepalive packet. If so, eat it. */
111 if (len == 1 && udpdata[0] == 0xff) {
113 } else if (len > sizeof(struct ip_esp_hdr) && udpdata32[0] != 0) {
114 /* ESP Packet without Non-ESP header */
115 len = sizeof(struct udphdr);
117 /* Must be an IKE packet.. pass it through */
120 case UDP_ENCAP_ESPINUDP_NON_IKE:
121 /* Check if this is a keepalive packet. If so, eat it. */
122 if (len == 1 && udpdata[0] == 0xff) {
124 } else if (len > 2 * sizeof(u32) + sizeof(struct ip_esp_hdr) &&
125 udpdata32[0] == 0 && udpdata32[1] == 0) {
127 /* ESP Packet with Non-IKE marker */
128 len = sizeof(struct udphdr) + 2 * sizeof(u32);
130 /* Must be an IKE packet.. pass it through */
135 /* At this point we are sure that this is an ESPinUDP packet,
136 * so we need to remove 'len' bytes from the packet (the UDP
137 * header and optional ESP marker bytes) and then modify the
138 * protocol to ESP, and then call into the transform receiver.
140 if (skb_unclone(skb, GFP_ATOMIC))
143 /* Now we can update and verify the packet length... */
144 ip6h = ipv6_hdr(skb);
145 ip6h->payload_len = htons(ntohs(ip6h->payload_len) - len);
146 if (skb->len < ip6hlen + len) {
147 /* packet is too small!?! */
151 /* pull the data buffer up to the ESP header and set the
152 * transport header to point to ESP. Keep UDP on the stack
155 __skb_pull(skb, len);
156 skb_reset_transport_header(skb);
159 return xfrm6_rcv_encap(skb, IPPROTO_ESP, 0, encap_type);
166 int xfrm6_rcv_tnl(struct sk_buff *skb, struct ip6_tnl *t)
168 return xfrm6_rcv_spi(skb, skb_network_header(skb)[IP6CB(skb)->nhoff],
171 EXPORT_SYMBOL(xfrm6_rcv_tnl);
173 int xfrm6_rcv(struct sk_buff *skb)
175 return xfrm6_rcv_tnl(skb, NULL);
177 EXPORT_SYMBOL(xfrm6_rcv);
178 int xfrm6_input_addr(struct sk_buff *skb, xfrm_address_t *daddr,
179 xfrm_address_t *saddr, u8 proto)
181 struct net *net = dev_net(skb->dev);
182 struct xfrm_state *x = NULL;
186 sp = secpath_set(skb);
188 XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
192 if (1 + sp->len == XFRM_MAX_DEPTH) {
193 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
197 for (i = 0; i < 3; i++) {
198 xfrm_address_t *dst, *src;
206 /* lookup state with wild-card source address */
208 src = (xfrm_address_t *)&in6addr_any;
211 /* lookup state with wild-card addresses */
212 dst = (xfrm_address_t *)&in6addr_any;
213 src = (xfrm_address_t *)&in6addr_any;
217 x = xfrm_state_lookup_byaddr(net, skb->mark, dst, src, proto, AF_INET6);
223 if ((!i || (x->props.flags & XFRM_STATE_WILDRECV)) &&
224 likely(x->km.state == XFRM_STATE_VALID) &&
225 !xfrm_state_check_expire(x)) {
226 spin_unlock(&x->lock);
227 if (x->type->input(x, skb) > 0) {
228 /* found a valid state */
232 spin_unlock(&x->lock);
239 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES);
240 xfrm_audit_state_notfound_simple(skb, AF_INET6);
244 sp->xvec[sp->len++] = x;
248 x->curlft.bytes += skb->len;
251 spin_unlock(&x->lock);
258 EXPORT_SYMBOL(xfrm6_input_addr);