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

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