Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

nic.c 54KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778
  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 "nic.h"
  17. #include "elf.h" /* FOR EM_CURRENT */
  18. struct arptable_t arptable[MAX_ARP];
  19. #if MULTICAST_LEVEL2
  20. unsigned long last_igmpv1 = 0;
  21. struct igmptable_t igmptable[MAX_IGMP];
  22. #endif
  23. /* Put rom_info in .nocompress section so romprefix.S can write to it */
  24. struct rom_info rom __attribute__ ((section (".text16.nocompress"))) = {0,0};
  25. static unsigned long netmask;
  26. /* Used by nfs.c */
  27. char *hostname = "";
  28. int hostnamelen = 0;
  29. static uint32_t xid;
  30. unsigned char *end_of_rfc1533 = NULL;
  31. static int vendorext_isvalid;
  32. static const unsigned char vendorext_magic[] = {0xE4,0x45,0x74,0x68}; /* äEth */
  33. static const unsigned char broadcast[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  34. static const in_addr zeroIP = { 0L };
  35. struct bootpd_t bootp_data;
  36. #ifdef NO_DHCP_SUPPORT
  37. static unsigned char rfc1533_cookie[5] = { RFC1533_COOKIE, RFC1533_END };
  38. #else /* !NO_DHCP_SUPPORT */
  39. static int dhcp_reply;
  40. static in_addr dhcp_server = { 0L };
  41. static in_addr dhcp_addr = { 0L };
  42. static unsigned char rfc1533_cookie[] = { RFC1533_COOKIE };
  43. #define DHCP_MACHINE_INFO_SIZE (sizeof dhcp_machine_info)
  44. static unsigned char dhcp_machine_info[] = {
  45. /* Our enclosing DHCP tag */
  46. RFC1533_VENDOR_ETHERBOOT_ENCAP, 11,
  47. /* Our boot device */
  48. RFC1533_VENDOR_NIC_DEV_ID, 5, PCI_BUS_TYPE, 0, 0, 0, 0,
  49. /* Our current architecture */
  50. RFC1533_VENDOR_ARCH, 2, EM_CURRENT & 0xff, (EM_CURRENT >> 8) & 0xff,
  51. #ifdef EM_CURRENT_64
  52. /* The 64bit version of our current architecture */
  53. RFC1533_VENDOR_ARCH, 2, EM_CURRENT_64 & 0xff, (EM_CURRENT_64 >> 8) & 0xff,
  54. #undef DHCP_MACHINE_INFO_SIZE
  55. #define DHCP_MACHINE_INFO_SIZE (sizeof(dhcp_machine_info) - (EM_CURRENT_64_PRESENT? 0: 4))
  56. #endif /* EM_CURRENT_64 */
  57. };
  58. static const unsigned char dhcpdiscover[] = {
  59. RFC2132_MSG_TYPE,1,DHCPDISCOVER,
  60. RFC2132_MAX_SIZE,2, /* request as much as we can */
  61. ETH_MAX_MTU / 256, ETH_MAX_MTU % 256,
  62. #ifdef PXE_DHCP_STRICT
  63. RFC3679_PXE_CLIENT_UUID,RFC3679_PXE_CLIENT_UUID_LENGTH,RFC3679_PXE_CLIENT_UUID_DEFAULT,
  64. RFC3679_PXE_CLIENT_ARCH,RFC3679_PXE_CLIENT_ARCH_LENGTH,RFC3679_PXE_CLIENT_ARCH_IAX86PC,
  65. RFC3679_PXE_CLIENT_NDI, RFC3679_PXE_CLIENT_NDI_LENGTH, RFC3679_PXE_CLIENT_NDI_21,
  66. RFC2132_VENDOR_CLASS_ID,RFC2132_VENDOR_CLASS_ID_PXE_LENGTH,RFC2132_VENDOR_CLASS_ID_PXE,
  67. #else
  68. RFC2132_VENDOR_CLASS_ID,13,'E','t','h','e','r','b','o','o','t',
  69. '-',VERSION_MAJOR+'0','.',VERSION_MINOR+'0',
  70. #endif /* PXE_DHCP_STRICT */
  71. #ifdef DHCP_CLIENT_ID
  72. /* Client ID Option */
  73. RFC2132_CLIENT_ID, ( DHCP_CLIENT_ID_LEN + 1 ),
  74. DHCP_CLIENT_ID_TYPE, DHCP_CLIENT_ID,
  75. #endif /* DHCP_CLIENT_ID */
  76. #ifdef DHCP_USER_CLASS
  77. /* User Class Option */
  78. RFC3004_USER_CLASS, DHCP_USER_CLASS_LEN, DHCP_USER_CLASS,
  79. #endif /* DHCP_USER_CLASS */
  80. RFC2132_PARAM_LIST,
  81. #define DHCPDISCOVER_PARAMS_BASE 4
  82. #ifdef PXE_DHCP_STRICT
  83. #define DHCPDISCOVER_PARAMS_PXE ( 1 + 8 )
  84. #else
  85. #define DHCPDISCOVER_PARAMS_PXE 0
  86. #endif /* PXE_DHCP_STRICT */
  87. #ifdef DNS_RESOLVER
  88. #define DHCPDISCOVER_PARAMS_DNS 1
  89. #else
  90. #define DHCPDISCOVER_PARAMS_DNS 0
  91. #endif /* DNS_RESOLVER */
  92. ( DHCPDISCOVER_PARAMS_BASE +
  93. DHCPDISCOVER_PARAMS_PXE+
  94. DHCPDISCOVER_PARAMS_DNS ),
  95. RFC1533_NETMASK,
  96. RFC1533_GATEWAY,
  97. RFC1533_HOSTNAME,
  98. RFC1533_VENDOR
  99. #ifdef PXE_DHCP_STRICT
  100. ,RFC2132_VENDOR_CLASS_ID,
  101. RFC1533_VENDOR_PXE_OPT128,
  102. RFC1533_VENDOR_PXE_OPT129,
  103. RFC1533_VENDOR_PXE_OPT130,
  104. RFC1533_VENDOR_PXE_OPT131,
  105. RFC1533_VENDOR_PXE_OPT132,
  106. RFC1533_VENDOR_PXE_OPT133,
  107. RFC1533_VENDOR_PXE_OPT134,
  108. RFC1533_VENDOR_PXE_OPT135
  109. #endif /* PXE_DHCP_STRICT */
  110. #ifdef DNS_RESOLVER
  111. ,RFC1533_DNS
  112. #endif
  113. };
  114. static const unsigned char dhcprequest [] = {
  115. RFC2132_MSG_TYPE,1,DHCPREQUEST,
  116. RFC2132_SRV_ID,4,0,0,0,0,
  117. RFC2132_REQ_ADDR,4,0,0,0,0,
  118. RFC2132_MAX_SIZE,2, /* request as much as we can */
  119. ETH_MAX_MTU / 256, ETH_MAX_MTU % 256,
  120. #ifdef PXE_DHCP_STRICT
  121. RFC3679_PXE_CLIENT_UUID,RFC3679_PXE_CLIENT_UUID_LENGTH,RFC3679_PXE_CLIENT_UUID_DEFAULT,
  122. RFC3679_PXE_CLIENT_ARCH,RFC3679_PXE_CLIENT_ARCH_LENGTH,RFC3679_PXE_CLIENT_ARCH_IAX86PC,
  123. RFC3679_PXE_CLIENT_NDI, RFC3679_PXE_CLIENT_NDI_LENGTH, RFC3679_PXE_CLIENT_NDI_21,
  124. RFC2132_VENDOR_CLASS_ID,RFC2132_VENDOR_CLASS_ID_PXE_LENGTH,RFC2132_VENDOR_CLASS_ID_PXE,
  125. #else
  126. RFC2132_VENDOR_CLASS_ID,13,'E','t','h','e','r','b','o','o','t',
  127. '-',VERSION_MAJOR+'0','.',VERSION_MINOR+'0',
  128. #endif /* PXE_DHCP_STRICT */
  129. #ifdef DHCP_CLIENT_ID
  130. /* Client ID Option */
  131. RFC2132_CLIENT_ID, ( DHCP_CLIENT_ID_LEN + 1 ),
  132. DHCP_CLIENT_ID_TYPE, DHCP_CLIENT_ID,
  133. #endif /* DHCP_CLIENT_ID */
  134. #ifdef DHCP_USER_CLASS
  135. /* User Class Option */
  136. RFC3004_USER_CLASS, DHCP_USER_CLASS_LEN, DHCP_USER_CLASS,
  137. #endif /* DHCP_USER_CLASS */
  138. /* request parameters */
  139. RFC2132_PARAM_LIST,
  140. #define DHCPREQUEST_PARAMS_BASE 5
  141. #ifdef PXE_DHCP_STRICT
  142. #define DHCPREQUEST_PARAMS_PXE 1
  143. #define DHCPREQUEST_PARAMS_VENDOR_PXE 8
  144. #define DHCPREQUEST_PARAMS_VENDOR_EB 0
  145. #else
  146. #define DHCPREQUEST_PARAMS_PXE 0
  147. #define DHCPREQUEST_PARAMS_VENDOR_PXE 0
  148. #define DHCPREQUEST_PARAMS_VENDOR_EB 4
  149. #endif /* PXE_DHCP_STRICT */
  150. #ifdef IMAGE_FREEBSD
  151. #define DHCPREQUEST_PARAMS_FREEBSD 2
  152. #else
  153. #define DHCPREQUEST_PARAMS_FREEBSD 0
  154. #endif /* IMAGE_FREEBSD */
  155. #ifdef DNS_RESOLVER
  156. #define DHCPREQUEST_PARAMS_DNS 1
  157. #else
  158. #define DHCPREQUEST_PARAMS_DNS 0
  159. #endif /* DNS_RESOLVER */
  160. ( DHCPREQUEST_PARAMS_BASE +
  161. DHCPREQUEST_PARAMS_PXE +
  162. DHCPREQUEST_PARAMS_VENDOR_PXE +
  163. DHCPREQUEST_PARAMS_VENDOR_EB +
  164. DHCPREQUEST_PARAMS_DNS +
  165. DHCPREQUEST_PARAMS_FREEBSD ),
  166. /* 5 Standard parameters */
  167. RFC1533_NETMASK,
  168. RFC1533_GATEWAY,
  169. RFC1533_HOSTNAME,
  170. RFC1533_VENDOR,
  171. RFC1533_ROOTPATH, /* only passed to the booted image */
  172. #ifndef PXE_DHCP_STRICT
  173. /* 4 Etherboot vendortags */
  174. RFC1533_VENDOR_MAGIC,
  175. RFC1533_VENDOR_ADDPARM,
  176. RFC1533_VENDOR_ETHDEV,
  177. RFC1533_VENDOR_ETHERBOOT_ENCAP,
  178. #endif /* ! PXE_DHCP_STRICT */
  179. #ifdef IMAGE_FREEBSD
  180. /* 2 FreeBSD options */
  181. RFC1533_VENDOR_HOWTO,
  182. RFC1533_VENDOR_KERNEL_ENV,
  183. #endif
  184. #ifdef DNS_RESOLVER
  185. /* 1 DNS option */
  186. RFC1533_DNS,
  187. #endif
  188. #ifdef PXE_DHCP_STRICT
  189. RFC2132_VENDOR_CLASS_ID,
  190. RFC1533_VENDOR_PXE_OPT128,
  191. RFC1533_VENDOR_PXE_OPT129,
  192. RFC1533_VENDOR_PXE_OPT130,
  193. RFC1533_VENDOR_PXE_OPT131,
  194. RFC1533_VENDOR_PXE_OPT132,
  195. RFC1533_VENDOR_PXE_OPT133,
  196. RFC1533_VENDOR_PXE_OPT134,
  197. RFC1533_VENDOR_PXE_OPT135,
  198. #endif /* PXE_DHCP_STRICT */
  199. };
  200. #ifdef PXE_EXPORT
  201. static const unsigned char proxydhcprequest [] = {
  202. RFC2132_MSG_TYPE,1,DHCPREQUEST,
  203. RFC2132_MAX_SIZE,2, /* request as much as we can */
  204. ETH_MAX_MTU / 256, ETH_MAX_MTU % 256,
  205. #ifdef PXE_DHCP_STRICT
  206. RFC3679_PXE_CLIENT_UUID,RFC3679_PXE_CLIENT_UUID_LENGTH,RFC3679_PXE_CLIENT_UUID_DEFAULT,
  207. RFC3679_PXE_CLIENT_ARCH,RFC3679_PXE_CLIENT_ARCH_LENGTH,RFC3679_PXE_CLIENT_ARCH_IAX86PC,
  208. RFC3679_PXE_CLIENT_NDI, RFC3679_PXE_CLIENT_NDI_LENGTH, RFC3679_PXE_CLIENT_NDI_21,
  209. RFC2132_VENDOR_CLASS_ID,RFC2132_VENDOR_CLASS_ID_PXE_LENGTH,RFC2132_VENDOR_CLASS_ID_PXE,
  210. #endif /* PXE_DHCP_STRICT */
  211. };
  212. #endif
  213. #ifdef REQUIRE_VCI_ETHERBOOT
  214. int vci_etherboot;
  215. #endif
  216. #endif /* NO_DHCP_SUPPORT */
  217. static int dummy(void *unused __unused)
  218. {
  219. return (0);
  220. }
  221. /* Careful. We need an aligned buffer to avoid problems on machines
  222. * that care about alignment. To trivally align the ethernet data
  223. * (the ip hdr and arp requests) we offset the packet by 2 bytes.
  224. * leaving the ethernet data 16 byte aligned. Beyond this
  225. * we use memmove but this makes the common cast simple and fast.
  226. */
  227. static char packet[ETH_FRAME_LEN + ETH_DATA_ALIGN] __aligned;
  228. struct nic nic =
  229. {
  230. {
  231. 0, /* dev.disable */
  232. {
  233. 0,
  234. 0,
  235. PCI_BUS_TYPE,
  236. }, /* dev.devid */
  237. 0, /* index */
  238. 0, /* type */
  239. PROBE_FIRST, /* how_pobe */
  240. PROBE_NONE, /* to_probe */
  241. 0, /* failsafe */
  242. 0, /* type_index */
  243. {}, /* state */
  244. },
  245. (int (*)(struct nic *, int))dummy, /* poll */
  246. (void (*)(struct nic *, const char *,
  247. unsigned int, unsigned int,
  248. const char *))dummy, /* transmit */
  249. (void (*)(struct nic *,
  250. irq_action_t))dummy, /* irq */
  251. 0, /* flags */
  252. &rom, /* rom_info */
  253. arptable[ARP_CLIENT].node, /* node_addr */
  254. packet + ETH_DATA_ALIGN, /* packet */
  255. 0, /* packetlen */
  256. 0, /* ioaddr */
  257. 0, /* irqno */
  258. 0, /* priv_data */
  259. };
  260. #ifdef RARP_NOT_BOOTP
  261. static int rarp(void);
  262. #else
  263. static int bootp(void);
  264. #endif
  265. static unsigned short tcpudpchksum(struct iphdr *ip);
  266. int eth_probe(struct dev *dev)
  267. {
  268. return probe(dev);
  269. }
  270. int eth_poll(int retrieve)
  271. {
  272. return ((*nic.poll)(&nic, retrieve));
  273. }
  274. void eth_transmit(const char *d, unsigned int t, unsigned int s, const void *p)
  275. {
  276. (*nic.transmit)(&nic, d, t, s, p);
  277. if (t == ETH_P_IP) twiddle();
  278. }
  279. void eth_disable(void)
  280. {
  281. #ifdef MULTICAST_LEVEL2
  282. int i;
  283. for(i = 0; i < MAX_IGMP; i++) {
  284. leave_group(i);
  285. }
  286. #endif
  287. disable(&nic.dev);
  288. }
  289. void eth_irq (irq_action_t action)
  290. {
  291. (*nic.irq)(&nic,action);
  292. }
  293. /*
  294. * Find out what our boot parameters are
  295. */
  296. int eth_load_configuration(struct dev *dev __unused)
  297. {
  298. int server_found;
  299. /* Find a server to get BOOTP reply from */
  300. #ifdef RARP_NOT_BOOTP
  301. printf("Searching for server (RARP)...");
  302. #else
  303. #ifndef NO_DHCP_SUPPORT
  304. printf("Searching for server (DHCP)...");
  305. #else
  306. printf("Searching for server (BOOTP)...");
  307. #endif
  308. #endif
  309. #ifdef RARP_NOT_BOOTP
  310. server_found = rarp();
  311. #else
  312. server_found = bootp();
  313. #endif
  314. if (!server_found) {
  315. printf("No Server found\n");
  316. longjmp(restart_etherboot, -1);
  317. }
  318. return 0;
  319. }
  320. /**************************************************************************
  321. LOAD - Try to get booted
  322. **************************************************************************/
  323. int eth_load(struct dev *dev __unused)
  324. {
  325. const char *kernel;
  326. printf("\nMe: %@", arptable[ARP_CLIENT].ipaddr.s_addr );
  327. #ifndef NO_DHCP_SUPPORT
  328. printf(", DHCP: %@", dhcp_server );
  329. #ifdef PXE_EXPORT
  330. if (arptable[ARP_PROXYDHCP].ipaddr.s_addr)
  331. printf(" (& %@)",
  332. arptable[ARP_PROXYDHCP].ipaddr.s_addr);
  333. #endif /* PXE_EXPORT */
  334. #endif /* ! NO_DHCP_SUPPORT */
  335. printf(", TFTP: %@", arptable[ARP_SERVER].ipaddr.s_addr);
  336. if (BOOTP_DATA_ADDR->bootp_reply.bp_giaddr.s_addr)
  337. printf(", Relay: %@",
  338. BOOTP_DATA_ADDR->bootp_reply.bp_giaddr.s_addr);
  339. if (arptable[ARP_GATEWAY].ipaddr.s_addr)
  340. printf(", Gateway %@", arptable[ARP_GATEWAY].ipaddr.s_addr);
  341. #ifdef DNS_RESOLVER
  342. if (arptable[ARP_NAMESERVER].ipaddr.s_addr)
  343. printf(", Nameserver %@", arptable[ARP_NAMESERVER].ipaddr.s_addr);
  344. #endif
  345. putchar('\n');
  346. #ifdef MDEBUG
  347. printf("\n=>>"); getchar();
  348. #endif
  349. /* Now use TFTP to load file */
  350. #ifdef DOWNLOAD_PROTO_NFS
  351. rpc_init();
  352. #endif
  353. kernel = KERNEL_BUF[0] == '\0' ?
  354. #ifdef DEFAULT_BOOTFILE
  355. DEFAULT_BOOTFILE
  356. #else
  357. NULL
  358. #endif
  359. : KERNEL_BUF;
  360. if ( kernel ) {
  361. loadkernel(kernel); /* We don't return except on error */
  362. printf("Unable to load file.\n");
  363. } else {
  364. printf("No filename\n");
  365. }
  366. interruptible_sleep(2); /* lay off the server for a while */
  367. longjmp(restart_etherboot, -1);
  368. }
  369. /**************************************************************************
  370. DEFAULT_NETMASK - Return default netmask for IP address
  371. **************************************************************************/
  372. static inline unsigned long default_netmask(void)
  373. {
  374. int net = ntohl(arptable[ARP_CLIENT].ipaddr.s_addr) >> 24;
  375. if (net <= 127)
  376. return(htonl(0xff000000));
  377. else if (net < 192)
  378. return(htonl(0xffff0000));
  379. else
  380. return(htonl(0xffffff00));
  381. }
  382. /**************************************************************************
  383. IP_TRANSMIT - Send an IP datagram
  384. **************************************************************************/
  385. static int await_arp(int ival, void *ptr,
  386. unsigned short ptype, struct iphdr *ip __unused, struct udphdr *udp __unused,
  387. struct tcphdr *tcp __unused)
  388. {
  389. struct arprequest *arpreply;
  390. if (ptype != ETH_P_ARP)
  391. return 0;
  392. if (nic.packetlen < ETH_HLEN + sizeof(struct arprequest))
  393. return 0;
  394. arpreply = (struct arprequest *)&nic.packet[ETH_HLEN];
  395. if (arpreply->opcode != htons(ARP_REPLY))
  396. return 0;
  397. if (memcmp(arpreply->sipaddr, ptr, sizeof(in_addr)) != 0)
  398. return 0;
  399. memcpy(arptable[ival].node, arpreply->shwaddr, ETH_ALEN);
  400. return 1;
  401. }
  402. int ip_transmit(int len, const void *buf)
  403. {
  404. unsigned long destip;
  405. struct iphdr *ip;
  406. struct arprequest arpreq;
  407. int arpentry, i;
  408. int retry;
  409. ip = (struct iphdr *)buf;
  410. destip = ip->dest.s_addr;
  411. if (destip == IP_BROADCAST) {
  412. eth_transmit(broadcast, ETH_P_IP, len, buf);
  413. #ifdef MULTICAST_LEVEL1
  414. } else if ((destip & htonl(MULTICAST_MASK)) == htonl(MULTICAST_NETWORK)) {
  415. unsigned char multicast[6];
  416. unsigned long hdestip;
  417. hdestip = ntohl(destip);
  418. multicast[0] = 0x01;
  419. multicast[1] = 0x00;
  420. multicast[2] = 0x5e;
  421. multicast[3] = (hdestip >> 16) & 0x7;
  422. multicast[4] = (hdestip >> 8) & 0xff;
  423. multicast[5] = hdestip & 0xff;
  424. eth_transmit(multicast, ETH_P_IP, len, buf);
  425. #endif
  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(IP);
  443. arpreq.hwlen = ETH_ALEN;
  444. arpreq.protolen = 4;
  445. arpreq.opcode = htons(ARP_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. #ifdef DOWNLOAD_PROTO_HTTP
  500. void build_tcp_hdr(unsigned long destip, unsigned int srcsock,
  501. unsigned int destsock, long send_seq, long recv_seq,
  502. int window, int flags, int ttl, int len, const void *buf)
  503. {
  504. struct iphdr *ip;
  505. struct tcphdr *tcp;
  506. ip = (struct iphdr *)buf;
  507. build_ip_hdr(destip, ttl, IP_TCP, 0, len, buf);
  508. tcp = (struct tcphdr *)(ip + 1);
  509. tcp->src = htons(srcsock);
  510. tcp->dst = htons(destsock);
  511. tcp->seq = htonl(send_seq);
  512. tcp->ack = htonl(recv_seq);
  513. tcp->ctrl = htons(flags + (5 << 12)); /* No TCP options */
  514. tcp->window = htons(window);
  515. tcp->chksum = 0;
  516. if ((tcp->chksum = tcpudpchksum(ip)) == 0)
  517. tcp->chksum = 0xffff;
  518. }
  519. #endif
  520. /**************************************************************************
  521. UDP_TRANSMIT - Send an UDP datagram
  522. **************************************************************************/
  523. int udp_transmit(unsigned long destip, unsigned int srcsock,
  524. unsigned int destsock, int len, const void *buf)
  525. {
  526. build_udp_hdr(destip, srcsock, destsock, 60, len, buf);
  527. return ip_transmit(len, buf);
  528. }
  529. /**************************************************************************
  530. TCP_TRANSMIT - Send a TCP packet
  531. **************************************************************************/
  532. #ifdef DOWNLOAD_PROTO_HTTP
  533. int tcp_transmit(unsigned long destip, unsigned int srcsock,
  534. unsigned int destsock, long send_seq, long recv_seq,
  535. int window, int flags, int len, const void *buf)
  536. {
  537. build_tcp_hdr(destip, srcsock, destsock, send_seq, recv_seq,
  538. window, flags, 60, len, buf);
  539. return ip_transmit(len, buf);
  540. }
  541. int tcp_reset(struct iphdr *ip) {
  542. struct tcphdr *tcp = (struct tcphdr *)(ip + 1);
  543. char buf[sizeof(struct iphdr) + sizeof(struct tcphdr)];
  544. if (!(tcp->ctrl & htons(RST))) {
  545. long seq = ntohl(tcp->seq) + ntohs(ip->len) -
  546. sizeof(struct iphdr) -
  547. ((ntohs(tcp->ctrl) >> 10) & 0x3C);
  548. if (tcp->ctrl & htons(SYN|FIN))
  549. seq++;
  550. return tcp_transmit(ntohl(ip->src.s_addr),
  551. ntohs(tcp->dst), ntohs(tcp->src),
  552. tcp->ctrl&htons(ACK) ? ntohl(tcp->ack) : 0,
  553. seq, TCP_MAX_WINDOW, RST, sizeof(buf), buf);
  554. }
  555. return (1);
  556. }
  557. #endif
  558. /**************************************************************************
  559. QDRAIN - clear the nic's receive queue
  560. **************************************************************************/
  561. static int await_qdrain(int ival __unused, void *ptr __unused,
  562. unsigned short ptype __unused,
  563. struct iphdr *ip __unused, struct udphdr *udp __unused,
  564. struct tcphdr *tcp __unused)
  565. {
  566. return 0;
  567. }
  568. void rx_qdrain(void)
  569. {
  570. /* Clear out the Rx queue first. It contains nothing of interest,
  571. * except possibly ARP requests from the DHCP/TFTP server. We use
  572. * polling throughout Etherboot, so some time may have passed since we
  573. * last polled the receive queue, which may now be filled with
  574. * broadcast packets. This will cause the reply to the packets we are
  575. * about to send to be lost immediately. Not very clever. */
  576. await_reply(await_qdrain, 0, NULL, 0);
  577. }
  578. #ifdef DOWNLOAD_PROTO_TFTP
  579. /**************************************************************************
  580. TFTP - Download extended BOOTP data, or kernel image
  581. **************************************************************************/
  582. static int await_tftp(int ival, void *ptr __unused,
  583. unsigned short ptype __unused, struct iphdr *ip, struct udphdr *udp,
  584. struct tcphdr *tcp __unused)
  585. {
  586. if (!udp) {
  587. return 0;
  588. }
  589. if (arptable[ARP_CLIENT].ipaddr.s_addr != ip->dest.s_addr)
  590. return 0;
  591. if (ntohs(udp->dest) != ival)
  592. return 0;
  593. return 1;
  594. }
  595. int tftp ( const char *name,
  596. int (*fnc)(unsigned char *, unsigned int, unsigned int, int) )
  597. {
  598. struct tftpreq_info_t request_data =
  599. { name, TFTP_PORT, TFTP_MAX_PACKET };
  600. struct tftpreq_info_t *request = &request_data;
  601. struct tftpblk_info_t block;
  602. int rc;
  603. while ( tftp_block ( request, &block ) ) {
  604. request = NULL; /* Send request only once */
  605. rc = fnc ( block.data, block.block, block.len, block.eof );
  606. if ( rc <= 0 ) return (rc);
  607. if ( block.eof ) {
  608. /* fnc should not have returned */
  609. printf ( "TFTP download complete, but\n" );
  610. return (0);
  611. }
  612. }
  613. return (0);
  614. }
  615. int tftp_block ( struct tftpreq_info_t *request, struct tftpblk_info_t *block )
  616. {
  617. static unsigned short lport = 2000; /* local port */
  618. static unsigned short rport = TFTP_PORT; /* remote port */
  619. struct tftp_t *rcvd = NULL;
  620. static struct tftpreq_t xmit;
  621. static unsigned short xmitlen = 0;
  622. static unsigned short blockidx = 0; /* Last block received */
  623. static unsigned short retry = 0; /* Retry attempts on last block */
  624. static int blksize = 0;
  625. unsigned short recvlen = 0;
  626. /* If this is a new request (i.e. if name is set), fill in
  627. * transmit block with RRQ and send it.
  628. */
  629. if ( request ) {
  630. rx_qdrain(); /* Flush receive queue */
  631. xmit.opcode = htons(TFTP_RRQ);
  632. xmitlen = (void*)&xmit.u.rrq - (void*)&xmit +
  633. sprintf((char*)xmit.u.rrq, "%s%coctet%cblksize%c%d",
  634. request->name, 0, 0, 0, request->blksize)
  635. + 1; /* null terminator */
  636. blockidx = 0; /* Reset counters */
  637. retry = 0;
  638. blksize = TFTP_DEFAULTSIZE_PACKET;
  639. lport++; /* Use new local port */
  640. rport = request->port;
  641. if ( !udp_transmit(arptable[ARP_SERVER].ipaddr.s_addr, lport,
  642. rport, xmitlen, &xmit) )
  643. return (0);
  644. }
  645. /* Exit if no transfer in progress */
  646. if ( !blksize ) return (0);
  647. /* Loop to wait until we get a packet we're interested in */
  648. block->data = NULL; /* Used as flag */
  649. while ( block->data == NULL ) {
  650. long timeout = rfc2131_sleep_interval ( blockidx ? TFTP_REXMT :
  651. TIMEOUT, retry );
  652. if ( !await_reply(await_tftp, lport, NULL, timeout) ) {
  653. /* No packet received */
  654. if ( retry++ > MAX_TFTP_RETRIES ) break;
  655. /* Retransmit last packet */
  656. if ( !blockidx ) lport++; /* New lport if new RRQ */
  657. if ( !udp_transmit(arptable[ARP_SERVER].ipaddr.s_addr,
  658. lport, rport, xmitlen, &xmit) )
  659. return (0);
  660. continue; /* Back to waiting for packet */
  661. }
  662. /* Packet has been received */
  663. rcvd = (struct tftp_t *)&nic.packet[ETH_HLEN];
  664. recvlen = ntohs(rcvd->udp.len) - sizeof(struct udphdr)
  665. - sizeof(rcvd->opcode);
  666. rport = ntohs(rcvd->udp.src);
  667. retry = 0; /* Reset retry counter */
  668. switch ( htons(rcvd->opcode) ) {
  669. case TFTP_ERROR : {
  670. printf ( "TFTP error %d (%s)\n",
  671. ntohs(rcvd->u.err.errcode),
  672. rcvd->u.err.errmsg );
  673. return (0); /* abort */
  674. }
  675. case TFTP_OACK : {
  676. const char *p = rcvd->u.oack.data;
  677. const char *e = p + recvlen - 10; /* "blksize\0\d\0" */
  678. *((char*)(p+recvlen-1)) = '\0'; /* Force final 0 */
  679. if ( blockidx || !request ) break; /* Too late */
  680. if ( recvlen <= TFTP_MAX_PACKET ) /* sanity */ {
  681. /* Check for blksize option honoured */
  682. while ( p < e ) {
  683. if ( strcasecmp("blksize",p) == 0 &&
  684. p[7] == '\0' ) {
  685. blksize = strtoul(p+8,&p,10);
  686. p++; /* skip null */
  687. }
  688. while ( *(p++) ) {};
  689. }
  690. }
  691. if ( blksize < TFTP_DEFAULTSIZE_PACKET || blksize > request->blksize ) {
  692. /* Incorrect blksize - error and abort */
  693. xmit.opcode = htons(TFTP_ERROR);
  694. xmit.u.err.errcode = 8;
  695. xmitlen = (void*)&xmit.u.err.errmsg
  696. - (void*)&xmit
  697. + sprintf((char*)xmit.u.err.errmsg,
  698. "RFC1782 error")
  699. + 1;
  700. udp_transmit(
  701. arptable[ARP_SERVER].ipaddr.s_addr,
  702. lport, rport, xmitlen, &xmit);
  703. return (0);
  704. }
  705. } break;
  706. case TFTP_DATA :
  707. if ( ntohs(rcvd->u.data.block) != ( blockidx + 1 ) )
  708. break; /* Re-ACK last block sent */
  709. if ( recvlen > ( blksize+sizeof(rcvd->u.data.block) ) )
  710. break; /* Too large; ignore */
  711. block->data = rcvd->u.data.download;
  712. block->block = ++blockidx;
  713. block->len = recvlen - sizeof(rcvd->u.data.block);
  714. block->eof = ( (unsigned short)block->len < blksize );
  715. /* If EOF, zero blksize to indicate transfer done */
  716. if ( block->eof ) blksize = 0;
  717. break;
  718. default: break; /* Do nothing */
  719. }
  720. /* Send ACK */
  721. xmit.opcode = htons(TFTP_ACK);
  722. xmit.u.ack.block = htons(blockidx);
  723. xmitlen = TFTP_MIN_PACKET;
  724. udp_transmit ( arptable[ARP_SERVER].ipaddr.s_addr,
  725. lport, rport, xmitlen, &xmit );
  726. }
  727. return ( block->data ? 1 : 0 );
  728. }
  729. #endif /* DOWNLOAD_PROTO_TFTP */
  730. #ifdef RARP_NOT_BOOTP
  731. /**************************************************************************
  732. RARP - Get my IP address and load information
  733. **************************************************************************/
  734. static int await_rarp(int ival, void *ptr,
  735. unsigned short ptype, struct iphdr *ip, struct udphdr *udp,
  736. struct tcphdr *tcp __unused)
  737. {
  738. struct arprequest *arpreply;
  739. if (ptype != ETH_P_RARP)
  740. return 0;
  741. if (nic.packetlen < ETH_HLEN + sizeof(struct arprequest))
  742. return 0;
  743. arpreply = (struct arprequest *)&nic.packet[ETH_HLEN];
  744. if (arpreply->opcode != htons(RARP_REPLY))
  745. return 0;
  746. if ((arpreply->opcode == htons(RARP_REPLY)) &&
  747. (memcmp(arpreply->thwaddr, ptr, ETH_ALEN) == 0)) {
  748. memcpy(arptable[ARP_SERVER].node, arpreply->shwaddr, ETH_ALEN);
  749. memcpy(&arptable[ARP_SERVER].ipaddr, arpreply->sipaddr, sizeof(in_addr));
  750. memcpy(&arptable[ARP_CLIENT].ipaddr, arpreply->tipaddr, sizeof(in_addr));
  751. return 1;
  752. }
  753. return 0;
  754. }
  755. static int rarp(void)
  756. {
  757. int retry;
  758. /* arp and rarp requests share the same packet structure. */
  759. struct arprequest rarpreq;
  760. memset(&rarpreq, 0, sizeof(rarpreq));
  761. rarpreq.hwtype = htons(1);
  762. rarpreq.protocol = htons(IP);
  763. rarpreq.hwlen = ETH_ALEN;
  764. rarpreq.protolen = 4;
  765. rarpreq.opcode = htons(RARP_REQUEST);
  766. memcpy(&rarpreq.shwaddr, arptable[ARP_CLIENT].node, ETH_ALEN);
  767. /* sipaddr is already zeroed out */
  768. memcpy(&rarpreq.thwaddr, arptable[ARP_CLIENT].node, ETH_ALEN);
  769. /* tipaddr is already zeroed out */
  770. for (retry = 0; retry < MAX_ARP_RETRIES; ++retry) {
  771. long timeout;
  772. eth_transmit(broadcast, ETH_P_RARP, sizeof(rarpreq), &rarpreq);
  773. timeout = rfc2131_sleep_interval(TIMEOUT, retry);
  774. if (await_reply(await_rarp, 0, rarpreq.shwaddr, timeout))
  775. break;
  776. }
  777. if (retry < MAX_ARP_RETRIES) {
  778. (void)sprintf(KERNEL_BUF, DEFAULT_KERNELPATH, arptable[ARP_CLIENT].ipaddr);
  779. return (1);
  780. }
  781. return (0);
  782. }
  783. #else
  784. /**************************************************************************
  785. BOOTP - Get my IP address and load information
  786. **************************************************************************/
  787. static int await_bootp(int ival __unused, void *ptr __unused,
  788. unsigned short ptype __unused, struct iphdr *ip __unused,
  789. struct udphdr *udp, struct tcphdr *tcp __unused)
  790. {
  791. struct bootp_t *bootpreply;
  792. if (!udp) {
  793. return 0;
  794. }
  795. bootpreply = (struct bootp_t *)&nic.packet[ETH_HLEN +
  796. sizeof(struct iphdr) + sizeof(struct udphdr)];
  797. if (nic.packetlen < ETH_HLEN + sizeof(struct iphdr) +
  798. sizeof(struct udphdr) +
  799. #ifdef NO_DHCP_SUPPORT
  800. sizeof(struct bootp_t)
  801. #else
  802. sizeof(struct bootp_t) - DHCP_OPT_LEN
  803. #endif /* NO_DHCP_SUPPORT */
  804. ) {
  805. return 0;
  806. }
  807. if (udp->dest != htons(BOOTP_CLIENT))
  808. return 0;
  809. if (bootpreply->bp_op != BOOTP_REPLY)
  810. return 0;
  811. if (bootpreply->bp_xid != xid)
  812. return 0;
  813. if (memcmp(&bootpreply->bp_siaddr, &zeroIP, sizeof(in_addr)) == 0)
  814. return 0;
  815. if ((memcmp(broadcast, bootpreply->bp_hwaddr, ETH_ALEN) != 0) &&
  816. (memcmp(arptable[ARP_CLIENT].node, bootpreply->bp_hwaddr, ETH_ALEN) != 0)) {
  817. return 0;
  818. }
  819. if ( bootpreply->bp_siaddr.s_addr ) {
  820. arptable[ARP_SERVER].ipaddr.s_addr = bootpreply->bp_siaddr.s_addr;
  821. memset(arptable[ARP_SERVER].node, 0, ETH_ALEN); /* Kill arp */
  822. }
  823. if ( bootpreply->bp_giaddr.s_addr ) {
  824. arptable[ARP_GATEWAY].ipaddr.s_addr = bootpreply->bp_giaddr.s_addr;
  825. memset(arptable[ARP_GATEWAY].node, 0, ETH_ALEN); /* Kill arp */
  826. }
  827. if (bootpreply->bp_yiaddr.s_addr) {
  828. /* Offer with an IP address */
  829. arptable[ARP_CLIENT].ipaddr.s_addr = bootpreply->bp_yiaddr.s_addr;
  830. #ifndef NO_DHCP_SUPPORT
  831. dhcp_addr.s_addr = bootpreply->bp_yiaddr.s_addr;
  832. #endif /* NO_DHCP_SUPPORT */
  833. netmask = default_netmask();
  834. /* bootpreply->bp_file will be copied to KERNEL_BUF in the memcpy */
  835. memcpy((char *)BOOTP_DATA_ADDR, (char *)bootpreply, sizeof(struct bootpd_t));
  836. decode_rfc1533(BOOTP_DATA_ADDR->bootp_reply.bp_vend, 0,
  837. #ifdef NO_DHCP_SUPPORT
  838. BOOTP_VENDOR_LEN + MAX_BOOTP_EXTLEN,
  839. #else
  840. DHCP_OPT_LEN + MAX_BOOTP_EXTLEN,
  841. #endif /* NO_DHCP_SUPPORT */
  842. 1);
  843. #ifdef PXE_EXPORT
  844. } else {
  845. /* Offer without an IP address - use as ProxyDHCP server */
  846. arptable[ARP_PROXYDHCP].ipaddr.s_addr = bootpreply->bp_siaddr.s_addr;
  847. memset(arptable[ARP_PROXYDHCP].node, 0, ETH_ALEN); /* Kill arp */
  848. /* Grab only the bootfile name from a ProxyDHCP packet */
  849. memcpy(KERNEL_BUF, bootpreply->bp_file, sizeof(KERNEL_BUF));
  850. #endif /* PXE_EXPORT */
  851. }
  852. #ifdef REQUIRE_VCI_ETHERBOOT
  853. if (!vci_etherboot)
  854. return (0);
  855. #endif
  856. return(1);
  857. }
  858. static int bootp(void)
  859. {
  860. int retry;
  861. #ifndef NO_DHCP_SUPPORT
  862. int reqretry;
  863. #endif /* NO_DHCP_SUPPORT */
  864. struct bootpip_t ip;
  865. unsigned long starttime;
  866. unsigned char *bp_vend;
  867. #ifndef NO_DHCP_SUPPORT
  868. dhcp_machine_info[4] = nic.dev.devid.bus_type;
  869. dhcp_machine_info[5] = nic.dev.devid.vendor_id & 0xff;
  870. dhcp_machine_info[6] = ((nic.dev.devid.vendor_id) >> 8) & 0xff;
  871. dhcp_machine_info[7] = nic.dev.devid.device_id & 0xff;
  872. dhcp_machine_info[8] = ((nic.dev.devid.device_id) >> 8) & 0xff;
  873. #endif /* NO_DHCP_SUPPORT */
  874. memset(&ip, 0, sizeof(struct bootpip_t));
  875. ip.bp.bp_op = BOOTP_REQUEST;
  876. ip.bp.bp_htype = 1;
  877. ip.bp.bp_hlen = ETH_ALEN;
  878. starttime = currticks();
  879. /* Use lower 32 bits of node address, more likely to be
  880. distinct than the time since booting */
  881. memcpy(&xid, &arptable[ARP_CLIENT].node[2], sizeof(xid));
  882. ip.bp.bp_xid = xid += htonl(starttime);
  883. memcpy(ip.bp.bp_hwaddr, arptable[ARP_CLIENT].node, ETH_ALEN);
  884. #ifdef NO_DHCP_SUPPORT
  885. memcpy(ip.bp.bp_vend, rfc1533_cookie, 5); /* request RFC-style options */
  886. #else
  887. memcpy(ip.bp.bp_vend, rfc1533_cookie, sizeof rfc1533_cookie); /* request RFC-style options */
  888. memcpy(ip.bp.bp_vend + sizeof rfc1533_cookie, dhcpdiscover, sizeof dhcpdiscover);
  889. /* Append machine_info to end, in encapsulated option */
  890. bp_vend = ip.bp.bp_vend + sizeof rfc1533_cookie + sizeof dhcpdiscover;
  891. memcpy(bp_vend, dhcp_machine_info, DHCP_MACHINE_INFO_SIZE);
  892. bp_vend += DHCP_MACHINE_INFO_SIZE;
  893. *bp_vend++ = RFC1533_END;
  894. #endif /* NO_DHCP_SUPPORT */
  895. for (retry = 0; retry < MAX_BOOTP_RETRIES; ) {
  896. uint8_t my_hwaddr[ETH_ALEN];
  897. unsigned long stop_time;
  898. long remaining_time;
  899. rx_qdrain();
  900. /* Kill arptable to avoid keeping stale entries */
  901. memcpy ( my_hwaddr, arptable[ARP_CLIENT].node, ETH_ALEN );
  902. memset ( arptable, 0, sizeof(arptable) );
  903. memcpy ( arptable[ARP_CLIENT].node, my_hwaddr, ETH_ALEN );
  904. udp_transmit(IP_BROADCAST, BOOTP_CLIENT, BOOTP_SERVER,
  905. sizeof(struct bootpip_t), &ip);
  906. remaining_time = rfc2131_sleep_interval(BOOTP_TIMEOUT, retry++);
  907. stop_time = currticks() + remaining_time;
  908. #ifdef NO_DHCP_SUPPORT
  909. if (await_reply(await_bootp, 0, NULL, timeout))
  910. return(1);
  911. #else
  912. while ( remaining_time > 0 ) {
  913. if (await_reply(await_bootp, 0, NULL, remaining_time)){
  914. }
  915. remaining_time = stop_time - currticks();
  916. }
  917. if ( ! arptable[ARP_CLIENT].ipaddr.s_addr ) {
  918. printf("No IP address\n");
  919. continue;
  920. }
  921. /* If not a DHCPOFFER then must be just a BOOTP reply,
  922. * be backward compatible with BOOTP then */
  923. if (dhcp_reply != DHCPOFFER)
  924. return(1);
  925. dhcp_reply = 0;
  926. /* Construct the DHCPREQUEST packet */
  927. memcpy(ip.bp.bp_vend, rfc1533_cookie, sizeof rfc1533_cookie);
  928. memcpy(ip.bp.bp_vend + sizeof rfc1533_cookie, dhcprequest, sizeof dhcprequest);
  929. /* Beware: the magic numbers 9 and 15 depend on
  930. the layout of dhcprequest */
  931. memcpy(&ip.bp.bp_vend[9], &dhcp_server, sizeof(in_addr));
  932. memcpy(&ip.bp.bp_vend[15], &dhcp_addr, sizeof(in_addr));
  933. bp_vend = ip.bp.bp_vend + sizeof rfc1533_cookie + sizeof dhcprequest;
  934. /* Append machine_info to end, in encapsulated option */
  935. memcpy(bp_vend, dhcp_machine_info, DHCP_MACHINE_INFO_SIZE);
  936. bp_vend += DHCP_MACHINE_INFO_SIZE;
  937. *bp_vend++ = RFC1533_END;
  938. for (reqretry = 0; reqretry < MAX_BOOTP_RETRIES; ) {
  939. unsigned long timeout;
  940. udp_transmit(IP_BROADCAST, BOOTP_CLIENT, BOOTP_SERVER,
  941. sizeof(struct bootpip_t), &ip);
  942. dhcp_reply=0;
  943. timeout = rfc2131_sleep_interval(TIMEOUT, reqretry++);
  944. if (!await_reply(await_bootp, 0, NULL, timeout))
  945. continue;
  946. if (dhcp_reply != DHCPACK)
  947. continue;
  948. dhcp_reply = 0;
  949. #ifdef PXE_EXPORT
  950. if ( arptable[ARP_PROXYDHCP].ipaddr.s_addr ) {
  951. /* Construct the ProxyDHCPREQUEST packet */
  952. memcpy(ip.bp.bp_vend, rfc1533_cookie, sizeof rfc1533_cookie);
  953. memcpy(ip.bp.bp_vend + sizeof rfc1533_cookie, proxydhcprequest, sizeof proxydhcprequest);
  954. for (reqretry = 0; reqretry < MAX_BOOTP_RETRIES; ) {
  955. printf ( "\nSending ProxyDHCP request to %@...", arptable[ARP_PROXYDHCP].ipaddr.s_addr);
  956. udp_transmit(arptable[ARP_PROXYDHCP].ipaddr.s_addr, BOOTP_CLIENT, PROXYDHCP_SERVER,
  957. sizeof(struct bootpip_t), &ip);
  958. timeout = rfc2131_sleep_interval(TIMEOUT, reqretry++);
  959. if (await_reply(await_bootp, 0, NULL, timeout)) {
  960. break;
  961. }
  962. }
  963. }
  964. #endif /* PXE_EXPORT */
  965. return(1);
  966. }
  967. #endif /* NO_DHCP_SUPPORT */
  968. ip.bp.bp_secs = htons((currticks()-starttime)/TICKS_PER_SEC);
  969. }
  970. return(0);
  971. }
  972. #endif /* RARP_NOT_BOOTP */
  973. static uint16_t tcpudpchksum(struct iphdr *ip)
  974. {
  975. struct udp_pseudo_hdr pseudo;
  976. uint16_t checksum;
  977. /* Compute the pseudo header */
  978. pseudo.src.s_addr = ip->src.s_addr;
  979. pseudo.dest.s_addr = ip->dest.s_addr;
  980. pseudo.unused = 0;
  981. pseudo.protocol = ip->protocol;
  982. pseudo.len = htons(ntohs(ip->len) - sizeof(struct iphdr));
  983. /* Sum the pseudo header */
  984. checksum = ipchksum(&pseudo, 12);
  985. /* Sum the rest of the tcp/udp packet */
  986. checksum = add_ipchksums(12, checksum, ipchksum(ip + 1,
  987. ntohs(ip->len) - sizeof(struct iphdr)));
  988. return checksum;
  989. }
  990. #ifdef MULTICAST_LEVEL2
  991. static void send_igmp_reports(unsigned long now)
  992. {
  993. int i;
  994. for(i = 0; i < MAX_IGMP; i++) {
  995. if (igmptable[i].time && (now >= igmptable[i].time)) {
  996. struct igmp_ip_t igmp;
  997. igmp.router_alert[0] = 0x94;
  998. igmp.router_alert[1] = 0x04;
  999. igmp.router_alert[2] = 0;
  1000. igmp.router_alert[3] = 0;
  1001. build_ip_hdr(igmptable[i].group.s_addr,
  1002. 1, IP_IGMP, sizeof(igmp.router_alert), sizeof(igmp), &igmp);
  1003. igmp.igmp.type = IGMPv2_REPORT;
  1004. if (last_igmpv1 &&
  1005. (now < last_igmpv1 + IGMPv1_ROUTER_PRESENT_TIMEOUT)) {
  1006. igmp.igmp.type = IGMPv1_REPORT;
  1007. }
  1008. igmp.igmp.response_time = 0;
  1009. igmp.igmp.chksum = 0;
  1010. igmp.igmp.group.s_addr = igmptable[i].group.s_addr;
  1011. igmp.igmp.chksum = ipchksum(&igmp.igmp, sizeof(igmp.igmp));
  1012. ip_transmit(sizeof(igmp), &igmp);
  1013. #ifdef MDEBUG
  1014. printf("Sent IGMP report to: %@\n", igmp.igmp.group.s_addr);
  1015. #endif
  1016. /* Don't send another igmp report until asked */
  1017. igmptable[i].time = 0;
  1018. }
  1019. }
  1020. }
  1021. static void process_igmp(struct iphdr *ip, unsigned long now)
  1022. {
  1023. struct igmp *igmp;
  1024. int i;
  1025. unsigned iplen;
  1026. if (!ip || (ip->protocol == IP_IGMP) ||
  1027. (nic.packetlen < sizeof(struct iphdr) + sizeof(struct igmp))) {
  1028. return;
  1029. }
  1030. iplen = (ip->verhdrlen & 0xf)*4;
  1031. igmp = (struct igmp *)&nic.packet[sizeof(struct iphdr)];
  1032. if (ipchksum(igmp, ntohs(ip->len) - iplen) != 0)
  1033. return;
  1034. if ((igmp->type == IGMP_QUERY) &&
  1035. (ip->dest.s_addr == htonl(GROUP_ALL_HOSTS))) {
  1036. unsigned long interval = IGMP_INTERVAL;
  1037. if (igmp->response_time == 0) {
  1038. last_igmpv1 = now;
  1039. } else {
  1040. interval = (igmp->response_time * TICKS_PER_SEC)/10;
  1041. }
  1042. #ifdef MDEBUG
  1043. printf("Received IGMP query for: %@\n", igmp->group.s_addr);
  1044. #endif
  1045. for(i = 0; i < MAX_IGMP; i++) {
  1046. uint32_t group = igmptable[i].group.s_addr;
  1047. if ((group == 0) || (group == igmp->group.s_addr)) {
  1048. unsigned long time;
  1049. time = currticks() + rfc1112_sleep_interval(interval, 0);
  1050. if (time < igmptable[i].time) {
  1051. igmptable[i].time = time;
  1052. }
  1053. }
  1054. }
  1055. }
  1056. if (((igmp->type == IGMPv1_REPORT) || (igmp->type == IGMPv2_REPORT)) &&
  1057. (ip->dest.s_addr == igmp->group.s_addr)) {
  1058. #ifdef MDEBUG
  1059. printf("Received IGMP report for: %@\n", igmp->group.s_addr);
  1060. #endif
  1061. for(i = 0; i < MAX_IGMP; i++) {
  1062. if ((igmptable[i].group.s_addr == igmp->group.s_addr) &&
  1063. igmptable[i].time != 0) {
  1064. igmptable[i].time = 0;
  1065. }
  1066. }
  1067. }
  1068. }
  1069. void leave_group(int slot)
  1070. {
  1071. /* Be very stupid and always send a leave group message if
  1072. * I have subscribed. Imperfect but it is standards
  1073. * compliant, easy and reliable to implement.
  1074. *
  1075. * The optimal group leave method is to only send leave when,
  1076. * we were the last host to respond to a query on this group,
  1077. * and igmpv1 compatibility is not enabled.
  1078. */
  1079. if (igmptable[slot].group.s_addr) {
  1080. struct igmp_ip_t igmp;
  1081. igmp.router_alert[0] = 0x94;
  1082. igmp.router_alert[1] = 0x04;
  1083. igmp.router_alert[2] = 0;
  1084. igmp.router_alert[3] = 0;
  1085. build_ip_hdr(htonl(GROUP_ALL_HOSTS),
  1086. 1, IP_IGMP, sizeof(igmp.router_alert), sizeof(igmp), &igmp);
  1087. igmp.igmp.type = IGMP_LEAVE;
  1088. igmp.igmp.response_time = 0;
  1089. igmp.igmp.chksum = 0;
  1090. igmp.igmp.group.s_addr = igmptable[slot].group.s_addr;
  1091. igmp.igmp.chksum = ipchksum(&igmp.igmp, sizeof(igmp));
  1092. ip_transmit(sizeof(igmp), &igmp);
  1093. #ifdef MDEBUG
  1094. printf("Sent IGMP leave for: %@\n", igmp.igmp.group.s_addr);
  1095. #endif
  1096. }
  1097. memset(&igmptable[slot], 0, sizeof(igmptable[0]));
  1098. }
  1099. void join_group(int slot, unsigned long group)
  1100. {
  1101. /* I have already joined */
  1102. if (igmptable[slot].group.s_addr == group)
  1103. return;
  1104. if (igmptable[slot].group.s_addr) {
  1105. leave_group(slot);
  1106. }
  1107. /* Only join a group if we are given a multicast ip, this way
  1108. * code can be given a non-multicast (broadcast or unicast ip)
  1109. * and still work...
  1110. */
  1111. if ((group & htonl(MULTICAST_MASK)) == htonl(MULTICAST_NETWORK)) {
  1112. igmptable[slot].group.s_addr = group;
  1113. igmptable[slot].time = currticks();
  1114. }
  1115. }
  1116. #else
  1117. #define send_igmp_reports(now) do {} while(0)
  1118. #define process_igmp(ip, now) do {} while(0)
  1119. #endif
  1120. #include "proto_eth_slow.c"
  1121. /**************************************************************************
  1122. TCP - Simple-minded TCP stack. Can only send data once and then
  1123. receive the response. The algorithm for computing window
  1124. sizes and delaying ack's is currently broken, and thus
  1125. disabled. Performance would probably improve a little, if
  1126. this gets fixed. FIXME
  1127. **************************************************************************/
  1128. #ifdef DOWNLOAD_PROTO_HTTP
  1129. static int await_tcp(int ival, void *ptr, unsigned short ptype __unused,
  1130. struct iphdr *ip, struct udphdr *udp __unused,
  1131. struct tcphdr *tcp)
  1132. {
  1133. if (!tcp) {
  1134. return 0;
  1135. }
  1136. if (arptable[ARP_CLIENT].ipaddr.s_addr != ip->dest.s_addr)
  1137. return 0;
  1138. if (ntohs(tcp->dst) != ival) {
  1139. tcp_reset(ip);
  1140. return 0;
  1141. }
  1142. *(void **)ptr = tcp;
  1143. return 1;
  1144. }
  1145. int tcp_transaction(unsigned long destip, unsigned int destsock, void *ptr,
  1146. int (*send)(int len, void *buf, void *ptr),
  1147. int (*recv)(int len, const void *buf, void *ptr)) {
  1148. static uint16_t srcsock = 0;
  1149. int rc = 1;
  1150. long send_seq = currticks();
  1151. long recv_seq = 0;
  1152. int can_send = 0;
  1153. int sent_all = 0;
  1154. struct iphdr *ip;
  1155. struct tcphdr *tcp;
  1156. int ctrl = SYN;
  1157. char buf[128]; /* Small outgoing buffer */
  1158. long payload;
  1159. int header_size;
  1160. int window = 3*TCP_MIN_WINDOW;
  1161. long last_ack = 0;
  1162. long last_sent = 0;
  1163. long rtt = 0;
  1164. long srtt = 0;
  1165. long rto = TCP_INITIAL_TIMEOUT;
  1166. int retry = TCP_MAX_TIMEOUT/TCP_INITIAL_TIMEOUT;
  1167. enum { CLOSED, SYN_RCVD, ESTABLISHED,
  1168. FIN_WAIT_1, FIN_WAIT_2 } state = CLOSED;
  1169. if (!srcsock) {
  1170. srcsock = currticks();
  1171. }
  1172. if (++srcsock < 1024)
  1173. srcsock += 1024;
  1174. await_reply(await_qdrain, 0, NULL, 0);
  1175. send_data:
  1176. if (ctrl & ACK)
  1177. last_ack = recv_seq;
  1178. if (!tcp_transmit(destip, srcsock, destsock, send_seq,
  1179. recv_seq, window, ctrl,
  1180. sizeof(struct iphdr) + sizeof(struct tcphdr)+
  1181. can_send, buf)) {
  1182. return (0);
  1183. }
  1184. last_sent = currticks();
  1185. recv_data:
  1186. if (!await_reply(await_tcp, srcsock, &tcp,
  1187. (state == ESTABLISHED && !can_send)
  1188. ? TCP_MAX_TIMEOUT : rto)) {
  1189. if (state == ESTABLISHED) {
  1190. close:
  1191. ctrl = FIN|ACK;
  1192. state = FIN_WAIT_1;
  1193. rc = 0;
  1194. goto send_data;
  1195. }
  1196. if (state == FIN_WAIT_1 || state == FIN_WAIT_2)
  1197. return (rc);
  1198. if (--retry <= 0) {
  1199. /* time out */
  1200. if (state == SYN_RCVD) {
  1201. tcp_transmit(destip, srcsock, destsock,
  1202. send_seq, 0, window, RST,
  1203. sizeof(struct iphdr) +
  1204. sizeof(struct tcphdr), buf);
  1205. }
  1206. return (0);
  1207. }
  1208. /* retransmit */
  1209. goto send_data;
  1210. }
  1211. got_data:
  1212. retry = TCP_MAX_RETRY;
  1213. if (tcp->ctrl & htons(ACK) ) {
  1214. char *data;
  1215. int syn_ack, consumed;
  1216. if (state == FIN_WAIT_1 || state == FIN_WAIT_2) {
  1217. state = FIN_WAIT_2;
  1218. ctrl = ACK;
  1219. goto consume_data;
  1220. }
  1221. syn_ack = state == CLOSED || state == SYN_RCVD;
  1222. consumed = ntohl(tcp->ack) - send_seq - syn_ack;
  1223. if (consumed < 0 || consumed > can_send) {
  1224. tcp_reset((struct iphdr *)&nic.packet[ETH_HLEN]);
  1225. goto recv_data;
  1226. }
  1227. rtt = currticks() - last_sent;
  1228. srtt = !srtt ? rtt : (srtt*4 + rtt)/5;
  1229. rto = srtt + srtt/2;
  1230. if (rto < TCP_MIN_TIMEOUT)
  1231. rto = TCP_MIN_TIMEOUT;
  1232. else if (rto > TCP_MAX_TIMEOUT)
  1233. rto = TCP_MAX_TIMEOUT;
  1234. can_send -= consumed;
  1235. send_seq += consumed + syn_ack;
  1236. data = buf + sizeof(struct iphdr) + sizeof(struct tcphdr);
  1237. if (can_send) {
  1238. memmove(data, data + consumed, can_send);
  1239. }
  1240. if (!sent_all) {
  1241. int more_data;
  1242. data += can_send;
  1243. more_data = buf + sizeof(buf) - data;
  1244. if (more_data > 0) {
  1245. more_data = send(more_data, data, ptr);
  1246. can_send += more_data;
  1247. }
  1248. sent_all = !more_data;
  1249. }
  1250. if (state == SYN_RCVD) {
  1251. state = ESTABLISHED;
  1252. ctrl = PSH|ACK;
  1253. goto consume_data;
  1254. }
  1255. if (tcp->ctrl & htons(RST))
  1256. return (0);
  1257. } else if (tcp->ctrl & htons(RST)) {
  1258. if (state == CLOSED)
  1259. goto recv_data;
  1260. return (0);
  1261. }
  1262. consume_data:
  1263. ip = (struct iphdr *)&nic.packet[ETH_HLEN];
  1264. header_size = sizeof(struct iphdr) + ((ntohs(tcp->ctrl)>>10)&0x3C);
  1265. payload = ntohs(ip->len) - header_size;
  1266. if (payload > 0 && state == ESTABLISHED) {
  1267. int old_bytes = recv_seq - (long)ntohl(tcp->seq);
  1268. if (old_bytes >= 0 && payload - old_bytes > 0) {
  1269. recv_seq += payload - old_bytes;
  1270. if (state != FIN_WAIT_1 && state != FIN_WAIT_2 &&
  1271. !recv(payload - old_bytes,
  1272. &nic.packet[ETH_HLEN+header_size+old_bytes],
  1273. ptr)) {
  1274. goto close;
  1275. }
  1276. if ((state == ESTABLISHED || state == SYN_RCVD) &&
  1277. !(tcp->ctrl & htons(FIN))) {
  1278. int in_window = window - 2*TCP_MIN_WINDOW >
  1279. recv_seq - last_ack;
  1280. ctrl = can_send ? PSH|ACK : ACK;
  1281. if (!can_send && in_window) {
  1282. /* Window scaling is broken right now, just fall back to acknowledging every */
  1283. /* packet immediately and unconditionally. FIXME */ /***/
  1284. /* if (await_reply(await_tcp, srcsock,
  1285. &tcp, rto))
  1286. goto got_data;
  1287. else */
  1288. goto send_data;
  1289. }
  1290. if (!in_window) {
  1291. window += TCP_MIN_WINDOW;
  1292. if (window > TCP_MAX_WINDOW)
  1293. window = TCP_MAX_WINDOW;
  1294. }
  1295. goto send_data;
  1296. }
  1297. } else {
  1298. /* saw old data again, must have lost packets */
  1299. window /= 2;
  1300. if (window < 2*TCP_MIN_WINDOW)
  1301. window = 2*TCP_MIN_WINDOW;
  1302. }
  1303. }
  1304. if (tcp->ctrl & htons(FIN)) {
  1305. if (state == ESTABLISHED) {
  1306. ctrl = FIN|ACK;
  1307. } else if (state == FIN_WAIT_1 || state == FIN_WAIT_2) {
  1308. ctrl = ACK;
  1309. } else {
  1310. ctrl = RST;
  1311. }
  1312. return (tcp_transmit(destip, srcsock, destsock,
  1313. send_seq, recv_seq + 1, window, ctrl,
  1314. sizeof(struct iphdr) +
  1315. sizeof(struct tcphdr), buf) &&
  1316. (state == ESTABLISHED ||
  1317. state == FIN_WAIT_1 || state == FIN_WAIT_2) &&
  1318. !can_send);
  1319. }
  1320. if (state == CLOSED) {
  1321. if (tcp->ctrl & htons(SYN)) {
  1322. recv_seq = ntohl(tcp->seq) + 1;
  1323. if (!(tcp->ctrl & htons(ACK))) {
  1324. state = SYN_RCVD;
  1325. ctrl = SYN|ACK|PSH;
  1326. goto send_data;
  1327. } else {
  1328. state = ESTABLISHED;
  1329. ctrl = PSH|ACK;
  1330. }
  1331. }
  1332. }
  1333. if (can_send || payload) {
  1334. goto send_data;
  1335. }
  1336. goto recv_data;
  1337. }
  1338. #endif
  1339. /**************************************************************************
  1340. AWAIT_REPLY - Wait until we get a response for our request
  1341. ************f**************************************************************/
  1342. int await_reply(reply_t reply, int ival, void *ptr, long timeout)
  1343. {
  1344. unsigned long time, now;
  1345. struct iphdr *ip;
  1346. unsigned iplen = 0;
  1347. struct udphdr *udp;
  1348. struct tcphdr *tcp;
  1349. unsigned short ptype;
  1350. int result;
  1351. time = timeout + currticks();
  1352. /* The timeout check is done below. The timeout is only checked if
  1353. * there is no packet in the Rx queue. This assumes that eth_poll()
  1354. * needs a negligible amount of time.
  1355. */
  1356. for (;;) {
  1357. now = currticks();
  1358. send_eth_slow_reports(now);
  1359. send_igmp_reports(now);
  1360. result = eth_poll(1);
  1361. if (result == 0) {
  1362. /* We don't have anything */
  1363. /* Check for abort key only if the Rx queue is empty -
  1364. * as long as we have something to process, don't
  1365. * assume that something failed. It is unlikely that
  1366. * we have no processing time left between packets. */
  1367. poll_interruptions();
  1368. /* Do the timeout after at least a full queue walk. */
  1369. if ((timeout == 0) || (currticks() > time)) {
  1370. break;
  1371. }
  1372. continue;
  1373. }
  1374. /* We have something! */
  1375. /* Find the Ethernet packet type */
  1376. if (nic.packetlen >= ETH_HLEN) {
  1377. ptype = ((unsigned short) nic.packet[12]) << 8
  1378. | ((unsigned short) nic.packet[13]);
  1379. } else continue; /* what else could we do with it? */
  1380. /* Verify an IP header */
  1381. ip = 0;
  1382. if ((ptype == ETH_P_IP) && (nic.packetlen >= ETH_HLEN + sizeof(struct iphdr))) {
  1383. unsigned ipoptlen;
  1384. ip = (struct iphdr *)&nic.packet[ETH_HLEN];
  1385. if ((ip->verhdrlen < 0x45) || (ip->verhdrlen > 0x4F))
  1386. continue;
  1387. iplen = (ip->verhdrlen & 0xf) * 4;
  1388. if (ipchksum(ip, iplen) != 0)
  1389. continue;
  1390. if (ip->frags & htons(0x3FFF)) {
  1391. static int warned_fragmentation = 0;
  1392. if (!warned_fragmentation) {
  1393. printf("ALERT: got a fragmented packet - reconfigure your server\n");
  1394. warned_fragmentation = 1;
  1395. }
  1396. continue;
  1397. }
  1398. if (ntohs(ip->len) > ETH_MAX_MTU)
  1399. continue;
  1400. ipoptlen = iplen - sizeof(struct iphdr);
  1401. if (ipoptlen) {
  1402. /* Delete the ip options, to guarantee
  1403. * good alignment, and make etherboot simpler.
  1404. */
  1405. memmove(&nic.packet[ETH_HLEN + sizeof(struct iphdr)],
  1406. &nic.packet[ETH_HLEN + iplen],
  1407. nic.packetlen - ipoptlen);
  1408. nic.packetlen -= ipoptlen;
  1409. }
  1410. }
  1411. udp = 0;
  1412. if (ip && (ip->protocol == IP_UDP) &&
  1413. (nic.packetlen >=
  1414. ETH_HLEN + sizeof(struct iphdr) + sizeof(struct udphdr))) {
  1415. udp = (struct udphdr *)&nic.packet[ETH_HLEN + sizeof(struct iphdr)];
  1416. /* Make certain we have a reasonable packet length */
  1417. if (ntohs(udp->len) > (ntohs(ip->len) - iplen))
  1418. continue;
  1419. if (udp->chksum && tcpudpchksum(ip)) {
  1420. printf("UDP checksum error\n");
  1421. continue;
  1422. }
  1423. }
  1424. tcp = 0;
  1425. #ifdef DOWNLOAD_PROTO_HTTP
  1426. if (ip && (ip->protocol == IP_TCP) &&
  1427. (nic.packetlen >=
  1428. ETH_HLEN + sizeof(struct iphdr) + sizeof(struct tcphdr))){
  1429. tcp = (struct tcphdr *)&nic.packet[ETH_HLEN +
  1430. sizeof(struct iphdr)];
  1431. /* Make certain we have a reasonable packet length */
  1432. if (((ntohs(tcp->ctrl) >> 10) & 0x3C) >
  1433. ntohs(ip->len) - (int)iplen)
  1434. continue;
  1435. if (tcpudpchksum(ip)) {
  1436. printf("TCP checksum error\n");
  1437. continue;
  1438. }
  1439. }
  1440. #endif
  1441. result = reply(ival, ptr, ptype, ip, udp, tcp);
  1442. if (result > 0) {
  1443. return result;
  1444. }
  1445. /* If it isn't a packet the upper layer wants see if there is a default
  1446. * action. This allows us reply to arp, igmp, and lacp queries.
  1447. */
  1448. if ((ptype == ETH_P_ARP) &&
  1449. (nic.packetlen >= ETH_HLEN + sizeof(struct arprequest))) {
  1450. struct arprequest *arpreply;
  1451. unsigned long tmp;
  1452. arpreply = (struct arprequest *)&nic.packet[ETH_HLEN];
  1453. memcpy(&tmp, arpreply->tipaddr, sizeof(in_addr));
  1454. if ((arpreply->opcode == htons(ARP_REQUEST)) &&
  1455. (tmp == arptable[ARP_CLIENT].ipaddr.s_addr)) {
  1456. arpreply->opcode = htons(ARP_REPLY);
  1457. memcpy(arpreply->tipaddr, arpreply->sipaddr, sizeof(in_addr));
  1458. memcpy(arpreply->thwaddr, arpreply->shwaddr, ETH_ALEN);
  1459. memcpy(arpreply->sipaddr, &arptable[ARP_CLIENT].ipaddr, sizeof(in_addr));
  1460. memcpy(arpreply->shwaddr, arptable[ARP_CLIENT].node, ETH_ALEN);
  1461. eth_transmit(arpreply->thwaddr, ETH_P_ARP,
  1462. sizeof(struct arprequest),
  1463. arpreply);
  1464. #ifdef MDEBUG
  1465. memcpy(&tmp, arpreply->tipaddr, sizeof(in_addr));
  1466. printf("Sent ARP reply to: %@\n",tmp);
  1467. #endif /* MDEBUG */
  1468. }
  1469. }
  1470. process_eth_slow(ptype, now);
  1471. process_igmp(ip, now);
  1472. }
  1473. return(0);
  1474. }
  1475. #ifdef REQUIRE_VCI_ETHERBOOT
  1476. /**************************************************************************
  1477. FIND_VCI_ETHERBOOT - Looks for "Etherboot" in Vendor Encapsulated Identifiers
  1478. On entry p points to byte count of VCI options
  1479. **************************************************************************/
  1480. static int find_vci_etherboot(unsigned char *p)
  1481. {
  1482. unsigned char *end = p + 1 + *p;
  1483. for (p++; p < end; ) {
  1484. if (*p == RFC2132_VENDOR_CLASS_ID) {
  1485. if (strncmp("Etherboot", p + 2, sizeof("Etherboot") - 1) == 0)
  1486. return (1);
  1487. } else if (*p == RFC1533_END)
  1488. return (0);
  1489. p += TAG_LEN(p) + 2;
  1490. }
  1491. return (0);
  1492. }
  1493. #endif /* REQUIRE_VCI_ETHERBOOT */
  1494. /**************************************************************************
  1495. DECODE_RFC1533 - Decodes RFC1533 header
  1496. **************************************************************************/
  1497. int decode_rfc1533(unsigned char *p, unsigned int block, unsigned int len, int eof)
  1498. {
  1499. static unsigned char *extdata = NULL, *extend = NULL;
  1500. unsigned char *extpath = NULL;
  1501. unsigned char *endp;
  1502. static unsigned char in_encapsulated_options = 0;
  1503. if (eof == -1) {
  1504. /* Encapsulated option block */
  1505. endp = p + len;
  1506. }
  1507. else if (block == 0) {
  1508. #ifdef REQUIRE_VCI_ETHERBOOT
  1509. vci_etherboot = 0;
  1510. #endif
  1511. end_of_rfc1533 = NULL;
  1512. #ifdef IMAGE_FREEBSD
  1513. /* yes this is a pain FreeBSD uses this for swap, however,
  1514. there are cases when you don't want swap and then
  1515. you want this set to get the extra features so lets
  1516. just set if dealing with FreeBSD. I haven't run into
  1517. any troubles with this but I have without it
  1518. */
  1519. vendorext_isvalid = 1;
  1520. #ifdef FREEBSD_KERNEL_ENV
  1521. memcpy(freebsd_kernel_env, FREEBSD_KERNEL_ENV,
  1522. sizeof(FREEBSD_KERNEL_ENV));
  1523. /* FREEBSD_KERNEL_ENV had better be a string constant */
  1524. #else
  1525. freebsd_kernel_env[0]='\0';
  1526. #endif
  1527. #else
  1528. vendorext_isvalid = 0;
  1529. #endif
  1530. if (memcmp(p, rfc1533_cookie, 4))
  1531. return(0); /* no RFC 1533 header found */
  1532. p += 4;
  1533. endp = p + len;
  1534. } else {
  1535. if (block == 1) {
  1536. if (memcmp(p, rfc1533_cookie, 4))
  1537. return(0); /* no RFC 1533 header found */
  1538. p += 4;
  1539. len -= 4; }
  1540. if (extend + len <= (unsigned char *)&(BOOTP_DATA_ADDR->bootp_extension[MAX_BOOTP_EXTLEN])) {
  1541. memcpy(extend, p, len);
  1542. extend += len;
  1543. } else {
  1544. printf("Overflow in vendor data buffer! Aborting...\n");
  1545. *extdata = RFC1533_END;
  1546. return(0);
  1547. }
  1548. p = extdata; endp = extend;
  1549. }
  1550. if (!eof)
  1551. return 1;
  1552. while (p < endp) {
  1553. unsigned char c = *p;
  1554. if (c == RFC1533_PAD) {
  1555. p++;
  1556. continue;
  1557. }
  1558. else if (c == RFC1533_END) {
  1559. end_of_rfc1533 = endp = p;
  1560. continue;
  1561. }
  1562. else if (NON_ENCAP_OPT c == RFC1533_NETMASK)
  1563. memcpy(&netmask, p+2, sizeof(in_addr));
  1564. else if (NON_ENCAP_OPT c == RFC1533_GATEWAY) {
  1565. /* This is a little simplistic, but it will
  1566. usually be sufficient.
  1567. Take only the first entry */
  1568. if (TAG_LEN(p) >= sizeof(in_addr))
  1569. memcpy(&arptable[ARP_GATEWAY].ipaddr, p+2, sizeof(in_addr));
  1570. }
  1571. else if (c == RFC1533_EXTENSIONPATH)
  1572. extpath = p;
  1573. #ifndef NO_DHCP_SUPPORT
  1574. #ifdef REQUIRE_VCI_ETHERBOOT
  1575. else if (NON_ENCAP_OPT c == RFC1533_VENDOR) {
  1576. vci_etherboot = find_vci_etherboot(p+1);
  1577. #ifdef MDEBUG
  1578. printf("vci_etherboot %d\n", vci_etherboot);
  1579. #endif
  1580. }
  1581. #endif /* REQUIRE_VCI_ETHERBOOT */
  1582. else if (NON_ENCAP_OPT c == RFC2132_MSG_TYPE)
  1583. dhcp_reply=*(p+2);
  1584. else if (NON_ENCAP_OPT c == RFC2132_SRV_ID)
  1585. memcpy(&dhcp_server, p+2, sizeof(in_addr));
  1586. #endif /* NO_DHCP_SUPPORT */
  1587. else if (NON_ENCAP_OPT c == RFC1533_HOSTNAME) {
  1588. hostname = p + 2;
  1589. hostnamelen = *(p + 1);
  1590. }
  1591. else if (ENCAP_OPT c == RFC1533_VENDOR_MAGIC
  1592. && TAG_LEN(p) >= 6 &&
  1593. !memcmp(p+2,vendorext_magic,4) &&
  1594. p[6] == RFC1533_VENDOR_MAJOR
  1595. )
  1596. vendorext_isvalid++;
  1597. else if (NON_ENCAP_OPT c == RFC1533_VENDOR_ETHERBOOT_ENCAP) {
  1598. in_encapsulated_options = 1;
  1599. decode_rfc1533(p+2, 0, TAG_LEN(p), -1);
  1600. in_encapsulated_options = 0;
  1601. }
  1602. #ifdef IMAGE_FREEBSD
  1603. else if (NON_ENCAP_OPT c == RFC1533_VENDOR_HOWTO)
  1604. freebsd_howto = ((p[2]*256+p[3])*256+p[4])*256+p[5];
  1605. else if (NON_ENCAP_OPT c == RFC1533_VENDOR_KERNEL_ENV){
  1606. if(*(p + 1) < sizeof(freebsd_kernel_env)){
  1607. memcpy(freebsd_kernel_env,p+2,*(p+1));
  1608. }else{
  1609. printf("Only support %ld bytes in Kernel Env\n",
  1610. sizeof(freebsd_kernel_env));
  1611. }
  1612. }
  1613. #endif
  1614. #ifdef DNS_RESOLVER
  1615. else if (NON_ENCAP_OPT c == RFC1533_DNS) {
  1616. // TODO: Copy the DNS IP somewhere reasonable
  1617. if (TAG_LEN(p) >= sizeof(in_addr))
  1618. memcpy(&arptable[ARP_NAMESERVER].ipaddr, p+2, sizeof(in_addr));
  1619. }
  1620. #endif
  1621. else {
  1622. #if 0
  1623. unsigned char *q;
  1624. printf("Unknown RFC1533-tag ");
  1625. for(q=p;q<p+2+TAG_LEN(p);q++)
  1626. printf("%hhX ",*q);
  1627. putchar('\n');
  1628. #endif
  1629. }
  1630. p += TAG_LEN(p) + 2;
  1631. }
  1632. extdata = extend = endp;
  1633. if (block <= 0 && extpath != NULL) {
  1634. char fname[64];
  1635. memcpy(fname, extpath+2, TAG_LEN(extpath));
  1636. fname[(int)TAG_LEN(extpath)] = '\0';
  1637. printf("Loading BOOTP-extension file: %s\n",fname);
  1638. tftp(fname, decode_rfc1533);
  1639. }
  1640. return 1; /* proceed with next block */
  1641. }
  1642. /* FIXME double check TWO_SECOND_DIVISOR */
  1643. #define TWO_SECOND_DIVISOR (RAND_MAX/TICKS_PER_SEC)
  1644. /**************************************************************************
  1645. RFC2131_SLEEP_INTERVAL - sleep for expotentially longer times (base << exp) +- 1 sec)
  1646. **************************************************************************/
  1647. long rfc2131_sleep_interval(long base, int exp)
  1648. {
  1649. unsigned long tmo;
  1650. #ifdef BACKOFF_LIMIT
  1651. if (exp > BACKOFF_LIMIT)
  1652. exp = BACKOFF_LIMIT;
  1653. #endif
  1654. tmo = (base << exp) + (TICKS_PER_SEC - (random()/TWO_SECOND_DIVISOR));
  1655. return tmo;
  1656. }
  1657. #ifdef MULTICAST_LEVEL2
  1658. /**************************************************************************
  1659. RFC1112_SLEEP_INTERVAL - sleep for expotentially longer times, up to (base << exp)
  1660. **************************************************************************/
  1661. long rfc1112_sleep_interval(long base, int exp)
  1662. {
  1663. unsigned long divisor, tmo;
  1664. #ifdef BACKOFF_LIMIT
  1665. if (exp > BACKOFF_LIMIT)
  1666. exp = BACKOFF_LIMIT;
  1667. #endif
  1668. divisor = RAND_MAX/(base << exp);
  1669. tmo = random()/divisor;
  1670. return tmo;
  1671. }
  1672. #endif /* MULTICAST_LEVEL_2 */