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