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.

nic.c 37KB

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