Forum Discussion
Extracting info for DHCP option
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
}
}
}
- cjun
Nimbostratus
Yes, that works, in fact, this is a POC for one big customer, it works perfect during the POC - Colin_Walker_12Historic F5 AccountVery cool example, I lifted most of it to show off in this week's 20 Lines or Less...good work!
- Nicola_DT
Nimbostratus
Hi heroes,
- Nicola_DT
Nimbostratus
Have found this tip myself by Hoolio: - hoolio
Cirrostratus
Hi Nicola, - Nicola_DT
Nimbostratus
Yup, I saw it in your other post and managed to fix accordingly. - brad_11480
Nimbostratus
With version 11.5.4 and 12 produces warnings about curly braces:
There were warnings: /Common/rule_dhcp_mac_sticky_example:16: warning: [use curly braces to avoid double substitution][$i] /Common/rule_dhcp_mac_sticky_example:22: warning: [use curly braces to avoid double substitution][$i] /Common/rule_dhcp_mac_sticky_example:23: warning: [use curly braces to avoid double substitution][0x$length_hex] /Common/rule_dhcp_mac_sticky_example:31: warning: [use curly braces to avoid double substitution][$i] /Common/rule_dhcp_mac_sticky_example:32: warning: [use curly braces to avoid double substitution][0x$length_hex] /Common/rule_dhcp_mac_sticky_example:38: warning: [use curly braces to avoid double substitution][$i]
Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com