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