blob: 88dab0c1670c355c21b3c0aecd253e85ec2fd28f [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;
Hannes Frederic Sowa3036fac2015-12-15 21:01:53 +010027 struct rcu_head rcu;
Tom Herbert23461552014-09-17 12:25:56 -070028};
29
Tom Herbertfe881ef2015-02-10 16:30:33 -080030#define FOU_F_REMCSUM_NOPARTIAL BIT(0)
31
Tom Herbert23461552014-09-17 12:25:56 -070032struct fou_cfg {
Tom Herbert37dd0242014-10-03 15:48:09 -070033 u16 type;
Tom Herbert23461552014-09-17 12:25:56 -070034 u8 protocol;
Tom Herbertfe881ef2015-02-10 16:30:33 -080035 u8 flags;
Tom Herbert23461552014-09-17 12:25:56 -070036 struct udp_port_cfg udp_config;
37};
38
WANG Cong02d793c2015-04-10 12:00:29 -070039static unsigned int fou_net_id;
40
41struct fou_net {
42 struct list_head fou_list;
43 struct mutex fou_lock;
44};
45
Tom Herbert23461552014-09-17 12:25:56 -070046static inline struct fou *fou_from_sock(struct sock *sk)
47{
48 return sk->sk_user_data;
49}
50
Tom Herbert5024c332014-11-04 09:06:53 -080051static void fou_recv_pull(struct sk_buff *skb, size_t len)
Tom Herbert23461552014-09-17 12:25:56 -070052{
53 struct iphdr *iph = ip_hdr(skb);
54
55 /* Remove 'len' bytes from the packet (UDP header and
Tom Herbert5024c332014-11-04 09:06:53 -080056 * FOU header if present).
Tom Herbert23461552014-09-17 12:25:56 -070057 */
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);
Tom Herbert23461552014-09-17 12:25:56 -070062}
63
64static int fou_udp_recv(struct sock *sk, struct sk_buff *skb)
65{
66 struct fou *fou = fou_from_sock(sk);
67
68 if (!fou)
69 return 1;
70
Tom Herbert5024c332014-11-04 09:06:53 -080071 fou_recv_pull(skb, sizeof(struct udphdr));
72
73 return -fou->protocol;
74}
75
Tom Herberta8d31c12014-11-04 09:06:57 -080076static struct guehdr *gue_remcsum(struct sk_buff *skb, struct guehdr *guehdr,
Tom Herbertfe881ef2015-02-10 16:30:33 -080077 void *data, size_t hdrlen, u8 ipproto,
78 bool nopartial)
Tom Herberta8d31c12014-11-04 09:06:57 -080079{
80 __be16 *pd = data;
Tom Herbert4fd671d2014-11-25 11:21:20 -080081 size_t start = ntohs(pd[0]);
82 size_t offset = ntohs(pd[1]);
Tom Herbertb7fe10e2015-08-19 17:07:32 -070083 size_t plen = sizeof(struct udphdr) + hdrlen +
84 max_t(size_t, offset + sizeof(u16), start);
85
86 if (skb->remcsum_offload)
87 return guehdr;
Tom Herberta8d31c12014-11-04 09:06:57 -080088
Tom Herberta8d31c12014-11-04 09:06:57 -080089 if (!pskb_may_pull(skb, plen))
90 return NULL;
91 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
92
Tom Herbertfe881ef2015-02-10 16:30:33 -080093 skb_remcsum_process(skb, (void *)guehdr + hdrlen,
94 start, offset, nopartial);
Tom Herberta8d31c12014-11-04 09:06:57 -080095
96 return guehdr;
97}
98
Tom Herbert5024c332014-11-04 09:06:53 -080099static int gue_control_message(struct sk_buff *skb, struct guehdr *guehdr)
100{
101 /* No support yet */
102 kfree_skb(skb);
103 return 0;
Tom Herbert23461552014-09-17 12:25:56 -0700104}
105
Tom Herbert37dd0242014-10-03 15:48:09 -0700106static int gue_udp_recv(struct sock *sk, struct sk_buff *skb)
107{
108 struct fou *fou = fou_from_sock(sk);
Tom Herbert5024c332014-11-04 09:06:53 -0800109 size_t len, optlen, hdrlen;
Tom Herbert37dd0242014-10-03 15:48:09 -0700110 struct guehdr *guehdr;
Tom Herbert5024c332014-11-04 09:06:53 -0800111 void *data;
Tom Herberta8d31c12014-11-04 09:06:57 -0800112 u16 doffset = 0;
Tom Herbert37dd0242014-10-03 15:48:09 -0700113
114 if (!fou)
115 return 1;
116
117 len = sizeof(struct udphdr) + sizeof(struct guehdr);
118 if (!pskb_may_pull(skb, len))
119 goto drop;
120
Tom Herbert5024c332014-11-04 09:06:53 -0800121 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
Tom Herbert37dd0242014-10-03 15:48:09 -0700122
Tom Herbert5024c332014-11-04 09:06:53 -0800123 optlen = guehdr->hlen << 2;
124 len += optlen;
125
Tom Herbert37dd0242014-10-03 15:48:09 -0700126 if (!pskb_may_pull(skb, len))
127 goto drop;
128
Tom Herbert5024c332014-11-04 09:06:53 -0800129 /* guehdr may change after pull */
130 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
Li RongQingd8f00d22014-10-17 16:53:47 +0800131
Tom Herbert5024c332014-11-04 09:06:53 -0800132 hdrlen = sizeof(struct guehdr) + optlen;
133
134 if (guehdr->version != 0 || validate_gue_flags(guehdr, optlen))
Tom Herbert37dd0242014-10-03 15:48:09 -0700135 goto drop;
136
Tom Herberta8d31c12014-11-04 09:06:57 -0800137 hdrlen = sizeof(struct guehdr) + optlen;
138
139 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(skb)->tot_len) - len);
140
Tom Herberta8d31c12014-11-04 09:06:57 -0800141 /* Pull csum through the guehdr now . This can be used if
142 * there is a remote checksum offload.
143 */
144 skb_postpull_rcsum(skb, udp_hdr(skb), len);
Tom Herbert5024c332014-11-04 09:06:53 -0800145
146 data = &guehdr[1];
147
148 if (guehdr->flags & GUE_FLAG_PRIV) {
Tom Herberta8d31c12014-11-04 09:06:57 -0800149 __be32 flags = *(__be32 *)(data + doffset);
Tom Herbert5024c332014-11-04 09:06:53 -0800150
Tom Herberta8d31c12014-11-04 09:06:57 -0800151 doffset += GUE_LEN_PRIV;
152
153 if (flags & GUE_PFLAG_REMCSUM) {
154 guehdr = gue_remcsum(skb, guehdr, data + doffset,
Tom Herbertfe881ef2015-02-10 16:30:33 -0800155 hdrlen, guehdr->proto_ctype,
156 !!(fou->flags &
157 FOU_F_REMCSUM_NOPARTIAL));
Tom Herberta8d31c12014-11-04 09:06:57 -0800158 if (!guehdr)
159 goto drop;
160
161 data = &guehdr[1];
162
163 doffset += GUE_PLEN_REMCSUM;
164 }
Tom Herbert37dd0242014-10-03 15:48:09 -0700165 }
166
Tom Herbert5024c332014-11-04 09:06:53 -0800167 if (unlikely(guehdr->control))
168 return gue_control_message(skb, guehdr);
169
Tom Herbert4fd671d2014-11-25 11:21:20 -0800170 __skb_pull(skb, sizeof(struct udphdr) + hdrlen);
Tom Herberta8d31c12014-11-04 09:06:57 -0800171 skb_reset_transport_header(skb);
172
Tom Herbert5024c332014-11-04 09:06:53 -0800173 return -guehdr->proto_ctype;
174
Tom Herbert37dd0242014-10-03 15:48:09 -0700175drop:
176 kfree_skb(skb);
177 return 0;
178}
179
Tom Herbertafe93322014-09-17 12:25:57 -0700180static struct sk_buff **fou_gro_receive(struct sk_buff **head,
Tom Herberta2b12f32015-01-12 17:00:37 -0800181 struct sk_buff *skb,
182 struct udp_offload *uoff)
Tom Herbertafe93322014-09-17 12:25:57 -0700183{
184 const struct net_offload *ops;
185 struct sk_buff **pp = NULL;
186 u8 proto = NAPI_GRO_CB(skb)->proto;
Tom Herbertefc98d02014-10-03 15:48:08 -0700187 const struct net_offload **offloads;
Tom Herbertafe93322014-09-17 12:25:57 -0700188
189 rcu_read_lock();
Tom Herbertefc98d02014-10-03 15:48:08 -0700190 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
Tom Herbertafe93322014-09-17 12:25:57 -0700191 ops = rcu_dereference(offloads[proto]);
192 if (!ops || !ops->callbacks.gro_receive)
193 goto out_unlock;
194
195 pp = ops->callbacks.gro_receive(head, skb);
196
197out_unlock:
198 rcu_read_unlock();
199
200 return pp;
201}
202
Tom Herberta2b12f32015-01-12 17:00:37 -0800203static int fou_gro_complete(struct sk_buff *skb, int nhoff,
204 struct udp_offload *uoff)
Tom Herbertafe93322014-09-17 12:25:57 -0700205{
206 const struct net_offload *ops;
207 u8 proto = NAPI_GRO_CB(skb)->proto;
208 int err = -ENOSYS;
Tom Herbertefc98d02014-10-03 15:48:08 -0700209 const struct net_offload **offloads;
Tom Herbertafe93322014-09-17 12:25:57 -0700210
Jesse Grosscfdf1e12014-11-10 11:45:13 -0800211 udp_tunnel_gro_complete(skb, nhoff);
212
Tom Herbertafe93322014-09-17 12:25:57 -0700213 rcu_read_lock();
Tom Herbertefc98d02014-10-03 15:48:08 -0700214 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
Tom Herbertafe93322014-09-17 12:25:57 -0700215 ops = rcu_dereference(offloads[proto]);
216 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
217 goto out_unlock;
218
219 err = ops->callbacks.gro_complete(skb, nhoff);
220
221out_unlock:
222 rcu_read_unlock();
223
224 return err;
225}
226
Tom Herberta8d31c12014-11-04 09:06:57 -0800227static struct guehdr *gue_gro_remcsum(struct sk_buff *skb, unsigned int off,
228 struct guehdr *guehdr, void *data,
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700229 size_t hdrlen, struct gro_remcsum *grc,
230 bool nopartial)
Tom Herberta8d31c12014-11-04 09:06:57 -0800231{
232 __be16 *pd = data;
Tom Herbert4fd671d2014-11-25 11:21:20 -0800233 size_t start = ntohs(pd[0]);
234 size_t offset = ntohs(pd[1]);
Tom Herberta8d31c12014-11-04 09:06:57 -0800235
236 if (skb->remcsum_offload)
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700237 return guehdr;
Tom Herberta8d31c12014-11-04 09:06:57 -0800238
Tom Herbert4fd671d2014-11-25 11:21:20 -0800239 if (!NAPI_GRO_CB(skb)->csum_valid)
Tom Herberta8d31c12014-11-04 09:06:57 -0800240 return NULL;
241
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700242 guehdr = skb_gro_remcsum_process(skb, (void *)guehdr, off, hdrlen,
243 start, offset, grc, nopartial);
Tom Herberta8d31c12014-11-04 09:06:57 -0800244
245 skb->remcsum_offload = 1;
246
247 return guehdr;
248}
249
Tom Herbert37dd0242014-10-03 15:48:09 -0700250static struct sk_buff **gue_gro_receive(struct sk_buff **head,
Tom Herberta2b12f32015-01-12 17:00:37 -0800251 struct sk_buff *skb,
252 struct udp_offload *uoff)
Tom Herbert37dd0242014-10-03 15:48:09 -0700253{
254 const struct net_offload **offloads;
255 const struct net_offload *ops;
256 struct sk_buff **pp = NULL;
257 struct sk_buff *p;
Tom Herbert37dd0242014-10-03 15:48:09 -0700258 struct guehdr *guehdr;
Tom Herbert5024c332014-11-04 09:06:53 -0800259 size_t len, optlen, hdrlen, off;
260 void *data;
Tom Herberta8d31c12014-11-04 09:06:57 -0800261 u16 doffset = 0;
Tom Herbert37dd0242014-10-03 15:48:09 -0700262 int flush = 1;
Tom Herbertfe881ef2015-02-10 16:30:33 -0800263 struct fou *fou = container_of(uoff, struct fou, udp_offloads);
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800264 struct gro_remcsum grc;
265
266 skb_gro_remcsum_init(&grc);
Tom Herbert37dd0242014-10-03 15:48:09 -0700267
268 off = skb_gro_offset(skb);
Tom Herbert5024c332014-11-04 09:06:53 -0800269 len = off + sizeof(*guehdr);
270
Tom Herbert37dd0242014-10-03 15:48:09 -0700271 guehdr = skb_gro_header_fast(skb, off);
Tom Herbert5024c332014-11-04 09:06:53 -0800272 if (skb_gro_header_hard(skb, len)) {
273 guehdr = skb_gro_header_slow(skb, len, off);
Tom Herbert37dd0242014-10-03 15:48:09 -0700274 if (unlikely(!guehdr))
275 goto out;
276 }
277
Tom Herbert5024c332014-11-04 09:06:53 -0800278 optlen = guehdr->hlen << 2;
279 len += optlen;
Tom Herbert37dd0242014-10-03 15:48:09 -0700280
Tom Herbert5024c332014-11-04 09:06:53 -0800281 if (skb_gro_header_hard(skb, len)) {
282 guehdr = skb_gro_header_slow(skb, len, off);
Tom Herbert37dd0242014-10-03 15:48:09 -0700283 if (unlikely(!guehdr))
Tom Herbert5024c332014-11-04 09:06:53 -0800284 goto out;
285 }
286
287 if (unlikely(guehdr->control) || guehdr->version != 0 ||
288 validate_gue_flags(guehdr, optlen))
289 goto out;
290
291 hdrlen = sizeof(*guehdr) + optlen;
292
Tom Herberta8d31c12014-11-04 09:06:57 -0800293 /* Adjust NAPI_GRO_CB(skb)->csum to account for guehdr,
294 * this is needed if there is a remote checkcsum offload.
295 */
Tom Herbert5024c332014-11-04 09:06:53 -0800296 skb_gro_postpull_rcsum(skb, guehdr, hdrlen);
297
298 data = &guehdr[1];
299
300 if (guehdr->flags & GUE_FLAG_PRIV) {
Tom Herberta8d31c12014-11-04 09:06:57 -0800301 __be32 flags = *(__be32 *)(data + doffset);
Tom Herbert5024c332014-11-04 09:06:53 -0800302
Tom Herberta8d31c12014-11-04 09:06:57 -0800303 doffset += GUE_LEN_PRIV;
304
305 if (flags & GUE_PFLAG_REMCSUM) {
306 guehdr = gue_gro_remcsum(skb, off, guehdr,
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700307 data + doffset, hdrlen, &grc,
Tom Herbertfe881ef2015-02-10 16:30:33 -0800308 !!(fou->flags &
309 FOU_F_REMCSUM_NOPARTIAL));
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700310
Tom Herberta8d31c12014-11-04 09:06:57 -0800311 if (!guehdr)
312 goto out;
313
314 data = &guehdr[1];
315
316 doffset += GUE_PLEN_REMCSUM;
317 }
Tom Herbert37dd0242014-10-03 15:48:09 -0700318 }
319
Tom Herberta8d31c12014-11-04 09:06:57 -0800320 skb_gro_pull(skb, hdrlen);
321
Tom Herbert37dd0242014-10-03 15:48:09 -0700322 flush = 0;
323
324 for (p = *head; p; p = p->next) {
325 const struct guehdr *guehdr2;
326
327 if (!NAPI_GRO_CB(p)->same_flow)
328 continue;
329
330 guehdr2 = (struct guehdr *)(p->data + off);
331
332 /* Compare base GUE header to be equal (covers
Tom Herbert5024c332014-11-04 09:06:53 -0800333 * hlen, version, proto_ctype, and flags.
Tom Herbert37dd0242014-10-03 15:48:09 -0700334 */
335 if (guehdr->word != guehdr2->word) {
336 NAPI_GRO_CB(p)->same_flow = 0;
337 continue;
338 }
339
340 /* Compare optional fields are the same. */
341 if (guehdr->hlen && memcmp(&guehdr[1], &guehdr2[1],
342 guehdr->hlen << 2)) {
343 NAPI_GRO_CB(p)->same_flow = 0;
344 continue;
345 }
346 }
347
Tom Herbert5024c332014-11-04 09:06:53 -0800348 rcu_read_lock();
349 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
350 ops = rcu_dereference(offloads[guehdr->proto_ctype]);
Tom Herbert27013662015-08-19 17:07:34 -0700351 if (WARN_ON_ONCE(!ops || !ops->callbacks.gro_receive))
Tom Herbert5024c332014-11-04 09:06:53 -0800352 goto out_unlock;
Tom Herbert37dd0242014-10-03 15:48:09 -0700353
354 pp = ops->callbacks.gro_receive(head, skb);
355
356out_unlock:
357 rcu_read_unlock();
358out:
359 NAPI_GRO_CB(skb)->flush |= flush;
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800360 skb_gro_remcsum_cleanup(skb, &grc);
Tom Herbert37dd0242014-10-03 15:48:09 -0700361
362 return pp;
363}
364
Tom Herberta2b12f32015-01-12 17:00:37 -0800365static int gue_gro_complete(struct sk_buff *skb, int nhoff,
366 struct udp_offload *uoff)
Tom Herbert37dd0242014-10-03 15:48:09 -0700367{
368 const struct net_offload **offloads;
369 struct guehdr *guehdr = (struct guehdr *)(skb->data + nhoff);
370 const struct net_offload *ops;
371 unsigned int guehlen;
372 u8 proto;
373 int err = -ENOENT;
374
Tom Herbert5024c332014-11-04 09:06:53 -0800375 proto = guehdr->proto_ctype;
Tom Herbert37dd0242014-10-03 15:48:09 -0700376
377 guehlen = sizeof(*guehdr) + (guehdr->hlen << 2);
378
379 rcu_read_lock();
380 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
381 ops = rcu_dereference(offloads[proto]);
382 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
383 goto out_unlock;
384
385 err = ops->callbacks.gro_complete(skb, nhoff + guehlen);
386
387out_unlock:
388 rcu_read_unlock();
389 return err;
390}
391
WANG Cong02d793c2015-04-10 12:00:29 -0700392static int fou_add_to_port_list(struct net *net, struct fou *fou)
Tom Herbert23461552014-09-17 12:25:56 -0700393{
WANG Cong02d793c2015-04-10 12:00:29 -0700394 struct fou_net *fn = net_generic(net, fou_net_id);
Tom Herbert23461552014-09-17 12:25:56 -0700395 struct fou *fout;
396
WANG Cong02d793c2015-04-10 12:00:29 -0700397 mutex_lock(&fn->fou_lock);
398 list_for_each_entry(fout, &fn->fou_list, list) {
Tom Herbert23461552014-09-17 12:25:56 -0700399 if (fou->port == fout->port) {
WANG Cong02d793c2015-04-10 12:00:29 -0700400 mutex_unlock(&fn->fou_lock);
Tom Herbert23461552014-09-17 12:25:56 -0700401 return -EALREADY;
402 }
403 }
404
WANG Cong02d793c2015-04-10 12:00:29 -0700405 list_add(&fou->list, &fn->fou_list);
406 mutex_unlock(&fn->fou_lock);
Tom Herbert23461552014-09-17 12:25:56 -0700407
408 return 0;
409}
410
411static void fou_release(struct fou *fou)
412{
413 struct socket *sock = fou->sock;
414 struct sock *sk = sock->sk;
415
WANG Cong9272f042015-04-10 12:00:26 -0700416 if (sk->sk_family == AF_INET)
417 udp_del_offload(&fou->udp_offloads);
Tom Herbert23461552014-09-17 12:25:56 -0700418 list_del(&fou->list);
WANG Cong02d793c2015-04-10 12:00:29 -0700419 udp_tunnel_sock_release(sock);
Tom Herbert23461552014-09-17 12:25:56 -0700420
Hannes Frederic Sowa3036fac2015-12-15 21:01:53 +0100421 kfree_rcu(fou, rcu);
Tom Herbert23461552014-09-17 12:25:56 -0700422}
423
Tom Herbert37dd0242014-10-03 15:48:09 -0700424static int fou_encap_init(struct sock *sk, struct fou *fou, struct fou_cfg *cfg)
425{
426 udp_sk(sk)->encap_rcv = fou_udp_recv;
427 fou->protocol = cfg->protocol;
428 fou->udp_offloads.callbacks.gro_receive = fou_gro_receive;
429 fou->udp_offloads.callbacks.gro_complete = fou_gro_complete;
430 fou->udp_offloads.port = cfg->udp_config.local_udp_port;
431 fou->udp_offloads.ipproto = cfg->protocol;
432
433 return 0;
434}
435
436static int gue_encap_init(struct sock *sk, struct fou *fou, struct fou_cfg *cfg)
437{
438 udp_sk(sk)->encap_rcv = gue_udp_recv;
439 fou->udp_offloads.callbacks.gro_receive = gue_gro_receive;
440 fou->udp_offloads.callbacks.gro_complete = gue_gro_complete;
441 fou->udp_offloads.port = cfg->udp_config.local_udp_port;
442
443 return 0;
444}
445
Tom Herbert23461552014-09-17 12:25:56 -0700446static int fou_create(struct net *net, struct fou_cfg *cfg,
447 struct socket **sockp)
448{
Tom Herbert23461552014-09-17 12:25:56 -0700449 struct socket *sock = NULL;
WANG Cong02d793c2015-04-10 12:00:29 -0700450 struct fou *fou = NULL;
Tom Herbert23461552014-09-17 12:25:56 -0700451 struct sock *sk;
WANG Cong02d793c2015-04-10 12:00:29 -0700452 int err;
Tom Herbert23461552014-09-17 12:25:56 -0700453
454 /* Open UDP socket */
455 err = udp_sock_create(net, &cfg->udp_config, &sock);
456 if (err < 0)
457 goto error;
458
459 /* Allocate FOU port structure */
460 fou = kzalloc(sizeof(*fou), GFP_KERNEL);
461 if (!fou) {
462 err = -ENOMEM;
463 goto error;
464 }
465
466 sk = sock->sk;
467
Tom Herbertfe881ef2015-02-10 16:30:33 -0800468 fou->flags = cfg->flags;
Tom Herbert37dd0242014-10-03 15:48:09 -0700469 fou->port = cfg->udp_config.local_udp_port;
470
471 /* Initial for fou type */
472 switch (cfg->type) {
473 case FOU_ENCAP_DIRECT:
474 err = fou_encap_init(sk, fou, cfg);
475 if (err)
476 goto error;
477 break;
478 case FOU_ENCAP_GUE:
479 err = gue_encap_init(sk, fou, cfg);
480 if (err)
481 goto error;
482 break;
483 default:
484 err = -EINVAL;
485 goto error;
486 }
Tom Herbert23461552014-09-17 12:25:56 -0700487
WANG Cong7a6c8c32015-04-10 12:00:30 -0700488 fou->type = cfg->type;
489
Tom Herbert23461552014-09-17 12:25:56 -0700490 udp_sk(sk)->encap_type = 1;
491 udp_encap_enable();
492
493 sk->sk_user_data = fou;
494 fou->sock = sock;
495
Tom Herbert224d0192015-01-05 13:56:14 -0800496 inet_inc_convert_csum(sk);
Tom Herbert23461552014-09-17 12:25:56 -0700497
498 sk->sk_allocation = GFP_ATOMIC;
499
Tom Herbertafe93322014-09-17 12:25:57 -0700500 if (cfg->udp_config.family == AF_INET) {
Hannes Frederic Sowa787d7ac2016-01-07 14:28:39 +0100501 err = udp_add_offload(net, &fou->udp_offloads);
Tom Herbertafe93322014-09-17 12:25:57 -0700502 if (err)
503 goto error;
504 }
505
WANG Cong02d793c2015-04-10 12:00:29 -0700506 err = fou_add_to_port_list(net, fou);
Tom Herbert23461552014-09-17 12:25:56 -0700507 if (err)
508 goto error;
509
510 if (sockp)
511 *sockp = sock;
512
513 return 0;
514
515error:
516 kfree(fou);
517 if (sock)
WANG Cong02d793c2015-04-10 12:00:29 -0700518 udp_tunnel_sock_release(sock);
Tom Herbert23461552014-09-17 12:25:56 -0700519
520 return err;
521}
522
523static int fou_destroy(struct net *net, struct fou_cfg *cfg)
524{
WANG Cong02d793c2015-04-10 12:00:29 -0700525 struct fou_net *fn = net_generic(net, fou_net_id);
WANG Cong4cbcdf22015-04-10 12:00:28 -0700526 __be16 port = cfg->udp_config.local_udp_port;
Tom Herbert23461552014-09-17 12:25:56 -0700527 int err = -EINVAL;
WANG Cong02d793c2015-04-10 12:00:29 -0700528 struct fou *fou;
Tom Herbert23461552014-09-17 12:25:56 -0700529
WANG Cong02d793c2015-04-10 12:00:29 -0700530 mutex_lock(&fn->fou_lock);
531 list_for_each_entry(fou, &fn->fou_list, list) {
Tom Herbert23461552014-09-17 12:25:56 -0700532 if (fou->port == port) {
Tom Herbert23461552014-09-17 12:25:56 -0700533 fou_release(fou);
534 err = 0;
535 break;
536 }
537 }
WANG Cong02d793c2015-04-10 12:00:29 -0700538 mutex_unlock(&fn->fou_lock);
Tom Herbert23461552014-09-17 12:25:56 -0700539
540 return err;
541}
542
543static struct genl_family fou_nl_family = {
544 .id = GENL_ID_GENERATE,
545 .hdrsize = 0,
546 .name = FOU_GENL_NAME,
547 .version = FOU_GENL_VERSION,
548 .maxattr = FOU_ATTR_MAX,
549 .netnsok = true,
550};
551
552static struct nla_policy fou_nl_policy[FOU_ATTR_MAX + 1] = {
553 [FOU_ATTR_PORT] = { .type = NLA_U16, },
554 [FOU_ATTR_AF] = { .type = NLA_U8, },
555 [FOU_ATTR_IPPROTO] = { .type = NLA_U8, },
Tom Herbert37dd0242014-10-03 15:48:09 -0700556 [FOU_ATTR_TYPE] = { .type = NLA_U8, },
Tom Herbertfe881ef2015-02-10 16:30:33 -0800557 [FOU_ATTR_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG, },
Tom Herbert23461552014-09-17 12:25:56 -0700558};
559
560static int parse_nl_config(struct genl_info *info,
561 struct fou_cfg *cfg)
562{
563 memset(cfg, 0, sizeof(*cfg));
564
565 cfg->udp_config.family = AF_INET;
566
567 if (info->attrs[FOU_ATTR_AF]) {
568 u8 family = nla_get_u8(info->attrs[FOU_ATTR_AF]);
569
Jiri Bencb9b66952015-08-28 20:48:21 +0200570 if (family != AF_INET)
Tom Herbert23461552014-09-17 12:25:56 -0700571 return -EINVAL;
572
573 cfg->udp_config.family = family;
574 }
575
576 if (info->attrs[FOU_ATTR_PORT]) {
WANG Cong4cbcdf22015-04-10 12:00:28 -0700577 __be16 port = nla_get_be16(info->attrs[FOU_ATTR_PORT]);
Tom Herbert23461552014-09-17 12:25:56 -0700578
579 cfg->udp_config.local_udp_port = port;
580 }
581
582 if (info->attrs[FOU_ATTR_IPPROTO])
583 cfg->protocol = nla_get_u8(info->attrs[FOU_ATTR_IPPROTO]);
584
Tom Herbert37dd0242014-10-03 15:48:09 -0700585 if (info->attrs[FOU_ATTR_TYPE])
586 cfg->type = nla_get_u8(info->attrs[FOU_ATTR_TYPE]);
587
Tom Herbertfe881ef2015-02-10 16:30:33 -0800588 if (info->attrs[FOU_ATTR_REMCSUM_NOPARTIAL])
589 cfg->flags |= FOU_F_REMCSUM_NOPARTIAL;
590
Tom Herbert23461552014-09-17 12:25:56 -0700591 return 0;
592}
593
594static int fou_nl_cmd_add_port(struct sk_buff *skb, struct genl_info *info)
595{
WANG Cong02d793c2015-04-10 12:00:29 -0700596 struct net *net = genl_info_net(info);
Tom Herbert23461552014-09-17 12:25:56 -0700597 struct fou_cfg cfg;
598 int err;
599
600 err = parse_nl_config(info, &cfg);
601 if (err)
602 return err;
603
WANG Cong02d793c2015-04-10 12:00:29 -0700604 return fou_create(net, &cfg, NULL);
Tom Herbert23461552014-09-17 12:25:56 -0700605}
606
607static int fou_nl_cmd_rm_port(struct sk_buff *skb, struct genl_info *info)
608{
WANG Cong02d793c2015-04-10 12:00:29 -0700609 struct net *net = genl_info_net(info);
Tom Herbert23461552014-09-17 12:25:56 -0700610 struct fou_cfg cfg;
WANG Cong67270632015-04-10 12:00:27 -0700611 int err;
Tom Herbert23461552014-09-17 12:25:56 -0700612
WANG Cong67270632015-04-10 12:00:27 -0700613 err = parse_nl_config(info, &cfg);
614 if (err)
615 return err;
Tom Herbert23461552014-09-17 12:25:56 -0700616
WANG Cong02d793c2015-04-10 12:00:29 -0700617 return fou_destroy(net, &cfg);
Tom Herbert23461552014-09-17 12:25:56 -0700618}
619
WANG Cong7a6c8c32015-04-10 12:00:30 -0700620static int fou_fill_info(struct fou *fou, struct sk_buff *msg)
621{
622 if (nla_put_u8(msg, FOU_ATTR_AF, fou->sock->sk->sk_family) ||
623 nla_put_be16(msg, FOU_ATTR_PORT, fou->port) ||
624 nla_put_u8(msg, FOU_ATTR_IPPROTO, fou->protocol) ||
625 nla_put_u8(msg, FOU_ATTR_TYPE, fou->type))
626 return -1;
627
628 if (fou->flags & FOU_F_REMCSUM_NOPARTIAL)
629 if (nla_put_flag(msg, FOU_ATTR_REMCSUM_NOPARTIAL))
630 return -1;
631 return 0;
632}
633
634static int fou_dump_info(struct fou *fou, u32 portid, u32 seq,
635 u32 flags, struct sk_buff *skb, u8 cmd)
636{
637 void *hdr;
638
639 hdr = genlmsg_put(skb, portid, seq, &fou_nl_family, flags, cmd);
640 if (!hdr)
641 return -ENOMEM;
642
643 if (fou_fill_info(fou, skb) < 0)
644 goto nla_put_failure;
645
646 genlmsg_end(skb, hdr);
647 return 0;
648
649nla_put_failure:
650 genlmsg_cancel(skb, hdr);
651 return -EMSGSIZE;
652}
653
654static int fou_nl_cmd_get_port(struct sk_buff *skb, struct genl_info *info)
655{
656 struct net *net = genl_info_net(info);
657 struct fou_net *fn = net_generic(net, fou_net_id);
658 struct sk_buff *msg;
659 struct fou_cfg cfg;
660 struct fou *fout;
661 __be16 port;
662 int ret;
663
664 ret = parse_nl_config(info, &cfg);
665 if (ret)
666 return ret;
667 port = cfg.udp_config.local_udp_port;
668 if (port == 0)
669 return -EINVAL;
670
671 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
672 if (!msg)
673 return -ENOMEM;
674
675 ret = -ESRCH;
676 mutex_lock(&fn->fou_lock);
677 list_for_each_entry(fout, &fn->fou_list, list) {
678 if (port == fout->port) {
679 ret = fou_dump_info(fout, info->snd_portid,
680 info->snd_seq, 0, msg,
681 info->genlhdr->cmd);
682 break;
683 }
684 }
685 mutex_unlock(&fn->fou_lock);
686 if (ret < 0)
687 goto out_free;
688
689 return genlmsg_reply(msg, info);
690
691out_free:
692 nlmsg_free(msg);
693 return ret;
694}
695
696static int fou_nl_dump(struct sk_buff *skb, struct netlink_callback *cb)
697{
698 struct net *net = sock_net(skb->sk);
699 struct fou_net *fn = net_generic(net, fou_net_id);
700 struct fou *fout;
701 int idx = 0, ret;
702
703 mutex_lock(&fn->fou_lock);
704 list_for_each_entry(fout, &fn->fou_list, list) {
705 if (idx++ < cb->args[0])
706 continue;
707 ret = fou_dump_info(fout, NETLINK_CB(cb->skb).portid,
708 cb->nlh->nlmsg_seq, NLM_F_MULTI,
709 skb, FOU_CMD_GET);
710 if (ret)
WANG Cong540207a2015-04-15 11:48:49 -0700711 break;
WANG Cong7a6c8c32015-04-10 12:00:30 -0700712 }
713 mutex_unlock(&fn->fou_lock);
714
WANG Cong7a6c8c32015-04-10 12:00:30 -0700715 cb->args[0] = idx;
716 return skb->len;
717}
718
Tom Herbert23461552014-09-17 12:25:56 -0700719static const struct genl_ops fou_nl_ops[] = {
720 {
721 .cmd = FOU_CMD_ADD,
722 .doit = fou_nl_cmd_add_port,
723 .policy = fou_nl_policy,
724 .flags = GENL_ADMIN_PERM,
725 },
726 {
727 .cmd = FOU_CMD_DEL,
728 .doit = fou_nl_cmd_rm_port,
729 .policy = fou_nl_policy,
730 .flags = GENL_ADMIN_PERM,
731 },
WANG Cong7a6c8c32015-04-10 12:00:30 -0700732 {
733 .cmd = FOU_CMD_GET,
734 .doit = fou_nl_cmd_get_port,
735 .dumpit = fou_nl_dump,
736 .policy = fou_nl_policy,
737 },
Tom Herbert23461552014-09-17 12:25:56 -0700738};
739
Tom Herberta8c5f902014-11-12 11:54:09 -0800740size_t fou_encap_hlen(struct ip_tunnel_encap *e)
741{
742 return sizeof(struct udphdr);
743}
744EXPORT_SYMBOL(fou_encap_hlen);
745
746size_t gue_encap_hlen(struct ip_tunnel_encap *e)
747{
748 size_t len;
749 bool need_priv = false;
750
751 len = sizeof(struct udphdr) + sizeof(struct guehdr);
752
753 if (e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) {
754 len += GUE_PLEN_REMCSUM;
755 need_priv = true;
756 }
757
758 len += need_priv ? GUE_LEN_PRIV : 0;
759
760 return len;
761}
762EXPORT_SYMBOL(gue_encap_hlen);
763
Tom Herbert63487ba2014-11-04 09:06:51 -0800764static void fou_build_udp(struct sk_buff *skb, struct ip_tunnel_encap *e,
765 struct flowi4 *fl4, u8 *protocol, __be16 sport)
766{
767 struct udphdr *uh;
768
769 skb_push(skb, sizeof(struct udphdr));
770 skb_reset_transport_header(skb);
771
772 uh = udp_hdr(skb);
773
774 uh->dest = e->dport;
775 uh->source = sport;
776 uh->len = htons(skb->len);
Tom Herbert63487ba2014-11-04 09:06:51 -0800777 udp_set_csum(!(e->flags & TUNNEL_ENCAP_FLAG_CSUM), skb,
778 fl4->saddr, fl4->daddr, skb->len);
779
780 *protocol = IPPROTO_UDP;
781}
782
783int fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
784 u8 *protocol, struct flowi4 *fl4)
785{
Edward Cree06f62292016-02-11 21:00:16 +0000786 int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM ? SKB_GSO_UDP_TUNNEL_CSUM :
787 SKB_GSO_UDP_TUNNEL;
Tom Herbert63487ba2014-11-04 09:06:51 -0800788 __be16 sport;
789
Edward Cree6fa79662016-02-11 21:02:31 +0000790 skb = iptunnel_handle_offloads(skb, type);
Tom Herbert63487ba2014-11-04 09:06:51 -0800791
792 if (IS_ERR(skb))
793 return PTR_ERR(skb);
794
795 sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
796 skb, 0, 0, false);
797 fou_build_udp(skb, e, fl4, protocol, sport);
798
799 return 0;
800}
801EXPORT_SYMBOL(fou_build_header);
802
803int gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
804 u8 *protocol, struct flowi4 *fl4)
805{
Edward Cree06f62292016-02-11 21:00:16 +0000806 int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM ? SKB_GSO_UDP_TUNNEL_CSUM :
807 SKB_GSO_UDP_TUNNEL;
Tom Herbert63487ba2014-11-04 09:06:51 -0800808 struct guehdr *guehdr;
Tom Herbertb17f7092014-11-04 09:06:56 -0800809 size_t hdrlen, optlen = 0;
Tom Herbert63487ba2014-11-04 09:06:51 -0800810 __be16 sport;
Tom Herbert5024c332014-11-04 09:06:53 -0800811 void *data;
812 bool need_priv = false;
813
Tom Herbertb17f7092014-11-04 09:06:56 -0800814 if ((e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) &&
815 skb->ip_summed == CHECKSUM_PARTIAL) {
Tom Herbertb17f7092014-11-04 09:06:56 -0800816 optlen += GUE_PLEN_REMCSUM;
817 type |= SKB_GSO_TUNNEL_REMCSUM;
818 need_priv = true;
819 }
820
Tom Herbert5024c332014-11-04 09:06:53 -0800821 optlen += need_priv ? GUE_LEN_PRIV : 0;
Tom Herbert63487ba2014-11-04 09:06:51 -0800822
Edward Cree6fa79662016-02-11 21:02:31 +0000823 skb = iptunnel_handle_offloads(skb, type);
Tom Herbert63487ba2014-11-04 09:06:51 -0800824
825 if (IS_ERR(skb))
826 return PTR_ERR(skb);
827
828 /* Get source port (based on flow hash) before skb_push */
829 sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
830 skb, 0, 0, false);
831
Tom Herbertb17f7092014-11-04 09:06:56 -0800832 hdrlen = sizeof(struct guehdr) + optlen;
833
834 skb_push(skb, hdrlen);
Tom Herbert63487ba2014-11-04 09:06:51 -0800835
836 guehdr = (struct guehdr *)skb->data;
837
Tom Herbert5024c332014-11-04 09:06:53 -0800838 guehdr->control = 0;
Tom Herbert63487ba2014-11-04 09:06:51 -0800839 guehdr->version = 0;
Tom Herbert5024c332014-11-04 09:06:53 -0800840 guehdr->hlen = optlen >> 2;
Tom Herbert63487ba2014-11-04 09:06:51 -0800841 guehdr->flags = 0;
Tom Herbert5024c332014-11-04 09:06:53 -0800842 guehdr->proto_ctype = *protocol;
843
844 data = &guehdr[1];
845
846 if (need_priv) {
847 __be32 *flags = data;
848
849 guehdr->flags |= GUE_FLAG_PRIV;
850 *flags = 0;
851 data += GUE_LEN_PRIV;
852
Tom Herbertb17f7092014-11-04 09:06:56 -0800853 if (type & SKB_GSO_TUNNEL_REMCSUM) {
854 u16 csum_start = skb_checksum_start_offset(skb);
855 __be16 *pd = data;
856
857 if (csum_start < hdrlen)
858 return -EINVAL;
859
860 csum_start -= hdrlen;
861 pd[0] = htons(csum_start);
862 pd[1] = htons(csum_start + skb->csum_offset);
863
864 if (!skb_is_gso(skb)) {
865 skb->ip_summed = CHECKSUM_NONE;
866 skb->encapsulation = 0;
867 }
868
869 *flags |= GUE_PFLAG_REMCSUM;
870 data += GUE_PLEN_REMCSUM;
871 }
872
Tom Herbert5024c332014-11-04 09:06:53 -0800873 }
Tom Herbert63487ba2014-11-04 09:06:51 -0800874
875 fou_build_udp(skb, e, fl4, protocol, sport);
876
877 return 0;
878}
879EXPORT_SYMBOL(gue_build_header);
880
Tom Herberta8c5f902014-11-12 11:54:09 -0800881#ifdef CONFIG_NET_FOU_IP_TUNNELS
882
Andi Kleen5eeb2922015-04-08 06:04:31 -0700883static const struct ip_tunnel_encap_ops fou_iptun_ops = {
Tom Herberta8c5f902014-11-12 11:54:09 -0800884 .encap_hlen = fou_encap_hlen,
885 .build_header = fou_build_header,
886};
887
Andi Kleen5eeb2922015-04-08 06:04:31 -0700888static const struct ip_tunnel_encap_ops gue_iptun_ops = {
Tom Herberta8c5f902014-11-12 11:54:09 -0800889 .encap_hlen = gue_encap_hlen,
890 .build_header = gue_build_header,
891};
892
893static int ip_tunnel_encap_add_fou_ops(void)
894{
895 int ret;
896
897 ret = ip_tunnel_encap_add_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
898 if (ret < 0) {
899 pr_err("can't add fou ops\n");
900 return ret;
901 }
902
903 ret = ip_tunnel_encap_add_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
904 if (ret < 0) {
905 pr_err("can't add gue ops\n");
906 ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
907 return ret;
908 }
909
910 return 0;
911}
912
913static void ip_tunnel_encap_del_fou_ops(void)
914{
915 ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
916 ip_tunnel_encap_del_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
917}
918
919#else
920
921static int ip_tunnel_encap_add_fou_ops(void)
922{
923 return 0;
924}
925
Thomas Graf882288c2014-11-13 12:48:21 +0100926static void ip_tunnel_encap_del_fou_ops(void)
Tom Herberta8c5f902014-11-12 11:54:09 -0800927{
928}
929
930#endif
931
WANG Cong02d793c2015-04-10 12:00:29 -0700932static __net_init int fou_init_net(struct net *net)
933{
934 struct fou_net *fn = net_generic(net, fou_net_id);
935
936 INIT_LIST_HEAD(&fn->fou_list);
937 mutex_init(&fn->fou_lock);
938 return 0;
939}
940
941static __net_exit void fou_exit_net(struct net *net)
942{
943 struct fou_net *fn = net_generic(net, fou_net_id);
944 struct fou *fou, *next;
945
946 /* Close all the FOU sockets */
947 mutex_lock(&fn->fou_lock);
948 list_for_each_entry_safe(fou, next, &fn->fou_list, list)
949 fou_release(fou);
950 mutex_unlock(&fn->fou_lock);
951}
952
953static struct pernet_operations fou_net_ops = {
954 .init = fou_init_net,
955 .exit = fou_exit_net,
956 .id = &fou_net_id,
957 .size = sizeof(struct fou_net),
958};
959
Tom Herbert23461552014-09-17 12:25:56 -0700960static int __init fou_init(void)
961{
962 int ret;
963
WANG Cong02d793c2015-04-10 12:00:29 -0700964 ret = register_pernet_device(&fou_net_ops);
965 if (ret)
Tom Herberta8c5f902014-11-12 11:54:09 -0800966 goto exit;
967
WANG Cong02d793c2015-04-10 12:00:29 -0700968 ret = genl_register_family_with_ops(&fou_nl_family,
969 fou_nl_ops);
Tom Herberta8c5f902014-11-12 11:54:09 -0800970 if (ret < 0)
WANG Cong02d793c2015-04-10 12:00:29 -0700971 goto unregister;
Tom Herberta8c5f902014-11-12 11:54:09 -0800972
WANG Cong02d793c2015-04-10 12:00:29 -0700973 ret = ip_tunnel_encap_add_fou_ops();
974 if (ret == 0)
975 return 0;
976
977 genl_unregister_family(&fou_nl_family);
978unregister:
979 unregister_pernet_device(&fou_net_ops);
Tom Herberta8c5f902014-11-12 11:54:09 -0800980exit:
Tom Herbert23461552014-09-17 12:25:56 -0700981 return ret;
982}
983
984static void __exit fou_fini(void)
985{
Tom Herberta8c5f902014-11-12 11:54:09 -0800986 ip_tunnel_encap_del_fou_ops();
Tom Herbert23461552014-09-17 12:25:56 -0700987 genl_unregister_family(&fou_nl_family);
WANG Cong02d793c2015-04-10 12:00:29 -0700988 unregister_pernet_device(&fou_net_ops);
Tom Herbert23461552014-09-17 12:25:56 -0700989}
990
991module_init(fou_init);
992module_exit(fou_fini);
993MODULE_AUTHOR("Tom Herbert <therbert@google.com>");
994MODULE_LICENSE("GPL");