blob: f4f2ddd8f2169d6f469a80e94c417b0cfd13160a [file] [log] [blame]
Tom Herbert234615512014-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 Herbert234615512014-09-17 12:25:56 -070011#include <net/ip.h>
Tom Herbertafe93322014-09-17 12:25:57 -070012#include <net/protocol.h>
Tom Herbert234615512014-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 Herbert234615512014-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 Herbert234615512014-09-17 12:25:56 -070025 struct list_head list;
Hannes Frederic Sowa3036fac2015-12-15 21:01:53 +010026 struct rcu_head rcu;
Tom Herbert234615512014-09-17 12:25:56 -070027};
28
Tom Herbertfe881ef2015-02-10 16:30:33 -080029#define FOU_F_REMCSUM_NOPARTIAL BIT(0)
30
Tom Herbert234615512014-09-17 12:25:56 -070031struct fou_cfg {
Tom Herbert37dd0242014-10-03 15:48:09 -070032 u16 type;
Tom Herbert234615512014-09-17 12:25:56 -070033 u8 protocol;
Tom Herbertfe881ef2015-02-10 16:30:33 -080034 u8 flags;
Tom Herbert234615512014-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 Herbert234615512014-09-17 12:25:56 -070045static inline struct fou *fou_from_sock(struct sock *sk)
46{
47 return sk->sk_user_data;
48}
49
Jesse Grossa09a4c82016-03-19 09:32:02 -070050static int fou_recv_pull(struct sk_buff *skb, size_t len)
Tom Herbert234615512014-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 Herbert234615512014-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);
Jesse Grossa09a4c82016-03-19 09:32:02 -070061 return iptunnel_pull_offloads(skb);
Tom Herbert234615512014-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
Jesse Grossa09a4c82016-03-19 09:32:02 -070071 if (fou_recv_pull(skb, sizeof(struct udphdr)))
72 goto drop;
Tom Herbert5024c332014-11-04 09:06:53 -080073
74 return -fou->protocol;
Jesse Grossa09a4c82016-03-19 09:32:02 -070075
76drop:
77 kfree_skb(skb);
78 return 0;
Tom Herbert5024c332014-11-04 09:06:53 -080079}
80
Tom Herberta8d31c12014-11-04 09:06:57 -080081static struct guehdr *gue_remcsum(struct sk_buff *skb, struct guehdr *guehdr,
Tom Herbertfe881ef2015-02-10 16:30:33 -080082 void *data, size_t hdrlen, u8 ipproto,
83 bool nopartial)
Tom Herberta8d31c12014-11-04 09:06:57 -080084{
85 __be16 *pd = data;
Tom Herbert4fd671d2014-11-25 11:21:20 -080086 size_t start = ntohs(pd[0]);
87 size_t offset = ntohs(pd[1]);
Tom Herbertb7fe10e2015-08-19 17:07:32 -070088 size_t plen = sizeof(struct udphdr) + hdrlen +
89 max_t(size_t, offset + sizeof(u16), start);
90
91 if (skb->remcsum_offload)
92 return guehdr;
Tom Herberta8d31c12014-11-04 09:06:57 -080093
Tom Herberta8d31c12014-11-04 09:06:57 -080094 if (!pskb_may_pull(skb, plen))
95 return NULL;
96 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
97
Tom Herbertfe881ef2015-02-10 16:30:33 -080098 skb_remcsum_process(skb, (void *)guehdr + hdrlen,
99 start, offset, nopartial);
Tom Herberta8d31c12014-11-04 09:06:57 -0800100
101 return guehdr;
102}
103
Tom Herbert5024c332014-11-04 09:06:53 -0800104static int gue_control_message(struct sk_buff *skb, struct guehdr *guehdr)
105{
106 /* No support yet */
107 kfree_skb(skb);
108 return 0;
Tom Herbert234615512014-09-17 12:25:56 -0700109}
110
Tom Herbert37dd0242014-10-03 15:48:09 -0700111static int gue_udp_recv(struct sock *sk, struct sk_buff *skb)
112{
113 struct fou *fou = fou_from_sock(sk);
Tom Herbert5024c332014-11-04 09:06:53 -0800114 size_t len, optlen, hdrlen;
Tom Herbert37dd0242014-10-03 15:48:09 -0700115 struct guehdr *guehdr;
Tom Herbert5024c332014-11-04 09:06:53 -0800116 void *data;
Tom Herberta8d31c12014-11-04 09:06:57 -0800117 u16 doffset = 0;
Tom Herbert37dd0242014-10-03 15:48:09 -0700118
119 if (!fou)
120 return 1;
121
122 len = sizeof(struct udphdr) + sizeof(struct guehdr);
123 if (!pskb_may_pull(skb, len))
124 goto drop;
125
Tom Herbert5024c332014-11-04 09:06:53 -0800126 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
Tom Herbert37dd0242014-10-03 15:48:09 -0700127
Tom Herbert5024c332014-11-04 09:06:53 -0800128 optlen = guehdr->hlen << 2;
129 len += optlen;
130
Tom Herbert37dd0242014-10-03 15:48:09 -0700131 if (!pskb_may_pull(skb, len))
132 goto drop;
133
Tom Herbert5024c332014-11-04 09:06:53 -0800134 /* guehdr may change after pull */
135 guehdr = (struct guehdr *)&udp_hdr(skb)[1];
Li RongQingd8f00d22014-10-17 16:53:47 +0800136
Tom Herbert5024c332014-11-04 09:06:53 -0800137 hdrlen = sizeof(struct guehdr) + optlen;
138
139 if (guehdr->version != 0 || validate_gue_flags(guehdr, optlen))
Tom Herbert37dd0242014-10-03 15:48:09 -0700140 goto drop;
141
Tom Herberta8d31c12014-11-04 09:06:57 -0800142 hdrlen = sizeof(struct guehdr) + optlen;
143
144 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(skb)->tot_len) - len);
145
Tom Herberta8d31c12014-11-04 09:06:57 -0800146 /* Pull csum through the guehdr now . This can be used if
147 * there is a remote checksum offload.
148 */
149 skb_postpull_rcsum(skb, udp_hdr(skb), len);
Tom Herbert5024c332014-11-04 09:06:53 -0800150
151 data = &guehdr[1];
152
153 if (guehdr->flags & GUE_FLAG_PRIV) {
Tom Herberta8d31c12014-11-04 09:06:57 -0800154 __be32 flags = *(__be32 *)(data + doffset);
Tom Herbert5024c332014-11-04 09:06:53 -0800155
Tom Herberta8d31c12014-11-04 09:06:57 -0800156 doffset += GUE_LEN_PRIV;
157
158 if (flags & GUE_PFLAG_REMCSUM) {
159 guehdr = gue_remcsum(skb, guehdr, data + doffset,
Tom Herbertfe881ef2015-02-10 16:30:33 -0800160 hdrlen, guehdr->proto_ctype,
161 !!(fou->flags &
162 FOU_F_REMCSUM_NOPARTIAL));
Tom Herberta8d31c12014-11-04 09:06:57 -0800163 if (!guehdr)
164 goto drop;
165
166 data = &guehdr[1];
167
168 doffset += GUE_PLEN_REMCSUM;
169 }
Tom Herbert37dd0242014-10-03 15:48:09 -0700170 }
171
Tom Herbert5024c332014-11-04 09:06:53 -0800172 if (unlikely(guehdr->control))
173 return gue_control_message(skb, guehdr);
174
Tom Herbert4fd671d2014-11-25 11:21:20 -0800175 __skb_pull(skb, sizeof(struct udphdr) + hdrlen);
Tom Herberta8d31c12014-11-04 09:06:57 -0800176 skb_reset_transport_header(skb);
177
Jesse Grossa09a4c82016-03-19 09:32:02 -0700178 if (iptunnel_pull_offloads(skb))
179 goto drop;
180
Tom Herbert5024c332014-11-04 09:06:53 -0800181 return -guehdr->proto_ctype;
182
Tom Herbert37dd0242014-10-03 15:48:09 -0700183drop:
184 kfree_skb(skb);
185 return 0;
186}
187
Tom Herbertd92283e2016-04-05 08:22:54 -0700188static struct sk_buff **fou_gro_receive(struct sock *sk,
189 struct sk_buff **head,
190 struct sk_buff *skb)
Tom Herbertafe93322014-09-17 12:25:57 -0700191{
192 const struct net_offload *ops;
193 struct sk_buff **pp = NULL;
Tom Herbertd92283e2016-04-05 08:22:54 -0700194 u8 proto = fou_from_sock(sk)->protocol;
Tom Herbertefc98d02014-10-03 15:48:08 -0700195 const struct net_offload **offloads;
Tom Herbertafe93322014-09-17 12:25:57 -0700196
Alexander Duyckc3483382016-03-29 14:55:22 -0700197 /* We can clear the encap_mark for FOU as we are essentially doing
198 * one of two possible things. We are either adding an L4 tunnel
199 * header to the outer L3 tunnel header, or we are are simply
200 * treating the GRE tunnel header as though it is a UDP protocol
201 * specific header such as VXLAN or GENEVE.
202 */
203 NAPI_GRO_CB(skb)->encap_mark = 0;
204
Alexander Duycka0ca1532016-04-05 09:13:39 -0700205 /* Flag this frame as already having an outer encap header */
206 NAPI_GRO_CB(skb)->is_fou = 1;
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 (!ops || !ops->callbacks.gro_receive)
212 goto out_unlock;
213
214 pp = ops->callbacks.gro_receive(head, skb);
215
216out_unlock:
217 rcu_read_unlock();
218
219 return pp;
220}
221
Tom Herbertd92283e2016-04-05 08:22:54 -0700222static int fou_gro_complete(struct sock *sk, struct sk_buff *skb,
223 int nhoff)
Tom Herbertafe93322014-09-17 12:25:57 -0700224{
225 const struct net_offload *ops;
Tom Herbertd92283e2016-04-05 08:22:54 -0700226 u8 proto = fou_from_sock(sk)->protocol;
Tom Herbertafe93322014-09-17 12:25:57 -0700227 int err = -ENOSYS;
Tom Herbertefc98d02014-10-03 15:48:08 -0700228 const struct net_offload **offloads;
Tom Herbertafe93322014-09-17 12:25:57 -0700229
230 rcu_read_lock();
Tom Herbertefc98d02014-10-03 15:48:08 -0700231 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
Tom Herbertafe93322014-09-17 12:25:57 -0700232 ops = rcu_dereference(offloads[proto]);
233 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
234 goto out_unlock;
235
236 err = ops->callbacks.gro_complete(skb, nhoff);
237
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700238 skb_set_inner_mac_header(skb, nhoff);
239
Tom Herbertafe93322014-09-17 12:25:57 -0700240out_unlock:
241 rcu_read_unlock();
242
243 return err;
244}
245
Tom Herberta8d31c12014-11-04 09:06:57 -0800246static struct guehdr *gue_gro_remcsum(struct sk_buff *skb, unsigned int off,
247 struct guehdr *guehdr, void *data,
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700248 size_t hdrlen, struct gro_remcsum *grc,
249 bool nopartial)
Tom Herberta8d31c12014-11-04 09:06:57 -0800250{
251 __be16 *pd = data;
Tom Herbert4fd671d2014-11-25 11:21:20 -0800252 size_t start = ntohs(pd[0]);
253 size_t offset = ntohs(pd[1]);
Tom Herberta8d31c12014-11-04 09:06:57 -0800254
255 if (skb->remcsum_offload)
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700256 return guehdr;
Tom Herberta8d31c12014-11-04 09:06:57 -0800257
Tom Herbert4fd671d2014-11-25 11:21:20 -0800258 if (!NAPI_GRO_CB(skb)->csum_valid)
Tom Herberta8d31c12014-11-04 09:06:57 -0800259 return NULL;
260
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700261 guehdr = skb_gro_remcsum_process(skb, (void *)guehdr, off, hdrlen,
262 start, offset, grc, nopartial);
Tom Herberta8d31c12014-11-04 09:06:57 -0800263
264 skb->remcsum_offload = 1;
265
266 return guehdr;
267}
268
Tom Herbertd92283e2016-04-05 08:22:54 -0700269static struct sk_buff **gue_gro_receive(struct sock *sk,
270 struct sk_buff **head,
271 struct sk_buff *skb)
Tom Herbert37dd0242014-10-03 15:48:09 -0700272{
273 const struct net_offload **offloads;
274 const struct net_offload *ops;
275 struct sk_buff **pp = NULL;
276 struct sk_buff *p;
Tom Herbert37dd0242014-10-03 15:48:09 -0700277 struct guehdr *guehdr;
Tom Herbert5024c332014-11-04 09:06:53 -0800278 size_t len, optlen, hdrlen, off;
279 void *data;
Tom Herberta8d31c12014-11-04 09:06:57 -0800280 u16 doffset = 0;
Tom Herbert37dd0242014-10-03 15:48:09 -0700281 int flush = 1;
Tom Herbertd92283e2016-04-05 08:22:54 -0700282 struct fou *fou = fou_from_sock(sk);
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800283 struct gro_remcsum grc;
284
285 skb_gro_remcsum_init(&grc);
Tom Herbert37dd0242014-10-03 15:48:09 -0700286
287 off = skb_gro_offset(skb);
Tom Herbert5024c332014-11-04 09:06:53 -0800288 len = off + sizeof(*guehdr);
289
Tom Herbert37dd0242014-10-03 15:48:09 -0700290 guehdr = skb_gro_header_fast(skb, off);
Tom Herbert5024c332014-11-04 09:06:53 -0800291 if (skb_gro_header_hard(skb, len)) {
292 guehdr = skb_gro_header_slow(skb, len, off);
Tom Herbert37dd0242014-10-03 15:48:09 -0700293 if (unlikely(!guehdr))
294 goto out;
295 }
296
Tom Herbert5024c332014-11-04 09:06:53 -0800297 optlen = guehdr->hlen << 2;
298 len += optlen;
Tom Herbert37dd0242014-10-03 15:48:09 -0700299
Tom Herbert5024c332014-11-04 09:06:53 -0800300 if (skb_gro_header_hard(skb, len)) {
301 guehdr = skb_gro_header_slow(skb, len, off);
Tom Herbert37dd0242014-10-03 15:48:09 -0700302 if (unlikely(!guehdr))
Tom Herbert5024c332014-11-04 09:06:53 -0800303 goto out;
304 }
305
306 if (unlikely(guehdr->control) || guehdr->version != 0 ||
307 validate_gue_flags(guehdr, optlen))
308 goto out;
309
310 hdrlen = sizeof(*guehdr) + optlen;
311
Tom Herberta8d31c12014-11-04 09:06:57 -0800312 /* Adjust NAPI_GRO_CB(skb)->csum to account for guehdr,
313 * this is needed if there is a remote checkcsum offload.
314 */
Tom Herbert5024c332014-11-04 09:06:53 -0800315 skb_gro_postpull_rcsum(skb, guehdr, hdrlen);
316
317 data = &guehdr[1];
318
319 if (guehdr->flags & GUE_FLAG_PRIV) {
Tom Herberta8d31c12014-11-04 09:06:57 -0800320 __be32 flags = *(__be32 *)(data + doffset);
Tom Herbert5024c332014-11-04 09:06:53 -0800321
Tom Herberta8d31c12014-11-04 09:06:57 -0800322 doffset += GUE_LEN_PRIV;
323
324 if (flags & GUE_PFLAG_REMCSUM) {
325 guehdr = gue_gro_remcsum(skb, off, guehdr,
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700326 data + doffset, hdrlen, &grc,
Tom Herbertfe881ef2015-02-10 16:30:33 -0800327 !!(fou->flags &
328 FOU_F_REMCSUM_NOPARTIAL));
Tom Herbertb7fe10e2015-08-19 17:07:32 -0700329
Tom Herberta8d31c12014-11-04 09:06:57 -0800330 if (!guehdr)
331 goto out;
332
333 data = &guehdr[1];
334
335 doffset += GUE_PLEN_REMCSUM;
336 }
Tom Herbert37dd0242014-10-03 15:48:09 -0700337 }
338
Tom Herberta8d31c12014-11-04 09:06:57 -0800339 skb_gro_pull(skb, hdrlen);
340
Tom Herbert37dd0242014-10-03 15:48:09 -0700341 for (p = *head; p; p = p->next) {
342 const struct guehdr *guehdr2;
343
344 if (!NAPI_GRO_CB(p)->same_flow)
345 continue;
346
347 guehdr2 = (struct guehdr *)(p->data + off);
348
349 /* Compare base GUE header to be equal (covers
Tom Herbert5024c332014-11-04 09:06:53 -0800350 * hlen, version, proto_ctype, and flags.
Tom Herbert37dd0242014-10-03 15:48:09 -0700351 */
352 if (guehdr->word != guehdr2->word) {
353 NAPI_GRO_CB(p)->same_flow = 0;
354 continue;
355 }
356
357 /* Compare optional fields are the same. */
358 if (guehdr->hlen && memcmp(&guehdr[1], &guehdr2[1],
359 guehdr->hlen << 2)) {
360 NAPI_GRO_CB(p)->same_flow = 0;
361 continue;
362 }
363 }
364
Alexander Duyckc3483382016-03-29 14:55:22 -0700365 /* We can clear the encap_mark for GUE as we are essentially doing
366 * one of two possible things. We are either adding an L4 tunnel
367 * header to the outer L3 tunnel header, or we are are simply
368 * treating the GRE tunnel header as though it is a UDP protocol
369 * specific header such as VXLAN or GENEVE.
370 */
371 NAPI_GRO_CB(skb)->encap_mark = 0;
372
Alexander Duycka0ca1532016-04-05 09:13:39 -0700373 /* Flag this frame as already having an outer encap header */
374 NAPI_GRO_CB(skb)->is_fou = 1;
375
Tom Herbert5024c332014-11-04 09:06:53 -0800376 rcu_read_lock();
377 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
378 ops = rcu_dereference(offloads[guehdr->proto_ctype]);
Tom Herbert27013662015-08-19 17:07:34 -0700379 if (WARN_ON_ONCE(!ops || !ops->callbacks.gro_receive))
Tom Herbert5024c332014-11-04 09:06:53 -0800380 goto out_unlock;
Tom Herbert37dd0242014-10-03 15:48:09 -0700381
382 pp = ops->callbacks.gro_receive(head, skb);
Alexander Duyckc194cf92016-03-09 09:24:23 -0800383 flush = 0;
Tom Herbert37dd0242014-10-03 15:48:09 -0700384
385out_unlock:
386 rcu_read_unlock();
387out:
388 NAPI_GRO_CB(skb)->flush |= flush;
Tom Herbert26c4f7d2015-02-10 16:30:27 -0800389 skb_gro_remcsum_cleanup(skb, &grc);
Tom Herbert37dd0242014-10-03 15:48:09 -0700390
391 return pp;
392}
393
Tom Herbertd92283e2016-04-05 08:22:54 -0700394static int gue_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
Tom Herbert37dd0242014-10-03 15:48:09 -0700395{
396 const struct net_offload **offloads;
397 struct guehdr *guehdr = (struct guehdr *)(skb->data + nhoff);
398 const struct net_offload *ops;
399 unsigned int guehlen;
400 u8 proto;
401 int err = -ENOENT;
402
Tom Herbert5024c332014-11-04 09:06:53 -0800403 proto = guehdr->proto_ctype;
Tom Herbert37dd0242014-10-03 15:48:09 -0700404
405 guehlen = sizeof(*guehdr) + (guehdr->hlen << 2);
406
407 rcu_read_lock();
408 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
409 ops = rcu_dereference(offloads[proto]);
410 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
411 goto out_unlock;
412
413 err = ops->callbacks.gro_complete(skb, nhoff + guehlen);
414
Jarno Rajahalme229740c2016-05-03 16:10:21 -0700415 skb_set_inner_mac_header(skb, nhoff + guehlen);
416
Tom Herbert37dd0242014-10-03 15:48:09 -0700417out_unlock:
418 rcu_read_unlock();
419 return err;
420}
421
WANG Cong02d793c2015-04-10 12:00:29 -0700422static int fou_add_to_port_list(struct net *net, struct fou *fou)
Tom Herbert234615512014-09-17 12:25:56 -0700423{
WANG Cong02d793c2015-04-10 12:00:29 -0700424 struct fou_net *fn = net_generic(net, fou_net_id);
Tom Herbert234615512014-09-17 12:25:56 -0700425 struct fou *fout;
426
WANG Cong02d793c2015-04-10 12:00:29 -0700427 mutex_lock(&fn->fou_lock);
428 list_for_each_entry(fout, &fn->fou_list, list) {
Tom Herbert234615512014-09-17 12:25:56 -0700429 if (fou->port == fout->port) {
WANG Cong02d793c2015-04-10 12:00:29 -0700430 mutex_unlock(&fn->fou_lock);
Tom Herbert234615512014-09-17 12:25:56 -0700431 return -EALREADY;
432 }
433 }
434
WANG Cong02d793c2015-04-10 12:00:29 -0700435 list_add(&fou->list, &fn->fou_list);
436 mutex_unlock(&fn->fou_lock);
Tom Herbert234615512014-09-17 12:25:56 -0700437
438 return 0;
439}
440
441static void fou_release(struct fou *fou)
442{
443 struct socket *sock = fou->sock;
Tom Herbert234615512014-09-17 12:25:56 -0700444
Tom Herbert234615512014-09-17 12:25:56 -0700445 list_del(&fou->list);
WANG Cong02d793c2015-04-10 12:00:29 -0700446 udp_tunnel_sock_release(sock);
Tom Herbert234615512014-09-17 12:25:56 -0700447
Hannes Frederic Sowa3036fac2015-12-15 21:01:53 +0100448 kfree_rcu(fou, rcu);
Tom Herbert234615512014-09-17 12:25:56 -0700449}
450
451static int fou_create(struct net *net, struct fou_cfg *cfg,
452 struct socket **sockp)
453{
Tom Herbert234615512014-09-17 12:25:56 -0700454 struct socket *sock = NULL;
WANG Cong02d793c2015-04-10 12:00:29 -0700455 struct fou *fou = NULL;
Tom Herbert234615512014-09-17 12:25:56 -0700456 struct sock *sk;
Tom Herbert440924b2016-05-18 09:06:14 -0700457 struct udp_tunnel_sock_cfg tunnel_cfg;
WANG Cong02d793c2015-04-10 12:00:29 -0700458 int err;
Tom Herbert234615512014-09-17 12:25:56 -0700459
460 /* Open UDP socket */
461 err = udp_sock_create(net, &cfg->udp_config, &sock);
462 if (err < 0)
463 goto error;
464
465 /* Allocate FOU port structure */
466 fou = kzalloc(sizeof(*fou), GFP_KERNEL);
467 if (!fou) {
468 err = -ENOMEM;
469 goto error;
470 }
471
472 sk = sock->sk;
473
Tom Herbertfe881ef2015-02-10 16:30:33 -0800474 fou->flags = cfg->flags;
Tom Herbert37dd0242014-10-03 15:48:09 -0700475 fou->port = cfg->udp_config.local_udp_port;
Tom Herbert440924b2016-05-18 09:06:14 -0700476 fou->type = cfg->type;
477 fou->sock = sock;
478
479 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
480 tunnel_cfg.encap_type = 1;
481 tunnel_cfg.sk_user_data = fou;
482 tunnel_cfg.encap_destroy = NULL;
Tom Herbert37dd0242014-10-03 15:48:09 -0700483
484 /* Initial for fou type */
485 switch (cfg->type) {
486 case FOU_ENCAP_DIRECT:
Tom Herbert440924b2016-05-18 09:06:14 -0700487 tunnel_cfg.encap_rcv = fou_udp_recv;
488 tunnel_cfg.gro_receive = fou_gro_receive;
489 tunnel_cfg.gro_complete = fou_gro_complete;
490 fou->protocol = cfg->protocol;
Tom Herbert37dd0242014-10-03 15:48:09 -0700491 break;
492 case FOU_ENCAP_GUE:
Tom Herbert440924b2016-05-18 09:06:14 -0700493 tunnel_cfg.encap_rcv = gue_udp_recv;
494 tunnel_cfg.gro_receive = gue_gro_receive;
495 tunnel_cfg.gro_complete = gue_gro_complete;
Tom Herbert37dd0242014-10-03 15:48:09 -0700496 break;
497 default:
498 err = -EINVAL;
499 goto error;
500 }
Tom Herbert234615512014-09-17 12:25:56 -0700501
Tom Herbert440924b2016-05-18 09:06:14 -0700502 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
Tom Herbert234615512014-09-17 12:25:56 -0700503
504 sk->sk_allocation = GFP_ATOMIC;
505
WANG Cong02d793c2015-04-10 12:00:29 -0700506 err = fou_add_to_port_list(net, fou);
Tom Herbert234615512014-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 Herbert234615512014-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 Herbert234615512014-09-17 12:25:56 -0700527 int err = -EINVAL;
WANG Cong02d793c2015-04-10 12:00:29 -0700528 struct fou *fou;
Tom Herbert234615512014-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 Herbert234615512014-09-17 12:25:56 -0700532 if (fou->port == port) {
Tom Herbert234615512014-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 Herbert234615512014-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 Herbert234615512014-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 Herbert234615512014-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 Herbert234615512014-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 Herbert234615512014-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 Herbert234615512014-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 Herbert234615512014-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 Herbert234615512014-09-17 12:25:56 -0700610 struct fou_cfg cfg;
WANG Cong67270632015-04-10 12:00:27 -0700611 int err;
Tom Herbert234615512014-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 Herbert234615512014-09-17 12:25:56 -0700616
WANG Cong02d793c2015-04-10 12:00:29 -0700617 return fou_destroy(net, &cfg);
Tom Herbert234615512014-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 Herbert234615512014-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 Herbert234615512014-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
Tom Herbertdc969b82016-05-18 09:06:15 -0700783int __fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
784 u8 *protocol, __be16 *sport, int type)
785{
786 int err;
787
788 err = iptunnel_handle_offloads(skb, type);
789 if (err)
790 return err;
791
792 *sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
793 skb, 0, 0, false);
794
795 return 0;
796}
797EXPORT_SYMBOL(__fou_build_header);
798
Tom Herbert63487ba2014-11-04 09:06:51 -0800799int fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
800 u8 *protocol, struct flowi4 *fl4)
801{
Edward Cree06f62292016-02-11 21:00:16 +0000802 int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM ? SKB_GSO_UDP_TUNNEL_CSUM :
803 SKB_GSO_UDP_TUNNEL;
Tom Herbert63487ba2014-11-04 09:06:51 -0800804 __be16 sport;
Alexander Duyckaed069d2016-04-14 15:33:37 -0400805 int err;
Tom Herbert63487ba2014-11-04 09:06:51 -0800806
Tom Herbertdc969b82016-05-18 09:06:15 -0700807 err = __fou_build_header(skb, e, protocol, &sport, type);
Alexander Duyckaed069d2016-04-14 15:33:37 -0400808 if (err)
809 return err;
Tom Herbert63487ba2014-11-04 09:06:51 -0800810
Tom Herbert63487ba2014-11-04 09:06:51 -0800811 fou_build_udp(skb, e, fl4, protocol, sport);
812
813 return 0;
814}
815EXPORT_SYMBOL(fou_build_header);
816
Tom Herbertdc969b82016-05-18 09:06:15 -0700817int __gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
818 u8 *protocol, __be16 *sport, int type)
Tom Herbert63487ba2014-11-04 09:06:51 -0800819{
Tom Herbert63487ba2014-11-04 09:06:51 -0800820 struct guehdr *guehdr;
Tom Herbertb17f7092014-11-04 09:06:56 -0800821 size_t hdrlen, optlen = 0;
Tom Herbert5024c332014-11-04 09:06:53 -0800822 void *data;
823 bool need_priv = false;
Alexander Duyckaed069d2016-04-14 15:33:37 -0400824 int err;
Tom Herbert5024c332014-11-04 09:06:53 -0800825
Tom Herbertb17f7092014-11-04 09:06:56 -0800826 if ((e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) &&
827 skb->ip_summed == CHECKSUM_PARTIAL) {
Tom Herbertb17f7092014-11-04 09:06:56 -0800828 optlen += GUE_PLEN_REMCSUM;
829 type |= SKB_GSO_TUNNEL_REMCSUM;
830 need_priv = true;
831 }
832
Tom Herbert5024c332014-11-04 09:06:53 -0800833 optlen += need_priv ? GUE_LEN_PRIV : 0;
Tom Herbert63487ba2014-11-04 09:06:51 -0800834
Alexander Duyckaed069d2016-04-14 15:33:37 -0400835 err = iptunnel_handle_offloads(skb, type);
836 if (err)
837 return err;
Tom Herbert63487ba2014-11-04 09:06:51 -0800838
839 /* Get source port (based on flow hash) before skb_push */
Tom Herbertdc969b82016-05-18 09:06:15 -0700840 *sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
841 skb, 0, 0, false);
Tom Herbert63487ba2014-11-04 09:06:51 -0800842
Tom Herbertb17f7092014-11-04 09:06:56 -0800843 hdrlen = sizeof(struct guehdr) + optlen;
844
845 skb_push(skb, hdrlen);
Tom Herbert63487ba2014-11-04 09:06:51 -0800846
847 guehdr = (struct guehdr *)skb->data;
848
Tom Herbert5024c332014-11-04 09:06:53 -0800849 guehdr->control = 0;
Tom Herbert63487ba2014-11-04 09:06:51 -0800850 guehdr->version = 0;
Tom Herbert5024c332014-11-04 09:06:53 -0800851 guehdr->hlen = optlen >> 2;
Tom Herbert63487ba2014-11-04 09:06:51 -0800852 guehdr->flags = 0;
Tom Herbert5024c332014-11-04 09:06:53 -0800853 guehdr->proto_ctype = *protocol;
854
855 data = &guehdr[1];
856
857 if (need_priv) {
858 __be32 *flags = data;
859
860 guehdr->flags |= GUE_FLAG_PRIV;
861 *flags = 0;
862 data += GUE_LEN_PRIV;
863
Tom Herbertb17f7092014-11-04 09:06:56 -0800864 if (type & SKB_GSO_TUNNEL_REMCSUM) {
865 u16 csum_start = skb_checksum_start_offset(skb);
866 __be16 *pd = data;
867
868 if (csum_start < hdrlen)
869 return -EINVAL;
870
871 csum_start -= hdrlen;
872 pd[0] = htons(csum_start);
873 pd[1] = htons(csum_start + skb->csum_offset);
874
875 if (!skb_is_gso(skb)) {
876 skb->ip_summed = CHECKSUM_NONE;
877 skb->encapsulation = 0;
878 }
879
880 *flags |= GUE_PFLAG_REMCSUM;
881 data += GUE_PLEN_REMCSUM;
882 }
883
Tom Herbert5024c332014-11-04 09:06:53 -0800884 }
Tom Herbert63487ba2014-11-04 09:06:51 -0800885
Tom Herbertdc969b82016-05-18 09:06:15 -0700886 return 0;
887}
888EXPORT_SYMBOL(__gue_build_header);
889
890int gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
891 u8 *protocol, struct flowi4 *fl4)
892{
893 int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM ? SKB_GSO_UDP_TUNNEL_CSUM :
894 SKB_GSO_UDP_TUNNEL;
895 __be16 sport;
896 int err;
897
898 err = __gue_build_header(skb, e, protocol, &sport, type);
899 if (err)
900 return err;
901
Tom Herbert63487ba2014-11-04 09:06:51 -0800902 fou_build_udp(skb, e, fl4, protocol, sport);
903
904 return 0;
905}
906EXPORT_SYMBOL(gue_build_header);
907
Tom Herberta8c5f902014-11-12 11:54:09 -0800908#ifdef CONFIG_NET_FOU_IP_TUNNELS
909
Andi Kleen5eeb2922015-04-08 06:04:31 -0700910static const struct ip_tunnel_encap_ops fou_iptun_ops = {
Tom Herberta8c5f902014-11-12 11:54:09 -0800911 .encap_hlen = fou_encap_hlen,
912 .build_header = fou_build_header,
913};
914
Andi Kleen5eeb2922015-04-08 06:04:31 -0700915static const struct ip_tunnel_encap_ops gue_iptun_ops = {
Tom Herberta8c5f902014-11-12 11:54:09 -0800916 .encap_hlen = gue_encap_hlen,
917 .build_header = gue_build_header,
918};
919
920static int ip_tunnel_encap_add_fou_ops(void)
921{
922 int ret;
923
924 ret = ip_tunnel_encap_add_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
925 if (ret < 0) {
926 pr_err("can't add fou ops\n");
927 return ret;
928 }
929
930 ret = ip_tunnel_encap_add_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
931 if (ret < 0) {
932 pr_err("can't add gue ops\n");
933 ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
934 return ret;
935 }
936
937 return 0;
938}
939
940static void ip_tunnel_encap_del_fou_ops(void)
941{
942 ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
943 ip_tunnel_encap_del_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
944}
945
946#else
947
948static int ip_tunnel_encap_add_fou_ops(void)
949{
950 return 0;
951}
952
Thomas Graf882288c2014-11-13 12:48:21 +0100953static void ip_tunnel_encap_del_fou_ops(void)
Tom Herberta8c5f902014-11-12 11:54:09 -0800954{
955}
956
957#endif
958
WANG Cong02d793c2015-04-10 12:00:29 -0700959static __net_init int fou_init_net(struct net *net)
960{
961 struct fou_net *fn = net_generic(net, fou_net_id);
962
963 INIT_LIST_HEAD(&fn->fou_list);
964 mutex_init(&fn->fou_lock);
965 return 0;
966}
967
968static __net_exit void fou_exit_net(struct net *net)
969{
970 struct fou_net *fn = net_generic(net, fou_net_id);
971 struct fou *fou, *next;
972
973 /* Close all the FOU sockets */
974 mutex_lock(&fn->fou_lock);
975 list_for_each_entry_safe(fou, next, &fn->fou_list, list)
976 fou_release(fou);
977 mutex_unlock(&fn->fou_lock);
978}
979
980static struct pernet_operations fou_net_ops = {
981 .init = fou_init_net,
982 .exit = fou_exit_net,
983 .id = &fou_net_id,
984 .size = sizeof(struct fou_net),
985};
986
Tom Herbert234615512014-09-17 12:25:56 -0700987static int __init fou_init(void)
988{
989 int ret;
990
WANG Cong02d793c2015-04-10 12:00:29 -0700991 ret = register_pernet_device(&fou_net_ops);
992 if (ret)
Tom Herberta8c5f902014-11-12 11:54:09 -0800993 goto exit;
994
WANG Cong02d793c2015-04-10 12:00:29 -0700995 ret = genl_register_family_with_ops(&fou_nl_family,
996 fou_nl_ops);
Tom Herberta8c5f902014-11-12 11:54:09 -0800997 if (ret < 0)
WANG Cong02d793c2015-04-10 12:00:29 -0700998 goto unregister;
Tom Herberta8c5f902014-11-12 11:54:09 -0800999
WANG Cong02d793c2015-04-10 12:00:29 -07001000 ret = ip_tunnel_encap_add_fou_ops();
1001 if (ret == 0)
1002 return 0;
1003
1004 genl_unregister_family(&fou_nl_family);
1005unregister:
1006 unregister_pernet_device(&fou_net_ops);
Tom Herberta8c5f902014-11-12 11:54:09 -08001007exit:
Tom Herbert234615512014-09-17 12:25:56 -07001008 return ret;
1009}
1010
1011static void __exit fou_fini(void)
1012{
Tom Herberta8c5f902014-11-12 11:54:09 -08001013 ip_tunnel_encap_del_fou_ops();
Tom Herbert234615512014-09-17 12:25:56 -07001014 genl_unregister_family(&fou_nl_family);
WANG Cong02d793c2015-04-10 12:00:29 -07001015 unregister_pernet_device(&fou_net_ops);
Tom Herbert234615512014-09-17 12:25:56 -07001016}
1017
1018module_init(fou_init);
1019module_exit(fou_fini);
1020MODULE_AUTHOR("Tom Herbert <therbert@google.com>");
1021MODULE_LICENSE("GPL");