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