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