1 // SPDX-License-Identifier: GPL-2.0
6 #include <linux/ipv6.h>
7 #include <bpf/bpf_helpers.h>
8 #include <bpf/bpf_endian.h>
16 int bpf_lwt_encap_gre(struct __sk_buff
*skb
)
24 memset(&hdr
, 0, sizeof(struct encap_hdr
));
29 hdr
.iph
.protocol
= 47; /* IPPROTO_GRE */
30 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
31 hdr
.iph
.saddr
= 0x640110ac; /* 172.16.1.100 */
32 hdr
.iph
.daddr
= 0x641010ac; /* 172.16.16.100 */
33 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
34 hdr
.iph
.saddr
= 0xac100164; /* 172.16.1.100 */
35 hdr
.iph
.daddr
= 0xac101064; /* 172.16.16.100 */
37 #error "Fix your compiler's __BYTE_ORDER__?!"
39 hdr
.iph
.tot_len
= bpf_htons(skb
->len
+ sizeof(struct encap_hdr
));
41 hdr
.greh
.protocol
= skb
->protocol
;
43 err
= bpf_lwt_push_encap(skb
, BPF_LWT_ENCAP_IP
, &hdr
,
44 sizeof(struct encap_hdr
));
48 return BPF_LWT_REROUTE
;
52 int bpf_lwt_encap_gre6(struct __sk_buff
*skb
)
55 struct ipv6hdr ip6hdr
;
60 memset(&hdr
, 0, sizeof(struct encap_hdr
));
62 hdr
.ip6hdr
.version
= 6;
63 hdr
.ip6hdr
.payload_len
= bpf_htons(skb
->len
+ sizeof(struct grehdr
));
64 hdr
.ip6hdr
.nexthdr
= 47; /* IPPROTO_GRE */
65 hdr
.ip6hdr
.hop_limit
= 0x40;
67 hdr
.ip6hdr
.saddr
.s6_addr
[0] = 0xfb;
68 hdr
.ip6hdr
.saddr
.s6_addr
[1] = 1;
69 hdr
.ip6hdr
.saddr
.s6_addr
[15] = 1;
71 hdr
.ip6hdr
.daddr
.s6_addr
[0] = 0xfb;
72 hdr
.ip6hdr
.daddr
.s6_addr
[1] = 0x10;
73 hdr
.ip6hdr
.daddr
.s6_addr
[15] = 1;
75 hdr
.greh
.protocol
= skb
->protocol
;
77 err
= bpf_lwt_push_encap(skb
, BPF_LWT_ENCAP_IP
, &hdr
,
78 sizeof(struct encap_hdr
));
82 return BPF_LWT_REROUTE
;
85 char _license
[] SEC("license") = "GPL";