2 # Copyright (c) 2016 Cisco and/or its affiliates.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 """Traffic script that sends DHCP packets."""
19 from scapy.all import Ether
20 from scapy.layers.inet import IP, UDP
21 from scapy.layers.inet import UDP_SERVICES
22 from scapy.layers.dhcp import DHCP, BOOTP
24 from resources.libraries.python.PacketVerifier import RxQueue, TxQueue
25 from resources.libraries.python.TrafficScriptArg import TrafficScriptArg
28 def dhcp_discover(args):
29 """Send DHCP DISCOVER packet."""
31 tx_if = args.get_arg('tx_if')
32 rx_if = args.get_arg('rx_if')
38 tx_dst_ip = "255.255.255.255"
40 server_ip = args.get_arg('server_ip')
41 proxy_ip = args.get_arg('proxy_ip')
42 client_mac = args.get_arg('client_mac')
46 dhcp_discover = Ether(src=client_mac, dst="ff:ff:ff:ff:ff:ff") / \
47 IP(src=tx_src_ip, dst=tx_dst_ip) / \
48 UDP(sport=68, dport=67) / \
50 DHCP(options=[("message-type", "discover"),
53 sent_packets.append(dhcp_discover)
54 txq.send(dhcp_discover)
59 raise RuntimeError('DHCP DISCOVER timeout')
61 if ether[IP].src != proxy_ip:
62 raise RuntimeError("Source IP address error.")
63 print "Source IP address: OK."
65 if ether[IP].dst != server_ip:
66 raise RuntimeError("Destination IP address error.")
67 print "Destination IP address: OK."
69 if ether[UDP].dport != UDP_SERVICES.bootps:
70 raise RuntimeError("UDP destination port error.")
71 print "UDP destination port: OK."
73 if ether[UDP].sport != UDP_SERVICES.bootpc:
74 raise RuntimeError("UDP source port error.")
75 print "UDP source port: OK."
77 if ether[DHCP].options[1][0] != 'relay_agent_Information': # option 82
78 raise RuntimeError("Relay agent information error.")
79 option_82 = ether[DHCP].options[1][1]
81 if ether[DHCP].options[0][1] != 1: # 1 - DISCOVER message
82 raise RuntimeError("DHCP DISCOVER message error.")
83 print "DHCP DISCOVER message OK."
84 dhcp_offer(args, option_82)
87 def dhcp_offer(args, option_82):
88 """Send DHCP OFFER packet."""
90 rx_if = args.get_arg('tx_if')
91 tx_if = args.get_arg('rx_if')
96 tx_dst_ip = "255.255.255.255"
97 server_ip = args.get_arg('server_ip')
98 server_mac = args.get_arg('server_mac')
99 client_ip = args.get_arg('client_ip')
100 proxy_ip = args.get_arg('proxy_ip')
104 dhcp_offer = Ether(src=server_mac, dst="ff:ff:ff:ff:ff:ff") / \
105 IP(src=server_ip, dst=tx_dst_ip) / \
106 UDP(sport=67, dport=68) / \
109 siaddr=server_ip) / \
111 [("message-type", "offer"),
112 ("server_id", server_ip),
113 ("relay_agent_Information", option_82),
117 sent_packets.append(dhcp_offer)
122 raise RuntimeError('DHCP OFFER timeout')
124 if ether[IP].dst != tx_dst_ip:
125 raise RuntimeError("Destination IP address error.")
126 print "Destination IP address: OK."
128 if ether[IP].src != proxy_ip:
129 raise RuntimeError("Source IP address error.")
130 print "Source IP address: OK."
132 if ether[UDP].dport != UDP_SERVICES.bootpc:
133 raise RuntimeError("UDP destination port error.")
134 print "UDP destination port: OK."
136 if ether[UDP].sport != UDP_SERVICES.bootps:
137 raise RuntimeError("UDP source port error.")
138 print "UDP source port: OK."
140 if ether[BOOTP].yiaddr != client_ip:
141 raise RuntimeError("Client IP address error.")
142 print "Client IP address: OK."
144 if ether[BOOTP].siaddr != server_ip:
145 raise RuntimeError("DHCP server IP address error.")
146 print "DHCP server IP address: OK."
148 if ether[DHCP].options[0][1] != 2: # 2 - OFFER message
149 raise RuntimeError("DHCP OFFER message error.")
150 print "DHCP OFFER message OK."
154 def dhcp_request(args):
155 """Send DHCP REQUEST packet."""
157 tx_if = args.get_arg('tx_if')
158 rx_if = args.get_arg('rx_if')
163 tx_dst_ip = "255.255.255.255"
164 server_ip = args.get_arg('server_ip')
165 client_ip = args.get_arg('client_ip')
166 client_mac = args.get_arg('client_mac')
167 proxy_ip = args.get_arg('proxy_ip')
171 dhcp_request = Ether(src=client_mac, dst="ff:ff:ff:ff:ff:ff") / \
172 IP(src="0.0.0.0", dst=tx_dst_ip) / \
173 UDP(sport=68, dport=67) / \
176 siaddr=server_ip) / \
177 DHCP(options=[("message-type", "request"),
178 ("server_id", server_ip),
179 ("requested_addr", client_ip),
182 sent_packets.append(dhcp_request)
183 txq.send(dhcp_request)
188 raise RuntimeError('DHCP REQUEST timeout')
190 if ether[IP].dst != server_ip:
191 raise RuntimeError("Destination IP address error.")
192 print "Destination IP address: OK."
194 if ether[IP].src != proxy_ip:
195 raise RuntimeError("Source IP address error.")
196 print "Source IP address: OK."
198 if ether[UDP].dport != UDP_SERVICES.bootps:
199 raise RuntimeError("UDP destination port error.")
200 print "UDP destination port: OK."
202 if ether[UDP].sport != UDP_SERVICES.bootpc:
203 raise RuntimeError("UDP source port error.")
204 print "UDP source port: OK."
206 if ether[BOOTP].siaddr != server_ip:
207 raise RuntimeError("DHCP server IP address error.")
208 print "DHCP server IP address: OK."
210 if ether[DHCP].options[2][1] != client_ip:
211 raise RuntimeError("Requested IP address error.")
212 print "Requested IP address: OK."
214 if ether[DHCP].options[3][0] != 'relay_agent_Information': # option 82
215 raise RuntimeError("Relay agent information error.")
216 option_82 = ether[DHCP].options[3][1]
218 if ether[DHCP].options[0][1] != 3: # 2 - REQUEST message
219 raise RuntimeError("DHCP REQUEST message error.")
220 print "DHCP REQUEST message: OK."
221 dhcp_ack(args, option_82)
224 def dhcp_ack(args, option_82):
225 """Send DHCP ACK packet."""
227 rx_if = args.get_arg('tx_if')
228 tx_if = args.get_arg('rx_if')
233 tx_dst_ip = "255.255.255.255"
234 server_ip = args.get_arg('server_ip')
235 server_mac = args.get_arg('server_mac')
236 client_ip = args.get_arg('client_ip')
237 proxy_ip = args.get_arg('proxy_ip')
238 lease_time = 43200 # 12 hours
242 dhcp_ack = Ether(src=server_mac, dst="ff:ff:ff:ff:ff:ff") / \
243 IP(src=server_ip, dst=tx_dst_ip) / \
244 UDP(sport=67, dport=68) / \
247 siaddr=server_ip) / \
249 [("message-type", "ack"),
250 ("server_id", server_ip),
251 ("lease_time", lease_time),
252 ("relay_agent_Information", option_82),
256 sent_packets.append(dhcp_ack)
261 raise RuntimeError('DHCP ACK timeout')
263 if ether[IP].dst != tx_dst_ip:
264 raise RuntimeError("Destination IP address error.")
265 print "Destination IP address: OK."
267 if ether[IP].src != proxy_ip:
268 raise RuntimeError("Source IP address error.")
269 print "Source IP address: OK."
271 if ether[UDP].dport != UDP_SERVICES.bootpc:
272 raise RuntimeError("UDP destination port error.")
273 print "UDP destination port: OK."
275 if ether[UDP].sport != UDP_SERVICES.bootps:
276 raise RuntimeError("UDP source port error.")
277 print "UDP source port: OK."
279 if ether[BOOTP].yiaddr != client_ip:
280 raise RuntimeError("Client IP address error.")
281 print "Client IP address: OK."
283 if ether[BOOTP].siaddr != server_ip:
284 raise RuntimeError("DHCP server IP address error.")
285 print "DHCP server IP address: OK."
287 if ether[DHCP].options[2][1] != lease_time:
288 raise RuntimeError("DHCP lease time error.")
289 print "DHCP lease time OK."
291 if ether[DHCP].options[0][1] != 5: # 5 - ACK message
292 raise RuntimeError("DHCP ACK message error.")
293 print "DHCP ACK message OK."
297 """Send DHCP messages."""
299 args = TrafficScriptArg(['server_ip', 'server_mac', 'client_ip',
300 'client_mac', 'proxy_ip'])
306 if __name__ == "__main__":