"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" |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 10 | |
| 11 | |
Denys Vlasenko | 0f62c4d | 2010-03-20 07:12:21 +0100 | [diff] [blame] | 12 | /* Supported options are easily added here. |
| 13 | * See RFC2132 for more options. |
Denys Vlasenko | 87fa216 | 2010-03-20 18:06:23 +0100 | [diff] [blame] | 14 | * OPTION_REQ: these options are requested by udhcpc (unless -o). |
Denys Vlasenko | 0f62c4d | 2010-03-20 07:12:21 +0100 | [diff] [blame] | 15 | */ |
Denis Vlasenko | c90c3f3 | 2006-11-23 12:57:49 +0000 | [diff] [blame] | 16 | const struct dhcp_option dhcp_options[] = { |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 17 | /* flags code */ |
| 18 | { OPTION_IP | OPTION_REQ, 0x01 }, /* DHCP_SUBNET */ |
| 19 | { OPTION_S32 , 0x02 }, /* DHCP_TIME_OFFSET */ |
| 20 | { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x03 }, /* DHCP_ROUTER */ |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 21 | // { OPTION_IP | OPTION_LIST , 0x04 }, /* DHCP_TIME_SERVER */ |
Denys Vlasenko | 777706c | 2010-03-19 23:42:23 +0100 | [diff] [blame] | 22 | // { OPTION_IP | OPTION_LIST , 0x05 }, /* DHCP_NAME_SERVER */ |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 23 | { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x06 }, /* DHCP_DNS_SERVER */ |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 24 | // { OPTION_IP | OPTION_LIST , 0x07 }, /* DHCP_LOG_SERVER */ |
Denys Vlasenko | 777706c | 2010-03-19 23:42:23 +0100 | [diff] [blame] | 25 | // { OPTION_IP | OPTION_LIST , 0x08 }, /* DHCP_COOKIE_SERVER */ |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 26 | { OPTION_IP | OPTION_LIST , 0x09 }, /* DHCP_LPR_SERVER */ |
| 27 | { OPTION_STRING | OPTION_REQ, 0x0c }, /* DHCP_HOST_NAME */ |
| 28 | { OPTION_U16 , 0x0d }, /* DHCP_BOOT_SIZE */ |
Denys Vlasenko | 0f62c4d | 2010-03-20 07:12:21 +0100 | [diff] [blame] | 29 | { OPTION_STRING | OPTION_REQ, 0x0f }, /* DHCP_DOMAIN_NAME */ |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 30 | { OPTION_IP , 0x10 }, /* DHCP_SWAP_SERVER */ |
| 31 | { OPTION_STRING , 0x11 }, /* DHCP_ROOT_PATH */ |
| 32 | { OPTION_U8 , 0x17 }, /* DHCP_IP_TTL */ |
| 33 | { OPTION_U16 , 0x1a }, /* DHCP_MTU */ |
| 34 | { OPTION_IP | OPTION_REQ, 0x1c }, /* DHCP_BROADCAST */ |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 35 | { OPTION_STRING , 0x28 }, /* DHCP_NIS_DOMAIN */ |
| 36 | { OPTION_IP | OPTION_LIST , 0x29 }, /* DHCP_NIS_SERVER */ |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 37 | { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x2a }, /* DHCP_NTP_SERVER */ |
| 38 | { OPTION_IP | OPTION_LIST , 0x2c }, /* DHCP_WINS_SERVER */ |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 39 | { OPTION_U32 , 0x33 }, /* DHCP_LEASE_TIME */ |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 40 | { OPTION_IP , 0x36 }, /* DHCP_SERVER_ID */ |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 41 | { OPTION_STRING , 0x38 }, /* DHCP_ERR_MESSAGE */ |
| 42 | //TODO: must be combined with 'sname' and 'file' handling: |
| 43 | { OPTION_STRING , 0x42 }, /* DHCP_TFTP_SERVER_NAME */ |
| 44 | { OPTION_STRING , 0x43 }, /* DHCP_BOOT_FILE */ |
Denys Vlasenko | 0f62c4d | 2010-03-20 07:12:21 +0100 | [diff] [blame] | 45 | //TODO: not a string, but a set of LASCII strings: |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 46 | // { OPTION_STRING , 0x4D }, /* DHCP_USER_CLASS */ |
Denis Vlasenko | 35a064b | 2008-11-06 00:49:59 +0000 | [diff] [blame] | 47 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 48 | { OPTION_STR1035 | OPTION_LIST , 0x77 }, /* DHCP_DOMAIN_SEARCH */ |
Denis Vlasenko | 5066473 | 2007-02-27 21:15:08 +0000 | [diff] [blame] | 49 | #endif |
Denys Vlasenko | 97ef654 | 2009-07-03 18:14:03 +0200 | [diff] [blame] | 50 | { OPTION_STATIC_ROUTES , 0x79 }, /* DHCP_STATIC_ROUTES */ |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 51 | { OPTION_STRING , 0xfc }, /* DHCP_WPAD */ |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 52 | |
| 53 | /* Options below have no match in dhcp_option_strings[], |
| 54 | * are not passed to dhcpc scripts, and cannot be specified |
Denys Vlasenko | 0f62c4d | 2010-03-20 07:12:21 +0100 | [diff] [blame] | 55 | * with "option XXX YYY" syntax in dhcpd config file. |
| 56 | * These entries are only used internally by udhcp[cd] |
| 57 | * to correctly encode options into packets. |
| 58 | */ |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 59 | |
Denys Vlasenko | 0f62c4d | 2010-03-20 07:12:21 +0100 | [diff] [blame] | 60 | { OPTION_IP , 0x32 }, /* DHCP_REQUESTED_IP */ |
| 61 | { OPTION_U8 , 0x35 }, /* DHCP_MESSAGE_TYPE */ |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 62 | { OPTION_U16 , 0x39 }, /* DHCP_MAX_SIZE */ |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 63 | { OPTION_STRING , 0x3c }, /* DHCP_VENDOR */ |
| 64 | //FIXME: handling of this option is not exactly correct: |
| 65 | { OPTION_STRING , 0x3d }, /* DHCP_CLIENT_ID */ |
Denys Vlasenko | 043b1e5 | 2009-09-06 12:47:55 +0200 | [diff] [blame] | 66 | { 0, 0 } /* zeroed terminating entry */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 69 | /* Used for converting options from incoming packets to env variables |
Denys Vlasenko | 0f62c4d | 2010-03-20 07:12:21 +0100 | [diff] [blame] | 70 | * for udhcpc stript, and for setting options for udhcpd via |
| 71 | * "opt OPTION_NAME OPTION_VALUE" directives in udhcpd.conf file. |
| 72 | */ |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 73 | /* Must match dhcp_options[] order */ |
| 74 | const char dhcp_option_strings[] ALIGN1 = |
| 75 | "subnet" "\0" /* DHCP_SUBNET */ |
| 76 | "timezone" "\0" /* DHCP_TIME_OFFSET */ |
| 77 | "router" "\0" /* DHCP_ROUTER */ |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 78 | // "timesrv" "\0" /* DHCP_TIME_SERVER */ |
Denys Vlasenko | 777706c | 2010-03-19 23:42:23 +0100 | [diff] [blame] | 79 | // "namesrv" "\0" /* DHCP_NAME_SERVER */ |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 80 | "dns" "\0" /* DHCP_DNS_SERVER */ |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 81 | // "logsrv" "\0" /* DHCP_LOG_SERVER */ |
Denys Vlasenko | 777706c | 2010-03-19 23:42:23 +0100 | [diff] [blame] | 82 | // "cookiesrv" "\0" /* DHCP_COOKIE_SERVER */ |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 83 | "lprsrv" "\0" /* DHCP_LPR_SERVER */ |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 84 | "hostname" "\0" /* DHCP_HOST_NAME */ |
| 85 | "bootsize" "\0" /* DHCP_BOOT_SIZE */ |
| 86 | "domain" "\0" /* DHCP_DOMAIN_NAME */ |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 87 | "swapsrv" "\0" /* DHCP_SWAP_SERVER */ |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 88 | "rootpath" "\0" /* DHCP_ROOT_PATH */ |
| 89 | "ipttl" "\0" /* DHCP_IP_TTL */ |
| 90 | "mtu" "\0" /* DHCP_MTU */ |
| 91 | "broadcast" "\0" /* DHCP_BROADCAST */ |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 92 | "nisdomain" "\0" /* DHCP_NIS_DOMAIN */ |
| 93 | "nissrv" "\0" /* DHCP_NIS_SERVER */ |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 94 | "ntpsrv" "\0" /* DHCP_NTP_SERVER */ |
| 95 | "wins" "\0" /* DHCP_WINS_SERVER */ |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 96 | "lease" "\0" /* DHCP_LEASE_TIME */ |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 97 | "serverid" "\0" /* DHCP_SERVER_ID */ |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 98 | "message" "\0" /* DHCP_ERR_MESSAGE */ |
| 99 | "tftp" "\0" /* DHCP_TFTP_SERVER_NAME */ |
| 100 | "bootfile" "\0" /* DHCP_BOOT_FILE */ |
| 101 | // "userclass" "\0" /* DHCP_USER_CLASS */ |
Denis Vlasenko | 35a064b | 2008-11-06 00:49:59 +0000 | [diff] [blame] | 102 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 103 | "search" "\0" /* DHCP_DOMAIN_SEARCH */ |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 104 | #endif |
Denys Vlasenko | 0f62c4d | 2010-03-20 07:12:21 +0100 | [diff] [blame] | 105 | // "staticroutes" is only used to set udhcpc environment, it doesn't work |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 106 | // in udhcpd.conf since OPTION_STATIC_ROUTES is not handled yet |
| 107 | // by "string->option" conversion code: |
| 108 | "staticroutes" "\0"/* DHCP_STATIC_ROUTES */ |
| 109 | "wpad" "\0" /* DHCP_WPAD */ |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 110 | ; |
| 111 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 112 | /* Lengths of the different option types */ |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 113 | const uint8_t dhcp_option_lengths[] ALIGN1 = { |
Denis Vlasenko | c90c3f3 | 2006-11-23 12:57:49 +0000 | [diff] [blame] | 114 | [OPTION_IP] = 4, |
| 115 | [OPTION_IP_PAIR] = 8, |
| 116 | [OPTION_BOOLEAN] = 1, |
| 117 | [OPTION_STRING] = 1, |
Denis Vlasenko | 35a064b | 2008-11-06 00:49:59 +0000 | [diff] [blame] | 118 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
Denis Vlasenko | 5066473 | 2007-02-27 21:15:08 +0000 | [diff] [blame] | 119 | [OPTION_STR1035] = 1, |
| 120 | #endif |
Denis Vlasenko | c90c3f3 | 2006-11-23 12:57:49 +0000 | [diff] [blame] | 121 | [OPTION_U8] = 1, |
| 122 | [OPTION_U16] = 2, |
| 123 | [OPTION_S16] = 2, |
| 124 | [OPTION_U32] = 4, |
Denys Vlasenko | 7895b91 | 2009-07-03 16:59:59 +0200 | [diff] [blame] | 125 | [OPTION_S32] = 4, |
Denys Vlasenko | 97ef654 | 2009-07-03 18:14:03 +0200 | [diff] [blame] | 126 | /* Just like OPTION_STRING, we use minimum length here */ |
| 127 | [OPTION_STATIC_ROUTES] = 5, |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 131 | #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2 |
| 132 | static void log_option(const char *pfx, const uint8_t *opt) |
| 133 | { |
| 134 | if (dhcp_verbose >= 2) { |
| 135 | char buf[256 * 2 + 2]; |
| 136 | *bin2hex(buf, (void*) (opt + OPT_DATA), opt[OPT_LEN]) = '\0'; |
| 137 | bb_info_msg("%s: 0x%02x %s", pfx, opt[OPT_CODE], buf); |
| 138 | } |
| 139 | } |
| 140 | #else |
| 141 | # define log_option(pfx, opt) ((void)0) |
| 142 | #endif |
| 143 | |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 144 | /* get an option with bounds checking (warning, result is not aligned). */ |
Denys Vlasenko | dde8bdc | 2010-03-22 14:29:13 +0100 | [diff] [blame] | 145 | uint8_t* FAST_FUNC udhcp_get_option(struct dhcp_packet *packet, int code) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 146 | { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 147 | uint8_t *optionptr; |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 148 | int len; |
| 149 | int rem; |
| 150 | int overload = 0; |
| 151 | enum { |
| 152 | FILE_FIELD101 = FILE_FIELD * 0x101, |
| 153 | SNAME_FIELD101 = SNAME_FIELD * 0x101, |
| 154 | }; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 155 | |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 156 | /* option bytes: [code][len][data1][data2]..[dataLEN] */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 157 | optionptr = packet->options; |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 158 | rem = sizeof(packet->options); |
Denis Vlasenko | 6884f66 | 2007-11-23 00:08:54 +0000 | [diff] [blame] | 159 | while (1) { |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 160 | if (rem <= 0) { |
Denys Vlasenko | dde8bdc | 2010-03-22 14:29:13 +0100 | [diff] [blame] | 161 | bb_error_msg("bad packet, malformed option field"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 162 | return NULL; |
| 163 | } |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 164 | if (optionptr[OPT_CODE] == DHCP_PADDING) { |
| 165 | rem--; |
| 166 | optionptr++; |
| 167 | continue; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 168 | } |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 169 | if (optionptr[OPT_CODE] == DHCP_END) { |
| 170 | if ((overload & FILE_FIELD101) == FILE_FIELD) { |
| 171 | /* can use packet->file, and didn't look at it yet */ |
| 172 | overload |= FILE_FIELD101; /* "we looked at it" */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 173 | optionptr = packet->file; |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 174 | rem = sizeof(packet->file); |
| 175 | continue; |
| 176 | } |
| 177 | if ((overload & SNAME_FIELD101) == SNAME_FIELD) { |
| 178 | /* can use packet->sname, and didn't look at it yet */ |
| 179 | overload |= SNAME_FIELD101; /* "we looked at it" */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 180 | optionptr = packet->sname; |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 181 | rem = sizeof(packet->sname); |
| 182 | continue; |
| 183 | } |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 184 | break; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 185 | } |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 186 | len = 2 + optionptr[OPT_LEN]; |
| 187 | rem -= len; |
| 188 | if (rem < 0) |
| 189 | continue; /* complain and return NULL */ |
| 190 | |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 191 | if (optionptr[OPT_CODE] == code) { |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 192 | log_option("Option found", optionptr); |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 193 | return optionptr + OPT_DATA; |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 194 | } |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 195 | |
| 196 | if (optionptr[OPT_CODE] == DHCP_OPTION_OVERLOAD) { |
| 197 | overload |= optionptr[OPT_DATA]; |
| 198 | /* fall through */ |
| 199 | } |
| 200 | optionptr += len; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 201 | } |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 202 | |
| 203 | /* log3 because udhcpc uses it a lot - very noisy */ |
| 204 | log3("Option 0x%02x not found", code); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 205 | return NULL; |
| 206 | } |
| 207 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 208 | /* return the position of the 'end' option (no bounds checking) */ |
Denys Vlasenko | dde8bdc | 2010-03-22 14:29:13 +0100 | [diff] [blame] | 209 | int FAST_FUNC udhcp_end_option(uint8_t *optionptr) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 210 | { |
| 211 | int i = 0; |
| 212 | |
| 213 | while (optionptr[i] != DHCP_END) { |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 214 | if (optionptr[i] != DHCP_PADDING) |
| 215 | i += optionptr[i + OPT_LEN] + 1; |
| 216 | i++; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 217 | } |
| 218 | return i; |
| 219 | } |
| 220 | |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 221 | /* add an option string to the options */ |
| 222 | /* option bytes: [code][len][data1][data2]..[dataLEN] */ |
Denys Vlasenko | dde8bdc | 2010-03-22 14:29:13 +0100 | [diff] [blame] | 223 | void FAST_FUNC udhcp_add_option_string(uint8_t *optionptr, uint8_t *string) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 224 | { |
Denys Vlasenko | dde8bdc | 2010-03-22 14:29:13 +0100 | [diff] [blame] | 225 | int end = udhcp_end_option(optionptr); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 226 | |
| 227 | /* end position + string length + option code/length + end option */ |
Denis Vlasenko | 72e7604 | 2007-11-25 03:15:24 +0000 | [diff] [blame] | 228 | if (end + string[OPT_LEN] + 2 + 1 >= DHCP_OPTIONS_BUFSIZE) { |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 229 | bb_error_msg("option 0x%02x did not fit into the packet", |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 230 | string[OPT_CODE]); |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 231 | return; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 232 | } |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 233 | log_option("Adding option", string); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 234 | memcpy(optionptr + end, string, string[OPT_LEN] + 2); |
| 235 | optionptr[end + string[OPT_LEN] + 2] = DHCP_END; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 238 | /* add a one to four byte option to a packet */ |
Denys Vlasenko | dde8bdc | 2010-03-22 14:29:13 +0100 | [diff] [blame] | 239 | void FAST_FUNC udhcp_add_simple_option(uint8_t *optionptr, uint8_t code, uint32_t data) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 240 | { |
Denis Vlasenko | c90c3f3 | 2006-11-23 12:57:49 +0000 | [diff] [blame] | 241 | const struct dhcp_option *dh; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 242 | |
Denis Vlasenko | c90c3f3 | 2006-11-23 12:57:49 +0000 | [diff] [blame] | 243 | for (dh = dhcp_options; dh->code; dh++) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 244 | if (dh->code == code) { |
| 245 | uint8_t option[6], len; |
| 246 | |
| 247 | option[OPT_CODE] = code; |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 248 | len = dhcp_option_lengths[dh->flags & OPTION_TYPE_MASK]; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 249 | option[OPT_LEN] = len; |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 250 | if (BB_BIG_ENDIAN) |
| 251 | data <<= 8 * (4 - len); |
Denis Vlasenko | efb545b | 2008-12-08 22:56:18 +0000 | [diff] [blame] | 252 | /* Assignment is unaligned! */ |
| 253 | move_to_unaligned32(&option[OPT_DATA], data); |
Denys Vlasenko | dde8bdc | 2010-03-22 14:29:13 +0100 | [diff] [blame] | 254 | udhcp_add_option_string(optionptr, option); |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 255 | return; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 259 | bb_error_msg("can't add option 0x%02x", code); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 260 | } |