"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 2 | /* |
| 3 | * options.c -- DHCP server option packet tools |
| 4 | * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001 |
Denis Vlasenko | db12d1d | 2008-12-07 00:52:58 +0000 | [diff] [blame] | 5 | * |
| 6 | * Licensed under GPLv2, see file LICENSE in this tarball for details. |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 9 | #include "common.h" |
| 10 | #include "dhcpd.h" |
| 11 | #include "options.h" |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 12 | |
| 13 | |
Denis Vlasenko | 35ff746 | 2007-11-28 19:23:12 +0000 | [diff] [blame] | 14 | /* Supported options are easily added here */ |
Denis Vlasenko | c90c3f3 | 2006-11-23 12:57:49 +0000 | [diff] [blame] | 15 | const struct dhcp_option dhcp_options[] = { |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 16 | /* flags code */ |
| 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_STRING , 0x28 }, /* nisdomain */ |
| 35 | { OPTION_IP | OPTION_LIST , 0x29 }, /* nissrv */ |
| 36 | { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x2a }, /* DHCP_NTP_SERVER */ |
| 37 | { OPTION_IP | OPTION_LIST , 0x2c }, /* DHCP_WINS_SERVER */ |
| 38 | { OPTION_IP , 0x32 }, /* DHCP_REQUESTED_IP */ |
| 39 | { OPTION_U32 , 0x33 }, /* DHCP_LEASE_TIME */ |
| 40 | { OPTION_U8 , 0x35 }, /* dhcptype */ |
| 41 | { OPTION_IP , 0x36 }, /* DHCP_SERVER_ID */ |
| 42 | { OPTION_STRING , 0x38 }, /* DHCP_MESSAGE */ |
| 43 | { OPTION_STRING , 0x3C }, /* DHCP_VENDOR */ |
| 44 | { OPTION_STRING , 0x3D }, /* DHCP_CLIENT_ID */ |
| 45 | { OPTION_STRING , 0x42 }, /* tftp */ |
| 46 | { OPTION_STRING , 0x43 }, /* bootfile */ |
| 47 | { OPTION_STRING , 0x4D }, /* userclass */ |
Denis Vlasenko | 35a064b | 2008-11-06 00:49:59 +0000 | [diff] [blame] | 48 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 49 | { OPTION_STR1035 | OPTION_LIST , 0x77 }, /* search */ |
Denis Vlasenko | 5066473 | 2007-02-27 21:15:08 +0000 | [diff] [blame] | 50 | #endif |
Denis Vlasenko | c90c3f3 | 2006-11-23 12:57:49 +0000 | [diff] [blame] | 51 | /* MSIE's "Web Proxy Autodiscovery Protocol" support */ |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 52 | { OPTION_STRING , 0xfc }, /* wpad */ |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 53 | |
| 54 | /* Options below have no match in dhcp_option_strings[], |
| 55 | * are not passed to dhcpc scripts, and cannot be specified |
| 56 | * with "option XXX YYY" syntax in dhcpd config file. */ |
| 57 | |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 58 | { OPTION_U16 , 0x39 }, /* DHCP_MAX_SIZE */ |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 59 | { } /* zeroed terminating entry */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 62 | /* Used for converting options from incoming packets to env variables |
| 63 | * for udhcpc stript */ |
| 64 | /* Must match dhcp_options[] order */ |
| 65 | const char dhcp_option_strings[] ALIGN1 = |
| 66 | "subnet" "\0" /* DHCP_SUBNET */ |
| 67 | "timezone" "\0" /* DHCP_TIME_OFFSET */ |
| 68 | "router" "\0" /* DHCP_ROUTER */ |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 69 | "timesrv" "\0" /* DHCP_TIME_SERVER */ |
| 70 | "namesrv" "\0" /* DHCP_NAME_SERVER */ |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 71 | "dns" "\0" /* DHCP_DNS_SERVER */ |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 72 | "logsrv" "\0" /* DHCP_LOG_SERVER */ |
| 73 | "cookiesrv" "\0" /* DHCP_COOKIE_SERVER */ |
| 74 | "lprsrv" "\0" /* DHCP_LPR_SERVER */ |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 75 | "hostname" "\0" /* DHCP_HOST_NAME */ |
| 76 | "bootsize" "\0" /* DHCP_BOOT_SIZE */ |
| 77 | "domain" "\0" /* DHCP_DOMAIN_NAME */ |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 78 | "swapsrv" "\0" /* DHCP_SWAP_SERVER */ |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 79 | "rootpath" "\0" /* DHCP_ROOT_PATH */ |
| 80 | "ipttl" "\0" /* DHCP_IP_TTL */ |
| 81 | "mtu" "\0" /* DHCP_MTU */ |
| 82 | "broadcast" "\0" /* DHCP_BROADCAST */ |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 83 | "nisdomain" "\0" /* */ |
| 84 | "nissrv" "\0" /* */ |
| 85 | "ntpsrv" "\0" /* DHCP_NTP_SERVER */ |
| 86 | "wins" "\0" /* DHCP_WINS_SERVER */ |
| 87 | "requestip" "\0" /* DHCP_REQUESTED_IP */ |
| 88 | "lease" "\0" /* DHCP_LEASE_TIME */ |
| 89 | "dhcptype" "\0" /* */ |
| 90 | "serverid" "\0" /* DHCP_SERVER_ID */ |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 91 | "message" "\0" /* DHCP_MESSAGE */ |
| 92 | "vendorclass" "\0" /* DHCP_VENDOR */ |
| 93 | "clientid" "\0" /* DHCP_CLIENT_ID */ |
| 94 | "tftp" "\0" |
| 95 | "bootfile" "\0" |
| 96 | "userclass" "\0" |
Denis Vlasenko | 35a064b | 2008-11-06 00:49:59 +0000 | [diff] [blame] | 97 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 98 | "search" "\0" |
| 99 | #endif |
| 100 | /* MSIE's "Web Proxy Autodiscovery Protocol" support */ |
| 101 | "wpad" "\0" |
| 102 | ; |
| 103 | |
Denis Vlasenko | 35ff746 | 2007-11-28 19:23:12 +0000 | [diff] [blame] | 104 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 105 | /* Lengths of the different option types */ |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 106 | const uint8_t dhcp_option_lengths[] ALIGN1 = { |
Denis Vlasenko | c90c3f3 | 2006-11-23 12:57:49 +0000 | [diff] [blame] | 107 | [OPTION_IP] = 4, |
| 108 | [OPTION_IP_PAIR] = 8, |
| 109 | [OPTION_BOOLEAN] = 1, |
| 110 | [OPTION_STRING] = 1, |
Denis Vlasenko | 35a064b | 2008-11-06 00:49:59 +0000 | [diff] [blame] | 111 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
Denis Vlasenko | 5066473 | 2007-02-27 21:15:08 +0000 | [diff] [blame] | 112 | [OPTION_STR1035] = 1, |
| 113 | #endif |
Denis Vlasenko | c90c3f3 | 2006-11-23 12:57:49 +0000 | [diff] [blame] | 114 | [OPTION_U8] = 1, |
| 115 | [OPTION_U16] = 2, |
| 116 | [OPTION_S16] = 2, |
| 117 | [OPTION_U32] = 4, |
Denys Vlasenko | 7895b91 | 2009-07-03 16:59:59 +0200 | [diff] [blame^] | 118 | [OPTION_S32] = 4, |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 122 | #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2 |
| 123 | static void log_option(const char *pfx, const uint8_t *opt) |
| 124 | { |
| 125 | if (dhcp_verbose >= 2) { |
| 126 | char buf[256 * 2 + 2]; |
| 127 | *bin2hex(buf, (void*) (opt + OPT_DATA), opt[OPT_LEN]) = '\0'; |
| 128 | bb_info_msg("%s: 0x%02x %s", pfx, opt[OPT_CODE], buf); |
| 129 | } |
| 130 | } |
| 131 | #else |
| 132 | # define log_option(pfx, opt) ((void)0) |
| 133 | #endif |
| 134 | |
| 135 | |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 136 | /* get an option with bounds checking (warning, result is not aligned). */ |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 137 | uint8_t* FAST_FUNC get_option(struct dhcp_packet *packet, int code) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 138 | { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 139 | uint8_t *optionptr; |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 140 | int len; |
| 141 | int rem; |
| 142 | int overload = 0; |
| 143 | enum { |
| 144 | FILE_FIELD101 = FILE_FIELD * 0x101, |
| 145 | SNAME_FIELD101 = SNAME_FIELD * 0x101, |
| 146 | }; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 147 | |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 148 | /* option bytes: [code][len][data1][data2]..[dataLEN] */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 149 | optionptr = packet->options; |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 150 | rem = sizeof(packet->options); |
Denis Vlasenko | 6884f66 | 2007-11-23 00:08:54 +0000 | [diff] [blame] | 151 | while (1) { |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 152 | if (rem <= 0) { |
| 153 | bb_error_msg("bogus packet, malformed option field"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 154 | return NULL; |
| 155 | } |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 156 | if (optionptr[OPT_CODE] == DHCP_PADDING) { |
| 157 | rem--; |
| 158 | optionptr++; |
| 159 | continue; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 160 | } |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 161 | if (optionptr[OPT_CODE] == DHCP_END) { |
| 162 | if ((overload & FILE_FIELD101) == FILE_FIELD) { |
| 163 | /* can use packet->file, and didn't look at it yet */ |
| 164 | overload |= FILE_FIELD101; /* "we looked at it" */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 165 | optionptr = packet->file; |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 166 | rem = sizeof(packet->file); |
| 167 | continue; |
| 168 | } |
| 169 | if ((overload & SNAME_FIELD101) == SNAME_FIELD) { |
| 170 | /* can use packet->sname, and didn't look at it yet */ |
| 171 | overload |= SNAME_FIELD101; /* "we looked at it" */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 172 | optionptr = packet->sname; |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 173 | rem = sizeof(packet->sname); |
| 174 | continue; |
| 175 | } |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 176 | break; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 177 | } |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 178 | len = 2 + optionptr[OPT_LEN]; |
| 179 | rem -= len; |
| 180 | if (rem < 0) |
| 181 | continue; /* complain and return NULL */ |
| 182 | |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 183 | if (optionptr[OPT_CODE] == code) { |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 184 | log_option("Option found", optionptr); |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 185 | return optionptr + OPT_DATA; |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 186 | } |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 187 | |
| 188 | if (optionptr[OPT_CODE] == DHCP_OPTION_OVERLOAD) { |
| 189 | overload |= optionptr[OPT_DATA]; |
| 190 | /* fall through */ |
| 191 | } |
| 192 | optionptr += len; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 193 | } |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 194 | |
| 195 | /* log3 because udhcpc uses it a lot - very noisy */ |
| 196 | log3("Option 0x%02x not found", code); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 197 | return NULL; |
| 198 | } |
| 199 | |
| 200 | |
| 201 | /* return the position of the 'end' option (no bounds checking) */ |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 202 | int FAST_FUNC end_option(uint8_t *optionptr) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 203 | { |
| 204 | int i = 0; |
| 205 | |
| 206 | while (optionptr[i] != DHCP_END) { |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 207 | if (optionptr[i] != DHCP_PADDING) |
| 208 | i += optionptr[i + OPT_LEN] + 1; |
| 209 | i++; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 210 | } |
| 211 | return i; |
| 212 | } |
| 213 | |
| 214 | |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 215 | /* add an option string to the options */ |
| 216 | /* option bytes: [code][len][data1][data2]..[dataLEN] */ |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 217 | int FAST_FUNC add_option_string(uint8_t *optionptr, uint8_t *string) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 218 | { |
| 219 | int end = end_option(optionptr); |
| 220 | |
| 221 | /* end position + string length + option code/length + end option */ |
Denis Vlasenko | 72e7604 | 2007-11-25 03:15:24 +0000 | [diff] [blame] | 222 | if (end + string[OPT_LEN] + 2 + 1 >= DHCP_OPTIONS_BUFSIZE) { |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 223 | bb_error_msg("option 0x%02x did not fit into the packet", |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 224 | string[OPT_CODE]); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 225 | return 0; |
| 226 | } |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 227 | log_option("Adding option", string); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 228 | memcpy(optionptr + end, string, string[OPT_LEN] + 2); |
| 229 | optionptr[end + string[OPT_LEN] + 2] = DHCP_END; |
| 230 | return string[OPT_LEN] + 2; |
| 231 | } |
| 232 | |
| 233 | |
| 234 | /* add a one to four byte option to a packet */ |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 235 | int FAST_FUNC add_simple_option(uint8_t *optionptr, uint8_t code, uint32_t data) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 236 | { |
Denis Vlasenko | c90c3f3 | 2006-11-23 12:57:49 +0000 | [diff] [blame] | 237 | const struct dhcp_option *dh; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 238 | |
Denis Vlasenko | c90c3f3 | 2006-11-23 12:57:49 +0000 | [diff] [blame] | 239 | for (dh = dhcp_options; dh->code; dh++) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 240 | if (dh->code == code) { |
| 241 | uint8_t option[6], len; |
| 242 | |
| 243 | option[OPT_CODE] = code; |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 244 | len = dhcp_option_lengths[dh->flags & TYPE_MASK]; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 245 | option[OPT_LEN] = len; |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 246 | if (BB_BIG_ENDIAN) |
| 247 | data <<= 8 * (4 - len); |
Denis Vlasenko | efb545b | 2008-12-08 22:56:18 +0000 | [diff] [blame] | 248 | /* Assignment is unaligned! */ |
| 249 | move_to_unaligned32(&option[OPT_DATA], data); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 250 | return add_option_string(optionptr, option); |
| 251 | } |
| 252 | } |
| 253 | |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 254 | bb_error_msg("cannot add option 0x%02x", code); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 255 | return 0; |
| 256 | } |