|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +#include <test_progs.h> |
| 4 | +#include <sys/utsname.h> |
| 5 | +#include <uapi/linux/filter.h> |
| 6 | +#include "socket_filter.skel.h" |
| 7 | + |
| 8 | +static int duration; |
| 9 | + |
| 10 | +void do_test(void) |
| 11 | +{ |
| 12 | + /* the filter below is the tcpdump filter: |
| 13 | + * tcpdump "not ether host 3c37121a2b3c and not ether host 184ecbca2a3a \ |
| 14 | + * and not ether host 14130b4d3f47 and not ether host f0f61cf440b7 \ |
| 15 | + * and not ether host a84b4dedf471 and not ether host d022be17e1d7 \ |
| 16 | + * and not ether host 5c497967208b and not ether host 706655784d5b" |
| 17 | + */ |
| 18 | + struct sock_filter code[] = { |
| 19 | + { 0x20, 0, 0, 0x00000008 }, |
| 20 | + { 0x15, 0, 2, 0x121a2b3c }, |
| 21 | + { 0x28, 0, 0, 0x00000006 }, |
| 22 | + { 0x15, 60, 0, 0x00003c37 }, |
| 23 | + { 0x20, 0, 0, 0x00000002 }, |
| 24 | + { 0x15, 0, 2, 0x121a2b3c }, |
| 25 | + { 0x28, 0, 0, 0x00000000 }, |
| 26 | + { 0x15, 56, 0, 0x00003c37 }, |
| 27 | + { 0x20, 0, 0, 0x00000008 }, |
| 28 | + { 0x15, 0, 2, 0xcbca2a3a }, |
| 29 | + { 0x28, 0, 0, 0x00000006 }, |
| 30 | + { 0x15, 52, 0, 0x0000184e }, |
| 31 | + { 0x20, 0, 0, 0x00000002 }, |
| 32 | + { 0x15, 0, 2, 0xcbca2a3a }, |
| 33 | + { 0x28, 0, 0, 0x00000000 }, |
| 34 | + { 0x15, 48, 0, 0x0000184e }, |
| 35 | + { 0x20, 0, 0, 0x00000008 }, |
| 36 | + { 0x15, 0, 2, 0x0b4d3f47 }, |
| 37 | + { 0x28, 0, 0, 0x00000006 }, |
| 38 | + { 0x15, 44, 0, 0x00001413 }, |
| 39 | + { 0x20, 0, 0, 0x00000002 }, |
| 40 | + { 0x15, 0, 2, 0x0b4d3f47 }, |
| 41 | + { 0x28, 0, 0, 0x00000000 }, |
| 42 | + { 0x15, 40, 0, 0x00001413 }, |
| 43 | + { 0x20, 0, 0, 0x00000008 }, |
| 44 | + { 0x15, 0, 2, 0x1cf440b7 }, |
| 45 | + { 0x28, 0, 0, 0x00000006 }, |
| 46 | + { 0x15, 36, 0, 0x0000f0f6 }, |
| 47 | + { 0x20, 0, 0, 0x00000002 }, |
| 48 | + { 0x15, 0, 2, 0x1cf440b7 }, |
| 49 | + { 0x28, 0, 0, 0x00000000 }, |
| 50 | + { 0x15, 32, 0, 0x0000f0f6 }, |
| 51 | + { 0x20, 0, 0, 0x00000008 }, |
| 52 | + { 0x15, 0, 2, 0x4dedf471 }, |
| 53 | + { 0x28, 0, 0, 0x00000006 }, |
| 54 | + { 0x15, 28, 0, 0x0000a84b }, |
| 55 | + { 0x20, 0, 0, 0x00000002 }, |
| 56 | + { 0x15, 0, 2, 0x4dedf471 }, |
| 57 | + { 0x28, 0, 0, 0x00000000 }, |
| 58 | + { 0x15, 24, 0, 0x0000a84b }, |
| 59 | + { 0x20, 0, 0, 0x00000008 }, |
| 60 | + { 0x15, 0, 2, 0xbe17e1d7 }, |
| 61 | + { 0x28, 0, 0, 0x00000006 }, |
| 62 | + { 0x15, 20, 0, 0x0000d022 }, |
| 63 | + { 0x20, 0, 0, 0x00000002 }, |
| 64 | + { 0x15, 0, 2, 0xbe17e1d7 }, |
| 65 | + { 0x28, 0, 0, 0x00000000 }, |
| 66 | + { 0x15, 16, 0, 0x0000d022 }, |
| 67 | + { 0x20, 0, 0, 0x00000008 }, |
| 68 | + { 0x15, 0, 2, 0x7967208b }, |
| 69 | + { 0x28, 0, 0, 0x00000006 }, |
| 70 | + { 0x15, 12, 0, 0x00005c49 }, |
| 71 | + { 0x20, 0, 0, 0x00000002 }, |
| 72 | + { 0x15, 0, 2, 0x7967208b }, |
| 73 | + { 0x28, 0, 0, 0x00000000 }, |
| 74 | + { 0x15, 8, 0, 0x00005c49 }, |
| 75 | + { 0x20, 0, 0, 0x00000008 }, |
| 76 | + { 0x15, 0, 2, 0x55784d5b }, |
| 77 | + { 0x28, 0, 0, 0x00000006 }, |
| 78 | + { 0x15, 4, 0, 0x00007066 }, |
| 79 | + { 0x20, 0, 0, 0x00000002 }, |
| 80 | + { 0x15, 0, 3, 0x55784d5b }, |
| 81 | + { 0x28, 0, 0, 0x00000000 }, |
| 82 | + { 0x15, 0, 1, 0x00007066 }, |
| 83 | + { 0x06, 0, 0, 0x00000000 }, |
| 84 | + { 0x06, 0, 0, 0x00040000 }, |
| 85 | + }; |
| 86 | + struct sock_fprog bpf = { |
| 87 | + .len = ARRAY_SIZE(code), |
| 88 | + .filter = code, |
| 89 | + }; |
| 90 | + int ret, sock = 0; |
| 91 | + |
| 92 | + sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); |
| 93 | + if (CHECK(sock < 0, "create socket", "errno %d\n", errno)) |
| 94 | + return; |
| 95 | + |
| 96 | + ret = setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &bpf, sizeof(bpf)); |
| 97 | + CHECK(ret < 0, "attach filter", "errno %d\n", errno); |
| 98 | + |
| 99 | + close(sock); |
| 100 | +} |
| 101 | + |
| 102 | +void test_socket_filter(void) |
| 103 | +{ |
| 104 | + struct socket_filter *skel; |
| 105 | + struct utsname uts; |
| 106 | + int err; |
| 107 | + |
| 108 | + err = uname(&uts); |
| 109 | + if (err < 0) |
| 110 | + return; |
| 111 | + |
| 112 | + skel = socket_filter__open_and_load(); |
| 113 | + if (!ASSERT_OK_PTR(skel, "skel_open")) |
| 114 | + return; |
| 115 | + |
| 116 | + /* The filter JIT fails on armv6 */ |
| 117 | + if (strncmp(uts.machine, "armv6", strlen("armv6")) == 0 && |
| 118 | + skel->kconfig->CONFIG_BPF_JIT_ALWAYS_ON) |
| 119 | + test__skip(); |
| 120 | + else |
| 121 | + do_test(); |
| 122 | + |
| 123 | + socket_filter__destroy(skel); |
| 124 | +} |
0 commit comments