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