blob: 805f6607f8d9a89661bed62db6b370b964d43f1d [file] [log] [blame]
Tom Herbert23461552014-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>
6#include <linux/udp.h>
7#include <linux/types.h>
8#include <linux/kernel.h>
9#include <net/genetlink.h>
Tom Herbert37dd0242014-10-03 15:48:09 -070010#include <net/gue.h>
Tom Herbert23461552014-09-17 12:25:56 -070011#include <net/ip.h>
Tom Herbertafe93322014-09-17 12:25:57 -070012#include <net/protocol.h>
Tom Herbert23461552014-09-17 12:25:56 -070013#include <net/udp.h>
14#include <net/udp_tunnel.h>
15#include <net/xfrm.h>
16#include <uapi/linux/fou.h>
17#include <uapi/linux/genetlink.h>
18
Tom Herbert23461552014-09-17 12:25:56 -070019struct fou {
20 struct socket *sock;
21 u8 protocol;
Tom Herbertfe881ef2015-02-10 16:30:33 -080022 u8 flags;
WANG Cong4cbcdf22015-04-10 12:00:28 -070023 __be16 port;
Tom Herbert5f914b62016-05-18 09:06:16 -070024 u8 family;
WANG Cong7a6c8c32015-04-10 12:00:30 -070025 u16 type;
Tom Herbert23461552014-09-17 12:25:56 -070026 struct list_head list;
Hannes Frederic Sowa3036fac2015-12-15 21:01:53 +010027 struct rcu_head rcu;
Tom Herbert23461552014-09-17 12:25:56 -070028};
29
Tom Herbertfe881ef2015-02-10 16:30:33 -080030#define FOU_F_REMCSUM_NOPARTIAL BIT(0)
31
Tom Herbert23461552014-09-17 12:25:56 -070032struct fou_cfg {
Tom Herbert37dd0242014-10-03 15:48:09 -070033 u16 type;
Tom Herbert23461552014-09-17 12:25:56 -070034 u8 protocol;
Tom Herbertfe881ef2015-02-10 16:30:33 -080035 u8 flags;
Tom Herbert23461552014-09-17 12:25:56 -070036 struct udp_port_cfg udp_config;
37};
38
WANG Cong02d793c2015-04-10 12:00:29 -070039static unsigned int fou_net_id;
40
41struct fou_net {
42 struct list_head fou_list;
43 struct mutex fou_lock;
44};
45
Tom Herbert23461552014-09-17 12:25:56 -070046static inline struct fou *fou_from_sock(struct sock *sk)
47{
48 return sk->sk_user_data;
49}
50
Tom Herbert5f914b62016-05-18 09:06:16 -070051static int fou_recv_pull(struct sk_buff *skb, struct fou *fou, size_t len)
Tom Herbert23461552014-09-17 12:25:56 -070052{
Tom Herbert23461552014-09-17 12:25:56 -070053 /* Remove 'len' bytes from the packet (UDP header and
Tom Herbert5024c332014-11-04 09:06:53 -080054 * FOU header if present).
Tom Herbert23461552014-09-17 12:25:56 -070055 */
Tom Herbert5f914b62016-05-18 09:06:16 -070056 if (fou->family == AF_INET)
57 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(skb)->tot_len) - len);
58 else
59 ipv6_hdr(skb)->payload_len =
60 htons(ntohs(ipv6_hdr(skb)->payload_len) - len);
61
Tom Herbert23461552014-09-17 12:25:56 -070062 __skb_pull(skb, len);
63 skb_postpull_rcsum(skb, udp_hdr(skb), len);
64 skb_reset_transport_header(skb);
Jesse Grossa09a4c82016-03-19 09:32:02 -070065 return iptunnel_pull_offloads(skb);
Tom Herbert23461552014-09-17 12:25:56 -070066}
67
68static int fou_udp_recv(struct sock *sk, struct sk_buff *skb)
69{
70 struct fou *fou = fou_from_sock(sk);
71
72 if (!fou)
73 return 1;
74
Tom Herbert5f914b62016-05-18 09:06:16 -070075 if (fou_recv_pull(skb, fou, sizeof(struct udphdr)))
Jesse Grossa09a4c82016-03-19 09:32:02 -070076 goto drop;
Tom Herbert5024c332014-11-04 09:06:53 -080077
78 return -fou->protocol;
Jesse Grossa09a4c82016-03-19 09:32:02 -070079
80drop:
81 kfree_skb(skb);
82 return 0;
Tom Herbert5024c332014-11-04 09:06:53 -080083}
84
Tom Herberta8d31c12014-11-04 09:06:57 -080085static struct guehdr *gue_remcsum(struct sk_buff *skb, struct guehdr *guehdr,
Tom Herbertfe881ef2015-02-10 16:30:33 -080086 void *data, size_t hdrlen, u8 ipproto,
87 bool nopartial)
Tom Herberta8d31c12014-11-04 09:06:57 -080088{
89 __be16 *pd = data;
Tom Herbert4fd671d2014-11-25 11:21:20 -080090 size_t start = ntohs(pd[0]);
91 size_t offset = ntohs(pd[1]);
Tom Herbertb7fe10e2015-08-19 17:07:32 -070092 size_t plen = sizeof(struct udphdr) + hdrlen +
93 max_t(size_t, offset + sizeof(u16), start);
94
95 if (skb->remcsum_offload)
96 return guehdr;
Tom Herberta8d31c12014-11-04 09:06:57 -080097
Tom Herberta8d31c12014-11-04 09:06:57 -080098 if (!pskb_may_pull(skb, plen))
99 return NULL;
100 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
101
Tom Herbertfe881ef2015-02-10 16:30:33 -0800102 skb_remcsum_process(skb, (void *)guehdr + hdrlen,
103 start, offset, nopartial);
Tom Herberta8d31c12014-11-04 09:06:57 -0800104
105 return guehdr;
106}
107
Tom Herbert5024c332014-11-04 09:06:53 -0800108static int gue_control_message(struct sk_buff *skb, struct guehdr *guehdr)
109{
110 /* No support yet */
111 kfree_skb(skb);
112 return 0;
Tom Herbert23461552014-09-17 12:25:56 -0700113}
114
Tom Herbert37dd0242014-10-03 15:48:09 -0700115static int gue_udp_recv(struct sock *sk, struct sk_buff *skb)
116{
117 struct fou *fou = fou_from_sock(sk);
Tom Herbert5024c332014-11-04 09:06:53 -0800118 size_t len, optlen, hdrlen;
Tom Herbert37dd0242014-10-03 15:48:09 -0700119 struct guehdr *guehdr;
Tom Herbert5024c332014-11-04 09:06:53 -0800120 void *data;
Tom Herberta8d31c12014-11-04 09:06:57 -0800121 u16 doffset = 0;
Tom Herbert37dd0242014-10-03 15:48:09 -0700122
123 if (!fou)
124 return 1;
125
126 len = sizeof(struct udphdr) + sizeof(struct guehdr);
127 if (!pskb_may_pull(skb, len))
128 goto drop;
129
Tom Herbert5024c332014-11-04 09:06:53 -0800130 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
Tom Herbert37dd0242014-10-03 15:48:09 -0700131
Tom Herbertc1e48af2016-06-06 16:06:02 -0700132 switch (guehdr->version) {
133 case 0: /* Full GUE header present */
134 break;
135
136 case 1: {
137 /* Direct encasulation of IPv4 or IPv6 */
138
139 int prot;
140
141 switch (((struct iphdr *)guehdr)->version) {
142 case 4:
143 prot = IPPROTO_IPIP;
144 break;
145 case 6:
146 prot = IPPROTO_IPV6;
147 break;
148 default:
149 goto drop;
150 }
151
152 if (fou_recv_pull(skb, fou, sizeof(struct udphdr)))
153 goto drop;
154
155 return -prot;
156 }
157
158 default: /* Undefined version */
159 goto drop;
160 }
161
Tom Herbert5024c332014-11-04 09:06:53 -0800162 optlen = guehdr->hlen << 2;
163 len += optlen;
164
Tom Herbert37dd0242014-10-03 15:48:09 -0700165 if (!pskb_may_pull(skb, len))
166 goto drop;
167
Tom Herbert5024c332014-11-04 09:06:53 -0800168 /* guehdr may change after pull */
169 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
Li RongQingd8f00d22014-10-17 16:53:47 +0800170
Tom Herbert5024c332014-11-04 09:06:53 -0800171 hdrlen = sizeof(struct guehdr) + optlen;
172
173 if (guehdr->version != 0 || validate_gue_flags(guehdr, optlen))
Tom Herbert37dd0242014-10-03 15:48:09 -0700174 goto drop;
175
Tom Herberta8d31c12014-11-04 09:06:57 -0800176 hdrlen = sizeof(struct guehdr) + optlen;
177
Tom Herbert5f914b62016-05-18 09:06:16 -0700178 if (fou->family == AF_INET)
179 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(skb)->tot_len) - len);
180 else
181 ipv6_hdr(skb)->payload_len =
182 htons(ntohs(ipv6_hdr(skb)->payload_len) - len);
Tom Herberta8d31c12014-11-04 09:06:57 -0800183
Tom Herberta8d31c12014-11-04 09:06:57 -0800184 /* Pull csum through the guehdr now . This can be used if
185 * there is a remote checksum offload.
186 */
187 skb_postpull_rcsum(skb, udp_hdr(skb), len);
Tom Herbert5024c332014-11-04 09:06:53 -0800188
189 data = &guehdr[1];
190
191 if (guehdr->flags & GUE_FLAG_PRIV) {
Tom Herberta8d31c12014-11-04 09:06:57 -0800192 __be32 flags = *(__be32 *)(data + doffset);
Tom Herbert5024c332014-11-04 09:06:53 -0800193
Tom Herberta8d31c12014-11-04 09:06:57 -0800194 doffset += GUE_LEN_PRIV;
195
196 if (flags & GUE_PFLAG_REMCSUM) {
197 guehdr = gue_remcsum(skb, guehdr, data + doffset,
Tom Herbertfe881ef2015-02-10 16:30:33 -0800198 hdrlen, guehdr->proto_ctype,
199 !!(fou->flags &
200 FOU_F_REMCSUM_NOPARTIAL));
Tom Herberta8d31c12014-11-04 09:06:57 -0800201 if (!guehdr)
202 goto drop;
203
204 data = &guehdr[1];
205
206 doffset += GUE_PLEN_REMCSUM;
207 }
Tom Herbert37dd0242014-10-03 15:48:09 -0700208 }
209
Tom Herbert5024c332014-11-04 09:06:53 -0800210 if (unlikely(guehdr->control))
211 return gue_control_message(skb, guehdr);
212
Tom Herbert4fd671d2014-11-25 11:21:20 -0800213 __skb_pull(skb, sizeof(struct udphdr) + hdrlen);
Tom Herberta8d31c12014-11-04 09:06:57 -0800214 skb_reset_transport_header(skb);
215
Jesse Grossa09a4c82016-03-19 09:32:02 -0700216 if (iptunnel_pull_offloads(skb))
217 goto drop;
218
Tom Herbert5024c332014-11-04 09:06:53 -0800219 return -guehdr->proto_ctype;
220
Tom Herbert37dd0242014-10-03 15:48:09 -0700221drop:
222 kfree_skb(skb);
223 return 0;
224}
225
Tom Herbertd92283e2016-04-05 08:22:54 -0700226static struct sk_buff **fou_gro_receive(struct sock *sk,
227 struct sk_buff **head,
228 struct sk_buff *skb)
Tom Herbertafe93322014-09-17 12:25:57 -0700229{
230 const struct net_offload *ops;
231 struct sk_buff **pp = NULL;
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;
Tom Herbertafe93322014-09-17 12:25:57 -0700234
Alexander Duyckc3483382016-03-29 14:55:22 -0700235 /* We can clear the encap_mark for FOU as we are essentially doing
236 * one of two possible things. We are either adding an L4 tunnel
237 * header to the outer L3 tunnel header, or we are are simply
238 * treating the GRE tunnel header as though it is a UDP protocol
239 * specific header such as VXLAN or GENEVE.
240 */
241 NAPI_GRO_CB(skb)->encap_mark = 0;
242
Alexander Duycka0ca1532016-04-05 09:13:39 -0700243 /* Flag this frame as already having an outer encap header */
244 NAPI_GRO_CB(skb)->is_fou = 1;
245
Tom Herbertafe93322014-09-17 12:25:57 -0700246 rcu_read_lock();
Tom Herbertefc98d02014-10-03 15:48:08 -0700247 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
Tom Herbertafe93322014-09-17 12:25:57 -0700248 ops = rcu_dereference(offloads[proto]);
249 if (!ops || !ops->callbacks.gro_receive)
250 goto out_unlock;
251
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200252 pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
Tom Herbertafe93322014-09-17 12:25:57 -0700253
254out_unlock:
255 rcu_read_unlock();
256
257 return pp;
258}
259
Tom Herbertd92283e2016-04-05 08:22:54 -0700260static int fou_gro_complete(struct sock *sk, struct sk_buff *skb,
261 int nhoff)
Tom Herbertafe93322014-09-17 12:25:57 -0700262{
263 const struct net_offload *ops;
Tom Herbertd92283e2016-04-05 08:22:54 -0700264 u8 proto = fou_from_sock(sk)->protocol;
Tom Herbertafe93322014-09-17 12:25:57 -0700265 int err = -ENOSYS;
Tom Herbertefc98d02014-10-03 15:48:08 -0700266 const struct net_offload **offloads;
Tom Herbertafe93322014-09-17 12:25:57 -0700267
268 rcu_read_lock();
Tom Herbertefc98d02014-10-03 15:48:08 -0700269 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
Tom Herbertafe93322014-09-17 12:25:57 -0700270 ops = rcu_dereference(offloads[proto]);
271 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
272 goto out_unlock;
273
274 err = ops->callbacks.gro_complete(skb, nhoff);
275
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700276 skb_set_inner_mac_header(skb, nhoff);
277
Tom Herbertafe93322014-09-17 12:25:57 -0700278out_unlock:
279 rcu_read_unlock();
280
281 return err;
282}
283
Tom Herberta8d31c12014-11-04 09:06:57 -0800284static struct guehdr *gue_gro_remcsum(struct sk_buff *skb, unsigned int off,
285 struct guehdr *guehdr, void *data,
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700286 size_t hdrlen, struct gro_remcsum *grc,
287 bool nopartial)
Tom Herberta8d31c12014-11-04 09:06:57 -0800288{
289 __be16 *pd = data;
Tom Herbert4fd671d2014-11-25 11:21:20 -0800290 size_t start = ntohs(pd[0]);
291 size_t offset = ntohs(pd[1]);
Tom Herberta8d31c12014-11-04 09:06:57 -0800292
293 if (skb->remcsum_offload)
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700294 return guehdr;
Tom Herberta8d31c12014-11-04 09:06:57 -0800295
Tom Herbert4fd671d2014-11-25 11:21:20 -0800296 if (!NAPI_GRO_CB(skb)->csum_valid)
Tom Herberta8d31c12014-11-04 09:06:57 -0800297 return NULL;
298
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700299 guehdr = skb_gro_remcsum_process(skb, (void *)guehdr, off, hdrlen,
300 start, offset, grc, nopartial);
Tom Herberta8d31c12014-11-04 09:06:57 -0800301
302 skb->remcsum_offload = 1;
303
304 return guehdr;
305}
306
Tom Herbertd92283e2016-04-05 08:22:54 -0700307static struct sk_buff **gue_gro_receive(struct sock *sk,
308 struct sk_buff **head,
309 struct sk_buff *skb)
Tom Herbert37dd0242014-10-03 15:48:09 -0700310{
311 const struct net_offload **offloads;
312 const struct net_offload *ops;
313 struct sk_buff **pp = NULL;
314 struct sk_buff *p;
Tom Herbert37dd0242014-10-03 15:48:09 -0700315 struct guehdr *guehdr;
Tom Herbert5024c332014-11-04 09:06:53 -0800316 size_t len, optlen, hdrlen, off;
317 void *data;
Tom Herberta8d31c12014-11-04 09:06:57 -0800318 u16 doffset = 0;
Tom Herbert37dd0242014-10-03 15:48:09 -0700319 int flush = 1;
Tom Herbertd92283e2016-04-05 08:22:54 -0700320 struct fou *fou = fou_from_sock(sk);
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800321 struct gro_remcsum grc;
Tom Herbertc1e48af2016-06-06 16:06:02 -0700322 u8 proto;
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800323
324 skb_gro_remcsum_init(&grc);
Tom Herbert37dd0242014-10-03 15:48:09 -0700325
326 off = skb_gro_offset(skb);
Tom Herbert5024c332014-11-04 09:06:53 -0800327 len = off + sizeof(*guehdr);
328
Tom Herbert37dd0242014-10-03 15:48:09 -0700329 guehdr = skb_gro_header_fast(skb, off);
Tom Herbert5024c332014-11-04 09:06:53 -0800330 if (skb_gro_header_hard(skb, len)) {
331 guehdr = skb_gro_header_slow(skb, len, off);
Tom Herbert37dd0242014-10-03 15:48:09 -0700332 if (unlikely(!guehdr))
333 goto out;
334 }
335
Tom Herbertc1e48af2016-06-06 16:06:02 -0700336 switch (guehdr->version) {
337 case 0:
338 break;
339 case 1:
340 switch (((struct iphdr *)guehdr)->version) {
341 case 4:
342 proto = IPPROTO_IPIP;
343 break;
344 case 6:
345 proto = IPPROTO_IPV6;
346 break;
347 default:
348 goto out;
349 }
350 goto next_proto;
351 default:
352 goto out;
353 }
354
Tom Herbert5024c332014-11-04 09:06:53 -0800355 optlen = guehdr->hlen << 2;
356 len += optlen;
Tom Herbert37dd0242014-10-03 15:48:09 -0700357
Tom Herbert5024c332014-11-04 09:06:53 -0800358 if (skb_gro_header_hard(skb, len)) {
359 guehdr = skb_gro_header_slow(skb, len, off);
Tom Herbert37dd0242014-10-03 15:48:09 -0700360 if (unlikely(!guehdr))
Tom Herbert5024c332014-11-04 09:06:53 -0800361 goto out;
362 }
363
364 if (unlikely(guehdr->control) || guehdr->version != 0 ||
365 validate_gue_flags(guehdr, optlen))
366 goto out;
367
368 hdrlen = sizeof(*guehdr) + optlen;
369
Tom Herberta8d31c12014-11-04 09:06:57 -0800370 /* Adjust NAPI_GRO_CB(skb)->csum to account for guehdr,
371 * this is needed if there is a remote checkcsum offload.
372 */
Tom Herbert5024c332014-11-04 09:06:53 -0800373 skb_gro_postpull_rcsum(skb, guehdr, hdrlen);
374
375 data = &guehdr[1];
376
377 if (guehdr->flags & GUE_FLAG_PRIV) {
Tom Herberta8d31c12014-11-04 09:06:57 -0800378 __be32 flags = *(__be32 *)(data + doffset);
Tom Herbert5024c332014-11-04 09:06:53 -0800379
Tom Herberta8d31c12014-11-04 09:06:57 -0800380 doffset += GUE_LEN_PRIV;
381
382 if (flags & GUE_PFLAG_REMCSUM) {
383 guehdr = gue_gro_remcsum(skb, off, guehdr,
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700384 data + doffset, hdrlen, &grc,
Tom Herbertfe881ef2015-02-10 16:30:33 -0800385 !!(fou->flags &
386 FOU_F_REMCSUM_NOPARTIAL));
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700387
Tom Herberta8d31c12014-11-04 09:06:57 -0800388 if (!guehdr)
389 goto out;
390
391 data = &guehdr[1];
392
393 doffset += GUE_PLEN_REMCSUM;
394 }
Tom Herbert37dd0242014-10-03 15:48:09 -0700395 }
396
Tom Herberta8d31c12014-11-04 09:06:57 -0800397 skb_gro_pull(skb, hdrlen);
398
Tom Herbert37dd0242014-10-03 15:48:09 -0700399 for (p = *head; p; p = p->next) {
400 const struct guehdr *guehdr2;
401
402 if (!NAPI_GRO_CB(p)->same_flow)
403 continue;
404
405 guehdr2 = (struct guehdr *)(p->data + off);
406
407 /* Compare base GUE header to be equal (covers
Tom Herbert5024c332014-11-04 09:06:53 -0800408 * hlen, version, proto_ctype, and flags.
Tom Herbert37dd0242014-10-03 15:48:09 -0700409 */
410 if (guehdr->word != guehdr2->word) {
411 NAPI_GRO_CB(p)->same_flow = 0;
412 continue;
413 }
414
415 /* Compare optional fields are the same. */
416 if (guehdr->hlen && memcmp(&guehdr[1], &guehdr2[1],
417 guehdr->hlen << 2)) {
418 NAPI_GRO_CB(p)->same_flow = 0;
419 continue;
420 }
421 }
422
Tom Herbertc1e48af2016-06-06 16:06:02 -0700423 proto = guehdr->proto_ctype;
424
425next_proto:
426
Alexander Duyckc3483382016-03-29 14:55:22 -0700427 /* We can clear the encap_mark for GUE as we are essentially doing
428 * one of two possible things. We are either adding an L4 tunnel
429 * header to the outer L3 tunnel header, or we are are simply
430 * treating the GRE tunnel header as though it is a UDP protocol
431 * specific header such as VXLAN or GENEVE.
432 */
433 NAPI_GRO_CB(skb)->encap_mark = 0;
434
Alexander Duycka0ca1532016-04-05 09:13:39 -0700435 /* Flag this frame as already having an outer encap header */
436 NAPI_GRO_CB(skb)->is_fou = 1;
437
Tom Herbert5024c332014-11-04 09:06:53 -0800438 rcu_read_lock();
439 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
Tom Herbertc1e48af2016-06-06 16:06:02 -0700440 ops = rcu_dereference(offloads[proto]);
Tom Herbert27013662015-08-19 17:07:34 -0700441 if (WARN_ON_ONCE(!ops || !ops->callbacks.gro_receive))
Tom Herbert5024c332014-11-04 09:06:53 -0800442 goto out_unlock;
Tom Herbert37dd0242014-10-03 15:48:09 -0700443
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200444 pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800445 flush = 0;
Tom Herbert37dd0242014-10-03 15:48:09 -0700446
447out_unlock:
448 rcu_read_unlock();
449out:
450 NAPI_GRO_CB(skb)->flush |= flush;
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800451 skb_gro_remcsum_cleanup(skb, &grc);
Tom Herbert37dd0242014-10-03 15:48:09 -0700452
453 return pp;
454}
455
Tom Herbertd92283e2016-04-05 08:22:54 -0700456static int gue_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
Tom Herbert37dd0242014-10-03 15:48:09 -0700457{
458 const struct net_offload **offloads;
459 struct guehdr *guehdr = (struct guehdr *)(skb->data + nhoff);
460 const struct net_offload *ops;
Tom Herbertc1e48af2016-06-06 16:06:02 -0700461 unsigned int guehlen = 0;
Tom Herbert37dd0242014-10-03 15:48:09 -0700462 u8 proto;
463 int err = -ENOENT;
464
Tom Herbertc1e48af2016-06-06 16:06:02 -0700465 switch (guehdr->version) {
466 case 0:
467 proto = guehdr->proto_ctype;
468 guehlen = sizeof(*guehdr) + (guehdr->hlen << 2);
469 break;
470 case 1:
471 switch (((struct iphdr *)guehdr)->version) {
472 case 4:
473 proto = IPPROTO_IPIP;
474 break;
475 case 6:
476 proto = IPPROTO_IPV6;
477 break;
478 default:
479 return err;
480 }
481 break;
482 default:
483 return err;
484 }
Tom Herbert37dd0242014-10-03 15:48:09 -0700485
486 rcu_read_lock();
487 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
488 ops = rcu_dereference(offloads[proto]);
489 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
490 goto out_unlock;
491
492 err = ops->callbacks.gro_complete(skb, nhoff + guehlen);
493
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700494 skb_set_inner_mac_header(skb, nhoff + guehlen);
495
Tom Herbert37dd0242014-10-03 15:48:09 -0700496out_unlock:
497 rcu_read_unlock();
498 return err;
499}
500
WANG Cong02d793c2015-04-10 12:00:29 -0700501static int fou_add_to_port_list(struct net *net, struct fou *fou)
Tom Herbert23461552014-09-17 12:25:56 -0700502{
WANG Cong02d793c2015-04-10 12:00:29 -0700503 struct fou_net *fn = net_generic(net, fou_net_id);
Tom Herbert23461552014-09-17 12:25:56 -0700504 struct fou *fout;
505
WANG Cong02d793c2015-04-10 12:00:29 -0700506 mutex_lock(&fn->fou_lock);
507 list_for_each_entry(fout, &fn->fou_list, list) {
Tom Herbert5f914b62016-05-18 09:06:16 -0700508 if (fou->port == fout->port &&
509 fou->family == fout->family) {
WANG Cong02d793c2015-04-10 12:00:29 -0700510 mutex_unlock(&fn->fou_lock);
Tom Herbert23461552014-09-17 12:25:56 -0700511 return -EALREADY;
512 }
513 }
514
WANG Cong02d793c2015-04-10 12:00:29 -0700515 list_add(&fou->list, &fn->fou_list);
516 mutex_unlock(&fn->fou_lock);
Tom Herbert23461552014-09-17 12:25:56 -0700517
518 return 0;
519}
520
521static void fou_release(struct fou *fou)
522{
523 struct socket *sock = fou->sock;
Tom Herbert23461552014-09-17 12:25:56 -0700524
Tom Herbert23461552014-09-17 12:25:56 -0700525 list_del(&fou->list);
WANG Cong02d793c2015-04-10 12:00:29 -0700526 udp_tunnel_sock_release(sock);
Tom Herbert23461552014-09-17 12:25:56 -0700527
Hannes Frederic Sowa3036fac2015-12-15 21:01:53 +0100528 kfree_rcu(fou, rcu);
Tom Herbert23461552014-09-17 12:25:56 -0700529}
530
531static int fou_create(struct net *net, struct fou_cfg *cfg,
532 struct socket **sockp)
533{
Tom Herbert23461552014-09-17 12:25:56 -0700534 struct socket *sock = NULL;
WANG Cong02d793c2015-04-10 12:00:29 -0700535 struct fou *fou = NULL;
Tom Herbert23461552014-09-17 12:25:56 -0700536 struct sock *sk;
Tom Herbert440924b2016-05-18 09:06:14 -0700537 struct udp_tunnel_sock_cfg tunnel_cfg;
WANG Cong02d793c2015-04-10 12:00:29 -0700538 int err;
Tom Herbert23461552014-09-17 12:25:56 -0700539
540 /* Open UDP socket */
541 err = udp_sock_create(net, &cfg->udp_config, &sock);
542 if (err < 0)
543 goto error;
544
545 /* Allocate FOU port structure */
546 fou = kzalloc(sizeof(*fou), GFP_KERNEL);
547 if (!fou) {
548 err = -ENOMEM;
549 goto error;
550 }
551
552 sk = sock->sk;
553
Tom Herbert37dd0242014-10-03 15:48:09 -0700554 fou->port = cfg->udp_config.local_udp_port;
Tom Herbert5f914b62016-05-18 09:06:16 -0700555 fou->family = cfg->udp_config.family;
556 fou->flags = cfg->flags;
Tom Herbert440924b2016-05-18 09:06:14 -0700557 fou->type = cfg->type;
558 fou->sock = sock;
559
560 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
561 tunnel_cfg.encap_type = 1;
562 tunnel_cfg.sk_user_data = fou;
563 tunnel_cfg.encap_destroy = NULL;
Tom Herbert37dd0242014-10-03 15:48:09 -0700564
565 /* Initial for fou type */
566 switch (cfg->type) {
567 case FOU_ENCAP_DIRECT:
Tom Herbert440924b2016-05-18 09:06:14 -0700568 tunnel_cfg.encap_rcv = fou_udp_recv;
569 tunnel_cfg.gro_receive = fou_gro_receive;
570 tunnel_cfg.gro_complete = fou_gro_complete;
571 fou->protocol = cfg->protocol;
Tom Herbert37dd0242014-10-03 15:48:09 -0700572 break;
573 case FOU_ENCAP_GUE:
Tom Herbert440924b2016-05-18 09:06:14 -0700574 tunnel_cfg.encap_rcv = gue_udp_recv;
575 tunnel_cfg.gro_receive = gue_gro_receive;
576 tunnel_cfg.gro_complete = gue_gro_complete;
Tom Herbert37dd0242014-10-03 15:48:09 -0700577 break;
578 default:
579 err = -EINVAL;
580 goto error;
581 }
Tom Herbert23461552014-09-17 12:25:56 -0700582
Tom Herbert440924b2016-05-18 09:06:14 -0700583 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Tom Herbert23461552014-09-17 12:25:56 -0700584
585 sk->sk_allocation = GFP_ATOMIC;
586
WANG Cong02d793c2015-04-10 12:00:29 -0700587 err = fou_add_to_port_list(net, fou);
Tom Herbert23461552014-09-17 12:25:56 -0700588 if (err)
589 goto error;
590
591 if (sockp)
592 *sockp = sock;
593
594 return 0;
595
596error:
597 kfree(fou);
598 if (sock)
WANG Cong02d793c2015-04-10 12:00:29 -0700599 udp_tunnel_sock_release(sock);
Tom Herbert23461552014-09-17 12:25:56 -0700600
601 return err;
602}
603
604static int fou_destroy(struct net *net, struct fou_cfg *cfg)
605{
WANG Cong02d793c2015-04-10 12:00:29 -0700606 struct fou_net *fn = net_generic(net, fou_net_id);
WANG Cong4cbcdf22015-04-10 12:00:28 -0700607 __be16 port = cfg->udp_config.local_udp_port;
Tom Herbert5f914b62016-05-18 09:06:16 -0700608 u8 family = cfg->udp_config.family;
Tom Herbert23461552014-09-17 12:25:56 -0700609 int err = -EINVAL;
WANG Cong02d793c2015-04-10 12:00:29 -0700610 struct fou *fou;
Tom Herbert23461552014-09-17 12:25:56 -0700611
WANG Cong02d793c2015-04-10 12:00:29 -0700612 mutex_lock(&fn->fou_lock);
613 list_for_each_entry(fou, &fn->fou_list, list) {
Tom Herbert5f914b62016-05-18 09:06:16 -0700614 if (fou->port == port && fou->family == family) {
Tom Herbert23461552014-09-17 12:25:56 -0700615 fou_release(fou);
616 err = 0;
617 break;
618 }
619 }
WANG Cong02d793c2015-04-10 12:00:29 -0700620 mutex_unlock(&fn->fou_lock);
Tom Herbert23461552014-09-17 12:25:56 -0700621
622 return err;
623}
624
Johannes Berg489111e2016-10-24 14:40:03 +0200625static struct genl_family fou_nl_family;
Tom Herbert23461552014-09-17 12:25:56 -0700626
stephen hemminger3f18ff22016-08-31 15:19:37 -0700627static const struct nla_policy fou_nl_policy[FOU_ATTR_MAX + 1] = {
Tom Herbert23461552014-09-17 12:25:56 -0700628 [FOU_ATTR_PORT] = { .type = NLA_U16, },
629 [FOU_ATTR_AF] = { .type = NLA_U8, },
630 [FOU_ATTR_IPPROTO] = { .type = NLA_U8, },
Tom Herbert37dd0242014-10-03 15:48:09 -0700631 [FOU_ATTR_TYPE] = { .type = NLA_U8, },
Tom Herbertfe881ef2015-02-10 16:30:33 -0800632 [FOU_ATTR_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG, },
Tom Herbert23461552014-09-17 12:25:56 -0700633};
634
635static int parse_nl_config(struct genl_info *info,
636 struct fou_cfg *cfg)
637{
638 memset(cfg, 0, sizeof(*cfg));
639
640 cfg->udp_config.family = AF_INET;
641
642 if (info->attrs[FOU_ATTR_AF]) {
643 u8 family = nla_get_u8(info->attrs[FOU_ATTR_AF]);
644
Tom Herbert5f914b62016-05-18 09:06:16 -0700645 switch (family) {
646 case AF_INET:
647 break;
648 case AF_INET6:
649 cfg->udp_config.ipv6_v6only = 1;
650 break;
651 default:
652 return -EAFNOSUPPORT;
653 }
Tom Herbert23461552014-09-17 12:25:56 -0700654
655 cfg->udp_config.family = family;
656 }
657
658 if (info->attrs[FOU_ATTR_PORT]) {
WANG Cong4cbcdf22015-04-10 12:00:28 -0700659 __be16 port = nla_get_be16(info->attrs[FOU_ATTR_PORT]);
Tom Herbert23461552014-09-17 12:25:56 -0700660
661 cfg->udp_config.local_udp_port = port;
662 }
663
664 if (info->attrs[FOU_ATTR_IPPROTO])
665 cfg->protocol = nla_get_u8(info->attrs[FOU_ATTR_IPPROTO]);
666
Tom Herbert37dd0242014-10-03 15:48:09 -0700667 if (info->attrs[FOU_ATTR_TYPE])
668 cfg->type = nla_get_u8(info->attrs[FOU_ATTR_TYPE]);
669
Tom Herbertfe881ef2015-02-10 16:30:33 -0800670 if (info->attrs[FOU_ATTR_REMCSUM_NOPARTIAL])
671 cfg->flags |= FOU_F_REMCSUM_NOPARTIAL;
672
Tom Herbert23461552014-09-17 12:25:56 -0700673 return 0;
674}
675
676static int fou_nl_cmd_add_port(struct sk_buff *skb, struct genl_info *info)
677{
WANG Cong02d793c2015-04-10 12:00:29 -0700678 struct net *net = genl_info_net(info);
Tom Herbert23461552014-09-17 12:25:56 -0700679 struct fou_cfg cfg;
680 int err;
681
682 err = parse_nl_config(info, &cfg);
683 if (err)
684 return err;
685
WANG Cong02d793c2015-04-10 12:00:29 -0700686 return fou_create(net, &cfg, NULL);
Tom Herbert23461552014-09-17 12:25:56 -0700687}
688
689static int fou_nl_cmd_rm_port(struct sk_buff *skb, struct genl_info *info)
690{
WANG Cong02d793c2015-04-10 12:00:29 -0700691 struct net *net = genl_info_net(info);
Tom Herbert23461552014-09-17 12:25:56 -0700692 struct fou_cfg cfg;
WANG Cong67270632015-04-10 12:00:27 -0700693 int err;
Tom Herbert23461552014-09-17 12:25:56 -0700694
WANG Cong67270632015-04-10 12:00:27 -0700695 err = parse_nl_config(info, &cfg);
696 if (err)
697 return err;
Tom Herbert23461552014-09-17 12:25:56 -0700698
WANG Cong02d793c2015-04-10 12:00:29 -0700699 return fou_destroy(net, &cfg);
Tom Herbert23461552014-09-17 12:25:56 -0700700}
701
WANG Cong7a6c8c32015-04-10 12:00:30 -0700702static int fou_fill_info(struct fou *fou, struct sk_buff *msg)
703{
704 if (nla_put_u8(msg, FOU_ATTR_AF, fou->sock->sk->sk_family) ||
705 nla_put_be16(msg, FOU_ATTR_PORT, fou->port) ||
706 nla_put_u8(msg, FOU_ATTR_IPPROTO, fou->protocol) ||
707 nla_put_u8(msg, FOU_ATTR_TYPE, fou->type))
708 return -1;
709
710 if (fou->flags & FOU_F_REMCSUM_NOPARTIAL)
711 if (nla_put_flag(msg, FOU_ATTR_REMCSUM_NOPARTIAL))
712 return -1;
713 return 0;
714}
715
716static int fou_dump_info(struct fou *fou, u32 portid, u32 seq,
717 u32 flags, struct sk_buff *skb, u8 cmd)
718{
719 void *hdr;
720
721 hdr = genlmsg_put(skb, portid, seq, &fou_nl_family, flags, cmd);
722 if (!hdr)
723 return -ENOMEM;
724
725 if (fou_fill_info(fou, skb) < 0)
726 goto nla_put_failure;
727
728 genlmsg_end(skb, hdr);
729 return 0;
730
731nla_put_failure:
732 genlmsg_cancel(skb, hdr);
733 return -EMSGSIZE;
734}
735
736static int fou_nl_cmd_get_port(struct sk_buff *skb, struct genl_info *info)
737{
738 struct net *net = genl_info_net(info);
739 struct fou_net *fn = net_generic(net, fou_net_id);
740 struct sk_buff *msg;
741 struct fou_cfg cfg;
742 struct fou *fout;
743 __be16 port;
Tom Herbert5f914b62016-05-18 09:06:16 -0700744 u8 family;
WANG Cong7a6c8c32015-04-10 12:00:30 -0700745 int ret;
746
747 ret = parse_nl_config(info, &cfg);
748 if (ret)
749 return ret;
750 port = cfg.udp_config.local_udp_port;
751 if (port == 0)
752 return -EINVAL;
753
Tom Herbert5f914b62016-05-18 09:06:16 -0700754 family = cfg.udp_config.family;
755 if (family != AF_INET && family != AF_INET6)
756 return -EINVAL;
757
WANG Cong7a6c8c32015-04-10 12:00:30 -0700758 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
759 if (!msg)
760 return -ENOMEM;
761
762 ret = -ESRCH;
763 mutex_lock(&fn->fou_lock);
764 list_for_each_entry(fout, &fn->fou_list, list) {
Tom Herbert5f914b62016-05-18 09:06:16 -0700765 if (port == fout->port && family == fout->family) {
WANG Cong7a6c8c32015-04-10 12:00:30 -0700766 ret = fou_dump_info(fout, info->snd_portid,
767 info->snd_seq, 0, msg,
768 info->genlhdr->cmd);
769 break;
770 }
771 }
772 mutex_unlock(&fn->fou_lock);
773 if (ret < 0)
774 goto out_free;
775
776 return genlmsg_reply(msg, info);
777
778out_free:
779 nlmsg_free(msg);
780 return ret;
781}
782
783static int fou_nl_dump(struct sk_buff *skb, struct netlink_callback *cb)
784{
785 struct net *net = sock_net(skb->sk);
786 struct fou_net *fn = net_generic(net, fou_net_id);
787 struct fou *fout;
788 int idx = 0, ret;
789
790 mutex_lock(&fn->fou_lock);
791 list_for_each_entry(fout, &fn->fou_list, list) {
792 if (idx++ < cb->args[0])
793 continue;
794 ret = fou_dump_info(fout, NETLINK_CB(cb->skb).portid,
795 cb->nlh->nlmsg_seq, NLM_F_MULTI,
796 skb, FOU_CMD_GET);
797 if (ret)
WANG Cong540207a2015-04-15 11:48:49 -0700798 break;
WANG Cong7a6c8c32015-04-10 12:00:30 -0700799 }
800 mutex_unlock(&fn->fou_lock);
801
WANG Cong7a6c8c32015-04-10 12:00:30 -0700802 cb->args[0] = idx;
803 return skb->len;
804}
805
Tom Herbert23461552014-09-17 12:25:56 -0700806static const struct genl_ops fou_nl_ops[] = {
807 {
808 .cmd = FOU_CMD_ADD,
809 .doit = fou_nl_cmd_add_port,
810 .policy = fou_nl_policy,
811 .flags = GENL_ADMIN_PERM,
812 },
813 {
814 .cmd = FOU_CMD_DEL,
815 .doit = fou_nl_cmd_rm_port,
816 .policy = fou_nl_policy,
817 .flags = GENL_ADMIN_PERM,
818 },
WANG Cong7a6c8c32015-04-10 12:00:30 -0700819 {
820 .cmd = FOU_CMD_GET,
821 .doit = fou_nl_cmd_get_port,
822 .dumpit = fou_nl_dump,
823 .policy = fou_nl_policy,
824 },
Tom Herbert23461552014-09-17 12:25:56 -0700825};
826
Johannes Berg56989f62016-10-24 14:40:05 +0200827static struct genl_family fou_nl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +0200828 .hdrsize = 0,
829 .name = FOU_GENL_NAME,
830 .version = FOU_GENL_VERSION,
831 .maxattr = FOU_ATTR_MAX,
832 .netnsok = true,
833 .module = THIS_MODULE,
834 .ops = fou_nl_ops,
835 .n_ops = ARRAY_SIZE(fou_nl_ops),
836};
837
Tom Herberta8c5f902014-11-12 11:54:09 -0800838size_t fou_encap_hlen(struct ip_tunnel_encap *e)
839{
840 return sizeof(struct udphdr);
841}
842EXPORT_SYMBOL(fou_encap_hlen);
843
844size_t gue_encap_hlen(struct ip_tunnel_encap *e)
845{
846 size_t len;
847 bool need_priv = false;
848
849 len = sizeof(struct udphdr) + sizeof(struct guehdr);
850
851 if (e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) {
852 len += GUE_PLEN_REMCSUM;
853 need_priv = true;
854 }
855
856 len += need_priv ? GUE_LEN_PRIV : 0;
857
858 return len;
859}
860EXPORT_SYMBOL(gue_encap_hlen);
861
Tom Herbert63487ba2014-11-04 09:06:51 -0800862static void fou_build_udp(struct sk_buff *skb, struct ip_tunnel_encap *e,
863 struct flowi4 *fl4, u8 *protocol, __be16 sport)
864{
865 struct udphdr *uh;
866
867 skb_push(skb, sizeof(struct udphdr));
868 skb_reset_transport_header(skb);
869
870 uh = udp_hdr(skb);
871
872 uh->dest = e->dport;
873 uh->source = sport;
874 uh->len = htons(skb->len);
Tom Herbert63487ba2014-11-04 09:06:51 -0800875 udp_set_csum(!(e->flags & TUNNEL_ENCAP_FLAG_CSUM), skb,
876 fl4->saddr, fl4->daddr, skb->len);
877
878 *protocol = IPPROTO_UDP;
879}
880
Tom Herbertdc969b82016-05-18 09:06:15 -0700881int __fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
882 u8 *protocol, __be16 *sport, int type)
883{
884 int err;
885
886 err = iptunnel_handle_offloads(skb, type);
887 if (err)
888 return err;
889
890 *sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
891 skb, 0, 0, false);
892
893 return 0;
894}
895EXPORT_SYMBOL(__fou_build_header);
896
Tom Herbert63487ba2014-11-04 09:06:51 -0800897int fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
898 u8 *protocol, struct flowi4 *fl4)
899{
Edward Cree06f62292016-02-11 21:00:16 +0000900 int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM ? SKB_GSO_UDP_TUNNEL_CSUM :
901 SKB_GSO_UDP_TUNNEL;
Tom Herbert63487ba2014-11-04 09:06:51 -0800902 __be16 sport;
Alexander Duyckaed069d2016-04-14 15:33:37 -0400903 int err;
Tom Herbert63487ba2014-11-04 09:06:51 -0800904
Tom Herbertdc969b82016-05-18 09:06:15 -0700905 err = __fou_build_header(skb, e, protocol, &sport, type);
Alexander Duyckaed069d2016-04-14 15:33:37 -0400906 if (err)
907 return err;
Tom Herbert63487ba2014-11-04 09:06:51 -0800908
Tom Herbert63487ba2014-11-04 09:06:51 -0800909 fou_build_udp(skb, e, fl4, protocol, sport);
910
911 return 0;
912}
913EXPORT_SYMBOL(fou_build_header);
914
Tom Herbertdc969b82016-05-18 09:06:15 -0700915int __gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
916 u8 *protocol, __be16 *sport, int type)
Tom Herbert63487ba2014-11-04 09:06:51 -0800917{
Tom Herbert63487ba2014-11-04 09:06:51 -0800918 struct guehdr *guehdr;
Tom Herbertb17f7092014-11-04 09:06:56 -0800919 size_t hdrlen, optlen = 0;
Tom Herbert5024c332014-11-04 09:06:53 -0800920 void *data;
921 bool need_priv = false;
Alexander Duyckaed069d2016-04-14 15:33:37 -0400922 int err;
Tom Herbert5024c332014-11-04 09:06:53 -0800923
Tom Herbertb17f7092014-11-04 09:06:56 -0800924 if ((e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) &&
925 skb->ip_summed == CHECKSUM_PARTIAL) {
Tom Herbertb17f7092014-11-04 09:06:56 -0800926 optlen += GUE_PLEN_REMCSUM;
927 type |= SKB_GSO_TUNNEL_REMCSUM;
928 need_priv = true;
929 }
930
Tom Herbert5024c332014-11-04 09:06:53 -0800931 optlen += need_priv ? GUE_LEN_PRIV : 0;
Tom Herbert63487ba2014-11-04 09:06:51 -0800932
Alexander Duyckaed069d2016-04-14 15:33:37 -0400933 err = iptunnel_handle_offloads(skb, type);
934 if (err)
935 return err;
Tom Herbert63487ba2014-11-04 09:06:51 -0800936
937 /* Get source port (based on flow hash) before skb_push */
Tom Herbertdc969b82016-05-18 09:06:15 -0700938 *sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
939 skb, 0, 0, false);
Tom Herbert63487ba2014-11-04 09:06:51 -0800940
Tom Herbertb17f7092014-11-04 09:06:56 -0800941 hdrlen = sizeof(struct guehdr) + optlen;
942
943 skb_push(skb, hdrlen);
Tom Herbert63487ba2014-11-04 09:06:51 -0800944
945 guehdr = (struct guehdr *)skb->data;
946
Tom Herbert5024c332014-11-04 09:06:53 -0800947 guehdr->control = 0;
Tom Herbert63487ba2014-11-04 09:06:51 -0800948 guehdr->version = 0;
Tom Herbert5024c332014-11-04 09:06:53 -0800949 guehdr->hlen = optlen >> 2;
Tom Herbert63487ba2014-11-04 09:06:51 -0800950 guehdr->flags = 0;
Tom Herbert5024c332014-11-04 09:06:53 -0800951 guehdr->proto_ctype = *protocol;
952
953 data = &guehdr[1];
954
955 if (need_priv) {
956 __be32 *flags = data;
957
958 guehdr->flags |= GUE_FLAG_PRIV;
959 *flags = 0;
960 data += GUE_LEN_PRIV;
961
Tom Herbertb17f7092014-11-04 09:06:56 -0800962 if (type & SKB_GSO_TUNNEL_REMCSUM) {
963 u16 csum_start = skb_checksum_start_offset(skb);
964 __be16 *pd = data;
965
966 if (csum_start < hdrlen)
967 return -EINVAL;
968
969 csum_start -= hdrlen;
970 pd[0] = htons(csum_start);
971 pd[1] = htons(csum_start + skb->csum_offset);
972
973 if (!skb_is_gso(skb)) {
974 skb->ip_summed = CHECKSUM_NONE;
975 skb->encapsulation = 0;
976 }
977
978 *flags |= GUE_PFLAG_REMCSUM;
979 data += GUE_PLEN_REMCSUM;
980 }
981
Tom Herbert5024c332014-11-04 09:06:53 -0800982 }
Tom Herbert63487ba2014-11-04 09:06:51 -0800983
Tom Herbertdc969b82016-05-18 09:06:15 -0700984 return 0;
985}
986EXPORT_SYMBOL(__gue_build_header);
987
988int gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
989 u8 *protocol, struct flowi4 *fl4)
990{
991 int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM ? SKB_GSO_UDP_TUNNEL_CSUM :
992 SKB_GSO_UDP_TUNNEL;
993 __be16 sport;
994 int err;
995
996 err = __gue_build_header(skb, e, protocol, &sport, type);
997 if (err)
998 return err;
999
Tom Herbert63487ba2014-11-04 09:06:51 -08001000 fou_build_udp(skb, e, fl4, protocol, sport);
1001
1002 return 0;
1003}
1004EXPORT_SYMBOL(gue_build_header);
1005
Tom Herberta8c5f902014-11-12 11:54:09 -08001006#ifdef CONFIG_NET_FOU_IP_TUNNELS
1007
Andi Kleen5eeb2922015-04-08 06:04:31 -07001008static const struct ip_tunnel_encap_ops fou_iptun_ops = {
Tom Herberta8c5f902014-11-12 11:54:09 -08001009 .encap_hlen = fou_encap_hlen,
1010 .build_header = fou_build_header,
1011};
1012
Andi Kleen5eeb2922015-04-08 06:04:31 -07001013static const struct ip_tunnel_encap_ops gue_iptun_ops = {
Tom Herberta8c5f902014-11-12 11:54:09 -08001014 .encap_hlen = gue_encap_hlen,
1015 .build_header = gue_build_header,
1016};
1017
1018static int ip_tunnel_encap_add_fou_ops(void)
1019{
1020 int ret;
1021
1022 ret = ip_tunnel_encap_add_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
1023 if (ret < 0) {
1024 pr_err("can't add fou ops\n");
1025 return ret;
1026 }
1027
1028 ret = ip_tunnel_encap_add_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
1029 if (ret < 0) {
1030 pr_err("can't add gue ops\n");
1031 ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
1032 return ret;
1033 }
1034
1035 return 0;
1036}
1037
1038static void ip_tunnel_encap_del_fou_ops(void)
1039{
1040 ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
1041 ip_tunnel_encap_del_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
1042}
1043
1044#else
1045
1046static int ip_tunnel_encap_add_fou_ops(void)
1047{
1048 return 0;
1049}
1050
Thomas Graf882288c2014-11-13 12:48:21 +01001051static void ip_tunnel_encap_del_fou_ops(void)
Tom Herberta8c5f902014-11-12 11:54:09 -08001052{
1053}
1054
1055#endif
1056
WANG Cong02d793c2015-04-10 12:00:29 -07001057static __net_init int fou_init_net(struct net *net)
1058{
1059 struct fou_net *fn = net_generic(net, fou_net_id);
1060
1061 INIT_LIST_HEAD(&fn->fou_list);
1062 mutex_init(&fn->fou_lock);
1063 return 0;
1064}
1065
1066static __net_exit void fou_exit_net(struct net *net)
1067{
1068 struct fou_net *fn = net_generic(net, fou_net_id);
1069 struct fou *fou, *next;
1070
1071 /* Close all the FOU sockets */
1072 mutex_lock(&fn->fou_lock);
1073 list_for_each_entry_safe(fou, next, &fn->fou_list, list)
1074 fou_release(fou);
1075 mutex_unlock(&fn->fou_lock);
1076}
1077
1078static struct pernet_operations fou_net_ops = {
1079 .init = fou_init_net,
1080 .exit = fou_exit_net,
1081 .id = &fou_net_id,
1082 .size = sizeof(struct fou_net),
1083};
1084
Tom Herbert23461552014-09-17 12:25:56 -07001085static int __init fou_init(void)
1086{
1087 int ret;
1088
WANG Cong02d793c2015-04-10 12:00:29 -07001089 ret = register_pernet_device(&fou_net_ops);
1090 if (ret)
Tom Herberta8c5f902014-11-12 11:54:09 -08001091 goto exit;
1092
Johannes Berg489111e2016-10-24 14:40:03 +02001093 ret = genl_register_family(&fou_nl_family);
Tom Herberta8c5f902014-11-12 11:54:09 -08001094 if (ret < 0)
WANG Cong02d793c2015-04-10 12:00:29 -07001095 goto unregister;
Tom Herberta8c5f902014-11-12 11:54:09 -08001096
WANG Cong02d793c2015-04-10 12:00:29 -07001097 ret = ip_tunnel_encap_add_fou_ops();
1098 if (ret == 0)
1099 return 0;
1100
1101 genl_unregister_family(&fou_nl_family);
1102unregister:
1103 unregister_pernet_device(&fou_net_ops);
Tom Herberta8c5f902014-11-12 11:54:09 -08001104exit:
Tom Herbert23461552014-09-17 12:25:56 -07001105 return ret;
1106}
1107
1108static void __exit fou_fini(void)
1109{
Tom Herberta8c5f902014-11-12 11:54:09 -08001110 ip_tunnel_encap_del_fou_ops();
Tom Herbert23461552014-09-17 12:25:56 -07001111 genl_unregister_family(&fou_nl_family);
WANG Cong02d793c2015-04-10 12:00:29 -07001112 unregister_pernet_device(&fou_net_ops);
Tom Herbert23461552014-09-17 12:25:56 -07001113}
1114
1115module_init(fou_init);
1116module_exit(fou_fini);
1117MODULE_AUTHOR("Tom Herbert <therbert@google.com>");
1118MODULE_LICENSE("GPL");