bootp.c 24 KB

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