Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <stddef.h>
0003 #include <string.h>
0004 #include <linux/bpf.h>
0005 #include <linux/ip.h>
0006 #include <linux/ipv6.h>
0007 #include <bpf/bpf_helpers.h>
0008 #include <bpf/bpf_endian.h>
0009 
0010 struct grehdr {
0011     __be16 flags;
0012     __be16 protocol;
0013 };
0014 
0015 SEC("encap_gre")
0016 int bpf_lwt_encap_gre(struct __sk_buff *skb)
0017 {
0018     struct encap_hdr {
0019         struct iphdr iph;
0020         struct grehdr greh;
0021     } hdr;
0022     int err;
0023 
0024     memset(&hdr, 0, sizeof(struct encap_hdr));
0025 
0026     hdr.iph.ihl = 5;
0027     hdr.iph.version = 4;
0028     hdr.iph.ttl = 0x40;
0029     hdr.iph.protocol = 47;  /* IPPROTO_GRE */
0030 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
0031     hdr.iph.saddr = 0x640110ac;  /* 172.16.1.100 */
0032     hdr.iph.daddr = 0x641010ac;  /* 172.16.16.100 */
0033 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
0034     hdr.iph.saddr = 0xac100164;  /* 172.16.1.100 */
0035     hdr.iph.daddr = 0xac101064;  /* 172.16.16.100 */
0036 #else
0037 #error "Fix your compiler's __BYTE_ORDER__?!"
0038 #endif
0039     hdr.iph.tot_len = bpf_htons(skb->len + sizeof(struct encap_hdr));
0040 
0041     hdr.greh.protocol = skb->protocol;
0042 
0043     err = bpf_lwt_push_encap(skb, BPF_LWT_ENCAP_IP, &hdr,
0044                  sizeof(struct encap_hdr));
0045     if (err)
0046         return BPF_DROP;
0047 
0048     return BPF_LWT_REROUTE;
0049 }
0050 
0051 SEC("encap_gre6")
0052 int bpf_lwt_encap_gre6(struct __sk_buff *skb)
0053 {
0054     struct encap_hdr {
0055         struct ipv6hdr ip6hdr;
0056         struct grehdr greh;
0057     } hdr;
0058     int err;
0059 
0060     memset(&hdr, 0, sizeof(struct encap_hdr));
0061 
0062     hdr.ip6hdr.version = 6;
0063     hdr.ip6hdr.payload_len = bpf_htons(skb->len + sizeof(struct grehdr));
0064     hdr.ip6hdr.nexthdr = 47;  /* IPPROTO_GRE */
0065     hdr.ip6hdr.hop_limit = 0x40;
0066     /* fb01::1 */
0067     hdr.ip6hdr.saddr.s6_addr[0] = 0xfb;
0068     hdr.ip6hdr.saddr.s6_addr[1] = 1;
0069     hdr.ip6hdr.saddr.s6_addr[15] = 1;
0070     /* fb10::1 */
0071     hdr.ip6hdr.daddr.s6_addr[0] = 0xfb;
0072     hdr.ip6hdr.daddr.s6_addr[1] = 0x10;
0073     hdr.ip6hdr.daddr.s6_addr[15] = 1;
0074 
0075     hdr.greh.protocol = skb->protocol;
0076 
0077     err = bpf_lwt_push_encap(skb, BPF_LWT_ENCAP_IP, &hdr,
0078                  sizeof(struct encap_hdr));
0079     if (err)
0080         return BPF_DROP;
0081 
0082     return BPF_LWT_REROUTE;
0083 }
0084 
0085 char _license[] SEC("license") = "GPL";