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