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