blob: 42b80af35d70026bf075642eb936c11ef19897e5 [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
7from framework import VppTestCase, VppTestRunner
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
36 # create 3 pg interfaces
37 self.create_pg_interfaces(range(4))
Neale Ranns15002542017-09-10 04:39:11 -070038 self.tables = []
Neale Rannsfca0c242017-01-13 07:57:46 -080039
40 # pg0 and 1 are IP configured in VRF 0 and 1.
41 # pg2 and 3 are non IP-configured in VRF 0 and 1
42 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
Neale Rannsfca0c242017-01-13 07:57:46 -080052 for i in self.pg_interfaces[:2]:
53 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
63 for i in self.pg_interfaces[2:]:
64 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):
Neale Ranns15002542017-09-10 04:39:11 -070070 for i in self.pg_interfaces[:2]:
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 Rannsfca0c242017-01-13 07:57:46 -080080 def send_and_assert_no_replies(self, intf, pkts, remark):
81 intf.add_stream(pkts)
82 self.pg_enable_capture(self.pg_interfaces)
83 self.pg_start()
84 for i in self.pg_interfaces:
85 i.assert_nothing_captured(remark=remark)
86
Neale Rannsa2fbf6b2017-07-18 08:23:32 -070087 def verify_dhcp_has_option(self, pkt, option, value):
88 dhcp = pkt[DHCP]
89 found = False
90
91 for i in dhcp.options:
92 if type(i) is tuple:
93 if i[0] == option:
94 self.assertEqual(i[1], value)
95 found = True
96
97 self.assertTrue(found)
98
Neale Ranns20a175a2017-02-14 07:28:41 -080099 def validate_relay_options(self, pkt, intf, ip_addr, fib_id, oui):
Neale Rannsfca0c242017-01-13 07:57:46 -0800100 dhcp = pkt[DHCP]
101 found = 0
102 data = []
103
104 for i in dhcp.options:
105 if type(i) is tuple:
106 if i[0] == "relay_agent_Information":
107 #
108 # There are two sb-options present - each of length 6.
109 #
110 data = i[1]
Neale Ranns20a175a2017-02-14 07:28:41 -0800111 if oui != 0:
112 self.assertEqual(len(data), 24)
113 else:
114 self.assertEqual(len(data), 12)
Neale Rannsfca0c242017-01-13 07:57:46 -0800115
116 #
117 # First sub-option is ID 1, len 4, then encoded
118 # sw_if_index. This test uses low valued indicies
119 # so [2:4] are 0.
120 # The ID space is VPP internal - so no matching value
121 # scapy
122 #
123 self.assertEqual(ord(data[0]), 1)
124 self.assertEqual(ord(data[1]), 4)
125 self.assertEqual(ord(data[2]), 0)
126 self.assertEqual(ord(data[3]), 0)
127 self.assertEqual(ord(data[4]), 0)
128 self.assertEqual(ord(data[5]), intf._sw_if_index)
129
130 #
131 # next sub-option is the IP address of the client side
132 # interface.
133 # sub-option ID=5, length (of a v4 address)=4
134 #
135 claddr = socket.inet_pton(AF_INET, ip_addr)
136
137 self.assertEqual(ord(data[6]), 5)
138 self.assertEqual(ord(data[7]), 4)
139 self.assertEqual(data[8], claddr[0])
140 self.assertEqual(data[9], claddr[1])
141 self.assertEqual(data[10], claddr[2])
142 self.assertEqual(data[11], claddr[3])
143
Neale Ranns20a175a2017-02-14 07:28:41 -0800144 if oui != 0:
145 # sub-option 151 encodes the 3 byte oui
146 # and the 4 byte fib_id
147 self.assertEqual(ord(data[12]), 151)
148 self.assertEqual(ord(data[13]), 8)
149 self.assertEqual(ord(data[14]), 1)
150 self.assertEqual(ord(data[15]), 0)
151 self.assertEqual(ord(data[16]), 0)
152 self.assertEqual(ord(data[17]), oui)
153 self.assertEqual(ord(data[18]), 0)
154 self.assertEqual(ord(data[19]), 0)
155 self.assertEqual(ord(data[20]), 0)
156 self.assertEqual(ord(data[21]), fib_id)
157
158 # VSS control sub-option
159 self.assertEqual(ord(data[22]), 152)
160 self.assertEqual(ord(data[23]), 0)
161
Neale Rannsfca0c242017-01-13 07:57:46 -0800162 found = 1
163 self.assertTrue(found)
164
165 return data
166
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700167 def verify_dhcp_msg_type(self, pkt, name):
168 dhcp = pkt[DHCP]
169 found = False
170 for o in dhcp.options:
171 if type(o) is tuple:
172 if o[0] == "message-type" \
173 and DHCPTypes[o[1]] == name:
174 found = True
175 self.assertTrue(found)
176
Neale Ranns3466c302017-02-16 07:45:03 -0800177 def verify_dhcp_offer(self, pkt, intf, fib_id=0, oui=0):
Neale Rannsfca0c242017-01-13 07:57:46 -0800178 ether = pkt[Ether]
179 self.assertEqual(ether.dst, "ff:ff:ff:ff:ff:ff")
180 self.assertEqual(ether.src, intf.local_mac)
181
182 ip = pkt[IP]
183 self.assertEqual(ip.dst, "255.255.255.255")
184 self.assertEqual(ip.src, intf.local_ip4)
185
186 udp = pkt[UDP]
187 self.assertEqual(udp.dport, DHCP4_CLIENT_PORT)
188 self.assertEqual(udp.sport, DHCP4_SERVER_PORT)
189
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700190 self.verify_dhcp_msg_type(pkt, "offer")
Neale Ranns3466c302017-02-16 07:45:03 -0800191 data = self.validate_relay_options(pkt, intf, intf.local_ip4,
192 fib_id, oui)
Neale Rannsfca0c242017-01-13 07:57:46 -0800193
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700194 def verify_orig_dhcp_pkt(self, pkt, intf):
195 ether = pkt[Ether]
196 self.assertEqual(ether.dst, "ff:ff:ff:ff:ff:ff")
197 self.assertEqual(ether.src, intf.local_mac)
198
199 ip = pkt[IP]
200 self.assertEqual(ip.dst, "255.255.255.255")
201 self.assertEqual(ip.src, "0.0.0.0")
202
203 udp = pkt[UDP]
204 self.assertEqual(udp.dport, DHCP4_SERVER_PORT)
205 self.assertEqual(udp.sport, DHCP4_CLIENT_PORT)
206
Neale Ranns51822bf2017-07-18 09:26:53 -0700207 def verify_orig_dhcp_discover(self, pkt, intf, hostname, client_id=None):
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700208 self.verify_orig_dhcp_pkt(pkt, intf)
209
210 self.verify_dhcp_msg_type(pkt, "discover")
211 self.verify_dhcp_has_option(pkt, "hostname", hostname)
Neale Ranns51822bf2017-07-18 09:26:53 -0700212 if client_id:
213 self.verify_dhcp_has_option(pkt, "client_id", client_id)
Neale Ranns808c5b22017-08-02 05:15:07 -0700214 bootp = pkt[BOOTP]
215 self.assertEqual(bootp.ciaddr, "0.0.0.0")
216 self.assertEqual(bootp.giaddr, "0.0.0.0")
217 self.assertEqual(bootp.flags, 0x8000)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700218
219 def verify_orig_dhcp_request(self, pkt, intf, hostname, ip):
220 self.verify_orig_dhcp_pkt(pkt, intf)
221
222 self.verify_dhcp_msg_type(pkt, "request")
223 self.verify_dhcp_has_option(pkt, "hostname", hostname)
224 self.verify_dhcp_has_option(pkt, "requested_addr", ip)
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")
228 self.assertEqual(bootp.flags, 0x8000)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700229
230 def verify_relayed_dhcp_discover(self, pkt, intf, src_intf=None,
231 fib_id=0, oui=0,
232 dst_mac=None, dst_ip=None):
Neale Ranns3466c302017-02-16 07:45:03 -0800233 if not dst_mac:
234 dst_mac = intf.remote_mac
235 if not dst_ip:
236 dst_ip = intf.remote_ip4
237
Neale Rannsfca0c242017-01-13 07:57:46 -0800238 ether = pkt[Ether]
Neale Ranns3466c302017-02-16 07:45:03 -0800239 self.assertEqual(ether.dst, dst_mac)
Neale Rannsfca0c242017-01-13 07:57:46 -0800240 self.assertEqual(ether.src, intf.local_mac)
241
242 ip = pkt[IP]
Neale Ranns3466c302017-02-16 07:45:03 -0800243 self.assertEqual(ip.dst, dst_ip)
Neale Rannsfca0c242017-01-13 07:57:46 -0800244 self.assertEqual(ip.src, intf.local_ip4)
245
246 udp = pkt[UDP]
247 self.assertEqual(udp.dport, DHCP4_SERVER_PORT)
248 self.assertEqual(udp.sport, DHCP4_CLIENT_PORT)
249
250 dhcp = pkt[DHCP]
251
252 is_discover = False
253 for o in dhcp.options:
254 if type(o) is tuple:
255 if o[0] == "message-type" \
256 and DHCPTypes[o[1]] == "discover":
257 is_discover = True
258 self.assertTrue(is_discover)
259
Neale Ranns20a175a2017-02-14 07:28:41 -0800260 data = self.validate_relay_options(pkt, src_intf,
261 src_intf.local_ip4,
262 fib_id, oui)
263 return data
Neale Rannsfca0c242017-01-13 07:57:46 -0800264
265 def verify_dhcp6_solicit(self, pkt, intf,
266 peer_ip, peer_mac,
267 fib_id=0,
Neale Ranns3466c302017-02-16 07:45:03 -0800268 oui=0,
269 dst_mac=None,
270 dst_ip=None):
271 if not dst_mac:
272 dst_mac = intf.remote_mac
273 if not dst_ip:
274 dst_ip = in6_ptop(intf.remote_ip6)
275
Neale Rannsfca0c242017-01-13 07:57:46 -0800276 ether = pkt[Ether]
Neale Ranns3466c302017-02-16 07:45:03 -0800277 self.assertEqual(ether.dst, dst_mac)
Neale Rannsfca0c242017-01-13 07:57:46 -0800278 self.assertEqual(ether.src, intf.local_mac)
279
280 ip = pkt[IPv6]
Neale Ranns3466c302017-02-16 07:45:03 -0800281 self.assertEqual(in6_ptop(ip.dst), dst_ip)
Neale Rannsfca0c242017-01-13 07:57:46 -0800282 self.assertEqual(in6_ptop(ip.src), in6_ptop(intf.local_ip6))
283
284 udp = pkt[UDP]
285 self.assertEqual(udp.dport, DHCP6_CLIENT_PORT)
286 self.assertEqual(udp.sport, DHCP6_SERVER_PORT)
287
288 relay = pkt[DHCP6_RelayForward]
289 self.assertEqual(in6_ptop(relay.peeraddr), in6_ptop(peer_ip))
290 oid = pkt[DHCP6OptIfaceId]
291 cll = pkt[DHCP6OptClientLinkLayerAddr]
292 self.assertEqual(cll.optlen, 8)
293 self.assertEqual(cll.lltype, 1)
294 self.assertEqual(cll.clladdr, peer_mac)
295
Neale Ranns20a175a2017-02-14 07:28:41 -0800296 if fib_id != 0:
297 vss = pkt[DHCP6OptVSS]
298 self.assertEqual(vss.optlen, 8)
299 self.assertEqual(vss.type, 1)
300 # the OUI and FIB-id are really 3 and 4 bytes resp.
301 # but the tested range is small
302 self.assertEqual(ord(vss.data[0]), 0)
303 self.assertEqual(ord(vss.data[1]), 0)
304 self.assertEqual(ord(vss.data[2]), oui)
305 self.assertEqual(ord(vss.data[3]), 0)
306 self.assertEqual(ord(vss.data[4]), 0)
307 self.assertEqual(ord(vss.data[5]), 0)
308 self.assertEqual(ord(vss.data[6]), fib_id)
Neale Rannsfca0c242017-01-13 07:57:46 -0800309
310 # the relay message should be an encoded Solicit
311 msg = pkt[DHCP6OptRelayMsg]
312 sol = DHCP6_Solicit()
313 self.assertEqual(msg.optlen, len(str(sol)))
314 self.assertEqual(str(sol), (str(msg[1]))[:msg.optlen])
315
316 def verify_dhcp6_advert(self, pkt, intf, peer):
317 ether = pkt[Ether]
318 self.assertEqual(ether.dst, "ff:ff:ff:ff:ff:ff")
319 self.assertEqual(ether.src, intf.local_mac)
320
321 ip = pkt[IPv6]
322 self.assertEqual(in6_ptop(ip.dst), in6_ptop(peer))
323 self.assertEqual(in6_ptop(ip.src), in6_ptop(intf.local_ip6))
324
325 udp = pkt[UDP]
326 self.assertEqual(udp.dport, DHCP6_SERVER_PORT)
327 self.assertEqual(udp.sport, DHCP6_CLIENT_PORT)
328
329 # not sure why this is not decoding
330 # adv = pkt[DHCP6_Advertise]
331
332 def test_dhcp_proxy(self):
333 """ DHCPv4 Proxy """
334
335 #
336 # Verify no response to DHCP request without DHCP config
337 #
338 p_disc_vrf0 = (Ether(dst="ff:ff:ff:ff:ff:ff",
339 src=self.pg2.remote_mac) /
340 IP(src="0.0.0.0", dst="255.255.255.255") /
341 UDP(sport=DHCP4_CLIENT_PORT,
342 dport=DHCP4_SERVER_PORT) /
343 BOOTP(op=1) /
344 DHCP(options=[('message-type', 'discover'), ('end')]))
345 pkts_disc_vrf0 = [p_disc_vrf0]
346 p_disc_vrf1 = (Ether(dst="ff:ff:ff:ff:ff:ff",
347 src=self.pg3.remote_mac) /
348 IP(src="0.0.0.0", dst="255.255.255.255") /
349 UDP(sport=DHCP4_CLIENT_PORT,
350 dport=DHCP4_SERVER_PORT) /
351 BOOTP(op=1) /
352 DHCP(options=[('message-type', 'discover'), ('end')]))
353 pkts_disc_vrf1 = [p_disc_vrf0]
354
355 self.send_and_assert_no_replies(self.pg2, pkts_disc_vrf0,
356 "DHCP with no configuration")
357 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf1,
358 "DHCP with no configuration")
359
360 #
361 # Enable DHCP proxy in VRF 0
362 #
363 server_addr = self.pg0.remote_ip4n
364 src_addr = self.pg0.local_ip4n
365
366 self.vapi.dhcp_proxy_config(server_addr,
367 src_addr,
368 rx_table_id=0)
369
370 #
Neale Ranns20a175a2017-02-14 07:28:41 -0800371 # Discover packets from the client are dropped because there is no
372 # IP address configured on the client facing interface
Neale Rannsfca0c242017-01-13 07:57:46 -0800373 #
Neale Ranns20a175a2017-02-14 07:28:41 -0800374 self.send_and_assert_no_replies(self.pg2, pkts_disc_vrf0,
375 "Discover DHCP no relay address")
Neale Rannsfca0c242017-01-13 07:57:46 -0800376
377 #
378 # Inject a response from the server
Neale Ranns20a175a2017-02-14 07:28:41 -0800379 # dropped, because there is no IP addrees on the
Neale Ranns2dd68522017-02-16 03:38:59 -0800380 # client interfce to fill in the option.
Neale Rannsfca0c242017-01-13 07:57:46 -0800381 #
382 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
383 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
384 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
385 BOOTP(op=1) /
386 DHCP(options=[('message-type', 'offer'), ('end')]))
387 pkts = [p]
388
Neale Ranns20a175a2017-02-14 07:28:41 -0800389 self.send_and_assert_no_replies(self.pg2, pkts,
390 "Offer DHCP no relay address")
Neale Rannsfca0c242017-01-13 07:57:46 -0800391
392 #
393 # configure an IP address on the client facing interface
394 #
395 self.pg2.config_ip4()
396
397 #
398 # Try again with a discover packet
399 # Rx'd packet should be to the server address and from the configured
400 # source address
401 # UDP source ports are unchanged
402 # we've no option 82 config so that should be absent
403 #
404 self.pg2.add_stream(pkts_disc_vrf0)
405 self.pg_enable_capture(self.pg_interfaces)
406 self.pg_start()
407
408 rx = self.pg0.get_capture(1)
409 rx = rx[0]
410
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700411 option_82 = self.verify_relayed_dhcp_discover(rx, self.pg0,
412 src_intf=self.pg2)
Neale Rannsfca0c242017-01-13 07:57:46 -0800413
414 #
415 # Create an DHCP offer reply from the server with a correctly formatted
416 # option 82. i.e. send back what we just captured
417 # The offer, sent mcast to the client, still has option 82.
418 #
419 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
420 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
421 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
422 BOOTP(op=1) /
423 DHCP(options=[('message-type', 'offer'),
424 ('relay_agent_Information', option_82),
425 ('end')]))
426 pkts = [p]
427
428 self.pg0.add_stream(pkts)
429 self.pg_enable_capture(self.pg_interfaces)
430 self.pg_start()
431
432 rx = self.pg2.get_capture(1)
433 rx = rx[0]
434
435 self.verify_dhcp_offer(rx, self.pg2)
436
437 #
438 # Bogus Option 82:
439 #
440 # 1. not our IP address = not checked by VPP? so offer is replayed
441 # to client
442 bad_ip = option_82[0:8] + chr(33) + option_82[9:]
443
444 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
445 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
446 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
447 BOOTP(op=1) /
448 DHCP(options=[('message-type', 'offer'),
449 ('relay_agent_Information', bad_ip),
450 ('end')]))
451 pkts = [p]
Neale Ranns20a175a2017-02-14 07:28:41 -0800452 self.send_and_assert_no_replies(self.pg0, pkts,
453 "DHCP offer option 82 bad address")
Neale Rannsfca0c242017-01-13 07:57:46 -0800454
455 # 2. Not a sw_if_index VPP knows
456 bad_if_index = option_82[0:2] + chr(33) + option_82[3:]
457
458 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
459 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
460 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
461 BOOTP(op=1) /
462 DHCP(options=[('message-type', 'offer'),
463 ('relay_agent_Information', bad_if_index),
464 ('end')]))
465 pkts = [p]
466 self.send_and_assert_no_replies(self.pg0, pkts,
467 "DHCP offer option 82 bad if index")
468
469 #
470 # Send a DHCP request in VRF 1. should be dropped.
471 #
472 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf1,
473 "DHCP with no configuration VRF 1")
474
475 #
476 # Delete the DHCP config in VRF 0
477 # Should now drop requests.
478 #
479 self.vapi.dhcp_proxy_config(server_addr,
480 src_addr,
481 rx_table_id=0,
Neale Ranns20a175a2017-02-14 07:28:41 -0800482 is_add=0)
Neale Rannsfca0c242017-01-13 07:57:46 -0800483
484 self.send_and_assert_no_replies(self.pg2, pkts_disc_vrf0,
485 "DHCP config removed VRF 0")
486 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf1,
487 "DHCP config removed VRF 1")
488
489 #
490 # Add DHCP config for VRF 1
491 #
492 server_addr = self.pg1.remote_ip4n
493 src_addr = self.pg1.local_ip4n
494 self.vapi.dhcp_proxy_config(server_addr,
495 src_addr,
496 rx_table_id=1,
Neale Ranns20a175a2017-02-14 07:28:41 -0800497 server_table_id=1)
Neale Rannsfca0c242017-01-13 07:57:46 -0800498
499 #
500 # Confim DHCP requests ok in VRF 1.
501 # - dropped on IP config on client interface
502 #
503 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf1,
504 "DHCP config removed VRF 1")
505
506 #
507 # configure an IP address on the client facing interface
508 #
509 self.pg3.config_ip4()
510
511 self.pg3.add_stream(pkts_disc_vrf1)
512 self.pg_enable_capture(self.pg_interfaces)
513 self.pg_start()
514
515 rx = self.pg1.get_capture(1)
516 rx = rx[0]
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700517 self.verify_relayed_dhcp_discover(rx, self.pg1, src_intf=self.pg3)
Neale Rannsfca0c242017-01-13 07:57:46 -0800518
519 #
Neale Ranns20a175a2017-02-14 07:28:41 -0800520 # Add VSS config
521 # table=1, fib=id=1, oui=4
522 self.vapi.dhcp_proxy_set_vss(1, 1, 4)
523
524 self.pg3.add_stream(pkts_disc_vrf1)
525 self.pg_enable_capture(self.pg_interfaces)
526 self.pg_start()
527
528 rx = self.pg1.get_capture(1)
529 rx = rx[0]
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700530 self.verify_relayed_dhcp_discover(rx, self.pg1,
531 src_intf=self.pg3,
532 fib_id=1, oui=4)
Neale Ranns20a175a2017-02-14 07:28:41 -0800533
534 #
Neale Ranns3466c302017-02-16 07:45:03 -0800535 # Add a second DHCP server in VRF 1
536 # expect clients messages to be relay to both configured servers
537 #
538 self.pg1.generate_remote_hosts(2)
539 server_addr2 = socket.inet_pton(AF_INET, self.pg1.remote_hosts[1].ip4)
540
541 self.vapi.dhcp_proxy_config(server_addr2,
542 src_addr,
543 rx_table_id=1,
544 server_table_id=1,
545 is_add=1)
546
547 #
548 # We'll need an ARP entry for the server to send it packets
549 #
550 arp_entry = VppNeighbor(self,
551 self.pg1.sw_if_index,
552 self.pg1.remote_hosts[1].mac,
553 self.pg1.remote_hosts[1].ip4)
554 arp_entry.add_vpp_config()
555
556 #
557 # Send a discover from the client. expect two relayed messages
558 # The frist packet is sent to the second server
559 # We're not enforcing that here, it's just the way it is.
560 #
561 self.pg3.add_stream(pkts_disc_vrf1)
562 self.pg_enable_capture(self.pg_interfaces)
563 self.pg_start()
564
565 rx = self.pg1.get_capture(2)
566
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700567 option_82 = self.verify_relayed_dhcp_discover(
Neale Ranns3466c302017-02-16 07:45:03 -0800568 rx[0], self.pg1,
569 src_intf=self.pg3,
570 dst_mac=self.pg1.remote_hosts[1].mac,
571 dst_ip=self.pg1.remote_hosts[1].ip4,
572 fib_id=1, oui=4)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700573 self.verify_relayed_dhcp_discover(rx[1], self.pg1,
574 src_intf=self.pg3,
575 fib_id=1, oui=4)
Neale Ranns3466c302017-02-16 07:45:03 -0800576
577 #
578 # Send both packets back. Client gets both.
579 #
580 p1 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
581 IP(src=self.pg1.remote_ip4, dst=self.pg1.local_ip4) /
582 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
583 BOOTP(op=1) /
584 DHCP(options=[('message-type', 'offer'),
585 ('relay_agent_Information', option_82),
586 ('end')]))
587 p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
588 IP(src=self.pg1.remote_hosts[1].ip4, dst=self.pg1.local_ip4) /
589 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
590 BOOTP(op=1) /
591 DHCP(options=[('message-type', 'offer'),
592 ('relay_agent_Information', option_82),
593 ('end')]))
594 pkts = [p1, p2]
595
596 self.pg1.add_stream(pkts)
597 self.pg_enable_capture(self.pg_interfaces)
598 self.pg_start()
599
600 rx = self.pg3.get_capture(2)
601
602 self.verify_dhcp_offer(rx[0], self.pg3, fib_id=1, oui=4)
603 self.verify_dhcp_offer(rx[1], self.pg3, fib_id=1, oui=4)
604
605 #
606 # Ensure offers from non-servers are dropeed
607 #
608 p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
609 IP(src="8.8.8.8", dst=self.pg1.local_ip4) /
610 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
611 BOOTP(op=1) /
612 DHCP(options=[('message-type', 'offer'),
613 ('relay_agent_Information', option_82),
614 ('end')]))
615 self.send_and_assert_no_replies(self.pg1, p2,
616 "DHCP offer from non-server")
617
618 #
619 # Ensure only the discover is sent to multiple servers
620 #
621 p_req_vrf1 = (Ether(dst="ff:ff:ff:ff:ff:ff",
622 src=self.pg3.remote_mac) /
623 IP(src="0.0.0.0", dst="255.255.255.255") /
624 UDP(sport=DHCP4_CLIENT_PORT,
625 dport=DHCP4_SERVER_PORT) /
626 BOOTP(op=1) /
627 DHCP(options=[('message-type', 'request'),
628 ('end')]))
629
630 self.pg3.add_stream(p_req_vrf1)
631 self.pg_enable_capture(self.pg_interfaces)
632 self.pg_start()
633
634 rx = self.pg1.get_capture(1)
635
636 #
637 # Remove the second DHCP server
638 #
639 self.vapi.dhcp_proxy_config(server_addr2,
640 src_addr,
641 rx_table_id=1,
642 server_table_id=1,
643 is_add=0)
644
645 #
646 # Test we can still relay with the first
647 #
648 self.pg3.add_stream(pkts_disc_vrf1)
649 self.pg_enable_capture(self.pg_interfaces)
650 self.pg_start()
651
652 rx = self.pg1.get_capture(1)
653 rx = rx[0]
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700654 self.verify_relayed_dhcp_discover(rx, self.pg1,
655 src_intf=self.pg3,
656 fib_id=1, oui=4)
Neale Ranns3466c302017-02-16 07:45:03 -0800657
658 #
Neale Ranns20a175a2017-02-14 07:28:41 -0800659 # Remove the VSS config
660 # relayed DHCP has default vlaues in the option.
661 #
662 self.vapi.dhcp_proxy_set_vss(1, 1, 4, is_add=0)
663
664 self.pg3.add_stream(pkts_disc_vrf1)
665 self.pg_enable_capture(self.pg_interfaces)
666 self.pg_start()
667
668 rx = self.pg1.get_capture(1)
669 rx = rx[0]
Neale Rannsa2fbf6b2017-07-18 08:23:32 -0700670 self.verify_relayed_dhcp_discover(rx, self.pg1, src_intf=self.pg3)
Neale Ranns20a175a2017-02-14 07:28:41 -0800671
672 #
Neale Rannsfca0c242017-01-13 07:57:46 -0800673 # remove DHCP config to cleanup
674 #
675 self.vapi.dhcp_proxy_config(server_addr,
676 src_addr,
677 rx_table_id=1,
Neale Ranns3466c302017-02-16 07:45:03 -0800678 server_table_id=1,
Neale Rannsfca0c242017-01-13 07:57:46 -0800679 is_add=0)
680
681 self.send_and_assert_no_replies(self.pg2, pkts_disc_vrf0,
682 "DHCP cleanup VRF 0")
683 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf1,
684 "DHCP cleanup VRF 1")
Neale Ranns15002542017-09-10 04:39:11 -0700685 self.pg2.unconfig_ip4()
686 self.pg3.unconfig_ip4()
Neale Rannsfca0c242017-01-13 07:57:46 -0800687
688 def test_dhcp6_proxy(self):
689 """ DHCPv6 Proxy"""
690 #
691 # Verify no response to DHCP request without DHCP config
692 #
693 dhcp_solicit_dst = "ff02::1:2"
694 dhcp_solicit_src_vrf0 = mk_ll_addr(self.pg2.remote_mac)
695 dhcp_solicit_src_vrf1 = mk_ll_addr(self.pg3.remote_mac)
696 server_addr_vrf0 = self.pg0.remote_ip6n
697 src_addr_vrf0 = self.pg0.local_ip6n
698 server_addr_vrf1 = self.pg1.remote_ip6n
699 src_addr_vrf1 = self.pg1.local_ip6n
700
Neale Rannsfca0c242017-01-13 07:57:46 -0800701 dmac = in6_getnsmac(inet_pton(socket.AF_INET6, dhcp_solicit_dst))
702 p_solicit_vrf0 = (Ether(dst=dmac, src=self.pg2.remote_mac) /
703 IPv6(src=dhcp_solicit_src_vrf0,
704 dst=dhcp_solicit_dst) /
705 UDP(sport=DHCP6_SERVER_PORT,
706 dport=DHCP6_CLIENT_PORT) /
707 DHCP6_Solicit())
Neale Rannsfca0c242017-01-13 07:57:46 -0800708 p_solicit_vrf1 = (Ether(dst=dmac, src=self.pg3.remote_mac) /
709 IPv6(src=dhcp_solicit_src_vrf1,
710 dst=dhcp_solicit_dst) /
711 UDP(sport=DHCP6_SERVER_PORT,
712 dport=DHCP6_CLIENT_PORT) /
713 DHCP6_Solicit())
Neale Rannsfca0c242017-01-13 07:57:46 -0800714
Neale Ranns3466c302017-02-16 07:45:03 -0800715 self.send_and_assert_no_replies(self.pg2, p_solicit_vrf0,
Neale Rannsfca0c242017-01-13 07:57:46 -0800716 "DHCP with no configuration")
Neale Ranns3466c302017-02-16 07:45:03 -0800717 self.send_and_assert_no_replies(self.pg3, p_solicit_vrf1,
Neale Rannsfca0c242017-01-13 07:57:46 -0800718 "DHCP with no configuration")
719
720 #
721 # DHCPv6 config in VRF 0.
722 # Packets still dropped because the client facing interface has no
723 # IPv6 config
724 #
725 self.vapi.dhcp_proxy_config(server_addr_vrf0,
726 src_addr_vrf0,
727 rx_table_id=0,
728 server_table_id=0,
Neale Rannsfca0c242017-01-13 07:57:46 -0800729 is_ipv6=1)
730
Neale Ranns3466c302017-02-16 07:45:03 -0800731 self.send_and_assert_no_replies(self.pg2, p_solicit_vrf0,
Neale Rannsfca0c242017-01-13 07:57:46 -0800732 "DHCP with no configuration")
Neale Ranns3466c302017-02-16 07:45:03 -0800733 self.send_and_assert_no_replies(self.pg3, p_solicit_vrf1,
Neale Rannsfca0c242017-01-13 07:57:46 -0800734 "DHCP with no configuration")
735
736 #
737 # configure an IP address on the client facing interface
738 #
739 self.pg2.config_ip6()
740
741 #
742 # Now the DHCP requests are relayed to the server
743 #
Neale Ranns3466c302017-02-16 07:45:03 -0800744 self.pg2.add_stream(p_solicit_vrf0)
Neale Rannsfca0c242017-01-13 07:57:46 -0800745 self.pg_enable_capture(self.pg_interfaces)
746 self.pg_start()
747
748 rx = self.pg0.get_capture(1)
Neale Ranns3466c302017-02-16 07:45:03 -0800749
750 self.verify_dhcp6_solicit(rx[0], self.pg0,
Neale Rannsfca0c242017-01-13 07:57:46 -0800751 dhcp_solicit_src_vrf0,
752 self.pg2.remote_mac)
753
754 #
755 # Exception cases for rejected relay responses
756 #
757
758 # 1 - not a relay reply
759 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
760 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
761 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
762 DHCP6_Advertise())
Neale Ranns3466c302017-02-16 07:45:03 -0800763 self.send_and_assert_no_replies(self.pg2, p_adv_vrf0,
Neale Rannsfca0c242017-01-13 07:57:46 -0800764 "DHCP6 not a relay reply")
765
766 # 2 - no relay message option
767 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
768 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
769 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
770 DHCP6_RelayReply() /
771 DHCP6_Advertise())
Neale Ranns3466c302017-02-16 07:45:03 -0800772 self.send_and_assert_no_replies(self.pg2, p_adv_vrf0,
Neale Rannsfca0c242017-01-13 07:57:46 -0800773 "DHCP not a relay message")
774
775 # 3 - no circuit ID
776 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
777 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
778 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
779 DHCP6_RelayReply() /
780 DHCP6OptRelayMsg(optlen=0) /
781 DHCP6_Advertise())
Neale Ranns3466c302017-02-16 07:45:03 -0800782 self.send_and_assert_no_replies(self.pg2, p_adv_vrf0,
Neale Rannsfca0c242017-01-13 07:57:46 -0800783 "DHCP6 no circuit ID")
784 # 4 - wrong circuit ID
785 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
786 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
787 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
788 DHCP6_RelayReply() /
789 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x05') /
790 DHCP6OptRelayMsg(optlen=0) /
791 DHCP6_Advertise())
Neale Ranns3466c302017-02-16 07:45:03 -0800792 self.send_and_assert_no_replies(self.pg2, p_adv_vrf0,
Neale Rannsfca0c242017-01-13 07:57:46 -0800793 "DHCP6 wrong circuit ID")
794
795 #
796 # Send the relay response (the advertisement)
797 # - no peer address
798 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
799 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
800 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
801 DHCP6_RelayReply() /
802 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x03') /
803 DHCP6OptRelayMsg(optlen=0) /
804 DHCP6_Advertise(trid=1) /
805 DHCP6OptStatusCode(statuscode=0))
806 pkts_adv_vrf0 = [p_adv_vrf0]
807
808 self.pg0.add_stream(pkts_adv_vrf0)
809 self.pg_enable_capture(self.pg_interfaces)
810 self.pg_start()
811
812 rx = self.pg2.get_capture(1)
Neale Ranns3466c302017-02-16 07:45:03 -0800813
814 self.verify_dhcp6_advert(rx[0], self.pg2, "::")
Neale Rannsfca0c242017-01-13 07:57:46 -0800815
816 #
817 # Send the relay response (the advertisement)
818 # - with peer address
819 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
820 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
821 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
822 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf0) /
823 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x03') /
824 DHCP6OptRelayMsg(optlen=0) /
825 DHCP6_Advertise(trid=1) /
826 DHCP6OptStatusCode(statuscode=0))
827 pkts_adv_vrf0 = [p_adv_vrf0]
828
829 self.pg0.add_stream(pkts_adv_vrf0)
830 self.pg_enable_capture(self.pg_interfaces)
831 self.pg_start()
832
833 rx = self.pg2.get_capture(1)
Neale Ranns3466c302017-02-16 07:45:03 -0800834
835 self.verify_dhcp6_advert(rx[0], self.pg2, dhcp_solicit_src_vrf0)
Neale Rannsfca0c242017-01-13 07:57:46 -0800836
837 #
838 # Add all the config for VRF 1
839 #
840 self.vapi.dhcp_proxy_config(server_addr_vrf1,
841 src_addr_vrf1,
842 rx_table_id=1,
843 server_table_id=1,
Neale Rannsfca0c242017-01-13 07:57:46 -0800844 is_ipv6=1)
845 self.pg3.config_ip6()
846
847 #
848 # VRF 1 solicit
849 #
Neale Ranns3466c302017-02-16 07:45:03 -0800850 self.pg3.add_stream(p_solicit_vrf1)
Neale Rannsfca0c242017-01-13 07:57:46 -0800851 self.pg_enable_capture(self.pg_interfaces)
852 self.pg_start()
853
854 rx = self.pg1.get_capture(1)
Neale Ranns3466c302017-02-16 07:45:03 -0800855
856 self.verify_dhcp6_solicit(rx[0], self.pg1,
Neale Rannsfca0c242017-01-13 07:57:46 -0800857 dhcp_solicit_src_vrf1,
858 self.pg3.remote_mac)
859
860 #
861 # VRF 1 Advert
862 #
863 p_adv_vrf1 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
864 IPv6(dst=self.pg1.local_ip6, src=self.pg1.remote_ip6) /
865 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
866 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) /
867 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x04') /
868 DHCP6OptRelayMsg(optlen=0) /
869 DHCP6_Advertise(trid=1) /
870 DHCP6OptStatusCode(statuscode=0))
871 pkts_adv_vrf1 = [p_adv_vrf1]
872
873 self.pg1.add_stream(pkts_adv_vrf1)
874 self.pg_enable_capture(self.pg_interfaces)
875 self.pg_start()
876
877 rx = self.pg3.get_capture(1)
Neale Ranns3466c302017-02-16 07:45:03 -0800878
879 self.verify_dhcp6_advert(rx[0], self.pg3, dhcp_solicit_src_vrf1)
Neale Rannsfca0c242017-01-13 07:57:46 -0800880
881 #
882 # Add VSS config
883 # table=1, fib=id=1, oui=4
884 self.vapi.dhcp_proxy_set_vss(1, 1, 4, is_ip6=1)
885
Neale Ranns3466c302017-02-16 07:45:03 -0800886 self.pg3.add_stream(p_solicit_vrf1)
Neale Rannsfca0c242017-01-13 07:57:46 -0800887 self.pg_enable_capture(self.pg_interfaces)
888 self.pg_start()
889
890 rx = self.pg1.get_capture(1)
Neale Ranns3466c302017-02-16 07:45:03 -0800891
892 self.verify_dhcp6_solicit(rx[0], self.pg1,
Neale Rannsfca0c242017-01-13 07:57:46 -0800893 dhcp_solicit_src_vrf1,
894 self.pg3.remote_mac,
895 fib_id=1,
896 oui=4)
897
898 #
899 # Remove the VSS config
900 # relayed DHCP has default vlaues in the option.
901 #
902 self.vapi.dhcp_proxy_set_vss(1, 1, 4, is_ip6=1, is_add=0)
903
Neale Ranns3466c302017-02-16 07:45:03 -0800904 self.pg3.add_stream(p_solicit_vrf1)
Neale Rannsfca0c242017-01-13 07:57:46 -0800905 self.pg_enable_capture(self.pg_interfaces)
906 self.pg_start()
907
908 rx = self.pg1.get_capture(1)
Neale Ranns3466c302017-02-16 07:45:03 -0800909
910 self.verify_dhcp6_solicit(rx[0], self.pg1,
911 dhcp_solicit_src_vrf1,
912 self.pg3.remote_mac)
913
914 #
915 # Add a second DHCP server in VRF 1
916 # expect clients messages to be relay to both configured servers
917 #
918 self.pg1.generate_remote_hosts(2)
919 server_addr2 = socket.inet_pton(AF_INET6, self.pg1.remote_hosts[1].ip6)
920
921 self.vapi.dhcp_proxy_config(server_addr2,
922 src_addr_vrf1,
923 rx_table_id=1,
924 server_table_id=1,
925 is_ipv6=1)
926
927 #
928 # We'll need an ND entry for the server to send it packets
929 #
930 nd_entry = VppNeighbor(self,
931 self.pg1.sw_if_index,
932 self.pg1.remote_hosts[1].mac,
933 self.pg1.remote_hosts[1].ip6,
934 af=AF_INET6)
935 nd_entry.add_vpp_config()
936
937 #
938 # Send a discover from the client. expect two relayed messages
939 # The frist packet is sent to the second server
940 # We're not enforcing that here, it's just the way it is.
941 #
942 self.pg3.add_stream(p_solicit_vrf1)
943 self.pg_enable_capture(self.pg_interfaces)
944 self.pg_start()
945
946 rx = self.pg1.get_capture(2)
947
948 self.verify_dhcp6_solicit(rx[0], self.pg1,
949 dhcp_solicit_src_vrf1,
950 self.pg3.remote_mac)
951 self.verify_dhcp6_solicit(rx[1], self.pg1,
952 dhcp_solicit_src_vrf1,
953 self.pg3.remote_mac,
954 dst_mac=self.pg1.remote_hosts[1].mac,
955 dst_ip=self.pg1.remote_hosts[1].ip6)
956
957 #
958 # Send both packets back. Client gets both.
959 #
960 p1 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
961 IPv6(dst=self.pg1.local_ip6, src=self.pg1.remote_ip6) /
962 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
963 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) /
964 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x04') /
965 DHCP6OptRelayMsg(optlen=0) /
966 DHCP6_Advertise(trid=1) /
967 DHCP6OptStatusCode(statuscode=0))
968 p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_hosts[1].mac) /
969 IPv6(dst=self.pg1.local_ip6, src=self.pg1._remote_hosts[1].ip6) /
970 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
971 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) /
972 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x04') /
973 DHCP6OptRelayMsg(optlen=0) /
974 DHCP6_Advertise(trid=1) /
975 DHCP6OptStatusCode(statuscode=0))
976
977 pkts = [p1, p2]
978
979 self.pg1.add_stream(pkts)
980 self.pg_enable_capture(self.pg_interfaces)
981 self.pg_start()
982
983 rx = self.pg3.get_capture(2)
984
985 self.verify_dhcp6_advert(rx[0], self.pg3, dhcp_solicit_src_vrf1)
986 self.verify_dhcp6_advert(rx[1], self.pg3, dhcp_solicit_src_vrf1)
987
988 #
989 # Ensure only solicit messages are duplicated
990 #
991 p_request_vrf1 = (Ether(dst=dmac, src=self.pg3.remote_mac) /
992 IPv6(src=dhcp_solicit_src_vrf1,
993 dst=dhcp_solicit_dst) /
994 UDP(sport=DHCP6_SERVER_PORT,
995 dport=DHCP6_CLIENT_PORT) /
996 DHCP6_Request())
997
998 self.pg3.add_stream(p_request_vrf1)
999 self.pg_enable_capture(self.pg_interfaces)
1000 self.pg_start()
1001
1002 rx = self.pg1.get_capture(1)
1003
1004 #
1005 # Test we drop DHCP packets from addresses that are not configured as
1006 # DHCP servers
1007 #
1008 p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_hosts[1].mac) /
1009 IPv6(dst=self.pg1.local_ip6, src="3001::1") /
1010 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
1011 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) /
1012 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x04') /
1013 DHCP6OptRelayMsg(optlen=0) /
1014 DHCP6_Advertise(trid=1) /
1015 DHCP6OptStatusCode(statuscode=0))
1016 self.send_and_assert_no_replies(self.pg1, p2,
1017 "DHCP6 not from server")
1018
1019 #
1020 # Remove the second DHCP server
1021 #
1022 self.vapi.dhcp_proxy_config(server_addr2,
1023 src_addr_vrf1,
1024 rx_table_id=1,
1025 server_table_id=1,
1026 is_ipv6=1,
1027 is_add=0)
1028
1029 #
1030 # Test we can still relay with the first
1031 #
1032 self.pg3.add_stream(p_solicit_vrf1)
1033 self.pg_enable_capture(self.pg_interfaces)
1034 self.pg_start()
1035
1036 rx = self.pg1.get_capture(1)
1037
1038 self.verify_dhcp6_solicit(rx[0], self.pg1,
Neale Rannsfca0c242017-01-13 07:57:46 -08001039 dhcp_solicit_src_vrf1,
1040 self.pg3.remote_mac)
1041
1042 #
1043 # Cleanup
1044 #
1045 self.vapi.dhcp_proxy_config(server_addr_vrf1,
1046 src_addr_vrf1,
1047 rx_table_id=1,
1048 server_table_id=1,
Neale Rannsfca0c242017-01-13 07:57:46 -08001049 is_ipv6=1,
1050 is_add=0)
Neale Ranns3466c302017-02-16 07:45:03 -08001051 self.vapi.dhcp_proxy_config(server_addr_vrf0,
1052 src_addr_vrf0,
1053 rx_table_id=0,
1054 server_table_id=0,
1055 is_ipv6=1,
1056 is_add=0)
1057
1058 # duplicate delete
1059 self.vapi.dhcp_proxy_config(server_addr_vrf0,
1060 src_addr_vrf0,
Neale Rannsfca0c242017-01-13 07:57:46 -08001061 rx_table_id=0,
1062 server_table_id=0,
Neale Rannsfca0c242017-01-13 07:57:46 -08001063 is_ipv6=1,
1064 is_add=0)
Neale Ranns15002542017-09-10 04:39:11 -07001065 self.pg2.unconfig_ip6()
1066 self.pg3.unconfig_ip6()
Neale Rannsfca0c242017-01-13 07:57:46 -08001067
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001068 def test_dhcp_client(self):
1069 """ DHCP Client"""
1070
1071 hostname = 'universal-dp'
1072
1073 self.pg_enable_capture(self.pg_interfaces)
1074
1075 #
1076 # Configure DHCP client on PG2 and capture the discover sent
1077 #
1078 self.vapi.dhcp_client(self.pg2.sw_if_index, hostname)
1079
1080 rx = self.pg2.get_capture(1)
1081
1082 self.verify_orig_dhcp_discover(rx[0], self.pg2, hostname)
1083
1084 #
1085 # Sned back on offer, expect the request
1086 #
Neale Ranns808c5b22017-08-02 05:15:07 -07001087 p_offer = (Ether(dst=self.pg2.local_mac, src=self.pg2.remote_mac) /
1088 IP(src=self.pg2.remote_ip4, dst="255.255.255.255") /
1089 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1090 BOOTP(op=1, yiaddr=self.pg2.local_ip4) /
1091 DHCP(options=[('message-type', 'offer'),
1092 ('server_id', self.pg2.remote_ip4),
1093 ('end')]))
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001094
Neale Ranns808c5b22017-08-02 05:15:07 -07001095 self.pg2.add_stream(p_offer)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001096 self.pg_enable_capture(self.pg_interfaces)
1097 self.pg_start()
1098
1099 rx = self.pg2.get_capture(1)
1100 self.verify_orig_dhcp_request(rx[0], self.pg2, hostname,
1101 self.pg2.local_ip4)
1102
1103 #
1104 # Send an acknowloedgement
1105 #
Neale Ranns808c5b22017-08-02 05:15:07 -07001106 p_ack = (Ether(dst=self.pg2.local_mac, src=self.pg2.remote_mac) /
1107 IP(src=self.pg2.remote_ip4, dst="255.255.255.255") /
1108 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1109 BOOTP(op=1, yiaddr=self.pg2.local_ip4) /
1110 DHCP(options=[('message-type', 'ack'),
1111 ('subnet_mask', "255.255.255.0"),
1112 ('router', self.pg2.remote_ip4),
1113 ('server_id', self.pg2.remote_ip4),
1114 ('lease_time', 43200),
1115 ('end')]))
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001116
Neale Ranns808c5b22017-08-02 05:15:07 -07001117 self.pg2.add_stream(p_ack)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001118 self.pg_enable_capture(self.pg_interfaces)
1119 self.pg_start()
1120
1121 #
Neale Ranns51822bf2017-07-18 09:26:53 -07001122 # We'll get an ARP request for the router address
1123 #
1124 rx = self.pg2.get_capture(1)
1125
1126 self.assertEqual(rx[0][ARP].pdst, self.pg2.remote_ip4)
1127 self.pg_enable_capture(self.pg_interfaces)
1128
1129 #
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001130 # At the end of this procedure there should be a connected route
1131 # in the FIB
1132 #
Neale Ranns808c5b22017-08-02 05:15:07 -07001133 self.assertTrue(find_route(self, self.pg2.local_ip4, 24))
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001134 self.assertTrue(find_route(self, self.pg2.local_ip4, 32))
1135
Neale Ranns51822bf2017-07-18 09:26:53 -07001136 # remove the left over ARP entry
1137 self.vapi.ip_neighbor_add_del(self.pg2.sw_if_index,
Ole Troan895b6e82017-10-20 13:28:20 +02001138 mactobinary(self.pg2.remote_mac),
Neale Ranns51822bf2017-07-18 09:26:53 -07001139 self.pg2.remote_ip4,
1140 is_add=0)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001141 #
1142 # remove the DHCP config
1143 #
1144 self.vapi.dhcp_client(self.pg2.sw_if_index, hostname, is_add=0)
1145
1146 #
1147 # and now the route should be gone
1148 #
1149 self.assertFalse(find_route(self, self.pg2.local_ip4, 32))
Neale Ranns808c5b22017-08-02 05:15:07 -07001150 self.assertFalse(find_route(self, self.pg2.local_ip4, 24))
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001151
Neale Ranns51822bf2017-07-18 09:26:53 -07001152 #
Neale Ranns808c5b22017-08-02 05:15:07 -07001153 # Start the procedure again. this time have VPP send the client-ID
Neale Ranns51822bf2017-07-18 09:26:53 -07001154 #
Neale Ranns808c5b22017-08-02 05:15:07 -07001155 self.pg2.admin_down()
1156 self.sleep(1)
1157 self.pg2.admin_up()
Neale Ranns51822bf2017-07-18 09:26:53 -07001158 self.vapi.dhcp_client(self.pg2.sw_if_index, hostname,
1159 client_id=self.pg2.local_mac)
1160
1161 rx = self.pg2.get_capture(1)
1162
1163 self.verify_orig_dhcp_discover(rx[0], self.pg2, hostname,
1164 self.pg2.local_mac)
1165
Neale Ranns808c5b22017-08-02 05:15:07 -07001166 self.pg2.add_stream(p_offer)
1167 self.pg_enable_capture(self.pg_interfaces)
1168 self.pg_start()
1169
1170 rx = self.pg2.get_capture(1)
1171 self.verify_orig_dhcp_request(rx[0], self.pg2, hostname,
1172 self.pg2.local_ip4)
1173
1174 #
1175 # unicast the ack to the offered address
1176 #
1177 p_ack = (Ether(dst=self.pg2.local_mac, src=self.pg2.remote_mac) /
1178 IP(src=self.pg2.remote_ip4, dst=self.pg2.local_ip4) /
1179 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1180 BOOTP(op=1, yiaddr=self.pg2.local_ip4) /
1181 DHCP(options=[('message-type', 'ack'),
1182 ('subnet_mask', "255.255.255.0"),
1183 ('router', self.pg2.remote_ip4),
1184 ('server_id', self.pg2.remote_ip4),
1185 ('lease_time', 43200),
1186 ('end')]))
1187
1188 self.pg2.add_stream(p_ack)
1189 self.pg_enable_capture(self.pg_interfaces)
1190 self.pg_start()
1191
1192 #
1193 # At the end of this procedure there should be a connected route
1194 # in the FIB
1195 #
1196 self.assertTrue(find_route(self, self.pg2.local_ip4, 32))
1197 self.assertTrue(find_route(self, self.pg2.local_ip4, 24))
1198
Neale Ranns51822bf2017-07-18 09:26:53 -07001199 #
1200 # remove the DHCP config
1201 #
1202 self.vapi.dhcp_client(self.pg2.sw_if_index, hostname, is_add=0)
Neale Rannsa2fbf6b2017-07-18 08:23:32 -07001203
Neale Ranns808c5b22017-08-02 05:15:07 -07001204 self.assertFalse(find_route(self, self.pg2.local_ip4, 32))
1205 self.assertFalse(find_route(self, self.pg2.local_ip4, 24))
1206
1207
Neale Rannsfca0c242017-01-13 07:57:46 -08001208if __name__ == '__main__':
1209 unittest.main(testRunner=VppTestRunner)