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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
|
import errno
import json
import os
import socket
import struct
import subprocess
import sys
from ctypes import c_byte
from ctypes import c_char
from ctypes import c_int
from ctypes import c_long
from ctypes import c_uint32
from ctypes import c_uint8
from ctypes import c_ulong
from ctypes import c_ushort
from ctypes import sizeof
from ctypes import Structure
from enum import Enum
from typing import Any
from typing import Dict
from typing import List
from typing import NamedTuple
from typing import Optional
from typing import Union
import pytest
from atf_python.sys.netpfil.ipfw.insns import Icmp6RejectCode
from atf_python.sys.netpfil.ipfw.insns import IcmpRejectCode
from atf_python.sys.netpfil.ipfw.insns import Insn
from atf_python.sys.netpfil.ipfw.insns import InsnComment
from atf_python.sys.netpfil.ipfw.insns import InsnEmpty
from atf_python.sys.netpfil.ipfw.insns import InsnIp
from atf_python.sys.netpfil.ipfw.insns import InsnIp6
from atf_python.sys.netpfil.ipfw.insns import InsnPorts
from atf_python.sys.netpfil.ipfw.insns import InsnProb
from atf_python.sys.netpfil.ipfw.insns import InsnProto
from atf_python.sys.netpfil.ipfw.insns import InsnReject
from atf_python.sys.netpfil.ipfw.insns import InsnTable
from atf_python.sys.netpfil.ipfw.insns import InsnU32
from atf_python.sys.netpfil.ipfw.insns import IpFwOpcode
from atf_python.sys.netpfil.ipfw.ioctl import CTlv
from atf_python.sys.netpfil.ipfw.ioctl import CTlvRule
from atf_python.sys.netpfil.ipfw.ioctl import IpFwTlvType
from atf_python.sys.netpfil.ipfw.ioctl import IpFwXRule
from atf_python.sys.netpfil.ipfw.ioctl import NTlv
from atf_python.sys.netpfil.ipfw.ioctl import Op3CmdType
from atf_python.sys.netpfil.ipfw.ioctl import RawRule
from atf_python.sys.netpfil.ipfw.ipfw import DebugIoReader
from atf_python.sys.netpfil.ipfw.utils import enum_from_int
from atf_python.utils import BaseTest
IPFW_PATH = "/sbin/ipfw"
def differ(w_obj, g_obj, w_stack=[], g_stack=[]):
if bytes(w_obj) == bytes(g_obj):
return True
num_objects = 0
for i, w_child in enumerate(w_obj.obj_list):
if i >= len(g_obj.obj_list):
print("MISSING object from chain {}".format(" / ".join(w_stack)))
w_child.print_obj()
print("==========================")
return False
g_child = g_obj.obj_list[i]
if bytes(w_child) == bytes(g_child):
num_objects += 1
continue
w_stack.append(w_obj.obj_name)
g_stack.append(g_obj.obj_name)
if not differ(w_child, g_child, w_stack, g_stack):
return False
break
if num_objects == len(w_obj.obj_list) and num_objects < len(g_obj.obj_list):
g_child = g_obj.obj_list[num_objects]
print("EXTRA object from chain {}".format(" / ".join(g_stack)))
g_child.print_obj()
print("==========================")
return False
print("OBJECTS DIFFER")
print("WANTED CHAIN: {}".format(" / ".join(w_stack)))
w_obj.print_obj()
w_obj.print_obj_hex()
print("==========================")
print("GOT CHAIN: {}".format(" / ".join(g_stack)))
g_obj.print_obj()
g_obj.print_obj_hex()
print("==========================")
return False
class TestAddRule(BaseTest):
def compile_rule(self, out):
tlvs = []
if "objs" in out:
tlvs.append(CTlv(IpFwTlvType.IPFW_TLV_TBLNAME_LIST, out["objs"]))
rule = RawRule(rulenum=out.get("rulenum", 0), obj_list=out["insns"])
tlvs.append(CTlvRule(obj_list=[rule]))
return IpFwXRule(Op3CmdType.IP_FW_XADD, tlvs)
def verify_rule(self, in_data: str, out_data):
# Prepare the desired output
expected = self.compile_rule(out_data)
reader = DebugIoReader(IPFW_PATH)
ioctls = reader.get_records(in_data)
assert len(ioctls) == 1 # Only 1 ioctl request expected
got = ioctls[0]
if not differ(expected, got):
print("=> CMD: {}".format(in_data))
print("=> WANTED:")
expected.print_obj()
print("==========================")
print("=> GOT:")
got.print_obj()
print("==========================")
assert bytes(got) == bytes(expected)
@pytest.mark.parametrize(
"rule",
[
pytest.param(
{
"in": "add 200 allow ip from any to any",
"out": {
"insns": [InsnEmpty(IpFwOpcode.O_ACCEPT)],
"rulenum": 200,
},
},
id="test_rulenum",
),
pytest.param(
{
"in": "add allow ip from { 1.2.3.4 or 2.3.4.5 } to any",
"out": {
"insns": [
InsnIp(IpFwOpcode.O_IP_SRC, ip="1.2.3.4", is_or=True),
InsnIp(IpFwOpcode.O_IP_SRC, ip="2.3.4.5"),
InsnEmpty(IpFwOpcode.O_ACCEPT),
],
},
},
id="test_or",
),
pytest.param(
{
"in": "add allow ip from table(AAA) to table(BBB)",
"out": {
"objs": [
NTlv(IpFwTlvType.IPFW_TLV_TBL_NAME, idx=1, name="AAA"),
NTlv(IpFwTlvType.IPFW_TLV_TBL_NAME, idx=2, name="BBB"),
],
"insns": [
InsnU32(IpFwOpcode.O_IP_SRC_LOOKUP, u32=1),
InsnU32(IpFwOpcode.O_IP_DST_LOOKUP, u32=2),
InsnEmpty(IpFwOpcode.O_ACCEPT),
],
},
},
id="test_tables",
),
pytest.param(
{
"in": "add allow ip from any to 1.2.3.4 // test comment",
"out": {
"insns": [
InsnIp(IpFwOpcode.O_IP_DST, ip="1.2.3.4"),
InsnComment(comment="test comment"),
InsnEmpty(IpFwOpcode.O_ACCEPT),
],
},
},
id="test_comment",
),
pytest.param(
{
"in": "add tcp-setmss 123 ip from any to 1.2.3.4",
"out": {
"objs": [
NTlv(IpFwTlvType.IPFW_TLV_EACTION, idx=1, name="tcp-setmss"),
],
"insns": [
InsnIp(IpFwOpcode.O_IP_DST, ip="1.2.3.4"),
InsnU32(IpFwOpcode.O_EXTERNAL_ACTION, u32=1),
Insn(IpFwOpcode.O_EXTERNAL_DATA, arg1=123),
],
},
},
id="test_eaction_tcp-setmss",
),
pytest.param(
{
"in": "add eaction ntpv6 AAA ip from any to 1.2.3.4",
"out": {
"objs": [
NTlv(IpFwTlvType.IPFW_TLV_EACTION, idx=1, name="ntpv6"),
NTlv(0, idx=2, name="AAA"),
],
"insns": [
InsnIp(IpFwOpcode.O_IP_DST, ip="1.2.3.4"),
InsnU32(IpFwOpcode.O_EXTERNAL_ACTION, u32=1),
InsnU32(IpFwOpcode.O_EXTERNAL_INSTANCE, u32=2),
],
},
},
id="test_eaction_ntp",
),
pytest.param(
{
"in": "add // test comment",
"out": {
"insns": [
InsnComment(comment="test comment"),
Insn(IpFwOpcode.O_COUNT),
],
},
},
id="test_action_comment",
),
pytest.param(
{
"in": "add check-state :OUT // test comment",
"out": {
"objs": [
NTlv(IpFwTlvType.IPFW_TLV_STATE_NAME, idx=1, name="OUT"),
],
"insns": [
InsnComment(comment="test comment"),
InsnU32(IpFwOpcode.O_CHECK_STATE, u32=1),
],
},
},
id="test_check_state",
),
pytest.param(
{
"in": "add allow tcp from any to any keep-state :OUT",
"out": {
"objs": [
NTlv(IpFwTlvType.IPFW_TLV_STATE_NAME, idx=1, name="OUT"),
],
"insns": [
InsnU32(IpFwOpcode.O_PROBE_STATE, u32=1),
Insn(IpFwOpcode.O_PROTO, arg1=6),
InsnU32(IpFwOpcode.O_KEEP_STATE, u32=1),
InsnEmpty(IpFwOpcode.O_ACCEPT),
],
},
},
id="test_keep_state",
),
pytest.param(
{
"in": "add allow tcp from any to any record-state",
"out": {
"objs": [
NTlv(IpFwTlvType.IPFW_TLV_STATE_NAME, idx=1, name="default"),
],
"insns": [
Insn(IpFwOpcode.O_PROTO, arg1=6),
InsnU32(IpFwOpcode.O_KEEP_STATE, u32=1),
InsnEmpty(IpFwOpcode.O_ACCEPT),
],
},
},
id="test_record_state",
),
],
)
def test_add_rule(self, rule):
"""Tests if the compiled rule is sane and matches the spec"""
self.verify_rule(rule["in"], rule["out"])
@pytest.mark.parametrize(
"action",
[
pytest.param(("allow", InsnEmpty(IpFwOpcode.O_ACCEPT)), id="test_allow"),
pytest.param(
(
"abort",
Insn(IpFwOpcode.O_REJECT, arg1=IcmpRejectCode.ICMP_REJECT_ABORT),
),
id="abort",
),
pytest.param(
(
"abort6",
Insn(
IpFwOpcode.O_UNREACH6, arg1=Icmp6RejectCode.ICMP6_UNREACH_ABORT
),
),
id="abort6",
),
pytest.param(("accept", InsnEmpty(IpFwOpcode.O_ACCEPT)), id="accept"),
pytest.param(("deny", InsnEmpty(IpFwOpcode.O_DENY)), id="deny"),
pytest.param(
(
"reject",
Insn(IpFwOpcode.O_REJECT, arg1=IcmpRejectCode.ICMP_UNREACH_HOST),
),
id="reject",
),
pytest.param(
(
"reset",
Insn(IpFwOpcode.O_REJECT, arg1=IcmpRejectCode.ICMP_REJECT_RST),
),
id="reset",
),
pytest.param(
(
"reset6",
Insn(IpFwOpcode.O_UNREACH6, arg1=Icmp6RejectCode.ICMP6_UNREACH_RST),
),
id="reset6",
),
pytest.param(
(
"unreach port",
InsnReject(
IpFwOpcode.O_REJECT, arg1=IcmpRejectCode.ICMP_UNREACH_PORT
),
),
id="unreach_port",
),
pytest.param(
(
"unreach port",
InsnReject(
IpFwOpcode.O_REJECT, arg1=IcmpRejectCode.ICMP_UNREACH_PORT
),
),
id="unreach_port",
),
pytest.param(
(
"unreach needfrag",
InsnReject(
IpFwOpcode.O_REJECT, arg1=IcmpRejectCode.ICMP_UNREACH_NEEDFRAG
),
),
id="unreach_needfrag",
),
pytest.param(
(
"unreach needfrag 1420",
InsnReject(
IpFwOpcode.O_REJECT,
arg1=IcmpRejectCode.ICMP_UNREACH_NEEDFRAG,
mtu=1420,
),
),
id="unreach_needfrag_mtu",
),
pytest.param(
(
"unreach6 port",
Insn(
IpFwOpcode.O_UNREACH6,
arg1=Icmp6RejectCode.ICMP6_DST_UNREACH_NOPORT,
),
),
id="unreach6_port",
),
pytest.param(("count", InsnEmpty(IpFwOpcode.O_COUNT)), id="count"),
# TOK_NAT
pytest.param(
("queue 42", Insn(IpFwOpcode.O_QUEUE, arg1=42)), id="queue_42"
),
pytest.param(("pipe 42", Insn(IpFwOpcode.O_PIPE, arg1=42)), id="pipe_42"),
pytest.param(
("skipto 42", InsnU32(IpFwOpcode.O_SKIPTO, u32=42)), id="skipto_42"
),
pytest.param(
("netgraph 42", Insn(IpFwOpcode.O_NETGRAPH, arg1=42)), id="netgraph_42"
),
pytest.param(
("ngtee 42", Insn(IpFwOpcode.O_NGTEE, arg1=42)), id="ngtee_42"
),
pytest.param(
("divert 42", Insn(IpFwOpcode.O_DIVERT, arg1=42)), id="divert_42"
),
pytest.param(
("divert natd", Insn(IpFwOpcode.O_DIVERT, arg1=8668)), id="divert_natd"
),
pytest.param(("tee 42", Insn(IpFwOpcode.O_TEE, arg1=42)), id="tee_42"),
pytest.param(
("call 420", InsnU32(IpFwOpcode.O_CALLRETURN, u32=420)), id="call_420"
),
# TOK_FORWARD
pytest.param(
("setfib 1", Insn(IpFwOpcode.O_SETFIB, arg1=1 | 0x8000)),
id="setfib_1",
marks=pytest.mark.skip("needs net.fibs>1"),
),
pytest.param(
("setdscp 42", Insn(IpFwOpcode.O_SETDSCP, arg1=42 | 0x8000)),
id="setdscp_42",
),
pytest.param(("reass", InsnEmpty(IpFwOpcode.O_REASS)), id="reass"),
pytest.param(
("return", InsnU32(IpFwOpcode.O_CALLRETURN, is_not=True)), id="return"
),
],
)
def test_add_action(self, action):
"""Tests if the rule action is compiled properly"""
rule_in = "add {} ip from any to any".format(action[0])
rule_out = {"insns": [action[1]]}
self.verify_rule(rule_in, rule_out)
@pytest.mark.parametrize(
"insn",
[
pytest.param(
{
"in": "add prob 0.7 allow ip from any to any",
"out": InsnProb(prob=0.7),
},
id="test_prob",
),
pytest.param(
{
"in": "add allow tcp from any to any",
"out": InsnProto(arg1=6),
},
id="test_proto",
),
pytest.param(
{
"in": "add allow ip from any to any 57",
"out": InsnPorts(IpFwOpcode.O_IP_DSTPORT, port_pairs=[57, 57]),
},
id="test_ports",
),
],
)
def test_add_single_instruction(self, insn):
"""Tests if the compiled rule is sane and matches the spec"""
# Prepare the desired output
out = {
"insns": [insn["out"], InsnEmpty(IpFwOpcode.O_ACCEPT)],
}
self.verify_rule(insn["in"], out)
@pytest.mark.parametrize(
"opcode",
[
pytest.param(IpFwOpcode.O_IP_SRCPORT, id="src"),
pytest.param(IpFwOpcode.O_IP_DSTPORT, id="dst"),
],
)
@pytest.mark.parametrize(
"params",
[
pytest.param(
{
"in": "57",
"out": [(57, 57)],
},
id="test_single",
),
pytest.param(
{
"in": "57-59",
"out": [(57, 59)],
},
id="test_range",
),
pytest.param(
{
"in": "57-59,41",
"out": [(57, 59), (41, 41)],
},
id="test_ranges",
),
],
)
def test_add_ports(self, params, opcode):
if opcode == IpFwOpcode.O_IP_DSTPORT:
txt = "add allow ip from any to any " + params["in"]
else:
txt = "add allow ip from any " + params["in"] + " to any"
out = {
"insns": [
InsnPorts(opcode, port_pairs=params["out"]),
InsnEmpty(IpFwOpcode.O_ACCEPT),
]
}
self.verify_rule(txt, out)
|