aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2010-03-21 00:43:11 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2010-03-21 00:43:11 +0100
commite5ce91b41b657a0dbd1db442ebc47f4c935e7d1f (patch)
treeca7537fff5a9026bdfe5c5c5104b65d6c2a5ebd0
parent87fa216e1e388c537cda2cff126eea816a4135ac (diff)
downloadbusybox-w32-e5ce91b41b657a0dbd1db442ebc47f4c935e7d1f.tar.gz
busybox-w32-e5ce91b41b657a0dbd1db442ebc47f4c935e7d1f.tar.bz2
busybox-w32-e5ce91b41b657a0dbd1db442ebc47f4c935e7d1f.zip
udhcp: code shrink; disable time and log server options
function old new delta add_server_options - 100 +100 udhcp_add_simple_option 92 90 -2 nobody_responds_to_arp 88 85 -3 dhcp_options 66 62 -4 udhcp_add_option_string 104 94 -10 udhcp_run_script 665 654 -11 dhcp_option_strings 203 188 -15 static.blank_chaddr 16 - -16 send_ACK 211 180 -31 add_bootp_options 61 - -61 udhcpd_main 1925 1846 -79 ------------------------------------------------------------------------------ (add/remove: 1/2 grow/shrink: 0/8 up/down: 100/-232) Total: -132 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--examples/udhcp/udhcpd.conf13
-rw-r--r--networking/udhcp/common.h4
-rw-r--r--networking/udhcp/dhcpd.c73
-rw-r--r--networking/udhcp/leases.c33
-rw-r--r--networking/udhcp/options.c64
-rw-r--r--networking/udhcp/options.h60
-rw-r--r--networking/udhcp/script.c6
7 files changed, 126 insertions, 127 deletions
diff --git a/examples/udhcp/udhcpd.conf b/examples/udhcp/udhcpd.conf
index 0ad982b55..a27a05357 100644
--- a/examples/udhcp/udhcpd.conf
+++ b/examples/udhcp/udhcpd.conf
@@ -77,7 +77,7 @@ option lease 864000 # 10 days
77#opt domain STRING - client's domain name 77#opt domain STRING - client's domain name
78#opt search STRING_LIST - search domains 78#opt search STRING_LIST - search domains
79#opt nisdomain STRING 79#opt nisdomain STRING
80#opt timezone NUM 80#opt timezone NUM - (localtime - UTC_time) in seconds. signed
81#opt tftp STRING - TFTP server name 81#opt tftp STRING - TFTP server name
82#opt bootfile STRING - file (kernel image) to load for booting 82#opt bootfile STRING - file (kernel image) to load for booting
83#opt bootsize NUM - size of that file 83#opt bootsize NUM - size of that file
@@ -89,10 +89,11 @@ option lease 864000 # 10 days
89#opt dns IP_LIST 89#opt dns IP_LIST
90#opt wins IP_LIST 90#opt wins IP_LIST
91#opt nissrv IP_LIST 91#opt nissrv IP_LIST
92#opt namesrv IP_LIST - obsolete, disabled 92#opt ntpsrv IP_LIST
93#opt logsrv IP_LIST
94#opt cookiesrv IP_LIST - rarely (never?) used, disabled
95#opt lprsrv IP_LIST 93#opt lprsrv IP_LIST
96#opt swapsrv IP 94#opt swapsrv IP
97#opt timesrv IP_LIST 95# Obsolete options, no longer supported
98#opt ntpsrv IP_LIST 96#opt logsrv IP_LIST - 704/UDP log server (not syslog!)
97#opt namesrv IP_LIST - IEN 116 name server, obsolete (August 1979!!!)
98#opt cookiesrv IP_LIST - RFC 865 "quote of the day" server, rarely (never?) used
99#opt timesrv IP_LIST - RFC 868 time server, rarely (never?) used
diff --git a/networking/udhcp/common.h b/networking/udhcp/common.h
index e88cfb1c7..d7c874e5b 100644
--- a/networking/udhcp/common.h
+++ b/networking/udhcp/common.h
@@ -44,6 +44,10 @@ struct dhcp_packet {
44 uint32_t cookie; /* fixed first four option bytes (99,130,83,99 dec) */ 44 uint32_t cookie; /* fixed first four option bytes (99,130,83,99 dec) */
45 uint8_t options[DHCP_OPTIONS_BUFSIZE + CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS]; 45 uint8_t options[DHCP_OPTIONS_BUFSIZE + CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS];
46} PACKED; 46} PACKED;
47#define DHCP_PKT_SNAME_LEN 64
48#define DHCP_PKT_FILE_LEN 128
49#define DHCP_PKT_SNAME_LEN_STR "64"
50#define DHCP_PKT_FILE_LEN_STR "128"
47 51
48struct ip_udp_dhcp_packet { 52struct ip_udp_dhcp_packet {
49 struct iphdr ip; 53 struct iphdr ip;
diff --git a/networking/udhcp/dhcpd.c b/networking/udhcp/dhcpd.c
index d7887d914..cc4cb9280 100644
--- a/networking/udhcp/dhcpd.c
+++ b/networking/udhcp/dhcpd.c
@@ -75,7 +75,9 @@ static int send_packet_to_client(struct dhcp_packet *dhcp_pkt, int force_broadca
75 server_config.ifindex); 75 server_config.ifindex);
76} 76}
77 77
78/* send a dhcp packet, if force broadcast is set, the packet will be broadcast to the client */ 78/* Send the dhcp packet.
79 * If force broadcast is set, the packet will be broadcast.
80 */
79static int send_packet(struct dhcp_packet *dhcp_pkt, int force_broadcast) 81static int send_packet(struct dhcp_packet *dhcp_pkt, int force_broadcast)
80{ 82{
81 if (dhcp_pkt->gateway_nip) 83 if (dhcp_pkt->gateway_nip)
@@ -94,10 +96,21 @@ static void init_packet(struct dhcp_packet *packet, struct dhcp_packet *oldpacke
94 add_simple_option(packet->options, DHCP_SERVER_ID, server_config.server_nip); 96 add_simple_option(packet->options, DHCP_SERVER_ID, server_config.server_nip);
95} 97}
96 98
97/* add in the bootp options */ 99/* Fill options field, siaddr_nip, and sname and boot_file fields.
98static void add_bootp_options(struct dhcp_packet *packet) 100 * TODO: teach this code to use overload option.
101 */
102static void add_server_options(struct dhcp_packet *packet)
99{ 103{
104 struct option_set *curr = server_config.options;
105
106 while (curr) {
107 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
108 add_option_string(packet->options, curr->data);
109 curr = curr->next;
110 }
111
100 packet->siaddr_nip = server_config.siaddr_nip; 112 packet->siaddr_nip = server_config.siaddr_nip;
113
101 if (server_config.sname) 114 if (server_config.sname)
102 strncpy((char*)packet->sname, server_config.sname, sizeof(packet->sname) - 1); 115 strncpy((char*)packet->sname, server_config.sname, sizeof(packet->sname) - 1);
103 if (server_config.boot_file) 116 if (server_config.boot_file)
@@ -123,17 +136,17 @@ static uint32_t select_lease_time(struct dhcp_packet *packet)
123static int send_offer(struct dhcp_packet *oldpacket, uint32_t static_lease_nip, struct dyn_lease *lease) 136static int send_offer(struct dhcp_packet *oldpacket, uint32_t static_lease_nip, struct dyn_lease *lease)
124{ 137{
125 struct dhcp_packet packet; 138 struct dhcp_packet packet;
126 uint32_t req_nip;
127 uint32_t lease_time_sec = server_config.max_lease_sec; 139 uint32_t lease_time_sec = server_config.max_lease_sec;
128 uint8_t *req_ip_opt;
129 const char *p_host_name; 140 const char *p_host_name;
130 struct option_set *curr;
131 struct in_addr addr; 141 struct in_addr addr;
132 142
133 init_packet(&packet, oldpacket, DHCPOFFER); 143 init_packet(&packet, oldpacket, DHCPOFFER);
134 144
135 /* ADDME: if static, short circuit */ 145 /* ADDME: if static, short circuit */
136 if (!static_lease_nip) { 146 if (!static_lease_nip) {
147 uint32_t req_nip;
148 uint8_t *req_ip_opt;
149
137 /* The client is in our lease/offered table */ 150 /* The client is in our lease/offered table */
138 if (lease) { 151 if (lease) {
139 packet.yiaddr = lease->lease_nip; 152 packet.yiaddr = lease->lease_nip;
@@ -145,10 +158,10 @@ static int send_offer(struct dhcp_packet *oldpacket, uint32_t static_lease_nip,
145 /* and the IP is in the lease range */ 158 /* and the IP is in the lease range */
146 && ntohl(req_nip) >= server_config.start_ip 159 && ntohl(req_nip) >= server_config.start_ip
147 && ntohl(req_nip) <= server_config.end_ip 160 && ntohl(req_nip) <= server_config.end_ip
148 /* and is not already taken/offered */ 161 /* and */
149 && (!(lease = find_lease_by_nip(req_nip)) 162 && ( !(lease = find_lease_by_nip(req_nip)) /* is not already taken */
150 /* or its taken, but expired */ 163 || is_expired_lease(lease) /* or is taken, but expired */
151 || is_expired_lease(lease)) 164 )
152 ) { 165 ) {
153 packet.yiaddr = req_nip; 166 packet.yiaddr = req_nip;
154 } 167 }
@@ -178,19 +191,11 @@ static int send_offer(struct dhcp_packet *oldpacket, uint32_t static_lease_nip,
178 } 191 }
179 192
180 add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_sec)); 193 add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_sec));
181 194 add_server_options(&packet);
182 curr = server_config.options;
183 while (curr) {
184 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
185 add_option_string(packet.options, curr->data);
186 curr = curr->next;
187 }
188
189 add_bootp_options(&packet);
190 195
191 addr.s_addr = packet.yiaddr; 196 addr.s_addr = packet.yiaddr;
192 bb_info_msg("Sending OFFER of %s", inet_ntoa(addr)); 197 bb_info_msg("Sending OFFER of %s", inet_ntoa(addr));
193 return send_packet(&packet, 0); 198 return send_packet(&packet, /*force_bcast:*/ 0);
194} 199}
195 200
196static int send_NAK(struct dhcp_packet *oldpacket) 201static int send_NAK(struct dhcp_packet *oldpacket)
@@ -200,13 +205,12 @@ static int send_NAK(struct dhcp_packet *oldpacket)
200 init_packet(&packet, oldpacket, DHCPNAK); 205 init_packet(&packet, oldpacket, DHCPNAK);
201 206
202 log1("Sending NAK"); 207 log1("Sending NAK");
203 return send_packet(&packet, 1); 208 return send_packet(&packet, /*force_bcast:*/ 1);
204} 209}
205 210
206static int send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr) 211static int send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr)
207{ 212{
208 struct dhcp_packet packet; 213 struct dhcp_packet packet;
209 struct option_set *curr;
210 uint32_t lease_time_sec; 214 uint32_t lease_time_sec;
211 struct in_addr addr; 215 struct in_addr addr;
212 const char *p_host_name; 216 const char *p_host_name;
@@ -217,20 +221,12 @@ static int send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr)
217 lease_time_sec = select_lease_time(oldpacket); 221 lease_time_sec = select_lease_time(oldpacket);
218 222
219 add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_sec)); 223 add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_sec));
220 224 add_server_options(&packet);
221 curr = server_config.options;
222 while (curr) {
223 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
224 add_option_string(packet.options, curr->data);
225 curr = curr->next;
226 }
227
228 add_bootp_options(&packet);
229 225
230 addr.s_addr = packet.yiaddr; 226 addr.s_addr = packet.yiaddr;
231 bb_info_msg("Sending ACK to %s", inet_ntoa(addr)); 227 bb_info_msg("Sending ACK to %s", inet_ntoa(addr));
232 228
233 if (send_packet(&packet, 0) < 0) 229 if (send_packet(&packet, /*force_bcast:*/ 0) < 0)
234 return -1; 230 return -1;
235 231
236 p_host_name = (const char*) get_option(oldpacket, DHCP_HOST_NAME); 232 p_host_name = (const char*) get_option(oldpacket, DHCP_HOST_NAME);
@@ -250,20 +246,11 @@ static int send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr)
250static int send_inform(struct dhcp_packet *oldpacket) 246static int send_inform(struct dhcp_packet *oldpacket)
251{ 247{
252 struct dhcp_packet packet; 248 struct dhcp_packet packet;
253 struct option_set *curr;
254 249
255 init_packet(&packet, oldpacket, DHCPACK); 250 init_packet(&packet, oldpacket, DHCPACK);
251 add_server_options(&packet);
256 252
257 curr = server_config.options; 253 return send_packet(&packet, /*force_bcast:*/ 0);
258 while (curr) {
259 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
260 add_option_string(packet.options, curr->data);
261 curr = curr->next;
262 }
263
264 add_bootp_options(&packet);
265
266 return send_packet(&packet, 0);
267} 254}
268 255
269 256
diff --git a/networking/udhcp/leases.c b/networking/udhcp/leases.c
index 78b0d0ade..efe67cf5d 100644
--- a/networking/udhcp/leases.c
+++ b/networking/udhcp/leases.c
@@ -29,16 +29,15 @@ static struct dyn_lease *oldest_expired_lease(void)
29} 29}
30 30
31 31
32/* Clear every lease out that chaddr OR yiaddr matches and is nonzero */ 32/* Clear out all leases with matching nonzero chaddr OR yiaddr.
33static void clear_lease(const uint8_t *chaddr, uint32_t yiaddr) 33 * If chaddr == NULL, this is a conflict lease.
34 */
35static void clear_leases(const uint8_t *chaddr, uint32_t yiaddr)
34{ 36{
35 unsigned i, j; 37 unsigned i;
36
37 for (j = 0; j < 16 && !chaddr[j]; j++)
38 continue;
39 38
40 for (i = 0; i < server_config.max_leases; i++) { 39 for (i = 0; i < server_config.max_leases; i++) {
41 if ((j != 16 && memcmp(g_leases[i].lease_mac, chaddr, 6) == 0) 40 if ((chaddr && memcmp(g_leases[i].lease_mac, chaddr, 6) == 0)
42 || (yiaddr && g_leases[i].lease_nip == yiaddr) 41 || (yiaddr && g_leases[i].lease_nip == yiaddr)
43 ) { 42 ) {
44 memset(&g_leases[i], 0, sizeof(g_leases[i])); 43 memset(&g_leases[i], 0, sizeof(g_leases[i]));
@@ -47,7 +46,9 @@ static void clear_lease(const uint8_t *chaddr, uint32_t yiaddr)
47} 46}
48 47
49 48
50/* Add a lease into the table, clearing out any old ones */ 49/* Add a lease into the table, clearing out any old ones
50 * If chaddr == NULL, this is a conflict lease.
51 */
51struct dyn_lease* FAST_FUNC add_lease( 52struct dyn_lease* FAST_FUNC add_lease(
52 const uint8_t *chaddr, uint32_t yiaddr, 53 const uint8_t *chaddr, uint32_t yiaddr,
53 leasetime_t leasetime, 54 leasetime_t leasetime,
@@ -56,12 +57,12 @@ struct dyn_lease* FAST_FUNC add_lease(
56 struct dyn_lease *oldest; 57 struct dyn_lease *oldest;
57 58
58 /* clean out any old ones */ 59 /* clean out any old ones */
59 clear_lease(chaddr, yiaddr); 60 clear_leases(chaddr, yiaddr);
60 61
61 oldest = oldest_expired_lease(); 62 oldest = oldest_expired_lease();
62 63
63 if (oldest) { 64 if (oldest) {
64 oldest->hostname[0] = '\0'; 65 memset(oldest, 0, sizeof(*oldest));
65 if (hostname) { 66 if (hostname) {
66 char *p; 67 char *p;
67 if (hostname_len > sizeof(oldest->hostname)) 68 if (hostname_len > sizeof(oldest->hostname))
@@ -74,7 +75,8 @@ struct dyn_lease* FAST_FUNC add_lease(
74 p++; 75 p++;
75 } 76 }
76 } 77 }
77 memcpy(oldest->lease_mac, chaddr, 6); 78 if (chaddr)
79 memcpy(oldest->lease_mac, chaddr, 6);
78 oldest->lease_nip = yiaddr; 80 oldest->lease_nip = yiaddr;
79 oldest->expires = time(NULL) + leasetime; 81 oldest->expires = time(NULL) + leasetime;
80 } 82 }
@@ -119,10 +121,6 @@ struct dyn_lease* FAST_FUNC find_lease_by_nip(uint32_t nip)
119/* Check if the IP is taken; if it is, add it to the lease table */ 121/* Check if the IP is taken; if it is, add it to the lease table */
120static int nobody_responds_to_arp(uint32_t nip, const uint8_t *safe_mac) 122static int nobody_responds_to_arp(uint32_t nip, const uint8_t *safe_mac)
121{ 123{
122 /* 16 zero bytes */
123 static const uint8_t blank_chaddr[16] = { 0 };
124 /* = { 0 } helps gcc to put it in rodata, not bss */
125
126 struct in_addr temp; 124 struct in_addr temp;
127 int r; 125 int r;
128 126
@@ -136,7 +134,7 @@ static int nobody_responds_to_arp(uint32_t nip, const uint8_t *safe_mac)
136 temp.s_addr = nip; 134 temp.s_addr = nip;
137 bb_info_msg("%s belongs to someone, reserving it for %u seconds", 135 bb_info_msg("%s belongs to someone, reserving it for %u seconds",
138 inet_ntoa(temp), (unsigned)server_config.conflict_time); 136 inet_ntoa(temp), (unsigned)server_config.conflict_time);
139 add_lease(blank_chaddr, nip, server_config.conflict_time, NULL, 0); 137 add_lease(NULL, nip, server_config.conflict_time, NULL, 0);
140 return 0; 138 return 0;
141} 139}
142 140
@@ -173,7 +171,8 @@ uint32_t FAST_FUNC find_free_or_expired_nip(const uint8_t *safe_mac)
173 } 171 }
174 } 172 }
175 173
176 if (oldest_lease && is_expired_lease(oldest_lease) 174 if (oldest_lease
175 && is_expired_lease(oldest_lease)
177 && nobody_responds_to_arp(oldest_lease->lease_nip, safe_mac) 176 && nobody_responds_to_arp(oldest_lease->lease_nip, safe_mac)
178 ) { 177 ) {
179 return oldest_lease->lease_nip; 178 return oldest_lease->lease_nip;
diff --git a/networking/udhcp/options.c b/networking/udhcp/options.c
index 09d31c69b..5bef985bc 100644
--- a/networking/udhcp/options.c
+++ b/networking/udhcp/options.c
@@ -20,10 +20,10 @@ const struct dhcp_option dhcp_options[] = {
20 { OPTION_IP | OPTION_REQ, 0x01 }, /* DHCP_SUBNET */ 20 { OPTION_IP | OPTION_REQ, 0x01 }, /* DHCP_SUBNET */
21 { OPTION_S32 , 0x02 }, /* DHCP_TIME_OFFSET */ 21 { OPTION_S32 , 0x02 }, /* DHCP_TIME_OFFSET */
22 { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x03 }, /* DHCP_ROUTER */ 22 { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x03 }, /* DHCP_ROUTER */
23 { OPTION_IP | OPTION_LIST , 0x04 }, /* DHCP_TIME_SERVER */ 23// { OPTION_IP | OPTION_LIST , 0x04 }, /* DHCP_TIME_SERVER */
24// { OPTION_IP | OPTION_LIST , 0x05 }, /* DHCP_NAME_SERVER */ 24// { OPTION_IP | OPTION_LIST , 0x05 }, /* DHCP_NAME_SERVER */
25 { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x06 }, /* DHCP_DNS_SERVER */ 25 { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x06 }, /* DHCP_DNS_SERVER */
26 { OPTION_IP | OPTION_LIST , 0x07 }, /* DHCP_LOG_SERVER */ 26// { OPTION_IP | OPTION_LIST , 0x07 }, /* DHCP_LOG_SERVER */
27// { OPTION_IP | OPTION_LIST , 0x08 }, /* DHCP_COOKIE_SERVER */ 27// { OPTION_IP | OPTION_LIST , 0x08 }, /* DHCP_COOKIE_SERVER */
28 { OPTION_IP | OPTION_LIST , 0x09 }, /* DHCP_LPR_SERVER */ 28 { OPTION_IP | OPTION_LIST , 0x09 }, /* DHCP_LPR_SERVER */
29 { OPTION_STRING | OPTION_REQ, 0x0c }, /* DHCP_HOST_NAME */ 29 { OPTION_STRING | OPTION_REQ, 0x0c }, /* DHCP_HOST_NAME */
@@ -34,23 +34,23 @@ const struct dhcp_option dhcp_options[] = {
34 { OPTION_U8 , 0x17 }, /* DHCP_IP_TTL */ 34 { OPTION_U8 , 0x17 }, /* DHCP_IP_TTL */
35 { OPTION_U16 , 0x1a }, /* DHCP_MTU */ 35 { OPTION_U16 , 0x1a }, /* DHCP_MTU */
36 { OPTION_IP | OPTION_REQ, 0x1c }, /* DHCP_BROADCAST */ 36 { OPTION_IP | OPTION_REQ, 0x1c }, /* DHCP_BROADCAST */
37 { OPTION_STRING , 0x28 }, /* nisdomain */ 37 { OPTION_STRING , 0x28 }, /* DHCP_NIS_DOMAIN */
38 { OPTION_IP | OPTION_LIST , 0x29 }, /* nissrv */ 38 { OPTION_IP | OPTION_LIST , 0x29 }, /* DHCP_NIS_SERVER */
39 { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x2a }, /* DHCP_NTP_SERVER */ 39 { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x2a }, /* DHCP_NTP_SERVER */
40 { OPTION_IP | OPTION_LIST , 0x2c }, /* DHCP_WINS_SERVER */ 40 { OPTION_IP | OPTION_LIST , 0x2c }, /* DHCP_WINS_SERVER */
41 { OPTION_U32 , 0x33 }, /* DHCP_LEASE_TIME */ 41 { OPTION_U32 , 0x33 }, /* DHCP_LEASE_TIME */
42 { OPTION_IP , 0x36 }, /* DHCP_SERVER_ID */ 42 { OPTION_IP , 0x36 }, /* DHCP_SERVER_ID */
43 { OPTION_STRING , 0x38 }, /* DHCP_MESSAGE */ 43 { OPTION_STRING , 0x38 }, /* DHCP_ERR_MESSAGE */
44 { OPTION_STRING , 0x42 }, /* tftp */ 44//TODO: must be combined with 'sname' and 'file' handling:
45 { OPTION_STRING , 0x43 }, /* bootfile */ 45 { OPTION_STRING , 0x42 }, /* DHCP_TFTP_SERVER_NAME */
46 { OPTION_STRING , 0x43 }, /* DHCP_BOOT_FILE */
46//TODO: not a string, but a set of LASCII strings: 47//TODO: not a string, but a set of LASCII strings:
47// { OPTION_STRING , 0x4D }, /* userclass */ 48// { OPTION_STRING , 0x4D }, /* DHCP_USER_CLASS */
48#if ENABLE_FEATURE_UDHCP_RFC3397 49#if ENABLE_FEATURE_UDHCP_RFC3397
49 { OPTION_STR1035 | OPTION_LIST , 0x77 }, /* search */ 50 { OPTION_STR1035 | OPTION_LIST , 0x77 }, /* DHCP_DOMAIN_SEARCH */
50#endif 51#endif
51 { OPTION_STATIC_ROUTES , 0x79 }, /* DHCP_STATIC_ROUTES */ 52 { OPTION_STATIC_ROUTES , 0x79 }, /* DHCP_STATIC_ROUTES */
52 /* MSIE's "Web Proxy Autodiscovery Protocol" support */ 53 { OPTION_STRING , 0xfc }, /* DHCP_WPAD */
53 { OPTION_STRING , 0xfc }, /* wpad */
54 54
55 /* Options below have no match in dhcp_option_strings[], 55 /* Options below have no match in dhcp_option_strings[],
56 * are not passed to dhcpc scripts, and cannot be specified 56 * are not passed to dhcpc scripts, and cannot be specified
@@ -62,8 +62,9 @@ const struct dhcp_option dhcp_options[] = {
62 { OPTION_IP , 0x32 }, /* DHCP_REQUESTED_IP */ 62 { OPTION_IP , 0x32 }, /* DHCP_REQUESTED_IP */
63 { OPTION_U8 , 0x35 }, /* DHCP_MESSAGE_TYPE */ 63 { OPTION_U8 , 0x35 }, /* DHCP_MESSAGE_TYPE */
64 { OPTION_U16 , 0x39 }, /* DHCP_MAX_SIZE */ 64 { OPTION_U16 , 0x39 }, /* DHCP_MAX_SIZE */
65 { OPTION_STRING , 0x3C }, /* DHCP_VENDOR */ 65 { OPTION_STRING , 0x3c }, /* DHCP_VENDOR */
66 { OPTION_STRING , 0x3D }, /* DHCP_CLIENT_ID */ 66//FIXME: handling of this option is not exactly correct:
67 { OPTION_STRING , 0x3d }, /* DHCP_CLIENT_ID */
67 { 0, 0 } /* zeroed terminating entry */ 68 { 0, 0 } /* zeroed terminating entry */
68}; 69};
69 70
@@ -76,10 +77,10 @@ const char dhcp_option_strings[] ALIGN1 =
76 "subnet" "\0" /* DHCP_SUBNET */ 77 "subnet" "\0" /* DHCP_SUBNET */
77 "timezone" "\0" /* DHCP_TIME_OFFSET */ 78 "timezone" "\0" /* DHCP_TIME_OFFSET */
78 "router" "\0" /* DHCP_ROUTER */ 79 "router" "\0" /* DHCP_ROUTER */
79 "timesrv" "\0" /* DHCP_TIME_SERVER */ 80// "timesrv" "\0" /* DHCP_TIME_SERVER */
80// "namesrv" "\0" /* DHCP_NAME_SERVER */ 81// "namesrv" "\0" /* DHCP_NAME_SERVER */
81 "dns" "\0" /* DHCP_DNS_SERVER */ 82 "dns" "\0" /* DHCP_DNS_SERVER */
82 "logsrv" "\0" /* DHCP_LOG_SERVER */ 83// "logsrv" "\0" /* DHCP_LOG_SERVER */
83// "cookiesrv" "\0" /* DHCP_COOKIE_SERVER */ 84// "cookiesrv" "\0" /* DHCP_COOKIE_SERVER */
84 "lprsrv" "\0" /* DHCP_LPR_SERVER */ 85 "lprsrv" "\0" /* DHCP_LPR_SERVER */
85 "hostname" "\0" /* DHCP_HOST_NAME */ 86 "hostname" "\0" /* DHCP_HOST_NAME */
@@ -90,24 +91,24 @@ const char dhcp_option_strings[] ALIGN1 =
90 "ipttl" "\0" /* DHCP_IP_TTL */ 91 "ipttl" "\0" /* DHCP_IP_TTL */
91 "mtu" "\0" /* DHCP_MTU */ 92 "mtu" "\0" /* DHCP_MTU */
92 "broadcast" "\0" /* DHCP_BROADCAST */ 93 "broadcast" "\0" /* DHCP_BROADCAST */
93 "nisdomain" "\0" /* */ 94 "nisdomain" "\0" /* DHCP_NIS_DOMAIN */
94 "nissrv" "\0" /* */ 95 "nissrv" "\0" /* DHCP_NIS_SERVER */
95 "ntpsrv" "\0" /* DHCP_NTP_SERVER */ 96 "ntpsrv" "\0" /* DHCP_NTP_SERVER */
96 "wins" "\0" /* DHCP_WINS_SERVER */ 97 "wins" "\0" /* DHCP_WINS_SERVER */
97 "lease" "\0" /* DHCP_LEASE_TIME */ 98 "lease" "\0" /* DHCP_LEASE_TIME */
98 "serverid" "\0" /* DHCP_SERVER_ID */ 99 "serverid" "\0" /* DHCP_SERVER_ID */
99 "message" "\0" /* DHCP_MESSAGE */ 100 "message" "\0" /* DHCP_ERR_MESSAGE */
100 "tftp" "\0" 101 "tftp" "\0" /* DHCP_TFTP_SERVER_NAME */
101 "bootfile" "\0" 102 "bootfile" "\0" /* DHCP_BOOT_FILE */
102// "userclass" "\0" 103// "userclass" "\0" /* DHCP_USER_CLASS */
103#if ENABLE_FEATURE_UDHCP_RFC3397 104#if ENABLE_FEATURE_UDHCP_RFC3397
104 "search" "\0" 105 "search" "\0" /* DHCP_DOMAIN_SEARCH */
105#endif 106#endif
106// "staticroutes" is only used to set udhcpc environment, it doesn't work 107// "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// in udhcpd.conf since OPTION_STATIC_ROUTES is not handled yet
108 "staticroutes" "\0" /* DHCP_STATIC_ROUTES */ 109// by "string->option" conversion code:
109 /* MSIE's "Web Proxy Autodiscovery Protocol" support */ 110 "staticroutes" "\0"/* DHCP_STATIC_ROUTES */
110 "wpad" "\0" 111 "wpad" "\0" /* DHCP_WPAD */
111 ; 112 ;
112 113
113 114
@@ -225,7 +226,7 @@ int FAST_FUNC end_option(uint8_t *optionptr)
225 226
226/* add an option string to the options */ 227/* add an option string to the options */
227/* option bytes: [code][len][data1][data2]..[dataLEN] */ 228/* option bytes: [code][len][data1][data2]..[dataLEN] */
228int FAST_FUNC add_option_string(uint8_t *optionptr, uint8_t *string) 229void FAST_FUNC add_option_string(uint8_t *optionptr, uint8_t *string)
229{ 230{
230 int end = end_option(optionptr); 231 int end = end_option(optionptr);
231 232
@@ -233,17 +234,16 @@ int FAST_FUNC add_option_string(uint8_t *optionptr, uint8_t *string)
233 if (end + string[OPT_LEN] + 2 + 1 >= DHCP_OPTIONS_BUFSIZE) { 234 if (end + string[OPT_LEN] + 2 + 1 >= DHCP_OPTIONS_BUFSIZE) {
234 bb_error_msg("option 0x%02x did not fit into the packet", 235 bb_error_msg("option 0x%02x did not fit into the packet",
235 string[OPT_CODE]); 236 string[OPT_CODE]);
236 return 0; 237 return;
237 } 238 }
238 log_option("Adding option", string); 239 log_option("Adding option", string);
239 memcpy(optionptr + end, string, string[OPT_LEN] + 2); 240 memcpy(optionptr + end, string, string[OPT_LEN] + 2);
240 optionptr[end + string[OPT_LEN] + 2] = DHCP_END; 241 optionptr[end + string[OPT_LEN] + 2] = DHCP_END;
241 return string[OPT_LEN] + 2;
242} 242}
243 243
244 244
245/* add a one to four byte option to a packet */ 245/* add a one to four byte option to a packet */
246int FAST_FUNC add_simple_option(uint8_t *optionptr, uint8_t code, uint32_t data) 246void FAST_FUNC add_simple_option(uint8_t *optionptr, uint8_t code, uint32_t data)
247{ 247{
248 const struct dhcp_option *dh; 248 const struct dhcp_option *dh;
249 249
@@ -258,10 +258,10 @@ int FAST_FUNC add_simple_option(uint8_t *optionptr, uint8_t code, uint32_t data)
258 data <<= 8 * (4 - len); 258 data <<= 8 * (4 - len);
259 /* Assignment is unaligned! */ 259 /* Assignment is unaligned! */
260 move_to_unaligned32(&option[OPT_DATA], data); 260 move_to_unaligned32(&option[OPT_DATA], data);
261 return add_option_string(optionptr, option); 261 add_option_string(optionptr, option);
262 return;
262 } 263 }
263 } 264 }
264 265
265 bb_error_msg("can't add option 0x%02x", code); 266 bb_error_msg("can't add option 0x%02x", code);
266 return 0;
267} 267}
diff --git a/networking/udhcp/options.h b/networking/udhcp/options.h
index 75087fa35..b7c9d8f8d 100644
--- a/networking/udhcp/options.h
+++ b/networking/udhcp/options.h
@@ -32,45 +32,55 @@ enum {
32/* Do not modify below here unless you know what you are doing!! */ 32/* Do not modify below here unless you know what you are doing!! */
33/*****************************************************************/ 33/*****************************************************************/
34 34
35/* DHCP protocol -- see RFC 2131 */ 35/* DHCP protocol. See RFC 2131 */
36#define DHCP_MAGIC 0x63825363 36#define DHCP_MAGIC 0x63825363
37 37
38/* DHCP option codes (partial list) */ 38/* DHCP option codes (partial list). See RFC 2132.
39 * Commented out options are handled by common option machinery,
40 * uncommented ones have spacial cases (grep for them to see).
41 */
39#define DHCP_PADDING 0x00 42#define DHCP_PADDING 0x00
40#define DHCP_SUBNET 0x01 43#define DHCP_SUBNET 0x01
41#define DHCP_TIME_OFFSET 0x02 44//#define DHCP_TIME_OFFSET 0x02 /* (localtime - UTC_time) in seconds. signed */
42#define DHCP_ROUTER 0x03 45//#define DHCP_ROUTER 0x03
43#define DHCP_TIME_SERVER 0x04 46//#define DHCP_TIME_SERVER 0x04 /* RFC 868 time server (32-bit, 0 = 1.1.1900) */
44//#define DHCP_NAME_SERVER 0x05 /* _really_ ancient kind of NS */ 47//#define DHCP_NAME_SERVER 0x05 /* IEN 116 _really_ ancient kind of NS */
45#define DHCP_DNS_SERVER 0x06 48//#define DHCP_DNS_SERVER 0x06
46#define DHCP_LOG_SERVER 0x07 49//#define DHCP_LOG_SERVER 0x07 /* port 704 UDP log (not syslog)
47//#define DHCP_COOKIE_SERVER 0x08 /* "quote of the day" server */ 50//#define DHCP_COOKIE_SERVER 0x08 /* "quote of the day" server */
48#define DHCP_LPR_SERVER 0x09 51//#define DHCP_LPR_SERVER 0x09
49#define DHCP_HOST_NAME 0x0c /* either client informs server or server gives name to client */ 52#define DHCP_HOST_NAME 0x0c /* either client informs server or server gives name to client */
50#define DHCP_BOOT_SIZE 0x0d 53//#define DHCP_BOOT_SIZE 0x0d
51#define DHCP_DOMAIN_NAME 0x0f /* server gives domain suffix */ 54//#define DHCP_DOMAIN_NAME 0x0f /* server gives domain suffix */
52#define DHCP_SWAP_SERVER 0x10 55//#define DHCP_SWAP_SERVER 0x10
53#define DHCP_ROOT_PATH 0x11 56//#define DHCP_ROOT_PATH 0x11
54#define DHCP_IP_TTL 0x17 57//#define DHCP_IP_TTL 0x17
55#define DHCP_MTU 0x1a 58//#define DHCP_MTU 0x1a
56#define DHCP_BROADCAST 0x1c 59//#define DHCP_BROADCAST 0x1c
57#define DHCP_NTP_SERVER 0x2a 60//#define DHCP_NIS_DOMAIN 0x28
58#define DHCP_WINS_SERVER 0x2c 61//#define DHCP_NIS_SERVER 0x29
62//#define DHCP_NTP_SERVER 0x2a
63//#define DHCP_WINS_SERVER 0x2c
59#define DHCP_REQUESTED_IP 0x32 /* sent by client if specific IP is wanted */ 64#define DHCP_REQUESTED_IP 0x32 /* sent by client if specific IP is wanted */
60#define DHCP_LEASE_TIME 0x33 65#define DHCP_LEASE_TIME 0x33
61#define DHCP_OPTION_OVERLOAD 0x34 66#define DHCP_OPTION_OVERLOAD 0x34
62#define DHCP_MESSAGE_TYPE 0x35 67#define DHCP_MESSAGE_TYPE 0x35
63#define DHCP_SERVER_ID 0x36 /* by default server's IP */ 68#define DHCP_SERVER_ID 0x36 /* by default server's IP */
64#define DHCP_PARAM_REQ 0x37 /* list of options client wants */ 69#define DHCP_PARAM_REQ 0x37 /* list of options client wants */
65#define DHCP_MESSAGE 0x38 /* error message when sending NAK etc */ 70//#define DHCP_ERR_MESSAGE 0x38 /* error message when sending NAK etc */
66#define DHCP_MAX_SIZE 0x39 71#define DHCP_MAX_SIZE 0x39
67//#define DHCP_T1 0x3a
68//#define DHCP_T2 0x3b
69#define DHCP_VENDOR 0x3c /* client's vendor (a string) */ 72#define DHCP_VENDOR 0x3c /* client's vendor (a string) */
70#define DHCP_CLIENT_ID 0x3d /* by default client's MAC addr, but may be arbitrarily long */ 73#define DHCP_CLIENT_ID 0x3d /* by default client's MAC addr, but may be arbitrarily long */
74//#define DHCP_TFTP_SERVER_NAME 0x42 /* same as 'sname' field */
75//#define DHCP_BOOT_FILE 0x43 /* same as 'file' field */
76//#define DHCP_USER_CLASS 0x4d /* RFC 3004. set of LASCII strings. "I am a printer" etc */
71#define DHCP_FQDN 0x51 /* client asks to update DNS to map its FQDN to its new IP */ 77#define DHCP_FQDN 0x51 /* client asks to update DNS to map its FQDN to its new IP */
72#define DHCP_STATIC_ROUTES 0x79 78//#define DHCP_DOMAIN_SEARCH 0x77 /* RFC 3397. set of ASCIZ string, DNS-style compressed */
73#define DHCP_END 0xFF 79//#define DHCP_STATIC_ROUTES 0x79 /* RFC 3442. (mask,ip,router) tuples */
80//#define DHCP_WPAD 0xfc /* MSIE's Web Proxy Autodiscovery Protocol */
81#define DHCP_END 0xff
82
83
74/* Offsets in option byte sequence */ 84/* Offsets in option byte sequence */
75#define OPT_CODE 0 85#define OPT_CODE 0
76#define OPT_LEN 1 86#define OPT_LEN 1
@@ -106,8 +116,8 @@ extern const uint8_t dhcp_option_lengths[];
106 116
107uint8_t *get_option(struct dhcp_packet *packet, int code) FAST_FUNC; 117uint8_t *get_option(struct dhcp_packet *packet, int code) FAST_FUNC;
108int end_option(uint8_t *optionptr) FAST_FUNC; 118int end_option(uint8_t *optionptr) FAST_FUNC;
109int add_option_string(uint8_t *optionptr, uint8_t *string) FAST_FUNC; 119void add_option_string(uint8_t *optionptr, uint8_t *string) FAST_FUNC;
110int add_simple_option(uint8_t *optionptr, uint8_t code, uint32_t data) FAST_FUNC; 120void add_simple_option(uint8_t *optionptr, uint8_t code, uint32_t data) FAST_FUNC;
111#if ENABLE_FEATURE_UDHCP_RFC3397 121#if ENABLE_FEATURE_UDHCP_RFC3397
112char *dname_dec(const uint8_t *cstr, int clen, const char *pre) FAST_FUNC; 122char *dname_dec(const uint8_t *cstr, int clen, const char *pre) FAST_FUNC;
113uint8_t *dname_enc(const uint8_t *cstr, int clen, const char *src, int *retlen) FAST_FUNC; 123uint8_t *dname_enc(const uint8_t *cstr, int clen, const char *src, int *retlen) FAST_FUNC;
diff --git a/networking/udhcp/script.c b/networking/udhcp/script.c
index 321b015e0..b4413fa16 100644
--- a/networking/udhcp/script.c
+++ b/networking/udhcp/script.c
@@ -244,14 +244,12 @@ static char **fill_envp(struct dhcp_packet *packet)
244 } 244 }
245 if (!(over & FILE_FIELD) && packet->file[0]) { 245 if (!(over & FILE_FIELD) && packet->file[0]) {
246 /* watch out for invalid packets */ 246 /* watch out for invalid packets */
247 packet->file[sizeof(packet->file) - 1] = '\0'; 247 *curr = xasprintf("boot_file=%."DHCP_PKT_FILE_LEN_STR"s", packet->file);
248 *curr = xasprintf("boot_file=%s", packet->file);
249 putenv(*curr++); 248 putenv(*curr++);
250 } 249 }
251 if (!(over & SNAME_FIELD) && packet->sname[0]) { 250 if (!(over & SNAME_FIELD) && packet->sname[0]) {
252 /* watch out for invalid packets */ 251 /* watch out for invalid packets */
253 packet->sname[sizeof(packet->sname) - 1] = '\0'; 252 *curr = xasprintf("sname=%."DHCP_PKT_SNAME_LEN_STR"s", packet->sname);
254 *curr = xasprintf("sname=%s", packet->sname);
255 putenv(*curr++); 253 putenv(*curr++);
256 } 254 }
257 return envp; 255 return envp;