blob: 21940ca99f0a78b0629b76cb56f32b7c6eb55c6c [file] [log] [blame]
Neale Rannsfca0c242017-01-13 07:57:46 -08001#!/usr/bin/env python
2
3import unittest
4import socket
Neale Ranns3466c302017-02-16 07:45:03 -08005import struct
Neale Rannsfca0c242017-01-13 07:57:46 -08006
John Lo70bfcaf2017-11-14 13:19:26 -05007from framework import VppTestCase, VppTestRunner, running_extended_tests
Neale Ranns3466c302017-02-16 07:45:03 -08008from vpp_neighbor import VppNeighbor
Neale Ranns15002542017-09-10 04:39:11 -07009from vpp_ip_route import find_route, VppIpTable
Neale Ranns2a3ea492017-04-19 05:24:40 -070010from util import mk_ll_addr
Neale Rannsfca0c242017-01-13 07:57:46 -080011
Neale Ranns51822bf2017-07-18 09:26:53 -070012from scapy.layers.l2 import Ether, getmacbyip, ARP
Neale Rannsfca0c242017-01-13 07:57:46 -080013from scapy.layers.inet import IP, UDP, ICMP
14from scapy.layers.inet6 import IPv6, in6_getnsmac, in6_mactoifaceid
15from scapy.layers.dhcp import DHCP, BOOTP, DHCPTypes
16from scapy.layers.dhcp6 import DHCP6, DHCP6_Solicit, DHCP6_RelayForward, \
17 DHCP6_RelayReply, DHCP6_Advertise, DHCP6OptRelayMsg, DHCP6OptIfaceId, \
Neale Ranns3466c302017-02-16 07:45:03 -080018 DHCP6OptStatusCode, DHCP6OptVSS, DHCP6OptClientLinkLayerAddr, DHCP6_Request
Neale Rannsfca0c242017-01-13 07:57:46 -080019from socket import AF_INET, AF_INET6
20from scapy.utils import inet_pton, inet_ntop
21from scapy.utils6 import in6_ptop
Ole Troan895b6e82017-10-20 13:28:20 +020022from util import mactobinary
Neale Rannsfca0c242017-01-13 07:57:46 -080023
24DHCP4_CLIENT_PORT = 68
25DHCP4_SERVER_PORT = 67
26DHCP6_CLIENT_PORT = 547
27DHCP6_SERVER_PORT = 546
28
29
Neale Rannsfca0c242017-01-13 07:57:46 -080030class TestDHCP(VppTestCase):
31 """ DHCP Test Case """
32
33 def setUp(self):
34 super(TestDHCP, self).setUp()
35
John Lo70bfcaf2017-11-14 13:19:26 -050036 # create 6 pg interfaces for pg0 to pg5
37 self.create_pg_interfaces(range(6))
Neale Ranns15002542017-09-10 04:39:11 -070038 self.tables = []
Neale Rannsfca0c242017-01-13 07:57:46 -080039
John Lo70bfcaf2017-11-14 13:19:26 -050040 # pg0 to 2 are IP configured in VRF 0, 1 and 2.
41 # pg3 to 5 are non IP-configured in VRF 0, 1 and 2.
Neale Rannsfca0c242017-01-13 07:57:46 -080042 table_id = 0
Neale Ranns15002542017-09-10 04:39:11 -070043 for table_id in range(1, 4):
44 tbl4 = VppIpTable(self, table_id)
45 tbl4.add_vpp_config()
46 self.tables.append(tbl4)
47 tbl6 = VppIpTable(self, table_id, is_ip6=1)
48 tbl6.add_vpp_config()
49 self.tables.append(tbl6)
50
51 table_id = 0
John Lo70bfcaf2017-11-14 13:19:26 -050052 for i in self.pg_interfaces[:3]:
Neale Rannsfca0c242017-01-13 07:57:46 -080053 i.admin_up()
54 i.set_table_ip4(table_id)
55 i.set_table_ip6(table_id)
56 i.config_ip4()
57 i.resolve_arp()
58 i.config_ip6()
59 i.resolve_ndp()
60 table_id += 1
61
62 table_id = 0
John Lo70bfcaf2017-11-14 13:19:26 -050063 for i in self.pg_interfaces[3:]:
Neale Rannsfca0c242017-01-13 07:57:46 -080064 i.admin_up()
65 i.set_table_ip4(table_id)
66 i.set_table_ip6(table_id)
67 table_id += 1
68
Neale Ranns4008ac92017-02-13 23:20:04 -080069 def tearDown(self):
John Lo70bfcaf2017-11-14 13:19:26 -050070 for i in self.pg_interfaces[:3]:
Neale Ranns4008ac92017-02-13 23:20:04 -080071 i.unconfig_ip4()
72 i.unconfig_ip6()
Neale Ranns15002542017-09-10 04:39:11 -070073
74 for i in self.pg_interfaces:
75 i.set_table_ip4(0)
76 i.set_table_ip6(0)
Neale Ranns4008ac92017-02-13 23:20:04 -080077 i.admin_down()
Neale Ranns15002542017-09-10 04:39:11 -070078 super(TestDHCP, self).tearDown()
Neale Ranns4008ac92017-02-13 23:20:04 -080079
Neale Rannsa2fbf6b2017-07-18 08:23:32 -070080 def verify_dhcp_has_option(self, pkt, option, value):
81 dhcp = pkt[DHCP]
82 found = False
83
84 for i in dhcp.options:
85 if type(i) is tuple:
86 if i[0] == option:
87 self.assertEqual(i[1], value)
88 found = True
89
90 self.assertTrue(found)
91
John Lo70bfcaf2017-11-14 13:19:26 -050092 def validate_relay_options(self, pkt, intf, ip_addr, vpn_id, fib_id, oui):
Neale Rannsfca0c242017-01-13 07:57:46 -080093 dhcp = pkt[DHCP]
94 found = 0
95 data = []
John Lo70bfcaf2017-11-14 13:19:26 -050096 id_len = len(vpn_id)
Neale Rannsfca0c242017-01-13 07:57:46 -080097
98 for i in dhcp.options:
99 if type(i) is tuple:
100 if i[0] == "relay_agent_Information":
101 #
102 # There are two sb-options present - each of length 6.
103 #
104 data = i[1]
Neale Ranns20a175a2017-02-14 07:28:41 -0800105 if oui != 0:
106 self.assertEqual(len(data), 24)
John Lo70bfcaf2017-11-14 13:19:26 -0500107 elif len(vpn_id) > 0:
108 self.assertEqual(len(data), len(vpn_id)+17)
Neale Ranns20a175a2017-02-14 07:28:41 -0800109 else:
110 self.assertEqual(len(data), 12)
Neale Rannsfca0c242017-01-13 07:57:46 -0800111
112 #
113 # First sub-option is ID 1, len 4, then encoded
114 # sw_if_index. This test uses low valued indicies
115 # so [2:4] are 0.
116 # The ID space is VPP internal - so no matching value
117 # scapy
118 #
119 self.assertEqual(ord(data[0]), 1)
120 self.assertEqual(ord(data[1]), 4)
121 self.assertEqual(ord(data[2]), 0)
122 self.assertEqual(ord(data[3]), 0)
123 self.assertEqual(ord(data[4]), 0)
124 self.assertEqual(ord(data[5]), intf._sw_if_index)
125
126 #
127 # next sub-option is the IP address of the client side
128 # interface.
129 # sub-option ID=5, length (of a v4 address)=4
130 #
131 claddr = socket.inet_pton(AF_INET, ip_addr)
132
133 self.assertEqual(ord(data[6]), 5)
134 self.assertEqual(ord(data[7]), 4)
135 self.assertEqual(data[8], claddr[0])
136 self.assertEqual(data[9], claddr[1])
137 self.assertEqual(data[10], claddr[2])
138 self.assertEqual(data[11], claddr[3])
139
Neale Ranns20a175a2017-02-14 07:28:41 -0800140 if oui != 0:
John Lo70bfcaf2017-11-14 13:19:26 -0500141 # sub-option 151 encodes vss_type 1,
142 # the 3 byte oui and the 4 byte fib_id
143 self.assertEqual(id_len, 0)
Neale Ranns20a175a2017-02-14 07:28:41 -0800144 self.assertEqual(ord(data[12]), 151)
145 self.assertEqual(ord(data[13]), 8)
146 self.assertEqual(ord(data[14]), 1)
147 self.assertEqual(ord(data[15]), 0)
148 self.assertEqual(ord(data[16]), 0)
149 self.assertEqual(ord(data[17]), oui)
150 self.assertEqual(ord(data[18]), 0)
151 self.assertEqual(ord(data[19]), 0)
152 self.assertEqual(ord(data[20]), 0)
153 self.assertEqual(ord(data[21]), fib_id)
154
155 # VSS control sub-option
156 self.assertEqual(ord(data[22]), 152)
157 self.assertEqual(ord(data[23]), 0)
158
John Lo70bfcaf2017-11-14 13:19:26 -0500159 if id_len > 0:
160 # sub-option 151 encode vss_type of 0
161 # followerd by vpn_id in ascii
162 self.assertEqual(oui, 0)
163 self.assertEqual(ord(data[12]), 151)
164 self.assertEqual(ord(data[13]), id_len+1)
165 self.assertEqual(ord(data[14]), 0)
166 self.assertEqual(data[15:15+id_len], vpn_id)
167
168 # VSS control sub-option
169 self.assertEqual(ord(data[15+len(vpn_id)]), 152)
170 self.assertEqual(ord(data[16+len(vpn_id)]), 0)
171
Neale Rannsfca0c242017-01-13 07:57:46 -0800172 found = 1
173 self.assertTrue(found)
174
175 return data
176
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700177 def verify_dhcp_msg_type(self, pkt, name):
178 dhcp = pkt[DHCP]
179 found = False
180 for o in dhcp.options:
181 if type(o) is tuple:
182 if o[0] == "message-type" \
183 and DHCPTypes[o[1]] == name:
184 found = True
185 self.assertTrue(found)
186
John Lo70bfcaf2017-11-14 13:19:26 -0500187 def verify_dhcp_offer(self, pkt, intf, vpn_id="", fib_id=0, oui=0):
Neale Rannsfca0c242017-01-13 07:57:46 -0800188 ether = pkt[Ether]
189 self.assertEqual(ether.dst, "ff:ff:ff:ff:ff:ff")
190 self.assertEqual(ether.src, intf.local_mac)
191
192 ip = pkt[IP]
193 self.assertEqual(ip.dst, "255.255.255.255")
194 self.assertEqual(ip.src, intf.local_ip4)
195
196 udp = pkt[UDP]
197 self.assertEqual(udp.dport, DHCP4_CLIENT_PORT)
198 self.assertEqual(udp.sport, DHCP4_SERVER_PORT)
199
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700200 self.verify_dhcp_msg_type(pkt, "offer")
Neale Ranns3466c302017-02-16 07:45:03 -0800201 data = self.validate_relay_options(pkt, intf, intf.local_ip4,
John Lo70bfcaf2017-11-14 13:19:26 -0500202 vpn_id, fib_id, oui)
Neale Rannsfca0c242017-01-13 07:57:46 -0800203
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700204 def verify_orig_dhcp_pkt(self, pkt, intf):
205 ether = pkt[Ether]
206 self.assertEqual(ether.dst, "ff:ff:ff:ff:ff:ff")
207 self.assertEqual(ether.src, intf.local_mac)
208
209 ip = pkt[IP]
210 self.assertEqual(ip.dst, "255.255.255.255")
211 self.assertEqual(ip.src, "0.0.0.0")
212
213 udp = pkt[UDP]
214 self.assertEqual(udp.dport, DHCP4_SERVER_PORT)
215 self.assertEqual(udp.sport, DHCP4_CLIENT_PORT)
216
Neale Ranns54c6dc42018-01-17 10:29:10 -0800217 def verify_orig_dhcp_discover(self, pkt, intf, hostname, client_id=None,
218 broadcast=1):
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700219 self.verify_orig_dhcp_pkt(pkt, intf)
220
221 self.verify_dhcp_msg_type(pkt, "discover")
222 self.verify_dhcp_has_option(pkt, "hostname", hostname)
Neale Ranns51822bf2017-07-18 09:26:53 -0700223 if client_id:
224 self.verify_dhcp_has_option(pkt, "client_id", client_id)
Neale Ranns808c5b22017-08-02 05:15:07 -0700225 bootp = pkt[BOOTP]
226 self.assertEqual(bootp.ciaddr, "0.0.0.0")
227 self.assertEqual(bootp.giaddr, "0.0.0.0")
Neale Ranns54c6dc42018-01-17 10:29:10 -0800228 if broadcast:
229 self.assertEqual(bootp.flags, 0x8000)
230 else:
231 self.assertEqual(bootp.flags, 0x0000)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700232
Neale Ranns54c6dc42018-01-17 10:29:10 -0800233 def verify_orig_dhcp_request(self, pkt, intf, hostname, ip,
234 broadcast=1):
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700235 self.verify_orig_dhcp_pkt(pkt, intf)
236
237 self.verify_dhcp_msg_type(pkt, "request")
238 self.verify_dhcp_has_option(pkt, "hostname", hostname)
239 self.verify_dhcp_has_option(pkt, "requested_addr", ip)
Neale Ranns808c5b22017-08-02 05:15:07 -0700240 bootp = pkt[BOOTP]
241 self.assertEqual(bootp.ciaddr, "0.0.0.0")
242 self.assertEqual(bootp.giaddr, "0.0.0.0")
Neale Ranns54c6dc42018-01-17 10:29:10 -0800243 if broadcast:
244 self.assertEqual(bootp.flags, 0x8000)
245 else:
246 self.assertEqual(bootp.flags, 0x0000)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700247
248 def verify_relayed_dhcp_discover(self, pkt, intf, src_intf=None,
249 fib_id=0, oui=0,
John Lo70bfcaf2017-11-14 13:19:26 -0500250 vpn_id="",
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700251 dst_mac=None, dst_ip=None):
Neale Ranns3466c302017-02-16 07:45:03 -0800252 if not dst_mac:
253 dst_mac = intf.remote_mac
254 if not dst_ip:
255 dst_ip = intf.remote_ip4
256
Neale Rannsfca0c242017-01-13 07:57:46 -0800257 ether = pkt[Ether]
Neale Ranns3466c302017-02-16 07:45:03 -0800258 self.assertEqual(ether.dst, dst_mac)
Neale Rannsfca0c242017-01-13 07:57:46 -0800259 self.assertEqual(ether.src, intf.local_mac)
260
261 ip = pkt[IP]
Neale Ranns3466c302017-02-16 07:45:03 -0800262 self.assertEqual(ip.dst, dst_ip)
Neale Rannsfca0c242017-01-13 07:57:46 -0800263 self.assertEqual(ip.src, intf.local_ip4)
264
265 udp = pkt[UDP]
266 self.assertEqual(udp.dport, DHCP4_SERVER_PORT)
267 self.assertEqual(udp.sport, DHCP4_CLIENT_PORT)
268
269 dhcp = pkt[DHCP]
270
271 is_discover = False
272 for o in dhcp.options:
273 if type(o) is tuple:
274 if o[0] == "message-type" \
275 and DHCPTypes[o[1]] == "discover":
276 is_discover = True
277 self.assertTrue(is_discover)
278
Neale Ranns20a175a2017-02-14 07:28:41 -0800279 data = self.validate_relay_options(pkt, src_intf,
280 src_intf.local_ip4,
John Lo70bfcaf2017-11-14 13:19:26 -0500281 vpn_id,
Neale Ranns20a175a2017-02-14 07:28:41 -0800282 fib_id, oui)
283 return data
Neale Rannsfca0c242017-01-13 07:57:46 -0800284
285 def verify_dhcp6_solicit(self, pkt, intf,
286 peer_ip, peer_mac,
John Lo70bfcaf2017-11-14 13:19:26 -0500287 vpn_id="",
Neale Rannsfca0c242017-01-13 07:57:46 -0800288 fib_id=0,
Neale Ranns3466c302017-02-16 07:45:03 -0800289 oui=0,
290 dst_mac=None,
291 dst_ip=None):
292 if not dst_mac:
293 dst_mac = intf.remote_mac
294 if not dst_ip:
295 dst_ip = in6_ptop(intf.remote_ip6)
296
Neale Rannsfca0c242017-01-13 07:57:46 -0800297 ether = pkt[Ether]
Neale Ranns3466c302017-02-16 07:45:03 -0800298 self.assertEqual(ether.dst, dst_mac)
Neale Rannsfca0c242017-01-13 07:57:46 -0800299 self.assertEqual(ether.src, intf.local_mac)
300
301 ip = pkt[IPv6]
Neale Ranns3466c302017-02-16 07:45:03 -0800302 self.assertEqual(in6_ptop(ip.dst), dst_ip)
Neale Rannsfca0c242017-01-13 07:57:46 -0800303 self.assertEqual(in6_ptop(ip.src), in6_ptop(intf.local_ip6))
304
305 udp = pkt[UDP]
306 self.assertEqual(udp.dport, DHCP6_CLIENT_PORT)
307 self.assertEqual(udp.sport, DHCP6_SERVER_PORT)
308
309 relay = pkt[DHCP6_RelayForward]
310 self.assertEqual(in6_ptop(relay.peeraddr), in6_ptop(peer_ip))
311 oid = pkt[DHCP6OptIfaceId]
312 cll = pkt[DHCP6OptClientLinkLayerAddr]
313 self.assertEqual(cll.optlen, 8)
314 self.assertEqual(cll.lltype, 1)
315 self.assertEqual(cll.clladdr, peer_mac)
316
John Lo70bfcaf2017-11-14 13:19:26 -0500317 id_len = len(vpn_id)
318
Neale Ranns20a175a2017-02-14 07:28:41 -0800319 if fib_id != 0:
John Lo70bfcaf2017-11-14 13:19:26 -0500320 self.assertEqual(id_len, 0)
Neale Ranns20a175a2017-02-14 07:28:41 -0800321 vss = pkt[DHCP6OptVSS]
322 self.assertEqual(vss.optlen, 8)
323 self.assertEqual(vss.type, 1)
324 # the OUI and FIB-id are really 3 and 4 bytes resp.
325 # but the tested range is small
326 self.assertEqual(ord(vss.data[0]), 0)
327 self.assertEqual(ord(vss.data[1]), 0)
328 self.assertEqual(ord(vss.data[2]), oui)
329 self.assertEqual(ord(vss.data[3]), 0)
330 self.assertEqual(ord(vss.data[4]), 0)
331 self.assertEqual(ord(vss.data[5]), 0)
332 self.assertEqual(ord(vss.data[6]), fib_id)
Neale Rannsfca0c242017-01-13 07:57:46 -0800333
John Lo70bfcaf2017-11-14 13:19:26 -0500334 if id_len > 0:
335 self.assertEqual(oui, 0)
336 vss = pkt[DHCP6OptVSS]
337 self.assertEqual(vss.optlen, id_len+1)
338 self.assertEqual(vss.type, 0)
339 self.assertEqual(vss.data[0:id_len], vpn_id)
340
Neale Rannsfca0c242017-01-13 07:57:46 -0800341 # the relay message should be an encoded Solicit
342 msg = pkt[DHCP6OptRelayMsg]
343 sol = DHCP6_Solicit()
344 self.assertEqual(msg.optlen, len(str(sol)))
345 self.assertEqual(str(sol), (str(msg[1]))[:msg.optlen])
346
347 def verify_dhcp6_advert(self, pkt, intf, peer):
348 ether = pkt[Ether]
349 self.assertEqual(ether.dst, "ff:ff:ff:ff:ff:ff")
350 self.assertEqual(ether.src, intf.local_mac)
351
352 ip = pkt[IPv6]
353 self.assertEqual(in6_ptop(ip.dst), in6_ptop(peer))
354 self.assertEqual(in6_ptop(ip.src), in6_ptop(intf.local_ip6))
355
356 udp = pkt[UDP]
357 self.assertEqual(udp.dport, DHCP6_SERVER_PORT)
358 self.assertEqual(udp.sport, DHCP6_CLIENT_PORT)
359
360 # not sure why this is not decoding
361 # adv = pkt[DHCP6_Advertise]
362
363 def test_dhcp_proxy(self):
364 """ DHCPv4 Proxy """
365
366 #
367 # Verify no response to DHCP request without DHCP config
368 #
369 p_disc_vrf0 = (Ether(dst="ff:ff:ff:ff:ff:ff",
John Lo70bfcaf2017-11-14 13:19:26 -0500370 src=self.pg3.remote_mac) /
Neale Rannsfca0c242017-01-13 07:57:46 -0800371 IP(src="0.0.0.0", dst="255.255.255.255") /
372 UDP(sport=DHCP4_CLIENT_PORT,
373 dport=DHCP4_SERVER_PORT) /
374 BOOTP(op=1) /
375 DHCP(options=[('message-type', 'discover'), ('end')]))
376 pkts_disc_vrf0 = [p_disc_vrf0]
377 p_disc_vrf1 = (Ether(dst="ff:ff:ff:ff:ff:ff",
John Lo70bfcaf2017-11-14 13:19:26 -0500378 src=self.pg4.remote_mac) /
Neale Rannsfca0c242017-01-13 07:57:46 -0800379 IP(src="0.0.0.0", dst="255.255.255.255") /
380 UDP(sport=DHCP4_CLIENT_PORT,
381 dport=DHCP4_SERVER_PORT) /
382 BOOTP(op=1) /
383 DHCP(options=[('message-type', 'discover'), ('end')]))
John Lo70bfcaf2017-11-14 13:19:26 -0500384 pkts_disc_vrf1 = [p_disc_vrf1]
385 p_disc_vrf2 = (Ether(dst="ff:ff:ff:ff:ff:ff",
386 src=self.pg5.remote_mac) /
387 IP(src="0.0.0.0", dst="255.255.255.255") /
388 UDP(sport=DHCP4_CLIENT_PORT,
389 dport=DHCP4_SERVER_PORT) /
390 BOOTP(op=1) /
391 DHCP(options=[('message-type', 'discover'), ('end')]))
392 pkts_disc_vrf2 = [p_disc_vrf2]
Neale Rannsfca0c242017-01-13 07:57:46 -0800393
John Lo70bfcaf2017-11-14 13:19:26 -0500394 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf0,
Neale Rannsfca0c242017-01-13 07:57:46 -0800395 "DHCP with no configuration")
John Lo70bfcaf2017-11-14 13:19:26 -0500396 self.send_and_assert_no_replies(self.pg4, pkts_disc_vrf1,
397 "DHCP with no configuration")
398 self.send_and_assert_no_replies(self.pg5, pkts_disc_vrf2,
Neale Rannsfca0c242017-01-13 07:57:46 -0800399 "DHCP with no configuration")
400
401 #
402 # Enable DHCP proxy in VRF 0
403 #
404 server_addr = self.pg0.remote_ip4n
405 src_addr = self.pg0.local_ip4n
406
407 self.vapi.dhcp_proxy_config(server_addr,
408 src_addr,
409 rx_table_id=0)
410
411 #
Neale Ranns20a175a2017-02-14 07:28:41 -0800412 # Discover packets from the client are dropped because there is no
413 # IP address configured on the client facing interface
Neale Rannsfca0c242017-01-13 07:57:46 -0800414 #
John Lo70bfcaf2017-11-14 13:19:26 -0500415 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf0,
Neale Ranns20a175a2017-02-14 07:28:41 -0800416 "Discover DHCP no relay address")
Neale Rannsfca0c242017-01-13 07:57:46 -0800417
418 #
419 # Inject a response from the server
Neale Ranns20a175a2017-02-14 07:28:41 -0800420 # dropped, because there is no IP addrees on the
Neale Ranns2dd68522017-02-16 03:38:59 -0800421 # client interfce to fill in the option.
Neale Rannsfca0c242017-01-13 07:57:46 -0800422 #
423 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
424 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
425 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
426 BOOTP(op=1) /
427 DHCP(options=[('message-type', 'offer'), ('end')]))
428 pkts = [p]
429
John Lo70bfcaf2017-11-14 13:19:26 -0500430 self.send_and_assert_no_replies(self.pg3, pkts,
Neale Ranns20a175a2017-02-14 07:28:41 -0800431 "Offer DHCP no relay address")
Neale Rannsfca0c242017-01-13 07:57:46 -0800432
433 #
434 # configure an IP address on the client facing interface
435 #
John Lo70bfcaf2017-11-14 13:19:26 -0500436 self.pg3.config_ip4()
Neale Rannsfca0c242017-01-13 07:57:46 -0800437
438 #
439 # Try again with a discover packet
440 # Rx'd packet should be to the server address and from the configured
441 # source address
442 # UDP source ports are unchanged
443 # we've no option 82 config so that should be absent
444 #
John Lo70bfcaf2017-11-14 13:19:26 -0500445 self.pg3.add_stream(pkts_disc_vrf0)
Neale Rannsfca0c242017-01-13 07:57:46 -0800446 self.pg_enable_capture(self.pg_interfaces)
447 self.pg_start()
448
449 rx = self.pg0.get_capture(1)
450 rx = rx[0]
451
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700452 option_82 = self.verify_relayed_dhcp_discover(rx, self.pg0,
John Lo70bfcaf2017-11-14 13:19:26 -0500453 src_intf=self.pg3)
Neale Rannsfca0c242017-01-13 07:57:46 -0800454
455 #
456 # Create an DHCP offer reply from the server with a correctly formatted
457 # option 82. i.e. send back what we just captured
458 # The offer, sent mcast to the client, still has option 82.
459 #
460 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
461 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
462 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
463 BOOTP(op=1) /
464 DHCP(options=[('message-type', 'offer'),
465 ('relay_agent_Information', option_82),
466 ('end')]))
467 pkts = [p]
468
469 self.pg0.add_stream(pkts)
470 self.pg_enable_capture(self.pg_interfaces)
471 self.pg_start()
472
John Lo70bfcaf2017-11-14 13:19:26 -0500473 rx = self.pg3.get_capture(1)
Neale Rannsfca0c242017-01-13 07:57:46 -0800474 rx = rx[0]
475
John Lo70bfcaf2017-11-14 13:19:26 -0500476 self.verify_dhcp_offer(rx, self.pg3)
Neale Rannsfca0c242017-01-13 07:57:46 -0800477
478 #
479 # Bogus Option 82:
480 #
481 # 1. not our IP address = not checked by VPP? so offer is replayed
482 # to client
483 bad_ip = option_82[0:8] + chr(33) + option_82[9:]
484
485 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
486 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
487 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
488 BOOTP(op=1) /
489 DHCP(options=[('message-type', 'offer'),
490 ('relay_agent_Information', bad_ip),
491 ('end')]))
492 pkts = [p]
Neale Ranns20a175a2017-02-14 07:28:41 -0800493 self.send_and_assert_no_replies(self.pg0, pkts,
494 "DHCP offer option 82 bad address")
Neale Rannsfca0c242017-01-13 07:57:46 -0800495
496 # 2. Not a sw_if_index VPP knows
497 bad_if_index = option_82[0:2] + chr(33) + option_82[3:]
498
499 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
500 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
501 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
502 BOOTP(op=1) /
503 DHCP(options=[('message-type', 'offer'),
504 ('relay_agent_Information', bad_if_index),
505 ('end')]))
506 pkts = [p]
507 self.send_and_assert_no_replies(self.pg0, pkts,
508 "DHCP offer option 82 bad if index")
509
510 #
511 # Send a DHCP request in VRF 1. should be dropped.
512 #
John Lo70bfcaf2017-11-14 13:19:26 -0500513 self.send_and_assert_no_replies(self.pg4, pkts_disc_vrf1,
Neale Rannsfca0c242017-01-13 07:57:46 -0800514 "DHCP with no configuration VRF 1")
515
516 #
517 # Delete the DHCP config in VRF 0
518 # Should now drop requests.
519 #
520 self.vapi.dhcp_proxy_config(server_addr,
521 src_addr,
522 rx_table_id=0,
Neale Ranns20a175a2017-02-14 07:28:41 -0800523 is_add=0)
Neale Rannsfca0c242017-01-13 07:57:46 -0800524
John Lo70bfcaf2017-11-14 13:19:26 -0500525 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf0,
Neale Rannsfca0c242017-01-13 07:57:46 -0800526 "DHCP config removed VRF 0")
John Lo70bfcaf2017-11-14 13:19:26 -0500527 self.send_and_assert_no_replies(self.pg4, pkts_disc_vrf1,
Neale Rannsfca0c242017-01-13 07:57:46 -0800528 "DHCP config removed VRF 1")
529
530 #
John Lo70bfcaf2017-11-14 13:19:26 -0500531 # Add DHCP config for VRF 1 & 2
Neale Rannsfca0c242017-01-13 07:57:46 -0800532 #
John Lo70bfcaf2017-11-14 13:19:26 -0500533 server_addr1 = self.pg1.remote_ip4n
534 src_addr1 = self.pg1.local_ip4n
535 self.vapi.dhcp_proxy_config(server_addr1,
536 src_addr1,
Neale Rannsfca0c242017-01-13 07:57:46 -0800537 rx_table_id=1,
Neale Ranns20a175a2017-02-14 07:28:41 -0800538 server_table_id=1)
John Lo70bfcaf2017-11-14 13:19:26 -0500539 server_addr2 = self.pg2.remote_ip4n
540 src_addr2 = self.pg2.local_ip4n
541 self.vapi.dhcp_proxy_config(server_addr2,
542 src_addr2,
543 rx_table_id=2,
544 server_table_id=2)
Neale Rannsfca0c242017-01-13 07:57:46 -0800545
546 #
John Lo70bfcaf2017-11-14 13:19:26 -0500547 # Confim DHCP requests ok in VRF 1 & 2.
Neale Rannsfca0c242017-01-13 07:57:46 -0800548 # - dropped on IP config on client interface
549 #
John Lo70bfcaf2017-11-14 13:19:26 -0500550 self.send_and_assert_no_replies(self.pg4, pkts_disc_vrf1,
Neale Rannsfca0c242017-01-13 07:57:46 -0800551 "DHCP config removed VRF 1")
John Lo70bfcaf2017-11-14 13:19:26 -0500552 self.send_and_assert_no_replies(self.pg5, pkts_disc_vrf2,
553 "DHCP config removed VRF 2")
Neale Rannsfca0c242017-01-13 07:57:46 -0800554
555 #
556 # configure an IP address on the client facing interface
557 #
John Lo70bfcaf2017-11-14 13:19:26 -0500558 self.pg4.config_ip4()
559 self.pg4.add_stream(pkts_disc_vrf1)
Neale Rannsfca0c242017-01-13 07:57:46 -0800560 self.pg_enable_capture(self.pg_interfaces)
561 self.pg_start()
Neale Rannsfca0c242017-01-13 07:57:46 -0800562 rx = self.pg1.get_capture(1)
563 rx = rx[0]
John Lo70bfcaf2017-11-14 13:19:26 -0500564 self.verify_relayed_dhcp_discover(rx, self.pg1, src_intf=self.pg4)
565
566 self.pg5.config_ip4()
567 self.pg5.add_stream(pkts_disc_vrf2)
568 self.pg_enable_capture(self.pg_interfaces)
569 self.pg_start()
570 rx = self.pg2.get_capture(1)
571 rx = rx[0]
572 self.verify_relayed_dhcp_discover(rx, self.pg2, src_intf=self.pg5)
Neale Rannsfca0c242017-01-13 07:57:46 -0800573
574 #
Neale Ranns20a175a2017-02-14 07:28:41 -0800575 # Add VSS config
John Lo70bfcaf2017-11-14 13:19:26 -0500576 # table=1, vss_type=1, vpn_index=1, oui=4
577 # table=2, vss_type=0, vpn_id = "ip4-table-2"
578 self.vapi.dhcp_proxy_set_vss(1, 1, vpn_index=1, oui=4, is_add=1)
579 self.vapi.dhcp_proxy_set_vss(2, 0, "ip4-table-2", is_add=1)
Neale Ranns20a175a2017-02-14 07:28:41 -0800580
John Lo70bfcaf2017-11-14 13:19:26 -0500581 self.pg4.add_stream(pkts_disc_vrf1)
Neale Ranns20a175a2017-02-14 07:28:41 -0800582 self.pg_enable_capture(self.pg_interfaces)
583 self.pg_start()
584
585 rx = self.pg1.get_capture(1)
586 rx = rx[0]
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700587 self.verify_relayed_dhcp_discover(rx, self.pg1,
John Lo70bfcaf2017-11-14 13:19:26 -0500588 src_intf=self.pg4,
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700589 fib_id=1, oui=4)
Neale Ranns20a175a2017-02-14 07:28:41 -0800590
John Lo70bfcaf2017-11-14 13:19:26 -0500591 self.pg5.add_stream(pkts_disc_vrf2)
592 self.pg_enable_capture(self.pg_interfaces)
593 self.pg_start()
594
595 rx = self.pg2.get_capture(1)
596 rx = rx[0]
597 self.verify_relayed_dhcp_discover(rx, self.pg2,
598 src_intf=self.pg5,
599 vpn_id="ip4-table-2")
600
Neale Ranns20a175a2017-02-14 07:28:41 -0800601 #
Neale Ranns3466c302017-02-16 07:45:03 -0800602 # Add a second DHCP server in VRF 1
603 # expect clients messages to be relay to both configured servers
604 #
605 self.pg1.generate_remote_hosts(2)
John Lo70bfcaf2017-11-14 13:19:26 -0500606 server_addr12 = socket.inet_pton(AF_INET, self.pg1.remote_hosts[1].ip4)
Neale Ranns3466c302017-02-16 07:45:03 -0800607
John Lo70bfcaf2017-11-14 13:19:26 -0500608 self.vapi.dhcp_proxy_config(server_addr12,
609 src_addr1,
Neale Ranns3466c302017-02-16 07:45:03 -0800610 rx_table_id=1,
611 server_table_id=1,
612 is_add=1)
613
614 #
615 # We'll need an ARP entry for the server to send it packets
616 #
617 arp_entry = VppNeighbor(self,
618 self.pg1.sw_if_index,
619 self.pg1.remote_hosts[1].mac,
620 self.pg1.remote_hosts[1].ip4)
621 arp_entry.add_vpp_config()
622
623 #
624 # Send a discover from the client. expect two relayed messages
625 # The frist packet is sent to the second server
626 # We're not enforcing that here, it's just the way it is.
627 #
John Lo70bfcaf2017-11-14 13:19:26 -0500628 self.pg4.add_stream(pkts_disc_vrf1)
Neale Ranns3466c302017-02-16 07:45:03 -0800629 self.pg_enable_capture(self.pg_interfaces)
630 self.pg_start()
631
632 rx = self.pg1.get_capture(2)
633
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700634 option_82 = self.verify_relayed_dhcp_discover(
Neale Ranns3466c302017-02-16 07:45:03 -0800635 rx[0], self.pg1,
John Lo70bfcaf2017-11-14 13:19:26 -0500636 src_intf=self.pg4,
Neale Ranns3466c302017-02-16 07:45:03 -0800637 dst_mac=self.pg1.remote_hosts[1].mac,
638 dst_ip=self.pg1.remote_hosts[1].ip4,
639 fib_id=1, oui=4)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700640 self.verify_relayed_dhcp_discover(rx[1], self.pg1,
John Lo70bfcaf2017-11-14 13:19:26 -0500641 src_intf=self.pg4,
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700642 fib_id=1, oui=4)
Neale Ranns3466c302017-02-16 07:45:03 -0800643
644 #
645 # Send both packets back. Client gets both.
646 #
647 p1 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
648 IP(src=self.pg1.remote_ip4, dst=self.pg1.local_ip4) /
649 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
650 BOOTP(op=1) /
651 DHCP(options=[('message-type', 'offer'),
652 ('relay_agent_Information', option_82),
653 ('end')]))
654 p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
655 IP(src=self.pg1.remote_hosts[1].ip4, dst=self.pg1.local_ip4) /
656 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
657 BOOTP(op=1) /
658 DHCP(options=[('message-type', 'offer'),
659 ('relay_agent_Information', option_82),
660 ('end')]))
661 pkts = [p1, p2]
662
663 self.pg1.add_stream(pkts)
664 self.pg_enable_capture(self.pg_interfaces)
665 self.pg_start()
666
John Lo70bfcaf2017-11-14 13:19:26 -0500667 rx = self.pg4.get_capture(2)
Neale Ranns3466c302017-02-16 07:45:03 -0800668
John Lo70bfcaf2017-11-14 13:19:26 -0500669 self.verify_dhcp_offer(rx[0], self.pg4, fib_id=1, oui=4)
670 self.verify_dhcp_offer(rx[1], self.pg4, fib_id=1, oui=4)
Neale Ranns3466c302017-02-16 07:45:03 -0800671
672 #
673 # Ensure offers from non-servers are dropeed
674 #
675 p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
676 IP(src="8.8.8.8", dst=self.pg1.local_ip4) /
677 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
678 BOOTP(op=1) /
679 DHCP(options=[('message-type', 'offer'),
680 ('relay_agent_Information', option_82),
681 ('end')]))
682 self.send_and_assert_no_replies(self.pg1, p2,
683 "DHCP offer from non-server")
684
685 #
686 # Ensure only the discover is sent to multiple servers
687 #
688 p_req_vrf1 = (Ether(dst="ff:ff:ff:ff:ff:ff",
John Lo70bfcaf2017-11-14 13:19:26 -0500689 src=self.pg4.remote_mac) /
Neale Ranns3466c302017-02-16 07:45:03 -0800690 IP(src="0.0.0.0", dst="255.255.255.255") /
691 UDP(sport=DHCP4_CLIENT_PORT,
692 dport=DHCP4_SERVER_PORT) /
693 BOOTP(op=1) /
694 DHCP(options=[('message-type', 'request'),
695 ('end')]))
696
John Lo70bfcaf2017-11-14 13:19:26 -0500697 self.pg4.add_stream(p_req_vrf1)
Neale Ranns3466c302017-02-16 07:45:03 -0800698 self.pg_enable_capture(self.pg_interfaces)
699 self.pg_start()
700
701 rx = self.pg1.get_capture(1)
702
703 #
704 # Remove the second DHCP server
705 #
John Lo70bfcaf2017-11-14 13:19:26 -0500706 self.vapi.dhcp_proxy_config(server_addr12,
707 src_addr1,
Neale Ranns3466c302017-02-16 07:45:03 -0800708 rx_table_id=1,
709 server_table_id=1,
710 is_add=0)
711
712 #
713 # Test we can still relay with the first
714 #
John Lo70bfcaf2017-11-14 13:19:26 -0500715 self.pg4.add_stream(pkts_disc_vrf1)
Neale Ranns3466c302017-02-16 07:45:03 -0800716 self.pg_enable_capture(self.pg_interfaces)
717 self.pg_start()
718
719 rx = self.pg1.get_capture(1)
720 rx = rx[0]
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700721 self.verify_relayed_dhcp_discover(rx, self.pg1,
John Lo70bfcaf2017-11-14 13:19:26 -0500722 src_intf=self.pg4,
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700723 fib_id=1, oui=4)
Neale Ranns3466c302017-02-16 07:45:03 -0800724
725 #
Neale Ranns20a175a2017-02-14 07:28:41 -0800726 # Remove the VSS config
727 # relayed DHCP has default vlaues in the option.
728 #
John Lo70bfcaf2017-11-14 13:19:26 -0500729 self.vapi.dhcp_proxy_set_vss(1, is_add=0)
730 self.vapi.dhcp_proxy_set_vss(2, is_add=0)
Neale Ranns20a175a2017-02-14 07:28:41 -0800731
John Lo70bfcaf2017-11-14 13:19:26 -0500732 self.pg4.add_stream(pkts_disc_vrf1)
Neale Ranns20a175a2017-02-14 07:28:41 -0800733 self.pg_enable_capture(self.pg_interfaces)
734 self.pg_start()
735
736 rx = self.pg1.get_capture(1)
737 rx = rx[0]
John Lo70bfcaf2017-11-14 13:19:26 -0500738 self.verify_relayed_dhcp_discover(rx, self.pg1, src_intf=self.pg4)
Neale Ranns20a175a2017-02-14 07:28:41 -0800739
740 #
Neale Rannsfca0c242017-01-13 07:57:46 -0800741 # remove DHCP config to cleanup
742 #
John Lo70bfcaf2017-11-14 13:19:26 -0500743 self.vapi.dhcp_proxy_config(server_addr1,
744 src_addr1,
Neale Rannsfca0c242017-01-13 07:57:46 -0800745 rx_table_id=1,
Neale Ranns3466c302017-02-16 07:45:03 -0800746 server_table_id=1,
Neale Rannsfca0c242017-01-13 07:57:46 -0800747 is_add=0)
John Lo70bfcaf2017-11-14 13:19:26 -0500748 self.vapi.dhcp_proxy_config(server_addr2,
749 src_addr2,
750 rx_table_id=2,
751 server_table_id=2,
752 is_add=0)
Neale Rannsfca0c242017-01-13 07:57:46 -0800753
John Lo70bfcaf2017-11-14 13:19:26 -0500754 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf0,
Neale Rannsfca0c242017-01-13 07:57:46 -0800755 "DHCP cleanup VRF 0")
John Lo70bfcaf2017-11-14 13:19:26 -0500756 self.send_and_assert_no_replies(self.pg4, pkts_disc_vrf1,
Neale Rannsfca0c242017-01-13 07:57:46 -0800757 "DHCP cleanup VRF 1")
John Lo70bfcaf2017-11-14 13:19:26 -0500758 self.send_and_assert_no_replies(self.pg5, pkts_disc_vrf2,
759 "DHCP cleanup VRF 2")
760
Neale Ranns15002542017-09-10 04:39:11 -0700761 self.pg3.unconfig_ip4()
John Lo70bfcaf2017-11-14 13:19:26 -0500762 self.pg4.unconfig_ip4()
763 self.pg5.unconfig_ip4()
Neale Rannsfca0c242017-01-13 07:57:46 -0800764
765 def test_dhcp6_proxy(self):
766 """ DHCPv6 Proxy"""
767 #
768 # Verify no response to DHCP request without DHCP config
769 #
770 dhcp_solicit_dst = "ff02::1:2"
John Lo70bfcaf2017-11-14 13:19:26 -0500771 dhcp_solicit_src_vrf0 = mk_ll_addr(self.pg3.remote_mac)
772 dhcp_solicit_src_vrf1 = mk_ll_addr(self.pg4.remote_mac)
773 dhcp_solicit_src_vrf2 = mk_ll_addr(self.pg5.remote_mac)
Neale Rannsfca0c242017-01-13 07:57:46 -0800774 server_addr_vrf0 = self.pg0.remote_ip6n
775 src_addr_vrf0 = self.pg0.local_ip6n
776 server_addr_vrf1 = self.pg1.remote_ip6n
777 src_addr_vrf1 = self.pg1.local_ip6n
John Lo70bfcaf2017-11-14 13:19:26 -0500778 server_addr_vrf2 = self.pg2.remote_ip6n
779 src_addr_vrf2 = self.pg2.local_ip6n
Neale Rannsfca0c242017-01-13 07:57:46 -0800780
Neale Rannsfca0c242017-01-13 07:57:46 -0800781 dmac = in6_getnsmac(inet_pton(socket.AF_INET6, dhcp_solicit_dst))
John Lo70bfcaf2017-11-14 13:19:26 -0500782 p_solicit_vrf0 = (Ether(dst=dmac, src=self.pg3.remote_mac) /
Neale Rannsfca0c242017-01-13 07:57:46 -0800783 IPv6(src=dhcp_solicit_src_vrf0,
784 dst=dhcp_solicit_dst) /
785 UDP(sport=DHCP6_SERVER_PORT,
786 dport=DHCP6_CLIENT_PORT) /
787 DHCP6_Solicit())
John Lo70bfcaf2017-11-14 13:19:26 -0500788 p_solicit_vrf1 = (Ether(dst=dmac, src=self.pg4.remote_mac) /
Neale Rannsfca0c242017-01-13 07:57:46 -0800789 IPv6(src=dhcp_solicit_src_vrf1,
790 dst=dhcp_solicit_dst) /
791 UDP(sport=DHCP6_SERVER_PORT,
792 dport=DHCP6_CLIENT_PORT) /
793 DHCP6_Solicit())
John Lo70bfcaf2017-11-14 13:19:26 -0500794 p_solicit_vrf2 = (Ether(dst=dmac, src=self.pg5.remote_mac) /
795 IPv6(src=dhcp_solicit_src_vrf2,
796 dst=dhcp_solicit_dst) /
797 UDP(sport=DHCP6_SERVER_PORT,
798 dport=DHCP6_CLIENT_PORT) /
799 DHCP6_Solicit())
Neale Rannsfca0c242017-01-13 07:57:46 -0800800
John Lo70bfcaf2017-11-14 13:19:26 -0500801 self.send_and_assert_no_replies(self.pg3, p_solicit_vrf0,
Neale Rannsfca0c242017-01-13 07:57:46 -0800802 "DHCP with no configuration")
John Lo70bfcaf2017-11-14 13:19:26 -0500803 self.send_and_assert_no_replies(self.pg4, p_solicit_vrf1,
804 "DHCP with no configuration")
805 self.send_and_assert_no_replies(self.pg5, p_solicit_vrf2,
Neale Rannsfca0c242017-01-13 07:57:46 -0800806 "DHCP with no configuration")
807
808 #
809 # DHCPv6 config in VRF 0.
810 # Packets still dropped because the client facing interface has no
811 # IPv6 config
812 #
813 self.vapi.dhcp_proxy_config(server_addr_vrf0,
814 src_addr_vrf0,
815 rx_table_id=0,
816 server_table_id=0,
Neale Rannsfca0c242017-01-13 07:57:46 -0800817 is_ipv6=1)
818
John Lo70bfcaf2017-11-14 13:19:26 -0500819 self.send_and_assert_no_replies(self.pg3, p_solicit_vrf0,
Neale Rannsfca0c242017-01-13 07:57:46 -0800820 "DHCP with no configuration")
John Lo70bfcaf2017-11-14 13:19:26 -0500821 self.send_and_assert_no_replies(self.pg4, p_solicit_vrf1,
Neale Rannsfca0c242017-01-13 07:57:46 -0800822 "DHCP with no configuration")
823
824 #
825 # configure an IP address on the client facing interface
826 #
John Lo70bfcaf2017-11-14 13:19:26 -0500827 self.pg3.config_ip6()
Neale Rannsfca0c242017-01-13 07:57:46 -0800828
829 #
830 # Now the DHCP requests are relayed to the server
831 #
John Lo70bfcaf2017-11-14 13:19:26 -0500832 self.pg3.add_stream(p_solicit_vrf0)
Neale Rannsfca0c242017-01-13 07:57:46 -0800833 self.pg_enable_capture(self.pg_interfaces)
834 self.pg_start()
835
836 rx = self.pg0.get_capture(1)
Neale Ranns3466c302017-02-16 07:45:03 -0800837
838 self.verify_dhcp6_solicit(rx[0], self.pg0,
Neale Rannsfca0c242017-01-13 07:57:46 -0800839 dhcp_solicit_src_vrf0,
John Lo70bfcaf2017-11-14 13:19:26 -0500840 self.pg3.remote_mac)
Neale Rannsfca0c242017-01-13 07:57:46 -0800841
842 #
843 # Exception cases for rejected relay responses
844 #
845
846 # 1 - not a relay reply
847 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
848 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
849 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
850 DHCP6_Advertise())
John Lo70bfcaf2017-11-14 13:19:26 -0500851 self.send_and_assert_no_replies(self.pg3, p_adv_vrf0,
Neale Rannsfca0c242017-01-13 07:57:46 -0800852 "DHCP6 not a relay reply")
853
854 # 2 - no relay message option
855 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
856 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
857 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
858 DHCP6_RelayReply() /
859 DHCP6_Advertise())
John Lo70bfcaf2017-11-14 13:19:26 -0500860 self.send_and_assert_no_replies(self.pg3, p_adv_vrf0,
Neale Rannsfca0c242017-01-13 07:57:46 -0800861 "DHCP not a relay message")
862
863 # 3 - no circuit ID
864 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
865 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
866 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
867 DHCP6_RelayReply() /
868 DHCP6OptRelayMsg(optlen=0) /
869 DHCP6_Advertise())
John Lo70bfcaf2017-11-14 13:19:26 -0500870 self.send_and_assert_no_replies(self.pg3, p_adv_vrf0,
Neale Rannsfca0c242017-01-13 07:57:46 -0800871 "DHCP6 no circuit ID")
872 # 4 - wrong circuit ID
873 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
874 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
875 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
876 DHCP6_RelayReply() /
877 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x05') /
878 DHCP6OptRelayMsg(optlen=0) /
879 DHCP6_Advertise())
John Lo70bfcaf2017-11-14 13:19:26 -0500880 self.send_and_assert_no_replies(self.pg3, p_adv_vrf0,
Neale Rannsfca0c242017-01-13 07:57:46 -0800881 "DHCP6 wrong circuit ID")
882
883 #
884 # Send the relay response (the advertisement)
885 # - no peer address
886 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
887 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
888 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
889 DHCP6_RelayReply() /
John Lo70bfcaf2017-11-14 13:19:26 -0500890 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x04') /
Neale Rannsfca0c242017-01-13 07:57:46 -0800891 DHCP6OptRelayMsg(optlen=0) /
892 DHCP6_Advertise(trid=1) /
893 DHCP6OptStatusCode(statuscode=0))
894 pkts_adv_vrf0 = [p_adv_vrf0]
895
896 self.pg0.add_stream(pkts_adv_vrf0)
897 self.pg_enable_capture(self.pg_interfaces)
898 self.pg_start()
899
John Lo70bfcaf2017-11-14 13:19:26 -0500900 rx = self.pg3.get_capture(1)
Neale Ranns3466c302017-02-16 07:45:03 -0800901
John Lo70bfcaf2017-11-14 13:19:26 -0500902 self.verify_dhcp6_advert(rx[0], self.pg3, "::")
Neale Rannsfca0c242017-01-13 07:57:46 -0800903
904 #
905 # Send the relay response (the advertisement)
906 # - with peer address
907 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
908 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
909 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
910 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf0) /
John Lo70bfcaf2017-11-14 13:19:26 -0500911 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x04') /
Neale Rannsfca0c242017-01-13 07:57:46 -0800912 DHCP6OptRelayMsg(optlen=0) /
913 DHCP6_Advertise(trid=1) /
914 DHCP6OptStatusCode(statuscode=0))
915 pkts_adv_vrf0 = [p_adv_vrf0]
916
917 self.pg0.add_stream(pkts_adv_vrf0)
918 self.pg_enable_capture(self.pg_interfaces)
919 self.pg_start()
920
John Lo70bfcaf2017-11-14 13:19:26 -0500921 rx = self.pg3.get_capture(1)
Neale Ranns3466c302017-02-16 07:45:03 -0800922
John Lo70bfcaf2017-11-14 13:19:26 -0500923 self.verify_dhcp6_advert(rx[0], self.pg3, dhcp_solicit_src_vrf0)
Neale Rannsfca0c242017-01-13 07:57:46 -0800924
925 #
John Lo70bfcaf2017-11-14 13:19:26 -0500926 # Add all the config for VRF 1 & 2
Neale Rannsfca0c242017-01-13 07:57:46 -0800927 #
928 self.vapi.dhcp_proxy_config(server_addr_vrf1,
929 src_addr_vrf1,
930 rx_table_id=1,
931 server_table_id=1,
Neale Rannsfca0c242017-01-13 07:57:46 -0800932 is_ipv6=1)
John Lo70bfcaf2017-11-14 13:19:26 -0500933 self.pg4.config_ip6()
934
935 self.vapi.dhcp_proxy_config(server_addr_vrf2,
936 src_addr_vrf2,
937 rx_table_id=2,
938 server_table_id=2,
939 is_ipv6=1)
940 self.pg5.config_ip6()
Neale Rannsfca0c242017-01-13 07:57:46 -0800941
942 #
943 # VRF 1 solicit
944 #
John Lo70bfcaf2017-11-14 13:19:26 -0500945 self.pg4.add_stream(p_solicit_vrf1)
Neale Rannsfca0c242017-01-13 07:57:46 -0800946 self.pg_enable_capture(self.pg_interfaces)
947 self.pg_start()
948
949 rx = self.pg1.get_capture(1)
Neale Ranns3466c302017-02-16 07:45:03 -0800950
951 self.verify_dhcp6_solicit(rx[0], self.pg1,
Neale Rannsfca0c242017-01-13 07:57:46 -0800952 dhcp_solicit_src_vrf1,
John Lo70bfcaf2017-11-14 13:19:26 -0500953 self.pg4.remote_mac)
954
955 #
956 # VRF 2 solicit
957 #
958 self.pg5.add_stream(p_solicit_vrf2)
959 self.pg_enable_capture(self.pg_interfaces)
960 self.pg_start()
961
962 rx = self.pg2.get_capture(1)
963
964 self.verify_dhcp6_solicit(rx[0], self.pg2,
965 dhcp_solicit_src_vrf2,
966 self.pg5.remote_mac)
Neale Rannsfca0c242017-01-13 07:57:46 -0800967
968 #
969 # VRF 1 Advert
970 #
971 p_adv_vrf1 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
972 IPv6(dst=self.pg1.local_ip6, src=self.pg1.remote_ip6) /
973 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
974 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) /
John Lo70bfcaf2017-11-14 13:19:26 -0500975 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x05') /
Neale Rannsfca0c242017-01-13 07:57:46 -0800976 DHCP6OptRelayMsg(optlen=0) /
977 DHCP6_Advertise(trid=1) /
978 DHCP6OptStatusCode(statuscode=0))
979 pkts_adv_vrf1 = [p_adv_vrf1]
980
981 self.pg1.add_stream(pkts_adv_vrf1)
982 self.pg_enable_capture(self.pg_interfaces)
983 self.pg_start()
984
John Lo70bfcaf2017-11-14 13:19:26 -0500985 rx = self.pg4.get_capture(1)
Neale Ranns3466c302017-02-16 07:45:03 -0800986
John Lo70bfcaf2017-11-14 13:19:26 -0500987 self.verify_dhcp6_advert(rx[0], self.pg4, dhcp_solicit_src_vrf1)
Neale Rannsfca0c242017-01-13 07:57:46 -0800988
989 #
990 # Add VSS config
John Lo70bfcaf2017-11-14 13:19:26 -0500991 # table=1, vss_type=1, vpn_index=1, oui=4
992 # table=2, vss_type=0, vpn_id = "ip6-table-2"
993 self.vapi.dhcp_proxy_set_vss(1, 1, oui=4, vpn_index=1, is_ip6=1)
994 self.vapi.dhcp_proxy_set_vss(2, 0, "IPv6-table-2", is_ip6=1)
Neale Rannsfca0c242017-01-13 07:57:46 -0800995
John Lo70bfcaf2017-11-14 13:19:26 -0500996 self.pg4.add_stream(p_solicit_vrf1)
Neale Rannsfca0c242017-01-13 07:57:46 -0800997 self.pg_enable_capture(self.pg_interfaces)
998 self.pg_start()
999
1000 rx = self.pg1.get_capture(1)
Neale Ranns3466c302017-02-16 07:45:03 -08001001
1002 self.verify_dhcp6_solicit(rx[0], self.pg1,
Neale Rannsfca0c242017-01-13 07:57:46 -08001003 dhcp_solicit_src_vrf1,
John Lo70bfcaf2017-11-14 13:19:26 -05001004 self.pg4.remote_mac,
Neale Rannsfca0c242017-01-13 07:57:46 -08001005 fib_id=1,
1006 oui=4)
1007
John Lo70bfcaf2017-11-14 13:19:26 -05001008 self.pg5.add_stream(p_solicit_vrf2)
1009 self.pg_enable_capture(self.pg_interfaces)
1010 self.pg_start()
1011
1012 rx = self.pg2.get_capture(1)
1013
1014 self.verify_dhcp6_solicit(rx[0], self.pg2,
1015 dhcp_solicit_src_vrf2,
1016 self.pg5.remote_mac,
1017 vpn_id="IPv6-table-2")
1018
Neale Rannsfca0c242017-01-13 07:57:46 -08001019 #
1020 # Remove the VSS config
1021 # relayed DHCP has default vlaues in the option.
1022 #
John Lo70bfcaf2017-11-14 13:19:26 -05001023 self.vapi.dhcp_proxy_set_vss(1, is_ip6=1, is_add=0)
Neale Rannsfca0c242017-01-13 07:57:46 -08001024
John Lo70bfcaf2017-11-14 13:19:26 -05001025 self.pg4.add_stream(p_solicit_vrf1)
Neale Rannsfca0c242017-01-13 07:57:46 -08001026 self.pg_enable_capture(self.pg_interfaces)
1027 self.pg_start()
1028
1029 rx = self.pg1.get_capture(1)
Neale Ranns3466c302017-02-16 07:45:03 -08001030
1031 self.verify_dhcp6_solicit(rx[0], self.pg1,
1032 dhcp_solicit_src_vrf1,
John Lo70bfcaf2017-11-14 13:19:26 -05001033 self.pg4.remote_mac)
Neale Ranns3466c302017-02-16 07:45:03 -08001034
1035 #
1036 # Add a second DHCP server in VRF 1
1037 # expect clients messages to be relay to both configured servers
1038 #
1039 self.pg1.generate_remote_hosts(2)
John Lo70bfcaf2017-11-14 13:19:26 -05001040 server_addr12 = socket.inet_pton(AF_INET6,
1041 self.pg1.remote_hosts[1].ip6)
Neale Ranns3466c302017-02-16 07:45:03 -08001042
John Lo70bfcaf2017-11-14 13:19:26 -05001043 self.vapi.dhcp_proxy_config(server_addr12,
Neale Ranns3466c302017-02-16 07:45:03 -08001044 src_addr_vrf1,
1045 rx_table_id=1,
1046 server_table_id=1,
1047 is_ipv6=1)
1048
1049 #
1050 # We'll need an ND entry for the server to send it packets
1051 #
1052 nd_entry = VppNeighbor(self,
1053 self.pg1.sw_if_index,
1054 self.pg1.remote_hosts[1].mac,
1055 self.pg1.remote_hosts[1].ip6,
1056 af=AF_INET6)
1057 nd_entry.add_vpp_config()
1058
1059 #
1060 # Send a discover from the client. expect two relayed messages
1061 # The frist packet is sent to the second server
1062 # We're not enforcing that here, it's just the way it is.
1063 #
John Lo70bfcaf2017-11-14 13:19:26 -05001064 self.pg4.add_stream(p_solicit_vrf1)
Neale Ranns3466c302017-02-16 07:45:03 -08001065 self.pg_enable_capture(self.pg_interfaces)
1066 self.pg_start()
1067
1068 rx = self.pg1.get_capture(2)
1069
1070 self.verify_dhcp6_solicit(rx[0], self.pg1,
1071 dhcp_solicit_src_vrf1,
John Lo70bfcaf2017-11-14 13:19:26 -05001072 self.pg4.remote_mac)
Neale Ranns3466c302017-02-16 07:45:03 -08001073 self.verify_dhcp6_solicit(rx[1], self.pg1,
1074 dhcp_solicit_src_vrf1,
John Lo70bfcaf2017-11-14 13:19:26 -05001075 self.pg4.remote_mac,
Neale Ranns3466c302017-02-16 07:45:03 -08001076 dst_mac=self.pg1.remote_hosts[1].mac,
1077 dst_ip=self.pg1.remote_hosts[1].ip6)
1078
1079 #
1080 # Send both packets back. Client gets both.
1081 #
1082 p1 = (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) /
John Lo70bfcaf2017-11-14 13:19:26 -05001086 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x05') /
Neale Ranns3466c302017-02-16 07:45:03 -08001087 DHCP6OptRelayMsg(optlen=0) /
1088 DHCP6_Advertise(trid=1) /
1089 DHCP6OptStatusCode(statuscode=0))
1090 p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_hosts[1].mac) /
1091 IPv6(dst=self.pg1.local_ip6, src=self.pg1._remote_hosts[1].ip6) /
1092 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
1093 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) /
John Lo70bfcaf2017-11-14 13:19:26 -05001094 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x05') /
Neale Ranns3466c302017-02-16 07:45:03 -08001095 DHCP6OptRelayMsg(optlen=0) /
1096 DHCP6_Advertise(trid=1) /
1097 DHCP6OptStatusCode(statuscode=0))
1098
1099 pkts = [p1, p2]
1100
1101 self.pg1.add_stream(pkts)
1102 self.pg_enable_capture(self.pg_interfaces)
1103 self.pg_start()
1104
John Lo70bfcaf2017-11-14 13:19:26 -05001105 rx = self.pg4.get_capture(2)
Neale Ranns3466c302017-02-16 07:45:03 -08001106
John Lo70bfcaf2017-11-14 13:19:26 -05001107 self.verify_dhcp6_advert(rx[0], self.pg4, dhcp_solicit_src_vrf1)
1108 self.verify_dhcp6_advert(rx[1], self.pg4, dhcp_solicit_src_vrf1)
Neale Ranns3466c302017-02-16 07:45:03 -08001109
1110 #
1111 # Ensure only solicit messages are duplicated
1112 #
John Lo70bfcaf2017-11-14 13:19:26 -05001113 p_request_vrf1 = (Ether(dst=dmac, src=self.pg4.remote_mac) /
Neale Ranns3466c302017-02-16 07:45:03 -08001114 IPv6(src=dhcp_solicit_src_vrf1,
1115 dst=dhcp_solicit_dst) /
1116 UDP(sport=DHCP6_SERVER_PORT,
1117 dport=DHCP6_CLIENT_PORT) /
1118 DHCP6_Request())
1119
John Lo70bfcaf2017-11-14 13:19:26 -05001120 self.pg4.add_stream(p_request_vrf1)
Neale Ranns3466c302017-02-16 07:45:03 -08001121 self.pg_enable_capture(self.pg_interfaces)
1122 self.pg_start()
1123
1124 rx = self.pg1.get_capture(1)
1125
1126 #
1127 # Test we drop DHCP packets from addresses that are not configured as
1128 # DHCP servers
1129 #
1130 p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_hosts[1].mac) /
1131 IPv6(dst=self.pg1.local_ip6, src="3001::1") /
1132 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
1133 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) /
John Lo70bfcaf2017-11-14 13:19:26 -05001134 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x05') /
Neale Ranns3466c302017-02-16 07:45:03 -08001135 DHCP6OptRelayMsg(optlen=0) /
1136 DHCP6_Advertise(trid=1) /
1137 DHCP6OptStatusCode(statuscode=0))
1138 self.send_and_assert_no_replies(self.pg1, p2,
1139 "DHCP6 not from server")
1140
1141 #
1142 # Remove the second DHCP server
1143 #
John Lo70bfcaf2017-11-14 13:19:26 -05001144 self.vapi.dhcp_proxy_config(server_addr12,
Neale Ranns3466c302017-02-16 07:45:03 -08001145 src_addr_vrf1,
1146 rx_table_id=1,
1147 server_table_id=1,
1148 is_ipv6=1,
1149 is_add=0)
1150
1151 #
1152 # Test we can still relay with the first
1153 #
John Lo70bfcaf2017-11-14 13:19:26 -05001154 self.pg4.add_stream(p_solicit_vrf1)
Neale Ranns3466c302017-02-16 07:45:03 -08001155 self.pg_enable_capture(self.pg_interfaces)
1156 self.pg_start()
1157
1158 rx = self.pg1.get_capture(1)
1159
1160 self.verify_dhcp6_solicit(rx[0], self.pg1,
Neale Rannsfca0c242017-01-13 07:57:46 -08001161 dhcp_solicit_src_vrf1,
John Lo70bfcaf2017-11-14 13:19:26 -05001162 self.pg4.remote_mac)
Neale Rannsfca0c242017-01-13 07:57:46 -08001163
1164 #
1165 # Cleanup
1166 #
John Lo70bfcaf2017-11-14 13:19:26 -05001167 self.vapi.dhcp_proxy_config(server_addr_vrf2,
1168 src_addr_vrf2,
1169 rx_table_id=2,
1170 server_table_id=2,
1171 is_ipv6=1,
1172 is_add=0)
Neale Rannsfca0c242017-01-13 07:57:46 -08001173 self.vapi.dhcp_proxy_config(server_addr_vrf1,
1174 src_addr_vrf1,
1175 rx_table_id=1,
1176 server_table_id=1,
Neale Rannsfca0c242017-01-13 07:57:46 -08001177 is_ipv6=1,
1178 is_add=0)
Neale Ranns3466c302017-02-16 07:45:03 -08001179 self.vapi.dhcp_proxy_config(server_addr_vrf0,
1180 src_addr_vrf0,
1181 rx_table_id=0,
1182 server_table_id=0,
1183 is_ipv6=1,
1184 is_add=0)
1185
1186 # duplicate delete
1187 self.vapi.dhcp_proxy_config(server_addr_vrf0,
1188 src_addr_vrf0,
Neale Rannsfca0c242017-01-13 07:57:46 -08001189 rx_table_id=0,
1190 server_table_id=0,
Neale Rannsfca0c242017-01-13 07:57:46 -08001191 is_ipv6=1,
1192 is_add=0)
Neale Ranns15002542017-09-10 04:39:11 -07001193 self.pg3.unconfig_ip6()
John Lo70bfcaf2017-11-14 13:19:26 -05001194 self.pg4.unconfig_ip6()
1195 self.pg5.unconfig_ip6()
Neale Rannsfca0c242017-01-13 07:57:46 -08001196
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001197 def test_dhcp_client(self):
1198 """ DHCP Client"""
1199
1200 hostname = 'universal-dp'
1201
1202 self.pg_enable_capture(self.pg_interfaces)
1203
1204 #
John Lo70bfcaf2017-11-14 13:19:26 -05001205 # Configure DHCP client on PG3 and capture the discover sent
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001206 #
John Lo70bfcaf2017-11-14 13:19:26 -05001207 self.vapi.dhcp_client(self.pg3.sw_if_index, hostname)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001208
John Lo70bfcaf2017-11-14 13:19:26 -05001209 rx = self.pg3.get_capture(1)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001210
John Lo70bfcaf2017-11-14 13:19:26 -05001211 self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001212
1213 #
John Lo70bfcaf2017-11-14 13:19:26 -05001214 # Send back on offer, expect the request
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001215 #
John Lo70bfcaf2017-11-14 13:19:26 -05001216 p_offer = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1217 IP(src=self.pg3.remote_ip4, dst="255.255.255.255") /
Neale Ranns808c5b22017-08-02 05:15:07 -07001218 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
John Lo70bfcaf2017-11-14 13:19:26 -05001219 BOOTP(op=1, yiaddr=self.pg3.local_ip4) /
Neale Ranns808c5b22017-08-02 05:15:07 -07001220 DHCP(options=[('message-type', 'offer'),
John Lo70bfcaf2017-11-14 13:19:26 -05001221 ('server_id', self.pg3.remote_ip4),
Neale Ranns808c5b22017-08-02 05:15:07 -07001222 ('end')]))
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001223
John Lo70bfcaf2017-11-14 13:19:26 -05001224 self.pg3.add_stream(p_offer)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001225 self.pg_enable_capture(self.pg_interfaces)
1226 self.pg_start()
1227
John Lo70bfcaf2017-11-14 13:19:26 -05001228 rx = self.pg3.get_capture(1)
1229 self.verify_orig_dhcp_request(rx[0], self.pg3, hostname,
1230 self.pg3.local_ip4)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001231
1232 #
1233 # Send an acknowloedgement
1234 #
John Lo70bfcaf2017-11-14 13:19:26 -05001235 p_ack = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1236 IP(src=self.pg3.remote_ip4, dst="255.255.255.255") /
Neale Ranns808c5b22017-08-02 05:15:07 -07001237 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
John Lo70bfcaf2017-11-14 13:19:26 -05001238 BOOTP(op=1, yiaddr=self.pg3.local_ip4) /
Neale Ranns808c5b22017-08-02 05:15:07 -07001239 DHCP(options=[('message-type', 'ack'),
1240 ('subnet_mask', "255.255.255.0"),
John Lo70bfcaf2017-11-14 13:19:26 -05001241 ('router', self.pg3.remote_ip4),
1242 ('server_id', self.pg3.remote_ip4),
Neale Ranns808c5b22017-08-02 05:15:07 -07001243 ('lease_time', 43200),
1244 ('end')]))
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001245
John Lo70bfcaf2017-11-14 13:19:26 -05001246 self.pg3.add_stream(p_ack)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001247 self.pg_enable_capture(self.pg_interfaces)
1248 self.pg_start()
1249
1250 #
Neale Ranns51822bf2017-07-18 09:26:53 -07001251 # We'll get an ARP request for the router address
1252 #
John Lo70bfcaf2017-11-14 13:19:26 -05001253 rx = self.pg3.get_capture(1)
Neale Ranns51822bf2017-07-18 09:26:53 -07001254
John Lo70bfcaf2017-11-14 13:19:26 -05001255 self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
Neale Ranns51822bf2017-07-18 09:26:53 -07001256 self.pg_enable_capture(self.pg_interfaces)
1257
1258 #
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001259 # At the end of this procedure there should be a connected route
1260 # in the FIB
1261 #
John Lo70bfcaf2017-11-14 13:19:26 -05001262 self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
1263 self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001264
Neale Ranns51822bf2017-07-18 09:26:53 -07001265 # remove the left over ARP entry
John Lo70bfcaf2017-11-14 13:19:26 -05001266 self.vapi.ip_neighbor_add_del(self.pg3.sw_if_index,
1267 mactobinary(self.pg3.remote_mac),
1268 self.pg3.remote_ip4,
Neale Ranns51822bf2017-07-18 09:26:53 -07001269 is_add=0)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001270 #
1271 # remove the DHCP config
1272 #
John Lo70bfcaf2017-11-14 13:19:26 -05001273 self.vapi.dhcp_client(self.pg3.sw_if_index, hostname, is_add=0)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001274
1275 #
1276 # and now the route should be gone
1277 #
John Lo70bfcaf2017-11-14 13:19:26 -05001278 self.assertFalse(find_route(self, self.pg3.local_ip4, 32))
1279 self.assertFalse(find_route(self, self.pg3.local_ip4, 24))
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001280
Neale Ranns51822bf2017-07-18 09:26:53 -07001281 #
Neale Ranns808c5b22017-08-02 05:15:07 -07001282 # Start the procedure again. this time have VPP send the client-ID
Neale Ranns51822bf2017-07-18 09:26:53 -07001283 #
John Lo70bfcaf2017-11-14 13:19:26 -05001284 self.pg3.admin_down()
Neale Ranns808c5b22017-08-02 05:15:07 -07001285 self.sleep(1)
John Lo70bfcaf2017-11-14 13:19:26 -05001286 self.pg3.admin_up()
1287 self.vapi.dhcp_client(self.pg3.sw_if_index, hostname,
1288 client_id=self.pg3.local_mac)
Neale Ranns51822bf2017-07-18 09:26:53 -07001289
John Lo70bfcaf2017-11-14 13:19:26 -05001290 rx = self.pg3.get_capture(1)
Neale Ranns51822bf2017-07-18 09:26:53 -07001291
John Lo70bfcaf2017-11-14 13:19:26 -05001292 self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname,
1293 self.pg3.local_mac)
Neale Ranns51822bf2017-07-18 09:26:53 -07001294
John Lo70bfcaf2017-11-14 13:19:26 -05001295 self.pg3.add_stream(p_offer)
Neale Ranns808c5b22017-08-02 05:15:07 -07001296 self.pg_enable_capture(self.pg_interfaces)
1297 self.pg_start()
1298
John Lo70bfcaf2017-11-14 13:19:26 -05001299 rx = self.pg3.get_capture(1)
1300 self.verify_orig_dhcp_request(rx[0], self.pg3, hostname,
1301 self.pg3.local_ip4)
Neale Ranns808c5b22017-08-02 05:15:07 -07001302
1303 #
1304 # unicast the ack to the offered address
1305 #
John Lo70bfcaf2017-11-14 13:19:26 -05001306 p_ack = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1307 IP(src=self.pg3.remote_ip4, dst=self.pg3.local_ip4) /
Neale Ranns808c5b22017-08-02 05:15:07 -07001308 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
John Lo70bfcaf2017-11-14 13:19:26 -05001309 BOOTP(op=1, yiaddr=self.pg3.local_ip4) /
Neale Ranns808c5b22017-08-02 05:15:07 -07001310 DHCP(options=[('message-type', 'ack'),
1311 ('subnet_mask', "255.255.255.0"),
John Lo70bfcaf2017-11-14 13:19:26 -05001312 ('router', self.pg3.remote_ip4),
1313 ('server_id', self.pg3.remote_ip4),
Neale Ranns808c5b22017-08-02 05:15:07 -07001314 ('lease_time', 43200),
1315 ('end')]))
1316
John Lo70bfcaf2017-11-14 13:19:26 -05001317 self.pg3.add_stream(p_ack)
Neale Ranns808c5b22017-08-02 05:15:07 -07001318 self.pg_enable_capture(self.pg_interfaces)
1319 self.pg_start()
1320
1321 #
Neale Ranns54c6dc42018-01-17 10:29:10 -08001322 # We'll get an ARP request for the router address
1323 #
1324 rx = self.pg3.get_capture(1)
1325
1326 self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
1327 self.pg_enable_capture(self.pg_interfaces)
1328
1329 #
Neale Ranns808c5b22017-08-02 05:15:07 -07001330 # At the end of this procedure there should be a connected route
1331 # in the FIB
1332 #
John Lo70bfcaf2017-11-14 13:19:26 -05001333 self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
1334 self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
Neale Ranns808c5b22017-08-02 05:15:07 -07001335
Neale Ranns51822bf2017-07-18 09:26:53 -07001336 #
1337 # remove the DHCP config
1338 #
John Lo70bfcaf2017-11-14 13:19:26 -05001339 self.vapi.dhcp_client(self.pg3.sw_if_index, hostname, is_add=0)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001340
John Lo70bfcaf2017-11-14 13:19:26 -05001341 self.assertFalse(find_route(self, self.pg3.local_ip4, 32))
1342 self.assertFalse(find_route(self, self.pg3.local_ip4, 24))
Neale Ranns808c5b22017-08-02 05:15:07 -07001343
Neale Ranns54c6dc42018-01-17 10:29:10 -08001344 #
1345 # Rince and repeat, this time with VPP configured not to set
1346 # the braodcast flag in the discover and request messages,
1347 # and for the server to unicast the responses.
1348 #
1349 # Configure DHCP client on PG3 and capture the discover sent
1350 #
1351 self.vapi.dhcp_client(self.pg3.sw_if_index, hostname,
1352 set_broadcast_flag=0)
1353
1354 rx = self.pg3.get_capture(1)
1355
1356 self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname,
1357 broadcast=0)
1358
1359 #
1360 # Send back on offer, unicasted to the offered address.
1361 # Expect the request.
1362 #
1363 p_offer = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1364 IP(src=self.pg3.remote_ip4, dst=self.pg3.local_ip4) /
1365 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1366 BOOTP(op=1, yiaddr=self.pg3.local_ip4) /
1367 DHCP(options=[('message-type', 'offer'),
1368 ('server_id', self.pg3.remote_ip4),
1369 ('end')]))
1370
1371 self.pg3.add_stream(p_offer)
1372 self.pg_enable_capture(self.pg_interfaces)
1373 self.pg_start()
1374
1375 rx = self.pg3.get_capture(1)
1376 self.verify_orig_dhcp_request(rx[0], self.pg3, hostname,
1377 self.pg3.local_ip4,
1378 broadcast=0)
1379
1380 #
1381 # Send an acknowloedgement
1382 #
1383 p_ack = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1384 IP(src=self.pg3.remote_ip4, dst=self.pg3.local_ip4) /
1385 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1386 BOOTP(op=1, yiaddr=self.pg3.local_ip4) /
1387 DHCP(options=[('message-type', 'ack'),
1388 ('subnet_mask', "255.255.255.0"),
1389 ('router', self.pg3.remote_ip4),
1390 ('server_id', self.pg3.remote_ip4),
1391 ('lease_time', 43200),
1392 ('end')]))
1393
1394 self.pg3.add_stream(p_ack)
1395 self.pg_enable_capture(self.pg_interfaces)
1396 self.pg_start()
1397
1398 #
1399 # We'll get an ARP request for the router address
1400 #
1401 rx = self.pg3.get_capture(1)
1402
1403 self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
1404 self.pg_enable_capture(self.pg_interfaces)
1405
1406 #
1407 # At the end of this procedure there should be a connected route
1408 # in the FIB
1409 #
1410 self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
1411 self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
1412
1413 # remove the left over ARP entry
1414 self.vapi.ip_neighbor_add_del(self.pg3.sw_if_index,
1415 mactobinary(self.pg3.remote_mac),
1416 self.pg3.remote_ip4,
1417 is_add=0)
1418 #
1419 # remove the DHCP config
1420 #
1421 self.vapi.dhcp_client(self.pg3.sw_if_index, hostname, is_add=0)
1422
1423 #
1424 # and now the route should be gone
1425 #
1426 self.assertFalse(find_route(self, self.pg3.local_ip4, 32))
1427 self.assertFalse(find_route(self, self.pg3.local_ip4, 24))
1428
Neale Ranns808c5b22017-08-02 05:15:07 -07001429
Neale Rannsfca0c242017-01-13 07:57:46 -08001430if __name__ == '__main__':
1431 unittest.main(testRunner=VppTestRunner)