Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

efi_pxe.c 43KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599
  1. /*
  2. * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <string.h>
  25. #include <errno.h>
  26. #include <ipxe/refcnt.h>
  27. #include <ipxe/list.h>
  28. #include <ipxe/netdevice.h>
  29. #include <ipxe/fakedhcp.h>
  30. #include <ipxe/process.h>
  31. #include <ipxe/uri.h>
  32. #include <ipxe/in.h>
  33. #include <ipxe/socket.h>
  34. #include <ipxe/tcpip.h>
  35. #include <ipxe/xferbuf.h>
  36. #include <ipxe/open.h>
  37. #include <ipxe/dhcppkt.h>
  38. #include <ipxe/udp.h>
  39. #include <ipxe/efi/efi.h>
  40. #include <ipxe/efi/efi_snp.h>
  41. #include <ipxe/efi/efi_pxe.h>
  42. #include <ipxe/efi/Protocol/PxeBaseCode.h>
  43. #include <usr/ifmgmt.h>
  44. #include <config/general.h>
  45. /** @file
  46. *
  47. * EFI PXE base code protocol
  48. *
  49. */
  50. /* Downgrade user experience if configured to do so
  51. *
  52. * See comments in efi_snp.c
  53. */
  54. #ifdef EFI_DOWNGRADE_UX
  55. static EFI_GUID dummy_pxe_base_code_protocol_guid = {
  56. 0x70647523, 0x2320, 0x7477,
  57. { 0x66, 0x20, 0x23, 0x6d, 0x6f, 0x72, 0x6f, 0x6e }
  58. };
  59. #define efi_pxe_base_code_protocol_guid dummy_pxe_base_code_protocol_guid
  60. #endif
  61. /** A PXE base code */
  62. struct efi_pxe {
  63. /** Reference count */
  64. struct refcnt refcnt;
  65. /** Underlying network device */
  66. struct net_device *netdev;
  67. /** Name */
  68. const char *name;
  69. /** List of PXE base codes */
  70. struct list_head list;
  71. /** Installed handle */
  72. EFI_HANDLE handle;
  73. /** PXE base code protocol */
  74. EFI_PXE_BASE_CODE_PROTOCOL base;
  75. /** PXE base code mode */
  76. EFI_PXE_BASE_CODE_MODE mode;
  77. /** TCP/IP network-layer protocol */
  78. struct tcpip_net_protocol *tcpip;
  79. /** Network-layer protocol */
  80. struct net_protocol *net;
  81. /** Data transfer buffer */
  82. struct xfer_buffer buf;
  83. /** (M)TFTP download interface */
  84. struct interface tftp;
  85. /** Block size (for TFTP) */
  86. size_t blksize;
  87. /** Overall return status */
  88. int rc;
  89. /** UDP interface */
  90. struct interface udp;
  91. /** List of received UDP packets */
  92. struct list_head queue;
  93. /** UDP interface closer process */
  94. struct process process;
  95. };
  96. /**
  97. * Free PXE base code
  98. *
  99. * @v refcnt Reference count
  100. */
  101. static void efi_pxe_free ( struct refcnt *refcnt ) {
  102. struct efi_pxe *pxe = container_of ( refcnt, struct efi_pxe, refcnt );
  103. netdev_put ( pxe->netdev );
  104. free ( pxe );
  105. }
  106. /** List of PXE base codes */
  107. static LIST_HEAD ( efi_pxes );
  108. /**
  109. * Locate PXE base code
  110. *
  111. * @v handle EFI handle
  112. * @ret pxe PXE base code, or NULL
  113. */
  114. static struct efi_pxe * efi_pxe_find ( EFI_HANDLE handle ) {
  115. struct efi_pxe *pxe;
  116. /* Locate base code */
  117. list_for_each_entry ( pxe, &efi_pxes, list ) {
  118. if ( pxe->handle == handle )
  119. return pxe;
  120. }
  121. return NULL;
  122. }
  123. /******************************************************************************
  124. *
  125. * IP addresses
  126. *
  127. ******************************************************************************
  128. */
  129. /**
  130. * An EFI socket address
  131. *
  132. */
  133. struct sockaddr_efi {
  134. /** Socket address family (part of struct @c sockaddr) */
  135. sa_family_t se_family;
  136. /** Flags (part of struct @c sockaddr_tcpip) */
  137. uint16_t se_flags;
  138. /** TCP/IP port (part of struct @c sockaddr_tcpip) */
  139. uint16_t se_port;
  140. /** Scope ID (part of struct @c sockaddr_tcpip)
  141. *
  142. * For link-local or multicast addresses, this is the network
  143. * device index.
  144. */
  145. uint16_t se_scope_id;
  146. /** IP address */
  147. EFI_IP_ADDRESS se_addr;
  148. /** Padding
  149. *
  150. * This ensures that a struct @c sockaddr_tcpip is large
  151. * enough to hold a socket address for any TCP/IP address
  152. * family.
  153. */
  154. char pad[ sizeof ( struct sockaddr ) -
  155. ( sizeof ( sa_family_t ) /* se_family */ +
  156. sizeof ( uint16_t ) /* se_flags */ +
  157. sizeof ( uint16_t ) /* se_port */ +
  158. sizeof ( uint16_t ) /* se_scope_id */ +
  159. sizeof ( EFI_IP_ADDRESS ) /* se_addr */ ) ];
  160. } __attribute__ (( packed, may_alias ));
  161. /**
  162. * Populate socket address from EFI IP address
  163. *
  164. * @v pxe PXE base code
  165. * @v ip EFI IP address
  166. * @v sa Socket address to fill in
  167. */
  168. static void efi_pxe_ip_sockaddr ( struct efi_pxe *pxe, EFI_IP_ADDRESS *ip,
  169. struct sockaddr *sa ) {
  170. union {
  171. struct sockaddr sa;
  172. struct sockaddr_efi se;
  173. } *sockaddr = container_of ( sa, typeof ( *sockaddr ), sa );
  174. /* Initialise socket address */
  175. memset ( sockaddr, 0, sizeof ( *sockaddr ) );
  176. sockaddr->sa.sa_family = pxe->tcpip->sa_family;
  177. memcpy ( &sockaddr->se.se_addr, ip, pxe->net->net_addr_len );
  178. sockaddr->se.se_scope_id = pxe->netdev->index;
  179. }
  180. /**
  181. * Transcribe EFI IP address (for debugging)
  182. *
  183. * @v pxe PXE base code
  184. * @v ip EFI IP address
  185. * @ret text Transcribed IP address
  186. */
  187. static const char * efi_pxe_ip_ntoa ( struct efi_pxe *pxe,
  188. EFI_IP_ADDRESS *ip ) {
  189. return pxe->net->ntoa ( ip );
  190. }
  191. /**
  192. * Populate local IP address
  193. *
  194. * @v pxe PXE base code
  195. * @ret rc Return status code
  196. */
  197. static int efi_pxe_ip ( struct efi_pxe *pxe ) {
  198. EFI_PXE_BASE_CODE_MODE *mode = &pxe->mode;
  199. struct in_addr address;
  200. struct in_addr netmask;
  201. /* It's unclear which of the potentially many IPv6 addresses
  202. * is supposed to be used.
  203. */
  204. if ( mode->UsingIpv6 )
  205. return -ENOTSUP;
  206. /* Fetch IP address and subnet mask */
  207. fetch_ipv4_setting ( netdev_settings ( pxe->netdev ), &ip_setting,
  208. &address );
  209. fetch_ipv4_setting ( netdev_settings ( pxe->netdev ), &netmask_setting,
  210. &netmask );
  211. /* Populate IP address and subnet mask */
  212. memset ( &mode->StationIp, 0, sizeof ( mode->StationIp ) );
  213. memcpy ( &mode->StationIp, &address, sizeof ( address ) );
  214. memset ( &mode->SubnetMask, 0, sizeof ( mode->SubnetMask ) );
  215. memcpy ( &mode->SubnetMask, &netmask, sizeof ( netmask ) );
  216. return 0;
  217. }
  218. /**
  219. * Check if IP address matches filter
  220. *
  221. * @v pxe PXE base code
  222. * @v ip EFI IP address
  223. * @ret is_match IP address matches filter
  224. */
  225. static int efi_pxe_ip_filter ( struct efi_pxe *pxe, EFI_IP_ADDRESS *ip ) {
  226. EFI_PXE_BASE_CODE_MODE *mode = &pxe->mode;
  227. EFI_PXE_BASE_CODE_IP_FILTER *filter = &mode->IpFilter;
  228. uint8_t filters = filter->Filters;
  229. union {
  230. EFI_IP_ADDRESS ip;
  231. struct in_addr in;
  232. struct in6_addr in6;
  233. } *u = container_of ( ip, typeof ( *u ), ip );
  234. size_t addr_len = pxe->net->net_addr_len;
  235. unsigned int i;
  236. /* Match everything, if applicable */
  237. if ( filters & EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS )
  238. return 1;
  239. /* Match all multicasts, if applicable */
  240. if ( filters & EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS_MULTICAST ) {
  241. if ( mode->UsingIpv6 ) {
  242. if ( IN6_IS_ADDR_MULTICAST ( &u->in6 ) )
  243. return 1;
  244. } else {
  245. if ( IN_IS_MULTICAST ( u->in.s_addr ) )
  246. return 1;
  247. }
  248. }
  249. /* Match IPv4 broadcasts, if applicable */
  250. if ( filters & EFI_PXE_BASE_CODE_IP_FILTER_BROADCAST ) {
  251. if ( ( ! mode->UsingIpv6 ) &&
  252. ( u->in.s_addr == INADDR_BROADCAST ) )
  253. return 1;
  254. }
  255. /* Match station address, if applicable */
  256. if ( filters & EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP ) {
  257. if ( memcmp ( ip, &mode->StationIp, addr_len ) == 0 )
  258. return 1;
  259. }
  260. /* Match explicit addresses, if applicable */
  261. for ( i = 0 ; i < filter->IpCnt ; i++ ) {
  262. if ( memcmp ( ip, &filter->IpList[i], addr_len ) == 0 )
  263. return 1;
  264. }
  265. return 0;
  266. }
  267. /******************************************************************************
  268. *
  269. * Data transfer buffer
  270. *
  271. ******************************************************************************
  272. */
  273. /**
  274. * Reallocate PXE data transfer buffer
  275. *
  276. * @v xferbuf Data transfer buffer
  277. * @v len New length (or zero to free buffer)
  278. * @ret rc Return status code
  279. */
  280. static int efi_pxe_buf_realloc ( struct xfer_buffer *xferbuf __unused,
  281. size_t len __unused ) {
  282. /* Can never reallocate: return EFI_BUFFER_TOO_SMALL */
  283. return -ERANGE;
  284. }
  285. /**
  286. * Write data to PXE data transfer buffer
  287. *
  288. * @v xferbuf Data transfer buffer
  289. * @v offset Starting offset
  290. * @v data Data to copy
  291. * @v len Length of data
  292. */
  293. static void efi_pxe_buf_write ( struct xfer_buffer *xferbuf, size_t offset,
  294. const void *data, size_t len ) {
  295. /* Copy data to buffer */
  296. memcpy ( ( xferbuf->data + offset ), data, len );
  297. }
  298. /** PXE data transfer buffer operations */
  299. static struct xfer_buffer_operations efi_pxe_buf_operations = {
  300. .realloc = efi_pxe_buf_realloc,
  301. .write = efi_pxe_buf_write,
  302. };
  303. /******************************************************************************
  304. *
  305. * (M)TFTP download interface
  306. *
  307. ******************************************************************************
  308. */
  309. /**
  310. * Close PXE (M)TFTP download interface
  311. *
  312. * @v pxe PXE base code
  313. * @v rc Reason for close
  314. */
  315. static void efi_pxe_tftp_close ( struct efi_pxe *pxe, int rc ) {
  316. /* Restart interface */
  317. intf_restart ( &pxe->tftp, rc );
  318. /* Record overall status */
  319. pxe->rc = rc;
  320. }
  321. /**
  322. * Check PXE (M)TFTP download flow control window
  323. *
  324. * @v pxe PXE base code
  325. * @ret len Length of window
  326. */
  327. static size_t efi_pxe_tftp_window ( struct efi_pxe *pxe ) {
  328. /* Return requested blocksize */
  329. return pxe->blksize;
  330. }
  331. /**
  332. * Receive new PXE (M)TFTP download data
  333. *
  334. * @v pxe PXE base code
  335. * @v iobuf I/O buffer
  336. * @v meta Transfer metadata
  337. * @ret rc Return status code
  338. */
  339. static int efi_pxe_tftp_deliver ( struct efi_pxe *pxe,
  340. struct io_buffer *iobuf,
  341. struct xfer_metadata *meta ) {
  342. int rc;
  343. /* Deliver to data transfer buffer */
  344. if ( ( rc = xferbuf_deliver ( &pxe->buf, iob_disown ( iobuf ),
  345. meta ) ) != 0 )
  346. goto err_deliver;
  347. return 0;
  348. err_deliver:
  349. efi_pxe_tftp_close ( pxe, rc );
  350. return rc;
  351. }
  352. /** PXE file data transfer interface operations */
  353. static struct interface_operation efi_pxe_tftp_operations[] = {
  354. INTF_OP ( xfer_deliver, struct efi_pxe *, efi_pxe_tftp_deliver ),
  355. INTF_OP ( xfer_window, struct efi_pxe *, efi_pxe_tftp_window ),
  356. INTF_OP ( intf_close, struct efi_pxe *, efi_pxe_tftp_close ),
  357. };
  358. /** PXE file data transfer interface descriptor */
  359. static struct interface_descriptor efi_pxe_tftp_desc =
  360. INTF_DESC ( struct efi_pxe, tftp, efi_pxe_tftp_operations );
  361. /**
  362. * Open (M)TFTP download interface
  363. *
  364. * @v pxe PXE base code
  365. * @v ip EFI IP address
  366. * @v filename Filename
  367. * @ret rc Return status code
  368. */
  369. static int efi_pxe_tftp_open ( struct efi_pxe *pxe, EFI_IP_ADDRESS *ip,
  370. const char *filename ) {
  371. struct sockaddr server;
  372. struct uri *uri;
  373. int rc;
  374. /* Parse server address and filename */
  375. efi_pxe_ip_sockaddr ( pxe, ip, &server );
  376. uri = pxe_uri ( &server, filename );
  377. if ( ! uri ) {
  378. DBGC ( pxe, "PXE %s could not parse %s:%s\n", pxe->name,
  379. efi_pxe_ip_ntoa ( pxe, ip ), filename );
  380. rc = -ENOTSUP;
  381. goto err_parse;
  382. }
  383. /* Open URI */
  384. if ( ( rc = xfer_open_uri ( &pxe->tftp, uri ) ) != 0 ) {
  385. DBGC ( pxe, "PXE %s could not open: %s\n",
  386. pxe->name, strerror ( rc ) );
  387. goto err_open;
  388. }
  389. err_open:
  390. uri_put ( uri );
  391. err_parse:
  392. return rc;
  393. }
  394. /******************************************************************************
  395. *
  396. * UDP interface
  397. *
  398. ******************************************************************************
  399. */
  400. /** EFI UDP pseudo-header */
  401. struct efi_pxe_udp_pseudo_header {
  402. /** Network-layer protocol */
  403. struct net_protocol *net;
  404. /** Destination port */
  405. uint16_t dest_port;
  406. /** Source port */
  407. uint16_t src_port;
  408. } __attribute__ (( packed ));
  409. /**
  410. * Close UDP interface
  411. *
  412. * @v pxe PXE base code
  413. * @v rc Reason for close
  414. */
  415. static void efi_pxe_udp_close ( struct efi_pxe *pxe, int rc ) {
  416. struct io_buffer *iobuf;
  417. struct io_buffer *tmp;
  418. /* Release our claim on SNP devices, if applicable */
  419. if ( process_running ( &pxe->process ) )
  420. efi_snp_release();
  421. /* Stop process */
  422. process_del ( &pxe->process );
  423. /* Restart UDP interface */
  424. intf_restart ( &pxe->udp, rc );
  425. /* Flush any received UDP packets */
  426. list_for_each_entry_safe ( iobuf, tmp, &pxe->queue, list ) {
  427. list_del ( &iobuf->list );
  428. free_iob ( iobuf );
  429. }
  430. }
  431. /**
  432. * Receive UDP packet
  433. *
  434. * @v pxe PXE base code
  435. * @v iobuf I/O buffer
  436. * @v meta Data transfer metadata
  437. * @ret rc Return status code
  438. */
  439. static int efi_pxe_udp_deliver ( struct efi_pxe *pxe, struct io_buffer *iobuf,
  440. struct xfer_metadata *meta ) {
  441. struct sockaddr_efi *se_src;
  442. struct sockaddr_efi *se_dest;
  443. struct tcpip_net_protocol *tcpip;
  444. struct net_protocol *net;
  445. struct efi_pxe_udp_pseudo_header *pshdr;
  446. size_t addr_len;
  447. size_t pshdr_len;
  448. int rc;
  449. /* Sanity checks */
  450. assert ( meta != NULL );
  451. se_src = ( ( struct sockaddr_efi * ) meta->src );
  452. assert ( se_src != NULL );
  453. se_dest = ( ( struct sockaddr_efi * ) meta->dest );
  454. assert ( se_dest != NULL );
  455. assert ( se_src->se_family == se_dest->se_family );
  456. /* Determine protocol */
  457. tcpip = tcpip_net_protocol ( se_src->se_family );
  458. if ( ! tcpip ) {
  459. rc = -ENOTSUP;
  460. goto err_unsupported;
  461. }
  462. net = tcpip->net_protocol;
  463. addr_len = net->net_addr_len;
  464. /* Construct pseudo-header */
  465. pshdr_len = ( sizeof ( *pshdr ) + ( 2 * addr_len ) );
  466. if ( ( rc = iob_ensure_headroom ( iobuf, pshdr_len ) ) != 0 )
  467. goto err_headroom;
  468. memcpy ( iob_push ( iobuf, addr_len ), &se_src->se_addr, addr_len );
  469. memcpy ( iob_push ( iobuf, addr_len ), &se_dest->se_addr, addr_len );
  470. pshdr = iob_push ( iobuf, sizeof ( *pshdr ) );
  471. pshdr->net = net;
  472. pshdr->dest_port = ntohs ( se_dest->se_port );
  473. pshdr->src_port = ntohs ( se_src->se_port );
  474. /* Add to queue */
  475. list_add_tail ( &iobuf->list, &pxe->queue );
  476. return 0;
  477. err_unsupported:
  478. err_headroom:
  479. free_iob ( iobuf );
  480. return rc;
  481. }
  482. /** PXE UDP interface operations */
  483. static struct interface_operation efi_pxe_udp_operations[] = {
  484. INTF_OP ( xfer_deliver, struct efi_pxe *, efi_pxe_udp_deliver ),
  485. INTF_OP ( intf_close, struct efi_pxe *, efi_pxe_udp_close ),
  486. };
  487. /** PXE UDP interface descriptor */
  488. static struct interface_descriptor efi_pxe_udp_desc =
  489. INTF_DESC ( struct efi_pxe, udp, efi_pxe_udp_operations );
  490. /**
  491. * Open UDP interface
  492. *
  493. * @v pxe PXE base code
  494. * @ret rc Return status code
  495. */
  496. static int efi_pxe_udp_open ( struct efi_pxe *pxe ) {
  497. int rc;
  498. /* If interface is already open, then cancel the scheduled close */
  499. if ( process_running ( &pxe->process ) ) {
  500. process_del ( &pxe->process );
  501. return 0;
  502. }
  503. /* Open promiscuous UDP interface */
  504. if ( ( rc = udp_open_promisc ( &pxe->udp ) ) != 0 ) {
  505. DBGC ( pxe, "PXE %s could not open UDP connection: %s\n",
  506. pxe->name, strerror ( rc ) );
  507. return rc;
  508. }
  509. /* Claim network devices */
  510. efi_snp_claim();
  511. return 0;
  512. }
  513. /**
  514. * Schedule close of UDP interface
  515. *
  516. * @v pxe PXE base code
  517. */
  518. static void efi_pxe_udp_schedule_close ( struct efi_pxe *pxe ) {
  519. /* The EFI PXE base code protocol does not provide any
  520. * explicit UDP open/close methods. To avoid the overhead of
  521. * reopening a socket for each read/write operation, we start
  522. * a process which will close the socket immediately if the
  523. * next call into iPXE is anything other than a UDP
  524. * read/write.
  525. */
  526. process_add ( &pxe->process );
  527. }
  528. /**
  529. * Scheduled close of UDP interface
  530. *
  531. * @v pxe PXE base code
  532. */
  533. static void efi_pxe_udp_scheduled_close ( struct efi_pxe *pxe ) {
  534. /* Close UDP interface */
  535. efi_pxe_udp_close ( pxe, 0 );
  536. }
  537. /** UDP close process descriptor */
  538. static struct process_descriptor efi_pxe_process_desc =
  539. PROC_DESC_ONCE ( struct efi_pxe, process, efi_pxe_udp_scheduled_close );
  540. /******************************************************************************
  541. *
  542. * Fake DHCP packets
  543. *
  544. ******************************************************************************
  545. */
  546. /**
  547. * Name fake DHCP packet
  548. *
  549. * @v pxe PXE base code
  550. * @v packet Packet
  551. * @ret name Name of packet
  552. */
  553. static const char * efi_pxe_fake_name ( struct efi_pxe *pxe,
  554. EFI_PXE_BASE_CODE_PACKET *packet ) {
  555. EFI_PXE_BASE_CODE_MODE *mode = &pxe->mode;
  556. if ( packet == &mode->DhcpDiscover ) {
  557. return "DhcpDiscover";
  558. } else if ( packet == &mode->DhcpAck ) {
  559. return "DhcpAck";
  560. } else if ( packet == &mode->ProxyOffer ) {
  561. return "ProxyOffer";
  562. } else if ( packet == &mode->PxeDiscover ) {
  563. return "PxeDiscover";
  564. } else if ( packet == &mode->PxeReply ) {
  565. return "PxeReply";
  566. } else if ( packet == &mode->PxeBisReply ) {
  567. return "PxeBisReply";
  568. } else {
  569. return "<UNKNOWN>";
  570. }
  571. }
  572. /**
  573. * Construct fake DHCP packet and flag
  574. *
  575. * @v pxe PXE base code
  576. * @v fake Fake packet constructor
  577. * @v packet Packet to fill in
  578. * @ret exists Packet existence flag
  579. */
  580. static BOOLEAN efi_pxe_fake ( struct efi_pxe *pxe,
  581. int ( * fake ) ( struct net_device *netdev,
  582. void *data, size_t len ),
  583. EFI_PXE_BASE_CODE_PACKET *packet ) {
  584. EFI_PXE_BASE_CODE_MODE *mode = &pxe->mode;
  585. struct dhcp_packet dhcppkt;
  586. struct dhcphdr *dhcphdr;
  587. unsigned int len;
  588. int rc;
  589. /* The fake packet constructors do not support IPv6 */
  590. if ( mode->UsingIpv6 )
  591. return FALSE;
  592. /* Attempt to construct packet */
  593. if ( ( rc = fake ( pxe->netdev, packet, sizeof ( *packet ) ) != 0 ) ) {
  594. DBGC ( pxe, "PXE %s could not fake %s: %s\n", pxe->name,
  595. efi_pxe_fake_name ( pxe, packet ), strerror ( rc ) );
  596. return FALSE;
  597. }
  598. /* The WDS bootstrap wdsmgfw.efi has a buggy DHCPv4 packet
  599. * parser which does not correctly handle DHCP padding bytes.
  600. * Specifically, if a padding byte (i.e. a zero) is
  601. * encountered, the parse will first increment the pointer by
  602. * one to skip over the padding byte but will then drop into
  603. * the code path for handling normal options, which increments
  604. * the pointer by two to skip over the (already-skipped) type
  605. * field and the (non-existent) length field.
  606. *
  607. * The upshot of this bug in WDS is that the parser will fail
  608. * with an error 0xc0000023 if the number of spare bytes after
  609. * the end of the options is not an exact multiple of three.
  610. *
  611. * Work around this buggy parser by adding an explicit
  612. * DHCP_END tag.
  613. */
  614. dhcphdr = container_of ( &packet->Dhcpv4.BootpOpcode,
  615. struct dhcphdr, op );
  616. dhcppkt_init ( &dhcppkt, dhcphdr, sizeof ( *packet ) );
  617. len = dhcppkt_len ( &dhcppkt );
  618. if ( len < sizeof ( *packet ) )
  619. packet->Raw[len] = DHCP_END;
  620. return TRUE;
  621. }
  622. /**
  623. * Construct fake DHCP packets
  624. *
  625. * @v pxe PXE base code
  626. */
  627. static void efi_pxe_fake_all ( struct efi_pxe *pxe ) {
  628. EFI_PXE_BASE_CODE_MODE *mode = &pxe->mode;
  629. /* Construct fake packets */
  630. mode->DhcpDiscoverValid =
  631. efi_pxe_fake ( pxe, create_fakedhcpdiscover,
  632. &mode->DhcpDiscover );
  633. mode->DhcpAckReceived =
  634. efi_pxe_fake ( pxe, create_fakedhcpack,
  635. &mode->DhcpAck );
  636. mode->PxeReplyReceived =
  637. efi_pxe_fake ( pxe, create_fakepxebsack,
  638. &mode->PxeReply );
  639. }
  640. /******************************************************************************
  641. *
  642. * Base code protocol
  643. *
  644. ******************************************************************************
  645. */
  646. /**
  647. * Start PXE base code
  648. *
  649. * @v base PXE base code protocol
  650. * @v use_ipv6 Use IPv6
  651. * @ret efirc EFI status code
  652. */
  653. static EFI_STATUS EFIAPI efi_pxe_start ( EFI_PXE_BASE_CODE_PROTOCOL *base,
  654. BOOLEAN use_ipv6 ) {
  655. struct efi_pxe *pxe = container_of ( base, struct efi_pxe, base );
  656. EFI_PXE_BASE_CODE_MODE *mode = &pxe->mode;
  657. struct tcpip_net_protocol *ipv6 = tcpip_net_protocol ( AF_INET6 );
  658. sa_family_t family = ( use_ipv6 ? AF_INET6 : AF_INET );
  659. int rc;
  660. DBGC ( pxe, "PXE %s START %s\n", pxe->name, ( ipv6 ? "IPv6" : "IPv4" ));
  661. /* Initialise mode structure */
  662. memset ( mode, 0, sizeof ( *mode ) );
  663. mode->AutoArp = TRUE;
  664. mode->TTL = DEFAULT_TTL;
  665. mode->ToS = DEFAULT_ToS;
  666. mode->IpFilter.Filters =
  667. ( EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP |
  668. EFI_PXE_BASE_CODE_IP_FILTER_BROADCAST |
  669. EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS |
  670. EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS_MULTICAST );
  671. /* Check for IPv4/IPv6 support */
  672. mode->Ipv6Supported = ( ipv6 != NULL );
  673. mode->Ipv6Available = ( ipv6 != NULL );
  674. pxe->tcpip = tcpip_net_protocol ( family );
  675. if ( ! pxe->tcpip ) {
  676. DBGC ( pxe, "PXE %s has no support for %s\n",
  677. pxe->name, socket_family_name ( family ) );
  678. return EFI_UNSUPPORTED;
  679. }
  680. pxe->net = pxe->tcpip->net_protocol;
  681. mode->UsingIpv6 = use_ipv6;
  682. /* Populate station IP address */
  683. if ( ( rc = efi_pxe_ip ( pxe ) ) != 0 )
  684. return rc;
  685. /* Construct fake DHCP packets */
  686. efi_pxe_fake_all ( pxe );
  687. /* Record that base code is started */
  688. mode->Started = TRUE;
  689. DBGC ( pxe, "PXE %s using %s\n",
  690. pxe->name, pxe->net->ntoa ( &mode->StationIp ) );
  691. return 0;
  692. }
  693. /**
  694. * Stop PXE base code
  695. *
  696. * @v base PXE base code protocol
  697. * @ret efirc EFI status code
  698. */
  699. static EFI_STATUS EFIAPI efi_pxe_stop ( EFI_PXE_BASE_CODE_PROTOCOL *base ) {
  700. struct efi_pxe *pxe = container_of ( base, struct efi_pxe, base );
  701. EFI_PXE_BASE_CODE_MODE *mode = &pxe->mode;
  702. DBGC ( pxe, "PXE %s STOP\n", pxe->name );
  703. /* Record that base code is stopped */
  704. mode->Started = FALSE;
  705. /* Close TFTP */
  706. efi_pxe_tftp_close ( pxe, 0 );
  707. /* Close UDP */
  708. efi_pxe_udp_close ( pxe, 0 );
  709. return 0;
  710. }
  711. /**
  712. * Perform DHCP
  713. *
  714. * @v base PXE base code protocol
  715. * @v sort Offers should be sorted
  716. * @ret efirc EFI status code
  717. */
  718. static EFI_STATUS EFIAPI efi_pxe_dhcp ( EFI_PXE_BASE_CODE_PROTOCOL *base,
  719. BOOLEAN sort ) {
  720. struct efi_pxe *pxe = container_of ( base, struct efi_pxe, base );
  721. struct net_device *netdev = pxe->netdev;
  722. int rc;
  723. DBGC ( pxe, "PXE %s DHCP %s\n",
  724. pxe->name, ( sort ? "sorted" : "unsorted" ) );
  725. /* Claim network devices */
  726. efi_snp_claim();
  727. /* Initiate configuration */
  728. if ( ( rc = netdev_configure_all ( netdev ) ) != 0 ) {
  729. DBGC ( pxe, "PXE %s could not initiate configuration: %s\n",
  730. pxe->name, strerror ( rc ) );
  731. goto err_configure;
  732. }
  733. /* Wait for configuration to complete (or time out) */
  734. while ( netdev_configuration_in_progress ( netdev ) )
  735. step();
  736. /* Report timeout if configuration failed */
  737. if ( ! netdev_configuration_ok ( netdev ) ) {
  738. rc = -ETIMEDOUT;
  739. goto err_timeout;
  740. }
  741. /* Update station IP address */
  742. if ( ( rc = efi_pxe_ip ( pxe ) ) != 0 )
  743. goto err_ip;
  744. /* Update faked DHCP packets */
  745. efi_pxe_fake_all ( pxe );
  746. err_ip:
  747. err_timeout:
  748. err_configure:
  749. efi_snp_release();
  750. return EFIRC ( rc );
  751. }
  752. /**
  753. * Perform boot server discovery
  754. *
  755. * @v base PXE base code protocol
  756. * @v type Boot server type
  757. * @v layer Boot server layer
  758. * @v bis Use boot integrity services
  759. * @v info Additional information
  760. * @ret efirc EFI status code
  761. */
  762. static EFI_STATUS EFIAPI
  763. efi_pxe_discover ( EFI_PXE_BASE_CODE_PROTOCOL *base, UINT16 type, UINT16 *layer,
  764. BOOLEAN bis, EFI_PXE_BASE_CODE_DISCOVER_INFO *info ) {
  765. struct efi_pxe *pxe = container_of ( base, struct efi_pxe, base );
  766. EFI_IP_ADDRESS *ip;
  767. unsigned int i;
  768. DBGC ( pxe, "PXE %s DISCOVER type %d layer %d%s\n",
  769. pxe->name, type, *layer, ( bis ? " bis" : "" ) );
  770. if ( info ) {
  771. DBGC ( pxe, "%s%s%s%s %s",
  772. ( info->UseMCast ? " mcast" : "" ),
  773. ( info->UseBCast ? " bcast" : "" ),
  774. ( info->UseUCast ? " ucast" : "" ),
  775. ( info->MustUseList ? " list" : "" ),
  776. efi_pxe_ip_ntoa ( pxe, &info->ServerMCastIp ) );
  777. for ( i = 0 ; i < info->IpCnt ; i++ ) {
  778. ip = &info->SrvList[i].IpAddr;
  779. DBGC ( pxe, " %d%s:%s", info->SrvList[i].Type,
  780. ( info->SrvList[i].AcceptAnyResponse ?
  781. ":any" : "" ), efi_pxe_ip_ntoa ( pxe, ip ) );
  782. }
  783. }
  784. DBGC ( pxe, "\n" );
  785. /* Not used by any bootstrap I can find to test with */
  786. return EFI_UNSUPPORTED;
  787. }
  788. /**
  789. * Perform (M)TFTP
  790. *
  791. * @v base PXE base code protocol
  792. * @v opcode TFTP opcode
  793. * @v data Data buffer
  794. * @v overwrite Overwrite file
  795. * @v len Length of data buffer
  796. * @v blksize Block size
  797. * @v ip Server address
  798. * @v filename Filename
  799. * @v info Additional information
  800. * @v callback Pass packets to callback instead of data buffer
  801. * @ret efirc EFI status code
  802. */
  803. static EFI_STATUS EFIAPI
  804. efi_pxe_mtftp ( EFI_PXE_BASE_CODE_PROTOCOL *base,
  805. EFI_PXE_BASE_CODE_TFTP_OPCODE opcode, VOID *data,
  806. BOOLEAN overwrite, UINT64 *len, UINTN *blksize,
  807. EFI_IP_ADDRESS *ip, UINT8 *filename,
  808. EFI_PXE_BASE_CODE_MTFTP_INFO *info, BOOLEAN callback ) {
  809. struct efi_pxe *pxe = container_of ( base, struct efi_pxe, base );
  810. int rc;
  811. DBGC ( pxe, "PXE %s MTFTP %d%s %p+%llx", pxe->name, opcode,
  812. ( overwrite ? " overwrite" : "" ), data, *len );
  813. if ( blksize )
  814. DBGC ( pxe, " blksize %zd", ( ( size_t ) *blksize ) );
  815. DBGC ( pxe, " %s:%s", efi_pxe_ip_ntoa ( pxe, ip ), filename );
  816. if ( info ) {
  817. DBGC ( pxe, " %s:%d:%d:%d:%d",
  818. efi_pxe_ip_ntoa ( pxe, &info->MCastIp ),
  819. info->CPort, info->SPort, info->ListenTimeout,
  820. info->TransmitTimeout );
  821. }
  822. DBGC ( pxe, "%s\n", ( callback ? " callback" : "" ) );
  823. /* Fail unless operation is supported */
  824. if ( ! ( ( opcode == EFI_PXE_BASE_CODE_TFTP_READ_FILE ) ||
  825. ( opcode == EFI_PXE_BASE_CODE_MTFTP_READ_FILE ) ) ) {
  826. DBGC ( pxe, "PXE %s unsupported MTFTP opcode %d\n",
  827. pxe->name, opcode );
  828. rc = -ENOTSUP;
  829. goto err_opcode;
  830. }
  831. /* Claim network devices */
  832. efi_snp_claim();
  833. /* Determine block size. Ignore the requested block size
  834. * unless we are using callbacks, since limiting HTTP to a
  835. * 512-byte TCP window is not sensible.
  836. */
  837. pxe->blksize = ( ( callback && blksize ) ? *blksize : -1UL );
  838. /* Initialise data transfer buffer */
  839. pxe->buf.data = data;
  840. pxe->buf.len = *len;
  841. /* Open download */
  842. if ( ( rc = efi_pxe_tftp_open ( pxe, ip,
  843. ( ( const char * ) filename ) ) ) != 0 )
  844. goto err_open;
  845. /* Wait for download to complete */
  846. pxe->rc = -EINPROGRESS;
  847. while ( pxe->rc == -EINPROGRESS )
  848. step();
  849. if ( ( rc = pxe->rc ) != 0 ) {
  850. DBGC ( pxe, "PXE %s download failed: %s\n",
  851. pxe->name, strerror ( rc ) );
  852. goto err_download;
  853. }
  854. err_download:
  855. efi_pxe_tftp_close ( pxe, rc );
  856. err_open:
  857. efi_snp_release();
  858. err_opcode:
  859. return EFIRC ( rc );
  860. }
  861. /**
  862. * Transmit UDP packet
  863. *
  864. * @v base PXE base code protocol
  865. * @v flags Operation flags
  866. * @v dest_ip Destination address
  867. * @v dest_port Destination port
  868. * @v gateway Gateway address
  869. * @v src_ip Source address
  870. * @v src_port Source port
  871. * @v hdr_len Header length
  872. * @v hdr Header data
  873. * @v len Length
  874. * @v data Data
  875. * @ret efirc EFI status code
  876. */
  877. static EFI_STATUS EFIAPI
  878. efi_pxe_udp_write ( EFI_PXE_BASE_CODE_PROTOCOL *base, UINT16 flags,
  879. EFI_IP_ADDRESS *dest_ip,
  880. EFI_PXE_BASE_CODE_UDP_PORT *dest_port,
  881. EFI_IP_ADDRESS *gateway, EFI_IP_ADDRESS *src_ip,
  882. EFI_PXE_BASE_CODE_UDP_PORT *src_port,
  883. UINTN *hdr_len, VOID *hdr, UINTN *len, VOID *data ) {
  884. struct efi_pxe *pxe = container_of ( base, struct efi_pxe, base );
  885. EFI_PXE_BASE_CODE_MODE *mode = &pxe->mode;
  886. struct io_buffer *iobuf;
  887. struct xfer_metadata meta;
  888. union {
  889. struct sockaddr_tcpip st;
  890. struct sockaddr sa;
  891. } dest;
  892. union {
  893. struct sockaddr_tcpip st;
  894. struct sockaddr sa;
  895. } src;
  896. int rc;
  897. DBGC2 ( pxe, "PXE %s UDP WRITE ", pxe->name );
  898. if ( src_ip )
  899. DBGC2 ( pxe, "%s", efi_pxe_ip_ntoa ( pxe, src_ip ) );
  900. DBGC2 ( pxe, ":" );
  901. if ( src_port &&
  902. ( ! ( flags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT ) ) ) {
  903. DBGC2 ( pxe, "%d", *src_port );
  904. } else {
  905. DBGC2 ( pxe, "*" );
  906. }
  907. DBGC2 ( pxe, "->%s:%d", efi_pxe_ip_ntoa ( pxe, dest_ip ), *dest_port );
  908. if ( gateway )
  909. DBGC2 ( pxe, " via %s", efi_pxe_ip_ntoa ( pxe, gateway ) );
  910. if ( hdr_len )
  911. DBGC2 ( pxe, " %p+%zx", hdr, ( ( size_t ) *hdr_len ) );
  912. DBGC2 ( pxe, " %p+%zx", data, ( ( size_t ) *len ) );
  913. if ( flags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_MAY_FRAGMENT )
  914. DBGC2 ( pxe, " frag" );
  915. DBGC2 ( pxe, "\n" );
  916. /* Open UDP connection (if applicable) */
  917. if ( ( rc = efi_pxe_udp_open ( pxe ) ) != 0 )
  918. goto err_open;
  919. /* Construct destination address */
  920. efi_pxe_ip_sockaddr ( pxe, dest_ip, &dest.sa );
  921. dest.st.st_port = htons ( *dest_port );
  922. /* Construct source address */
  923. efi_pxe_ip_sockaddr ( pxe, ( src_ip ? src_ip : &mode->StationIp ),
  924. &src.sa );
  925. if ( src_port &&
  926. ( ! ( flags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT ) ) ) {
  927. src.st.st_port = htons ( *src_port );
  928. } else {
  929. /* The API does not allow for a sensible concept of
  930. * binding to a local port, so just use a random value.
  931. */
  932. src.st.st_port = ( random() | htons ( 1024 ) );
  933. if ( src_port )
  934. *src_port = ntohs ( src.st.st_port );
  935. }
  936. /* Allocate I/O buffer */
  937. iobuf = xfer_alloc_iob ( &pxe->udp,
  938. ( *len + ( hdr_len ? *hdr_len : 0 ) ) );
  939. if ( ! iobuf ) {
  940. rc = -ENOMEM;
  941. goto err_alloc;
  942. }
  943. /* Populate I/O buffer */
  944. if ( hdr_len )
  945. memcpy ( iob_put ( iobuf, *hdr_len ), hdr, *hdr_len );
  946. memcpy ( iob_put ( iobuf, *len ), data, *len );
  947. /* Construct metadata */
  948. memset ( &meta, 0, sizeof ( meta ) );
  949. meta.src = &src.sa;
  950. meta.dest = &dest.sa;
  951. meta.netdev = pxe->netdev;
  952. /* Deliver I/O buffer */
  953. if ( ( rc = xfer_deliver ( &pxe->udp, iob_disown ( iobuf ),
  954. &meta ) ) != 0 ) {
  955. DBGC ( pxe, "PXE %s could not transmit: %s\n",
  956. pxe->name, strerror ( rc ) );
  957. goto err_deliver;
  958. }
  959. err_deliver:
  960. free_iob ( iobuf );
  961. err_alloc:
  962. efi_pxe_udp_schedule_close ( pxe );
  963. err_open:
  964. return EFIRC ( rc );
  965. }
  966. /**
  967. * Receive UDP packet
  968. *
  969. * @v base PXE base code protocol
  970. * @v flags Operation flags
  971. * @v dest_ip Destination address
  972. * @v dest_port Destination port
  973. * @v src_ip Source address
  974. * @v src_port Source port
  975. * @v hdr_len Header length
  976. * @v hdr Header data
  977. * @v len Length
  978. * @v data Data
  979. * @ret efirc EFI status code
  980. */
  981. static EFI_STATUS EFIAPI
  982. efi_pxe_udp_read ( EFI_PXE_BASE_CODE_PROTOCOL *base, UINT16 flags,
  983. EFI_IP_ADDRESS *dest_ip,
  984. EFI_PXE_BASE_CODE_UDP_PORT *dest_port,
  985. EFI_IP_ADDRESS *src_ip,
  986. EFI_PXE_BASE_CODE_UDP_PORT *src_port,
  987. UINTN *hdr_len, VOID *hdr, UINTN *len, VOID *data ) {
  988. struct efi_pxe *pxe = container_of ( base, struct efi_pxe, base );
  989. struct io_buffer *iobuf;
  990. struct efi_pxe_udp_pseudo_header *pshdr;
  991. EFI_IP_ADDRESS *actual_dest_ip;
  992. EFI_IP_ADDRESS *actual_src_ip;
  993. size_t addr_len;
  994. size_t frag_len;
  995. int rc;
  996. DBGC2 ( pxe, "PXE %s UDP READ ", pxe->name );
  997. if ( flags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_USE_FILTER ) {
  998. DBGC2 ( pxe, "(filter)" );
  999. } else if ( flags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_IP ) {
  1000. DBGC2 ( pxe, "*" );
  1001. } else if ( dest_ip ) {
  1002. DBGC2 ( pxe, "%s", efi_pxe_ip_ntoa ( pxe, dest_ip ) );
  1003. }
  1004. DBGC2 ( pxe, ":" );
  1005. if ( flags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT ) {
  1006. DBGC2 ( pxe, "*" );
  1007. } else if ( dest_port ) {
  1008. DBGC2 ( pxe, "%d", *dest_port );
  1009. } else {
  1010. DBGC2 ( pxe, "<NULL>" );
  1011. }
  1012. DBGC2 ( pxe, "<-" );
  1013. if ( flags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_IP ) {
  1014. DBGC2 ( pxe, "*" );
  1015. } else if ( src_ip ) {
  1016. DBGC2 ( pxe, "%s", efi_pxe_ip_ntoa ( pxe, src_ip ) );
  1017. } else {
  1018. DBGC2 ( pxe, "<NULL>" );
  1019. }
  1020. DBGC2 ( pxe, ":" );
  1021. if ( flags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT ) {
  1022. DBGC2 ( pxe, "*" );
  1023. } else if ( src_port ) {
  1024. DBGC2 ( pxe, "%d", *src_port );
  1025. } else {
  1026. DBGC2 ( pxe, "<NULL>" );
  1027. }
  1028. if ( hdr_len )
  1029. DBGC2 ( pxe, " %p+%zx", hdr, ( ( size_t ) *hdr_len ) );
  1030. DBGC2 ( pxe, " %p+%zx\n", data, ( ( size_t ) *len ) );
  1031. /* Open UDP connection (if applicable) */
  1032. if ( ( rc = efi_pxe_udp_open ( pxe ) ) != 0 )
  1033. goto err_open;
  1034. /* Try receiving a packet, if the queue is empty */
  1035. if ( list_empty ( &pxe->queue ) )
  1036. step();
  1037. /* Remove first packet from the queue */
  1038. iobuf = list_first_entry ( &pxe->queue, struct io_buffer, list );
  1039. if ( ! iobuf ) {
  1040. rc = -ETIMEDOUT; /* "no packet" */
  1041. goto err_empty;
  1042. }
  1043. list_del ( &iobuf->list );
  1044. /* Strip pseudo-header */
  1045. pshdr = iobuf->data;
  1046. addr_len = ( pshdr->net->net_addr_len );
  1047. iob_pull ( iobuf, sizeof ( *pshdr ) );
  1048. actual_dest_ip = iobuf->data;
  1049. iob_pull ( iobuf, addr_len );
  1050. actual_src_ip = iobuf->data;
  1051. iob_pull ( iobuf, addr_len );
  1052. DBGC2 ( pxe, "PXE %s UDP RX %s:%d", pxe->name,
  1053. pshdr->net->ntoa ( actual_dest_ip ), pshdr->dest_port );
  1054. DBGC2 ( pxe, "<-%s:%d len %#zx\n", pshdr->net->ntoa ( actual_src_ip ),
  1055. pshdr->src_port, iob_len ( iobuf ) );
  1056. /* Filter based on network-layer protocol */
  1057. if ( pshdr->net != pxe->net ) {
  1058. DBGC2 ( pxe, "PXE %s filtered out %s packet\n",
  1059. pxe->name, pshdr->net->name );
  1060. rc = -ETIMEDOUT; /* "no packet" */
  1061. goto err_filter;
  1062. }
  1063. /* Filter based on port numbers */
  1064. if ( ! ( ( flags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT ) ||
  1065. ( dest_port && ( *dest_port == pshdr->dest_port ) ) ) ) {
  1066. DBGC2 ( pxe, "PXE %s filtered out destination port %d\n",
  1067. pxe->name, pshdr->dest_port );
  1068. rc = -ETIMEDOUT; /* "no packet" */
  1069. goto err_filter;
  1070. }
  1071. if ( ! ( ( flags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT ) ||
  1072. ( src_port && ( *src_port == pshdr->src_port ) ) ) ) {
  1073. DBGC2 ( pxe, "PXE %s filtered out source port %d\n",
  1074. pxe->name, pshdr->src_port );
  1075. rc = -ETIMEDOUT; /* "no packet" */
  1076. goto err_filter;
  1077. }
  1078. /* Filter based on source IP address */
  1079. if ( ! ( ( flags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_IP ) ||
  1080. ( src_ip &&
  1081. ( memcmp ( src_ip, actual_src_ip, addr_len ) == 0 ) ) ) ) {
  1082. DBGC2 ( pxe, "PXE %s filtered out source IP %s\n",
  1083. pxe->name, pshdr->net->ntoa ( actual_src_ip ) );
  1084. rc = -ETIMEDOUT; /* "no packet" */
  1085. goto err_filter;
  1086. }
  1087. /* Filter based on destination IP address */
  1088. if ( ! ( ( ( flags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_USE_FILTER ) &&
  1089. efi_pxe_ip_filter ( pxe, actual_dest_ip ) ) ||
  1090. ( ( ! ( flags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_USE_FILTER ) ) &&
  1091. ( ( flags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_IP ) ||
  1092. ( dest_ip && ( memcmp ( dest_ip, actual_dest_ip,
  1093. addr_len ) == 0 ) ) ) ) ) ) {
  1094. DBGC2 ( pxe, "PXE %s filtered out destination IP %s\n",
  1095. pxe->name, pshdr->net->ntoa ( actual_dest_ip ) );
  1096. rc = -ETIMEDOUT; /* "no packet" */
  1097. goto err_filter;
  1098. }
  1099. /* Fill in addresses and port numbers */
  1100. if ( dest_ip )
  1101. memcpy ( dest_ip, actual_dest_ip, addr_len );
  1102. if ( dest_port )
  1103. *dest_port = pshdr->dest_port;
  1104. if ( src_ip )
  1105. memcpy ( src_ip, actual_src_ip, addr_len );
  1106. if ( src_port )
  1107. *src_port = pshdr->src_port;
  1108. /* Fill in header, if applicable */
  1109. if ( hdr_len ) {
  1110. frag_len = iob_len ( iobuf );
  1111. if ( frag_len > *hdr_len )
  1112. frag_len = *hdr_len;
  1113. memcpy ( hdr, iobuf->data, frag_len );
  1114. iob_pull ( iobuf, frag_len );
  1115. *hdr_len = frag_len;
  1116. }
  1117. /* Fill in data buffer */
  1118. frag_len = iob_len ( iobuf );
  1119. if ( frag_len > *len )
  1120. frag_len = *len;
  1121. memcpy ( data, iobuf->data, frag_len );
  1122. iob_pull ( iobuf, frag_len );
  1123. *len = frag_len;
  1124. /* Check for overflow */
  1125. if ( iob_len ( iobuf ) ) {
  1126. rc = -ERANGE;
  1127. goto err_too_short;
  1128. }
  1129. /* Success */
  1130. rc = 0;
  1131. err_too_short:
  1132. err_filter:
  1133. free_iob ( iobuf );
  1134. err_empty:
  1135. efi_pxe_udp_schedule_close ( pxe );
  1136. err_open:
  1137. return EFIRC ( rc );
  1138. }
  1139. /**
  1140. * Set receive filter
  1141. *
  1142. * @v base PXE base code protocol
  1143. * @v filter Receive filter
  1144. * @ret efirc EFI status code
  1145. */
  1146. static EFI_STATUS EFIAPI
  1147. efi_pxe_set_ip_filter ( EFI_PXE_BASE_CODE_PROTOCOL *base,
  1148. EFI_PXE_BASE_CODE_IP_FILTER *filter ) {
  1149. struct efi_pxe *pxe = container_of ( base, struct efi_pxe, base );
  1150. EFI_PXE_BASE_CODE_MODE *mode = &pxe->mode;
  1151. unsigned int i;
  1152. DBGC ( pxe, "PXE %s SET IP FILTER %02x",
  1153. pxe->name, filter->Filters );
  1154. for ( i = 0 ; i < filter->IpCnt ; i++ ) {
  1155. DBGC ( pxe, " %s",
  1156. efi_pxe_ip_ntoa ( pxe, &filter->IpList[i] ) );
  1157. }
  1158. DBGC ( pxe, "\n" );
  1159. /* Update filter */
  1160. memcpy ( &mode->IpFilter, filter, sizeof ( mode->IpFilter ) );
  1161. return 0;
  1162. }
  1163. /**
  1164. * Resolve MAC address
  1165. *
  1166. * @v base PXE base code protocol
  1167. * @v ip IP address
  1168. * @v mac MAC address to fill in
  1169. * @ret efirc EFI status code
  1170. */
  1171. static EFI_STATUS EFIAPI efi_pxe_arp ( EFI_PXE_BASE_CODE_PROTOCOL *base,
  1172. EFI_IP_ADDRESS *ip,
  1173. EFI_MAC_ADDRESS *mac ) {
  1174. struct efi_pxe *pxe = container_of ( base, struct efi_pxe, base );
  1175. DBGC ( pxe, "PXE %s ARP %s %p\n",
  1176. pxe->name, efi_pxe_ip_ntoa ( pxe, ip ), mac );
  1177. /* Not used by any bootstrap I can find to test with */
  1178. return EFI_UNSUPPORTED;
  1179. }
  1180. /**
  1181. * Set parameters
  1182. *
  1183. * @v base PXE base code protocol
  1184. * @v autoarp Automatic ARP packet generation
  1185. * @v sendguid Send GUID as client hardware address
  1186. * @v ttl IP time to live
  1187. * @v tos IP type of service
  1188. * @v callback Make callbacks
  1189. * @ret efirc EFI status code
  1190. */
  1191. static EFI_STATUS EFIAPI
  1192. efi_pxe_set_parameters ( EFI_PXE_BASE_CODE_PROTOCOL *base,
  1193. BOOLEAN *autoarp, BOOLEAN *sendguid, UINT8 *ttl,
  1194. UINT8 *tos, BOOLEAN *callback ) {
  1195. struct efi_pxe *pxe = container_of ( base, struct efi_pxe, base );
  1196. EFI_PXE_BASE_CODE_MODE *mode = &pxe->mode;
  1197. DBGC ( pxe, "PXE %s SET PARAMETERS", pxe->name );
  1198. if ( autoarp )
  1199. DBGC ( pxe, " %s", ( *autoarp ? "autoarp" : "noautoarp" ) );
  1200. if ( sendguid )
  1201. DBGC ( pxe, " %s", ( *sendguid ? "sendguid" : "sendmac" ) );
  1202. if ( ttl )
  1203. DBGC ( pxe, " ttl %d", *ttl );
  1204. if ( tos )
  1205. DBGC ( pxe, " tos %d", *tos );
  1206. if ( callback ) {
  1207. DBGC ( pxe, " %s",
  1208. ( *callback ? "callback" : "nocallback" ) );
  1209. }
  1210. DBGC ( pxe, "\n" );
  1211. /* Update parameters */
  1212. if ( autoarp )
  1213. mode->AutoArp = *autoarp;
  1214. if ( sendguid )
  1215. mode->SendGUID = *sendguid;
  1216. if ( ttl )
  1217. mode->TTL = *ttl;
  1218. if ( tos )
  1219. mode->ToS = *tos;
  1220. if ( callback )
  1221. mode->MakeCallbacks = *callback;
  1222. return 0;
  1223. }
  1224. /**
  1225. * Set IP address
  1226. *
  1227. * @v base PXE base code protocol
  1228. * @v ip IP address
  1229. * @v netmask Subnet mask
  1230. * @ret efirc EFI status code
  1231. */
  1232. static EFI_STATUS EFIAPI
  1233. efi_pxe_set_station_ip ( EFI_PXE_BASE_CODE_PROTOCOL *base,
  1234. EFI_IP_ADDRESS *ip, EFI_IP_ADDRESS *netmask ) {
  1235. struct efi_pxe *pxe = container_of ( base, struct efi_pxe, base );
  1236. EFI_PXE_BASE_CODE_MODE *mode = &pxe->mode;
  1237. DBGC ( pxe, "PXE %s SET STATION IP ", pxe->name );
  1238. if ( ip )
  1239. DBGC ( pxe, "%s", efi_pxe_ip_ntoa ( pxe, ip ) );
  1240. if ( netmask )
  1241. DBGC ( pxe, "/%s", efi_pxe_ip_ntoa ( pxe, netmask ) );
  1242. DBGC ( pxe, "\n" );
  1243. /* Update IP address and netmask */
  1244. if ( ip )
  1245. memcpy ( &mode->StationIp, ip, sizeof ( mode->StationIp ) );
  1246. if ( netmask )
  1247. memcpy ( &mode->SubnetMask, netmask, sizeof (mode->SubnetMask));
  1248. return 0;
  1249. }
  1250. /**
  1251. * Update cached DHCP packets
  1252. *
  1253. * @v base PXE base code protocol
  1254. * @v dhcpdisc_ok DHCPDISCOVER is valid
  1255. * @v dhcpack_ok DHCPACK received
  1256. * @v proxyoffer_ok ProxyDHCPOFFER received
  1257. * @v pxebsdisc_ok PxeBsDISCOVER valid
  1258. * @v pxebsack_ok PxeBsACK received
  1259. * @v pxebsbis_ok PxeBsBIS received
  1260. * @v dhcpdisc DHCPDISCOVER packet
  1261. * @v dhcpack DHCPACK packet
  1262. * @v proxyoffer ProxyDHCPOFFER packet
  1263. * @v pxebsdisc PxeBsDISCOVER packet
  1264. * @v pxebsack PxeBsACK packet
  1265. * @v pxebsbis PxeBsBIS packet
  1266. * @ret efirc EFI status code
  1267. */
  1268. static EFI_STATUS EFIAPI
  1269. efi_pxe_set_packets ( EFI_PXE_BASE_CODE_PROTOCOL *base, BOOLEAN *dhcpdisc_ok,
  1270. BOOLEAN *dhcpack_ok, BOOLEAN *proxyoffer_ok,
  1271. BOOLEAN *pxebsdisc_ok, BOOLEAN *pxebsack_ok,
  1272. BOOLEAN *pxebsbis_ok, EFI_PXE_BASE_CODE_PACKET *dhcpdisc,
  1273. EFI_PXE_BASE_CODE_PACKET *dhcpack,
  1274. EFI_PXE_BASE_CODE_PACKET *proxyoffer,
  1275. EFI_PXE_BASE_CODE_PACKET *pxebsdisc,
  1276. EFI_PXE_BASE_CODE_PACKET *pxebsack,
  1277. EFI_PXE_BASE_CODE_PACKET *pxebsbis ) {
  1278. struct efi_pxe *pxe = container_of ( base, struct efi_pxe, base );
  1279. EFI_PXE_BASE_CODE_MODE *mode = &pxe->mode;
  1280. DBGC ( pxe, "PXE %s SET PACKETS\n", pxe->name );
  1281. /* Update fake packet flags */
  1282. if ( dhcpdisc_ok )
  1283. mode->DhcpDiscoverValid = *dhcpdisc_ok;
  1284. if ( dhcpack_ok )
  1285. mode->DhcpAckReceived = *dhcpack_ok;
  1286. if ( proxyoffer_ok )
  1287. mode->ProxyOfferReceived = *proxyoffer_ok;
  1288. if ( pxebsdisc_ok )
  1289. mode->PxeDiscoverValid = *pxebsdisc_ok;
  1290. if ( pxebsack_ok )
  1291. mode->PxeReplyReceived = *pxebsack_ok;
  1292. if ( pxebsbis_ok )
  1293. mode->PxeBisReplyReceived = *pxebsbis_ok;
  1294. /* Update fake packet contents */
  1295. if ( dhcpdisc )
  1296. memcpy ( &mode->DhcpDiscover, dhcpdisc, sizeof ( *dhcpdisc ) );
  1297. if ( dhcpack )
  1298. memcpy ( &mode->DhcpAck, dhcpack, sizeof ( *dhcpack ) );
  1299. if ( proxyoffer )
  1300. memcpy ( &mode->ProxyOffer, proxyoffer, sizeof ( *proxyoffer ));
  1301. if ( pxebsdisc )
  1302. memcpy ( &mode->PxeDiscover, pxebsdisc, sizeof ( *pxebsdisc ) );
  1303. if ( pxebsack )
  1304. memcpy ( &mode->PxeReply, pxebsack, sizeof ( *pxebsack ) );
  1305. if ( pxebsbis )
  1306. memcpy ( &mode->PxeBisReply, pxebsbis, sizeof ( *pxebsbis ) );
  1307. return 0;
  1308. }
  1309. /** PXE base code protocol */
  1310. static EFI_PXE_BASE_CODE_PROTOCOL efi_pxe_base_code_protocol = {
  1311. .Revision = EFI_PXE_BASE_CODE_PROTOCOL_REVISION,
  1312. .Start = efi_pxe_start,
  1313. .Stop = efi_pxe_stop,
  1314. .Dhcp = efi_pxe_dhcp,
  1315. .Discover = efi_pxe_discover,
  1316. .Mtftp = efi_pxe_mtftp,
  1317. .UdpWrite = efi_pxe_udp_write,
  1318. .UdpRead = efi_pxe_udp_read,
  1319. .SetIpFilter = efi_pxe_set_ip_filter,
  1320. .Arp = efi_pxe_arp,
  1321. .SetParameters = efi_pxe_set_parameters,
  1322. .SetStationIp = efi_pxe_set_station_ip,
  1323. .SetPackets = efi_pxe_set_packets,
  1324. };
  1325. /**
  1326. * Install PXE base code protocol
  1327. *
  1328. * @v handle EFI handle
  1329. * @v netdev Underlying network device
  1330. * @ret rc Return status code
  1331. */
  1332. int efi_pxe_install ( EFI_HANDLE handle, struct net_device *netdev ) {
  1333. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  1334. struct tcpip_net_protocol *ipv6 = tcpip_net_protocol ( AF_INET6 );
  1335. struct efi_pxe *pxe;
  1336. struct in_addr ip;
  1337. BOOLEAN use_ipv6;
  1338. EFI_STATUS efirc;
  1339. int rc;
  1340. /* Allocate and initialise structure */
  1341. pxe = zalloc ( sizeof ( *pxe ) );
  1342. if ( ! pxe ) {
  1343. rc = -ENOMEM;
  1344. goto err_alloc;
  1345. }
  1346. ref_init ( &pxe->refcnt, efi_pxe_free );
  1347. pxe->netdev = netdev_get ( netdev );
  1348. pxe->name = netdev->name;
  1349. pxe->handle = handle;
  1350. memcpy ( &pxe->base, &efi_pxe_base_code_protocol, sizeof ( pxe->base ));
  1351. pxe->base.Mode = &pxe->mode;
  1352. pxe->buf.op = &efi_pxe_buf_operations;
  1353. intf_init ( &pxe->tftp, &efi_pxe_tftp_desc, &pxe->refcnt );
  1354. intf_init ( &pxe->udp, &efi_pxe_udp_desc, &pxe->refcnt );
  1355. INIT_LIST_HEAD ( &pxe->queue );
  1356. process_init_stopped ( &pxe->process, &efi_pxe_process_desc,
  1357. &pxe->refcnt );
  1358. /* Crude heuristic: assume that we prefer to use IPv4 if we
  1359. * have an IPv4 address for the network device, otherwise
  1360. * prefer IPv6 (if available).
  1361. */
  1362. fetch_ipv4_setting ( netdev_settings ( netdev ), &ip_setting, &ip );
  1363. use_ipv6 = ( ip.s_addr ? FALSE : ( ipv6 != NULL ) );
  1364. /* Start base code */
  1365. efi_pxe_start ( &pxe->base, use_ipv6 );
  1366. /* Install PXE base code protocol */
  1367. if ( ( efirc = bs->InstallMultipleProtocolInterfaces (
  1368. &handle, &efi_pxe_base_code_protocol_guid, &pxe->base,
  1369. NULL ) ) != 0 ) {
  1370. rc = -EEFI ( efirc );
  1371. DBGC ( pxe, "PXE %s could not install base code protocol: %s\n",
  1372. pxe->name, strerror ( rc ) );
  1373. goto err_install_protocol;
  1374. }
  1375. /* Transfer reference to list and return */
  1376. list_add_tail ( &pxe->list, &efi_pxes );
  1377. DBGC ( pxe, "PXE %s installed for %s\n",
  1378. pxe->name, efi_handle_name ( handle ) );
  1379. return 0;
  1380. bs->UninstallMultipleProtocolInterfaces (
  1381. handle, &efi_pxe_base_code_protocol_guid, &pxe->base,
  1382. NULL );
  1383. err_install_protocol:
  1384. ref_put ( &pxe->refcnt );
  1385. err_alloc:
  1386. return rc;
  1387. }
  1388. /**
  1389. * Uninstall PXE base code protocol
  1390. *
  1391. * @v handle EFI handle
  1392. */
  1393. void efi_pxe_uninstall ( EFI_HANDLE handle ) {
  1394. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  1395. struct efi_pxe *pxe;
  1396. /* Locate PXE base code */
  1397. pxe = efi_pxe_find ( handle );
  1398. if ( ! handle ) {
  1399. DBG ( "PXE could not find base code for %s\n",
  1400. efi_handle_name ( handle ) );
  1401. return;
  1402. }
  1403. /* Stop base code */
  1404. efi_pxe_stop ( &pxe->base );
  1405. /* Uninstall PXE base code protocol */
  1406. bs->UninstallMultipleProtocolInterfaces (
  1407. handle, &efi_pxe_base_code_protocol_guid, &pxe->base,
  1408. NULL );
  1409. /* Remove from list and drop list's reference */
  1410. list_del ( &pxe->list );
  1411. ref_put ( &pxe->refcnt );
  1412. }