1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * xfrm4_input.c
4 *
5 * Changes:
6 * YOSHIFUJI Hideaki @USAGI
7 * Split up af-specific portion
8 * Derek Atkins <derek@ihtfp.com>
9 * Add Encapsulation support
10 *
11 */
12
13 #include <linux/slab.h>
14 #include <linux/module.h>
15 #include <linux/string.h>
16 #include <linux/netfilter.h>
17 #include <linux/netfilter_ipv4.h>
18 #include <net/ip.h>
19 #include <net/xfrm.h>
20 #include <net/protocol.h>
21 #include <net/gro.h>
22
xfrm4_rcv_encap_finish2(struct net * net,struct sock * sk,struct sk_buff * skb)23 static int xfrm4_rcv_encap_finish2(struct net *net, struct sock *sk,
24 struct sk_buff *skb)
25 {
26 return dst_input(skb);
27 }
28
xfrm4_rcv_encap_finish(struct net * net,struct sock * sk,struct sk_buff * skb)29 static inline int xfrm4_rcv_encap_finish(struct net *net, struct sock *sk,
30 struct sk_buff *skb)
31 {
32 if (!skb_dst(skb)) {
33 const struct iphdr *iph = ip_hdr(skb);
34
35 if (ip_route_input_noref(skb, iph->daddr, iph->saddr,
36 ip4h_dscp(iph), skb->dev))
37 goto drop;
38 }
39
40 if (xfrm_trans_queue(skb, xfrm4_rcv_encap_finish2))
41 goto drop;
42
43 return 0;
44 drop:
45 kfree_skb(skb);
46 return NET_RX_DROP;
47 }
48
xfrm4_transport_finish(struct sk_buff * skb,int async)49 int xfrm4_transport_finish(struct sk_buff *skb, int async)
50 {
51 struct xfrm_offload *xo = xfrm_offload(skb);
52 struct iphdr *iph = ip_hdr(skb);
53 struct net_device *dev = skb->dev;
54
55 iph->protocol = XFRM_MODE_SKB_CB(skb)->protocol;
56
57 #ifndef CONFIG_NETFILTER
58 if (!async)
59 return -iph->protocol;
60 #endif
61
62 __skb_push(skb, -skb_network_offset(skb));
63 iph->tot_len = htons(skb->len);
64 ip_send_check(iph);
65
66 if (xo && (xo->flags & XFRM_GRO)) {
67 /* The full l2 header needs to be preserved so that re-injecting the packet at l2
68 * works correctly in the presence of vlan tags.
69 */
70 skb_mac_header_rebuild_full(skb, xo->orig_mac_len);
71 skb_reset_network_header(skb);
72 skb_reset_transport_header(skb);
73 return 0;
74 }
75
76 NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING,
77 dev_net(dev), NULL, skb, dev, NULL,
78 xfrm4_rcv_encap_finish);
79 return 0;
80 }
81
__xfrm4_udp_encap_rcv(struct sock * sk,struct sk_buff * skb,bool pull)82 static int __xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb, bool pull)
83 {
84 struct udp_sock *up = udp_sk(sk);
85 struct udphdr *uh;
86 struct iphdr *iph;
87 int iphlen, len;
88 __u8 *udpdata;
89 __be32 *udpdata32;
90 u16 encap_type;
91
92 encap_type = READ_ONCE(up->encap_type);
93 /* if this is not encapsulated socket, then just return now */
94 if (!encap_type)
95 return 1;
96
97 /* If this is a paged skb, make sure we pull up
98 * whatever data we need to look at. */
99 len = skb->len - sizeof(struct udphdr);
100 if (!pskb_may_pull(skb, sizeof(struct udphdr) + min(len, 8)))
101 return 1;
102
103 /* Now we can get the pointers */
104 uh = udp_hdr(skb);
105 udpdata = (__u8 *)uh + sizeof(struct udphdr);
106 udpdata32 = (__be32 *)udpdata;
107
108 switch (encap_type) {
109 default:
110 case UDP_ENCAP_ESPINUDP:
111 /* Check if this is a keepalive packet. If so, eat it. */
112 if (len == 1 && udpdata[0] == 0xff) {
113 return -EINVAL;
114 } else if (len > sizeof(struct ip_esp_hdr) && udpdata32[0] != 0) {
115 /* ESP Packet without Non-ESP header */
116 len = sizeof(struct udphdr);
117 } else
118 /* Must be an IKE packet.. pass it through */
119 return 1;
120 break;
121 }
122
123 /* At this point we are sure that this is an ESPinUDP packet,
124 * so we need to remove 'len' bytes from the packet (the UDP
125 * header and optional ESP marker bytes) and then modify the
126 * protocol to ESP, and then call into the transform receiver.
127 */
128 if (skb_unclone(skb, GFP_ATOMIC))
129 return -EINVAL;
130
131 /* Now we can update and verify the packet length... */
132 iph = ip_hdr(skb);
133 iphlen = iph->ihl << 2;
134 iph->tot_len = htons(ntohs(iph->tot_len) - len);
135 if (skb->len < iphlen + len) {
136 /* packet is too small!?! */
137 return -EINVAL;
138 }
139
140 /* pull the data buffer up to the ESP header and set the
141 * transport header to point to ESP. Keep UDP on the stack
142 * for later.
143 */
144 if (pull) {
145 __skb_pull(skb, len);
146 skb_reset_transport_header(skb);
147 } else {
148 skb_set_transport_header(skb, len);
149 }
150
151 /* process ESP */
152 return 0;
153 }
154
155 /* If it's a keepalive packet, then just eat it.
156 * If it's an encapsulated packet, then pass it to the
157 * IPsec xfrm input.
158 * Returns 0 if skb passed to xfrm or was dropped.
159 * Returns >0 if skb should be passed to UDP.
160 * Returns <0 if skb should be resubmitted (-ret is protocol)
161 */
xfrm4_udp_encap_rcv(struct sock * sk,struct sk_buff * skb)162 int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb)
163 {
164 int ret;
165
166 ret = __xfrm4_udp_encap_rcv(sk, skb, true);
167 if (!ret)
168 return xfrm4_rcv_encap(skb, IPPROTO_ESP, 0,
169 udp_sk(sk)->encap_type);
170
171 if (ret < 0) {
172 kfree_skb(skb);
173 return 0;
174 }
175
176 return ret;
177 }
178 EXPORT_SYMBOL(xfrm4_udp_encap_rcv);
179
xfrm4_gro_udp_encap_rcv(struct sock * sk,struct list_head * head,struct sk_buff * skb)180 struct sk_buff *xfrm4_gro_udp_encap_rcv(struct sock *sk, struct list_head *head,
181 struct sk_buff *skb)
182 {
183 int offset = skb_gro_offset(skb);
184 const struct net_offload *ops;
185 struct sk_buff *pp = NULL;
186 int len, dlen;
187 __u8 *udpdata;
188 __be32 *udpdata32;
189
190 len = skb->len - offset;
191 dlen = offset + min(len, 8);
192 udpdata = skb_gro_header(skb, dlen, offset);
193 udpdata32 = (__be32 *)udpdata;
194 if (unlikely(!udpdata))
195 return NULL;
196
197 rcu_read_lock();
198 ops = rcu_dereference(inet_offloads[IPPROTO_ESP]);
199 if (!ops || !ops->callbacks.gro_receive)
200 goto out;
201
202 /* check if it is a keepalive or IKE packet */
203 if (len <= sizeof(struct ip_esp_hdr) || udpdata32[0] == 0)
204 goto out;
205
206 /* set the transport header to ESP */
207 skb_set_transport_header(skb, offset);
208
209 NAPI_GRO_CB(skb)->proto = IPPROTO_UDP;
210
211 pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
212 rcu_read_unlock();
213
214 return pp;
215
216 out:
217 rcu_read_unlock();
218 NAPI_GRO_CB(skb)->same_flow = 0;
219 NAPI_GRO_CB(skb)->flush = 1;
220
221 return NULL;
222 }
223 EXPORT_SYMBOL(xfrm4_gro_udp_encap_rcv);
224
xfrm4_rcv(struct sk_buff * skb)225 int xfrm4_rcv(struct sk_buff *skb)
226 {
227 return xfrm4_rcv_spi(skb, ip_hdr(skb)->protocol, 0);
228 }
229 EXPORT_SYMBOL(xfrm4_rcv);
230