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