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