1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
# $OpenBSD: Makefile,v 1.7 2021/01/20 13:50:09 bluhm Exp $
# Copyright (c) 2021 Alexander Bluhm <bluhm@openbsd.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
# Basic testing of the pflog(4) interface. Create special routing
# domain, load rules into pf(4) regress anchor, tcpdump on pflog,
# send packets over lo(4), grep for expected result in tcpdump output.
# This test uses routing domain 11 and pflog interface number 11, 12, 13.
# Adjust it here, if you want to use something else.
N1 = 11
N2 = 12
N3 = 13
N = ${N1}
NUMS = ${N1} ${N2} ${N3}
IPS = 1 2 3 4 5 6 11 12 14
UID !!= id -u
.include <bsd.own.mk>
.if ! (make(clean) || make(cleandir) || make(obj))
PF_STATUS != ${SUDO} pfctl -si | sed -n 's/^Status: \([^ ]*\) .*/\1/p'
.if empty(PF_STATUS:MEnabled)
regress:
@echo pf status: "${PF_STATUS}"
@echo Enable pf to run this regress.
@echo SKIPPED
.endif
PF_SKIP != ${SUDO} pfctl -sI -v | sed -n 's/ (skip)//p'
.if ! empty(PF_SKIP:Mlo*:Nlo0)
regress:
@echo pf skip: "${PF_SKIP}"
@echo Do not set skip on interface lo or lo$N.
@echo SKIPPED
.endif
PF_ANCHOR != ${SUDO} pfctl -sr | sed -n 's/^anchor "\([^"]*\)" all$$/\1/p'
.if empty(PF_ANCHOR:Mregress)
regress:
@echo pf anchor: "${PF_ANCHOR}"
@echo Need anchor '"regress"' in pf.conf to load additional rules.
@echo SKIPPED
.endif
SYSCTL_FORWARDING != sysctl net.inet.ip.forwarding
SYSCTL_FORWARDING6 != sysctl net.inet6.ip6.forwarding
.if ${SYSCTL_FORWARDING:C/.*=//} != 1 || ${SYSCTL_FORWARDING6:C/.*=//} != 1
# Do not skip, but run tests. Although they fail, their packets are logged.
REGRESS_EXPECTED_FAILURES = run-ping-14 run-ping6-14
.endif
.endif
.PHONY: busy-rdomains ifconfig unconfig pfctl
REGRESS_SETUP_ONCE += busy-rdomains
busy-rdomains:
# Check if rdomains are busy.
@if /sbin/ifconfig | grep -v '^lo$N:' | grep ' rdomain $N '; then\
echo routing domain $N is already used >&2; exit 1; fi
REGRESS_SETUP_ONCE += ifconfig
ifconfig: unconfig
# Create and configure pflog and loopback interfaces.
.for n in ${NUMS}
${SUDO} ifconfig pflog$n create
.endfor
${SUDO} ifconfig lo$N rdomain $N
${SUDO} ifconfig lo$N inet 127.0.0.1/8
${SUDO} ifconfig lo$N inet6 ::1/128
.for i in ${IPS} 21 22 23 24
${SUDO} ifconfig lo$N inet 169.254.0.$i/32 alias
${SUDO} ifconfig lo$N inet6 fc00::$i/128
.endfor
REGRESS_CLEANUP += unconfig
unconfig: stamp-stop
# Destroy interfaces.
.for i in ${IPS} 21 22 23 24
-${SUDO} ifconfig lo$N inet 169.254.0.$i delete
-${SUDO} ifconfig lo$N inet6 fc00::$i delete
.endfor
-${SUDO} ifconfig lo$N inet 127.0.0.1 delete
-${SUDO} ifconfig lo$N inet6 ::1 delete
.for n in ${NUMS}
-${SUDO} ifconfig pflog$n destroy
.endfor
rm -f stamp-ifconfig
addr.py: Makefile
# Create python include file containing the addresses.
rm -f $@ $@.tmp
echo 'N="$N"' >>$@.tmp
echo 'LO="lo$N"' >>$@.tmp
.for var in N1 N2 N3
echo '${var}="${${var}}"' >>$@.tmp
echo 'PFLOG_${var}="pflog${${var}}"' >>$@.tmp
.endfor
mv $@.tmp $@
REGRESS_SETUP_ONCE += pfctl
pfctl: addr.py pf.conf
# Load the pf rules into the kernel.
cat addr.py ${.CURDIR}/pf.conf | /sbin/pfctl -n -f -
cat addr.py ${.CURDIR}/pf.conf | ${SUDO} pfctl -a regress -f -
# Run tcpdump on pflog devices.
DUMPCMD = /usr/sbin/tcpdump -l -e -vvv -s 2048 -ni
stamp-bpf: stamp-bpf-${N1} stamp-bpf-${N2} stamp-bpf-${N3}
sleep 2 # XXX
@date >$@
.for n in ${NUMS}
stamp-bpf-$n: stamp-ifconfig
rm -f pflog$n.tcpdump
${SUDO} pkill -f '^${DUMPCMD} pflog$n' || true
${SUDO} ${DUMPCMD} pflog$n >pflog$n.tcpdump &
rm -f stamp-stop
@date >$@
.endfor
stamp-stop:
sleep 2 # XXX
-${SUDO} pkill -f '^${DUMPCMD}'
rm -f stamp-bpf*
@date >$@
.for i in ${IPS}
REGRESS_TARGETS += run-ping-$i
run-ping-$i: stamp-bpf
ping -n -w 1 -c 1 -V $N 169.254.0.$i
REGRESS_TARGETS += run-ping6-$i
run-ping6-$i: stamp-bpf
ping6 -n -w 1 -c 1 -V $N fc00::$i
REGRESS_TARGETS += run-udp-$i
run-udp-$i: stamp-bpf
# ignore errors, just send packet fast
echo foo | nc -u -w 1 -V $N 169.254.0.$i discard &
REGRESS_TARGETS += run-udp6-$i
run-udp6-$i: stamp-bpf
# ignore errors, just send packet fast
echo foo | nc -u -w 1 -V $N fc00::$i discard &
.endfor
REGRESS_TARGETS += run-ping6-0
run-ping6-0: stamp-bpf
ping6 -n -w 1 -c 1 -V $N ::1
REGRESS_TARGETS += run-udp6-0
run-udp6-0: stamp-bpf
echo foo | nc -u -w 1 -V $N ::1 discard
.for n in ${NUMS}
REGRESS_TARGETS += run-bpf-$n
run-bpf-$n: stamp-stop
# show full logs
cat pflog$n.tcpdump
.endfor
REGRESS_TARGETS += run-bpf-nothing
run-bpf-nothing: stamp-stop
# rule with pflog${N3} is never used
! grep . pflog${N3}.tcpdump
REGRESS_TARGETS += run-bpf-everything
run-bpf-everything: stamp-stop
# rule with pflog${N2} matches on every packet
.for i in ${IPS}
grep 'regress\.1/.* > 169.254.0.$i:' pflog${N2}.tcpdump
.endfor
REGRESS_TARGETS += run-bpf-everything6
run-bpf-everything6: stamp-stop
# rule with pflog${N2} matches on every packet
.for i in ${IPS}
grep 'regress\.1/.* > fc00::$i:' pflog${N2}.tcpdump
.endfor
REGRESS_TARGETS += run-bpf-all
run-bpf-all: stamp-stop
# reply without keep state
grep 'regress\.3/.* 169.254.0.1 > 169.254.0.1:\
icmp: echo request' pflog${N1}.tcpdump
grep 'regress\.3/.* 169.254.0.1 > 169.254.0.1:\
icmp: echo reply' pflog${N1}.tcpdump
# no reply with keep state and without all
grep 'regress\.4/.* 169.254.0.2 > 169.254.0.2:\
icmp: echo request' pflog${N1}.tcpdump
! grep 'regress\.4/.* 169.254.0.2 >169.254.0.2:\
icmp: echo reply' pflog${N1}.tcpdump
# reply with keep state and with all
grep 'regress\.5/.* 169.254.0.3 > 169.254.0.3:\
icmp: echo request' pflog${N1}.tcpdump
# XXX anchor name missing
grep '/.* 169.254.0.3 > 169.254.0.3:\
icmp: echo reply' pflog${N1}.tcpdump
REGRESS_TARGETS += run-bpf-all6
run-bpf-all6: stamp-stop
# reply without keep state
grep 'regress\.11/.* fc00::1 > fc00::1:\
icmp6: echo request' pflog${N1}.tcpdump
grep 'regress\.11/.* fc00::1 > fc00::1:\
icmp6: echo reply' pflog${N1}.tcpdump
# no reply with keep state and without all
grep 'regress\.12/.* fc00::2 > fc00::2:\
icmp6: echo request' pflog${N1}.tcpdump
! grep 'regress\.12/.* fc00::2 > fc00::2:\
icmp6: echo reply' pflog${N1}.tcpdump
# reply with keep state and with all
grep 'regress\.13/.* fc00::3 > fc00::3:\
icmp6: echo request' pflog${N1}.tcpdump
# XXX anchor name missing
grep '/.* fc00::3 > fc00::3:\
icmp6: echo reply' pflog${N1}.tcpdump
REGRESS_TARGETS += run-bpf-user
run-bpf-user: stamp-stop
# out rule creates log entry with uid
grep 'regress\.6/.* pass out on lo$N: \[uid ${UID}, pid [0-9]*\]\
169.254.0.4\.[0-9]* > 169.254.0.4\.9:\
.* udp [0-9]' pflog${N1}.tcpdump
# in rule has no uid at log entry
grep 'regress\.6/.* pass in on lo$N:\
169.254.0.4\.[0-9]* > 169.254.0.4\.9:\
.* udp [0-9]' pflog${N1}.tcpdump
# icmp has no uid at log entry
grep 'regress\.6/.* pass out on lo$N:\
169.254.0.4 > 169.254.0\.4:\
icmp: echo request' pflog${N1}.tcpdump
# rule without user has no uid in log entry
grep 'regress\.3/.* pass out on lo$N:\
169.254.0.1\.[0-9]* > 169.254.0.1\.9:\
.* udp [0-9]' pflog${N1}.tcpdump
REGRESS_TARGETS += run-bpf-user6
run-bpf-user6: stamp-stop
# out rule creates log entry with uid
grep 'regress\.14/.* pass out on lo$N: \[uid ${UID}, pid [0-9]*\]\
fc00::4\.[0-9]* > fc00::4\.9:.* udp [0-9]' pflog${N1}.tcpdump
# in rule has no uid at log entry
grep 'regress\.14/.* pass in on lo$N:\
fc00::4\.[0-9]* > fc00::4\.9:.* udp [0-9]' pflog${N1}.tcpdump
# icmp has no uid at log entry
grep 'regress\.14/.* pass out on lo$N:\
fc00::4 > fc00::4: icmp6: echo request' pflog${N1}.tcpdump
# rule without user has no uid in log entry
grep 'regress\.11/.* pass out on lo$N:\
fc00::1\.[0-9]* > fc00::1\.9:.* udp [0-9]' pflog${N1}.tcpdump
run-bpf-matches run-bpf-matches6:
# XXX The log matches keyword seems to be totally broken.
# pf_log_matches() is never called. Investigate later.
@echo DISABLED
REGRESS_TARGETS += run-bpf-matches
run-bpf-matches: stamp-stop
grep 'regress\.9/.* .*: 169.254.0.6 > 169.254.0.6:\
icmp: echo request' pflog${N1}.tcpdump
! grep 'regress\.8/.* icmp: echo request' pflog${N1}.tcpdump
! grep 'regress\.7/.* icmp: echo request' pflog${N1}.tcpdump
REGRESS_TARGETS += run-bpf-rdr
run-bpf-rdr: stamp-stop
# loopback input logs redirected packet
grep 'regress\.2/.* pass in .*:.* 169.254.0.11 > 169.254.0.21:\
icmp: echo request' pflog${N1}.tcpdump
# loopback output redirects and logs original packet
grep 'regress\.18/.* pass out .*:.* 169.254.0.11 > 169.254.0.11:\
icmp: echo request' pflog${N1}.tcpdump
REGRESS_TARGETS += run-bpf-rdr6
run-bpf-rdr6: stamp-stop
# loopback input logs redirected packet
grep 'regress\.10/.* pass in .*:.* fc00::11 > fc00::21:\
icmp6: echo request' pflog${N1}.tcpdump
# loopback output redirects and logs original packet
grep 'regress\.20/.* pass out .*:.* fc00::11 > fc00::11:\
icmp6: echo request' pflog${N1}.tcpdump
REGRESS_TARGETS += run-bpf-nat
run-bpf-nat: stamp-stop
# loopback input logs redirected packet
grep 'regress\.2/.* pass in .*:.* 169.254.0.22 > 169.254.0.12:\
icmp: echo request' pflog${N1}.tcpdump
# loopback output redirects and logs original packet
grep 'regress\.19/.* pass out .*:.* 169.254.0.12 > 169.254.0.12:\
icmp: echo request' pflog${N1}.tcpdump
REGRESS_TARGETS += run-bpf-nat6
run-bpf-nat6: stamp-stop
# loopback input logs redirected packet
grep 'regress\.10/.* pass in .*:.* fc00::22 > fc00::12:\
icmp6: echo request' pflog${N1}.tcpdump
# loopback output redirects and logs original packet
grep 'regress\.21/.* pass out .*:.* fc00::12 > fc00::12:\
icmp6: echo request' pflog${N1}.tcpdump
REGRESS_TARGETS += run-bpf-af
run-bpf-af: stamp-stop
# pf in rule logs original IPv4 packet
grep 'regress\.22/.* pass in .*:.* 169.254.0.14 > 169.254.0.14:\
icmp: echo request' pflog${N1}.tcpdump
REGRESS_TARGETS += run-bpf-af6
run-bpf-af6: stamp-stop
# pf in rule logs original IPv6 packet
grep 'regress\.23/.* pass in .*:.* fc00::14 > fc00::14:\
icmp6: echo request' pflog${N1}.tcpdump
REGRESS_TARGETS += run-bpf-rewrite
run-bpf-rewrite: stamp-stop
# rdr-to address has been rewritten
grep '\[rewritten: src 169.254.0.11:[0-9]*, dst 169.254.0.21:[0-9]*\]\
169.254.0.11 > 169.254.0.11' pflog${N1}.tcpdump
# nat-to address has been rewritten
grep '\[rewritten: src 169.254.0.22:[0-9]*, dst 169.254.0.12:[0-9]*\]\
169.254.0.12 > 169.254.0.12' pflog${N1}.tcpdump
# af-to address has been rewritten
grep '\[rewritten: src fc00::23:[0-9]*, dst fc00::24:[0-9]*\]\
169.254.0.14 > 169.254.0.14' pflog${N1}.tcpdump
REGRESS_TARGETS += run-bpf-rewrite6
run-bpf-rewrite6: stamp-stop
# rdr-to address has been rewritten
grep '\[rewritten: src fc00::11:[0-9]*, dst fc00::21:[0-9]*\]\
fc00::11 > fc00::11' pflog${N1}.tcpdump
# nat-to address has been rewritten
grep '\[rewritten: src fc00::22:[0-9]*, dst fc00::12:[0-9]*\]\
fc00::12 > fc00::12' pflog${N1}.tcpdump
# af-to address has been rewritten
grep '\[rewritten: src 169.254.0.23:[0-9]*, dst 169.254.0.24:[0-9]*\]\
fc00::14 > fc00::14' pflog${N1}.tcpdump
CLEANFILES += addr.py *.pyc *.tcpdump *.log stamp-*
.include <bsd.regress.mk>
|