blob: 34968cd5c1464bf896ba1a2130fc223c50973499 [file] [log] [blame]
Tom Herbert23461552014-09-17 12:25:56 -07001#include <linux/module.h>
2#include <linux/errno.h>
3#include <linux/socket.h>
4#include <linux/skbuff.h>
5#include <linux/ip.h>
6#include <linux/udp.h>
7#include <linux/types.h>
8#include <linux/kernel.h>
9#include <net/genetlink.h>
Tom Herbert37dd0242014-10-03 15:48:09 -070010#include <net/gue.h>
Tom Herbert23461552014-09-17 12:25:56 -070011#include <net/ip.h>
Tom Herbertafe93322014-09-17 12:25:57 -070012#include <net/protocol.h>
Tom Herbert23461552014-09-17 12:25:56 -070013#include <net/udp.h>
14#include <net/udp_tunnel.h>
15#include <net/xfrm.h>
16#include <uapi/linux/fou.h>
17#include <uapi/linux/genetlink.h>
18
Tom Herbert23461552014-09-17 12:25:56 -070019struct fou {
20 struct socket *sock;
21 u8 protocol;
Tom Herbertfe881ef2015-02-10 16:30:33 -080022 u8 flags;
WANG Cong4cbcdf22015-04-10 12:00:28 -070023 __be16 port;
WANG Cong7a6c8c32015-04-10 12:00:30 -070024 u16 type;
Tom Herbertafe93322014-09-17 12:25:57 -070025 struct udp_offload udp_offloads;
Tom Herbert23461552014-09-17 12:25:56 -070026 struct list_head list;
27};
28
Tom Herbertfe881ef2015-02-10 16:30:33 -080029#define FOU_F_REMCSUM_NOPARTIAL BIT(0)
30
Tom Herbert23461552014-09-17 12:25:56 -070031struct fou_cfg {
Tom Herbert37dd0242014-10-03 15:48:09 -070032 u16 type;
Tom Herbert23461552014-09-17 12:25:56 -070033 u8 protocol;
Tom Herbertfe881ef2015-02-10 16:30:33 -080034 u8 flags;
Tom Herbert23461552014-09-17 12:25:56 -070035 struct udp_port_cfg udp_config;
36};
37
WANG Cong02d793c2015-04-10 12:00:29 -070038static unsigned int fou_net_id;
39
40struct fou_net {
41 struct list_head fou_list;
42 struct mutex fou_lock;
43};
44
Tom Herbert23461552014-09-17 12:25:56 -070045static inline struct fou *fou_from_sock(struct sock *sk)
46{
47 return sk->sk_user_data;
48}
49
Tom Herbert5024c332014-11-04 09:06:53 -080050static void fou_recv_pull(struct sk_buff *skb, size_t len)
Tom Herbert23461552014-09-17 12:25:56 -070051{
52 struct iphdr *iph = ip_hdr(skb);
53
54 /* 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 */
57 iph->tot_len = htons(ntohs(iph->tot_len) - len);
58 __skb_pull(skb, len);
59 skb_postpull_rcsum(skb, udp_hdr(skb), len);
60 skb_reset_transport_header(skb);
Tom Herbert23461552014-09-17 12:25:56 -070061}
62
63static int fou_udp_recv(struct sock *sk, struct sk_buff *skb)
64{
65 struct fou *fou = fou_from_sock(sk);
66
67 if (!fou)
68 return 1;
69
Tom Herbert5024c332014-11-04 09:06:53 -080070 fou_recv_pull(skb, sizeof(struct udphdr));
71
72 return -fou->protocol;
73}
74
Tom Herberta8d31c12014-11-04 09:06:57 -080075static struct guehdr *gue_remcsum(struct sk_buff *skb, struct guehdr *guehdr,
Tom Herbertfe881ef2015-02-10 16:30:33 -080076 void *data, size_t hdrlen, u8 ipproto,
77 bool nopartial)
Tom Herberta8d31c12014-11-04 09:06:57 -080078{
79 __be16 *pd = data;
Tom Herbert4fd671d2014-11-25 11:21:20 -080080 size_t start = ntohs(pd[0]);
81 size_t offset = ntohs(pd[1]);
82 size_t plen = hdrlen + max_t(size_t, offset + sizeof(u16), start);
Tom Herberta8d31c12014-11-04 09:06:57 -080083
Tom Herberta8d31c12014-11-04 09:06:57 -080084 if (!pskb_may_pull(skb, plen))
85 return NULL;
86 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
87
Tom Herbertfe881ef2015-02-10 16:30:33 -080088 skb_remcsum_process(skb, (void *)guehdr + hdrlen,
89 start, offset, nopartial);
Tom Herberta8d31c12014-11-04 09:06:57 -080090
91 return guehdr;
92}
93
Tom Herbert5024c332014-11-04 09:06:53 -080094static int gue_control_message(struct sk_buff *skb, struct guehdr *guehdr)
95{
96 /* No support yet */
97 kfree_skb(skb);
98 return 0;
Tom Herbert23461552014-09-17 12:25:56 -070099}
100
Tom Herbert37dd0242014-10-03 15:48:09 -0700101static int gue_udp_recv(struct sock *sk, struct sk_buff *skb)
102{
103 struct fou *fou = fou_from_sock(sk);
Tom Herbert5024c332014-11-04 09:06:53 -0800104 size_t len, optlen, hdrlen;
Tom Herbert37dd0242014-10-03 15:48:09 -0700105 struct guehdr *guehdr;
Tom Herbert5024c332014-11-04 09:06:53 -0800106 void *data;
Tom Herberta8d31c12014-11-04 09:06:57 -0800107 u16 doffset = 0;
Tom Herbert37dd0242014-10-03 15:48:09 -0700108
109 if (!fou)
110 return 1;
111
112 len = sizeof(struct udphdr) + sizeof(struct guehdr);
113 if (!pskb_may_pull(skb, len))
114 goto drop;
115
Tom Herbert5024c332014-11-04 09:06:53 -0800116 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
Tom Herbert37dd0242014-10-03 15:48:09 -0700117
Tom Herbert5024c332014-11-04 09:06:53 -0800118 optlen = guehdr->hlen << 2;
119 len += optlen;
120
Tom Herbert37dd0242014-10-03 15:48:09 -0700121 if (!pskb_may_pull(skb, len))
122 goto drop;
123
Tom Herbert5024c332014-11-04 09:06:53 -0800124 /* guehdr may change after pull */
125 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
Li RongQingd8f00d22014-10-17 16:53:47 +0800126
Tom Herbert5024c332014-11-04 09:06:53 -0800127 hdrlen = sizeof(struct guehdr) + optlen;
128
129 if (guehdr->version != 0 || validate_gue_flags(guehdr, optlen))
Tom Herbert37dd0242014-10-03 15:48:09 -0700130 goto drop;
131
Tom Herberta8d31c12014-11-04 09:06:57 -0800132 hdrlen = sizeof(struct guehdr) + optlen;
133
134 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(skb)->tot_len) - len);
135
Tom Herberta8d31c12014-11-04 09:06:57 -0800136 /* Pull csum through the guehdr now . This can be used if
137 * there is a remote checksum offload.
138 */
139 skb_postpull_rcsum(skb, udp_hdr(skb), len);
Tom Herbert5024c332014-11-04 09:06:53 -0800140
141 data = &guehdr[1];
142
143 if (guehdr->flags & GUE_FLAG_PRIV) {
Tom Herberta8d31c12014-11-04 09:06:57 -0800144 __be32 flags = *(__be32 *)(data + doffset);
Tom Herbert5024c332014-11-04 09:06:53 -0800145
Tom Herberta8d31c12014-11-04 09:06:57 -0800146 doffset += GUE_LEN_PRIV;
147
148 if (flags & GUE_PFLAG_REMCSUM) {
149 guehdr = gue_remcsum(skb, guehdr, data + doffset,
Tom Herbertfe881ef2015-02-10 16:30:33 -0800150 hdrlen, guehdr->proto_ctype,
151 !!(fou->flags &
152 FOU_F_REMCSUM_NOPARTIAL));
Tom Herberta8d31c12014-11-04 09:06:57 -0800153 if (!guehdr)
154 goto drop;
155
156 data = &guehdr[1];
157
158 doffset += GUE_PLEN_REMCSUM;
159 }
Tom Herbert37dd0242014-10-03 15:48:09 -0700160 }
161
Tom Herbert5024c332014-11-04 09:06:53 -0800162 if (unlikely(guehdr->control))
163 return gue_control_message(skb, guehdr);
164
Tom Herbert4fd671d2014-11-25 11:21:20 -0800165 __skb_pull(skb, sizeof(struct udphdr) + hdrlen);
Tom Herberta8d31c12014-11-04 09:06:57 -0800166 skb_reset_transport_header(skb);
167
Tom Herbert5024c332014-11-04 09:06:53 -0800168 return -guehdr->proto_ctype;
169
Tom Herbert37dd0242014-10-03 15:48:09 -0700170drop:
171 kfree_skb(skb);
172 return 0;
173}
174
Tom Herbertafe93322014-09-17 12:25:57 -0700175static struct sk_buff **fou_gro_receive(struct sk_buff **head,
Tom Herberta2b12f32015-01-12 17:00:37 -0800176 struct sk_buff *skb,
177 struct udp_offload *uoff)
Tom Herbertafe93322014-09-17 12:25:57 -0700178{
179 const struct net_offload *ops;
180 struct sk_buff **pp = NULL;
181 u8 proto = NAPI_GRO_CB(skb)->proto;
Tom Herbertefc98d02014-10-03 15:48:08 -0700182 const struct net_offload **offloads;
Tom Herbertafe93322014-09-17 12:25:57 -0700183
184 rcu_read_lock();
Tom Herbertefc98d02014-10-03 15:48:08 -0700185 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
Tom Herbertafe93322014-09-17 12:25:57 -0700186 ops = rcu_dereference(offloads[proto]);
187 if (!ops || !ops->callbacks.gro_receive)
188 goto out_unlock;
189
190 pp = ops->callbacks.gro_receive(head, skb);
191
192out_unlock:
193 rcu_read_unlock();
194
195 return pp;
196}
197
Tom Herberta2b12f32015-01-12 17:00:37 -0800198static int fou_gro_complete(struct sk_buff *skb, int nhoff,
199 struct udp_offload *uoff)
Tom Herbertafe93322014-09-17 12:25:57 -0700200{
201 const struct net_offload *ops;
202 u8 proto = NAPI_GRO_CB(skb)->proto;
203 int err = -ENOSYS;
Tom Herbertefc98d02014-10-03 15:48:08 -0700204 const struct net_offload **offloads;
Tom Herbertafe93322014-09-17 12:25:57 -0700205
Jesse Grosscfdf1e12014-11-10 11:45:13 -0800206 udp_tunnel_gro_complete(skb, nhoff);
207
Tom Herbertafe93322014-09-17 12:25:57 -0700208 rcu_read_lock();
Tom Herbertefc98d02014-10-03 15:48:08 -0700209 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
Tom Herbertafe93322014-09-17 12:25:57 -0700210 ops = rcu_dereference(offloads[proto]);
211 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
212 goto out_unlock;
213
214 err = ops->callbacks.gro_complete(skb, nhoff);
215
216out_unlock:
217 rcu_read_unlock();
218
219 return err;
220}
221
Tom Herberta8d31c12014-11-04 09:06:57 -0800222static struct guehdr *gue_gro_remcsum(struct sk_buff *skb, unsigned int off,
223 struct guehdr *guehdr, void *data,
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800224 size_t hdrlen, u8 ipproto,
Tom Herbertfe881ef2015-02-10 16:30:33 -0800225 struct gro_remcsum *grc, bool nopartial)
Tom Herberta8d31c12014-11-04 09:06:57 -0800226{
227 __be16 *pd = data;
Tom Herbert4fd671d2014-11-25 11:21:20 -0800228 size_t start = ntohs(pd[0]);
229 size_t offset = ntohs(pd[1]);
230 size_t plen = hdrlen + max_t(size_t, offset + sizeof(u16), start);
Tom Herberta8d31c12014-11-04 09:06:57 -0800231
232 if (skb->remcsum_offload)
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800233 return NULL;
Tom Herberta8d31c12014-11-04 09:06:57 -0800234
Tom Herbert4fd671d2014-11-25 11:21:20 -0800235 if (!NAPI_GRO_CB(skb)->csum_valid)
Tom Herberta8d31c12014-11-04 09:06:57 -0800236 return NULL;
237
Tom Herberta8d31c12014-11-04 09:06:57 -0800238 /* Pull checksum that will be written */
239 if (skb_gro_header_hard(skb, off + plen)) {
240 guehdr = skb_gro_header_slow(skb, off + plen, off);
241 if (!guehdr)
242 return NULL;
243 }
244
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800245 skb_gro_remcsum_process(skb, (void *)guehdr + hdrlen,
Tom Herbertfe881ef2015-02-10 16:30:33 -0800246 start, offset, grc, nopartial);
Tom Herberta8d31c12014-11-04 09:06:57 -0800247
248 skb->remcsum_offload = 1;
249
250 return guehdr;
251}
252
Tom Herbert37dd0242014-10-03 15:48:09 -0700253static struct sk_buff **gue_gro_receive(struct sk_buff **head,
Tom Herberta2b12f32015-01-12 17:00:37 -0800254 struct sk_buff *skb,
255 struct udp_offload *uoff)
Tom Herbert37dd0242014-10-03 15:48:09 -0700256{
257 const struct net_offload **offloads;
258 const struct net_offload *ops;
259 struct sk_buff **pp = NULL;
260 struct sk_buff *p;
Tom Herbert37dd0242014-10-03 15:48:09 -0700261 struct guehdr *guehdr;
Tom Herbert5024c332014-11-04 09:06:53 -0800262 size_t len, optlen, hdrlen, off;
263 void *data;
Tom Herberta8d31c12014-11-04 09:06:57 -0800264 u16 doffset = 0;
Tom Herbert37dd0242014-10-03 15:48:09 -0700265 int flush = 1;
Tom Herbertfe881ef2015-02-10 16:30:33 -0800266 struct fou *fou = container_of(uoff, struct fou, udp_offloads);
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800267 struct gro_remcsum grc;
268
269 skb_gro_remcsum_init(&grc);
Tom Herbert37dd0242014-10-03 15:48:09 -0700270
271 off = skb_gro_offset(skb);
Tom Herbert5024c332014-11-04 09:06:53 -0800272 len = off + sizeof(*guehdr);
273
Tom Herbert37dd0242014-10-03 15:48:09 -0700274 guehdr = skb_gro_header_fast(skb, off);
Tom Herbert5024c332014-11-04 09:06:53 -0800275 if (skb_gro_header_hard(skb, len)) {
276 guehdr = skb_gro_header_slow(skb, len, off);
Tom Herbert37dd0242014-10-03 15:48:09 -0700277 if (unlikely(!guehdr))
278 goto out;
279 }
280
Tom Herbert5024c332014-11-04 09:06:53 -0800281 optlen = guehdr->hlen << 2;
282 len += optlen;
Tom Herbert37dd0242014-10-03 15:48:09 -0700283
Tom Herbert5024c332014-11-04 09:06:53 -0800284 if (skb_gro_header_hard(skb, len)) {
285 guehdr = skb_gro_header_slow(skb, len, off);
Tom Herbert37dd0242014-10-03 15:48:09 -0700286 if (unlikely(!guehdr))
Tom Herbert5024c332014-11-04 09:06:53 -0800287 goto out;
288 }
289
290 if (unlikely(guehdr->control) || guehdr->version != 0 ||
291 validate_gue_flags(guehdr, optlen))
292 goto out;
293
294 hdrlen = sizeof(*guehdr) + optlen;
295
Tom Herberta8d31c12014-11-04 09:06:57 -0800296 /* Adjust NAPI_GRO_CB(skb)->csum to account for guehdr,
297 * this is needed if there is a remote checkcsum offload.
298 */
Tom Herbert5024c332014-11-04 09:06:53 -0800299 skb_gro_postpull_rcsum(skb, guehdr, hdrlen);
300
301 data = &guehdr[1];
302
303 if (guehdr->flags & GUE_FLAG_PRIV) {
Tom Herberta8d31c12014-11-04 09:06:57 -0800304 __be32 flags = *(__be32 *)(data + doffset);
Tom Herbert5024c332014-11-04 09:06:53 -0800305
Tom Herberta8d31c12014-11-04 09:06:57 -0800306 doffset += GUE_LEN_PRIV;
307
308 if (flags & GUE_PFLAG_REMCSUM) {
309 guehdr = gue_gro_remcsum(skb, off, guehdr,
310 data + doffset, hdrlen,
Tom Herbertfe881ef2015-02-10 16:30:33 -0800311 guehdr->proto_ctype, &grc,
312 !!(fou->flags &
313 FOU_F_REMCSUM_NOPARTIAL));
Tom Herberta8d31c12014-11-04 09:06:57 -0800314 if (!guehdr)
315 goto out;
316
317 data = &guehdr[1];
318
319 doffset += GUE_PLEN_REMCSUM;
320 }
Tom Herbert37dd0242014-10-03 15:48:09 -0700321 }
322
Tom Herberta8d31c12014-11-04 09:06:57 -0800323 skb_gro_pull(skb, hdrlen);
324
Tom Herbert37dd0242014-10-03 15:48:09 -0700325 flush = 0;
326
327 for (p = *head; p; p = p->next) {
328 const struct guehdr *guehdr2;
329
330 if (!NAPI_GRO_CB(p)->same_flow)
331 continue;
332
333 guehdr2 = (struct guehdr *)(p->data + off);
334
335 /* Compare base GUE header to be equal (covers
Tom Herbert5024c332014-11-04 09:06:53 -0800336 * hlen, version, proto_ctype, and flags.
Tom Herbert37dd0242014-10-03 15:48:09 -0700337 */
338 if (guehdr->word != guehdr2->word) {
339 NAPI_GRO_CB(p)->same_flow = 0;
340 continue;
341 }
342
343 /* Compare optional fields are the same. */
344 if (guehdr->hlen && memcmp(&guehdr[1], &guehdr2[1],
345 guehdr->hlen << 2)) {
346 NAPI_GRO_CB(p)->same_flow = 0;
347 continue;
348 }
349 }
350
Tom Herbert5024c332014-11-04 09:06:53 -0800351 rcu_read_lock();
352 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
353 ops = rcu_dereference(offloads[guehdr->proto_ctype]);
354 if (WARN_ON(!ops || !ops->callbacks.gro_receive))
355 goto out_unlock;
Tom Herbert37dd0242014-10-03 15:48:09 -0700356
357 pp = ops->callbacks.gro_receive(head, skb);
358
359out_unlock:
360 rcu_read_unlock();
361out:
362 NAPI_GRO_CB(skb)->flush |= flush;
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800363 skb_gro_remcsum_cleanup(skb, &grc);
Tom Herbert37dd0242014-10-03 15:48:09 -0700364
365 return pp;
366}
367
Tom Herberta2b12f32015-01-12 17:00:37 -0800368static int gue_gro_complete(struct sk_buff *skb, int nhoff,
369 struct udp_offload *uoff)
Tom Herbert37dd0242014-10-03 15:48:09 -0700370{
371 const struct net_offload **offloads;
372 struct guehdr *guehdr = (struct guehdr *)(skb->data + nhoff);
373 const struct net_offload *ops;
374 unsigned int guehlen;
375 u8 proto;
376 int err = -ENOENT;
377
Tom Herbert5024c332014-11-04 09:06:53 -0800378 proto = guehdr->proto_ctype;
Tom Herbert37dd0242014-10-03 15:48:09 -0700379
380 guehlen = sizeof(*guehdr) + (guehdr->hlen << 2);
381
382 rcu_read_lock();
383 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
384 ops = rcu_dereference(offloads[proto]);
385 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
386 goto out_unlock;
387
388 err = ops->callbacks.gro_complete(skb, nhoff + guehlen);
389
390out_unlock:
391 rcu_read_unlock();
392 return err;
393}
394
WANG Cong02d793c2015-04-10 12:00:29 -0700395static int fou_add_to_port_list(struct net *net, struct fou *fou)
Tom Herbert23461552014-09-17 12:25:56 -0700396{
WANG Cong02d793c2015-04-10 12:00:29 -0700397 struct fou_net *fn = net_generic(net, fou_net_id);
Tom Herbert23461552014-09-17 12:25:56 -0700398 struct fou *fout;
399
WANG Cong02d793c2015-04-10 12:00:29 -0700400 mutex_lock(&fn->fou_lock);
401 list_for_each_entry(fout, &fn->fou_list, list) {
Tom Herbert23461552014-09-17 12:25:56 -0700402 if (fou->port == fout->port) {
WANG Cong02d793c2015-04-10 12:00:29 -0700403 mutex_unlock(&fn->fou_lock);
Tom Herbert23461552014-09-17 12:25:56 -0700404 return -EALREADY;
405 }
406 }
407
WANG Cong02d793c2015-04-10 12:00:29 -0700408 list_add(&fou->list, &fn->fou_list);
409 mutex_unlock(&fn->fou_lock);
Tom Herbert23461552014-09-17 12:25:56 -0700410
411 return 0;
412}
413
414static void fou_release(struct fou *fou)
415{
416 struct socket *sock = fou->sock;
417 struct sock *sk = sock->sk;
418
WANG Cong9272f042015-04-10 12:00:26 -0700419 if (sk->sk_family == AF_INET)
420 udp_del_offload(&fou->udp_offloads);
Tom Herbert23461552014-09-17 12:25:56 -0700421 list_del(&fou->list);
WANG Cong02d793c2015-04-10 12:00:29 -0700422 udp_tunnel_sock_release(sock);
Tom Herbert23461552014-09-17 12:25:56 -0700423
424 kfree(fou);
425}
426
Tom Herbert37dd0242014-10-03 15:48:09 -0700427static int fou_encap_init(struct sock *sk, struct fou *fou, struct fou_cfg *cfg)
428{
429 udp_sk(sk)->encap_rcv = fou_udp_recv;
430 fou->protocol = cfg->protocol;
431 fou->udp_offloads.callbacks.gro_receive = fou_gro_receive;
432 fou->udp_offloads.callbacks.gro_complete = fou_gro_complete;
433 fou->udp_offloads.port = cfg->udp_config.local_udp_port;
434 fou->udp_offloads.ipproto = cfg->protocol;
435
436 return 0;
437}
438
439static int gue_encap_init(struct sock *sk, struct fou *fou, struct fou_cfg *cfg)
440{
441 udp_sk(sk)->encap_rcv = gue_udp_recv;
442 fou->udp_offloads.callbacks.gro_receive = gue_gro_receive;
443 fou->udp_offloads.callbacks.gro_complete = gue_gro_complete;
444 fou->udp_offloads.port = cfg->udp_config.local_udp_port;
445
446 return 0;
447}
448
Tom Herbert23461552014-09-17 12:25:56 -0700449static int fou_create(struct net *net, struct fou_cfg *cfg,
450 struct socket **sockp)
451{
Tom Herbert23461552014-09-17 12:25:56 -0700452 struct socket *sock = NULL;
WANG Cong02d793c2015-04-10 12:00:29 -0700453 struct fou *fou = NULL;
Tom Herbert23461552014-09-17 12:25:56 -0700454 struct sock *sk;
WANG Cong02d793c2015-04-10 12:00:29 -0700455 int err;
Tom Herbert23461552014-09-17 12:25:56 -0700456
457 /* Open UDP socket */
458 err = udp_sock_create(net, &cfg->udp_config, &sock);
459 if (err < 0)
460 goto error;
461
462 /* Allocate FOU port structure */
463 fou = kzalloc(sizeof(*fou), GFP_KERNEL);
464 if (!fou) {
465 err = -ENOMEM;
466 goto error;
467 }
468
469 sk = sock->sk;
470
Tom Herbertfe881ef2015-02-10 16:30:33 -0800471 fou->flags = cfg->flags;
Tom Herbert37dd0242014-10-03 15:48:09 -0700472 fou->port = cfg->udp_config.local_udp_port;
473
474 /* Initial for fou type */
475 switch (cfg->type) {
476 case FOU_ENCAP_DIRECT:
477 err = fou_encap_init(sk, fou, cfg);
478 if (err)
479 goto error;
480 break;
481 case FOU_ENCAP_GUE:
482 err = gue_encap_init(sk, fou, cfg);
483 if (err)
484 goto error;
485 break;
486 default:
487 err = -EINVAL;
488 goto error;
489 }
Tom Herbert23461552014-09-17 12:25:56 -0700490
WANG Cong7a6c8c32015-04-10 12:00:30 -0700491 fou->type = cfg->type;
492
Tom Herbert23461552014-09-17 12:25:56 -0700493 udp_sk(sk)->encap_type = 1;
494 udp_encap_enable();
495
496 sk->sk_user_data = fou;
497 fou->sock = sock;
498
Tom Herbert224d0192015-01-05 13:56:14 -0800499 inet_inc_convert_csum(sk);
Tom Herbert23461552014-09-17 12:25:56 -0700500
501 sk->sk_allocation = GFP_ATOMIC;
502
Tom Herbertafe93322014-09-17 12:25:57 -0700503 if (cfg->udp_config.family == AF_INET) {
504 err = udp_add_offload(&fou->udp_offloads);
505 if (err)
506 goto error;
507 }
508
WANG Cong02d793c2015-04-10 12:00:29 -0700509 err = fou_add_to_port_list(net, fou);
Tom Herbert23461552014-09-17 12:25:56 -0700510 if (err)
511 goto error;
512
513 if (sockp)
514 *sockp = sock;
515
516 return 0;
517
518error:
519 kfree(fou);
520 if (sock)
WANG Cong02d793c2015-04-10 12:00:29 -0700521 udp_tunnel_sock_release(sock);
Tom Herbert23461552014-09-17 12:25:56 -0700522
523 return err;
524}
525
526static int fou_destroy(struct net *net, struct fou_cfg *cfg)
527{
WANG Cong02d793c2015-04-10 12:00:29 -0700528 struct fou_net *fn = net_generic(net, fou_net_id);
WANG Cong4cbcdf22015-04-10 12:00:28 -0700529 __be16 port = cfg->udp_config.local_udp_port;
Tom Herbert23461552014-09-17 12:25:56 -0700530 int err = -EINVAL;
WANG Cong02d793c2015-04-10 12:00:29 -0700531 struct fou *fou;
Tom Herbert23461552014-09-17 12:25:56 -0700532
WANG Cong02d793c2015-04-10 12:00:29 -0700533 mutex_lock(&fn->fou_lock);
534 list_for_each_entry(fou, &fn->fou_list, list) {
Tom Herbert23461552014-09-17 12:25:56 -0700535 if (fou->port == port) {
Tom Herbert23461552014-09-17 12:25:56 -0700536 fou_release(fou);
537 err = 0;
538 break;
539 }
540 }
WANG Cong02d793c2015-04-10 12:00:29 -0700541 mutex_unlock(&fn->fou_lock);
Tom Herbert23461552014-09-17 12:25:56 -0700542
543 return err;
544}
545
546static struct genl_family fou_nl_family = {
547 .id = GENL_ID_GENERATE,
548 .hdrsize = 0,
549 .name = FOU_GENL_NAME,
550 .version = FOU_GENL_VERSION,
551 .maxattr = FOU_ATTR_MAX,
552 .netnsok = true,
553};
554
555static struct nla_policy fou_nl_policy[FOU_ATTR_MAX + 1] = {
556 [FOU_ATTR_PORT] = { .type = NLA_U16, },
557 [FOU_ATTR_AF] = { .type = NLA_U8, },
558 [FOU_ATTR_IPPROTO] = { .type = NLA_U8, },
Tom Herbert37dd0242014-10-03 15:48:09 -0700559 [FOU_ATTR_TYPE] = { .type = NLA_U8, },
Tom Herbertfe881ef2015-02-10 16:30:33 -0800560 [FOU_ATTR_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG, },
Tom Herbert23461552014-09-17 12:25:56 -0700561};
562
563static int parse_nl_config(struct genl_info *info,
564 struct fou_cfg *cfg)
565{
566 memset(cfg, 0, sizeof(*cfg));
567
568 cfg->udp_config.family = AF_INET;
569
570 if (info->attrs[FOU_ATTR_AF]) {
571 u8 family = nla_get_u8(info->attrs[FOU_ATTR_AF]);
572
573 if (family != AF_INET && family != AF_INET6)
574 return -EINVAL;
575
576 cfg->udp_config.family = family;
577 }
578
579 if (info->attrs[FOU_ATTR_PORT]) {
WANG Cong4cbcdf22015-04-10 12:00:28 -0700580 __be16 port = nla_get_be16(info->attrs[FOU_ATTR_PORT]);
Tom Herbert23461552014-09-17 12:25:56 -0700581
582 cfg->udp_config.local_udp_port = port;
583 }
584
585 if (info->attrs[FOU_ATTR_IPPROTO])
586 cfg->protocol = nla_get_u8(info->attrs[FOU_ATTR_IPPROTO]);
587
Tom Herbert37dd0242014-10-03 15:48:09 -0700588 if (info->attrs[FOU_ATTR_TYPE])
589 cfg->type = nla_get_u8(info->attrs[FOU_ATTR_TYPE]);
590
Tom Herbertfe881ef2015-02-10 16:30:33 -0800591 if (info->attrs[FOU_ATTR_REMCSUM_NOPARTIAL])
592 cfg->flags |= FOU_F_REMCSUM_NOPARTIAL;
593
Tom Herbert23461552014-09-17 12:25:56 -0700594 return 0;
595}
596
597static int fou_nl_cmd_add_port(struct sk_buff *skb, struct genl_info *info)
598{
WANG Cong02d793c2015-04-10 12:00:29 -0700599 struct net *net = genl_info_net(info);
Tom Herbert23461552014-09-17 12:25:56 -0700600 struct fou_cfg cfg;
601 int err;
602
603 err = parse_nl_config(info, &cfg);
604 if (err)
605 return err;
606
WANG Cong02d793c2015-04-10 12:00:29 -0700607 return fou_create(net, &cfg, NULL);
Tom Herbert23461552014-09-17 12:25:56 -0700608}
609
610static int fou_nl_cmd_rm_port(struct sk_buff *skb, struct genl_info *info)
611{
WANG Cong02d793c2015-04-10 12:00:29 -0700612 struct net *net = genl_info_net(info);
Tom Herbert23461552014-09-17 12:25:56 -0700613 struct fou_cfg cfg;
WANG Cong67270632015-04-10 12:00:27 -0700614 int err;
Tom Herbert23461552014-09-17 12:25:56 -0700615
WANG Cong67270632015-04-10 12:00:27 -0700616 err = parse_nl_config(info, &cfg);
617 if (err)
618 return err;
Tom Herbert23461552014-09-17 12:25:56 -0700619
WANG Cong02d793c2015-04-10 12:00:29 -0700620 return fou_destroy(net, &cfg);
Tom Herbert23461552014-09-17 12:25:56 -0700621}
622
WANG Cong7a6c8c32015-04-10 12:00:30 -0700623static int fou_fill_info(struct fou *fou, struct sk_buff *msg)
624{
625 if (nla_put_u8(msg, FOU_ATTR_AF, fou->sock->sk->sk_family) ||
626 nla_put_be16(msg, FOU_ATTR_PORT, fou->port) ||
627 nla_put_u8(msg, FOU_ATTR_IPPROTO, fou->protocol) ||
628 nla_put_u8(msg, FOU_ATTR_TYPE, fou->type))
629 return -1;
630
631 if (fou->flags & FOU_F_REMCSUM_NOPARTIAL)
632 if (nla_put_flag(msg, FOU_ATTR_REMCSUM_NOPARTIAL))
633 return -1;
634 return 0;
635}
636
637static int fou_dump_info(struct fou *fou, u32 portid, u32 seq,
638 u32 flags, struct sk_buff *skb, u8 cmd)
639{
640 void *hdr;
641
642 hdr = genlmsg_put(skb, portid, seq, &fou_nl_family, flags, cmd);
643 if (!hdr)
644 return -ENOMEM;
645
646 if (fou_fill_info(fou, skb) < 0)
647 goto nla_put_failure;
648
649 genlmsg_end(skb, hdr);
650 return 0;
651
652nla_put_failure:
653 genlmsg_cancel(skb, hdr);
654 return -EMSGSIZE;
655}
656
657static int fou_nl_cmd_get_port(struct sk_buff *skb, struct genl_info *info)
658{
659 struct net *net = genl_info_net(info);
660 struct fou_net *fn = net_generic(net, fou_net_id);
661 struct sk_buff *msg;
662 struct fou_cfg cfg;
663 struct fou *fout;
664 __be16 port;
665 int ret;
666
667 ret = parse_nl_config(info, &cfg);
668 if (ret)
669 return ret;
670 port = cfg.udp_config.local_udp_port;
671 if (port == 0)
672 return -EINVAL;
673
674 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
675 if (!msg)
676 return -ENOMEM;
677
678 ret = -ESRCH;
679 mutex_lock(&fn->fou_lock);
680 list_for_each_entry(fout, &fn->fou_list, list) {
681 if (port == fout->port) {
682 ret = fou_dump_info(fout, info->snd_portid,
683 info->snd_seq, 0, msg,
684 info->genlhdr->cmd);
685 break;
686 }
687 }
688 mutex_unlock(&fn->fou_lock);
689 if (ret < 0)
690 goto out_free;
691
692 return genlmsg_reply(msg, info);
693
694out_free:
695 nlmsg_free(msg);
696 return ret;
697}
698
699static int fou_nl_dump(struct sk_buff *skb, struct netlink_callback *cb)
700{
701 struct net *net = sock_net(skb->sk);
702 struct fou_net *fn = net_generic(net, fou_net_id);
703 struct fou *fout;
704 int idx = 0, ret;
705
706 mutex_lock(&fn->fou_lock);
707 list_for_each_entry(fout, &fn->fou_list, list) {
708 if (idx++ < cb->args[0])
709 continue;
710 ret = fou_dump_info(fout, NETLINK_CB(cb->skb).portid,
711 cb->nlh->nlmsg_seq, NLM_F_MULTI,
712 skb, FOU_CMD_GET);
713 if (ret)
WANG Cong540207a2015-04-15 11:48:49 -0700714 break;
WANG Cong7a6c8c32015-04-10 12:00:30 -0700715 }
716 mutex_unlock(&fn->fou_lock);
717
WANG Cong7a6c8c32015-04-10 12:00:30 -0700718 cb->args[0] = idx;
719 return skb->len;
720}
721
Tom Herbert23461552014-09-17 12:25:56 -0700722static const struct genl_ops fou_nl_ops[] = {
723 {
724 .cmd = FOU_CMD_ADD,
725 .doit = fou_nl_cmd_add_port,
726 .policy = fou_nl_policy,
727 .flags = GENL_ADMIN_PERM,
728 },
729 {
730 .cmd = FOU_CMD_DEL,
731 .doit = fou_nl_cmd_rm_port,
732 .policy = fou_nl_policy,
733 .flags = GENL_ADMIN_PERM,
734 },
WANG Cong7a6c8c32015-04-10 12:00:30 -0700735 {
736 .cmd = FOU_CMD_GET,
737 .doit = fou_nl_cmd_get_port,
738 .dumpit = fou_nl_dump,
739 .policy = fou_nl_policy,
740 },
Tom Herbert23461552014-09-17 12:25:56 -0700741};
742
Tom Herberta8c5f902014-11-12 11:54:09 -0800743size_t fou_encap_hlen(struct ip_tunnel_encap *e)
744{
745 return sizeof(struct udphdr);
746}
747EXPORT_SYMBOL(fou_encap_hlen);
748
749size_t gue_encap_hlen(struct ip_tunnel_encap *e)
750{
751 size_t len;
752 bool need_priv = false;
753
754 len = sizeof(struct udphdr) + sizeof(struct guehdr);
755
756 if (e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) {
757 len += GUE_PLEN_REMCSUM;
758 need_priv = true;
759 }
760
761 len += need_priv ? GUE_LEN_PRIV : 0;
762
763 return len;
764}
765EXPORT_SYMBOL(gue_encap_hlen);
766
Tom Herbert63487ba2014-11-04 09:06:51 -0800767static void fou_build_udp(struct sk_buff *skb, struct ip_tunnel_encap *e,
768 struct flowi4 *fl4, u8 *protocol, __be16 sport)
769{
770 struct udphdr *uh;
771
772 skb_push(skb, sizeof(struct udphdr));
773 skb_reset_transport_header(skb);
774
775 uh = udp_hdr(skb);
776
777 uh->dest = e->dport;
778 uh->source = sport;
779 uh->len = htons(skb->len);
780 uh->check = 0;
781 udp_set_csum(!(e->flags & TUNNEL_ENCAP_FLAG_CSUM), skb,
782 fl4->saddr, fl4->daddr, skb->len);
783
784 *protocol = IPPROTO_UDP;
785}
786
787int fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
788 u8 *protocol, struct flowi4 *fl4)
789{
790 bool csum = !!(e->flags & TUNNEL_ENCAP_FLAG_CSUM);
791 int type = csum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
792 __be16 sport;
793
794 skb = iptunnel_handle_offloads(skb, csum, type);
795
796 if (IS_ERR(skb))
797 return PTR_ERR(skb);
798
799 sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
800 skb, 0, 0, false);
801 fou_build_udp(skb, e, fl4, protocol, sport);
802
803 return 0;
804}
805EXPORT_SYMBOL(fou_build_header);
806
807int gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
808 u8 *protocol, struct flowi4 *fl4)
809{
810 bool csum = !!(e->flags & TUNNEL_ENCAP_FLAG_CSUM);
811 int type = csum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
812 struct guehdr *guehdr;
Tom Herbertb17f7092014-11-04 09:06:56 -0800813 size_t hdrlen, optlen = 0;
Tom Herbert63487ba2014-11-04 09:06:51 -0800814 __be16 sport;
Tom Herbert5024c332014-11-04 09:06:53 -0800815 void *data;
816 bool need_priv = false;
817
Tom Herbertb17f7092014-11-04 09:06:56 -0800818 if ((e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) &&
819 skb->ip_summed == CHECKSUM_PARTIAL) {
820 csum = false;
821 optlen += GUE_PLEN_REMCSUM;
822 type |= SKB_GSO_TUNNEL_REMCSUM;
823 need_priv = true;
824 }
825
Tom Herbert5024c332014-11-04 09:06:53 -0800826 optlen += need_priv ? GUE_LEN_PRIV : 0;
Tom Herbert63487ba2014-11-04 09:06:51 -0800827
828 skb = iptunnel_handle_offloads(skb, csum, type);
829
830 if (IS_ERR(skb))
831 return PTR_ERR(skb);
832
833 /* Get source port (based on flow hash) before skb_push */
834 sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
835 skb, 0, 0, false);
836
Tom Herbertb17f7092014-11-04 09:06:56 -0800837 hdrlen = sizeof(struct guehdr) + optlen;
838
839 skb_push(skb, hdrlen);
Tom Herbert63487ba2014-11-04 09:06:51 -0800840
841 guehdr = (struct guehdr *)skb->data;
842
Tom Herbert5024c332014-11-04 09:06:53 -0800843 guehdr->control = 0;
Tom Herbert63487ba2014-11-04 09:06:51 -0800844 guehdr->version = 0;
Tom Herbert5024c332014-11-04 09:06:53 -0800845 guehdr->hlen = optlen >> 2;
Tom Herbert63487ba2014-11-04 09:06:51 -0800846 guehdr->flags = 0;
Tom Herbert5024c332014-11-04 09:06:53 -0800847 guehdr->proto_ctype = *protocol;
848
849 data = &guehdr[1];
850
851 if (need_priv) {
852 __be32 *flags = data;
853
854 guehdr->flags |= GUE_FLAG_PRIV;
855 *flags = 0;
856 data += GUE_LEN_PRIV;
857
Tom Herbertb17f7092014-11-04 09:06:56 -0800858 if (type & SKB_GSO_TUNNEL_REMCSUM) {
859 u16 csum_start = skb_checksum_start_offset(skb);
860 __be16 *pd = data;
861
862 if (csum_start < hdrlen)
863 return -EINVAL;
864
865 csum_start -= hdrlen;
866 pd[0] = htons(csum_start);
867 pd[1] = htons(csum_start + skb->csum_offset);
868
869 if (!skb_is_gso(skb)) {
870 skb->ip_summed = CHECKSUM_NONE;
871 skb->encapsulation = 0;
872 }
873
874 *flags |= GUE_PFLAG_REMCSUM;
875 data += GUE_PLEN_REMCSUM;
876 }
877
Tom Herbert5024c332014-11-04 09:06:53 -0800878 }
Tom Herbert63487ba2014-11-04 09:06:51 -0800879
880 fou_build_udp(skb, e, fl4, protocol, sport);
881
882 return 0;
883}
884EXPORT_SYMBOL(gue_build_header);
885
Tom Herberta8c5f902014-11-12 11:54:09 -0800886#ifdef CONFIG_NET_FOU_IP_TUNNELS
887
Andi Kleen5eeb2922015-04-08 06:04:31 -0700888static const struct ip_tunnel_encap_ops fou_iptun_ops = {
Tom Herberta8c5f902014-11-12 11:54:09 -0800889 .encap_hlen = fou_encap_hlen,
890 .build_header = fou_build_header,
891};
892
Andi Kleen5eeb2922015-04-08 06:04:31 -0700893static const struct ip_tunnel_encap_ops gue_iptun_ops = {
Tom Herberta8c5f902014-11-12 11:54:09 -0800894 .encap_hlen = gue_encap_hlen,
895 .build_header = gue_build_header,
896};
897
898static int ip_tunnel_encap_add_fou_ops(void)
899{
900 int ret;
901
902 ret = ip_tunnel_encap_add_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
903 if (ret < 0) {
904 pr_err("can't add fou ops\n");
905 return ret;
906 }
907
908 ret = ip_tunnel_encap_add_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
909 if (ret < 0) {
910 pr_err("can't add gue ops\n");
911 ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
912 return ret;
913 }
914
915 return 0;
916}
917
918static void ip_tunnel_encap_del_fou_ops(void)
919{
920 ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
921 ip_tunnel_encap_del_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
922}
923
924#else
925
926static int ip_tunnel_encap_add_fou_ops(void)
927{
928 return 0;
929}
930
Thomas Graf882288c2014-11-13 12:48:21 +0100931static void ip_tunnel_encap_del_fou_ops(void)
Tom Herberta8c5f902014-11-12 11:54:09 -0800932{
933}
934
935#endif
936
WANG Cong02d793c2015-04-10 12:00:29 -0700937static __net_init int fou_init_net(struct net *net)
938{
939 struct fou_net *fn = net_generic(net, fou_net_id);
940
941 INIT_LIST_HEAD(&fn->fou_list);
942 mutex_init(&fn->fou_lock);
943 return 0;
944}
945
946static __net_exit void fou_exit_net(struct net *net)
947{
948 struct fou_net *fn = net_generic(net, fou_net_id);
949 struct fou *fou, *next;
950
951 /* Close all the FOU sockets */
952 mutex_lock(&fn->fou_lock);
953 list_for_each_entry_safe(fou, next, &fn->fou_list, list)
954 fou_release(fou);
955 mutex_unlock(&fn->fou_lock);
956}
957
958static struct pernet_operations fou_net_ops = {
959 .init = fou_init_net,
960 .exit = fou_exit_net,
961 .id = &fou_net_id,
962 .size = sizeof(struct fou_net),
963};
964
Tom Herbert23461552014-09-17 12:25:56 -0700965static int __init fou_init(void)
966{
967 int ret;
968
WANG Cong02d793c2015-04-10 12:00:29 -0700969 ret = register_pernet_device(&fou_net_ops);
970 if (ret)
Tom Herberta8c5f902014-11-12 11:54:09 -0800971 goto exit;
972
WANG Cong02d793c2015-04-10 12:00:29 -0700973 ret = genl_register_family_with_ops(&fou_nl_family,
974 fou_nl_ops);
Tom Herberta8c5f902014-11-12 11:54:09 -0800975 if (ret < 0)
WANG Cong02d793c2015-04-10 12:00:29 -0700976 goto unregister;
Tom Herberta8c5f902014-11-12 11:54:09 -0800977
WANG Cong02d793c2015-04-10 12:00:29 -0700978 ret = ip_tunnel_encap_add_fou_ops();
979 if (ret == 0)
980 return 0;
981
982 genl_unregister_family(&fou_nl_family);
983unregister:
984 unregister_pernet_device(&fou_net_ops);
Tom Herberta8c5f902014-11-12 11:54:09 -0800985exit:
Tom Herbert23461552014-09-17 12:25:56 -0700986 return ret;
987}
988
989static void __exit fou_fini(void)
990{
Tom Herberta8c5f902014-11-12 11:54:09 -0800991 ip_tunnel_encap_del_fou_ops();
Tom Herbert23461552014-09-17 12:25:56 -0700992 genl_unregister_family(&fou_nl_family);
WANG Cong02d793c2015-04-10 12:00:29 -0700993 unregister_pernet_device(&fou_net_ops);
Tom Herbert23461552014-09-17 12:25:56 -0700994}
995
996module_init(fou_init);
997module_exit(fou_fini);
998MODULE_AUTHOR("Tom Herbert <therbert@google.com>");
999MODULE_LICENSE("GPL");