blob: 5f9207c039e704e77c52fcf840e56a46b6b899fd [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;
Tom Herbert5f914b62016-05-18 09:06:16 -070024 u8 family;
WANG Cong7a6c8c32015-04-10 12:00:30 -070025 u16 type;
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 Herbert5f914b62016-05-18 09:06:16 -070051static int fou_recv_pull(struct sk_buff *skb, struct fou *fou, size_t len)
Tom Herbert23461552014-09-17 12:25:56 -070052{
Tom Herbert23461552014-09-17 12:25:56 -070053 /* Remove 'len' bytes from the packet (UDP header and
Tom Herbert5024c332014-11-04 09:06:53 -080054 * FOU header if present).
Tom Herbert23461552014-09-17 12:25:56 -070055 */
Tom Herbert5f914b62016-05-18 09:06:16 -070056 if (fou->family == AF_INET)
57 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(skb)->tot_len) - len);
58 else
59 ipv6_hdr(skb)->payload_len =
60 htons(ntohs(ipv6_hdr(skb)->payload_len) - len);
61
Tom Herbert23461552014-09-17 12:25:56 -070062 __skb_pull(skb, len);
63 skb_postpull_rcsum(skb, udp_hdr(skb), len);
64 skb_reset_transport_header(skb);
Jesse Grossa09a4c82016-03-19 09:32:02 -070065 return iptunnel_pull_offloads(skb);
Tom Herbert23461552014-09-17 12:25:56 -070066}
67
68static int fou_udp_recv(struct sock *sk, struct sk_buff *skb)
69{
70 struct fou *fou = fou_from_sock(sk);
71
72 if (!fou)
73 return 1;
74
Tom Herbert5f914b62016-05-18 09:06:16 -070075 if (fou_recv_pull(skb, fou, sizeof(struct udphdr)))
Jesse Grossa09a4c82016-03-19 09:32:02 -070076 goto drop;
Tom Herbert5024c332014-11-04 09:06:53 -080077
78 return -fou->protocol;
Jesse Grossa09a4c82016-03-19 09:32:02 -070079
80drop:
81 kfree_skb(skb);
82 return 0;
Tom Herbert5024c332014-11-04 09:06:53 -080083}
84
Tom Herberta8d31c12014-11-04 09:06:57 -080085static struct guehdr *gue_remcsum(struct sk_buff *skb, struct guehdr *guehdr,
Tom Herbertfe881ef2015-02-10 16:30:33 -080086 void *data, size_t hdrlen, u8 ipproto,
87 bool nopartial)
Tom Herberta8d31c12014-11-04 09:06:57 -080088{
89 __be16 *pd = data;
Tom Herbert4fd671d2014-11-25 11:21:20 -080090 size_t start = ntohs(pd[0]);
91 size_t offset = ntohs(pd[1]);
Tom Herbertb7fe10e2015-08-19 17:07:32 -070092 size_t plen = sizeof(struct udphdr) + hdrlen +
93 max_t(size_t, offset + sizeof(u16), start);
94
95 if (skb->remcsum_offload)
96 return guehdr;
Tom Herberta8d31c12014-11-04 09:06:57 -080097
Tom Herberta8d31c12014-11-04 09:06:57 -080098 if (!pskb_may_pull(skb, plen))
99 return NULL;
100 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
101
Tom Herbertfe881ef2015-02-10 16:30:33 -0800102 skb_remcsum_process(skb, (void *)guehdr + hdrlen,
103 start, offset, nopartial);
Tom Herberta8d31c12014-11-04 09:06:57 -0800104
105 return guehdr;
106}
107
Tom Herbert5024c332014-11-04 09:06:53 -0800108static int gue_control_message(struct sk_buff *skb, struct guehdr *guehdr)
109{
110 /* No support yet */
111 kfree_skb(skb);
112 return 0;
Tom Herbert23461552014-09-17 12:25:56 -0700113}
114
Tom Herbert37dd0242014-10-03 15:48:09 -0700115static int gue_udp_recv(struct sock *sk, struct sk_buff *skb)
116{
117 struct fou *fou = fou_from_sock(sk);
Tom Herbert5024c332014-11-04 09:06:53 -0800118 size_t len, optlen, hdrlen;
Tom Herbert37dd0242014-10-03 15:48:09 -0700119 struct guehdr *guehdr;
Tom Herbert5024c332014-11-04 09:06:53 -0800120 void *data;
Tom Herberta8d31c12014-11-04 09:06:57 -0800121 u16 doffset = 0;
Tom Herbert37dd0242014-10-03 15:48:09 -0700122
123 if (!fou)
124 return 1;
125
126 len = sizeof(struct udphdr) + sizeof(struct guehdr);
127 if (!pskb_may_pull(skb, len))
128 goto drop;
129
Tom Herbert5024c332014-11-04 09:06:53 -0800130 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
Tom Herbert37dd0242014-10-03 15:48:09 -0700131
Tom Herbert5024c332014-11-04 09:06:53 -0800132 optlen = guehdr->hlen << 2;
133 len += optlen;
134
Tom Herbert37dd0242014-10-03 15:48:09 -0700135 if (!pskb_may_pull(skb, len))
136 goto drop;
137
Tom Herbert5024c332014-11-04 09:06:53 -0800138 /* guehdr may change after pull */
139 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
Li RongQingd8f00d22014-10-17 16:53:47 +0800140
Tom Herbert5024c332014-11-04 09:06:53 -0800141 hdrlen = sizeof(struct guehdr) + optlen;
142
143 if (guehdr->version != 0 || validate_gue_flags(guehdr, optlen))
Tom Herbert37dd0242014-10-03 15:48:09 -0700144 goto drop;
145
Tom Herberta8d31c12014-11-04 09:06:57 -0800146 hdrlen = sizeof(struct guehdr) + optlen;
147
Tom Herbert5f914b62016-05-18 09:06:16 -0700148 if (fou->family == AF_INET)
149 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(skb)->tot_len) - len);
150 else
151 ipv6_hdr(skb)->payload_len =
152 htons(ntohs(ipv6_hdr(skb)->payload_len) - len);
Tom Herberta8d31c12014-11-04 09:06:57 -0800153
Tom Herberta8d31c12014-11-04 09:06:57 -0800154 /* Pull csum through the guehdr now . This can be used if
155 * there is a remote checksum offload.
156 */
157 skb_postpull_rcsum(skb, udp_hdr(skb), len);
Tom Herbert5024c332014-11-04 09:06:53 -0800158
159 data = &guehdr[1];
160
161 if (guehdr->flags & GUE_FLAG_PRIV) {
Tom Herberta8d31c12014-11-04 09:06:57 -0800162 __be32 flags = *(__be32 *)(data + doffset);
Tom Herbert5024c332014-11-04 09:06:53 -0800163
Tom Herberta8d31c12014-11-04 09:06:57 -0800164 doffset += GUE_LEN_PRIV;
165
166 if (flags & GUE_PFLAG_REMCSUM) {
167 guehdr = gue_remcsum(skb, guehdr, data + doffset,
Tom Herbertfe881ef2015-02-10 16:30:33 -0800168 hdrlen, guehdr->proto_ctype,
169 !!(fou->flags &
170 FOU_F_REMCSUM_NOPARTIAL));
Tom Herberta8d31c12014-11-04 09:06:57 -0800171 if (!guehdr)
172 goto drop;
173
174 data = &guehdr[1];
175
176 doffset += GUE_PLEN_REMCSUM;
177 }
Tom Herbert37dd0242014-10-03 15:48:09 -0700178 }
179
Tom Herbert5024c332014-11-04 09:06:53 -0800180 if (unlikely(guehdr->control))
181 return gue_control_message(skb, guehdr);
182
Tom Herbert4fd671d2014-11-25 11:21:20 -0800183 __skb_pull(skb, sizeof(struct udphdr) + hdrlen);
Tom Herberta8d31c12014-11-04 09:06:57 -0800184 skb_reset_transport_header(skb);
185
Jesse Grossa09a4c82016-03-19 09:32:02 -0700186 if (iptunnel_pull_offloads(skb))
187 goto drop;
188
Tom Herbert5024c332014-11-04 09:06:53 -0800189 return -guehdr->proto_ctype;
190
Tom Herbert37dd0242014-10-03 15:48:09 -0700191drop:
192 kfree_skb(skb);
193 return 0;
194}
195
Tom Herbertd92283e2016-04-05 08:22:54 -0700196static struct sk_buff **fou_gro_receive(struct sock *sk,
197 struct sk_buff **head,
198 struct sk_buff *skb)
Tom Herbertafe93322014-09-17 12:25:57 -0700199{
200 const struct net_offload *ops;
201 struct sk_buff **pp = NULL;
Tom Herbertd92283e2016-04-05 08:22:54 -0700202 u8 proto = fou_from_sock(sk)->protocol;
Tom Herbertefc98d02014-10-03 15:48:08 -0700203 const struct net_offload **offloads;
Tom Herbertafe93322014-09-17 12:25:57 -0700204
Alexander Duyckc3483382016-03-29 14:55:22 -0700205 /* We can clear the encap_mark for FOU as we are essentially doing
206 * one of two possible things. We are either adding an L4 tunnel
207 * header to the outer L3 tunnel header, or we are are simply
208 * treating the GRE tunnel header as though it is a UDP protocol
209 * specific header such as VXLAN or GENEVE.
210 */
211 NAPI_GRO_CB(skb)->encap_mark = 0;
212
Alexander Duycka0ca1532016-04-05 09:13:39 -0700213 /* Flag this frame as already having an outer encap header */
214 NAPI_GRO_CB(skb)->is_fou = 1;
215
Tom Herbertafe93322014-09-17 12:25:57 -0700216 rcu_read_lock();
Tom Herbertefc98d02014-10-03 15:48:08 -0700217 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
Tom Herbertafe93322014-09-17 12:25:57 -0700218 ops = rcu_dereference(offloads[proto]);
219 if (!ops || !ops->callbacks.gro_receive)
220 goto out_unlock;
221
222 pp = ops->callbacks.gro_receive(head, skb);
223
224out_unlock:
225 rcu_read_unlock();
226
227 return pp;
228}
229
Tom Herbertd92283e2016-04-05 08:22:54 -0700230static int fou_gro_complete(struct sock *sk, struct sk_buff *skb,
231 int nhoff)
Tom Herbertafe93322014-09-17 12:25:57 -0700232{
233 const struct net_offload *ops;
Tom Herbertd92283e2016-04-05 08:22:54 -0700234 u8 proto = fou_from_sock(sk)->protocol;
Tom Herbertafe93322014-09-17 12:25:57 -0700235 int err = -ENOSYS;
Tom Herbertefc98d02014-10-03 15:48:08 -0700236 const struct net_offload **offloads;
Tom Herbertafe93322014-09-17 12:25:57 -0700237
238 rcu_read_lock();
Tom Herbertefc98d02014-10-03 15:48:08 -0700239 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
Tom Herbertafe93322014-09-17 12:25:57 -0700240 ops = rcu_dereference(offloads[proto]);
241 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
242 goto out_unlock;
243
244 err = ops->callbacks.gro_complete(skb, nhoff);
245
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700246 skb_set_inner_mac_header(skb, nhoff);
247
Tom Herbertafe93322014-09-17 12:25:57 -0700248out_unlock:
249 rcu_read_unlock();
250
251 return err;
252}
253
Tom Herberta8d31c12014-11-04 09:06:57 -0800254static struct guehdr *gue_gro_remcsum(struct sk_buff *skb, unsigned int off,
255 struct guehdr *guehdr, void *data,
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700256 size_t hdrlen, struct gro_remcsum *grc,
257 bool nopartial)
Tom Herberta8d31c12014-11-04 09:06:57 -0800258{
259 __be16 *pd = data;
Tom Herbert4fd671d2014-11-25 11:21:20 -0800260 size_t start = ntohs(pd[0]);
261 size_t offset = ntohs(pd[1]);
Tom Herberta8d31c12014-11-04 09:06:57 -0800262
263 if (skb->remcsum_offload)
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700264 return guehdr;
Tom Herberta8d31c12014-11-04 09:06:57 -0800265
Tom Herbert4fd671d2014-11-25 11:21:20 -0800266 if (!NAPI_GRO_CB(skb)->csum_valid)
Tom Herberta8d31c12014-11-04 09:06:57 -0800267 return NULL;
268
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700269 guehdr = skb_gro_remcsum_process(skb, (void *)guehdr, off, hdrlen,
270 start, offset, grc, nopartial);
Tom Herberta8d31c12014-11-04 09:06:57 -0800271
272 skb->remcsum_offload = 1;
273
274 return guehdr;
275}
276
Tom Herbertd92283e2016-04-05 08:22:54 -0700277static struct sk_buff **gue_gro_receive(struct sock *sk,
278 struct sk_buff **head,
279 struct sk_buff *skb)
Tom Herbert37dd0242014-10-03 15:48:09 -0700280{
281 const struct net_offload **offloads;
282 const struct net_offload *ops;
283 struct sk_buff **pp = NULL;
284 struct sk_buff *p;
Tom Herbert37dd0242014-10-03 15:48:09 -0700285 struct guehdr *guehdr;
Tom Herbert5024c332014-11-04 09:06:53 -0800286 size_t len, optlen, hdrlen, off;
287 void *data;
Tom Herberta8d31c12014-11-04 09:06:57 -0800288 u16 doffset = 0;
Tom Herbert37dd0242014-10-03 15:48:09 -0700289 int flush = 1;
Tom Herbertd92283e2016-04-05 08:22:54 -0700290 struct fou *fou = fou_from_sock(sk);
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800291 struct gro_remcsum grc;
292
293 skb_gro_remcsum_init(&grc);
Tom Herbert37dd0242014-10-03 15:48:09 -0700294
295 off = skb_gro_offset(skb);
Tom Herbert5024c332014-11-04 09:06:53 -0800296 len = off + sizeof(*guehdr);
297
Tom Herbert37dd0242014-10-03 15:48:09 -0700298 guehdr = skb_gro_header_fast(skb, off);
Tom Herbert5024c332014-11-04 09:06:53 -0800299 if (skb_gro_header_hard(skb, len)) {
300 guehdr = skb_gro_header_slow(skb, len, off);
Tom Herbert37dd0242014-10-03 15:48:09 -0700301 if (unlikely(!guehdr))
302 goto out;
303 }
304
Tom Herbert5024c332014-11-04 09:06:53 -0800305 optlen = guehdr->hlen << 2;
306 len += optlen;
Tom Herbert37dd0242014-10-03 15:48:09 -0700307
Tom Herbert5024c332014-11-04 09:06:53 -0800308 if (skb_gro_header_hard(skb, len)) {
309 guehdr = skb_gro_header_slow(skb, len, off);
Tom Herbert37dd0242014-10-03 15:48:09 -0700310 if (unlikely(!guehdr))
Tom Herbert5024c332014-11-04 09:06:53 -0800311 goto out;
312 }
313
314 if (unlikely(guehdr->control) || guehdr->version != 0 ||
315 validate_gue_flags(guehdr, optlen))
316 goto out;
317
318 hdrlen = sizeof(*guehdr) + optlen;
319
Tom Herberta8d31c12014-11-04 09:06:57 -0800320 /* Adjust NAPI_GRO_CB(skb)->csum to account for guehdr,
321 * this is needed if there is a remote checkcsum offload.
322 */
Tom Herbert5024c332014-11-04 09:06:53 -0800323 skb_gro_postpull_rcsum(skb, guehdr, hdrlen);
324
325 data = &guehdr[1];
326
327 if (guehdr->flags & GUE_FLAG_PRIV) {
Tom Herberta8d31c12014-11-04 09:06:57 -0800328 __be32 flags = *(__be32 *)(data + doffset);
Tom Herbert5024c332014-11-04 09:06:53 -0800329
Tom Herberta8d31c12014-11-04 09:06:57 -0800330 doffset += GUE_LEN_PRIV;
331
332 if (flags & GUE_PFLAG_REMCSUM) {
333 guehdr = gue_gro_remcsum(skb, off, guehdr,
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700334 data + doffset, hdrlen, &grc,
Tom Herbertfe881ef2015-02-10 16:30:33 -0800335 !!(fou->flags &
336 FOU_F_REMCSUM_NOPARTIAL));
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700337
Tom Herberta8d31c12014-11-04 09:06:57 -0800338 if (!guehdr)
339 goto out;
340
341 data = &guehdr[1];
342
343 doffset += GUE_PLEN_REMCSUM;
344 }
Tom Herbert37dd0242014-10-03 15:48:09 -0700345 }
346
Tom Herberta8d31c12014-11-04 09:06:57 -0800347 skb_gro_pull(skb, hdrlen);
348
Tom Herbert37dd0242014-10-03 15:48:09 -0700349 for (p = *head; p; p = p->next) {
350 const struct guehdr *guehdr2;
351
352 if (!NAPI_GRO_CB(p)->same_flow)
353 continue;
354
355 guehdr2 = (struct guehdr *)(p->data + off);
356
357 /* Compare base GUE header to be equal (covers
Tom Herbert5024c332014-11-04 09:06:53 -0800358 * hlen, version, proto_ctype, and flags.
Tom Herbert37dd0242014-10-03 15:48:09 -0700359 */
360 if (guehdr->word != guehdr2->word) {
361 NAPI_GRO_CB(p)->same_flow = 0;
362 continue;
363 }
364
365 /* Compare optional fields are the same. */
366 if (guehdr->hlen && memcmp(&guehdr[1], &guehdr2[1],
367 guehdr->hlen << 2)) {
368 NAPI_GRO_CB(p)->same_flow = 0;
369 continue;
370 }
371 }
372
Alexander Duyckc3483382016-03-29 14:55:22 -0700373 /* We can clear the encap_mark for GUE as we are essentially doing
374 * one of two possible things. We are either adding an L4 tunnel
375 * header to the outer L3 tunnel header, or we are are simply
376 * treating the GRE tunnel header as though it is a UDP protocol
377 * specific header such as VXLAN or GENEVE.
378 */
379 NAPI_GRO_CB(skb)->encap_mark = 0;
380
Alexander Duycka0ca1532016-04-05 09:13:39 -0700381 /* Flag this frame as already having an outer encap header */
382 NAPI_GRO_CB(skb)->is_fou = 1;
383
Tom Herbert5024c332014-11-04 09:06:53 -0800384 rcu_read_lock();
385 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
386 ops = rcu_dereference(offloads[guehdr->proto_ctype]);
Tom Herbert27013662015-08-19 17:07:34 -0700387 if (WARN_ON_ONCE(!ops || !ops->callbacks.gro_receive))
Tom Herbert5024c332014-11-04 09:06:53 -0800388 goto out_unlock;
Tom Herbert37dd0242014-10-03 15:48:09 -0700389
390 pp = ops->callbacks.gro_receive(head, skb);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800391 flush = 0;
Tom Herbert37dd0242014-10-03 15:48:09 -0700392
393out_unlock:
394 rcu_read_unlock();
395out:
396 NAPI_GRO_CB(skb)->flush |= flush;
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800397 skb_gro_remcsum_cleanup(skb, &grc);
Tom Herbert37dd0242014-10-03 15:48:09 -0700398
399 return pp;
400}
401
Tom Herbertd92283e2016-04-05 08:22:54 -0700402static int gue_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
Tom Herbert37dd0242014-10-03 15:48:09 -0700403{
404 const struct net_offload **offloads;
405 struct guehdr *guehdr = (struct guehdr *)(skb->data + nhoff);
406 const struct net_offload *ops;
407 unsigned int guehlen;
408 u8 proto;
409 int err = -ENOENT;
410
Tom Herbert5024c332014-11-04 09:06:53 -0800411 proto = guehdr->proto_ctype;
Tom Herbert37dd0242014-10-03 15:48:09 -0700412
413 guehlen = sizeof(*guehdr) + (guehdr->hlen << 2);
414
415 rcu_read_lock();
416 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
417 ops = rcu_dereference(offloads[proto]);
418 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
419 goto out_unlock;
420
421 err = ops->callbacks.gro_complete(skb, nhoff + guehlen);
422
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700423 skb_set_inner_mac_header(skb, nhoff + guehlen);
424
Tom Herbert37dd0242014-10-03 15:48:09 -0700425out_unlock:
426 rcu_read_unlock();
427 return err;
428}
429
WANG Cong02d793c2015-04-10 12:00:29 -0700430static int fou_add_to_port_list(struct net *net, struct fou *fou)
Tom Herbert23461552014-09-17 12:25:56 -0700431{
WANG Cong02d793c2015-04-10 12:00:29 -0700432 struct fou_net *fn = net_generic(net, fou_net_id);
Tom Herbert23461552014-09-17 12:25:56 -0700433 struct fou *fout;
434
WANG Cong02d793c2015-04-10 12:00:29 -0700435 mutex_lock(&fn->fou_lock);
436 list_for_each_entry(fout, &fn->fou_list, list) {
Tom Herbert5f914b62016-05-18 09:06:16 -0700437 if (fou->port == fout->port &&
438 fou->family == fout->family) {
WANG Cong02d793c2015-04-10 12:00:29 -0700439 mutex_unlock(&fn->fou_lock);
Tom Herbert23461552014-09-17 12:25:56 -0700440 return -EALREADY;
441 }
442 }
443
WANG Cong02d793c2015-04-10 12:00:29 -0700444 list_add(&fou->list, &fn->fou_list);
445 mutex_unlock(&fn->fou_lock);
Tom Herbert23461552014-09-17 12:25:56 -0700446
447 return 0;
448}
449
450static void fou_release(struct fou *fou)
451{
452 struct socket *sock = fou->sock;
Tom Herbert23461552014-09-17 12:25:56 -0700453
Tom Herbert23461552014-09-17 12:25:56 -0700454 list_del(&fou->list);
WANG Cong02d793c2015-04-10 12:00:29 -0700455 udp_tunnel_sock_release(sock);
Tom Herbert23461552014-09-17 12:25:56 -0700456
Hannes Frederic Sowa3036fac2015-12-15 21:01:53 +0100457 kfree_rcu(fou, rcu);
Tom Herbert23461552014-09-17 12:25:56 -0700458}
459
460static int fou_create(struct net *net, struct fou_cfg *cfg,
461 struct socket **sockp)
462{
Tom Herbert23461552014-09-17 12:25:56 -0700463 struct socket *sock = NULL;
WANG Cong02d793c2015-04-10 12:00:29 -0700464 struct fou *fou = NULL;
Tom Herbert23461552014-09-17 12:25:56 -0700465 struct sock *sk;
Tom Herbert440924b2016-05-18 09:06:14 -0700466 struct udp_tunnel_sock_cfg tunnel_cfg;
WANG Cong02d793c2015-04-10 12:00:29 -0700467 int err;
Tom Herbert23461552014-09-17 12:25:56 -0700468
469 /* Open UDP socket */
470 err = udp_sock_create(net, &cfg->udp_config, &sock);
471 if (err < 0)
472 goto error;
473
474 /* Allocate FOU port structure */
475 fou = kzalloc(sizeof(*fou), GFP_KERNEL);
476 if (!fou) {
477 err = -ENOMEM;
478 goto error;
479 }
480
481 sk = sock->sk;
482
Tom Herbert37dd0242014-10-03 15:48:09 -0700483 fou->port = cfg->udp_config.local_udp_port;
Tom Herbert5f914b62016-05-18 09:06:16 -0700484 fou->family = cfg->udp_config.family;
485 fou->flags = cfg->flags;
Tom Herbert440924b2016-05-18 09:06:14 -0700486 fou->type = cfg->type;
487 fou->sock = sock;
488
489 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
490 tunnel_cfg.encap_type = 1;
491 tunnel_cfg.sk_user_data = fou;
492 tunnel_cfg.encap_destroy = NULL;
Tom Herbert37dd0242014-10-03 15:48:09 -0700493
494 /* Initial for fou type */
495 switch (cfg->type) {
496 case FOU_ENCAP_DIRECT:
Tom Herbert440924b2016-05-18 09:06:14 -0700497 tunnel_cfg.encap_rcv = fou_udp_recv;
498 tunnel_cfg.gro_receive = fou_gro_receive;
499 tunnel_cfg.gro_complete = fou_gro_complete;
500 fou->protocol = cfg->protocol;
Tom Herbert37dd0242014-10-03 15:48:09 -0700501 break;
502 case FOU_ENCAP_GUE:
Tom Herbert440924b2016-05-18 09:06:14 -0700503 tunnel_cfg.encap_rcv = gue_udp_recv;
504 tunnel_cfg.gro_receive = gue_gro_receive;
505 tunnel_cfg.gro_complete = gue_gro_complete;
Tom Herbert37dd0242014-10-03 15:48:09 -0700506 break;
507 default:
508 err = -EINVAL;
509 goto error;
510 }
Tom Herbert23461552014-09-17 12:25:56 -0700511
Tom Herbert440924b2016-05-18 09:06:14 -0700512 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Tom Herbert23461552014-09-17 12:25:56 -0700513
514 sk->sk_allocation = GFP_ATOMIC;
515
WANG Cong02d793c2015-04-10 12:00:29 -0700516 err = fou_add_to_port_list(net, fou);
Tom Herbert23461552014-09-17 12:25:56 -0700517 if (err)
518 goto error;
519
520 if (sockp)
521 *sockp = sock;
522
523 return 0;
524
525error:
526 kfree(fou);
527 if (sock)
WANG Cong02d793c2015-04-10 12:00:29 -0700528 udp_tunnel_sock_release(sock);
Tom Herbert23461552014-09-17 12:25:56 -0700529
530 return err;
531}
532
533static int fou_destroy(struct net *net, struct fou_cfg *cfg)
534{
WANG Cong02d793c2015-04-10 12:00:29 -0700535 struct fou_net *fn = net_generic(net, fou_net_id);
WANG Cong4cbcdf22015-04-10 12:00:28 -0700536 __be16 port = cfg->udp_config.local_udp_port;
Tom Herbert5f914b62016-05-18 09:06:16 -0700537 u8 family = cfg->udp_config.family;
Tom Herbert23461552014-09-17 12:25:56 -0700538 int err = -EINVAL;
WANG Cong02d793c2015-04-10 12:00:29 -0700539 struct fou *fou;
Tom Herbert23461552014-09-17 12:25:56 -0700540
WANG Cong02d793c2015-04-10 12:00:29 -0700541 mutex_lock(&fn->fou_lock);
542 list_for_each_entry(fou, &fn->fou_list, list) {
Tom Herbert5f914b62016-05-18 09:06:16 -0700543 if (fou->port == port && fou->family == family) {
Tom Herbert23461552014-09-17 12:25:56 -0700544 fou_release(fou);
545 err = 0;
546 break;
547 }
548 }
WANG Cong02d793c2015-04-10 12:00:29 -0700549 mutex_unlock(&fn->fou_lock);
Tom Herbert23461552014-09-17 12:25:56 -0700550
551 return err;
552}
553
554static struct genl_family fou_nl_family = {
555 .id = GENL_ID_GENERATE,
556 .hdrsize = 0,
557 .name = FOU_GENL_NAME,
558 .version = FOU_GENL_VERSION,
559 .maxattr = FOU_ATTR_MAX,
560 .netnsok = true,
561};
562
563static struct nla_policy fou_nl_policy[FOU_ATTR_MAX + 1] = {
564 [FOU_ATTR_PORT] = { .type = NLA_U16, },
565 [FOU_ATTR_AF] = { .type = NLA_U8, },
566 [FOU_ATTR_IPPROTO] = { .type = NLA_U8, },
Tom Herbert37dd0242014-10-03 15:48:09 -0700567 [FOU_ATTR_TYPE] = { .type = NLA_U8, },
Tom Herbertfe881ef2015-02-10 16:30:33 -0800568 [FOU_ATTR_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG, },
Tom Herbert23461552014-09-17 12:25:56 -0700569};
570
571static int parse_nl_config(struct genl_info *info,
572 struct fou_cfg *cfg)
573{
574 memset(cfg, 0, sizeof(*cfg));
575
576 cfg->udp_config.family = AF_INET;
577
578 if (info->attrs[FOU_ATTR_AF]) {
579 u8 family = nla_get_u8(info->attrs[FOU_ATTR_AF]);
580
Tom Herbert5f914b62016-05-18 09:06:16 -0700581 switch (family) {
582 case AF_INET:
583 break;
584 case AF_INET6:
585 cfg->udp_config.ipv6_v6only = 1;
586 break;
587 default:
588 return -EAFNOSUPPORT;
589 }
Tom Herbert23461552014-09-17 12:25:56 -0700590
591 cfg->udp_config.family = family;
592 }
593
594 if (info->attrs[FOU_ATTR_PORT]) {
WANG Cong4cbcdf22015-04-10 12:00:28 -0700595 __be16 port = nla_get_be16(info->attrs[FOU_ATTR_PORT]);
Tom Herbert23461552014-09-17 12:25:56 -0700596
597 cfg->udp_config.local_udp_port = port;
598 }
599
600 if (info->attrs[FOU_ATTR_IPPROTO])
601 cfg->protocol = nla_get_u8(info->attrs[FOU_ATTR_IPPROTO]);
602
Tom Herbert37dd0242014-10-03 15:48:09 -0700603 if (info->attrs[FOU_ATTR_TYPE])
604 cfg->type = nla_get_u8(info->attrs[FOU_ATTR_TYPE]);
605
Tom Herbertfe881ef2015-02-10 16:30:33 -0800606 if (info->attrs[FOU_ATTR_REMCSUM_NOPARTIAL])
607 cfg->flags |= FOU_F_REMCSUM_NOPARTIAL;
608
Tom Herbert23461552014-09-17 12:25:56 -0700609 return 0;
610}
611
612static int fou_nl_cmd_add_port(struct sk_buff *skb, struct genl_info *info)
613{
WANG Cong02d793c2015-04-10 12:00:29 -0700614 struct net *net = genl_info_net(info);
Tom Herbert23461552014-09-17 12:25:56 -0700615 struct fou_cfg cfg;
616 int err;
617
618 err = parse_nl_config(info, &cfg);
619 if (err)
620 return err;
621
WANG Cong02d793c2015-04-10 12:00:29 -0700622 return fou_create(net, &cfg, NULL);
Tom Herbert23461552014-09-17 12:25:56 -0700623}
624
625static int fou_nl_cmd_rm_port(struct sk_buff *skb, struct genl_info *info)
626{
WANG Cong02d793c2015-04-10 12:00:29 -0700627 struct net *net = genl_info_net(info);
Tom Herbert23461552014-09-17 12:25:56 -0700628 struct fou_cfg cfg;
WANG Cong67270632015-04-10 12:00:27 -0700629 int err;
Tom Herbert23461552014-09-17 12:25:56 -0700630
WANG Cong67270632015-04-10 12:00:27 -0700631 err = parse_nl_config(info, &cfg);
632 if (err)
633 return err;
Tom Herbert23461552014-09-17 12:25:56 -0700634
WANG Cong02d793c2015-04-10 12:00:29 -0700635 return fou_destroy(net, &cfg);
Tom Herbert23461552014-09-17 12:25:56 -0700636}
637
WANG Cong7a6c8c32015-04-10 12:00:30 -0700638static int fou_fill_info(struct fou *fou, struct sk_buff *msg)
639{
640 if (nla_put_u8(msg, FOU_ATTR_AF, fou->sock->sk->sk_family) ||
641 nla_put_be16(msg, FOU_ATTR_PORT, fou->port) ||
642 nla_put_u8(msg, FOU_ATTR_IPPROTO, fou->protocol) ||
643 nla_put_u8(msg, FOU_ATTR_TYPE, fou->type))
644 return -1;
645
646 if (fou->flags & FOU_F_REMCSUM_NOPARTIAL)
647 if (nla_put_flag(msg, FOU_ATTR_REMCSUM_NOPARTIAL))
648 return -1;
649 return 0;
650}
651
652static int fou_dump_info(struct fou *fou, u32 portid, u32 seq,
653 u32 flags, struct sk_buff *skb, u8 cmd)
654{
655 void *hdr;
656
657 hdr = genlmsg_put(skb, portid, seq, &fou_nl_family, flags, cmd);
658 if (!hdr)
659 return -ENOMEM;
660
661 if (fou_fill_info(fou, skb) < 0)
662 goto nla_put_failure;
663
664 genlmsg_end(skb, hdr);
665 return 0;
666
667nla_put_failure:
668 genlmsg_cancel(skb, hdr);
669 return -EMSGSIZE;
670}
671
672static int fou_nl_cmd_get_port(struct sk_buff *skb, struct genl_info *info)
673{
674 struct net *net = genl_info_net(info);
675 struct fou_net *fn = net_generic(net, fou_net_id);
676 struct sk_buff *msg;
677 struct fou_cfg cfg;
678 struct fou *fout;
679 __be16 port;
Tom Herbert5f914b62016-05-18 09:06:16 -0700680 u8 family;
WANG Cong7a6c8c32015-04-10 12:00:30 -0700681 int ret;
682
683 ret = parse_nl_config(info, &cfg);
684 if (ret)
685 return ret;
686 port = cfg.udp_config.local_udp_port;
687 if (port == 0)
688 return -EINVAL;
689
Tom Herbert5f914b62016-05-18 09:06:16 -0700690 family = cfg.udp_config.family;
691 if (family != AF_INET && family != AF_INET6)
692 return -EINVAL;
693
WANG Cong7a6c8c32015-04-10 12:00:30 -0700694 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
695 if (!msg)
696 return -ENOMEM;
697
698 ret = -ESRCH;
699 mutex_lock(&fn->fou_lock);
700 list_for_each_entry(fout, &fn->fou_list, list) {
Tom Herbert5f914b62016-05-18 09:06:16 -0700701 if (port == fout->port && family == fout->family) {
WANG Cong7a6c8c32015-04-10 12:00:30 -0700702 ret = fou_dump_info(fout, info->snd_portid,
703 info->snd_seq, 0, msg,
704 info->genlhdr->cmd);
705 break;
706 }
707 }
708 mutex_unlock(&fn->fou_lock);
709 if (ret < 0)
710 goto out_free;
711
712 return genlmsg_reply(msg, info);
713
714out_free:
715 nlmsg_free(msg);
716 return ret;
717}
718
719static int fou_nl_dump(struct sk_buff *skb, struct netlink_callback *cb)
720{
721 struct net *net = sock_net(skb->sk);
722 struct fou_net *fn = net_generic(net, fou_net_id);
723 struct fou *fout;
724 int idx = 0, ret;
725
726 mutex_lock(&fn->fou_lock);
727 list_for_each_entry(fout, &fn->fou_list, list) {
728 if (idx++ < cb->args[0])
729 continue;
730 ret = fou_dump_info(fout, NETLINK_CB(cb->skb).portid,
731 cb->nlh->nlmsg_seq, NLM_F_MULTI,
732 skb, FOU_CMD_GET);
733 if (ret)
WANG Cong540207a2015-04-15 11:48:49 -0700734 break;
WANG Cong7a6c8c32015-04-10 12:00:30 -0700735 }
736 mutex_unlock(&fn->fou_lock);
737
WANG Cong7a6c8c32015-04-10 12:00:30 -0700738 cb->args[0] = idx;
739 return skb->len;
740}
741
Tom Herbert23461552014-09-17 12:25:56 -0700742static const struct genl_ops fou_nl_ops[] = {
743 {
744 .cmd = FOU_CMD_ADD,
745 .doit = fou_nl_cmd_add_port,
746 .policy = fou_nl_policy,
747 .flags = GENL_ADMIN_PERM,
748 },
749 {
750 .cmd = FOU_CMD_DEL,
751 .doit = fou_nl_cmd_rm_port,
752 .policy = fou_nl_policy,
753 .flags = GENL_ADMIN_PERM,
754 },
WANG Cong7a6c8c32015-04-10 12:00:30 -0700755 {
756 .cmd = FOU_CMD_GET,
757 .doit = fou_nl_cmd_get_port,
758 .dumpit = fou_nl_dump,
759 .policy = fou_nl_policy,
760 },
Tom Herbert23461552014-09-17 12:25:56 -0700761};
762
Tom Herberta8c5f902014-11-12 11:54:09 -0800763size_t fou_encap_hlen(struct ip_tunnel_encap *e)
764{
765 return sizeof(struct udphdr);
766}
767EXPORT_SYMBOL(fou_encap_hlen);
768
769size_t gue_encap_hlen(struct ip_tunnel_encap *e)
770{
771 size_t len;
772 bool need_priv = false;
773
774 len = sizeof(struct udphdr) + sizeof(struct guehdr);
775
776 if (e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) {
777 len += GUE_PLEN_REMCSUM;
778 need_priv = true;
779 }
780
781 len += need_priv ? GUE_LEN_PRIV : 0;
782
783 return len;
784}
785EXPORT_SYMBOL(gue_encap_hlen);
786
Tom Herbert63487ba2014-11-04 09:06:51 -0800787static void fou_build_udp(struct sk_buff *skb, struct ip_tunnel_encap *e,
788 struct flowi4 *fl4, u8 *protocol, __be16 sport)
789{
790 struct udphdr *uh;
791
792 skb_push(skb, sizeof(struct udphdr));
793 skb_reset_transport_header(skb);
794
795 uh = udp_hdr(skb);
796
797 uh->dest = e->dport;
798 uh->source = sport;
799 uh->len = htons(skb->len);
Tom Herbert63487ba2014-11-04 09:06:51 -0800800 udp_set_csum(!(e->flags & TUNNEL_ENCAP_FLAG_CSUM), skb,
801 fl4->saddr, fl4->daddr, skb->len);
802
803 *protocol = IPPROTO_UDP;
804}
805
Tom Herbertdc969b82016-05-18 09:06:15 -0700806int __fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
807 u8 *protocol, __be16 *sport, int type)
808{
809 int err;
810
811 err = iptunnel_handle_offloads(skb, type);
812 if (err)
813 return err;
814
815 *sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
816 skb, 0, 0, false);
817
818 return 0;
819}
820EXPORT_SYMBOL(__fou_build_header);
821
Tom Herbert63487ba2014-11-04 09:06:51 -0800822int fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
823 u8 *protocol, struct flowi4 *fl4)
824{
Edward Cree06f62292016-02-11 21:00:16 +0000825 int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM ? SKB_GSO_UDP_TUNNEL_CSUM :
826 SKB_GSO_UDP_TUNNEL;
Tom Herbert63487ba2014-11-04 09:06:51 -0800827 __be16 sport;
Alexander Duyckaed069d2016-04-14 15:33:37 -0400828 int err;
Tom Herbert63487ba2014-11-04 09:06:51 -0800829
Tom Herbertdc969b82016-05-18 09:06:15 -0700830 err = __fou_build_header(skb, e, protocol, &sport, type);
Alexander Duyckaed069d2016-04-14 15:33:37 -0400831 if (err)
832 return err;
Tom Herbert63487ba2014-11-04 09:06:51 -0800833
Tom Herbert63487ba2014-11-04 09:06:51 -0800834 fou_build_udp(skb, e, fl4, protocol, sport);
835
836 return 0;
837}
838EXPORT_SYMBOL(fou_build_header);
839
Tom Herbertdc969b82016-05-18 09:06:15 -0700840int __gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
841 u8 *protocol, __be16 *sport, int type)
Tom Herbert63487ba2014-11-04 09:06:51 -0800842{
Tom Herbert63487ba2014-11-04 09:06:51 -0800843 struct guehdr *guehdr;
Tom Herbertb17f7092014-11-04 09:06:56 -0800844 size_t hdrlen, optlen = 0;
Tom Herbert5024c332014-11-04 09:06:53 -0800845 void *data;
846 bool need_priv = false;
Alexander Duyckaed069d2016-04-14 15:33:37 -0400847 int err;
Tom Herbert5024c332014-11-04 09:06:53 -0800848
Tom Herbertb17f7092014-11-04 09:06:56 -0800849 if ((e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) &&
850 skb->ip_summed == CHECKSUM_PARTIAL) {
Tom Herbertb17f7092014-11-04 09:06:56 -0800851 optlen += GUE_PLEN_REMCSUM;
852 type |= SKB_GSO_TUNNEL_REMCSUM;
853 need_priv = true;
854 }
855
Tom Herbert5024c332014-11-04 09:06:53 -0800856 optlen += need_priv ? GUE_LEN_PRIV : 0;
Tom Herbert63487ba2014-11-04 09:06:51 -0800857
Alexander Duyckaed069d2016-04-14 15:33:37 -0400858 err = iptunnel_handle_offloads(skb, type);
859 if (err)
860 return err;
Tom Herbert63487ba2014-11-04 09:06:51 -0800861
862 /* Get source port (based on flow hash) before skb_push */
Tom Herbertdc969b82016-05-18 09:06:15 -0700863 *sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
864 skb, 0, 0, false);
Tom Herbert63487ba2014-11-04 09:06:51 -0800865
Tom Herbertb17f7092014-11-04 09:06:56 -0800866 hdrlen = sizeof(struct guehdr) + optlen;
867
868 skb_push(skb, hdrlen);
Tom Herbert63487ba2014-11-04 09:06:51 -0800869
870 guehdr = (struct guehdr *)skb->data;
871
Tom Herbert5024c332014-11-04 09:06:53 -0800872 guehdr->control = 0;
Tom Herbert63487ba2014-11-04 09:06:51 -0800873 guehdr->version = 0;
Tom Herbert5024c332014-11-04 09:06:53 -0800874 guehdr->hlen = optlen >> 2;
Tom Herbert63487ba2014-11-04 09:06:51 -0800875 guehdr->flags = 0;
Tom Herbert5024c332014-11-04 09:06:53 -0800876 guehdr->proto_ctype = *protocol;
877
878 data = &guehdr[1];
879
880 if (need_priv) {
881 __be32 *flags = data;
882
883 guehdr->flags |= GUE_FLAG_PRIV;
884 *flags = 0;
885 data += GUE_LEN_PRIV;
886
Tom Herbertb17f7092014-11-04 09:06:56 -0800887 if (type & SKB_GSO_TUNNEL_REMCSUM) {
888 u16 csum_start = skb_checksum_start_offset(skb);
889 __be16 *pd = data;
890
891 if (csum_start < hdrlen)
892 return -EINVAL;
893
894 csum_start -= hdrlen;
895 pd[0] = htons(csum_start);
896 pd[1] = htons(csum_start + skb->csum_offset);
897
898 if (!skb_is_gso(skb)) {
899 skb->ip_summed = CHECKSUM_NONE;
900 skb->encapsulation = 0;
901 }
902
903 *flags |= GUE_PFLAG_REMCSUM;
904 data += GUE_PLEN_REMCSUM;
905 }
906
Tom Herbert5024c332014-11-04 09:06:53 -0800907 }
Tom Herbert63487ba2014-11-04 09:06:51 -0800908
Tom Herbertdc969b82016-05-18 09:06:15 -0700909 return 0;
910}
911EXPORT_SYMBOL(__gue_build_header);
912
913int gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
914 u8 *protocol, struct flowi4 *fl4)
915{
916 int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM ? SKB_GSO_UDP_TUNNEL_CSUM :
917 SKB_GSO_UDP_TUNNEL;
918 __be16 sport;
919 int err;
920
921 err = __gue_build_header(skb, e, protocol, &sport, type);
922 if (err)
923 return err;
924
Tom Herbert63487ba2014-11-04 09:06:51 -0800925 fou_build_udp(skb, e, fl4, protocol, sport);
926
927 return 0;
928}
929EXPORT_SYMBOL(gue_build_header);
930
Tom Herberta8c5f902014-11-12 11:54:09 -0800931#ifdef CONFIG_NET_FOU_IP_TUNNELS
932
Andi Kleen5eeb2922015-04-08 06:04:31 -0700933static const struct ip_tunnel_encap_ops fou_iptun_ops = {
Tom Herberta8c5f902014-11-12 11:54:09 -0800934 .encap_hlen = fou_encap_hlen,
935 .build_header = fou_build_header,
936};
937
Andi Kleen5eeb2922015-04-08 06:04:31 -0700938static const struct ip_tunnel_encap_ops gue_iptun_ops = {
Tom Herberta8c5f902014-11-12 11:54:09 -0800939 .encap_hlen = gue_encap_hlen,
940 .build_header = gue_build_header,
941};
942
943static int ip_tunnel_encap_add_fou_ops(void)
944{
945 int ret;
946
947 ret = ip_tunnel_encap_add_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
948 if (ret < 0) {
949 pr_err("can't add fou ops\n");
950 return ret;
951 }
952
953 ret = ip_tunnel_encap_add_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
954 if (ret < 0) {
955 pr_err("can't add gue ops\n");
956 ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
957 return ret;
958 }
959
960 return 0;
961}
962
963static void ip_tunnel_encap_del_fou_ops(void)
964{
965 ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
966 ip_tunnel_encap_del_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
967}
968
969#else
970
971static int ip_tunnel_encap_add_fou_ops(void)
972{
973 return 0;
974}
975
Thomas Graf882288c2014-11-13 12:48:21 +0100976static void ip_tunnel_encap_del_fou_ops(void)
Tom Herberta8c5f902014-11-12 11:54:09 -0800977{
978}
979
980#endif
981
WANG Cong02d793c2015-04-10 12:00:29 -0700982static __net_init int fou_init_net(struct net *net)
983{
984 struct fou_net *fn = net_generic(net, fou_net_id);
985
986 INIT_LIST_HEAD(&fn->fou_list);
987 mutex_init(&fn->fou_lock);
988 return 0;
989}
990
991static __net_exit void fou_exit_net(struct net *net)
992{
993 struct fou_net *fn = net_generic(net, fou_net_id);
994 struct fou *fou, *next;
995
996 /* Close all the FOU sockets */
997 mutex_lock(&fn->fou_lock);
998 list_for_each_entry_safe(fou, next, &fn->fou_list, list)
999 fou_release(fou);
1000 mutex_unlock(&fn->fou_lock);
1001}
1002
1003static struct pernet_operations fou_net_ops = {
1004 .init = fou_init_net,
1005 .exit = fou_exit_net,
1006 .id = &fou_net_id,
1007 .size = sizeof(struct fou_net),
1008};
1009
Tom Herbert23461552014-09-17 12:25:56 -07001010static int __init fou_init(void)
1011{
1012 int ret;
1013
WANG Cong02d793c2015-04-10 12:00:29 -07001014 ret = register_pernet_device(&fou_net_ops);
1015 if (ret)
Tom Herberta8c5f902014-11-12 11:54:09 -08001016 goto exit;
1017
WANG Cong02d793c2015-04-10 12:00:29 -07001018 ret = genl_register_family_with_ops(&fou_nl_family,
1019 fou_nl_ops);
Tom Herberta8c5f902014-11-12 11:54:09 -08001020 if (ret < 0)
WANG Cong02d793c2015-04-10 12:00:29 -07001021 goto unregister;
Tom Herberta8c5f902014-11-12 11:54:09 -08001022
WANG Cong02d793c2015-04-10 12:00:29 -07001023 ret = ip_tunnel_encap_add_fou_ops();
1024 if (ret == 0)
1025 return 0;
1026
1027 genl_unregister_family(&fou_nl_family);
1028unregister:
1029 unregister_pernet_device(&fou_net_ops);
Tom Herberta8c5f902014-11-12 11:54:09 -08001030exit:
Tom Herbert23461552014-09-17 12:25:56 -07001031 return ret;
1032}
1033
1034static void __exit fou_fini(void)
1035{
Tom Herberta8c5f902014-11-12 11:54:09 -08001036 ip_tunnel_encap_del_fou_ops();
Tom Herbert23461552014-09-17 12:25:56 -07001037 genl_unregister_family(&fou_nl_family);
WANG Cong02d793c2015-04-10 12:00:29 -07001038 unregister_pernet_device(&fou_net_ops);
Tom Herbert23461552014-09-17 12:25:56 -07001039}
1040
1041module_init(fou_init);
1042module_exit(fou_fini);
1043MODULE_AUTHOR("Tom Herbert <therbert@google.com>");
1044MODULE_LICENSE("GPL");