blob: 0c9f171fb085e15c50416b5969e68c98569faecd [file] [log] [blame]
Tom Herbert234615512014-09-17 12:25:56 -07001#include <linux/module.h>
2#include <linux/errno.h>
3#include <linux/socket.h>
4#include <linux/skbuff.h>
5#include <linux/ip.h>
Stefano Briviob8a51b32018-11-08 12:19:23 +01006#include <linux/icmp.h>
Tom Herbert234615512014-09-17 12:25:56 -07007#include <linux/udp.h>
8#include <linux/types.h>
9#include <linux/kernel.h>
10#include <net/genetlink.h>
Tom Herbert37dd0242014-10-03 15:48:09 -070011#include <net/gue.h>
stephen hemminger9dc621a2017-05-19 09:55:54 -070012#include <net/fou.h>
Tom Herbert234615512014-09-17 12:25:56 -070013#include <net/ip.h>
Tom Herbertafe93322014-09-17 12:25:57 -070014#include <net/protocol.h>
Tom Herbert234615512014-09-17 12:25:56 -070015#include <net/udp.h>
16#include <net/udp_tunnel.h>
17#include <net/xfrm.h>
18#include <uapi/linux/fou.h>
19#include <uapi/linux/genetlink.h>
20
Tom Herbert234615512014-09-17 12:25:56 -070021struct fou {
22 struct socket *sock;
23 u8 protocol;
Tom Herbertfe881ef2015-02-10 16:30:33 -080024 u8 flags;
WANG Cong4cbcdf22015-04-10 12:00:28 -070025 __be16 port;
Tom Herbert5f914b62016-05-18 09:06:16 -070026 u8 family;
WANG Cong7a6c8c32015-04-10 12:00:30 -070027 u16 type;
Tom Herbert234615512014-09-17 12:25:56 -070028 struct list_head list;
Hannes Frederic Sowa3036fac2015-12-15 21:01:53 +010029 struct rcu_head rcu;
Tom Herbert234615512014-09-17 12:25:56 -070030};
31
Tom Herbertfe881ef2015-02-10 16:30:33 -080032#define FOU_F_REMCSUM_NOPARTIAL BIT(0)
33
Tom Herbert234615512014-09-17 12:25:56 -070034struct fou_cfg {
Tom Herbert37dd0242014-10-03 15:48:09 -070035 u16 type;
Tom Herbert234615512014-09-17 12:25:56 -070036 u8 protocol;
Tom Herbertfe881ef2015-02-10 16:30:33 -080037 u8 flags;
Tom Herbert234615512014-09-17 12:25:56 -070038 struct udp_port_cfg udp_config;
39};
40
WANG Cong02d793c2015-04-10 12:00:29 -070041static unsigned int fou_net_id;
42
43struct fou_net {
44 struct list_head fou_list;
45 struct mutex fou_lock;
46};
47
Tom Herbert234615512014-09-17 12:25:56 -070048static inline struct fou *fou_from_sock(struct sock *sk)
49{
50 return sk->sk_user_data;
51}
52
Tom Herbert5f914b62016-05-18 09:06:16 -070053static int fou_recv_pull(struct sk_buff *skb, struct fou *fou, size_t len)
Tom Herbert234615512014-09-17 12:25:56 -070054{
Tom Herbert234615512014-09-17 12:25:56 -070055 /* Remove 'len' bytes from the packet (UDP header and
Tom Herbert5024c332014-11-04 09:06:53 -080056 * FOU header if present).
Tom Herbert234615512014-09-17 12:25:56 -070057 */
Tom Herbert5f914b62016-05-18 09:06:16 -070058 if (fou->family == AF_INET)
59 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(skb)->tot_len) - len);
60 else
61 ipv6_hdr(skb)->payload_len =
62 htons(ntohs(ipv6_hdr(skb)->payload_len) - len);
63
Tom Herbert234615512014-09-17 12:25:56 -070064 __skb_pull(skb, len);
65 skb_postpull_rcsum(skb, udp_hdr(skb), len);
66 skb_reset_transport_header(skb);
Jesse Grossa09a4c82016-03-19 09:32:02 -070067 return iptunnel_pull_offloads(skb);
Tom Herbert234615512014-09-17 12:25:56 -070068}
69
70static int fou_udp_recv(struct sock *sk, struct sk_buff *skb)
71{
72 struct fou *fou = fou_from_sock(sk);
73
74 if (!fou)
75 return 1;
76
Tom Herbert5f914b62016-05-18 09:06:16 -070077 if (fou_recv_pull(skb, fou, sizeof(struct udphdr)))
Jesse Grossa09a4c82016-03-19 09:32:02 -070078 goto drop;
Tom Herbert5024c332014-11-04 09:06:53 -080079
80 return -fou->protocol;
Jesse Grossa09a4c82016-03-19 09:32:02 -070081
82drop:
83 kfree_skb(skb);
84 return 0;
Tom Herbert5024c332014-11-04 09:06:53 -080085}
86
Tom Herberta8d31c12014-11-04 09:06:57 -080087static struct guehdr *gue_remcsum(struct sk_buff *skb, struct guehdr *guehdr,
Tom Herbertfe881ef2015-02-10 16:30:33 -080088 void *data, size_t hdrlen, u8 ipproto,
89 bool nopartial)
Tom Herberta8d31c12014-11-04 09:06:57 -080090{
91 __be16 *pd = data;
Tom Herbert4fd671d2014-11-25 11:21:20 -080092 size_t start = ntohs(pd[0]);
93 size_t offset = ntohs(pd[1]);
Tom Herbertb7fe10e2015-08-19 17:07:32 -070094 size_t plen = sizeof(struct udphdr) + hdrlen +
95 max_t(size_t, offset + sizeof(u16), start);
96
97 if (skb->remcsum_offload)
98 return guehdr;
Tom Herberta8d31c12014-11-04 09:06:57 -080099
Tom Herberta8d31c12014-11-04 09:06:57 -0800100 if (!pskb_may_pull(skb, plen))
101 return NULL;
102 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
103
Tom Herbertfe881ef2015-02-10 16:30:33 -0800104 skb_remcsum_process(skb, (void *)guehdr + hdrlen,
105 start, offset, nopartial);
Tom Herberta8d31c12014-11-04 09:06:57 -0800106
107 return guehdr;
108}
109
Tom Herbert5024c332014-11-04 09:06:53 -0800110static int gue_control_message(struct sk_buff *skb, struct guehdr *guehdr)
111{
112 /* No support yet */
113 kfree_skb(skb);
114 return 0;
Tom Herbert234615512014-09-17 12:25:56 -0700115}
116
Tom Herbert37dd0242014-10-03 15:48:09 -0700117static int gue_udp_recv(struct sock *sk, struct sk_buff *skb)
118{
119 struct fou *fou = fou_from_sock(sk);
Tom Herbert5024c332014-11-04 09:06:53 -0800120 size_t len, optlen, hdrlen;
Tom Herbert37dd0242014-10-03 15:48:09 -0700121 struct guehdr *guehdr;
Tom Herbert5024c332014-11-04 09:06:53 -0800122 void *data;
Tom Herberta8d31c12014-11-04 09:06:57 -0800123 u16 doffset = 0;
Tom Herbert37dd0242014-10-03 15:48:09 -0700124
125 if (!fou)
126 return 1;
127
128 len = sizeof(struct udphdr) + sizeof(struct guehdr);
129 if (!pskb_may_pull(skb, len))
130 goto drop;
131
Tom Herbert5024c332014-11-04 09:06:53 -0800132 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
Tom Herbert37dd0242014-10-03 15:48:09 -0700133
Tom Herbertc1e48af2016-06-06 16:06:02 -0700134 switch (guehdr->version) {
135 case 0: /* Full GUE header present */
136 break;
137
138 case 1: {
139 /* Direct encasulation of IPv4 or IPv6 */
140
141 int prot;
142
143 switch (((struct iphdr *)guehdr)->version) {
144 case 4:
145 prot = IPPROTO_IPIP;
146 break;
147 case 6:
148 prot = IPPROTO_IPV6;
149 break;
150 default:
151 goto drop;
152 }
153
154 if (fou_recv_pull(skb, fou, sizeof(struct udphdr)))
155 goto drop;
156
157 return -prot;
158 }
159
160 default: /* Undefined version */
161 goto drop;
162 }
163
Tom Herbert5024c332014-11-04 09:06:53 -0800164 optlen = guehdr->hlen << 2;
165 len += optlen;
166
Tom Herbert37dd0242014-10-03 15:48:09 -0700167 if (!pskb_may_pull(skb, len))
168 goto drop;
169
Tom Herbert5024c332014-11-04 09:06:53 -0800170 /* guehdr may change after pull */
171 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
Li RongQingd8f00d22014-10-17 16:53:47 +0800172
Tom Herbert5024c332014-11-04 09:06:53 -0800173 hdrlen = sizeof(struct guehdr) + optlen;
174
175 if (guehdr->version != 0 || 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
Tom Herbert4fd671d2014-11-25 11:21:20 -0800215 __skb_pull(skb, sizeof(struct udphdr) + hdrlen);
Tom Herberta8d31c12014-11-04 09:06:57 -0800216 skb_reset_transport_header(skb);
217
Jesse Grossa09a4c82016-03-19 09:32:02 -0700218 if (iptunnel_pull_offloads(skb))
219 goto drop;
220
Tom Herbert5024c332014-11-04 09:06:53 -0800221 return -guehdr->proto_ctype;
222
Tom Herbert37dd0242014-10-03 15:48:09 -0700223drop:
224 kfree_skb(skb);
225 return 0;
226}
227
David Millerd4546c22018-06-24 14:13:49 +0900228static struct sk_buff *fou_gro_receive(struct sock *sk,
229 struct list_head *head,
230 struct sk_buff *skb)
Tom Herbertafe93322014-09-17 12:25:57 -0700231{
Tom Herbertd92283e2016-04-05 08:22:54 -0700232 u8 proto = fou_from_sock(sk)->protocol;
Tom Herbertefc98d02014-10-03 15:48:08 -0700233 const struct net_offload **offloads;
David Millerd4546c22018-06-24 14:13:49 +0900234 const struct net_offload *ops;
235 struct sk_buff *pp = NULL;
Tom Herbertafe93322014-09-17 12:25:57 -0700236
Alexander Duyckc3483382016-03-29 14:55:22 -0700237 /* We can clear the encap_mark for FOU as we are essentially doing
238 * one of two possible things. We are either adding an L4 tunnel
239 * header to the outer L3 tunnel header, or we are are simply
240 * treating the GRE tunnel header as though it is a UDP protocol
241 * specific header such as VXLAN or GENEVE.
242 */
243 NAPI_GRO_CB(skb)->encap_mark = 0;
244
Alexander Duycka0ca1532016-04-05 09:13:39 -0700245 /* Flag this frame as already having an outer encap header */
246 NAPI_GRO_CB(skb)->is_fou = 1;
247
Tom Herbertafe93322014-09-17 12:25:57 -0700248 rcu_read_lock();
Tom Herbertefc98d02014-10-03 15:48:08 -0700249 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
Tom Herbertafe93322014-09-17 12:25:57 -0700250 ops = rcu_dereference(offloads[proto]);
251 if (!ops || !ops->callbacks.gro_receive)
252 goto out_unlock;
253
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200254 pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
Tom Herbertafe93322014-09-17 12:25:57 -0700255
256out_unlock:
257 rcu_read_unlock();
258
259 return pp;
260}
261
Tom Herbertd92283e2016-04-05 08:22:54 -0700262static int fou_gro_complete(struct sock *sk, struct sk_buff *skb,
263 int nhoff)
Tom Herbertafe93322014-09-17 12:25:57 -0700264{
265 const struct net_offload *ops;
Tom Herbertd92283e2016-04-05 08:22:54 -0700266 u8 proto = fou_from_sock(sk)->protocol;
Tom Herbertafe93322014-09-17 12:25:57 -0700267 int err = -ENOSYS;
Tom Herbertefc98d02014-10-03 15:48:08 -0700268 const struct net_offload **offloads;
Tom Herbertafe93322014-09-17 12:25:57 -0700269
270 rcu_read_lock();
Tom Herbertefc98d02014-10-03 15:48:08 -0700271 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
Tom Herbertafe93322014-09-17 12:25:57 -0700272 ops = rcu_dereference(offloads[proto]);
273 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
274 goto out_unlock;
275
276 err = ops->callbacks.gro_complete(skb, nhoff);
277
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700278 skb_set_inner_mac_header(skb, nhoff);
279
Tom Herbertafe93322014-09-17 12:25:57 -0700280out_unlock:
281 rcu_read_unlock();
282
283 return err;
284}
285
Tom Herberta8d31c12014-11-04 09:06:57 -0800286static struct guehdr *gue_gro_remcsum(struct sk_buff *skb, unsigned int off,
287 struct guehdr *guehdr, void *data,
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700288 size_t hdrlen, struct gro_remcsum *grc,
289 bool nopartial)
Tom Herberta8d31c12014-11-04 09:06:57 -0800290{
291 __be16 *pd = data;
Tom Herbert4fd671d2014-11-25 11:21:20 -0800292 size_t start = ntohs(pd[0]);
293 size_t offset = ntohs(pd[1]);
Tom Herberta8d31c12014-11-04 09:06:57 -0800294
295 if (skb->remcsum_offload)
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700296 return guehdr;
Tom Herberta8d31c12014-11-04 09:06:57 -0800297
Tom Herbert4fd671d2014-11-25 11:21:20 -0800298 if (!NAPI_GRO_CB(skb)->csum_valid)
Tom Herberta8d31c12014-11-04 09:06:57 -0800299 return NULL;
300
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700301 guehdr = skb_gro_remcsum_process(skb, (void *)guehdr, off, hdrlen,
302 start, offset, grc, nopartial);
Tom Herberta8d31c12014-11-04 09:06:57 -0800303
304 skb->remcsum_offload = 1;
305
306 return guehdr;
307}
308
David Millerd4546c22018-06-24 14:13:49 +0900309static struct sk_buff *gue_gro_receive(struct sock *sk,
310 struct list_head *head,
311 struct sk_buff *skb)
Tom Herbert37dd0242014-10-03 15:48:09 -0700312{
313 const struct net_offload **offloads;
314 const struct net_offload *ops;
David Millerd4546c22018-06-24 14:13:49 +0900315 struct sk_buff *pp = NULL;
Tom Herbert37dd0242014-10-03 15:48:09 -0700316 struct sk_buff *p;
Tom Herbert37dd0242014-10-03 15:48:09 -0700317 struct guehdr *guehdr;
Tom Herbert5024c332014-11-04 09:06:53 -0800318 size_t len, optlen, hdrlen, off;
319 void *data;
Tom Herberta8d31c12014-11-04 09:06:57 -0800320 u16 doffset = 0;
Tom Herbert37dd0242014-10-03 15:48:09 -0700321 int flush = 1;
Tom Herbertd92283e2016-04-05 08:22:54 -0700322 struct fou *fou = fou_from_sock(sk);
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800323 struct gro_remcsum grc;
Tom Herbertc1e48af2016-06-06 16:06:02 -0700324 u8 proto;
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800325
326 skb_gro_remcsum_init(&grc);
Tom Herbert37dd0242014-10-03 15:48:09 -0700327
328 off = skb_gro_offset(skb);
Tom Herbert5024c332014-11-04 09:06:53 -0800329 len = off + sizeof(*guehdr);
330
Tom Herbert37dd0242014-10-03 15:48:09 -0700331 guehdr = skb_gro_header_fast(skb, off);
Tom Herbert5024c332014-11-04 09:06:53 -0800332 if (skb_gro_header_hard(skb, len)) {
333 guehdr = skb_gro_header_slow(skb, len, off);
Tom Herbert37dd0242014-10-03 15:48:09 -0700334 if (unlikely(!guehdr))
335 goto out;
336 }
337
Tom Herbertc1e48af2016-06-06 16:06:02 -0700338 switch (guehdr->version) {
339 case 0:
340 break;
341 case 1:
342 switch (((struct iphdr *)guehdr)->version) {
343 case 4:
344 proto = IPPROTO_IPIP;
345 break;
346 case 6:
347 proto = IPPROTO_IPV6;
348 break;
349 default:
350 goto out;
351 }
352 goto next_proto;
353 default:
354 goto out;
355 }
356
Tom Herbert5024c332014-11-04 09:06:53 -0800357 optlen = guehdr->hlen << 2;
358 len += optlen;
Tom Herbert37dd0242014-10-03 15:48:09 -0700359
Tom Herbert5024c332014-11-04 09:06:53 -0800360 if (skb_gro_header_hard(skb, len)) {
361 guehdr = skb_gro_header_slow(skb, len, off);
Tom Herbert37dd0242014-10-03 15:48:09 -0700362 if (unlikely(!guehdr))
Tom Herbert5024c332014-11-04 09:06:53 -0800363 goto out;
364 }
365
366 if (unlikely(guehdr->control) || guehdr->version != 0 ||
367 validate_gue_flags(guehdr, optlen))
368 goto out;
369
370 hdrlen = sizeof(*guehdr) + optlen;
371
Tom Herberta8d31c12014-11-04 09:06:57 -0800372 /* Adjust NAPI_GRO_CB(skb)->csum to account for guehdr,
373 * this is needed if there is a remote checkcsum offload.
374 */
Tom Herbert5024c332014-11-04 09:06:53 -0800375 skb_gro_postpull_rcsum(skb, guehdr, hdrlen);
376
377 data = &guehdr[1];
378
379 if (guehdr->flags & GUE_FLAG_PRIV) {
Tom Herberta8d31c12014-11-04 09:06:57 -0800380 __be32 flags = *(__be32 *)(data + doffset);
Tom Herbert5024c332014-11-04 09:06:53 -0800381
Tom Herberta8d31c12014-11-04 09:06:57 -0800382 doffset += GUE_LEN_PRIV;
383
384 if (flags & GUE_PFLAG_REMCSUM) {
385 guehdr = gue_gro_remcsum(skb, off, guehdr,
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700386 data + doffset, hdrlen, &grc,
Tom Herbertfe881ef2015-02-10 16:30:33 -0800387 !!(fou->flags &
388 FOU_F_REMCSUM_NOPARTIAL));
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700389
Tom Herberta8d31c12014-11-04 09:06:57 -0800390 if (!guehdr)
391 goto out;
392
393 data = &guehdr[1];
394
395 doffset += GUE_PLEN_REMCSUM;
396 }
Tom Herbert37dd0242014-10-03 15:48:09 -0700397 }
398
Tom Herberta8d31c12014-11-04 09:06:57 -0800399 skb_gro_pull(skb, hdrlen);
400
David Millerd4546c22018-06-24 14:13:49 +0900401 list_for_each_entry(p, head, list) {
Tom Herbert37dd0242014-10-03 15:48:09 -0700402 const struct guehdr *guehdr2;
403
404 if (!NAPI_GRO_CB(p)->same_flow)
405 continue;
406
407 guehdr2 = (struct guehdr *)(p->data + off);
408
409 /* Compare base GUE header to be equal (covers
Tom Herbert5024c332014-11-04 09:06:53 -0800410 * hlen, version, proto_ctype, and flags.
Tom Herbert37dd0242014-10-03 15:48:09 -0700411 */
412 if (guehdr->word != guehdr2->word) {
413 NAPI_GRO_CB(p)->same_flow = 0;
414 continue;
415 }
416
417 /* Compare optional fields are the same. */
418 if (guehdr->hlen && memcmp(&guehdr[1], &guehdr2[1],
419 guehdr->hlen << 2)) {
420 NAPI_GRO_CB(p)->same_flow = 0;
421 continue;
422 }
423 }
424
Tom Herbertc1e48af2016-06-06 16:06:02 -0700425 proto = guehdr->proto_ctype;
426
427next_proto:
428
Alexander Duyckc3483382016-03-29 14:55:22 -0700429 /* We can clear the encap_mark for GUE as we are essentially doing
430 * one of two possible things. We are either adding an L4 tunnel
431 * header to the outer L3 tunnel header, or we are are simply
432 * treating the GRE tunnel header as though it is a UDP protocol
433 * specific header such as VXLAN or GENEVE.
434 */
435 NAPI_GRO_CB(skb)->encap_mark = 0;
436
Alexander Duycka0ca1532016-04-05 09:13:39 -0700437 /* Flag this frame as already having an outer encap header */
438 NAPI_GRO_CB(skb)->is_fou = 1;
439
Tom Herbert5024c332014-11-04 09:06:53 -0800440 rcu_read_lock();
441 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
Tom Herbertc1e48af2016-06-06 16:06:02 -0700442 ops = rcu_dereference(offloads[proto]);
Tom Herbert27013662015-08-19 17:07:34 -0700443 if (WARN_ON_ONCE(!ops || !ops->callbacks.gro_receive))
Tom Herbert5024c332014-11-04 09:06:53 -0800444 goto out_unlock;
Tom Herbert37dd0242014-10-03 15:48:09 -0700445
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200446 pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800447 flush = 0;
Tom Herbert37dd0242014-10-03 15:48:09 -0700448
449out_unlock:
450 rcu_read_unlock();
451out:
Sabrina Dubroca603d4cf2018-06-30 17:38:55 +0200452 skb_gro_flush_final_remcsum(skb, pp, flush, &grc);
Tom Herbert37dd0242014-10-03 15:48:09 -0700453
454 return pp;
455}
456
Tom Herbertd92283e2016-04-05 08:22:54 -0700457static int gue_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
Tom Herbert37dd0242014-10-03 15:48:09 -0700458{
459 const struct net_offload **offloads;
460 struct guehdr *guehdr = (struct guehdr *)(skb->data + nhoff);
461 const struct net_offload *ops;
Tom Herbertc1e48af2016-06-06 16:06:02 -0700462 unsigned int guehlen = 0;
Tom Herbert37dd0242014-10-03 15:48:09 -0700463 u8 proto;
464 int err = -ENOENT;
465
Tom Herbertc1e48af2016-06-06 16:06:02 -0700466 switch (guehdr->version) {
467 case 0:
468 proto = guehdr->proto_ctype;
469 guehlen = sizeof(*guehdr) + (guehdr->hlen << 2);
470 break;
471 case 1:
472 switch (((struct iphdr *)guehdr)->version) {
473 case 4:
474 proto = IPPROTO_IPIP;
475 break;
476 case 6:
477 proto = IPPROTO_IPV6;
478 break;
479 default:
480 return err;
481 }
482 break;
483 default:
484 return err;
485 }
Tom Herbert37dd0242014-10-03 15:48:09 -0700486
487 rcu_read_lock();
488 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
489 ops = rcu_dereference(offloads[proto]);
490 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
491 goto out_unlock;
492
493 err = ops->callbacks.gro_complete(skb, nhoff + guehlen);
494
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700495 skb_set_inner_mac_header(skb, nhoff + guehlen);
496
Tom Herbert37dd0242014-10-03 15:48:09 -0700497out_unlock:
498 rcu_read_unlock();
499 return err;
500}
501
WANG Cong02d793c2015-04-10 12:00:29 -0700502static int fou_add_to_port_list(struct net *net, struct fou *fou)
Tom Herbert234615512014-09-17 12:25:56 -0700503{
WANG Cong02d793c2015-04-10 12:00:29 -0700504 struct fou_net *fn = net_generic(net, fou_net_id);
Tom Herbert234615512014-09-17 12:25:56 -0700505 struct fou *fout;
506
WANG Cong02d793c2015-04-10 12:00:29 -0700507 mutex_lock(&fn->fou_lock);
508 list_for_each_entry(fout, &fn->fou_list, list) {
Tom Herbert5f914b62016-05-18 09:06:16 -0700509 if (fou->port == fout->port &&
510 fou->family == fout->family) {
WANG Cong02d793c2015-04-10 12:00:29 -0700511 mutex_unlock(&fn->fou_lock);
Tom Herbert234615512014-09-17 12:25:56 -0700512 return -EALREADY;
513 }
514 }
515
WANG Cong02d793c2015-04-10 12:00:29 -0700516 list_add(&fou->list, &fn->fou_list);
517 mutex_unlock(&fn->fou_lock);
Tom Herbert234615512014-09-17 12:25:56 -0700518
519 return 0;
520}
521
522static void fou_release(struct fou *fou)
523{
524 struct socket *sock = fou->sock;
Tom Herbert234615512014-09-17 12:25:56 -0700525
Tom Herbert234615512014-09-17 12:25:56 -0700526 list_del(&fou->list);
WANG Cong02d793c2015-04-10 12:00:29 -0700527 udp_tunnel_sock_release(sock);
Tom Herbert234615512014-09-17 12:25:56 -0700528
Hannes Frederic Sowa3036fac2015-12-15 21:01:53 +0100529 kfree_rcu(fou, rcu);
Tom Herbert234615512014-09-17 12:25:56 -0700530}
531
532static int fou_create(struct net *net, struct fou_cfg *cfg,
533 struct socket **sockp)
534{
Tom Herbert234615512014-09-17 12:25:56 -0700535 struct socket *sock = NULL;
WANG Cong02d793c2015-04-10 12:00:29 -0700536 struct fou *fou = NULL;
Tom Herbert234615512014-09-17 12:25:56 -0700537 struct sock *sk;
Tom Herbert440924b2016-05-18 09:06:14 -0700538 struct udp_tunnel_sock_cfg tunnel_cfg;
WANG Cong02d793c2015-04-10 12:00:29 -0700539 int err;
Tom Herbert234615512014-09-17 12:25:56 -0700540
541 /* Open UDP socket */
542 err = udp_sock_create(net, &cfg->udp_config, &sock);
543 if (err < 0)
544 goto error;
545
546 /* Allocate FOU port structure */
547 fou = kzalloc(sizeof(*fou), GFP_KERNEL);
548 if (!fou) {
549 err = -ENOMEM;
550 goto error;
551 }
552
553 sk = sock->sk;
554
Tom Herbert37dd0242014-10-03 15:48:09 -0700555 fou->port = cfg->udp_config.local_udp_port;
Tom Herbert5f914b62016-05-18 09:06:16 -0700556 fou->family = cfg->udp_config.family;
557 fou->flags = cfg->flags;
Tom Herbert440924b2016-05-18 09:06:14 -0700558 fou->type = cfg->type;
559 fou->sock = sock;
560
561 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
562 tunnel_cfg.encap_type = 1;
563 tunnel_cfg.sk_user_data = fou;
564 tunnel_cfg.encap_destroy = NULL;
Tom Herbert37dd0242014-10-03 15:48:09 -0700565
566 /* Initial for fou type */
567 switch (cfg->type) {
568 case FOU_ENCAP_DIRECT:
Tom Herbert440924b2016-05-18 09:06:14 -0700569 tunnel_cfg.encap_rcv = fou_udp_recv;
570 tunnel_cfg.gro_receive = fou_gro_receive;
571 tunnel_cfg.gro_complete = fou_gro_complete;
572 fou->protocol = cfg->protocol;
Tom Herbert37dd0242014-10-03 15:48:09 -0700573 break;
574 case FOU_ENCAP_GUE:
Tom Herbert440924b2016-05-18 09:06:14 -0700575 tunnel_cfg.encap_rcv = gue_udp_recv;
576 tunnel_cfg.gro_receive = gue_gro_receive;
577 tunnel_cfg.gro_complete = gue_gro_complete;
Tom Herbert37dd0242014-10-03 15:48:09 -0700578 break;
579 default:
580 err = -EINVAL;
581 goto error;
582 }
Tom Herbert234615512014-09-17 12:25:56 -0700583
Tom Herbert440924b2016-05-18 09:06:14 -0700584 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Tom Herbert234615512014-09-17 12:25:56 -0700585
586 sk->sk_allocation = GFP_ATOMIC;
587
WANG Cong02d793c2015-04-10 12:00:29 -0700588 err = fou_add_to_port_list(net, fou);
Tom Herbert234615512014-09-17 12:25:56 -0700589 if (err)
590 goto error;
591
592 if (sockp)
593 *sockp = sock;
594
595 return 0;
596
597error:
598 kfree(fou);
599 if (sock)
WANG Cong02d793c2015-04-10 12:00:29 -0700600 udp_tunnel_sock_release(sock);
Tom Herbert234615512014-09-17 12:25:56 -0700601
602 return err;
603}
604
605static int fou_destroy(struct net *net, struct fou_cfg *cfg)
606{
WANG Cong02d793c2015-04-10 12:00:29 -0700607 struct fou_net *fn = net_generic(net, fou_net_id);
WANG Cong4cbcdf22015-04-10 12:00:28 -0700608 __be16 port = cfg->udp_config.local_udp_port;
Tom Herbert5f914b62016-05-18 09:06:16 -0700609 u8 family = cfg->udp_config.family;
Tom Herbert234615512014-09-17 12:25:56 -0700610 int err = -EINVAL;
WANG Cong02d793c2015-04-10 12:00:29 -0700611 struct fou *fou;
Tom Herbert234615512014-09-17 12:25:56 -0700612
WANG Cong02d793c2015-04-10 12:00:29 -0700613 mutex_lock(&fn->fou_lock);
614 list_for_each_entry(fou, &fn->fou_list, list) {
Tom Herbert5f914b62016-05-18 09:06:16 -0700615 if (fou->port == port && fou->family == family) {
Tom Herbert234615512014-09-17 12:25:56 -0700616 fou_release(fou);
617 err = 0;
618 break;
619 }
620 }
WANG Cong02d793c2015-04-10 12:00:29 -0700621 mutex_unlock(&fn->fou_lock);
Tom Herbert234615512014-09-17 12:25:56 -0700622
623 return err;
624}
625
Johannes Berg489111e2016-10-24 14:40:03 +0200626static struct genl_family fou_nl_family;
Tom Herbert234615512014-09-17 12:25:56 -0700627
stephen hemminger3f18ff22016-08-31 15:19:37 -0700628static const struct nla_policy fou_nl_policy[FOU_ATTR_MAX + 1] = {
Tom Herbert234615512014-09-17 12:25:56 -0700629 [FOU_ATTR_PORT] = { .type = NLA_U16, },
630 [FOU_ATTR_AF] = { .type = NLA_U8, },
631 [FOU_ATTR_IPPROTO] = { .type = NLA_U8, },
Tom Herbert37dd0242014-10-03 15:48:09 -0700632 [FOU_ATTR_TYPE] = { .type = NLA_U8, },
Tom Herbertfe881ef2015-02-10 16:30:33 -0800633 [FOU_ATTR_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG, },
Tom Herbert234615512014-09-17 12:25:56 -0700634};
635
636static int parse_nl_config(struct genl_info *info,
637 struct fou_cfg *cfg)
638{
639 memset(cfg, 0, sizeof(*cfg));
640
641 cfg->udp_config.family = AF_INET;
642
643 if (info->attrs[FOU_ATTR_AF]) {
644 u8 family = nla_get_u8(info->attrs[FOU_ATTR_AF]);
645
Tom Herbert5f914b62016-05-18 09:06:16 -0700646 switch (family) {
647 case AF_INET:
648 break;
649 case AF_INET6:
650 cfg->udp_config.ipv6_v6only = 1;
651 break;
652 default:
653 return -EAFNOSUPPORT;
654 }
Tom Herbert234615512014-09-17 12:25:56 -0700655
656 cfg->udp_config.family = family;
657 }
658
659 if (info->attrs[FOU_ATTR_PORT]) {
WANG Cong4cbcdf22015-04-10 12:00:28 -0700660 __be16 port = nla_get_be16(info->attrs[FOU_ATTR_PORT]);
Tom Herbert234615512014-09-17 12:25:56 -0700661
662 cfg->udp_config.local_udp_port = port;
663 }
664
665 if (info->attrs[FOU_ATTR_IPPROTO])
666 cfg->protocol = nla_get_u8(info->attrs[FOU_ATTR_IPPROTO]);
667
Tom Herbert37dd0242014-10-03 15:48:09 -0700668 if (info->attrs[FOU_ATTR_TYPE])
669 cfg->type = nla_get_u8(info->attrs[FOU_ATTR_TYPE]);
670
Tom Herbertfe881ef2015-02-10 16:30:33 -0800671 if (info->attrs[FOU_ATTR_REMCSUM_NOPARTIAL])
672 cfg->flags |= FOU_F_REMCSUM_NOPARTIAL;
673
Tom Herbert234615512014-09-17 12:25:56 -0700674 return 0;
675}
676
677static int fou_nl_cmd_add_port(struct sk_buff *skb, struct genl_info *info)
678{
WANG Cong02d793c2015-04-10 12:00:29 -0700679 struct net *net = genl_info_net(info);
Tom Herbert234615512014-09-17 12:25:56 -0700680 struct fou_cfg cfg;
681 int err;
682
683 err = parse_nl_config(info, &cfg);
684 if (err)
685 return err;
686
WANG Cong02d793c2015-04-10 12:00:29 -0700687 return fou_create(net, &cfg, NULL);
Tom Herbert234615512014-09-17 12:25:56 -0700688}
689
690static int fou_nl_cmd_rm_port(struct sk_buff *skb, struct genl_info *info)
691{
WANG Cong02d793c2015-04-10 12:00:29 -0700692 struct net *net = genl_info_net(info);
Tom Herbert234615512014-09-17 12:25:56 -0700693 struct fou_cfg cfg;
WANG Cong67270632015-04-10 12:00:27 -0700694 int err;
Tom Herbert234615512014-09-17 12:25:56 -0700695
WANG Cong67270632015-04-10 12:00:27 -0700696 err = parse_nl_config(info, &cfg);
697 if (err)
698 return err;
Tom Herbert234615512014-09-17 12:25:56 -0700699
WANG Cong02d793c2015-04-10 12:00:29 -0700700 return fou_destroy(net, &cfg);
Tom Herbert234615512014-09-17 12:25:56 -0700701}
702
WANG Cong7a6c8c32015-04-10 12:00:30 -0700703static int fou_fill_info(struct fou *fou, struct sk_buff *msg)
704{
705 if (nla_put_u8(msg, FOU_ATTR_AF, fou->sock->sk->sk_family) ||
706 nla_put_be16(msg, FOU_ATTR_PORT, fou->port) ||
707 nla_put_u8(msg, FOU_ATTR_IPPROTO, fou->protocol) ||
708 nla_put_u8(msg, FOU_ATTR_TYPE, fou->type))
709 return -1;
710
711 if (fou->flags & FOU_F_REMCSUM_NOPARTIAL)
712 if (nla_put_flag(msg, FOU_ATTR_REMCSUM_NOPARTIAL))
713 return -1;
714 return 0;
715}
716
717static int fou_dump_info(struct fou *fou, u32 portid, u32 seq,
718 u32 flags, struct sk_buff *skb, u8 cmd)
719{
720 void *hdr;
721
722 hdr = genlmsg_put(skb, portid, seq, &fou_nl_family, flags, cmd);
723 if (!hdr)
724 return -ENOMEM;
725
726 if (fou_fill_info(fou, skb) < 0)
727 goto nla_put_failure;
728
729 genlmsg_end(skb, hdr);
730 return 0;
731
732nla_put_failure:
733 genlmsg_cancel(skb, hdr);
734 return -EMSGSIZE;
735}
736
737static int fou_nl_cmd_get_port(struct sk_buff *skb, struct genl_info *info)
738{
739 struct net *net = genl_info_net(info);
740 struct fou_net *fn = net_generic(net, fou_net_id);
741 struct sk_buff *msg;
742 struct fou_cfg cfg;
743 struct fou *fout;
744 __be16 port;
Tom Herbert5f914b62016-05-18 09:06:16 -0700745 u8 family;
WANG Cong7a6c8c32015-04-10 12:00:30 -0700746 int ret;
747
748 ret = parse_nl_config(info, &cfg);
749 if (ret)
750 return ret;
751 port = cfg.udp_config.local_udp_port;
752 if (port == 0)
753 return -EINVAL;
754
Tom Herbert5f914b62016-05-18 09:06:16 -0700755 family = cfg.udp_config.family;
756 if (family != AF_INET && family != AF_INET6)
757 return -EINVAL;
758
WANG Cong7a6c8c32015-04-10 12:00:30 -0700759 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
760 if (!msg)
761 return -ENOMEM;
762
763 ret = -ESRCH;
764 mutex_lock(&fn->fou_lock);
765 list_for_each_entry(fout, &fn->fou_list, list) {
Tom Herbert5f914b62016-05-18 09:06:16 -0700766 if (port == fout->port && family == fout->family) {
WANG Cong7a6c8c32015-04-10 12:00:30 -0700767 ret = fou_dump_info(fout, info->snd_portid,
768 info->snd_seq, 0, msg,
769 info->genlhdr->cmd);
770 break;
771 }
772 }
773 mutex_unlock(&fn->fou_lock);
774 if (ret < 0)
775 goto out_free;
776
777 return genlmsg_reply(msg, info);
778
779out_free:
780 nlmsg_free(msg);
781 return ret;
782}
783
784static int fou_nl_dump(struct sk_buff *skb, struct netlink_callback *cb)
785{
786 struct net *net = sock_net(skb->sk);
787 struct fou_net *fn = net_generic(net, fou_net_id);
788 struct fou *fout;
789 int idx = 0, ret;
790
791 mutex_lock(&fn->fou_lock);
792 list_for_each_entry(fout, &fn->fou_list, list) {
793 if (idx++ < cb->args[0])
794 continue;
795 ret = fou_dump_info(fout, NETLINK_CB(cb->skb).portid,
796 cb->nlh->nlmsg_seq, NLM_F_MULTI,
797 skb, FOU_CMD_GET);
798 if (ret)
WANG Cong540207a2015-04-15 11:48:49 -0700799 break;
WANG Cong7a6c8c32015-04-10 12:00:30 -0700800 }
801 mutex_unlock(&fn->fou_lock);
802
WANG Cong7a6c8c32015-04-10 12:00:30 -0700803 cb->args[0] = idx;
804 return skb->len;
805}
806
Tom Herbert234615512014-09-17 12:25:56 -0700807static const struct genl_ops fou_nl_ops[] = {
808 {
809 .cmd = FOU_CMD_ADD,
810 .doit = fou_nl_cmd_add_port,
811 .policy = fou_nl_policy,
812 .flags = GENL_ADMIN_PERM,
813 },
814 {
815 .cmd = FOU_CMD_DEL,
816 .doit = fou_nl_cmd_rm_port,
817 .policy = fou_nl_policy,
818 .flags = GENL_ADMIN_PERM,
819 },
WANG Cong7a6c8c32015-04-10 12:00:30 -0700820 {
821 .cmd = FOU_CMD_GET,
822 .doit = fou_nl_cmd_get_port,
823 .dumpit = fou_nl_dump,
824 .policy = fou_nl_policy,
825 },
Tom Herbert234615512014-09-17 12:25:56 -0700826};
827
Johannes Berg56989f62016-10-24 14:40:05 +0200828static struct genl_family fou_nl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +0200829 .hdrsize = 0,
830 .name = FOU_GENL_NAME,
831 .version = FOU_GENL_VERSION,
832 .maxattr = FOU_ATTR_MAX,
833 .netnsok = true,
834 .module = THIS_MODULE,
835 .ops = fou_nl_ops,
836 .n_ops = ARRAY_SIZE(fou_nl_ops),
837};
838
Tom Herberta8c5f902014-11-12 11:54:09 -0800839size_t fou_encap_hlen(struct ip_tunnel_encap *e)
840{
841 return sizeof(struct udphdr);
842}
843EXPORT_SYMBOL(fou_encap_hlen);
844
845size_t gue_encap_hlen(struct ip_tunnel_encap *e)
846{
847 size_t len;
848 bool need_priv = false;
849
850 len = sizeof(struct udphdr) + sizeof(struct guehdr);
851
852 if (e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) {
853 len += GUE_PLEN_REMCSUM;
854 need_priv = true;
855 }
856
857 len += need_priv ? GUE_LEN_PRIV : 0;
858
859 return len;
860}
861EXPORT_SYMBOL(gue_encap_hlen);
862
Tom Herbertdc969b82016-05-18 09:06:15 -0700863int __fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
864 u8 *protocol, __be16 *sport, int type)
865{
866 int err;
867
868 err = iptunnel_handle_offloads(skb, type);
869 if (err)
870 return err;
871
872 *sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
873 skb, 0, 0, false);
874
875 return 0;
876}
877EXPORT_SYMBOL(__fou_build_header);
878
Tom Herbertdc969b82016-05-18 09:06:15 -0700879int __gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
880 u8 *protocol, __be16 *sport, int type)
Tom Herbert63487ba2014-11-04 09:06:51 -0800881{
Tom Herbert63487ba2014-11-04 09:06:51 -0800882 struct guehdr *guehdr;
Tom Herbertb17f7092014-11-04 09:06:56 -0800883 size_t hdrlen, optlen = 0;
Tom Herbert5024c332014-11-04 09:06:53 -0800884 void *data;
885 bool need_priv = false;
Alexander Duyckaed069d2016-04-14 15:33:37 -0400886 int err;
Tom Herbert5024c332014-11-04 09:06:53 -0800887
Tom Herbertb17f7092014-11-04 09:06:56 -0800888 if ((e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) &&
889 skb->ip_summed == CHECKSUM_PARTIAL) {
Tom Herbertb17f7092014-11-04 09:06:56 -0800890 optlen += GUE_PLEN_REMCSUM;
891 type |= SKB_GSO_TUNNEL_REMCSUM;
892 need_priv = true;
893 }
894
Tom Herbert5024c332014-11-04 09:06:53 -0800895 optlen += need_priv ? GUE_LEN_PRIV : 0;
Tom Herbert63487ba2014-11-04 09:06:51 -0800896
Alexander Duyckaed069d2016-04-14 15:33:37 -0400897 err = iptunnel_handle_offloads(skb, type);
898 if (err)
899 return err;
Tom Herbert63487ba2014-11-04 09:06:51 -0800900
901 /* Get source port (based on flow hash) before skb_push */
Tom Herbertdc969b82016-05-18 09:06:15 -0700902 *sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
903 skb, 0, 0, false);
Tom Herbert63487ba2014-11-04 09:06:51 -0800904
Tom Herbertb17f7092014-11-04 09:06:56 -0800905 hdrlen = sizeof(struct guehdr) + optlen;
906
907 skb_push(skb, hdrlen);
Tom Herbert63487ba2014-11-04 09:06:51 -0800908
909 guehdr = (struct guehdr *)skb->data;
910
Tom Herbert5024c332014-11-04 09:06:53 -0800911 guehdr->control = 0;
Tom Herbert63487ba2014-11-04 09:06:51 -0800912 guehdr->version = 0;
Tom Herbert5024c332014-11-04 09:06:53 -0800913 guehdr->hlen = optlen >> 2;
Tom Herbert63487ba2014-11-04 09:06:51 -0800914 guehdr->flags = 0;
Tom Herbert5024c332014-11-04 09:06:53 -0800915 guehdr->proto_ctype = *protocol;
916
917 data = &guehdr[1];
918
919 if (need_priv) {
920 __be32 *flags = data;
921
922 guehdr->flags |= GUE_FLAG_PRIV;
923 *flags = 0;
924 data += GUE_LEN_PRIV;
925
Tom Herbertb17f7092014-11-04 09:06:56 -0800926 if (type & SKB_GSO_TUNNEL_REMCSUM) {
927 u16 csum_start = skb_checksum_start_offset(skb);
928 __be16 *pd = data;
929
930 if (csum_start < hdrlen)
931 return -EINVAL;
932
933 csum_start -= hdrlen;
934 pd[0] = htons(csum_start);
935 pd[1] = htons(csum_start + skb->csum_offset);
936
937 if (!skb_is_gso(skb)) {
938 skb->ip_summed = CHECKSUM_NONE;
939 skb->encapsulation = 0;
940 }
941
942 *flags |= GUE_PFLAG_REMCSUM;
943 data += GUE_PLEN_REMCSUM;
944 }
945
Tom Herbert5024c332014-11-04 09:06:53 -0800946 }
Tom Herbert63487ba2014-11-04 09:06:51 -0800947
Tom Herbertdc969b82016-05-18 09:06:15 -0700948 return 0;
949}
950EXPORT_SYMBOL(__gue_build_header);
951
stephen hemminger9dc621a2017-05-19 09:55:54 -0700952#ifdef CONFIG_NET_FOU_IP_TUNNELS
953
954static void fou_build_udp(struct sk_buff *skb, struct ip_tunnel_encap *e,
955 struct flowi4 *fl4, u8 *protocol, __be16 sport)
956{
957 struct udphdr *uh;
958
959 skb_push(skb, sizeof(struct udphdr));
960 skb_reset_transport_header(skb);
961
962 uh = udp_hdr(skb);
963
964 uh->dest = e->dport;
965 uh->source = sport;
966 uh->len = htons(skb->len);
967 udp_set_csum(!(e->flags & TUNNEL_ENCAP_FLAG_CSUM), skb,
968 fl4->saddr, fl4->daddr, skb->len);
969
970 *protocol = IPPROTO_UDP;
971}
972
973static int fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
974 u8 *protocol, struct flowi4 *fl4)
975{
976 int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM ? SKB_GSO_UDP_TUNNEL_CSUM :
977 SKB_GSO_UDP_TUNNEL;
978 __be16 sport;
979 int err;
980
981 err = __fou_build_header(skb, e, protocol, &sport, type);
982 if (err)
983 return err;
984
985 fou_build_udp(skb, e, fl4, protocol, sport);
986
987 return 0;
988}
989
990static int gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
991 u8 *protocol, struct flowi4 *fl4)
Tom Herbertdc969b82016-05-18 09:06:15 -0700992{
993 int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM ? SKB_GSO_UDP_TUNNEL_CSUM :
994 SKB_GSO_UDP_TUNNEL;
995 __be16 sport;
996 int err;
997
998 err = __gue_build_header(skb, e, protocol, &sport, type);
999 if (err)
1000 return err;
1001
Tom Herbert63487ba2014-11-04 09:06:51 -08001002 fou_build_udp(skb, e, fl4, protocol, sport);
1003
1004 return 0;
1005}
Tom Herbert63487ba2014-11-04 09:06:51 -08001006
Stefano Briviob8a51b32018-11-08 12:19:23 +01001007static int gue_err_proto_handler(int proto, struct sk_buff *skb, u32 info)
1008{
1009 const struct net_protocol *ipprot = rcu_dereference(inet_protos[proto]);
1010
1011 if (ipprot && ipprot->err_handler) {
1012 if (!ipprot->err_handler(skb, info))
1013 return 0;
1014 }
1015
1016 return -ENOENT;
1017}
1018
1019static int gue_err(struct sk_buff *skb, u32 info)
1020{
1021 int transport_offset = skb_transport_offset(skb);
1022 struct guehdr *guehdr;
1023 size_t optlen;
1024 int ret;
1025
1026 if (skb->len < sizeof(struct udphdr) + sizeof(struct guehdr))
1027 return -EINVAL;
1028
1029 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
1030
1031 switch (guehdr->version) {
1032 case 0: /* Full GUE header present */
1033 break;
1034 case 1: {
1035 /* Direct encasulation of IPv4 or IPv6 */
1036 skb_set_transport_header(skb, -(int)sizeof(struct icmphdr));
1037
1038 switch (((struct iphdr *)guehdr)->version) {
1039 case 4:
1040 ret = gue_err_proto_handler(IPPROTO_IPIP, skb, info);
1041 goto out;
1042#if IS_ENABLED(CONFIG_IPV6)
1043 case 6:
1044 ret = gue_err_proto_handler(IPPROTO_IPV6, skb, info);
1045 goto out;
1046#endif
1047 default:
1048 ret = -EOPNOTSUPP;
1049 goto out;
1050 }
1051 }
1052 default: /* Undefined version */
1053 return -EOPNOTSUPP;
1054 }
1055
1056 if (guehdr->control)
1057 return -ENOENT;
1058
1059 optlen = guehdr->hlen << 2;
1060
1061 if (validate_gue_flags(guehdr, optlen))
1062 return -EINVAL;
1063
Stefano Brivio11789032018-12-18 00:13:17 +01001064 /* Handling exceptions for direct UDP encapsulation in GUE would lead to
1065 * recursion. Besides, this kind of encapsulation can't even be
1066 * configured currently. Discard this.
1067 */
1068 if (guehdr->proto_ctype == IPPROTO_UDP)
1069 return -EOPNOTSUPP;
1070
Stefano Briviob8a51b32018-11-08 12:19:23 +01001071 skb_set_transport_header(skb, -(int)sizeof(struct icmphdr));
1072 ret = gue_err_proto_handler(guehdr->proto_ctype, skb, info);
1073
1074out:
1075 skb_set_transport_header(skb, transport_offset);
1076 return ret;
1077}
1078
Tom Herberta8c5f902014-11-12 11:54:09 -08001079
Andi Kleen5eeb2922015-04-08 06:04:31 -07001080static const struct ip_tunnel_encap_ops fou_iptun_ops = {
Tom Herberta8c5f902014-11-12 11:54:09 -08001081 .encap_hlen = fou_encap_hlen,
1082 .build_header = fou_build_header,
Stefano Briviob8a51b32018-11-08 12:19:23 +01001083 .err_handler = gue_err,
Tom Herberta8c5f902014-11-12 11:54:09 -08001084};
1085
Andi Kleen5eeb2922015-04-08 06:04:31 -07001086static const struct ip_tunnel_encap_ops gue_iptun_ops = {
Tom Herberta8c5f902014-11-12 11:54:09 -08001087 .encap_hlen = gue_encap_hlen,
1088 .build_header = gue_build_header,
Stefano Briviob8a51b32018-11-08 12:19:23 +01001089 .err_handler = gue_err,
Tom Herberta8c5f902014-11-12 11:54:09 -08001090};
1091
1092static int ip_tunnel_encap_add_fou_ops(void)
1093{
1094 int ret;
1095
1096 ret = ip_tunnel_encap_add_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
1097 if (ret < 0) {
1098 pr_err("can't add fou ops\n");
1099 return ret;
1100 }
1101
1102 ret = ip_tunnel_encap_add_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
1103 if (ret < 0) {
1104 pr_err("can't add gue ops\n");
1105 ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
1106 return ret;
1107 }
1108
1109 return 0;
1110}
1111
1112static void ip_tunnel_encap_del_fou_ops(void)
1113{
1114 ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
1115 ip_tunnel_encap_del_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
1116}
1117
1118#else
1119
1120static int ip_tunnel_encap_add_fou_ops(void)
1121{
1122 return 0;
1123}
1124
Thomas Graf882288c2014-11-13 12:48:21 +01001125static void ip_tunnel_encap_del_fou_ops(void)
Tom Herberta8c5f902014-11-12 11:54:09 -08001126{
1127}
1128
1129#endif
1130
WANG Cong02d793c2015-04-10 12:00:29 -07001131static __net_init int fou_init_net(struct net *net)
1132{
1133 struct fou_net *fn = net_generic(net, fou_net_id);
1134
1135 INIT_LIST_HEAD(&fn->fou_list);
1136 mutex_init(&fn->fou_lock);
1137 return 0;
1138}
1139
1140static __net_exit void fou_exit_net(struct net *net)
1141{
1142 struct fou_net *fn = net_generic(net, fou_net_id);
1143 struct fou *fou, *next;
1144
1145 /* Close all the FOU sockets */
1146 mutex_lock(&fn->fou_lock);
1147 list_for_each_entry_safe(fou, next, &fn->fou_list, list)
1148 fou_release(fou);
1149 mutex_unlock(&fn->fou_lock);
1150}
1151
1152static struct pernet_operations fou_net_ops = {
1153 .init = fou_init_net,
1154 .exit = fou_exit_net,
1155 .id = &fou_net_id,
1156 .size = sizeof(struct fou_net),
1157};
1158
Tom Herbert234615512014-09-17 12:25:56 -07001159static int __init fou_init(void)
1160{
1161 int ret;
1162
WANG Cong02d793c2015-04-10 12:00:29 -07001163 ret = register_pernet_device(&fou_net_ops);
1164 if (ret)
Tom Herberta8c5f902014-11-12 11:54:09 -08001165 goto exit;
1166
Johannes Berg489111e2016-10-24 14:40:03 +02001167 ret = genl_register_family(&fou_nl_family);
Tom Herberta8c5f902014-11-12 11:54:09 -08001168 if (ret < 0)
WANG Cong02d793c2015-04-10 12:00:29 -07001169 goto unregister;
Tom Herberta8c5f902014-11-12 11:54:09 -08001170
WANG Cong02d793c2015-04-10 12:00:29 -07001171 ret = ip_tunnel_encap_add_fou_ops();
1172 if (ret == 0)
1173 return 0;
1174
1175 genl_unregister_family(&fou_nl_family);
1176unregister:
1177 unregister_pernet_device(&fou_net_ops);
Tom Herberta8c5f902014-11-12 11:54:09 -08001178exit:
Tom Herbert234615512014-09-17 12:25:56 -07001179 return ret;
1180}
1181
1182static void __exit fou_fini(void)
1183{
Tom Herberta8c5f902014-11-12 11:54:09 -08001184 ip_tunnel_encap_del_fou_ops();
Tom Herbert234615512014-09-17 12:25:56 -07001185 genl_unregister_family(&fou_nl_family);
WANG Cong02d793c2015-04-10 12:00:29 -07001186 unregister_pernet_device(&fou_net_ops);
Tom Herbert234615512014-09-17 12:25:56 -07001187}
1188
1189module_init(fou_init);
1190module_exit(fou_fini);
1191MODULE_AUTHOR("Tom Herbert <therbert@google.com>");
1192MODULE_LICENSE("GPL");