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

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

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ 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 <linux/bpf.h>
  3 #include <bpf/bpf_helpers.h>
  4 #include <bpf/bpf_endian.h>
  5 
  6 #include <linux/ip.h>
  7 #include <linux/in.h>
  8 #include <linux/if_ether.h>
  9 #include <linux/pkt_cls.h>
 10 #include <stdbool.h>
 11 
 12 int lookup_status;
 13 bool test_xdp;
 14 bool tcp_skc;
 15 
 16 #define CUR_NS BPF_F_CURRENT_NETNS
 17 
 18 static void socket_lookup(void *ctx, void *data_end, void *data)
 19 {
 20         struct ethhdr *eth = data;
 21         struct bpf_sock_tuple *tp;
 22         struct bpf_sock *sk;
 23         struct iphdr *iph;
 24         int tplen;
 25 
 26         if (eth + 1 > data_end)
 27                 return;
 28 
 29         if (eth->h_proto != bpf_htons(ETH_P_IP))
 30                 return;
 31 
 32         iph = (struct iphdr *)(eth + 1);
 33         if (iph + 1 > data_end)
 34                 return;
 35 
 36         tp = (struct bpf_sock_tuple *)&iph->saddr;
 37         tplen = sizeof(tp->ipv4);
 38         if ((void *)tp + tplen > data_end)
 39                 return;
 40 
 41         switch (iph->protocol) {
 42         case IPPROTO_TCP:
 43                 if (tcp_skc)
 44                         sk = bpf_skc_lookup_tcp(ctx, tp, tplen, CUR_NS, 0);
 45                 else
 46                         sk = bpf_sk_lookup_tcp(ctx, tp, tplen, CUR_NS, 0);
 47                 break;
 48         case IPPROTO_UDP:
 49                 sk = bpf_sk_lookup_udp(ctx, tp, tplen, CUR_NS, 0);
 50                 break;
 51         default:
 52                 return;
 53         }
 54 
 55         lookup_status = 0;
 56 
 57         if (sk) {
 58                 bpf_sk_release(sk);
 59                 lookup_status = 1;
 60         }
 61 }
 62 
 63 SEC("tc")
 64 int tc_socket_lookup(struct __sk_buff *skb)
 65 {
 66         void *data_end = (void *)(long)skb->data_end;
 67         void *data = (void *)(long)skb->data;
 68 
 69         if (test_xdp)
 70                 return TC_ACT_UNSPEC;
 71 
 72         socket_lookup(skb, data_end, data);
 73         return TC_ACT_UNSPEC;
 74 }
 75 
 76 SEC("xdp")
 77 int xdp_socket_lookup(struct xdp_md *xdp)
 78 {
 79         void *data_end = (void *)(long)xdp->data_end;
 80         void *data = (void *)(long)xdp->data;
 81 
 82         if (!test_xdp)
 83                 return XDP_PASS;
 84 
 85         socket_lookup(xdp, data_end, data);
 86         return XDP_PASS;
 87 }
 88 
 89 char _license[] SEC("license") = "GPL";
 90 

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

kernel.org | git.kernel.org | LWN.net | Project Home | 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.

sflogo.php