blob: e5f69b0bf3df551e28344ddd1c5068069c823cd1 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Tom Herbert234615512014-09-17 12:25:56 -07002#include <linux/module.h>
3#include <linux/errno.h>
4#include <linux/socket.h>
5#include <linux/skbuff.h>
6#include <linux/ip.h>
Stefano Briviob8a51b32018-11-08 12:19:23 +01007#include <linux/icmp.h>
Tom Herbert234615512014-09-17 12:25:56 -07008#include <linux/udp.h>
9#include <linux/types.h>
10#include <linux/kernel.h>
11#include <net/genetlink.h>
Tom Herbert37dd0242014-10-03 15:48:09 -070012#include <net/gue.h>
stephen hemminger9dc621a2017-05-19 09:55:54 -070013#include <net/fou.h>
Tom Herbert234615512014-09-17 12:25:56 -070014#include <net/ip.h>
Tom Herbertafe93322014-09-17 12:25:57 -070015#include <net/protocol.h>
Tom Herbert234615512014-09-17 12:25:56 -070016#include <net/udp.h>
17#include <net/udp_tunnel.h>
18#include <net/xfrm.h>
19#include <uapi/linux/fou.h>
20#include <uapi/linux/genetlink.h>
21
Tom Herbert234615512014-09-17 12:25:56 -070022struct fou {
23 struct socket *sock;
24 u8 protocol;
Tom Herbertfe881ef2015-02-10 16:30:33 -080025 u8 flags;
WANG Cong4cbcdf22015-04-10 12:00:28 -070026 __be16 port;
Tom Herbert5f914b62016-05-18 09:06:16 -070027 u8 family;
WANG Cong7a6c8c32015-04-10 12:00:30 -070028 u16 type;
Tom Herbert234615512014-09-17 12:25:56 -070029 struct list_head list;
Hannes Frederic Sowa3036fac2015-12-15 21:01:53 +010030 struct rcu_head rcu;
Tom Herbert234615512014-09-17 12:25:56 -070031};
32
Tom Herbertfe881ef2015-02-10 16:30:33 -080033#define FOU_F_REMCSUM_NOPARTIAL BIT(0)
34
Tom Herbert234615512014-09-17 12:25:56 -070035struct fou_cfg {
Tom Herbert37dd0242014-10-03 15:48:09 -070036 u16 type;
Tom Herbert234615512014-09-17 12:25:56 -070037 u8 protocol;
Tom Herbertfe881ef2015-02-10 16:30:33 -080038 u8 flags;
Tom Herbert234615512014-09-17 12:25:56 -070039 struct udp_port_cfg udp_config;
40};
41
WANG Cong02d793c2015-04-10 12:00:29 -070042static unsigned int fou_net_id;
43
44struct fou_net {
45 struct list_head fou_list;
46 struct mutex fou_lock;
47};
48
Tom Herbert234615512014-09-17 12:25:56 -070049static inline struct fou *fou_from_sock(struct sock *sk)
50{
51 return sk->sk_user_data;
52}
53
Tom Herbert5f914b62016-05-18 09:06:16 -070054static int fou_recv_pull(struct sk_buff *skb, struct fou *fou, size_t len)
Tom Herbert234615512014-09-17 12:25:56 -070055{
Tom Herbert234615512014-09-17 12:25:56 -070056 /* Remove 'len' bytes from the packet (UDP header and
Tom Herbert5024c332014-11-04 09:06:53 -080057 * FOU header if present).
Tom Herbert234615512014-09-17 12:25:56 -070058 */
Tom Herbert5f914b62016-05-18 09:06:16 -070059 if (fou->family == AF_INET)
60 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(skb)->tot_len) - len);
61 else
62 ipv6_hdr(skb)->payload_len =
63 htons(ntohs(ipv6_hdr(skb)->payload_len) - len);
64
Tom Herbert234615512014-09-17 12:25:56 -070065 __skb_pull(skb, len);
66 skb_postpull_rcsum(skb, udp_hdr(skb), len);
67 skb_reset_transport_header(skb);
Jesse Grossa09a4c82016-03-19 09:32:02 -070068 return iptunnel_pull_offloads(skb);
Tom Herbert234615512014-09-17 12:25:56 -070069}
70
71static int fou_udp_recv(struct sock *sk, struct sk_buff *skb)
72{
73 struct fou *fou = fou_from_sock(sk);
74
75 if (!fou)
76 return 1;
77
Tom Herbert5f914b62016-05-18 09:06:16 -070078 if (fou_recv_pull(skb, fou, sizeof(struct udphdr)))
Jesse Grossa09a4c82016-03-19 09:32:02 -070079 goto drop;
Tom Herbert5024c332014-11-04 09:06:53 -080080
81 return -fou->protocol;
Jesse Grossa09a4c82016-03-19 09:32:02 -070082
83drop:
84 kfree_skb(skb);
85 return 0;
Tom Herbert5024c332014-11-04 09:06:53 -080086}
87
Tom Herberta8d31c12014-11-04 09:06:57 -080088static struct guehdr *gue_remcsum(struct sk_buff *skb, struct guehdr *guehdr,
Tom Herbertfe881ef2015-02-10 16:30:33 -080089 void *data, size_t hdrlen, u8 ipproto,
90 bool nopartial)
Tom Herberta8d31c12014-11-04 09:06:57 -080091{
92 __be16 *pd = data;
Tom Herbert4fd671d2014-11-25 11:21:20 -080093 size_t start = ntohs(pd[0]);
94 size_t offset = ntohs(pd[1]);
Tom Herbertb7fe10e2015-08-19 17:07:32 -070095 size_t plen = sizeof(struct udphdr) + hdrlen +
96 max_t(size_t, offset + sizeof(u16), start);
97
98 if (skb->remcsum_offload)
99 return guehdr;
Tom Herberta8d31c12014-11-04 09:06:57 -0800100
Tom Herberta8d31c12014-11-04 09:06:57 -0800101 if (!pskb_may_pull(skb, plen))
102 return NULL;
103 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
104
Tom Herbertfe881ef2015-02-10 16:30:33 -0800105 skb_remcsum_process(skb, (void *)guehdr + hdrlen,
106 start, offset, nopartial);
Tom Herberta8d31c12014-11-04 09:06:57 -0800107
108 return guehdr;
109}
110
Tom Herbert5024c332014-11-04 09:06:53 -0800111static int gue_control_message(struct sk_buff *skb, struct guehdr *guehdr)
112{
113 /* No support yet */
114 kfree_skb(skb);
115 return 0;
Tom Herbert234615512014-09-17 12:25:56 -0700116}
117
Tom Herbert37dd0242014-10-03 15:48:09 -0700118static int gue_udp_recv(struct sock *sk, struct sk_buff *skb)
119{
120 struct fou *fou = fou_from_sock(sk);
Tom Herbert5024c332014-11-04 09:06:53 -0800121 size_t len, optlen, hdrlen;
Tom Herbert37dd0242014-10-03 15:48:09 -0700122 struct guehdr *guehdr;
Tom Herbert5024c332014-11-04 09:06:53 -0800123 void *data;
Tom Herberta8d31c12014-11-04 09:06:57 -0800124 u16 doffset = 0;
Lorenzo Bianconi988dc4a2019-04-09 11:47:20 +0200125 u8 proto_ctype;
Tom Herbert37dd0242014-10-03 15:48:09 -0700126
127 if (!fou)
128 return 1;
129
130 len = sizeof(struct udphdr) + sizeof(struct guehdr);
131 if (!pskb_may_pull(skb, len))
132 goto drop;
133
Tom Herbert5024c332014-11-04 09:06:53 -0800134 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
Tom Herbert37dd0242014-10-03 15:48:09 -0700135
Tom Herbertc1e48af2016-06-06 16:06:02 -0700136 switch (guehdr->version) {
137 case 0: /* Full GUE header present */
138 break;
139
140 case 1: {
Simon Hormanc9d52f22019-04-09 09:59:07 +0200141 /* Direct encapsulation of IPv4 or IPv6 */
Tom Herbertc1e48af2016-06-06 16:06:02 -0700142
143 int prot;
144
145 switch (((struct iphdr *)guehdr)->version) {
146 case 4:
147 prot = IPPROTO_IPIP;
148 break;
149 case 6:
150 prot = IPPROTO_IPV6;
151 break;
152 default:
153 goto drop;
154 }
155
156 if (fou_recv_pull(skb, fou, sizeof(struct udphdr)))
157 goto drop;
158
159 return -prot;
160 }
161
162 default: /* Undefined version */
163 goto drop;
164 }
165
Tom Herbert5024c332014-11-04 09:06:53 -0800166 optlen = guehdr->hlen << 2;
167 len += optlen;
168
Tom Herbert37dd0242014-10-03 15:48:09 -0700169 if (!pskb_may_pull(skb, len))
170 goto drop;
171
Tom Herbert5024c332014-11-04 09:06:53 -0800172 /* guehdr may change after pull */
173 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
Li RongQingd8f00d22014-10-17 16:53:47 +0800174
Lorenzo Bianconi526bb572019-04-09 12:03:07 +0200175 if (validate_gue_flags(guehdr, optlen))
Tom Herbert37dd0242014-10-03 15:48:09 -0700176 goto drop;
177
Tom Herberta8d31c12014-11-04 09:06:57 -0800178 hdrlen = sizeof(struct guehdr) + optlen;
179
Tom Herbert5f914b62016-05-18 09:06:16 -0700180 if (fou->family == AF_INET)
181 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(skb)->tot_len) - len);
182 else
183 ipv6_hdr(skb)->payload_len =
184 htons(ntohs(ipv6_hdr(skb)->payload_len) - len);
Tom Herberta8d31c12014-11-04 09:06:57 -0800185
Tom Herberta8d31c12014-11-04 09:06:57 -0800186 /* Pull csum through the guehdr now . This can be used if
187 * there is a remote checksum offload.
188 */
189 skb_postpull_rcsum(skb, udp_hdr(skb), len);
Tom Herbert5024c332014-11-04 09:06:53 -0800190
191 data = &guehdr[1];
192
193 if (guehdr->flags & GUE_FLAG_PRIV) {
Tom Herberta8d31c12014-11-04 09:06:57 -0800194 __be32 flags = *(__be32 *)(data + doffset);
Tom Herbert5024c332014-11-04 09:06:53 -0800195
Tom Herberta8d31c12014-11-04 09:06:57 -0800196 doffset += GUE_LEN_PRIV;
197
198 if (flags & GUE_PFLAG_REMCSUM) {
199 guehdr = gue_remcsum(skb, guehdr, data + doffset,
Tom Herbertfe881ef2015-02-10 16:30:33 -0800200 hdrlen, guehdr->proto_ctype,
201 !!(fou->flags &
202 FOU_F_REMCSUM_NOPARTIAL));
Tom Herberta8d31c12014-11-04 09:06:57 -0800203 if (!guehdr)
204 goto drop;
205
206 data = &guehdr[1];
207
208 doffset += GUE_PLEN_REMCSUM;
209 }
Tom Herbert37dd0242014-10-03 15:48:09 -0700210 }
211
Tom Herbert5024c332014-11-04 09:06:53 -0800212 if (unlikely(guehdr->control))
213 return gue_control_message(skb, guehdr);
214
Lorenzo Bianconi988dc4a2019-04-09 11:47:20 +0200215 proto_ctype = guehdr->proto_ctype;
Tom Herbert4fd671d2014-11-25 11:21:20 -0800216 __skb_pull(skb, sizeof(struct udphdr) + hdrlen);
Tom Herberta8d31c12014-11-04 09:06:57 -0800217 skb_reset_transport_header(skb);
218
Jesse Grossa09a4c82016-03-19 09:32:02 -0700219 if (iptunnel_pull_offloads(skb))
220 goto drop;
221
Lorenzo Bianconi988dc4a2019-04-09 11:47:20 +0200222 return -proto_ctype;
Tom Herbert5024c332014-11-04 09:06:53 -0800223
Tom Herbert37dd0242014-10-03 15:48:09 -0700224drop:
225 kfree_skb(skb);
226 return 0;
227}
228
David Millerd4546c22018-06-24 14:13:49 +0900229static struct sk_buff *fou_gro_receive(struct sock *sk,
230 struct list_head *head,
231 struct sk_buff *skb)
Tom Herbertafe93322014-09-17 12:25:57 -0700232{
Tom Herbertd92283e2016-04-05 08:22:54 -0700233 u8 proto = fou_from_sock(sk)->protocol;
Tom Herbertefc98d02014-10-03 15:48:08 -0700234 const struct net_offload **offloads;
David Millerd4546c22018-06-24 14:13:49 +0900235 const struct net_offload *ops;
236 struct sk_buff *pp = NULL;
Tom Herbertafe93322014-09-17 12:25:57 -0700237
Alexander Duyckc3483382016-03-29 14:55:22 -0700238 /* We can clear the encap_mark for FOU as we are essentially doing
239 * one of two possible things. We are either adding an L4 tunnel
Randy Dunlap2bdcc732020-08-22 16:31:41 -0700240 * header to the outer L3 tunnel header, or we are simply
Alexander Duyckc3483382016-03-29 14:55:22 -0700241 * treating the GRE tunnel header as though it is a UDP protocol
242 * specific header such as VXLAN or GENEVE.
243 */
244 NAPI_GRO_CB(skb)->encap_mark = 0;
245
Alexander Duycka0ca1532016-04-05 09:13:39 -0700246 /* Flag this frame as already having an outer encap header */
247 NAPI_GRO_CB(skb)->is_fou = 1;
248
Tom Herbertafe93322014-09-17 12:25:57 -0700249 rcu_read_lock();
Tom Herbertefc98d02014-10-03 15:48:08 -0700250 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
Tom Herbertafe93322014-09-17 12:25:57 -0700251 ops = rcu_dereference(offloads[proto]);
252 if (!ops || !ops->callbacks.gro_receive)
253 goto out_unlock;
254
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200255 pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
Tom Herbertafe93322014-09-17 12:25:57 -0700256
257out_unlock:
258 rcu_read_unlock();
259
260 return pp;
261}
262
Tom Herbertd92283e2016-04-05 08:22:54 -0700263static int fou_gro_complete(struct sock *sk, struct sk_buff *skb,
264 int nhoff)
Tom Herbertafe93322014-09-17 12:25:57 -0700265{
266 const struct net_offload *ops;
Tom Herbertd92283e2016-04-05 08:22:54 -0700267 u8 proto = fou_from_sock(sk)->protocol;
Tom Herbertafe93322014-09-17 12:25:57 -0700268 int err = -ENOSYS;
Tom Herbertefc98d02014-10-03 15:48:08 -0700269 const struct net_offload **offloads;
Tom Herbertafe93322014-09-17 12:25:57 -0700270
271 rcu_read_lock();
Tom Herbertefc98d02014-10-03 15:48:08 -0700272 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
Tom Herbertafe93322014-09-17 12:25:57 -0700273 ops = rcu_dereference(offloads[proto]);
274 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
275 goto out_unlock;
276
277 err = ops->callbacks.gro_complete(skb, nhoff);
278
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700279 skb_set_inner_mac_header(skb, nhoff);
280
Tom Herbertafe93322014-09-17 12:25:57 -0700281out_unlock:
282 rcu_read_unlock();
283
284 return err;
285}
286
Tom Herberta8d31c12014-11-04 09:06:57 -0800287static struct guehdr *gue_gro_remcsum(struct sk_buff *skb, unsigned int off,
288 struct guehdr *guehdr, void *data,
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700289 size_t hdrlen, struct gro_remcsum *grc,
290 bool nopartial)
Tom Herberta8d31c12014-11-04 09:06:57 -0800291{
292 __be16 *pd = data;
Tom Herbert4fd671d2014-11-25 11:21:20 -0800293 size_t start = ntohs(pd[0]);
294 size_t offset = ntohs(pd[1]);
Tom Herberta8d31c12014-11-04 09:06:57 -0800295
296 if (skb->remcsum_offload)
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700297 return guehdr;
Tom Herberta8d31c12014-11-04 09:06:57 -0800298
Tom Herbert4fd671d2014-11-25 11:21:20 -0800299 if (!NAPI_GRO_CB(skb)->csum_valid)
Tom Herberta8d31c12014-11-04 09:06:57 -0800300 return NULL;
301
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700302 guehdr = skb_gro_remcsum_process(skb, (void *)guehdr, off, hdrlen,
303 start, offset, grc, nopartial);
Tom Herberta8d31c12014-11-04 09:06:57 -0800304
305 skb->remcsum_offload = 1;
306
307 return guehdr;
308}
309
David Millerd4546c22018-06-24 14:13:49 +0900310static struct sk_buff *gue_gro_receive(struct sock *sk,
311 struct list_head *head,
312 struct sk_buff *skb)
Tom Herbert37dd0242014-10-03 15:48:09 -0700313{
314 const struct net_offload **offloads;
315 const struct net_offload *ops;
David Millerd4546c22018-06-24 14:13:49 +0900316 struct sk_buff *pp = NULL;
Tom Herbert37dd0242014-10-03 15:48:09 -0700317 struct sk_buff *p;
Tom Herbert37dd0242014-10-03 15:48:09 -0700318 struct guehdr *guehdr;
Tom Herbert5024c332014-11-04 09:06:53 -0800319 size_t len, optlen, hdrlen, off;
320 void *data;
Tom Herberta8d31c12014-11-04 09:06:57 -0800321 u16 doffset = 0;
Tom Herbert37dd0242014-10-03 15:48:09 -0700322 int flush = 1;
Tom Herbertd92283e2016-04-05 08:22:54 -0700323 struct fou *fou = fou_from_sock(sk);
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800324 struct gro_remcsum grc;
Tom Herbertc1e48af2016-06-06 16:06:02 -0700325 u8 proto;
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800326
327 skb_gro_remcsum_init(&grc);
Tom Herbert37dd0242014-10-03 15:48:09 -0700328
329 off = skb_gro_offset(skb);
Tom Herbert5024c332014-11-04 09:06:53 -0800330 len = off + sizeof(*guehdr);
331
Tom Herbert37dd0242014-10-03 15:48:09 -0700332 guehdr = skb_gro_header_fast(skb, off);
Tom Herbert5024c332014-11-04 09:06:53 -0800333 if (skb_gro_header_hard(skb, len)) {
334 guehdr = skb_gro_header_slow(skb, len, off);
Tom Herbert37dd0242014-10-03 15:48:09 -0700335 if (unlikely(!guehdr))
336 goto out;
337 }
338
Tom Herbertc1e48af2016-06-06 16:06:02 -0700339 switch (guehdr->version) {
340 case 0:
341 break;
342 case 1:
343 switch (((struct iphdr *)guehdr)->version) {
344 case 4:
345 proto = IPPROTO_IPIP;
346 break;
347 case 6:
348 proto = IPPROTO_IPV6;
349 break;
350 default:
351 goto out;
352 }
353 goto next_proto;
354 default:
355 goto out;
356 }
357
Tom Herbert5024c332014-11-04 09:06:53 -0800358 optlen = guehdr->hlen << 2;
359 len += optlen;
Tom Herbert37dd0242014-10-03 15:48:09 -0700360
Tom Herbert5024c332014-11-04 09:06:53 -0800361 if (skb_gro_header_hard(skb, len)) {
362 guehdr = skb_gro_header_slow(skb, len, off);
Tom Herbert37dd0242014-10-03 15:48:09 -0700363 if (unlikely(!guehdr))
Tom Herbert5024c332014-11-04 09:06:53 -0800364 goto out;
365 }
366
367 if (unlikely(guehdr->control) || guehdr->version != 0 ||
368 validate_gue_flags(guehdr, optlen))
369 goto out;
370
371 hdrlen = sizeof(*guehdr) + optlen;
372
Tom Herberta8d31c12014-11-04 09:06:57 -0800373 /* Adjust NAPI_GRO_CB(skb)->csum to account for guehdr,
374 * this is needed if there is a remote checkcsum offload.
375 */
Tom Herbert5024c332014-11-04 09:06:53 -0800376 skb_gro_postpull_rcsum(skb, guehdr, hdrlen);
377
378 data = &guehdr[1];
379
380 if (guehdr->flags & GUE_FLAG_PRIV) {
Tom Herberta8d31c12014-11-04 09:06:57 -0800381 __be32 flags = *(__be32 *)(data + doffset);
Tom Herbert5024c332014-11-04 09:06:53 -0800382
Tom Herberta8d31c12014-11-04 09:06:57 -0800383 doffset += GUE_LEN_PRIV;
384
385 if (flags & GUE_PFLAG_REMCSUM) {
386 guehdr = gue_gro_remcsum(skb, off, guehdr,
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700387 data + doffset, hdrlen, &grc,
Tom Herbertfe881ef2015-02-10 16:30:33 -0800388 !!(fou->flags &
389 FOU_F_REMCSUM_NOPARTIAL));
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700390
Tom Herberta8d31c12014-11-04 09:06:57 -0800391 if (!guehdr)
392 goto out;
393
394 data = &guehdr[1];
395
396 doffset += GUE_PLEN_REMCSUM;
397 }
Tom Herbert37dd0242014-10-03 15:48:09 -0700398 }
399
Tom Herberta8d31c12014-11-04 09:06:57 -0800400 skb_gro_pull(skb, hdrlen);
401
David Millerd4546c22018-06-24 14:13:49 +0900402 list_for_each_entry(p, head, list) {
Tom Herbert37dd0242014-10-03 15:48:09 -0700403 const struct guehdr *guehdr2;
404
405 if (!NAPI_GRO_CB(p)->same_flow)
406 continue;
407
408 guehdr2 = (struct guehdr *)(p->data + off);
409
410 /* Compare base GUE header to be equal (covers
Tom Herbert5024c332014-11-04 09:06:53 -0800411 * hlen, version, proto_ctype, and flags.
Tom Herbert37dd0242014-10-03 15:48:09 -0700412 */
413 if (guehdr->word != guehdr2->word) {
414 NAPI_GRO_CB(p)->same_flow = 0;
415 continue;
416 }
417
418 /* Compare optional fields are the same. */
419 if (guehdr->hlen && memcmp(&guehdr[1], &guehdr2[1],
420 guehdr->hlen << 2)) {
421 NAPI_GRO_CB(p)->same_flow = 0;
422 continue;
423 }
424 }
425
Tom Herbertc1e48af2016-06-06 16:06:02 -0700426 proto = guehdr->proto_ctype;
427
428next_proto:
429
Alexander Duyckc3483382016-03-29 14:55:22 -0700430 /* We can clear the encap_mark for GUE as we are essentially doing
431 * one of two possible things. We are either adding an L4 tunnel
Randy Dunlap2bdcc732020-08-22 16:31:41 -0700432 * header to the outer L3 tunnel header, or we are simply
Alexander Duyckc3483382016-03-29 14:55:22 -0700433 * treating the GRE tunnel header as though it is a UDP protocol
434 * specific header such as VXLAN or GENEVE.
435 */
436 NAPI_GRO_CB(skb)->encap_mark = 0;
437
Alexander Duycka0ca1532016-04-05 09:13:39 -0700438 /* Flag this frame as already having an outer encap header */
439 NAPI_GRO_CB(skb)->is_fou = 1;
440
Tom Herbert5024c332014-11-04 09:06:53 -0800441 rcu_read_lock();
442 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
Tom Herbertc1e48af2016-06-06 16:06:02 -0700443 ops = rcu_dereference(offloads[proto]);
Tom Herbert27013662015-08-19 17:07:34 -0700444 if (WARN_ON_ONCE(!ops || !ops->callbacks.gro_receive))
Tom Herbert5024c332014-11-04 09:06:53 -0800445 goto out_unlock;
Tom Herbert37dd0242014-10-03 15:48:09 -0700446
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200447 pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800448 flush = 0;
Tom Herbert37dd0242014-10-03 15:48:09 -0700449
450out_unlock:
451 rcu_read_unlock();
452out:
Sabrina Dubroca603d4cf2018-06-30 17:38:55 +0200453 skb_gro_flush_final_remcsum(skb, pp, flush, &grc);
Tom Herbert37dd0242014-10-03 15:48:09 -0700454
455 return pp;
456}
457
Tom Herbertd92283e2016-04-05 08:22:54 -0700458static int gue_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
Tom Herbert37dd0242014-10-03 15:48:09 -0700459{
460 const struct net_offload **offloads;
461 struct guehdr *guehdr = (struct guehdr *)(skb->data + nhoff);
462 const struct net_offload *ops;
Tom Herbertc1e48af2016-06-06 16:06:02 -0700463 unsigned int guehlen = 0;
Tom Herbert37dd0242014-10-03 15:48:09 -0700464 u8 proto;
465 int err = -ENOENT;
466
Tom Herbertc1e48af2016-06-06 16:06:02 -0700467 switch (guehdr->version) {
468 case 0:
469 proto = guehdr->proto_ctype;
470 guehlen = sizeof(*guehdr) + (guehdr->hlen << 2);
471 break;
472 case 1:
473 switch (((struct iphdr *)guehdr)->version) {
474 case 4:
475 proto = IPPROTO_IPIP;
476 break;
477 case 6:
478 proto = IPPROTO_IPV6;
479 break;
480 default:
481 return err;
482 }
483 break;
484 default:
485 return err;
486 }
Tom Herbert37dd0242014-10-03 15:48:09 -0700487
488 rcu_read_lock();
489 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
490 ops = rcu_dereference(offloads[proto]);
491 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
492 goto out_unlock;
493
494 err = ops->callbacks.gro_complete(skb, nhoff + guehlen);
495
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700496 skb_set_inner_mac_header(skb, nhoff + guehlen);
497
Tom Herbert37dd0242014-10-03 15:48:09 -0700498out_unlock:
499 rcu_read_unlock();
500 return err;
501}
502
Kristian Evensen1713cb32019-03-27 11:16:03 +0100503static bool fou_cfg_cmp(struct fou *fou, struct fou_cfg *cfg)
504{
505 struct sock *sk = fou->sock->sk;
506 struct udp_port_cfg *udp_cfg = &cfg->udp_config;
507
508 if (fou->family != udp_cfg->family ||
509 fou->port != udp_cfg->local_udp_port ||
510 sk->sk_dport != udp_cfg->peer_udp_port ||
511 sk->sk_bound_dev_if != udp_cfg->bind_ifindex)
512 return false;
513
514 if (fou->family == AF_INET) {
515 if (sk->sk_rcv_saddr != udp_cfg->local_ip.s_addr ||
516 sk->sk_daddr != udp_cfg->peer_ip.s_addr)
517 return false;
518 else
519 return true;
520#if IS_ENABLED(CONFIG_IPV6)
521 } else {
522 if (ipv6_addr_cmp(&sk->sk_v6_rcv_saddr, &udp_cfg->local_ip6) ||
523 ipv6_addr_cmp(&sk->sk_v6_daddr, &udp_cfg->peer_ip6))
524 return false;
525 else
526 return true;
527#endif
528 }
529
530 return false;
531}
532
533static int fou_add_to_port_list(struct net *net, struct fou *fou,
534 struct fou_cfg *cfg)
Tom Herbert234615512014-09-17 12:25:56 -0700535{
WANG Cong02d793c2015-04-10 12:00:29 -0700536 struct fou_net *fn = net_generic(net, fou_net_id);
Tom Herbert234615512014-09-17 12:25:56 -0700537 struct fou *fout;
538
WANG Cong02d793c2015-04-10 12:00:29 -0700539 mutex_lock(&fn->fou_lock);
540 list_for_each_entry(fout, &fn->fou_list, list) {
Kristian Evensen1713cb32019-03-27 11:16:03 +0100541 if (fou_cfg_cmp(fout, cfg)) {
WANG Cong02d793c2015-04-10 12:00:29 -0700542 mutex_unlock(&fn->fou_lock);
Tom Herbert234615512014-09-17 12:25:56 -0700543 return -EALREADY;
544 }
545 }
546
WANG Cong02d793c2015-04-10 12:00:29 -0700547 list_add(&fou->list, &fn->fou_list);
548 mutex_unlock(&fn->fou_lock);
Tom Herbert234615512014-09-17 12:25:56 -0700549
550 return 0;
551}
552
553static void fou_release(struct fou *fou)
554{
555 struct socket *sock = fou->sock;
Tom Herbert234615512014-09-17 12:25:56 -0700556
Tom Herbert234615512014-09-17 12:25:56 -0700557 list_del(&fou->list);
WANG Cong02d793c2015-04-10 12:00:29 -0700558 udp_tunnel_sock_release(sock);
Tom Herbert234615512014-09-17 12:25:56 -0700559
Hannes Frederic Sowa3036fac2015-12-15 21:01:53 +0100560 kfree_rcu(fou, rcu);
Tom Herbert234615512014-09-17 12:25:56 -0700561}
562
563static int fou_create(struct net *net, struct fou_cfg *cfg,
564 struct socket **sockp)
565{
Tom Herbert234615512014-09-17 12:25:56 -0700566 struct socket *sock = NULL;
WANG Cong02d793c2015-04-10 12:00:29 -0700567 struct fou *fou = NULL;
Tom Herbert234615512014-09-17 12:25:56 -0700568 struct sock *sk;
Tom Herbert440924b2016-05-18 09:06:14 -0700569 struct udp_tunnel_sock_cfg tunnel_cfg;
WANG Cong02d793c2015-04-10 12:00:29 -0700570 int err;
Tom Herbert234615512014-09-17 12:25:56 -0700571
572 /* Open UDP socket */
573 err = udp_sock_create(net, &cfg->udp_config, &sock);
574 if (err < 0)
575 goto error;
576
577 /* Allocate FOU port structure */
578 fou = kzalloc(sizeof(*fou), GFP_KERNEL);
579 if (!fou) {
580 err = -ENOMEM;
581 goto error;
582 }
583
584 sk = sock->sk;
585
Tom Herbert37dd0242014-10-03 15:48:09 -0700586 fou->port = cfg->udp_config.local_udp_port;
Tom Herbert5f914b62016-05-18 09:06:16 -0700587 fou->family = cfg->udp_config.family;
588 fou->flags = cfg->flags;
Tom Herbert440924b2016-05-18 09:06:14 -0700589 fou->type = cfg->type;
590 fou->sock = sock;
591
592 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
593 tunnel_cfg.encap_type = 1;
594 tunnel_cfg.sk_user_data = fou;
595 tunnel_cfg.encap_destroy = NULL;
Tom Herbert37dd0242014-10-03 15:48:09 -0700596
597 /* Initial for fou type */
598 switch (cfg->type) {
599 case FOU_ENCAP_DIRECT:
Tom Herbert440924b2016-05-18 09:06:14 -0700600 tunnel_cfg.encap_rcv = fou_udp_recv;
601 tunnel_cfg.gro_receive = fou_gro_receive;
602 tunnel_cfg.gro_complete = fou_gro_complete;
603 fou->protocol = cfg->protocol;
Tom Herbert37dd0242014-10-03 15:48:09 -0700604 break;
605 case FOU_ENCAP_GUE:
Tom Herbert440924b2016-05-18 09:06:14 -0700606 tunnel_cfg.encap_rcv = gue_udp_recv;
607 tunnel_cfg.gro_receive = gue_gro_receive;
608 tunnel_cfg.gro_complete = gue_gro_complete;
Tom Herbert37dd0242014-10-03 15:48:09 -0700609 break;
610 default:
611 err = -EINVAL;
612 goto error;
613 }
Tom Herbert234615512014-09-17 12:25:56 -0700614
Tom Herbert440924b2016-05-18 09:06:14 -0700615 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Tom Herbert234615512014-09-17 12:25:56 -0700616
617 sk->sk_allocation = GFP_ATOMIC;
618
Kristian Evensen1713cb32019-03-27 11:16:03 +0100619 err = fou_add_to_port_list(net, fou, cfg);
Tom Herbert234615512014-09-17 12:25:56 -0700620 if (err)
621 goto error;
622
623 if (sockp)
624 *sockp = sock;
625
626 return 0;
627
628error:
629 kfree(fou);
630 if (sock)
WANG Cong02d793c2015-04-10 12:00:29 -0700631 udp_tunnel_sock_release(sock);
Tom Herbert234615512014-09-17 12:25:56 -0700632
633 return err;
634}
635
636static int fou_destroy(struct net *net, struct fou_cfg *cfg)
637{
WANG Cong02d793c2015-04-10 12:00:29 -0700638 struct fou_net *fn = net_generic(net, fou_net_id);
Tom Herbert234615512014-09-17 12:25:56 -0700639 int err = -EINVAL;
WANG Cong02d793c2015-04-10 12:00:29 -0700640 struct fou *fou;
Tom Herbert234615512014-09-17 12:25:56 -0700641
WANG Cong02d793c2015-04-10 12:00:29 -0700642 mutex_lock(&fn->fou_lock);
643 list_for_each_entry(fou, &fn->fou_list, list) {
Kristian Evensen1713cb32019-03-27 11:16:03 +0100644 if (fou_cfg_cmp(fou, cfg)) {
Tom Herbert234615512014-09-17 12:25:56 -0700645 fou_release(fou);
646 err = 0;
647 break;
648 }
649 }
WANG Cong02d793c2015-04-10 12:00:29 -0700650 mutex_unlock(&fn->fou_lock);
Tom Herbert234615512014-09-17 12:25:56 -0700651
652 return err;
653}
654
Johannes Berg489111e2016-10-24 14:40:03 +0200655static struct genl_family fou_nl_family;
Tom Herbert234615512014-09-17 12:25:56 -0700656
stephen hemminger3f18ff22016-08-31 15:19:37 -0700657static const struct nla_policy fou_nl_policy[FOU_ATTR_MAX + 1] = {
Kristian Evensen1713cb32019-03-27 11:16:03 +0100658 [FOU_ATTR_PORT] = { .type = NLA_U16, },
659 [FOU_ATTR_AF] = { .type = NLA_U8, },
660 [FOU_ATTR_IPPROTO] = { .type = NLA_U8, },
661 [FOU_ATTR_TYPE] = { .type = NLA_U8, },
662 [FOU_ATTR_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG, },
663 [FOU_ATTR_LOCAL_V4] = { .type = NLA_U32, },
664 [FOU_ATTR_PEER_V4] = { .type = NLA_U32, },
Kristian Evensenbb48eb92020-01-23 13:20:18 +0100665 [FOU_ATTR_LOCAL_V6] = { .len = sizeof(struct in6_addr), },
666 [FOU_ATTR_PEER_V6] = { .len = sizeof(struct in6_addr), },
Kristian Evensen1713cb32019-03-27 11:16:03 +0100667 [FOU_ATTR_PEER_PORT] = { .type = NLA_U16, },
668 [FOU_ATTR_IFINDEX] = { .type = NLA_S32, },
Tom Herbert234615512014-09-17 12:25:56 -0700669};
670
671static int parse_nl_config(struct genl_info *info,
672 struct fou_cfg *cfg)
673{
Kristian Evensen1713cb32019-03-27 11:16:03 +0100674 bool has_local = false, has_peer = false;
675 struct nlattr *attr;
676 int ifindex;
677 __be16 port;
678
Tom Herbert234615512014-09-17 12:25:56 -0700679 memset(cfg, 0, sizeof(*cfg));
680
681 cfg->udp_config.family = AF_INET;
682
683 if (info->attrs[FOU_ATTR_AF]) {
684 u8 family = nla_get_u8(info->attrs[FOU_ATTR_AF]);
685
Tom Herbert5f914b62016-05-18 09:06:16 -0700686 switch (family) {
687 case AF_INET:
688 break;
689 case AF_INET6:
690 cfg->udp_config.ipv6_v6only = 1;
691 break;
692 default:
693 return -EAFNOSUPPORT;
694 }
Tom Herbert234615512014-09-17 12:25:56 -0700695
696 cfg->udp_config.family = family;
697 }
698
699 if (info->attrs[FOU_ATTR_PORT]) {
Kristian Evensen1713cb32019-03-27 11:16:03 +0100700 port = nla_get_be16(info->attrs[FOU_ATTR_PORT]);
Tom Herbert234615512014-09-17 12:25:56 -0700701 cfg->udp_config.local_udp_port = port;
702 }
703
704 if (info->attrs[FOU_ATTR_IPPROTO])
705 cfg->protocol = nla_get_u8(info->attrs[FOU_ATTR_IPPROTO]);
706
Tom Herbert37dd0242014-10-03 15:48:09 -0700707 if (info->attrs[FOU_ATTR_TYPE])
708 cfg->type = nla_get_u8(info->attrs[FOU_ATTR_TYPE]);
709
Tom Herbertfe881ef2015-02-10 16:30:33 -0800710 if (info->attrs[FOU_ATTR_REMCSUM_NOPARTIAL])
711 cfg->flags |= FOU_F_REMCSUM_NOPARTIAL;
712
Kristian Evensen1713cb32019-03-27 11:16:03 +0100713 if (cfg->udp_config.family == AF_INET) {
714 if (info->attrs[FOU_ATTR_LOCAL_V4]) {
715 attr = info->attrs[FOU_ATTR_LOCAL_V4];
716 cfg->udp_config.local_ip.s_addr = nla_get_in_addr(attr);
717 has_local = true;
718 }
719
720 if (info->attrs[FOU_ATTR_PEER_V4]) {
721 attr = info->attrs[FOU_ATTR_PEER_V4];
722 cfg->udp_config.peer_ip.s_addr = nla_get_in_addr(attr);
723 has_peer = true;
724 }
725#if IS_ENABLED(CONFIG_IPV6)
726 } else {
727 if (info->attrs[FOU_ATTR_LOCAL_V6]) {
728 attr = info->attrs[FOU_ATTR_LOCAL_V6];
729 cfg->udp_config.local_ip6 = nla_get_in6_addr(attr);
730 has_local = true;
731 }
732
733 if (info->attrs[FOU_ATTR_PEER_V6]) {
734 attr = info->attrs[FOU_ATTR_PEER_V6];
735 cfg->udp_config.peer_ip6 = nla_get_in6_addr(attr);
736 has_peer = true;
737 }
738#endif
739 }
740
741 if (has_peer) {
742 if (info->attrs[FOU_ATTR_PEER_PORT]) {
743 port = nla_get_be16(info->attrs[FOU_ATTR_PEER_PORT]);
744 cfg->udp_config.peer_udp_port = port;
745 } else {
746 return -EINVAL;
747 }
748 }
749
750 if (info->attrs[FOU_ATTR_IFINDEX]) {
751 if (!has_local)
752 return -EINVAL;
753
754 ifindex = nla_get_s32(info->attrs[FOU_ATTR_IFINDEX]);
755
756 cfg->udp_config.bind_ifindex = ifindex;
757 }
758
Tom Herbert234615512014-09-17 12:25:56 -0700759 return 0;
760}
761
762static int fou_nl_cmd_add_port(struct sk_buff *skb, struct genl_info *info)
763{
WANG Cong02d793c2015-04-10 12:00:29 -0700764 struct net *net = genl_info_net(info);
Tom Herbert234615512014-09-17 12:25:56 -0700765 struct fou_cfg cfg;
766 int err;
767
768 err = parse_nl_config(info, &cfg);
769 if (err)
770 return err;
771
WANG Cong02d793c2015-04-10 12:00:29 -0700772 return fou_create(net, &cfg, NULL);
Tom Herbert234615512014-09-17 12:25:56 -0700773}
774
775static int fou_nl_cmd_rm_port(struct sk_buff *skb, struct genl_info *info)
776{
WANG Cong02d793c2015-04-10 12:00:29 -0700777 struct net *net = genl_info_net(info);
Tom Herbert234615512014-09-17 12:25:56 -0700778 struct fou_cfg cfg;
WANG Cong67270632015-04-10 12:00:27 -0700779 int err;
Tom Herbert234615512014-09-17 12:25:56 -0700780
WANG Cong67270632015-04-10 12:00:27 -0700781 err = parse_nl_config(info, &cfg);
782 if (err)
783 return err;
Tom Herbert234615512014-09-17 12:25:56 -0700784
WANG Cong02d793c2015-04-10 12:00:29 -0700785 return fou_destroy(net, &cfg);
Tom Herbert234615512014-09-17 12:25:56 -0700786}
787
WANG Cong7a6c8c32015-04-10 12:00:30 -0700788static int fou_fill_info(struct fou *fou, struct sk_buff *msg)
789{
Kristian Evensen1713cb32019-03-27 11:16:03 +0100790 struct sock *sk = fou->sock->sk;
791
WANG Cong7a6c8c32015-04-10 12:00:30 -0700792 if (nla_put_u8(msg, FOU_ATTR_AF, fou->sock->sk->sk_family) ||
793 nla_put_be16(msg, FOU_ATTR_PORT, fou->port) ||
Kristian Evensen1713cb32019-03-27 11:16:03 +0100794 nla_put_be16(msg, FOU_ATTR_PEER_PORT, sk->sk_dport) ||
WANG Cong7a6c8c32015-04-10 12:00:30 -0700795 nla_put_u8(msg, FOU_ATTR_IPPROTO, fou->protocol) ||
Kristian Evensen1713cb32019-03-27 11:16:03 +0100796 nla_put_u8(msg, FOU_ATTR_TYPE, fou->type) ||
797 nla_put_s32(msg, FOU_ATTR_IFINDEX, sk->sk_bound_dev_if))
WANG Cong7a6c8c32015-04-10 12:00:30 -0700798 return -1;
799
800 if (fou->flags & FOU_F_REMCSUM_NOPARTIAL)
801 if (nla_put_flag(msg, FOU_ATTR_REMCSUM_NOPARTIAL))
802 return -1;
Kristian Evensen1713cb32019-03-27 11:16:03 +0100803
804 if (fou->sock->sk->sk_family == AF_INET) {
805 if (nla_put_in_addr(msg, FOU_ATTR_LOCAL_V4, sk->sk_rcv_saddr))
806 return -1;
807
808 if (nla_put_in_addr(msg, FOU_ATTR_PEER_V4, sk->sk_daddr))
809 return -1;
810#if IS_ENABLED(CONFIG_IPV6)
811 } else {
812 if (nla_put_in6_addr(msg, FOU_ATTR_LOCAL_V6,
813 &sk->sk_v6_rcv_saddr))
814 return -1;
815
816 if (nla_put_in6_addr(msg, FOU_ATTR_PEER_V6, &sk->sk_v6_daddr))
817 return -1;
818#endif
819 }
820
WANG Cong7a6c8c32015-04-10 12:00:30 -0700821 return 0;
822}
823
824static int fou_dump_info(struct fou *fou, u32 portid, u32 seq,
825 u32 flags, struct sk_buff *skb, u8 cmd)
826{
827 void *hdr;
828
829 hdr = genlmsg_put(skb, portid, seq, &fou_nl_family, flags, cmd);
830 if (!hdr)
831 return -ENOMEM;
832
833 if (fou_fill_info(fou, skb) < 0)
834 goto nla_put_failure;
835
836 genlmsg_end(skb, hdr);
837 return 0;
838
839nla_put_failure:
840 genlmsg_cancel(skb, hdr);
841 return -EMSGSIZE;
842}
843
844static int fou_nl_cmd_get_port(struct sk_buff *skb, struct genl_info *info)
845{
846 struct net *net = genl_info_net(info);
847 struct fou_net *fn = net_generic(net, fou_net_id);
848 struct sk_buff *msg;
849 struct fou_cfg cfg;
850 struct fou *fout;
851 __be16 port;
Tom Herbert5f914b62016-05-18 09:06:16 -0700852 u8 family;
WANG Cong7a6c8c32015-04-10 12:00:30 -0700853 int ret;
854
855 ret = parse_nl_config(info, &cfg);
856 if (ret)
857 return ret;
858 port = cfg.udp_config.local_udp_port;
859 if (port == 0)
860 return -EINVAL;
861
Tom Herbert5f914b62016-05-18 09:06:16 -0700862 family = cfg.udp_config.family;
863 if (family != AF_INET && family != AF_INET6)
864 return -EINVAL;
865
WANG Cong7a6c8c32015-04-10 12:00:30 -0700866 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
867 if (!msg)
868 return -ENOMEM;
869
870 ret = -ESRCH;
871 mutex_lock(&fn->fou_lock);
872 list_for_each_entry(fout, &fn->fou_list, list) {
Kristian Evensen1713cb32019-03-27 11:16:03 +0100873 if (fou_cfg_cmp(fout, &cfg)) {
WANG Cong7a6c8c32015-04-10 12:00:30 -0700874 ret = fou_dump_info(fout, info->snd_portid,
875 info->snd_seq, 0, msg,
876 info->genlhdr->cmd);
877 break;
878 }
879 }
880 mutex_unlock(&fn->fou_lock);
881 if (ret < 0)
882 goto out_free;
883
884 return genlmsg_reply(msg, info);
885
886out_free:
887 nlmsg_free(msg);
888 return ret;
889}
890
891static int fou_nl_dump(struct sk_buff *skb, struct netlink_callback *cb)
892{
893 struct net *net = sock_net(skb->sk);
894 struct fou_net *fn = net_generic(net, fou_net_id);
895 struct fou *fout;
896 int idx = 0, ret;
897
898 mutex_lock(&fn->fou_lock);
899 list_for_each_entry(fout, &fn->fou_list, list) {
900 if (idx++ < cb->args[0])
901 continue;
902 ret = fou_dump_info(fout, NETLINK_CB(cb->skb).portid,
903 cb->nlh->nlmsg_seq, NLM_F_MULTI,
904 skb, FOU_CMD_GET);
905 if (ret)
WANG Cong540207a2015-04-15 11:48:49 -0700906 break;
WANG Cong7a6c8c32015-04-10 12:00:30 -0700907 }
908 mutex_unlock(&fn->fou_lock);
909
WANG Cong7a6c8c32015-04-10 12:00:30 -0700910 cb->args[0] = idx;
911 return skb->len;
912}
913
Jakub Kicinski66a9b922020-10-02 14:49:54 -0700914static const struct genl_small_ops fou_nl_ops[] = {
Tom Herbert234615512014-09-17 12:25:56 -0700915 {
916 .cmd = FOU_CMD_ADD,
Johannes Bergef6243a2019-04-26 14:07:31 +0200917 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Tom Herbert234615512014-09-17 12:25:56 -0700918 .doit = fou_nl_cmd_add_port,
Tom Herbert234615512014-09-17 12:25:56 -0700919 .flags = GENL_ADMIN_PERM,
920 },
921 {
922 .cmd = FOU_CMD_DEL,
Johannes Bergef6243a2019-04-26 14:07:31 +0200923 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Tom Herbert234615512014-09-17 12:25:56 -0700924 .doit = fou_nl_cmd_rm_port,
Tom Herbert234615512014-09-17 12:25:56 -0700925 .flags = GENL_ADMIN_PERM,
926 },
WANG Cong7a6c8c32015-04-10 12:00:30 -0700927 {
928 .cmd = FOU_CMD_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +0200929 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
WANG Cong7a6c8c32015-04-10 12:00:30 -0700930 .doit = fou_nl_cmd_get_port,
931 .dumpit = fou_nl_dump,
WANG Cong7a6c8c32015-04-10 12:00:30 -0700932 },
Tom Herbert234615512014-09-17 12:25:56 -0700933};
934
Johannes Berg56989f62016-10-24 14:40:05 +0200935static struct genl_family fou_nl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +0200936 .hdrsize = 0,
937 .name = FOU_GENL_NAME,
938 .version = FOU_GENL_VERSION,
939 .maxattr = FOU_ATTR_MAX,
Johannes Berg3b0f31f2019-03-21 22:51:02 +0100940 .policy = fou_nl_policy,
Johannes Berg489111e2016-10-24 14:40:03 +0200941 .netnsok = true,
942 .module = THIS_MODULE,
Jakub Kicinski66a9b922020-10-02 14:49:54 -0700943 .small_ops = fou_nl_ops,
944 .n_small_ops = ARRAY_SIZE(fou_nl_ops),
Johannes Berg489111e2016-10-24 14:40:03 +0200945};
946
Tom Herberta8c5f902014-11-12 11:54:09 -0800947size_t fou_encap_hlen(struct ip_tunnel_encap *e)
948{
949 return sizeof(struct udphdr);
950}
951EXPORT_SYMBOL(fou_encap_hlen);
952
953size_t gue_encap_hlen(struct ip_tunnel_encap *e)
954{
955 size_t len;
956 bool need_priv = false;
957
958 len = sizeof(struct udphdr) + sizeof(struct guehdr);
959
960 if (e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) {
961 len += GUE_PLEN_REMCSUM;
962 need_priv = true;
963 }
964
965 len += need_priv ? GUE_LEN_PRIV : 0;
966
967 return len;
968}
969EXPORT_SYMBOL(gue_encap_hlen);
970
Tom Herbertdc969b82016-05-18 09:06:15 -0700971int __fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
972 u8 *protocol, __be16 *sport, int type)
973{
974 int err;
975
976 err = iptunnel_handle_offloads(skb, type);
977 if (err)
978 return err;
979
980 *sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
981 skb, 0, 0, false);
982
983 return 0;
984}
985EXPORT_SYMBOL(__fou_build_header);
986
Tom Herbertdc969b82016-05-18 09:06:15 -0700987int __gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
988 u8 *protocol, __be16 *sport, int type)
Tom Herbert63487ba2014-11-04 09:06:51 -0800989{
Tom Herbert63487ba2014-11-04 09:06:51 -0800990 struct guehdr *guehdr;
Tom Herbertb17f7092014-11-04 09:06:56 -0800991 size_t hdrlen, optlen = 0;
Tom Herbert5024c332014-11-04 09:06:53 -0800992 void *data;
993 bool need_priv = false;
Alexander Duyckaed069d2016-04-14 15:33:37 -0400994 int err;
Tom Herbert5024c332014-11-04 09:06:53 -0800995
Tom Herbertb17f7092014-11-04 09:06:56 -0800996 if ((e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) &&
997 skb->ip_summed == CHECKSUM_PARTIAL) {
Tom Herbertb17f7092014-11-04 09:06:56 -0800998 optlen += GUE_PLEN_REMCSUM;
999 type |= SKB_GSO_TUNNEL_REMCSUM;
1000 need_priv = true;
1001 }
1002
Tom Herbert5024c332014-11-04 09:06:53 -08001003 optlen += need_priv ? GUE_LEN_PRIV : 0;
Tom Herbert63487ba2014-11-04 09:06:51 -08001004
Alexander Duyckaed069d2016-04-14 15:33:37 -04001005 err = iptunnel_handle_offloads(skb, type);
1006 if (err)
1007 return err;
Tom Herbert63487ba2014-11-04 09:06:51 -08001008
1009 /* Get source port (based on flow hash) before skb_push */
Tom Herbertdc969b82016-05-18 09:06:15 -07001010 *sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
1011 skb, 0, 0, false);
Tom Herbert63487ba2014-11-04 09:06:51 -08001012
Tom Herbertb17f7092014-11-04 09:06:56 -08001013 hdrlen = sizeof(struct guehdr) + optlen;
1014
1015 skb_push(skb, hdrlen);
Tom Herbert63487ba2014-11-04 09:06:51 -08001016
1017 guehdr = (struct guehdr *)skb->data;
1018
Tom Herbert5024c332014-11-04 09:06:53 -08001019 guehdr->control = 0;
Tom Herbert63487ba2014-11-04 09:06:51 -08001020 guehdr->version = 0;
Tom Herbert5024c332014-11-04 09:06:53 -08001021 guehdr->hlen = optlen >> 2;
Tom Herbert63487ba2014-11-04 09:06:51 -08001022 guehdr->flags = 0;
Tom Herbert5024c332014-11-04 09:06:53 -08001023 guehdr->proto_ctype = *protocol;
1024
1025 data = &guehdr[1];
1026
1027 if (need_priv) {
1028 __be32 *flags = data;
1029
1030 guehdr->flags |= GUE_FLAG_PRIV;
1031 *flags = 0;
1032 data += GUE_LEN_PRIV;
1033
Tom Herbertb17f7092014-11-04 09:06:56 -08001034 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1035 u16 csum_start = skb_checksum_start_offset(skb);
1036 __be16 *pd = data;
1037
1038 if (csum_start < hdrlen)
1039 return -EINVAL;
1040
1041 csum_start -= hdrlen;
1042 pd[0] = htons(csum_start);
1043 pd[1] = htons(csum_start + skb->csum_offset);
1044
1045 if (!skb_is_gso(skb)) {
1046 skb->ip_summed = CHECKSUM_NONE;
1047 skb->encapsulation = 0;
1048 }
1049
1050 *flags |= GUE_PFLAG_REMCSUM;
1051 data += GUE_PLEN_REMCSUM;
1052 }
1053
Tom Herbert5024c332014-11-04 09:06:53 -08001054 }
Tom Herbert63487ba2014-11-04 09:06:51 -08001055
Tom Herbertdc969b82016-05-18 09:06:15 -07001056 return 0;
1057}
1058EXPORT_SYMBOL(__gue_build_header);
1059
stephen hemminger9dc621a2017-05-19 09:55:54 -07001060#ifdef CONFIG_NET_FOU_IP_TUNNELS
1061
1062static void fou_build_udp(struct sk_buff *skb, struct ip_tunnel_encap *e,
1063 struct flowi4 *fl4, u8 *protocol, __be16 sport)
1064{
1065 struct udphdr *uh;
1066
1067 skb_push(skb, sizeof(struct udphdr));
1068 skb_reset_transport_header(skb);
1069
1070 uh = udp_hdr(skb);
1071
1072 uh->dest = e->dport;
1073 uh->source = sport;
1074 uh->len = htons(skb->len);
1075 udp_set_csum(!(e->flags & TUNNEL_ENCAP_FLAG_CSUM), skb,
1076 fl4->saddr, fl4->daddr, skb->len);
1077
1078 *protocol = IPPROTO_UDP;
1079}
1080
1081static int fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
1082 u8 *protocol, struct flowi4 *fl4)
1083{
1084 int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM ? SKB_GSO_UDP_TUNNEL_CSUM :
1085 SKB_GSO_UDP_TUNNEL;
1086 __be16 sport;
1087 int err;
1088
1089 err = __fou_build_header(skb, e, protocol, &sport, type);
1090 if (err)
1091 return err;
1092
1093 fou_build_udp(skb, e, fl4, protocol, sport);
1094
1095 return 0;
1096}
1097
1098static int gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
1099 u8 *protocol, struct flowi4 *fl4)
Tom Herbertdc969b82016-05-18 09:06:15 -07001100{
1101 int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM ? SKB_GSO_UDP_TUNNEL_CSUM :
1102 SKB_GSO_UDP_TUNNEL;
1103 __be16 sport;
1104 int err;
1105
1106 err = __gue_build_header(skb, e, protocol, &sport, type);
1107 if (err)
1108 return err;
1109
Tom Herbert63487ba2014-11-04 09:06:51 -08001110 fou_build_udp(skb, e, fl4, protocol, sport);
1111
1112 return 0;
1113}
Tom Herbert63487ba2014-11-04 09:06:51 -08001114
Stefano Briviob8a51b32018-11-08 12:19:23 +01001115static int gue_err_proto_handler(int proto, struct sk_buff *skb, u32 info)
1116{
1117 const struct net_protocol *ipprot = rcu_dereference(inet_protos[proto]);
1118
1119 if (ipprot && ipprot->err_handler) {
1120 if (!ipprot->err_handler(skb, info))
1121 return 0;
1122 }
1123
1124 return -ENOENT;
1125}
1126
1127static int gue_err(struct sk_buff *skb, u32 info)
1128{
1129 int transport_offset = skb_transport_offset(skb);
1130 struct guehdr *guehdr;
Eric Dumazet26fc1812019-01-11 06:27:35 -08001131 size_t len, optlen;
Stefano Briviob8a51b32018-11-08 12:19:23 +01001132 int ret;
1133
Eric Dumazet26fc1812019-01-11 06:27:35 -08001134 len = sizeof(struct udphdr) + sizeof(struct guehdr);
Eric Dumazet5355ed62019-03-06 10:41:00 -08001135 if (!pskb_may_pull(skb, transport_offset + len))
Stefano Briviob8a51b32018-11-08 12:19:23 +01001136 return -EINVAL;
1137
1138 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
1139
1140 switch (guehdr->version) {
1141 case 0: /* Full GUE header present */
1142 break;
1143 case 1: {
Simon Hormanc9d52f22019-04-09 09:59:07 +02001144 /* Direct encapsulation of IPv4 or IPv6 */
Stefano Briviob8a51b32018-11-08 12:19:23 +01001145 skb_set_transport_header(skb, -(int)sizeof(struct icmphdr));
1146
1147 switch (((struct iphdr *)guehdr)->version) {
1148 case 4:
1149 ret = gue_err_proto_handler(IPPROTO_IPIP, skb, info);
1150 goto out;
1151#if IS_ENABLED(CONFIG_IPV6)
1152 case 6:
1153 ret = gue_err_proto_handler(IPPROTO_IPV6, skb, info);
1154 goto out;
1155#endif
1156 default:
1157 ret = -EOPNOTSUPP;
1158 goto out;
1159 }
1160 }
1161 default: /* Undefined version */
1162 return -EOPNOTSUPP;
1163 }
1164
1165 if (guehdr->control)
1166 return -ENOENT;
1167
1168 optlen = guehdr->hlen << 2;
1169
Eric Dumazet5355ed62019-03-06 10:41:00 -08001170 if (!pskb_may_pull(skb, transport_offset + len + optlen))
Eric Dumazet26fc1812019-01-11 06:27:35 -08001171 return -EINVAL;
1172
1173 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
Stefano Briviob8a51b32018-11-08 12:19:23 +01001174 if (validate_gue_flags(guehdr, optlen))
1175 return -EINVAL;
1176
Stefano Brivio11789032018-12-18 00:13:17 +01001177 /* Handling exceptions for direct UDP encapsulation in GUE would lead to
1178 * recursion. Besides, this kind of encapsulation can't even be
1179 * configured currently. Discard this.
1180 */
Stefano Briviobc6e0192019-01-03 21:43:34 +01001181 if (guehdr->proto_ctype == IPPROTO_UDP ||
1182 guehdr->proto_ctype == IPPROTO_UDPLITE)
Stefano Brivio11789032018-12-18 00:13:17 +01001183 return -EOPNOTSUPP;
1184
Stefano Briviob8a51b32018-11-08 12:19:23 +01001185 skb_set_transport_header(skb, -(int)sizeof(struct icmphdr));
1186 ret = gue_err_proto_handler(guehdr->proto_ctype, skb, info);
1187
1188out:
1189 skb_set_transport_header(skb, transport_offset);
1190 return ret;
1191}
1192
Tom Herberta8c5f902014-11-12 11:54:09 -08001193
Andi Kleen5eeb2922015-04-08 06:04:31 -07001194static const struct ip_tunnel_encap_ops fou_iptun_ops = {
Tom Herberta8c5f902014-11-12 11:54:09 -08001195 .encap_hlen = fou_encap_hlen,
1196 .build_header = fou_build_header,
Stefano Briviob8a51b32018-11-08 12:19:23 +01001197 .err_handler = gue_err,
Tom Herberta8c5f902014-11-12 11:54:09 -08001198};
1199
Andi Kleen5eeb2922015-04-08 06:04:31 -07001200static const struct ip_tunnel_encap_ops gue_iptun_ops = {
Tom Herberta8c5f902014-11-12 11:54:09 -08001201 .encap_hlen = gue_encap_hlen,
1202 .build_header = gue_build_header,
Stefano Briviob8a51b32018-11-08 12:19:23 +01001203 .err_handler = gue_err,
Tom Herberta8c5f902014-11-12 11:54:09 -08001204};
1205
1206static int ip_tunnel_encap_add_fou_ops(void)
1207{
1208 int ret;
1209
1210 ret = ip_tunnel_encap_add_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
1211 if (ret < 0) {
1212 pr_err("can't add fou ops\n");
1213 return ret;
1214 }
1215
1216 ret = ip_tunnel_encap_add_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
1217 if (ret < 0) {
1218 pr_err("can't add gue ops\n");
1219 ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
1220 return ret;
1221 }
1222
1223 return 0;
1224}
1225
1226static void ip_tunnel_encap_del_fou_ops(void)
1227{
1228 ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
1229 ip_tunnel_encap_del_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
1230}
1231
1232#else
1233
1234static int ip_tunnel_encap_add_fou_ops(void)
1235{
1236 return 0;
1237}
1238
Thomas Graf882288c2014-11-13 12:48:21 +01001239static void ip_tunnel_encap_del_fou_ops(void)
Tom Herberta8c5f902014-11-12 11:54:09 -08001240{
1241}
1242
1243#endif
1244
WANG Cong02d793c2015-04-10 12:00:29 -07001245static __net_init int fou_init_net(struct net *net)
1246{
1247 struct fou_net *fn = net_generic(net, fou_net_id);
1248
1249 INIT_LIST_HEAD(&fn->fou_list);
1250 mutex_init(&fn->fou_lock);
1251 return 0;
1252}
1253
1254static __net_exit void fou_exit_net(struct net *net)
1255{
1256 struct fou_net *fn = net_generic(net, fou_net_id);
1257 struct fou *fou, *next;
1258
1259 /* Close all the FOU sockets */
1260 mutex_lock(&fn->fou_lock);
1261 list_for_each_entry_safe(fou, next, &fn->fou_list, list)
1262 fou_release(fou);
1263 mutex_unlock(&fn->fou_lock);
1264}
1265
1266static struct pernet_operations fou_net_ops = {
1267 .init = fou_init_net,
1268 .exit = fou_exit_net,
1269 .id = &fou_net_id,
1270 .size = sizeof(struct fou_net),
1271};
1272
Tom Herbert234615512014-09-17 12:25:56 -07001273static int __init fou_init(void)
1274{
1275 int ret;
1276
WANG Cong02d793c2015-04-10 12:00:29 -07001277 ret = register_pernet_device(&fou_net_ops);
1278 if (ret)
Tom Herberta8c5f902014-11-12 11:54:09 -08001279 goto exit;
1280
Johannes Berg489111e2016-10-24 14:40:03 +02001281 ret = genl_register_family(&fou_nl_family);
Tom Herberta8c5f902014-11-12 11:54:09 -08001282 if (ret < 0)
WANG Cong02d793c2015-04-10 12:00:29 -07001283 goto unregister;
Tom Herberta8c5f902014-11-12 11:54:09 -08001284
WANG Cong02d793c2015-04-10 12:00:29 -07001285 ret = ip_tunnel_encap_add_fou_ops();
1286 if (ret == 0)
1287 return 0;
1288
1289 genl_unregister_family(&fou_nl_family);
1290unregister:
1291 unregister_pernet_device(&fou_net_ops);
Tom Herberta8c5f902014-11-12 11:54:09 -08001292exit:
Tom Herbert234615512014-09-17 12:25:56 -07001293 return ret;
1294}
1295
1296static void __exit fou_fini(void)
1297{
Tom Herberta8c5f902014-11-12 11:54:09 -08001298 ip_tunnel_encap_del_fou_ops();
Tom Herbert234615512014-09-17 12:25:56 -07001299 genl_unregister_family(&fou_nl_family);
WANG Cong02d793c2015-04-10 12:00:29 -07001300 unregister_pernet_device(&fou_net_ops);
Tom Herbert234615512014-09-17 12:25:56 -07001301}
1302
1303module_init(fou_init);
1304module_exit(fou_fini);
1305MODULE_AUTHOR("Tom Herbert <therbert@google.com>");
1306MODULE_LICENSE("GPL");
Rob Gill67c20de2020-06-20 02:08:25 +00001307MODULE_DESCRIPTION("Foo over UDP");