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