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