blob: 8e0257d0120097770e37017439684a2345619f5e [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>
stephen hemminger9dc621a2017-05-19 09:55:54 -070011#include <net/fou.h>
Tom Herbert23461552014-09-17 12:25:56 -070012#include <net/ip.h>
Tom Herbertafe93322014-09-17 12:25:57 -070013#include <net/protocol.h>
Tom Herbert23461552014-09-17 12:25:56 -070014#include <net/udp.h>
15#include <net/udp_tunnel.h>
16#include <net/xfrm.h>
17#include <uapi/linux/fou.h>
18#include <uapi/linux/genetlink.h>
19
Tom Herbert23461552014-09-17 12:25:56 -070020struct fou {
21 struct socket *sock;
22 u8 protocol;
Tom Herbertfe881ef2015-02-10 16:30:33 -080023 u8 flags;
WANG Cong4cbcdf22015-04-10 12:00:28 -070024 __be16 port;
Tom Herbert5f914b62016-05-18 09:06:16 -070025 u8 family;
WANG Cong7a6c8c32015-04-10 12:00:30 -070026 u16 type;
Tom Herbert23461552014-09-17 12:25:56 -070027 struct list_head list;
Hannes Frederic Sowa3036fac2015-12-15 21:01:53 +010028 struct rcu_head rcu;
Tom Herbert23461552014-09-17 12:25:56 -070029};
30
Tom Herbertfe881ef2015-02-10 16:30:33 -080031#define FOU_F_REMCSUM_NOPARTIAL BIT(0)
32
Tom Herbert23461552014-09-17 12:25:56 -070033struct fou_cfg {
Tom Herbert37dd0242014-10-03 15:48:09 -070034 u16 type;
Tom Herbert23461552014-09-17 12:25:56 -070035 u8 protocol;
Tom Herbertfe881ef2015-02-10 16:30:33 -080036 u8 flags;
Tom Herbert23461552014-09-17 12:25:56 -070037 struct udp_port_cfg udp_config;
38};
39
WANG Cong02d793c2015-04-10 12:00:29 -070040static unsigned int fou_net_id;
41
42struct fou_net {
43 struct list_head fou_list;
44 struct mutex fou_lock;
45};
46
Tom Herbert23461552014-09-17 12:25:56 -070047static inline struct fou *fou_from_sock(struct sock *sk)
48{
49 return sk->sk_user_data;
50}
51
Tom Herbert5f914b62016-05-18 09:06:16 -070052static int fou_recv_pull(struct sk_buff *skb, struct fou *fou, size_t len)
Tom Herbert23461552014-09-17 12:25:56 -070053{
Tom Herbert23461552014-09-17 12:25:56 -070054 /* Remove 'len' bytes from the packet (UDP header and
Tom Herbert5024c332014-11-04 09:06:53 -080055 * FOU header if present).
Tom Herbert23461552014-09-17 12:25:56 -070056 */
Tom Herbert5f914b62016-05-18 09:06:16 -070057 if (fou->family == AF_INET)
58 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(skb)->tot_len) - len);
59 else
60 ipv6_hdr(skb)->payload_len =
61 htons(ntohs(ipv6_hdr(skb)->payload_len) - len);
62
Tom Herbert23461552014-09-17 12:25:56 -070063 __skb_pull(skb, len);
64 skb_postpull_rcsum(skb, udp_hdr(skb), len);
65 skb_reset_transport_header(skb);
Jesse Grossa09a4c82016-03-19 09:32:02 -070066 return iptunnel_pull_offloads(skb);
Tom Herbert23461552014-09-17 12:25:56 -070067}
68
69static int fou_udp_recv(struct sock *sk, struct sk_buff *skb)
70{
71 struct fou *fou = fou_from_sock(sk);
72
73 if (!fou)
74 return 1;
75
Tom Herbert5f914b62016-05-18 09:06:16 -070076 if (fou_recv_pull(skb, fou, sizeof(struct udphdr)))
Jesse Grossa09a4c82016-03-19 09:32:02 -070077 goto drop;
Tom Herbert5024c332014-11-04 09:06:53 -080078
79 return -fou->protocol;
Jesse Grossa09a4c82016-03-19 09:32:02 -070080
81drop:
82 kfree_skb(skb);
83 return 0;
Tom Herbert5024c332014-11-04 09:06:53 -080084}
85
Tom Herberta8d31c12014-11-04 09:06:57 -080086static struct guehdr *gue_remcsum(struct sk_buff *skb, struct guehdr *guehdr,
Tom Herbertfe881ef2015-02-10 16:30:33 -080087 void *data, size_t hdrlen, u8 ipproto,
88 bool nopartial)
Tom Herberta8d31c12014-11-04 09:06:57 -080089{
90 __be16 *pd = data;
Tom Herbert4fd671d2014-11-25 11:21:20 -080091 size_t start = ntohs(pd[0]);
92 size_t offset = ntohs(pd[1]);
Tom Herbertb7fe10e2015-08-19 17:07:32 -070093 size_t plen = sizeof(struct udphdr) + hdrlen +
94 max_t(size_t, offset + sizeof(u16), start);
95
96 if (skb->remcsum_offload)
97 return guehdr;
Tom Herberta8d31c12014-11-04 09:06:57 -080098
Tom Herberta8d31c12014-11-04 09:06:57 -080099 if (!pskb_may_pull(skb, plen))
100 return NULL;
101 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
102
Tom Herbertfe881ef2015-02-10 16:30:33 -0800103 skb_remcsum_process(skb, (void *)guehdr + hdrlen,
104 start, offset, nopartial);
Tom Herberta8d31c12014-11-04 09:06:57 -0800105
106 return guehdr;
107}
108
Tom Herbert5024c332014-11-04 09:06:53 -0800109static int gue_control_message(struct sk_buff *skb, struct guehdr *guehdr)
110{
111 /* No support yet */
112 kfree_skb(skb);
113 return 0;
Tom Herbert23461552014-09-17 12:25:56 -0700114}
115
Tom Herbert37dd0242014-10-03 15:48:09 -0700116static int gue_udp_recv(struct sock *sk, struct sk_buff *skb)
117{
118 struct fou *fou = fou_from_sock(sk);
Tom Herbert5024c332014-11-04 09:06:53 -0800119 size_t len, optlen, hdrlen;
Tom Herbert37dd0242014-10-03 15:48:09 -0700120 struct guehdr *guehdr;
Tom Herbert5024c332014-11-04 09:06:53 -0800121 void *data;
Tom Herberta8d31c12014-11-04 09:06:57 -0800122 u16 doffset = 0;
Tom Herbert37dd0242014-10-03 15:48:09 -0700123
124 if (!fou)
125 return 1;
126
127 len = sizeof(struct udphdr) + sizeof(struct guehdr);
128 if (!pskb_may_pull(skb, len))
129 goto drop;
130
Tom Herbert5024c332014-11-04 09:06:53 -0800131 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
Tom Herbert37dd0242014-10-03 15:48:09 -0700132
Tom Herbertc1e48af2016-06-06 16:06:02 -0700133 switch (guehdr->version) {
134 case 0: /* Full GUE header present */
135 break;
136
137 case 1: {
138 /* Direct encasulation of IPv4 or IPv6 */
139
140 int prot;
141
142 switch (((struct iphdr *)guehdr)->version) {
143 case 4:
144 prot = IPPROTO_IPIP;
145 break;
146 case 6:
147 prot = IPPROTO_IPV6;
148 break;
149 default:
150 goto drop;
151 }
152
153 if (fou_recv_pull(skb, fou, sizeof(struct udphdr)))
154 goto drop;
155
156 return -prot;
157 }
158
159 default: /* Undefined version */
160 goto drop;
161 }
162
Tom Herbert5024c332014-11-04 09:06:53 -0800163 optlen = guehdr->hlen << 2;
164 len += optlen;
165
Tom Herbert37dd0242014-10-03 15:48:09 -0700166 if (!pskb_may_pull(skb, len))
167 goto drop;
168
Tom Herbert5024c332014-11-04 09:06:53 -0800169 /* guehdr may change after pull */
170 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
Li RongQingd8f00d22014-10-17 16:53:47 +0800171
Tom Herbert5024c332014-11-04 09:06:53 -0800172 hdrlen = sizeof(struct guehdr) + optlen;
173
174 if (guehdr->version != 0 || validate_gue_flags(guehdr, optlen))
Tom Herbert37dd0242014-10-03 15:48:09 -0700175 goto drop;
176
Tom Herberta8d31c12014-11-04 09:06:57 -0800177 hdrlen = sizeof(struct guehdr) + optlen;
178
Tom Herbert5f914b62016-05-18 09:06:16 -0700179 if (fou->family == AF_INET)
180 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(skb)->tot_len) - len);
181 else
182 ipv6_hdr(skb)->payload_len =
183 htons(ntohs(ipv6_hdr(skb)->payload_len) - len);
Tom Herberta8d31c12014-11-04 09:06:57 -0800184
Tom Herberta8d31c12014-11-04 09:06:57 -0800185 /* Pull csum through the guehdr now . This can be used if
186 * there is a remote checksum offload.
187 */
188 skb_postpull_rcsum(skb, udp_hdr(skb), len);
Tom Herbert5024c332014-11-04 09:06:53 -0800189
190 data = &guehdr[1];
191
192 if (guehdr->flags & GUE_FLAG_PRIV) {
Tom Herberta8d31c12014-11-04 09:06:57 -0800193 __be32 flags = *(__be32 *)(data + doffset);
Tom Herbert5024c332014-11-04 09:06:53 -0800194
Tom Herberta8d31c12014-11-04 09:06:57 -0800195 doffset += GUE_LEN_PRIV;
196
197 if (flags & GUE_PFLAG_REMCSUM) {
198 guehdr = gue_remcsum(skb, guehdr, data + doffset,
Tom Herbertfe881ef2015-02-10 16:30:33 -0800199 hdrlen, guehdr->proto_ctype,
200 !!(fou->flags &
201 FOU_F_REMCSUM_NOPARTIAL));
Tom Herberta8d31c12014-11-04 09:06:57 -0800202 if (!guehdr)
203 goto drop;
204
205 data = &guehdr[1];
206
207 doffset += GUE_PLEN_REMCSUM;
208 }
Tom Herbert37dd0242014-10-03 15:48:09 -0700209 }
210
Tom Herbert5024c332014-11-04 09:06:53 -0800211 if (unlikely(guehdr->control))
212 return gue_control_message(skb, guehdr);
213
Tom Herbert4fd671d2014-11-25 11:21:20 -0800214 __skb_pull(skb, sizeof(struct udphdr) + hdrlen);
Tom Herberta8d31c12014-11-04 09:06:57 -0800215 skb_reset_transport_header(skb);
216
Jesse Grossa09a4c82016-03-19 09:32:02 -0700217 if (iptunnel_pull_offloads(skb))
218 goto drop;
219
Tom Herbert5024c332014-11-04 09:06:53 -0800220 return -guehdr->proto_ctype;
221
Tom Herbert37dd0242014-10-03 15:48:09 -0700222drop:
223 kfree_skb(skb);
224 return 0;
225}
226
Tom Herbertd92283e2016-04-05 08:22:54 -0700227static struct sk_buff **fou_gro_receive(struct sock *sk,
228 struct sk_buff **head,
229 struct sk_buff *skb)
Tom Herbertafe93322014-09-17 12:25:57 -0700230{
231 const struct net_offload *ops;
232 struct sk_buff **pp = NULL;
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;
Tom Herbertafe93322014-09-17 12:25:57 -0700235
Alexander Duyckc3483382016-03-29 14:55:22 -0700236 /* We can clear the encap_mark for FOU as we are essentially doing
237 * one of two possible things. We are either adding an L4 tunnel
238 * header to the outer L3 tunnel header, or we are are simply
239 * treating the GRE tunnel header as though it is a UDP protocol
240 * specific header such as VXLAN or GENEVE.
241 */
242 NAPI_GRO_CB(skb)->encap_mark = 0;
243
Alexander Duycka0ca1532016-04-05 09:13:39 -0700244 /* Flag this frame as already having an outer encap header */
245 NAPI_GRO_CB(skb)->is_fou = 1;
246
Tom Herbertafe93322014-09-17 12:25:57 -0700247 rcu_read_lock();
Tom Herbertefc98d02014-10-03 15:48:08 -0700248 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
Tom Herbertafe93322014-09-17 12:25:57 -0700249 ops = rcu_dereference(offloads[proto]);
250 if (!ops || !ops->callbacks.gro_receive)
251 goto out_unlock;
252
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200253 pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
Tom Herbertafe93322014-09-17 12:25:57 -0700254
255out_unlock:
256 rcu_read_unlock();
257
258 return pp;
259}
260
Tom Herbertd92283e2016-04-05 08:22:54 -0700261static int fou_gro_complete(struct sock *sk, struct sk_buff *skb,
262 int nhoff)
Tom Herbertafe93322014-09-17 12:25:57 -0700263{
264 const struct net_offload *ops;
Tom Herbertd92283e2016-04-05 08:22:54 -0700265 u8 proto = fou_from_sock(sk)->protocol;
Tom Herbertafe93322014-09-17 12:25:57 -0700266 int err = -ENOSYS;
Tom Herbertefc98d02014-10-03 15:48:08 -0700267 const struct net_offload **offloads;
Tom Herbertafe93322014-09-17 12:25:57 -0700268
269 rcu_read_lock();
Tom Herbertefc98d02014-10-03 15:48:08 -0700270 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
Tom Herbertafe93322014-09-17 12:25:57 -0700271 ops = rcu_dereference(offloads[proto]);
272 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
273 goto out_unlock;
274
275 err = ops->callbacks.gro_complete(skb, nhoff);
276
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700277 skb_set_inner_mac_header(skb, nhoff);
278
Tom Herbertafe93322014-09-17 12:25:57 -0700279out_unlock:
280 rcu_read_unlock();
281
282 return err;
283}
284
Tom Herberta8d31c12014-11-04 09:06:57 -0800285static struct guehdr *gue_gro_remcsum(struct sk_buff *skb, unsigned int off,
286 struct guehdr *guehdr, void *data,
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700287 size_t hdrlen, struct gro_remcsum *grc,
288 bool nopartial)
Tom Herberta8d31c12014-11-04 09:06:57 -0800289{
290 __be16 *pd = data;
Tom Herbert4fd671d2014-11-25 11:21:20 -0800291 size_t start = ntohs(pd[0]);
292 size_t offset = ntohs(pd[1]);
Tom Herberta8d31c12014-11-04 09:06:57 -0800293
294 if (skb->remcsum_offload)
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700295 return guehdr;
Tom Herberta8d31c12014-11-04 09:06:57 -0800296
Tom Herbert4fd671d2014-11-25 11:21:20 -0800297 if (!NAPI_GRO_CB(skb)->csum_valid)
Tom Herberta8d31c12014-11-04 09:06:57 -0800298 return NULL;
299
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700300 guehdr = skb_gro_remcsum_process(skb, (void *)guehdr, off, hdrlen,
301 start, offset, grc, nopartial);
Tom Herberta8d31c12014-11-04 09:06:57 -0800302
303 skb->remcsum_offload = 1;
304
305 return guehdr;
306}
307
Tom Herbertd92283e2016-04-05 08:22:54 -0700308static struct sk_buff **gue_gro_receive(struct sock *sk,
309 struct sk_buff **head,
310 struct sk_buff *skb)
Tom Herbert37dd0242014-10-03 15:48:09 -0700311{
312 const struct net_offload **offloads;
313 const struct net_offload *ops;
314 struct sk_buff **pp = NULL;
315 struct sk_buff *p;
Tom Herbert37dd0242014-10-03 15:48:09 -0700316 struct guehdr *guehdr;
Tom Herbert5024c332014-11-04 09:06:53 -0800317 size_t len, optlen, hdrlen, off;
318 void *data;
Tom Herberta8d31c12014-11-04 09:06:57 -0800319 u16 doffset = 0;
Tom Herbert37dd0242014-10-03 15:48:09 -0700320 int flush = 1;
Tom Herbertd92283e2016-04-05 08:22:54 -0700321 struct fou *fou = fou_from_sock(sk);
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800322 struct gro_remcsum grc;
Tom Herbertc1e48af2016-06-06 16:06:02 -0700323 u8 proto;
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800324
325 skb_gro_remcsum_init(&grc);
Tom Herbert37dd0242014-10-03 15:48:09 -0700326
327 off = skb_gro_offset(skb);
Tom Herbert5024c332014-11-04 09:06:53 -0800328 len = off + sizeof(*guehdr);
329
Tom Herbert37dd0242014-10-03 15:48:09 -0700330 guehdr = skb_gro_header_fast(skb, off);
Tom Herbert5024c332014-11-04 09:06:53 -0800331 if (skb_gro_header_hard(skb, len)) {
332 guehdr = skb_gro_header_slow(skb, len, off);
Tom Herbert37dd0242014-10-03 15:48:09 -0700333 if (unlikely(!guehdr))
334 goto out;
335 }
336
Tom Herbertc1e48af2016-06-06 16:06:02 -0700337 switch (guehdr->version) {
338 case 0:
339 break;
340 case 1:
341 switch (((struct iphdr *)guehdr)->version) {
342 case 4:
343 proto = IPPROTO_IPIP;
344 break;
345 case 6:
346 proto = IPPROTO_IPV6;
347 break;
348 default:
349 goto out;
350 }
351 goto next_proto;
352 default:
353 goto out;
354 }
355
Tom Herbert5024c332014-11-04 09:06:53 -0800356 optlen = guehdr->hlen << 2;
357 len += optlen;
Tom Herbert37dd0242014-10-03 15:48:09 -0700358
Tom Herbert5024c332014-11-04 09:06:53 -0800359 if (skb_gro_header_hard(skb, len)) {
360 guehdr = skb_gro_header_slow(skb, len, off);
Tom Herbert37dd0242014-10-03 15:48:09 -0700361 if (unlikely(!guehdr))
Tom Herbert5024c332014-11-04 09:06:53 -0800362 goto out;
363 }
364
365 if (unlikely(guehdr->control) || guehdr->version != 0 ||
366 validate_gue_flags(guehdr, optlen))
367 goto out;
368
369 hdrlen = sizeof(*guehdr) + optlen;
370
Tom Herberta8d31c12014-11-04 09:06:57 -0800371 /* Adjust NAPI_GRO_CB(skb)->csum to account for guehdr,
372 * this is needed if there is a remote checkcsum offload.
373 */
Tom Herbert5024c332014-11-04 09:06:53 -0800374 skb_gro_postpull_rcsum(skb, guehdr, hdrlen);
375
376 data = &guehdr[1];
377
378 if (guehdr->flags & GUE_FLAG_PRIV) {
Tom Herberta8d31c12014-11-04 09:06:57 -0800379 __be32 flags = *(__be32 *)(data + doffset);
Tom Herbert5024c332014-11-04 09:06:53 -0800380
Tom Herberta8d31c12014-11-04 09:06:57 -0800381 doffset += GUE_LEN_PRIV;
382
383 if (flags & GUE_PFLAG_REMCSUM) {
384 guehdr = gue_gro_remcsum(skb, off, guehdr,
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700385 data + doffset, hdrlen, &grc,
Tom Herbertfe881ef2015-02-10 16:30:33 -0800386 !!(fou->flags &
387 FOU_F_REMCSUM_NOPARTIAL));
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700388
Tom Herberta8d31c12014-11-04 09:06:57 -0800389 if (!guehdr)
390 goto out;
391
392 data = &guehdr[1];
393
394 doffset += GUE_PLEN_REMCSUM;
395 }
Tom Herbert37dd0242014-10-03 15:48:09 -0700396 }
397
Tom Herberta8d31c12014-11-04 09:06:57 -0800398 skb_gro_pull(skb, hdrlen);
399
Tom Herbert37dd0242014-10-03 15:48:09 -0700400 for (p = *head; p; p = p->next) {
401 const struct guehdr *guehdr2;
402
403 if (!NAPI_GRO_CB(p)->same_flow)
404 continue;
405
406 guehdr2 = (struct guehdr *)(p->data + off);
407
408 /* Compare base GUE header to be equal (covers
Tom Herbert5024c332014-11-04 09:06:53 -0800409 * hlen, version, proto_ctype, and flags.
Tom Herbert37dd0242014-10-03 15:48:09 -0700410 */
411 if (guehdr->word != guehdr2->word) {
412 NAPI_GRO_CB(p)->same_flow = 0;
413 continue;
414 }
415
416 /* Compare optional fields are the same. */
417 if (guehdr->hlen && memcmp(&guehdr[1], &guehdr2[1],
418 guehdr->hlen << 2)) {
419 NAPI_GRO_CB(p)->same_flow = 0;
420 continue;
421 }
422 }
423
Tom Herbertc1e48af2016-06-06 16:06:02 -0700424 proto = guehdr->proto_ctype;
425
426next_proto:
427
Alexander Duyckc3483382016-03-29 14:55:22 -0700428 /* We can clear the encap_mark for GUE as we are essentially doing
429 * one of two possible things. We are either adding an L4 tunnel
430 * header to the outer L3 tunnel header, or we are are simply
431 * treating the GRE tunnel header as though it is a UDP protocol
432 * specific header such as VXLAN or GENEVE.
433 */
434 NAPI_GRO_CB(skb)->encap_mark = 0;
435
Alexander Duycka0ca1532016-04-05 09:13:39 -0700436 /* Flag this frame as already having an outer encap header */
437 NAPI_GRO_CB(skb)->is_fou = 1;
438
Tom Herbert5024c332014-11-04 09:06:53 -0800439 rcu_read_lock();
440 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
Tom Herbertc1e48af2016-06-06 16:06:02 -0700441 ops = rcu_dereference(offloads[proto]);
Tom Herbert27013662015-08-19 17:07:34 -0700442 if (WARN_ON_ONCE(!ops || !ops->callbacks.gro_receive))
Tom Herbert5024c332014-11-04 09:06:53 -0800443 goto out_unlock;
Tom Herbert37dd0242014-10-03 15:48:09 -0700444
Sabrina Dubrocafcd91dd2016-10-20 15:58:02 +0200445 pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800446 flush = 0;
Tom Herbert37dd0242014-10-03 15:48:09 -0700447
448out_unlock:
449 rcu_read_unlock();
450out:
451 NAPI_GRO_CB(skb)->flush |= flush;
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800452 skb_gro_remcsum_cleanup(skb, &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 Herbert23461552014-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 Herbert23461552014-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 Herbert23461552014-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 Herbert23461552014-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 Herbert23461552014-09-17 12:25:56 -0700525
Tom Herbert23461552014-09-17 12:25:56 -0700526 list_del(&fou->list);
WANG Cong02d793c2015-04-10 12:00:29 -0700527 udp_tunnel_sock_release(sock);
Tom Herbert23461552014-09-17 12:25:56 -0700528
Hannes Frederic Sowa3036fac2015-12-15 21:01:53 +0100529 kfree_rcu(fou, rcu);
Tom Herbert23461552014-09-17 12:25:56 -0700530}
531
532static int fou_create(struct net *net, struct fou_cfg *cfg,
533 struct socket **sockp)
534{
Tom Herbert23461552014-09-17 12:25:56 -0700535 struct socket *sock = NULL;
WANG Cong02d793c2015-04-10 12:00:29 -0700536 struct fou *fou = NULL;
Tom Herbert23461552014-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 Herbert23461552014-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 Herbert23461552014-09-17 12:25:56 -0700583
Tom Herbert440924b2016-05-18 09:06:14 -0700584 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Tom Herbert23461552014-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 Herbert23461552014-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 Herbert23461552014-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 Herbert23461552014-09-17 12:25:56 -0700610 int err = -EINVAL;
WANG Cong02d793c2015-04-10 12:00:29 -0700611 struct fou *fou;
Tom Herbert23461552014-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 Herbert23461552014-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 Herbert23461552014-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 Herbert23461552014-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 Herbert23461552014-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 Herbert23461552014-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 Herbert23461552014-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 Herbert23461552014-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 Herbert23461552014-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 Herbert23461552014-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 Herbert23461552014-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 Herbert23461552014-09-17 12:25:56 -0700693 struct fou_cfg cfg;
WANG Cong67270632015-04-10 12:00:27 -0700694 int err;
Tom Herbert23461552014-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 Herbert23461552014-09-17 12:25:56 -0700699
WANG Cong02d793c2015-04-10 12:00:29 -0700700 return fou_destroy(net, &cfg);
Tom Herbert23461552014-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 Herbert23461552014-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 Herbert23461552014-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
Tom Herberta8c5f902014-11-12 11:54:09 -08001007
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");