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