Chetan Gaonker | cb122cc | 2016-05-10 10:58:34 -0700 | [diff] [blame^] | 1 | #!/usr/bin/env python |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 2 | # |
| 3 | # Copyright 2016-present Ciena Corporation |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 17 | from scapy.all import * |
| 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 | |
| 24 | def __init__(self, seed_ip = '192.168.1.1', iface = 'veth0'): |
| 25 | self.seed_ip = seed_ip |
| 26 | self.seed_mac = self.ipToMac(self.seed_ip) |
| 27 | self.iface = iface |
| 28 | self.mac_map = {} |
| 29 | self.mac_inverse_map = {} |
Chetan Gaonker | f72ca40 | 2016-05-02 16:29:32 -0700 | [diff] [blame] | 30 | self.bootpmac = None |
| 31 | self.dhcpresp = None |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 32 | |
| 33 | def is_mcast(self, ip): |
| 34 | mcast_octet = (atol(ip) >> 24) & 0xff |
| 35 | return True if mcast_octet >= 224 and mcast_octet <= 239 else False |
| 36 | |
Chetan Gaonker | 1f7c3f8 | 2016-03-08 12:17:37 -0800 | [diff] [blame] | 37 | def discover(self, mac = None, update_seed = False): |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 38 | '''Send a DHCP discover/offer''' |
| 39 | |
| 40 | if mac is None: |
| 41 | mac = self.seed_mac |
| 42 | if update_seed: |
| 43 | self.seed_ip = self.incIP(self.seed_ip) |
| 44 | self.seed_mac = self.ipToMac(self.seed_ip) |
| 45 | mac = self.seed_mac |
Chetan Gaonker | f148386 | 2016-05-06 14:14:31 -0700 | [diff] [blame] | 46 | |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 47 | chmac = self.macToChaddr(mac) |
Chetan Gaonker | f72ca40 | 2016-05-02 16:29:32 -0700 | [diff] [blame] | 48 | self.bootpmac = chmac |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 49 | L2 = Ether(dst="ff:ff:ff:ff:ff:ff", src=mac) |
| 50 | L3 = IP(src="0.0.0.0", dst="255.255.255.255") |
| 51 | L4 = UDP(sport=68, dport=67) |
| 52 | L5 = BOOTP(chaddr=chmac) |
| 53 | L6 = DHCP(options=[("message-type","discover"),"end"]) |
Chetan Gaonker | 49ef085 | 2016-03-23 15:06:18 -0700 | [diff] [blame] | 54 | resp = srp1(L2/L3/L4/L5/L6, filter="udp and port 68", timeout=10, iface=self.iface) |
Chetan Gaonker | f72ca40 | 2016-05-02 16:29:32 -0700 | [diff] [blame] | 55 | self.dhcpresp = resp |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 56 | try: |
| 57 | srcIP = resp.yiaddr |
| 58 | serverIP = resp.siaddr |
| 59 | except AttributeError: |
| 60 | print("Failed to acquire IP via DHCP for %s on interface %s" %(mac, self.iface)) |
| 61 | return (None, None) |
| 62 | |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 63 | subnet_mask = "0.0.0.0" |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 64 | for x in resp.lastlayer().options: |
| 65 | if(x == 'end'): |
| 66 | break |
| 67 | op,val = x |
| 68 | if(op == "subnet_mask"): |
| 69 | subnet_mask = val |
| 70 | elif(op == 'server_id'): |
| 71 | server_id = val |
Chetan Gaonker | f148386 | 2016-05-06 14:14:31 -0700 | [diff] [blame] | 72 | |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 73 | L5 = BOOTP(chaddr=chmac, yiaddr=srcIP) |
Chetan Gaonker | f148386 | 2016-05-06 14:14:31 -0700 | [diff] [blame] | 74 | L6 = DHCP(options=[("message-type","request"), ("server_id",server_id), |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 75 | ("subnet_mask",subnet_mask), ("requested_addr",srcIP), "end"]) |
Chetan Gaonker | 49ef085 | 2016-03-23 15:06:18 -0700 | [diff] [blame] | 76 | srp(L2/L3/L4/L5/L6, filter="udp and port 68", timeout=10, iface=self.iface) |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 77 | self.mac_map[mac] = (srcIP, serverIP) |
| 78 | self.mac_inverse_map[srcIP] = (mac, serverIP) |
| 79 | return (srcIP, serverIP) |
| 80 | |
Chetan Gaonker | f148386 | 2016-05-06 14:14:31 -0700 | [diff] [blame] | 81 | def only_discover(self, mac = None, desired = False, lease_time = False): |
Chetan Gaonker | f72ca40 | 2016-05-02 16:29:32 -0700 | [diff] [blame] | 82 | '''Send a DHCP discover''' |
| 83 | |
| 84 | if mac is None: |
| 85 | mac = self.seed_mac |
| 86 | |
| 87 | chmac = self.macToChaddr(mac) |
Chetan Gaonker | f148386 | 2016-05-06 14:14:31 -0700 | [diff] [blame] | 88 | self.bootpmac = chmac |
Chetan Gaonker | f72ca40 | 2016-05-02 16:29:32 -0700 | [diff] [blame] | 89 | L2 = Ether(dst="ff:ff:ff:ff:ff:ff", src=mac) |
| 90 | L3 = IP(src="0.0.0.0", dst="255.255.255.255") |
| 91 | L4 = UDP(sport=68, dport=67) |
Chetan Gaonker | f148386 | 2016-05-06 14:14:31 -0700 | [diff] [blame] | 92 | L5 = BOOTP(chaddr=chmac) |
Chetan Gaonker | f72ca40 | 2016-05-02 16:29:32 -0700 | [diff] [blame] | 93 | if desired: |
| 94 | L6 = DHCP(options=[("message-type","discover"),("requested_addr",self.seed_ip),"end"]) |
Chetan Gaonker | f148386 | 2016-05-06 14:14:31 -0700 | [diff] [blame] | 95 | |
| 96 | elif time: |
| 97 | L6 = DHCP(options=[("message-type","discover"),("lease_time",700),"end"]) |
| 98 | |
| 99 | elif lease_time: |
Chetan Gaonker | f72ca40 | 2016-05-02 16:29:32 -0700 | [diff] [blame] | 100 | L6 = DHCP(options=[("message-type","discover"),"end"]) |
| 101 | |
Chetan Gaonker | f72ca40 | 2016-05-02 16:29:32 -0700 | [diff] [blame] | 102 | |
Chetan Gaonker | f148386 | 2016-05-06 14:14:31 -0700 | [diff] [blame] | 103 | |
| 104 | resp = srp1(L2/L3/L4/L5/L6, filter="udp and port 68", timeout=10, iface=self.iface) |
| 105 | if resp == None: |
| 106 | return (None, None, None) |
| 107 | |
| 108 | self.dhcpresp = resp |
| 109 | for x in resp.lastlayer().options: |
| 110 | if(x == 'end'): |
| 111 | break |
| 112 | op,val = x |
| 113 | if(op == "message-type"): |
| 114 | |
| 115 | if(val == 2): |
| 116 | |
| 117 | try: |
| 118 | srcIP = resp.yiaddr |
| 119 | serverIP = resp.siaddr |
| 120 | except AttributeError: |
| 121 | print "In Attribute error." |
| 122 | print("Failed to acquire IP via DHCP for %s on interface %s" %(mac, self.iface)) |
| 123 | return (None, None, None) |
| 124 | |
| 125 | if lease_time == True: |
| 126 | for x in resp.lastlayer().options: |
| 127 | if(x == 'end'): |
| 128 | break |
| 129 | op,val = x |
| 130 | if(op == "lease_time"): |
| 131 | return (srcIP, serverIP, mac, val) |
| 132 | else: |
| 133 | return (srcIP, serverIP, mac) |
| 134 | |
| 135 | elif(val == 6): |
| 136 | |
| 137 | return (None, None, mac) |
Chetan Gaonker | f72ca40 | 2016-05-02 16:29:32 -0700 | [diff] [blame] | 138 | |
| 139 | |
| 140 | def only_request(self, cip, mac): |
| 141 | '''Send a DHCP offer''' |
| 142 | |
Chetan Gaonker | f72ca40 | 2016-05-02 16:29:32 -0700 | [diff] [blame] | 143 | subnet_mask = "0.0.0.0" |
| 144 | for x in self.dhcpresp.lastlayer().options: |
Chetan Gaonker | f148386 | 2016-05-06 14:14:31 -0700 | [diff] [blame] | 145 | if(x == 'end'): |
| 146 | break |
| 147 | op,val = x |
| 148 | if(op == "subnet_mask"): |
| 149 | subnet_mask = val |
| 150 | elif(op == 'server_id'): |
| 151 | server_id = val |
Chetan Gaonker | f72ca40 | 2016-05-02 16:29:32 -0700 | [diff] [blame] | 152 | |
| 153 | L2 = Ether(dst="ff:ff:ff:ff:ff:ff", src=mac) |
| 154 | L3 = IP(src="0.0.0.0", dst="255.255.255.255") |
| 155 | L4 = UDP(sport=68, dport=67) |
| 156 | L5 = BOOTP(chaddr=self.bootpmac, yiaddr=cip) |
| 157 | L6 = DHCP(options=[("message-type","request"), ("server_id",server_id), |
| 158 | ("subnet_mask",subnet_mask), ("requested_addr",cip), "end"]) |
| 159 | resp=srp1(L2/L3/L4/L5/L6, filter="udp and port 68", timeout=10, iface=self.iface) |
Chetan Gaonker | f148386 | 2016-05-06 14:14:31 -0700 | [diff] [blame] | 160 | if resp == None: |
| 161 | return (None, None) |
| 162 | |
| 163 | for x in resp.lastlayer().options: |
| 164 | if(x == 'end'): |
| 165 | break |
| 166 | op,val = x |
| 167 | if(op == "message-type"): |
| 168 | |
| 169 | if(val == 5): |
| 170 | |
| 171 | try: |
| 172 | srcIP = resp.yiaddr |
| 173 | serverIP = resp.siaddr |
| 174 | except AttributeError: |
| 175 | print "In Attribute error." |
| 176 | print("Failed to acquire IP via DHCP for %s on interface %s" %(mac, self.iface)) |
| 177 | return (None, None) |
| 178 | self.mac_map[mac] = (srcIP, serverIP) |
| 179 | self.mac_inverse_map[srcIP] = (mac, serverIP) |
Chetan Gaonker | f72ca40 | 2016-05-02 16:29:32 -0700 | [diff] [blame] | 180 | |
Chetan Gaonker | f148386 | 2016-05-06 14:14:31 -0700 | [diff] [blame] | 181 | return (srcIP, serverIP) |
| 182 | |
| 183 | elif(val == 6): |
| 184 | |
| 185 | return (None, None) |
| 186 | |
| 187 | |
Chetan Gaonker | f72ca40 | 2016-05-02 16:29:32 -0700 | [diff] [blame] | 188 | |
Chetan Gaonker | 1f7c3f8 | 2016-03-08 12:17:37 -0800 | [diff] [blame] | 189 | def discover_next(self): |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 190 | '''Send next dhcp discover/request with updated mac''' |
Chetan Gaonker | 1f7c3f8 | 2016-03-08 12:17:37 -0800 | [diff] [blame] | 191 | return self.discover(update_seed = True) |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 192 | |
Chetan Gaonker | 1f7c3f8 | 2016-03-08 12:17:37 -0800 | [diff] [blame] | 193 | def release(self, ip): |
| 194 | '''Send a DHCP discover/offer''' |
| 195 | if ip is None: |
| 196 | return False |
| 197 | if not self.mac_inverse_map.has_key(ip): |
| 198 | return False |
| 199 | mac, server_ip = self.mac_inverse_map[ip] |
| 200 | chmac = self.macToChaddr(mac) |
| 201 | L2 = Ether(dst="ff:ff:ff:ff:ff:ff", src=mac) |
| 202 | L3 = IP(src="0.0.0.0", dst="255.255.255.255") |
| 203 | L4 = UDP(sport=68, dport=67) |
| 204 | L5 = BOOTP(chaddr=chmac, ciaddr = ip) |
| 205 | L6 = DHCP(options=[("message-type","release"), ("server_id", server_ip), "end"]) |
| 206 | sendp(L2/L3/L4/L5/L6, iface = self.iface) |
| 207 | del self.mac_map[mac] |
| 208 | del self.mac_inverse_map[ip] |
| 209 | return True |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 210 | |
| 211 | def macToChaddr(self, mac): |
| 212 | rv = [] |
| 213 | mac = mac.split(":") |
| 214 | for x in mac: |
| 215 | rv.append(chr(int(x, 16))) |
| 216 | return reduce(lambda x,y: x + y, rv) |
| 217 | |
| 218 | def get_ip(self, mac): |
| 219 | if self.mac_map.has_key(mac): |
| 220 | return self.mac_map[mac] |
| 221 | return (None, None) |
| 222 | |
| 223 | def get_mac(self, ip): |
| 224 | if self.mac_inverse_map.has_key(ip): |
| 225 | return self.mac_inverse_map[ip] |
| 226 | return (None, None) |
| 227 | |
| 228 | def ipToMac(self, ip): |
| 229 | '''Generate a mac from a ip''' |
| 230 | |
| 231 | mcast = self.is_mcast(ip) |
| 232 | mac = "01:00:5e" if mcast == True else "00:00:00" |
| 233 | octets = ip.split(".") |
| 234 | for x in range(1,4): |
| 235 | num = str(hex(int(octets[x]))) |
| 236 | num = num.split("x")[1] |
| 237 | if len(num) < 2: |
| 238 | num = "0" + str(num) |
| 239 | mac += ":" + num |
| 240 | return mac |
| 241 | |
| 242 | def incIP(self, ip, n=1): |
| 243 | '''Increment an IP''' |
| 244 | |
Chetan Gaonker | f148386 | 2016-05-06 14:14:31 -0700 | [diff] [blame] | 245 | if n < 1: |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 246 | return ip |
| 247 | o = ip.split(".") |
| 248 | for ii in range(3,-1,-1): |
| 249 | if int(o[ii]) < 255: |
| 250 | o[ii] = str(int(o[ii]) + 1) |
| 251 | break |
Chetan Gaonker | f148386 | 2016-05-06 14:14:31 -0700 | [diff] [blame] | 252 | else: |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 253 | o[ii] = str(0) |
| 254 | |
| 255 | n -= 1 |
| 256 | return self.incIP(".".join(o), n) |