8 from framework import VppTestCase, VppTestRunner, running_extended_tests
9 from vpp_neighbor import VppNeighbor
10 from vpp_ip_route import find_route, VppIpTable
11 from util import mk_ll_addr
13 from scapy.layers.l2 import Ether, getmacbyip, ARP, Dot1Q
14 from scapy.layers.inet import IP, UDP, ICMP
15 from scapy.layers.inet6 import IPv6, in6_getnsmac
16 from scapy.utils6 import in6_mactoifaceid
17 from scapy.layers.dhcp import DHCP, BOOTP, DHCPTypes
18 from scapy.layers.dhcp6 import DHCP6, DHCP6_Solicit, DHCP6_RelayForward, \
19 DHCP6_RelayReply, DHCP6_Advertise, DHCP6OptRelayMsg, DHCP6OptIfaceId, \
20 DHCP6OptStatusCode, DHCP6OptVSS, DHCP6OptClientLinkLayerAddr, DHCP6_Request
21 from socket import AF_INET, AF_INET6
22 from scapy.utils import inet_pton, inet_ntop
23 from scapy.utils6 import in6_ptop
24 from vpp_papi import mac_pton, VppEnum
25 from vpp_sub_interface import VppDot1QSubint
26 from vpp_qos import VppQosEgressMap, VppQosMark
27 from vpp_dhcp import VppDHCPClient, VppDHCPProxy
30 DHCP4_CLIENT_PORT = 68
31 DHCP4_SERVER_PORT = 67
32 DHCP6_CLIENT_PORT = 547
33 DHCP6_SERVER_PORT = 546
36 class TestDHCP(VppTestCase):
37 """ DHCP Test Case """
41 super(TestDHCP, cls).setUpClass()
44 def tearDownClass(cls):
45 super(TestDHCP, cls).tearDownClass()
48 super(TestDHCP, self).setUp()
50 # create 6 pg interfaces for pg0 to pg5
51 self.create_pg_interfaces(range(6))
54 # pg0 to 2 are IP configured in VRF 0, 1 and 2.
55 # pg3 to 5 are non IP-configured in VRF 0, 1 and 2.
57 for table_id in range(1, 4):
58 tbl4 = VppIpTable(self, table_id)
60 self.tables.append(tbl4)
61 tbl6 = VppIpTable(self, table_id, is_ip6=1)
63 self.tables.append(tbl6)
66 for i in self.pg_interfaces[:3]:
68 i.set_table_ip4(table_id)
69 i.set_table_ip6(table_id)
77 for i in self.pg_interfaces[3:]:
79 i.set_table_ip4(table_id)
80 i.set_table_ip6(table_id)
84 for i in self.pg_interfaces[:3]:
88 for i in self.pg_interfaces:
92 super(TestDHCP, self).tearDown()
94 def verify_dhcp_has_option(self, pkt, option, value):
98 for i in dhcp.options:
99 if isinstance(i, tuple):
101 self.assertEqual(i[1], value)
104 self.assertTrue(found)
106 def validate_relay_options(self, pkt, intf, ip_addr, vpn_id, fib_id, oui):
112 for i in dhcp.options:
113 if isinstance(i, tuple):
114 if i[0] == "relay_agent_Information":
116 # There are two sb-options present - each of length 6.
120 self.assertEqual(len(data), 24)
121 elif len(vpn_id) > 0:
122 self.assertEqual(len(data), len(vpn_id) + 17)
124 self.assertEqual(len(data), 12)
127 # First sub-option is ID 1, len 4, then encoded
128 # sw_if_index. This test uses low valued indicies
130 # The ID space is VPP internal - so no matching value
133 self.assertEqual(six.byte2int(data[0:1]), 1)
134 self.assertEqual(six.byte2int(data[1:2]), 4)
135 self.assertEqual(six.byte2int(data[2:3]), 0)
136 self.assertEqual(six.byte2int(data[3:4]), 0)
137 self.assertEqual(six.byte2int(data[4:5]), 0)
138 self.assertEqual(six.byte2int(data[5:6]),
142 # next sub-option is the IP address of the client side
144 # sub-option ID=5, length (of a v4 address)=4
146 claddr = socket.inet_pton(AF_INET, ip_addr)
148 self.assertEqual(six.byte2int(data[6:7]), 5)
149 self.assertEqual(six.byte2int(data[7:8]), 4)
150 self.assertEqual(data[8], claddr[0])
151 self.assertEqual(data[9], claddr[1])
152 self.assertEqual(data[10], claddr[2])
153 self.assertEqual(data[11], claddr[3])
156 # sub-option 151 encodes vss_type 1,
157 # the 3 byte oui and the 4 byte fib_id
158 self.assertEqual(id_len, 0)
159 self.assertEqual(six.byte2int(data[12:13]), 151)
160 self.assertEqual(six.byte2int(data[13:14]), 8)
161 self.assertEqual(six.byte2int(data[14:15]), 1)
162 self.assertEqual(six.byte2int(data[15:16]), 0)
163 self.assertEqual(six.byte2int(data[16:17]), 0)
164 self.assertEqual(six.byte2int(data[17:18]), oui)
165 self.assertEqual(six.byte2int(data[18:19]), 0)
166 self.assertEqual(six.byte2int(data[19:20]), 0)
167 self.assertEqual(six.byte2int(data[20:21]), 0)
168 self.assertEqual(six.byte2int(data[21:22]), fib_id)
170 # VSS control sub-option
171 self.assertEqual(six.byte2int(data[22:23]), 152)
172 self.assertEqual(six.byte2int(data[23:24]), 0)
175 # sub-option 151 encode vss_type of 0
176 # followerd by vpn_id in ascii
177 self.assertEqual(oui, 0)
178 self.assertEqual(six.byte2int(data[12:13]), 151)
179 self.assertEqual(six.byte2int(data[13:14]), id_len + 1)
180 self.assertEqual(six.byte2int(data[14:15]), 0)
181 self.assertEqual(data[15:15 + id_len].decode('ascii'),
184 # VSS control sub-option
185 self.assertEqual(six.byte2int(data[15 + len(vpn_id):
188 self.assertEqual(six.byte2int(data[16 + len(vpn_id):
193 self.assertTrue(found)
197 def verify_dhcp_msg_type(self, pkt, name):
200 for o in dhcp.options:
201 if isinstance(o, tuple):
202 if o[0] == "message-type" \
203 and DHCPTypes[o[1]] == name:
205 self.assertTrue(found)
207 def verify_dhcp_offer(self, pkt, intf, vpn_id="", fib_id=0, oui=0):
209 self.assertEqual(ether.dst, "ff:ff:ff:ff:ff:ff")
210 self.assertEqual(ether.src, intf.local_mac)
213 self.assertEqual(ip.dst, "255.255.255.255")
214 self.assertEqual(ip.src, intf.local_ip4)
217 self.assertEqual(udp.dport, DHCP4_CLIENT_PORT)
218 self.assertEqual(udp.sport, DHCP4_SERVER_PORT)
220 self.verify_dhcp_msg_type(pkt, "offer")
221 data = self.validate_relay_options(pkt, intf, intf.local_ip4,
224 def verify_orig_dhcp_pkt(self, pkt, intf, dscp, l2_bc=True):
227 self.assertEqual(ether.dst, "ff:ff:ff:ff:ff:ff")
229 self.assertEqual(ether.dst, intf.remote_mac)
230 self.assertEqual(ether.src, intf.local_mac)
235 self.assertEqual(ip.dst, "255.255.255.255")
236 self.assertEqual(ip.src, "0.0.0.0")
238 self.assertEqual(ip.dst, intf.remote_ip4)
239 self.assertEqual(ip.src, intf.local_ip4)
240 self.assertEqual(ip.tos, dscp)
243 self.assertEqual(udp.dport, DHCP4_SERVER_PORT)
244 self.assertEqual(udp.sport, DHCP4_CLIENT_PORT)
246 def verify_orig_dhcp_discover(self, pkt, intf, hostname, client_id=None,
247 broadcast=True, dscp=0):
248 self.verify_orig_dhcp_pkt(pkt, intf, dscp)
250 self.verify_dhcp_msg_type(pkt, "discover")
251 self.verify_dhcp_has_option(pkt, "hostname",
252 hostname.encode('ascii'))
254 client_id = '\x00' + client_id
255 self.verify_dhcp_has_option(pkt, "client_id",
256 client_id.encode('ascii'))
258 self.assertEqual(bootp.ciaddr, "0.0.0.0")
259 self.assertEqual(bootp.giaddr, "0.0.0.0")
261 self.assertEqual(bootp.flags, 0x8000)
263 self.assertEqual(bootp.flags, 0x0000)
265 def verify_orig_dhcp_request(self, pkt, intf, hostname, ip,
269 self.verify_orig_dhcp_pkt(pkt, intf, dscp, l2_bc=l2_bc)
271 self.verify_dhcp_msg_type(pkt, "request")
272 self.verify_dhcp_has_option(pkt, "hostname",
273 hostname.encode('ascii'))
274 self.verify_dhcp_has_option(pkt, "requested_addr", ip)
278 self.assertEqual(bootp.ciaddr, "0.0.0.0")
280 self.assertEqual(bootp.ciaddr, intf.local_ip4)
281 self.assertEqual(bootp.giaddr, "0.0.0.0")
284 self.assertEqual(bootp.flags, 0x8000)
286 self.assertEqual(bootp.flags, 0x0000)
288 def verify_relayed_dhcp_discover(self, pkt, intf, src_intf=None,
291 dst_mac=None, dst_ip=None):
293 dst_mac = intf.remote_mac
295 dst_ip = intf.remote_ip4
298 self.assertEqual(ether.dst, dst_mac)
299 self.assertEqual(ether.src, intf.local_mac)
302 self.assertEqual(ip.dst, dst_ip)
303 self.assertEqual(ip.src, intf.local_ip4)
306 self.assertEqual(udp.dport, DHCP4_SERVER_PORT)
307 self.assertEqual(udp.sport, DHCP4_CLIENT_PORT)
312 for o in dhcp.options:
313 if isinstance(o, tuple):
314 if o[0] == "message-type" \
315 and DHCPTypes[o[1]] == "discover":
317 self.assertTrue(is_discover)
319 data = self.validate_relay_options(pkt, src_intf,
325 def verify_dhcp6_solicit(self, pkt, intf,
333 dst_mac = intf.remote_mac
335 dst_ip = in6_ptop(intf.remote_ip6)
338 self.assertEqual(ether.dst, dst_mac)
339 self.assertEqual(ether.src, intf.local_mac)
342 self.assertEqual(in6_ptop(ip.dst), dst_ip)
343 self.assertEqual(in6_ptop(ip.src), in6_ptop(intf.local_ip6))
346 self.assertEqual(udp.dport, DHCP6_CLIENT_PORT)
347 self.assertEqual(udp.sport, DHCP6_SERVER_PORT)
349 relay = pkt[DHCP6_RelayForward]
350 self.assertEqual(in6_ptop(relay.peeraddr), in6_ptop(peer_ip))
351 oid = pkt[DHCP6OptIfaceId]
352 cll = pkt[DHCP6OptClientLinkLayerAddr]
353 self.assertEqual(cll.optlen, 8)
354 self.assertEqual(cll.lltype, 1)
355 self.assertEqual(cll.clladdr, peer_mac)
360 self.assertEqual(id_len, 0)
361 vss = pkt[DHCP6OptVSS]
362 self.assertEqual(vss.optlen, 8)
363 self.assertEqual(vss.type, 1)
364 # the OUI and FIB-id are really 3 and 4 bytes resp.
365 # but the tested range is small
366 self.assertEqual(six.byte2int(vss.data[0:1]), 0)
367 self.assertEqual(six.byte2int(vss.data[1:2]), 0)
368 self.assertEqual(six.byte2int(vss.data[2:3]), oui)
369 self.assertEqual(six.byte2int(vss.data[3:4]), 0)
370 self.assertEqual(six.byte2int(vss.data[4:5]), 0)
371 self.assertEqual(six.byte2int(vss.data[5:6]), 0)
372 self.assertEqual(six.byte2int(vss.data[6:7]), fib_id)
375 self.assertEqual(oui, 0)
376 vss = pkt[DHCP6OptVSS]
377 self.assertEqual(vss.optlen, id_len + 1)
378 self.assertEqual(vss.type, 0)
379 self.assertEqual(vss.data[0:id_len].decode('ascii'),
382 # the relay message should be an encoded Solicit
383 msg = pkt[DHCP6OptRelayMsg]
384 sol = DHCP6_Solicit()
385 self.assertEqual(msg.optlen, len(sol))
386 self.assertEqual(sol, msg[1])
388 def verify_dhcp6_advert(self, pkt, intf, peer):
390 self.assertEqual(ether.dst, "ff:ff:ff:ff:ff:ff")
391 self.assertEqual(ether.src, intf.local_mac)
394 self.assertEqual(in6_ptop(ip.dst), in6_ptop(peer))
395 self.assertEqual(in6_ptop(ip.src), in6_ptop(intf.local_ip6))
398 self.assertEqual(udp.dport, DHCP6_SERVER_PORT)
399 self.assertEqual(udp.sport, DHCP6_CLIENT_PORT)
401 # not sure why this is not decoding
402 # adv = pkt[DHCP6_Advertise]
404 def wait_for_no_route(self, address, length,
405 n_tries=50, s_time=1):
407 if not find_route(self, address, length):
409 n_tries = n_tries - 1
414 def test_dhcp_proxy(self):
418 # Verify no response to DHCP request without DHCP config
420 p_disc_vrf0 = (Ether(dst="ff:ff:ff:ff:ff:ff",
421 src=self.pg3.remote_mac) /
422 IP(src="0.0.0.0", dst="255.255.255.255") /
423 UDP(sport=DHCP4_CLIENT_PORT,
424 dport=DHCP4_SERVER_PORT) /
426 DHCP(options=[('message-type', 'discover'), ('end')]))
427 pkts_disc_vrf0 = [p_disc_vrf0]
428 p_disc_vrf1 = (Ether(dst="ff:ff:ff:ff:ff:ff",
429 src=self.pg4.remote_mac) /
430 IP(src="0.0.0.0", dst="255.255.255.255") /
431 UDP(sport=DHCP4_CLIENT_PORT,
432 dport=DHCP4_SERVER_PORT) /
434 DHCP(options=[('message-type', 'discover'), ('end')]))
435 pkts_disc_vrf1 = [p_disc_vrf1]
436 p_disc_vrf2 = (Ether(dst="ff:ff:ff:ff:ff:ff",
437 src=self.pg5.remote_mac) /
438 IP(src="0.0.0.0", dst="255.255.255.255") /
439 UDP(sport=DHCP4_CLIENT_PORT,
440 dport=DHCP4_SERVER_PORT) /
442 DHCP(options=[('message-type', 'discover'), ('end')]))
443 pkts_disc_vrf2 = [p_disc_vrf2]
445 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf0,
446 "DHCP with no configuration")
447 self.send_and_assert_no_replies(self.pg4, pkts_disc_vrf1,
448 "DHCP with no configuration")
449 self.send_and_assert_no_replies(self.pg5, pkts_disc_vrf2,
450 "DHCP with no configuration")
453 # Enable DHCP proxy in VRF 0
455 server_addr = self.pg0.remote_ip4
456 src_addr = self.pg0.local_ip4
458 Proxy = VppDHCPProxy(self, server_addr, src_addr, rx_vrf_id=0)
459 Proxy.add_vpp_config()
462 # Discover packets from the client are dropped because there is no
463 # IP address configured on the client facing interface
465 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf0,
466 "Discover DHCP no relay address")
469 # Inject a response from the server
470 # dropped, because there is no IP addrees on the
471 # client interfce to fill in the option.
473 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
474 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
475 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
477 DHCP(options=[('message-type', 'offer'), ('end')]))
480 self.send_and_assert_no_replies(self.pg3, pkts,
481 "Offer DHCP no relay address")
484 # configure an IP address on the client facing interface
486 self.pg3.config_ip4()
489 # Try again with a discover packet
490 # Rx'd packet should be to the server address and from the configured
492 # UDP source ports are unchanged
493 # we've no option 82 config so that should be absent
495 self.pg3.add_stream(pkts_disc_vrf0)
496 self.pg_enable_capture(self.pg_interfaces)
499 rx = self.pg0.get_capture(1)
502 option_82 = self.verify_relayed_dhcp_discover(rx, self.pg0,
506 # Create an DHCP offer reply from the server with a correctly formatted
507 # option 82. i.e. send back what we just captured
508 # The offer, sent mcast to the client, still has option 82.
510 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
511 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
512 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
514 DHCP(options=[('message-type', 'offer'),
515 ('relay_agent_Information', option_82),
519 self.pg0.add_stream(pkts)
520 self.pg_enable_capture(self.pg_interfaces)
523 rx = self.pg3.get_capture(1)
526 self.verify_dhcp_offer(rx, self.pg3)
531 # 1. not our IP address = not checked by VPP? so offer is replayed
533 bad_ip = option_82[0:8] + scapy.compat.chb(33) + option_82[9:]
535 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
536 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
537 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
539 DHCP(options=[('message-type', 'offer'),
540 ('relay_agent_Information', bad_ip),
543 self.send_and_assert_no_replies(self.pg0, pkts,
544 "DHCP offer option 82 bad address")
546 # 2. Not a sw_if_index VPP knows
547 bad_if_index = option_82[0:2] + scapy.compat.chb(33) + option_82[3:]
549 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
550 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
551 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
553 DHCP(options=[('message-type', 'offer'),
554 ('relay_agent_Information', bad_if_index),
557 self.send_and_assert_no_replies(self.pg0, pkts,
558 "DHCP offer option 82 bad if index")
561 # Send a DHCP request in VRF 1. should be dropped.
563 self.send_and_assert_no_replies(self.pg4, pkts_disc_vrf1,
564 "DHCP with no configuration VRF 1")
567 # Delete the DHCP config in VRF 0
568 # Should now drop requests.
570 Proxy.remove_vpp_config()
572 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf0,
573 "DHCP config removed VRF 0")
574 self.send_and_assert_no_replies(self.pg4, pkts_disc_vrf1,
575 "DHCP config removed VRF 1")
578 # Add DHCP config for VRF 1 & 2
580 server_addr1 = self.pg1.remote_ip4
581 src_addr1 = self.pg1.local_ip4
582 Proxy1 = VppDHCPProxy(
588 Proxy1.add_vpp_config()
590 server_addr2 = self.pg2.remote_ip4
591 src_addr2 = self.pg2.local_ip4
592 Proxy2 = VppDHCPProxy(
598 Proxy2.add_vpp_config()
601 # Confim DHCP requests ok in VRF 1 & 2.
602 # - dropped on IP config on client interface
604 self.send_and_assert_no_replies(self.pg4, pkts_disc_vrf1,
605 "DHCP config removed VRF 1")
606 self.send_and_assert_no_replies(self.pg5, pkts_disc_vrf2,
607 "DHCP config removed VRF 2")
610 # configure an IP address on the client facing interface
612 self.pg4.config_ip4()
613 self.pg4.add_stream(pkts_disc_vrf1)
614 self.pg_enable_capture(self.pg_interfaces)
616 rx = self.pg1.get_capture(1)
618 self.verify_relayed_dhcp_discover(rx, self.pg1, src_intf=self.pg4)
620 self.pg5.config_ip4()
621 self.pg5.add_stream(pkts_disc_vrf2)
622 self.pg_enable_capture(self.pg_interfaces)
624 rx = self.pg2.get_capture(1)
626 self.verify_relayed_dhcp_discover(rx, self.pg2, src_intf=self.pg5)
630 # table=1, vss_type=1, vpn_index=1, oui=4
631 # table=2, vss_type=0, vpn_id = "ip4-table-2"
632 self.vapi.dhcp_proxy_set_vss(tbl_id=1, vss_type=1,
633 vpn_index=1, oui=4, is_add=1)
634 self.vapi.dhcp_proxy_set_vss(tbl_id=2, vss_type=0,
635 vpn_ascii_id="ip4-table-2", is_add=1)
637 self.pg4.add_stream(pkts_disc_vrf1)
638 self.pg_enable_capture(self.pg_interfaces)
641 rx = self.pg1.get_capture(1)
643 self.verify_relayed_dhcp_discover(rx, self.pg1,
647 self.pg5.add_stream(pkts_disc_vrf2)
648 self.pg_enable_capture(self.pg_interfaces)
651 rx = self.pg2.get_capture(1)
653 self.verify_relayed_dhcp_discover(rx, self.pg2,
655 vpn_id="ip4-table-2")
658 # Add a second DHCP server in VRF 1
659 # expect clients messages to be relay to both configured servers
661 self.pg1.generate_remote_hosts(2)
662 server_addr12 = self.pg1.remote_hosts[1].ip4
664 Proxy12 = VppDHCPProxy(
670 Proxy12.add_vpp_config()
673 # We'll need an ARP entry for the server to send it packets
675 arp_entry = VppNeighbor(self,
676 self.pg1.sw_if_index,
677 self.pg1.remote_hosts[1].mac,
678 self.pg1.remote_hosts[1].ip4)
679 arp_entry.add_vpp_config()
682 # Send a discover from the client. expect two relayed messages
683 # The frist packet is sent to the second server
684 # We're not enforcing that here, it's just the way it is.
686 self.pg4.add_stream(pkts_disc_vrf1)
687 self.pg_enable_capture(self.pg_interfaces)
690 rx = self.pg1.get_capture(2)
692 option_82 = self.verify_relayed_dhcp_discover(
695 dst_mac=self.pg1.remote_hosts[1].mac,
696 dst_ip=self.pg1.remote_hosts[1].ip4,
698 self.verify_relayed_dhcp_discover(rx[1], self.pg1,
703 # Send both packets back. Client gets both.
705 p1 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
706 IP(src=self.pg1.remote_ip4, dst=self.pg1.local_ip4) /
707 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
709 DHCP(options=[('message-type', 'offer'),
710 ('relay_agent_Information', option_82),
712 p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
713 IP(src=self.pg1.remote_hosts[1].ip4, dst=self.pg1.local_ip4) /
714 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
716 DHCP(options=[('message-type', 'offer'),
717 ('relay_agent_Information', option_82),
721 self.pg1.add_stream(pkts)
722 self.pg_enable_capture(self.pg_interfaces)
725 rx = self.pg4.get_capture(2)
727 self.verify_dhcp_offer(rx[0], self.pg4, fib_id=1, oui=4)
728 self.verify_dhcp_offer(rx[1], self.pg4, fib_id=1, oui=4)
731 # Ensure offers from non-servers are dropeed
733 p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
734 IP(src="8.8.8.8", dst=self.pg1.local_ip4) /
735 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
737 DHCP(options=[('message-type', 'offer'),
738 ('relay_agent_Information', option_82),
740 self.send_and_assert_no_replies(self.pg1, p2,
741 "DHCP offer from non-server")
744 # Ensure only the discover is sent to multiple servers
746 p_req_vrf1 = (Ether(dst="ff:ff:ff:ff:ff:ff",
747 src=self.pg4.remote_mac) /
748 IP(src="0.0.0.0", dst="255.255.255.255") /
749 UDP(sport=DHCP4_CLIENT_PORT,
750 dport=DHCP4_SERVER_PORT) /
752 DHCP(options=[('message-type', 'request'),
755 self.pg4.add_stream(p_req_vrf1)
756 self.pg_enable_capture(self.pg_interfaces)
759 rx = self.pg1.get_capture(1)
762 # Remove the second DHCP server
764 Proxy12.remove_vpp_config()
767 # Test we can still relay with the first
769 self.pg4.add_stream(pkts_disc_vrf1)
770 self.pg_enable_capture(self.pg_interfaces)
773 rx = self.pg1.get_capture(1)
775 self.verify_relayed_dhcp_discover(rx, self.pg1,
780 # Remove the VSS config
781 # relayed DHCP has default vlaues in the option.
783 self.vapi.dhcp_proxy_set_vss(tbl_id=1, is_add=0)
784 self.vapi.dhcp_proxy_set_vss(tbl_id=2, is_add=0)
786 self.pg4.add_stream(pkts_disc_vrf1)
787 self.pg_enable_capture(self.pg_interfaces)
790 rx = self.pg1.get_capture(1)
792 self.verify_relayed_dhcp_discover(rx, self.pg1, src_intf=self.pg4)
795 # remove DHCP config to cleanup
797 Proxy1.remove_vpp_config()
798 Proxy2.remove_vpp_config()
800 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf0,
801 "DHCP cleanup VRF 0")
802 self.send_and_assert_no_replies(self.pg4, pkts_disc_vrf1,
803 "DHCP cleanup VRF 1")
804 self.send_and_assert_no_replies(self.pg5, pkts_disc_vrf2,
805 "DHCP cleanup VRF 2")
807 self.pg3.unconfig_ip4()
808 self.pg4.unconfig_ip4()
809 self.pg5.unconfig_ip4()
811 def test_dhcp6_proxy(self):
814 # Verify no response to DHCP request without DHCP config
816 dhcp_solicit_dst = "ff02::1:2"
817 dhcp_solicit_src_vrf0 = mk_ll_addr(self.pg3.remote_mac)
818 dhcp_solicit_src_vrf1 = mk_ll_addr(self.pg4.remote_mac)
819 dhcp_solicit_src_vrf2 = mk_ll_addr(self.pg5.remote_mac)
820 server_addr_vrf0 = self.pg0.remote_ip6
821 src_addr_vrf0 = self.pg0.local_ip6
822 server_addr_vrf1 = self.pg1.remote_ip6
823 src_addr_vrf1 = self.pg1.local_ip6
824 server_addr_vrf2 = self.pg2.remote_ip6
825 src_addr_vrf2 = self.pg2.local_ip6
827 dmac = in6_getnsmac(inet_pton(socket.AF_INET6, dhcp_solicit_dst))
828 p_solicit_vrf0 = (Ether(dst=dmac, src=self.pg3.remote_mac) /
829 IPv6(src=dhcp_solicit_src_vrf0,
830 dst=dhcp_solicit_dst) /
831 UDP(sport=DHCP6_SERVER_PORT,
832 dport=DHCP6_CLIENT_PORT) /
834 p_solicit_vrf1 = (Ether(dst=dmac, src=self.pg4.remote_mac) /
835 IPv6(src=dhcp_solicit_src_vrf1,
836 dst=dhcp_solicit_dst) /
837 UDP(sport=DHCP6_SERVER_PORT,
838 dport=DHCP6_CLIENT_PORT) /
840 p_solicit_vrf2 = (Ether(dst=dmac, src=self.pg5.remote_mac) /
841 IPv6(src=dhcp_solicit_src_vrf2,
842 dst=dhcp_solicit_dst) /
843 UDP(sport=DHCP6_SERVER_PORT,
844 dport=DHCP6_CLIENT_PORT) /
847 self.send_and_assert_no_replies(self.pg3, p_solicit_vrf0,
848 "DHCP with no configuration")
849 self.send_and_assert_no_replies(self.pg4, p_solicit_vrf1,
850 "DHCP with no configuration")
851 self.send_and_assert_no_replies(self.pg5, p_solicit_vrf2,
852 "DHCP with no configuration")
855 # DHCPv6 config in VRF 0.
856 # Packets still dropped because the client facing interface has no
859 Proxy = VppDHCPProxy(
865 Proxy.add_vpp_config()
867 self.send_and_assert_no_replies(self.pg3, p_solicit_vrf0,
868 "DHCP with no configuration")
869 self.send_and_assert_no_replies(self.pg4, p_solicit_vrf1,
870 "DHCP with no configuration")
873 # configure an IP address on the client facing interface
875 self.pg3.config_ip6()
878 # Now the DHCP requests are relayed to the server
880 self.pg3.add_stream(p_solicit_vrf0)
881 self.pg_enable_capture(self.pg_interfaces)
884 rx = self.pg0.get_capture(1)
886 self.verify_dhcp6_solicit(rx[0], self.pg0,
887 dhcp_solicit_src_vrf0,
891 # Exception cases for rejected relay responses
894 # 1 - not a relay reply
895 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
896 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
897 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
899 self.send_and_assert_no_replies(self.pg3, p_adv_vrf0,
900 "DHCP6 not a relay reply")
902 # 2 - no relay message option
903 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
904 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
905 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
908 self.send_and_assert_no_replies(self.pg3, p_adv_vrf0,
909 "DHCP not a relay message")
912 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
913 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
914 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
916 DHCP6OptRelayMsg(optlen=0) /
918 self.send_and_assert_no_replies(self.pg3, p_adv_vrf0,
919 "DHCP6 no circuit ID")
920 # 4 - wrong circuit ID
921 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
922 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
923 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
925 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x05') /
926 DHCP6OptRelayMsg(optlen=0) /
928 self.send_and_assert_no_replies(self.pg3, p_adv_vrf0,
929 "DHCP6 wrong circuit ID")
932 # Send the relay response (the advertisement)
934 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
935 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
936 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
938 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x04') /
939 DHCP6OptRelayMsg(optlen=0) /
940 DHCP6_Advertise(trid=1) /
941 DHCP6OptStatusCode(statuscode=0))
942 pkts_adv_vrf0 = [p_adv_vrf0]
944 self.pg0.add_stream(pkts_adv_vrf0)
945 self.pg_enable_capture(self.pg_interfaces)
948 rx = self.pg3.get_capture(1)
950 self.verify_dhcp6_advert(rx[0], self.pg3, "::")
953 # Send the relay response (the advertisement)
954 # - with peer address
955 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
956 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
957 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
958 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf0) /
959 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x04') /
960 DHCP6OptRelayMsg(optlen=0) /
961 DHCP6_Advertise(trid=1) /
962 DHCP6OptStatusCode(statuscode=0))
963 pkts_adv_vrf0 = [p_adv_vrf0]
965 self.pg0.add_stream(pkts_adv_vrf0)
966 self.pg_enable_capture(self.pg_interfaces)
969 rx = self.pg3.get_capture(1)
971 self.verify_dhcp6_advert(rx[0], self.pg3, dhcp_solicit_src_vrf0)
974 # Add all the config for VRF 1 & 2
976 Proxy1 = VppDHCPProxy(
982 Proxy1.add_vpp_config()
983 self.pg4.config_ip6()
985 Proxy2 = VppDHCPProxy(
991 Proxy2.add_vpp_config()
992 self.pg5.config_ip6()
997 self.pg4.add_stream(p_solicit_vrf1)
998 self.pg_enable_capture(self.pg_interfaces)
1001 rx = self.pg1.get_capture(1)
1003 self.verify_dhcp6_solicit(rx[0], self.pg1,
1004 dhcp_solicit_src_vrf1,
1005 self.pg4.remote_mac)
1010 self.pg5.add_stream(p_solicit_vrf2)
1011 self.pg_enable_capture(self.pg_interfaces)
1014 rx = self.pg2.get_capture(1)
1016 self.verify_dhcp6_solicit(rx[0], self.pg2,
1017 dhcp_solicit_src_vrf2,
1018 self.pg5.remote_mac)
1023 p_adv_vrf1 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
1024 IPv6(dst=self.pg1.local_ip6, src=self.pg1.remote_ip6) /
1025 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
1026 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) /
1027 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x05') /
1028 DHCP6OptRelayMsg(optlen=0) /
1029 DHCP6_Advertise(trid=1) /
1030 DHCP6OptStatusCode(statuscode=0))
1031 pkts_adv_vrf1 = [p_adv_vrf1]
1033 self.pg1.add_stream(pkts_adv_vrf1)
1034 self.pg_enable_capture(self.pg_interfaces)
1037 rx = self.pg4.get_capture(1)
1039 self.verify_dhcp6_advert(rx[0], self.pg4, dhcp_solicit_src_vrf1)
1044 self.vapi.dhcp_proxy_set_vss(
1045 tbl_id=1, vss_type=1, oui=4, vpn_index=1, is_ipv6=1, is_add=1)
1046 self.vapi.dhcp_proxy_set_vss(
1049 vpn_ascii_id="IPv6-table-2",
1053 self.pg4.add_stream(p_solicit_vrf1)
1054 self.pg_enable_capture(self.pg_interfaces)
1057 rx = self.pg1.get_capture(1)
1059 self.verify_dhcp6_solicit(rx[0], self.pg1,
1060 dhcp_solicit_src_vrf1,
1061 self.pg4.remote_mac,
1065 self.pg5.add_stream(p_solicit_vrf2)
1066 self.pg_enable_capture(self.pg_interfaces)
1069 rx = self.pg2.get_capture(1)
1071 self.verify_dhcp6_solicit(rx[0], self.pg2,
1072 dhcp_solicit_src_vrf2,
1073 self.pg5.remote_mac,
1074 vpn_id="IPv6-table-2")
1077 # Remove the VSS config
1078 # relayed DHCP has default vlaues in the option.
1080 self.vapi.dhcp_proxy_set_vss(tbl_id=1, is_ipv6=1, is_add=0)
1082 self.pg4.add_stream(p_solicit_vrf1)
1083 self.pg_enable_capture(self.pg_interfaces)
1086 rx = self.pg1.get_capture(1)
1088 self.verify_dhcp6_solicit(rx[0], self.pg1,
1089 dhcp_solicit_src_vrf1,
1090 self.pg4.remote_mac)
1093 # Add a second DHCP server in VRF 1
1094 # expect clients messages to be relay to both configured servers
1096 self.pg1.generate_remote_hosts(2)
1097 server_addr12 = self.pg1.remote_hosts[1].ip6
1099 Proxy12 = VppDHCPProxy(
1105 Proxy12.add_vpp_config()
1108 # We'll need an ND entry for the server to send it packets
1110 nd_entry = VppNeighbor(self,
1111 self.pg1.sw_if_index,
1112 self.pg1.remote_hosts[1].mac,
1113 self.pg1.remote_hosts[1].ip6)
1114 nd_entry.add_vpp_config()
1117 # Send a discover from the client. expect two relayed messages
1118 # The frist packet is sent to the second server
1119 # We're not enforcing that here, it's just the way it is.
1121 self.pg4.add_stream(p_solicit_vrf1)
1122 self.pg_enable_capture(self.pg_interfaces)
1125 rx = self.pg1.get_capture(2)
1127 self.verify_dhcp6_solicit(rx[0], self.pg1,
1128 dhcp_solicit_src_vrf1,
1129 self.pg4.remote_mac)
1130 self.verify_dhcp6_solicit(rx[1], self.pg1,
1131 dhcp_solicit_src_vrf1,
1132 self.pg4.remote_mac,
1133 dst_mac=self.pg1.remote_hosts[1].mac,
1134 dst_ip=self.pg1.remote_hosts[1].ip6)
1137 # Send both packets back. Client gets both.
1139 p1 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
1140 IPv6(dst=self.pg1.local_ip6, src=self.pg1.remote_ip6) /
1141 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
1142 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) /
1143 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x05') /
1144 DHCP6OptRelayMsg(optlen=0) /
1145 DHCP6_Advertise(trid=1) /
1146 DHCP6OptStatusCode(statuscode=0))
1147 p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_hosts[1].mac) /
1148 IPv6(dst=self.pg1.local_ip6, src=self.pg1._remote_hosts[1].ip6) /
1149 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
1150 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) /
1151 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x05') /
1152 DHCP6OptRelayMsg(optlen=0) /
1153 DHCP6_Advertise(trid=1) /
1154 DHCP6OptStatusCode(statuscode=0))
1158 self.pg1.add_stream(pkts)
1159 self.pg_enable_capture(self.pg_interfaces)
1162 rx = self.pg4.get_capture(2)
1164 self.verify_dhcp6_advert(rx[0], self.pg4, dhcp_solicit_src_vrf1)
1165 self.verify_dhcp6_advert(rx[1], self.pg4, dhcp_solicit_src_vrf1)
1168 # Ensure only solicit messages are duplicated
1170 p_request_vrf1 = (Ether(dst=dmac, src=self.pg4.remote_mac) /
1171 IPv6(src=dhcp_solicit_src_vrf1,
1172 dst=dhcp_solicit_dst) /
1173 UDP(sport=DHCP6_SERVER_PORT,
1174 dport=DHCP6_CLIENT_PORT) /
1177 self.pg4.add_stream(p_request_vrf1)
1178 self.pg_enable_capture(self.pg_interfaces)
1181 rx = self.pg1.get_capture(1)
1184 # Test we drop DHCP packets from addresses that are not configured as
1187 p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_hosts[1].mac) /
1188 IPv6(dst=self.pg1.local_ip6, src="3001::1") /
1189 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
1190 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) /
1191 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x05') /
1192 DHCP6OptRelayMsg(optlen=0) /
1193 DHCP6_Advertise(trid=1) /
1194 DHCP6OptStatusCode(statuscode=0))
1195 self.send_and_assert_no_replies(self.pg1, p2,
1196 "DHCP6 not from server")
1199 # Remove the second DHCP server
1201 Proxy12.remove_vpp_config()
1204 # Test we can still relay with the first
1206 self.pg4.add_stream(p_solicit_vrf1)
1207 self.pg_enable_capture(self.pg_interfaces)
1210 rx = self.pg1.get_capture(1)
1212 self.verify_dhcp6_solicit(rx[0], self.pg1,
1213 dhcp_solicit_src_vrf1,
1214 self.pg4.remote_mac)
1219 Proxy.remove_vpp_config()
1220 Proxy1.remove_vpp_config()
1221 Proxy2.remove_vpp_config()
1223 self.pg3.unconfig_ip6()
1224 self.pg4.unconfig_ip6()
1225 self.pg5.unconfig_ip6()
1227 def test_dhcp_client(self):
1230 vdscp = VppEnum.vl_api_ip_dscp_t
1231 hostname = 'universal-dp'
1233 self.pg_enable_capture(self.pg_interfaces)
1236 # Configure DHCP client on PG3 and capture the discover sent
1238 Client = VppDHCPClient(self, self.pg3.sw_if_index, hostname)
1239 Client.add_vpp_config()
1240 self.assertTrue(Client.query_vpp_config())
1242 rx = self.pg3.get_capture(1)
1244 self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname)
1247 # Send back on offer, expect the request
1249 p_offer = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1250 IP(src=self.pg3.remote_ip4, dst="255.255.255.255") /
1251 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1253 yiaddr=self.pg3.local_ip4,
1254 chaddr=mac_pton(self.pg3.local_mac)) /
1255 DHCP(options=[('message-type', 'offer'),
1256 ('server_id', self.pg3.remote_ip4),
1259 self.pg3.add_stream(p_offer)
1260 self.pg_enable_capture(self.pg_interfaces)
1263 rx = self.pg3.get_capture(1)
1264 self.verify_orig_dhcp_request(rx[0], self.pg3, hostname,
1268 # Send an acknowledgment
1270 p_ack = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1271 IP(src=self.pg3.remote_ip4, dst="255.255.255.255") /
1272 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1273 BOOTP(op=1, yiaddr=self.pg3.local_ip4,
1274 chaddr=mac_pton(self.pg3.local_mac)) /
1275 DHCP(options=[('message-type', 'ack'),
1276 ('subnet_mask', "255.255.255.0"),
1277 ('router', self.pg3.remote_ip4),
1278 ('server_id', self.pg3.remote_ip4),
1279 ('lease_time', 43200),
1282 self.pg3.add_stream(p_ack)
1283 self.pg_enable_capture(self.pg_interfaces)
1287 # We'll get an ARP request for the router address
1289 rx = self.pg3.get_capture(1)
1291 self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
1292 self.pg_enable_capture(self.pg_interfaces)
1295 # At the end of this procedure there should be a connected route
1298 self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
1299 self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
1301 # remove the left over ARP entry
1302 self.vapi.ip_neighbor_add_del(self.pg3.sw_if_index,
1303 self.pg3.remote_mac,
1304 self.pg3.remote_ip4,
1308 # remove the DHCP config
1310 Client.remove_vpp_config()
1313 # and now the route should be gone
1315 self.assertFalse(find_route(self, self.pg3.local_ip4, 32))
1316 self.assertFalse(find_route(self, self.pg3.local_ip4, 24))
1319 # Start the procedure again. this time have VPP send the client-ID
1320 # and set the DSCP value
1322 self.pg3.admin_down()
1325 Client.set_client(self.pg3.sw_if_index, hostname,
1326 id=self.pg3.local_mac,
1327 dscp=vdscp.IP_API_DSCP_EF)
1328 Client.add_vpp_config()
1330 rx = self.pg3.get_capture(1)
1332 self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname,
1334 dscp=vdscp.IP_API_DSCP_EF)
1336 # TODO: VPP DHCP client should not accept DHCP OFFER message with
1337 # the XID (Transaction ID) not matching the XID of the most recent
1338 # DHCP DISCOVERY message.
1339 # Such DHCP OFFER message must be silently discarded - RFC2131.
1340 # Reported in Jira ticket: VPP-99
1341 self.pg3.add_stream(p_offer)
1342 self.pg_enable_capture(self.pg_interfaces)
1345 rx = self.pg3.get_capture(1)
1346 self.verify_orig_dhcp_request(rx[0], self.pg3, hostname,
1348 dscp=vdscp.IP_API_DSCP_EF)
1351 # unicast the ack to the offered address
1353 p_ack = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1354 IP(src=self.pg3.remote_ip4, dst=self.pg3.local_ip4) /
1355 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1356 BOOTP(op=1, yiaddr=self.pg3.local_ip4,
1357 chaddr=mac_pton(self.pg3.local_mac)) /
1358 DHCP(options=[('message-type', 'ack'),
1359 ('subnet_mask', "255.255.255.0"),
1360 ('router', self.pg3.remote_ip4),
1361 ('server_id', self.pg3.remote_ip4),
1362 ('lease_time', 43200),
1365 self.pg3.add_stream(p_ack)
1366 self.pg_enable_capture(self.pg_interfaces)
1370 # We'll get an ARP request for the router address
1372 rx = self.pg3.get_capture(1)
1374 self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
1375 self.pg_enable_capture(self.pg_interfaces)
1378 # At the end of this procedure there should be a connected route
1381 self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
1382 self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
1385 # remove the DHCP config
1387 Client.remove_vpp_config()
1389 self.assertFalse(find_route(self, self.pg3.local_ip4, 32))
1390 self.assertFalse(find_route(self, self.pg3.local_ip4, 24))
1393 # Rince and repeat, this time with VPP configured not to set
1394 # the braodcast flag in the discover and request messages,
1395 # and for the server to unicast the responses.
1397 # Configure DHCP client on PG3 and capture the discover sent
1400 self.pg3.sw_if_index,
1402 set_broadcast_flag=False)
1403 Client.add_vpp_config()
1405 rx = self.pg3.get_capture(1)
1407 self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname,
1411 # Send back on offer, unicasted to the offered address.
1412 # Expect the request.
1414 p_offer = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1415 IP(src=self.pg3.remote_ip4, dst=self.pg3.local_ip4) /
1416 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1417 BOOTP(op=1, yiaddr=self.pg3.local_ip4,
1418 chaddr=mac_pton(self.pg3.local_mac)) /
1419 DHCP(options=[('message-type', 'offer'),
1420 ('server_id', self.pg3.remote_ip4),
1423 self.pg3.add_stream(p_offer)
1424 self.pg_enable_capture(self.pg_interfaces)
1427 rx = self.pg3.get_capture(1)
1428 self.verify_orig_dhcp_request(rx[0], self.pg3, hostname,
1433 # Send an acknowledgment, the lease renewal time is 2 seconds
1434 # so we should expect the renew straight after
1436 p_ack = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1437 IP(src=self.pg3.remote_ip4, dst=self.pg3.local_ip4) /
1438 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1439 BOOTP(op=1, yiaddr=self.pg3.local_ip4,
1440 chaddr=mac_pton(self.pg3.local_mac)) /
1441 DHCP(options=[('message-type', 'ack'),
1442 ('subnet_mask', "255.255.255.0"),
1443 ('router', self.pg3.remote_ip4),
1444 ('server_id', self.pg3.remote_ip4),
1445 ('lease_time', 43200),
1446 ('renewal_time', 2),
1449 self.pg3.add_stream(p_ack)
1450 self.pg_enable_capture(self.pg_interfaces)
1454 # We'll get an ARP request for the router address
1456 rx = self.pg3.get_capture(1)
1458 self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
1459 self.pg_enable_capture(self.pg_interfaces)
1462 # At the end of this procedure there should be a connected route
1465 self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
1466 self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
1469 # read the DHCP client details from a dump
1471 clients = self.vapi.dhcp_client_dump()
1473 self.assertEqual(clients[0].client.sw_if_index,
1474 self.pg3.sw_if_index)
1475 self.assertEqual(clients[0].lease.sw_if_index,
1476 self.pg3.sw_if_index)
1477 self.assertEqual(clients[0].client.hostname, hostname)
1478 self.assertEqual(clients[0].lease.hostname, hostname)
1479 # 0 = DISCOVER, 1 = REQUEST, 2 = BOUND
1480 self.assertEqual(clients[0].lease.state, 2)
1481 self.assertEqual(clients[0].lease.mask_width, 24)
1482 self.assertEqual(str(clients[0].lease.router_address),
1483 self.pg3.remote_ip4)
1484 self.assertEqual(str(clients[0].lease.host_address),
1488 # wait for the unicasted renewal
1489 # the first attempt will be an ARP packet, since we have not yet
1490 # responded to VPP's request
1492 self.logger.info(self.vapi.cli("sh dhcp client intfc pg3 verbose"))
1493 rx = self.pg3.get_capture(1, timeout=10)
1495 self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
1497 # respond to the arp
1498 p_arp = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1500 hwdst=self.pg3.local_mac,
1501 hwsrc=self.pg3.remote_mac,
1502 pdst=self.pg3.local_ip4,
1503 psrc=self.pg3.remote_ip4))
1504 self.pg3.add_stream(p_arp)
1505 self.pg_enable_capture(self.pg_interfaces)
1508 # the next packet is the unicasted renewal
1509 rx = self.pg3.get_capture(1, timeout=10)
1510 self.verify_orig_dhcp_request(rx[0], self.pg3, hostname,
1515 # send an ACK with different data from the original offer *
1516 self.pg3.generate_remote_hosts(4)
1517 p_ack = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1518 IP(src=self.pg3.remote_ip4, dst=self.pg3.local_ip4) /
1519 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1520 BOOTP(op=1, yiaddr=self.pg3.remote_hosts[3].ip4,
1521 chaddr=mac_pton(self.pg3.local_mac)) /
1522 DHCP(options=[('message-type', 'ack'),
1523 ('subnet_mask', "255.255.255.0"),
1524 ('router', self.pg3.remote_hosts[1].ip4),
1525 ('server_id', self.pg3.remote_hosts[2].ip4),
1526 ('lease_time', 43200),
1527 ('renewal_time', 2),
1530 self.pg3.add_stream(p_ack)
1531 self.pg_enable_capture(self.pg_interfaces)
1535 # read the DHCP client details from a dump
1537 clients = self.vapi.dhcp_client_dump()
1539 self.assertEqual(clients[0].client.sw_if_index,
1540 self.pg3.sw_if_index)
1541 self.assertEqual(clients[0].lease.sw_if_index,
1542 self.pg3.sw_if_index)
1543 self.assertEqual(clients[0].client.hostname, hostname)
1544 self.assertEqual(clients[0].lease.hostname, hostname)
1545 # 0 = DISCOVER, 1 = REQUEST, 2 = BOUND
1546 self.assertEqual(clients[0].lease.state, 2)
1547 self.assertEqual(clients[0].lease.mask_width, 24)
1548 self.assertEqual(str(clients[0].lease.router_address),
1549 self.pg3.remote_hosts[1].ip4)
1550 self.assertEqual(str(clients[0].lease.host_address),
1551 self.pg3.remote_hosts[3].ip4)
1554 # remove the DHCP config
1556 Client.remove_vpp_config()
1559 # and now the route should be gone
1561 self.assertFalse(find_route(self, self.pg3.local_ip4, 32))
1562 self.assertFalse(find_route(self, self.pg3.local_ip4, 24))
1565 # Start the procedure again. Use requested lease time option.
1566 # this time wait for the lease to expire and the client to
1570 self.pg3.admin_down()
1573 self.pg_enable_capture(self.pg_interfaces)
1574 Client.set_client(self.pg3.sw_if_index, hostname)
1575 Client.add_vpp_config()
1577 rx = self.pg3.get_capture(1)
1579 self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname)
1582 # Send back on offer with requested lease time, expect the request
1585 p_offer = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1586 IP(src=self.pg3.remote_ip4, dst='255.255.255.255') /
1587 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1589 yiaddr=self.pg3.local_ip4,
1590 chaddr=mac_pton(self.pg3.local_mac)) /
1591 DHCP(options=[('message-type', 'offer'),
1592 ('server_id', self.pg3.remote_ip4),
1593 ('lease_time', lease_time),
1596 self.pg3.add_stream(p_offer)
1597 self.pg_enable_capture(self.pg_interfaces)
1600 rx = self.pg3.get_capture(1)
1601 self.verify_orig_dhcp_request(rx[0], self.pg3, hostname,
1605 # Send an acknowledgment
1607 p_ack = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1608 IP(src=self.pg3.remote_ip4, dst='255.255.255.255') /
1609 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1610 BOOTP(op=1, yiaddr=self.pg3.local_ip4,
1611 chaddr=mac_pton(self.pg3.local_mac)) /
1612 DHCP(options=[('message-type', 'ack'),
1613 ('subnet_mask', '255.255.255.0'),
1614 ('router', self.pg3.remote_ip4),
1615 ('server_id', self.pg3.remote_ip4),
1616 ('lease_time', lease_time),
1619 self.pg3.add_stream(p_ack)
1620 self.pg_enable_capture(self.pg_interfaces)
1624 # We'll get an ARP request for the router address
1626 rx = self.pg3.get_capture(1)
1628 self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
1631 # At the end of this procedure there should be a connected route
1634 self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
1635 self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
1638 # the route should be gone after the lease expires
1640 self.assertTrue(self.wait_for_no_route(self.pg3.local_ip4, 32))
1641 self.assertTrue(self.wait_for_no_route(self.pg3.local_ip4, 24))
1644 # remove the DHCP config
1646 Client.remove_vpp_config()
1648 def test_dhcp_client_vlan(self):
1649 """ DHCP Client w/ VLAN"""
1651 vdscp = VppEnum.vl_api_ip_dscp_t
1652 vqos = VppEnum.vl_api_qos_source_t
1653 hostname = 'universal-dp'
1655 self.pg_enable_capture(self.pg_interfaces)
1657 vlan_100 = VppDot1QSubint(self, self.pg3, 100)
1660 output = [scapy.compat.chb(4)] * 256
1661 os = b''.join(output)
1662 rows = [{'outputs': os},
1667 qem1 = VppQosEgressMap(self, 1, rows).add_vpp_config()
1668 qm1 = VppQosMark(self, vlan_100, qem1,
1669 vqos.QOS_API_SOURCE_VLAN).add_vpp_config()
1672 # Configure DHCP client on PG3 and capture the discover sent
1674 Client = VppDHCPClient(
1676 vlan_100.sw_if_index,
1678 dscp=vdscp.IP_API_DSCP_EF)
1679 Client.add_vpp_config()
1681 rx = self.pg3.get_capture(1)
1683 self.assertEqual(rx[0][Dot1Q].vlan, 100)
1684 self.assertEqual(rx[0][Dot1Q].prio, 2)
1686 self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname,
1687 dscp=vdscp.IP_API_DSCP_EF)
1690 if __name__ == '__main__':
1691 unittest.main(testRunner=VppTestRunner)