blob: c5b1fa5fe3bd0579c37fb618310d5c2680e1e1fe [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
Neale Ranns2bc94022018-02-25 12:27:18 -080014from scapy.layers.inet6 import IPv6, in6_getnsmac
15from scapy.utils6 import in6_mactoifaceid
Neale Rannsfca0c242017-01-13 07:57:46 -080016from scapy.layers.dhcp import DHCP, BOOTP, DHCPTypes
17from scapy.layers.dhcp6 import DHCP6, DHCP6_Solicit, DHCP6_RelayForward, \
18 DHCP6_RelayReply, DHCP6_Advertise, DHCP6OptRelayMsg, DHCP6OptIfaceId, \
Neale Ranns3466c302017-02-16 07:45:03 -080019 DHCP6OptStatusCode, DHCP6OptVSS, DHCP6OptClientLinkLayerAddr, DHCP6_Request
Neale Rannsfca0c242017-01-13 07:57:46 -080020from socket import AF_INET, AF_INET6
21from scapy.utils import inet_pton, inet_ntop
22from scapy.utils6 import in6_ptop
Ole Troan895b6e82017-10-20 13:28:20 +020023from util import mactobinary
Neale Rannsfca0c242017-01-13 07:57:46 -080024
25DHCP4_CLIENT_PORT = 68
26DHCP4_SERVER_PORT = 67
27DHCP6_CLIENT_PORT = 547
28DHCP6_SERVER_PORT = 546
29
30
Neale Rannsfca0c242017-01-13 07:57:46 -080031class TestDHCP(VppTestCase):
32 """ DHCP Test Case """
33
34 def setUp(self):
35 super(TestDHCP, self).setUp()
36
John Lo70bfcaf2017-11-14 13:19:26 -050037 # create 6 pg interfaces for pg0 to pg5
38 self.create_pg_interfaces(range(6))
Neale Ranns15002542017-09-10 04:39:11 -070039 self.tables = []
Neale Rannsfca0c242017-01-13 07:57:46 -080040
John Lo70bfcaf2017-11-14 13:19:26 -050041 # pg0 to 2 are IP configured in VRF 0, 1 and 2.
42 # pg3 to 5 are non IP-configured in VRF 0, 1 and 2.
Neale Rannsfca0c242017-01-13 07:57:46 -080043 table_id = 0
Neale Ranns15002542017-09-10 04:39:11 -070044 for table_id in range(1, 4):
45 tbl4 = VppIpTable(self, table_id)
46 tbl4.add_vpp_config()
47 self.tables.append(tbl4)
48 tbl6 = VppIpTable(self, table_id, is_ip6=1)
49 tbl6.add_vpp_config()
50 self.tables.append(tbl6)
51
52 table_id = 0
John Lo70bfcaf2017-11-14 13:19:26 -050053 for i in self.pg_interfaces[:3]:
Neale Rannsfca0c242017-01-13 07:57:46 -080054 i.admin_up()
55 i.set_table_ip4(table_id)
56 i.set_table_ip6(table_id)
57 i.config_ip4()
58 i.resolve_arp()
59 i.config_ip6()
60 i.resolve_ndp()
61 table_id += 1
62
63 table_id = 0
John Lo70bfcaf2017-11-14 13:19:26 -050064 for i in self.pg_interfaces[3:]:
Neale Rannsfca0c242017-01-13 07:57:46 -080065 i.admin_up()
66 i.set_table_ip4(table_id)
67 i.set_table_ip6(table_id)
68 table_id += 1
69
Neale Ranns4008ac92017-02-13 23:20:04 -080070 def tearDown(self):
John Lo70bfcaf2017-11-14 13:19:26 -050071 for i in self.pg_interfaces[:3]:
Neale Ranns4008ac92017-02-13 23:20:04 -080072 i.unconfig_ip4()
73 i.unconfig_ip6()
Neale Ranns15002542017-09-10 04:39:11 -070074
75 for i in self.pg_interfaces:
76 i.set_table_ip4(0)
77 i.set_table_ip6(0)
Neale Ranns4008ac92017-02-13 23:20:04 -080078 i.admin_down()
Neale Ranns15002542017-09-10 04:39:11 -070079 super(TestDHCP, self).tearDown()
Neale Ranns4008ac92017-02-13 23:20:04 -080080
Neale Rannsa2fbf6b2017-07-18 08:23:32 -070081 def verify_dhcp_has_option(self, pkt, option, value):
82 dhcp = pkt[DHCP]
83 found = False
84
85 for i in dhcp.options:
86 if type(i) is tuple:
87 if i[0] == option:
88 self.assertEqual(i[1], value)
89 found = True
90
91 self.assertTrue(found)
92
John Lo70bfcaf2017-11-14 13:19:26 -050093 def validate_relay_options(self, pkt, intf, ip_addr, vpn_id, fib_id, oui):
Neale Rannsfca0c242017-01-13 07:57:46 -080094 dhcp = pkt[DHCP]
95 found = 0
96 data = []
John Lo70bfcaf2017-11-14 13:19:26 -050097 id_len = len(vpn_id)
Neale Rannsfca0c242017-01-13 07:57:46 -080098
99 for i in dhcp.options:
100 if type(i) is tuple:
101 if i[0] == "relay_agent_Information":
102 #
103 # There are two sb-options present - each of length 6.
104 #
105 data = i[1]
Neale Ranns20a175a2017-02-14 07:28:41 -0800106 if oui != 0:
107 self.assertEqual(len(data), 24)
John Lo70bfcaf2017-11-14 13:19:26 -0500108 elif len(vpn_id) > 0:
109 self.assertEqual(len(data), len(vpn_id)+17)
Neale Ranns20a175a2017-02-14 07:28:41 -0800110 else:
111 self.assertEqual(len(data), 12)
Neale Rannsfca0c242017-01-13 07:57:46 -0800112
113 #
114 # First sub-option is ID 1, len 4, then encoded
115 # sw_if_index. This test uses low valued indicies
116 # so [2:4] are 0.
117 # The ID space is VPP internal - so no matching value
118 # scapy
119 #
120 self.assertEqual(ord(data[0]), 1)
121 self.assertEqual(ord(data[1]), 4)
122 self.assertEqual(ord(data[2]), 0)
123 self.assertEqual(ord(data[3]), 0)
124 self.assertEqual(ord(data[4]), 0)
125 self.assertEqual(ord(data[5]), intf._sw_if_index)
126
127 #
128 # next sub-option is the IP address of the client side
129 # interface.
130 # sub-option ID=5, length (of a v4 address)=4
131 #
132 claddr = socket.inet_pton(AF_INET, ip_addr)
133
134 self.assertEqual(ord(data[6]), 5)
135 self.assertEqual(ord(data[7]), 4)
136 self.assertEqual(data[8], claddr[0])
137 self.assertEqual(data[9], claddr[1])
138 self.assertEqual(data[10], claddr[2])
139 self.assertEqual(data[11], claddr[3])
140
Neale Ranns20a175a2017-02-14 07:28:41 -0800141 if oui != 0:
John Lo70bfcaf2017-11-14 13:19:26 -0500142 # sub-option 151 encodes vss_type 1,
143 # the 3 byte oui and the 4 byte fib_id
144 self.assertEqual(id_len, 0)
Neale Ranns20a175a2017-02-14 07:28:41 -0800145 self.assertEqual(ord(data[12]), 151)
146 self.assertEqual(ord(data[13]), 8)
147 self.assertEqual(ord(data[14]), 1)
148 self.assertEqual(ord(data[15]), 0)
149 self.assertEqual(ord(data[16]), 0)
150 self.assertEqual(ord(data[17]), oui)
151 self.assertEqual(ord(data[18]), 0)
152 self.assertEqual(ord(data[19]), 0)
153 self.assertEqual(ord(data[20]), 0)
154 self.assertEqual(ord(data[21]), fib_id)
155
156 # VSS control sub-option
157 self.assertEqual(ord(data[22]), 152)
158 self.assertEqual(ord(data[23]), 0)
159
John Lo70bfcaf2017-11-14 13:19:26 -0500160 if id_len > 0:
161 # sub-option 151 encode vss_type of 0
162 # followerd by vpn_id in ascii
163 self.assertEqual(oui, 0)
164 self.assertEqual(ord(data[12]), 151)
165 self.assertEqual(ord(data[13]), id_len+1)
166 self.assertEqual(ord(data[14]), 0)
167 self.assertEqual(data[15:15+id_len], vpn_id)
168
169 # VSS control sub-option
170 self.assertEqual(ord(data[15+len(vpn_id)]), 152)
171 self.assertEqual(ord(data[16+len(vpn_id)]), 0)
172
Neale Rannsfca0c242017-01-13 07:57:46 -0800173 found = 1
174 self.assertTrue(found)
175
176 return data
177
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700178 def verify_dhcp_msg_type(self, pkt, name):
179 dhcp = pkt[DHCP]
180 found = False
181 for o in dhcp.options:
182 if type(o) is tuple:
183 if o[0] == "message-type" \
184 and DHCPTypes[o[1]] == name:
185 found = True
186 self.assertTrue(found)
187
John Lo70bfcaf2017-11-14 13:19:26 -0500188 def verify_dhcp_offer(self, pkt, intf, vpn_id="", fib_id=0, oui=0):
Neale Rannsfca0c242017-01-13 07:57:46 -0800189 ether = pkt[Ether]
190 self.assertEqual(ether.dst, "ff:ff:ff:ff:ff:ff")
191 self.assertEqual(ether.src, intf.local_mac)
192
193 ip = pkt[IP]
194 self.assertEqual(ip.dst, "255.255.255.255")
195 self.assertEqual(ip.src, intf.local_ip4)
196
197 udp = pkt[UDP]
198 self.assertEqual(udp.dport, DHCP4_CLIENT_PORT)
199 self.assertEqual(udp.sport, DHCP4_SERVER_PORT)
200
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700201 self.verify_dhcp_msg_type(pkt, "offer")
Neale Ranns3466c302017-02-16 07:45:03 -0800202 data = self.validate_relay_options(pkt, intf, intf.local_ip4,
John Lo70bfcaf2017-11-14 13:19:26 -0500203 vpn_id, fib_id, oui)
Neale Rannsfca0c242017-01-13 07:57:46 -0800204
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700205 def verify_orig_dhcp_pkt(self, pkt, intf):
206 ether = pkt[Ether]
207 self.assertEqual(ether.dst, "ff:ff:ff:ff:ff:ff")
208 self.assertEqual(ether.src, intf.local_mac)
209
210 ip = pkt[IP]
211 self.assertEqual(ip.dst, "255.255.255.255")
212 self.assertEqual(ip.src, "0.0.0.0")
213
214 udp = pkt[UDP]
215 self.assertEqual(udp.dport, DHCP4_SERVER_PORT)
216 self.assertEqual(udp.sport, DHCP4_CLIENT_PORT)
217
Neale Ranns54c6dc42018-01-17 10:29:10 -0800218 def verify_orig_dhcp_discover(self, pkt, intf, hostname, client_id=None,
219 broadcast=1):
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700220 self.verify_orig_dhcp_pkt(pkt, intf)
221
222 self.verify_dhcp_msg_type(pkt, "discover")
223 self.verify_dhcp_has_option(pkt, "hostname", hostname)
Neale Ranns51822bf2017-07-18 09:26:53 -0700224 if client_id:
225 self.verify_dhcp_has_option(pkt, "client_id", client_id)
Neale Ranns808c5b22017-08-02 05:15:07 -0700226 bootp = pkt[BOOTP]
227 self.assertEqual(bootp.ciaddr, "0.0.0.0")
228 self.assertEqual(bootp.giaddr, "0.0.0.0")
Neale Ranns54c6dc42018-01-17 10:29:10 -0800229 if broadcast:
230 self.assertEqual(bootp.flags, 0x8000)
231 else:
232 self.assertEqual(bootp.flags, 0x0000)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700233
Neale Ranns54c6dc42018-01-17 10:29:10 -0800234 def verify_orig_dhcp_request(self, pkt, intf, hostname, ip,
235 broadcast=1):
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700236 self.verify_orig_dhcp_pkt(pkt, intf)
237
238 self.verify_dhcp_msg_type(pkt, "request")
239 self.verify_dhcp_has_option(pkt, "hostname", hostname)
240 self.verify_dhcp_has_option(pkt, "requested_addr", ip)
Neale Ranns808c5b22017-08-02 05:15:07 -0700241 bootp = pkt[BOOTP]
242 self.assertEqual(bootp.ciaddr, "0.0.0.0")
243 self.assertEqual(bootp.giaddr, "0.0.0.0")
Neale Ranns54c6dc42018-01-17 10:29:10 -0800244 if broadcast:
245 self.assertEqual(bootp.flags, 0x8000)
246 else:
247 self.assertEqual(bootp.flags, 0x0000)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700248
249 def verify_relayed_dhcp_discover(self, pkt, intf, src_intf=None,
250 fib_id=0, oui=0,
John Lo70bfcaf2017-11-14 13:19:26 -0500251 vpn_id="",
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700252 dst_mac=None, dst_ip=None):
Neale Ranns3466c302017-02-16 07:45:03 -0800253 if not dst_mac:
254 dst_mac = intf.remote_mac
255 if not dst_ip:
256 dst_ip = intf.remote_ip4
257
Neale Rannsfca0c242017-01-13 07:57:46 -0800258 ether = pkt[Ether]
Neale Ranns3466c302017-02-16 07:45:03 -0800259 self.assertEqual(ether.dst, dst_mac)
Neale Rannsfca0c242017-01-13 07:57:46 -0800260 self.assertEqual(ether.src, intf.local_mac)
261
262 ip = pkt[IP]
Neale Ranns3466c302017-02-16 07:45:03 -0800263 self.assertEqual(ip.dst, dst_ip)
Neale Rannsfca0c242017-01-13 07:57:46 -0800264 self.assertEqual(ip.src, intf.local_ip4)
265
266 udp = pkt[UDP]
267 self.assertEqual(udp.dport, DHCP4_SERVER_PORT)
268 self.assertEqual(udp.sport, DHCP4_CLIENT_PORT)
269
270 dhcp = pkt[DHCP]
271
272 is_discover = False
273 for o in dhcp.options:
274 if type(o) is tuple:
275 if o[0] == "message-type" \
276 and DHCPTypes[o[1]] == "discover":
277 is_discover = True
278 self.assertTrue(is_discover)
279
Neale Ranns20a175a2017-02-14 07:28:41 -0800280 data = self.validate_relay_options(pkt, src_intf,
281 src_intf.local_ip4,
John Lo70bfcaf2017-11-14 13:19:26 -0500282 vpn_id,
Neale Ranns20a175a2017-02-14 07:28:41 -0800283 fib_id, oui)
284 return data
Neale Rannsfca0c242017-01-13 07:57:46 -0800285
286 def verify_dhcp6_solicit(self, pkt, intf,
287 peer_ip, peer_mac,
John Lo70bfcaf2017-11-14 13:19:26 -0500288 vpn_id="",
Neale Rannsfca0c242017-01-13 07:57:46 -0800289 fib_id=0,
Neale Ranns3466c302017-02-16 07:45:03 -0800290 oui=0,
291 dst_mac=None,
292 dst_ip=None):
293 if not dst_mac:
294 dst_mac = intf.remote_mac
295 if not dst_ip:
296 dst_ip = in6_ptop(intf.remote_ip6)
297
Neale Rannsfca0c242017-01-13 07:57:46 -0800298 ether = pkt[Ether]
Neale Ranns3466c302017-02-16 07:45:03 -0800299 self.assertEqual(ether.dst, dst_mac)
Neale Rannsfca0c242017-01-13 07:57:46 -0800300 self.assertEqual(ether.src, intf.local_mac)
301
302 ip = pkt[IPv6]
Neale Ranns3466c302017-02-16 07:45:03 -0800303 self.assertEqual(in6_ptop(ip.dst), dst_ip)
Neale Rannsfca0c242017-01-13 07:57:46 -0800304 self.assertEqual(in6_ptop(ip.src), in6_ptop(intf.local_ip6))
305
306 udp = pkt[UDP]
307 self.assertEqual(udp.dport, DHCP6_CLIENT_PORT)
308 self.assertEqual(udp.sport, DHCP6_SERVER_PORT)
309
310 relay = pkt[DHCP6_RelayForward]
311 self.assertEqual(in6_ptop(relay.peeraddr), in6_ptop(peer_ip))
312 oid = pkt[DHCP6OptIfaceId]
313 cll = pkt[DHCP6OptClientLinkLayerAddr]
314 self.assertEqual(cll.optlen, 8)
315 self.assertEqual(cll.lltype, 1)
316 self.assertEqual(cll.clladdr, peer_mac)
317
John Lo70bfcaf2017-11-14 13:19:26 -0500318 id_len = len(vpn_id)
319
Neale Ranns20a175a2017-02-14 07:28:41 -0800320 if fib_id != 0:
John Lo70bfcaf2017-11-14 13:19:26 -0500321 self.assertEqual(id_len, 0)
Neale Ranns20a175a2017-02-14 07:28:41 -0800322 vss = pkt[DHCP6OptVSS]
323 self.assertEqual(vss.optlen, 8)
324 self.assertEqual(vss.type, 1)
325 # the OUI and FIB-id are really 3 and 4 bytes resp.
326 # but the tested range is small
327 self.assertEqual(ord(vss.data[0]), 0)
328 self.assertEqual(ord(vss.data[1]), 0)
329 self.assertEqual(ord(vss.data[2]), oui)
330 self.assertEqual(ord(vss.data[3]), 0)
331 self.assertEqual(ord(vss.data[4]), 0)
332 self.assertEqual(ord(vss.data[5]), 0)
333 self.assertEqual(ord(vss.data[6]), fib_id)
Neale Rannsfca0c242017-01-13 07:57:46 -0800334
John Lo70bfcaf2017-11-14 13:19:26 -0500335 if id_len > 0:
336 self.assertEqual(oui, 0)
337 vss = pkt[DHCP6OptVSS]
338 self.assertEqual(vss.optlen, id_len+1)
339 self.assertEqual(vss.type, 0)
340 self.assertEqual(vss.data[0:id_len], vpn_id)
341
Neale Rannsfca0c242017-01-13 07:57:46 -0800342 # the relay message should be an encoded Solicit
343 msg = pkt[DHCP6OptRelayMsg]
344 sol = DHCP6_Solicit()
345 self.assertEqual(msg.optlen, len(str(sol)))
346 self.assertEqual(str(sol), (str(msg[1]))[:msg.optlen])
347
348 def verify_dhcp6_advert(self, pkt, intf, peer):
349 ether = pkt[Ether]
350 self.assertEqual(ether.dst, "ff:ff:ff:ff:ff:ff")
351 self.assertEqual(ether.src, intf.local_mac)
352
353 ip = pkt[IPv6]
354 self.assertEqual(in6_ptop(ip.dst), in6_ptop(peer))
355 self.assertEqual(in6_ptop(ip.src), in6_ptop(intf.local_ip6))
356
357 udp = pkt[UDP]
358 self.assertEqual(udp.dport, DHCP6_SERVER_PORT)
359 self.assertEqual(udp.sport, DHCP6_CLIENT_PORT)
360
361 # not sure why this is not decoding
362 # adv = pkt[DHCP6_Advertise]
363
364 def test_dhcp_proxy(self):
365 """ DHCPv4 Proxy """
366
367 #
368 # Verify no response to DHCP request without DHCP config
369 #
370 p_disc_vrf0 = (Ether(dst="ff:ff:ff:ff:ff:ff",
John Lo70bfcaf2017-11-14 13:19:26 -0500371 src=self.pg3.remote_mac) /
Neale Rannsfca0c242017-01-13 07:57:46 -0800372 IP(src="0.0.0.0", dst="255.255.255.255") /
373 UDP(sport=DHCP4_CLIENT_PORT,
374 dport=DHCP4_SERVER_PORT) /
375 BOOTP(op=1) /
376 DHCP(options=[('message-type', 'discover'), ('end')]))
377 pkts_disc_vrf0 = [p_disc_vrf0]
378 p_disc_vrf1 = (Ether(dst="ff:ff:ff:ff:ff:ff",
John Lo70bfcaf2017-11-14 13:19:26 -0500379 src=self.pg4.remote_mac) /
Neale Rannsfca0c242017-01-13 07:57:46 -0800380 IP(src="0.0.0.0", dst="255.255.255.255") /
381 UDP(sport=DHCP4_CLIENT_PORT,
382 dport=DHCP4_SERVER_PORT) /
383 BOOTP(op=1) /
384 DHCP(options=[('message-type', 'discover'), ('end')]))
John Lo70bfcaf2017-11-14 13:19:26 -0500385 pkts_disc_vrf1 = [p_disc_vrf1]
386 p_disc_vrf2 = (Ether(dst="ff:ff:ff:ff:ff:ff",
387 src=self.pg5.remote_mac) /
388 IP(src="0.0.0.0", dst="255.255.255.255") /
389 UDP(sport=DHCP4_CLIENT_PORT,
390 dport=DHCP4_SERVER_PORT) /
391 BOOTP(op=1) /
392 DHCP(options=[('message-type', 'discover'), ('end')]))
393 pkts_disc_vrf2 = [p_disc_vrf2]
Neale Rannsfca0c242017-01-13 07:57:46 -0800394
John Lo70bfcaf2017-11-14 13:19:26 -0500395 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf0,
Neale Rannsfca0c242017-01-13 07:57:46 -0800396 "DHCP with no configuration")
John Lo70bfcaf2017-11-14 13:19:26 -0500397 self.send_and_assert_no_replies(self.pg4, pkts_disc_vrf1,
398 "DHCP with no configuration")
399 self.send_and_assert_no_replies(self.pg5, pkts_disc_vrf2,
Neale Rannsfca0c242017-01-13 07:57:46 -0800400 "DHCP with no configuration")
401
402 #
403 # Enable DHCP proxy in VRF 0
404 #
405 server_addr = self.pg0.remote_ip4n
406 src_addr = self.pg0.local_ip4n
407
408 self.vapi.dhcp_proxy_config(server_addr,
409 src_addr,
410 rx_table_id=0)
411
412 #
Neale Ranns20a175a2017-02-14 07:28:41 -0800413 # Discover packets from the client are dropped because there is no
414 # IP address configured on the client facing interface
Neale Rannsfca0c242017-01-13 07:57:46 -0800415 #
John Lo70bfcaf2017-11-14 13:19:26 -0500416 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf0,
Neale Ranns20a175a2017-02-14 07:28:41 -0800417 "Discover DHCP no relay address")
Neale Rannsfca0c242017-01-13 07:57:46 -0800418
419 #
420 # Inject a response from the server
Neale Ranns20a175a2017-02-14 07:28:41 -0800421 # dropped, because there is no IP addrees on the
Neale Ranns2dd68522017-02-16 03:38:59 -0800422 # client interfce to fill in the option.
Neale Rannsfca0c242017-01-13 07:57:46 -0800423 #
424 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
425 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
426 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
427 BOOTP(op=1) /
428 DHCP(options=[('message-type', 'offer'), ('end')]))
429 pkts = [p]
430
John Lo70bfcaf2017-11-14 13:19:26 -0500431 self.send_and_assert_no_replies(self.pg3, pkts,
Neale Ranns20a175a2017-02-14 07:28:41 -0800432 "Offer DHCP no relay address")
Neale Rannsfca0c242017-01-13 07:57:46 -0800433
434 #
435 # configure an IP address on the client facing interface
436 #
John Lo70bfcaf2017-11-14 13:19:26 -0500437 self.pg3.config_ip4()
Neale Rannsfca0c242017-01-13 07:57:46 -0800438
439 #
440 # Try again with a discover packet
441 # Rx'd packet should be to the server address and from the configured
442 # source address
443 # UDP source ports are unchanged
444 # we've no option 82 config so that should be absent
445 #
John Lo70bfcaf2017-11-14 13:19:26 -0500446 self.pg3.add_stream(pkts_disc_vrf0)
Neale Rannsfca0c242017-01-13 07:57:46 -0800447 self.pg_enable_capture(self.pg_interfaces)
448 self.pg_start()
449
450 rx = self.pg0.get_capture(1)
451 rx = rx[0]
452
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700453 option_82 = self.verify_relayed_dhcp_discover(rx, self.pg0,
John Lo70bfcaf2017-11-14 13:19:26 -0500454 src_intf=self.pg3)
Neale Rannsfca0c242017-01-13 07:57:46 -0800455
456 #
457 # Create an DHCP offer reply from the server with a correctly formatted
458 # option 82. i.e. send back what we just captured
459 # The offer, sent mcast to the client, still has option 82.
460 #
461 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
462 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
463 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
464 BOOTP(op=1) /
465 DHCP(options=[('message-type', 'offer'),
466 ('relay_agent_Information', option_82),
467 ('end')]))
468 pkts = [p]
469
470 self.pg0.add_stream(pkts)
471 self.pg_enable_capture(self.pg_interfaces)
472 self.pg_start()
473
John Lo70bfcaf2017-11-14 13:19:26 -0500474 rx = self.pg3.get_capture(1)
Neale Rannsfca0c242017-01-13 07:57:46 -0800475 rx = rx[0]
476
John Lo70bfcaf2017-11-14 13:19:26 -0500477 self.verify_dhcp_offer(rx, self.pg3)
Neale Rannsfca0c242017-01-13 07:57:46 -0800478
479 #
480 # Bogus Option 82:
481 #
482 # 1. not our IP address = not checked by VPP? so offer is replayed
483 # to client
484 bad_ip = option_82[0:8] + chr(33) + option_82[9:]
485
486 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
487 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
488 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
489 BOOTP(op=1) /
490 DHCP(options=[('message-type', 'offer'),
491 ('relay_agent_Information', bad_ip),
492 ('end')]))
493 pkts = [p]
Neale Ranns20a175a2017-02-14 07:28:41 -0800494 self.send_and_assert_no_replies(self.pg0, pkts,
495 "DHCP offer option 82 bad address")
Neale Rannsfca0c242017-01-13 07:57:46 -0800496
497 # 2. Not a sw_if_index VPP knows
498 bad_if_index = option_82[0:2] + chr(33) + option_82[3:]
499
500 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
501 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
502 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
503 BOOTP(op=1) /
504 DHCP(options=[('message-type', 'offer'),
505 ('relay_agent_Information', bad_if_index),
506 ('end')]))
507 pkts = [p]
508 self.send_and_assert_no_replies(self.pg0, pkts,
509 "DHCP offer option 82 bad if index")
510
511 #
512 # Send a DHCP request in VRF 1. should be dropped.
513 #
John Lo70bfcaf2017-11-14 13:19:26 -0500514 self.send_and_assert_no_replies(self.pg4, pkts_disc_vrf1,
Neale Rannsfca0c242017-01-13 07:57:46 -0800515 "DHCP with no configuration VRF 1")
516
517 #
518 # Delete the DHCP config in VRF 0
519 # Should now drop requests.
520 #
521 self.vapi.dhcp_proxy_config(server_addr,
522 src_addr,
523 rx_table_id=0,
Neale Ranns20a175a2017-02-14 07:28:41 -0800524 is_add=0)
Neale Rannsfca0c242017-01-13 07:57:46 -0800525
John Lo70bfcaf2017-11-14 13:19:26 -0500526 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf0,
Neale Rannsfca0c242017-01-13 07:57:46 -0800527 "DHCP config removed VRF 0")
John Lo70bfcaf2017-11-14 13:19:26 -0500528 self.send_and_assert_no_replies(self.pg4, pkts_disc_vrf1,
Neale Rannsfca0c242017-01-13 07:57:46 -0800529 "DHCP config removed VRF 1")
530
531 #
John Lo70bfcaf2017-11-14 13:19:26 -0500532 # Add DHCP config for VRF 1 & 2
Neale Rannsfca0c242017-01-13 07:57:46 -0800533 #
John Lo70bfcaf2017-11-14 13:19:26 -0500534 server_addr1 = self.pg1.remote_ip4n
535 src_addr1 = self.pg1.local_ip4n
536 self.vapi.dhcp_proxy_config(server_addr1,
537 src_addr1,
Neale Rannsfca0c242017-01-13 07:57:46 -0800538 rx_table_id=1,
Neale Ranns20a175a2017-02-14 07:28:41 -0800539 server_table_id=1)
John Lo70bfcaf2017-11-14 13:19:26 -0500540 server_addr2 = self.pg2.remote_ip4n
541 src_addr2 = self.pg2.local_ip4n
542 self.vapi.dhcp_proxy_config(server_addr2,
543 src_addr2,
544 rx_table_id=2,
545 server_table_id=2)
Neale Rannsfca0c242017-01-13 07:57:46 -0800546
547 #
John Lo70bfcaf2017-11-14 13:19:26 -0500548 # Confim DHCP requests ok in VRF 1 & 2.
Neale Rannsfca0c242017-01-13 07:57:46 -0800549 # - dropped on IP config on client interface
550 #
John Lo70bfcaf2017-11-14 13:19:26 -0500551 self.send_and_assert_no_replies(self.pg4, pkts_disc_vrf1,
Neale Rannsfca0c242017-01-13 07:57:46 -0800552 "DHCP config removed VRF 1")
John Lo70bfcaf2017-11-14 13:19:26 -0500553 self.send_and_assert_no_replies(self.pg5, pkts_disc_vrf2,
554 "DHCP config removed VRF 2")
Neale Rannsfca0c242017-01-13 07:57:46 -0800555
556 #
557 # configure an IP address on the client facing interface
558 #
John Lo70bfcaf2017-11-14 13:19:26 -0500559 self.pg4.config_ip4()
560 self.pg4.add_stream(pkts_disc_vrf1)
Neale Rannsfca0c242017-01-13 07:57:46 -0800561 self.pg_enable_capture(self.pg_interfaces)
562 self.pg_start()
Neale Rannsfca0c242017-01-13 07:57:46 -0800563 rx = self.pg1.get_capture(1)
564 rx = rx[0]
John Lo70bfcaf2017-11-14 13:19:26 -0500565 self.verify_relayed_dhcp_discover(rx, self.pg1, src_intf=self.pg4)
566
567 self.pg5.config_ip4()
568 self.pg5.add_stream(pkts_disc_vrf2)
569 self.pg_enable_capture(self.pg_interfaces)
570 self.pg_start()
571 rx = self.pg2.get_capture(1)
572 rx = rx[0]
573 self.verify_relayed_dhcp_discover(rx, self.pg2, src_intf=self.pg5)
Neale Rannsfca0c242017-01-13 07:57:46 -0800574
575 #
Neale Ranns20a175a2017-02-14 07:28:41 -0800576 # Add VSS config
John Lo70bfcaf2017-11-14 13:19:26 -0500577 # table=1, vss_type=1, vpn_index=1, oui=4
578 # table=2, vss_type=0, vpn_id = "ip4-table-2"
579 self.vapi.dhcp_proxy_set_vss(1, 1, vpn_index=1, oui=4, is_add=1)
580 self.vapi.dhcp_proxy_set_vss(2, 0, "ip4-table-2", is_add=1)
Neale Ranns20a175a2017-02-14 07:28:41 -0800581
John Lo70bfcaf2017-11-14 13:19:26 -0500582 self.pg4.add_stream(pkts_disc_vrf1)
Neale Ranns20a175a2017-02-14 07:28:41 -0800583 self.pg_enable_capture(self.pg_interfaces)
584 self.pg_start()
585
586 rx = self.pg1.get_capture(1)
587 rx = rx[0]
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700588 self.verify_relayed_dhcp_discover(rx, self.pg1,
John Lo70bfcaf2017-11-14 13:19:26 -0500589 src_intf=self.pg4,
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700590 fib_id=1, oui=4)
Neale Ranns20a175a2017-02-14 07:28:41 -0800591
John Lo70bfcaf2017-11-14 13:19:26 -0500592 self.pg5.add_stream(pkts_disc_vrf2)
593 self.pg_enable_capture(self.pg_interfaces)
594 self.pg_start()
595
596 rx = self.pg2.get_capture(1)
597 rx = rx[0]
598 self.verify_relayed_dhcp_discover(rx, self.pg2,
599 src_intf=self.pg5,
600 vpn_id="ip4-table-2")
601
Neale Ranns20a175a2017-02-14 07:28:41 -0800602 #
Neale Ranns3466c302017-02-16 07:45:03 -0800603 # Add a second DHCP server in VRF 1
604 # expect clients messages to be relay to both configured servers
605 #
606 self.pg1.generate_remote_hosts(2)
John Lo70bfcaf2017-11-14 13:19:26 -0500607 server_addr12 = socket.inet_pton(AF_INET, self.pg1.remote_hosts[1].ip4)
Neale Ranns3466c302017-02-16 07:45:03 -0800608
John Lo70bfcaf2017-11-14 13:19:26 -0500609 self.vapi.dhcp_proxy_config(server_addr12,
610 src_addr1,
Neale Ranns3466c302017-02-16 07:45:03 -0800611 rx_table_id=1,
612 server_table_id=1,
613 is_add=1)
614
615 #
616 # We'll need an ARP entry for the server to send it packets
617 #
618 arp_entry = VppNeighbor(self,
619 self.pg1.sw_if_index,
620 self.pg1.remote_hosts[1].mac,
621 self.pg1.remote_hosts[1].ip4)
622 arp_entry.add_vpp_config()
623
624 #
625 # Send a discover from the client. expect two relayed messages
626 # The frist packet is sent to the second server
627 # We're not enforcing that here, it's just the way it is.
628 #
John Lo70bfcaf2017-11-14 13:19:26 -0500629 self.pg4.add_stream(pkts_disc_vrf1)
Neale Ranns3466c302017-02-16 07:45:03 -0800630 self.pg_enable_capture(self.pg_interfaces)
631 self.pg_start()
632
633 rx = self.pg1.get_capture(2)
634
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700635 option_82 = self.verify_relayed_dhcp_discover(
Neale Ranns3466c302017-02-16 07:45:03 -0800636 rx[0], self.pg1,
John Lo70bfcaf2017-11-14 13:19:26 -0500637 src_intf=self.pg4,
Neale Ranns3466c302017-02-16 07:45:03 -0800638 dst_mac=self.pg1.remote_hosts[1].mac,
639 dst_ip=self.pg1.remote_hosts[1].ip4,
640 fib_id=1, oui=4)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700641 self.verify_relayed_dhcp_discover(rx[1], self.pg1,
John Lo70bfcaf2017-11-14 13:19:26 -0500642 src_intf=self.pg4,
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700643 fib_id=1, oui=4)
Neale Ranns3466c302017-02-16 07:45:03 -0800644
645 #
646 # Send both packets back. Client gets both.
647 #
648 p1 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
649 IP(src=self.pg1.remote_ip4, dst=self.pg1.local_ip4) /
650 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
651 BOOTP(op=1) /
652 DHCP(options=[('message-type', 'offer'),
653 ('relay_agent_Information', option_82),
654 ('end')]))
655 p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
656 IP(src=self.pg1.remote_hosts[1].ip4, dst=self.pg1.local_ip4) /
657 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
658 BOOTP(op=1) /
659 DHCP(options=[('message-type', 'offer'),
660 ('relay_agent_Information', option_82),
661 ('end')]))
662 pkts = [p1, p2]
663
664 self.pg1.add_stream(pkts)
665 self.pg_enable_capture(self.pg_interfaces)
666 self.pg_start()
667
John Lo70bfcaf2017-11-14 13:19:26 -0500668 rx = self.pg4.get_capture(2)
Neale Ranns3466c302017-02-16 07:45:03 -0800669
John Lo70bfcaf2017-11-14 13:19:26 -0500670 self.verify_dhcp_offer(rx[0], self.pg4, fib_id=1, oui=4)
671 self.verify_dhcp_offer(rx[1], self.pg4, fib_id=1, oui=4)
Neale Ranns3466c302017-02-16 07:45:03 -0800672
673 #
674 # Ensure offers from non-servers are dropeed
675 #
676 p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
677 IP(src="8.8.8.8", dst=self.pg1.local_ip4) /
678 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
679 BOOTP(op=1) /
680 DHCP(options=[('message-type', 'offer'),
681 ('relay_agent_Information', option_82),
682 ('end')]))
683 self.send_and_assert_no_replies(self.pg1, p2,
684 "DHCP offer from non-server")
685
686 #
687 # Ensure only the discover is sent to multiple servers
688 #
689 p_req_vrf1 = (Ether(dst="ff:ff:ff:ff:ff:ff",
John Lo70bfcaf2017-11-14 13:19:26 -0500690 src=self.pg4.remote_mac) /
Neale Ranns3466c302017-02-16 07:45:03 -0800691 IP(src="0.0.0.0", dst="255.255.255.255") /
692 UDP(sport=DHCP4_CLIENT_PORT,
693 dport=DHCP4_SERVER_PORT) /
694 BOOTP(op=1) /
695 DHCP(options=[('message-type', 'request'),
696 ('end')]))
697
John Lo70bfcaf2017-11-14 13:19:26 -0500698 self.pg4.add_stream(p_req_vrf1)
Neale Ranns3466c302017-02-16 07:45:03 -0800699 self.pg_enable_capture(self.pg_interfaces)
700 self.pg_start()
701
702 rx = self.pg1.get_capture(1)
703
704 #
705 # Remove the second DHCP server
706 #
John Lo70bfcaf2017-11-14 13:19:26 -0500707 self.vapi.dhcp_proxy_config(server_addr12,
708 src_addr1,
Neale Ranns3466c302017-02-16 07:45:03 -0800709 rx_table_id=1,
710 server_table_id=1,
711 is_add=0)
712
713 #
714 # Test we can still relay with the first
715 #
John Lo70bfcaf2017-11-14 13:19:26 -0500716 self.pg4.add_stream(pkts_disc_vrf1)
Neale Ranns3466c302017-02-16 07:45:03 -0800717 self.pg_enable_capture(self.pg_interfaces)
718 self.pg_start()
719
720 rx = self.pg1.get_capture(1)
721 rx = rx[0]
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700722 self.verify_relayed_dhcp_discover(rx, self.pg1,
John Lo70bfcaf2017-11-14 13:19:26 -0500723 src_intf=self.pg4,
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700724 fib_id=1, oui=4)
Neale Ranns3466c302017-02-16 07:45:03 -0800725
726 #
Neale Ranns20a175a2017-02-14 07:28:41 -0800727 # Remove the VSS config
728 # relayed DHCP has default vlaues in the option.
729 #
John Lo70bfcaf2017-11-14 13:19:26 -0500730 self.vapi.dhcp_proxy_set_vss(1, is_add=0)
731 self.vapi.dhcp_proxy_set_vss(2, is_add=0)
Neale Ranns20a175a2017-02-14 07:28:41 -0800732
John Lo70bfcaf2017-11-14 13:19:26 -0500733 self.pg4.add_stream(pkts_disc_vrf1)
Neale Ranns20a175a2017-02-14 07:28:41 -0800734 self.pg_enable_capture(self.pg_interfaces)
735 self.pg_start()
736
737 rx = self.pg1.get_capture(1)
738 rx = rx[0]
John Lo70bfcaf2017-11-14 13:19:26 -0500739 self.verify_relayed_dhcp_discover(rx, self.pg1, src_intf=self.pg4)
Neale Ranns20a175a2017-02-14 07:28:41 -0800740
741 #
Neale Rannsfca0c242017-01-13 07:57:46 -0800742 # remove DHCP config to cleanup
743 #
John Lo70bfcaf2017-11-14 13:19:26 -0500744 self.vapi.dhcp_proxy_config(server_addr1,
745 src_addr1,
Neale Rannsfca0c242017-01-13 07:57:46 -0800746 rx_table_id=1,
Neale Ranns3466c302017-02-16 07:45:03 -0800747 server_table_id=1,
Neale Rannsfca0c242017-01-13 07:57:46 -0800748 is_add=0)
John Lo70bfcaf2017-11-14 13:19:26 -0500749 self.vapi.dhcp_proxy_config(server_addr2,
750 src_addr2,
751 rx_table_id=2,
752 server_table_id=2,
753 is_add=0)
Neale Rannsfca0c242017-01-13 07:57:46 -0800754
John Lo70bfcaf2017-11-14 13:19:26 -0500755 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf0,
Neale Rannsfca0c242017-01-13 07:57:46 -0800756 "DHCP cleanup VRF 0")
John Lo70bfcaf2017-11-14 13:19:26 -0500757 self.send_and_assert_no_replies(self.pg4, pkts_disc_vrf1,
Neale Rannsfca0c242017-01-13 07:57:46 -0800758 "DHCP cleanup VRF 1")
John Lo70bfcaf2017-11-14 13:19:26 -0500759 self.send_and_assert_no_replies(self.pg5, pkts_disc_vrf2,
760 "DHCP cleanup VRF 2")
761
Neale Ranns15002542017-09-10 04:39:11 -0700762 self.pg3.unconfig_ip4()
John Lo70bfcaf2017-11-14 13:19:26 -0500763 self.pg4.unconfig_ip4()
764 self.pg5.unconfig_ip4()
Neale Rannsfca0c242017-01-13 07:57:46 -0800765
766 def test_dhcp6_proxy(self):
767 """ DHCPv6 Proxy"""
768 #
769 # Verify no response to DHCP request without DHCP config
770 #
771 dhcp_solicit_dst = "ff02::1:2"
John Lo70bfcaf2017-11-14 13:19:26 -0500772 dhcp_solicit_src_vrf0 = mk_ll_addr(self.pg3.remote_mac)
773 dhcp_solicit_src_vrf1 = mk_ll_addr(self.pg4.remote_mac)
774 dhcp_solicit_src_vrf2 = mk_ll_addr(self.pg5.remote_mac)
Neale Rannsfca0c242017-01-13 07:57:46 -0800775 server_addr_vrf0 = self.pg0.remote_ip6n
776 src_addr_vrf0 = self.pg0.local_ip6n
777 server_addr_vrf1 = self.pg1.remote_ip6n
778 src_addr_vrf1 = self.pg1.local_ip6n
John Lo70bfcaf2017-11-14 13:19:26 -0500779 server_addr_vrf2 = self.pg2.remote_ip6n
780 src_addr_vrf2 = self.pg2.local_ip6n
Neale Rannsfca0c242017-01-13 07:57:46 -0800781
Neale Rannsfca0c242017-01-13 07:57:46 -0800782 dmac = in6_getnsmac(inet_pton(socket.AF_INET6, dhcp_solicit_dst))
John Lo70bfcaf2017-11-14 13:19:26 -0500783 p_solicit_vrf0 = (Ether(dst=dmac, src=self.pg3.remote_mac) /
Neale Rannsfca0c242017-01-13 07:57:46 -0800784 IPv6(src=dhcp_solicit_src_vrf0,
785 dst=dhcp_solicit_dst) /
786 UDP(sport=DHCP6_SERVER_PORT,
787 dport=DHCP6_CLIENT_PORT) /
788 DHCP6_Solicit())
John Lo70bfcaf2017-11-14 13:19:26 -0500789 p_solicit_vrf1 = (Ether(dst=dmac, src=self.pg4.remote_mac) /
Neale Rannsfca0c242017-01-13 07:57:46 -0800790 IPv6(src=dhcp_solicit_src_vrf1,
791 dst=dhcp_solicit_dst) /
792 UDP(sport=DHCP6_SERVER_PORT,
793 dport=DHCP6_CLIENT_PORT) /
794 DHCP6_Solicit())
John Lo70bfcaf2017-11-14 13:19:26 -0500795 p_solicit_vrf2 = (Ether(dst=dmac, src=self.pg5.remote_mac) /
796 IPv6(src=dhcp_solicit_src_vrf2,
797 dst=dhcp_solicit_dst) /
798 UDP(sport=DHCP6_SERVER_PORT,
799 dport=DHCP6_CLIENT_PORT) /
800 DHCP6_Solicit())
Neale Rannsfca0c242017-01-13 07:57:46 -0800801
John Lo70bfcaf2017-11-14 13:19:26 -0500802 self.send_and_assert_no_replies(self.pg3, p_solicit_vrf0,
Neale Rannsfca0c242017-01-13 07:57:46 -0800803 "DHCP with no configuration")
John Lo70bfcaf2017-11-14 13:19:26 -0500804 self.send_and_assert_no_replies(self.pg4, p_solicit_vrf1,
805 "DHCP with no configuration")
806 self.send_and_assert_no_replies(self.pg5, p_solicit_vrf2,
Neale Rannsfca0c242017-01-13 07:57:46 -0800807 "DHCP with no configuration")
808
809 #
810 # DHCPv6 config in VRF 0.
811 # Packets still dropped because the client facing interface has no
812 # IPv6 config
813 #
814 self.vapi.dhcp_proxy_config(server_addr_vrf0,
815 src_addr_vrf0,
816 rx_table_id=0,
817 server_table_id=0,
Neale Rannsfca0c242017-01-13 07:57:46 -0800818 is_ipv6=1)
819
John Lo70bfcaf2017-11-14 13:19:26 -0500820 self.send_and_assert_no_replies(self.pg3, p_solicit_vrf0,
Neale Rannsfca0c242017-01-13 07:57:46 -0800821 "DHCP with no configuration")
John Lo70bfcaf2017-11-14 13:19:26 -0500822 self.send_and_assert_no_replies(self.pg4, p_solicit_vrf1,
Neale Rannsfca0c242017-01-13 07:57:46 -0800823 "DHCP with no configuration")
824
825 #
826 # configure an IP address on the client facing interface
827 #
John Lo70bfcaf2017-11-14 13:19:26 -0500828 self.pg3.config_ip6()
Neale Rannsfca0c242017-01-13 07:57:46 -0800829
830 #
831 # Now the DHCP requests are relayed to the server
832 #
John Lo70bfcaf2017-11-14 13:19:26 -0500833 self.pg3.add_stream(p_solicit_vrf0)
Neale Rannsfca0c242017-01-13 07:57:46 -0800834 self.pg_enable_capture(self.pg_interfaces)
835 self.pg_start()
836
837 rx = self.pg0.get_capture(1)
Neale Ranns3466c302017-02-16 07:45:03 -0800838
839 self.verify_dhcp6_solicit(rx[0], self.pg0,
Neale Rannsfca0c242017-01-13 07:57:46 -0800840 dhcp_solicit_src_vrf0,
John Lo70bfcaf2017-11-14 13:19:26 -0500841 self.pg3.remote_mac)
Neale Rannsfca0c242017-01-13 07:57:46 -0800842
843 #
844 # Exception cases for rejected relay responses
845 #
846
847 # 1 - not a relay reply
848 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
849 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
850 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
851 DHCP6_Advertise())
John Lo70bfcaf2017-11-14 13:19:26 -0500852 self.send_and_assert_no_replies(self.pg3, p_adv_vrf0,
Neale Rannsfca0c242017-01-13 07:57:46 -0800853 "DHCP6 not a relay reply")
854
855 # 2 - no relay message option
856 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
857 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
858 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
859 DHCP6_RelayReply() /
860 DHCP6_Advertise())
John Lo70bfcaf2017-11-14 13:19:26 -0500861 self.send_and_assert_no_replies(self.pg3, p_adv_vrf0,
Neale Rannsfca0c242017-01-13 07:57:46 -0800862 "DHCP not a relay message")
863
864 # 3 - no circuit ID
865 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
866 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
867 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
868 DHCP6_RelayReply() /
869 DHCP6OptRelayMsg(optlen=0) /
870 DHCP6_Advertise())
John Lo70bfcaf2017-11-14 13:19:26 -0500871 self.send_and_assert_no_replies(self.pg3, p_adv_vrf0,
Neale Rannsfca0c242017-01-13 07:57:46 -0800872 "DHCP6 no circuit ID")
873 # 4 - wrong circuit ID
874 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
875 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
876 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
877 DHCP6_RelayReply() /
878 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x05') /
879 DHCP6OptRelayMsg(optlen=0) /
880 DHCP6_Advertise())
John Lo70bfcaf2017-11-14 13:19:26 -0500881 self.send_and_assert_no_replies(self.pg3, p_adv_vrf0,
Neale Rannsfca0c242017-01-13 07:57:46 -0800882 "DHCP6 wrong circuit ID")
883
884 #
885 # Send the relay response (the advertisement)
886 # - no peer address
887 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
888 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
889 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
890 DHCP6_RelayReply() /
John Lo70bfcaf2017-11-14 13:19:26 -0500891 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x04') /
Neale Rannsfca0c242017-01-13 07:57:46 -0800892 DHCP6OptRelayMsg(optlen=0) /
893 DHCP6_Advertise(trid=1) /
894 DHCP6OptStatusCode(statuscode=0))
895 pkts_adv_vrf0 = [p_adv_vrf0]
896
897 self.pg0.add_stream(pkts_adv_vrf0)
898 self.pg_enable_capture(self.pg_interfaces)
899 self.pg_start()
900
John Lo70bfcaf2017-11-14 13:19:26 -0500901 rx = self.pg3.get_capture(1)
Neale Ranns3466c302017-02-16 07:45:03 -0800902
John Lo70bfcaf2017-11-14 13:19:26 -0500903 self.verify_dhcp6_advert(rx[0], self.pg3, "::")
Neale Rannsfca0c242017-01-13 07:57:46 -0800904
905 #
906 # Send the relay response (the advertisement)
907 # - with peer address
908 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
909 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
910 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
911 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf0) /
John Lo70bfcaf2017-11-14 13:19:26 -0500912 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x04') /
Neale Rannsfca0c242017-01-13 07:57:46 -0800913 DHCP6OptRelayMsg(optlen=0) /
914 DHCP6_Advertise(trid=1) /
915 DHCP6OptStatusCode(statuscode=0))
916 pkts_adv_vrf0 = [p_adv_vrf0]
917
918 self.pg0.add_stream(pkts_adv_vrf0)
919 self.pg_enable_capture(self.pg_interfaces)
920 self.pg_start()
921
John Lo70bfcaf2017-11-14 13:19:26 -0500922 rx = self.pg3.get_capture(1)
Neale Ranns3466c302017-02-16 07:45:03 -0800923
John Lo70bfcaf2017-11-14 13:19:26 -0500924 self.verify_dhcp6_advert(rx[0], self.pg3, dhcp_solicit_src_vrf0)
Neale Rannsfca0c242017-01-13 07:57:46 -0800925
926 #
John Lo70bfcaf2017-11-14 13:19:26 -0500927 # Add all the config for VRF 1 & 2
Neale Rannsfca0c242017-01-13 07:57:46 -0800928 #
929 self.vapi.dhcp_proxy_config(server_addr_vrf1,
930 src_addr_vrf1,
931 rx_table_id=1,
932 server_table_id=1,
Neale Rannsfca0c242017-01-13 07:57:46 -0800933 is_ipv6=1)
John Lo70bfcaf2017-11-14 13:19:26 -0500934 self.pg4.config_ip6()
935
936 self.vapi.dhcp_proxy_config(server_addr_vrf2,
937 src_addr_vrf2,
938 rx_table_id=2,
939 server_table_id=2,
940 is_ipv6=1)
941 self.pg5.config_ip6()
Neale Rannsfca0c242017-01-13 07:57:46 -0800942
943 #
944 # VRF 1 solicit
945 #
John Lo70bfcaf2017-11-14 13:19:26 -0500946 self.pg4.add_stream(p_solicit_vrf1)
Neale Rannsfca0c242017-01-13 07:57:46 -0800947 self.pg_enable_capture(self.pg_interfaces)
948 self.pg_start()
949
950 rx = self.pg1.get_capture(1)
Neale Ranns3466c302017-02-16 07:45:03 -0800951
952 self.verify_dhcp6_solicit(rx[0], self.pg1,
Neale Rannsfca0c242017-01-13 07:57:46 -0800953 dhcp_solicit_src_vrf1,
John Lo70bfcaf2017-11-14 13:19:26 -0500954 self.pg4.remote_mac)
955
956 #
957 # VRF 2 solicit
958 #
959 self.pg5.add_stream(p_solicit_vrf2)
960 self.pg_enable_capture(self.pg_interfaces)
961 self.pg_start()
962
963 rx = self.pg2.get_capture(1)
964
965 self.verify_dhcp6_solicit(rx[0], self.pg2,
966 dhcp_solicit_src_vrf2,
967 self.pg5.remote_mac)
Neale Rannsfca0c242017-01-13 07:57:46 -0800968
969 #
970 # VRF 1 Advert
971 #
972 p_adv_vrf1 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
973 IPv6(dst=self.pg1.local_ip6, src=self.pg1.remote_ip6) /
974 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
975 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) /
John Lo70bfcaf2017-11-14 13:19:26 -0500976 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x05') /
Neale Rannsfca0c242017-01-13 07:57:46 -0800977 DHCP6OptRelayMsg(optlen=0) /
978 DHCP6_Advertise(trid=1) /
979 DHCP6OptStatusCode(statuscode=0))
980 pkts_adv_vrf1 = [p_adv_vrf1]
981
982 self.pg1.add_stream(pkts_adv_vrf1)
983 self.pg_enable_capture(self.pg_interfaces)
984 self.pg_start()
985
John Lo70bfcaf2017-11-14 13:19:26 -0500986 rx = self.pg4.get_capture(1)
Neale Ranns3466c302017-02-16 07:45:03 -0800987
John Lo70bfcaf2017-11-14 13:19:26 -0500988 self.verify_dhcp6_advert(rx[0], self.pg4, dhcp_solicit_src_vrf1)
Neale Rannsfca0c242017-01-13 07:57:46 -0800989
990 #
991 # Add VSS config
John Lo70bfcaf2017-11-14 13:19:26 -0500992 # table=1, vss_type=1, vpn_index=1, oui=4
993 # table=2, vss_type=0, vpn_id = "ip6-table-2"
994 self.vapi.dhcp_proxy_set_vss(1, 1, oui=4, vpn_index=1, is_ip6=1)
995 self.vapi.dhcp_proxy_set_vss(2, 0, "IPv6-table-2", is_ip6=1)
Neale Rannsfca0c242017-01-13 07:57:46 -0800996
John Lo70bfcaf2017-11-14 13:19:26 -0500997 self.pg4.add_stream(p_solicit_vrf1)
Neale Rannsfca0c242017-01-13 07:57:46 -0800998 self.pg_enable_capture(self.pg_interfaces)
999 self.pg_start()
1000
1001 rx = self.pg1.get_capture(1)
Neale Ranns3466c302017-02-16 07:45:03 -08001002
1003 self.verify_dhcp6_solicit(rx[0], self.pg1,
Neale Rannsfca0c242017-01-13 07:57:46 -08001004 dhcp_solicit_src_vrf1,
John Lo70bfcaf2017-11-14 13:19:26 -05001005 self.pg4.remote_mac,
Neale Rannsfca0c242017-01-13 07:57:46 -08001006 fib_id=1,
1007 oui=4)
1008
John Lo70bfcaf2017-11-14 13:19:26 -05001009 self.pg5.add_stream(p_solicit_vrf2)
1010 self.pg_enable_capture(self.pg_interfaces)
1011 self.pg_start()
1012
1013 rx = self.pg2.get_capture(1)
1014
1015 self.verify_dhcp6_solicit(rx[0], self.pg2,
1016 dhcp_solicit_src_vrf2,
1017 self.pg5.remote_mac,
1018 vpn_id="IPv6-table-2")
1019
Neale Rannsfca0c242017-01-13 07:57:46 -08001020 #
1021 # Remove the VSS config
1022 # relayed DHCP has default vlaues in the option.
1023 #
John Lo70bfcaf2017-11-14 13:19:26 -05001024 self.vapi.dhcp_proxy_set_vss(1, is_ip6=1, is_add=0)
Neale Rannsfca0c242017-01-13 07:57:46 -08001025
John Lo70bfcaf2017-11-14 13:19:26 -05001026 self.pg4.add_stream(p_solicit_vrf1)
Neale Rannsfca0c242017-01-13 07:57:46 -08001027 self.pg_enable_capture(self.pg_interfaces)
1028 self.pg_start()
1029
1030 rx = self.pg1.get_capture(1)
Neale Ranns3466c302017-02-16 07:45:03 -08001031
1032 self.verify_dhcp6_solicit(rx[0], self.pg1,
1033 dhcp_solicit_src_vrf1,
John Lo70bfcaf2017-11-14 13:19:26 -05001034 self.pg4.remote_mac)
Neale Ranns3466c302017-02-16 07:45:03 -08001035
1036 #
1037 # Add a second DHCP server in VRF 1
1038 # expect clients messages to be relay to both configured servers
1039 #
1040 self.pg1.generate_remote_hosts(2)
John Lo70bfcaf2017-11-14 13:19:26 -05001041 server_addr12 = socket.inet_pton(AF_INET6,
1042 self.pg1.remote_hosts[1].ip6)
Neale Ranns3466c302017-02-16 07:45:03 -08001043
John Lo70bfcaf2017-11-14 13:19:26 -05001044 self.vapi.dhcp_proxy_config(server_addr12,
Neale Ranns3466c302017-02-16 07:45:03 -08001045 src_addr_vrf1,
1046 rx_table_id=1,
1047 server_table_id=1,
1048 is_ipv6=1)
1049
1050 #
1051 # We'll need an ND entry for the server to send it packets
1052 #
1053 nd_entry = VppNeighbor(self,
1054 self.pg1.sw_if_index,
1055 self.pg1.remote_hosts[1].mac,
1056 self.pg1.remote_hosts[1].ip6,
1057 af=AF_INET6)
1058 nd_entry.add_vpp_config()
1059
1060 #
1061 # Send a discover from the client. expect two relayed messages
1062 # The frist packet is sent to the second server
1063 # We're not enforcing that here, it's just the way it is.
1064 #
John Lo70bfcaf2017-11-14 13:19:26 -05001065 self.pg4.add_stream(p_solicit_vrf1)
Neale Ranns3466c302017-02-16 07:45:03 -08001066 self.pg_enable_capture(self.pg_interfaces)
1067 self.pg_start()
1068
1069 rx = self.pg1.get_capture(2)
1070
1071 self.verify_dhcp6_solicit(rx[0], self.pg1,
1072 dhcp_solicit_src_vrf1,
John Lo70bfcaf2017-11-14 13:19:26 -05001073 self.pg4.remote_mac)
Neale Ranns3466c302017-02-16 07:45:03 -08001074 self.verify_dhcp6_solicit(rx[1], self.pg1,
1075 dhcp_solicit_src_vrf1,
John Lo70bfcaf2017-11-14 13:19:26 -05001076 self.pg4.remote_mac,
Neale Ranns3466c302017-02-16 07:45:03 -08001077 dst_mac=self.pg1.remote_hosts[1].mac,
1078 dst_ip=self.pg1.remote_hosts[1].ip6)
1079
1080 #
1081 # Send both packets back. Client gets both.
1082 #
1083 p1 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
1084 IPv6(dst=self.pg1.local_ip6, src=self.pg1.remote_ip6) /
1085 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
1086 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) /
John Lo70bfcaf2017-11-14 13:19:26 -05001087 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x05') /
Neale Ranns3466c302017-02-16 07:45:03 -08001088 DHCP6OptRelayMsg(optlen=0) /
1089 DHCP6_Advertise(trid=1) /
1090 DHCP6OptStatusCode(statuscode=0))
1091 p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_hosts[1].mac) /
1092 IPv6(dst=self.pg1.local_ip6, src=self.pg1._remote_hosts[1].ip6) /
1093 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
1094 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) /
John Lo70bfcaf2017-11-14 13:19:26 -05001095 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x05') /
Neale Ranns3466c302017-02-16 07:45:03 -08001096 DHCP6OptRelayMsg(optlen=0) /
1097 DHCP6_Advertise(trid=1) /
1098 DHCP6OptStatusCode(statuscode=0))
1099
1100 pkts = [p1, p2]
1101
1102 self.pg1.add_stream(pkts)
1103 self.pg_enable_capture(self.pg_interfaces)
1104 self.pg_start()
1105
John Lo70bfcaf2017-11-14 13:19:26 -05001106 rx = self.pg4.get_capture(2)
Neale Ranns3466c302017-02-16 07:45:03 -08001107
John Lo70bfcaf2017-11-14 13:19:26 -05001108 self.verify_dhcp6_advert(rx[0], self.pg4, dhcp_solicit_src_vrf1)
1109 self.verify_dhcp6_advert(rx[1], self.pg4, dhcp_solicit_src_vrf1)
Neale Ranns3466c302017-02-16 07:45:03 -08001110
1111 #
1112 # Ensure only solicit messages are duplicated
1113 #
John Lo70bfcaf2017-11-14 13:19:26 -05001114 p_request_vrf1 = (Ether(dst=dmac, src=self.pg4.remote_mac) /
Neale Ranns3466c302017-02-16 07:45:03 -08001115 IPv6(src=dhcp_solicit_src_vrf1,
1116 dst=dhcp_solicit_dst) /
1117 UDP(sport=DHCP6_SERVER_PORT,
1118 dport=DHCP6_CLIENT_PORT) /
1119 DHCP6_Request())
1120
John Lo70bfcaf2017-11-14 13:19:26 -05001121 self.pg4.add_stream(p_request_vrf1)
Neale Ranns3466c302017-02-16 07:45:03 -08001122 self.pg_enable_capture(self.pg_interfaces)
1123 self.pg_start()
1124
1125 rx = self.pg1.get_capture(1)
1126
1127 #
1128 # Test we drop DHCP packets from addresses that are not configured as
1129 # DHCP servers
1130 #
1131 p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_hosts[1].mac) /
1132 IPv6(dst=self.pg1.local_ip6, src="3001::1") /
1133 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
1134 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) /
John Lo70bfcaf2017-11-14 13:19:26 -05001135 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x05') /
Neale Ranns3466c302017-02-16 07:45:03 -08001136 DHCP6OptRelayMsg(optlen=0) /
1137 DHCP6_Advertise(trid=1) /
1138 DHCP6OptStatusCode(statuscode=0))
1139 self.send_and_assert_no_replies(self.pg1, p2,
1140 "DHCP6 not from server")
1141
1142 #
1143 # Remove the second DHCP server
1144 #
John Lo70bfcaf2017-11-14 13:19:26 -05001145 self.vapi.dhcp_proxy_config(server_addr12,
Neale Ranns3466c302017-02-16 07:45:03 -08001146 src_addr_vrf1,
1147 rx_table_id=1,
1148 server_table_id=1,
1149 is_ipv6=1,
1150 is_add=0)
1151
1152 #
1153 # Test we can still relay with the first
1154 #
John Lo70bfcaf2017-11-14 13:19:26 -05001155 self.pg4.add_stream(p_solicit_vrf1)
Neale Ranns3466c302017-02-16 07:45:03 -08001156 self.pg_enable_capture(self.pg_interfaces)
1157 self.pg_start()
1158
1159 rx = self.pg1.get_capture(1)
1160
1161 self.verify_dhcp6_solicit(rx[0], self.pg1,
Neale Rannsfca0c242017-01-13 07:57:46 -08001162 dhcp_solicit_src_vrf1,
John Lo70bfcaf2017-11-14 13:19:26 -05001163 self.pg4.remote_mac)
Neale Rannsfca0c242017-01-13 07:57:46 -08001164
1165 #
1166 # Cleanup
1167 #
John Lo70bfcaf2017-11-14 13:19:26 -05001168 self.vapi.dhcp_proxy_config(server_addr_vrf2,
1169 src_addr_vrf2,
1170 rx_table_id=2,
1171 server_table_id=2,
1172 is_ipv6=1,
1173 is_add=0)
Neale Rannsfca0c242017-01-13 07:57:46 -08001174 self.vapi.dhcp_proxy_config(server_addr_vrf1,
1175 src_addr_vrf1,
1176 rx_table_id=1,
1177 server_table_id=1,
Neale Rannsfca0c242017-01-13 07:57:46 -08001178 is_ipv6=1,
1179 is_add=0)
Neale Ranns3466c302017-02-16 07:45:03 -08001180 self.vapi.dhcp_proxy_config(server_addr_vrf0,
1181 src_addr_vrf0,
1182 rx_table_id=0,
1183 server_table_id=0,
1184 is_ipv6=1,
1185 is_add=0)
1186
1187 # duplicate delete
1188 self.vapi.dhcp_proxy_config(server_addr_vrf0,
1189 src_addr_vrf0,
Neale Rannsfca0c242017-01-13 07:57:46 -08001190 rx_table_id=0,
1191 server_table_id=0,
Neale Rannsfca0c242017-01-13 07:57:46 -08001192 is_ipv6=1,
1193 is_add=0)
Neale Ranns15002542017-09-10 04:39:11 -07001194 self.pg3.unconfig_ip6()
John Lo70bfcaf2017-11-14 13:19:26 -05001195 self.pg4.unconfig_ip6()
1196 self.pg5.unconfig_ip6()
Neale Rannsfca0c242017-01-13 07:57:46 -08001197
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001198 def test_dhcp_client(self):
1199 """ DHCP Client"""
1200
1201 hostname = 'universal-dp'
1202
1203 self.pg_enable_capture(self.pg_interfaces)
1204
1205 #
John Lo70bfcaf2017-11-14 13:19:26 -05001206 # Configure DHCP client on PG3 and capture the discover sent
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001207 #
John Lo70bfcaf2017-11-14 13:19:26 -05001208 self.vapi.dhcp_client(self.pg3.sw_if_index, hostname)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001209
John Lo70bfcaf2017-11-14 13:19:26 -05001210 rx = self.pg3.get_capture(1)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001211
John Lo70bfcaf2017-11-14 13:19:26 -05001212 self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001213
1214 #
John Lo70bfcaf2017-11-14 13:19:26 -05001215 # Send back on offer, expect the request
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001216 #
John Lo70bfcaf2017-11-14 13:19:26 -05001217 p_offer = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1218 IP(src=self.pg3.remote_ip4, dst="255.255.255.255") /
Neale Ranns808c5b22017-08-02 05:15:07 -07001219 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
Neale Ranns1f2f4b72018-03-23 11:53:12 -07001220 BOOTP(op=1,
1221 yiaddr=self.pg3.local_ip4,
1222 chaddr=mactobinary(self.pg3.local_mac)) /
Neale Ranns808c5b22017-08-02 05:15:07 -07001223 DHCP(options=[('message-type', 'offer'),
John Lo70bfcaf2017-11-14 13:19:26 -05001224 ('server_id', self.pg3.remote_ip4),
Neale Ranns808c5b22017-08-02 05:15:07 -07001225 ('end')]))
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001226
John Lo70bfcaf2017-11-14 13:19:26 -05001227 self.pg3.add_stream(p_offer)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001228 self.pg_enable_capture(self.pg_interfaces)
1229 self.pg_start()
1230
John Lo70bfcaf2017-11-14 13:19:26 -05001231 rx = self.pg3.get_capture(1)
1232 self.verify_orig_dhcp_request(rx[0], self.pg3, hostname,
1233 self.pg3.local_ip4)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001234
1235 #
1236 # Send an acknowloedgement
1237 #
John Lo70bfcaf2017-11-14 13:19:26 -05001238 p_ack = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1239 IP(src=self.pg3.remote_ip4, dst="255.255.255.255") /
Neale Ranns808c5b22017-08-02 05:15:07 -07001240 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
Neale Ranns1f2f4b72018-03-23 11:53:12 -07001241 BOOTP(op=1, yiaddr=self.pg3.local_ip4,
1242 chaddr=mactobinary(self.pg3.local_mac)) /
Neale Ranns808c5b22017-08-02 05:15:07 -07001243 DHCP(options=[('message-type', 'ack'),
1244 ('subnet_mask', "255.255.255.0"),
John Lo70bfcaf2017-11-14 13:19:26 -05001245 ('router', self.pg3.remote_ip4),
1246 ('server_id', self.pg3.remote_ip4),
Neale Ranns808c5b22017-08-02 05:15:07 -07001247 ('lease_time', 43200),
1248 ('end')]))
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001249
John Lo70bfcaf2017-11-14 13:19:26 -05001250 self.pg3.add_stream(p_ack)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001251 self.pg_enable_capture(self.pg_interfaces)
1252 self.pg_start()
1253
1254 #
Neale Ranns51822bf2017-07-18 09:26:53 -07001255 # We'll get an ARP request for the router address
1256 #
John Lo70bfcaf2017-11-14 13:19:26 -05001257 rx = self.pg3.get_capture(1)
Neale Ranns51822bf2017-07-18 09:26:53 -07001258
John Lo70bfcaf2017-11-14 13:19:26 -05001259 self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
Neale Ranns51822bf2017-07-18 09:26:53 -07001260 self.pg_enable_capture(self.pg_interfaces)
1261
1262 #
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001263 # At the end of this procedure there should be a connected route
1264 # in the FIB
1265 #
John Lo70bfcaf2017-11-14 13:19:26 -05001266 self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
1267 self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001268
Neale Ranns51822bf2017-07-18 09:26:53 -07001269 # remove the left over ARP entry
John Lo70bfcaf2017-11-14 13:19:26 -05001270 self.vapi.ip_neighbor_add_del(self.pg3.sw_if_index,
1271 mactobinary(self.pg3.remote_mac),
1272 self.pg3.remote_ip4,
Neale Ranns51822bf2017-07-18 09:26:53 -07001273 is_add=0)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001274 #
1275 # remove the DHCP config
1276 #
John Lo70bfcaf2017-11-14 13:19:26 -05001277 self.vapi.dhcp_client(self.pg3.sw_if_index, hostname, is_add=0)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001278
1279 #
1280 # and now the route should be gone
1281 #
John Lo70bfcaf2017-11-14 13:19:26 -05001282 self.assertFalse(find_route(self, self.pg3.local_ip4, 32))
1283 self.assertFalse(find_route(self, self.pg3.local_ip4, 24))
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001284
Neale Ranns51822bf2017-07-18 09:26:53 -07001285 #
Neale Ranns808c5b22017-08-02 05:15:07 -07001286 # Start the procedure again. this time have VPP send the client-ID
Neale Ranns51822bf2017-07-18 09:26:53 -07001287 #
John Lo70bfcaf2017-11-14 13:19:26 -05001288 self.pg3.admin_down()
Neale Ranns808c5b22017-08-02 05:15:07 -07001289 self.sleep(1)
John Lo70bfcaf2017-11-14 13:19:26 -05001290 self.pg3.admin_up()
1291 self.vapi.dhcp_client(self.pg3.sw_if_index, hostname,
1292 client_id=self.pg3.local_mac)
Neale Ranns51822bf2017-07-18 09:26:53 -07001293
John Lo70bfcaf2017-11-14 13:19:26 -05001294 rx = self.pg3.get_capture(1)
Neale Ranns51822bf2017-07-18 09:26:53 -07001295
John Lo70bfcaf2017-11-14 13:19:26 -05001296 self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname,
1297 self.pg3.local_mac)
Neale Ranns51822bf2017-07-18 09:26:53 -07001298
John Lo70bfcaf2017-11-14 13:19:26 -05001299 self.pg3.add_stream(p_offer)
Neale Ranns808c5b22017-08-02 05:15:07 -07001300 self.pg_enable_capture(self.pg_interfaces)
1301 self.pg_start()
1302
John Lo70bfcaf2017-11-14 13:19:26 -05001303 rx = self.pg3.get_capture(1)
1304 self.verify_orig_dhcp_request(rx[0], self.pg3, hostname,
1305 self.pg3.local_ip4)
Neale Ranns808c5b22017-08-02 05:15:07 -07001306
1307 #
1308 # unicast the ack to the offered address
1309 #
John Lo70bfcaf2017-11-14 13:19:26 -05001310 p_ack = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1311 IP(src=self.pg3.remote_ip4, dst=self.pg3.local_ip4) /
Neale Ranns808c5b22017-08-02 05:15:07 -07001312 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
Neale Ranns1f2f4b72018-03-23 11:53:12 -07001313 BOOTP(op=1, yiaddr=self.pg3.local_ip4,
1314 chaddr=mactobinary(self.pg3.local_mac)) /
Neale Ranns808c5b22017-08-02 05:15:07 -07001315 DHCP(options=[('message-type', 'ack'),
1316 ('subnet_mask', "255.255.255.0"),
John Lo70bfcaf2017-11-14 13:19:26 -05001317 ('router', self.pg3.remote_ip4),
1318 ('server_id', self.pg3.remote_ip4),
Neale Ranns808c5b22017-08-02 05:15:07 -07001319 ('lease_time', 43200),
1320 ('end')]))
1321
John Lo70bfcaf2017-11-14 13:19:26 -05001322 self.pg3.add_stream(p_ack)
Neale Ranns808c5b22017-08-02 05:15:07 -07001323 self.pg_enable_capture(self.pg_interfaces)
1324 self.pg_start()
1325
1326 #
Neale Ranns54c6dc42018-01-17 10:29:10 -08001327 # We'll get an ARP request for the router address
1328 #
1329 rx = self.pg3.get_capture(1)
1330
1331 self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
1332 self.pg_enable_capture(self.pg_interfaces)
1333
1334 #
Neale Ranns808c5b22017-08-02 05:15:07 -07001335 # At the end of this procedure there should be a connected route
1336 # in the FIB
1337 #
John Lo70bfcaf2017-11-14 13:19:26 -05001338 self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
1339 self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
Neale Ranns808c5b22017-08-02 05:15:07 -07001340
Neale Ranns51822bf2017-07-18 09:26:53 -07001341 #
1342 # remove the DHCP config
1343 #
John Lo70bfcaf2017-11-14 13:19:26 -05001344 self.vapi.dhcp_client(self.pg3.sw_if_index, hostname, is_add=0)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001345
John Lo70bfcaf2017-11-14 13:19:26 -05001346 self.assertFalse(find_route(self, self.pg3.local_ip4, 32))
1347 self.assertFalse(find_route(self, self.pg3.local_ip4, 24))
Neale Ranns808c5b22017-08-02 05:15:07 -07001348
Neale Ranns54c6dc42018-01-17 10:29:10 -08001349 #
1350 # Rince and repeat, this time with VPP configured not to set
1351 # the braodcast flag in the discover and request messages,
1352 # and for the server to unicast the responses.
1353 #
1354 # Configure DHCP client on PG3 and capture the discover sent
1355 #
1356 self.vapi.dhcp_client(self.pg3.sw_if_index, hostname,
1357 set_broadcast_flag=0)
1358
1359 rx = self.pg3.get_capture(1)
1360
1361 self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname,
1362 broadcast=0)
1363
1364 #
1365 # Send back on offer, unicasted to the offered address.
1366 # Expect the request.
1367 #
1368 p_offer = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1369 IP(src=self.pg3.remote_ip4, dst=self.pg3.local_ip4) /
1370 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
Neale Ranns1f2f4b72018-03-23 11:53:12 -07001371 BOOTP(op=1, yiaddr=self.pg3.local_ip4,
1372 chaddr=mactobinary(self.pg3.local_mac)) /
Neale Ranns54c6dc42018-01-17 10:29:10 -08001373 DHCP(options=[('message-type', 'offer'),
1374 ('server_id', self.pg3.remote_ip4),
1375 ('end')]))
1376
1377 self.pg3.add_stream(p_offer)
1378 self.pg_enable_capture(self.pg_interfaces)
1379 self.pg_start()
1380
1381 rx = self.pg3.get_capture(1)
1382 self.verify_orig_dhcp_request(rx[0], self.pg3, hostname,
1383 self.pg3.local_ip4,
1384 broadcast=0)
1385
1386 #
1387 # Send an acknowloedgement
1388 #
1389 p_ack = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1390 IP(src=self.pg3.remote_ip4, dst=self.pg3.local_ip4) /
1391 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
Neale Ranns1f2f4b72018-03-23 11:53:12 -07001392 BOOTP(op=1, yiaddr=self.pg3.local_ip4,
1393 chaddr=mactobinary(self.pg3.local_mac)) /
Neale Ranns54c6dc42018-01-17 10:29:10 -08001394 DHCP(options=[('message-type', 'ack'),
1395 ('subnet_mask', "255.255.255.0"),
1396 ('router', self.pg3.remote_ip4),
1397 ('server_id', self.pg3.remote_ip4),
1398 ('lease_time', 43200),
1399 ('end')]))
1400
1401 self.pg3.add_stream(p_ack)
1402 self.pg_enable_capture(self.pg_interfaces)
1403 self.pg_start()
1404
1405 #
1406 # We'll get an ARP request for the router address
1407 #
1408 rx = self.pg3.get_capture(1)
1409
1410 self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
1411 self.pg_enable_capture(self.pg_interfaces)
1412
1413 #
1414 # At the end of this procedure there should be a connected route
1415 # in the FIB
1416 #
1417 self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
1418 self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
1419
1420 # remove the left over ARP entry
1421 self.vapi.ip_neighbor_add_del(self.pg3.sw_if_index,
1422 mactobinary(self.pg3.remote_mac),
1423 self.pg3.remote_ip4,
1424 is_add=0)
1425 #
1426 # remove the DHCP config
1427 #
1428 self.vapi.dhcp_client(self.pg3.sw_if_index, hostname, is_add=0)
1429
1430 #
1431 # and now the route should be gone
1432 #
1433 self.assertFalse(find_route(self, self.pg3.local_ip4, 32))
1434 self.assertFalse(find_route(self, self.pg3.local_ip4, 24))
1435
Neale Ranns808c5b22017-08-02 05:15:07 -07001436
Neale Rannsfca0c242017-01-13 07:57:46 -08001437if __name__ == '__main__':
1438 unittest.main(testRunner=VppTestRunner)