1 //==========================================================================
5 // Stand-alone minimal BOOTP support for RedBoot
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
12 // Copyright (C) 2002, 2003 Gary Thomas
14 // eCos is free software; you can redistribute it and/or modify it under
15 // the terms of the GNU General Public License as published by the Free
16 // Software Foundation; either version 2 or (at your option) any later version.
18 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
19 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 // You should have received a copy of the GNU General Public License along
24 // with eCos; if not, write to the Free Software Foundation, Inc.,
25 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
27 // As a special exception, if other files instantiate templates or use macros
28 // or inline functions from this file, or you compile this file and link it
29 // with other works to produce a work based on this file, this file does not
30 // by itself cause the resulting work to be covered by the GNU General Public
31 // License. However the source code for this file must still be made available
32 // in accordance with section (3) of the GNU General Public License.
34 // This exception does not invalidate any other reasons why a work based on
35 // this file might be covered by the GNU General Public License.
37 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
38 // at http://sources.redhat.com/ecos/ecos-license/
39 // -------------------------------------------
40 //####ECOSGPLCOPYRIGHTEND####
41 //==========================================================================
42 //#####DESCRIPTIONBEGIN####
45 // Contributors: gthomas
50 // This code is part of RedBoot (tm).
52 //####DESCRIPTIONEND####
54 //==========================================================================
56 #include <pkgconf/isoinfra.h>
58 #include <stdlib.h> /* for rand() prototype */
60 #define SHOULD_BE_RANDOM 0x12345555
64 #include <net/bootp.h>
67 /* How many milliseconds to wait before retrying the request */
68 #define RETRY_TIME 2000
71 static bootp_header_t *bp_info;
73 #ifdef CYGSEM_REDBOOT_NETWORKING_DHCP
74 static const unsigned char dhcpCookie[] = {99,130,83,99};
75 static const unsigned char dhcpEnd[] = {255};
76 static const unsigned char dhcpDiscover[] = {53,1,1};
77 static const unsigned char dhcpRequest[] = {53,1,3};
78 static const unsigned char dhcpRequestIP[] = {50,4};
79 static const unsigned char dhcpParamRequestList[] = {55,3,1,3,6};
90 bootp_handler(udp_socket_t *skt, void *buf, int len,
91 ip_route_t *src_route, word src_port)
94 #ifdef CYGSEM_REDBOOT_NETWORKING_DHCP
95 unsigned char *p, expected = 0;
100 memset(bp_info,0,sizeof *bp_info);
101 if (len > sizeof *bp_info)
102 len = sizeof *bp_info;
103 memcpy(bp_info, b, len);
106 // Only accept pure REPLY responses
107 if (b->bp_op != BOOTREPLY)
110 // Must be sent to me, as well!
111 if (memcmp(b->bp_chaddr, __local_enet_addr, 6))
114 #ifdef CYGSEM_REDBOOT_NETWORKING_DHCP
116 if (memcmp(p, dhcpCookie, sizeof(dhcpCookie)))
120 // Find the DHCP Message Type tag
121 while (*p != TAG_DHCP_MESS_TYPE) {
123 if (p >= (unsigned char*)b + sizeof(*bp_info))
131 // The discover message has been sent, only accept an offer reply
132 if (*p == DHCP_MESS_TYPE_OFFER) {
133 dhcpState = DHCP_OFFER;
136 expected = DHCP_MESS_TYPE_OFFER;
140 // The request message has been sent, only accept an ack reply
141 if (*p == DHCP_MESS_TYPE_ACK) {
142 dhcpState = DHCP_ACK;
145 expected = DHCP_MESS_TYPE_ACK;
151 // Quitely ignore these - they indicate repeated message from server
154 // See if we've been NAK'd - if so, give up and try again
155 if (*p == DHCP_MESS_TYPE_NAK) {
156 dhcpState = DHCP_NONE;
159 diag_printf("DHCP reply: %d, not %d\n", (int)*p, (int)expected);
162 // Simple BOOTP - this is all there is!
163 memcpy(__local_ip_addr, &b->bp_yiaddr, 4);
167 #define AddOption(p,d) do {memcpy(p,d,sizeof d); p += sizeof d;} while (0)
169 static int get_xid(void)
174 static int xid = SHOULD_BE_RANDOM;
180 * Find our IP address and copy to __local_ip_addr.
181 * Return zero if successful, -1 if not.
184 __bootp_find_local_ip(bootp_header_t *info)
186 udp_socket_t udp_skt;
191 ip_addr_t saved_ip_addr;
192 #ifdef CYGSEM_REDBOOT_NETWORKING_DHCP
199 #ifdef CYGSEM_REDBOOT_NETWORKING_DHCP
200 dhcpState = DHCP_NONE;
203 // Where we want the results saved
205 // Preserve any IP address we currently have, just in case
206 memcpy(saved_ip_addr, __local_ip_addr, sizeof(__local_ip_addr));
208 // fill out route for a broadcast
213 r.enet_addr[0] = 255;
214 r.enet_addr[1] = 255;
215 r.enet_addr[2] = 255;
216 r.enet_addr[3] = 255;
217 r.enet_addr[4] = 255;
218 r.enet_addr[5] = 255;
220 // setup a socket listener for bootp replies
221 __udp_install_listener(&udp_skt, IPPORT_BOOTPC, bootp_handler);
227 // Build up the BOOTP/DHCP request
228 memset(&b, 0, sizeof(b));
229 b.bp_op = BOOTREQUEST;
230 b.bp_htype = HTYPE_ETHERNET;
232 b.bp_xid = get_xid();
233 memcpy(b.bp_chaddr, __local_enet_addr, 6);
234 memset(__local_ip_addr, 0, sizeof(__local_ip_addr));
236 #ifdef CYGSEM_REDBOOT_NETWORKING_DHCP
241 AddOption(p,dhcpCookie);
242 AddOption(p,dhcpDiscover);
243 AddOption(p,dhcpParamRequestList);
244 AddOption(p,dhcpEnd);
245 dhcpState = DHCP_DISCOVER;
250 b.bp_xid = bp_info->bp_xid; // Match what server sent
251 AddOption(p,dhcpCookie);
252 AddOption(p,dhcpRequest);
253 AddOption(p,dhcpRequestIP);
254 memcpy(p, &bp_info->bp_yiaddr, 4); p += 4; // Ask for the address just given
255 AddOption(p,dhcpParamRequestList);
256 AddOption(p,dhcpEnd);
257 dhcpState = DHCP_REQUEST;
258 memset(&b.bp_yiaddr, 0xFF, 4);
259 memset(&b.bp_siaddr, 0xFF, 4);
260 memset(&b.bp_yiaddr, 0x00, 4);
261 memset(&b.bp_siaddr, 0x00, 4);
264 // Ignore these states (they won't happen)
268 // Some servers insist on a minimum amount of "vendor" data
269 if (p < &b.bp_vend[BP_MIN_VEND_SIZE]) p = &b.bp_vend[BP_MIN_VEND_SIZE];
270 txSize = p - (unsigned char*)&b;
271 oldState = dhcpState;
276 __udp_send((char *)&b, txSize, &r, IPPORT_BOOTPS, IPPORT_BOOTPC);
278 // If we're retrying, inform the user
279 if (retry == (MAX_RETRIES - 1))
280 diag_printf("... waiting for BOOTP information\n");
284 #ifdef CYGSEM_REDBOOT_NETWORKING_DHCP
285 if (dhcpState != oldState) {
286 if (dhcpState == DHCP_ACK) {
289 // Address information has now arrived!
290 memcpy(__local_ip_addr, &bp_info->bp_yiaddr, 4);
291 #ifdef CYGSEM_REDBOOT_NETWORKING_USE_GATEWAY
292 memcpy(__local_ip_gate, &bp_info->bp_giaddr, 4);
294 p = bp_info->bp_vend+4;
295 end = (unsigned char *)bp_info+sizeof(*bp_info);
297 unsigned char tag = *p;
306 #ifdef CYGSEM_REDBOOT_NETWORKING_USE_GATEWAY
307 case TAG_SUBNET_MASK: // subnet mask
308 memcpy(__local_ip_mask,p,4);
310 case TAG_GATEWAY: // router
311 memcpy(__local_ip_gate,p,4);
314 #ifdef CYGPKG_REDBOOT_NETWORKING_DNS
315 case TAG_DOMAIN_SERVER:
316 // diag_printf(" DNS server found!\n");
317 memcpy(&__bootp_dns_addr, p, 4);
320 #ifdef CYGPKG_REDBOOT_NETWORKING_DNS_DHCP_DOMAIN
321 case TAG_DOMAIN_NAME:
322 if(optlen < sizeof(__bootp_dns_domain)) {
323 memcpy(__bootp_dns_domain, p, optlen);
324 __bootp_dns_domain[optlen] = '\0';
325 __bootp_dns_domain_set = 1;
327 diag_printf("DNS domain name too long\n");
330 #endif //CYGPKG_REDBOOT_NETWORKING_DNS_DHCP_DOMAIN
331 #endif //CYGPKG_REDBOOT_NETWORKING_DNS
338 __udp_remove_listener(IPPORT_BOOTPC);
341 break; // State changed, handle it
345 // All done, if address response has arrived
346 if (__local_ip_addr[0] || __local_ip_addr[1] ||
347 __local_ip_addr[2] || __local_ip_addr[3]) {
349 __udp_remove_listener(IPPORT_BOOTPC);
353 if (retry < MAX_RETRIES) {
355 // The user typed ^C on the console
359 start--; /* account for time spent in _rb_break() */
361 } while ((int)(MS_TICKS_DELAY() - start) < RETRY_TIME);
362 } while (!abort && (retry-- > 0));
365 __udp_remove_listener(IPPORT_BOOTPC);
366 // Restore any previous IP address
367 memcpy(__local_ip_addr, saved_ip_addr, sizeof(__local_ip_addr));