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