Skip to content

Commit

Permalink
ICMP BPF filter
Browse files Browse the repository at this point in the history
- Fix BPF filter for ICMPv6 and use shorter form
- Add tests for ICMP and ICMP6
  • Loading branch information
jelu committed Sep 10, 2024
1 parent fe480b5 commit aa14a9f
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/bpft.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void prepare_bpft(void)
else
len += text_add(&bpfl, "("); /* ( transports ... */
if (wanticmp) {
len += text_add(&bpfl, " ( ip proto 1 or ip proto 58 ) or");
len += text_add(&bpfl, " ( icmp or icmp6 ) or");
}
if (wantfrags) {
len += text_add(&bpfl, " ( ip[6:2] & 0x1fff != 0 or ip6[6] = 44 ) or");
Expand Down
8 changes: 5 additions & 3 deletions src/test/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ CLEANFILES = test*.log test*.trs \
test13.out \
test14.out \
test_regex_match.out \
edns.out
edns.out \
test_icmp.out

TESTS = test1.sh test2.sh test3.sh test4.sh test5.sh test6.sh test7.sh \
test8.sh test9.sh test10.sh test11.sh test12.sh test13.sh test14.sh \
test_regex_match.sh test_edns.sh
test_regex_match.sh test_edns.sh test_icmp.sh

EXTRA_DIST = $(TESTS) \
dns.gold dns.pcap \
Expand All @@ -36,4 +37,5 @@ EXTRA_DIST = $(TESTS) \
dns6.pcap test10.gold \
test14.gold \
test_regex_match.gold \
edns.pcap edns.gold
edns.pcap edns.gold \
gen_icmp.py icmp.pcap icmp.gold
10 changes: 10 additions & 0 deletions src/test/gen_icmp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import sys
from scapy.all import *

pcap = sys.argv[1]

pkts = []
pkts.append(Ether()/IPv6(dst="2001:db8:dead::1")/ICMPv6DestUnreach())
pkts.append(Ether()/IP(dst="0.0.0.0")/ICMP(type=3))

wrpcap(pcap, pkts)
4 changes: 4 additions & 0 deletions src/test/icmp.gold
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[48] 2024-09-10 08:52:52.252793 [#0 icmp.pcap 4095] \
[fd00::242:ac11:9].0 [2001:db8:dead::1].0
[28] 2024-09-10 08:52:52.253461 [#1 icmp.pcap 4095] \
[172.17.0.9].0 [0.0.0.0].0
Binary file added src/test/icmp.pcap
Binary file not shown.
11 changes: 11 additions & 0 deletions src/test/test_icmp.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh -xe

test -e icmp.pcap || ln -s "$srcdir/icmp.pcap" icmp.pcap

../dnscap -g -I -r icmp.pcap 2>test_icmp.out

mv test_icmp.out test_icmp.out.old
grep -v "^libgcov profiling error:" test_icmp.out.old > test_icmp.out
rm test_icmp.out.old

diff test_icmp.out "$srcdir/icmp.gold"

0 comments on commit aa14a9f

Please sign in to comment.