ChetanGaonker | 42d7581 | 2016-06-06 16:32:52 -0700 | [diff] [blame] | 1 | # |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 2 | # Copyright 2016-present Ciena Corporation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
ChetanGaonker | 42d7581 | 2016-06-06 16:32:52 -0700 | [diff] [blame] | 7 | # |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
ChetanGaonker | 42d7581 | 2016-06-06 16:32:52 -0700 | [diff] [blame] | 9 | # |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 16 | from scapy.all import * |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 17 | from CordTestUtils import log_test |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 18 | |
| 19 | conf.verb = 0 # Disable Scapy verbosity |
| 20 | conf.checkIPaddr = 0 # Don't check response packets for matching destination IPs |
| 21 | |
| 22 | class DHCPTest: |
| 23 | |
Anil Kumar Sanka | cfa7c58 | 2016-12-09 23:17:22 +0000 | [diff] [blame] | 24 | def __init__(self, seed_ip = '192.168.1.1', iface = 'veth0',lease_time=600): |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 25 | self.seed_ip = seed_ip |
| 26 | self.seed_mac = self.ipToMac(self.seed_ip) |
| 27 | self.iface = iface |
Anil Kumar Sanka | cfa7c58 | 2016-12-09 23:17:22 +0000 | [diff] [blame] | 28 | self.lease_time = lease_time |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 29 | self.mac_map = {} |
| 30 | self.mac_inverse_map = {} |
Chetan Gaonker | f72ca40 | 2016-05-02 16:29:32 -0700 | [diff] [blame] | 31 | self.bootpmac = None |
| 32 | self.dhcpresp = None |
Chetan Gaonker | c11d322 | 2016-05-11 17:39:36 -0700 | [diff] [blame] | 33 | self.servermac = None |
Chetan Gaonker | 1dabecc | 2016-05-16 14:56:01 -0700 | [diff] [blame] | 34 | self.return_option = None |
Chetan Gaonker | c11d322 | 2016-05-11 17:39:36 -0700 | [diff] [blame] | 35 | self.after_T2 = False |
Chetan Gaonker | 1dabecc | 2016-05-16 14:56:01 -0700 | [diff] [blame] | 36 | self.send_different_option = None |
ChetanGaonker | 5b984cb | 2016-07-12 15:50:49 -0700 | [diff] [blame] | 37 | self.specific_lease = None |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 38 | |
| 39 | def is_mcast(self, ip): |
| 40 | mcast_octet = (atol(ip) >> 24) & 0xff |
| 41 | return True if mcast_octet >= 224 and mcast_octet <= 239 else False |
| 42 | |
Chetan Gaonker | 1f7c3f8 | 2016-03-08 12:17:37 -0800 | [diff] [blame] | 43 | def discover(self, mac = None, update_seed = False): |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 44 | '''Send a DHCP discover/offer''' |
| 45 | |
| 46 | if mac is None: |
| 47 | mac = self.seed_mac |
| 48 | if update_seed: |
| 49 | self.seed_ip = self.incIP(self.seed_ip) |
| 50 | self.seed_mac = self.ipToMac(self.seed_ip) |
| 51 | mac = self.seed_mac |
Chetan Gaonker | f148386 | 2016-05-06 14:14:31 -0700 | [diff] [blame] | 52 | |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 53 | chmac = self.macToChaddr(mac) |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 54 | #log_test.info('mac and chmac are %s %s'%(mac, chmac)) |
Chetan Gaonker | f72ca40 | 2016-05-02 16:29:32 -0700 | [diff] [blame] | 55 | self.bootpmac = chmac |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 56 | L2 = Ether(dst="ff:ff:ff:ff:ff:ff", src=mac) |
| 57 | L3 = IP(src="0.0.0.0", dst="255.255.255.255") |
| 58 | L4 = UDP(sport=68, dport=67) |
| 59 | L5 = BOOTP(chaddr=chmac) |
| 60 | L6 = DHCP(options=[("message-type","discover"),"end"]) |
Chetan Gaonker | 49ef085 | 2016-03-23 15:06:18 -0700 | [diff] [blame] | 61 | resp = srp1(L2/L3/L4/L5/L6, filter="udp and port 68", timeout=10, iface=self.iface) |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 62 | #log_test.info('dhcp discover packet is %s'%(L2/L3/L4/L5/L6).show()) |
ChetanGaonker | 42d7581 | 2016-06-06 16:32:52 -0700 | [diff] [blame] | 63 | self.dhcpresp = resp |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 64 | #log_test.info('discover response is %s'%resp.show()) |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 65 | try: |
| 66 | srcIP = resp.yiaddr |
| 67 | serverIP = resp.siaddr |
| 68 | except AttributeError: |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 69 | log_test.info("Failed to acquire IP via DHCP for %s on interface %s" %(mac, self.iface)) |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 70 | return (None, None) |
| 71 | |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 72 | subnet_mask = "0.0.0.0" |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 73 | for x in resp.lastlayer().options: |
| 74 | if(x == 'end'): |
| 75 | break |
| 76 | op,val = x |
| 77 | if(op == "subnet_mask"): |
| 78 | subnet_mask = val |
| 79 | elif(op == 'server_id'): |
| 80 | server_id = val |
Chetan Gaonker | f148386 | 2016-05-06 14:14:31 -0700 | [diff] [blame] | 81 | |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 82 | L5 = BOOTP(chaddr=chmac, yiaddr=srcIP) |
Chetan Gaonker | f148386 | 2016-05-06 14:14:31 -0700 | [diff] [blame] | 83 | L6 = DHCP(options=[("message-type","request"), ("server_id",server_id), |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 84 | ("subnet_mask",subnet_mask), ("requested_addr",srcIP), "end"]) |
Anil Kumar Sanka | cfa7c58 | 2016-12-09 23:17:22 +0000 | [diff] [blame] | 85 | resp2 = srp1(L2/L3/L4/L5/L6, filter="udp and port 68", timeout=10, iface=self.iface) |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 86 | #log_test.info('request response is %s'%resp2.show()) |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 87 | self.mac_map[mac] = (srcIP, serverIP) |
| 88 | self.mac_inverse_map[srcIP] = (mac, serverIP) |
| 89 | return (srcIP, serverIP) |
| 90 | |
Anil Kumar Sanka | cfa7c58 | 2016-12-09 23:17:22 +0000 | [diff] [blame] | 91 | def only_discover(self, mac = None, desired = False, lease_time = False, lease_value=600, multiple = False): |
Chetan Gaonker | f72ca40 | 2016-05-02 16:29:32 -0700 | [diff] [blame] | 92 | '''Send a DHCP discover''' |
| 93 | |
| 94 | if mac is None: |
ChetanGaonker | 42d7581 | 2016-06-06 16:32:52 -0700 | [diff] [blame] | 95 | if multiple: |
| 96 | mac = RandMAC()._fix() |
| 97 | else: |
| 98 | mac = self.seed_mac |
| 99 | |
Chetan Gaonker | f72ca40 | 2016-05-02 16:29:32 -0700 | [diff] [blame] | 100 | |
| 101 | chmac = self.macToChaddr(mac) |
Chetan Gaonker | f148386 | 2016-05-06 14:14:31 -0700 | [diff] [blame] | 102 | self.bootpmac = chmac |
Chetan Gaonker | f72ca40 | 2016-05-02 16:29:32 -0700 | [diff] [blame] | 103 | L2 = Ether(dst="ff:ff:ff:ff:ff:ff", src=mac) |
| 104 | L3 = IP(src="0.0.0.0", dst="255.255.255.255") |
| 105 | L4 = UDP(sport=68, dport=67) |
Chetan Gaonker | f148386 | 2016-05-06 14:14:31 -0700 | [diff] [blame] | 106 | L5 = BOOTP(chaddr=chmac) |
Chetan Gaonker | f72ca40 | 2016-05-02 16:29:32 -0700 | [diff] [blame] | 107 | if desired: |
| 108 | L6 = DHCP(options=[("message-type","discover"),("requested_addr",self.seed_ip),"end"]) |
ChetanGaonker | 42d7581 | 2016-06-06 16:32:52 -0700 | [diff] [blame] | 109 | |
Chetan Gaonker | c11d322 | 2016-05-11 17:39:36 -0700 | [diff] [blame] | 110 | elif lease_time: |
Anil Kumar Sanka | cfa7c58 | 2016-12-09 23:17:22 +0000 | [diff] [blame] | 111 | L6 = DHCP(options=[("message-type","discover"),("lease_time",lease_value),"end"]) |
ChetanGaonker | 42d7581 | 2016-06-06 16:32:52 -0700 | [diff] [blame] | 112 | |
Chetan Gaonker | c11d322 | 2016-05-11 17:39:36 -0700 | [diff] [blame] | 113 | else: |
| 114 | L6 = DHCP(options=[("message-type","discover"),"end"]) |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 115 | #log_test.info('only discover packet is %s'%(L2/L3/L4/L5/L6).show()) |
Chetan Gaonker | f148386 | 2016-05-06 14:14:31 -0700 | [diff] [blame] | 116 | |
| 117 | resp = srp1(L2/L3/L4/L5/L6, filter="udp and port 68", timeout=10, iface=self.iface) |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 118 | #log_test.info('discovery packet is %s'%(L2/L3/L4/L5/L6).show()) |
Chetan Gaonker | f148386 | 2016-05-06 14:14:31 -0700 | [diff] [blame] | 119 | if resp == None: |
ChetanGaonker | 42d7581 | 2016-06-06 16:32:52 -0700 | [diff] [blame] | 120 | return (None, None, mac, None) |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 121 | #log_test.info('only discover response is %s'%resp.show()) |
ChetanGaonker | 42d7581 | 2016-06-06 16:32:52 -0700 | [diff] [blame] | 122 | |
Chetan Gaonker | f148386 | 2016-05-06 14:14:31 -0700 | [diff] [blame] | 123 | self.dhcpresp = resp |
| 124 | for x in resp.lastlayer().options: |
| 125 | if(x == 'end'): |
| 126 | break |
| 127 | op,val = x |
| 128 | if(op == "message-type"): |
ChetanGaonker | 42d7581 | 2016-06-06 16:32:52 -0700 | [diff] [blame] | 129 | |
Chetan Gaonker | f148386 | 2016-05-06 14:14:31 -0700 | [diff] [blame] | 130 | if(val == 2): |
ChetanGaonker | 42d7581 | 2016-06-06 16:32:52 -0700 | [diff] [blame] | 131 | |
Chetan Gaonker | f148386 | 2016-05-06 14:14:31 -0700 | [diff] [blame] | 132 | try: |
| 133 | srcIP = resp.yiaddr |
| 134 | serverIP = resp.siaddr |
| 135 | except AttributeError: |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 136 | log_test.info("In Attribute error.") |
| 137 | log_test.info("Failed to acquire IP via DHCP for %s on interface %s" %(mac, self.iface)) |
ChetanGaonker | 42d7581 | 2016-06-06 16:32:52 -0700 | [diff] [blame] | 138 | return (None, None, None, None) |
Chetan Gaonker | 1dabecc | 2016-05-16 14:56:01 -0700 | [diff] [blame] | 139 | |
| 140 | if self.return_option: |
Chetan Gaonker | 717b294 | 2016-05-13 17:42:59 -0700 | [diff] [blame] | 141 | for x in resp.lastlayer().options: |
| 142 | if(x == 'end'): |
| 143 | break |
| 144 | op,val = x |
ChetanGaonker | 42d7581 | 2016-06-06 16:32:52 -0700 | [diff] [blame] | 145 | |
Chetan Gaonker | 717b294 | 2016-05-13 17:42:59 -0700 | [diff] [blame] | 146 | if op == "lease_time": |
Chetan Gaonker | 1dabecc | 2016-05-16 14:56:01 -0700 | [diff] [blame] | 147 | if self.return_option == 'lease': |
Chetan Gaonker | 717b294 | 2016-05-13 17:42:59 -0700 | [diff] [blame] | 148 | return (srcIP, serverIP, mac, val) |
| 149 | |
| 150 | elif op == "subnet_mask": |
Chetan Gaonker | 1dabecc | 2016-05-16 14:56:01 -0700 | [diff] [blame] | 151 | if self.return_option == 'subnet': |
ChetanGaonker | 42d7581 | 2016-06-06 16:32:52 -0700 | [diff] [blame] | 152 | return (srcIP, serverIP, mac, val) |
Chetan Gaonker | 1dabecc | 2016-05-16 14:56:01 -0700 | [diff] [blame] | 153 | elif op == "router": |
| 154 | if self.return_option == 'router': |
ChetanGaonker | 42d7581 | 2016-06-06 16:32:52 -0700 | [diff] [blame] | 155 | return (srcIP, serverIP, mac, val) |
Chetan Gaonker | 1dabecc | 2016-05-16 14:56:01 -0700 | [diff] [blame] | 156 | elif op == "broadcast_address": |
| 157 | if self.return_option == 'broadcast_address': |
ChetanGaonker | 42d7581 | 2016-06-06 16:32:52 -0700 | [diff] [blame] | 158 | return (srcIP, serverIP, mac, val) |
Chetan Gaonker | 1dabecc | 2016-05-16 14:56:01 -0700 | [diff] [blame] | 159 | elif op == "name_server": |
| 160 | if self.return_option == 'dns': |
ChetanGaonker | 42d7581 | 2016-06-06 16:32:52 -0700 | [diff] [blame] | 161 | return (srcIP, serverIP, mac, val) |
Chetan Gaonker | c11d322 | 2016-05-11 17:39:36 -0700 | [diff] [blame] | 162 | |
ChetanGaonker | 42d7581 | 2016-06-06 16:32:52 -0700 | [diff] [blame] | 163 | |
Chetan Gaonker | 1dabecc | 2016-05-16 14:56:01 -0700 | [diff] [blame] | 164 | else: |
ChetanGaonker | 42d7581 | 2016-06-06 16:32:52 -0700 | [diff] [blame] | 165 | return (srcIP, serverIP, mac, None) |
Chetan Gaonker | f148386 | 2016-05-06 14:14:31 -0700 | [diff] [blame] | 166 | elif(val == 6): |
ChetanGaonker | 42d7581 | 2016-06-06 16:32:52 -0700 | [diff] [blame] | 167 | return (None, None, mac, None) |
Chetan Gaonker | f72ca40 | 2016-05-02 16:29:32 -0700 | [diff] [blame] | 168 | |
ChetanGaonker | 42d7581 | 2016-06-06 16:32:52 -0700 | [diff] [blame] | 169 | |
Anil Kumar Sanka | cfa7c58 | 2016-12-09 23:17:22 +0000 | [diff] [blame] | 170 | def only_request(self, cip, mac, cl_reboot = False, lease_time = False, lease_value=600, renew_time = False, rebind_time = False, unicast = False): |
Chetan Gaonker | f72ca40 | 2016-05-02 16:29:32 -0700 | [diff] [blame] | 171 | '''Send a DHCP offer''' |
ChetanGaonker | 42d7581 | 2016-06-06 16:32:52 -0700 | [diff] [blame] | 172 | |
Chetan Gaonker | f72ca40 | 2016-05-02 16:29:32 -0700 | [diff] [blame] | 173 | subnet_mask = "0.0.0.0" |
| 174 | for x in self.dhcpresp.lastlayer().options: |
Chetan Gaonker | f148386 | 2016-05-06 14:14:31 -0700 | [diff] [blame] | 175 | if(x == 'end'): |
| 176 | break |
| 177 | op,val = x |
| 178 | if(op == "subnet_mask"): |
| 179 | subnet_mask = val |
| 180 | elif(op == 'server_id'): |
| 181 | server_id = val |
Chetan Gaonker | f72ca40 | 2016-05-02 16:29:32 -0700 | [diff] [blame] | 182 | |
Chetan Gaonker | c11d322 | 2016-05-11 17:39:36 -0700 | [diff] [blame] | 183 | if unicast and self.servermac: |
| 184 | L2 = Ether(dst=self.servermac, src=mac) |
| 185 | L3 = IP(src=cip, dst=server_id) |
ChetanGaonker | 42d7581 | 2016-06-06 16:32:52 -0700 | [diff] [blame] | 186 | else: |
Chetan Gaonker | c11d322 | 2016-05-11 17:39:36 -0700 | [diff] [blame] | 187 | L2 = Ether(dst="ff:ff:ff:ff:ff:ff", src=mac) |
| 188 | if self.after_T2: |
| 189 | L3 = IP(src=cip, dst="255.255.255.255") |
| 190 | else: |
| 191 | L3 = IP(src="0.0.0.0", dst="255.255.255.255") |
Chetan Gaonker | f72ca40 | 2016-05-02 16:29:32 -0700 | [diff] [blame] | 192 | L4 = UDP(sport=68, dport=67) |
ChetanGaonker | 42d7581 | 2016-06-06 16:32:52 -0700 | [diff] [blame] | 193 | |
Chetan Gaonker | c11d322 | 2016-05-11 17:39:36 -0700 | [diff] [blame] | 194 | if self.after_T2 == True: |
| 195 | L5 = BOOTP(chaddr=self.bootpmac, ciaddr = cip) |
| 196 | else: |
| 197 | |
| 198 | L5 = BOOTP(chaddr=self.bootpmac, yiaddr=cip) |
ChetanGaonker | 42d7581 | 2016-06-06 16:32:52 -0700 | [diff] [blame] | 199 | |
Chetan Gaonker | c11d322 | 2016-05-11 17:39:36 -0700 | [diff] [blame] | 200 | if cl_reboot or self.after_T2: |
ChetanGaonker | 42d7581 | 2016-06-06 16:32:52 -0700 | [diff] [blame] | 201 | L6 = DHCP(options=[("message-type","request"),("subnet_mask",subnet_mask), ("requested_addr",cip), "end"]) |
Chetan Gaonker | 1dabecc | 2016-05-16 14:56:01 -0700 | [diff] [blame] | 202 | elif self.send_different_option: |
| 203 | if self.send_different_option == 'subnet': |
| 204 | L6 = DHCP(options=[("message-type","request"),("server_id",server_id), |
Anil Kumar Sanka | cfa7c58 | 2016-12-09 23:17:22 +0000 | [diff] [blame] | 205 | ("subnet_mask",'255.255.252.252'), ("requested_addr",cip), "end"]) |
Chetan Gaonker | 1dabecc | 2016-05-16 14:56:01 -0700 | [diff] [blame] | 206 | elif self.send_different_option == 'router': |
| 207 | L6 = DHCP(options=[("message-type","request"),("server_id",server_id), |
| 208 | ("subnet_mask",subnet_mask), ("router",'1.1.1.1'), ("requested_addr",cip), "end"]) |
| 209 | elif self.send_different_option == 'broadcast_address': |
| 210 | L6 = DHCP(options=[("message-type","request"),("server_id",server_id), |
| 211 | ("subnet_mask",subnet_mask), ("broadcast_address",'1.1.1.1'), ("requested_addr",cip), "end"]) |
| 212 | |
| 213 | elif self.send_different_option == 'dns': |
| 214 | L6 = DHCP(options=[("message-type","request"),("server_id",server_id), |
| 215 | ("subnet_mask",subnet_mask), ("name_server",'1.1.1.1'), ("requested_addr",cip), "end"]) |
ChetanGaonker | 42d7581 | 2016-06-06 16:32:52 -0700 | [diff] [blame] | 216 | |
Anil Kumar Sanka | cfa7c58 | 2016-12-09 23:17:22 +0000 | [diff] [blame] | 217 | elif lease_time: |
ChetanGaonker | 5860c18 | 2016-07-05 16:33:06 -0700 | [diff] [blame] | 218 | L6 = DHCP(options=[("message-type","request"), ("server_id",server_id), |
Anil Kumar Sanka | cfa7c58 | 2016-12-09 23:17:22 +0000 | [diff] [blame] | 219 | ("subnet_mask",subnet_mask), ("requested_addr",cip),("lease_time",lease_value), "end"]) |
Chetan Gaonker | c11d322 | 2016-05-11 17:39:36 -0700 | [diff] [blame] | 220 | else: |
ChetanGaonker | 5860c18 | 2016-07-05 16:33:06 -0700 | [diff] [blame] | 221 | L6 = DHCP(options=[("message-type","request"), ("server_id",server_id), |
Chetan Gaonker | c11d322 | 2016-05-11 17:39:36 -0700 | [diff] [blame] | 222 | ("subnet_mask",subnet_mask), ("requested_addr",cip), "end"]) |
| 223 | |
Chetan Gaonker | f72ca40 | 2016-05-02 16:29:32 -0700 | [diff] [blame] | 224 | resp=srp1(L2/L3/L4/L5/L6, filter="udp and port 68", timeout=10, iface=self.iface) |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 225 | #log_test.info('request packet is %s'%(L2/L3/L4/L5/L6).show()) |
| 226 | #log_test.info('response packet is %s'%resp.show()) |
Chetan Gaonker | f148386 | 2016-05-06 14:14:31 -0700 | [diff] [blame] | 227 | if resp == None: |
| 228 | return (None, None) |
Chetan Gaonker | 1dabecc | 2016-05-16 14:56:01 -0700 | [diff] [blame] | 229 | |
| 230 | |
| 231 | self.servermac = resp.getlayer(Ether).src |
| 232 | |
Chetan Gaonker | f148386 | 2016-05-06 14:14:31 -0700 | [diff] [blame] | 233 | for x in resp.lastlayer().options: |
| 234 | if(x == 'end'): |
| 235 | break |
| 236 | op,val = x |
| 237 | if(op == "message-type"): |
ChetanGaonker | 42d7581 | 2016-06-06 16:32:52 -0700 | [diff] [blame] | 238 | |
Chetan Gaonker | f148386 | 2016-05-06 14:14:31 -0700 | [diff] [blame] | 239 | if(val == 5): |
Chetan Gaonker | f148386 | 2016-05-06 14:14:31 -0700 | [diff] [blame] | 240 | try: |
| 241 | srcIP = resp.yiaddr |
| 242 | serverIP = resp.siaddr |
ChetanGaonker | 5860c18 | 2016-07-05 16:33:06 -0700 | [diff] [blame] | 243 | self.mac_map[mac] = (srcIP, serverIP) |
| 244 | self.mac_inverse_map[srcIP] = (mac, serverIP) |
Chetan Gaonker | f148386 | 2016-05-06 14:14:31 -0700 | [diff] [blame] | 245 | except AttributeError: |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 246 | log_test.info("In Attribute error.") |
| 247 | log_test.info("Failed to acquire IP via DHCP for %s on interface %s" %(mac, self.iface)) |
Chetan Gaonker | f148386 | 2016-05-06 14:14:31 -0700 | [diff] [blame] | 248 | return (None, None) |
ChetanGaonker | 42d7581 | 2016-06-06 16:32:52 -0700 | [diff] [blame] | 249 | |
ChetanGaonker | 5860c18 | 2016-07-05 16:33:06 -0700 | [diff] [blame] | 250 | if lease_time or renew_time or rebind_time or self.specific_lease: |
Chetan Gaonker | c11d322 | 2016-05-11 17:39:36 -0700 | [diff] [blame] | 251 | for x in resp.lastlayer().options: |
| 252 | if(x == 'end'): |
| 253 | break |
| 254 | op,val = x |
ChetanGaonker | 42d7581 | 2016-06-06 16:32:52 -0700 | [diff] [blame] | 255 | |
Chetan Gaonker | c11d322 | 2016-05-11 17:39:36 -0700 | [diff] [blame] | 256 | if op == "lease_time": |
ChetanGaonker | 5860c18 | 2016-07-05 16:33:06 -0700 | [diff] [blame] | 257 | |
| 258 | if self.specific_lease: |
| 259 | return (srcIP, serverIP, val) |
Chetan Gaonker | c11d322 | 2016-05-11 17:39:36 -0700 | [diff] [blame] | 260 | if lease_time == True: |
| 261 | self.mac_map[mac] = (srcIP, serverIP) |
| 262 | self.mac_inverse_map[srcIP] = (mac, serverIP) |
| 263 | return (srcIP, serverIP, val) |
| 264 | elif op == "renewal_time": |
| 265 | if renew_time == True: |
| 266 | self.mac_map[mac] = (srcIP, serverIP) |
| 267 | self.mac_inverse_map[srcIP] = (mac, serverIP) |
| 268 | return (srcIP, serverIP, val) |
| 269 | elif op == "rebinding_time": |
| 270 | if rebind_time == True: |
| 271 | self.mac_map[mac] = (srcIP, serverIP) |
| 272 | self.mac_inverse_map[srcIP] = (mac, serverIP) |
| 273 | return (srcIP, serverIP, val) |
| 274 | else: |
| 275 | self.mac_map[mac] = (srcIP, serverIP) |
| 276 | self.mac_inverse_map[srcIP] = (mac, serverIP) |
| 277 | return (srcIP, serverIP) |
Chetan Gaonker | f148386 | 2016-05-06 14:14:31 -0700 | [diff] [blame] | 278 | elif(val == 6): |
ChetanGaonker | 42d7581 | 2016-06-06 16:32:52 -0700 | [diff] [blame] | 279 | |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 280 | log_test.info("Got DHCP NAK.") |
Chetan Gaonker | f148386 | 2016-05-06 14:14:31 -0700 | [diff] [blame] | 281 | return (None, None) |
ChetanGaonker | 42d7581 | 2016-06-06 16:32:52 -0700 | [diff] [blame] | 282 | |
| 283 | |
Chetan Gaonker | f72ca40 | 2016-05-02 16:29:32 -0700 | [diff] [blame] | 284 | |
Chetan Gaonker | 1f7c3f8 | 2016-03-08 12:17:37 -0800 | [diff] [blame] | 285 | def discover_next(self): |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 286 | '''Send next dhcp discover/request with updated mac''' |
Chetan Gaonker | 1f7c3f8 | 2016-03-08 12:17:37 -0800 | [diff] [blame] | 287 | return self.discover(update_seed = True) |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 288 | |
Chetan Gaonker | 1f7c3f8 | 2016-03-08 12:17:37 -0800 | [diff] [blame] | 289 | def release(self, ip): |
| 290 | '''Send a DHCP discover/offer''' |
| 291 | if ip is None: |
| 292 | return False |
| 293 | if not self.mac_inverse_map.has_key(ip): |
| 294 | return False |
| 295 | mac, server_ip = self.mac_inverse_map[ip] |
| 296 | chmac = self.macToChaddr(mac) |
| 297 | L2 = Ether(dst="ff:ff:ff:ff:ff:ff", src=mac) |
| 298 | L3 = IP(src="0.0.0.0", dst="255.255.255.255") |
| 299 | L4 = UDP(sport=68, dport=67) |
| 300 | L5 = BOOTP(chaddr=chmac, ciaddr = ip) |
| 301 | L6 = DHCP(options=[("message-type","release"), ("server_id", server_ip), "end"]) |
Anil Kumar Sanka | cfa7c58 | 2016-12-09 23:17:22 +0000 | [diff] [blame] | 302 | sendp(L2/L3/L4/L5/L6, iface = self.iface, count=2) |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 303 | #log_test.info('release response is %s'%resp) |
Chetan Gaonker | 1f7c3f8 | 2016-03-08 12:17:37 -0800 | [diff] [blame] | 304 | del self.mac_map[mac] |
| 305 | del self.mac_inverse_map[ip] |
| 306 | return True |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 307 | |
| 308 | def macToChaddr(self, mac): |
| 309 | rv = [] |
| 310 | mac = mac.split(":") |
| 311 | for x in mac: |
| 312 | rv.append(chr(int(x, 16))) |
| 313 | return reduce(lambda x,y: x + y, rv) |
| 314 | |
| 315 | def get_ip(self, mac): |
| 316 | if self.mac_map.has_key(mac): |
| 317 | return self.mac_map[mac] |
| 318 | return (None, None) |
| 319 | |
| 320 | def get_mac(self, ip): |
| 321 | if self.mac_inverse_map.has_key(ip): |
| 322 | return self.mac_inverse_map[ip] |
| 323 | return (None, None) |
| 324 | |
| 325 | def ipToMac(self, ip): |
| 326 | '''Generate a mac from a ip''' |
| 327 | |
| 328 | mcast = self.is_mcast(ip) |
| 329 | mac = "01:00:5e" if mcast == True else "00:00:00" |
| 330 | octets = ip.split(".") |
| 331 | for x in range(1,4): |
| 332 | num = str(hex(int(octets[x]))) |
| 333 | num = num.split("x")[1] |
| 334 | if len(num) < 2: |
| 335 | num = "0" + str(num) |
| 336 | mac += ":" + num |
| 337 | return mac |
| 338 | |
| 339 | def incIP(self, ip, n=1): |
| 340 | '''Increment an IP''' |
| 341 | |
Chetan Gaonker | f148386 | 2016-05-06 14:14:31 -0700 | [diff] [blame] | 342 | if n < 1: |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 343 | return ip |
| 344 | o = ip.split(".") |
| 345 | for ii in range(3,-1,-1): |
| 346 | if int(o[ii]) < 255: |
| 347 | o[ii] = str(int(o[ii]) + 1) |
| 348 | break |
Chetan Gaonker | f148386 | 2016-05-06 14:14:31 -0700 | [diff] [blame] | 349 | else: |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 350 | o[ii] = str(0) |
| 351 | |
| 352 | n -= 1 |
| 353 | return self.incIP(".".join(o), n) |