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 self.verify_dhcp_has_option(pkt, "client_id",
255 client_id.encode('ascii'))
257 self.assertEqual(bootp.ciaddr, "0.0.0.0")
258 self.assertEqual(bootp.giaddr, "0.0.0.0")
260 self.assertEqual(bootp.flags, 0x8000)
262 self.assertEqual(bootp.flags, 0x0000)
264 def verify_orig_dhcp_request(self, pkt, intf, hostname, ip,
268 self.verify_orig_dhcp_pkt(pkt, intf, dscp, l2_bc=l2_bc)
270 self.verify_dhcp_msg_type(pkt, "request")
271 self.verify_dhcp_has_option(pkt, "hostname",
272 hostname.encode('ascii'))
273 self.verify_dhcp_has_option(pkt, "requested_addr", ip)
277 self.assertEqual(bootp.ciaddr, "0.0.0.0")
279 self.assertEqual(bootp.ciaddr, intf.local_ip4)
280 self.assertEqual(bootp.giaddr, "0.0.0.0")
283 self.assertEqual(bootp.flags, 0x8000)
285 self.assertEqual(bootp.flags, 0x0000)
287 def verify_relayed_dhcp_discover(self, pkt, intf, src_intf=None,
290 dst_mac=None, dst_ip=None):
292 dst_mac = intf.remote_mac
294 dst_ip = intf.remote_ip4
297 self.assertEqual(ether.dst, dst_mac)
298 self.assertEqual(ether.src, intf.local_mac)
301 self.assertEqual(ip.dst, dst_ip)
302 self.assertEqual(ip.src, intf.local_ip4)
305 self.assertEqual(udp.dport, DHCP4_SERVER_PORT)
306 self.assertEqual(udp.sport, DHCP4_CLIENT_PORT)
311 for o in dhcp.options:
312 if isinstance(o, tuple):
313 if o[0] == "message-type" \
314 and DHCPTypes[o[1]] == "discover":
316 self.assertTrue(is_discover)
318 data = self.validate_relay_options(pkt, src_intf,
324 def verify_dhcp6_solicit(self, pkt, intf,
332 dst_mac = intf.remote_mac
334 dst_ip = in6_ptop(intf.remote_ip6)
337 self.assertEqual(ether.dst, dst_mac)
338 self.assertEqual(ether.src, intf.local_mac)
341 self.assertEqual(in6_ptop(ip.dst), dst_ip)
342 self.assertEqual(in6_ptop(ip.src), in6_ptop(intf.local_ip6))
345 self.assertEqual(udp.dport, DHCP6_CLIENT_PORT)
346 self.assertEqual(udp.sport, DHCP6_SERVER_PORT)
348 relay = pkt[DHCP6_RelayForward]
349 self.assertEqual(in6_ptop(relay.peeraddr), in6_ptop(peer_ip))
350 oid = pkt[DHCP6OptIfaceId]
351 cll = pkt[DHCP6OptClientLinkLayerAddr]
352 self.assertEqual(cll.optlen, 8)
353 self.assertEqual(cll.lltype, 1)
354 self.assertEqual(cll.clladdr, peer_mac)
359 self.assertEqual(id_len, 0)
360 vss = pkt[DHCP6OptVSS]
361 self.assertEqual(vss.optlen, 8)
362 self.assertEqual(vss.type, 1)
363 # the OUI and FIB-id are really 3 and 4 bytes resp.
364 # but the tested range is small
365 self.assertEqual(six.byte2int(vss.data[0:1]), 0)
366 self.assertEqual(six.byte2int(vss.data[1:2]), 0)
367 self.assertEqual(six.byte2int(vss.data[2:3]), oui)
368 self.assertEqual(six.byte2int(vss.data[3:4]), 0)
369 self.assertEqual(six.byte2int(vss.data[4:5]), 0)
370 self.assertEqual(six.byte2int(vss.data[5:6]), 0)
371 self.assertEqual(six.byte2int(vss.data[6:7]), fib_id)
374 self.assertEqual(oui, 0)
375 vss = pkt[DHCP6OptVSS]
376 self.assertEqual(vss.optlen, id_len + 1)
377 self.assertEqual(vss.type, 0)
378 self.assertEqual(vss.data[0:id_len].decode('ascii'),
381 # the relay message should be an encoded Solicit
382 msg = pkt[DHCP6OptRelayMsg]
383 sol = DHCP6_Solicit()
384 self.assertEqual(msg.optlen, len(sol))
385 self.assertEqual(sol, msg[1])
387 def verify_dhcp6_advert(self, pkt, intf, peer):
389 self.assertEqual(ether.dst, "ff:ff:ff:ff:ff:ff")
390 self.assertEqual(ether.src, intf.local_mac)
393 self.assertEqual(in6_ptop(ip.dst), in6_ptop(peer))
394 self.assertEqual(in6_ptop(ip.src), in6_ptop(intf.local_ip6))
397 self.assertEqual(udp.dport, DHCP6_SERVER_PORT)
398 self.assertEqual(udp.sport, DHCP6_CLIENT_PORT)
400 # not sure why this is not decoding
401 # adv = pkt[DHCP6_Advertise]
403 def wait_for_no_route(self, address, length,
404 n_tries=50, s_time=1):
406 if not find_route(self, address, length):
408 n_tries = n_tries - 1
413 def test_dhcp_proxy(self):
417 # Verify no response to DHCP request without DHCP config
419 p_disc_vrf0 = (Ether(dst="ff:ff:ff:ff:ff:ff",
420 src=self.pg3.remote_mac) /
421 IP(src="0.0.0.0", dst="255.255.255.255") /
422 UDP(sport=DHCP4_CLIENT_PORT,
423 dport=DHCP4_SERVER_PORT) /
425 DHCP(options=[('message-type', 'discover'), ('end')]))
426 pkts_disc_vrf0 = [p_disc_vrf0]
427 p_disc_vrf1 = (Ether(dst="ff:ff:ff:ff:ff:ff",
428 src=self.pg4.remote_mac) /
429 IP(src="0.0.0.0", dst="255.255.255.255") /
430 UDP(sport=DHCP4_CLIENT_PORT,
431 dport=DHCP4_SERVER_PORT) /
433 DHCP(options=[('message-type', 'discover'), ('end')]))
434 pkts_disc_vrf1 = [p_disc_vrf1]
435 p_disc_vrf2 = (Ether(dst="ff:ff:ff:ff:ff:ff",
436 src=self.pg5.remote_mac) /
437 IP(src="0.0.0.0", dst="255.255.255.255") /
438 UDP(sport=DHCP4_CLIENT_PORT,
439 dport=DHCP4_SERVER_PORT) /
441 DHCP(options=[('message-type', 'discover'), ('end')]))
442 pkts_disc_vrf2 = [p_disc_vrf2]
444 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf0,
445 "DHCP with no configuration")
446 self.send_and_assert_no_replies(self.pg4, pkts_disc_vrf1,
447 "DHCP with no configuration")
448 self.send_and_assert_no_replies(self.pg5, pkts_disc_vrf2,
449 "DHCP with no configuration")
452 # Enable DHCP proxy in VRF 0
454 server_addr = self.pg0.remote_ip4
455 src_addr = self.pg0.local_ip4
457 Proxy = VppDHCPProxy(self, server_addr, src_addr, rx_vrf_id=0)
458 Proxy.add_vpp_config()
461 # Discover packets from the client are dropped because there is no
462 # IP address configured on the client facing interface
464 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf0,
465 "Discover DHCP no relay address")
468 # Inject a response from the server
469 # dropped, because there is no IP addrees on the
470 # client interfce to fill in the option.
472 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
473 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
474 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
476 DHCP(options=[('message-type', 'offer'), ('end')]))
479 self.send_and_assert_no_replies(self.pg3, pkts,
480 "Offer DHCP no relay address")
483 # configure an IP address on the client facing interface
485 self.pg3.config_ip4()
488 # Try again with a discover packet
489 # Rx'd packet should be to the server address and from the configured
491 # UDP source ports are unchanged
492 # we've no option 82 config so that should be absent
494 self.pg3.add_stream(pkts_disc_vrf0)
495 self.pg_enable_capture(self.pg_interfaces)
498 rx = self.pg0.get_capture(1)
501 option_82 = self.verify_relayed_dhcp_discover(rx, self.pg0,
505 # Create an DHCP offer reply from the server with a correctly formatted
506 # option 82. i.e. send back what we just captured
507 # The offer, sent mcast to the client, still has option 82.
509 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
510 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
511 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
513 DHCP(options=[('message-type', 'offer'),
514 ('relay_agent_Information', option_82),
518 self.pg0.add_stream(pkts)
519 self.pg_enable_capture(self.pg_interfaces)
522 rx = self.pg3.get_capture(1)
525 self.verify_dhcp_offer(rx, self.pg3)
530 # 1. not our IP address = not checked by VPP? so offer is replayed
532 bad_ip = option_82[0:8] + scapy.compat.chb(33) + option_82[9:]
534 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
535 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
536 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
538 DHCP(options=[('message-type', 'offer'),
539 ('relay_agent_Information', bad_ip),
542 self.send_and_assert_no_replies(self.pg0, pkts,
543 "DHCP offer option 82 bad address")
545 # 2. Not a sw_if_index VPP knows
546 bad_if_index = option_82[0:2] + scapy.compat.chb(33) + option_82[3:]
548 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
549 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
550 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
552 DHCP(options=[('message-type', 'offer'),
553 ('relay_agent_Information', bad_if_index),
556 self.send_and_assert_no_replies(self.pg0, pkts,
557 "DHCP offer option 82 bad if index")
560 # Send a DHCP request in VRF 1. should be dropped.
562 self.send_and_assert_no_replies(self.pg4, pkts_disc_vrf1,
563 "DHCP with no configuration VRF 1")
566 # Delete the DHCP config in VRF 0
567 # Should now drop requests.
569 Proxy.remove_vpp_config()
571 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf0,
572 "DHCP config removed VRF 0")
573 self.send_and_assert_no_replies(self.pg4, pkts_disc_vrf1,
574 "DHCP config removed VRF 1")
577 # Add DHCP config for VRF 1 & 2
579 server_addr1 = self.pg1.remote_ip4
580 src_addr1 = self.pg1.local_ip4
581 Proxy1 = VppDHCPProxy(
587 Proxy1.add_vpp_config()
589 server_addr2 = self.pg2.remote_ip4
590 src_addr2 = self.pg2.local_ip4
591 Proxy2 = VppDHCPProxy(
597 Proxy2.add_vpp_config()
600 # Confim DHCP requests ok in VRF 1 & 2.
601 # - dropped on IP config on client interface
603 self.send_and_assert_no_replies(self.pg4, pkts_disc_vrf1,
604 "DHCP config removed VRF 1")
605 self.send_and_assert_no_replies(self.pg5, pkts_disc_vrf2,
606 "DHCP config removed VRF 2")
609 # configure an IP address on the client facing interface
611 self.pg4.config_ip4()
612 self.pg4.add_stream(pkts_disc_vrf1)
613 self.pg_enable_capture(self.pg_interfaces)
615 rx = self.pg1.get_capture(1)
617 self.verify_relayed_dhcp_discover(rx, self.pg1, src_intf=self.pg4)
619 self.pg5.config_ip4()
620 self.pg5.add_stream(pkts_disc_vrf2)
621 self.pg_enable_capture(self.pg_interfaces)
623 rx = self.pg2.get_capture(1)
625 self.verify_relayed_dhcp_discover(rx, self.pg2, src_intf=self.pg5)
629 # table=1, vss_type=1, vpn_index=1, oui=4
630 # table=2, vss_type=0, vpn_id = "ip4-table-2"
631 self.vapi.dhcp_proxy_set_vss(tbl_id=1, vss_type=1,
632 vpn_index=1, oui=4, is_add=1)
633 self.vapi.dhcp_proxy_set_vss(tbl_id=2, vss_type=0,
634 vpn_ascii_id="ip4-table-2", is_add=1)
636 self.pg4.add_stream(pkts_disc_vrf1)
637 self.pg_enable_capture(self.pg_interfaces)
640 rx = self.pg1.get_capture(1)
642 self.verify_relayed_dhcp_discover(rx, self.pg1,
646 self.pg5.add_stream(pkts_disc_vrf2)
647 self.pg_enable_capture(self.pg_interfaces)
650 rx = self.pg2.get_capture(1)
652 self.verify_relayed_dhcp_discover(rx, self.pg2,
654 vpn_id="ip4-table-2")
657 # Add a second DHCP server in VRF 1
658 # expect clients messages to be relay to both configured servers
660 self.pg1.generate_remote_hosts(2)
661 server_addr12 = self.pg1.remote_hosts[1].ip4
663 Proxy12 = VppDHCPProxy(
669 Proxy12.add_vpp_config()
672 # We'll need an ARP entry for the server to send it packets
674 arp_entry = VppNeighbor(self,
675 self.pg1.sw_if_index,
676 self.pg1.remote_hosts[1].mac,
677 self.pg1.remote_hosts[1].ip4)
678 arp_entry.add_vpp_config()
681 # Send a discover from the client. expect two relayed messages
682 # The frist packet is sent to the second server
683 # We're not enforcing that here, it's just the way it is.
685 self.pg4.add_stream(pkts_disc_vrf1)
686 self.pg_enable_capture(self.pg_interfaces)
689 rx = self.pg1.get_capture(2)
691 option_82 = self.verify_relayed_dhcp_discover(
694 dst_mac=self.pg1.remote_hosts[1].mac,
695 dst_ip=self.pg1.remote_hosts[1].ip4,
697 self.verify_relayed_dhcp_discover(rx[1], self.pg1,
702 # Send both packets back. Client gets both.
704 p1 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
705 IP(src=self.pg1.remote_ip4, dst=self.pg1.local_ip4) /
706 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
708 DHCP(options=[('message-type', 'offer'),
709 ('relay_agent_Information', option_82),
711 p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
712 IP(src=self.pg1.remote_hosts[1].ip4, dst=self.pg1.local_ip4) /
713 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
715 DHCP(options=[('message-type', 'offer'),
716 ('relay_agent_Information', option_82),
720 self.pg1.add_stream(pkts)
721 self.pg_enable_capture(self.pg_interfaces)
724 rx = self.pg4.get_capture(2)
726 self.verify_dhcp_offer(rx[0], self.pg4, fib_id=1, oui=4)
727 self.verify_dhcp_offer(rx[1], self.pg4, fib_id=1, oui=4)
730 # Ensure offers from non-servers are dropeed
732 p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
733 IP(src="8.8.8.8", dst=self.pg1.local_ip4) /
734 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
736 DHCP(options=[('message-type', 'offer'),
737 ('relay_agent_Information', option_82),
739 self.send_and_assert_no_replies(self.pg1, p2,
740 "DHCP offer from non-server")
743 # Ensure only the discover is sent to multiple servers
745 p_req_vrf1 = (Ether(dst="ff:ff:ff:ff:ff:ff",
746 src=self.pg4.remote_mac) /
747 IP(src="0.0.0.0", dst="255.255.255.255") /
748 UDP(sport=DHCP4_CLIENT_PORT,
749 dport=DHCP4_SERVER_PORT) /
751 DHCP(options=[('message-type', 'request'),
754 self.pg4.add_stream(p_req_vrf1)
755 self.pg_enable_capture(self.pg_interfaces)
758 rx = self.pg1.get_capture(1)
761 # Remove the second DHCP server
763 Proxy12.remove_vpp_config()
766 # Test we can still relay with the first
768 self.pg4.add_stream(pkts_disc_vrf1)
769 self.pg_enable_capture(self.pg_interfaces)
772 rx = self.pg1.get_capture(1)
774 self.verify_relayed_dhcp_discover(rx, self.pg1,
779 # Remove the VSS config
780 # relayed DHCP has default vlaues in the option.
782 self.vapi.dhcp_proxy_set_vss(tbl_id=1, is_add=0)
783 self.vapi.dhcp_proxy_set_vss(tbl_id=2, is_add=0)
785 self.pg4.add_stream(pkts_disc_vrf1)
786 self.pg_enable_capture(self.pg_interfaces)
789 rx = self.pg1.get_capture(1)
791 self.verify_relayed_dhcp_discover(rx, self.pg1, src_intf=self.pg4)
794 # remove DHCP config to cleanup
796 Proxy1.remove_vpp_config()
797 Proxy2.remove_vpp_config()
799 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf0,
800 "DHCP cleanup VRF 0")
801 self.send_and_assert_no_replies(self.pg4, pkts_disc_vrf1,
802 "DHCP cleanup VRF 1")
803 self.send_and_assert_no_replies(self.pg5, pkts_disc_vrf2,
804 "DHCP cleanup VRF 2")
806 self.pg3.unconfig_ip4()
807 self.pg4.unconfig_ip4()
808 self.pg5.unconfig_ip4()
810 def test_dhcp6_proxy(self):
813 # Verify no response to DHCP request without DHCP config
815 dhcp_solicit_dst = "ff02::1:2"
816 dhcp_solicit_src_vrf0 = mk_ll_addr(self.pg3.remote_mac)
817 dhcp_solicit_src_vrf1 = mk_ll_addr(self.pg4.remote_mac)
818 dhcp_solicit_src_vrf2 = mk_ll_addr(self.pg5.remote_mac)
819 server_addr_vrf0 = self.pg0.remote_ip6
820 src_addr_vrf0 = self.pg0.local_ip6
821 server_addr_vrf1 = self.pg1.remote_ip6
822 src_addr_vrf1 = self.pg1.local_ip6
823 server_addr_vrf2 = self.pg2.remote_ip6
824 src_addr_vrf2 = self.pg2.local_ip6
826 dmac = in6_getnsmac(inet_pton(socket.AF_INET6, dhcp_solicit_dst))
827 p_solicit_vrf0 = (Ether(dst=dmac, src=self.pg3.remote_mac) /
828 IPv6(src=dhcp_solicit_src_vrf0,
829 dst=dhcp_solicit_dst) /
830 UDP(sport=DHCP6_SERVER_PORT,
831 dport=DHCP6_CLIENT_PORT) /
833 p_solicit_vrf1 = (Ether(dst=dmac, src=self.pg4.remote_mac) /
834 IPv6(src=dhcp_solicit_src_vrf1,
835 dst=dhcp_solicit_dst) /
836 UDP(sport=DHCP6_SERVER_PORT,
837 dport=DHCP6_CLIENT_PORT) /
839 p_solicit_vrf2 = (Ether(dst=dmac, src=self.pg5.remote_mac) /
840 IPv6(src=dhcp_solicit_src_vrf2,
841 dst=dhcp_solicit_dst) /
842 UDP(sport=DHCP6_SERVER_PORT,
843 dport=DHCP6_CLIENT_PORT) /
846 self.send_and_assert_no_replies(self.pg3, p_solicit_vrf0,
847 "DHCP with no configuration")
848 self.send_and_assert_no_replies(self.pg4, p_solicit_vrf1,
849 "DHCP with no configuration")
850 self.send_and_assert_no_replies(self.pg5, p_solicit_vrf2,
851 "DHCP with no configuration")
854 # DHCPv6 config in VRF 0.
855 # Packets still dropped because the client facing interface has no
858 Proxy = VppDHCPProxy(
864 Proxy.add_vpp_config()
866 self.send_and_assert_no_replies(self.pg3, p_solicit_vrf0,
867 "DHCP with no configuration")
868 self.send_and_assert_no_replies(self.pg4, p_solicit_vrf1,
869 "DHCP with no configuration")
872 # configure an IP address on the client facing interface
874 self.pg3.config_ip6()
877 # Now the DHCP requests are relayed to the server
879 self.pg3.add_stream(p_solicit_vrf0)
880 self.pg_enable_capture(self.pg_interfaces)
883 rx = self.pg0.get_capture(1)
885 self.verify_dhcp6_solicit(rx[0], self.pg0,
886 dhcp_solicit_src_vrf0,
890 # Exception cases for rejected relay responses
893 # 1 - not a relay reply
894 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
895 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
896 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
898 self.send_and_assert_no_replies(self.pg3, p_adv_vrf0,
899 "DHCP6 not a relay reply")
901 # 2 - no relay message option
902 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
903 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
904 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
907 self.send_and_assert_no_replies(self.pg3, p_adv_vrf0,
908 "DHCP not a relay message")
911 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
912 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
913 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
915 DHCP6OptRelayMsg(optlen=0) /
917 self.send_and_assert_no_replies(self.pg3, p_adv_vrf0,
918 "DHCP6 no circuit ID")
919 # 4 - wrong circuit ID
920 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
921 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
922 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
924 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x05') /
925 DHCP6OptRelayMsg(optlen=0) /
927 self.send_and_assert_no_replies(self.pg3, p_adv_vrf0,
928 "DHCP6 wrong circuit ID")
931 # Send the relay response (the advertisement)
933 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
934 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
935 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
937 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x04') /
938 DHCP6OptRelayMsg(optlen=0) /
939 DHCP6_Advertise(trid=1) /
940 DHCP6OptStatusCode(statuscode=0))
941 pkts_adv_vrf0 = [p_adv_vrf0]
943 self.pg0.add_stream(pkts_adv_vrf0)
944 self.pg_enable_capture(self.pg_interfaces)
947 rx = self.pg3.get_capture(1)
949 self.verify_dhcp6_advert(rx[0], self.pg3, "::")
952 # Send the relay response (the advertisement)
953 # - with peer address
954 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
955 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
956 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
957 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf0) /
958 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x04') /
959 DHCP6OptRelayMsg(optlen=0) /
960 DHCP6_Advertise(trid=1) /
961 DHCP6OptStatusCode(statuscode=0))
962 pkts_adv_vrf0 = [p_adv_vrf0]
964 self.pg0.add_stream(pkts_adv_vrf0)
965 self.pg_enable_capture(self.pg_interfaces)
968 rx = self.pg3.get_capture(1)
970 self.verify_dhcp6_advert(rx[0], self.pg3, dhcp_solicit_src_vrf0)
973 # Add all the config for VRF 1 & 2
975 Proxy1 = VppDHCPProxy(
981 Proxy1.add_vpp_config()
982 self.pg4.config_ip6()
984 Proxy2 = VppDHCPProxy(
990 Proxy2.add_vpp_config()
991 self.pg5.config_ip6()
996 self.pg4.add_stream(p_solicit_vrf1)
997 self.pg_enable_capture(self.pg_interfaces)
1000 rx = self.pg1.get_capture(1)
1002 self.verify_dhcp6_solicit(rx[0], self.pg1,
1003 dhcp_solicit_src_vrf1,
1004 self.pg4.remote_mac)
1009 self.pg5.add_stream(p_solicit_vrf2)
1010 self.pg_enable_capture(self.pg_interfaces)
1013 rx = self.pg2.get_capture(1)
1015 self.verify_dhcp6_solicit(rx[0], self.pg2,
1016 dhcp_solicit_src_vrf2,
1017 self.pg5.remote_mac)
1022 p_adv_vrf1 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
1023 IPv6(dst=self.pg1.local_ip6, src=self.pg1.remote_ip6) /
1024 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
1025 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) /
1026 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x05') /
1027 DHCP6OptRelayMsg(optlen=0) /
1028 DHCP6_Advertise(trid=1) /
1029 DHCP6OptStatusCode(statuscode=0))
1030 pkts_adv_vrf1 = [p_adv_vrf1]
1032 self.pg1.add_stream(pkts_adv_vrf1)
1033 self.pg_enable_capture(self.pg_interfaces)
1036 rx = self.pg4.get_capture(1)
1038 self.verify_dhcp6_advert(rx[0], self.pg4, dhcp_solicit_src_vrf1)
1043 self.vapi.dhcp_proxy_set_vss(
1044 tbl_id=1, vss_type=1, oui=4, vpn_index=1, is_ipv6=1, is_add=1)
1045 self.vapi.dhcp_proxy_set_vss(
1048 vpn_ascii_id="IPv6-table-2",
1052 self.pg4.add_stream(p_solicit_vrf1)
1053 self.pg_enable_capture(self.pg_interfaces)
1056 rx = self.pg1.get_capture(1)
1058 self.verify_dhcp6_solicit(rx[0], self.pg1,
1059 dhcp_solicit_src_vrf1,
1060 self.pg4.remote_mac,
1064 self.pg5.add_stream(p_solicit_vrf2)
1065 self.pg_enable_capture(self.pg_interfaces)
1068 rx = self.pg2.get_capture(1)
1070 self.verify_dhcp6_solicit(rx[0], self.pg2,
1071 dhcp_solicit_src_vrf2,
1072 self.pg5.remote_mac,
1073 vpn_id="IPv6-table-2")
1076 # Remove the VSS config
1077 # relayed DHCP has default vlaues in the option.
1079 self.vapi.dhcp_proxy_set_vss(tbl_id=1, is_ipv6=1, is_add=0)
1081 self.pg4.add_stream(p_solicit_vrf1)
1082 self.pg_enable_capture(self.pg_interfaces)
1085 rx = self.pg1.get_capture(1)
1087 self.verify_dhcp6_solicit(rx[0], self.pg1,
1088 dhcp_solicit_src_vrf1,
1089 self.pg4.remote_mac)
1092 # Add a second DHCP server in VRF 1
1093 # expect clients messages to be relay to both configured servers
1095 self.pg1.generate_remote_hosts(2)
1096 server_addr12 = self.pg1.remote_hosts[1].ip6
1098 Proxy12 = VppDHCPProxy(
1104 Proxy12.add_vpp_config()
1107 # We'll need an ND entry for the server to send it packets
1109 nd_entry = VppNeighbor(self,
1110 self.pg1.sw_if_index,
1111 self.pg1.remote_hosts[1].mac,
1112 self.pg1.remote_hosts[1].ip6)
1113 nd_entry.add_vpp_config()
1116 # Send a discover from the client. expect two relayed messages
1117 # The frist packet is sent to the second server
1118 # We're not enforcing that here, it's just the way it is.
1120 self.pg4.add_stream(p_solicit_vrf1)
1121 self.pg_enable_capture(self.pg_interfaces)
1124 rx = self.pg1.get_capture(2)
1126 self.verify_dhcp6_solicit(rx[0], self.pg1,
1127 dhcp_solicit_src_vrf1,
1128 self.pg4.remote_mac)
1129 self.verify_dhcp6_solicit(rx[1], self.pg1,
1130 dhcp_solicit_src_vrf1,
1131 self.pg4.remote_mac,
1132 dst_mac=self.pg1.remote_hosts[1].mac,
1133 dst_ip=self.pg1.remote_hosts[1].ip6)
1136 # Send both packets back. Client gets both.
1138 p1 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
1139 IPv6(dst=self.pg1.local_ip6, src=self.pg1.remote_ip6) /
1140 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
1141 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) /
1142 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x05') /
1143 DHCP6OptRelayMsg(optlen=0) /
1144 DHCP6_Advertise(trid=1) /
1145 DHCP6OptStatusCode(statuscode=0))
1146 p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_hosts[1].mac) /
1147 IPv6(dst=self.pg1.local_ip6, src=self.pg1._remote_hosts[1].ip6) /
1148 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
1149 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) /
1150 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x05') /
1151 DHCP6OptRelayMsg(optlen=0) /
1152 DHCP6_Advertise(trid=1) /
1153 DHCP6OptStatusCode(statuscode=0))
1157 self.pg1.add_stream(pkts)
1158 self.pg_enable_capture(self.pg_interfaces)
1161 rx = self.pg4.get_capture(2)
1163 self.verify_dhcp6_advert(rx[0], self.pg4, dhcp_solicit_src_vrf1)
1164 self.verify_dhcp6_advert(rx[1], self.pg4, dhcp_solicit_src_vrf1)
1167 # Ensure only solicit messages are duplicated
1169 p_request_vrf1 = (Ether(dst=dmac, src=self.pg4.remote_mac) /
1170 IPv6(src=dhcp_solicit_src_vrf1,
1171 dst=dhcp_solicit_dst) /
1172 UDP(sport=DHCP6_SERVER_PORT,
1173 dport=DHCP6_CLIENT_PORT) /
1176 self.pg4.add_stream(p_request_vrf1)
1177 self.pg_enable_capture(self.pg_interfaces)
1180 rx = self.pg1.get_capture(1)
1183 # Test we drop DHCP packets from addresses that are not configured as
1186 p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_hosts[1].mac) /
1187 IPv6(dst=self.pg1.local_ip6, src="3001::1") /
1188 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
1189 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) /
1190 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x05') /
1191 DHCP6OptRelayMsg(optlen=0) /
1192 DHCP6_Advertise(trid=1) /
1193 DHCP6OptStatusCode(statuscode=0))
1194 self.send_and_assert_no_replies(self.pg1, p2,
1195 "DHCP6 not from server")
1198 # Remove the second DHCP server
1200 Proxy12.remove_vpp_config()
1203 # Test we can still relay with the first
1205 self.pg4.add_stream(p_solicit_vrf1)
1206 self.pg_enable_capture(self.pg_interfaces)
1209 rx = self.pg1.get_capture(1)
1211 self.verify_dhcp6_solicit(rx[0], self.pg1,
1212 dhcp_solicit_src_vrf1,
1213 self.pg4.remote_mac)
1218 Proxy.remove_vpp_config()
1219 Proxy1.remove_vpp_config()
1220 Proxy2.remove_vpp_config()
1222 self.pg3.unconfig_ip6()
1223 self.pg4.unconfig_ip6()
1224 self.pg5.unconfig_ip6()
1226 def test_dhcp_client(self):
1229 vdscp = VppEnum.vl_api_ip_dscp_t
1230 hostname = 'universal-dp'
1232 self.pg_enable_capture(self.pg_interfaces)
1235 # Configure DHCP client on PG3 and capture the discover sent
1237 Client = VppDHCPClient(self, self.pg3.sw_if_index, hostname)
1238 Client.add_vpp_config()
1239 self.assertTrue(Client.query_vpp_config())
1241 rx = self.pg3.get_capture(1)
1243 self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname)
1246 # Send back on offer, expect the request
1248 p_offer = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1249 IP(src=self.pg3.remote_ip4, dst="255.255.255.255") /
1250 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1252 yiaddr=self.pg3.local_ip4,
1253 chaddr=mac_pton(self.pg3.local_mac)) /
1254 DHCP(options=[('message-type', 'offer'),
1255 ('server_id', self.pg3.remote_ip4),
1258 self.pg3.add_stream(p_offer)
1259 self.pg_enable_capture(self.pg_interfaces)
1262 rx = self.pg3.get_capture(1)
1263 self.verify_orig_dhcp_request(rx[0], self.pg3, hostname,
1267 # Send an acknowledgment
1269 p_ack = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1270 IP(src=self.pg3.remote_ip4, dst="255.255.255.255") /
1271 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1272 BOOTP(op=1, yiaddr=self.pg3.local_ip4,
1273 chaddr=mac_pton(self.pg3.local_mac)) /
1274 DHCP(options=[('message-type', 'ack'),
1275 ('subnet_mask', "255.255.255.0"),
1276 ('router', self.pg3.remote_ip4),
1277 ('server_id', self.pg3.remote_ip4),
1278 ('lease_time', 43200),
1281 self.pg3.add_stream(p_ack)
1282 self.pg_enable_capture(self.pg_interfaces)
1286 # We'll get an ARP request for the router address
1288 rx = self.pg3.get_capture(1)
1290 self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
1291 self.pg_enable_capture(self.pg_interfaces)
1294 # At the end of this procedure there should be a connected route
1297 self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
1298 self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
1300 # remove the left over ARP entry
1301 self.vapi.ip_neighbor_add_del(self.pg3.sw_if_index,
1302 self.pg3.remote_mac,
1303 self.pg3.remote_ip4,
1307 # remove the DHCP config
1309 Client.remove_vpp_config()
1312 # and now the route should be gone
1314 self.assertFalse(find_route(self, self.pg3.local_ip4, 32))
1315 self.assertFalse(find_route(self, self.pg3.local_ip4, 24))
1318 # Start the procedure again. this time have VPP send the client-ID
1319 # and set the DSCP value
1321 self.pg3.admin_down()
1324 Client.set_client(self.pg3.sw_if_index, hostname,
1325 id=self.pg3.local_mac,
1326 dscp=vdscp.IP_API_DSCP_EF)
1327 Client.add_vpp_config()
1329 rx = self.pg3.get_capture(1)
1331 self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname,
1333 dscp=vdscp.IP_API_DSCP_EF)
1335 # TODO: VPP DHCP client should not accept DHCP OFFER message with
1336 # the XID (Transaction ID) not matching the XID of the most recent
1337 # DHCP DISCOVERY message.
1338 # Such DHCP OFFER message must be silently discarded - RFC2131.
1339 # Reported in Jira ticket: VPP-99
1340 self.pg3.add_stream(p_offer)
1341 self.pg_enable_capture(self.pg_interfaces)
1344 rx = self.pg3.get_capture(1)
1345 self.verify_orig_dhcp_request(rx[0], self.pg3, hostname,
1347 dscp=vdscp.IP_API_DSCP_EF)
1350 # unicast the ack to the offered address
1352 p_ack = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1353 IP(src=self.pg3.remote_ip4, dst=self.pg3.local_ip4) /
1354 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1355 BOOTP(op=1, yiaddr=self.pg3.local_ip4,
1356 chaddr=mac_pton(self.pg3.local_mac)) /
1357 DHCP(options=[('message-type', 'ack'),
1358 ('subnet_mask', "255.255.255.0"),
1359 ('router', self.pg3.remote_ip4),
1360 ('server_id', self.pg3.remote_ip4),
1361 ('lease_time', 43200),
1364 self.pg3.add_stream(p_ack)
1365 self.pg_enable_capture(self.pg_interfaces)
1369 # We'll get an ARP request for the router address
1371 rx = self.pg3.get_capture(1)
1373 self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
1374 self.pg_enable_capture(self.pg_interfaces)
1377 # At the end of this procedure there should be a connected route
1380 self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
1381 self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
1384 # remove the DHCP config
1386 Client.remove_vpp_config()
1388 self.assertFalse(find_route(self, self.pg3.local_ip4, 32))
1389 self.assertFalse(find_route(self, self.pg3.local_ip4, 24))
1392 # Rince and repeat, this time with VPP configured not to set
1393 # the braodcast flag in the discover and request messages,
1394 # and for the server to unicast the responses.
1396 # Configure DHCP client on PG3 and capture the discover sent
1399 self.pg3.sw_if_index,
1401 set_broadcast_flag=False)
1402 Client.add_vpp_config()
1404 rx = self.pg3.get_capture(1)
1406 self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname,
1410 # Send back on offer, unicasted to the offered address.
1411 # Expect the request.
1413 p_offer = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1414 IP(src=self.pg3.remote_ip4, dst=self.pg3.local_ip4) /
1415 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1416 BOOTP(op=1, yiaddr=self.pg3.local_ip4,
1417 chaddr=mac_pton(self.pg3.local_mac)) /
1418 DHCP(options=[('message-type', 'offer'),
1419 ('server_id', self.pg3.remote_ip4),
1422 self.pg3.add_stream(p_offer)
1423 self.pg_enable_capture(self.pg_interfaces)
1426 rx = self.pg3.get_capture(1)
1427 self.verify_orig_dhcp_request(rx[0], self.pg3, hostname,
1432 # Send an acknowledgment, the lease renewal time is 2 seconds
1433 # so we should expect the renew straight after
1435 p_ack = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1436 IP(src=self.pg3.remote_ip4, dst=self.pg3.local_ip4) /
1437 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1438 BOOTP(op=1, yiaddr=self.pg3.local_ip4,
1439 chaddr=mac_pton(self.pg3.local_mac)) /
1440 DHCP(options=[('message-type', 'ack'),
1441 ('subnet_mask', "255.255.255.0"),
1442 ('router', self.pg3.remote_ip4),
1443 ('server_id', self.pg3.remote_ip4),
1444 ('lease_time', 43200),
1445 ('renewal_time', 2),
1448 self.pg3.add_stream(p_ack)
1449 self.pg_enable_capture(self.pg_interfaces)
1453 # We'll get an ARP request for the router address
1455 rx = self.pg3.get_capture(1)
1457 self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
1458 self.pg_enable_capture(self.pg_interfaces)
1461 # At the end of this procedure there should be a connected route
1464 self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
1465 self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
1468 # read the DHCP client details from a dump
1470 clients = self.vapi.dhcp_client_dump()
1472 self.assertEqual(clients[0].client.sw_if_index,
1473 self.pg3.sw_if_index)
1474 self.assertEqual(clients[0].lease.sw_if_index,
1475 self.pg3.sw_if_index)
1476 self.assertEqual(clients[0].client.hostname, hostname)
1477 self.assertEqual(clients[0].lease.hostname, hostname)
1478 # 0 = DISCOVER, 1 = REQUEST, 2 = BOUND
1479 self.assertEqual(clients[0].lease.state, 2)
1480 self.assertEqual(clients[0].lease.mask_width, 24)
1481 self.assertEqual(str(clients[0].lease.router_address),
1482 self.pg3.remote_ip4)
1483 self.assertEqual(str(clients[0].lease.host_address),
1487 # wait for the unicasted renewal
1488 # the first attempt will be an ARP packet, since we have not yet
1489 # responded to VPP's request
1491 self.logger.info(self.vapi.cli("sh dhcp client intfc pg3 verbose"))
1492 rx = self.pg3.get_capture(1, timeout=10)
1494 self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
1496 # respond to the arp
1497 p_arp = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1499 hwdst=self.pg3.local_mac,
1500 hwsrc=self.pg3.remote_mac,
1501 pdst=self.pg3.local_ip4,
1502 psrc=self.pg3.remote_ip4))
1503 self.pg3.add_stream(p_arp)
1504 self.pg_enable_capture(self.pg_interfaces)
1507 # the next packet is the unicasted renewal
1508 rx = self.pg3.get_capture(1, timeout=10)
1509 self.verify_orig_dhcp_request(rx[0], self.pg3, hostname,
1514 # send an ACK with different data from the original offer *
1515 self.pg3.generate_remote_hosts(4)
1516 p_ack = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1517 IP(src=self.pg3.remote_ip4, dst=self.pg3.local_ip4) /
1518 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1519 BOOTP(op=1, yiaddr=self.pg3.remote_hosts[3].ip4,
1520 chaddr=mac_pton(self.pg3.local_mac)) /
1521 DHCP(options=[('message-type', 'ack'),
1522 ('subnet_mask', "255.255.255.0"),
1523 ('router', self.pg3.remote_hosts[1].ip4),
1524 ('server_id', self.pg3.remote_hosts[2].ip4),
1525 ('lease_time', 43200),
1526 ('renewal_time', 2),
1529 self.pg3.add_stream(p_ack)
1530 self.pg_enable_capture(self.pg_interfaces)
1534 # read the DHCP client details from a dump
1536 clients = self.vapi.dhcp_client_dump()
1538 self.assertEqual(clients[0].client.sw_if_index,
1539 self.pg3.sw_if_index)
1540 self.assertEqual(clients[0].lease.sw_if_index,
1541 self.pg3.sw_if_index)
1542 self.assertEqual(clients[0].client.hostname, hostname)
1543 self.assertEqual(clients[0].lease.hostname, hostname)
1544 # 0 = DISCOVER, 1 = REQUEST, 2 = BOUND
1545 self.assertEqual(clients[0].lease.state, 2)
1546 self.assertEqual(clients[0].lease.mask_width, 24)
1547 self.assertEqual(str(clients[0].lease.router_address),
1548 self.pg3.remote_hosts[1].ip4)
1549 self.assertEqual(str(clients[0].lease.host_address),
1550 self.pg3.remote_hosts[3].ip4)
1553 # remove the DHCP config
1555 Client.remove_vpp_config()
1558 # and now the route should be gone
1560 self.assertFalse(find_route(self, self.pg3.local_ip4, 32))
1561 self.assertFalse(find_route(self, self.pg3.local_ip4, 24))
1564 # Start the procedure again. Use requested lease time option.
1565 # this time wait for the lease to expire and the client to
1569 self.pg3.admin_down()
1572 self.pg_enable_capture(self.pg_interfaces)
1573 Client.set_client(self.pg3.sw_if_index, hostname)
1574 Client.add_vpp_config()
1576 rx = self.pg3.get_capture(1)
1578 self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname)
1581 # Send back on offer with requested lease time, expect the request
1584 p_offer = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1585 IP(src=self.pg3.remote_ip4, dst='255.255.255.255') /
1586 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1588 yiaddr=self.pg3.local_ip4,
1589 chaddr=mac_pton(self.pg3.local_mac)) /
1590 DHCP(options=[('message-type', 'offer'),
1591 ('server_id', self.pg3.remote_ip4),
1592 ('lease_time', lease_time),
1595 self.pg3.add_stream(p_offer)
1596 self.pg_enable_capture(self.pg_interfaces)
1599 rx = self.pg3.get_capture(1)
1600 self.verify_orig_dhcp_request(rx[0], self.pg3, hostname,
1604 # Send an acknowledgment
1606 p_ack = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1607 IP(src=self.pg3.remote_ip4, dst='255.255.255.255') /
1608 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1609 BOOTP(op=1, yiaddr=self.pg3.local_ip4,
1610 chaddr=mac_pton(self.pg3.local_mac)) /
1611 DHCP(options=[('message-type', 'ack'),
1612 ('subnet_mask', '255.255.255.0'),
1613 ('router', self.pg3.remote_ip4),
1614 ('server_id', self.pg3.remote_ip4),
1615 ('lease_time', lease_time),
1618 self.pg3.add_stream(p_ack)
1619 self.pg_enable_capture(self.pg_interfaces)
1623 # We'll get an ARP request for the router address
1625 rx = self.pg3.get_capture(1)
1627 self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
1630 # At the end of this procedure there should be a connected route
1633 self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
1634 self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
1637 # the route should be gone after the lease expires
1639 self.assertTrue(self.wait_for_no_route(self.pg3.local_ip4, 32))
1640 self.assertTrue(self.wait_for_no_route(self.pg3.local_ip4, 24))
1643 # remove the DHCP config
1645 Client.remove_vpp_config()
1647 def test_dhcp_client_vlan(self):
1648 """ DHCP Client w/ VLAN"""
1650 vdscp = VppEnum.vl_api_ip_dscp_t
1651 vqos = VppEnum.vl_api_qos_source_t
1652 hostname = 'universal-dp'
1654 self.pg_enable_capture(self.pg_interfaces)
1656 vlan_100 = VppDot1QSubint(self, self.pg3, 100)
1659 output = [scapy.compat.chb(4)] * 256
1660 os = b''.join(output)
1661 rows = [{'outputs': os},
1666 qem1 = VppQosEgressMap(self, 1, rows).add_vpp_config()
1667 qm1 = VppQosMark(self, vlan_100, qem1,
1668 vqos.QOS_API_SOURCE_VLAN).add_vpp_config()
1671 # Configure DHCP client on PG3 and capture the discover sent
1673 Client = VppDHCPClient(
1675 vlan_100.sw_if_index,
1677 dscp=vdscp.IP_API_DSCP_EF)
1678 Client.add_vpp_config()
1680 rx = self.pg3.get_capture(1)
1682 self.assertEqual(rx[0][Dot1Q].vlan, 100)
1683 self.assertEqual(rx[0][Dot1Q].prio, 2)
1685 self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname,
1686 dscp=vdscp.IP_API_DSCP_EF)
1689 if __name__ == '__main__':
1690 unittest.main(testRunner=VppTestRunner)