blob: a349356119a5eca94a93fc934d27588b9f173bee [file] [log] [blame]
Renato Botelho do Coutoead1e532019-10-31 13:31:07 -05001#!/usr/bin/env python3
Neale Rannsfca0c242017-01-13 07:57:46 -08002
3import unittest
4import socket
Neale Ranns3466c302017-02-16 07:45:03 -08005import struct
Ole Troan12966a72019-10-18 14:33:54 +02006import six
Neale Rannsfca0c242017-01-13 07:57:46 -08007
Klement Sekerab23ffd72021-05-31 16:08:53 +02008from framework import VppTestCase, VppTestRunner
Andrew Yourtchenko06f32812021-01-14 10:19:08 +00009from framework import tag_run_solo
Neale Ranns3466c302017-02-16 07:45:03 -080010from vpp_neighbor import VppNeighbor
Neale Ranns15002542017-09-10 04:39:11 -070011from vpp_ip_route import find_route, VppIpTable
Neale Ranns2a3ea492017-04-19 05:24:40 -070012from util import mk_ll_addr
Paul Vinciguerraa7427ec2019-03-10 10:04:23 -070013import scapy.compat
Neale Ranns038e1df2019-07-19 14:01:02 +000014from scapy.layers.l2 import Ether, getmacbyip, ARP, Dot1Q
Neale Rannsfca0c242017-01-13 07:57:46 -080015from scapy.layers.inet import IP, UDP, ICMP
Neale Ranns2bc94022018-02-25 12:27:18 -080016from scapy.layers.inet6 import IPv6, in6_getnsmac
17from scapy.utils6 import in6_mactoifaceid
Neale Rannsfca0c242017-01-13 07:57:46 -080018from scapy.layers.dhcp import DHCP, BOOTP, DHCPTypes
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020019from scapy.layers.dhcp6 import (
20 DHCP6,
21 DHCP6_Solicit,
22 DHCP6_RelayForward,
23 DHCP6_RelayReply,
24 DHCP6_Advertise,
25 DHCP6OptRelayMsg,
26 DHCP6OptIfaceId,
27 DHCP6OptStatusCode,
28 DHCP6OptVSS,
29 DHCP6OptClientLinkLayerAddr,
30 DHCP6_Request,
31)
snaramre5d4b8912019-12-13 23:39:35 +000032from socket import AF_INET, AF_INET6, inet_pton, inet_ntop
Neale Rannsfca0c242017-01-13 07:57:46 -080033from scapy.utils6 import in6_ptop
Neale Ranns038e1df2019-07-19 14:01:02 +000034from vpp_papi import mac_pton, VppEnum
35from vpp_sub_interface import VppDot1QSubint
36from vpp_qos import VppQosEgressMap, VppQosMark
Jakub Grajciar103ba6b2019-10-01 11:30:56 +020037from vpp_dhcp import VppDHCPClient, VppDHCPProxy
Neale Ranns038e1df2019-07-19 14:01:02 +000038
Neale Rannsfca0c242017-01-13 07:57:46 -080039
40DHCP4_CLIENT_PORT = 68
41DHCP4_SERVER_PORT = 67
42DHCP6_CLIENT_PORT = 547
43DHCP6_SERVER_PORT = 546
44
45
Andrew Yourtchenko06f32812021-01-14 10:19:08 +000046@tag_run_solo
Neale Rannsfca0c242017-01-13 07:57:46 -080047class TestDHCP(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020048 """DHCP Test Case"""
Neale Rannsfca0c242017-01-13 07:57:46 -080049
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -070050 @classmethod
51 def setUpClass(cls):
52 super(TestDHCP, cls).setUpClass()
53
54 @classmethod
55 def tearDownClass(cls):
56 super(TestDHCP, cls).tearDownClass()
57
Neale Rannsfca0c242017-01-13 07:57:46 -080058 def setUp(self):
59 super(TestDHCP, self).setUp()
60
John Lo70bfcaf2017-11-14 13:19:26 -050061 # create 6 pg interfaces for pg0 to pg5
62 self.create_pg_interfaces(range(6))
Neale Ranns15002542017-09-10 04:39:11 -070063 self.tables = []
Neale Rannsfca0c242017-01-13 07:57:46 -080064
John Lo70bfcaf2017-11-14 13:19:26 -050065 # pg0 to 2 are IP configured in VRF 0, 1 and 2.
66 # pg3 to 5 are non IP-configured in VRF 0, 1 and 2.
Neale Rannsfca0c242017-01-13 07:57:46 -080067 table_id = 0
Neale Ranns15002542017-09-10 04:39:11 -070068 for table_id in range(1, 4):
69 tbl4 = VppIpTable(self, table_id)
70 tbl4.add_vpp_config()
71 self.tables.append(tbl4)
72 tbl6 = VppIpTable(self, table_id, is_ip6=1)
73 tbl6.add_vpp_config()
74 self.tables.append(tbl6)
75
76 table_id = 0
John Lo70bfcaf2017-11-14 13:19:26 -050077 for i in self.pg_interfaces[:3]:
Neale Rannsfca0c242017-01-13 07:57:46 -080078 i.admin_up()
79 i.set_table_ip4(table_id)
80 i.set_table_ip6(table_id)
81 i.config_ip4()
82 i.resolve_arp()
83 i.config_ip6()
84 i.resolve_ndp()
85 table_id += 1
86
87 table_id = 0
John Lo70bfcaf2017-11-14 13:19:26 -050088 for i in self.pg_interfaces[3:]:
Neale Rannsfca0c242017-01-13 07:57:46 -080089 i.admin_up()
90 i.set_table_ip4(table_id)
91 i.set_table_ip6(table_id)
92 table_id += 1
93
Neale Ranns4008ac92017-02-13 23:20:04 -080094 def tearDown(self):
John Lo70bfcaf2017-11-14 13:19:26 -050095 for i in self.pg_interfaces[:3]:
Neale Ranns4008ac92017-02-13 23:20:04 -080096 i.unconfig_ip4()
97 i.unconfig_ip6()
Neale Ranns15002542017-09-10 04:39:11 -070098
99 for i in self.pg_interfaces:
100 i.set_table_ip4(0)
101 i.set_table_ip6(0)
Neale Ranns4008ac92017-02-13 23:20:04 -0800102 i.admin_down()
Neale Ranns15002542017-09-10 04:39:11 -0700103 super(TestDHCP, self).tearDown()
Neale Ranns4008ac92017-02-13 23:20:04 -0800104
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700105 def verify_dhcp_has_option(self, pkt, option, value):
106 dhcp = pkt[DHCP]
107 found = False
108
109 for i in dhcp.options:
Jakub Grajciar103ba6b2019-10-01 11:30:56 +0200110 if isinstance(i, tuple):
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700111 if i[0] == option:
112 self.assertEqual(i[1], value)
113 found = True
114
115 self.assertTrue(found)
116
John Lo70bfcaf2017-11-14 13:19:26 -0500117 def validate_relay_options(self, pkt, intf, ip_addr, vpn_id, fib_id, oui):
Neale Rannsfca0c242017-01-13 07:57:46 -0800118 dhcp = pkt[DHCP]
119 found = 0
120 data = []
John Lo70bfcaf2017-11-14 13:19:26 -0500121 id_len = len(vpn_id)
Neale Rannsfca0c242017-01-13 07:57:46 -0800122
123 for i in dhcp.options:
Jakub Grajciar103ba6b2019-10-01 11:30:56 +0200124 if isinstance(i, tuple):
Neale Rannsfca0c242017-01-13 07:57:46 -0800125 if i[0] == "relay_agent_Information":
126 #
127 # There are two sb-options present - each of length 6.
128 #
129 data = i[1]
Neale Ranns20a175a2017-02-14 07:28:41 -0800130 if oui != 0:
131 self.assertEqual(len(data), 24)
John Lo70bfcaf2017-11-14 13:19:26 -0500132 elif len(vpn_id) > 0:
Jakub Grajciar103ba6b2019-10-01 11:30:56 +0200133 self.assertEqual(len(data), len(vpn_id) + 17)
Neale Ranns20a175a2017-02-14 07:28:41 -0800134 else:
135 self.assertEqual(len(data), 12)
Neale Rannsfca0c242017-01-13 07:57:46 -0800136
137 #
138 # First sub-option is ID 1, len 4, then encoded
139 # sw_if_index. This test uses low valued indicies
140 # so [2:4] are 0.
141 # The ID space is VPP internal - so no matching value
142 # scapy
143 #
Ole Troan12966a72019-10-18 14:33:54 +0200144 self.assertEqual(six.byte2int(data[0:1]), 1)
145 self.assertEqual(six.byte2int(data[1:2]), 4)
146 self.assertEqual(six.byte2int(data[2:3]), 0)
147 self.assertEqual(six.byte2int(data[3:4]), 0)
148 self.assertEqual(six.byte2int(data[4:5]), 0)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200149 self.assertEqual(six.byte2int(data[5:6]), intf._sw_if_index)
Neale Rannsfca0c242017-01-13 07:57:46 -0800150
151 #
152 # next sub-option is the IP address of the client side
153 # interface.
154 # sub-option ID=5, length (of a v4 address)=4
155 #
156 claddr = socket.inet_pton(AF_INET, ip_addr)
157
Ole Troan12966a72019-10-18 14:33:54 +0200158 self.assertEqual(six.byte2int(data[6:7]), 5)
159 self.assertEqual(six.byte2int(data[7:8]), 4)
Neale Rannsfca0c242017-01-13 07:57:46 -0800160 self.assertEqual(data[8], claddr[0])
161 self.assertEqual(data[9], claddr[1])
162 self.assertEqual(data[10], claddr[2])
163 self.assertEqual(data[11], claddr[3])
164
Neale Ranns20a175a2017-02-14 07:28:41 -0800165 if oui != 0:
John Lo70bfcaf2017-11-14 13:19:26 -0500166 # sub-option 151 encodes vss_type 1,
167 # the 3 byte oui and the 4 byte fib_id
168 self.assertEqual(id_len, 0)
Ole Troan12966a72019-10-18 14:33:54 +0200169 self.assertEqual(six.byte2int(data[12:13]), 151)
170 self.assertEqual(six.byte2int(data[13:14]), 8)
171 self.assertEqual(six.byte2int(data[14:15]), 1)
172 self.assertEqual(six.byte2int(data[15:16]), 0)
173 self.assertEqual(six.byte2int(data[16:17]), 0)
174 self.assertEqual(six.byte2int(data[17:18]), oui)
175 self.assertEqual(six.byte2int(data[18:19]), 0)
176 self.assertEqual(six.byte2int(data[19:20]), 0)
177 self.assertEqual(six.byte2int(data[20:21]), 0)
178 self.assertEqual(six.byte2int(data[21:22]), fib_id)
Neale Ranns20a175a2017-02-14 07:28:41 -0800179
180 # VSS control sub-option
Ole Troan12966a72019-10-18 14:33:54 +0200181 self.assertEqual(six.byte2int(data[22:23]), 152)
182 self.assertEqual(six.byte2int(data[23:24]), 0)
Neale Ranns20a175a2017-02-14 07:28:41 -0800183
John Lo70bfcaf2017-11-14 13:19:26 -0500184 if id_len > 0:
185 # sub-option 151 encode vss_type of 0
186 # followerd by vpn_id in ascii
187 self.assertEqual(oui, 0)
Ole Troan12966a72019-10-18 14:33:54 +0200188 self.assertEqual(six.byte2int(data[12:13]), 151)
189 self.assertEqual(six.byte2int(data[13:14]), id_len + 1)
190 self.assertEqual(six.byte2int(data[14:15]), 0)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200191 self.assertEqual(data[15 : 15 + id_len].decode("ascii"), vpn_id)
John Lo70bfcaf2017-11-14 13:19:26 -0500192
193 # VSS control sub-option
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200194 self.assertEqual(
195 six.byte2int(data[15 + len(vpn_id) : 16 + len(vpn_id)]), 152
196 )
197 self.assertEqual(
198 six.byte2int(data[16 + len(vpn_id) : 17 + len(vpn_id)]), 0
199 )
John Lo70bfcaf2017-11-14 13:19:26 -0500200
Neale Rannsfca0c242017-01-13 07:57:46 -0800201 found = 1
202 self.assertTrue(found)
203
204 return data
205
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700206 def verify_dhcp_msg_type(self, pkt, name):
207 dhcp = pkt[DHCP]
208 found = False
209 for o in dhcp.options:
Jakub Grajciar103ba6b2019-10-01 11:30:56 +0200210 if isinstance(o, tuple):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200211 if o[0] == "message-type" and DHCPTypes[o[1]] == name:
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700212 found = True
213 self.assertTrue(found)
214
John Lo70bfcaf2017-11-14 13:19:26 -0500215 def verify_dhcp_offer(self, pkt, intf, vpn_id="", fib_id=0, oui=0):
Neale Rannsfca0c242017-01-13 07:57:46 -0800216 ether = pkt[Ether]
217 self.assertEqual(ether.dst, "ff:ff:ff:ff:ff:ff")
218 self.assertEqual(ether.src, intf.local_mac)
219
220 ip = pkt[IP]
221 self.assertEqual(ip.dst, "255.255.255.255")
222 self.assertEqual(ip.src, intf.local_ip4)
223
224 udp = pkt[UDP]
225 self.assertEqual(udp.dport, DHCP4_CLIENT_PORT)
226 self.assertEqual(udp.sport, DHCP4_SERVER_PORT)
227
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700228 self.verify_dhcp_msg_type(pkt, "offer")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200229 data = self.validate_relay_options(
230 pkt, intf, intf.local_ip4, vpn_id, fib_id, oui
231 )
Neale Rannsfca0c242017-01-13 07:57:46 -0800232
Neale Ranns038e1df2019-07-19 14:01:02 +0000233 def verify_orig_dhcp_pkt(self, pkt, intf, dscp, l2_bc=True):
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700234 ether = pkt[Ether]
Neale Ranns99536f42019-07-25 06:11:58 -0700235 if l2_bc:
236 self.assertEqual(ether.dst, "ff:ff:ff:ff:ff:ff")
237 else:
238 self.assertEqual(ether.dst, intf.remote_mac)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700239 self.assertEqual(ether.src, intf.local_mac)
240
241 ip = pkt[IP]
Neale Ranns99536f42019-07-25 06:11:58 -0700242
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200243 if l2_bc:
Neale Ranns99536f42019-07-25 06:11:58 -0700244 self.assertEqual(ip.dst, "255.255.255.255")
245 self.assertEqual(ip.src, "0.0.0.0")
246 else:
247 self.assertEqual(ip.dst, intf.remote_ip4)
248 self.assertEqual(ip.src, intf.local_ip4)
Neale Ranns038e1df2019-07-19 14:01:02 +0000249 self.assertEqual(ip.tos, dscp)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700250
251 udp = pkt[UDP]
252 self.assertEqual(udp.dport, DHCP4_SERVER_PORT)
253 self.assertEqual(udp.sport, DHCP4_CLIENT_PORT)
254
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200255 def verify_orig_dhcp_discover(
256 self, pkt, intf, hostname, client_id=None, broadcast=True, dscp=0
257 ):
Neale Ranns038e1df2019-07-19 14:01:02 +0000258 self.verify_orig_dhcp_pkt(pkt, intf, dscp)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700259
260 self.verify_dhcp_msg_type(pkt, "discover")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200261 self.verify_dhcp_has_option(pkt, "hostname", hostname.encode("ascii"))
Neale Ranns51822bf2017-07-18 09:26:53 -0700262 if client_id:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200263 client_id = "\x00" + client_id
264 self.verify_dhcp_has_option(pkt, "client_id", client_id.encode("ascii"))
Neale Ranns808c5b22017-08-02 05:15:07 -0700265 bootp = pkt[BOOTP]
266 self.assertEqual(bootp.ciaddr, "0.0.0.0")
267 self.assertEqual(bootp.giaddr, "0.0.0.0")
Neale Ranns54c6dc42018-01-17 10:29:10 -0800268 if broadcast:
269 self.assertEqual(bootp.flags, 0x8000)
270 else:
271 self.assertEqual(bootp.flags, 0x0000)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700272
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200273 def verify_orig_dhcp_request(
274 self, pkt, intf, hostname, ip, broadcast=True, l2_bc=True, dscp=0
275 ):
Neale Ranns038e1df2019-07-19 14:01:02 +0000276 self.verify_orig_dhcp_pkt(pkt, intf, dscp, l2_bc=l2_bc)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700277
278 self.verify_dhcp_msg_type(pkt, "request")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200279 self.verify_dhcp_has_option(pkt, "hostname", hostname.encode("ascii"))
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700280 self.verify_dhcp_has_option(pkt, "requested_addr", ip)
Neale Ranns808c5b22017-08-02 05:15:07 -0700281 bootp = pkt[BOOTP]
Neale Ranns99536f42019-07-25 06:11:58 -0700282
283 if l2_bc:
284 self.assertEqual(bootp.ciaddr, "0.0.0.0")
285 else:
286 self.assertEqual(bootp.ciaddr, intf.local_ip4)
Neale Ranns808c5b22017-08-02 05:15:07 -0700287 self.assertEqual(bootp.giaddr, "0.0.0.0")
Neale Ranns99536f42019-07-25 06:11:58 -0700288
Neale Ranns54c6dc42018-01-17 10:29:10 -0800289 if broadcast:
290 self.assertEqual(bootp.flags, 0x8000)
291 else:
292 self.assertEqual(bootp.flags, 0x0000)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700293
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200294 def verify_relayed_dhcp_discover(
295 self,
296 pkt,
297 intf,
298 src_intf=None,
299 fib_id=0,
300 oui=0,
301 vpn_id="",
302 dst_mac=None,
303 dst_ip=None,
304 ):
Neale Ranns3466c302017-02-16 07:45:03 -0800305 if not dst_mac:
306 dst_mac = intf.remote_mac
307 if not dst_ip:
308 dst_ip = intf.remote_ip4
309
Neale Rannsfca0c242017-01-13 07:57:46 -0800310 ether = pkt[Ether]
Neale Ranns3466c302017-02-16 07:45:03 -0800311 self.assertEqual(ether.dst, dst_mac)
Neale Rannsfca0c242017-01-13 07:57:46 -0800312 self.assertEqual(ether.src, intf.local_mac)
313
314 ip = pkt[IP]
Neale Ranns3466c302017-02-16 07:45:03 -0800315 self.assertEqual(ip.dst, dst_ip)
Neale Rannsfca0c242017-01-13 07:57:46 -0800316 self.assertEqual(ip.src, intf.local_ip4)
317
318 udp = pkt[UDP]
319 self.assertEqual(udp.dport, DHCP4_SERVER_PORT)
320 self.assertEqual(udp.sport, DHCP4_CLIENT_PORT)
321
322 dhcp = pkt[DHCP]
323
324 is_discover = False
325 for o in dhcp.options:
Jakub Grajciar103ba6b2019-10-01 11:30:56 +0200326 if isinstance(o, tuple):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200327 if o[0] == "message-type" and DHCPTypes[o[1]] == "discover":
Neale Rannsfca0c242017-01-13 07:57:46 -0800328 is_discover = True
329 self.assertTrue(is_discover)
330
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200331 data = self.validate_relay_options(
332 pkt, src_intf, src_intf.local_ip4, vpn_id, fib_id, oui
333 )
Neale Ranns20a175a2017-02-14 07:28:41 -0800334 return data
Neale Rannsfca0c242017-01-13 07:57:46 -0800335
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200336 def verify_dhcp6_solicit(
337 self,
338 pkt,
339 intf,
340 peer_ip,
341 peer_mac,
342 vpn_id="",
343 fib_id=0,
344 oui=0,
345 dst_mac=None,
346 dst_ip=None,
347 ):
Neale Ranns3466c302017-02-16 07:45:03 -0800348 if not dst_mac:
349 dst_mac = intf.remote_mac
350 if not dst_ip:
351 dst_ip = in6_ptop(intf.remote_ip6)
352
Neale Rannsfca0c242017-01-13 07:57:46 -0800353 ether = pkt[Ether]
Neale Ranns3466c302017-02-16 07:45:03 -0800354 self.assertEqual(ether.dst, dst_mac)
Neale Rannsfca0c242017-01-13 07:57:46 -0800355 self.assertEqual(ether.src, intf.local_mac)
356
357 ip = pkt[IPv6]
Neale Ranns3466c302017-02-16 07:45:03 -0800358 self.assertEqual(in6_ptop(ip.dst), dst_ip)
Neale Rannsfca0c242017-01-13 07:57:46 -0800359 self.assertEqual(in6_ptop(ip.src), in6_ptop(intf.local_ip6))
360
361 udp = pkt[UDP]
362 self.assertEqual(udp.dport, DHCP6_CLIENT_PORT)
363 self.assertEqual(udp.sport, DHCP6_SERVER_PORT)
364
365 relay = pkt[DHCP6_RelayForward]
366 self.assertEqual(in6_ptop(relay.peeraddr), in6_ptop(peer_ip))
367 oid = pkt[DHCP6OptIfaceId]
368 cll = pkt[DHCP6OptClientLinkLayerAddr]
369 self.assertEqual(cll.optlen, 8)
370 self.assertEqual(cll.lltype, 1)
371 self.assertEqual(cll.clladdr, peer_mac)
372
John Lo70bfcaf2017-11-14 13:19:26 -0500373 id_len = len(vpn_id)
374
Neale Ranns20a175a2017-02-14 07:28:41 -0800375 if fib_id != 0:
John Lo70bfcaf2017-11-14 13:19:26 -0500376 self.assertEqual(id_len, 0)
Neale Ranns20a175a2017-02-14 07:28:41 -0800377 vss = pkt[DHCP6OptVSS]
378 self.assertEqual(vss.optlen, 8)
379 self.assertEqual(vss.type, 1)
380 # the OUI and FIB-id are really 3 and 4 bytes resp.
381 # but the tested range is small
Ole Troan12966a72019-10-18 14:33:54 +0200382 self.assertEqual(six.byte2int(vss.data[0:1]), 0)
383 self.assertEqual(six.byte2int(vss.data[1:2]), 0)
384 self.assertEqual(six.byte2int(vss.data[2:3]), oui)
385 self.assertEqual(six.byte2int(vss.data[3:4]), 0)
386 self.assertEqual(six.byte2int(vss.data[4:5]), 0)
387 self.assertEqual(six.byte2int(vss.data[5:6]), 0)
388 self.assertEqual(six.byte2int(vss.data[6:7]), fib_id)
Neale Rannsfca0c242017-01-13 07:57:46 -0800389
John Lo70bfcaf2017-11-14 13:19:26 -0500390 if id_len > 0:
391 self.assertEqual(oui, 0)
392 vss = pkt[DHCP6OptVSS]
Jakub Grajciar103ba6b2019-10-01 11:30:56 +0200393 self.assertEqual(vss.optlen, id_len + 1)
John Lo70bfcaf2017-11-14 13:19:26 -0500394 self.assertEqual(vss.type, 0)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200395 self.assertEqual(vss.data[0:id_len].decode("ascii"), vpn_id)
John Lo70bfcaf2017-11-14 13:19:26 -0500396
Neale Rannsfca0c242017-01-13 07:57:46 -0800397 # the relay message should be an encoded Solicit
398 msg = pkt[DHCP6OptRelayMsg]
399 sol = DHCP6_Solicit()
Ole Troan12966a72019-10-18 14:33:54 +0200400 self.assertEqual(msg.optlen, len(sol))
401 self.assertEqual(sol, msg[1])
Neale Rannsfca0c242017-01-13 07:57:46 -0800402
403 def verify_dhcp6_advert(self, pkt, intf, peer):
404 ether = pkt[Ether]
405 self.assertEqual(ether.dst, "ff:ff:ff:ff:ff:ff")
406 self.assertEqual(ether.src, intf.local_mac)
407
408 ip = pkt[IPv6]
409 self.assertEqual(in6_ptop(ip.dst), in6_ptop(peer))
410 self.assertEqual(in6_ptop(ip.src), in6_ptop(intf.local_ip6))
411
412 udp = pkt[UDP]
413 self.assertEqual(udp.dport, DHCP6_SERVER_PORT)
414 self.assertEqual(udp.sport, DHCP6_CLIENT_PORT)
415
416 # not sure why this is not decoding
417 # adv = pkt[DHCP6_Advertise]
418
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200419 def wait_for_no_route(self, address, length, n_tries=50, s_time=1):
420 while n_tries:
Neale Rannsf6e9b012019-01-25 06:37:15 -0800421 if not find_route(self, address, length):
422 return True
423 n_tries = n_tries - 1
424 self.sleep(s_time)
425
426 return False
427
Neale Rannsfca0c242017-01-13 07:57:46 -0800428 def test_dhcp_proxy(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200429 """DHCPv4 Proxy"""
Neale Rannsfca0c242017-01-13 07:57:46 -0800430
431 #
432 # Verify no response to DHCP request without DHCP config
433 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200434 p_disc_vrf0 = (
435 Ether(dst="ff:ff:ff:ff:ff:ff", src=self.pg3.remote_mac)
436 / IP(src="0.0.0.0", dst="255.255.255.255")
437 / UDP(sport=DHCP4_CLIENT_PORT, dport=DHCP4_SERVER_PORT)
438 / BOOTP(op=1)
439 / DHCP(options=[("message-type", "discover"), ("end")])
440 )
Neale Rannsfca0c242017-01-13 07:57:46 -0800441 pkts_disc_vrf0 = [p_disc_vrf0]
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200442 p_disc_vrf1 = (
443 Ether(dst="ff:ff:ff:ff:ff:ff", src=self.pg4.remote_mac)
444 / IP(src="0.0.0.0", dst="255.255.255.255")
445 / UDP(sport=DHCP4_CLIENT_PORT, dport=DHCP4_SERVER_PORT)
446 / BOOTP(op=1)
447 / DHCP(options=[("message-type", "discover"), ("end")])
448 )
John Lo70bfcaf2017-11-14 13:19:26 -0500449 pkts_disc_vrf1 = [p_disc_vrf1]
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200450 p_disc_vrf2 = (
451 Ether(dst="ff:ff:ff:ff:ff:ff", src=self.pg5.remote_mac)
452 / IP(src="0.0.0.0", dst="255.255.255.255")
453 / UDP(sport=DHCP4_CLIENT_PORT, dport=DHCP4_SERVER_PORT)
454 / BOOTP(op=1)
455 / DHCP(options=[("message-type", "discover"), ("end")])
456 )
John Lo70bfcaf2017-11-14 13:19:26 -0500457 pkts_disc_vrf2 = [p_disc_vrf2]
Neale Rannsfca0c242017-01-13 07:57:46 -0800458
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200459 self.send_and_assert_no_replies(
460 self.pg3, pkts_disc_vrf0, "DHCP with no configuration"
461 )
462 self.send_and_assert_no_replies(
463 self.pg4, pkts_disc_vrf1, "DHCP with no configuration"
464 )
465 self.send_and_assert_no_replies(
466 self.pg5, pkts_disc_vrf2, "DHCP with no configuration"
467 )
Neale Rannsfca0c242017-01-13 07:57:46 -0800468
469 #
470 # Enable DHCP proxy in VRF 0
471 #
Jakub Grajciar103ba6b2019-10-01 11:30:56 +0200472 server_addr = self.pg0.remote_ip4
473 src_addr = self.pg0.local_ip4
Neale Rannsfca0c242017-01-13 07:57:46 -0800474
Jakub Grajciar103ba6b2019-10-01 11:30:56 +0200475 Proxy = VppDHCPProxy(self, server_addr, src_addr, rx_vrf_id=0)
476 Proxy.add_vpp_config()
Neale Rannsfca0c242017-01-13 07:57:46 -0800477
478 #
Neale Ranns20a175a2017-02-14 07:28:41 -0800479 # Discover packets from the client are dropped because there is no
480 # IP address configured on the client facing interface
Neale Rannsfca0c242017-01-13 07:57:46 -0800481 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200482 self.send_and_assert_no_replies(
483 self.pg3, pkts_disc_vrf0, "Discover DHCP no relay address"
484 )
Neale Rannsfca0c242017-01-13 07:57:46 -0800485
486 #
487 # Inject a response from the server
Neale Ranns20a175a2017-02-14 07:28:41 -0800488 # dropped, because there is no IP addrees on the
Neale Ranns2dd68522017-02-16 03:38:59 -0800489 # client interfce to fill in the option.
Neale Rannsfca0c242017-01-13 07:57:46 -0800490 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200491 p = (
492 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
493 / IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4)
494 / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT)
495 / BOOTP(op=1)
496 / DHCP(options=[("message-type", "offer"), ("end")])
497 )
Neale Rannsfca0c242017-01-13 07:57:46 -0800498 pkts = [p]
499
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200500 self.send_and_assert_no_replies(self.pg3, pkts, "Offer DHCP no relay address")
Neale Rannsfca0c242017-01-13 07:57:46 -0800501
502 #
503 # configure an IP address on the client facing interface
504 #
John Lo70bfcaf2017-11-14 13:19:26 -0500505 self.pg3.config_ip4()
Neale Rannsfca0c242017-01-13 07:57:46 -0800506
507 #
508 # Try again with a discover packet
509 # Rx'd packet should be to the server address and from the configured
510 # source address
511 # UDP source ports are unchanged
512 # we've no option 82 config so that should be absent
513 #
John Lo70bfcaf2017-11-14 13:19:26 -0500514 self.pg3.add_stream(pkts_disc_vrf0)
Neale Rannsfca0c242017-01-13 07:57:46 -0800515 self.pg_enable_capture(self.pg_interfaces)
516 self.pg_start()
517
518 rx = self.pg0.get_capture(1)
519 rx = rx[0]
520
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200521 option_82 = self.verify_relayed_dhcp_discover(rx, self.pg0, src_intf=self.pg3)
Neale Rannsfca0c242017-01-13 07:57:46 -0800522
523 #
524 # Create an DHCP offer reply from the server with a correctly formatted
525 # option 82. i.e. send back what we just captured
526 # The offer, sent mcast to the client, still has option 82.
527 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200528 p = (
529 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
530 / IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4)
531 / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT)
532 / BOOTP(op=1)
533 / DHCP(
534 options=[
535 ("message-type", "offer"),
536 ("relay_agent_Information", option_82),
537 ("end"),
538 ]
539 )
540 )
Neale Rannsfca0c242017-01-13 07:57:46 -0800541 pkts = [p]
542
543 self.pg0.add_stream(pkts)
544 self.pg_enable_capture(self.pg_interfaces)
545 self.pg_start()
546
John Lo70bfcaf2017-11-14 13:19:26 -0500547 rx = self.pg3.get_capture(1)
Neale Rannsfca0c242017-01-13 07:57:46 -0800548 rx = rx[0]
549
John Lo70bfcaf2017-11-14 13:19:26 -0500550 self.verify_dhcp_offer(rx, self.pg3)
Neale Rannsfca0c242017-01-13 07:57:46 -0800551
552 #
553 # Bogus Option 82:
554 #
555 # 1. not our IP address = not checked by VPP? so offer is replayed
556 # to client
Paul Vinciguerraa7427ec2019-03-10 10:04:23 -0700557 bad_ip = option_82[0:8] + scapy.compat.chb(33) + option_82[9:]
Neale Rannsfca0c242017-01-13 07:57:46 -0800558
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200559 p = (
560 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
561 / IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4)
562 / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT)
563 / BOOTP(op=1)
564 / DHCP(
565 options=[
566 ("message-type", "offer"),
567 ("relay_agent_Information", bad_ip),
568 ("end"),
569 ]
570 )
571 )
Neale Rannsfca0c242017-01-13 07:57:46 -0800572 pkts = [p]
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200573 self.send_and_assert_no_replies(
574 self.pg0, pkts, "DHCP offer option 82 bad address"
575 )
Neale Rannsfca0c242017-01-13 07:57:46 -0800576
577 # 2. Not a sw_if_index VPP knows
Paul Vinciguerraa7427ec2019-03-10 10:04:23 -0700578 bad_if_index = option_82[0:2] + scapy.compat.chb(33) + option_82[3:]
Neale Rannsfca0c242017-01-13 07:57:46 -0800579
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200580 p = (
581 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
582 / IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4)
583 / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT)
584 / BOOTP(op=1)
585 / DHCP(
586 options=[
587 ("message-type", "offer"),
588 ("relay_agent_Information", bad_if_index),
589 ("end"),
590 ]
591 )
592 )
Neale Rannsfca0c242017-01-13 07:57:46 -0800593 pkts = [p]
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200594 self.send_and_assert_no_replies(
595 self.pg0, pkts, "DHCP offer option 82 bad if index"
596 )
Neale Rannsfca0c242017-01-13 07:57:46 -0800597
598 #
599 # Send a DHCP request in VRF 1. should be dropped.
600 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200601 self.send_and_assert_no_replies(
602 self.pg4, pkts_disc_vrf1, "DHCP with no configuration VRF 1"
603 )
Neale Rannsfca0c242017-01-13 07:57:46 -0800604
605 #
606 # Delete the DHCP config in VRF 0
607 # Should now drop requests.
608 #
Jakub Grajciar103ba6b2019-10-01 11:30:56 +0200609 Proxy.remove_vpp_config()
Neale Rannsfca0c242017-01-13 07:57:46 -0800610
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200611 self.send_and_assert_no_replies(
612 self.pg3, pkts_disc_vrf0, "DHCP config removed VRF 0"
613 )
614 self.send_and_assert_no_replies(
615 self.pg4, pkts_disc_vrf1, "DHCP config removed VRF 1"
616 )
Neale Rannsfca0c242017-01-13 07:57:46 -0800617
618 #
John Lo70bfcaf2017-11-14 13:19:26 -0500619 # Add DHCP config for VRF 1 & 2
Neale Rannsfca0c242017-01-13 07:57:46 -0800620 #
Jakub Grajciar103ba6b2019-10-01 11:30:56 +0200621 server_addr1 = self.pg1.remote_ip4
622 src_addr1 = self.pg1.local_ip4
623 Proxy1 = VppDHCPProxy(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200624 self, server_addr1, src_addr1, rx_vrf_id=1, server_vrf_id=1
625 )
Jakub Grajciar103ba6b2019-10-01 11:30:56 +0200626 Proxy1.add_vpp_config()
627
628 server_addr2 = self.pg2.remote_ip4
629 src_addr2 = self.pg2.local_ip4
630 Proxy2 = VppDHCPProxy(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200631 self, server_addr2, src_addr2, rx_vrf_id=2, server_vrf_id=2
632 )
Jakub Grajciar103ba6b2019-10-01 11:30:56 +0200633 Proxy2.add_vpp_config()
Neale Rannsfca0c242017-01-13 07:57:46 -0800634
635 #
John Lo70bfcaf2017-11-14 13:19:26 -0500636 # Confim DHCP requests ok in VRF 1 & 2.
Neale Rannsfca0c242017-01-13 07:57:46 -0800637 # - dropped on IP config on client interface
638 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200639 self.send_and_assert_no_replies(
640 self.pg4, pkts_disc_vrf1, "DHCP config removed VRF 1"
641 )
642 self.send_and_assert_no_replies(
643 self.pg5, pkts_disc_vrf2, "DHCP config removed VRF 2"
644 )
Neale Rannsfca0c242017-01-13 07:57:46 -0800645
646 #
647 # configure an IP address on the client facing interface
648 #
John Lo70bfcaf2017-11-14 13:19:26 -0500649 self.pg4.config_ip4()
650 self.pg4.add_stream(pkts_disc_vrf1)
Neale Rannsfca0c242017-01-13 07:57:46 -0800651 self.pg_enable_capture(self.pg_interfaces)
652 self.pg_start()
Neale Rannsfca0c242017-01-13 07:57:46 -0800653 rx = self.pg1.get_capture(1)
654 rx = rx[0]
John Lo70bfcaf2017-11-14 13:19:26 -0500655 self.verify_relayed_dhcp_discover(rx, self.pg1, src_intf=self.pg4)
656
657 self.pg5.config_ip4()
658 self.pg5.add_stream(pkts_disc_vrf2)
659 self.pg_enable_capture(self.pg_interfaces)
660 self.pg_start()
661 rx = self.pg2.get_capture(1)
662 rx = rx[0]
663 self.verify_relayed_dhcp_discover(rx, self.pg2, src_intf=self.pg5)
Neale Rannsfca0c242017-01-13 07:57:46 -0800664
665 #
Neale Ranns20a175a2017-02-14 07:28:41 -0800666 # Add VSS config
John Lo70bfcaf2017-11-14 13:19:26 -0500667 # table=1, vss_type=1, vpn_index=1, oui=4
668 # table=2, vss_type=0, vpn_id = "ip4-table-2"
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200669 self.vapi.dhcp_proxy_set_vss(tbl_id=1, vss_type=1, vpn_index=1, oui=4, is_add=1)
670 self.vapi.dhcp_proxy_set_vss(
671 tbl_id=2, vss_type=0, vpn_ascii_id="ip4-table-2", is_add=1
672 )
Neale Ranns20a175a2017-02-14 07:28:41 -0800673
John Lo70bfcaf2017-11-14 13:19:26 -0500674 self.pg4.add_stream(pkts_disc_vrf1)
Neale Ranns20a175a2017-02-14 07:28:41 -0800675 self.pg_enable_capture(self.pg_interfaces)
676 self.pg_start()
677
678 rx = self.pg1.get_capture(1)
679 rx = rx[0]
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200680 self.verify_relayed_dhcp_discover(
681 rx, self.pg1, src_intf=self.pg4, fib_id=1, oui=4
682 )
Neale Ranns20a175a2017-02-14 07:28:41 -0800683
John Lo70bfcaf2017-11-14 13:19:26 -0500684 self.pg5.add_stream(pkts_disc_vrf2)
685 self.pg_enable_capture(self.pg_interfaces)
686 self.pg_start()
687
688 rx = self.pg2.get_capture(1)
689 rx = rx[0]
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200690 self.verify_relayed_dhcp_discover(
691 rx, self.pg2, src_intf=self.pg5, vpn_id="ip4-table-2"
692 )
John Lo70bfcaf2017-11-14 13:19:26 -0500693
Neale Ranns20a175a2017-02-14 07:28:41 -0800694 #
Neale Ranns3466c302017-02-16 07:45:03 -0800695 # Add a second DHCP server in VRF 1
696 # expect clients messages to be relay to both configured servers
697 #
698 self.pg1.generate_remote_hosts(2)
Jakub Grajciar103ba6b2019-10-01 11:30:56 +0200699 server_addr12 = self.pg1.remote_hosts[1].ip4
Neale Ranns3466c302017-02-16 07:45:03 -0800700
Jakub Grajciar103ba6b2019-10-01 11:30:56 +0200701 Proxy12 = VppDHCPProxy(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200702 self, server_addr12, src_addr, rx_vrf_id=1, server_vrf_id=1
703 )
Jakub Grajciar103ba6b2019-10-01 11:30:56 +0200704 Proxy12.add_vpp_config()
Neale Ranns3466c302017-02-16 07:45:03 -0800705
706 #
707 # We'll need an ARP entry for the server to send it packets
708 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200709 arp_entry = VppNeighbor(
710 self,
711 self.pg1.sw_if_index,
712 self.pg1.remote_hosts[1].mac,
713 self.pg1.remote_hosts[1].ip4,
714 )
Neale Ranns3466c302017-02-16 07:45:03 -0800715 arp_entry.add_vpp_config()
716
717 #
718 # Send a discover from the client. expect two relayed messages
719 # The frist packet is sent to the second server
720 # We're not enforcing that here, it's just the way it is.
721 #
John Lo70bfcaf2017-11-14 13:19:26 -0500722 self.pg4.add_stream(pkts_disc_vrf1)
Neale Ranns3466c302017-02-16 07:45:03 -0800723 self.pg_enable_capture(self.pg_interfaces)
724 self.pg_start()
725
726 rx = self.pg1.get_capture(2)
727
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700728 option_82 = self.verify_relayed_dhcp_discover(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200729 rx[0],
730 self.pg1,
John Lo70bfcaf2017-11-14 13:19:26 -0500731 src_intf=self.pg4,
Neale Ranns3466c302017-02-16 07:45:03 -0800732 dst_mac=self.pg1.remote_hosts[1].mac,
733 dst_ip=self.pg1.remote_hosts[1].ip4,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200734 fib_id=1,
735 oui=4,
736 )
737 self.verify_relayed_dhcp_discover(
738 rx[1], self.pg1, src_intf=self.pg4, fib_id=1, oui=4
739 )
Neale Ranns3466c302017-02-16 07:45:03 -0800740
741 #
742 # Send both packets back. Client gets both.
743 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200744 p1 = (
745 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
746 / IP(src=self.pg1.remote_ip4, dst=self.pg1.local_ip4)
747 / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT)
748 / BOOTP(op=1)
749 / DHCP(
750 options=[
751 ("message-type", "offer"),
752 ("relay_agent_Information", option_82),
753 ("end"),
754 ]
755 )
756 )
757 p2 = (
758 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
759 / IP(src=self.pg1.remote_hosts[1].ip4, dst=self.pg1.local_ip4)
760 / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT)
761 / BOOTP(op=1)
762 / DHCP(
763 options=[
764 ("message-type", "offer"),
765 ("relay_agent_Information", option_82),
766 ("end"),
767 ]
768 )
769 )
Neale Ranns3466c302017-02-16 07:45:03 -0800770 pkts = [p1, p2]
771
772 self.pg1.add_stream(pkts)
773 self.pg_enable_capture(self.pg_interfaces)
774 self.pg_start()
775
John Lo70bfcaf2017-11-14 13:19:26 -0500776 rx = self.pg4.get_capture(2)
Neale Ranns3466c302017-02-16 07:45:03 -0800777
John Lo70bfcaf2017-11-14 13:19:26 -0500778 self.verify_dhcp_offer(rx[0], self.pg4, fib_id=1, oui=4)
779 self.verify_dhcp_offer(rx[1], self.pg4, fib_id=1, oui=4)
Neale Ranns3466c302017-02-16 07:45:03 -0800780
781 #
782 # Ensure offers from non-servers are dropeed
783 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200784 p2 = (
785 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
786 / IP(src="8.8.8.8", dst=self.pg1.local_ip4)
787 / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT)
788 / BOOTP(op=1)
789 / DHCP(
790 options=[
791 ("message-type", "offer"),
792 ("relay_agent_Information", option_82),
793 ("end"),
794 ]
795 )
796 )
797 self.send_and_assert_no_replies(self.pg1, p2, "DHCP offer from non-server")
Neale Ranns3466c302017-02-16 07:45:03 -0800798
799 #
800 # Ensure only the discover is sent to multiple servers
801 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200802 p_req_vrf1 = (
803 Ether(dst="ff:ff:ff:ff:ff:ff", src=self.pg4.remote_mac)
804 / IP(src="0.0.0.0", dst="255.255.255.255")
805 / UDP(sport=DHCP4_CLIENT_PORT, dport=DHCP4_SERVER_PORT)
806 / BOOTP(op=1)
807 / DHCP(options=[("message-type", "request"), ("end")])
808 )
Neale Ranns3466c302017-02-16 07:45:03 -0800809
John Lo70bfcaf2017-11-14 13:19:26 -0500810 self.pg4.add_stream(p_req_vrf1)
Neale Ranns3466c302017-02-16 07:45:03 -0800811 self.pg_enable_capture(self.pg_interfaces)
812 self.pg_start()
813
814 rx = self.pg1.get_capture(1)
815
816 #
817 # Remove the second DHCP server
818 #
Jakub Grajciar103ba6b2019-10-01 11:30:56 +0200819 Proxy12.remove_vpp_config()
Neale Ranns3466c302017-02-16 07:45:03 -0800820
821 #
822 # Test we can still relay with the first
823 #
John Lo70bfcaf2017-11-14 13:19:26 -0500824 self.pg4.add_stream(pkts_disc_vrf1)
Neale Ranns3466c302017-02-16 07:45:03 -0800825 self.pg_enable_capture(self.pg_interfaces)
826 self.pg_start()
827
828 rx = self.pg1.get_capture(1)
829 rx = rx[0]
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200830 self.verify_relayed_dhcp_discover(
831 rx, self.pg1, src_intf=self.pg4, fib_id=1, oui=4
832 )
Neale Ranns3466c302017-02-16 07:45:03 -0800833
834 #
Neale Ranns20a175a2017-02-14 07:28:41 -0800835 # Remove the VSS config
836 # relayed DHCP has default vlaues in the option.
837 #
Neale Ranns02bfd642019-10-07 00:39:28 -0700838 self.vapi.dhcp_proxy_set_vss(tbl_id=1, is_add=0)
839 self.vapi.dhcp_proxy_set_vss(tbl_id=2, is_add=0)
Neale Ranns20a175a2017-02-14 07:28:41 -0800840
John Lo70bfcaf2017-11-14 13:19:26 -0500841 self.pg4.add_stream(pkts_disc_vrf1)
Neale Ranns20a175a2017-02-14 07:28:41 -0800842 self.pg_enable_capture(self.pg_interfaces)
843 self.pg_start()
844
845 rx = self.pg1.get_capture(1)
846 rx = rx[0]
John Lo70bfcaf2017-11-14 13:19:26 -0500847 self.verify_relayed_dhcp_discover(rx, self.pg1, src_intf=self.pg4)
Neale Ranns20a175a2017-02-14 07:28:41 -0800848
849 #
Neale Rannsfca0c242017-01-13 07:57:46 -0800850 # remove DHCP config to cleanup
851 #
Jakub Grajciar103ba6b2019-10-01 11:30:56 +0200852 Proxy1.remove_vpp_config()
853 Proxy2.remove_vpp_config()
Neale Rannsfca0c242017-01-13 07:57:46 -0800854
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200855 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf0, "DHCP cleanup VRF 0")
856 self.send_and_assert_no_replies(self.pg4, pkts_disc_vrf1, "DHCP cleanup VRF 1")
857 self.send_and_assert_no_replies(self.pg5, pkts_disc_vrf2, "DHCP cleanup VRF 2")
John Lo70bfcaf2017-11-14 13:19:26 -0500858
Neale Ranns15002542017-09-10 04:39:11 -0700859 self.pg3.unconfig_ip4()
John Lo70bfcaf2017-11-14 13:19:26 -0500860 self.pg4.unconfig_ip4()
861 self.pg5.unconfig_ip4()
Neale Rannsfca0c242017-01-13 07:57:46 -0800862
863 def test_dhcp6_proxy(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200864 """DHCPv6 Proxy"""
Neale Rannsfca0c242017-01-13 07:57:46 -0800865 #
866 # Verify no response to DHCP request without DHCP config
867 #
868 dhcp_solicit_dst = "ff02::1:2"
John Lo70bfcaf2017-11-14 13:19:26 -0500869 dhcp_solicit_src_vrf0 = mk_ll_addr(self.pg3.remote_mac)
870 dhcp_solicit_src_vrf1 = mk_ll_addr(self.pg4.remote_mac)
871 dhcp_solicit_src_vrf2 = mk_ll_addr(self.pg5.remote_mac)
Jakub Grajciar103ba6b2019-10-01 11:30:56 +0200872 server_addr_vrf0 = self.pg0.remote_ip6
873 src_addr_vrf0 = self.pg0.local_ip6
874 server_addr_vrf1 = self.pg1.remote_ip6
875 src_addr_vrf1 = self.pg1.local_ip6
876 server_addr_vrf2 = self.pg2.remote_ip6
877 src_addr_vrf2 = self.pg2.local_ip6
Neale Rannsfca0c242017-01-13 07:57:46 -0800878
Neale Rannsfca0c242017-01-13 07:57:46 -0800879 dmac = in6_getnsmac(inet_pton(socket.AF_INET6, dhcp_solicit_dst))
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200880 p_solicit_vrf0 = (
881 Ether(dst=dmac, src=self.pg3.remote_mac)
882 / IPv6(src=dhcp_solicit_src_vrf0, dst=dhcp_solicit_dst)
883 / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_CLIENT_PORT)
884 / DHCP6_Solicit()
885 )
886 p_solicit_vrf1 = (
887 Ether(dst=dmac, src=self.pg4.remote_mac)
888 / IPv6(src=dhcp_solicit_src_vrf1, dst=dhcp_solicit_dst)
889 / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_CLIENT_PORT)
890 / DHCP6_Solicit()
891 )
892 p_solicit_vrf2 = (
893 Ether(dst=dmac, src=self.pg5.remote_mac)
894 / IPv6(src=dhcp_solicit_src_vrf2, dst=dhcp_solicit_dst)
895 / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_CLIENT_PORT)
896 / DHCP6_Solicit()
897 )
Neale Rannsfca0c242017-01-13 07:57:46 -0800898
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200899 self.send_and_assert_no_replies(
900 self.pg3, p_solicit_vrf0, "DHCP with no configuration"
901 )
902 self.send_and_assert_no_replies(
903 self.pg4, p_solicit_vrf1, "DHCP with no configuration"
904 )
905 self.send_and_assert_no_replies(
906 self.pg5, p_solicit_vrf2, "DHCP with no configuration"
907 )
Neale Rannsfca0c242017-01-13 07:57:46 -0800908
909 #
910 # DHCPv6 config in VRF 0.
911 # Packets still dropped because the client facing interface has no
912 # IPv6 config
913 #
Jakub Grajciar103ba6b2019-10-01 11:30:56 +0200914 Proxy = VppDHCPProxy(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200915 self, server_addr_vrf0, src_addr_vrf0, rx_vrf_id=0, server_vrf_id=0
916 )
Jakub Grajciar103ba6b2019-10-01 11:30:56 +0200917 Proxy.add_vpp_config()
Neale Rannsfca0c242017-01-13 07:57:46 -0800918
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200919 self.send_and_assert_no_replies(
920 self.pg3, p_solicit_vrf0, "DHCP with no configuration"
921 )
922 self.send_and_assert_no_replies(
923 self.pg4, p_solicit_vrf1, "DHCP with no configuration"
924 )
Neale Rannsfca0c242017-01-13 07:57:46 -0800925
926 #
927 # configure an IP address on the client facing interface
928 #
John Lo70bfcaf2017-11-14 13:19:26 -0500929 self.pg3.config_ip6()
Neale Rannsfca0c242017-01-13 07:57:46 -0800930
931 #
932 # Now the DHCP requests are relayed to the server
933 #
John Lo70bfcaf2017-11-14 13:19:26 -0500934 self.pg3.add_stream(p_solicit_vrf0)
Neale Rannsfca0c242017-01-13 07:57:46 -0800935 self.pg_enable_capture(self.pg_interfaces)
936 self.pg_start()
937
938 rx = self.pg0.get_capture(1)
Neale Ranns3466c302017-02-16 07:45:03 -0800939
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200940 self.verify_dhcp6_solicit(
941 rx[0], self.pg0, dhcp_solicit_src_vrf0, self.pg3.remote_mac
942 )
Neale Rannsfca0c242017-01-13 07:57:46 -0800943
944 #
945 # Exception cases for rejected relay responses
946 #
947
948 # 1 - not a relay reply
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200949 p_adv_vrf0 = (
950 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
951 / IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6)
952 / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT)
953 / DHCP6_Advertise()
954 )
955 self.send_and_assert_no_replies(self.pg3, p_adv_vrf0, "DHCP6 not a relay reply")
Neale Rannsfca0c242017-01-13 07:57:46 -0800956
957 # 2 - no relay message option
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200958 p_adv_vrf0 = (
959 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
960 / IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6)
961 / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT)
962 / DHCP6_RelayReply()
963 / DHCP6_Advertise()
964 )
965 self.send_and_assert_no_replies(
966 self.pg3, p_adv_vrf0, "DHCP not a relay message"
967 )
Neale Rannsfca0c242017-01-13 07:57:46 -0800968
969 # 3 - no circuit ID
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200970 p_adv_vrf0 = (
971 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
972 / IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6)
973 / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT)
974 / DHCP6_RelayReply()
975 / DHCP6OptRelayMsg(optlen=0)
976 / DHCP6_Advertise()
977 )
978 self.send_and_assert_no_replies(self.pg3, p_adv_vrf0, "DHCP6 no circuit ID")
Neale Rannsfca0c242017-01-13 07:57:46 -0800979 # 4 - wrong circuit ID
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200980 p_adv_vrf0 = (
981 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
982 / IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6)
983 / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT)
984 / DHCP6_RelayReply()
985 / DHCP6OptIfaceId(optlen=4, ifaceid="\x00\x00\x00\x05")
986 / DHCP6OptRelayMsg(optlen=0)
987 / DHCP6_Advertise()
988 )
989 self.send_and_assert_no_replies(self.pg3, p_adv_vrf0, "DHCP6 wrong circuit ID")
Neale Rannsfca0c242017-01-13 07:57:46 -0800990
991 #
992 # Send the relay response (the advertisement)
993 # - no peer address
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200994 p_adv_vrf0 = (
995 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
996 / IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6)
997 / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT)
998 / DHCP6_RelayReply()
999 / DHCP6OptIfaceId(optlen=4, ifaceid="\x00\x00\x00\x04")
1000 / DHCP6OptRelayMsg(optlen=0)
1001 / DHCP6_Advertise(trid=1)
1002 / DHCP6OptStatusCode(statuscode=0)
1003 )
Neale Rannsfca0c242017-01-13 07:57:46 -08001004 pkts_adv_vrf0 = [p_adv_vrf0]
1005
1006 self.pg0.add_stream(pkts_adv_vrf0)
1007 self.pg_enable_capture(self.pg_interfaces)
1008 self.pg_start()
1009
John Lo70bfcaf2017-11-14 13:19:26 -05001010 rx = self.pg3.get_capture(1)
Neale Ranns3466c302017-02-16 07:45:03 -08001011
John Lo70bfcaf2017-11-14 13:19:26 -05001012 self.verify_dhcp6_advert(rx[0], self.pg3, "::")
Neale Rannsfca0c242017-01-13 07:57:46 -08001013
1014 #
1015 # Send the relay response (the advertisement)
1016 # - with peer address
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001017 p_adv_vrf0 = (
1018 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
1019 / IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6)
1020 / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT)
1021 / DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf0)
1022 / DHCP6OptIfaceId(optlen=4, ifaceid="\x00\x00\x00\x04")
1023 / DHCP6OptRelayMsg(optlen=0)
1024 / DHCP6_Advertise(trid=1)
1025 / DHCP6OptStatusCode(statuscode=0)
1026 )
Neale Rannsfca0c242017-01-13 07:57:46 -08001027 pkts_adv_vrf0 = [p_adv_vrf0]
1028
1029 self.pg0.add_stream(pkts_adv_vrf0)
1030 self.pg_enable_capture(self.pg_interfaces)
1031 self.pg_start()
1032
John Lo70bfcaf2017-11-14 13:19:26 -05001033 rx = self.pg3.get_capture(1)
Neale Ranns3466c302017-02-16 07:45:03 -08001034
John Lo70bfcaf2017-11-14 13:19:26 -05001035 self.verify_dhcp6_advert(rx[0], self.pg3, dhcp_solicit_src_vrf0)
Neale Rannsfca0c242017-01-13 07:57:46 -08001036
1037 #
John Lo70bfcaf2017-11-14 13:19:26 -05001038 # Add all the config for VRF 1 & 2
Neale Rannsfca0c242017-01-13 07:57:46 -08001039 #
Jakub Grajciar103ba6b2019-10-01 11:30:56 +02001040 Proxy1 = VppDHCPProxy(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001041 self, server_addr_vrf1, src_addr_vrf1, rx_vrf_id=1, server_vrf_id=1
1042 )
Jakub Grajciar103ba6b2019-10-01 11:30:56 +02001043 Proxy1.add_vpp_config()
John Lo70bfcaf2017-11-14 13:19:26 -05001044 self.pg4.config_ip6()
1045
Jakub Grajciar103ba6b2019-10-01 11:30:56 +02001046 Proxy2 = VppDHCPProxy(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001047 self, server_addr_vrf2, src_addr_vrf2, rx_vrf_id=2, server_vrf_id=2
1048 )
Jakub Grajciar103ba6b2019-10-01 11:30:56 +02001049 Proxy2.add_vpp_config()
John Lo70bfcaf2017-11-14 13:19:26 -05001050 self.pg5.config_ip6()
Neale Rannsfca0c242017-01-13 07:57:46 -08001051
1052 #
1053 # VRF 1 solicit
1054 #
John Lo70bfcaf2017-11-14 13:19:26 -05001055 self.pg4.add_stream(p_solicit_vrf1)
Neale Rannsfca0c242017-01-13 07:57:46 -08001056 self.pg_enable_capture(self.pg_interfaces)
1057 self.pg_start()
1058
1059 rx = self.pg1.get_capture(1)
Neale Ranns3466c302017-02-16 07:45:03 -08001060
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001061 self.verify_dhcp6_solicit(
1062 rx[0], self.pg1, dhcp_solicit_src_vrf1, self.pg4.remote_mac
1063 )
John Lo70bfcaf2017-11-14 13:19:26 -05001064
1065 #
1066 # VRF 2 solicit
1067 #
1068 self.pg5.add_stream(p_solicit_vrf2)
1069 self.pg_enable_capture(self.pg_interfaces)
1070 self.pg_start()
1071
1072 rx = self.pg2.get_capture(1)
1073
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001074 self.verify_dhcp6_solicit(
1075 rx[0], self.pg2, dhcp_solicit_src_vrf2, self.pg5.remote_mac
1076 )
Neale Rannsfca0c242017-01-13 07:57:46 -08001077
1078 #
1079 # VRF 1 Advert
1080 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001081 p_adv_vrf1 = (
1082 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
1083 / IPv6(dst=self.pg1.local_ip6, src=self.pg1.remote_ip6)
1084 / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT)
1085 / DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1)
1086 / DHCP6OptIfaceId(optlen=4, ifaceid="\x00\x00\x00\x05")
1087 / DHCP6OptRelayMsg(optlen=0)
1088 / DHCP6_Advertise(trid=1)
1089 / DHCP6OptStatusCode(statuscode=0)
1090 )
Neale Rannsfca0c242017-01-13 07:57:46 -08001091 pkts_adv_vrf1 = [p_adv_vrf1]
1092
1093 self.pg1.add_stream(pkts_adv_vrf1)
1094 self.pg_enable_capture(self.pg_interfaces)
1095 self.pg_start()
1096
John Lo70bfcaf2017-11-14 13:19:26 -05001097 rx = self.pg4.get_capture(1)
Neale Ranns3466c302017-02-16 07:45:03 -08001098
John Lo70bfcaf2017-11-14 13:19:26 -05001099 self.verify_dhcp6_advert(rx[0], self.pg4, dhcp_solicit_src_vrf1)
Neale Rannsfca0c242017-01-13 07:57:46 -08001100
1101 #
1102 # Add VSS config
Jakub Grajciar103ba6b2019-10-01 11:30:56 +02001103 #
1104 self.vapi.dhcp_proxy_set_vss(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001105 tbl_id=1, vss_type=1, oui=4, vpn_index=1, is_ipv6=1, is_add=1
1106 )
Jakub Grajciar103ba6b2019-10-01 11:30:56 +02001107 self.vapi.dhcp_proxy_set_vss(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001108 tbl_id=2, vss_type=0, vpn_ascii_id="IPv6-table-2", is_ipv6=1, is_add=1
1109 )
Neale Rannsfca0c242017-01-13 07:57:46 -08001110
John Lo70bfcaf2017-11-14 13:19:26 -05001111 self.pg4.add_stream(p_solicit_vrf1)
Neale Rannsfca0c242017-01-13 07:57:46 -08001112 self.pg_enable_capture(self.pg_interfaces)
1113 self.pg_start()
1114
1115 rx = self.pg1.get_capture(1)
Neale Ranns3466c302017-02-16 07:45:03 -08001116
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001117 self.verify_dhcp6_solicit(
1118 rx[0], self.pg1, dhcp_solicit_src_vrf1, self.pg4.remote_mac, fib_id=1, oui=4
1119 )
Neale Rannsfca0c242017-01-13 07:57:46 -08001120
John Lo70bfcaf2017-11-14 13:19:26 -05001121 self.pg5.add_stream(p_solicit_vrf2)
1122 self.pg_enable_capture(self.pg_interfaces)
1123 self.pg_start()
1124
1125 rx = self.pg2.get_capture(1)
1126
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001127 self.verify_dhcp6_solicit(
1128 rx[0],
1129 self.pg2,
1130 dhcp_solicit_src_vrf2,
1131 self.pg5.remote_mac,
1132 vpn_id="IPv6-table-2",
1133 )
John Lo70bfcaf2017-11-14 13:19:26 -05001134
Neale Rannsfca0c242017-01-13 07:57:46 -08001135 #
1136 # Remove the VSS config
1137 # relayed DHCP has default vlaues in the option.
1138 #
Neale Ranns02bfd642019-10-07 00:39:28 -07001139 self.vapi.dhcp_proxy_set_vss(tbl_id=1, is_ipv6=1, is_add=0)
Neale Rannsfca0c242017-01-13 07:57:46 -08001140
John Lo70bfcaf2017-11-14 13:19:26 -05001141 self.pg4.add_stream(p_solicit_vrf1)
Neale Rannsfca0c242017-01-13 07:57:46 -08001142 self.pg_enable_capture(self.pg_interfaces)
1143 self.pg_start()
1144
1145 rx = self.pg1.get_capture(1)
Neale Ranns3466c302017-02-16 07:45:03 -08001146
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001147 self.verify_dhcp6_solicit(
1148 rx[0], self.pg1, dhcp_solicit_src_vrf1, self.pg4.remote_mac
1149 )
Neale Ranns3466c302017-02-16 07:45:03 -08001150
1151 #
1152 # Add a second DHCP server in VRF 1
1153 # expect clients messages to be relay to both configured servers
1154 #
1155 self.pg1.generate_remote_hosts(2)
Jakub Grajciar103ba6b2019-10-01 11:30:56 +02001156 server_addr12 = self.pg1.remote_hosts[1].ip6
Neale Ranns3466c302017-02-16 07:45:03 -08001157
Jakub Grajciar103ba6b2019-10-01 11:30:56 +02001158 Proxy12 = VppDHCPProxy(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001159 self, server_addr12, src_addr_vrf1, rx_vrf_id=1, server_vrf_id=1
1160 )
Jakub Grajciar103ba6b2019-10-01 11:30:56 +02001161 Proxy12.add_vpp_config()
Neale Ranns3466c302017-02-16 07:45:03 -08001162
1163 #
1164 # We'll need an ND entry for the server to send it packets
1165 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001166 nd_entry = VppNeighbor(
1167 self,
1168 self.pg1.sw_if_index,
1169 self.pg1.remote_hosts[1].mac,
1170 self.pg1.remote_hosts[1].ip6,
1171 )
Neale Ranns3466c302017-02-16 07:45:03 -08001172 nd_entry.add_vpp_config()
1173
1174 #
1175 # Send a discover from the client. expect two relayed messages
1176 # The frist packet is sent to the second server
1177 # We're not enforcing that here, it's just the way it is.
1178 #
John Lo70bfcaf2017-11-14 13:19:26 -05001179 self.pg4.add_stream(p_solicit_vrf1)
Neale Ranns3466c302017-02-16 07:45:03 -08001180 self.pg_enable_capture(self.pg_interfaces)
1181 self.pg_start()
1182
1183 rx = self.pg1.get_capture(2)
1184
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001185 self.verify_dhcp6_solicit(
1186 rx[0], self.pg1, dhcp_solicit_src_vrf1, self.pg4.remote_mac
1187 )
1188 self.verify_dhcp6_solicit(
1189 rx[1],
1190 self.pg1,
1191 dhcp_solicit_src_vrf1,
1192 self.pg4.remote_mac,
1193 dst_mac=self.pg1.remote_hosts[1].mac,
1194 dst_ip=self.pg1.remote_hosts[1].ip6,
1195 )
Neale Ranns3466c302017-02-16 07:45:03 -08001196
1197 #
1198 # Send both packets back. Client gets both.
1199 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001200 p1 = (
1201 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
1202 / IPv6(dst=self.pg1.local_ip6, src=self.pg1.remote_ip6)
1203 / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT)
1204 / DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1)
1205 / DHCP6OptIfaceId(optlen=4, ifaceid="\x00\x00\x00\x05")
1206 / DHCP6OptRelayMsg(optlen=0)
1207 / DHCP6_Advertise(trid=1)
1208 / DHCP6OptStatusCode(statuscode=0)
1209 )
1210 p2 = (
1211 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_hosts[1].mac)
1212 / IPv6(dst=self.pg1.local_ip6, src=self.pg1._remote_hosts[1].ip6)
1213 / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT)
1214 / DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1)
1215 / DHCP6OptIfaceId(optlen=4, ifaceid="\x00\x00\x00\x05")
1216 / DHCP6OptRelayMsg(optlen=0)
1217 / DHCP6_Advertise(trid=1)
1218 / DHCP6OptStatusCode(statuscode=0)
1219 )
Neale Ranns3466c302017-02-16 07:45:03 -08001220
1221 pkts = [p1, p2]
1222
1223 self.pg1.add_stream(pkts)
1224 self.pg_enable_capture(self.pg_interfaces)
1225 self.pg_start()
1226
John Lo70bfcaf2017-11-14 13:19:26 -05001227 rx = self.pg4.get_capture(2)
Neale Ranns3466c302017-02-16 07:45:03 -08001228
John Lo70bfcaf2017-11-14 13:19:26 -05001229 self.verify_dhcp6_advert(rx[0], self.pg4, dhcp_solicit_src_vrf1)
1230 self.verify_dhcp6_advert(rx[1], self.pg4, dhcp_solicit_src_vrf1)
Neale Ranns3466c302017-02-16 07:45:03 -08001231
1232 #
1233 # Ensure only solicit messages are duplicated
1234 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001235 p_request_vrf1 = (
1236 Ether(dst=dmac, src=self.pg4.remote_mac)
1237 / IPv6(src=dhcp_solicit_src_vrf1, dst=dhcp_solicit_dst)
1238 / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_CLIENT_PORT)
1239 / DHCP6_Request()
1240 )
Neale Ranns3466c302017-02-16 07:45:03 -08001241
John Lo70bfcaf2017-11-14 13:19:26 -05001242 self.pg4.add_stream(p_request_vrf1)
Neale Ranns3466c302017-02-16 07:45:03 -08001243 self.pg_enable_capture(self.pg_interfaces)
1244 self.pg_start()
1245
1246 rx = self.pg1.get_capture(1)
1247
1248 #
1249 # Test we drop DHCP packets from addresses that are not configured as
1250 # DHCP servers
1251 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001252 p2 = (
1253 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_hosts[1].mac)
1254 / IPv6(dst=self.pg1.local_ip6, src="3001::1")
1255 / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT)
1256 / DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1)
1257 / DHCP6OptIfaceId(optlen=4, ifaceid="\x00\x00\x00\x05")
1258 / DHCP6OptRelayMsg(optlen=0)
1259 / DHCP6_Advertise(trid=1)
1260 / DHCP6OptStatusCode(statuscode=0)
1261 )
1262 self.send_and_assert_no_replies(self.pg1, p2, "DHCP6 not from server")
Neale Ranns3466c302017-02-16 07:45:03 -08001263
1264 #
1265 # Remove the second DHCP server
1266 #
Jakub Grajciar103ba6b2019-10-01 11:30:56 +02001267 Proxy12.remove_vpp_config()
Neale Ranns3466c302017-02-16 07:45:03 -08001268
1269 #
1270 # Test we can still relay with the first
1271 #
John Lo70bfcaf2017-11-14 13:19:26 -05001272 self.pg4.add_stream(p_solicit_vrf1)
Neale Ranns3466c302017-02-16 07:45:03 -08001273 self.pg_enable_capture(self.pg_interfaces)
1274 self.pg_start()
1275
1276 rx = self.pg1.get_capture(1)
1277
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001278 self.verify_dhcp6_solicit(
1279 rx[0], self.pg1, dhcp_solicit_src_vrf1, self.pg4.remote_mac
1280 )
Neale Rannsfca0c242017-01-13 07:57:46 -08001281
1282 #
1283 # Cleanup
1284 #
Jakub Grajciar103ba6b2019-10-01 11:30:56 +02001285 Proxy.remove_vpp_config()
1286 Proxy1.remove_vpp_config()
1287 Proxy2.remove_vpp_config()
Neale Ranns3466c302017-02-16 07:45:03 -08001288
Neale Ranns15002542017-09-10 04:39:11 -07001289 self.pg3.unconfig_ip6()
John Lo70bfcaf2017-11-14 13:19:26 -05001290 self.pg4.unconfig_ip6()
1291 self.pg5.unconfig_ip6()
Neale Rannsfca0c242017-01-13 07:57:46 -08001292
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001293 def test_dhcp_client(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001294 """DHCP Client"""
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001295
Neale Ranns038e1df2019-07-19 14:01:02 +00001296 vdscp = VppEnum.vl_api_ip_dscp_t
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001297 hostname = "universal-dp"
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001298
1299 self.pg_enable_capture(self.pg_interfaces)
1300
1301 #
John Lo70bfcaf2017-11-14 13:19:26 -05001302 # Configure DHCP client on PG3 and capture the discover sent
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001303 #
Jakub Grajciar103ba6b2019-10-01 11:30:56 +02001304 Client = VppDHCPClient(self, self.pg3.sw_if_index, hostname)
1305 Client.add_vpp_config()
1306 self.assertTrue(Client.query_vpp_config())
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001307
John Lo70bfcaf2017-11-14 13:19:26 -05001308 rx = self.pg3.get_capture(1)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001309
John Lo70bfcaf2017-11-14 13:19:26 -05001310 self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001311
1312 #
John Lo70bfcaf2017-11-14 13:19:26 -05001313 # Send back on offer, expect the request
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001314 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001315 p_offer = (
1316 Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac)
1317 / IP(src=self.pg3.remote_ip4, dst="255.255.255.255")
1318 / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT)
1319 / BOOTP(
1320 op=1, yiaddr=self.pg3.local_ip4, chaddr=mac_pton(self.pg3.local_mac)
1321 )
1322 / DHCP(
1323 options=[
1324 ("message-type", "offer"),
1325 ("server_id", self.pg3.remote_ip4),
1326 "end",
1327 ]
1328 )
1329 )
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001330
John Lo70bfcaf2017-11-14 13:19:26 -05001331 self.pg3.add_stream(p_offer)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001332 self.pg_enable_capture(self.pg_interfaces)
1333 self.pg_start()
1334
John Lo70bfcaf2017-11-14 13:19:26 -05001335 rx = self.pg3.get_capture(1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001336 self.verify_orig_dhcp_request(rx[0], self.pg3, hostname, self.pg3.local_ip4)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001337
1338 #
Jan Gelety2a3fb1a2018-06-27 10:56:17 +02001339 # Send an acknowledgment
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001340 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001341 p_ack = (
1342 Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac)
1343 / IP(src=self.pg3.remote_ip4, dst="255.255.255.255")
1344 / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT)
1345 / BOOTP(
1346 op=1, yiaddr=self.pg3.local_ip4, chaddr=mac_pton(self.pg3.local_mac)
1347 )
1348 / DHCP(
1349 options=[
1350 ("message-type", "ack"),
1351 ("subnet_mask", "255.255.255.0"),
1352 ("router", self.pg3.remote_ip4),
1353 ("server_id", self.pg3.remote_ip4),
1354 ("lease_time", 43200),
1355 "end",
1356 ]
1357 )
1358 )
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001359
John Lo70bfcaf2017-11-14 13:19:26 -05001360 self.pg3.add_stream(p_ack)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001361 self.pg_enable_capture(self.pg_interfaces)
1362 self.pg_start()
1363
1364 #
Neale Ranns51822bf2017-07-18 09:26:53 -07001365 # We'll get an ARP request for the router address
1366 #
John Lo70bfcaf2017-11-14 13:19:26 -05001367 rx = self.pg3.get_capture(1)
Neale Ranns51822bf2017-07-18 09:26:53 -07001368
John Lo70bfcaf2017-11-14 13:19:26 -05001369 self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
Neale Ranns51822bf2017-07-18 09:26:53 -07001370 self.pg_enable_capture(self.pg_interfaces)
1371
1372 #
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001373 # At the end of this procedure there should be a connected route
1374 # in the FIB
1375 #
John Lo70bfcaf2017-11-14 13:19:26 -05001376 self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
1377 self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001378
1379 #
1380 # remove the DHCP config
1381 #
Jakub Grajciar103ba6b2019-10-01 11:30:56 +02001382 Client.remove_vpp_config()
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001383
1384 #
1385 # and now the route should be gone
1386 #
John Lo70bfcaf2017-11-14 13:19:26 -05001387 self.assertFalse(find_route(self, self.pg3.local_ip4, 32))
1388 self.assertFalse(find_route(self, self.pg3.local_ip4, 24))
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001389
Neale Ranns51822bf2017-07-18 09:26:53 -07001390 #
Neale Ranns808c5b22017-08-02 05:15:07 -07001391 # Start the procedure again. this time have VPP send the client-ID
Neale Ranns038e1df2019-07-19 14:01:02 +00001392 # and set the DSCP value
Neale Ranns51822bf2017-07-18 09:26:53 -07001393 #
John Lo70bfcaf2017-11-14 13:19:26 -05001394 self.pg3.admin_down()
Neale Ranns808c5b22017-08-02 05:15:07 -07001395 self.sleep(1)
John Lo70bfcaf2017-11-14 13:19:26 -05001396 self.pg3.admin_up()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001397 Client.set_client(
1398 self.pg3.sw_if_index,
1399 hostname,
1400 id=self.pg3.local_mac,
1401 dscp=vdscp.IP_API_DSCP_EF,
1402 )
Jakub Grajciar103ba6b2019-10-01 11:30:56 +02001403 Client.add_vpp_config()
Neale Ranns51822bf2017-07-18 09:26:53 -07001404
John Lo70bfcaf2017-11-14 13:19:26 -05001405 rx = self.pg3.get_capture(1)
Neale Ranns51822bf2017-07-18 09:26:53 -07001406
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001407 self.verify_orig_dhcp_discover(
1408 rx[0], self.pg3, hostname, self.pg3.local_mac, dscp=vdscp.IP_API_DSCP_EF
1409 )
Neale Ranns51822bf2017-07-18 09:26:53 -07001410
Jan Gelety2a3fb1a2018-06-27 10:56:17 +02001411 # TODO: VPP DHCP client should not accept DHCP OFFER message with
1412 # the XID (Transaction ID) not matching the XID of the most recent
1413 # DHCP DISCOVERY message.
1414 # Such DHCP OFFER message must be silently discarded - RFC2131.
1415 # Reported in Jira ticket: VPP-99
John Lo70bfcaf2017-11-14 13:19:26 -05001416 self.pg3.add_stream(p_offer)
Neale Ranns808c5b22017-08-02 05:15:07 -07001417 self.pg_enable_capture(self.pg_interfaces)
1418 self.pg_start()
1419
John Lo70bfcaf2017-11-14 13:19:26 -05001420 rx = self.pg3.get_capture(1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001421 self.verify_orig_dhcp_request(
1422 rx[0], self.pg3, hostname, self.pg3.local_ip4, dscp=vdscp.IP_API_DSCP_EF
1423 )
Neale Ranns808c5b22017-08-02 05:15:07 -07001424
1425 #
1426 # unicast the ack to the offered address
1427 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001428 p_ack = (
1429 Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac)
1430 / IP(src=self.pg3.remote_ip4, dst=self.pg3.local_ip4)
1431 / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT)
1432 / BOOTP(
1433 op=1, yiaddr=self.pg3.local_ip4, chaddr=mac_pton(self.pg3.local_mac)
1434 )
1435 / DHCP(
1436 options=[
1437 ("message-type", "ack"),
1438 ("subnet_mask", "255.255.255.0"),
1439 ("router", self.pg3.remote_ip4),
1440 ("server_id", self.pg3.remote_ip4),
1441 ("lease_time", 43200),
1442 "end",
1443 ]
1444 )
1445 )
Neale Ranns808c5b22017-08-02 05:15:07 -07001446
John Lo70bfcaf2017-11-14 13:19:26 -05001447 self.pg3.add_stream(p_ack)
Neale Ranns808c5b22017-08-02 05:15:07 -07001448 self.pg_enable_capture(self.pg_interfaces)
1449 self.pg_start()
1450
1451 #
Neale Ranns54c6dc42018-01-17 10:29:10 -08001452 # We'll get an ARP request for the router address
1453 #
1454 rx = self.pg3.get_capture(1)
1455
1456 self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
1457 self.pg_enable_capture(self.pg_interfaces)
1458
1459 #
Neale Ranns808c5b22017-08-02 05:15:07 -07001460 # At the end of this procedure there should be a connected route
1461 # in the FIB
1462 #
John Lo70bfcaf2017-11-14 13:19:26 -05001463 self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
1464 self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
Neale Ranns808c5b22017-08-02 05:15:07 -07001465
Neale Ranns51822bf2017-07-18 09:26:53 -07001466 #
1467 # remove the DHCP config
1468 #
Jakub Grajciar103ba6b2019-10-01 11:30:56 +02001469 Client.remove_vpp_config()
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001470
John Lo70bfcaf2017-11-14 13:19:26 -05001471 self.assertFalse(find_route(self, self.pg3.local_ip4, 32))
1472 self.assertFalse(find_route(self, self.pg3.local_ip4, 24))
Neale Ranns808c5b22017-08-02 05:15:07 -07001473
Neale Ranns54c6dc42018-01-17 10:29:10 -08001474 #
1475 # Rince and repeat, this time with VPP configured not to set
1476 # the braodcast flag in the discover and request messages,
1477 # and for the server to unicast the responses.
1478 #
1479 # Configure DHCP client on PG3 and capture the discover sent
1480 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001481 Client.set_client(self.pg3.sw_if_index, hostname, set_broadcast_flag=False)
Jakub Grajciar103ba6b2019-10-01 11:30:56 +02001482 Client.add_vpp_config()
Neale Ranns54c6dc42018-01-17 10:29:10 -08001483
1484 rx = self.pg3.get_capture(1)
1485
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001486 self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname, broadcast=False)
Neale Ranns54c6dc42018-01-17 10:29:10 -08001487
1488 #
1489 # Send back on offer, unicasted to the offered address.
1490 # Expect the request.
1491 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001492 p_offer = (
1493 Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac)
1494 / IP(src=self.pg3.remote_ip4, dst=self.pg3.local_ip4)
1495 / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT)
1496 / BOOTP(
1497 op=1, yiaddr=self.pg3.local_ip4, chaddr=mac_pton(self.pg3.local_mac)
1498 )
1499 / DHCP(
1500 options=[
1501 ("message-type", "offer"),
1502 ("server_id", self.pg3.remote_ip4),
1503 "end",
1504 ]
1505 )
1506 )
Neale Ranns54c6dc42018-01-17 10:29:10 -08001507
1508 self.pg3.add_stream(p_offer)
1509 self.pg_enable_capture(self.pg_interfaces)
1510 self.pg_start()
1511
1512 rx = self.pg3.get_capture(1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001513 self.verify_orig_dhcp_request(
1514 rx[0], self.pg3, hostname, self.pg3.local_ip4, broadcast=False
1515 )
Neale Ranns54c6dc42018-01-17 10:29:10 -08001516
1517 #
Neale Ranns99536f42019-07-25 06:11:58 -07001518 # Send an acknowledgment, the lease renewal time is 2 seconds
1519 # so we should expect the renew straight after
Neale Ranns54c6dc42018-01-17 10:29:10 -08001520 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001521 p_ack = (
1522 Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac)
1523 / IP(src=self.pg3.remote_ip4, dst=self.pg3.local_ip4)
1524 / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT)
1525 / BOOTP(
1526 op=1, yiaddr=self.pg3.local_ip4, chaddr=mac_pton(self.pg3.local_mac)
1527 )
1528 / DHCP(
1529 options=[
1530 ("message-type", "ack"),
1531 ("subnet_mask", "255.255.255.0"),
1532 ("router", self.pg3.remote_ip4),
1533 ("server_id", self.pg3.remote_ip4),
1534 ("lease_time", 43200),
1535 ("renewal_time", 2),
1536 "end",
1537 ]
1538 )
1539 )
Neale Ranns54c6dc42018-01-17 10:29:10 -08001540
1541 self.pg3.add_stream(p_ack)
1542 self.pg_enable_capture(self.pg_interfaces)
1543 self.pg_start()
1544
1545 #
1546 # We'll get an ARP request for the router address
1547 #
1548 rx = self.pg3.get_capture(1)
1549
1550 self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
1551 self.pg_enable_capture(self.pg_interfaces)
1552
1553 #
1554 # At the end of this procedure there should be a connected route
1555 # in the FIB
1556 #
1557 self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
1558 self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
1559
Neale Ranns99536f42019-07-25 06:11:58 -07001560 #
Neale Ranns6bcc6a42019-10-15 15:47:55 +00001561 # read the DHCP client details from a dump
1562 #
1563 clients = self.vapi.dhcp_client_dump()
1564
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001565 self.assertEqual(clients[0].client.sw_if_index, self.pg3.sw_if_index)
1566 self.assertEqual(clients[0].lease.sw_if_index, self.pg3.sw_if_index)
Neale Ranns6bcc6a42019-10-15 15:47:55 +00001567 self.assertEqual(clients[0].client.hostname, hostname)
1568 self.assertEqual(clients[0].lease.hostname, hostname)
1569 # 0 = DISCOVER, 1 = REQUEST, 2 = BOUND
1570 self.assertEqual(clients[0].lease.state, 2)
1571 self.assertEqual(clients[0].lease.mask_width, 24)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001572 self.assertEqual(str(clients[0].lease.router_address), self.pg3.remote_ip4)
1573 self.assertEqual(str(clients[0].lease.host_address), self.pg3.local_ip4)
Neale Ranns6bcc6a42019-10-15 15:47:55 +00001574
1575 #
Neale Ranns99536f42019-07-25 06:11:58 -07001576 # wait for the unicasted renewal
1577 # the first attempt will be an ARP packet, since we have not yet
1578 # responded to VPP's request
1579 #
1580 self.logger.info(self.vapi.cli("sh dhcp client intfc pg3 verbose"))
1581 rx = self.pg3.get_capture(1, timeout=10)
1582
1583 self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
1584
1585 # respond to the arp
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001586 p_arp = Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) / ARP(
1587 op="is-at",
1588 hwdst=self.pg3.local_mac,
1589 hwsrc=self.pg3.remote_mac,
1590 pdst=self.pg3.local_ip4,
1591 psrc=self.pg3.remote_ip4,
1592 )
Neale Ranns99536f42019-07-25 06:11:58 -07001593 self.pg3.add_stream(p_arp)
1594 self.pg_enable_capture(self.pg_interfaces)
1595 self.pg_start()
1596
1597 # the next packet is the unicasted renewal
1598 rx = self.pg3.get_capture(1, timeout=10)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001599 self.verify_orig_dhcp_request(
1600 rx[0], self.pg3, hostname, self.pg3.local_ip4, l2_bc=False, broadcast=False
1601 )
Neale Rannsdaff1782018-05-16 04:12:18 -07001602
Neale Ranns6bcc6a42019-10-15 15:47:55 +00001603 # send an ACK with different data from the original offer *
1604 self.pg3.generate_remote_hosts(4)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001605 p_ack = (
1606 Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac)
1607 / IP(src=self.pg3.remote_ip4, dst=self.pg3.local_ip4)
1608 / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT)
1609 / BOOTP(
1610 op=1,
1611 yiaddr=self.pg3.remote_hosts[3].ip4,
1612 chaddr=mac_pton(self.pg3.local_mac),
1613 )
1614 / DHCP(
1615 options=[
1616 ("message-type", "ack"),
1617 ("subnet_mask", "255.255.255.0"),
1618 ("router", self.pg3.remote_hosts[1].ip4),
1619 ("server_id", self.pg3.remote_hosts[2].ip4),
1620 ("lease_time", 43200),
1621 ("renewal_time", 2),
1622 "end",
1623 ]
1624 )
1625 )
Neale Ranns6bcc6a42019-10-15 15:47:55 +00001626
1627 self.pg3.add_stream(p_ack)
1628 self.pg_enable_capture(self.pg_interfaces)
1629 self.pg_start()
1630
Neale Rannsdaff1782018-05-16 04:12:18 -07001631 #
1632 # read the DHCP client details from a dump
1633 #
1634 clients = self.vapi.dhcp_client_dump()
1635
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001636 self.assertEqual(clients[0].client.sw_if_index, self.pg3.sw_if_index)
1637 self.assertEqual(clients[0].lease.sw_if_index, self.pg3.sw_if_index)
Neale Ranns6bcc6a42019-10-15 15:47:55 +00001638 self.assertEqual(clients[0].client.hostname, hostname)
1639 self.assertEqual(clients[0].lease.hostname, hostname)
Neale Rannsdaff1782018-05-16 04:12:18 -07001640 # 0 = DISCOVER, 1 = REQUEST, 2 = BOUND
1641 self.assertEqual(clients[0].lease.state, 2)
1642 self.assertEqual(clients[0].lease.mask_width, 24)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001643 self.assertEqual(
1644 str(clients[0].lease.router_address), self.pg3.remote_hosts[1].ip4
1645 )
1646 self.assertEqual(
1647 str(clients[0].lease.host_address), self.pg3.remote_hosts[3].ip4
1648 )
Neale Ranns99536f42019-07-25 06:11:58 -07001649
Neale Ranns54c6dc42018-01-17 10:29:10 -08001650 #
1651 # remove the DHCP config
1652 #
Jakub Grajciar103ba6b2019-10-01 11:30:56 +02001653 Client.remove_vpp_config()
Neale Ranns54c6dc42018-01-17 10:29:10 -08001654
1655 #
1656 # and now the route should be gone
1657 #
1658 self.assertFalse(find_route(self, self.pg3.local_ip4, 32))
1659 self.assertFalse(find_route(self, self.pg3.local_ip4, 24))
1660
Jan Gelety2a3fb1a2018-06-27 10:56:17 +02001661 #
1662 # Start the procedure again. Use requested lease time option.
Neale Ranns6bcc6a42019-10-15 15:47:55 +00001663 # this time wait for the lease to expire and the client to
1664 # self-destruct
Jan Gelety2a3fb1a2018-06-27 10:56:17 +02001665 #
Neale Ranns99536f42019-07-25 06:11:58 -07001666 hostname += "-2"
Jan Gelety2a3fb1a2018-06-27 10:56:17 +02001667 self.pg3.admin_down()
1668 self.sleep(1)
1669 self.pg3.admin_up()
Neale Ranns99536f42019-07-25 06:11:58 -07001670 self.pg_enable_capture(self.pg_interfaces)
Jakub Grajciar103ba6b2019-10-01 11:30:56 +02001671 Client.set_client(self.pg3.sw_if_index, hostname)
1672 Client.add_vpp_config()
Jan Gelety2a3fb1a2018-06-27 10:56:17 +02001673
1674 rx = self.pg3.get_capture(1)
1675
1676 self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname)
1677
1678 #
1679 # Send back on offer with requested lease time, expect the request
1680 #
1681 lease_time = 1
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001682 p_offer = (
1683 Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac)
1684 / IP(src=self.pg3.remote_ip4, dst="255.255.255.255")
1685 / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT)
1686 / BOOTP(
1687 op=1, yiaddr=self.pg3.local_ip4, chaddr=mac_pton(self.pg3.local_mac)
1688 )
1689 / DHCP(
1690 options=[
1691 ("message-type", "offer"),
1692 ("server_id", self.pg3.remote_ip4),
1693 ("lease_time", lease_time),
1694 "end",
1695 ]
1696 )
1697 )
Jan Gelety2a3fb1a2018-06-27 10:56:17 +02001698
1699 self.pg3.add_stream(p_offer)
1700 self.pg_enable_capture(self.pg_interfaces)
1701 self.pg_start()
1702
1703 rx = self.pg3.get_capture(1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001704 self.verify_orig_dhcp_request(rx[0], self.pg3, hostname, self.pg3.local_ip4)
Jan Gelety2a3fb1a2018-06-27 10:56:17 +02001705
1706 #
1707 # Send an acknowledgment
1708 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001709 p_ack = (
1710 Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac)
1711 / IP(src=self.pg3.remote_ip4, dst="255.255.255.255")
1712 / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT)
1713 / BOOTP(
1714 op=1, yiaddr=self.pg3.local_ip4, chaddr=mac_pton(self.pg3.local_mac)
1715 )
1716 / DHCP(
1717 options=[
1718 ("message-type", "ack"),
1719 ("subnet_mask", "255.255.255.0"),
1720 ("router", self.pg3.remote_ip4),
1721 ("server_id", self.pg3.remote_ip4),
1722 ("lease_time", lease_time),
1723 "end",
1724 ]
1725 )
1726 )
Jan Gelety2a3fb1a2018-06-27 10:56:17 +02001727
1728 self.pg3.add_stream(p_ack)
1729 self.pg_enable_capture(self.pg_interfaces)
1730 self.pg_start()
1731
1732 #
1733 # We'll get an ARP request for the router address
1734 #
1735 rx = self.pg3.get_capture(1)
1736
1737 self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
1738
1739 #
1740 # At the end of this procedure there should be a connected route
1741 # in the FIB
1742 #
1743 self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
1744 self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
1745
Jan Gelety2a3fb1a2018-06-27 10:56:17 +02001746 #
Neale Rannsf6e9b012019-01-25 06:37:15 -08001747 # the route should be gone after the lease expires
Jan Gelety2a3fb1a2018-06-27 10:56:17 +02001748 #
Neale Rannsf6e9b012019-01-25 06:37:15 -08001749 self.assertTrue(self.wait_for_no_route(self.pg3.local_ip4, 32))
1750 self.assertTrue(self.wait_for_no_route(self.pg3.local_ip4, 24))
Jan Gelety2a3fb1a2018-06-27 10:56:17 +02001751
1752 #
1753 # remove the DHCP config
1754 #
Jakub Grajciar103ba6b2019-10-01 11:30:56 +02001755 Client.remove_vpp_config()
Jan Gelety2a3fb1a2018-06-27 10:56:17 +02001756
Neale Ranns038e1df2019-07-19 14:01:02 +00001757 def test_dhcp_client_vlan(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001758 """DHCP Client w/ VLAN"""
Neale Ranns038e1df2019-07-19 14:01:02 +00001759
1760 vdscp = VppEnum.vl_api_ip_dscp_t
1761 vqos = VppEnum.vl_api_qos_source_t
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001762 hostname = "universal-dp"
Neale Ranns038e1df2019-07-19 14:01:02 +00001763
1764 self.pg_enable_capture(self.pg_interfaces)
1765
1766 vlan_100 = VppDot1QSubint(self, self.pg3, 100)
1767 vlan_100.admin_up()
1768
1769 output = [scapy.compat.chb(4)] * 256
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001770 os = b"".join(output)
1771 rows = [{"outputs": os}, {"outputs": os}, {"outputs": os}, {"outputs": os}]
Neale Ranns038e1df2019-07-19 14:01:02 +00001772
1773 qem1 = VppQosEgressMap(self, 1, rows).add_vpp_config()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001774 qm1 = VppQosMark(
1775 self, vlan_100, qem1, vqos.QOS_API_SOURCE_VLAN
1776 ).add_vpp_config()
Neale Ranns038e1df2019-07-19 14:01:02 +00001777
1778 #
1779 # Configure DHCP client on PG3 and capture the discover sent
1780 #
Jakub Grajciar103ba6b2019-10-01 11:30:56 +02001781 Client = VppDHCPClient(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001782 self, vlan_100.sw_if_index, hostname, dscp=vdscp.IP_API_DSCP_EF
1783 )
Jakub Grajciar103ba6b2019-10-01 11:30:56 +02001784 Client.add_vpp_config()
Neale Ranns038e1df2019-07-19 14:01:02 +00001785
1786 rx = self.pg3.get_capture(1)
1787
1788 self.assertEqual(rx[0][Dot1Q].vlan, 100)
Prashant Maheshwari3bcf1a92019-07-31 21:37:33 +05301789 self.assertEqual(rx[0][Dot1Q].prio, 2)
Neale Ranns038e1df2019-07-19 14:01:02 +00001790
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001791 self.verify_orig_dhcp_discover(
1792 rx[0], self.pg3, hostname, dscp=vdscp.IP_API_DSCP_EF
1793 )
Neale Ranns038e1df2019-07-19 14:01:02 +00001794
Neale Ranns808c5b22017-08-02 05:15:07 -07001795
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001796if __name__ == "__main__":
Neale Rannsfca0c242017-01-13 07:57:46 -08001797 unittest.main(testRunner=VppTestRunner)