blob: cafa95ee49c40071c2ee7125c557d024ccc6cf5d [file] [log] [blame]
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -08001from scapy.all import *
2
3conf.verb = 0 # Disable Scapy verbosity
4conf.checkIPaddr = 0 # Don't check response packets for matching destination IPs
5
6class DHCPTest:
7
8 def __init__(self, seed_ip = '192.168.1.1', iface = 'veth0'):
9 self.seed_ip = seed_ip
10 self.seed_mac = self.ipToMac(self.seed_ip)
11 self.iface = iface
12 self.mac_map = {}
13 self.mac_inverse_map = {}
14
15 def is_mcast(self, ip):
16 mcast_octet = (atol(ip) >> 24) & 0xff
17 return True if mcast_octet >= 224 and mcast_octet <= 239 else False
18
19 def send(self, mac = None, update_seed = False):
20 '''Send a DHCP discover/offer'''
21
22 if mac is None:
23 mac = self.seed_mac
24 if update_seed:
25 self.seed_ip = self.incIP(self.seed_ip)
26 self.seed_mac = self.ipToMac(self.seed_ip)
27 mac = self.seed_mac
28
29 chmac = self.macToChaddr(mac)
30 L2 = Ether(dst="ff:ff:ff:ff:ff:ff", src=mac)
31 L3 = IP(src="0.0.0.0", dst="255.255.255.255")
32 L4 = UDP(sport=68, dport=67)
33 L5 = BOOTP(chaddr=chmac)
34 L6 = DHCP(options=[("message-type","discover"),"end"])
35 resp = srp1(L2/L3/L4/L5/L6, filter="udp and port 68", timeout=5, iface=self.iface)
36 try:
37 srcIP = resp.yiaddr
38 serverIP = resp.siaddr
39 except AttributeError:
40 print("Failed to acquire IP via DHCP for %s on interface %s" %(mac, self.iface))
41 return (None, None)
42
43 for x in resp.lastlayer().options:
44 if(x == 'end'):
45 break
46 op,val = x
47 if(op == "subnet_mask"):
48 subnet_mask = val
49 elif(op == 'server_id'):
50 server_id = val
51
52 L5 = BOOTP(chaddr=chmac, yiaddr=srcIP)
53 L6 = DHCP(options=[("message-type","request"), ("server_id",server_id),
54 ("subnet_mask",subnet_mask), ("requested_addr",srcIP), "end"])
55 srp1(L2/L3/L4/L5/L6, filter="udp and port 68", timeout=5, iface=self.iface)
56 self.mac_map[mac] = (srcIP, serverIP)
57 self.mac_inverse_map[srcIP] = (mac, serverIP)
58 return (srcIP, serverIP)
59
60 def send_next(self):
61 '''Send next dhcp discover/request with updated mac'''
62
63 return self.send(update_seed = True)
64
65 def macToChaddr(self, mac):
66 rv = []
67 mac = mac.split(":")
68 for x in mac:
69 rv.append(chr(int(x, 16)))
70 return reduce(lambda x,y: x + y, rv)
71
72 def get_ip(self, mac):
73 if self.mac_map.has_key(mac):
74 return self.mac_map[mac]
75 return (None, None)
76
77 def get_mac(self, ip):
78 if self.mac_inverse_map.has_key(ip):
79 return self.mac_inverse_map[ip]
80 return (None, None)
81
82 def ipToMac(self, ip):
83 '''Generate a mac from a ip'''
84
85 mcast = self.is_mcast(ip)
86 mac = "01:00:5e" if mcast == True else "00:00:00"
87 octets = ip.split(".")
88 for x in range(1,4):
89 num = str(hex(int(octets[x])))
90 num = num.split("x")[1]
91 if len(num) < 2:
92 num = "0" + str(num)
93 mac += ":" + num
94 return mac
95
96 def incIP(self, ip, n=1):
97 '''Increment an IP'''
98
99 if n < 1:
100 return ip
101 o = ip.split(".")
102 for ii in range(3,-1,-1):
103 if int(o[ii]) < 255:
104 o[ii] = str(int(o[ii]) + 1)
105 break
106 else:
107 o[ii] = str(0)
108
109 n -= 1
110 return self.incIP(".".join(o), n)