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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
|
import pytest
import ctypes
import socket
import ipaddress
import re
from atf_python.sys.net.tools import ToolsHelper
from atf_python.sys.net.vnet import VnetTestTemplate
import time
SCTP_UNORDERED = 0x0400
SCTP_NODELAY = 0x00000004
SCTP_SET_PEER_PRIMARY_ADDR = 0x00000006
SCTP_PRIMARY_ADDR = 0x00000007
SCTP_BINDX_ADD_ADDR = 0x00008001
SCTP_BINDX_REM_ADDR = 0x00008002
class sockaddr_in(ctypes.Structure):
_fields_ = [
('sin_len', ctypes.c_uint8),
('sin_family', ctypes.c_uint8),
('sin_port', ctypes.c_uint16),
('sin_addr', ctypes.c_uint32),
('sin_zero', ctypes.c_int8 * 8)
]
class sockaddr_in6(ctypes.Structure):
_fields_ = [
('sin6_len', ctypes.c_uint8),
('sin6_family', ctypes.c_uint8),
('sin6_port', ctypes.c_uint16),
('sin6_flowinfo', ctypes.c_uint32),
('sin6_addr', ctypes.c_uint8 * 16),
('sin6_scope_id', ctypes.c_uint32)
]
class sockaddr_storage(ctypes.Union):
_fields_ = [
("v4", sockaddr_in),
("v6", sockaddr_in6)
]
class sctp_sndrcvinfo(ctypes.Structure):
_fields_ = [
('sinfo_stream', ctypes.c_uint16),
('sinfo_ssn', ctypes.c_uint16),
('sinfo_flags', ctypes.c_uint16),
('sinfo_ppid', ctypes.c_uint32),
('sinfo_context', ctypes.c_uint32),
('sinfo_timetolive', ctypes.c_uint32),
('sinfo_tsn', ctypes.c_uint32),
('sinfo_cumtsn', ctypes.c_uint32),
('sinfo_assoc_id', ctypes.c_uint32),
]
class sctp_setprim(ctypes.Structure):
_fields_ = [
('ssp_addr', sockaddr_storage),
('ssp_pad', ctypes.c_int8 * (128 - 16)),
('ssp_assoc_id', ctypes.c_uint32),
('ssp_padding', ctypes.c_uint32)
]
def to_sockaddr(ip, port):
ip = ipaddress.ip_address(ip)
if ip.version == 4:
addr = sockaddr_in()
addr.sin_len = ctypes.sizeof(addr)
addr.sin_family = socket.AF_INET
addr.sin_port = socket.htons(port)
addr.sin_addr = socket.htonl(int.from_bytes(ip.packed, byteorder='big'))
else:
assert ip.version == 6
addr = sockaddr_in6()
addr.sin6_len = ctypes.sizeof(addr)
addr.sin6_family = socket.AF_INET6
addr.sin6_port = socket.htons(port)
for i in range(0, 16):
addr.sin6_addr[i] = ip.packed[i]
return addr
class SCTPServer:
def __init__(self, family, port=1234):
self._libc = ctypes.CDLL("libc.so.7", use_errno=True)
self._listen_fd = self._libc.socket(family, socket.SOCK_STREAM, socket.IPPROTO_SCTP)
if self._listen_fd == -1:
raise Exception("Failed to create socket")
if family == socket.AF_INET:
srvaddr = sockaddr_in()
srvaddr.sin_len = ctypes.sizeof(srvaddr)
srvaddr.sin_family = socket.AF_INET
srvaddr.sin_port = socket.htons(port)
srvaddr.sin_addr = socket.INADDR_ANY
else:
srvaddr = sockaddr_in6()
srvaddr.sin6_len = ctypes.sizeof(srvaddr)
srvaddr.sin6_family = family
srvaddr.sin6_port = socket.htons(port)
# Leave sin_addr empty, because ANY is zero
ret = self._libc.bind(self._listen_fd, ctypes.pointer(srvaddr),
ctypes.sizeof(srvaddr))
if ret == -1:
raise Exception("Failed to bind: %d" % ctypes.get_errno())
ret = self._libc.listen(self._listen_fd, 2)
if ret == -1:
raise Exception("Failed to listen")
def _to_string(self, buf):
return ''.join([chr(int.from_bytes(i, byteorder='big')) for i in buf]).rstrip('\x00')
def accept(self, vnet):
fd = self._libc.accept(self._listen_fd, 0, 0)
if fd < 0:
raise Exception("Failed to accept")
print("SCTPServer: connection opened")
while True:
rcvinfo = sctp_sndrcvinfo()
flags = ctypes.c_int()
buf = ctypes.create_string_buffer(128)
# Receive a single message, and inform the other vnet about it.
ret = self._libc.sctp_recvmsg(fd, ctypes.cast(buf, ctypes.c_void_p), 128,
0, 0, ctypes.pointer(rcvinfo), ctypes.pointer(flags))
if ret < 0:
print("SCTPServer: connection closed")
return
if ret == 0:
continue
rcvd = {}
rcvd['ppid'] = socket.ntohl(rcvinfo.sinfo_ppid)
rcvd['data'] = self._to_string(buf)
rcvd['len'] = ret
print(rcvd)
vnet.pipe.send(rcvd)
class SCTPClient:
def __init__(self, ip, port=1234, fromaddr=None):
self._libc = ctypes.CDLL("libc.so.7", use_errno=True)
if ipaddress.ip_address(ip).version == 4:
family = socket.AF_INET
else:
family = socket.AF_INET6
self._fd = self._libc.socket(family, socket.SOCK_STREAM,
socket.IPPROTO_SCTP)
if self._fd == -1:
raise Exception("Failed to open socket")
if fromaddr is not None:
addr = to_sockaddr(fromaddr, 0)
ret = self._libc.bind(self._fd, ctypes.pointer(addr), ctypes.sizeof(addr))
if ret != 0:
print("bind() => %d", ctypes.get_errno())
raise
addr = to_sockaddr(ip, port)
ret = self._libc.connect(self._fd, ctypes.pointer(addr), ctypes.sizeof(addr))
if ret == -1:
raise Exception("Failed to connect")
# Enable NODELAY, because otherwise the sending host may wait for SACK
# on a data chunk we've removed
enable = ctypes.c_int(1)
ret = self._libc.setsockopt(self._fd, socket.IPPROTO_SCTP,
SCTP_NODELAY, ctypes.pointer(enable), 4)
def newpeer(self, addr):
print("newpeer(%s)" % (addr))
setp = sctp_setprim()
a = to_sockaddr(addr, 0)
if type(a) is sockaddr_in:
setp.ssp_addr.v4 = a
else:
assert type(a) is sockaddr_in6
setp.ssp_addr.v6 = a
ret = self._libc.setsockopt(self._fd, socket.IPPROTO_SCTP,
SCTP_PRIMARY_ADDR, ctypes.pointer(setp), ctypes.sizeof(setp))
if ret != 0:
print("errno %d" % ctypes.get_errno())
raise Exception(ctypes.get_errno())
def newprimary(self, addr):
print("newprimary(%s)" % (addr))
# Strictly speaking needs to be struct sctp_setpeerprim, but that's
# identical to sctp_setprim
setp = sctp_setprim()
a = to_sockaddr(addr, 0)
if type(a) is sockaddr_in:
setp.ssp_addr.v4 = a
else:
assert type(a) is sockaddr_in6
setp.ssp_addr.v6 = a
ret = self._libc.setsockopt(self._fd, socket.IPPROTO_SCTP,
SCTP_SET_PEER_PRIMARY_ADDR, ctypes.pointer(setp), ctypes.sizeof(setp))
if ret != 0:
print("errno %d" % ctypes.get_errno())
raise
def bindx(self, addr, add):
print("bindx(%s, %s)" % (addr, add))
addr = to_sockaddr(addr, 0)
if add:
flag = SCTP_BINDX_ADD_ADDR
else:
flag = SCTP_BINDX_REM_ADDR
ret = self._libc.sctp_bindx(self._fd, ctypes.pointer(addr), 1, flag)
if ret != 0:
print("sctp_bindx() errno %d" % ctypes.get_errno())
raise
def send(self, buf, ppid, ordered=False):
flags = 0
if not ordered:
flags = SCTP_UNORDERED
ppid = socket.htonl(ppid)
ret = self._libc.sctp_sendmsg(self._fd, ctypes.c_char_p(buf), len(buf),
ctypes.c_void_p(0), 0, ppid, flags, 0, 0, 0)
if ret < 0:
raise Exception("Failed to send message")
def close(self):
self._libc.close(self._fd)
self._fd = -1
class TestSCTP(VnetTestTemplate):
REQUIRED_MODULES = ["sctp", "pf"]
TOPOLOGY = {
"vnet1": {"ifaces": ["if1"]},
"vnet2": {"ifaces": ["if1"]},
"if1": {"prefixes4": [("192.0.2.1/24", "192.0.2.2/24")]},
}
def vnet2_handler(self, vnet):
# Give ourself a second IP address, for multihome testing
ifname = vnet.iface_alias_map["if1"].name
ToolsHelper.print_output("/sbin/ifconfig %s inet alias 192.0.2.3/24" % ifname)
# Start an SCTP server process, pipe the ppid + data back to the other vnet?
srv = SCTPServer(socket.AF_INET, port=1234)
while True:
srv.accept(vnet)
@pytest.mark.require_user("root")
def test_multihome(self):
srv_vnet = self.vnet_map["vnet2"]
ToolsHelper.print_output("/sbin/pfctl -e")
ToolsHelper.pf_rules([
"block proto sctp",
"pass inet proto sctp to 192.0.2.0/24",
"pass on lo"])
# Give the server some time to come up
time.sleep(3)
# Sanity check, we can communicate with the primary address.
client = SCTPClient("192.0.2.3", 1234)
client.send(b"hello", 0)
rcvd = self.wait_object(srv_vnet.pipe)
print(rcvd)
assert rcvd['ppid'] == 0
assert rcvd['data'] == "hello"
try:
client.newpeer("192.0.2.2")
client.send(b"world", 0)
rcvd = self.wait_object(srv_vnet.pipe)
print(rcvd)
assert rcvd['ppid'] == 0
assert rcvd['data'] == "world"
finally:
# Debug output
ToolsHelper.print_output("/sbin/pfctl -ss")
ToolsHelper.print_output("/sbin/pfctl -sr -vv")
# Check that we have a state for 192.0.2.3 and 192.0.2.2 to 192.0.2.1
states = ToolsHelper.get_output("/sbin/pfctl -ss")
assert re.search(r"all sctp 192.0.2.1:.*192.0.2.3:1234", states)
assert re.search(r"all sctp 192.0.2.1:.*192.0.2.2:1234", states)
@pytest.mark.require_user("root")
def test_multihome_asconf(self):
srv_vnet = self.vnet_map["vnet2"]
# Assign a second IP to ourselves
ToolsHelper.print_output("/sbin/ifconfig %s inet alias 192.0.2.10/24"
% self.vnet.iface_alias_map["if1"].name)
ToolsHelper.print_output("/sbin/pfctl -e")
ToolsHelper.pf_rules([
"block proto sctp",
"pass on lo",
"pass inet proto sctp from 192.0.2.0/24"])
# Give the server some time to come up
time.sleep(3)
# Sanity check, we can communicate with the primary address.
client = SCTPClient("192.0.2.3", 1234, "192.0.2.1")
client.send(b"hello", 0)
rcvd = self.wait_object(srv_vnet.pipe)
print(rcvd)
assert rcvd['ppid'] == 0
assert rcvd['data'] == "hello"
# Now add our second address to the connection
client.bindx("192.0.2.10", True)
# We can still communicate
client.send(b"world", 0)
rcvd = self.wait_object(srv_vnet.pipe)
print(rcvd)
assert rcvd['ppid'] == 0
assert rcvd['data'] == "world"
# Now change to a different peer address
try:
client.newprimary("192.0.2.10")
client.send(b"!", 0)
rcvd = self.wait_object(srv_vnet.pipe, 5)
print(rcvd)
assert rcvd['ppid'] == 0
assert rcvd['data'] == "!"
finally:
# Debug output
ToolsHelper.print_output("/sbin/pfctl -ss -vv")
# Ensure we have the states we'd expect
states = ToolsHelper.get_output("/sbin/pfctl -ss")
assert re.search(r"all sctp 192.0.2.1:.*192.0.2.3:1234", states)
assert re.search(r"all sctp 192.0.2.10:.*192.0.2.3:1234", states)
# Now remove 192.0.2.1 as an address
client.bindx("192.0.2.1", False)
# We can still communicate
try:
client.send(b"More data", 0)
rcvd = self.wait_object(srv_vnet.pipe, 5)
print(rcvd)
assert rcvd['ppid'] == 0
assert rcvd['data'] =="More data"
finally:
# Debug output
ToolsHelper.print_output("/sbin/pfctl -ss -vv")
# Verify that state is closing
states = ToolsHelper.get_output("/sbin/pfctl -ss")
assert re.search(r"all sctp 192.0.2.1:.*192.0.2.3:1234.*SHUTDOWN", states)
@pytest.mark.require_user("root")
def test_permutation_if_bound(self):
# Test that we generate all permutations of src/dst addresses.
# Assign two addresses to each end, and check for the expected states
srv_vnet = self.vnet_map["vnet2"]
ifname = self.vnet_map["vnet1"].iface_alias_map["if1"].name
ToolsHelper.print_output("/sbin/ifconfig %s inet alias 192.0.2.4/24" % ifname)
ToolsHelper.print_output("/sbin/pfctl -e")
ToolsHelper.pf_rules([
"set state-policy if-bound",
"block proto sctp",
"pass on lo",
"pass inet proto sctp to 192.0.2.0/24"])
# Give the server some time to come up
time.sleep(3)
# Sanity check, we can communicate with the primary address.
client = SCTPClient("192.0.2.3", 1234)
client.send(b"hello", 0)
rcvd = self.wait_object(srv_vnet.pipe)
print(rcvd)
assert rcvd['ppid'] == 0
assert rcvd['data'] == "hello"
# Check that we have a state for 192.0.2.3 and 192.0.2.2 to 192.0.2.1, but also to 192.0.2.4
states = ToolsHelper.get_output("/sbin/pfctl -ss")
print(states)
assert re.search(r"epair.*sctp 192.0.2.1:.*192.0.2.3:1234", states)
assert re.search(r"epair.*sctp 192.0.2.1:.*192.0.2.2:1234", states)
assert re.search(r"epair.*sctp 192.0.2.4:.*192.0.2.3:1234", states)
assert re.search(r"epair.*sctp 192.0.2.4:.*192.0.2.2:1234", states)
@pytest.mark.require_user("root")
def test_permutation_floating(self):
# Test that we generate all permutations of src/dst addresses.
# Assign two addresses to each end, and check for the expected states
srv_vnet = self.vnet_map["vnet2"]
ifname = self.vnet_map["vnet1"].iface_alias_map["if1"].name
ToolsHelper.print_output("/sbin/ifconfig %s inet alias 192.0.2.4/24" % ifname)
ToolsHelper.print_output("/sbin/pfctl -e")
ToolsHelper.pf_rules([
"block proto sctp",
"pass on lo",
"pass inet proto sctp to 192.0.2.0/24"])
# Give the server some time to come up
time.sleep(3)
# Sanity check, we can communicate with the primary address.
client = SCTPClient("192.0.2.3", 1234)
client.send(b"hello", 0)
rcvd = self.wait_object(srv_vnet.pipe)
print(rcvd)
assert rcvd['ppid'] == 0
assert rcvd['data'] == "hello"
# Check that we have a state for 192.0.2.3 and 192.0.2.2 to 192.0.2.1, but also to 192.0.2.4
states = ToolsHelper.get_output("/sbin/pfctl -ss")
print(states)
assert re.search(r"all sctp 192.0.2.1:.*192.0.2.3:1234", states)
assert re.search(r"all sctp 192.0.2.1:.*192.0.2.2:1234", states)
assert re.search(r"all sctp 192.0.2.4:.*192.0.2.3:1234", states)
assert re.search(r"all sctp 192.0.2.4:.*192.0.2.2:1234", states)
@pytest.mark.require_user("root")
def test_limit_addresses(self):
srv_vnet = self.vnet_map["vnet2"]
ifname = self.vnet_map["vnet1"].iface_alias_map["if1"].name
for i in range(0, 16):
ToolsHelper.print_output("/sbin/ifconfig %s inet alias 192.0.2.%d/24" % (ifname, 4 + i))
ToolsHelper.print_output("/sbin/pfctl -e")
ToolsHelper.pf_rules([
"block proto sctp",
"pass on lo",
"pass inet proto sctp to 192.0.2.0/24"])
# Give the server some time to come up
time.sleep(3)
# Set up a connection, which will try to create states for all addresses
# we have assigned
client = SCTPClient("192.0.2.3", 1234)
client.send(b"hello", 0)
rcvd = self.wait_object(srv_vnet.pipe)
print(rcvd)
assert rcvd['ppid'] == 0
assert rcvd['data'] == "hello"
# But the number should be limited to 9 (original + 8 extra)
states = ToolsHelper.get_output("/sbin/pfctl -ss | grep 192.0.2.2")
print(states)
assert(states.count('\n') <= 9)
@pytest.mark.require_user("root")
def test_disallow_related(self):
srv_vnet = self.vnet_map["vnet2"]
ToolsHelper.print_output("/sbin/pfctl -e")
ToolsHelper.pf_rules([
"block proto sctp",
"pass inet proto sctp to 192.0.2.3",
"pass on lo"])
# Give the server some time to come up
time.sleep(3)
# Sanity check, we can communicate with the primary address.
client = SCTPClient("192.0.2.3", 1234)
client.send(b"hello", 0)
rcvd = self.wait_object(srv_vnet.pipe)
print(rcvd)
assert rcvd['ppid'] == 0
assert rcvd['data'] == "hello"
# This shouldn't work
success=False
try:
client.newpeer("192.0.2.2")
client.send(b"world", 0)
rcvd = self.wait_object(srv_vnet.pipe)
print(rcvd)
assert rcvd['ppid'] == 0
assert rcvd['data'] == "world"
success=True
except:
success=False
assert not success
# Check that we have a state for 192.0.2.3, but not 192.0.2.2 to 192.0.2.1
states = ToolsHelper.get_output("/sbin/pfctl -ss")
assert re.search(r"all sctp 192.0.2.1:.*192.0.2.3:1234", states)
assert not re.search(r"all sctp 192.0.2.1:.*192.0.2.2:1234", states)
@pytest.mark.require_user("root")
def test_allow_related(self):
srv_vnet = self.vnet_map["vnet2"]
ToolsHelper.print_output("/sbin/pfctl -e")
ToolsHelper.pf_rules([
"set state-policy if-bound",
"block proto sctp",
"pass inet proto sctp to 192.0.2.3 keep state (allow-related)",
"pass on lo"])
# Give the server some time to come up
time.sleep(3)
# Sanity check, we can communicate with the primary address.
client = SCTPClient("192.0.2.3", 1234)
client.send(b"hello", 0)
rcvd = self.wait_object(srv_vnet.pipe)
print(rcvd)
assert rcvd['ppid'] == 0
assert rcvd['data'] == "hello"
success=False
try:
client.newpeer("192.0.2.2")
client.send(b"world", 0)
rcvd = self.wait_object(srv_vnet.pipe)
print(rcvd)
assert rcvd['ppid'] == 0
assert rcvd['data'] == "world"
success=True
finally:
# Debug output
ToolsHelper.print_output("/sbin/pfctl -ss")
ToolsHelper.print_output("/sbin/pfctl -sr -vv")
assert success
# Check that we have a state for 192.0.2.3 and 192.0.2.2 to 192.0.2.1
states = ToolsHelper.get_output("/sbin/pfctl -ss")
assert re.search(r"epair.*sctp 192.0.2.1:.*192.0.2.3:1234", states)
assert re.search(r"epair.*sctp 192.0.2.1:.*192.0.2.2:1234", states)
class TestSCTPv6(VnetTestTemplate):
REQUIRED_MODULES = ["sctp", "pf"]
TOPOLOGY = {
"vnet1": {"ifaces": ["if1"]},
"vnet2": {"ifaces": ["if1"]},
"if1": {"prefixes6": [("2001:db8::1/64", "2001:db8::2/64")]},
}
def vnet2_handler(self, vnet):
# Give ourself a second IP address, for multihome testing
ifname = vnet.iface_alias_map["if1"].name
ToolsHelper.print_output("/sbin/ifconfig %s inet6 alias 2001:db8::3/64" % ifname)
# Start an SCTP server process, pipe the ppid + data back to the other vnet?
srv = SCTPServer(socket.AF_INET6, port=1234)
while True:
srv.accept(vnet)
@pytest.mark.require_user("root")
def test_multihome(self):
srv_vnet = self.vnet_map["vnet2"]
ToolsHelper.print_output("/sbin/pfctl -e")
ToolsHelper.pf_rules([
"block proto sctp",
"pass on lo",
"pass inet6 proto sctp to 2001:db8::0/64"])
# Give the server some time to come up
time.sleep(3)
# Sanity check, we can communicate with the primary address.
client = SCTPClient("2001:db8::3", 1234)
client.send(b"hello", 0)
rcvd = self.wait_object(srv_vnet.pipe)
print(rcvd)
assert rcvd['ppid'] == 0
assert rcvd['data'] == "hello"
# Now change to a different peer address
try:
client.newpeer("2001:db8::2")
client.send(b"world", 0)
rcvd = self.wait_object(srv_vnet.pipe)
print(rcvd)
assert rcvd['ppid'] == 0
assert rcvd['data'] == "world"
finally:
# Debug output
ToolsHelper.print_output("/sbin/pfctl -ss -vv")
# Check that we have the expected states
states = ToolsHelper.get_output("/sbin/pfctl -ss")
assert re.search(r"all sctp 2001:db8::1\[.*2001:db8::3\[1234\]", states)
assert re.search(r"all sctp 2001:db8::1\[.*2001:db8::2\[1234\]", states)
@pytest.mark.require_user("root")
def test_multihome_asconf(self):
srv_vnet = self.vnet_map["vnet2"]
# Assign a second IP to ourselves
ToolsHelper.print_output("/sbin/ifconfig %s inet6 alias 2001:db8::10/64"
% self.vnet.iface_alias_map["if1"].name)
ToolsHelper.print_output("/sbin/pfctl -e")
ToolsHelper.pf_rules([
"block proto sctp",
"pass on lo",
"pass inet6 proto sctp from 2001:db8::/64"])
# Give the server some time to come up
time.sleep(3)
# Sanity check, we can communicate with the primary address.
client = SCTPClient("2001:db8::3", 1234, "2001:db8::1")
client.send(b"hello", 0)
rcvd = self.wait_object(srv_vnet.pipe)
print(rcvd)
assert rcvd['ppid'] == 0
assert rcvd['data'] == "hello"
# Now add our second address to the connection
client.bindx("2001:db8::10", True)
# We can still communicate
client.send(b"world", 0)
rcvd = self.wait_object(srv_vnet.pipe)
print(rcvd)
assert rcvd['ppid'] == 0
assert rcvd['data'] == "world"
# Now change to a different peer address
try:
client.newprimary("2001:db8::10")
client.send(b"!", 0)
rcvd = self.wait_object(srv_vnet.pipe, 5)
print(rcvd)
assert rcvd['ppid'] == 0
assert rcvd['data'] == "!"
finally:
# Debug output
ToolsHelper.print_output("/sbin/pfctl -ss -vv")
# Check that we have the expected states
states = ToolsHelper.get_output("/sbin/pfctl -ss")
assert re.search(r"all sctp 2001:db8::1\[.*2001:db8::3\[1234\]", states)
assert re.search(r"all sctp 2001:db8::10\[.*2001:db8::3\[1234\]", states)
# Now remove 2001:db8::1 as an address
client.bindx("2001:db8::1", False)
# Wecan still communicate
try:
client.send(b"More data", 0)
rcvd = self.wait_object(srv_vnet.pipe, 5)
print(rcvd)
assert rcvd['ppid'] == 0
assert rcvd['data'] == "More data"
finally:
# Debug output
ToolsHelper.print_output("/sbin/pfctl -ss -vv")
# Verify that the state is closing
states = ToolsHelper.get_output("/sbin/pfctl -ss")
assert re.search(r"all sctp 2001:db8::1\[.*2001:db8::3\[1234\].*SHUTDOWN", states)
@pytest.mark.require_user("root")
def test_permutation(self):
# Test that we generate all permutations of src/dst addresses.
# Assign two addresses to each end, and check for the expected states
srv_vnet = self.vnet_map["vnet2"]
ifname = self.vnet_map["vnet1"].iface_alias_map["if1"].name
ToolsHelper.print_output("/sbin/ifconfig %s inet6 alias 2001:db8::4/64" % ifname)
ToolsHelper.print_output("/sbin/pfctl -e")
ToolsHelper.pf_rules([
"set state-policy if-bound",
"block proto sctp",
"pass on lo",
"pass inet6 proto sctp to 2001:db8::0/64"])
# Give the server some time to come up
time.sleep(3)
# Sanity check, we can communicate with the primary address.
client = SCTPClient("2001:db8::3", 1234)
client.send(b"hello", 0)
rcvd = self.wait_object(srv_vnet.pipe)
print(rcvd)
assert rcvd['ppid'] == 0
assert rcvd['data'] == "hello"
# Check that we have a state for 2001:db8::3 and 2001:db8::2 to 2001:db8::1, but also to 2001:db8::4
states = ToolsHelper.get_output("/sbin/pfctl -ss")
print(states)
assert re.search(r"epair.*sctp 2001:db8::1\[.*2001:db8::2\[1234\]", states)
assert re.search(r"epair.*sctp 2001:db8::1\[.*2001:db8::3\[1234\]", states)
assert re.search(r"epair.*sctp 2001:db8::4\[.*2001:db8::2\[1234\]", states)
assert re.search(r"epair.*sctp 2001:db8::4\[.*2001:db8::3\[1234\]", states)
@pytest.mark.require_user("root")
def test_permutation_floating(self):
# Test that we generate all permutations of src/dst addresses.
# Assign two addresses to each end, and check for the expected states
srv_vnet = self.vnet_map["vnet2"]
ifname = self.vnet_map["vnet1"].iface_alias_map["if1"].name
ToolsHelper.print_output("/sbin/ifconfig %s inet6 alias 2001:db8::4/64" % ifname)
ToolsHelper.print_output("/sbin/pfctl -e")
ToolsHelper.pf_rules([
"block proto sctp",
"pass on lo",
"pass inet6 proto sctp to 2001:db8::0/64"])
# Give the server some time to come up
time.sleep(3)
# Sanity check, we can communicate with the primary address.
client = SCTPClient("2001:db8::3", 1234)
client.send(b"hello", 0)
rcvd = self.wait_object(srv_vnet.pipe)
print(rcvd)
assert rcvd['ppid'] == 0
assert rcvd['data'] == "hello"
# Check that we have a state for 2001:db8::3 and 2001:db8::2 to 2001:db8::1, but also to 2001:db8::4
states = ToolsHelper.get_output("/sbin/pfctl -ss")
print(states)
assert re.search(r"all sctp 2001:db8::1\[.*2001:db8::2\[1234\]", states)
assert re.search(r"all sctp 2001:db8::1\[.*2001:db8::3\[1234\]", states)
assert re.search(r"all sctp 2001:db8::4\[.*2001:db8::2\[1234\]", states)
assert re.search(r"all sctp 2001:db8::4\[.*2001:db8::3\[1234\]", states)
|