7 from framework import VppTestCase, VppTestRunner
8 from vpp_neighbor import VppNeighbor
9 from vpp_ip_route import find_route, VppIpTable
10 from util import mk_ll_addr
12 from scapy.layers.l2 import Ether, getmacbyip, ARP
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))
39 # pg0 and 1 are IP configured in VRF 0 and 1.
40 # pg2 and 3 are non IP-configured in VRF 0 and 1
42 for table_id in range(1, 4):
43 tbl4 = VppIpTable(self, table_id)
45 self.tables.append(tbl4)
46 tbl6 = VppIpTable(self, table_id, is_ip6=1)
48 self.tables.append(tbl6)
51 for i in self.pg_interfaces[:2]:
53 i.set_table_ip4(table_id)
54 i.set_table_ip6(table_id)
62 for i in self.pg_interfaces[2:]:
64 i.set_table_ip4(table_id)
65 i.set_table_ip6(table_id)
69 for i in self.pg_interfaces[:2]:
73 for i in self.pg_interfaces:
77 super(TestDHCP, self).tearDown()
79 def send_and_assert_no_replies(self, intf, pkts, remark):
81 self.pg_enable_capture(self.pg_interfaces)
83 for i in self.pg_interfaces:
84 i.assert_nothing_captured(remark=remark)
86 def verify_dhcp_has_option(self, pkt, option, value):
90 for i in dhcp.options:
93 self.assertEqual(i[1], value)
96 self.assertTrue(found)
98 def validate_relay_options(self, pkt, intf, ip_addr, fib_id, oui):
103 for i in dhcp.options:
105 if i[0] == "relay_agent_Information":
107 # There are two sb-options present - each of length 6.
111 self.assertEqual(len(data), 24)
113 self.assertEqual(len(data), 12)
116 # First sub-option is ID 1, len 4, then encoded
117 # sw_if_index. This test uses low valued indicies
119 # The ID space is VPP internal - so no matching value
122 self.assertEqual(ord(data[0]), 1)
123 self.assertEqual(ord(data[1]), 4)
124 self.assertEqual(ord(data[2]), 0)
125 self.assertEqual(ord(data[3]), 0)
126 self.assertEqual(ord(data[4]), 0)
127 self.assertEqual(ord(data[5]), intf._sw_if_index)
130 # next sub-option is the IP address of the client side
132 # sub-option ID=5, length (of a v4 address)=4
134 claddr = socket.inet_pton(AF_INET, ip_addr)
136 self.assertEqual(ord(data[6]), 5)
137 self.assertEqual(ord(data[7]), 4)
138 self.assertEqual(data[8], claddr[0])
139 self.assertEqual(data[9], claddr[1])
140 self.assertEqual(data[10], claddr[2])
141 self.assertEqual(data[11], claddr[3])
144 # sub-option 151 encodes the 3 byte oui
145 # and the 4 byte fib_id
146 self.assertEqual(ord(data[12]), 151)
147 self.assertEqual(ord(data[13]), 8)
148 self.assertEqual(ord(data[14]), 1)
149 self.assertEqual(ord(data[15]), 0)
150 self.assertEqual(ord(data[16]), 0)
151 self.assertEqual(ord(data[17]), oui)
152 self.assertEqual(ord(data[18]), 0)
153 self.assertEqual(ord(data[19]), 0)
154 self.assertEqual(ord(data[20]), 0)
155 self.assertEqual(ord(data[21]), fib_id)
157 # VSS control sub-option
158 self.assertEqual(ord(data[22]), 152)
159 self.assertEqual(ord(data[23]), 0)
162 self.assertTrue(found)
166 def verify_dhcp_msg_type(self, pkt, name):
169 for o in dhcp.options:
171 if o[0] == "message-type" \
172 and DHCPTypes[o[1]] == name:
174 self.assertTrue(found)
176 def verify_dhcp_offer(self, pkt, intf, fib_id=0, oui=0):
178 self.assertEqual(ether.dst, "ff:ff:ff:ff:ff:ff")
179 self.assertEqual(ether.src, intf.local_mac)
182 self.assertEqual(ip.dst, "255.255.255.255")
183 self.assertEqual(ip.src, intf.local_ip4)
186 self.assertEqual(udp.dport, DHCP4_CLIENT_PORT)
187 self.assertEqual(udp.sport, DHCP4_SERVER_PORT)
189 self.verify_dhcp_msg_type(pkt, "offer")
190 data = self.validate_relay_options(pkt, intf, intf.local_ip4,
193 def verify_orig_dhcp_pkt(self, pkt, intf):
195 self.assertEqual(ether.dst, "ff:ff:ff:ff:ff:ff")
196 self.assertEqual(ether.src, intf.local_mac)
199 self.assertEqual(ip.dst, "255.255.255.255")
200 self.assertEqual(ip.src, "0.0.0.0")
203 self.assertEqual(udp.dport, DHCP4_SERVER_PORT)
204 self.assertEqual(udp.sport, DHCP4_CLIENT_PORT)
206 def verify_orig_dhcp_discover(self, pkt, intf, hostname, client_id=None):
207 self.verify_orig_dhcp_pkt(pkt, intf)
209 self.verify_dhcp_msg_type(pkt, "discover")
210 self.verify_dhcp_has_option(pkt, "hostname", hostname)
212 self.verify_dhcp_has_option(pkt, "client_id", client_id)
214 self.assertEqual(bootp.ciaddr, "0.0.0.0")
215 self.assertEqual(bootp.giaddr, "0.0.0.0")
216 self.assertEqual(bootp.flags, 0x8000)
218 def verify_orig_dhcp_request(self, pkt, intf, hostname, ip):
219 self.verify_orig_dhcp_pkt(pkt, intf)
221 self.verify_dhcp_msg_type(pkt, "request")
222 self.verify_dhcp_has_option(pkt, "hostname", hostname)
223 self.verify_dhcp_has_option(pkt, "requested_addr", ip)
225 self.assertEqual(bootp.ciaddr, "0.0.0.0")
226 self.assertEqual(bootp.giaddr, "0.0.0.0")
227 self.assertEqual(bootp.flags, 0x8000)
229 def verify_relayed_dhcp_discover(self, pkt, intf, src_intf=None,
231 dst_mac=None, dst_ip=None):
233 dst_mac = intf.remote_mac
235 dst_ip = intf.remote_ip4
238 self.assertEqual(ether.dst, dst_mac)
239 self.assertEqual(ether.src, intf.local_mac)
242 self.assertEqual(ip.dst, dst_ip)
243 self.assertEqual(ip.src, intf.local_ip4)
246 self.assertEqual(udp.dport, DHCP4_SERVER_PORT)
247 self.assertEqual(udp.sport, DHCP4_CLIENT_PORT)
252 for o in dhcp.options:
254 if o[0] == "message-type" \
255 and DHCPTypes[o[1]] == "discover":
257 self.assertTrue(is_discover)
259 data = self.validate_relay_options(pkt, src_intf,
264 def verify_dhcp6_solicit(self, pkt, intf,
271 dst_mac = intf.remote_mac
273 dst_ip = in6_ptop(intf.remote_ip6)
276 self.assertEqual(ether.dst, dst_mac)
277 self.assertEqual(ether.src, intf.local_mac)
280 self.assertEqual(in6_ptop(ip.dst), dst_ip)
281 self.assertEqual(in6_ptop(ip.src), in6_ptop(intf.local_ip6))
284 self.assertEqual(udp.dport, DHCP6_CLIENT_PORT)
285 self.assertEqual(udp.sport, DHCP6_SERVER_PORT)
287 relay = pkt[DHCP6_RelayForward]
288 self.assertEqual(in6_ptop(relay.peeraddr), in6_ptop(peer_ip))
289 oid = pkt[DHCP6OptIfaceId]
290 cll = pkt[DHCP6OptClientLinkLayerAddr]
291 self.assertEqual(cll.optlen, 8)
292 self.assertEqual(cll.lltype, 1)
293 self.assertEqual(cll.clladdr, peer_mac)
296 vss = pkt[DHCP6OptVSS]
297 self.assertEqual(vss.optlen, 8)
298 self.assertEqual(vss.type, 1)
299 # the OUI and FIB-id are really 3 and 4 bytes resp.
300 # but the tested range is small
301 self.assertEqual(ord(vss.data[0]), 0)
302 self.assertEqual(ord(vss.data[1]), 0)
303 self.assertEqual(ord(vss.data[2]), oui)
304 self.assertEqual(ord(vss.data[3]), 0)
305 self.assertEqual(ord(vss.data[4]), 0)
306 self.assertEqual(ord(vss.data[5]), 0)
307 self.assertEqual(ord(vss.data[6]), fib_id)
309 # the relay message should be an encoded Solicit
310 msg = pkt[DHCP6OptRelayMsg]
311 sol = DHCP6_Solicit()
312 self.assertEqual(msg.optlen, len(str(sol)))
313 self.assertEqual(str(sol), (str(msg[1]))[:msg.optlen])
315 def verify_dhcp6_advert(self, pkt, intf, peer):
317 self.assertEqual(ether.dst, "ff:ff:ff:ff:ff:ff")
318 self.assertEqual(ether.src, intf.local_mac)
321 self.assertEqual(in6_ptop(ip.dst), in6_ptop(peer))
322 self.assertEqual(in6_ptop(ip.src), in6_ptop(intf.local_ip6))
325 self.assertEqual(udp.dport, DHCP6_SERVER_PORT)
326 self.assertEqual(udp.sport, DHCP6_CLIENT_PORT)
328 # not sure why this is not decoding
329 # adv = pkt[DHCP6_Advertise]
331 def test_dhcp_proxy(self):
335 # Verify no response to DHCP request without DHCP config
337 p_disc_vrf0 = (Ether(dst="ff:ff:ff:ff:ff:ff",
338 src=self.pg2.remote_mac) /
339 IP(src="0.0.0.0", dst="255.255.255.255") /
340 UDP(sport=DHCP4_CLIENT_PORT,
341 dport=DHCP4_SERVER_PORT) /
343 DHCP(options=[('message-type', 'discover'), ('end')]))
344 pkts_disc_vrf0 = [p_disc_vrf0]
345 p_disc_vrf1 = (Ether(dst="ff:ff:ff:ff:ff:ff",
346 src=self.pg3.remote_mac) /
347 IP(src="0.0.0.0", dst="255.255.255.255") /
348 UDP(sport=DHCP4_CLIENT_PORT,
349 dport=DHCP4_SERVER_PORT) /
351 DHCP(options=[('message-type', 'discover'), ('end')]))
352 pkts_disc_vrf1 = [p_disc_vrf0]
354 self.send_and_assert_no_replies(self.pg2, pkts_disc_vrf0,
355 "DHCP with no configuration")
356 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf1,
357 "DHCP with no configuration")
360 # Enable DHCP proxy in VRF 0
362 server_addr = self.pg0.remote_ip4n
363 src_addr = self.pg0.local_ip4n
365 self.vapi.dhcp_proxy_config(server_addr,
370 # Discover packets from the client are dropped because there is no
371 # IP address configured on the client facing interface
373 self.send_and_assert_no_replies(self.pg2, pkts_disc_vrf0,
374 "Discover DHCP no relay address")
377 # Inject a response from the server
378 # dropped, because there is no IP addrees on the
379 # client interfce to fill in the option.
381 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
382 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
383 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
385 DHCP(options=[('message-type', 'offer'), ('end')]))
388 self.send_and_assert_no_replies(self.pg2, pkts,
389 "Offer DHCP no relay address")
392 # configure an IP address on the client facing interface
394 self.pg2.config_ip4()
397 # Try again with a discover packet
398 # Rx'd packet should be to the server address and from the configured
400 # UDP source ports are unchanged
401 # we've no option 82 config so that should be absent
403 self.pg2.add_stream(pkts_disc_vrf0)
404 self.pg_enable_capture(self.pg_interfaces)
407 rx = self.pg0.get_capture(1)
410 option_82 = self.verify_relayed_dhcp_discover(rx, self.pg0,
414 # Create an DHCP offer reply from the server with a correctly formatted
415 # option 82. i.e. send back what we just captured
416 # The offer, sent mcast to the client, still has option 82.
418 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
419 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
420 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
422 DHCP(options=[('message-type', 'offer'),
423 ('relay_agent_Information', option_82),
427 self.pg0.add_stream(pkts)
428 self.pg_enable_capture(self.pg_interfaces)
431 rx = self.pg2.get_capture(1)
434 self.verify_dhcp_offer(rx, self.pg2)
439 # 1. not our IP address = not checked by VPP? so offer is replayed
441 bad_ip = option_82[0:8] + chr(33) + option_82[9:]
443 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
444 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
445 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
447 DHCP(options=[('message-type', 'offer'),
448 ('relay_agent_Information', bad_ip),
451 self.send_and_assert_no_replies(self.pg0, pkts,
452 "DHCP offer option 82 bad address")
454 # 2. Not a sw_if_index VPP knows
455 bad_if_index = option_82[0:2] + chr(33) + option_82[3:]
457 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
458 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
459 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
461 DHCP(options=[('message-type', 'offer'),
462 ('relay_agent_Information', bad_if_index),
465 self.send_and_assert_no_replies(self.pg0, pkts,
466 "DHCP offer option 82 bad if index")
469 # Send a DHCP request in VRF 1. should be dropped.
471 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf1,
472 "DHCP with no configuration VRF 1")
475 # Delete the DHCP config in VRF 0
476 # Should now drop requests.
478 self.vapi.dhcp_proxy_config(server_addr,
483 self.send_and_assert_no_replies(self.pg2, pkts_disc_vrf0,
484 "DHCP config removed VRF 0")
485 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf1,
486 "DHCP config removed VRF 1")
489 # Add DHCP config for VRF 1
491 server_addr = self.pg1.remote_ip4n
492 src_addr = self.pg1.local_ip4n
493 self.vapi.dhcp_proxy_config(server_addr,
499 # Confim DHCP requests ok in VRF 1.
500 # - dropped on IP config on client interface
502 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf1,
503 "DHCP config removed VRF 1")
506 # configure an IP address on the client facing interface
508 self.pg3.config_ip4()
510 self.pg3.add_stream(pkts_disc_vrf1)
511 self.pg_enable_capture(self.pg_interfaces)
514 rx = self.pg1.get_capture(1)
516 self.verify_relayed_dhcp_discover(rx, self.pg1, src_intf=self.pg3)
520 # table=1, fib=id=1, oui=4
521 self.vapi.dhcp_proxy_set_vss(1, 1, 4)
523 self.pg3.add_stream(pkts_disc_vrf1)
524 self.pg_enable_capture(self.pg_interfaces)
527 rx = self.pg1.get_capture(1)
529 self.verify_relayed_dhcp_discover(rx, self.pg1,
534 # Add a second DHCP server in VRF 1
535 # expect clients messages to be relay to both configured servers
537 self.pg1.generate_remote_hosts(2)
538 server_addr2 = socket.inet_pton(AF_INET, self.pg1.remote_hosts[1].ip4)
540 self.vapi.dhcp_proxy_config(server_addr2,
547 # We'll need an ARP entry for the server to send it packets
549 arp_entry = VppNeighbor(self,
550 self.pg1.sw_if_index,
551 self.pg1.remote_hosts[1].mac,
552 self.pg1.remote_hosts[1].ip4)
553 arp_entry.add_vpp_config()
556 # Send a discover from the client. expect two relayed messages
557 # The frist packet is sent to the second server
558 # We're not enforcing that here, it's just the way it is.
560 self.pg3.add_stream(pkts_disc_vrf1)
561 self.pg_enable_capture(self.pg_interfaces)
564 rx = self.pg1.get_capture(2)
566 option_82 = self.verify_relayed_dhcp_discover(
569 dst_mac=self.pg1.remote_hosts[1].mac,
570 dst_ip=self.pg1.remote_hosts[1].ip4,
572 self.verify_relayed_dhcp_discover(rx[1], self.pg1,
577 # Send both packets back. Client gets both.
579 p1 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
580 IP(src=self.pg1.remote_ip4, dst=self.pg1.local_ip4) /
581 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
583 DHCP(options=[('message-type', 'offer'),
584 ('relay_agent_Information', option_82),
586 p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
587 IP(src=self.pg1.remote_hosts[1].ip4, dst=self.pg1.local_ip4) /
588 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
590 DHCP(options=[('message-type', 'offer'),
591 ('relay_agent_Information', option_82),
595 self.pg1.add_stream(pkts)
596 self.pg_enable_capture(self.pg_interfaces)
599 rx = self.pg3.get_capture(2)
601 self.verify_dhcp_offer(rx[0], self.pg3, fib_id=1, oui=4)
602 self.verify_dhcp_offer(rx[1], self.pg3, fib_id=1, oui=4)
605 # Ensure offers from non-servers are dropeed
607 p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
608 IP(src="8.8.8.8", dst=self.pg1.local_ip4) /
609 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
611 DHCP(options=[('message-type', 'offer'),
612 ('relay_agent_Information', option_82),
614 self.send_and_assert_no_replies(self.pg1, p2,
615 "DHCP offer from non-server")
618 # Ensure only the discover is sent to multiple servers
620 p_req_vrf1 = (Ether(dst="ff:ff:ff:ff:ff:ff",
621 src=self.pg3.remote_mac) /
622 IP(src="0.0.0.0", dst="255.255.255.255") /
623 UDP(sport=DHCP4_CLIENT_PORT,
624 dport=DHCP4_SERVER_PORT) /
626 DHCP(options=[('message-type', 'request'),
629 self.pg3.add_stream(p_req_vrf1)
630 self.pg_enable_capture(self.pg_interfaces)
633 rx = self.pg1.get_capture(1)
636 # Remove the second DHCP server
638 self.vapi.dhcp_proxy_config(server_addr2,
645 # Test we can still relay with the first
647 self.pg3.add_stream(pkts_disc_vrf1)
648 self.pg_enable_capture(self.pg_interfaces)
651 rx = self.pg1.get_capture(1)
653 self.verify_relayed_dhcp_discover(rx, self.pg1,
658 # Remove the VSS config
659 # relayed DHCP has default vlaues in the option.
661 self.vapi.dhcp_proxy_set_vss(1, 1, 4, is_add=0)
663 self.pg3.add_stream(pkts_disc_vrf1)
664 self.pg_enable_capture(self.pg_interfaces)
667 rx = self.pg1.get_capture(1)
669 self.verify_relayed_dhcp_discover(rx, self.pg1, src_intf=self.pg3)
672 # remove DHCP config to cleanup
674 self.vapi.dhcp_proxy_config(server_addr,
680 self.send_and_assert_no_replies(self.pg2, pkts_disc_vrf0,
681 "DHCP cleanup VRF 0")
682 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf1,
683 "DHCP cleanup VRF 1")
684 self.pg2.unconfig_ip4()
685 self.pg3.unconfig_ip4()
687 def test_dhcp6_proxy(self):
690 # Verify no response to DHCP request without DHCP config
692 dhcp_solicit_dst = "ff02::1:2"
693 dhcp_solicit_src_vrf0 = mk_ll_addr(self.pg2.remote_mac)
694 dhcp_solicit_src_vrf1 = mk_ll_addr(self.pg3.remote_mac)
695 server_addr_vrf0 = self.pg0.remote_ip6n
696 src_addr_vrf0 = self.pg0.local_ip6n
697 server_addr_vrf1 = self.pg1.remote_ip6n
698 src_addr_vrf1 = self.pg1.local_ip6n
700 dmac = in6_getnsmac(inet_pton(socket.AF_INET6, dhcp_solicit_dst))
701 p_solicit_vrf0 = (Ether(dst=dmac, src=self.pg2.remote_mac) /
702 IPv6(src=dhcp_solicit_src_vrf0,
703 dst=dhcp_solicit_dst) /
704 UDP(sport=DHCP6_SERVER_PORT,
705 dport=DHCP6_CLIENT_PORT) /
707 p_solicit_vrf1 = (Ether(dst=dmac, src=self.pg3.remote_mac) /
708 IPv6(src=dhcp_solicit_src_vrf1,
709 dst=dhcp_solicit_dst) /
710 UDP(sport=DHCP6_SERVER_PORT,
711 dport=DHCP6_CLIENT_PORT) /
714 self.send_and_assert_no_replies(self.pg2, p_solicit_vrf0,
715 "DHCP with no configuration")
716 self.send_and_assert_no_replies(self.pg3, p_solicit_vrf1,
717 "DHCP with no configuration")
720 # DHCPv6 config in VRF 0.
721 # Packets still dropped because the client facing interface has no
724 self.vapi.dhcp_proxy_config(server_addr_vrf0,
730 self.send_and_assert_no_replies(self.pg2, p_solicit_vrf0,
731 "DHCP with no configuration")
732 self.send_and_assert_no_replies(self.pg3, p_solicit_vrf1,
733 "DHCP with no configuration")
736 # configure an IP address on the client facing interface
738 self.pg2.config_ip6()
741 # Now the DHCP requests are relayed to the server
743 self.pg2.add_stream(p_solicit_vrf0)
744 self.pg_enable_capture(self.pg_interfaces)
747 rx = self.pg0.get_capture(1)
749 self.verify_dhcp6_solicit(rx[0], self.pg0,
750 dhcp_solicit_src_vrf0,
754 # Exception cases for rejected relay responses
757 # 1 - not a relay reply
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 self.send_and_assert_no_replies(self.pg2, p_adv_vrf0,
763 "DHCP6 not a relay reply")
765 # 2 - no relay message option
766 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
767 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
768 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
771 self.send_and_assert_no_replies(self.pg2, p_adv_vrf0,
772 "DHCP not a relay message")
775 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
776 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
777 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
779 DHCP6OptRelayMsg(optlen=0) /
781 self.send_and_assert_no_replies(self.pg2, p_adv_vrf0,
782 "DHCP6 no circuit ID")
783 # 4 - wrong circuit ID
784 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
785 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
786 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
788 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x05') /
789 DHCP6OptRelayMsg(optlen=0) /
791 self.send_and_assert_no_replies(self.pg2, p_adv_vrf0,
792 "DHCP6 wrong circuit ID")
795 # Send the relay response (the advertisement)
797 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
798 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
799 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
801 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x03') /
802 DHCP6OptRelayMsg(optlen=0) /
803 DHCP6_Advertise(trid=1) /
804 DHCP6OptStatusCode(statuscode=0))
805 pkts_adv_vrf0 = [p_adv_vrf0]
807 self.pg0.add_stream(pkts_adv_vrf0)
808 self.pg_enable_capture(self.pg_interfaces)
811 rx = self.pg2.get_capture(1)
813 self.verify_dhcp6_advert(rx[0], self.pg2, "::")
816 # Send the relay response (the advertisement)
817 # - with peer address
818 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
819 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
820 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
821 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf0) /
822 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x03') /
823 DHCP6OptRelayMsg(optlen=0) /
824 DHCP6_Advertise(trid=1) /
825 DHCP6OptStatusCode(statuscode=0))
826 pkts_adv_vrf0 = [p_adv_vrf0]
828 self.pg0.add_stream(pkts_adv_vrf0)
829 self.pg_enable_capture(self.pg_interfaces)
832 rx = self.pg2.get_capture(1)
834 self.verify_dhcp6_advert(rx[0], self.pg2, dhcp_solicit_src_vrf0)
837 # Add all the config for VRF 1
839 self.vapi.dhcp_proxy_config(server_addr_vrf1,
844 self.pg3.config_ip6()
849 self.pg3.add_stream(p_solicit_vrf1)
850 self.pg_enable_capture(self.pg_interfaces)
853 rx = self.pg1.get_capture(1)
855 self.verify_dhcp6_solicit(rx[0], self.pg1,
856 dhcp_solicit_src_vrf1,
862 p_adv_vrf1 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
863 IPv6(dst=self.pg1.local_ip6, src=self.pg1.remote_ip6) /
864 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
865 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) /
866 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x04') /
867 DHCP6OptRelayMsg(optlen=0) /
868 DHCP6_Advertise(trid=1) /
869 DHCP6OptStatusCode(statuscode=0))
870 pkts_adv_vrf1 = [p_adv_vrf1]
872 self.pg1.add_stream(pkts_adv_vrf1)
873 self.pg_enable_capture(self.pg_interfaces)
876 rx = self.pg3.get_capture(1)
878 self.verify_dhcp6_advert(rx[0], self.pg3, dhcp_solicit_src_vrf1)
882 # table=1, fib=id=1, oui=4
883 self.vapi.dhcp_proxy_set_vss(1, 1, 4, is_ip6=1)
885 self.pg3.add_stream(p_solicit_vrf1)
886 self.pg_enable_capture(self.pg_interfaces)
889 rx = self.pg1.get_capture(1)
891 self.verify_dhcp6_solicit(rx[0], self.pg1,
892 dhcp_solicit_src_vrf1,
898 # Remove the VSS config
899 # relayed DHCP has default vlaues in the option.
901 self.vapi.dhcp_proxy_set_vss(1, 1, 4, is_ip6=1, is_add=0)
903 self.pg3.add_stream(p_solicit_vrf1)
904 self.pg_enable_capture(self.pg_interfaces)
907 rx = self.pg1.get_capture(1)
909 self.verify_dhcp6_solicit(rx[0], self.pg1,
910 dhcp_solicit_src_vrf1,
914 # Add a second DHCP server in VRF 1
915 # expect clients messages to be relay to both configured servers
917 self.pg1.generate_remote_hosts(2)
918 server_addr2 = socket.inet_pton(AF_INET6, self.pg1.remote_hosts[1].ip6)
920 self.vapi.dhcp_proxy_config(server_addr2,
927 # We'll need an ND entry for the server to send it packets
929 nd_entry = VppNeighbor(self,
930 self.pg1.sw_if_index,
931 self.pg1.remote_hosts[1].mac,
932 self.pg1.remote_hosts[1].ip6,
934 nd_entry.add_vpp_config()
937 # Send a discover from the client. expect two relayed messages
938 # The frist packet is sent to the second server
939 # We're not enforcing that here, it's just the way it is.
941 self.pg3.add_stream(p_solicit_vrf1)
942 self.pg_enable_capture(self.pg_interfaces)
945 rx = self.pg1.get_capture(2)
947 self.verify_dhcp6_solicit(rx[0], self.pg1,
948 dhcp_solicit_src_vrf1,
950 self.verify_dhcp6_solicit(rx[1], self.pg1,
951 dhcp_solicit_src_vrf1,
953 dst_mac=self.pg1.remote_hosts[1].mac,
954 dst_ip=self.pg1.remote_hosts[1].ip6)
957 # Send both packets back. Client gets both.
959 p1 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
960 IPv6(dst=self.pg1.local_ip6, src=self.pg1.remote_ip6) /
961 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
962 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) /
963 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x04') /
964 DHCP6OptRelayMsg(optlen=0) /
965 DHCP6_Advertise(trid=1) /
966 DHCP6OptStatusCode(statuscode=0))
967 p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_hosts[1].mac) /
968 IPv6(dst=self.pg1.local_ip6, src=self.pg1._remote_hosts[1].ip6) /
969 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
970 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) /
971 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x04') /
972 DHCP6OptRelayMsg(optlen=0) /
973 DHCP6_Advertise(trid=1) /
974 DHCP6OptStatusCode(statuscode=0))
978 self.pg1.add_stream(pkts)
979 self.pg_enable_capture(self.pg_interfaces)
982 rx = self.pg3.get_capture(2)
984 self.verify_dhcp6_advert(rx[0], self.pg3, dhcp_solicit_src_vrf1)
985 self.verify_dhcp6_advert(rx[1], self.pg3, dhcp_solicit_src_vrf1)
988 # Ensure only solicit messages are duplicated
990 p_request_vrf1 = (Ether(dst=dmac, src=self.pg3.remote_mac) /
991 IPv6(src=dhcp_solicit_src_vrf1,
992 dst=dhcp_solicit_dst) /
993 UDP(sport=DHCP6_SERVER_PORT,
994 dport=DHCP6_CLIENT_PORT) /
997 self.pg3.add_stream(p_request_vrf1)
998 self.pg_enable_capture(self.pg_interfaces)
1001 rx = self.pg1.get_capture(1)
1004 # Test we drop DHCP packets from addresses that are not configured as
1007 p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_hosts[1].mac) /
1008 IPv6(dst=self.pg1.local_ip6, src="3001::1") /
1009 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
1010 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) /
1011 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x04') /
1012 DHCP6OptRelayMsg(optlen=0) /
1013 DHCP6_Advertise(trid=1) /
1014 DHCP6OptStatusCode(statuscode=0))
1015 self.send_and_assert_no_replies(self.pg1, p2,
1016 "DHCP6 not from server")
1019 # Remove the second DHCP server
1021 self.vapi.dhcp_proxy_config(server_addr2,
1029 # Test we can still relay with the first
1031 self.pg3.add_stream(p_solicit_vrf1)
1032 self.pg_enable_capture(self.pg_interfaces)
1035 rx = self.pg1.get_capture(1)
1037 self.verify_dhcp6_solicit(rx[0], self.pg1,
1038 dhcp_solicit_src_vrf1,
1039 self.pg3.remote_mac)
1044 self.vapi.dhcp_proxy_config(server_addr_vrf1,
1050 self.vapi.dhcp_proxy_config(server_addr_vrf0,
1058 self.vapi.dhcp_proxy_config(server_addr_vrf0,
1064 self.pg2.unconfig_ip6()
1065 self.pg3.unconfig_ip6()
1067 def test_dhcp_client(self):
1070 hostname = 'universal-dp'
1072 self.pg_enable_capture(self.pg_interfaces)
1075 # Configure DHCP client on PG2 and capture the discover sent
1077 self.vapi.dhcp_client(self.pg2.sw_if_index, hostname)
1079 rx = self.pg2.get_capture(1)
1081 self.verify_orig_dhcp_discover(rx[0], self.pg2, hostname)
1084 # Sned back on offer, expect the request
1086 p_offer = (Ether(dst=self.pg2.local_mac, src=self.pg2.remote_mac) /
1087 IP(src=self.pg2.remote_ip4, dst="255.255.255.255") /
1088 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1089 BOOTP(op=1, yiaddr=self.pg2.local_ip4) /
1090 DHCP(options=[('message-type', 'offer'),
1091 ('server_id', self.pg2.remote_ip4),
1094 self.pg2.add_stream(p_offer)
1095 self.pg_enable_capture(self.pg_interfaces)
1098 rx = self.pg2.get_capture(1)
1099 self.verify_orig_dhcp_request(rx[0], self.pg2, hostname,
1103 # Send an acknowloedgement
1105 p_ack = (Ether(dst=self.pg2.local_mac, src=self.pg2.remote_mac) /
1106 IP(src=self.pg2.remote_ip4, dst="255.255.255.255") /
1107 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1108 BOOTP(op=1, yiaddr=self.pg2.local_ip4) /
1109 DHCP(options=[('message-type', 'ack'),
1110 ('subnet_mask', "255.255.255.0"),
1111 ('router', self.pg2.remote_ip4),
1112 ('server_id', self.pg2.remote_ip4),
1113 ('lease_time', 43200),
1116 self.pg2.add_stream(p_ack)
1117 self.pg_enable_capture(self.pg_interfaces)
1121 # We'll get an ARP request for the router address
1123 rx = self.pg2.get_capture(1)
1125 self.assertEqual(rx[0][ARP].pdst, self.pg2.remote_ip4)
1126 self.pg_enable_capture(self.pg_interfaces)
1129 # At the end of this procedure there should be a connected route
1132 self.assertTrue(find_route(self, self.pg2.local_ip4, 24))
1133 self.assertTrue(find_route(self, self.pg2.local_ip4, 32))
1135 # remove the left over ARP entry
1136 self.vapi.ip_neighbor_add_del(self.pg2.sw_if_index,
1137 self.pg2.remote_mac,
1138 self.pg2.remote_ip4,
1141 # remove the DHCP config
1143 self.vapi.dhcp_client(self.pg2.sw_if_index, hostname, is_add=0)
1146 # and now the route should be gone
1148 self.assertFalse(find_route(self, self.pg2.local_ip4, 32))
1149 self.assertFalse(find_route(self, self.pg2.local_ip4, 24))
1152 # Start the procedure again. this time have VPP send the client-ID
1154 self.pg2.admin_down()
1157 self.vapi.dhcp_client(self.pg2.sw_if_index, hostname,
1158 client_id=self.pg2.local_mac)
1160 rx = self.pg2.get_capture(1)
1162 self.verify_orig_dhcp_discover(rx[0], self.pg2, hostname,
1165 self.pg2.add_stream(p_offer)
1166 self.pg_enable_capture(self.pg_interfaces)
1169 rx = self.pg2.get_capture(1)
1170 self.verify_orig_dhcp_request(rx[0], self.pg2, hostname,
1174 # unicast the ack to the offered address
1176 p_ack = (Ether(dst=self.pg2.local_mac, src=self.pg2.remote_mac) /
1177 IP(src=self.pg2.remote_ip4, dst=self.pg2.local_ip4) /
1178 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1179 BOOTP(op=1, yiaddr=self.pg2.local_ip4) /
1180 DHCP(options=[('message-type', 'ack'),
1181 ('subnet_mask', "255.255.255.0"),
1182 ('router', self.pg2.remote_ip4),
1183 ('server_id', self.pg2.remote_ip4),
1184 ('lease_time', 43200),
1187 self.pg2.add_stream(p_ack)
1188 self.pg_enable_capture(self.pg_interfaces)
1192 # At the end of this procedure there should be a connected route
1195 self.assertTrue(find_route(self, self.pg2.local_ip4, 32))
1196 self.assertTrue(find_route(self, self.pg2.local_ip4, 24))
1199 # remove the DHCP config
1201 self.vapi.dhcp_client(self.pg2.sw_if_index, hostname, is_add=0)
1203 self.assertFalse(find_route(self, self.pg2.local_ip4, 32))
1204 self.assertFalse(find_route(self, self.pg2.local_ip4, 24))
1207 if __name__ == '__main__':
1208 unittest.main(testRunner=VppTestRunner)