]>
Commit | Line | Data |
---|---|---|
23461551 TH |
1 | #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> | |
37dd0247 | 10 | #include <net/gue.h> |
23461551 | 11 | #include <net/ip.h> |
afe93325 | 12 | #include <net/protocol.h> |
23461551 TH |
13 | #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 | ||
19 | static DEFINE_SPINLOCK(fou_lock); | |
20 | static LIST_HEAD(fou_list); | |
21 | ||
22 | struct fou { | |
23 | struct socket *sock; | |
24 | u8 protocol; | |
25 | u16 port; | |
afe93325 | 26 | struct udp_offload udp_offloads; |
23461551 TH |
27 | struct list_head list; |
28 | }; | |
29 | ||
30 | struct fou_cfg { | |
37dd0247 | 31 | u16 type; |
23461551 TH |
32 | u8 protocol; |
33 | struct udp_port_cfg udp_config; | |
34 | }; | |
35 | ||
36 | static inline struct fou *fou_from_sock(struct sock *sk) | |
37 | { | |
38 | return sk->sk_user_data; | |
39 | } | |
40 | ||
5024c33a | 41 | static void fou_recv_pull(struct sk_buff *skb, size_t len) |
23461551 TH |
42 | { |
43 | struct iphdr *iph = ip_hdr(skb); | |
44 | ||
45 | /* Remove 'len' bytes from the packet (UDP header and | |
5024c33a | 46 | * FOU header if present). |
23461551 TH |
47 | */ |
48 | iph->tot_len = htons(ntohs(iph->tot_len) - len); | |
49 | __skb_pull(skb, len); | |
50 | skb_postpull_rcsum(skb, udp_hdr(skb), len); | |
51 | skb_reset_transport_header(skb); | |
23461551 TH |
52 | } |
53 | ||
54 | static int fou_udp_recv(struct sock *sk, struct sk_buff *skb) | |
55 | { | |
56 | struct fou *fou = fou_from_sock(sk); | |
57 | ||
58 | if (!fou) | |
59 | return 1; | |
60 | ||
5024c33a TH |
61 | fou_recv_pull(skb, sizeof(struct udphdr)); |
62 | ||
63 | return -fou->protocol; | |
64 | } | |
65 | ||
a8d31c12 | 66 | static struct guehdr *gue_remcsum(struct sk_buff *skb, struct guehdr *guehdr, |
4fd671de | 67 | void *data, size_t hdrlen, u8 ipproto) |
a8d31c12 TH |
68 | { |
69 | __be16 *pd = data; | |
4fd671de TH |
70 | size_t start = ntohs(pd[0]); |
71 | size_t offset = ntohs(pd[1]); | |
72 | size_t plen = hdrlen + max_t(size_t, offset + sizeof(u16), start); | |
a8d31c12 | 73 | |
a8d31c12 TH |
74 | if (!pskb_may_pull(skb, plen)) |
75 | return NULL; | |
76 | guehdr = (struct guehdr *)&udp_hdr(skb)[1]; | |
77 | ||
dcdc8994 | 78 | skb_remcsum_process(skb, (void *)guehdr + hdrlen, start, offset); |
a8d31c12 TH |
79 | |
80 | return guehdr; | |
81 | } | |
82 | ||
5024c33a TH |
83 | static int gue_control_message(struct sk_buff *skb, struct guehdr *guehdr) |
84 | { | |
85 | /* No support yet */ | |
86 | kfree_skb(skb); | |
87 | return 0; | |
23461551 TH |
88 | } |
89 | ||
37dd0247 TH |
90 | static int gue_udp_recv(struct sock *sk, struct sk_buff *skb) |
91 | { | |
92 | struct fou *fou = fou_from_sock(sk); | |
5024c33a | 93 | size_t len, optlen, hdrlen; |
37dd0247 | 94 | struct guehdr *guehdr; |
5024c33a | 95 | void *data; |
a8d31c12 | 96 | u16 doffset = 0; |
37dd0247 TH |
97 | |
98 | if (!fou) | |
99 | return 1; | |
100 | ||
101 | len = sizeof(struct udphdr) + sizeof(struct guehdr); | |
102 | if (!pskb_may_pull(skb, len)) | |
103 | goto drop; | |
104 | ||
5024c33a TH |
105 | guehdr = (struct guehdr *)&udp_hdr(skb)[1]; |
106 | ||
107 | optlen = guehdr->hlen << 2; | |
108 | len += optlen; | |
37dd0247 | 109 | |
37dd0247 TH |
110 | if (!pskb_may_pull(skb, len)) |
111 | goto drop; | |
112 | ||
5024c33a TH |
113 | /* guehdr may change after pull */ |
114 | guehdr = (struct guehdr *)&udp_hdr(skb)[1]; | |
d8f00d27 | 115 | |
5024c33a | 116 | hdrlen = sizeof(struct guehdr) + optlen; |
37dd0247 | 117 | |
5024c33a | 118 | if (guehdr->version != 0 || validate_gue_flags(guehdr, optlen)) |
37dd0247 | 119 | goto drop; |
5024c33a | 120 | |
a8d31c12 TH |
121 | hdrlen = sizeof(struct guehdr) + optlen; |
122 | ||
123 | ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(skb)->tot_len) - len); | |
124 | ||
a8d31c12 TH |
125 | /* Pull csum through the guehdr now . This can be used if |
126 | * there is a remote checksum offload. | |
127 | */ | |
128 | skb_postpull_rcsum(skb, udp_hdr(skb), len); | |
5024c33a TH |
129 | |
130 | data = &guehdr[1]; | |
131 | ||
132 | if (guehdr->flags & GUE_FLAG_PRIV) { | |
a8d31c12 TH |
133 | __be32 flags = *(__be32 *)(data + doffset); |
134 | ||
135 | doffset += GUE_LEN_PRIV; | |
136 | ||
137 | if (flags & GUE_PFLAG_REMCSUM) { | |
138 | guehdr = gue_remcsum(skb, guehdr, data + doffset, | |
139 | hdrlen, guehdr->proto_ctype); | |
140 | if (!guehdr) | |
141 | goto drop; | |
142 | ||
143 | data = &guehdr[1]; | |
5024c33a | 144 | |
a8d31c12 TH |
145 | doffset += GUE_PLEN_REMCSUM; |
146 | } | |
37dd0247 TH |
147 | } |
148 | ||
5024c33a TH |
149 | if (unlikely(guehdr->control)) |
150 | return gue_control_message(skb, guehdr); | |
151 | ||
4fd671de | 152 | __skb_pull(skb, sizeof(struct udphdr) + hdrlen); |
a8d31c12 TH |
153 | skb_reset_transport_header(skb); |
154 | ||
5024c33a TH |
155 | return -guehdr->proto_ctype; |
156 | ||
37dd0247 TH |
157 | drop: |
158 | kfree_skb(skb); | |
159 | return 0; | |
160 | } | |
161 | ||
afe93325 | 162 | static struct sk_buff **fou_gro_receive(struct sk_buff **head, |
a2b12f3c TH |
163 | struct sk_buff *skb, |
164 | struct udp_offload *uoff) | |
afe93325 TH |
165 | { |
166 | const struct net_offload *ops; | |
167 | struct sk_buff **pp = NULL; | |
168 | u8 proto = NAPI_GRO_CB(skb)->proto; | |
efc98d08 | 169 | const struct net_offload **offloads; |
afe93325 TH |
170 | |
171 | rcu_read_lock(); | |
efc98d08 | 172 | offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads; |
afe93325 TH |
173 | ops = rcu_dereference(offloads[proto]); |
174 | if (!ops || !ops->callbacks.gro_receive) | |
175 | goto out_unlock; | |
176 | ||
177 | pp = ops->callbacks.gro_receive(head, skb); | |
178 | ||
179 | out_unlock: | |
180 | rcu_read_unlock(); | |
181 | ||
182 | return pp; | |
183 | } | |
184 | ||
a2b12f3c TH |
185 | static int fou_gro_complete(struct sk_buff *skb, int nhoff, |
186 | struct udp_offload *uoff) | |
afe93325 TH |
187 | { |
188 | const struct net_offload *ops; | |
189 | u8 proto = NAPI_GRO_CB(skb)->proto; | |
190 | int err = -ENOSYS; | |
efc98d08 | 191 | const struct net_offload **offloads; |
afe93325 | 192 | |
cfdf1e1b JG |
193 | udp_tunnel_gro_complete(skb, nhoff); |
194 | ||
afe93325 | 195 | rcu_read_lock(); |
efc98d08 | 196 | offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads; |
afe93325 TH |
197 | ops = rcu_dereference(offloads[proto]); |
198 | if (WARN_ON(!ops || !ops->callbacks.gro_complete)) | |
199 | goto out_unlock; | |
200 | ||
201 | err = ops->callbacks.gro_complete(skb, nhoff); | |
202 | ||
203 | out_unlock: | |
204 | rcu_read_unlock(); | |
205 | ||
206 | return err; | |
207 | } | |
208 | ||
a8d31c12 TH |
209 | static struct guehdr *gue_gro_remcsum(struct sk_buff *skb, unsigned int off, |
210 | struct guehdr *guehdr, void *data, | |
26c4f7da TH |
211 | size_t hdrlen, u8 ipproto, |
212 | struct gro_remcsum *grc) | |
a8d31c12 TH |
213 | { |
214 | __be16 *pd = data; | |
4fd671de TH |
215 | size_t start = ntohs(pd[0]); |
216 | size_t offset = ntohs(pd[1]); | |
217 | size_t plen = hdrlen + max_t(size_t, offset + sizeof(u16), start); | |
a8d31c12 TH |
218 | |
219 | if (skb->remcsum_offload) | |
26c4f7da | 220 | return NULL; |
a8d31c12 | 221 | |
4fd671de | 222 | if (!NAPI_GRO_CB(skb)->csum_valid) |
a8d31c12 TH |
223 | return NULL; |
224 | ||
a8d31c12 TH |
225 | /* Pull checksum that will be written */ |
226 | if (skb_gro_header_hard(skb, off + plen)) { | |
227 | guehdr = skb_gro_header_slow(skb, off + plen, off); | |
228 | if (!guehdr) | |
229 | return NULL; | |
230 | } | |
231 | ||
26c4f7da TH |
232 | skb_gro_remcsum_process(skb, (void *)guehdr + hdrlen, |
233 | start, offset, grc); | |
a8d31c12 TH |
234 | |
235 | skb->remcsum_offload = 1; | |
236 | ||
237 | return guehdr; | |
238 | } | |
239 | ||
37dd0247 | 240 | static struct sk_buff **gue_gro_receive(struct sk_buff **head, |
a2b12f3c TH |
241 | struct sk_buff *skb, |
242 | struct udp_offload *uoff) | |
37dd0247 TH |
243 | { |
244 | const struct net_offload **offloads; | |
245 | const struct net_offload *ops; | |
246 | struct sk_buff **pp = NULL; | |
247 | struct sk_buff *p; | |
37dd0247 | 248 | struct guehdr *guehdr; |
5024c33a TH |
249 | size_t len, optlen, hdrlen, off; |
250 | void *data; | |
a8d31c12 | 251 | u16 doffset = 0; |
37dd0247 | 252 | int flush = 1; |
26c4f7da TH |
253 | struct gro_remcsum grc; |
254 | ||
255 | skb_gro_remcsum_init(&grc); | |
37dd0247 TH |
256 | |
257 | off = skb_gro_offset(skb); | |
5024c33a TH |
258 | len = off + sizeof(*guehdr); |
259 | ||
37dd0247 | 260 | guehdr = skb_gro_header_fast(skb, off); |
5024c33a TH |
261 | if (skb_gro_header_hard(skb, len)) { |
262 | guehdr = skb_gro_header_slow(skb, len, off); | |
37dd0247 TH |
263 | if (unlikely(!guehdr)) |
264 | goto out; | |
265 | } | |
266 | ||
5024c33a TH |
267 | optlen = guehdr->hlen << 2; |
268 | len += optlen; | |
37dd0247 | 269 | |
5024c33a TH |
270 | if (skb_gro_header_hard(skb, len)) { |
271 | guehdr = skb_gro_header_slow(skb, len, off); | |
272 | if (unlikely(!guehdr)) | |
273 | goto out; | |
274 | } | |
37dd0247 | 275 | |
5024c33a TH |
276 | if (unlikely(guehdr->control) || guehdr->version != 0 || |
277 | validate_gue_flags(guehdr, optlen)) | |
278 | goto out; | |
37dd0247 | 279 | |
5024c33a TH |
280 | hdrlen = sizeof(*guehdr) + optlen; |
281 | ||
a8d31c12 TH |
282 | /* Adjust NAPI_GRO_CB(skb)->csum to account for guehdr, |
283 | * this is needed if there is a remote checkcsum offload. | |
284 | */ | |
5024c33a TH |
285 | skb_gro_postpull_rcsum(skb, guehdr, hdrlen); |
286 | ||
287 | data = &guehdr[1]; | |
288 | ||
289 | if (guehdr->flags & GUE_FLAG_PRIV) { | |
a8d31c12 | 290 | __be32 flags = *(__be32 *)(data + doffset); |
5024c33a | 291 | |
a8d31c12 TH |
292 | doffset += GUE_LEN_PRIV; |
293 | ||
294 | if (flags & GUE_PFLAG_REMCSUM) { | |
295 | guehdr = gue_gro_remcsum(skb, off, guehdr, | |
296 | data + doffset, hdrlen, | |
26c4f7da | 297 | guehdr->proto_ctype, &grc); |
a8d31c12 TH |
298 | if (!guehdr) |
299 | goto out; | |
300 | ||
301 | data = &guehdr[1]; | |
302 | ||
303 | doffset += GUE_PLEN_REMCSUM; | |
304 | } | |
37dd0247 TH |
305 | } |
306 | ||
a8d31c12 TH |
307 | skb_gro_pull(skb, hdrlen); |
308 | ||
37dd0247 TH |
309 | flush = 0; |
310 | ||
311 | for (p = *head; p; p = p->next) { | |
312 | const struct guehdr *guehdr2; | |
313 | ||
314 | if (!NAPI_GRO_CB(p)->same_flow) | |
315 | continue; | |
316 | ||
317 | guehdr2 = (struct guehdr *)(p->data + off); | |
318 | ||
319 | /* Compare base GUE header to be equal (covers | |
5024c33a | 320 | * hlen, version, proto_ctype, and flags. |
37dd0247 TH |
321 | */ |
322 | if (guehdr->word != guehdr2->word) { | |
323 | NAPI_GRO_CB(p)->same_flow = 0; | |
324 | continue; | |
325 | } | |
326 | ||
327 | /* Compare optional fields are the same. */ | |
328 | if (guehdr->hlen && memcmp(&guehdr[1], &guehdr2[1], | |
329 | guehdr->hlen << 2)) { | |
330 | NAPI_GRO_CB(p)->same_flow = 0; | |
331 | continue; | |
332 | } | |
333 | } | |
334 | ||
5024c33a TH |
335 | rcu_read_lock(); |
336 | offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads; | |
337 | ops = rcu_dereference(offloads[guehdr->proto_ctype]); | |
338 | if (WARN_ON(!ops || !ops->callbacks.gro_receive)) | |
339 | goto out_unlock; | |
37dd0247 TH |
340 | |
341 | pp = ops->callbacks.gro_receive(head, skb); | |
342 | ||
343 | out_unlock: | |
344 | rcu_read_unlock(); | |
345 | out: | |
346 | NAPI_GRO_CB(skb)->flush |= flush; | |
26c4f7da | 347 | skb_gro_remcsum_cleanup(skb, &grc); |
37dd0247 TH |
348 | |
349 | return pp; | |
350 | } | |
351 | ||
a2b12f3c TH |
352 | static int gue_gro_complete(struct sk_buff *skb, int nhoff, |
353 | struct udp_offload *uoff) | |
37dd0247 TH |
354 | { |
355 | const struct net_offload **offloads; | |
356 | struct guehdr *guehdr = (struct guehdr *)(skb->data + nhoff); | |
357 | const struct net_offload *ops; | |
358 | unsigned int guehlen; | |
359 | u8 proto; | |
360 | int err = -ENOENT; | |
361 | ||
5024c33a | 362 | proto = guehdr->proto_ctype; |
37dd0247 TH |
363 | |
364 | guehlen = sizeof(*guehdr) + (guehdr->hlen << 2); | |
365 | ||
366 | rcu_read_lock(); | |
367 | offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads; | |
368 | ops = rcu_dereference(offloads[proto]); | |
369 | if (WARN_ON(!ops || !ops->callbacks.gro_complete)) | |
370 | goto out_unlock; | |
371 | ||
372 | err = ops->callbacks.gro_complete(skb, nhoff + guehlen); | |
373 | ||
374 | out_unlock: | |
375 | rcu_read_unlock(); | |
376 | return err; | |
377 | } | |
378 | ||
23461551 TH |
379 | static int fou_add_to_port_list(struct fou *fou) |
380 | { | |
381 | struct fou *fout; | |
382 | ||
383 | spin_lock(&fou_lock); | |
384 | list_for_each_entry(fout, &fou_list, list) { | |
385 | if (fou->port == fout->port) { | |
386 | spin_unlock(&fou_lock); | |
387 | return -EALREADY; | |
388 | } | |
389 | } | |
390 | ||
391 | list_add(&fou->list, &fou_list); | |
392 | spin_unlock(&fou_lock); | |
393 | ||
394 | return 0; | |
395 | } | |
396 | ||
397 | static void fou_release(struct fou *fou) | |
398 | { | |
399 | struct socket *sock = fou->sock; | |
400 | struct sock *sk = sock->sk; | |
401 | ||
402 | udp_del_offload(&fou->udp_offloads); | |
403 | ||
404 | list_del(&fou->list); | |
405 | ||
406 | /* Remove hooks into tunnel socket */ | |
407 | sk->sk_user_data = NULL; | |
408 | ||
409 | sock_release(sock); | |
410 | ||
411 | kfree(fou); | |
412 | } | |
413 | ||
37dd0247 TH |
414 | static int fou_encap_init(struct sock *sk, struct fou *fou, struct fou_cfg *cfg) |
415 | { | |
416 | udp_sk(sk)->encap_rcv = fou_udp_recv; | |
417 | fou->protocol = cfg->protocol; | |
418 | fou->udp_offloads.callbacks.gro_receive = fou_gro_receive; | |
419 | fou->udp_offloads.callbacks.gro_complete = fou_gro_complete; | |
420 | fou->udp_offloads.port = cfg->udp_config.local_udp_port; | |
421 | fou->udp_offloads.ipproto = cfg->protocol; | |
422 | ||
423 | return 0; | |
424 | } | |
425 | ||
426 | static int gue_encap_init(struct sock *sk, struct fou *fou, struct fou_cfg *cfg) | |
427 | { | |
428 | udp_sk(sk)->encap_rcv = gue_udp_recv; | |
429 | fou->udp_offloads.callbacks.gro_receive = gue_gro_receive; | |
430 | fou->udp_offloads.callbacks.gro_complete = gue_gro_complete; | |
431 | fou->udp_offloads.port = cfg->udp_config.local_udp_port; | |
432 | ||
433 | return 0; | |
434 | } | |
435 | ||
23461551 TH |
436 | static int fou_create(struct net *net, struct fou_cfg *cfg, |
437 | struct socket **sockp) | |
438 | { | |
439 | struct fou *fou = NULL; | |
440 | int err; | |
441 | struct socket *sock = NULL; | |
442 | struct sock *sk; | |
443 | ||
444 | /* Open UDP socket */ | |
445 | err = udp_sock_create(net, &cfg->udp_config, &sock); | |
446 | if (err < 0) | |
447 | goto error; | |
448 | ||
449 | /* Allocate FOU port structure */ | |
450 | fou = kzalloc(sizeof(*fou), GFP_KERNEL); | |
451 | if (!fou) { | |
452 | err = -ENOMEM; | |
453 | goto error; | |
454 | } | |
455 | ||
456 | sk = sock->sk; | |
457 | ||
37dd0247 TH |
458 | fou->port = cfg->udp_config.local_udp_port; |
459 | ||
460 | /* Initial for fou type */ | |
461 | switch (cfg->type) { | |
462 | case FOU_ENCAP_DIRECT: | |
463 | err = fou_encap_init(sk, fou, cfg); | |
464 | if (err) | |
465 | goto error; | |
466 | break; | |
467 | case FOU_ENCAP_GUE: | |
468 | err = gue_encap_init(sk, fou, cfg); | |
469 | if (err) | |
470 | goto error; | |
471 | break; | |
472 | default: | |
473 | err = -EINVAL; | |
474 | goto error; | |
475 | } | |
23461551 TH |
476 | |
477 | udp_sk(sk)->encap_type = 1; | |
478 | udp_encap_enable(); | |
479 | ||
480 | sk->sk_user_data = fou; | |
481 | fou->sock = sock; | |
482 | ||
224d019c | 483 | inet_inc_convert_csum(sk); |
23461551 TH |
484 | |
485 | sk->sk_allocation = GFP_ATOMIC; | |
486 | ||
afe93325 TH |
487 | if (cfg->udp_config.family == AF_INET) { |
488 | err = udp_add_offload(&fou->udp_offloads); | |
489 | if (err) | |
490 | goto error; | |
491 | } | |
492 | ||
23461551 TH |
493 | err = fou_add_to_port_list(fou); |
494 | if (err) | |
495 | goto error; | |
496 | ||
497 | if (sockp) | |
498 | *sockp = sock; | |
499 | ||
500 | return 0; | |
501 | ||
502 | error: | |
503 | kfree(fou); | |
504 | if (sock) | |
505 | sock_release(sock); | |
506 | ||
507 | return err; | |
508 | } | |
509 | ||
510 | static int fou_destroy(struct net *net, struct fou_cfg *cfg) | |
511 | { | |
512 | struct fou *fou; | |
513 | u16 port = cfg->udp_config.local_udp_port; | |
514 | int err = -EINVAL; | |
515 | ||
516 | spin_lock(&fou_lock); | |
517 | list_for_each_entry(fou, &fou_list, list) { | |
518 | if (fou->port == port) { | |
afe93325 | 519 | udp_del_offload(&fou->udp_offloads); |
23461551 TH |
520 | fou_release(fou); |
521 | err = 0; | |
522 | break; | |
523 | } | |
524 | } | |
525 | spin_unlock(&fou_lock); | |
526 | ||
527 | return err; | |
528 | } | |
529 | ||
530 | static struct genl_family fou_nl_family = { | |
531 | .id = GENL_ID_GENERATE, | |
532 | .hdrsize = 0, | |
533 | .name = FOU_GENL_NAME, | |
534 | .version = FOU_GENL_VERSION, | |
535 | .maxattr = FOU_ATTR_MAX, | |
536 | .netnsok = true, | |
537 | }; | |
538 | ||
539 | static struct nla_policy fou_nl_policy[FOU_ATTR_MAX + 1] = { | |
540 | [FOU_ATTR_PORT] = { .type = NLA_U16, }, | |
541 | [FOU_ATTR_AF] = { .type = NLA_U8, }, | |
542 | [FOU_ATTR_IPPROTO] = { .type = NLA_U8, }, | |
37dd0247 | 543 | [FOU_ATTR_TYPE] = { .type = NLA_U8, }, |
23461551 TH |
544 | }; |
545 | ||
546 | static int parse_nl_config(struct genl_info *info, | |
547 | struct fou_cfg *cfg) | |
548 | { | |
549 | memset(cfg, 0, sizeof(*cfg)); | |
550 | ||
551 | cfg->udp_config.family = AF_INET; | |
552 | ||
553 | if (info->attrs[FOU_ATTR_AF]) { | |
554 | u8 family = nla_get_u8(info->attrs[FOU_ATTR_AF]); | |
555 | ||
556 | if (family != AF_INET && family != AF_INET6) | |
557 | return -EINVAL; | |
558 | ||
559 | cfg->udp_config.family = family; | |
560 | } | |
561 | ||
562 | if (info->attrs[FOU_ATTR_PORT]) { | |
563 | u16 port = nla_get_u16(info->attrs[FOU_ATTR_PORT]); | |
564 | ||
565 | cfg->udp_config.local_udp_port = port; | |
566 | } | |
567 | ||
568 | if (info->attrs[FOU_ATTR_IPPROTO]) | |
569 | cfg->protocol = nla_get_u8(info->attrs[FOU_ATTR_IPPROTO]); | |
570 | ||
37dd0247 TH |
571 | if (info->attrs[FOU_ATTR_TYPE]) |
572 | cfg->type = nla_get_u8(info->attrs[FOU_ATTR_TYPE]); | |
573 | ||
23461551 TH |
574 | return 0; |
575 | } | |
576 | ||
577 | static int fou_nl_cmd_add_port(struct sk_buff *skb, struct genl_info *info) | |
578 | { | |
579 | struct fou_cfg cfg; | |
580 | int err; | |
581 | ||
582 | err = parse_nl_config(info, &cfg); | |
583 | if (err) | |
584 | return err; | |
585 | ||
586 | return fou_create(&init_net, &cfg, NULL); | |
587 | } | |
588 | ||
589 | static int fou_nl_cmd_rm_port(struct sk_buff *skb, struct genl_info *info) | |
590 | { | |
591 | struct fou_cfg cfg; | |
592 | ||
593 | parse_nl_config(info, &cfg); | |
594 | ||
595 | return fou_destroy(&init_net, &cfg); | |
596 | } | |
597 | ||
598 | static const struct genl_ops fou_nl_ops[] = { | |
599 | { | |
600 | .cmd = FOU_CMD_ADD, | |
601 | .doit = fou_nl_cmd_add_port, | |
602 | .policy = fou_nl_policy, | |
603 | .flags = GENL_ADMIN_PERM, | |
604 | }, | |
605 | { | |
606 | .cmd = FOU_CMD_DEL, | |
607 | .doit = fou_nl_cmd_rm_port, | |
608 | .policy = fou_nl_policy, | |
609 | .flags = GENL_ADMIN_PERM, | |
610 | }, | |
611 | }; | |
612 | ||
a8c5f90f TH |
613 | size_t fou_encap_hlen(struct ip_tunnel_encap *e) |
614 | { | |
615 | return sizeof(struct udphdr); | |
616 | } | |
617 | EXPORT_SYMBOL(fou_encap_hlen); | |
618 | ||
619 | size_t gue_encap_hlen(struct ip_tunnel_encap *e) | |
620 | { | |
621 | size_t len; | |
622 | bool need_priv = false; | |
623 | ||
624 | len = sizeof(struct udphdr) + sizeof(struct guehdr); | |
625 | ||
626 | if (e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) { | |
627 | len += GUE_PLEN_REMCSUM; | |
628 | need_priv = true; | |
629 | } | |
630 | ||
631 | len += need_priv ? GUE_LEN_PRIV : 0; | |
632 | ||
633 | return len; | |
634 | } | |
635 | EXPORT_SYMBOL(gue_encap_hlen); | |
636 | ||
63487bab TH |
637 | static void fou_build_udp(struct sk_buff *skb, struct ip_tunnel_encap *e, |
638 | struct flowi4 *fl4, u8 *protocol, __be16 sport) | |
639 | { | |
640 | struct udphdr *uh; | |
641 | ||
642 | skb_push(skb, sizeof(struct udphdr)); | |
643 | skb_reset_transport_header(skb); | |
644 | ||
645 | uh = udp_hdr(skb); | |
646 | ||
647 | uh->dest = e->dport; | |
648 | uh->source = sport; | |
649 | uh->len = htons(skb->len); | |
650 | uh->check = 0; | |
651 | udp_set_csum(!(e->flags & TUNNEL_ENCAP_FLAG_CSUM), skb, | |
652 | fl4->saddr, fl4->daddr, skb->len); | |
653 | ||
654 | *protocol = IPPROTO_UDP; | |
655 | } | |
656 | ||
657 | int fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e, | |
658 | u8 *protocol, struct flowi4 *fl4) | |
659 | { | |
660 | bool csum = !!(e->flags & TUNNEL_ENCAP_FLAG_CSUM); | |
661 | int type = csum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL; | |
662 | __be16 sport; | |
663 | ||
664 | skb = iptunnel_handle_offloads(skb, csum, type); | |
665 | ||
666 | if (IS_ERR(skb)) | |
667 | return PTR_ERR(skb); | |
668 | ||
669 | sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev), | |
670 | skb, 0, 0, false); | |
671 | fou_build_udp(skb, e, fl4, protocol, sport); | |
672 | ||
673 | return 0; | |
674 | } | |
675 | EXPORT_SYMBOL(fou_build_header); | |
676 | ||
677 | int gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e, | |
678 | u8 *protocol, struct flowi4 *fl4) | |
679 | { | |
680 | bool csum = !!(e->flags & TUNNEL_ENCAP_FLAG_CSUM); | |
681 | int type = csum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL; | |
682 | struct guehdr *guehdr; | |
b17f709a | 683 | size_t hdrlen, optlen = 0; |
63487bab | 684 | __be16 sport; |
5024c33a TH |
685 | void *data; |
686 | bool need_priv = false; | |
687 | ||
b17f709a TH |
688 | if ((e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) && |
689 | skb->ip_summed == CHECKSUM_PARTIAL) { | |
690 | csum = false; | |
691 | optlen += GUE_PLEN_REMCSUM; | |
692 | type |= SKB_GSO_TUNNEL_REMCSUM; | |
693 | need_priv = true; | |
694 | } | |
695 | ||
5024c33a | 696 | optlen += need_priv ? GUE_LEN_PRIV : 0; |
63487bab TH |
697 | |
698 | skb = iptunnel_handle_offloads(skb, csum, type); | |
699 | ||
700 | if (IS_ERR(skb)) | |
701 | return PTR_ERR(skb); | |
702 | ||
703 | /* Get source port (based on flow hash) before skb_push */ | |
704 | sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev), | |
705 | skb, 0, 0, false); | |
706 | ||
b17f709a TH |
707 | hdrlen = sizeof(struct guehdr) + optlen; |
708 | ||
709 | skb_push(skb, hdrlen); | |
63487bab TH |
710 | |
711 | guehdr = (struct guehdr *)skb->data; | |
712 | ||
5024c33a | 713 | guehdr->control = 0; |
63487bab | 714 | guehdr->version = 0; |
5024c33a | 715 | guehdr->hlen = optlen >> 2; |
63487bab | 716 | guehdr->flags = 0; |
5024c33a TH |
717 | guehdr->proto_ctype = *protocol; |
718 | ||
719 | data = &guehdr[1]; | |
720 | ||
721 | if (need_priv) { | |
722 | __be32 *flags = data; | |
723 | ||
724 | guehdr->flags |= GUE_FLAG_PRIV; | |
725 | *flags = 0; | |
726 | data += GUE_LEN_PRIV; | |
727 | ||
b17f709a TH |
728 | if (type & SKB_GSO_TUNNEL_REMCSUM) { |
729 | u16 csum_start = skb_checksum_start_offset(skb); | |
730 | __be16 *pd = data; | |
731 | ||
732 | if (csum_start < hdrlen) | |
733 | return -EINVAL; | |
734 | ||
735 | csum_start -= hdrlen; | |
736 | pd[0] = htons(csum_start); | |
737 | pd[1] = htons(csum_start + skb->csum_offset); | |
738 | ||
739 | if (!skb_is_gso(skb)) { | |
740 | skb->ip_summed = CHECKSUM_NONE; | |
741 | skb->encapsulation = 0; | |
742 | } | |
743 | ||
744 | *flags |= GUE_PFLAG_REMCSUM; | |
745 | data += GUE_PLEN_REMCSUM; | |
746 | } | |
747 | ||
5024c33a | 748 | } |
63487bab TH |
749 | |
750 | fou_build_udp(skb, e, fl4, protocol, sport); | |
751 | ||
752 | return 0; | |
753 | } | |
754 | EXPORT_SYMBOL(gue_build_header); | |
755 | ||
a8c5f90f TH |
756 | #ifdef CONFIG_NET_FOU_IP_TUNNELS |
757 | ||
758 | static const struct ip_tunnel_encap_ops __read_mostly fou_iptun_ops = { | |
759 | .encap_hlen = fou_encap_hlen, | |
760 | .build_header = fou_build_header, | |
761 | }; | |
762 | ||
763 | static const struct ip_tunnel_encap_ops __read_mostly gue_iptun_ops = { | |
764 | .encap_hlen = gue_encap_hlen, | |
765 | .build_header = gue_build_header, | |
766 | }; | |
767 | ||
768 | static int ip_tunnel_encap_add_fou_ops(void) | |
769 | { | |
770 | int ret; | |
771 | ||
772 | ret = ip_tunnel_encap_add_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU); | |
773 | if (ret < 0) { | |
774 | pr_err("can't add fou ops\n"); | |
775 | return ret; | |
776 | } | |
777 | ||
778 | ret = ip_tunnel_encap_add_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE); | |
779 | if (ret < 0) { | |
780 | pr_err("can't add gue ops\n"); | |
781 | ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU); | |
782 | return ret; | |
783 | } | |
784 | ||
785 | return 0; | |
786 | } | |
787 | ||
788 | static void ip_tunnel_encap_del_fou_ops(void) | |
789 | { | |
790 | ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU); | |
791 | ip_tunnel_encap_del_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE); | |
792 | } | |
793 | ||
794 | #else | |
795 | ||
796 | static int ip_tunnel_encap_add_fou_ops(void) | |
797 | { | |
798 | return 0; | |
799 | } | |
800 | ||
882288c0 | 801 | static void ip_tunnel_encap_del_fou_ops(void) |
a8c5f90f TH |
802 | { |
803 | } | |
804 | ||
805 | #endif | |
806 | ||
23461551 TH |
807 | static int __init fou_init(void) |
808 | { | |
809 | int ret; | |
810 | ||
811 | ret = genl_register_family_with_ops(&fou_nl_family, | |
812 | fou_nl_ops); | |
813 | ||
a8c5f90f TH |
814 | if (ret < 0) |
815 | goto exit; | |
816 | ||
817 | ret = ip_tunnel_encap_add_fou_ops(); | |
818 | if (ret < 0) | |
819 | genl_unregister_family(&fou_nl_family); | |
820 | ||
821 | exit: | |
23461551 TH |
822 | return ret; |
823 | } | |
824 | ||
825 | static void __exit fou_fini(void) | |
826 | { | |
827 | struct fou *fou, *next; | |
828 | ||
a8c5f90f TH |
829 | ip_tunnel_encap_del_fou_ops(); |
830 | ||
23461551 TH |
831 | genl_unregister_family(&fou_nl_family); |
832 | ||
833 | /* Close all the FOU sockets */ | |
834 | ||
835 | spin_lock(&fou_lock); | |
836 | list_for_each_entry_safe(fou, next, &fou_list, list) | |
837 | fou_release(fou); | |
838 | spin_unlock(&fou_lock); | |
839 | } | |
840 | ||
841 | module_init(fou_init); | |
842 | module_exit(fou_fini); | |
843 | MODULE_AUTHOR("Tom Herbert <therbert@google.com>"); | |
844 | MODULE_LICENSE("GPL"); |