bootp.c 23 KB

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