1 // SPDX-License-Identifier: GPL-2.0
7 #include <linux/stddef.h>
8 #include <linux/pkt_cls.h>
9 #include <linux/if_ether.h>
12 #include <linux/ipv6.h>
14 #include <bpf/bpf_helpers.h>
15 #include <bpf/bpf_endian.h>
18 # define ctx_ptr(field) (void *)(long)(field)
21 #define ip4_src 0xac100164 /* 172.16.1.100 */
22 #define ip4_dst 0xac100264 /* 172.16.2.100 */
24 #define ip6_src { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
25 0x00, 0x01, 0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe }
26 #define ip6_dst { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
27 0x00, 0x02, 0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe }
30 # define v6_equal(a, b) (a.s6_addr32[0] == b.s6_addr32[0] && \
31 a.s6_addr32[1] == b.s6_addr32[1] && \
32 a.s6_addr32[2] == b.s6_addr32[2] && \
33 a.s6_addr32[3] == b.s6_addr32[3])
41 struct bpf_map_def
SEC("maps") ifindex_map
= {
42 .type
= BPF_MAP_TYPE_ARRAY
,
43 .key_size
= sizeof(int),
44 .value_size
= sizeof(int),
48 static __always_inline
bool is_remote_ep_v4(struct __sk_buff
*skb
,
51 void *data_end
= ctx_ptr(skb
->data_end
);
52 void *data
= ctx_ptr(skb
->data
);
55 if (data
+ sizeof(struct ethhdr
) > data_end
)
58 ip4h
= (struct iphdr
*)(data
+ sizeof(struct ethhdr
));
59 if ((void *)(ip4h
+ 1) > data_end
)
62 return ip4h
->daddr
== addr
;
65 static __always_inline
bool is_remote_ep_v6(struct __sk_buff
*skb
,
68 void *data_end
= ctx_ptr(skb
->data_end
);
69 void *data
= ctx_ptr(skb
->data
);
72 if (data
+ sizeof(struct ethhdr
) > data_end
)
75 ip6h
= (struct ipv6hdr
*)(data
+ sizeof(struct ethhdr
));
76 if ((void *)(ip6h
+ 1) > data_end
)
79 return v6_equal(ip6h
->daddr
, addr
);
82 static __always_inline
int get_dev_ifindex(int which
)
84 int *ifindex
= bpf_map_lookup_elem(&ifindex_map
, &which
);
86 return ifindex
? *ifindex
: 0;
89 SEC("chk_egress") int tc_chk(struct __sk_buff
*skb
)
91 void *data_end
= ctx_ptr(skb
->data_end
);
92 void *data
= ctx_ptr(skb
->data
);
95 if (data
+ sizeof(struct ethhdr
) > data_end
)
98 return !raw
[0] && !raw
[1] && !raw
[2] ? TC_ACT_SHOT
: TC_ACT_OK
;
101 SEC("dst_ingress") int tc_dst(struct __sk_buff
*skb
)
103 __u8 zero
[ETH_ALEN
* 2];
104 bool redirect
= false;
106 switch (skb
->protocol
) {
107 case __bpf_constant_htons(ETH_P_IP
):
108 redirect
= is_remote_ep_v4(skb
, __bpf_constant_htonl(ip4_src
));
110 case __bpf_constant_htons(ETH_P_IPV6
):
111 redirect
= is_remote_ep_v6(skb
, (struct in6_addr
)ip6_src
);
118 __builtin_memset(&zero
, 0, sizeof(zero
));
119 if (bpf_skb_store_bytes(skb
, 0, &zero
, sizeof(zero
), 0) < 0)
122 return bpf_redirect_neigh(get_dev_ifindex(dev_src
), NULL
, 0, 0);
125 SEC("src_ingress") int tc_src(struct __sk_buff
*skb
)
127 __u8 zero
[ETH_ALEN
* 2];
128 bool redirect
= false;
130 switch (skb
->protocol
) {
131 case __bpf_constant_htons(ETH_P_IP
):
132 redirect
= is_remote_ep_v4(skb
, __bpf_constant_htonl(ip4_dst
));
134 case __bpf_constant_htons(ETH_P_IPV6
):
135 redirect
= is_remote_ep_v6(skb
, (struct in6_addr
)ip6_dst
);
142 __builtin_memset(&zero
, 0, sizeof(zero
));
143 if (bpf_skb_store_bytes(skb
, 0, &zero
, sizeof(zero
), 0) < 0)
146 return bpf_redirect_neigh(get_dev_ifindex(dev_dst
), NULL
, 0, 0);
149 char __license
[] SEC("license") = "GPL";