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