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 |
Simon Glass | 0934986 | 2011-06-13 16:13:12 -0700 | [diff] [blame] | 167 | } |
Simon Glass | 58f317d | 2011-10-24 18:00:05 +0000 | [diff] [blame] | 168 | TftpStart(TFTPGET); |
Simon Glass | 0934986 | 2011-06-13 16:13:12 -0700 | [diff] [blame] | 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; |
Jason Hobbs | d2b5d5c | 2011-08-31 05:37:31 +0000 | [diff] [blame] | 384 | #if defined(CONFIG_BOOTP_PXE) |
| 385 | char *uuid; |
| 386 | size_t vci_strlen; |
| 387 | u16 clientarch; |
| 388 | #endif |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 389 | |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 390 | #if defined(CONFIG_BOOTP_VENDOREX) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 391 | u8 *x; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 392 | #endif |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 393 | #if defined(CONFIG_BOOTP_SEND_HOSTNAME) |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 394 | char *hostname; |
stroese | fe389a8 | 2003-08-28 14:17:32 +0000 | [diff] [blame] | 395 | #endif |
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++ = 99; /* RFC1048 Magic Cookie */ |
| 398 | *e++ = 130; |
| 399 | *e++ = 83; |
| 400 | *e++ = 99; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 401 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 402 | *e++ = 53; /* DHCP Message Type */ |
| 403 | *e++ = 1; |
| 404 | *e++ = message_type; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 405 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 406 | *e++ = 57; /* Maximum DHCP Message Size */ |
| 407 | *e++ = 2; |
| 408 | *e++ = (576 - 312 + OPT_SIZE) >> 8; |
| 409 | *e++ = (576 - 312 + OPT_SIZE) & 0xff; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 410 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 411 | if (ServerID) { |
| 412 | int tmp = ntohl (ServerID); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 413 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 414 | *e++ = 54; /* ServerID */ |
| 415 | *e++ = 4; |
| 416 | *e++ = tmp >> 24; |
| 417 | *e++ = tmp >> 16; |
| 418 | *e++ = tmp >> 8; |
| 419 | *e++ = tmp & 0xff; |
| 420 | } |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 421 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 422 | if (RequestedIP) { |
| 423 | int tmp = ntohl (RequestedIP); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 424 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 425 | *e++ = 50; /* Requested IP */ |
| 426 | *e++ = 4; |
| 427 | *e++ = tmp >> 24; |
| 428 | *e++ = tmp >> 16; |
| 429 | *e++ = tmp >> 8; |
| 430 | *e++ = tmp & 0xff; |
| 431 | } |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 432 | #if defined(CONFIG_BOOTP_SEND_HOSTNAME) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 433 | if ((hostname = getenv ("hostname"))) { |
| 434 | int hostnamelen = strlen (hostname); |
| 435 | |
| 436 | *e++ = 12; /* Hostname */ |
| 437 | *e++ = hostnamelen; |
| 438 | memcpy (e, hostname, hostnamelen); |
| 439 | e += hostnamelen; |
| 440 | } |
stroese | fe389a8 | 2003-08-28 14:17:32 +0000 | [diff] [blame] | 441 | #endif |
| 442 | |
Jason Hobbs | d2b5d5c | 2011-08-31 05:37:31 +0000 | [diff] [blame] | 443 | #if defined(CONFIG_BOOTP_PXE) |
| 444 | clientarch = CONFIG_BOOTP_PXE_CLIENTARCH; |
| 445 | *e++ = 93; /* Client System Architecture */ |
| 446 | *e++ = 2; |
| 447 | *e++ = (clientarch >> 8) & 0xff; |
| 448 | *e++ = clientarch & 0xff; |
| 449 | |
| 450 | *e++ = 94; /* Client Network Interface Identifier */ |
| 451 | *e++ = 3; |
| 452 | *e++ = 1; /* type field for UNDI */ |
| 453 | *e++ = 0; /* major revision */ |
| 454 | *e++ = 0; /* minor revision */ |
| 455 | |
| 456 | uuid = getenv("pxeuuid"); |
| 457 | |
| 458 | if (uuid) { |
| 459 | if (uuid_str_valid(uuid)) { |
| 460 | *e++ = 97; /* Client Machine Identifier */ |
| 461 | *e++ = 17; |
| 462 | *e++ = 0; /* type 0 - UUID */ |
| 463 | |
| 464 | uuid_str_to_bin(uuid, e); |
| 465 | e += 16; |
| 466 | } else { |
| 467 | printf("Invalid pxeuuid: %s\n", uuid); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | *e++ = 60; /* Vendor Class Identifier */ |
| 472 | vci_strlen = strlen(CONFIG_BOOTP_VCI_STRING); |
| 473 | *e++ = vci_strlen; |
| 474 | memcpy(e, CONFIG_BOOTP_VCI_STRING, vci_strlen); |
| 475 | e += vci_strlen; |
| 476 | #endif |
| 477 | |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 478 | #if defined(CONFIG_BOOTP_VENDOREX) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 479 | if ((x = dhcp_vendorex_prep (e))) |
| 480 | return x - start; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 481 | #endif |
| 482 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 483 | *e++ = 55; /* Parameter Request List */ |
| 484 | cnt = e++; /* Pointer to count of requested items */ |
| 485 | *cnt = 0; |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 486 | #if defined(CONFIG_BOOTP_SUBNETMASK) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 487 | *e++ = 1; /* Subnet Mask */ |
| 488 | *cnt += 1; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 489 | #endif |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 490 | #if defined(CONFIG_BOOTP_TIMEOFFSET) |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 491 | *e++ = 2; |
| 492 | *cnt += 1; |
| 493 | #endif |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 494 | #if defined(CONFIG_BOOTP_GATEWAY) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 495 | *e++ = 3; /* Router Option */ |
| 496 | *cnt += 1; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 497 | #endif |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 498 | #if defined(CONFIG_BOOTP_DNS) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 499 | *e++ = 6; /* DNS Server(s) */ |
| 500 | *cnt += 1; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 501 | #endif |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 502 | #if defined(CONFIG_BOOTP_HOSTNAME) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 503 | *e++ = 12; /* Hostname */ |
| 504 | *cnt += 1; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 505 | #endif |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 506 | #if defined(CONFIG_BOOTP_BOOTFILESIZE) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 507 | *e++ = 13; /* Boot File Size */ |
| 508 | *cnt += 1; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 509 | #endif |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 510 | #if defined(CONFIG_BOOTP_BOOTPATH) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 511 | *e++ = 17; /* Boot path */ |
| 512 | *cnt += 1; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 513 | #endif |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 514 | #if defined(CONFIG_BOOTP_NISDOMAIN) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 515 | *e++ = 40; /* NIS Domain name request */ |
| 516 | *cnt += 1; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 517 | #endif |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 518 | #if defined(CONFIG_BOOTP_NTPSERVER) |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 519 | *e++ = 42; |
| 520 | *cnt += 1; |
| 521 | #endif |
Jason Liu | 258ccd6 | 2010-11-14 12:23:09 +0800 | [diff] [blame] | 522 | /* no options, so back up to avoid sending an empty request list */ |
| 523 | if (*cnt == 0) |
| 524 | e -= 2; |
| 525 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 526 | *e++ = 255; /* End of the list */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 527 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 528 | /* Pad to minimal length */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 529 | #ifdef CONFIG_DHCP_MIN_EXT_LEN |
Simon Glass | 21076f6 | 2011-02-02 15:03:28 -0800 | [diff] [blame] | 530 | while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 531 | *e++ = 0; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 532 | #endif |
| 533 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 534 | return e - start; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Jon Loeliger | 610f2e9 | 2007-07-10 11:05:02 -0500 | [diff] [blame] | 537 | #else |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 538 | /* |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 539 | * Warning: no field size check - change CONFIG_BOOTP_* at your own risk! |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 540 | */ |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 541 | static int BootpExtended (u8 * e) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 542 | { |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 543 | u8 *start = e; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 544 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 545 | *e++ = 99; /* RFC1048 Magic Cookie */ |
| 546 | *e++ = 130; |
| 547 | *e++ = 83; |
| 548 | *e++ = 99; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 549 | |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 550 | #if defined(CONFIG_CMD_DHCP) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 551 | *e++ = 53; /* DHCP Message Type */ |
| 552 | *e++ = 1; |
| 553 | *e++ = DHCP_DISCOVER; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 554 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 555 | *e++ = 57; /* Maximum DHCP Message Size */ |
| 556 | *e++ = 2; |
| 557 | *e++ = (576 - 312 + OPT_SIZE) >> 16; |
| 558 | *e++ = (576 - 312 + OPT_SIZE) & 0xff; |
Jon Loeliger | 610f2e9 | 2007-07-10 11:05:02 -0500 | [diff] [blame] | 559 | #endif |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 560 | |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 561 | #if defined(CONFIG_BOOTP_SUBNETMASK) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 562 | *e++ = 1; /* Subnet mask request */ |
| 563 | *e++ = 4; |
| 564 | e += 4; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 565 | #endif |
| 566 | |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 567 | #if defined(CONFIG_BOOTP_GATEWAY) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 568 | *e++ = 3; /* Default gateway request */ |
| 569 | *e++ = 4; |
| 570 | e += 4; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 571 | #endif |
| 572 | |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 573 | #if defined(CONFIG_BOOTP_DNS) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 574 | *e++ = 6; /* Domain Name Server */ |
| 575 | *e++ = 4; |
| 576 | e += 4; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 577 | #endif |
| 578 | |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 579 | #if defined(CONFIG_BOOTP_HOSTNAME) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 580 | *e++ = 12; /* Host name request */ |
| 581 | *e++ = 32; |
| 582 | e += 32; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 583 | #endif |
| 584 | |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 585 | #if defined(CONFIG_BOOTP_BOOTFILESIZE) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 586 | *e++ = 13; /* Boot file size */ |
| 587 | *e++ = 2; |
| 588 | e += 2; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 589 | #endif |
| 590 | |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 591 | #if defined(CONFIG_BOOTP_BOOTPATH) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 592 | *e++ = 17; /* Boot path */ |
| 593 | *e++ = 32; |
| 594 | e += 32; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 595 | #endif |
| 596 | |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 597 | #if defined(CONFIG_BOOTP_NISDOMAIN) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 598 | *e++ = 40; /* NIS Domain name request */ |
| 599 | *e++ = 32; |
| 600 | e += 32; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 601 | #endif |
Luuk Paulussen | 09e3a67 | 2011-05-16 18:29:19 +0000 | [diff] [blame] | 602 | #if defined(CONFIG_BOOTP_NTPSERVER) |
| 603 | *e++ = 42; |
| 604 | *e++ = 4; |
| 605 | e += 4; |
| 606 | #endif |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 607 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 608 | *e++ = 255; /* End of the list */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 609 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 610 | return e - start; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 611 | } |
Jon Loeliger | 610f2e9 | 2007-07-10 11:05:02 -0500 | [diff] [blame] | 612 | #endif |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 613 | |
| 614 | void |
| 615 | BootpRequest (void) |
| 616 | { |
| 617 | volatile uchar *pkt, *iphdr; |
| 618 | Bootp_t *bp; |
| 619 | int ext_len, pktlen, iplen; |
| 620 | |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 621 | #if defined(CONFIG_CMD_DHCP) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 622 | dhcp_state = INIT; |
| 623 | #endif |
| 624 | |
| 625 | #ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */ |
| 626 | unsigned char bi_enetaddr[6]; |
| 627 | int reg; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 628 | ulong tst1, tst2, sum, m_mask, m_value = 0; |
| 629 | |
| 630 | if (BootpTry ==0) { |
| 631 | /* get our mac */ |
Mike Frysinger | 95823ca | 2009-02-11 18:23:48 -0500 | [diff] [blame] | 632 | eth_getenv_enetaddr("ethaddr", bi_enetaddr); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 633 | |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 634 | debug("BootpRequest => Our Mac: "); |
| 635 | for (reg=0; reg<6; reg++) |
| 636 | debug("%x%c", bi_enetaddr[reg], reg==5 ? '\n' : ':'); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 637 | |
| 638 | /* Mac-Manipulation 2 get seed1 */ |
| 639 | tst1=0; |
| 640 | tst2=0; |
| 641 | for (reg=2; reg<6; reg++) { |
| 642 | tst1 = tst1 << 8; |
| 643 | tst1 = tst1 | bi_enetaddr[reg]; |
| 644 | } |
| 645 | for (reg=0; reg<2; reg++) { |
| 646 | tst2 = tst2 | bi_enetaddr[reg]; |
| 647 | tst2 = tst2 << 8; |
| 648 | } |
| 649 | |
| 650 | seed1 = tst1^tst2; |
| 651 | |
| 652 | /* Mirror seed1*/ |
| 653 | m_mask=0x1; |
| 654 | for (reg=1;reg<=32;reg++) { |
| 655 | m_value |= (m_mask & seed1); |
| 656 | seed1 = seed1 >> 1; |
| 657 | m_value = m_value << 1; |
| 658 | } |
| 659 | seed1 = m_value; |
| 660 | seed2 = 0xB78D0945; |
| 661 | } |
| 662 | |
| 663 | /* Random Number Generator */ |
| 664 | |
| 665 | for (reg=0;reg<=0;reg++) { |
| 666 | sum = seed1 + seed2; |
| 667 | if (sum < seed1 || sum < seed2) |
| 668 | sum++; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 669 | seed2 = seed1; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 670 | seed1 = sum; |
| 671 | |
| 672 | if (BootpTry<=2) { /* Start with max 1024 * 1ms */ |
| 673 | sum = sum >> (22-BootpTry); |
| 674 | } else { /*After 3rd BOOTP request max 8192 * 1ms */ |
| 675 | sum = sum >> 19; |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | printf ("Random delay: %ld ms...\n", sum); |
| 680 | for (reg=0; reg <sum; reg++) { |
| 681 | udelay(1000); /*Wait 1ms*/ |
| 682 | } |
| 683 | #endif /* CONFIG_BOOTP_RANDOM_DELAY */ |
| 684 | |
| 685 | printf("BOOTP broadcast %d\n", ++BootpTry); |
| 686 | pkt = NetTxPacket; |
| 687 | memset ((void*)pkt, 0, PKTSIZE); |
| 688 | |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 689 | pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 690 | |
| 691 | /* |
| 692 | * Next line results in incorrect packet size being transmitted, resulting |
| 693 | * in errors in some DHCP servers, reporting missing bytes. Size must be |
| 694 | * set in packet header after extension length has been determined. |
| 695 | * C. Hallinan, DS4.COM, Inc. |
| 696 | */ |
| 697 | /* NetSetIP(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, sizeof (Bootp_t)); */ |
| 698 | iphdr = pkt; /* We need this later for NetSetIP() */ |
| 699 | pkt += IP_HDR_SIZE; |
| 700 | |
| 701 | bp = (Bootp_t *)pkt; |
| 702 | bp->bp_op = OP_BOOTREQUEST; |
| 703 | bp->bp_htype = HWT_ETHER; |
| 704 | bp->bp_hlen = HWL_ETHER; |
| 705 | bp->bp_hops = 0; |
Bartlomiej Sieka | 49f3bdb | 2008-10-01 15:26:28 +0200 | [diff] [blame] | 706 | bp->bp_secs = htons(get_timer(0) / 1000); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 707 | NetWriteIP(&bp->bp_ciaddr, 0); |
| 708 | NetWriteIP(&bp->bp_yiaddr, 0); |
| 709 | NetWriteIP(&bp->bp_siaddr, 0); |
| 710 | NetWriteIP(&bp->bp_giaddr, 0); |
| 711 | memcpy (bp->bp_chaddr, NetOurEther, 6); |
| 712 | copy_filename (bp->bp_file, BootFile, sizeof(bp->bp_file)); |
| 713 | |
| 714 | /* Request additional information from the BOOTP/DHCP server */ |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 715 | #if defined(CONFIG_CMD_DHCP) |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 716 | ext_len = DhcpExtended((u8 *)bp->bp_vend, DHCP_DISCOVER, 0, 0); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 717 | #else |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 718 | ext_len = BootpExtended((u8 *)bp->bp_vend); |
Jon Loeliger | 610f2e9 | 2007-07-10 11:05:02 -0500 | [diff] [blame] | 719 | #endif |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 720 | |
| 721 | /* |
| 722 | * Bootp ID is the lower 4 bytes of our ethernet address |
Bartlomiej Sieka | 49f3bdb | 2008-10-01 15:26:28 +0200 | [diff] [blame] | 723 | * plus the current time in ms. |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 724 | */ |
| 725 | BootpID = ((ulong)NetOurEther[2] << 24) |
| 726 | | ((ulong)NetOurEther[3] << 16) |
| 727 | | ((ulong)NetOurEther[4] << 8) |
| 728 | | (ulong)NetOurEther[5]; |
| 729 | BootpID += get_timer(0); |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 730 | BootpID = htonl(BootpID); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 731 | NetCopyLong(&bp->bp_id, &BootpID); |
| 732 | |
| 733 | /* |
| 734 | * Calculate proper packet lengths taking into account the |
| 735 | * variable size of the options field |
| 736 | */ |
Norbert van Bolhuis | c9a2aab | 2009-06-04 09:39:48 +0200 | [diff] [blame] | 737 | pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 738 | iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + ext_len; |
| 739 | NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen); |
Bartlomiej Sieka | 49f3bdb | 2008-10-01 15:26:28 +0200 | [diff] [blame] | 740 | NetSetTimeout(SELECT_TIMEOUT, BootpTimeout); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 741 | |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 742 | #if defined(CONFIG_CMD_DHCP) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 743 | dhcp_state = SELECTING; |
| 744 | NetSetHandler(DhcpHandler); |
| 745 | #else |
| 746 | NetSetHandler(BootpHandler); |
Jon Loeliger | 610f2e9 | 2007-07-10 11:05:02 -0500 | [diff] [blame] | 747 | #endif |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 748 | NetSendPacket(NetTxPacket, pktlen); |
| 749 | } |
| 750 | |
Jon Loeliger | 643d1ab | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 751 | #if defined(CONFIG_CMD_DHCP) |
Wolfgang Denk | 3b2e4fd | 2006-03-12 18:26:46 +0100 | [diff] [blame] | 752 | static void DhcpOptionsProcess (uchar * popt, Bootp_t *bp) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 753 | { |
wdenk | 3e38691 | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 754 | uchar *end = popt + BOOTP_HDR_SIZE; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 755 | int oplen, size; |
Wolfgang Denk | d8d8724 | 2009-09-11 09:05:32 +0200 | [diff] [blame] | 756 | #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET) |
| 757 | int *to_ptr; |
| 758 | #endif |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 759 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 760 | while (popt < end && *popt != 0xff) { |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 761 | oplen = *(popt + 1); |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 762 | switch (*popt) { |
| 763 | case 1: |
| 764 | NetCopyIP (&NetOurSubnetMask, (popt + 2)); |
| 765 | break; |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 766 | #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET) |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 767 | case 2: /* Time offset */ |
Wolfgang Denk | d8d8724 | 2009-09-11 09:05:32 +0200 | [diff] [blame] | 768 | to_ptr = &NetTimeOffset; |
| 769 | NetCopyLong ((ulong *)to_ptr, (ulong *)(popt + 2)); |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 770 | NetTimeOffset = ntohl (NetTimeOffset); |
| 771 | break; |
| 772 | #endif |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 773 | case 3: |
| 774 | NetCopyIP (&NetOurGatewayIP, (popt + 2)); |
| 775 | break; |
| 776 | case 6: |
| 777 | NetCopyIP (&NetOurDNSIP, (popt + 2)); |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 778 | #if defined(CONFIG_BOOTP_DNS2) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 779 | if (*(popt + 1) > 4) { |
| 780 | NetCopyIP (&NetOurDNS2IP, (popt + 2 + 4)); |
| 781 | } |
stroese | fe389a8 | 2003-08-28 14:17:32 +0000 | [diff] [blame] | 782 | #endif |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 783 | break; |
| 784 | case 12: |
| 785 | size = truncate_sz ("Host Name", sizeof (NetOurHostName), oplen); |
| 786 | memcpy (&NetOurHostName, popt + 2, size); |
| 787 | NetOurHostName[size] = 0; |
| 788 | break; |
| 789 | case 15: /* Ignore Domain Name Option */ |
| 790 | break; |
| 791 | case 17: |
| 792 | size = truncate_sz ("Root Path", sizeof (NetOurRootPath), oplen); |
| 793 | memcpy (&NetOurRootPath, popt + 2, size); |
| 794 | NetOurRootPath[size] = 0; |
| 795 | break; |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 796 | #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER) |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 797 | case 42: /* NTP server IP */ |
| 798 | NetCopyIP (&NetNtpServerIP, (popt + 2)); |
| 799 | break; |
| 800 | #endif |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 801 | case 51: |
| 802 | NetCopyLong (&dhcp_leasetime, (ulong *) (popt + 2)); |
| 803 | break; |
| 804 | case 53: /* Ignore Message Type Option */ |
| 805 | break; |
| 806 | case 54: |
| 807 | NetCopyIP (&NetDHCPServerIP, (popt + 2)); |
| 808 | break; |
| 809 | case 58: /* Ignore Renewal Time Option */ |
| 810 | break; |
| 811 | case 59: /* Ignore Rebinding Time Option */ |
| 812 | break; |
Wolfgang Denk | 3b2e4fd | 2006-03-12 18:26:46 +0100 | [diff] [blame] | 813 | case 66: /* Ignore TFTP server name */ |
| 814 | break; |
| 815 | case 67: /* vendor opt bootfile */ |
| 816 | /* |
| 817 | * I can't use dhcp_vendorex_proc here because I need |
| 818 | * to write into the bootp packet - even then I had to |
| 819 | * pass the bootp packet pointer into here as the |
| 820 | * second arg |
| 821 | */ |
| 822 | size = truncate_sz ("Opt Boot File", |
| 823 | sizeof(bp->bp_file), |
| 824 | oplen); |
| 825 | if (bp->bp_file[0] == '\0' && size > 0) { |
| 826 | /* |
| 827 | * only use vendor boot file if we didn't |
| 828 | * receive a boot file in the main non-vendor |
| 829 | * part of the packet - god only knows why |
| 830 | * some vendors chose not to use this perfectly |
| 831 | * good spot to store the boot file (join on |
| 832 | * Tru64 Unix) it seems mind bogglingly crazy |
| 833 | * to me |
| 834 | */ |
| 835 | printf("*** WARNING: using vendor " |
| 836 | "optional boot file\n"); |
| 837 | memcpy(bp->bp_file, popt + 2, size); |
| 838 | bp->bp_file[size] = '\0'; |
| 839 | } |
| 840 | break; |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 841 | default: |
Jon Loeliger | 1fe80d7 | 2007-07-09 22:08:34 -0500 | [diff] [blame] | 842 | #if defined(CONFIG_BOOTP_VENDOREX) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 843 | if (dhcp_vendorex_proc (popt)) |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 844 | break; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 845 | #endif |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 846 | printf ("*** Unhandled DHCP Option in OFFER/ACK: %d\n", *popt); |
| 847 | break; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 848 | } |
| 849 | popt += oplen + 2; /* Process next option */ |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | static int DhcpMessageType(unsigned char *popt) |
| 854 | { |
| 855 | if (NetReadLong((ulong*)popt) != htonl(BOOTP_VENDOR_MAGIC)) |
| 856 | return -1; |
| 857 | |
| 858 | popt += 4; |
| 859 | while ( *popt != 0xff ) { |
| 860 | if ( *popt == 53 ) /* DHCP Message Type */ |
| 861 | return *(popt + 2); |
| 862 | popt += *(popt + 1) + 2; /* Scan through all options */ |
| 863 | } |
| 864 | return -1; |
| 865 | } |
| 866 | |
wdenk | 3e38691 | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 867 | static void DhcpSendRequestPkt(Bootp_t *bp_offer) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 868 | { |
| 869 | volatile uchar *pkt, *iphdr; |
| 870 | Bootp_t *bp; |
| 871 | int pktlen, iplen, extlen; |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 872 | IPaddr_t OfferedIP; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 873 | |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 874 | debug("DhcpSendRequestPkt: Sending DHCPREQUEST\n"); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 875 | pkt = NetTxPacket; |
| 876 | memset ((void*)pkt, 0, PKTSIZE); |
| 877 | |
wdenk | a3d991b | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 878 | pkt += NetSetEther(pkt, NetBcastAddr, PROT_IP); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 879 | |
| 880 | iphdr = pkt; /* We'll need this later to set proper pkt size */ |
| 881 | pkt += IP_HDR_SIZE; |
| 882 | |
| 883 | bp = (Bootp_t *)pkt; |
| 884 | bp->bp_op = OP_BOOTREQUEST; |
| 885 | bp->bp_htype = HWT_ETHER; |
| 886 | bp->bp_hlen = HWL_ETHER; |
| 887 | bp->bp_hops = 0; |
Bartlomiej Sieka | 49f3bdb | 2008-10-01 15:26:28 +0200 | [diff] [blame] | 888 | bp->bp_secs = htons(get_timer(0) / 1000); |
Justin Flammia | e5c794e | 2007-10-29 17:40:35 -0400 | [diff] [blame] | 889 | /* Do not set the client IP, your IP, or server IP yet, since it hasn't been ACK'ed by |
| 890 | * the server yet */ |
| 891 | |
Wolfgang Denk | c668670 | 2006-10-12 00:01:08 +0200 | [diff] [blame] | 892 | /* |
Wolfgang Denk | d82718f | 2006-10-09 01:26:14 +0200 | [diff] [blame] | 893 | * RFC3046 requires Relay Agents to discard packets with |
| 894 | * nonzero and offered giaddr |
| 895 | */ |
| 896 | NetWriteIP(&bp->bp_giaddr, 0); |
| 897 | |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 898 | memcpy (bp->bp_chaddr, NetOurEther, 6); |
| 899 | |
| 900 | /* |
| 901 | * ID is the id of the OFFER packet |
| 902 | */ |
| 903 | |
| 904 | NetCopyLong(&bp->bp_id, &bp_offer->bp_id); |
| 905 | |
| 906 | /* |
| 907 | * Copy options from OFFER packet if present |
| 908 | */ |
Justin Flammia | e5c794e | 2007-10-29 17:40:35 -0400 | [diff] [blame] | 909 | |
| 910 | /* Copy offered IP into the parameters request list */ |
| 911 | NetCopyIP(&OfferedIP, &bp_offer->bp_yiaddr); |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 912 | extlen = DhcpExtended((u8 *)bp->bp_vend, DHCP_REQUEST, NetDHCPServerIP, OfferedIP); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 913 | |
Norbert van Bolhuis | c9a2aab | 2009-06-04 09:39:48 +0200 | [diff] [blame] | 914 | pktlen = ((int)(pkt-NetTxPacket)) + BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 915 | iplen = BOOTP_HDR_SIZE - sizeof(bp->bp_vend) + extlen; |
| 916 | NetSetIP(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen); |
| 917 | |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 918 | debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen); |
Aras Vaichas | d9a2f41 | 2008-03-26 09:43:57 +1100 | [diff] [blame] | 919 | #ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY |
| 920 | udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY); |
| 921 | #endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 922 | NetSendPacket(NetTxPacket, pktlen); |
| 923 | } |
| 924 | |
| 925 | /* |
| 926 | * Handle DHCP received packets. |
| 927 | */ |
| 928 | static void |
Luca Ceresoli | 03eb129 | 2011-04-18 06:19:50 +0000 | [diff] [blame] | 929 | DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, |
| 930 | unsigned len) |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 931 | { |
| 932 | Bootp_t *bp = (Bootp_t *)pkt; |
| 933 | |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 934 | debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n", |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 935 | src, dest, len, dhcp_state); |
| 936 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 937 | if (BootpCheckPkt(pkt, dest, src, len)) /* Filter out pkts we don't want */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 938 | return; |
| 939 | |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 940 | 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] | 941 | src, dest, len, dhcp_state); |
| 942 | |
| 943 | switch (dhcp_state) { |
| 944 | case SELECTING: |
| 945 | /* |
| 946 | * Wait an appropriate time for any potential DHCPOFFER packets |
| 947 | * to arrive. Then select one, and generate DHCPREQUEST response. |
| 948 | * If filename is in format we recognize, assume it is a valid |
| 949 | * OFFER from a server we want. |
| 950 | */ |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 951 | 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] | 952 | #ifdef CONFIG_SYS_BOOTFILE_PREFIX |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 953 | if (strncmp(bp->bp_file, |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 954 | CONFIG_SYS_BOOTFILE_PREFIX, |
| 955 | strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0 ) { |
| 956 | #endif /* CONFIG_SYS_BOOTFILE_PREFIX */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 957 | |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 958 | debug("TRANSITIONING TO REQUESTING STATE\n"); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 959 | dhcp_state = REQUESTING; |
stroese | 759a51b | 2003-04-10 13:26:44 +0000 | [diff] [blame] | 960 | |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 961 | if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC)) |
Wolfgang Denk | 3b2e4fd | 2006-03-12 18:26:46 +0100 | [diff] [blame] | 962 | DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 963 | |
Bartlomiej Sieka | 49f3bdb | 2008-10-01 15:26:28 +0200 | [diff] [blame] | 964 | NetSetTimeout(TIMEOUT, BootpTimeout); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 965 | DhcpSendRequestPkt(bp); |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 966 | #ifdef CONFIG_SYS_BOOTFILE_PREFIX |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 967 | } |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 968 | #endif /* CONFIG_SYS_BOOTFILE_PREFIX */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 969 | |
| 970 | return; |
| 971 | break; |
| 972 | case REQUESTING: |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 973 | debug("DHCP State: REQUESTING\n"); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 974 | |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 975 | if ( DhcpMessageType((u8 *)bp->bp_vend) == DHCP_ACK ) { |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 976 | if (NetReadLong((ulong*)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC)) |
Wolfgang Denk | 3b2e4fd | 2006-03-12 18:26:46 +0100 | [diff] [blame] | 977 | DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp); |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 978 | BootpCopyNetParams(bp); /* Store net params from reply */ |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 979 | dhcp_state = BOUND; |
Mike Frysinger | b6446b6 | 2009-02-17 00:00:53 -0500 | [diff] [blame] | 980 | printf ("DHCP client bound to address %pI4\n", &NetOurIP); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 981 | |
Simon Glass | 0934986 | 2011-06-13 16:13:12 -0700 | [diff] [blame] | 982 | auto_load(); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 983 | return; |
| 984 | } |
| 985 | break; |
Remy Bohmer | 51dfe13 | 2008-08-20 11:30:28 +0200 | [diff] [blame] | 986 | case BOUND: |
| 987 | /* DHCP client bound to address */ |
| 988 | break; |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 989 | default: |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 990 | puts ("DHCP: INVALID STATE\n"); |
wdenk | 3861aa5 | 2002-09-27 23:19:37 +0000 | [diff] [blame] | 991 | break; |
| 992 | } |
| 993 | |
| 994 | } |
| 995 | |
| 996 | void DhcpRequest(void) |
| 997 | { |
| 998 | BootpRequest(); |
| 999 | } |
Wolfgang Denk | 992742a | 2007-11-03 23:09:27 +0100 | [diff] [blame] | 1000 | #endif /* CONFIG_CMD_DHCP */ |