1 /* vi: set sw=4 ts=4: */
3 * options.c -- DHCP server option packet tools
4 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
6 * Licensed under GPLv2, see file LICENSE in this tarball for details.
14 /* Supported options are easily added here */
15 const struct dhcp_option dhcp_options
[] = {
17 { OPTION_IP
| OPTION_REQ
, 0x01 }, /* DHCP_SUBNET */
18 { OPTION_S32
, 0x02 }, /* DHCP_TIME_OFFSET */
19 { OPTION_IP
| OPTION_LIST
| OPTION_REQ
, 0x03 }, /* DHCP_ROUTER */
20 { OPTION_IP
| OPTION_LIST
, 0x04 }, /* DHCP_TIME_SERVER */
21 { OPTION_IP
| OPTION_LIST
, 0x05 }, /* DHCP_NAME_SERVER */
22 { OPTION_IP
| OPTION_LIST
| OPTION_REQ
, 0x06 }, /* DHCP_DNS_SERVER */
23 { OPTION_IP
| OPTION_LIST
, 0x07 }, /* DHCP_LOG_SERVER */
24 { OPTION_IP
| OPTION_LIST
, 0x08 }, /* DHCP_COOKIE_SERVER */
25 { OPTION_IP
| OPTION_LIST
, 0x09 }, /* DHCP_LPR_SERVER */
26 { OPTION_STRING
| OPTION_REQ
, 0x0c }, /* DHCP_HOST_NAME */
27 { OPTION_U16
, 0x0d }, /* DHCP_BOOT_SIZE */
28 { OPTION_STRING
| OPTION_LIST
| OPTION_REQ
, 0x0f }, /* DHCP_DOMAIN_NAME */
29 { OPTION_IP
, 0x10 }, /* DHCP_SWAP_SERVER */
30 { OPTION_STRING
, 0x11 }, /* DHCP_ROOT_PATH */
31 { OPTION_U8
, 0x17 }, /* DHCP_IP_TTL */
32 { OPTION_U16
, 0x1a }, /* DHCP_MTU */
33 { OPTION_IP
| OPTION_REQ
, 0x1c }, /* DHCP_BROADCAST */
34 { OPTION_IP
| OPTION_LIST
| OPTION_REQ
, 0x21 }, /* routes */
35 { OPTION_STRING
, 0x28 }, /* nisdomain */
36 { OPTION_IP
| OPTION_LIST
, 0x29 }, /* nissrv */
37 { OPTION_IP
| OPTION_LIST
| OPTION_REQ
, 0x2a }, /* DHCP_NTP_SERVER */
38 { OPTION_IP
| OPTION_LIST
, 0x2c }, /* DHCP_WINS_SERVER */
39 { OPTION_IP
, 0x32 }, /* DHCP_REQUESTED_IP */
40 { OPTION_U32
, 0x33 }, /* DHCP_LEASE_TIME */
41 { OPTION_U8
, 0x35 }, /* dhcptype */
42 { OPTION_IP
, 0x36 }, /* DHCP_SERVER_ID */
43 { OPTION_STRING
, 0x38 }, /* DHCP_MESSAGE */
44 { OPTION_STRING
, 0x3C }, /* DHCP_VENDOR */
45 { OPTION_STRING
, 0x3D }, /* DHCP_CLIENT_ID */
46 { OPTION_STRING
, 0x42 }, /* tftp */
47 { OPTION_STRING
, 0x43 }, /* bootfile */
48 { OPTION_STRING
, 0x4D }, /* userclass */
49 #if ENABLE_FEATURE_UDHCP_RFC3397
50 { OPTION_STR1035
| OPTION_LIST
, 0x77 }, /* search */
52 { OPTION_STATIC_ROUTES
, 0x79 }, /* DHCP_STATIC_ROUTES */
53 { OPTION_U8
| OPTION_LIST
| OPTION_REQ
, 0xF9 }, /* msroutes */
54 /* MSIE's "Web Proxy Autodiscovery Protocol" support */
55 { OPTION_STRING
, 0xfc }, /* wpad */
57 /* Options below have no match in dhcp_option_strings[],
58 * are not passed to dhcpc scripts, and cannot be specified
59 * with "option XXX YYY" syntax in dhcpd config file. */
61 { OPTION_U16
, 0x39 }, /* DHCP_MAX_SIZE */
62 { } /* zeroed terminating entry */
65 /* Used for converting options from incoming packets to env variables
66 * for udhcpc stript */
67 /* Must match dhcp_options[] order */
68 const char dhcp_option_strings
[] ALIGN1
=
69 "subnet" "\0" /* DHCP_SUBNET */
70 "timezone" "\0" /* DHCP_TIME_OFFSET */
71 "router" "\0" /* DHCP_ROUTER */
72 "timesrv" "\0" /* DHCP_TIME_SERVER */
73 "namesrv" "\0" /* DHCP_NAME_SERVER */
74 "dns" "\0" /* DHCP_DNS_SERVER */
75 "logsrv" "\0" /* DHCP_LOG_SERVER */
76 "cookiesrv" "\0" /* DHCP_COOKIE_SERVER */
77 "lprsrv" "\0" /* DHCP_LPR_SERVER */
78 "hostname" "\0" /* DHCP_HOST_NAME */
79 "bootsize" "\0" /* DHCP_BOOT_SIZE */
80 "domain" "\0" /* DHCP_DOMAIN_NAME */
81 "swapsrv" "\0" /* DHCP_SWAP_SERVER */
82 "rootpath" "\0" /* DHCP_ROOT_PATH */
83 "ipttl" "\0" /* DHCP_IP_TTL */
84 "mtu" "\0" /* DHCP_MTU */
85 "broadcast" "\0" /* DHCP_BROADCAST */
87 "nisdomain" "\0" /* */
89 "ntpsrv" "\0" /* DHCP_NTP_SERVER */
90 "wins" "\0" /* DHCP_WINS_SERVER */
91 "requestip" "\0" /* DHCP_REQUESTED_IP */
92 "lease" "\0" /* DHCP_LEASE_TIME */
94 "serverid" "\0" /* DHCP_SERVER_ID */
95 "message" "\0" /* DHCP_MESSAGE */
96 "vendorclass" "\0" /* DHCP_VENDOR */
97 "clientid" "\0" /* DHCP_CLIENT_ID */
101 #if ENABLE_FEATURE_UDHCP_RFC3397
104 "staticroutes" "\0" /* DHCP_STATIC_ROUTES */
106 /* MSIE's "Web Proxy Autodiscovery Protocol" support */
111 /* Lengths of the different option types */
112 const uint8_t dhcp_option_lengths
[] ALIGN1
= {
114 [OPTION_IP_PAIR
] = 8,
115 [OPTION_BOOLEAN
] = 1,
117 #if ENABLE_FEATURE_UDHCP_RFC3397
118 [OPTION_STR1035
] = 1,
125 /* Just like OPTION_STRING, we use minimum length here */
126 [OPTION_STATIC_ROUTES
] = 5,
130 /* get an option with bounds checking (warning, result is not aligned). */
131 uint8_t* FAST_FUNC
get_option(struct dhcpMessage
*packet
, int code
)
138 FILE_FIELD101
= FILE_FIELD
* 0x101,
139 SNAME_FIELD101
= SNAME_FIELD
* 0x101,
142 /* option bytes: [code][len][data1][data2]..[dataLEN] */
143 optionptr
= packet
->options
;
144 rem
= sizeof(packet
->options
);
147 bb_error_msg("bogus packet, malformed option field");
150 if (optionptr
[OPT_CODE
] == DHCP_PADDING
) {
155 if (optionptr
[OPT_CODE
] == DHCP_END
) {
156 if ((overload
& FILE_FIELD101
) == FILE_FIELD
) {
157 /* can use packet->file, and didn't look at it yet */
158 overload
|= FILE_FIELD101
; /* "we looked at it" */
159 optionptr
= packet
->file
;
160 rem
= sizeof(packet
->file
);
163 if ((overload
& SNAME_FIELD101
) == SNAME_FIELD
) {
164 /* can use packet->sname, and didn't look at it yet */
165 overload
|= SNAME_FIELD101
; /* "we looked at it" */
166 optionptr
= packet
->sname
;
167 rem
= sizeof(packet
->sname
);
172 len
= 2 + optionptr
[OPT_LEN
];
175 continue; /* complain and return NULL */
177 if (optionptr
[OPT_CODE
] == code
)
178 return optionptr
+ OPT_DATA
;
180 if (optionptr
[OPT_CODE
] == DHCP_OPTION_OVERLOAD
) {
181 overload
|= optionptr
[OPT_DATA
];
190 /* return the position of the 'end' option (no bounds checking) */
191 int FAST_FUNC
end_option(uint8_t *optionptr
)
195 while (optionptr
[i
] != DHCP_END
) {
196 if (optionptr
[i
] != DHCP_PADDING
)
197 i
+= optionptr
[i
+ OPT_LEN
] + 1;
204 /* add an option string to the options */
205 /* option bytes: [code][len][data1][data2]..[dataLEN] */
206 int FAST_FUNC
add_option_string(uint8_t *optionptr
, uint8_t *string
)
208 int end
= end_option(optionptr
);
210 /* end position + string length + option code/length + end option */
211 if (end
+ string
[OPT_LEN
] + 2 + 1 >= DHCP_OPTIONS_BUFSIZE
) {
212 bb_error_msg("option 0x%02x did not fit into the packet",
216 DEBUG("adding option 0x%02x", string
[OPT_CODE
]);
217 memcpy(optionptr
+ end
, string
, string
[OPT_LEN
] + 2);
218 optionptr
[end
+ string
[OPT_LEN
] + 2] = DHCP_END
;
219 return string
[OPT_LEN
] + 2;
223 /* add a one to four byte option to a packet */
224 int FAST_FUNC
add_simple_option(uint8_t *optionptr
, uint8_t code
, uint32_t data
)
226 const struct dhcp_option
*dh
;
228 for (dh
= dhcp_options
; dh
->code
; dh
++) {
229 if (dh
->code
== code
) {
230 uint8_t option
[6], len
;
232 option
[OPT_CODE
] = code
;
233 len
= dhcp_option_lengths
[dh
->flags
& TYPE_MASK
];
234 option
[OPT_LEN
] = len
;
236 data
<<= 8 * (4 - len
);
237 /* Assignment is unaligned! */
238 move_to_unaligned32(&option
[OPT_DATA
], data
);
239 return add_option_string(optionptr
, option
);
243 bb_error_msg("cannot add option 0x%02x", code
);