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