Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | * options.c -- DHCP server option packet tools |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 3 | * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001 |
| 4 | */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 6 | #include <stdlib.h> |
| 7 | #include <string.h> |
| 8 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 9 | #include "dhcpd.h" |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 10 | #include "options.h" |
Paul Fox | b6f7164 | 2005-09-22 19:03:24 +0000 | [diff] [blame^] | 11 | #include "files.h" |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 12 | #include "common.h" |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 13 | |
| 14 | |
| 15 | /* supported options are easily added here */ |
Russ Dill | 4a9e34c | 2003-12-15 22:09:36 +0000 | [diff] [blame] | 16 | struct dhcp_option dhcp_options[] = { |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 17 | /* name[10] flags code */ |
Russ Dill | 1eb7a17 | 2002-12-11 21:12:45 +0000 | [diff] [blame] | 18 | {"subnet", OPTION_IP | OPTION_REQ, 0x01}, |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 19 | {"timezone", OPTION_S32, 0x02}, |
| 20 | {"router", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x03}, |
| 21 | {"timesvr", OPTION_IP | OPTION_LIST, 0x04}, |
| 22 | {"namesvr", OPTION_IP | OPTION_LIST, 0x05}, |
| 23 | {"dns", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x06}, |
| 24 | {"logsvr", OPTION_IP | OPTION_LIST, 0x07}, |
| 25 | {"cookiesvr", OPTION_IP | OPTION_LIST, 0x08}, |
| 26 | {"lprsvr", OPTION_IP | OPTION_LIST, 0x09}, |
| 27 | {"hostname", OPTION_STRING | OPTION_REQ, 0x0c}, |
| 28 | {"bootsize", OPTION_U16, 0x0d}, |
| 29 | {"domain", OPTION_STRING | OPTION_REQ, 0x0f}, |
| 30 | {"swapsvr", OPTION_IP, 0x10}, |
| 31 | {"rootpath", OPTION_STRING, 0x11}, |
| 32 | {"ipttl", OPTION_U8, 0x17}, |
| 33 | {"mtu", OPTION_U16, 0x1a}, |
| 34 | {"broadcast", OPTION_IP | OPTION_REQ, 0x1c}, |
Mike Frysinger | d824853 | 2004-12-06 14:59:45 +0000 | [diff] [blame] | 35 | {"nisdomain", OPTION_STRING | OPTION_REQ, 0x28}, |
| 36 | {"nissrv", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x29}, |
| 37 | {"ntpsrv", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x2a}, |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 38 | {"wins", OPTION_IP | OPTION_LIST, 0x2c}, |
| 39 | {"requestip", OPTION_IP, 0x32}, |
| 40 | {"lease", OPTION_U32, 0x33}, |
| 41 | {"dhcptype", OPTION_U8, 0x35}, |
| 42 | {"serverid", OPTION_IP, 0x36}, |
| 43 | {"message", OPTION_STRING, 0x38}, |
| 44 | {"tftp", OPTION_STRING, 0x42}, |
| 45 | {"bootfile", OPTION_STRING, 0x43}, |
| 46 | {"", 0x00, 0x00} |
| 47 | }; |
| 48 | |
| 49 | /* Lengths of the different option types */ |
| 50 | int option_lengths[] = { |
| 51 | [OPTION_IP] = 4, |
| 52 | [OPTION_IP_PAIR] = 8, |
| 53 | [OPTION_BOOLEAN] = 1, |
| 54 | [OPTION_STRING] = 1, |
| 55 | [OPTION_U8] = 1, |
| 56 | [OPTION_U16] = 2, |
| 57 | [OPTION_S16] = 2, |
| 58 | [OPTION_U32] = 4, |
| 59 | [OPTION_S32] = 4 |
| 60 | }; |
| 61 | |
| 62 | |
| 63 | /* get an option with bounds checking (warning, not aligned). */ |
Eric Andersen | ad95373 | 2004-01-30 23:45:53 +0000 | [diff] [blame] | 64 | uint8_t *get_option(struct dhcpMessage *packet, int code) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 65 | { |
| 66 | int i, length; |
Eric Andersen | ad95373 | 2004-01-30 23:45:53 +0000 | [diff] [blame] | 67 | uint8_t *optionptr; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 68 | int over = 0, done = 0, curr = OPTION_FIELD; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 69 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 70 | optionptr = packet->options; |
| 71 | i = 0; |
| 72 | length = 308; |
| 73 | while (!done) { |
| 74 | if (i >= length) { |
| 75 | LOG(LOG_WARNING, "bogus packet, option fields too long."); |
| 76 | return NULL; |
| 77 | } |
| 78 | if (optionptr[i + OPT_CODE] == code) { |
| 79 | if (i + 1 + optionptr[i + OPT_LEN] >= length) { |
| 80 | LOG(LOG_WARNING, "bogus packet, option fields too long."); |
| 81 | return NULL; |
| 82 | } |
| 83 | return optionptr + i + 2; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 84 | } |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 85 | switch (optionptr[i + OPT_CODE]) { |
| 86 | case DHCP_PADDING: |
| 87 | i++; |
| 88 | break; |
| 89 | case DHCP_OPTION_OVER: |
| 90 | if (i + 1 + optionptr[i + OPT_LEN] >= length) { |
| 91 | LOG(LOG_WARNING, "bogus packet, option fields too long."); |
| 92 | return NULL; |
| 93 | } |
| 94 | over = optionptr[i + 3]; |
| 95 | i += optionptr[OPT_LEN] + 2; |
| 96 | break; |
| 97 | case DHCP_END: |
| 98 | if (curr == OPTION_FIELD && over & FILE_FIELD) { |
| 99 | optionptr = packet->file; |
| 100 | i = 0; |
| 101 | length = 128; |
| 102 | curr = FILE_FIELD; |
| 103 | } else if (curr == FILE_FIELD && over & SNAME_FIELD) { |
| 104 | optionptr = packet->sname; |
| 105 | i = 0; |
| 106 | length = 64; |
| 107 | curr = SNAME_FIELD; |
| 108 | } else done = 1; |
| 109 | break; |
| 110 | default: |
| 111 | i += optionptr[OPT_LEN + i] + 2; |
| 112 | } |
| 113 | } |
| 114 | return NULL; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | /* return the position of the 'end' option (no bounds checking) */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 119 | int end_option(uint8_t *optionptr) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 120 | { |
| 121 | int i = 0; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 122 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 123 | while (optionptr[i] != DHCP_END) { |
| 124 | if (optionptr[i] == DHCP_PADDING) i++; |
| 125 | else i += optionptr[i + OPT_LEN] + 2; |
| 126 | } |
| 127 | return i; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | /* add an option string to the options (an option string contains an option code, |
| 132 | * length, then data) */ |
Eric Andersen | ad95373 | 2004-01-30 23:45:53 +0000 | [diff] [blame] | 133 | int add_option_string(uint8_t *optionptr, uint8_t *string) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 134 | { |
| 135 | int end = end_option(optionptr); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 136 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 137 | /* end position + string length + option code/length + end option */ |
| 138 | if (end + string[OPT_LEN] + 2 + 1 >= 308) { |
| 139 | LOG(LOG_ERR, "Option 0x%02x did not fit into the packet!", string[OPT_CODE]); |
| 140 | return 0; |
| 141 | } |
| 142 | DEBUG(LOG_INFO, "adding option 0x%02x", string[OPT_CODE]); |
| 143 | memcpy(optionptr + end, string, string[OPT_LEN] + 2); |
| 144 | optionptr[end + string[OPT_LEN] + 2] = DHCP_END; |
| 145 | return string[OPT_LEN] + 2; |
| 146 | } |
| 147 | |
| 148 | |
| 149 | /* add a one to four byte option to a packet */ |
Eric Andersen | ad95373 | 2004-01-30 23:45:53 +0000 | [diff] [blame] | 150 | int add_simple_option(uint8_t *optionptr, uint8_t code, uint32_t data) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 151 | { |
Rob Landley | 230b411 | 2005-09-08 03:22:09 +0000 | [diff] [blame] | 152 | struct dhcp_option *dh; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 153 | |
Rob Landley | 230b411 | 2005-09-08 03:22:09 +0000 | [diff] [blame] | 154 | for (dh=dhcp_options; dh->code; dh++) { |
| 155 | if (dh->code == code) { |
| 156 | uint8_t option[6], len; |
| 157 | |
| 158 | option[OPT_CODE] = code; |
| 159 | len = option_lengths[dh->flags & TYPE_MASK]; |
| 160 | option[OPT_LEN] = len; |
| 161 | if (__BYTE_ORDER == __BIG_ENDIAN) |
| 162 | data <<= 8 * (4 - len); |
| 163 | /* This memcpy is for broken processors which can't |
| 164 | * handle a simple unaligned 32-bit assignment */ |
| 165 | memcpy(&option[OPT_DATA], &data, 4); |
| 166 | return add_option_string(optionptr, option); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 167 | } |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 168 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 169 | |
Rob Landley | 230b411 | 2005-09-08 03:22:09 +0000 | [diff] [blame] | 170 | DEBUG(LOG_ERR, "Could not add option 0x%02x", code); |
| 171 | return 0; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 172 | } |
| 173 | |