dhcp-eval (5) Linux Manual Page
dhcp-eval – ISC DHCP conditional evaluation
Description
The Internet Systems Consortium DHCP client and server both provide the ability to perform conditional behavior depending on the contents of packets they receive. The syntax for specifying this conditional behaviour is documented here.
Reference: Conditional Behaviour
Conditional behaviour may be specified using the if statement and the else or elsif statements or the switch and case statements. A conditional statement can appear anywhere that a regular statement (e.g., an option statement) can appear, and can enclose one or more such statements.
CONDITIONAL BEHAVIOUR: IF
A typical conditional if statement in a server might be:
if option dhcp-user-class = "accounting" {
max-lease-time 17600;
option domain-name "accounting.example.org";
option domain-name-servers ns1.accounting.example.org,
ns2.accounting.example.org;
} elsif option dhcp-user-class = "sales" {
max-lease-time 17600;
option domain-name "sales.example.org";
option domain-name-servers ns1.sales.example.org,
ns2.sales.example.org;
} elsif option dhcp-user-class = "engineering" {
max-lease-time 17600;
option domain-name "engineering.example.org";
option domain-name-servers ns1.engineering.example.org,
ns2.engineering.example.org;
} else {
max-lease-time 600;
option domain-name "misc.example.org";
option domain-name-servers ns1.misc.example.org,
ns2.misc.example.org;
}
On the client side, an example of conditional evaluation might be:
# example.org filters DNS at its firewall, so we have to use their DNS
# servers when we connect to their network. If we are not at
# example.org, prefer our own DNS server.
if not option domain-name = "example.org" {
prepend domain-name-servers 127.0.0.1;
}
The if statement and the elsif continuation statement both take boolean expressions as their arguments. That is, they take expressions that, when evaluated, produce a boolean result. If the expression evaluates to true, then the statements enclosed in braces following the if statement are executed, and all subsequent elsif and else clauses are skipped. Otherwise, each subsequent elsif clause’s expression is checked, until an elsif clause is encountered whose test evaluates to true. If such a clause is found, the statements in braces following it are executed, and then any subsequent elsif and else clauses are skipped. If all the if and elsif clauses are checked but none of their expressions evaluate true, then if there is an else clause, the statements enclosed in braces following the else are evaluated. Boolean expressions that evaluate to null are treated as false in conditionals.
CONDITIONAL BEHAVIOUR: SWITCH
The above example can be rewritten using a switch construct as well.
switch (option dhcp-user-class) {
case "accounting":
max-lease-time 17600;
option domain-name "accounting.example.org";
option domain-name-servers ns1.accounting.example.org,
ns2.accounting.example.org;
case "sales":
max-lease-time 17600;
option domain-name "sales.example.org";
option domain-name-servers ns1.sales.example.org,
ns2.sales.example.org;
break;
case "engineering":
max-lease-time 17600;
option domain-name "engineering.example.org";
option domain-name-servers ns1.engineering.example.org,
ns2.engineering.example.org;
break;
default:
max-lease-time 600;
option domain-name "misc.example.org";
option domain-name-servers ns1.misc.example.org,
ns2.misc.example.org;
break;
}
The switch statement and the case statements can both be data expressions or numeric expressions. Within a switch statement they all must be the same type. The server evaluates the expression from the switch statement and then it evaluates the expressions from the case statements until it finds a match.
If it finds a match it starts executing statements from that case until the next break statement. If it doesn’t find a match it starts from the default statement and again proceeds to the next break statement. If there is no match and no default it does nothing.
Boolean Expressions
The following is the current list of boolean expressions that are supported by the DHCP distribution.
data-expression-1 = data-expression-2
- The
=operator compares the values of two data expressions, returning true if they are the same, false if they are not. If either the left-hand side or the right-hand side are null, the result is also null.
data-expression-1 ~= data-expression-2 data-expression-1 ~~ data-expression-2
- The
~=and~~operators (not available on all systems) perform extended regex(7) matching of the values of two data expressions, returning true if data-expression-1 matches against the regular expression evaluated by data-expression-2, or false if it does not match or encounters some error. If either the left-hand side or the right-hand side are null or empty strings, the result is also false. The~~operator differs from the~=operator in that it is case-insensitive.
boolean-expression-1 and boolean-expression-2
- The
andoperator evaluates to true if the boolean expression on the left-hand side and the boolean expression on the right-hand side both evaluate to true. Otherwise, it evaluates to false. If either the expression on the left-hand side or the expression on the right-hand side are null, the result is null.
boolean-expression-1 or boolean-expression-2
- The
oroperator evaluates to true if either the boolean expression on the left-hand side or the boolean expression on the right-hand side evaluate to true. Otherwise, it evaluates to false. If either the expression on the left-hand side or the expression on the right-hand side are null, the result is null.
not boolean-expression
- The
notoperator evaluates to true if boolean-expression evaluates to false, and returns false if boolean-expression evaluates to true. If boolean-expression evaluates to null, the result is also null.
exists option-name
- The
existsexpression returns true if the specified option exists in the incoming DHCP packet being processed.
known
- The
knownexpression returns true if the client whose request is currently being processed is known – that is, if there’s a host declaration for it.
static
- The
staticexpression returns true if the lease assigned to the client whose request is currently being processed is derived from a static address assignment.
Data Expressions
Several of the boolean expressions above depend on the results of evaluating data expressions. A list of these expressions is provided here.
substring (data-expr, offset, length)
- The
substringoperator evaluates the data expression and returns the substring of the result of that evaluation that starts offset bytes from the beginning, continuing for length bytes. Offset and length are both numeric expressions. If data-expr, offset or length evaluate to null, then the result is also null. If offset is greater than or equal to the length of the evaluated data, then a zero-length data string is returned. If length is greater then the remaining length of the evaluated data after offset, then a data string containing all data from offset to the end of the evaluated data is returned.
suffix (data-expr, length)
- The
suffixoperator evaluates data-expr and returns the last length bytes of the result of that evaluation. Length is a numeric expression. If data-expr or length evaluate to null, then the result is also null. If suffix evaluates to a number greater than the length of the evaluated data, then the evaluated data is returned.
lcase (data-expr)
- The
lcasefunction returns the result of evaluating data-expr converted to lower case. If data-expr evaluates to null, then the result is also null.
ucase (data-expr)
- The
ucasefunction returns the result of evaluating data-expr converted to upper case. If data-expr evaluates to null, then the result is also null.
option option-name
- The
optionoperator returns the contents of the specified option in the packet to which the server is responding.
config-option option-name
- The
config-optionoperator returns the value for the specified option that the DHCP client or server has been configured to send.
gethostname()
- The
gethostname()function returns a data string whose contents are a character string, the results of calling gethostname() on the local system with a size limit of 255 bytes (not including NULL terminator). This can be used for example to configure dhclient to send the local hostname without knowing the local hostname at the time dhclient.conf is written.
hardware
- The
hardwareoperator returns a data string whose first element is the type of network interface indicated in packet being considered, and whose subsequent elements are client’s link-layer address. If there is no packet, or if the RFC2131 hlen field is invalid, then the result is null. Hardware types include ethernet (1), token-ring (6), and fddi (8). Hardware types are specified by the IETF, and details on how the type numbers are defined can be found in RFC2131 (in the ISC DHCP distribution, this is included in the doc/ subdirectory).
packet (offset, length)
- The
packetoperator returns the specified portion of the packet being considered, or null in contexts where no packet is being considered. Offset and length are applied to the contents packet as in thesubstringoperator.
string
- A string, enclosed in quotes, may be specified as a data expression, and returns the text between the quotes, encoded in ASCII. The backslash (‘\’) character is treated specially, as in C programming: ‘ ’ means TAB, ‘
’ means carriage return, ‘
’ means newline, and ‘’ means bell. Any octal value can be specified with ‘
nn’, where nnn is any positive octal number less than 0400. Any hexadecimal value can be specified with ‘
