2efa9a7e244dbb1de80b7bf6f77859176321d0f1
[vpp.git] / test / test_dhcp.py
1 #!/usr/bin/env python
2
3 import unittest
4 import socket
5 import struct
6
7 from framework import VppTestCase, VppTestRunner, running_extended_tests
8 from vpp_neighbor import VppNeighbor
9 from vpp_ip_route import find_route, VppIpTable
10 from util import mk_ll_addr
11 from scapy.layers.l2 import Ether, getmacbyip, ARP
12 from scapy.layers.inet import IP, UDP, ICMP
13 from scapy.layers.inet6 import IPv6, in6_getnsmac
14 from scapy.utils6 import in6_mactoifaceid
15 from scapy.layers.dhcp import DHCP, BOOTP, DHCPTypes
16 from scapy.layers.dhcp6 import DHCP6, DHCP6_Solicit, DHCP6_RelayForward, \
17     DHCP6_RelayReply, DHCP6_Advertise, DHCP6OptRelayMsg, DHCP6OptIfaceId, \
18     DHCP6OptStatusCode, DHCP6OptVSS, DHCP6OptClientLinkLayerAddr, DHCP6_Request
19 from socket import AF_INET, AF_INET6
20 from scapy.utils import inet_pton, inet_ntop
21 from scapy.utils6 import in6_ptop
22 from vpp_papi import mac_pton
23
24 DHCP4_CLIENT_PORT = 68
25 DHCP4_SERVER_PORT = 67
26 DHCP6_CLIENT_PORT = 547
27 DHCP6_SERVER_PORT = 546
28
29
30 class TestDHCP(VppTestCase):
31     """ DHCP Test Case """
32
33     def setUp(self):
34         super(TestDHCP, self).setUp()
35
36         # create 6 pg interfaces for pg0 to pg5
37         self.create_pg_interfaces(range(6))
38         self.tables = []
39
40         # pg0 to 2 are IP configured in VRF 0, 1 and 2.
41         # pg3 to 5 are non IP-configured in VRF 0, 1 and 2.
42         table_id = 0
43         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
52         for i in self.pg_interfaces[:3]:
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[3:]:
64             i.admin_up()
65             i.set_table_ip4(table_id)
66             i.set_table_ip6(table_id)
67             table_id += 1
68
69     def tearDown(self):
70         for i in self.pg_interfaces[:3]:
71             i.unconfig_ip4()
72             i.unconfig_ip6()
73
74         for i in self.pg_interfaces:
75             i.set_table_ip4(0)
76             i.set_table_ip6(0)
77             i.admin_down()
78         super(TestDHCP, self).tearDown()
79
80     def verify_dhcp_has_option(self, pkt, option, value):
81         dhcp = pkt[DHCP]
82         found = False
83
84         for i in dhcp.options:
85             if type(i) is tuple:
86                 if i[0] == option:
87                     self.assertEqual(i[1], value)
88                     found = True
89
90         self.assertTrue(found)
91
92     def validate_relay_options(self, pkt, intf, ip_addr, vpn_id, fib_id, oui):
93         dhcp = pkt[DHCP]
94         found = 0
95         data = []
96         id_len = len(vpn_id)
97
98         for i in dhcp.options:
99             if type(i) is tuple:
100                 if i[0] == "relay_agent_Information":
101                     #
102                     # There are two sb-options present - each of length 6.
103                     #
104                     data = i[1]
105                     if oui != 0:
106                         self.assertEqual(len(data), 24)
107                     elif len(vpn_id) > 0:
108                         self.assertEqual(len(data), len(vpn_id)+17)
109                     else:
110                         self.assertEqual(len(data), 12)
111
112                     #
113                     # First sub-option is ID 1, len 4, then encoded
114                     #  sw_if_index. This test uses low valued indicies
115                     # so [2:4] are 0.
116                     # The ID space is VPP internal - so no matching value
117                     # scapy
118                     #
119                     self.assertEqual(ord(data[0]), 1)
120                     self.assertEqual(ord(data[1]), 4)
121                     self.assertEqual(ord(data[2]), 0)
122                     self.assertEqual(ord(data[3]), 0)
123                     self.assertEqual(ord(data[4]), 0)
124                     self.assertEqual(ord(data[5]), intf._sw_if_index)
125
126                     #
127                     # next sub-option is the IP address of the client side
128                     # interface.
129                     # sub-option ID=5, length (of a v4 address)=4
130                     #
131                     claddr = socket.inet_pton(AF_INET, ip_addr)
132
133                     self.assertEqual(ord(data[6]), 5)
134                     self.assertEqual(ord(data[7]), 4)
135                     self.assertEqual(data[8], claddr[0])
136                     self.assertEqual(data[9], claddr[1])
137                     self.assertEqual(data[10], claddr[2])
138                     self.assertEqual(data[11], claddr[3])
139
140                     if oui != 0:
141                         # sub-option 151 encodes vss_type 1,
142                         # the 3 byte oui and the 4 byte fib_id
143                         self.assertEqual(id_len, 0)
144                         self.assertEqual(ord(data[12]), 151)
145                         self.assertEqual(ord(data[13]), 8)
146                         self.assertEqual(ord(data[14]), 1)
147                         self.assertEqual(ord(data[15]), 0)
148                         self.assertEqual(ord(data[16]), 0)
149                         self.assertEqual(ord(data[17]), oui)
150                         self.assertEqual(ord(data[18]), 0)
151                         self.assertEqual(ord(data[19]), 0)
152                         self.assertEqual(ord(data[20]), 0)
153                         self.assertEqual(ord(data[21]), fib_id)
154
155                         # VSS control sub-option
156                         self.assertEqual(ord(data[22]), 152)
157                         self.assertEqual(ord(data[23]), 0)
158
159                     if id_len > 0:
160                         # sub-option 151 encode vss_type of 0
161                         # followerd by vpn_id in ascii
162                         self.assertEqual(oui, 0)
163                         self.assertEqual(ord(data[12]), 151)
164                         self.assertEqual(ord(data[13]), id_len+1)
165                         self.assertEqual(ord(data[14]), 0)
166                         self.assertEqual(data[15:15+id_len], vpn_id)
167
168                         # VSS control sub-option
169                         self.assertEqual(ord(data[15+len(vpn_id)]), 152)
170                         self.assertEqual(ord(data[16+len(vpn_id)]), 0)
171
172                     found = 1
173         self.assertTrue(found)
174
175         return data
176
177     def verify_dhcp_msg_type(self, pkt, name):
178         dhcp = pkt[DHCP]
179         found = False
180         for o in dhcp.options:
181             if type(o) is tuple:
182                 if o[0] == "message-type" \
183                    and DHCPTypes[o[1]] == name:
184                     found = True
185         self.assertTrue(found)
186
187     def verify_dhcp_offer(self, pkt, intf, vpn_id="", fib_id=0, oui=0):
188         ether = pkt[Ether]
189         self.assertEqual(ether.dst, "ff:ff:ff:ff:ff:ff")
190         self.assertEqual(ether.src, intf.local_mac)
191
192         ip = pkt[IP]
193         self.assertEqual(ip.dst, "255.255.255.255")
194         self.assertEqual(ip.src, intf.local_ip4)
195
196         udp = pkt[UDP]
197         self.assertEqual(udp.dport, DHCP4_CLIENT_PORT)
198         self.assertEqual(udp.sport, DHCP4_SERVER_PORT)
199
200         self.verify_dhcp_msg_type(pkt, "offer")
201         data = self.validate_relay_options(pkt, intf, intf.local_ip4,
202                                            vpn_id, fib_id, oui)
203
204     def verify_orig_dhcp_pkt(self, pkt, intf):
205         ether = pkt[Ether]
206         self.assertEqual(ether.dst, "ff:ff:ff:ff:ff:ff")
207         self.assertEqual(ether.src, intf.local_mac)
208
209         ip = pkt[IP]
210         self.assertEqual(ip.dst, "255.255.255.255")
211         self.assertEqual(ip.src, "0.0.0.0")
212
213         udp = pkt[UDP]
214         self.assertEqual(udp.dport, DHCP4_SERVER_PORT)
215         self.assertEqual(udp.sport, DHCP4_CLIENT_PORT)
216
217     def verify_orig_dhcp_discover(self, pkt, intf, hostname, client_id=None,
218                                   broadcast=1):
219         self.verify_orig_dhcp_pkt(pkt, intf)
220
221         self.verify_dhcp_msg_type(pkt, "discover")
222         self.verify_dhcp_has_option(pkt, "hostname", hostname)
223         if client_id:
224             self.verify_dhcp_has_option(pkt, "client_id", client_id)
225         bootp = pkt[BOOTP]
226         self.assertEqual(bootp.ciaddr, "0.0.0.0")
227         self.assertEqual(bootp.giaddr, "0.0.0.0")
228         if broadcast:
229             self.assertEqual(bootp.flags, 0x8000)
230         else:
231             self.assertEqual(bootp.flags, 0x0000)
232
233     def verify_orig_dhcp_request(self, pkt, intf, hostname, ip,
234                                  broadcast=1):
235         self.verify_orig_dhcp_pkt(pkt, intf)
236
237         self.verify_dhcp_msg_type(pkt, "request")
238         self.verify_dhcp_has_option(pkt, "hostname", hostname)
239         self.verify_dhcp_has_option(pkt, "requested_addr", ip)
240         bootp = pkt[BOOTP]
241         self.assertEqual(bootp.ciaddr, "0.0.0.0")
242         self.assertEqual(bootp.giaddr, "0.0.0.0")
243         if broadcast:
244             self.assertEqual(bootp.flags, 0x8000)
245         else:
246             self.assertEqual(bootp.flags, 0x0000)
247
248     def verify_relayed_dhcp_discover(self, pkt, intf, src_intf=None,
249                                      fib_id=0, oui=0,
250                                      vpn_id="",
251                                      dst_mac=None, dst_ip=None):
252         if not dst_mac:
253             dst_mac = intf.remote_mac
254         if not dst_ip:
255             dst_ip = intf.remote_ip4
256
257         ether = pkt[Ether]
258         self.assertEqual(ether.dst, dst_mac)
259         self.assertEqual(ether.src, intf.local_mac)
260
261         ip = pkt[IP]
262         self.assertEqual(ip.dst, dst_ip)
263         self.assertEqual(ip.src, intf.local_ip4)
264
265         udp = pkt[UDP]
266         self.assertEqual(udp.dport, DHCP4_SERVER_PORT)
267         self.assertEqual(udp.sport, DHCP4_CLIENT_PORT)
268
269         dhcp = pkt[DHCP]
270
271         is_discover = False
272         for o in dhcp.options:
273             if type(o) is tuple:
274                 if o[0] == "message-type" \
275                    and DHCPTypes[o[1]] == "discover":
276                     is_discover = True
277         self.assertTrue(is_discover)
278
279         data = self.validate_relay_options(pkt, src_intf,
280                                            src_intf.local_ip4,
281                                            vpn_id,
282                                            fib_id, oui)
283         return data
284
285     def verify_dhcp6_solicit(self, pkt, intf,
286                              peer_ip, peer_mac,
287                              vpn_id="",
288                              fib_id=0,
289                              oui=0,
290                              dst_mac=None,
291                              dst_ip=None):
292         if not dst_mac:
293             dst_mac = intf.remote_mac
294         if not dst_ip:
295             dst_ip = in6_ptop(intf.remote_ip6)
296
297         ether = pkt[Ether]
298         self.assertEqual(ether.dst, dst_mac)
299         self.assertEqual(ether.src, intf.local_mac)
300
301         ip = pkt[IPv6]
302         self.assertEqual(in6_ptop(ip.dst), dst_ip)
303         self.assertEqual(in6_ptop(ip.src), in6_ptop(intf.local_ip6))
304
305         udp = pkt[UDP]
306         self.assertEqual(udp.dport, DHCP6_CLIENT_PORT)
307         self.assertEqual(udp.sport, DHCP6_SERVER_PORT)
308
309         relay = pkt[DHCP6_RelayForward]
310         self.assertEqual(in6_ptop(relay.peeraddr), in6_ptop(peer_ip))
311         oid = pkt[DHCP6OptIfaceId]
312         cll = pkt[DHCP6OptClientLinkLayerAddr]
313         self.assertEqual(cll.optlen, 8)
314         self.assertEqual(cll.lltype, 1)
315         self.assertEqual(cll.clladdr, peer_mac)
316
317         id_len = len(vpn_id)
318
319         if fib_id != 0:
320             self.assertEqual(id_len, 0)
321             vss = pkt[DHCP6OptVSS]
322             self.assertEqual(vss.optlen, 8)
323             self.assertEqual(vss.type, 1)
324             # the OUI and FIB-id are really 3 and 4 bytes resp.
325             # but the tested range is small
326             self.assertEqual(ord(vss.data[0]), 0)
327             self.assertEqual(ord(vss.data[1]), 0)
328             self.assertEqual(ord(vss.data[2]), oui)
329             self.assertEqual(ord(vss.data[3]), 0)
330             self.assertEqual(ord(vss.data[4]), 0)
331             self.assertEqual(ord(vss.data[5]), 0)
332             self.assertEqual(ord(vss.data[6]), fib_id)
333
334         if id_len > 0:
335             self.assertEqual(oui, 0)
336             vss = pkt[DHCP6OptVSS]
337             self.assertEqual(vss.optlen, id_len+1)
338             self.assertEqual(vss.type, 0)
339             self.assertEqual(vss.data[0:id_len], vpn_id)
340
341         # the relay message should be an encoded Solicit
342         msg = pkt[DHCP6OptRelayMsg]
343         sol = DHCP6_Solicit()
344         self.assertEqual(msg.optlen, len(str(sol)))
345         self.assertEqual(str(sol), (str(msg[1]))[:msg.optlen])
346
347     def verify_dhcp6_advert(self, pkt, intf, peer):
348         ether = pkt[Ether]
349         self.assertEqual(ether.dst, "ff:ff:ff:ff:ff:ff")
350         self.assertEqual(ether.src, intf.local_mac)
351
352         ip = pkt[IPv6]
353         self.assertEqual(in6_ptop(ip.dst), in6_ptop(peer))
354         self.assertEqual(in6_ptop(ip.src), in6_ptop(intf.local_ip6))
355
356         udp = pkt[UDP]
357         self.assertEqual(udp.dport, DHCP6_SERVER_PORT)
358         self.assertEqual(udp.sport, DHCP6_CLIENT_PORT)
359
360         # not sure why this is not decoding
361         # adv = pkt[DHCP6_Advertise]
362
363     def wait_for_no_route(self, address, length,
364                           n_tries=50, s_time=1):
365         while (n_tries):
366             if not find_route(self, address, length):
367                 return True
368             n_tries = n_tries - 1
369             self.sleep(s_time)
370
371         return False
372
373     def test_dhcp_proxy(self):
374         """ DHCPv4 Proxy """
375
376         #
377         # Verify no response to DHCP request without DHCP config
378         #
379         p_disc_vrf0 = (Ether(dst="ff:ff:ff:ff:ff:ff",
380                              src=self.pg3.remote_mac) /
381                        IP(src="0.0.0.0", dst="255.255.255.255") /
382                        UDP(sport=DHCP4_CLIENT_PORT,
383                            dport=DHCP4_SERVER_PORT) /
384                        BOOTP(op=1) /
385                        DHCP(options=[('message-type', 'discover'), ('end')]))
386         pkts_disc_vrf0 = [p_disc_vrf0]
387         p_disc_vrf1 = (Ether(dst="ff:ff:ff:ff:ff:ff",
388                              src=self.pg4.remote_mac) /
389                        IP(src="0.0.0.0", dst="255.255.255.255") /
390                        UDP(sport=DHCP4_CLIENT_PORT,
391                            dport=DHCP4_SERVER_PORT) /
392                        BOOTP(op=1) /
393                        DHCP(options=[('message-type', 'discover'), ('end')]))
394         pkts_disc_vrf1 = [p_disc_vrf1]
395         p_disc_vrf2 = (Ether(dst="ff:ff:ff:ff:ff:ff",
396                              src=self.pg5.remote_mac) /
397                        IP(src="0.0.0.0", dst="255.255.255.255") /
398                        UDP(sport=DHCP4_CLIENT_PORT,
399                            dport=DHCP4_SERVER_PORT) /
400                        BOOTP(op=1) /
401                        DHCP(options=[('message-type', 'discover'), ('end')]))
402         pkts_disc_vrf2 = [p_disc_vrf2]
403
404         self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf0,
405                                         "DHCP with no configuration")
406         self.send_and_assert_no_replies(self.pg4, pkts_disc_vrf1,
407                                         "DHCP with no configuration")
408         self.send_and_assert_no_replies(self.pg5, pkts_disc_vrf2,
409                                         "DHCP with no configuration")
410
411         #
412         # Enable DHCP proxy in VRF 0
413         #
414         server_addr = self.pg0.remote_ip4n
415         src_addr = self.pg0.local_ip4n
416
417         self.vapi.dhcp_proxy_config(server_addr,
418                                     src_addr,
419                                     rx_table_id=0)
420
421         #
422         # Discover packets from the client are dropped because there is no
423         # IP address configured on the client facing interface
424         #
425         self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf0,
426                                         "Discover DHCP no relay address")
427
428         #
429         # Inject a response from the server
430         #  dropped, because there is no IP addrees on the
431         #  client interfce to fill in the option.
432         #
433         p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
434              IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
435              UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
436              BOOTP(op=1) /
437              DHCP(options=[('message-type', 'offer'), ('end')]))
438         pkts = [p]
439
440         self.send_and_assert_no_replies(self.pg3, pkts,
441                                         "Offer DHCP no relay address")
442
443         #
444         # configure an IP address on the client facing interface
445         #
446         self.pg3.config_ip4()
447
448         #
449         # Try again with a discover packet
450         # Rx'd packet should be to the server address and from the configured
451         # source address
452         # UDP source ports are unchanged
453         # we've no option 82 config so that should be absent
454         #
455         self.pg3.add_stream(pkts_disc_vrf0)
456         self.pg_enable_capture(self.pg_interfaces)
457         self.pg_start()
458
459         rx = self.pg0.get_capture(1)
460         rx = rx[0]
461
462         option_82 = self.verify_relayed_dhcp_discover(rx, self.pg0,
463                                                       src_intf=self.pg3)
464
465         #
466         # Create an DHCP offer reply from the server with a correctly formatted
467         # option 82. i.e. send back what we just captured
468         # The offer, sent mcast to the client, still has option 82.
469         #
470         p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
471              IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
472              UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
473              BOOTP(op=1) /
474              DHCP(options=[('message-type', 'offer'),
475                            ('relay_agent_Information', option_82),
476                            ('end')]))
477         pkts = [p]
478
479         self.pg0.add_stream(pkts)
480         self.pg_enable_capture(self.pg_interfaces)
481         self.pg_start()
482
483         rx = self.pg3.get_capture(1)
484         rx = rx[0]
485
486         self.verify_dhcp_offer(rx, self.pg3)
487
488         #
489         # Bogus Option 82:
490         #
491         # 1. not our IP address = not checked by VPP? so offer is replayed
492         #    to client
493         bad_ip = option_82[0:8] + chr(33) + option_82[9:]
494
495         p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
496              IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
497              UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
498              BOOTP(op=1) /
499              DHCP(options=[('message-type', 'offer'),
500                            ('relay_agent_Information', bad_ip),
501                            ('end')]))
502         pkts = [p]
503         self.send_and_assert_no_replies(self.pg0, pkts,
504                                         "DHCP offer option 82 bad address")
505
506         # 2. Not a sw_if_index VPP knows
507         bad_if_index = option_82[0:2] + chr(33) + option_82[3:]
508
509         p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
510              IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
511              UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
512              BOOTP(op=1) /
513              DHCP(options=[('message-type', 'offer'),
514                            ('relay_agent_Information', bad_if_index),
515                            ('end')]))
516         pkts = [p]
517         self.send_and_assert_no_replies(self.pg0, pkts,
518                                         "DHCP offer option 82 bad if index")
519
520         #
521         # Send a DHCP request in VRF 1. should be dropped.
522         #
523         self.send_and_assert_no_replies(self.pg4, pkts_disc_vrf1,
524                                         "DHCP with no configuration VRF 1")
525
526         #
527         # Delete the DHCP config in VRF 0
528         # Should now drop requests.
529         #
530         self.vapi.dhcp_proxy_config(server_addr,
531                                     src_addr,
532                                     rx_table_id=0,
533                                     is_add=0)
534
535         self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf0,
536                                         "DHCP config removed VRF 0")
537         self.send_and_assert_no_replies(self.pg4, pkts_disc_vrf1,
538                                         "DHCP config removed VRF 1")
539
540         #
541         # Add DHCP config for VRF 1 & 2
542         #
543         server_addr1 = self.pg1.remote_ip4n
544         src_addr1 = self.pg1.local_ip4n
545         self.vapi.dhcp_proxy_config(server_addr1,
546                                     src_addr1,
547                                     rx_table_id=1,
548                                     server_table_id=1)
549         server_addr2 = self.pg2.remote_ip4n
550         src_addr2 = self.pg2.local_ip4n
551         self.vapi.dhcp_proxy_config(server_addr2,
552                                     src_addr2,
553                                     rx_table_id=2,
554                                     server_table_id=2)
555
556         #
557         # Confim DHCP requests ok in VRF 1 & 2.
558         #  - dropped on IP config on client interface
559         #
560         self.send_and_assert_no_replies(self.pg4, pkts_disc_vrf1,
561                                         "DHCP config removed VRF 1")
562         self.send_and_assert_no_replies(self.pg5, pkts_disc_vrf2,
563                                         "DHCP config removed VRF 2")
564
565         #
566         # configure an IP address on the client facing interface
567         #
568         self.pg4.config_ip4()
569         self.pg4.add_stream(pkts_disc_vrf1)
570         self.pg_enable_capture(self.pg_interfaces)
571         self.pg_start()
572         rx = self.pg1.get_capture(1)
573         rx = rx[0]
574         self.verify_relayed_dhcp_discover(rx, self.pg1, src_intf=self.pg4)
575
576         self.pg5.config_ip4()
577         self.pg5.add_stream(pkts_disc_vrf2)
578         self.pg_enable_capture(self.pg_interfaces)
579         self.pg_start()
580         rx = self.pg2.get_capture(1)
581         rx = rx[0]
582         self.verify_relayed_dhcp_discover(rx, self.pg2, src_intf=self.pg5)
583
584         #
585         # Add VSS config
586         #  table=1, vss_type=1, vpn_index=1, oui=4
587         #  table=2, vss_type=0, vpn_id = "ip4-table-2"
588         self.vapi.dhcp_proxy_set_vss(1, 1, vpn_index=1, oui=4, is_add=1)
589         self.vapi.dhcp_proxy_set_vss(2, 0, "ip4-table-2", is_add=1)
590
591         self.pg4.add_stream(pkts_disc_vrf1)
592         self.pg_enable_capture(self.pg_interfaces)
593         self.pg_start()
594
595         rx = self.pg1.get_capture(1)
596         rx = rx[0]
597         self.verify_relayed_dhcp_discover(rx, self.pg1,
598                                           src_intf=self.pg4,
599                                           fib_id=1, oui=4)
600
601         self.pg5.add_stream(pkts_disc_vrf2)
602         self.pg_enable_capture(self.pg_interfaces)
603         self.pg_start()
604
605         rx = self.pg2.get_capture(1)
606         rx = rx[0]
607         self.verify_relayed_dhcp_discover(rx, self.pg2,
608                                           src_intf=self.pg5,
609                                           vpn_id="ip4-table-2")
610
611         #
612         # Add a second DHCP server in VRF 1
613         #  expect clients messages to be relay to both configured servers
614         #
615         self.pg1.generate_remote_hosts(2)
616         server_addr12 = socket.inet_pton(AF_INET, self.pg1.remote_hosts[1].ip4)
617
618         self.vapi.dhcp_proxy_config(server_addr12,
619                                     src_addr1,
620                                     rx_table_id=1,
621                                     server_table_id=1,
622                                     is_add=1)
623
624         #
625         # We'll need an ARP entry for the server to send it packets
626         #
627         arp_entry = VppNeighbor(self,
628                                 self.pg1.sw_if_index,
629                                 self.pg1.remote_hosts[1].mac,
630                                 self.pg1.remote_hosts[1].ip4)
631         arp_entry.add_vpp_config()
632
633         #
634         # Send a discover from the client. expect two relayed messages
635         # The frist packet is sent to the second server
636         # We're not enforcing that here, it's just the way it is.
637         #
638         self.pg4.add_stream(pkts_disc_vrf1)
639         self.pg_enable_capture(self.pg_interfaces)
640         self.pg_start()
641
642         rx = self.pg1.get_capture(2)
643
644         option_82 = self.verify_relayed_dhcp_discover(
645             rx[0], self.pg1,
646             src_intf=self.pg4,
647             dst_mac=self.pg1.remote_hosts[1].mac,
648             dst_ip=self.pg1.remote_hosts[1].ip4,
649             fib_id=1, oui=4)
650         self.verify_relayed_dhcp_discover(rx[1], self.pg1,
651                                           src_intf=self.pg4,
652                                           fib_id=1, oui=4)
653
654         #
655         # Send both packets back. Client gets both.
656         #
657         p1 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
658               IP(src=self.pg1.remote_ip4, dst=self.pg1.local_ip4) /
659               UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
660               BOOTP(op=1) /
661               DHCP(options=[('message-type', 'offer'),
662                             ('relay_agent_Information', option_82),
663                             ('end')]))
664         p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
665               IP(src=self.pg1.remote_hosts[1].ip4, dst=self.pg1.local_ip4) /
666               UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
667               BOOTP(op=1) /
668               DHCP(options=[('message-type', 'offer'),
669                             ('relay_agent_Information', option_82),
670                             ('end')]))
671         pkts = [p1, p2]
672
673         self.pg1.add_stream(pkts)
674         self.pg_enable_capture(self.pg_interfaces)
675         self.pg_start()
676
677         rx = self.pg4.get_capture(2)
678
679         self.verify_dhcp_offer(rx[0], self.pg4, fib_id=1, oui=4)
680         self.verify_dhcp_offer(rx[1], self.pg4, fib_id=1, oui=4)
681
682         #
683         # Ensure offers from non-servers are dropeed
684         #
685         p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
686               IP(src="8.8.8.8", dst=self.pg1.local_ip4) /
687               UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
688               BOOTP(op=1) /
689               DHCP(options=[('message-type', 'offer'),
690                             ('relay_agent_Information', option_82),
691                             ('end')]))
692         self.send_and_assert_no_replies(self.pg1, p2,
693                                         "DHCP offer from non-server")
694
695         #
696         # Ensure only the discover is sent to multiple servers
697         #
698         p_req_vrf1 = (Ether(dst="ff:ff:ff:ff:ff:ff",
699                             src=self.pg4.remote_mac) /
700                       IP(src="0.0.0.0", dst="255.255.255.255") /
701                       UDP(sport=DHCP4_CLIENT_PORT,
702                           dport=DHCP4_SERVER_PORT) /
703                       BOOTP(op=1) /
704                       DHCP(options=[('message-type', 'request'),
705                                     ('end')]))
706
707         self.pg4.add_stream(p_req_vrf1)
708         self.pg_enable_capture(self.pg_interfaces)
709         self.pg_start()
710
711         rx = self.pg1.get_capture(1)
712
713         #
714         # Remove the second DHCP server
715         #
716         self.vapi.dhcp_proxy_config(server_addr12,
717                                     src_addr1,
718                                     rx_table_id=1,
719                                     server_table_id=1,
720                                     is_add=0)
721
722         #
723         # Test we can still relay with the first
724         #
725         self.pg4.add_stream(pkts_disc_vrf1)
726         self.pg_enable_capture(self.pg_interfaces)
727         self.pg_start()
728
729         rx = self.pg1.get_capture(1)
730         rx = rx[0]
731         self.verify_relayed_dhcp_discover(rx, self.pg1,
732                                           src_intf=self.pg4,
733                                           fib_id=1, oui=4)
734
735         #
736         # Remove the VSS config
737         #  relayed DHCP has default vlaues in the option.
738         #
739         self.vapi.dhcp_proxy_set_vss(1, is_add=0)
740         self.vapi.dhcp_proxy_set_vss(2, is_add=0)
741
742         self.pg4.add_stream(pkts_disc_vrf1)
743         self.pg_enable_capture(self.pg_interfaces)
744         self.pg_start()
745
746         rx = self.pg1.get_capture(1)
747         rx = rx[0]
748         self.verify_relayed_dhcp_discover(rx, self.pg1, src_intf=self.pg4)
749
750         #
751         # remove DHCP config to cleanup
752         #
753         self.vapi.dhcp_proxy_config(server_addr1,
754                                     src_addr1,
755                                     rx_table_id=1,
756                                     server_table_id=1,
757                                     is_add=0)
758         self.vapi.dhcp_proxy_config(server_addr2,
759                                     src_addr2,
760                                     rx_table_id=2,
761                                     server_table_id=2,
762                                     is_add=0)
763
764         self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf0,
765                                         "DHCP cleanup VRF 0")
766         self.send_and_assert_no_replies(self.pg4, pkts_disc_vrf1,
767                                         "DHCP cleanup VRF 1")
768         self.send_and_assert_no_replies(self.pg5, pkts_disc_vrf2,
769                                         "DHCP cleanup VRF 2")
770
771         self.pg3.unconfig_ip4()
772         self.pg4.unconfig_ip4()
773         self.pg5.unconfig_ip4()
774
775     def test_dhcp6_proxy(self):
776         """ DHCPv6 Proxy"""
777         #
778         # Verify no response to DHCP request without DHCP config
779         #
780         dhcp_solicit_dst = "ff02::1:2"
781         dhcp_solicit_src_vrf0 = mk_ll_addr(self.pg3.remote_mac)
782         dhcp_solicit_src_vrf1 = mk_ll_addr(self.pg4.remote_mac)
783         dhcp_solicit_src_vrf2 = mk_ll_addr(self.pg5.remote_mac)
784         server_addr_vrf0 = self.pg0.remote_ip6n
785         src_addr_vrf0 = self.pg0.local_ip6n
786         server_addr_vrf1 = self.pg1.remote_ip6n
787         src_addr_vrf1 = self.pg1.local_ip6n
788         server_addr_vrf2 = self.pg2.remote_ip6n
789         src_addr_vrf2 = self.pg2.local_ip6n
790
791         dmac = in6_getnsmac(inet_pton(socket.AF_INET6, dhcp_solicit_dst))
792         p_solicit_vrf0 = (Ether(dst=dmac, src=self.pg3.remote_mac) /
793                           IPv6(src=dhcp_solicit_src_vrf0,
794                                dst=dhcp_solicit_dst) /
795                           UDP(sport=DHCP6_SERVER_PORT,
796                               dport=DHCP6_CLIENT_PORT) /
797                           DHCP6_Solicit())
798         p_solicit_vrf1 = (Ether(dst=dmac, src=self.pg4.remote_mac) /
799                           IPv6(src=dhcp_solicit_src_vrf1,
800                                dst=dhcp_solicit_dst) /
801                           UDP(sport=DHCP6_SERVER_PORT,
802                               dport=DHCP6_CLIENT_PORT) /
803                           DHCP6_Solicit())
804         p_solicit_vrf2 = (Ether(dst=dmac, src=self.pg5.remote_mac) /
805                           IPv6(src=dhcp_solicit_src_vrf2,
806                                dst=dhcp_solicit_dst) /
807                           UDP(sport=DHCP6_SERVER_PORT,
808                               dport=DHCP6_CLIENT_PORT) /
809                           DHCP6_Solicit())
810
811         self.send_and_assert_no_replies(self.pg3, p_solicit_vrf0,
812                                         "DHCP with no configuration")
813         self.send_and_assert_no_replies(self.pg4, p_solicit_vrf1,
814                                         "DHCP with no configuration")
815         self.send_and_assert_no_replies(self.pg5, p_solicit_vrf2,
816                                         "DHCP with no configuration")
817
818         #
819         # DHCPv6 config in VRF 0.
820         # Packets still dropped because the client facing interface has no
821         # IPv6 config
822         #
823         self.vapi.dhcp_proxy_config(server_addr_vrf0,
824                                     src_addr_vrf0,
825                                     rx_table_id=0,
826                                     server_table_id=0,
827                                     is_ipv6=1)
828
829         self.send_and_assert_no_replies(self.pg3, p_solicit_vrf0,
830                                         "DHCP with no configuration")
831         self.send_and_assert_no_replies(self.pg4, p_solicit_vrf1,
832                                         "DHCP with no configuration")
833
834         #
835         # configure an IP address on the client facing interface
836         #
837         self.pg3.config_ip6()
838
839         #
840         # Now the DHCP requests are relayed to the server
841         #
842         self.pg3.add_stream(p_solicit_vrf0)
843         self.pg_enable_capture(self.pg_interfaces)
844         self.pg_start()
845
846         rx = self.pg0.get_capture(1)
847
848         self.verify_dhcp6_solicit(rx[0], self.pg0,
849                                   dhcp_solicit_src_vrf0,
850                                   self.pg3.remote_mac)
851
852         #
853         # Exception cases for rejected relay responses
854         #
855
856         # 1 - not a relay reply
857         p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
858                       IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
859                       UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
860                       DHCP6_Advertise())
861         self.send_and_assert_no_replies(self.pg3, p_adv_vrf0,
862                                         "DHCP6 not a relay reply")
863
864         # 2 - no relay message option
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                       DHCP6_Advertise())
870         self.send_and_assert_no_replies(self.pg3, p_adv_vrf0,
871                                         "DHCP not a relay message")
872
873         # 3 - no 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                       DHCP6OptRelayMsg(optlen=0) /
879                       DHCP6_Advertise())
880         self.send_and_assert_no_replies(self.pg3, p_adv_vrf0,
881                                         "DHCP6 no circuit ID")
882         # 4 - wrong circuit ID
883         p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
884                       IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
885                       UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
886                       DHCP6_RelayReply() /
887                       DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x05') /
888                       DHCP6OptRelayMsg(optlen=0) /
889                       DHCP6_Advertise())
890         self.send_and_assert_no_replies(self.pg3, p_adv_vrf0,
891                                         "DHCP6 wrong circuit ID")
892
893         #
894         # Send the relay response (the advertisement)
895         #   - no peer address
896         p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
897                       IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
898                       UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
899                       DHCP6_RelayReply() /
900                       DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x04') /
901                       DHCP6OptRelayMsg(optlen=0) /
902                       DHCP6_Advertise(trid=1) /
903                       DHCP6OptStatusCode(statuscode=0))
904         pkts_adv_vrf0 = [p_adv_vrf0]
905
906         self.pg0.add_stream(pkts_adv_vrf0)
907         self.pg_enable_capture(self.pg_interfaces)
908         self.pg_start()
909
910         rx = self.pg3.get_capture(1)
911
912         self.verify_dhcp6_advert(rx[0], self.pg3, "::")
913
914         #
915         # Send the relay response (the advertisement)
916         #   - with peer address
917         p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
918                       IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
919                       UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
920                       DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf0) /
921                       DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x04') /
922                       DHCP6OptRelayMsg(optlen=0) /
923                       DHCP6_Advertise(trid=1) /
924                       DHCP6OptStatusCode(statuscode=0))
925         pkts_adv_vrf0 = [p_adv_vrf0]
926
927         self.pg0.add_stream(pkts_adv_vrf0)
928         self.pg_enable_capture(self.pg_interfaces)
929         self.pg_start()
930
931         rx = self.pg3.get_capture(1)
932
933         self.verify_dhcp6_advert(rx[0], self.pg3, dhcp_solicit_src_vrf0)
934
935         #
936         # Add all the config for VRF 1 & 2
937         #
938         self.vapi.dhcp_proxy_config(server_addr_vrf1,
939                                     src_addr_vrf1,
940                                     rx_table_id=1,
941                                     server_table_id=1,
942                                     is_ipv6=1)
943         self.pg4.config_ip6()
944
945         self.vapi.dhcp_proxy_config(server_addr_vrf2,
946                                     src_addr_vrf2,
947                                     rx_table_id=2,
948                                     server_table_id=2,
949                                     is_ipv6=1)
950         self.pg5.config_ip6()
951
952         #
953         # VRF 1 solicit
954         #
955         self.pg4.add_stream(p_solicit_vrf1)
956         self.pg_enable_capture(self.pg_interfaces)
957         self.pg_start()
958
959         rx = self.pg1.get_capture(1)
960
961         self.verify_dhcp6_solicit(rx[0], self.pg1,
962                                   dhcp_solicit_src_vrf1,
963                                   self.pg4.remote_mac)
964
965         #
966         # VRF 2 solicit
967         #
968         self.pg5.add_stream(p_solicit_vrf2)
969         self.pg_enable_capture(self.pg_interfaces)
970         self.pg_start()
971
972         rx = self.pg2.get_capture(1)
973
974         self.verify_dhcp6_solicit(rx[0], self.pg2,
975                                   dhcp_solicit_src_vrf2,
976                                   self.pg5.remote_mac)
977
978         #
979         # VRF 1 Advert
980         #
981         p_adv_vrf1 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
982                       IPv6(dst=self.pg1.local_ip6, src=self.pg1.remote_ip6) /
983                       UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
984                       DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) /
985                       DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x05') /
986                       DHCP6OptRelayMsg(optlen=0) /
987                       DHCP6_Advertise(trid=1) /
988                       DHCP6OptStatusCode(statuscode=0))
989         pkts_adv_vrf1 = [p_adv_vrf1]
990
991         self.pg1.add_stream(pkts_adv_vrf1)
992         self.pg_enable_capture(self.pg_interfaces)
993         self.pg_start()
994
995         rx = self.pg4.get_capture(1)
996
997         self.verify_dhcp6_advert(rx[0], self.pg4, dhcp_solicit_src_vrf1)
998
999         #
1000         # Add VSS config
1001         #  table=1, vss_type=1, vpn_index=1, oui=4
1002         #  table=2, vss_type=0, vpn_id = "ip6-table-2"
1003         self.vapi.dhcp_proxy_set_vss(1, 1, oui=4, vpn_index=1, is_ip6=1)
1004         self.vapi.dhcp_proxy_set_vss(2, 0, "IPv6-table-2", is_ip6=1)
1005
1006         self.pg4.add_stream(p_solicit_vrf1)
1007         self.pg_enable_capture(self.pg_interfaces)
1008         self.pg_start()
1009
1010         rx = self.pg1.get_capture(1)
1011
1012         self.verify_dhcp6_solicit(rx[0], self.pg1,
1013                                   dhcp_solicit_src_vrf1,
1014                                   self.pg4.remote_mac,
1015                                   fib_id=1,
1016                                   oui=4)
1017
1018         self.pg5.add_stream(p_solicit_vrf2)
1019         self.pg_enable_capture(self.pg_interfaces)
1020         self.pg_start()
1021
1022         rx = self.pg2.get_capture(1)
1023
1024         self.verify_dhcp6_solicit(rx[0], self.pg2,
1025                                   dhcp_solicit_src_vrf2,
1026                                   self.pg5.remote_mac,
1027                                   vpn_id="IPv6-table-2")
1028
1029         #
1030         # Remove the VSS config
1031         #  relayed DHCP has default vlaues in the option.
1032         #
1033         self.vapi.dhcp_proxy_set_vss(1, is_ip6=1, is_add=0)
1034
1035         self.pg4.add_stream(p_solicit_vrf1)
1036         self.pg_enable_capture(self.pg_interfaces)
1037         self.pg_start()
1038
1039         rx = self.pg1.get_capture(1)
1040
1041         self.verify_dhcp6_solicit(rx[0], self.pg1,
1042                                   dhcp_solicit_src_vrf1,
1043                                   self.pg4.remote_mac)
1044
1045         #
1046         # Add a second DHCP server in VRF 1
1047         #  expect clients messages to be relay to both configured servers
1048         #
1049         self.pg1.generate_remote_hosts(2)
1050         server_addr12 = socket.inet_pton(AF_INET6,
1051                                          self.pg1.remote_hosts[1].ip6)
1052
1053         self.vapi.dhcp_proxy_config(server_addr12,
1054                                     src_addr_vrf1,
1055                                     rx_table_id=1,
1056                                     server_table_id=1,
1057                                     is_ipv6=1)
1058
1059         #
1060         # We'll need an ND entry for the server to send it packets
1061         #
1062         nd_entry = VppNeighbor(self,
1063                                self.pg1.sw_if_index,
1064                                self.pg1.remote_hosts[1].mac,
1065                                self.pg1.remote_hosts[1].ip6)
1066         nd_entry.add_vpp_config()
1067
1068         #
1069         # Send a discover from the client. expect two relayed messages
1070         # The frist packet is sent to the second server
1071         # We're not enforcing that here, it's just the way it is.
1072         #
1073         self.pg4.add_stream(p_solicit_vrf1)
1074         self.pg_enable_capture(self.pg_interfaces)
1075         self.pg_start()
1076
1077         rx = self.pg1.get_capture(2)
1078
1079         self.verify_dhcp6_solicit(rx[0], self.pg1,
1080                                   dhcp_solicit_src_vrf1,
1081                                   self.pg4.remote_mac)
1082         self.verify_dhcp6_solicit(rx[1], self.pg1,
1083                                   dhcp_solicit_src_vrf1,
1084                                   self.pg4.remote_mac,
1085                                   dst_mac=self.pg1.remote_hosts[1].mac,
1086                                   dst_ip=self.pg1.remote_hosts[1].ip6)
1087
1088         #
1089         # Send both packets back. Client gets both.
1090         #
1091         p1 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
1092               IPv6(dst=self.pg1.local_ip6, src=self.pg1.remote_ip6) /
1093               UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
1094               DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) /
1095               DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x05') /
1096               DHCP6OptRelayMsg(optlen=0) /
1097               DHCP6_Advertise(trid=1) /
1098               DHCP6OptStatusCode(statuscode=0))
1099         p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_hosts[1].mac) /
1100               IPv6(dst=self.pg1.local_ip6, src=self.pg1._remote_hosts[1].ip6) /
1101               UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
1102               DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) /
1103               DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x05') /
1104               DHCP6OptRelayMsg(optlen=0) /
1105               DHCP6_Advertise(trid=1) /
1106               DHCP6OptStatusCode(statuscode=0))
1107
1108         pkts = [p1, p2]
1109
1110         self.pg1.add_stream(pkts)
1111         self.pg_enable_capture(self.pg_interfaces)
1112         self.pg_start()
1113
1114         rx = self.pg4.get_capture(2)
1115
1116         self.verify_dhcp6_advert(rx[0], self.pg4, dhcp_solicit_src_vrf1)
1117         self.verify_dhcp6_advert(rx[1], self.pg4, dhcp_solicit_src_vrf1)
1118
1119         #
1120         # Ensure only solicit messages are duplicated
1121         #
1122         p_request_vrf1 = (Ether(dst=dmac, src=self.pg4.remote_mac) /
1123                           IPv6(src=dhcp_solicit_src_vrf1,
1124                                dst=dhcp_solicit_dst) /
1125                           UDP(sport=DHCP6_SERVER_PORT,
1126                               dport=DHCP6_CLIENT_PORT) /
1127                           DHCP6_Request())
1128
1129         self.pg4.add_stream(p_request_vrf1)
1130         self.pg_enable_capture(self.pg_interfaces)
1131         self.pg_start()
1132
1133         rx = self.pg1.get_capture(1)
1134
1135         #
1136         # Test we drop DHCP packets from addresses that are not configured as
1137         # DHCP servers
1138         #
1139         p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_hosts[1].mac) /
1140               IPv6(dst=self.pg1.local_ip6, src="3001::1") /
1141               UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
1142               DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) /
1143               DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x05') /
1144               DHCP6OptRelayMsg(optlen=0) /
1145               DHCP6_Advertise(trid=1) /
1146               DHCP6OptStatusCode(statuscode=0))
1147         self.send_and_assert_no_replies(self.pg1, p2,
1148                                         "DHCP6 not from server")
1149
1150         #
1151         # Remove the second DHCP server
1152         #
1153         self.vapi.dhcp_proxy_config(server_addr12,
1154                                     src_addr_vrf1,
1155                                     rx_table_id=1,
1156                                     server_table_id=1,
1157                                     is_ipv6=1,
1158                                     is_add=0)
1159
1160         #
1161         # Test we can still relay with the first
1162         #
1163         self.pg4.add_stream(p_solicit_vrf1)
1164         self.pg_enable_capture(self.pg_interfaces)
1165         self.pg_start()
1166
1167         rx = self.pg1.get_capture(1)
1168
1169         self.verify_dhcp6_solicit(rx[0], self.pg1,
1170                                   dhcp_solicit_src_vrf1,
1171                                   self.pg4.remote_mac)
1172
1173         #
1174         # Cleanup
1175         #
1176         self.vapi.dhcp_proxy_config(server_addr_vrf2,
1177                                     src_addr_vrf2,
1178                                     rx_table_id=2,
1179                                     server_table_id=2,
1180                                     is_ipv6=1,
1181                                     is_add=0)
1182         self.vapi.dhcp_proxy_config(server_addr_vrf1,
1183                                     src_addr_vrf1,
1184                                     rx_table_id=1,
1185                                     server_table_id=1,
1186                                     is_ipv6=1,
1187                                     is_add=0)
1188         self.vapi.dhcp_proxy_config(server_addr_vrf0,
1189                                     src_addr_vrf0,
1190                                     rx_table_id=0,
1191                                     server_table_id=0,
1192                                     is_ipv6=1,
1193                                     is_add=0)
1194
1195         # duplicate delete
1196         self.vapi.dhcp_proxy_config(server_addr_vrf0,
1197                                     src_addr_vrf0,
1198                                     rx_table_id=0,
1199                                     server_table_id=0,
1200                                     is_ipv6=1,
1201                                     is_add=0)
1202         self.pg3.unconfig_ip6()
1203         self.pg4.unconfig_ip6()
1204         self.pg5.unconfig_ip6()
1205
1206     def test_dhcp_client(self):
1207         """ DHCP Client"""
1208
1209         hostname = 'universal-dp'
1210
1211         self.pg_enable_capture(self.pg_interfaces)
1212
1213         #
1214         # Configure DHCP client on PG3 and capture the discover sent
1215         #
1216         self.vapi.dhcp_client_config(self.pg3.sw_if_index, hostname)
1217
1218         rx = self.pg3.get_capture(1)
1219
1220         self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname)
1221
1222         #
1223         # Send back on offer, expect the request
1224         #
1225         p_offer = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1226                    IP(src=self.pg3.remote_ip4, dst="255.255.255.255") /
1227                    UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1228                    BOOTP(op=1,
1229                          yiaddr=self.pg3.local_ip4,
1230                          chaddr=mac_pton(self.pg3.local_mac)) /
1231                    DHCP(options=[('message-type', 'offer'),
1232                                  ('server_id', self.pg3.remote_ip4),
1233                                  'end']))
1234
1235         self.pg3.add_stream(p_offer)
1236         self.pg_enable_capture(self.pg_interfaces)
1237         self.pg_start()
1238
1239         rx = self.pg3.get_capture(1)
1240         self.verify_orig_dhcp_request(rx[0], self.pg3, hostname,
1241                                       self.pg3.local_ip4)
1242
1243         #
1244         # Send an acknowledgment
1245         #
1246         p_ack = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1247                  IP(src=self.pg3.remote_ip4, dst="255.255.255.255") /
1248                  UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1249                  BOOTP(op=1, yiaddr=self.pg3.local_ip4,
1250                        chaddr=mac_pton(self.pg3.local_mac)) /
1251                  DHCP(options=[('message-type', 'ack'),
1252                                ('subnet_mask', "255.255.255.0"),
1253                                ('router', self.pg3.remote_ip4),
1254                                ('server_id', self.pg3.remote_ip4),
1255                                ('lease_time', 43200),
1256                                'end']))
1257
1258         self.pg3.add_stream(p_ack)
1259         self.pg_enable_capture(self.pg_interfaces)
1260         self.pg_start()
1261
1262         #
1263         # We'll get an ARP request for the router address
1264         #
1265         rx = self.pg3.get_capture(1)
1266
1267         self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
1268         self.pg_enable_capture(self.pg_interfaces)
1269
1270         #
1271         # At the end of this procedure there should be a connected route
1272         # in the FIB
1273         #
1274         self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
1275         self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
1276
1277         # remove the left over ARP entry
1278         self.vapi.ip_neighbor_add_del(self.pg3.sw_if_index,
1279                                       self.pg3.remote_mac,
1280                                       self.pg3.remote_ip4,
1281                                       is_add=0)
1282
1283         #
1284         # remove the DHCP config
1285         #
1286         self.vapi.dhcp_client_config(self.pg3.sw_if_index, hostname, is_add=0)
1287
1288         #
1289         # and now the route should be gone
1290         #
1291         self.assertFalse(find_route(self, self.pg3.local_ip4, 32))
1292         self.assertFalse(find_route(self, self.pg3.local_ip4, 24))
1293
1294         #
1295         # Start the procedure again. this time have VPP send the client-ID
1296         #
1297         self.pg3.admin_down()
1298         self.sleep(1)
1299         self.pg3.admin_up()
1300         self.vapi.dhcp_client_config(self.pg3.sw_if_index, hostname,
1301                                      client_id=self.pg3.local_mac)
1302
1303         rx = self.pg3.get_capture(1)
1304
1305         self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname,
1306                                        self.pg3.local_mac)
1307
1308         # TODO: VPP DHCP client should not accept DHCP OFFER message with
1309         # the XID (Transaction ID) not matching the XID of the most recent
1310         # DHCP DISCOVERY message.
1311         # Such DHCP OFFER message must be silently discarded - RFC2131.
1312         # Reported in Jira ticket: VPP-99
1313         self.pg3.add_stream(p_offer)
1314         self.pg_enable_capture(self.pg_interfaces)
1315         self.pg_start()
1316
1317         rx = self.pg3.get_capture(1)
1318         self.verify_orig_dhcp_request(rx[0], self.pg3, hostname,
1319                                       self.pg3.local_ip4)
1320
1321         #
1322         # unicast the ack to the offered address
1323         #
1324         p_ack = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1325                  IP(src=self.pg3.remote_ip4, dst=self.pg3.local_ip4) /
1326                  UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1327                  BOOTP(op=1, yiaddr=self.pg3.local_ip4,
1328                        chaddr=mac_pton(self.pg3.local_mac)) /
1329                  DHCP(options=[('message-type', 'ack'),
1330                                ('subnet_mask', "255.255.255.0"),
1331                                ('router', self.pg3.remote_ip4),
1332                                ('server_id', self.pg3.remote_ip4),
1333                                ('lease_time', 43200),
1334                                'end']))
1335
1336         self.pg3.add_stream(p_ack)
1337         self.pg_enable_capture(self.pg_interfaces)
1338         self.pg_start()
1339
1340         #
1341         # We'll get an ARP request for the router address
1342         #
1343         rx = self.pg3.get_capture(1)
1344
1345         self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
1346         self.pg_enable_capture(self.pg_interfaces)
1347
1348         #
1349         # At the end of this procedure there should be a connected route
1350         # in the FIB
1351         #
1352         self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
1353         self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
1354
1355         #
1356         # remove the DHCP config
1357         #
1358         self.vapi.dhcp_client_config(self.pg3.sw_if_index, hostname, is_add=0)
1359
1360         self.assertFalse(find_route(self, self.pg3.local_ip4, 32))
1361         self.assertFalse(find_route(self, self.pg3.local_ip4, 24))
1362
1363         #
1364         # Rince and repeat, this time with VPP configured not to set
1365         # the braodcast flag in the discover and request messages,
1366         # and for the server to unicast the responses.
1367         #
1368         # Configure DHCP client on PG3 and capture the discover sent
1369         #
1370         self.vapi.dhcp_client_config(self.pg3.sw_if_index, hostname,
1371                                      set_broadcast_flag=0)
1372
1373         rx = self.pg3.get_capture(1)
1374
1375         self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname,
1376                                        broadcast=0)
1377
1378         #
1379         # Send back on offer, unicasted to the offered address.
1380         # Expect the request.
1381         #
1382         p_offer = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1383                    IP(src=self.pg3.remote_ip4, dst=self.pg3.local_ip4) /
1384                    UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1385                    BOOTP(op=1, yiaddr=self.pg3.local_ip4,
1386                          chaddr=mac_pton(self.pg3.local_mac)) /
1387                    DHCP(options=[('message-type', 'offer'),
1388                                  ('server_id', self.pg3.remote_ip4),
1389                                  'end']))
1390
1391         self.pg3.add_stream(p_offer)
1392         self.pg_enable_capture(self.pg_interfaces)
1393         self.pg_start()
1394
1395         rx = self.pg3.get_capture(1)
1396         self.verify_orig_dhcp_request(rx[0], self.pg3, hostname,
1397                                       self.pg3.local_ip4,
1398                                       broadcast=0)
1399
1400         #
1401         # Send an acknowledgment
1402         #
1403         p_ack = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1404                  IP(src=self.pg3.remote_ip4, dst=self.pg3.local_ip4) /
1405                  UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1406                  BOOTP(op=1, yiaddr=self.pg3.local_ip4,
1407                        chaddr=mac_pton(self.pg3.local_mac)) /
1408                  DHCP(options=[('message-type', 'ack'),
1409                                ('subnet_mask', "255.255.255.0"),
1410                                ('router', self.pg3.remote_ip4),
1411                                ('server_id', self.pg3.remote_ip4),
1412                                ('lease_time', 43200),
1413                                'end']))
1414
1415         self.pg3.add_stream(p_ack)
1416         self.pg_enable_capture(self.pg_interfaces)
1417         self.pg_start()
1418
1419         #
1420         # We'll get an ARP request for the router address
1421         #
1422         rx = self.pg3.get_capture(1)
1423
1424         self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
1425         self.pg_enable_capture(self.pg_interfaces)
1426
1427         #
1428         # At the end of this procedure there should be a connected route
1429         # in the FIB
1430         #
1431         self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
1432         self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
1433
1434         # remove the left over ARP entry
1435         self.vapi.ip_neighbor_add_del(self.pg3.sw_if_index,
1436                                       self.pg3.remote_mac,
1437                                       self.pg3.remote_ip4,
1438                                       is_add=0)
1439
1440         #
1441         # read the DHCP client details from a dump
1442         #
1443         clients = self.vapi.dhcp_client_dump()
1444
1445         self.assertEqual(clients[0].client.sw_if_index,
1446                          self.pg3.sw_if_index)
1447         self.assertEqual(clients[0].lease.sw_if_index,
1448                          self.pg3.sw_if_index)
1449         self.assertEqual(clients[0].client.hostname.rstrip('\0'),
1450                          hostname)
1451         self.assertEqual(clients[0].lease.hostname.rstrip('\0'),
1452                          hostname)
1453         self.assertEqual(clients[0].lease.is_ipv6, 0)
1454         # 0 = DISCOVER, 1 = REQUEST, 2 = BOUND
1455         self.assertEqual(clients[0].lease.state, 2)
1456         self.assertEqual(clients[0].lease.mask_width, 24)
1457         self.assertEqual(clients[0].lease.router_address.rstrip('\0'),
1458                          self.pg3.remote_ip4n)
1459         self.assertEqual(clients[0].lease.host_address.rstrip('\0'),
1460                          self.pg3.local_ip4n)
1461
1462         #
1463         # remove the DHCP config
1464         #
1465         self.vapi.dhcp_client_config(self.pg3.sw_if_index, hostname, is_add=0)
1466
1467         #
1468         # and now the route should be gone
1469         #
1470         self.assertFalse(find_route(self, self.pg3.local_ip4, 32))
1471         self.assertFalse(find_route(self, self.pg3.local_ip4, 24))
1472
1473         #
1474         # Start the procedure again. Use requested lease time option.
1475         #
1476         self.pg3.admin_down()
1477         self.sleep(1)
1478         self.pg3.admin_up()
1479         self.vapi.dhcp_client_config(self.pg3.sw_if_index, hostname)
1480
1481         rx = self.pg3.get_capture(1)
1482
1483         self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname)
1484
1485         #
1486         # Send back on offer with requested lease time, expect the request
1487         #
1488         lease_time = 1
1489         p_offer = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1490                    IP(src=self.pg3.remote_ip4, dst='255.255.255.255') /
1491                    UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1492                    BOOTP(op=1,
1493                          yiaddr=self.pg3.local_ip4,
1494                          chaddr=mac_pton(self.pg3.local_mac)) /
1495                    DHCP(options=[('message-type', 'offer'),
1496                                  ('server_id', self.pg3.remote_ip4),
1497                                  ('lease_time', lease_time),
1498                                  'end']))
1499
1500         self.pg3.add_stream(p_offer)
1501         self.pg_enable_capture(self.pg_interfaces)
1502         self.pg_start()
1503
1504         rx = self.pg3.get_capture(1)
1505         self.verify_orig_dhcp_request(rx[0], self.pg3, hostname,
1506                                       self.pg3.local_ip4)
1507
1508         #
1509         # Send an acknowledgment
1510         #
1511         p_ack = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1512                  IP(src=self.pg3.remote_ip4, dst='255.255.255.255') /
1513                  UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1514                  BOOTP(op=1, yiaddr=self.pg3.local_ip4,
1515                        chaddr=mac_pton(self.pg3.local_mac)) /
1516                  DHCP(options=[('message-type', 'ack'),
1517                                ('subnet_mask', '255.255.255.0'),
1518                                ('router', self.pg3.remote_ip4),
1519                                ('server_id', self.pg3.remote_ip4),
1520                                ('lease_time', lease_time),
1521                                'end']))
1522
1523         self.pg3.add_stream(p_ack)
1524         self.pg_enable_capture(self.pg_interfaces)
1525         self.pg_start()
1526
1527         #
1528         # We'll get an ARP request for the router address
1529         #
1530         rx = self.pg3.get_capture(1)
1531
1532         self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
1533
1534         #
1535         # At the end of this procedure there should be a connected route
1536         # in the FIB
1537         #
1538         self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
1539         self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
1540
1541         # remove the left over ARP entry
1542         self.vapi.ip_neighbor_add_del(self.pg3.sw_if_index,
1543                                       self.pg3.remote_mac,
1544                                       self.pg3.remote_ip4,
1545                                       is_add=0)
1546
1547         #
1548         # the route should be gone after the lease expires
1549         #
1550         self.assertTrue(self.wait_for_no_route(self.pg3.local_ip4, 32))
1551         self.assertTrue(self.wait_for_no_route(self.pg3.local_ip4, 24))
1552
1553         #
1554         # remove the DHCP config
1555         #
1556         self.vapi.dhcp_client_config(self.pg3.sw_if_index, hostname, is_add=0)
1557
1558
1559 if __name__ == '__main__':
1560     unittest.main(testRunner=VppTestRunner)