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