Skip to content

Commit 89790e3

Browse files
committed
add pure_reflector
1 parent e60f72c commit 89790e3

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

Makefile

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
XDP_TARGETS := reflector
1+
XDP_TARGETS := reflector pure_reflector
22
USER_TARGETS :=
33

4+
# compile parameter:
5+
OUR_MAC := "{ 0x11, 0x22, 0x33, 0x44, 0x55, 0x66}"
6+
47
LLC ?= llc
58
CLANG ?= clang
69
CC ?= gcc
@@ -60,6 +63,7 @@ $(XDP_OBJ): %.o: %.c Makefile $(OBJECT_LIBBPF)
6063
$(CLANG) -S \
6164
-target bpf \
6265
-D __BPF_TRACING__ \
66+
-DOUR_MAC="$(OUR_MAC)" \
6367
$(BPF_CFLAGS) \
6468
-Wall \
6569
-Wno-unused-value \

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,18 @@ This project is similarly structured as the
4949
[XDP tutorial](https://github.com/xdp-project/xdp-tutorial),
5050
which is a great resource for learning XDP prorgamming.
5151

52+
## Reflector variant: `pure_reflector`
53+
54+
The normal reflector properly switches the src and dst MAC address before sending the packet back into the network.
55+
As a variant, `pure_reflector` does not touch MAC addresses.
56+
It can be used to reflect packets back into the network that don't belong to this host.
57+
For that compile it with your local hosts MAC address (so that these packet are not reflected), and allow the host to accept packets destined for other hosts.
58+
```bash
59+
rm pure_reflector.o
60+
make pure_reflector.o OUR_MAC="{ 0x77, 0x96, 0x91, 0xb3, 0x8b, 0x77}"
61+
sudo ip link set <DEV> promisc on
62+
sudo ip link set <DEV> xdpgeneric obj pure_reflector.o sec xdp
63+
```
64+
5265
## License
5366
This project is distributed under [MIT](LICENSE) license.

pure_reflector.c

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <linux/bpf.h>
2+
#include <linux/if_ether.h>
3+
#include <bpf/bpf_helpers.h>
4+
5+
SEC("xdp")
6+
/* Reflect ethernet frames back to the sender */
7+
int xdp_reflector(struct xdp_md *ctx)
8+
{
9+
void *data_end = (void *)(long)ctx->data_end;
10+
void *data = (void *)(long)ctx->data;
11+
struct ethhdr *eth = data;
12+
13+
if (data + sizeof(*eth) > data_end)
14+
return XDP_DROP;
15+
16+
__u8 our_mac[ETH_ALEN] = OUR_MAC;
17+
if (__builtin_memcmp(eth->h_dest, our_mac, ETH_ALEN) == 0) {
18+
// packet is for us. Let it pass.
19+
return XDP_PASS; // TODO
20+
}
21+
22+
// packet is for someone else. Send it back into the network.
23+
return XDP_TX;
24+
}
25+
26+
char _license[] SEC("license") = "MIT";

0 commit comments

Comments
 (0)