You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  1. /**************************************************************************
  2. Etherboot - Network Bootstrap Program
  3. Literature dealing with the network protocols:
  4. ARP - RFC826
  5. RARP - RFC903
  6. IP - RFC791
  7. UDP - RFC768
  8. BOOTP - RFC951, RFC2132 (vendor extensions)
  9. DHCP - RFC2131, RFC2132, RFC3004 (options)
  10. TFTP - RFC1350, RFC2347 (options), RFC2348 (blocksize), RFC2349 (tsize)
  11. RPC - RFC1831, RFC1832 (XDR), RFC1833 (rpcbind/portmapper)
  12. NFS - RFC1094, RFC1813 (v3, useful for clarifications, not implemented)
  13. IGMP - RFC1112, RFC2113, RFC2365, RFC2236, RFC3171
  14. **************************************************************************/
  15. #include "etherboot.h"
  16. #include "console.h"
  17. #include "url.h"
  18. #include "proto.h"
  19. #include "resolv.h"
  20. #include "dev.h"
  21. #include "nic.h"
  22. #include "background.h"
  23. #include "elf.h" /* FOR EM_CURRENT */
  24. struct arprequest {
  25. uint16_t hwtype;
  26. uint16_t protocol;
  27. uint8_t hwlen;
  28. uint8_t protolen;
  29. uint16_t opcode;
  30. uint8_t shwaddr[6];
  31. uint8_t sipaddr[4];
  32. uint8_t thwaddr[6];
  33. uint8_t tipaddr[4];
  34. } PACKED;
  35. struct arptable_t arptable[MAX_ARP];
  36. /* Put rom_info in .nocompress section so romprefix.S can write to it */
  37. struct rom_info rom __attribute__ ((section (".text16.nocompress"))) = {0,0};
  38. static unsigned long netmask;
  39. /* Used by nfs.c */
  40. char *hostname = "";
  41. int hostnamelen = 0;
  42. static uint32_t xid;
  43. unsigned char *end_of_rfc1533 = NULL;
  44. unsigned char *addparam;
  45. int addparamlen;
  46. #ifdef IMAGE_FREEBSD
  47. int freebsd_howto = 0;
  48. char freebsd_kernel_env[FREEBSD_KERNEL_ENV_SIZE];
  49. #endif /* IMAGE_FREEBSD */
  50. static int vendorext_isvalid;
  51. static const unsigned char vendorext_magic[] = {0xE4,0x45,0x74,0x68}; /* äEth */
  52. static const unsigned char broadcast[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  53. static const in_addr zeroIP = { 0L };
  54. struct bootpd_t bootp_data;
  55. #ifdef NO_DHCP_SUPPORT
  56. static unsigned char rfc1533_cookie[5] = { RFC1533_COOKIE, RFC1533_END };
  57. #else /* !NO_DHCP_SUPPORT */
  58. static int dhcp_reply;
  59. static in_addr dhcp_server = { 0L };
  60. static in_addr dhcp_addr = { 0L };
  61. static unsigned char rfc1533_cookie[] = { RFC1533_COOKIE };
  62. #define DHCP_MACHINE_INFO_SIZE (sizeof dhcp_machine_info)
  63. static unsigned char dhcp_machine_info[] = {
  64. /* Our enclosing DHCP tag */
  65. RFC1533_VENDOR_ETHERBOOT_ENCAP, 11,
  66. /* Our boot device */
  67. RFC1533_VENDOR_NIC_DEV_ID, 5, 0, 0, 0, 0, 0,
  68. /* Our current architecture */
  69. RFC1533_VENDOR_ARCH, 2, EM_CURRENT & 0xff, (EM_CURRENT >> 8) & 0xff,
  70. #ifdef EM_CURRENT_64
  71. /* The 64bit version of our current architecture */
  72. RFC1533_VENDOR_ARCH, 2, EM_CURRENT_64 & 0xff, (EM_CURRENT_64 >> 8) & 0xff,
  73. #undef DHCP_MACHINE_INFO_SIZE
  74. #define DHCP_MACHINE_INFO_SIZE (sizeof(dhcp_machine_info) - (EM_CURRENT_64_PRESENT? 0: 4))
  75. #endif /* EM_CURRENT_64 */
  76. };
  77. static const unsigned char dhcpdiscover[] = {
  78. RFC2132_MSG_TYPE,1,DHCPDISCOVER,
  79. RFC2132_MAX_SIZE,2, /* request as much as we can */
  80. ETH_MAX_MTU / 256, ETH_MAX_MTU % 256,
  81. #ifdef PXE_DHCP_STRICT
  82. RFC3679_PXE_CLIENT_UUID,RFC3679_PXE_CLIENT_UUID_LENGTH,RFC3679_PXE_CLIENT_UUID_DEFAULT,
  83. RFC3679_PXE_CLIENT_ARCH,RFC3679_PXE_CLIENT_ARCH_LENGTH,RFC3679_PXE_CLIENT_ARCH_IAX86PC,
  84. RFC3679_PXE_CLIENT_NDI, RFC3679_PXE_CLIENT_NDI_LENGTH, RFC3679_PXE_CLIENT_NDI_21,
  85. RFC2132_VENDOR_CLASS_ID,RFC2132_VENDOR_CLASS_ID_PXE_LENGTH,RFC2132_VENDOR_CLASS_ID_PXE,
  86. #else
  87. RFC2132_VENDOR_CLASS_ID,13,'E','t','h','e','r','b','o','o','t',
  88. '-',VERSION_MAJOR+'0','.',VERSION_MINOR+'0',
  89. #endif /* PXE_DHCP_STRICT */
  90. #ifdef DHCP_CLIENT_ID
  91. /* Client ID Option */
  92. RFC2132_CLIENT_ID, ( DHCP_CLIENT_ID_LEN + 1 ),
  93. DHCP_CLIENT_ID_TYPE, DHCP_CLIENT_ID,
  94. #endif /* DHCP_CLIENT_ID */
  95. #ifdef DHCP_USER_CLASS
  96. /* User Class Option */
  97. RFC3004_USER_CLASS, DHCP_USER_CLASS_LEN, DHCP_USER_CLASS,
  98. #endif /* DHCP_USER_CLASS */
  99. RFC2132_PARAM_LIST,
  100. #define DHCPDISCOVER_PARAMS_BASE 4
  101. #ifdef PXE_DHCP_STRICT
  102. #define DHCPDISCOVER_PARAMS_PXE ( 1 + 8 )
  103. #else
  104. #define DHCPDISCOVER_PARAMS_PXE 0
  105. #endif /* PXE_DHCP_STRICT */
  106. #define DHCPDISCOVER_PARAMS_DNS 1
  107. ( DHCPDISCOVER_PARAMS_BASE +
  108. DHCPDISCOVER_PARAMS_PXE+
  109. DHCPDISCOVER_PARAMS_DNS ),
  110. RFC1533_NETMASK,
  111. RFC1533_GATEWAY,
  112. RFC1533_HOSTNAME,
  113. RFC1533_VENDOR,
  114. #ifdef PXE_DHCP_STRICT
  115. ,RFC2132_VENDOR_CLASS_ID,
  116. RFC1533_VENDOR_PXE_OPT128,
  117. RFC1533_VENDOR_PXE_OPT129,
  118. RFC1533_VENDOR_PXE_OPT130,
  119. RFC1533_VENDOR_PXE_OPT131,
  120. RFC1533_VENDOR_PXE_OPT132,
  121. RFC1533_VENDOR_PXE_OPT133,
  122. RFC1533_VENDOR_PXE_OPT134,
  123. RFC1533_VENDOR_PXE_OPT135,
  124. #endif /* PXE_DHCP_STRICT */
  125. RFC1533_DNS
  126. };
  127. static const unsigned char dhcprequest [] = {
  128. RFC2132_MSG_TYPE,1,DHCPREQUEST,
  129. RFC2132_SRV_ID,4,0,0,0,0,
  130. RFC2132_REQ_ADDR,4,0,0,0,0,
  131. RFC2132_MAX_SIZE,2, /* request as much as we can */
  132. ETH_MAX_MTU / 256, ETH_MAX_MTU % 256,
  133. #ifdef PXE_DHCP_STRICT
  134. RFC3679_PXE_CLIENT_UUID,RFC3679_PXE_CLIENT_UUID_LENGTH,RFC3679_PXE_CLIENT_UUID_DEFAULT,
  135. RFC3679_PXE_CLIENT_ARCH,RFC3679_PXE_CLIENT_ARCH_LENGTH,RFC3679_PXE_CLIENT_ARCH_IAX86PC,
  136. RFC3679_PXE_CLIENT_NDI, RFC3679_PXE_CLIENT_NDI_LENGTH, RFC3679_PXE_CLIENT_NDI_21,
  137. RFC2132_VENDOR_CLASS_ID,RFC2132_VENDOR_CLASS_ID_PXE_LENGTH,RFC2132_VENDOR_CLASS_ID_PXE,
  138. #else
  139. RFC2132_VENDOR_CLASS_ID,13,'E','t','h','e','r','b','o','o','t',
  140. '-',VERSION_MAJOR+'0','.',VERSION_MINOR+'0',
  141. #endif /* PXE_DHCP_STRICT */
  142. #ifdef DHCP_CLIENT_ID
  143. /* Client ID Option */
  144. RFC2132_CLIENT_ID, ( DHCP_CLIENT_ID_LEN + 1 ),
  145. DHCP_CLIENT_ID_TYPE, DHCP_CLIENT_ID,
  146. #endif /* DHCP_CLIENT_ID */
  147. #ifdef DHCP_USER_CLASS
  148. /* User Class Option */
  149. RFC3004_USER_CLASS, DHCP_USER_CLASS_LEN, DHCP_USER_CLASS,
  150. #endif /* DHCP_USER_CLASS */
  151. /* request parameters */
  152. RFC2132_PARAM_LIST,
  153. #define DHCPREQUEST_PARAMS_BASE 5
  154. #ifdef PXE_DHCP_STRICT
  155. #define DHCPREQUEST_PARAMS_PXE 1
  156. #define DHCPREQUEST_PARAMS_VENDOR_PXE 8
  157. #define DHCPREQUEST_PARAMS_VENDOR_EB 0
  158. #else
  159. #define DHCPREQUEST_PARAMS_PXE 0
  160. #define DHCPREQUEST_PARAMS_VENDOR_PXE 0
  161. #define DHCPREQUEST_PARAMS_VENDOR_EB 4
  162. #endif /* PXE_DHCP_STRICT */
  163. #ifdef IMAGE_FREEBSD
  164. #define DHCPREQUEST_PARAMS_FREEBSD 2
  165. #else
  166. #define DHCPREQUEST_PARAMS_FREEBSD 0
  167. #endif /* IMAGE_FREEBSD */
  168. #define DHCPREQUEST_PARAMS_DNS 1
  169. ( DHCPREQUEST_PARAMS_BASE +
  170. DHCPREQUEST_PARAMS_PXE +
  171. DHCPREQUEST_PARAMS_VENDOR_PXE +
  172. DHCPREQUEST_PARAMS_VENDOR_EB +
  173. DHCPREQUEST_PARAMS_DNS +
  174. DHCPREQUEST_PARAMS_FREEBSD ),
  175. /* 5 Standard parameters */
  176. RFC1533_NETMASK,
  177. RFC1533_GATEWAY,
  178. RFC1533_HOSTNAME,
  179. RFC1533_VENDOR,
  180. RFC1533_ROOTPATH, /* only passed to the booted image */
  181. #ifndef PXE_DHCP_STRICT
  182. /* 4 Etherboot vendortags */
  183. RFC1533_VENDOR_MAGIC,
  184. RFC1533_VENDOR_ADDPARM,
  185. RFC1533_VENDOR_ETHDEV,
  186. RFC1533_VENDOR_ETHERBOOT_ENCAP,
  187. #endif /* ! PXE_DHCP_STRICT */
  188. #ifdef IMAGE_FREEBSD
  189. /* 2 FreeBSD options */
  190. RFC1533_VENDOR_HOWTO,
  191. RFC1533_VENDOR_KERNEL_ENV,
  192. #endif
  193. /* 1 DNS option */
  194. RFC1533_DNS,
  195. #ifdef PXE_DHCP_STRICT
  196. RFC2132_VENDOR_CLASS_ID,
  197. RFC1533_VENDOR_PXE_OPT128,
  198. RFC1533_VENDOR_PXE_OPT129,
  199. RFC1533_VENDOR_PXE_OPT130,
  200. RFC1533_VENDOR_PXE_OPT131,
  201. RFC1533_VENDOR_PXE_OPT132,
  202. RFC1533_VENDOR_PXE_OPT133,
  203. RFC1533_VENDOR_PXE_OPT134,
  204. RFC1533_VENDOR_PXE_OPT135,
  205. #endif /* PXE_DHCP_STRICT */
  206. };
  207. #ifdef PXE_EXPORT
  208. static const unsigned char proxydhcprequest [] = {
  209. RFC2132_MSG_TYPE,1,DHCPREQUEST,
  210. RFC2132_MAX_SIZE,2, /* request as much as we can */
  211. ETH_MAX_MTU / 256, ETH_MAX_MTU % 256,
  212. #ifdef PXE_DHCP_STRICT
  213. RFC3679_PXE_CLIENT_UUID,RFC3679_PXE_CLIENT_UUID_LENGTH,RFC3679_PXE_CLIENT_UUID_DEFAULT,
  214. RFC3679_PXE_CLIENT_ARCH,RFC3679_PXE_CLIENT_ARCH_LENGTH,RFC3679_PXE_CLIENT_ARCH_IAX86PC,
  215. RFC3679_PXE_CLIENT_NDI, RFC3679_PXE_CLIENT_NDI_LENGTH, RFC3679_PXE_CLIENT_NDI_21,
  216. RFC2132_VENDOR_CLASS_ID,RFC2132_VENDOR_CLASS_ID_PXE_LENGTH,RFC2132_VENDOR_CLASS_ID_PXE,
  217. #endif /* PXE_DHCP_STRICT */
  218. };
  219. #endif
  220. #ifdef REQUIRE_VCI_ETHERBOOT
  221. int vci_etherboot;
  222. #endif
  223. #endif /* NO_DHCP_SUPPORT */
  224. #ifdef RARP_NOT_BOOTP
  225. static int rarp(void);
  226. #else
  227. static int bootp(void);
  228. #endif
  229. /*
  230. * Find out what our boot parameters are
  231. */
  232. static int nic_configure ( struct type_dev *type_dev ) {
  233. struct nic *nic = ( struct nic * ) type_dev;
  234. int server_found;
  235. if ( ! nic->nic_op->connect ( nic ) ) {
  236. printf ( "No connection to network\n" );
  237. return 0;
  238. }
  239. /* Find a server to get BOOTP reply from */
  240. #ifdef RARP_NOT_BOOTP
  241. printf("Searching for server (RARP)...");
  242. #else
  243. #ifndef NO_DHCP_SUPPORT
  244. printf("Searching for server (DHCP)...");
  245. #else
  246. printf("Searching for server (BOOTP)...");
  247. #endif
  248. #endif
  249. #ifdef RARP_NOT_BOOTP
  250. server_found = rarp();
  251. #else
  252. server_found = bootp();
  253. #endif
  254. if (!server_found) {
  255. printf("No Server found\n");
  256. return 0;
  257. }
  258. printf("\nMe: %@", arptable[ARP_CLIENT].ipaddr.s_addr );
  259. #ifndef NO_DHCP_SUPPORT
  260. printf(", DHCP: %@", dhcp_server );
  261. #ifdef PXE_EXPORT
  262. if (arptable[ARP_PROXYDHCP].ipaddr.s_addr)
  263. printf(" (& %@)",
  264. arptable[ARP_PROXYDHCP].ipaddr.s_addr);
  265. #endif /* PXE_EXPORT */
  266. #endif /* ! NO_DHCP_SUPPORT */
  267. printf(", TFTP: %@", arptable[ARP_SERVER].ipaddr.s_addr);
  268. if (bootp_data.bootp_reply.bp_giaddr.s_addr)
  269. printf(", Relay: %@", bootp_data.bootp_reply.bp_giaddr.s_addr);
  270. if (arptable[ARP_GATEWAY].ipaddr.s_addr)
  271. printf(", Gateway %@", arptable[ARP_GATEWAY].ipaddr.s_addr);
  272. if (arptable[ARP_NAMESERVER].ipaddr.s_addr)
  273. printf(", Nameserver %@", arptable[ARP_NAMESERVER].ipaddr.s_addr);
  274. putchar('\n');
  275. #ifdef MDEBUG
  276. printf("\n=>>"); getchar();
  277. #endif
  278. return 1;
  279. }
  280. /*
  281. * Download a file from the specified URL into the specified buffer
  282. *
  283. */
  284. int download_url ( char *url, struct buffer *buffer ) {
  285. struct protocol *proto;
  286. struct sockaddr_in server;
  287. char *filename;
  288. printf ( "Loading %s\n", url );
  289. /* Parse URL */
  290. if ( ! parse_url ( url, &proto, &server, &filename ) ) {
  291. DBG ( "Unusable URL %s\n", url );
  292. return 0;
  293. }
  294. /* Call protocol's method to download the file */
  295. return proto->load ( url, &server, filename, buffer );
  296. }
  297. /**************************************************************************
  298. LOAD - Try to get booted
  299. **************************************************************************/
  300. static int nic_load ( struct type_dev *type_dev, struct buffer *buffer ) {
  301. char *kernel;
  302. /* Now use TFTP to load file */
  303. kernel = KERNEL_BUF[0] == '\0' ?
  304. #ifdef DEFAULT_BOOTFILE
  305. DEFAULT_BOOTFILE
  306. #else
  307. NULL
  308. #endif
  309. : KERNEL_BUF;
  310. #ifdef ZPXE_SUFFIX_STRIP
  311. {
  312. int i = 0;
  313. while (kernel[i++]);
  314. if(i > 5) {
  315. if(kernel[i - 6] == '.' &&
  316. kernel[i - 5] == 'z' &&
  317. kernel[i - 4] == 'p' &&
  318. kernel[i - 3] == 'x' &&
  319. kernel[i - 2] == 'e') {
  320. printf("Trimming .zpxe extension\n");
  321. kernel[i - 6] = 0;
  322. }
  323. }
  324. }
  325. #endif
  326. if ( kernel ) {
  327. return download_url ( kernel, buffer );
  328. } else {
  329. printf("No filename\n");
  330. }
  331. return 0;
  332. }
  333. void nic_disable ( struct nic *nic __unused ) {
  334. #ifdef MULTICAST_LEVEL2
  335. int i;
  336. for(i = 0; i < MAX_IGMP; i++) {
  337. leave_group(i);
  338. }
  339. #endif
  340. }
  341. static char * nic_describe_device ( struct type_dev *type_dev ) {
  342. struct nic *nic = ( struct nic * ) type_dev;
  343. static char nic_description[] = "MAC 00:00:00:00:00:00";
  344. sprintf ( nic_description + 4, "%!", nic->node_addr );
  345. return nic_description;
  346. }
  347. /*
  348. * Device operations tables
  349. *
  350. */
  351. struct type_driver nic_driver = {
  352. .name = "NIC",
  353. .type_dev = ( struct type_dev * ) &nic,
  354. .describe_device = nic_describe_device,
  355. .configure = nic_configure,
  356. .load = nic_load,
  357. };
  358. #if 0
  359. /* Careful. We need an aligned buffer to avoid problems on machines
  360. * that care about alignment. To trivally align the ethernet data
  361. * (the ip hdr and arp requests) we offset the packet by 2 bytes.
  362. * leaving the ethernet data 16 byte aligned. Beyond this
  363. * we use memmove but this makes the common cast simple and fast.
  364. */
  365. static char packet[ETH_FRAME_LEN + ETH_DATA_ALIGN] __aligned;
  366. struct nic nic = {
  367. .node_addr = arptable[ARP_CLIENT].node,
  368. .packet = packet + ETH_DATA_ALIGN,
  369. };
  370. #endif
  371. /**************************************************************************
  372. DEFAULT_NETMASK - Return default netmask for IP address
  373. **************************************************************************/
  374. static inline unsigned long default_netmask(void)
  375. {
  376. int net = ntohl(arptable[ARP_CLIENT].ipaddr.s_addr) >> 24;
  377. if (net <= 127)
  378. return(htonl(0xff000000));
  379. else if (net < 192)
  380. return(htonl(0xffff0000));
  381. else
  382. return(htonl(0xffffff00));
  383. }
  384. /**************************************************************************
  385. IP_TRANSMIT - Send an IP datagram
  386. **************************************************************************/
  387. static int await_arp(int ival, void *ptr,
  388. unsigned short ptype, struct iphdr *ip __unused, struct udphdr *udp __unused,
  389. struct tcphdr *tcp __unused)
  390. {
  391. struct arprequest *arpreply;
  392. if (ptype != ETH_P_ARP)
  393. return 0;
  394. if (nic.packetlen < ETH_HLEN + sizeof(struct arprequest))
  395. return 0;
  396. arpreply = (struct arprequest *)&nic.packet[ETH_HLEN];
  397. if (arpreply->opcode != htons(ARPOP_REPLY))
  398. return 0;
  399. if (memcmp(arpreply->sipaddr, ptr, sizeof(in_addr)) != 0)
  400. return 0;
  401. memcpy(arptable[ival].node, arpreply->shwaddr, ETH_ALEN);
  402. return 1;
  403. }
  404. int ip_transmit(int len, const void *buf)
  405. {
  406. unsigned long destip;
  407. struct iphdr *ip;
  408. struct arprequest arpreq;
  409. int arpentry, i;
  410. int retry;
  411. ip = (struct iphdr *)buf;
  412. destip = ip->dest.s_addr;
  413. if (destip == INADDR_BROADCAST) {
  414. eth_transmit(broadcast, ETH_P_IP, len, buf);
  415. } else if ((destip & htonl(MULTICAST_MASK)) == htonl(MULTICAST_NETWORK)) {
  416. unsigned char multicast[6];
  417. unsigned long hdestip;
  418. hdestip = ntohl(destip);
  419. multicast[0] = 0x01;
  420. multicast[1] = 0x00;
  421. multicast[2] = 0x5e;
  422. multicast[3] = (hdestip >> 16) & 0x7;
  423. multicast[4] = (hdestip >> 8) & 0xff;
  424. multicast[5] = hdestip & 0xff;
  425. eth_transmit(multicast, ETH_P_IP, len, buf);
  426. } else {
  427. if (((destip & netmask) !=
  428. (arptable[ARP_CLIENT].ipaddr.s_addr & netmask)) &&
  429. arptable[ARP_GATEWAY].ipaddr.s_addr)
  430. destip = arptable[ARP_GATEWAY].ipaddr.s_addr;
  431. for(arpentry = 0; arpentry<MAX_ARP; arpentry++)
  432. if (arptable[arpentry].ipaddr.s_addr == destip) break;
  433. if (arpentry == MAX_ARP) {
  434. printf("%@ is not in my arp table!\n", destip);
  435. return(0);
  436. }
  437. for (i = 0; i < ETH_ALEN; i++)
  438. if (arptable[arpentry].node[i])
  439. break;
  440. if (i == ETH_ALEN) { /* Need to do arp request */
  441. arpreq.hwtype = htons(1);
  442. arpreq.protocol = htons(ETH_P_IP);
  443. arpreq.hwlen = ETH_ALEN;
  444. arpreq.protolen = 4;
  445. arpreq.opcode = htons(ARPOP_REQUEST);
  446. memcpy(arpreq.shwaddr, arptable[ARP_CLIENT].node, ETH_ALEN);
  447. memcpy(arpreq.sipaddr, &arptable[ARP_CLIENT].ipaddr, sizeof(in_addr));
  448. memset(arpreq.thwaddr, 0, ETH_ALEN);
  449. memcpy(arpreq.tipaddr, &destip, sizeof(in_addr));
  450. for (retry = 1; retry <= MAX_ARP_RETRIES; retry++) {
  451. long timeout;
  452. eth_transmit(broadcast, ETH_P_ARP, sizeof(arpreq),
  453. &arpreq);
  454. timeout = rfc2131_sleep_interval(TIMEOUT, retry);
  455. if (await_reply(await_arp, arpentry,
  456. arpreq.tipaddr, timeout)) goto xmit;
  457. }
  458. return(0);
  459. }
  460. xmit:
  461. eth_transmit(arptable[arpentry].node, ETH_P_IP, len, buf);
  462. }
  463. return 1;
  464. }
  465. void build_ip_hdr(unsigned long destip, int ttl, int protocol, int option_len,
  466. int len, const void *buf)
  467. {
  468. struct iphdr *ip;
  469. ip = (struct iphdr *)buf;
  470. ip->verhdrlen = 0x45;
  471. ip->verhdrlen += (option_len/4);
  472. ip->service = 0;
  473. ip->len = htons(len);
  474. ip->ident = 0;
  475. ip->frags = 0; /* Should we set don't fragment? */
  476. ip->ttl = ttl;
  477. ip->protocol = protocol;
  478. ip->chksum = 0;
  479. ip->src.s_addr = arptable[ARP_CLIENT].ipaddr.s_addr;
  480. ip->dest.s_addr = destip;
  481. ip->chksum = ipchksum(buf, sizeof(struct iphdr) + option_len);
  482. }
  483. void build_udp_hdr(unsigned long destip,
  484. unsigned int srcsock, unsigned int destsock, int ttl,
  485. int len, const void *buf)
  486. {
  487. struct iphdr *ip;
  488. struct udphdr *udp;
  489. ip = (struct iphdr *)buf;
  490. build_ip_hdr(destip, ttl, IP_UDP, 0, len, buf);
  491. udp = (struct udphdr *)((char *)buf + sizeof(struct iphdr));
  492. udp->src = htons(srcsock);
  493. udp->dest = htons(destsock);
  494. udp->len = htons(len - sizeof(struct iphdr));
  495. udp->chksum = 0;
  496. if ((udp->chksum = tcpudpchksum(ip)) == 0)
  497. udp->chksum = 0xffff;
  498. }
  499. /**************************************************************************
  500. UDP_TRANSMIT - Send an UDP datagram
  501. **************************************************************************/
  502. int udp_transmit(unsigned long destip, unsigned int srcsock,
  503. unsigned int destsock, int len, const void *buf)
  504. {
  505. build_udp_hdr(destip, srcsock, destsock, 60, len, buf);
  506. return ip_transmit(len, buf);
  507. }
  508. /**************************************************************************
  509. QDRAIN - clear the nic's receive queue
  510. **************************************************************************/
  511. static int await_qdrain(int ival __unused, void *ptr __unused,
  512. unsigned short ptype __unused,
  513. struct iphdr *ip __unused, struct udphdr *udp __unused,
  514. struct tcphdr *tcp __unused)
  515. {
  516. return 0;
  517. }
  518. void rx_qdrain(void)
  519. {
  520. /* Clear out the Rx queue first. It contains nothing of interest,
  521. * except possibly ARP requests from the DHCP/TFTP server. We use
  522. * polling throughout Etherboot, so some time may have passed since we
  523. * last polled the receive queue, which may now be filled with
  524. * broadcast packets. This will cause the reply to the packets we are
  525. * about to send to be lost immediately. Not very clever. */
  526. await_reply(await_qdrain, 0, NULL, 0);
  527. }
  528. #ifdef RARP_NOT_BOOTP
  529. /**************************************************************************
  530. RARP - Get my IP address and load information
  531. **************************************************************************/
  532. static int await_rarp(int ival, void *ptr,
  533. unsigned short ptype, struct iphdr *ip, struct udphdr *udp,
  534. struct tcphdr *tcp __unused)
  535. {
  536. struct arprequest *arpreply;
  537. if (ptype != ETH_P_RARP)
  538. return 0;
  539. if (nic.packetlen < ETH_HLEN + sizeof(struct arprequest))
  540. return 0;
  541. arpreply = (struct arprequest *)&nic.packet[ETH_HLEN];
  542. if (arpreply->opcode != htons(RARP_REPLY))
  543. return 0;
  544. if ((arpreply->opcode == htons(RARP_REPLY)) &&
  545. (memcmp(arpreply->thwaddr, ptr, ETH_ALEN) == 0)) {
  546. memcpy(arptable[ARP_SERVER].node, arpreply->shwaddr, ETH_ALEN);
  547. memcpy(&arptable[ARP_SERVER].ipaddr, arpreply->sipaddr, sizeof(in_addr));
  548. memcpy(&arptable[ARP_CLIENT].ipaddr, arpreply->tipaddr, sizeof(in_addr));
  549. return 1;
  550. }
  551. return 0;
  552. }
  553. static int rarp(void)
  554. {
  555. int retry;
  556. /* arp and rarp requests share the same packet structure. */
  557. struct arprequest rarpreq;
  558. memset(&rarpreq, 0, sizeof(rarpreq));
  559. rarpreq.hwtype = htons(1);
  560. rarpreq.protocol = htons(IP);
  561. rarpreq.hwlen = ETH_ALEN;
  562. rarpreq.protolen = 4;
  563. rarpreq.opcode = htons(RARP_REQUEST);
  564. memcpy(&rarpreq.shwaddr, arptable[ARP_CLIENT].node, ETH_ALEN);
  565. /* sipaddr is already zeroed out */
  566. memcpy(&rarpreq.thwaddr, arptable[ARP_CLIENT].node, ETH_ALEN);
  567. /* tipaddr is already zeroed out */
  568. for (retry = 0; retry < MAX_ARP_RETRIES; ++retry) {
  569. long timeout;
  570. eth_transmit(broadcast, ETH_P_RARP, sizeof(rarpreq), &rarpreq);
  571. timeout = rfc2131_sleep_interval(TIMEOUT, retry);
  572. if (await_reply(await_rarp, 0, rarpreq.shwaddr, timeout))
  573. break;
  574. }
  575. if (retry < MAX_ARP_RETRIES) {
  576. (void)sprintf(KERNEL_BUF, DEFAULT_KERNELPATH, arptable[ARP_CLIENT].ipaddr);
  577. return (1);
  578. }
  579. return (0);
  580. }
  581. #else
  582. /**************************************************************************
  583. BOOTP - Get my IP address and load information
  584. **************************************************************************/
  585. static int await_bootp(int ival __unused, void *ptr __unused,
  586. unsigned short ptype __unused, struct iphdr *ip __unused,
  587. struct udphdr *udp, struct tcphdr *tcp __unused)
  588. {
  589. struct bootp_t *bootpreply;
  590. if (!udp) {
  591. return 0;
  592. }
  593. bootpreply = (struct bootp_t *)&nic.packet[ETH_HLEN +
  594. sizeof(struct iphdr) + sizeof(struct udphdr)];
  595. if (nic.packetlen < ETH_HLEN + sizeof(struct iphdr) +
  596. sizeof(struct udphdr) +
  597. #ifdef NO_DHCP_SUPPORT
  598. sizeof(struct bootp_t)
  599. #else
  600. sizeof(struct bootp_t) - DHCP_OPT_LEN
  601. #endif /* NO_DHCP_SUPPORT */
  602. ) {
  603. return 0;
  604. }
  605. if (udp->dest != htons(BOOTP_CLIENT))
  606. return 0;
  607. if (bootpreply->bp_op != BOOTP_REPLY)
  608. return 0;
  609. if (bootpreply->bp_xid != xid)
  610. return 0;
  611. if (memcmp(&bootpreply->bp_siaddr, &zeroIP, sizeof(in_addr)) == 0)
  612. return 0;
  613. if ((memcmp(broadcast, bootpreply->bp_hwaddr, ETH_ALEN) != 0) &&
  614. (memcmp(arptable[ARP_CLIENT].node, bootpreply->bp_hwaddr, ETH_ALEN) != 0)) {
  615. return 0;
  616. }
  617. if ( bootpreply->bp_siaddr.s_addr ) {
  618. arptable[ARP_SERVER].ipaddr.s_addr = bootpreply->bp_siaddr.s_addr;
  619. memset(arptable[ARP_SERVER].node, 0, ETH_ALEN); /* Kill arp */
  620. }
  621. if ( bootpreply->bp_giaddr.s_addr ) {
  622. arptable[ARP_GATEWAY].ipaddr.s_addr = bootpreply->bp_giaddr.s_addr;
  623. memset(arptable[ARP_GATEWAY].node, 0, ETH_ALEN); /* Kill arp */
  624. }
  625. if (bootpreply->bp_yiaddr.s_addr) {
  626. /* Offer with an IP address */
  627. arptable[ARP_CLIENT].ipaddr.s_addr = bootpreply->bp_yiaddr.s_addr;
  628. #ifndef NO_DHCP_SUPPORT
  629. dhcp_addr.s_addr = bootpreply->bp_yiaddr.s_addr;
  630. #endif /* NO_DHCP_SUPPORT */
  631. netmask = default_netmask();
  632. /* bootpreply->bp_file will be copied to KERNEL_BUF in the memcpy */
  633. memcpy((char *)&bootp_data, (char *)bootpreply, sizeof(struct bootpd_t));
  634. decode_rfc1533(bootp_data.bootp_reply.bp_vend, 0,
  635. #ifdef NO_DHCP_SUPPORT
  636. BOOTP_VENDOR_LEN + MAX_BOOTP_EXTLEN,
  637. #else
  638. DHCP_OPT_LEN + MAX_BOOTP_EXTLEN,
  639. #endif /* NO_DHCP_SUPPORT */
  640. 1);
  641. #ifdef PXE_EXPORT
  642. } else {
  643. /* Offer without an IP address - use as ProxyDHCP server */
  644. arptable[ARP_PROXYDHCP].ipaddr.s_addr = bootpreply->bp_siaddr.s_addr;
  645. memset(arptable[ARP_PROXYDHCP].node, 0, ETH_ALEN); /* Kill arp */
  646. /* Grab only the bootfile name from a ProxyDHCP packet */
  647. memcpy(KERNEL_BUF, bootpreply->bp_file, sizeof(KERNEL_BUF));
  648. #endif /* PXE_EXPORT */
  649. }
  650. #ifdef REQUIRE_VCI_ETHERBOOT
  651. if (!vci_etherboot)
  652. return (0);
  653. #endif
  654. return(1);
  655. }
  656. static int bootp(void)
  657. {
  658. int retry;
  659. #ifndef NO_DHCP_SUPPORT
  660. int reqretry;
  661. #endif /* NO_DHCP_SUPPORT */
  662. struct bootpip_t ip;
  663. unsigned long starttime;
  664. unsigned char *bp_vend;
  665. #ifndef NO_DHCP_SUPPORT
  666. * ( ( struct dhcp_dev_id * ) &dhcp_machine_info[4] ) = nic.dhcp_dev_id;
  667. #endif /* NO_DHCP_SUPPORT */
  668. memset(&ip, 0, sizeof(struct bootpip_t));
  669. ip.bp.bp_op = BOOTP_REQUEST;
  670. ip.bp.bp_htype = 1;
  671. ip.bp.bp_hlen = ETH_ALEN;
  672. starttime = currticks();
  673. /* Use lower 32 bits of node address, more likely to be
  674. distinct than the time since booting */
  675. memcpy(&xid, &arptable[ARP_CLIENT].node[2], sizeof(xid));
  676. ip.bp.bp_xid = xid += htonl(starttime);
  677. memcpy(ip.bp.bp_hwaddr, arptable[ARP_CLIENT].node, ETH_ALEN);
  678. #ifdef NO_DHCP_SUPPORT
  679. memcpy(ip.bp.bp_vend, rfc1533_cookie, 5); /* request RFC-style options */
  680. #else
  681. memcpy(ip.bp.bp_vend, rfc1533_cookie, sizeof rfc1533_cookie); /* request RFC-style options */
  682. memcpy(ip.bp.bp_vend + sizeof rfc1533_cookie, dhcpdiscover, sizeof dhcpdiscover);
  683. /* Append machine_info to end, in encapsulated option */
  684. bp_vend = ip.bp.bp_vend + sizeof rfc1533_cookie + sizeof dhcpdiscover;
  685. memcpy(bp_vend, dhcp_machine_info, DHCP_MACHINE_INFO_SIZE);
  686. bp_vend += DHCP_MACHINE_INFO_SIZE;
  687. *bp_vend++ = RFC1533_END;
  688. #endif /* NO_DHCP_SUPPORT */
  689. for (retry = 0; retry < MAX_BOOTP_RETRIES; ) {
  690. uint8_t my_hwaddr[ETH_ALEN];
  691. unsigned long stop_time;
  692. long remaining_time;
  693. rx_qdrain();
  694. /* Kill arptable to avoid keeping stale entries */
  695. memcpy ( my_hwaddr, arptable[ARP_CLIENT].node, ETH_ALEN );
  696. memset ( arptable, 0, sizeof(arptable) );
  697. memcpy ( arptable[ARP_CLIENT].node, my_hwaddr, ETH_ALEN );
  698. udp_transmit(INADDR_BROADCAST, BOOTP_CLIENT, BOOTP_SERVER,
  699. sizeof(struct bootpip_t), &ip);
  700. remaining_time = rfc2131_sleep_interval(BOOTP_TIMEOUT, retry++);
  701. stop_time = currticks() + remaining_time;
  702. #ifdef NO_DHCP_SUPPORT
  703. if (await_reply(await_bootp, 0, NULL, remaining_time))
  704. return(1);
  705. #else
  706. while ( remaining_time > 0 ) {
  707. if (await_reply(await_bootp, 0, NULL, remaining_time)){
  708. if (arptable[ARP_CLIENT].ipaddr.s_addr)
  709. break;
  710. }
  711. remaining_time = stop_time - currticks();
  712. }
  713. if ( ! arptable[ARP_CLIENT].ipaddr.s_addr ) {
  714. printf("No IP address\n");
  715. continue;
  716. }
  717. /* If not a DHCPOFFER then must be just a BOOTP reply,
  718. * be backward compatible with BOOTP then */
  719. if (dhcp_reply != DHCPOFFER)
  720. return(1);
  721. dhcp_reply = 0;
  722. /* Construct the DHCPREQUEST packet */
  723. memcpy(ip.bp.bp_vend, rfc1533_cookie, sizeof rfc1533_cookie);
  724. memcpy(ip.bp.bp_vend + sizeof rfc1533_cookie, dhcprequest, sizeof dhcprequest);
  725. /* Beware: the magic numbers 9 and 15 depend on
  726. the layout of dhcprequest */
  727. memcpy(&ip.bp.bp_vend[9], &dhcp_server, sizeof(in_addr));
  728. memcpy(&ip.bp.bp_vend[15], &dhcp_addr, sizeof(in_addr));
  729. bp_vend = ip.bp.bp_vend + sizeof rfc1533_cookie + sizeof dhcprequest;
  730. /* Append machine_info to end, in encapsulated option */
  731. memcpy(bp_vend, dhcp_machine_info, DHCP_MACHINE_INFO_SIZE);
  732. bp_vend += DHCP_MACHINE_INFO_SIZE;
  733. *bp_vend++ = RFC1533_END;
  734. for (reqretry = 0; reqretry < MAX_BOOTP_RETRIES; ) {
  735. unsigned long timeout;
  736. udp_transmit(INADDR_BROADCAST, BOOTP_CLIENT,
  737. BOOTP_SERVER, sizeof(struct bootpip_t),
  738. &ip);
  739. dhcp_reply=0;
  740. timeout = rfc2131_sleep_interval(TIMEOUT, reqretry++);
  741. if (!await_reply(await_bootp, 0, NULL, timeout))
  742. continue;
  743. if (dhcp_reply != DHCPACK)
  744. continue;
  745. dhcp_reply = 0;
  746. #ifdef PXE_EXPORT
  747. if ( arptable[ARP_PROXYDHCP].ipaddr.s_addr ) {
  748. /* Construct the ProxyDHCPREQUEST packet */
  749. memcpy(ip.bp.bp_vend, rfc1533_cookie, sizeof rfc1533_cookie);
  750. memcpy(ip.bp.bp_vend + sizeof rfc1533_cookie, proxydhcprequest, sizeof proxydhcprequest);
  751. for (reqretry = 0; reqretry < MAX_BOOTP_RETRIES; ) {
  752. printf ( "\nSending ProxyDHCP request to %@...", arptable[ARP_PROXYDHCP].ipaddr.s_addr);
  753. udp_transmit(arptable[ARP_PROXYDHCP].ipaddr.s_addr, BOOTP_CLIENT, PROXYDHCP_SERVER,
  754. sizeof(struct bootpip_t), &ip);
  755. timeout = rfc2131_sleep_interval(TIMEOUT, reqretry++);
  756. if (await_reply(await_bootp, 0, NULL, timeout)) {
  757. break;
  758. }
  759. }
  760. }
  761. #endif /* PXE_EXPORT */
  762. return(1);
  763. }
  764. #endif /* NO_DHCP_SUPPORT */
  765. ip.bp.bp_secs = htons((currticks()-starttime)/TICKS_PER_SEC);
  766. }
  767. return(0);
  768. }
  769. #endif /* RARP_NOT_BOOTP */
  770. uint16_t tcpudpchksum(struct iphdr *ip)
  771. {
  772. struct udp_pseudo_hdr pseudo;
  773. uint16_t checksum;
  774. /* Compute the pseudo header */
  775. pseudo.src.s_addr = ip->src.s_addr;
  776. pseudo.dest.s_addr = ip->dest.s_addr;
  777. pseudo.unused = 0;
  778. pseudo.protocol = ip->protocol;
  779. pseudo.len = htons(ntohs(ip->len) - sizeof(struct iphdr));
  780. /* Sum the pseudo header */
  781. checksum = ipchksum(&pseudo, 12);
  782. /* Sum the rest of the tcp/udp packet */
  783. checksum = add_ipchksums(12, checksum, ipchksum(ip + 1,
  784. ntohs(ip->len) - sizeof(struct iphdr)));
  785. return checksum;
  786. }
  787. #include "proto_eth_slow.c"
  788. /**************************************************************************
  789. AWAIT_REPLY - Wait until we get a response for our request
  790. ************f**************************************************************/
  791. int await_reply(reply_t reply, int ival, void *ptr, long timeout)
  792. {
  793. unsigned long time, now;
  794. struct iphdr *ip;
  795. unsigned iplen = 0;
  796. struct udphdr *udp;
  797. struct tcphdr *tcp;
  798. unsigned short ptype;
  799. int result;
  800. time = timeout + currticks();
  801. /* The timeout check is done below. The timeout is only checked if
  802. * there is no packet in the Rx queue. This assumes that eth_poll()
  803. * needs a negligible amount of time.
  804. */
  805. for (;;) {
  806. now = currticks();
  807. background_send(now);
  808. send_eth_slow_reports(now);
  809. result = eth_poll(1);
  810. if (result == 0) {
  811. /* We don't have anything */
  812. /* Check for abort key only if the Rx queue is empty -
  813. * as long as we have something to process, don't
  814. * assume that something failed. It is unlikely that
  815. * we have no processing time left between packets. */
  816. poll_interruptions();
  817. /* Do the timeout after at least a full queue walk. */
  818. if ((timeout == 0) || (currticks() > time)) {
  819. break;
  820. }
  821. continue;
  822. }
  823. /* We have something! */
  824. /* Find the Ethernet packet type */
  825. if (nic.packetlen >= ETH_HLEN) {
  826. ptype = ((unsigned short) nic.packet[12]) << 8
  827. | ((unsigned short) nic.packet[13]);
  828. } else continue; /* what else could we do with it? */
  829. /* Verify an IP header */
  830. ip = 0;
  831. if ((ptype == ETH_P_IP) && (nic.packetlen >= ETH_HLEN + sizeof(struct iphdr))) {
  832. unsigned ipoptlen;
  833. ip = (struct iphdr *)&nic.packet[ETH_HLEN];
  834. if ((ip->verhdrlen < 0x45) || (ip->verhdrlen > 0x4F))
  835. continue;
  836. iplen = (ip->verhdrlen & 0xf) * 4;
  837. if (ipchksum(ip, iplen) != 0)
  838. continue;
  839. if (ip->frags & htons(0x3FFF)) {
  840. static int warned_fragmentation = 0;
  841. if (!warned_fragmentation) {
  842. printf("ALERT: got a fragmented packet - reconfigure your server\n");
  843. warned_fragmentation = 1;
  844. }
  845. continue;
  846. }
  847. if (ntohs(ip->len) > ETH_MAX_MTU)
  848. continue;
  849. ipoptlen = iplen - sizeof(struct iphdr);
  850. if (ipoptlen) {
  851. /* Delete the ip options, to guarantee
  852. * good alignment, and make etherboot simpler.
  853. */
  854. memmove(&nic.packet[ETH_HLEN + sizeof(struct iphdr)],
  855. &nic.packet[ETH_HLEN + iplen],
  856. nic.packetlen - ipoptlen);
  857. nic.packetlen -= ipoptlen;
  858. }
  859. }
  860. udp = 0;
  861. if (ip && (ip->protocol == IP_UDP) &&
  862. (nic.packetlen >=
  863. ETH_HLEN + sizeof(struct iphdr) + sizeof(struct udphdr))) {
  864. udp = (struct udphdr *)&nic.packet[ETH_HLEN + sizeof(struct iphdr)];
  865. /* Make certain we have a reasonable packet length */
  866. if (ntohs(udp->len) > (ntohs(ip->len) - iplen))
  867. continue;
  868. if (udp->chksum && tcpudpchksum(ip)) {
  869. printf("UDP checksum error\n");
  870. continue;
  871. }
  872. }
  873. tcp = 0;
  874. if (ip && (ip->protocol == IP_TCP) &&
  875. (nic.packetlen >=
  876. ETH_HLEN + sizeof(struct iphdr) + sizeof(struct tcphdr))){
  877. tcp = (struct tcphdr *)&nic.packet[ETH_HLEN +
  878. sizeof(struct iphdr)];
  879. /* Make certain we have a reasonable packet length */
  880. if (((ntohs(tcp->ctrl) >> 10) & 0x3C) >
  881. ntohs(ip->len) - (int)iplen)
  882. continue;
  883. if (tcpudpchksum(ip)) {
  884. printf("TCP checksum error\n");
  885. continue;
  886. }
  887. }
  888. result = reply(ival, ptr, ptype, ip, udp, tcp);
  889. if (result > 0) {
  890. return result;
  891. }
  892. /* If it isn't a packet the upper layer wants see if there is a default
  893. * action. This allows us reply to arp, igmp, and lacp queries.
  894. */
  895. if ((ptype == ETH_P_ARP) &&
  896. (nic.packetlen >= ETH_HLEN + sizeof(struct arprequest))) {
  897. struct arprequest *arpreply;
  898. unsigned long tmp;
  899. arpreply = (struct arprequest *)&nic.packet[ETH_HLEN];
  900. memcpy(&tmp, arpreply->tipaddr, sizeof(in_addr));
  901. if ((arpreply->opcode == htons(ARPOP_REQUEST)) &&
  902. (tmp == arptable[ARP_CLIENT].ipaddr.s_addr)) {
  903. arpreply->opcode = htons(ARPOP_REPLY);
  904. memcpy(arpreply->tipaddr, arpreply->sipaddr, sizeof(in_addr));
  905. memcpy(arpreply->thwaddr, arpreply->shwaddr, ETH_ALEN);
  906. memcpy(arpreply->sipaddr, &arptable[ARP_CLIENT].ipaddr, sizeof(in_addr));
  907. memcpy(arpreply->shwaddr, arptable[ARP_CLIENT].node, ETH_ALEN);
  908. eth_transmit(arpreply->thwaddr, ETH_P_ARP,
  909. sizeof(struct arprequest),
  910. arpreply);
  911. #ifdef MDEBUG
  912. memcpy(&tmp, arpreply->tipaddr, sizeof(in_addr));
  913. printf("Sent ARP reply to: %@\n",tmp);
  914. #endif /* MDEBUG */
  915. }
  916. }
  917. background_process(now, ptype, ip);
  918. process_eth_slow(ptype, now);
  919. }
  920. return(0);
  921. }
  922. #ifdef REQUIRE_VCI_ETHERBOOT
  923. /**************************************************************************
  924. FIND_VCI_ETHERBOOT - Looks for "Etherboot" in Vendor Encapsulated Identifiers
  925. On entry p points to byte count of VCI options
  926. **************************************************************************/
  927. static int find_vci_etherboot(unsigned char *p)
  928. {
  929. unsigned char *end = p + 1 + *p;
  930. for (p++; p < end; ) {
  931. if (*p == RFC2132_VENDOR_CLASS_ID) {
  932. if (strncmp("Etherboot", p + 2, sizeof("Etherboot") - 1) == 0)
  933. return (1);
  934. } else if (*p == RFC1533_END)
  935. return (0);
  936. p += TAG_LEN(p) + 2;
  937. }
  938. return (0);
  939. }
  940. #endif /* REQUIRE_VCI_ETHERBOOT */
  941. /**************************************************************************
  942. DECODE_RFC1533 - Decodes RFC1533 header
  943. **************************************************************************/
  944. int decode_rfc1533(unsigned char *p, unsigned int block, unsigned int len, int eof)
  945. {
  946. static unsigned char *extdata = NULL, *extend = NULL;
  947. unsigned char *extpath = NULL;
  948. unsigned char *endp;
  949. static unsigned char in_encapsulated_options = 0;
  950. if (eof == -1) {
  951. /* Encapsulated option block */
  952. endp = p + len;
  953. }
  954. else if (block == 0) {
  955. #ifdef REQUIRE_VCI_ETHERBOOT
  956. vci_etherboot = 0;
  957. #endif
  958. end_of_rfc1533 = NULL;
  959. #ifdef IMAGE_FREEBSD
  960. /* yes this is a pain FreeBSD uses this for swap, however,
  961. there are cases when you don't want swap and then
  962. you want this set to get the extra features so lets
  963. just set if dealing with FreeBSD. I haven't run into
  964. any troubles with this but I have without it
  965. */
  966. vendorext_isvalid = 1;
  967. #ifdef FREEBSD_KERNEL_ENV
  968. memcpy(freebsd_kernel_env, FREEBSD_KERNEL_ENV,
  969. sizeof(FREEBSD_KERNEL_ENV));
  970. /* FREEBSD_KERNEL_ENV had better be a string constant */
  971. #else
  972. freebsd_kernel_env[0]='\0';
  973. #endif
  974. #else
  975. vendorext_isvalid = 0;
  976. #endif
  977. addparam = NULL;
  978. addparamlen = 0;
  979. if (memcmp(p, rfc1533_cookie, 4))
  980. return(0); /* no RFC 1533 header found */
  981. p += 4;
  982. endp = p + len;
  983. } else {
  984. if (block == 1) {
  985. if (memcmp(p, rfc1533_cookie, 4))
  986. return(0); /* no RFC 1533 header found */
  987. p += 4;
  988. len -= 4; }
  989. if (extend + len <= (unsigned char *)&(bootp_data.bootp_extension[MAX_BOOTP_EXTLEN])) {
  990. memcpy(extend, p, len);
  991. extend += len;
  992. } else {
  993. printf("Overflow in vendor data buffer! Aborting...\n");
  994. *extdata = RFC1533_END;
  995. return(0);
  996. }
  997. p = extdata; endp = extend;
  998. }
  999. if (!eof)
  1000. return 1;
  1001. while (p < endp) {
  1002. unsigned char c = *p;
  1003. if (c == RFC1533_PAD) {
  1004. p++;
  1005. continue;
  1006. }
  1007. else if (c == RFC1533_END) {
  1008. end_of_rfc1533 = endp = p;
  1009. continue;
  1010. }
  1011. else if (NON_ENCAP_OPT c == RFC1533_NETMASK)
  1012. memcpy(&netmask, p+2, sizeof(in_addr));
  1013. else if (NON_ENCAP_OPT c == RFC1533_GATEWAY) {
  1014. /* This is a little simplistic, but it will
  1015. usually be sufficient.
  1016. Take only the first entry */
  1017. if (TAG_LEN(p) >= sizeof(in_addr))
  1018. memcpy(&arptable[ARP_GATEWAY].ipaddr, p+2, sizeof(in_addr));
  1019. }
  1020. else if (c == RFC1533_EXTENSIONPATH)
  1021. extpath = p;
  1022. #ifndef NO_DHCP_SUPPORT
  1023. #ifdef REQUIRE_VCI_ETHERBOOT
  1024. else if (NON_ENCAP_OPT c == RFC1533_VENDOR) {
  1025. vci_etherboot = find_vci_etherboot(p+1);
  1026. #ifdef MDEBUG
  1027. printf("vci_etherboot %d\n", vci_etherboot);
  1028. #endif
  1029. }
  1030. #endif /* REQUIRE_VCI_ETHERBOOT */
  1031. else if (NON_ENCAP_OPT c == RFC2132_MSG_TYPE)
  1032. dhcp_reply=*(p+2);
  1033. else if (NON_ENCAP_OPT c == RFC2132_SRV_ID)
  1034. memcpy(&dhcp_server, p+2, sizeof(in_addr));
  1035. #endif /* NO_DHCP_SUPPORT */
  1036. else if (NON_ENCAP_OPT c == RFC1533_HOSTNAME) {
  1037. hostname = p + 2;
  1038. hostnamelen = *(p + 1);
  1039. }
  1040. else if (ENCAP_OPT c == RFC1533_VENDOR_MAGIC
  1041. && TAG_LEN(p) >= 6 &&
  1042. !memcmp(p+2,vendorext_magic,4) &&
  1043. p[6] == RFC1533_VENDOR_MAJOR
  1044. )
  1045. vendorext_isvalid++;
  1046. else if (c == RFC1533_VENDOR_ADDPARM) {
  1047. /* This tag intentionally works for BOTH the encapsulated and
  1048. * non-encapsulated case, since the current menu code (in mknbi)
  1049. * creates this tag without encapsulation. In the future both the
  1050. * menu from mknbi and this code should learn about the proper
  1051. * encapsulation (which will require substantial changes to various
  1052. * stuff from mknbi, which will break compatibility with older
  1053. * versions of Etherboot). */
  1054. addparam = p + 2;
  1055. addparamlen = *(p + 1);
  1056. }
  1057. else if (NON_ENCAP_OPT c == RFC1533_VENDOR_ETHERBOOT_ENCAP) {
  1058. in_encapsulated_options = 1;
  1059. decode_rfc1533(p+2, 0, TAG_LEN(p), -1);
  1060. in_encapsulated_options = 0;
  1061. }
  1062. #ifdef IMAGE_FREEBSD
  1063. else if (NON_ENCAP_OPT c == RFC1533_VENDOR_HOWTO)
  1064. freebsd_howto = ((p[2]*256+p[3])*256+p[4])*256+p[5];
  1065. else if (NON_ENCAP_OPT c == RFC1533_VENDOR_KERNEL_ENV){
  1066. if(*(p + 1) < sizeof(freebsd_kernel_env)){
  1067. memcpy(freebsd_kernel_env,p+2,*(p+1));
  1068. }else{
  1069. printf("Only support %ld bytes in Kernel Env\n",
  1070. sizeof(freebsd_kernel_env));
  1071. }
  1072. }
  1073. #endif
  1074. else if (NON_ENCAP_OPT c == RFC1533_DNS) {
  1075. // TODO: Copy the DNS IP somewhere reasonable
  1076. if (TAG_LEN(p) >= sizeof(in_addr))
  1077. memcpy(&arptable[ARP_NAMESERVER].ipaddr, p+2, sizeof(in_addr));
  1078. }
  1079. else {
  1080. #if 0
  1081. unsigned char *q;
  1082. printf("Unknown RFC1533-tag ");
  1083. for(q=p;q<p+2+TAG_LEN(p);q++)
  1084. printf("%hhX ",*q);
  1085. putchar('\n');
  1086. #endif
  1087. }
  1088. p += TAG_LEN(p) + 2;
  1089. }
  1090. extdata = extend = endp;
  1091. if (block <= 0 && extpath != NULL) {
  1092. char fname[64];
  1093. memcpy(fname, extpath+2, TAG_LEN(extpath));
  1094. fname[(int)TAG_LEN(extpath)] = '\0';
  1095. printf("Loading BOOTP-extension file: %s\n",fname);
  1096. #warning "BOOTP extension files are broken"
  1097. /* tftp(fname, decode_rfc1533); */
  1098. }
  1099. return 1; /* proceed with next block */
  1100. }
  1101. /* FIXME double check TWO_SECOND_DIVISOR */
  1102. #define TWO_SECOND_DIVISOR (RAND_MAX/TICKS_PER_SEC)
  1103. /**************************************************************************
  1104. RFC2131_SLEEP_INTERVAL - sleep for expotentially longer times (base << exp) +- 1 sec)
  1105. **************************************************************************/
  1106. long rfc2131_sleep_interval(long base, int exp)
  1107. {
  1108. unsigned long tmo;
  1109. if (exp > BACKOFF_LIMIT)
  1110. exp = BACKOFF_LIMIT;
  1111. tmo = (base << exp) + (TICKS_PER_SEC - (random()/TWO_SECOND_DIVISOR));
  1112. return tmo;
  1113. }