GNU Linux-libre 5.10.217-gnu1
[releases.git] / net / ipv6 / xfrm6_input.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * xfrm6_input.c: based on net/ipv4/xfrm4_input.c
4  *
5  * Authors:
6  *      Mitsuru KANDA @USAGI
7  *      Kazunori MIYAZAWA @USAGI
8  *      Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9  *      YOSHIFUJI Hideaki @USAGI
10  *              IPv6 support
11  */
12
13 #include <linux/module.h>
14 #include <linux/string.h>
15 #include <linux/netfilter.h>
16 #include <linux/netfilter_ipv6.h>
17 #include <net/ipv6.h>
18 #include <net/xfrm.h>
19
20 int xfrm6_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi,
21                   struct ip6_tnl *t)
22 {
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);
27 }
28 EXPORT_SYMBOL(xfrm6_rcv_spi);
29
30 static int xfrm6_transport_finish2(struct net *net, struct sock *sk,
31                                    struct sk_buff *skb)
32 {
33         if (xfrm_trans_queue(skb, ip6_rcv_finish)) {
34                 kfree_skb(skb);
35                 return NET_RX_DROP;
36         }
37
38         return 0;
39 }
40
41 int xfrm6_transport_finish(struct sk_buff *skb, int async)
42 {
43         struct xfrm_offload *xo = xfrm_offload(skb);
44         int nhlen = skb->data - skb_network_header(skb);
45
46         skb_network_header(skb)[IP6CB(skb)->nhoff] =
47                 XFRM_MODE_SKB_CB(skb)->protocol;
48
49 #ifndef CONFIG_NETFILTER
50         if (!async)
51                 return 1;
52 #endif
53
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);
57
58         if (xo && (xo->flags & XFRM_GRO)) {
59                 /* The full l2 header needs to be preserved so that re-injecting the packet at l2
60                  * works correctly in the presence of vlan tags.
61                  */
62                 skb_mac_header_rebuild_full(skb, xo->orig_mac_len);
63                 skb_reset_network_header(skb);
64                 skb_reset_transport_header(skb);
65                 return 0;
66         }
67
68         NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING,
69                 dev_net(skb->dev), NULL, skb, skb->dev, NULL,
70                 xfrm6_transport_finish2);
71         return 0;
72 }
73
74 /* If it's a keepalive packet, then just eat it.
75  * If it's an encapsulated packet, then pass it to the
76  * IPsec xfrm input.
77  * Returns 0 if skb passed to xfrm or was dropped.
78  * Returns >0 if skb should be passed to UDP.
79  * Returns <0 if skb should be resubmitted (-ret is protocol)
80  */
81 int xfrm6_udp_encap_rcv(struct sock *sk, struct sk_buff *skb)
82 {
83         struct udp_sock *up = udp_sk(sk);
84         struct udphdr *uh;
85         struct ipv6hdr *ip6h;
86         int len;
87         int ip6hlen = sizeof(struct ipv6hdr);
88
89         __u8 *udpdata;
90         __be32 *udpdata32;
91         __u16 encap_type = up->encap_type;
92
93         if (skb->protocol == htons(ETH_P_IP))
94                 return xfrm4_udp_encap_rcv(sk, skb);
95
96         /* if this is not encapsulated socket, then just return now */
97         if (!encap_type)
98                 return 1;
99
100         /* If this is a paged skb, make sure we pull up
101          * whatever data we need to look at. */
102         len = skb->len - sizeof(struct udphdr);
103         if (!pskb_may_pull(skb, sizeof(struct udphdr) + min(len, 8)))
104                 return 1;
105
106         /* Now we can get the pointers */
107         uh = udp_hdr(skb);
108         udpdata = (__u8 *)uh + sizeof(struct udphdr);
109         udpdata32 = (__be32 *)udpdata;
110
111         switch (encap_type) {
112         default:
113         case UDP_ENCAP_ESPINUDP:
114                 /* Check if this is a keepalive packet.  If so, eat it. */
115                 if (len == 1 && udpdata[0] == 0xff) {
116                         goto drop;
117                 } else if (len > sizeof(struct ip_esp_hdr) && udpdata32[0] != 0) {
118                         /* ESP Packet without Non-ESP header */
119                         len = sizeof(struct udphdr);
120                 } else
121                         /* Must be an IKE packet.. pass it through */
122                         return 1;
123                 break;
124         case UDP_ENCAP_ESPINUDP_NON_IKE:
125                 /* Check if this is a keepalive packet.  If so, eat it. */
126                 if (len == 1 && udpdata[0] == 0xff) {
127                         goto drop;
128                 } else if (len > 2 * sizeof(u32) + sizeof(struct ip_esp_hdr) &&
129                            udpdata32[0] == 0 && udpdata32[1] == 0) {
130
131                         /* ESP Packet with Non-IKE marker */
132                         len = sizeof(struct udphdr) + 2 * sizeof(u32);
133                 } else
134                         /* Must be an IKE packet.. pass it through */
135                         return 1;
136                 break;
137         }
138
139         /* At this point we are sure that this is an ESPinUDP packet,
140          * so we need to remove 'len' bytes from the packet (the UDP
141          * header and optional ESP marker bytes) and then modify the
142          * protocol to ESP, and then call into the transform receiver.
143          */
144         if (skb_unclone(skb, GFP_ATOMIC))
145                 goto drop;
146
147         /* Now we can update and verify the packet length... */
148         ip6h = ipv6_hdr(skb);
149         ip6h->payload_len = htons(ntohs(ip6h->payload_len) - len);
150         if (skb->len < ip6hlen + len) {
151                 /* packet is too small!?! */
152                 goto drop;
153         }
154
155         /* pull the data buffer up to the ESP header and set the
156          * transport header to point to ESP.  Keep UDP on the stack
157          * for later.
158          */
159         __skb_pull(skb, len);
160         skb_reset_transport_header(skb);
161
162         /* process ESP */
163         return xfrm6_rcv_encap(skb, IPPROTO_ESP, 0, encap_type);
164
165 drop:
166         kfree_skb(skb);
167         return 0;
168 }
169
170 int xfrm6_rcv_tnl(struct sk_buff *skb, struct ip6_tnl *t)
171 {
172         return xfrm6_rcv_spi(skb, skb_network_header(skb)[IP6CB(skb)->nhoff],
173                              0, t);
174 }
175 EXPORT_SYMBOL(xfrm6_rcv_tnl);
176
177 int xfrm6_rcv(struct sk_buff *skb)
178 {
179         return xfrm6_rcv_tnl(skb, NULL);
180 }
181 EXPORT_SYMBOL(xfrm6_rcv);
182 int xfrm6_input_addr(struct sk_buff *skb, xfrm_address_t *daddr,
183                      xfrm_address_t *saddr, u8 proto)
184 {
185         struct net *net = dev_net(skb->dev);
186         struct xfrm_state *x = NULL;
187         struct sec_path *sp;
188         int i = 0;
189
190         sp = secpath_set(skb);
191         if (!sp) {
192                 XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
193                 goto drop;
194         }
195
196         if (1 + sp->len == XFRM_MAX_DEPTH) {
197                 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
198                 goto drop;
199         }
200
201         for (i = 0; i < 3; i++) {
202                 xfrm_address_t *dst, *src;
203
204                 switch (i) {
205                 case 0:
206                         dst = daddr;
207                         src = saddr;
208                         break;
209                 case 1:
210                         /* lookup state with wild-card source address */
211                         dst = daddr;
212                         src = (xfrm_address_t *)&in6addr_any;
213                         break;
214                 default:
215                         /* lookup state with wild-card addresses */
216                         dst = (xfrm_address_t *)&in6addr_any;
217                         src = (xfrm_address_t *)&in6addr_any;
218                         break;
219                 }
220
221                 x = xfrm_state_lookup_byaddr(net, skb->mark, dst, src, proto, AF_INET6);
222                 if (!x)
223                         continue;
224
225                 spin_lock(&x->lock);
226
227                 if ((!i || (x->props.flags & XFRM_STATE_WILDRECV)) &&
228                     likely(x->km.state == XFRM_STATE_VALID) &&
229                     !xfrm_state_check_expire(x)) {
230                         spin_unlock(&x->lock);
231                         if (x->type->input(x, skb) > 0) {
232                                 /* found a valid state */
233                                 break;
234                         }
235                 } else
236                         spin_unlock(&x->lock);
237
238                 xfrm_state_put(x);
239                 x = NULL;
240         }
241
242         if (!x) {
243                 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES);
244                 xfrm_audit_state_notfound_simple(skb, AF_INET6);
245                 goto drop;
246         }
247
248         sp->xvec[sp->len++] = x;
249
250         spin_lock(&x->lock);
251
252         x->curlft.bytes += skb->len;
253         x->curlft.packets++;
254
255         spin_unlock(&x->lock);
256
257         return 1;
258
259 drop:
260         return -1;
261 }
262 EXPORT_SYMBOL(xfrm6_input_addr);