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