~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/bpf/progs/test_lwt_ip_encap.c

Version: ~ [ linux-5.19-rc3 ] ~ [ linux-5.18.5 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.48 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.123 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.199 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.248 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.284 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.319 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.302 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 #include <stddef.h>
  3 #include <string.h>
  4 #include <linux/bpf.h>
  5 #include <linux/ip.h>
  6 #include <linux/ipv6.h>
  7 #include <bpf/bpf_helpers.h>
  8 #include <bpf/bpf_endian.h>
  9 
 10 struct grehdr {
 11         __be16 flags;
 12         __be16 protocol;
 13 };
 14 
 15 SEC("encap_gre")
 16 int bpf_lwt_encap_gre(struct __sk_buff *skb)
 17 {
 18         struct encap_hdr {
 19                 struct iphdr iph;
 20                 struct grehdr greh;
 21         } hdr;
 22         int err;
 23 
 24         memset(&hdr, 0, sizeof(struct encap_hdr));
 25 
 26         hdr.iph.ihl = 5;
 27         hdr.iph.version = 4;
 28         hdr.iph.ttl = 0x40;
 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 */
 36 #else
 37 #error "Fix your compiler's __BYTE_ORDER__?!"
 38 #endif
 39         hdr.iph.tot_len = bpf_htons(skb->len + sizeof(struct encap_hdr));
 40 
 41         hdr.greh.protocol = skb->protocol;
 42 
 43         err = bpf_lwt_push_encap(skb, BPF_LWT_ENCAP_IP, &hdr,
 44                                  sizeof(struct encap_hdr));
 45         if (err)
 46                 return BPF_DROP;
 47 
 48         return BPF_LWT_REROUTE;
 49 }
 50 
 51 SEC("encap_gre6")
 52 int bpf_lwt_encap_gre6(struct __sk_buff *skb)
 53 {
 54         struct encap_hdr {
 55                 struct ipv6hdr ip6hdr;
 56                 struct grehdr greh;
 57         } hdr;
 58         int err;
 59 
 60         memset(&hdr, 0, sizeof(struct encap_hdr));
 61 
 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;
 66         /* fb01::1 */
 67         hdr.ip6hdr.saddr.s6_addr[0] = 0xfb;
 68         hdr.ip6hdr.saddr.s6_addr[1] = 1;
 69         hdr.ip6hdr.saddr.s6_addr[15] = 1;
 70         /* fb10::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;
 74 
 75         hdr.greh.protocol = skb->protocol;
 76 
 77         err = bpf_lwt_push_encap(skb, BPF_LWT_ENCAP_IP, &hdr,
 78                                  sizeof(struct encap_hdr));
 79         if (err)
 80                 return BPF_DROP;
 81 
 82         return BPF_LWT_REROUTE;
 83 }
 84 
 85 char _license[] SEC("license") = "GPL";
 86 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp