cjun
Jul 05, 2008Nimbostratus
Extracting info for DHCP option
The iRule here is to find out value for DHCP option 82 for further processing. It can be used
for extracting info for other kind of packet as long as packet format is known.
Few important point here:
1. Use H option to scan the payload
binary scan [UDP::payload] x240H* dhcp_option_payload
This command will scan the payload in HEX string format
2. Use a2 option to scan individual options
binary scan $dhcp_option_payload x[expr $i]a2 option
Since every dhcp option has option type, which has a byte, so in HEX string, it will represent
as two character, for example, 52, which is option 82.
3. Convert the length from HEX string to Decimal
Every dhcp option has different length, the length ocupy a byte as well, so it will represent as
two charater in HEX string as well, for example, 0f. We needto convert that to decimal so that
we are able to scan next option, until option 82 found.
when CLIENT_DATA {
if { [UDP::payload length] < 200 } {
log local0. "drop due to length"
drop
return
} else {
extract out all dhcp options in Hex string
binary scan [UDP::payload] x240H* dhcp_option_payload
extract out circuit_id
set option 0
set option_length [expr {([UDP::payload length] -240) * 2 }]
for {set i 0} {$option != 52 && $i < $option_length} {incr i [expr { $length * 2 +2 }]} {
extract option value
binary scan $dhcp_option_payload x[expr $i]a2 option
move index to get length field
incr i 2
extract length value and convert length from Hex string to decimal
binary scan $dhcp_option_payload x[expr $i]a2 length_hex
set length [expr 0x$length_hex]
}
if { $i < $option_length } {
move index to suboption's length field of option 82
incr i -[expr { $length * 2 -2 }]
extract the length for suboption, and convert the length from Hex string to decimal
binary scan $dhcp_option_payload x[expr $i]a2 length_hex
set length [expr 0x$length_hex]
move index to suboption's value field
incr i 2
extract the suboption's value
binary scan $dhcp_option_payload x[expr $i]a[expr { $length * 2 }] circuit_id
} else {
Circuit_id not found
log local0. "Agent < [IP::remote_addr]> did not contain DHCP option 82"
drop
return
}
}
}