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 receives an DHCP packet on given interface and check if
16 is correct DHCP DISCOVER message.
21 from scapy.layers.inet import UDP_SERVICES
23 from resources.libraries.python.PacketVerifier import RxQueue
24 from resources.libraries.python.TrafficScriptArg import TrafficScriptArg
28 args = TrafficScriptArg(['rx_src_mac'], ['hostname'])
30 rx_if = args.get_arg('rx_if')
31 rx_src_mac = args.get_arg('rx_src_mac')
32 hostname = args.get_arg('hostname')
34 rx_dst_mac = 'ff:ff:ff:ff:ff:ff'
36 rx_dst_ip = '255.255.255.255'
38 dhcp_magic = 'c\x82Sc'
45 raise RuntimeError("DHCP DISCOVER Rx timeout.")
47 if ether.dst != rx_dst_mac:
48 raise RuntimeError("Destination MAC address error.")
49 print "Destination MAC address: OK."
51 if ether.src != rx_src_mac:
52 raise RuntimeError("Source MAC address error.")
53 print "Source MAC address: OK."
55 if ether['IP'].dst != rx_dst_ip:
56 raise RuntimeError("Destination IP address error.")
57 print "Destination IP address: OK."
59 if ether['IP'].src != rx_src_ip:
60 raise RuntimeError("Source IP address error.")
61 print "Source IP address: OK."
63 if ether['IP']['UDP'].dport != UDP_SERVICES.bootps:
64 raise RuntimeError("UDP destination port error.")
65 print "UDP destination port: OK."
67 if ether['IP']['UDP'].sport != UDP_SERVICES.bootpc:
68 raise RuntimeError("UDP source port error.")
69 print "UDP source port: OK."
71 bootp = ether['BOOTP']
73 if bootp.op != boot_request:
74 raise RuntimeError("BOOTP message type error.")
75 print "BOOTP message type: OK"
77 if bootp.ciaddr != '0.0.0.0':
78 raise RuntimeError("BOOTP client IP address error.")
79 print "BOOTP client IP address: OK"
81 if bootp.yiaddr != '0.0.0.0':
82 raise RuntimeError("BOOTP 'your' (client) IP address error.")
83 print "BOOTP 'your' (client) IP address: OK"
85 if bootp.siaddr != '0.0.0.0':
86 raise RuntimeError("BOOTP next server IP address error.")
87 print "BOOTP next server IP address: OK"
89 if bootp.giaddr != '0.0.0.0':
90 raise RuntimeError("BOOTP relay agent IP address error.")
91 print "BOOTP relay agent IP address: OK"
93 chaddr = bootp.chaddr[:bootp.hlen].encode('hex')
94 if chaddr != rx_src_mac.replace(':', ''):
95 raise RuntimeError("BOOTP client hardware address error.")
96 print "BOOTP client hardware address: OK"
99 if bootp.sname != 64*'\x00':
100 raise RuntimeError("BOOTP server name error.")
101 print "BOOTP server name: OK"
104 if bootp.file != 128*'\x00':
105 raise RuntimeError("BOOTP boot file name error.")
106 print "BOOTP boot file name: OK"
109 if bootp.options != dhcp_magic:
110 raise RuntimeError("DHCP magic error.")
111 print "DHCP magic: OK"
114 dhcp_options = ether['DHCP options'].options
117 hn = filter(lambda x: x[0] == 'hostname', dhcp_options)
120 if hn[0][1] != hostname:
121 raise RuntimeError("Client's hostname doesn't match.")
123 raise RuntimeError("Option list doesn't contain hostname option.")
126 raise RuntimeError("Option list contains hostname option.")
127 print "Option 12 hostname: OK"
130 mt = filter(lambda x: x[0] == 'message-type', dhcp_options)[0][1]
132 raise RuntimeError("Option 53 message-type error.")
133 print "Option 53 message-type: OK"
136 prl = filter(lambda x: x[0] == 'param_req_list', dhcp_options)[0][1]
137 if prl != '\x01\x1c\x02\x03\x0f\x06w\x0c,/\x1ay*':
138 raise RuntimeError("Option 55 param_req_list error.")
139 print "Option 55 param_req_list: OK"
142 if 'end' not in dhcp_options:
143 raise RuntimeError("end option error.")
144 print "end option: OK"
149 if __name__ == "__main__":