7 from framework import VppTestCase, VppTestRunner
8 from vpp_neighbor import VppNeighbor
9 from vpp_ip_route import find_route
10 from util import mk_ll_addr
12 from scapy.layers.l2 import Ether, getmacbyip
13 from scapy.layers.inet import IP, UDP, ICMP
14 from scapy.layers.inet6 import IPv6, in6_getnsmac, in6_mactoifaceid
15 from scapy.layers.dhcp import DHCP, BOOTP, DHCPTypes
16 from scapy.layers.dhcp6 import DHCP6, DHCP6_Solicit, DHCP6_RelayForward, \
17 DHCP6_RelayReply, DHCP6_Advertise, DHCP6OptRelayMsg, DHCP6OptIfaceId, \
18 DHCP6OptStatusCode, DHCP6OptVSS, DHCP6OptClientLinkLayerAddr, DHCP6_Request
19 from socket import AF_INET, AF_INET6
20 from scapy.utils import inet_pton, inet_ntop
21 from scapy.utils6 import in6_ptop
23 DHCP4_CLIENT_PORT = 68
24 DHCP4_SERVER_PORT = 67
25 DHCP6_CLIENT_PORT = 547
26 DHCP6_SERVER_PORT = 546
29 class TestDHCP(VppTestCase):
30 """ DHCP Test Case """
33 super(TestDHCP, self).setUp()
35 # create 3 pg interfaces
36 self.create_pg_interfaces(range(4))
38 # pg0 and 1 are IP configured in VRF 0 and 1.
39 # pg2 and 3 are non IP-configured in VRF 0 and 1
41 for i in self.pg_interfaces[:2]:
43 i.set_table_ip4(table_id)
44 i.set_table_ip6(table_id)
52 for i in self.pg_interfaces[2:]:
54 i.set_table_ip4(table_id)
55 i.set_table_ip6(table_id)
59 super(TestDHCP, self).tearDown()
60 for i in self.pg_interfaces:
65 def send_and_assert_no_replies(self, intf, pkts, remark):
67 self.pg_enable_capture(self.pg_interfaces)
69 for i in self.pg_interfaces:
70 i.assert_nothing_captured(remark=remark)
72 def verify_dhcp_has_option(self, pkt, option, value):
76 for i in dhcp.options:
79 self.assertEqual(i[1], value)
82 self.assertTrue(found)
84 def validate_relay_options(self, pkt, intf, ip_addr, fib_id, oui):
89 for i in dhcp.options:
91 if i[0] == "relay_agent_Information":
93 # There are two sb-options present - each of length 6.
97 self.assertEqual(len(data), 24)
99 self.assertEqual(len(data), 12)
102 # First sub-option is ID 1, len 4, then encoded
103 # sw_if_index. This test uses low valued indicies
105 # The ID space is VPP internal - so no matching value
108 self.assertEqual(ord(data[0]), 1)
109 self.assertEqual(ord(data[1]), 4)
110 self.assertEqual(ord(data[2]), 0)
111 self.assertEqual(ord(data[3]), 0)
112 self.assertEqual(ord(data[4]), 0)
113 self.assertEqual(ord(data[5]), intf._sw_if_index)
116 # next sub-option is the IP address of the client side
118 # sub-option ID=5, length (of a v4 address)=4
120 claddr = socket.inet_pton(AF_INET, ip_addr)
122 self.assertEqual(ord(data[6]), 5)
123 self.assertEqual(ord(data[7]), 4)
124 self.assertEqual(data[8], claddr[0])
125 self.assertEqual(data[9], claddr[1])
126 self.assertEqual(data[10], claddr[2])
127 self.assertEqual(data[11], claddr[3])
130 # sub-option 151 encodes the 3 byte oui
131 # and the 4 byte fib_id
132 self.assertEqual(ord(data[12]), 151)
133 self.assertEqual(ord(data[13]), 8)
134 self.assertEqual(ord(data[14]), 1)
135 self.assertEqual(ord(data[15]), 0)
136 self.assertEqual(ord(data[16]), 0)
137 self.assertEqual(ord(data[17]), oui)
138 self.assertEqual(ord(data[18]), 0)
139 self.assertEqual(ord(data[19]), 0)
140 self.assertEqual(ord(data[20]), 0)
141 self.assertEqual(ord(data[21]), fib_id)
143 # VSS control sub-option
144 self.assertEqual(ord(data[22]), 152)
145 self.assertEqual(ord(data[23]), 0)
148 self.assertTrue(found)
152 def verify_dhcp_msg_type(self, pkt, name):
155 for o in dhcp.options:
157 if o[0] == "message-type" \
158 and DHCPTypes[o[1]] == name:
160 self.assertTrue(found)
162 def verify_dhcp_offer(self, pkt, intf, fib_id=0, oui=0):
164 self.assertEqual(ether.dst, "ff:ff:ff:ff:ff:ff")
165 self.assertEqual(ether.src, intf.local_mac)
168 self.assertEqual(ip.dst, "255.255.255.255")
169 self.assertEqual(ip.src, intf.local_ip4)
172 self.assertEqual(udp.dport, DHCP4_CLIENT_PORT)
173 self.assertEqual(udp.sport, DHCP4_SERVER_PORT)
175 self.verify_dhcp_msg_type(pkt, "offer")
176 data = self.validate_relay_options(pkt, intf, intf.local_ip4,
179 def verify_orig_dhcp_pkt(self, pkt, intf):
181 self.assertEqual(ether.dst, "ff:ff:ff:ff:ff:ff")
182 self.assertEqual(ether.src, intf.local_mac)
185 self.assertEqual(ip.dst, "255.255.255.255")
186 self.assertEqual(ip.src, "0.0.0.0")
189 self.assertEqual(udp.dport, DHCP4_SERVER_PORT)
190 self.assertEqual(udp.sport, DHCP4_CLIENT_PORT)
192 def verify_orig_dhcp_discover(self, pkt, intf, hostname):
193 self.verify_orig_dhcp_pkt(pkt, intf)
195 self.verify_dhcp_msg_type(pkt, "discover")
196 self.verify_dhcp_has_option(pkt, "hostname", hostname)
198 def verify_orig_dhcp_request(self, pkt, intf, hostname, ip):
199 self.verify_orig_dhcp_pkt(pkt, intf)
201 self.verify_dhcp_msg_type(pkt, "request")
202 self.verify_dhcp_has_option(pkt, "hostname", hostname)
203 self.verify_dhcp_has_option(pkt, "requested_addr", ip)
205 def verify_relayed_dhcp_discover(self, pkt, intf, src_intf=None,
207 dst_mac=None, dst_ip=None):
209 dst_mac = intf.remote_mac
211 dst_ip = intf.remote_ip4
214 self.assertEqual(ether.dst, dst_mac)
215 self.assertEqual(ether.src, intf.local_mac)
218 self.assertEqual(ip.dst, dst_ip)
219 self.assertEqual(ip.src, intf.local_ip4)
222 self.assertEqual(udp.dport, DHCP4_SERVER_PORT)
223 self.assertEqual(udp.sport, DHCP4_CLIENT_PORT)
228 for o in dhcp.options:
230 if o[0] == "message-type" \
231 and DHCPTypes[o[1]] == "discover":
233 self.assertTrue(is_discover)
235 data = self.validate_relay_options(pkt, src_intf,
240 def verify_dhcp6_solicit(self, pkt, intf,
247 dst_mac = intf.remote_mac
249 dst_ip = in6_ptop(intf.remote_ip6)
252 self.assertEqual(ether.dst, dst_mac)
253 self.assertEqual(ether.src, intf.local_mac)
256 self.assertEqual(in6_ptop(ip.dst), dst_ip)
257 self.assertEqual(in6_ptop(ip.src), in6_ptop(intf.local_ip6))
260 self.assertEqual(udp.dport, DHCP6_CLIENT_PORT)
261 self.assertEqual(udp.sport, DHCP6_SERVER_PORT)
263 relay = pkt[DHCP6_RelayForward]
264 self.assertEqual(in6_ptop(relay.peeraddr), in6_ptop(peer_ip))
265 oid = pkt[DHCP6OptIfaceId]
266 cll = pkt[DHCP6OptClientLinkLayerAddr]
267 self.assertEqual(cll.optlen, 8)
268 self.assertEqual(cll.lltype, 1)
269 self.assertEqual(cll.clladdr, peer_mac)
272 vss = pkt[DHCP6OptVSS]
273 self.assertEqual(vss.optlen, 8)
274 self.assertEqual(vss.type, 1)
275 # the OUI and FIB-id are really 3 and 4 bytes resp.
276 # but the tested range is small
277 self.assertEqual(ord(vss.data[0]), 0)
278 self.assertEqual(ord(vss.data[1]), 0)
279 self.assertEqual(ord(vss.data[2]), oui)
280 self.assertEqual(ord(vss.data[3]), 0)
281 self.assertEqual(ord(vss.data[4]), 0)
282 self.assertEqual(ord(vss.data[5]), 0)
283 self.assertEqual(ord(vss.data[6]), fib_id)
285 # the relay message should be an encoded Solicit
286 msg = pkt[DHCP6OptRelayMsg]
287 sol = DHCP6_Solicit()
288 self.assertEqual(msg.optlen, len(str(sol)))
289 self.assertEqual(str(sol), (str(msg[1]))[:msg.optlen])
291 def verify_dhcp6_advert(self, pkt, intf, peer):
293 self.assertEqual(ether.dst, "ff:ff:ff:ff:ff:ff")
294 self.assertEqual(ether.src, intf.local_mac)
297 self.assertEqual(in6_ptop(ip.dst), in6_ptop(peer))
298 self.assertEqual(in6_ptop(ip.src), in6_ptop(intf.local_ip6))
301 self.assertEqual(udp.dport, DHCP6_SERVER_PORT)
302 self.assertEqual(udp.sport, DHCP6_CLIENT_PORT)
304 # not sure why this is not decoding
305 # adv = pkt[DHCP6_Advertise]
307 def test_dhcp_proxy(self):
311 # Verify no response to DHCP request without DHCP config
313 p_disc_vrf0 = (Ether(dst="ff:ff:ff:ff:ff:ff",
314 src=self.pg2.remote_mac) /
315 IP(src="0.0.0.0", dst="255.255.255.255") /
316 UDP(sport=DHCP4_CLIENT_PORT,
317 dport=DHCP4_SERVER_PORT) /
319 DHCP(options=[('message-type', 'discover'), ('end')]))
320 pkts_disc_vrf0 = [p_disc_vrf0]
321 p_disc_vrf1 = (Ether(dst="ff:ff:ff:ff:ff:ff",
322 src=self.pg3.remote_mac) /
323 IP(src="0.0.0.0", dst="255.255.255.255") /
324 UDP(sport=DHCP4_CLIENT_PORT,
325 dport=DHCP4_SERVER_PORT) /
327 DHCP(options=[('message-type', 'discover'), ('end')]))
328 pkts_disc_vrf1 = [p_disc_vrf0]
330 self.send_and_assert_no_replies(self.pg2, pkts_disc_vrf0,
331 "DHCP with no configuration")
332 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf1,
333 "DHCP with no configuration")
336 # Enable DHCP proxy in VRF 0
338 server_addr = self.pg0.remote_ip4n
339 src_addr = self.pg0.local_ip4n
341 self.vapi.dhcp_proxy_config(server_addr,
346 # Discover packets from the client are dropped because there is no
347 # IP address configured on the client facing interface
349 self.send_and_assert_no_replies(self.pg2, pkts_disc_vrf0,
350 "Discover DHCP no relay address")
353 # Inject a response from the server
354 # dropped, because there is no IP addrees on the
355 # client interfce to fill in the option.
357 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
358 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
359 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
361 DHCP(options=[('message-type', 'offer'), ('end')]))
364 self.send_and_assert_no_replies(self.pg2, pkts,
365 "Offer DHCP no relay address")
368 # configure an IP address on the client facing interface
370 self.pg2.config_ip4()
373 # Try again with a discover packet
374 # Rx'd packet should be to the server address and from the configured
376 # UDP source ports are unchanged
377 # we've no option 82 config so that should be absent
379 self.pg2.add_stream(pkts_disc_vrf0)
380 self.pg_enable_capture(self.pg_interfaces)
383 rx = self.pg0.get_capture(1)
386 option_82 = self.verify_relayed_dhcp_discover(rx, self.pg0,
390 # Create an DHCP offer reply from the server with a correctly formatted
391 # option 82. i.e. send back what we just captured
392 # The offer, sent mcast to the client, still has option 82.
394 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
395 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
396 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
398 DHCP(options=[('message-type', 'offer'),
399 ('relay_agent_Information', option_82),
403 self.pg0.add_stream(pkts)
404 self.pg_enable_capture(self.pg_interfaces)
407 rx = self.pg2.get_capture(1)
410 self.verify_dhcp_offer(rx, self.pg2)
415 # 1. not our IP address = not checked by VPP? so offer is replayed
417 bad_ip = option_82[0:8] + chr(33) + option_82[9:]
419 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
420 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
421 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
423 DHCP(options=[('message-type', 'offer'),
424 ('relay_agent_Information', bad_ip),
427 self.send_and_assert_no_replies(self.pg0, pkts,
428 "DHCP offer option 82 bad address")
430 # 2. Not a sw_if_index VPP knows
431 bad_if_index = option_82[0:2] + chr(33) + option_82[3:]
433 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
434 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
435 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
437 DHCP(options=[('message-type', 'offer'),
438 ('relay_agent_Information', bad_if_index),
441 self.send_and_assert_no_replies(self.pg0, pkts,
442 "DHCP offer option 82 bad if index")
445 # Send a DHCP request in VRF 1. should be dropped.
447 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf1,
448 "DHCP with no configuration VRF 1")
451 # Delete the DHCP config in VRF 0
452 # Should now drop requests.
454 self.vapi.dhcp_proxy_config(server_addr,
459 self.send_and_assert_no_replies(self.pg2, pkts_disc_vrf0,
460 "DHCP config removed VRF 0")
461 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf1,
462 "DHCP config removed VRF 1")
465 # Add DHCP config for VRF 1
467 server_addr = self.pg1.remote_ip4n
468 src_addr = self.pg1.local_ip4n
469 self.vapi.dhcp_proxy_config(server_addr,
475 # Confim DHCP requests ok in VRF 1.
476 # - dropped on IP config on client interface
478 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf1,
479 "DHCP config removed VRF 1")
482 # configure an IP address on the client facing interface
484 self.pg3.config_ip4()
486 self.pg3.add_stream(pkts_disc_vrf1)
487 self.pg_enable_capture(self.pg_interfaces)
490 rx = self.pg1.get_capture(1)
492 self.verify_relayed_dhcp_discover(rx, self.pg1, src_intf=self.pg3)
496 # table=1, fib=id=1, oui=4
497 self.vapi.dhcp_proxy_set_vss(1, 1, 4)
499 self.pg3.add_stream(pkts_disc_vrf1)
500 self.pg_enable_capture(self.pg_interfaces)
503 rx = self.pg1.get_capture(1)
505 self.verify_relayed_dhcp_discover(rx, self.pg1,
510 # Add a second DHCP server in VRF 1
511 # expect clients messages to be relay to both configured servers
513 self.pg1.generate_remote_hosts(2)
514 server_addr2 = socket.inet_pton(AF_INET, self.pg1.remote_hosts[1].ip4)
516 self.vapi.dhcp_proxy_config(server_addr2,
523 # We'll need an ARP entry for the server to send it packets
525 arp_entry = VppNeighbor(self,
526 self.pg1.sw_if_index,
527 self.pg1.remote_hosts[1].mac,
528 self.pg1.remote_hosts[1].ip4)
529 arp_entry.add_vpp_config()
532 # Send a discover from the client. expect two relayed messages
533 # The frist packet is sent to the second server
534 # We're not enforcing that here, it's just the way it is.
536 self.pg3.add_stream(pkts_disc_vrf1)
537 self.pg_enable_capture(self.pg_interfaces)
540 rx = self.pg1.get_capture(2)
542 option_82 = self.verify_relayed_dhcp_discover(
545 dst_mac=self.pg1.remote_hosts[1].mac,
546 dst_ip=self.pg1.remote_hosts[1].ip4,
548 self.verify_relayed_dhcp_discover(rx[1], self.pg1,
553 # Send both packets back. Client gets both.
555 p1 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
556 IP(src=self.pg1.remote_ip4, dst=self.pg1.local_ip4) /
557 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
559 DHCP(options=[('message-type', 'offer'),
560 ('relay_agent_Information', option_82),
562 p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
563 IP(src=self.pg1.remote_hosts[1].ip4, dst=self.pg1.local_ip4) /
564 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
566 DHCP(options=[('message-type', 'offer'),
567 ('relay_agent_Information', option_82),
571 self.pg1.add_stream(pkts)
572 self.pg_enable_capture(self.pg_interfaces)
575 rx = self.pg3.get_capture(2)
577 self.verify_dhcp_offer(rx[0], self.pg3, fib_id=1, oui=4)
578 self.verify_dhcp_offer(rx[1], self.pg3, fib_id=1, oui=4)
581 # Ensure offers from non-servers are dropeed
583 p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
584 IP(src="8.8.8.8", dst=self.pg1.local_ip4) /
585 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
587 DHCP(options=[('message-type', 'offer'),
588 ('relay_agent_Information', option_82),
590 self.send_and_assert_no_replies(self.pg1, p2,
591 "DHCP offer from non-server")
594 # Ensure only the discover is sent to multiple servers
596 p_req_vrf1 = (Ether(dst="ff:ff:ff:ff:ff:ff",
597 src=self.pg3.remote_mac) /
598 IP(src="0.0.0.0", dst="255.255.255.255") /
599 UDP(sport=DHCP4_CLIENT_PORT,
600 dport=DHCP4_SERVER_PORT) /
602 DHCP(options=[('message-type', 'request'),
605 self.pg3.add_stream(p_req_vrf1)
606 self.pg_enable_capture(self.pg_interfaces)
609 rx = self.pg1.get_capture(1)
612 # Remove the second DHCP server
614 self.vapi.dhcp_proxy_config(server_addr2,
621 # Test we can still relay with the first
623 self.pg3.add_stream(pkts_disc_vrf1)
624 self.pg_enable_capture(self.pg_interfaces)
627 rx = self.pg1.get_capture(1)
629 self.verify_relayed_dhcp_discover(rx, self.pg1,
634 # Remove the VSS config
635 # relayed DHCP has default vlaues in the option.
637 self.vapi.dhcp_proxy_set_vss(1, 1, 4, is_add=0)
639 self.pg3.add_stream(pkts_disc_vrf1)
640 self.pg_enable_capture(self.pg_interfaces)
643 rx = self.pg1.get_capture(1)
645 self.verify_relayed_dhcp_discover(rx, self.pg1, src_intf=self.pg3)
648 # remove DHCP config to cleanup
650 self.vapi.dhcp_proxy_config(server_addr,
656 self.send_and_assert_no_replies(self.pg2, pkts_disc_vrf0,
657 "DHCP cleanup VRF 0")
658 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf1,
659 "DHCP cleanup VRF 1")
661 def test_dhcp6_proxy(self):
664 # Verify no response to DHCP request without DHCP config
666 dhcp_solicit_dst = "ff02::1:2"
667 dhcp_solicit_src_vrf0 = mk_ll_addr(self.pg2.remote_mac)
668 dhcp_solicit_src_vrf1 = mk_ll_addr(self.pg3.remote_mac)
669 server_addr_vrf0 = self.pg0.remote_ip6n
670 src_addr_vrf0 = self.pg0.local_ip6n
671 server_addr_vrf1 = self.pg1.remote_ip6n
672 src_addr_vrf1 = self.pg1.local_ip6n
674 dmac = in6_getnsmac(inet_pton(socket.AF_INET6, dhcp_solicit_dst))
675 p_solicit_vrf0 = (Ether(dst=dmac, src=self.pg2.remote_mac) /
676 IPv6(src=dhcp_solicit_src_vrf0,
677 dst=dhcp_solicit_dst) /
678 UDP(sport=DHCP6_SERVER_PORT,
679 dport=DHCP6_CLIENT_PORT) /
681 p_solicit_vrf1 = (Ether(dst=dmac, src=self.pg3.remote_mac) /
682 IPv6(src=dhcp_solicit_src_vrf1,
683 dst=dhcp_solicit_dst) /
684 UDP(sport=DHCP6_SERVER_PORT,
685 dport=DHCP6_CLIENT_PORT) /
688 self.send_and_assert_no_replies(self.pg2, p_solicit_vrf0,
689 "DHCP with no configuration")
690 self.send_and_assert_no_replies(self.pg3, p_solicit_vrf1,
691 "DHCP with no configuration")
694 # DHCPv6 config in VRF 0.
695 # Packets still dropped because the client facing interface has no
698 self.vapi.dhcp_proxy_config(server_addr_vrf0,
704 self.send_and_assert_no_replies(self.pg2, p_solicit_vrf0,
705 "DHCP with no configuration")
706 self.send_and_assert_no_replies(self.pg3, p_solicit_vrf1,
707 "DHCP with no configuration")
710 # configure an IP address on the client facing interface
712 self.pg2.config_ip6()
715 # Now the DHCP requests are relayed to the server
717 self.pg2.add_stream(p_solicit_vrf0)
718 self.pg_enable_capture(self.pg_interfaces)
721 rx = self.pg0.get_capture(1)
723 self.verify_dhcp6_solicit(rx[0], self.pg0,
724 dhcp_solicit_src_vrf0,
728 # Exception cases for rejected relay responses
731 # 1 - not a relay reply
732 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
733 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
734 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
736 self.send_and_assert_no_replies(self.pg2, p_adv_vrf0,
737 "DHCP6 not a relay reply")
739 # 2 - no relay message option
740 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
741 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
742 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
745 self.send_and_assert_no_replies(self.pg2, p_adv_vrf0,
746 "DHCP not a relay message")
749 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
750 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
751 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
753 DHCP6OptRelayMsg(optlen=0) /
755 self.send_and_assert_no_replies(self.pg2, p_adv_vrf0,
756 "DHCP6 no circuit ID")
757 # 4 - wrong circuit ID
758 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
759 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
760 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
762 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x05') /
763 DHCP6OptRelayMsg(optlen=0) /
765 self.send_and_assert_no_replies(self.pg2, p_adv_vrf0,
766 "DHCP6 wrong circuit ID")
769 # Send the relay response (the advertisement)
771 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
772 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
773 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
775 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x03') /
776 DHCP6OptRelayMsg(optlen=0) /
777 DHCP6_Advertise(trid=1) /
778 DHCP6OptStatusCode(statuscode=0))
779 pkts_adv_vrf0 = [p_adv_vrf0]
781 self.pg0.add_stream(pkts_adv_vrf0)
782 self.pg_enable_capture(self.pg_interfaces)
785 rx = self.pg2.get_capture(1)
787 self.verify_dhcp6_advert(rx[0], self.pg2, "::")
790 # Send the relay response (the advertisement)
791 # - with peer address
792 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
793 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
794 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
795 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf0) /
796 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x03') /
797 DHCP6OptRelayMsg(optlen=0) /
798 DHCP6_Advertise(trid=1) /
799 DHCP6OptStatusCode(statuscode=0))
800 pkts_adv_vrf0 = [p_adv_vrf0]
802 self.pg0.add_stream(pkts_adv_vrf0)
803 self.pg_enable_capture(self.pg_interfaces)
806 rx = self.pg2.get_capture(1)
808 self.verify_dhcp6_advert(rx[0], self.pg2, dhcp_solicit_src_vrf0)
811 # Add all the config for VRF 1
813 self.vapi.dhcp_proxy_config(server_addr_vrf1,
818 self.pg3.config_ip6()
823 self.pg3.add_stream(p_solicit_vrf1)
824 self.pg_enable_capture(self.pg_interfaces)
827 rx = self.pg1.get_capture(1)
829 self.verify_dhcp6_solicit(rx[0], self.pg1,
830 dhcp_solicit_src_vrf1,
836 p_adv_vrf1 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
837 IPv6(dst=self.pg1.local_ip6, src=self.pg1.remote_ip6) /
838 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
839 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) /
840 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x04') /
841 DHCP6OptRelayMsg(optlen=0) /
842 DHCP6_Advertise(trid=1) /
843 DHCP6OptStatusCode(statuscode=0))
844 pkts_adv_vrf1 = [p_adv_vrf1]
846 self.pg1.add_stream(pkts_adv_vrf1)
847 self.pg_enable_capture(self.pg_interfaces)
850 rx = self.pg3.get_capture(1)
852 self.verify_dhcp6_advert(rx[0], self.pg3, dhcp_solicit_src_vrf1)
856 # table=1, fib=id=1, oui=4
857 self.vapi.dhcp_proxy_set_vss(1, 1, 4, is_ip6=1)
859 self.pg3.add_stream(p_solicit_vrf1)
860 self.pg_enable_capture(self.pg_interfaces)
863 rx = self.pg1.get_capture(1)
865 self.verify_dhcp6_solicit(rx[0], self.pg1,
866 dhcp_solicit_src_vrf1,
872 # Remove the VSS config
873 # relayed DHCP has default vlaues in the option.
875 self.vapi.dhcp_proxy_set_vss(1, 1, 4, is_ip6=1, is_add=0)
877 self.pg3.add_stream(p_solicit_vrf1)
878 self.pg_enable_capture(self.pg_interfaces)
881 rx = self.pg1.get_capture(1)
883 self.verify_dhcp6_solicit(rx[0], self.pg1,
884 dhcp_solicit_src_vrf1,
888 # Add a second DHCP server in VRF 1
889 # expect clients messages to be relay to both configured servers
891 self.pg1.generate_remote_hosts(2)
892 server_addr2 = socket.inet_pton(AF_INET6, self.pg1.remote_hosts[1].ip6)
894 self.vapi.dhcp_proxy_config(server_addr2,
901 # We'll need an ND entry for the server to send it packets
903 nd_entry = VppNeighbor(self,
904 self.pg1.sw_if_index,
905 self.pg1.remote_hosts[1].mac,
906 self.pg1.remote_hosts[1].ip6,
908 nd_entry.add_vpp_config()
911 # Send a discover from the client. expect two relayed messages
912 # The frist packet is sent to the second server
913 # We're not enforcing that here, it's just the way it is.
915 self.pg3.add_stream(p_solicit_vrf1)
916 self.pg_enable_capture(self.pg_interfaces)
919 rx = self.pg1.get_capture(2)
921 self.verify_dhcp6_solicit(rx[0], self.pg1,
922 dhcp_solicit_src_vrf1,
924 self.verify_dhcp6_solicit(rx[1], self.pg1,
925 dhcp_solicit_src_vrf1,
927 dst_mac=self.pg1.remote_hosts[1].mac,
928 dst_ip=self.pg1.remote_hosts[1].ip6)
931 # Send both packets back. Client gets both.
933 p1 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
934 IPv6(dst=self.pg1.local_ip6, src=self.pg1.remote_ip6) /
935 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
936 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) /
937 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x04') /
938 DHCP6OptRelayMsg(optlen=0) /
939 DHCP6_Advertise(trid=1) /
940 DHCP6OptStatusCode(statuscode=0))
941 p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_hosts[1].mac) /
942 IPv6(dst=self.pg1.local_ip6, src=self.pg1._remote_hosts[1].ip6) /
943 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
944 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) /
945 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x04') /
946 DHCP6OptRelayMsg(optlen=0) /
947 DHCP6_Advertise(trid=1) /
948 DHCP6OptStatusCode(statuscode=0))
952 self.pg1.add_stream(pkts)
953 self.pg_enable_capture(self.pg_interfaces)
956 rx = self.pg3.get_capture(2)
958 self.verify_dhcp6_advert(rx[0], self.pg3, dhcp_solicit_src_vrf1)
959 self.verify_dhcp6_advert(rx[1], self.pg3, dhcp_solicit_src_vrf1)
962 # Ensure only solicit messages are duplicated
964 p_request_vrf1 = (Ether(dst=dmac, src=self.pg3.remote_mac) /
965 IPv6(src=dhcp_solicit_src_vrf1,
966 dst=dhcp_solicit_dst) /
967 UDP(sport=DHCP6_SERVER_PORT,
968 dport=DHCP6_CLIENT_PORT) /
971 self.pg3.add_stream(p_request_vrf1)
972 self.pg_enable_capture(self.pg_interfaces)
975 rx = self.pg1.get_capture(1)
978 # Test we drop DHCP packets from addresses that are not configured as
981 p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_hosts[1].mac) /
982 IPv6(dst=self.pg1.local_ip6, src="3001::1") /
983 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
984 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) /
985 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x04') /
986 DHCP6OptRelayMsg(optlen=0) /
987 DHCP6_Advertise(trid=1) /
988 DHCP6OptStatusCode(statuscode=0))
989 self.send_and_assert_no_replies(self.pg1, p2,
990 "DHCP6 not from server")
993 # Remove the second DHCP server
995 self.vapi.dhcp_proxy_config(server_addr2,
1003 # Test we can still relay with the first
1005 self.pg3.add_stream(p_solicit_vrf1)
1006 self.pg_enable_capture(self.pg_interfaces)
1009 rx = self.pg1.get_capture(1)
1011 self.verify_dhcp6_solicit(rx[0], self.pg1,
1012 dhcp_solicit_src_vrf1,
1013 self.pg3.remote_mac)
1018 self.vapi.dhcp_proxy_config(server_addr_vrf1,
1024 self.vapi.dhcp_proxy_config(server_addr_vrf0,
1032 self.vapi.dhcp_proxy_config(server_addr_vrf0,
1039 def test_dhcp_client(self):
1042 hostname = 'universal-dp'
1044 self.pg_enable_capture(self.pg_interfaces)
1047 # Configure DHCP client on PG2 and capture the discover sent
1049 self.vapi.dhcp_client(self.pg2.sw_if_index, hostname)
1051 rx = self.pg2.get_capture(1)
1053 self.verify_orig_dhcp_discover(rx[0], self.pg2, hostname)
1056 # Sned back on offer, expect the request
1058 p = (Ether(dst=self.pg2.local_mac, src=self.pg2.remote_mac) /
1059 IP(src=self.pg2.remote_ip4, dst="255.255.255.255") /
1060 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1062 yiaddr=self.pg2.local_ip4) /
1063 DHCP(options=[('message-type', 'offer'), ('end')]))
1065 self.pg2.add_stream(p)
1066 self.pg_enable_capture(self.pg_interfaces)
1069 rx = self.pg2.get_capture(1)
1070 self.verify_orig_dhcp_request(rx[0], self.pg2, hostname,
1074 # Send an acknowloedgement
1076 p = (Ether(dst=self.pg2.local_mac, src=self.pg2.remote_mac) /
1077 IP(src=self.pg2.remote_ip4, dst="255.255.255.255") /
1078 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1080 yiaddr=self.pg2.local_ip4) /
1081 DHCP(options=[('message-type', 'ack'),
1082 ('subnet_mask', "255.255.255.0"),
1083 ('router', self.pg2.remote_ip4),
1084 ('server_id', self.pg2.remote_ip4),
1085 ('lease_time', 43200),
1088 self.pg2.add_stream(p)
1089 self.pg_enable_capture(self.pg_interfaces)
1093 # At the end of this procedure there should be a connected route
1096 self.assertTrue(find_route(self, self.pg2.local_ip4, 32))
1099 # remove the DHCP config
1101 self.vapi.dhcp_client(self.pg2.sw_if_index, hostname, is_add=0)
1104 # and now the route should be gone
1106 self.assertFalse(find_route(self, self.pg2.local_ip4, 32))
1109 if __name__ == '__main__':
1110 unittest.main(testRunner=VppTestRunner)