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