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