wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Based on LiMon - BOOTP. |
| 3 | * |
| 4 | * Copyright 1994, 1995, 2000 Neil Russell. |
| 5 | * (See License) |
| 6 | * Copyright 2000 Roland Borde |
| 7 | * Copyright 2000 Paolo Scaffardi |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 8 | * Copyright 2000-2004 Wolfgang Denk, wd@denx.de |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #if 0 |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 12 | #define DEBUG 1 /* general debug */ |
| 13 | #define DEBUG_BOOTP_EXT 1 /* Debug received vendor fields */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 14 | #endif |
| 15 | |
| 16 | #ifdef DEBUG_BOOTP_EXT |
| 17 | #define debug_ext(fmt,args...) printf (fmt ,##args) |
| 18 | #else |
| 19 | #define debug_ext(fmt,args...) |
| 20 | #endif |
| 21 | |
| 22 | #include <common.h> |
| 23 | #include <command.h> |
| 24 | #include <net.h> |
| 25 | #include "bootp.h" |
| 26 | #include "tftp.h" |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 27 | #include "nfs.h" |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 28 | #ifdef CONFIG_STATUS_LED |
| 29 | #include <status_led.h> |
| 30 | #endif |
| 31 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 32 | #define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 33 | |
| 34 | #if (CONFIG_COMMANDS & CFG_CMD_NET) |
| 35 | |
| 36 | #define TIMEOUT 5 /* Seconds before trying BOOTP again */ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 37 | #ifndef CONFIG_NET_RETRY_COUNT |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 38 | # define TIMEOUT_COUNT 5 /* # of timeouts before giving up */ |
| 39 | #else |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 40 | # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 41 | #endif |
| 42 | |
| 43 | #define PORT_BOOTPS 67 /* BOOTP server UDP port */ |
| 44 | #define PORT_BOOTPC 68 /* BOOTP client UDP port */ |
| 45 | |
| 46 | #ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 47 | #define CONFIG_DHCP_MIN_EXT_LEN 64 |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 48 | #endif |
| 49 | |
| 50 | ulong BootpID; |
| 51 | int BootpTry; |
| 52 | #ifdef CONFIG_BOOTP_RANDOM_DELAY |
| 53 | ulong seed1, seed2; |
| 54 | #endif |
| 55 | |
| 56 | #if (CONFIG_COMMANDS & CFG_CMD_DHCP) |
| 57 | dhcp_state_t dhcp_state = INIT; |
wdenk | 4e112c1 | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 58 | unsigned long dhcp_leasetime = 0; |
stroese | 5fa6e90 | 2003-04-10 13:26:44 +0000 | [diff] [blame] | 59 | IPaddr_t NetDHCPServerIP = 0; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 60 | static void DhcpHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len); |
| 61 | |
| 62 | /* For Debug */ |
wdenk | b02744a | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 63 | #if 0 |
| 64 | static char *dhcpmsg2str(int type) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 65 | { |
| 66 | switch (type) { |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 67 | case 1: return "DHCPDISCOVER"; break; |
| 68 | case 2: return "DHCPOFFER"; break; |
| 69 | case 3: return "DHCPREQUEST"; break; |
| 70 | case 4: return "DHCPDECLINE"; break; |
| 71 | case 5: return "DHCPACK"; break; |
| 72 | case 6: return "DHCPNACK"; break; |
| 73 | case 7: return "DHCPRELEASE"; break; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 74 | default: return "UNKNOWN/INVALID MSG TYPE"; break; |
| 75 | } |
| 76 | } |
wdenk | b02744a | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 77 | #endif |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 78 | |
| 79 | #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_VENDOREX) |
| 80 | extern u8 *dhcp_vendorex_prep (u8 *e); /*rtn new e after add own opts. */ |
| 81 | extern u8 *dhcp_vendorex_proc (u8 *e); /*rtn next e if mine,else NULL */ |
| 82 | #endif |
| 83 | |
| 84 | #endif /* CFG_CMD_DHCP */ |
| 85 | |
| 86 | static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len) |
| 87 | { |
| 88 | Bootp_t *bp = (Bootp_t *) pkt; |
| 89 | int retval = 0; |
| 90 | |
| 91 | if (dest != PORT_BOOTPC || src != PORT_BOOTPS) |
| 92 | retval = -1; |
| 93 | else if (len < sizeof (Bootp_t) - OPT_SIZE) |
| 94 | retval = -2; |
| 95 | else if (bp->bp_op != OP_BOOTREQUEST && |
| 96 | bp->bp_op != OP_BOOTREPLY && |
| 97 | bp->bp_op != DHCP_OFFER && |
| 98 | bp->bp_op != DHCP_ACK && |
| 99 | bp->bp_op != DHCP_NAK ) { |
| 100 | retval = -3; |
| 101 | } |
| 102 | else if (bp->bp_htype != HWT_ETHER) |
| 103 | retval = -4; |
| 104 | else if (bp->bp_hlen != HWL_ETHER) |
| 105 | retval = -5; |
| 106 | else if (NetReadLong((ulong*)&bp->bp_id) != BootpID) { |
| 107 | retval = -6; |
| 108 | } |
| 109 | |
| 110 | debug ("Filtering pkt = %d\n", retval); |
| 111 | |
| 112 | return retval; |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet |
| 117 | */ |
wdenk | b02744a | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 118 | static void BootpCopyNetParams(Bootp_t *bp) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 119 | { |
| 120 | NetCopyIP(&NetOurIP, &bp->bp_yiaddr); |
| 121 | NetCopyIP(&NetServerIP, &bp->bp_siaddr); |
| 122 | memcpy (NetServerEther, ((Ethernet_t *)NetRxPkt)->et_src, 6); |
| 123 | copy_filename (BootFile, bp->bp_file, sizeof(BootFile)); |
| 124 | |
| 125 | debug ("Bootfile: %s\n", BootFile); |
| 126 | |
| 127 | /* Propagate to environment: |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 128 | * don't delete exising entry when BOOTP / DHCP reply does |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 129 | * not contain a new value |
| 130 | */ |
| 131 | if (*BootFile) { |
| 132 | setenv ("bootfile", BootFile); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | static int truncate_sz (const char *name, int maxlen, int curlen) |
| 137 | { |
| 138 | if (curlen >= maxlen) { |
| 139 | printf("*** WARNING: %s is too long (%d - max: %d) - truncated\n", |
| 140 | name, curlen, maxlen); |
| 141 | curlen = maxlen - 1; |
| 142 | } |
| 143 | return (curlen); |
| 144 | } |
| 145 | |
| 146 | #if !(CONFIG_COMMANDS & CFG_CMD_DHCP) |
| 147 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 148 | static void BootpVendorFieldProcess (u8 * ext) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 149 | { |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 150 | int size = *(ext + 1); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 151 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 152 | debug_ext ("[BOOTP] Processing extension %d... (%d bytes)\n", *ext, |
| 153 | *(ext + 1)); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 154 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 155 | NetBootFileSize = 0; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 156 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 157 | switch (*ext) { |
| 158 | /* Fixed length fields */ |
| 159 | case 1: /* Subnet mask */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 160 | if (NetOurSubnetMask == 0) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 161 | NetCopyIP (&NetOurSubnetMask, (IPaddr_t *) (ext + 2)); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 162 | break; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 163 | case 2: /* Time offset - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 164 | break; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 165 | /* Variable length fields */ |
| 166 | case 3: /* Gateways list */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 167 | if (NetOurGatewayIP == 0) { |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 168 | NetCopyIP (&NetOurGatewayIP, (IPaddr_t *) (ext + 2)); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 169 | } |
| 170 | break; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 171 | case 4: /* Time server - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 172 | break; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 173 | case 5: /* IEN-116 name server - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 174 | break; |
| 175 | case 6: |
| 176 | if (NetOurDNSIP == 0) { |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 177 | NetCopyIP (&NetOurDNSIP, (IPaddr_t *) (ext + 2)); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 178 | } |
stroese | e0aadfb | 2003-08-28 14:17:32 +0000 | [diff] [blame] | 179 | #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_DNS2) |
| 180 | if ((NetOurDNS2IP == 0) && (size > 4)) { |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 181 | NetCopyIP (&NetOurDNS2IP, (IPaddr_t *) (ext + 2 + 4)); |
stroese | e0aadfb | 2003-08-28 14:17:32 +0000 | [diff] [blame] | 182 | } |
| 183 | #endif |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 184 | break; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 185 | case 7: /* Log server - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 186 | break; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 187 | case 8: /* Cookie/Quote server - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 188 | break; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 189 | case 9: /* LPR server - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 190 | break; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 191 | case 10: /* Impress server - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 192 | break; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 193 | case 11: /* RPL server - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 194 | break; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 195 | case 12: /* Host name */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 196 | if (NetOurHostName[0] == 0) { |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 197 | size = truncate_sz ("Host Name", sizeof (NetOurHostName), size); |
| 198 | memcpy (&NetOurHostName, ext + 2, size); |
| 199 | NetOurHostName[size] = 0; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 200 | } |
| 201 | break; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 202 | case 13: /* Boot file size */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 203 | if (size == 2) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 204 | NetBootFileSize = ntohs (*(ushort *) (ext + 2)); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 205 | else if (size == 4) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 206 | NetBootFileSize = ntohl (*(ulong *) (ext + 2)); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 207 | break; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 208 | case 14: /* Merit dump file - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 209 | break; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 210 | case 15: /* Domain name - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 211 | break; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 212 | case 16: /* Swap server - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 213 | break; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 214 | case 17: /* Root path */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 215 | if (NetOurRootPath[0] == 0) { |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 216 | size = truncate_sz ("Root Path", sizeof (NetOurRootPath), size); |
| 217 | memcpy (&NetOurRootPath, ext + 2, size); |
| 218 | NetOurRootPath[size] = 0; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 219 | } |
| 220 | break; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 221 | case 18: /* Extension path - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 222 | /* |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 223 | * This can be used to send the information of the |
| 224 | * vendor area in another file that the client can |
| 225 | * access via TFTP. |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 226 | */ |
| 227 | break; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 228 | /* IP host layer fields */ |
| 229 | case 40: /* NIS Domain name */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 230 | if (NetOurNISDomain[0] == 0) { |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 231 | size = truncate_sz ("NIS Domain Name", sizeof (NetOurNISDomain), size); |
| 232 | memcpy (&NetOurNISDomain, ext + 2, size); |
| 233 | NetOurNISDomain[size] = 0; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 234 | } |
| 235 | break; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 236 | /* Application layer fields */ |
| 237 | case 43: /* Vendor specific info - Not yet supported */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 238 | /* |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 239 | * Binary information to exchange specific |
| 240 | * product information. |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 241 | */ |
| 242 | break; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 243 | /* Reserved (custom) fields (128..254) */ |
| 244 | } |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 245 | } |
| 246 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 247 | static void BootpVendorProcess (u8 * ext, int size) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 248 | { |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 249 | u8 *end = ext + size; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 250 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 251 | debug_ext ("[BOOTP] Checking extension (%d bytes)...\n", size); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 252 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 253 | while ((ext < end) && (*ext != 0xff)) { |
| 254 | if (*ext == 0) { |
| 255 | ext++; |
| 256 | } else { |
| 257 | u8 *opt = ext; |
| 258 | |
| 259 | ext += ext[1] + 2; |
| 260 | if (ext <= end) |
| 261 | BootpVendorFieldProcess (opt); |
| 262 | } |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 263 | } |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 264 | |
| 265 | #ifdef DEBUG_BOOTP_EXT |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 266 | printf ("[BOOTP] Received fields: \n"); |
| 267 | if (NetOurSubnetMask) { |
| 268 | puts ("NetOurSubnetMask : "); |
| 269 | print_IPaddr (NetOurSubnetMask); |
| 270 | putc ('\n'); |
| 271 | } |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 272 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 273 | if (NetOurGatewayIP) { |
| 274 | puts ("NetOurGatewayIP : "); |
| 275 | print_IPaddr (NetOurGatewayIP); |
| 276 | putc ('\n'); |
| 277 | } |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 278 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 279 | if (NetBootFileSize) { |
| 280 | printf ("NetBootFileSize : %d\n", NetBootFileSize); |
| 281 | } |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 282 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 283 | if (NetOurHostName[0]) { |
| 284 | printf ("NetOurHostName : %s\n", NetOurHostName); |
| 285 | } |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 286 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 287 | if (NetOurRootPath[0]) { |
| 288 | printf ("NetOurRootPath : %s\n", NetOurRootPath); |
| 289 | } |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 290 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 291 | if (NetOurNISDomain[0]) { |
| 292 | printf ("NetOurNISDomain : %s\n", NetOurNISDomain); |
| 293 | } |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 294 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 295 | if (NetBootFileSize) { |
| 296 | printf ("NetBootFileSize: %d\n", NetBootFileSize); |
| 297 | } |
| 298 | #endif /* DEBUG_BOOTP_EXT */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 299 | } |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 300 | /* |
| 301 | * Handle a BOOTP received packet. |
| 302 | */ |
| 303 | static void |
| 304 | BootpHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len) |
| 305 | { |
| 306 | Bootp_t *bp; |
| 307 | char *s; |
| 308 | |
| 309 | debug ("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%d)\n", |
| 310 | src, dest, len, sizeof (Bootp_t)); |
| 311 | |
| 312 | bp = (Bootp_t *)pkt; |
| 313 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 314 | if (BootpCheckPkt(pkt, dest, src, len)) /* Filter out pkts we don't want */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 315 | return; |
| 316 | |
| 317 | /* |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 318 | * Got a good BOOTP reply. Copy the data into our variables. |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 319 | */ |
| 320 | #ifdef CONFIG_STATUS_LED |
| 321 | status_led_set (STATUS_LED_BOOT, STATUS_LED_OFF); |
| 322 | #endif |
| 323 | |
| 324 | BootpCopyNetParams(bp); /* Store net parameters from reply */ |
| 325 | |
| 326 | /* Retrieve extended information (we must parse the vendor area) */ |
| 327 | if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC)) |
| 328 | BootpVendorProcess(&bp->bp_vend[4], len); |
| 329 | |
| 330 | NetSetTimeout(0, (thand_f *)0); |
| 331 | |
| 332 | debug ("Got good BOOTP\n"); |
| 333 | |
wdenk | 6ea1cf0 | 2004-02-27 08:20:54 +0000 | [diff] [blame] | 334 | if ((s = getenv("autoload")) != NULL) { |
| 335 | if (*s == 'n') { |
| 336 | /* |
| 337 | * Just use BOOTP to configure system; |
| 338 | * Do not use TFTP to load the bootfile. |
| 339 | */ |
| 340 | NetState = NETLOOP_SUCCESS; |
| 341 | return; |
wdenk | 00fe161 | 2004-03-14 00:07:33 +0000 | [diff] [blame^] | 342 | #if (CONFIG_COMMANDS & CFG_CMD_NFS) |
wdenk | 6ea1cf0 | 2004-02-27 08:20:54 +0000 | [diff] [blame] | 343 | } else if (strcmp(s, "NFS") == 0) { |
| 344 | /* |
| 345 | * Use NFS to load the bootfile. |
| 346 | */ |
| 347 | NfsStart(); |
| 348 | return; |
wdenk | 00fe161 | 2004-03-14 00:07:33 +0000 | [diff] [blame^] | 349 | #endif |
wdenk | 6ea1cf0 | 2004-02-27 08:20:54 +0000 | [diff] [blame] | 350 | } |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 351 | } |
| 352 | |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 353 | TftpStart(); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 354 | } |
| 355 | #endif /* !CFG_CMD_DHCP */ |
| 356 | |
| 357 | /* |
| 358 | * Timeout on BOOTP/DHCP request. |
| 359 | */ |
| 360 | static void |
| 361 | BootpTimeout(void) |
| 362 | { |
| 363 | if (BootpTry >= TIMEOUT_COUNT) { |
| 364 | puts ("\nRetry count exceeded; starting again\n"); |
| 365 | NetStartAgain (); |
| 366 | } else { |
| 367 | NetSetTimeout (TIMEOUT * CFG_HZ, BootpTimeout); |
| 368 | BootpRequest (); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | /* |
| 373 | * Initialize BOOTP extension fields in the request. |
| 374 | */ |
| 375 | #if (CONFIG_COMMANDS & CFG_CMD_DHCP) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 376 | static int DhcpExtended (u8 * e, int message_type, IPaddr_t ServerID, IPaddr_t RequestedIP) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 377 | { |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 378 | u8 *start = e; |
| 379 | u8 *cnt; |
| 380 | |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 381 | #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_VENDOREX) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 382 | u8 *x; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 383 | #endif |
stroese | e0aadfb | 2003-08-28 14:17:32 +0000 | [diff] [blame] | 384 | #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_SEND_HOSTNAME) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 385 | uchar *hostname; |
stroese | e0aadfb | 2003-08-28 14:17:32 +0000 | [diff] [blame] | 386 | #endif |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 387 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 388 | *e++ = 99; /* RFC1048 Magic Cookie */ |
| 389 | *e++ = 130; |
| 390 | *e++ = 83; |
| 391 | *e++ = 99; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 392 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 393 | *e++ = 53; /* DHCP Message Type */ |
| 394 | *e++ = 1; |
| 395 | *e++ = message_type; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 396 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 397 | *e++ = 57; /* Maximum DHCP Message Size */ |
| 398 | *e++ = 2; |
| 399 | *e++ = (576 - 312 + OPT_SIZE) >> 8; |
| 400 | *e++ = (576 - 312 + OPT_SIZE) & 0xff; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 401 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 402 | if (ServerID) { |
| 403 | int tmp = ntohl (ServerID); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 404 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 405 | *e++ = 54; /* ServerID */ |
| 406 | *e++ = 4; |
| 407 | *e++ = tmp >> 24; |
| 408 | *e++ = tmp >> 16; |
| 409 | *e++ = tmp >> 8; |
| 410 | *e++ = tmp & 0xff; |
| 411 | } |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 412 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 413 | if (RequestedIP) { |
| 414 | int tmp = ntohl (RequestedIP); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 415 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 416 | *e++ = 50; /* Requested IP */ |
| 417 | *e++ = 4; |
| 418 | *e++ = tmp >> 24; |
| 419 | *e++ = tmp >> 16; |
| 420 | *e++ = tmp >> 8; |
| 421 | *e++ = tmp & 0xff; |
| 422 | } |
stroese | e0aadfb | 2003-08-28 14:17:32 +0000 | [diff] [blame] | 423 | #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_SEND_HOSTNAME) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 424 | if ((hostname = getenv ("hostname"))) { |
| 425 | int hostnamelen = strlen (hostname); |
| 426 | |
| 427 | *e++ = 12; /* Hostname */ |
| 428 | *e++ = hostnamelen; |
| 429 | memcpy (e, hostname, hostnamelen); |
| 430 | e += hostnamelen; |
| 431 | } |
stroese | e0aadfb | 2003-08-28 14:17:32 +0000 | [diff] [blame] | 432 | #endif |
| 433 | |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 434 | #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_VENDOREX) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 435 | if ((x = dhcp_vendorex_prep (e))) |
| 436 | return x - start; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 437 | #endif |
| 438 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 439 | *e++ = 55; /* Parameter Request List */ |
| 440 | cnt = e++; /* Pointer to count of requested items */ |
| 441 | *cnt = 0; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 442 | #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_SUBNETMASK) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 443 | *e++ = 1; /* Subnet Mask */ |
| 444 | *cnt += 1; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 445 | #endif |
| 446 | #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_GATEWAY) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 447 | *e++ = 3; /* Router Option */ |
| 448 | *cnt += 1; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 449 | #endif |
| 450 | #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_DNS) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 451 | *e++ = 6; /* DNS Server(s) */ |
| 452 | *cnt += 1; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 453 | #endif |
| 454 | #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_HOSTNAME) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 455 | *e++ = 12; /* Hostname */ |
| 456 | *cnt += 1; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 457 | #endif |
| 458 | #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_BOOTFILESIZE) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 459 | *e++ = 13; /* Boot File Size */ |
| 460 | *cnt += 1; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 461 | #endif |
| 462 | #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_BOOTPATH) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 463 | *e++ = 17; /* Boot path */ |
| 464 | *cnt += 1; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 465 | #endif |
| 466 | #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_NISDOMAIN) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 467 | *e++ = 40; /* NIS Domain name request */ |
| 468 | *cnt += 1; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 469 | #endif |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 470 | *e++ = 255; /* End of the list */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 471 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 472 | /* Pad to minimal length */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 473 | #ifdef CONFIG_DHCP_MIN_EXT_LEN |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 474 | while ((e - start) <= CONFIG_DHCP_MIN_EXT_LEN) |
| 475 | *e++ = 0; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 476 | #endif |
| 477 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 478 | return e - start; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | #else /* CFG_CMD_DHCP */ |
| 482 | /* |
| 483 | * Warning: no field size check - change CONFIG_BOOTP_MASK at your own risk! |
| 484 | */ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 485 | static int BootpExtended (u8 * e) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 486 | { |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 487 | u8 *start = e; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 488 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 489 | *e++ = 99; /* RFC1048 Magic Cookie */ |
| 490 | *e++ = 130; |
| 491 | *e++ = 83; |
| 492 | *e++ = 99; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 493 | |
| 494 | #if (CONFIG_COMMANDS & CFG_CMD_DHCP) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 495 | *e++ = 53; /* DHCP Message Type */ |
| 496 | *e++ = 1; |
| 497 | *e++ = DHCP_DISCOVER; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 498 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 499 | *e++ = 57; /* Maximum DHCP Message Size */ |
| 500 | *e++ = 2; |
| 501 | *e++ = (576 - 312 + OPT_SIZE) >> 16; |
| 502 | *e++ = (576 - 312 + OPT_SIZE) & 0xff; |
| 503 | #endif /* CFG_CMD_DHCP */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 504 | |
| 505 | #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_SUBNETMASK) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 506 | *e++ = 1; /* Subnet mask request */ |
| 507 | *e++ = 4; |
| 508 | e += 4; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 509 | #endif |
| 510 | |
| 511 | #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_GATEWAY) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 512 | *e++ = 3; /* Default gateway request */ |
| 513 | *e++ = 4; |
| 514 | e += 4; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 515 | #endif |
| 516 | |
| 517 | #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_DNS) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 518 | *e++ = 6; /* Domain Name Server */ |
| 519 | *e++ = 4; |
| 520 | e += 4; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 521 | #endif |
| 522 | |
| 523 | #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_HOSTNAME) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 524 | *e++ = 12; /* Host name request */ |
| 525 | *e++ = 32; |
| 526 | e += 32; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 527 | #endif |
| 528 | |
| 529 | #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_BOOTFILESIZE) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 530 | *e++ = 13; /* Boot file size */ |
| 531 | *e++ = 2; |
| 532 | e += 2; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 533 | #endif |
| 534 | |
| 535 | #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_BOOTPATH) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 536 | *e++ = 17; /* Boot path */ |
| 537 | *e++ = 32; |
| 538 | e += 32; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 539 | #endif |
| 540 | |
| 541 | #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_NISDOMAIN) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 542 | *e++ = 40; /* NIS Domain name request */ |
| 543 | *e++ = 32; |
| 544 | e += 32; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 545 | #endif |
| 546 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 547 | *e++ = 255; /* End of the list */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 548 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 549 | return e - start; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 550 | } |
| 551 | #endif /* CFG_CMD_DHCP */ |
| 552 | |
| 553 | void |
| 554 | BootpRequest (void) |
| 555 | { |
| 556 | volatile uchar *pkt, *iphdr; |
| 557 | Bootp_t *bp; |
| 558 | int ext_len, pktlen, iplen; |
| 559 | |
| 560 | #if (CONFIG_COMMANDS & CFG_CMD_DHCP) |
| 561 | dhcp_state = INIT; |
| 562 | #endif |
| 563 | |
| 564 | #ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */ |
| 565 | unsigned char bi_enetaddr[6]; |
| 566 | int reg; |
| 567 | char *e,*s; |
| 568 | uchar tmp[64]; |
| 569 | ulong tst1, tst2, sum, m_mask, m_value = 0; |
| 570 | |
| 571 | if (BootpTry ==0) { |
| 572 | /* get our mac */ |
| 573 | reg = getenv_r ("ethaddr", tmp, sizeof(tmp)); |
| 574 | s = (reg > 0) ? tmp : NULL; |
| 575 | |
| 576 | for (reg=0; reg<6; ++reg) { |
| 577 | bi_enetaddr[reg] = s ? simple_strtoul(s, &e, 16) : 0; |
| 578 | if (s) { |
| 579 | s = (*e) ? e+1 : e; |
| 580 | } |
| 581 | } |
| 582 | #ifdef DEBUG |
| 583 | printf("BootpRequest => Our Mac: "); |
| 584 | for (reg=0; reg<6; reg++) { |
| 585 | printf ("%x%c", |
| 586 | bi_enetaddr[reg], |
| 587 | reg==5 ? '\n' : ':'); |
| 588 | } |
| 589 | #endif /* DEBUG */ |
| 590 | |
| 591 | /* Mac-Manipulation 2 get seed1 */ |
| 592 | tst1=0; |
| 593 | tst2=0; |
| 594 | for (reg=2; reg<6; reg++) { |
| 595 | tst1 = tst1 << 8; |
| 596 | tst1 = tst1 | bi_enetaddr[reg]; |
| 597 | } |
| 598 | for (reg=0; reg<2; reg++) { |
| 599 | tst2 = tst2 | bi_enetaddr[reg]; |
| 600 | tst2 = tst2 << 8; |
| 601 | } |
| 602 | |
| 603 | seed1 = tst1^tst2; |
| 604 | |
| 605 | /* Mirror seed1*/ |
| 606 | m_mask=0x1; |
| 607 | for (reg=1;reg<=32;reg++) { |
| 608 | m_value |= (m_mask & seed1); |
| 609 | seed1 = seed1 >> 1; |
| 610 | m_value = m_value << 1; |
| 611 | } |
| 612 | seed1 = m_value; |
| 613 | seed2 = 0xB78D0945; |
| 614 | } |
| 615 | |
| 616 | /* Random Number Generator */ |
| 617 | |
| 618 | for (reg=0;reg<=0;reg++) { |
| 619 | sum = seed1 + seed2; |
| 620 | if (sum < seed1 || sum < seed2) |
| 621 | sum++; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 622 | seed2 = seed1; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 623 | seed1 = sum; |
| 624 | |
| 625 | if (BootpTry<=2) { /* Start with max 1024 * 1ms */ |
| 626 | sum = sum >> (22-BootpTry); |
| 627 | } else { /*After 3rd BOOTP request max 8192 * 1ms */ |
| 628 | sum = sum >> 19; |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | printf ("Random delay: %ld ms...\n", sum); |
| 633 | for (reg=0; reg <sum; reg++) { |
| 634 | udelay(1000); /*Wait 1ms*/ |
| 635 | } |
| 636 | #endif /* CONFIG_BOOTP_RANDOM_DELAY */ |
| 637 | |
| 638 | printf("BOOTP broadcast %d\n", ++BootpTry); |
| 639 | pkt = NetTxPacket; |
| 640 | memset ((void*)pkt, 0, PKTSIZE); |
| 641 | |
| 642 | NetSetEther(pkt, NetBcastAddr, PROT_IP); |
| 643 | pkt += ETHER_HDR_SIZE; |
| 644 | |
| 645 | /* |
| 646 | * Next line results in incorrect packet size being transmitted, resulting |
| 647 | * in errors in some DHCP servers, reporting missing bytes. Size must be |
| 648 | * set in packet header after extension length has been determined. |
| 649 | * C. Hallinan, DS4.COM, Inc. |
| 650 | */ |
| 651 | /* NetSetIP(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, sizeof (Bootp_t)); */ |
| 652 | iphdr = pkt; /* We need this later for NetSetIP() */ |
| 653 | pkt += IP_HDR_SIZE; |
| 654 | |
| 655 | bp = (Bootp_t *)pkt; |
| 656 | bp->bp_op = OP_BOOTREQUEST; |
| 657 | bp->bp_htype = HWT_ETHER; |
| 658 | bp->bp_hlen = HWL_ETHER; |
| 659 | bp->bp_hops = 0; |
| 660 | bp->bp_secs = htons(get_timer(0) / CFG_HZ); |
| 661 | NetWriteIP(&bp->bp_ciaddr, 0); |
| 662 | NetWriteIP(&bp->bp_yiaddr, 0); |
| 663 | NetWriteIP(&bp->bp_siaddr, 0); |
| 664 | NetWriteIP(&bp->bp_giaddr, 0); |
| 665 | memcpy (bp->bp_chaddr, NetOurEther, 6); |
| 666 | copy_filename (bp->bp_file, BootFile, sizeof(bp->bp_file)); |
| 667 | |
| 668 | /* Request additional information from the BOOTP/DHCP server */ |
| 669 | #if (CONFIG_COMMANDS & CFG_CMD_DHCP) |
| 670 | ext_len = DhcpExtended(bp->bp_vend, DHCP_DISCOVER, 0, 0); |
| 671 | #else |
| 672 | ext_len = BootpExtended(bp->bp_vend); |
| 673 | #endif /* CFG_CMD_DHCP */ |
| 674 | |
| 675 | /* |
| 676 | * Bootp ID is the lower 4 bytes of our ethernet address |
| 677 | * plus the current time in HZ. |
| 678 | */ |
| 679 | BootpID = ((ulong)NetOurEther[2] << 24) |
| 680 | | ((ulong)NetOurEther[3] << 16) |
| 681 | | ((ulong)NetOurEther[4] << 8) |
| 682 | | (ulong)NetOurEther[5]; |
| 683 | BootpID += get_timer(0); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 684 | BootpID = htonl(BootpID); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 685 | NetCopyLong(&bp->bp_id, &BootpID); |
| 686 | |
| 687 | /* |
| 688 | * Calculate proper packet lengths taking into account the |
| 689 | * variable size of the options field |
| 690 | */ |
| 691 | pktlen = BOOTP_SIZE - sizeof(bp->bp_vend) + ext_len; |
| 692 | iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len; |
| 693 | NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen); |
| 694 | NetSetTimeout(SELECT_TIMEOUT * CFG_HZ, BootpTimeout); |
| 695 | |
| 696 | #if (CONFIG_COMMANDS & CFG_CMD_DHCP) |
| 697 | dhcp_state = SELECTING; |
| 698 | NetSetHandler(DhcpHandler); |
| 699 | #else |
| 700 | NetSetHandler(BootpHandler); |
| 701 | #endif /* CFG_CMD_DHCP */ |
| 702 | NetSendPacket(NetTxPacket, pktlen); |
| 703 | } |
| 704 | |
| 705 | #if (CONFIG_COMMANDS & CFG_CMD_DHCP) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 706 | static void DhcpOptionsProcess (uchar * popt) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 707 | { |
wdenk | b02744a | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 708 | uchar *end = popt + BOOTP_HDR_SIZE; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 709 | int oplen, size; |
| 710 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 711 | while (popt < end && *popt != 0xff) { |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 712 | oplen = *(popt + 1); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 713 | switch (*popt) { |
| 714 | case 1: |
| 715 | NetCopyIP (&NetOurSubnetMask, (popt + 2)); |
| 716 | break; |
| 717 | case 3: |
| 718 | NetCopyIP (&NetOurGatewayIP, (popt + 2)); |
| 719 | break; |
| 720 | case 6: |
| 721 | NetCopyIP (&NetOurDNSIP, (popt + 2)); |
stroese | e0aadfb | 2003-08-28 14:17:32 +0000 | [diff] [blame] | 722 | #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_DNS2) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 723 | if (*(popt + 1) > 4) { |
| 724 | NetCopyIP (&NetOurDNS2IP, (popt + 2 + 4)); |
| 725 | } |
stroese | e0aadfb | 2003-08-28 14:17:32 +0000 | [diff] [blame] | 726 | #endif |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 727 | break; |
| 728 | case 12: |
| 729 | size = truncate_sz ("Host Name", sizeof (NetOurHostName), oplen); |
| 730 | memcpy (&NetOurHostName, popt + 2, size); |
| 731 | NetOurHostName[size] = 0; |
| 732 | break; |
| 733 | case 15: /* Ignore Domain Name Option */ |
| 734 | break; |
| 735 | case 17: |
| 736 | size = truncate_sz ("Root Path", sizeof (NetOurRootPath), oplen); |
| 737 | memcpy (&NetOurRootPath, popt + 2, size); |
| 738 | NetOurRootPath[size] = 0; |
| 739 | break; |
| 740 | case 51: |
| 741 | NetCopyLong (&dhcp_leasetime, (ulong *) (popt + 2)); |
| 742 | break; |
| 743 | case 53: /* Ignore Message Type Option */ |
| 744 | break; |
| 745 | case 54: |
| 746 | NetCopyIP (&NetDHCPServerIP, (popt + 2)); |
| 747 | break; |
| 748 | case 58: /* Ignore Renewal Time Option */ |
| 749 | break; |
| 750 | case 59: /* Ignore Rebinding Time Option */ |
| 751 | break; |
| 752 | default: |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 753 | #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_VENDOREX) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 754 | if (dhcp_vendorex_proc (popt)) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 755 | break; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 756 | #endif |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 757 | printf ("*** Unhandled DHCP Option in OFFER/ACK: %d\n", *popt); |
| 758 | break; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 759 | } |
| 760 | popt += oplen + 2; /* Process next option */ |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | static int DhcpMessageType(unsigned char *popt) |
| 765 | { |
| 766 | if (NetReadLong((ulong*)popt) != htonl(BOOTP_VENDOR_MAGIC)) |
| 767 | return -1; |
| 768 | |
| 769 | popt += 4; |
| 770 | while ( *popt != 0xff ) { |
| 771 | if ( *popt == 53 ) /* DHCP Message Type */ |
| 772 | return *(popt + 2); |
| 773 | popt += *(popt + 1) + 2; /* Scan through all options */ |
| 774 | } |
| 775 | return -1; |
| 776 | } |
| 777 | |
wdenk | b02744a | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 778 | static void DhcpSendRequestPkt(Bootp_t *bp_offer) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 779 | { |
| 780 | volatile uchar *pkt, *iphdr; |
| 781 | Bootp_t *bp; |
| 782 | int pktlen, iplen, extlen; |
wdenk | cc1e256 | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 783 | IPaddr_t OfferedIP; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 784 | |
| 785 | debug ("DhcpSendRequestPkt: Sending DHCPREQUEST\n"); |
| 786 | pkt = NetTxPacket; |
| 787 | memset ((void*)pkt, 0, PKTSIZE); |
| 788 | |
| 789 | NetSetEther(pkt, NetBcastAddr, PROT_IP); |
| 790 | pkt += ETHER_HDR_SIZE; |
| 791 | |
| 792 | iphdr = pkt; /* We'll need this later to set proper pkt size */ |
| 793 | pkt += IP_HDR_SIZE; |
| 794 | |
| 795 | bp = (Bootp_t *)pkt; |
| 796 | bp->bp_op = OP_BOOTREQUEST; |
| 797 | bp->bp_htype = HWT_ETHER; |
| 798 | bp->bp_hlen = HWL_ETHER; |
| 799 | bp->bp_hops = 0; |
| 800 | bp->bp_secs = htons(get_timer(0) / CFG_HZ); |
| 801 | NetCopyIP(&bp->bp_ciaddr, &bp_offer->bp_ciaddr); /* both in network byte order */ |
| 802 | NetCopyIP(&bp->bp_yiaddr, &bp_offer->bp_yiaddr); |
| 803 | NetCopyIP(&bp->bp_siaddr, &bp_offer->bp_siaddr); |
| 804 | NetCopyIP(&bp->bp_giaddr, &bp_offer->bp_giaddr); |
| 805 | memcpy (bp->bp_chaddr, NetOurEther, 6); |
| 806 | |
| 807 | /* |
| 808 | * ID is the id of the OFFER packet |
| 809 | */ |
| 810 | |
| 811 | NetCopyLong(&bp->bp_id, &bp_offer->bp_id); |
| 812 | |
| 813 | /* |
| 814 | * Copy options from OFFER packet if present |
| 815 | */ |
wdenk | cc1e256 | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 816 | NetCopyIP(&OfferedIP, &bp->bp_yiaddr); |
stroese | 5fa6e90 | 2003-04-10 13:26:44 +0000 | [diff] [blame] | 817 | extlen = DhcpExtended(bp->bp_vend, DHCP_REQUEST, NetDHCPServerIP, OfferedIP); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 818 | |
| 819 | pktlen = BOOTP_SIZE - sizeof(bp->bp_vend) + extlen; |
| 820 | iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen; |
| 821 | NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen); |
| 822 | |
| 823 | debug ("Transmitting DHCPREQUEST packet: len = %d\n", pktlen); |
| 824 | NetSendPacket(NetTxPacket, pktlen); |
| 825 | } |
| 826 | |
| 827 | /* |
| 828 | * Handle DHCP received packets. |
| 829 | */ |
| 830 | static void |
| 831 | DhcpHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len) |
| 832 | { |
| 833 | Bootp_t *bp = (Bootp_t *)pkt; |
| 834 | |
| 835 | debug ("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n", |
| 836 | src, dest, len, dhcp_state); |
| 837 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 838 | if (BootpCheckPkt(pkt, dest, src, len)) /* Filter out pkts we don't want */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 839 | return; |
| 840 | |
| 841 | debug ("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state: %d\n", |
| 842 | src, dest, len, dhcp_state); |
| 843 | |
| 844 | switch (dhcp_state) { |
| 845 | case SELECTING: |
| 846 | /* |
| 847 | * Wait an appropriate time for any potential DHCPOFFER packets |
| 848 | * to arrive. Then select one, and generate DHCPREQUEST response. |
| 849 | * If filename is in format we recognize, assume it is a valid |
| 850 | * OFFER from a server we want. |
| 851 | */ |
| 852 | debug ("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file); |
| 853 | #ifdef CFG_BOOTFILE_PREFIX |
| 854 | if (strncmp(bp->bp_file, |
| 855 | CFG_BOOTFILE_PREFIX, |
| 856 | strlen(CFG_BOOTFILE_PREFIX)) == 0 ) { |
| 857 | #endif /* CFG_BOOTFILE_PREFIX */ |
| 858 | |
| 859 | debug ("TRANSITIONING TO REQUESTING STATE\n"); |
| 860 | dhcp_state = REQUESTING; |
stroese | 5fa6e90 | 2003-04-10 13:26:44 +0000 | [diff] [blame] | 861 | |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 862 | if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC)) |
| 863 | DhcpOptionsProcess(&bp->bp_vend[4]); |
| 864 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 865 | BootpCopyNetParams(bp); /* Store net params from reply */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 866 | |
| 867 | NetSetTimeout(TIMEOUT * CFG_HZ, BootpTimeout); |
| 868 | DhcpSendRequestPkt(bp); |
| 869 | #ifdef CFG_BOOTFILE_PREFIX |
| 870 | } |
| 871 | #endif /* CFG_BOOTFILE_PREFIX */ |
| 872 | |
| 873 | return; |
| 874 | break; |
| 875 | case REQUESTING: |
| 876 | debug ("DHCP State: REQUESTING\n"); |
| 877 | |
| 878 | if ( DhcpMessageType(bp->bp_vend) == DHCP_ACK ) { |
| 879 | char *s; |
| 880 | |
| 881 | if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC)) |
| 882 | DhcpOptionsProcess(&bp->bp_vend[4]); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 883 | BootpCopyNetParams(bp); /* Store net params from reply */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 884 | dhcp_state = BOUND; |
| 885 | printf("DHCP client bound to address "); |
| 886 | print_IPaddr(NetOurIP); |
| 887 | printf("\n"); |
| 888 | |
| 889 | /* Obey the 'autoload' setting */ |
wdenk | 6ea1cf0 | 2004-02-27 08:20:54 +0000 | [diff] [blame] | 890 | if ((s = getenv("autoload")) != NULL) { |
| 891 | if (*s == 'n') { |
| 892 | /* |
| 893 | * Just use BOOTP to configure system; |
| 894 | * Do not use TFTP to load the bootfile. |
| 895 | */ |
| 896 | NetState = NETLOOP_SUCCESS; |
| 897 | return; |
wdenk | 00fe161 | 2004-03-14 00:07:33 +0000 | [diff] [blame^] | 898 | #if (CONFIG_COMMANDS & CFG_CMD_NFS) |
wdenk | 6ea1cf0 | 2004-02-27 08:20:54 +0000 | [diff] [blame] | 899 | } else if (strcmp(s, "NFS") == 0) { |
| 900 | /* |
| 901 | * Use NFS to load the bootfile. |
| 902 | */ |
| 903 | NfsStart(); |
| 904 | return; |
wdenk | 00fe161 | 2004-03-14 00:07:33 +0000 | [diff] [blame^] | 905 | #endif |
wdenk | 6ea1cf0 | 2004-02-27 08:20:54 +0000 | [diff] [blame] | 906 | } |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 907 | } |
wdenk | e6466f6 | 2003-06-05 19:27:42 +0000 | [diff] [blame] | 908 | TftpStart(); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 909 | return; |
| 910 | } |
| 911 | break; |
| 912 | default: |
| 913 | printf("DHCP: INVALID STATE\n"); |
| 914 | break; |
| 915 | } |
| 916 | |
| 917 | } |
| 918 | |
| 919 | void DhcpRequest(void) |
| 920 | { |
| 921 | BootpRequest(); |
| 922 | } |
| 923 | #endif /* CFG_CMD_DHCP */ |
| 924 | |
| 925 | #endif /* CFG_CMD_NET */ |