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, char *buf, int len,
91 ip_route_t *src_route, word src_port)
94 #ifdef CYGSEM_REDBOOT_NETWORKING_DHCP
95 unsigned char *p, expected = 0;
98 b = (bootp_header_t *)buf;
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)
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
200 #ifdef CYGSEM_REDBOOT_NETWORKING_DHCP
201 dhcpState = DHCP_NONE;
204 // Where we want the results saved
206 // Preserve any IP address we currently have, just in case
207 memcpy(saved_ip_addr, __local_ip_addr, sizeof(__local_ip_addr));
209 // fill out route for a broadcast
214 r.enet_addr[0] = 255;
215 r.enet_addr[1] = 255;
216 r.enet_addr[2] = 255;
217 r.enet_addr[3] = 255;
218 r.enet_addr[4] = 255;
219 r.enet_addr[5] = 255;
221 // setup a socket listener for bootp replies
222 __udp_install_listener(&udp_skt, IPPORT_BOOTPC, bootp_handler);
228 // Build up the BOOTP/DHCP request
229 memset(&b, 0, sizeof(b));
230 b.bp_op = BOOTREQUEST;
231 b.bp_htype = HTYPE_ETHERNET;
233 b.bp_xid = get_xid();
234 memcpy(b.bp_chaddr, __local_enet_addr, 6);
235 memset(__local_ip_addr, 0, sizeof(__local_ip_addr));
237 #ifdef CYGSEM_REDBOOT_NETWORKING_DHCP
242 AddOption(p,dhcpCookie);
243 AddOption(p,dhcpDiscover);
244 AddOption(p,dhcpParamRequestList);
245 AddOption(p,dhcpEnd);
246 dhcpState = DHCP_DISCOVER;
251 b.bp_xid = bp_info->bp_xid; // Match what server sent
252 AddOption(p,dhcpCookie);
253 AddOption(p,dhcpRequest);
254 AddOption(p,dhcpRequestIP);
255 memcpy(p, &bp_info->bp_yiaddr, 4); p += 4; // Ask for the address just given
256 AddOption(p,dhcpParamRequestList);
257 AddOption(p,dhcpEnd);
258 dhcpState = DHCP_REQUEST;
259 memset(&b.bp_yiaddr, 0xFF, 4);
260 memset(&b.bp_siaddr, 0xFF, 4);
261 memset(&b.bp_yiaddr, 0x00, 4);
262 memset(&b.bp_siaddr, 0x00, 4);
265 // Ignore these states (they won't happen)
269 // Some servers insist on a minimum amount of "vendor" data
270 if (p < &b.bp_vend[BP_MIN_VEND_SIZE]) p = &b.bp_vend[BP_MIN_VEND_SIZE];
271 txSize = p - (unsigned char*)&b;
272 oldState = dhcpState;
277 __udp_send((char *)&b, txSize, &r, IPPORT_BOOTPS, IPPORT_BOOTPC);
279 // If we're retrying, inform the user
280 if (retry == (MAX_RETRIES-1))
281 diag_printf("... waiting for BOOTP information\n");
285 #ifdef CYGSEM_REDBOOT_NETWORKING_DHCP
286 if (dhcpState != oldState) {
287 if (dhcpState == DHCP_ACK) {
290 // Address information has now arrived!
291 memcpy(__local_ip_addr, &bp_info->bp_yiaddr, 4);
292 #ifdef CYGSEM_REDBOOT_NETWORKING_USE_GATEWAY
293 memcpy(__local_ip_gate, &bp_info->bp_giaddr, 4);
295 p = bp_info->bp_vend+4;
296 end = (unsigned char *)bp_info+sizeof(*bp_info);
298 unsigned char tag = *p;
307 #ifdef CYGSEM_REDBOOT_NETWORKING_USE_GATEWAY
308 case TAG_SUBNET_MASK: // subnet mask
309 memcpy(__local_ip_mask,p,4);
311 case TAG_GATEWAY: // router
312 memcpy(__local_ip_gate,p,4);
315 #ifdef CYGPKG_REDBOOT_NETWORKING_DNS
316 case TAG_DOMAIN_SERVER:
317 // diag_printf(" DNS server found!\n");
318 memcpy(&__bootp_dns_addr, p, 4);
328 __udp_remove_listener(IPPORT_BOOTPC);
331 break; // State changed, handle it
335 // All done, if address response has arrived
336 if (__local_ip_addr[0] || __local_ip_addr[1] ||
337 __local_ip_addr[2] || __local_ip_addr[3]) {
339 __udp_remove_listener(IPPORT_BOOTPC);
344 // The user typed ^C on the console
348 start--; /* account for time spent in _rb_break() */
349 } while ((int)(MS_TICKS_DELAY() - start) < RETRY_TIME);
350 } while (!abort && (retry-- > 0));
353 __udp_remove_listener(IPPORT_BOOTPC);
354 // Restore any previous IP address
355 memcpy(__local_ip_addr, saved_ip_addr, sizeof(__local_ip_addr));