xref: /linux/net/core/lwt_bpf.c (revision 5e96760b25fdedbbf9312827f1f237d6b18b03e8)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2016 Thomas Graf <tgraf@tgraf.ch>
3  */
4 
5 #include <linux/filter.h>
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/skbuff.h>
9 #include <linux/types.h>
10 #include <linux/bpf.h>
11 #include <net/flow.h>
12 #include <net/lwtunnel.h>
13 #include <net/gre.h>
14 #include <net/ip.h>
15 #include <net/ip6_route.h>
16 
17 struct bpf_lwt_prog {
18 	struct bpf_prog *prog;
19 	char *name;
20 };
21 
22 struct bpf_lwt {
23 	struct bpf_lwt_prog in;
24 	struct bpf_lwt_prog out;
25 	struct bpf_lwt_prog xmit;
26 	int family;
27 };
28 
29 #define MAX_PROG_NAME 256
30 
31 static inline struct bpf_lwt *bpf_lwt_lwtunnel(struct lwtunnel_state *lwt)
32 {
33 	return (struct bpf_lwt *)lwt->data;
34 }
35 
36 #define NO_REDIRECT false
37 #define CAN_REDIRECT true
38 
39 static int run_lwt_bpf(struct sk_buff *skb, struct bpf_lwt_prog *lwt,
40 		       struct dst_entry *dst, bool can_redirect)
41 {
42 	struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx;
43 	int ret;
44 
45 	/* Disabling BH is needed to protect per-CPU bpf_redirect_info between
46 	 * BPF prog and skb_do_redirect().
47 	 */
48 	local_bh_disable();
49 	bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx);
50 	bpf_compute_data_pointers(skb);
51 	ret = bpf_prog_run_save_cb(lwt->prog, skb);
52 
53 	switch (ret) {
54 	case BPF_OK:
55 	case BPF_LWT_REROUTE:
56 		break;
57 
58 	case BPF_REDIRECT:
59 		if (unlikely(!can_redirect)) {
60 			pr_warn_once("Illegal redirect return code in prog %s\n",
61 				     lwt->name ? : "<unknown>");
62 			ret = BPF_OK;
63 		} else {
64 			skb_reset_mac_header(skb);
65 			skb_do_redirect(skb);
66 			ret = BPF_REDIRECT;
67 		}
68 		break;
69 
70 	case BPF_DROP:
71 		kfree_skb(skb);
72 		ret = -EPERM;
73 		break;
74 
75 	default:
76 		pr_warn_once("bpf-lwt: Illegal return value %u, expect packet loss\n", ret);
77 		kfree_skb(skb);
78 		ret = -EINVAL;
79 		break;
80 	}
81 
82 	bpf_net_ctx_clear(bpf_net_ctx);
83 	local_bh_enable();
84 
85 	return ret;
86 }
87 
88 /*
89  * skb->cb still describes the IP header that was the first one before the
90  * prog ran. The option offsets in IPCB(skb)->opt are wrong for a pushed
91  * header, and a header of the other family needs the other layout. iif
92  * stays: behind a VRF skb->skb_iif is the L3 master and not the port.
93  * bpf_lwt_push_encap() cannot do this, because bpf_prog_run_save_cb() can
94  * restore skb->cb after the prog.
95  */
96 static void bpf_lwt_reset_cb(struct sk_buff *skb, __be16 proto)
97 {
98 	int iif;
99 
100 	if (skb->protocol == proto) {
101 		if (proto == htons(ETH_P_IP))
102 			memset(&IPCB(skb)->opt, 0, sizeof(IPCB(skb)->opt));
103 	} else if (skb->protocol == htons(ETH_P_IP)) {
104 		iif = IP6CB(skb)->iif;
105 		memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
106 		IPCB(skb)->iif = iif;
107 	} else {
108 		iif = IPCB(skb)->iif;
109 		memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
110 		IP6CB(skb)->iif = iif;
111 	}
112 }
113 
114 static int bpf_lwt_input_reroute(struct sk_buff *skb)
115 {
116 	enum skb_drop_reason reason;
117 	int err = -EINVAL;
118 
119 	if (skb->protocol == htons(ETH_P_IP)) {
120 		struct net_device *dev = skb_dst(skb)->dev;
121 		const struct iphdr *iph = ip_hdr(skb);
122 
123 		dev_hold(dev);
124 		skb_dst_drop(skb);
125 		reason = ip_route_input_noref(skb, iph->daddr, iph->saddr,
126 					      ip4h_dscp(iph), dev);
127 		err = reason ? -EINVAL : 0;
128 		dev_put(dev);
129 	} else if (skb->protocol == htons(ETH_P_IPV6)) {
130 		skb_dst_drop(skb);
131 		if (ipv6_mod_enabled()) {
132 			ip6_route_input(skb);
133 			err = skb_dst(skb)->error;
134 		} else {
135 			err = -EAFNOSUPPORT;
136 		}
137 	} else {
138 		err = -EAFNOSUPPORT;
139 	}
140 
141 	if (err)
142 		goto err;
143 	return dst_input(skb);
144 
145 err:
146 	kfree_skb(skb);
147 	return err;
148 }
149 
150 static int bpf_input(struct sk_buff *skb)
151 {
152 	struct dst_entry *dst = skb_dst(skb);
153 	__be16 proto = skb->protocol;
154 	unsigned int len = skb->len;
155 	struct bpf_lwt *bpf;
156 	int ret;
157 
158 	bpf = bpf_lwt_lwtunnel(dst->lwtstate);
159 	if (bpf->in.prog) {
160 		ret = run_lwt_bpf(skb, &bpf->in, dst, NO_REDIRECT);
161 		if (ret < 0)
162 			return ret;
163 		/* An lwt_in prog can change the length only with a push. */
164 		if (skb->len != len)
165 			bpf_lwt_reset_cb(skb, proto);
166 		if (ret == BPF_LWT_REROUTE)
167 			return bpf_lwt_input_reroute(skb);
168 		/*
169 		 * orig_input() is for the old protocol only, see bpf_xmit().
170 		 * It also needs a dst of the old family. BPF_LWT_ENCAP_SEG6
171 		 * puts an IPv6 dst into the skb.
172 		 */
173 		if (skb->protocol != proto ||
174 		    skb_dst(skb)->ops->family != dst->ops->family) {
175 			kfree_skb(skb);
176 			return -EINVAL;
177 		}
178 	}
179 
180 	if (unlikely(!dst->lwtstate->orig_input)) {
181 		kfree_skb(skb);
182 		return -EINVAL;
183 	}
184 
185 	return dst->lwtstate->orig_input(skb);
186 }
187 
188 static int bpf_output(struct net *net, struct sock *sk, struct sk_buff *skb)
189 {
190 	struct dst_entry *dst = skb_dst(skb);
191 	struct bpf_lwt *bpf;
192 	int ret;
193 
194 	bpf = bpf_lwt_lwtunnel(dst->lwtstate);
195 	if (bpf->out.prog) {
196 		ret = run_lwt_bpf(skb, &bpf->out, dst, NO_REDIRECT);
197 		if (ret < 0)
198 			return ret;
199 	}
200 
201 	if (unlikely(!dst->lwtstate->orig_output)) {
202 		pr_warn_once("orig_output not set on dst for prog %s\n",
203 			     bpf->out.name);
204 		kfree_skb(skb);
205 		return -EINVAL;
206 	}
207 
208 	return dst->lwtstate->orig_output(net, sk, skb);
209 }
210 
211 static int xmit_check_headroom(struct sk_buff *skb, int hroom)
212 {
213 	if (skb_headroom(skb) < hroom) {
214 		int nhead = hroom - skb_headroom(skb);
215 
216 		if (pskb_expand_head(skb, nhead, 0, GFP_ATOMIC))
217 			return -ENOMEM;
218 	}
219 
220 	return 0;
221 }
222 
223 static int bpf_lwt_xmit_reroute(struct sk_buff *skb)
224 {
225 	struct net_device *l3mdev = l3mdev_master_dev_rcu(skb_dst(skb)->dev);
226 	int oif = l3mdev ? l3mdev->ifindex : 0;
227 	struct dst_entry *dst = NULL;
228 	int err = -EAFNOSUPPORT;
229 	struct sock *sk;
230 	struct net *net;
231 	bool ipv4;
232 
233 	if (skb->protocol == htons(ETH_P_IP))
234 		ipv4 = true;
235 	else if (skb->protocol == htons(ETH_P_IPV6))
236 		ipv4 = false;
237 	else
238 		goto err;
239 
240 	sk = sk_to_full_sk(skb->sk);
241 	if (sk) {
242 		if (sk->sk_bound_dev_if)
243 			oif = sk->sk_bound_dev_if;
244 		net = sock_net(sk);
245 	} else {
246 		net = dev_net(skb_dst(skb)->dev);
247 	}
248 
249 	if (ipv4) {
250 		struct iphdr *iph = ip_hdr(skb);
251 		struct flowi4 fl4 = {};
252 		struct rtable *rt;
253 
254 		fl4.flowi4_oif = oif;
255 		fl4.flowi4_mark = skb->mark;
256 		fl4.flowi4_uid = sock_net_uid(net, sk);
257 		fl4.flowi4_dscp = ip4h_dscp(iph);
258 		fl4.flowi4_flags = FLOWI_FLAG_ANYSRC;
259 		fl4.flowi4_proto = iph->protocol;
260 		fl4.daddr = iph->daddr;
261 		fl4.saddr = iph->saddr;
262 
263 		rt = ip_route_output_key(net, &fl4);
264 		if (IS_ERR(rt)) {
265 			err = PTR_ERR(rt);
266 			goto err;
267 		}
268 		dst = &rt->dst;
269 	} else {
270 		struct ipv6hdr *iph6 = ipv6_hdr(skb);
271 		struct flowi6 fl6 = {};
272 
273 		fl6.flowi6_oif = oif;
274 		fl6.flowi6_mark = skb->mark;
275 		fl6.flowi6_uid = sock_net_uid(net, sk);
276 		fl6.flowlabel = ip6_flowinfo(iph6);
277 		fl6.flowi6_proto = iph6->nexthdr;
278 		fl6.daddr = iph6->daddr;
279 		fl6.saddr = iph6->saddr;
280 
281 		dst = ip6_dst_lookup_flow(net, skb->sk, &fl6, NULL);
282 		if (IS_ERR(dst)) {
283 			err = PTR_ERR(dst);
284 			goto err;
285 		}
286 	}
287 	if (unlikely(dst->error)) {
288 		err = dst->error;
289 		dst_release(dst);
290 		goto err;
291 	}
292 
293 	/* Although skb header was reserved in bpf_lwt_push_ip_encap(), it
294 	 * was done for the previous dst, so we are doing it here again, in
295 	 * case the new dst needs much more space. The call below is a noop
296 	 * if there is enough header space in skb.
297 	 */
298 	err = skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev));
299 	if (unlikely(err)) {
300 		dst_release(dst);
301 		goto err;
302 	}
303 
304 	skb_dst_drop(skb);
305 	skb_dst_set(skb, dst);
306 
307 	err = dst_output(dev_net(skb_dst(skb)->dev), skb->sk, skb);
308 	if (unlikely(err))
309 		return net_xmit_errno(err);
310 
311 	/* ip[6]_finish_output2 understand LWTUNNEL_XMIT_DONE */
312 	return LWTUNNEL_XMIT_DONE;
313 
314 err:
315 	kfree_skb(skb);
316 	return err;
317 }
318 
319 static int bpf_xmit(struct sk_buff *skb)
320 {
321 	struct dst_entry *dst = skb_dst(skb);
322 	struct bpf_lwt *bpf;
323 
324 	bpf = bpf_lwt_lwtunnel(dst->lwtstate);
325 	if (bpf->xmit.prog) {
326 		int hroom = LL_RESERVED_SPACE(dst->dev);
327 		__be16 proto = skb->protocol;
328 		int ret;
329 
330 		ret = run_lwt_bpf(skb, &bpf->xmit, dst, CAN_REDIRECT);
331 		switch (ret) {
332 		case BPF_OK:
333 			/* If the header changed, e.g. via bpf_lwt_push_encap,
334 			 * BPF_LWT_REROUTE below should have been used if the
335 			 * protocol was also changed.
336 			 */
337 			if (skb->protocol != proto) {
338 				kfree_skb(skb);
339 				return -EINVAL;
340 			}
341 			/* If the header was expanded, headroom might be too
342 			 * small for the L2 header to come, expand as needed.
343 			 * neigh_hh_output() copies the cached header in
344 			 * HH_DATA_MOD aligned chunks, so match the reservation
345 			 * made before LWT xmit.
346 			 */
347 			ret = xmit_check_headroom(skb, hroom);
348 			if (unlikely(ret)) {
349 				kfree_skb(skb);
350 				return ret;
351 			}
352 
353 			return LWTUNNEL_XMIT_CONTINUE;
354 		case BPF_REDIRECT:
355 			return LWTUNNEL_XMIT_DONE;
356 		case BPF_LWT_REROUTE:
357 			/* An lwt_xmit prog can also write to the options. */
358 			bpf_lwt_reset_cb(skb, proto);
359 			return bpf_lwt_xmit_reroute(skb);
360 		default:
361 			return ret;
362 		}
363 	}
364 
365 	return LWTUNNEL_XMIT_CONTINUE;
366 }
367 
368 static void bpf_lwt_prog_destroy(struct bpf_lwt_prog *prog)
369 {
370 	if (prog->prog)
371 		bpf_prog_put(prog->prog);
372 
373 	kfree(prog->name);
374 }
375 
376 static void bpf_destroy_state(struct lwtunnel_state *lwt)
377 {
378 	struct bpf_lwt *bpf = bpf_lwt_lwtunnel(lwt);
379 
380 	bpf_lwt_prog_destroy(&bpf->in);
381 	bpf_lwt_prog_destroy(&bpf->out);
382 	bpf_lwt_prog_destroy(&bpf->xmit);
383 }
384 
385 static const struct nla_policy bpf_prog_policy[LWT_BPF_PROG_MAX + 1] = {
386 	[LWT_BPF_PROG_FD]   = { .type = NLA_U32, },
387 	[LWT_BPF_PROG_NAME] = { .type = NLA_NUL_STRING,
388 				.len = MAX_PROG_NAME },
389 };
390 
391 static int bpf_parse_prog(struct nlattr *attr, struct bpf_lwt_prog *prog,
392 			  enum bpf_prog_type type)
393 {
394 	struct nlattr *tb[LWT_BPF_PROG_MAX + 1];
395 	struct bpf_prog *p;
396 	int ret;
397 	u32 fd;
398 
399 	ret = nla_parse_nested_deprecated(tb, LWT_BPF_PROG_MAX, attr,
400 					  bpf_prog_policy, NULL);
401 	if (ret < 0)
402 		return ret;
403 
404 	if (!tb[LWT_BPF_PROG_FD] || !tb[LWT_BPF_PROG_NAME])
405 		return -EINVAL;
406 
407 	prog->name = nla_memdup(tb[LWT_BPF_PROG_NAME], GFP_ATOMIC);
408 	if (!prog->name)
409 		return -ENOMEM;
410 
411 	fd = nla_get_u32(tb[LWT_BPF_PROG_FD]);
412 	p = bpf_prog_get_type(fd, type);
413 	if (IS_ERR(p))
414 		return PTR_ERR(p);
415 
416 	prog->prog = p;
417 
418 	return 0;
419 }
420 
421 static const struct nla_policy bpf_nl_policy[LWT_BPF_MAX + 1] = {
422 	[LWT_BPF_IN]		= { .type = NLA_NESTED, },
423 	[LWT_BPF_OUT]		= { .type = NLA_NESTED, },
424 	[LWT_BPF_XMIT]		= { .type = NLA_NESTED, },
425 	[LWT_BPF_XMIT_HEADROOM]	= { .type = NLA_U32 },
426 };
427 
428 static int bpf_build_state(struct net *net, struct nlattr *nla,
429 			   unsigned int family, const void *cfg,
430 			   struct lwtunnel_state **ts,
431 			   struct netlink_ext_ack *extack)
432 {
433 	struct nlattr *tb[LWT_BPF_MAX + 1];
434 	struct lwtunnel_state *newts;
435 	struct bpf_lwt *bpf;
436 	int ret;
437 
438 	if (family != AF_INET && family != AF_INET6)
439 		return -EAFNOSUPPORT;
440 
441 	ret = nla_parse_nested_deprecated(tb, LWT_BPF_MAX, nla, bpf_nl_policy,
442 					  extack);
443 	if (ret < 0)
444 		return ret;
445 
446 	if (!tb[LWT_BPF_IN] && !tb[LWT_BPF_OUT] && !tb[LWT_BPF_XMIT])
447 		return -EINVAL;
448 
449 	newts = lwtunnel_state_alloc(sizeof(*bpf));
450 	if (!newts)
451 		return -ENOMEM;
452 
453 	newts->type = LWTUNNEL_ENCAP_BPF;
454 	bpf = bpf_lwt_lwtunnel(newts);
455 
456 	if (tb[LWT_BPF_IN]) {
457 		newts->flags |= LWTUNNEL_STATE_INPUT_REDIRECT;
458 		ret = bpf_parse_prog(tb[LWT_BPF_IN], &bpf->in,
459 				     BPF_PROG_TYPE_LWT_IN);
460 		if (ret  < 0)
461 			goto errout;
462 	}
463 
464 	if (tb[LWT_BPF_OUT]) {
465 		newts->flags |= LWTUNNEL_STATE_OUTPUT_REDIRECT;
466 		ret = bpf_parse_prog(tb[LWT_BPF_OUT], &bpf->out,
467 				     BPF_PROG_TYPE_LWT_OUT);
468 		if (ret < 0)
469 			goto errout;
470 	}
471 
472 	if (tb[LWT_BPF_XMIT]) {
473 		newts->flags |= LWTUNNEL_STATE_XMIT_REDIRECT;
474 		ret = bpf_parse_prog(tb[LWT_BPF_XMIT], &bpf->xmit,
475 				     BPF_PROG_TYPE_LWT_XMIT);
476 		if (ret < 0)
477 			goto errout;
478 	}
479 
480 	if (tb[LWT_BPF_XMIT_HEADROOM]) {
481 		u32 headroom = nla_get_u32(tb[LWT_BPF_XMIT_HEADROOM]);
482 
483 		if (headroom > LWT_BPF_MAX_HEADROOM) {
484 			ret = -ERANGE;
485 			goto errout;
486 		}
487 
488 		newts->headroom = headroom;
489 	}
490 
491 	bpf->family = family;
492 	*ts = newts;
493 
494 	return 0;
495 
496 errout:
497 	bpf_destroy_state(newts);
498 	kfree(newts);
499 	return ret;
500 }
501 
502 static int bpf_fill_lwt_prog(struct sk_buff *skb, int attr,
503 			     struct bpf_lwt_prog *prog)
504 {
505 	struct nlattr *nest;
506 
507 	if (!prog->prog)
508 		return 0;
509 
510 	nest = nla_nest_start_noflag(skb, attr);
511 	if (!nest)
512 		return -EMSGSIZE;
513 
514 	if (prog->name &&
515 	    nla_put_string(skb, LWT_BPF_PROG_NAME, prog->name))
516 		return -EMSGSIZE;
517 
518 	return nla_nest_end(skb, nest);
519 }
520 
521 static int bpf_fill_encap_info(struct sk_buff *skb, struct lwtunnel_state *lwt)
522 {
523 	struct bpf_lwt *bpf = bpf_lwt_lwtunnel(lwt);
524 
525 	if (bpf_fill_lwt_prog(skb, LWT_BPF_IN, &bpf->in) < 0 ||
526 	    bpf_fill_lwt_prog(skb, LWT_BPF_OUT, &bpf->out) < 0 ||
527 	    bpf_fill_lwt_prog(skb, LWT_BPF_XMIT, &bpf->xmit) < 0)
528 		return -EMSGSIZE;
529 
530 	return 0;
531 }
532 
533 static int bpf_encap_nlsize(struct lwtunnel_state *lwtstate)
534 {
535 	int nest_len = nla_total_size(sizeof(struct nlattr)) +
536 		       nla_total_size(MAX_PROG_NAME) + /* LWT_BPF_PROG_NAME */
537 		       0;
538 
539 	return nest_len + /* LWT_BPF_IN */
540 	       nest_len + /* LWT_BPF_OUT */
541 	       nest_len + /* LWT_BPF_XMIT */
542 	       0;
543 }
544 
545 static int bpf_lwt_prog_cmp(struct bpf_lwt_prog *a, struct bpf_lwt_prog *b)
546 {
547 	/* FIXME:
548 	 * The LWT state is currently rebuilt for delete requests which
549 	 * results in a new bpf_prog instance. Comparing names for now.
550 	 */
551 	if (!a->name && !b->name)
552 		return 0;
553 
554 	if (!a->name || !b->name)
555 		return 1;
556 
557 	return strcmp(a->name, b->name);
558 }
559 
560 static int bpf_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
561 {
562 	struct bpf_lwt *a_bpf = bpf_lwt_lwtunnel(a);
563 	struct bpf_lwt *b_bpf = bpf_lwt_lwtunnel(b);
564 
565 	return bpf_lwt_prog_cmp(&a_bpf->in, &b_bpf->in) ||
566 	       bpf_lwt_prog_cmp(&a_bpf->out, &b_bpf->out) ||
567 	       bpf_lwt_prog_cmp(&a_bpf->xmit, &b_bpf->xmit);
568 }
569 
570 static const struct lwtunnel_encap_ops bpf_encap_ops = {
571 	.build_state	= bpf_build_state,
572 	.destroy_state	= bpf_destroy_state,
573 	.input		= bpf_input,
574 	.output		= bpf_output,
575 	.xmit		= bpf_xmit,
576 	.fill_encap	= bpf_fill_encap_info,
577 	.get_encap_size = bpf_encap_nlsize,
578 	.cmp_encap	= bpf_encap_cmp,
579 	.owner		= THIS_MODULE,
580 };
581 
582 static int handle_gso_type(struct sk_buff *skb, unsigned int gso_type,
583 			   int encap_len)
584 {
585 	struct skb_shared_info *shinfo = skb_shinfo(skb);
586 
587 	gso_type |= SKB_GSO_DODGY;
588 	shinfo->gso_type |= gso_type;
589 	skb_decrease_gso_size(shinfo, encap_len);
590 	shinfo->gso_segs = 0;
591 	return 0;
592 }
593 
594 static int handle_gso_encap(struct sk_buff *skb, bool ipv4, int encap_len)
595 {
596 	int next_hdr_offset;
597 	void *next_hdr;
598 	__u8 protocol;
599 
600 	/* SCTP and UDP_L4 gso need more nuanced handling than what
601 	 * handle_gso_type() does above: skb_decrease_gso_size() is not enough.
602 	 * So at the moment only TCP GSO packets are let through.
603 	 */
604 	if (!(skb_shinfo(skb)->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
605 		return -ENOTSUPP;
606 
607 	if (ipv4) {
608 		protocol = ip_hdr(skb)->protocol;
609 		next_hdr_offset = sizeof(struct iphdr);
610 		next_hdr = skb_network_header(skb) + next_hdr_offset;
611 	} else {
612 		protocol = ipv6_hdr(skb)->nexthdr;
613 		next_hdr_offset = sizeof(struct ipv6hdr);
614 		next_hdr = skb_network_header(skb) + next_hdr_offset;
615 	}
616 
617 	switch (protocol) {
618 	case IPPROTO_GRE:
619 		next_hdr_offset += sizeof(struct gre_base_hdr);
620 		if (next_hdr_offset > encap_len)
621 			return -EINVAL;
622 
623 		if (((struct gre_base_hdr *)next_hdr)->flags & GRE_CSUM)
624 			return handle_gso_type(skb, SKB_GSO_GRE_CSUM,
625 					       encap_len);
626 		return handle_gso_type(skb, SKB_GSO_GRE, encap_len);
627 
628 	case IPPROTO_UDP:
629 		next_hdr_offset += sizeof(struct udphdr);
630 		if (next_hdr_offset > encap_len)
631 			return -EINVAL;
632 
633 		if (((struct udphdr *)next_hdr)->check)
634 			return handle_gso_type(skb, SKB_GSO_UDP_TUNNEL_CSUM,
635 					       encap_len);
636 		return handle_gso_type(skb, SKB_GSO_UDP_TUNNEL, encap_len);
637 
638 	case IPPROTO_IP:
639 	case IPPROTO_IPV6:
640 		if (ipv4)
641 			return handle_gso_type(skb, SKB_GSO_IPXIP4, encap_len);
642 		else
643 			return handle_gso_type(skb, SKB_GSO_IPXIP6, encap_len);
644 
645 	default:
646 		return -EPROTONOSUPPORT;
647 	}
648 }
649 
650 int bpf_lwt_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len, bool ingress)
651 {
652 	bool is_udp_tunnel;
653 	struct iphdr *iph;
654 	bool ipv4;
655 	int err;
656 
657 	if (unlikely(len < sizeof(struct iphdr) || len > LWT_BPF_MAX_HEADROOM))
658 		return -EINVAL;
659 
660 	/* validate protocol and length */
661 	iph = (struct iphdr *)hdr;
662 	if (iph->version == 4) {
663 		ipv4 = true;
664 		if (unlikely(len < iph->ihl * 4))
665 			return -EINVAL;
666 		is_udp_tunnel = iph->protocol == IPPROTO_UDP;
667 		if (unlikely(is_udp_tunnel && len < iph->ihl * 4 + sizeof(struct udphdr)))
668 			return -EINVAL;
669 	} else if (iph->version == 6) {
670 		ipv4 = false;
671 		if (unlikely(len < sizeof(struct ipv6hdr)))
672 			return -EINVAL;
673 		is_udp_tunnel = ((struct ipv6hdr *)iph)->nexthdr == NEXTHDR_UDP;
674 		if (unlikely(is_udp_tunnel && len < sizeof(struct ipv6hdr) + sizeof(struct udphdr)))
675 			return -EINVAL;
676 	} else {
677 		return -EINVAL;
678 	}
679 
680 	if (ingress)
681 		err = skb_cow_head(skb, len + skb->mac_len);
682 	else
683 		err = skb_cow_head(skb,
684 				   len + LL_RESERVED_SPACE(skb_dst(skb)->dev));
685 	if (unlikely(err))
686 		return err;
687 
688 	/* push the encap headers and fix pointers */
689 	skb_reset_inner_headers(skb);
690 	skb_reset_inner_mac_header(skb);  /* mac header is not yet set */
691 	skb_set_inner_protocol(skb, skb->protocol);
692 	skb->encapsulation = 1;
693 	skb_push(skb, len);
694 	if (ingress)
695 		skb_postpush_rcsum(skb, iph, len);
696 	skb_reset_network_header(skb);
697 	/*
698 	 * The new header goes over the MAC header. On ingress move it first.
699 	 * On egress the packet gets a new one, so forget the old one.
700 	 */
701 	if (ingress)
702 		skb_mac_header_rebuild(skb);
703 	else
704 		skb_unset_mac_header(skb);
705 	if (is_udp_tunnel) {
706 		size_t iph_sz = ipv4 ? iph->ihl * 4 : sizeof(struct ipv6hdr);
707 
708 		skb_set_transport_header(skb, skb_network_offset(skb) + iph_sz);
709 	}
710 	memcpy(skb_network_header(skb), hdr, len);
711 	bpf_compute_data_pointers(skb);
712 	skb_clear_hash(skb);
713 
714 	if (ipv4) {
715 		skb->protocol = htons(ETH_P_IP);
716 		iph = ip_hdr(skb);
717 
718 		if (!iph->check)
719 			iph->check = ip_fast_csum((unsigned char *)iph,
720 						  iph->ihl);
721 	} else {
722 		skb->protocol = htons(ETH_P_IPV6);
723 	}
724 
725 	if (skb_is_gso(skb))
726 		return handle_gso_encap(skb, ipv4, len);
727 
728 	return 0;
729 }
730 
731 static int __init bpf_lwt_init(void)
732 {
733 	return lwtunnel_encap_add_ops(&bpf_encap_ops, LWTUNNEL_ENCAP_BPF);
734 }
735 
736 subsys_initcall(bpf_lwt_init)
737