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

pxe_udp.c 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /** @file
  2. *
  3. * PXE UDP API
  4. *
  5. */
  6. #include <string.h>
  7. #include <byteswap.h>
  8. #include <ipxe/iobuf.h>
  9. #include <ipxe/xfer.h>
  10. #include <ipxe/udp.h>
  11. #include <ipxe/uaccess.h>
  12. #include <ipxe/process.h>
  13. #include <pxe.h>
  14. /*
  15. * Copyright (C) 2004 Michael Brown <mbrown@fensystems.co.uk>.
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License as
  19. * published by the Free Software Foundation; either version 2 of the
  20. * License, or any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful, but
  23. * WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  25. * General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  30. * 02110-1301, USA.
  31. *
  32. * You can also choose to distribute this program under the terms of
  33. * the Unmodified Binary Distribution Licence (as given in the file
  34. * COPYING.UBDL), provided that you have satisfied its requirements.
  35. */
  36. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  37. /** A PXE UDP pseudo-header */
  38. struct pxe_udp_pseudo_header {
  39. /** Source IP address */
  40. IP4_t src_ip;
  41. /** Source port */
  42. UDP_PORT_t s_port;
  43. /** Destination IP address */
  44. IP4_t dest_ip;
  45. /** Destination port */
  46. UDP_PORT_t d_port;
  47. } __attribute__ (( packed ));
  48. /** A PXE UDP connection */
  49. struct pxe_udp_connection {
  50. /** Data transfer interface to UDP stack */
  51. struct interface xfer;
  52. /** Local address */
  53. struct sockaddr_in local;
  54. /** List of received packets */
  55. struct list_head list;
  56. };
  57. /**
  58. * Receive PXE UDP data
  59. *
  60. * @v pxe_udp PXE UDP connection
  61. * @v iobuf I/O buffer
  62. * @v meta Data transfer metadata
  63. * @ret rc Return status code
  64. *
  65. * Receives a packet as part of the current pxenv_udp_read()
  66. * operation.
  67. */
  68. static int pxe_udp_deliver ( struct pxe_udp_connection *pxe_udp,
  69. struct io_buffer *iobuf,
  70. struct xfer_metadata *meta ) {
  71. struct pxe_udp_pseudo_header *pshdr;
  72. struct sockaddr_in *sin_src;
  73. struct sockaddr_in *sin_dest;
  74. int rc;
  75. /* Extract metadata */
  76. assert ( meta );
  77. sin_src = ( struct sockaddr_in * ) meta->src;
  78. assert ( sin_src );
  79. assert ( sin_src->sin_family == AF_INET );
  80. sin_dest = ( struct sockaddr_in * ) meta->dest;
  81. assert ( sin_dest );
  82. assert ( sin_dest->sin_family == AF_INET );
  83. /* Construct pseudo-header */
  84. if ( ( rc = iob_ensure_headroom ( iobuf, sizeof ( *pshdr ) ) ) != 0 ) {
  85. DBG ( "PXE could not prepend pseudo-header\n" );
  86. rc = -ENOMEM;
  87. goto drop;
  88. }
  89. pshdr = iob_push ( iobuf, sizeof ( *pshdr ) );
  90. pshdr->src_ip = sin_src->sin_addr.s_addr;
  91. pshdr->s_port = sin_src->sin_port;
  92. pshdr->dest_ip = sin_dest->sin_addr.s_addr;
  93. pshdr->d_port = sin_dest->sin_port;
  94. /* Add to queue */
  95. list_add_tail ( &iobuf->list, &pxe_udp->list );
  96. return 0;
  97. drop:
  98. free_iob ( iobuf );
  99. return rc;
  100. }
  101. /** PXE UDP data transfer interface operations */
  102. static struct interface_operation pxe_udp_xfer_operations[] = {
  103. INTF_OP ( xfer_deliver, struct pxe_udp_connection *, pxe_udp_deliver ),
  104. };
  105. /** PXE UDP data transfer interface descriptor */
  106. static struct interface_descriptor pxe_udp_xfer_desc =
  107. INTF_DESC ( struct pxe_udp_connection, xfer, pxe_udp_xfer_operations );
  108. /** The PXE UDP connection */
  109. static struct pxe_udp_connection pxe_udp = {
  110. .xfer = INTF_INIT ( pxe_udp_xfer_desc ),
  111. .local = {
  112. .sin_family = AF_INET,
  113. },
  114. .list = LIST_HEAD_INIT ( pxe_udp.list ),
  115. };
  116. /**
  117. * UDP OPEN
  118. *
  119. * @v pxenv_udp_open Pointer to a struct s_PXENV_UDP_OPEN
  120. * @v s_PXENV_UDP_OPEN::src_ip IP address of this station, or 0.0.0.0
  121. * @ret #PXENV_EXIT_SUCCESS Always
  122. * @ret s_PXENV_UDP_OPEN::Status PXE status code
  123. * @err #PXENV_STATUS_UDP_OPEN UDP connection already open
  124. * @err #PXENV_STATUS_OUT_OF_RESOURCES Could not open connection
  125. *
  126. * Prepares the PXE stack for communication using pxenv_udp_write()
  127. * and pxenv_udp_read().
  128. *
  129. * The IP address supplied in s_PXENV_UDP_OPEN::src_ip will be
  130. * recorded and used as the local station's IP address for all further
  131. * communication, including communication by means other than
  132. * pxenv_udp_write() and pxenv_udp_read(). (If
  133. * s_PXENV_UDP_OPEN::src_ip is 0.0.0.0, the local station's IP address
  134. * will remain unchanged.)
  135. *
  136. * You can only have one open UDP connection at a time. This is not a
  137. * meaningful restriction, since pxenv_udp_write() and
  138. * pxenv_udp_read() allow you to specify arbitrary local and remote
  139. * ports and an arbitrary remote address for each packet. According
  140. * to the PXE specifiation, you cannot have a UDP connection open at
  141. * the same time as a TFTP connection; this restriction does not apply
  142. * to Etherboot.
  143. *
  144. * On x86, you must set the s_PXE::StatusCallout field to a nonzero
  145. * value before calling this function in protected mode. You cannot
  146. * call this function with a 32-bit stack segment. (See the relevant
  147. * @ref pxe_x86_pmode16 "implementation note" for more details.)
  148. *
  149. * @note The PXE specification does not make it clear whether the IP
  150. * address supplied in s_PXENV_UDP_OPEN::src_ip should be used only
  151. * for this UDP connection, or retained for all future communication.
  152. * The latter seems more consistent with typical PXE stack behaviour.
  153. *
  154. * @note Etherboot currently ignores the s_PXENV_UDP_OPEN::src_ip
  155. * parameter.
  156. *
  157. */
  158. static PXENV_EXIT_t pxenv_udp_open ( struct s_PXENV_UDP_OPEN *pxenv_udp_open ) {
  159. int rc;
  160. DBG ( "PXENV_UDP_OPEN" );
  161. /* Record source IP address */
  162. pxe_udp.local.sin_addr.s_addr = pxenv_udp_open->src_ip;
  163. DBG ( " %s\n", inet_ntoa ( pxe_udp.local.sin_addr ) );
  164. /* Open promiscuous UDP connection */
  165. intf_restart ( &pxe_udp.xfer, 0 );
  166. if ( ( rc = udp_open_promisc ( &pxe_udp.xfer ) ) != 0 ) {
  167. DBG ( "PXENV_UDP_OPEN could not open promiscuous socket: %s\n",
  168. strerror ( rc ) );
  169. pxenv_udp_open->Status = PXENV_STATUS ( rc );
  170. return PXENV_EXIT_FAILURE;
  171. }
  172. pxenv_udp_open->Status = PXENV_STATUS_SUCCESS;
  173. return PXENV_EXIT_SUCCESS;
  174. }
  175. /**
  176. * UDP CLOSE
  177. *
  178. * @v pxenv_udp_close Pointer to a struct s_PXENV_UDP_CLOSE
  179. * @ret #PXENV_EXIT_SUCCESS Always
  180. * @ret s_PXENV_UDP_CLOSE::Status PXE status code
  181. * @err None -
  182. *
  183. * Closes a UDP connection opened with pxenv_udp_open().
  184. *
  185. * You can only have one open UDP connection at a time. You cannot
  186. * have a UDP connection open at the same time as a TFTP connection.
  187. * You cannot use pxenv_udp_close() to close a TFTP connection; use
  188. * pxenv_tftp_close() instead.
  189. *
  190. * On x86, you must set the s_PXE::StatusCallout field to a nonzero
  191. * value before calling this function in protected mode. You cannot
  192. * call this function with a 32-bit stack segment. (See the relevant
  193. * @ref pxe_x86_pmode16 "implementation note" for more details.)
  194. *
  195. */
  196. static PXENV_EXIT_t
  197. pxenv_udp_close ( struct s_PXENV_UDP_CLOSE *pxenv_udp_close ) {
  198. struct io_buffer *iobuf;
  199. struct io_buffer *tmp;
  200. DBG ( "PXENV_UDP_CLOSE\n" );
  201. /* Close UDP connection */
  202. intf_restart ( &pxe_udp.xfer, 0 );
  203. /* Discard any received packets */
  204. list_for_each_entry_safe ( iobuf, tmp, &pxe_udp.list, list ) {
  205. list_del ( &iobuf->list );
  206. free_iob ( iobuf );
  207. }
  208. pxenv_udp_close->Status = PXENV_STATUS_SUCCESS;
  209. return PXENV_EXIT_SUCCESS;
  210. }
  211. /**
  212. * UDP WRITE
  213. *
  214. * @v pxenv_udp_write Pointer to a struct s_PXENV_UDP_WRITE
  215. * @v s_PXENV_UDP_WRITE::ip Destination IP address
  216. * @v s_PXENV_UDP_WRITE::gw Relay agent IP address, or 0.0.0.0
  217. * @v s_PXENV_UDP_WRITE::src_port Source UDP port, or 0
  218. * @v s_PXENV_UDP_WRITE::dst_port Destination UDP port
  219. * @v s_PXENV_UDP_WRITE::buffer_size Length of the UDP payload
  220. * @v s_PXENV_UDP_WRITE::buffer Address of the UDP payload
  221. * @ret #PXENV_EXIT_SUCCESS Packet was transmitted successfully
  222. * @ret #PXENV_EXIT_FAILURE Packet could not be transmitted
  223. * @ret s_PXENV_UDP_WRITE::Status PXE status code
  224. * @err #PXENV_STATUS_UDP_CLOSED UDP connection is not open
  225. * @err #PXENV_STATUS_UNDI_TRANSMIT_ERROR Could not transmit packet
  226. *
  227. * Transmits a single UDP packet. A valid IP and UDP header will be
  228. * prepended to the payload in s_PXENV_UDP_WRITE::buffer; the buffer
  229. * should not contain precomputed IP and UDP headers, nor should it
  230. * contain space allocated for these headers. The first byte of the
  231. * buffer will be transmitted as the first byte following the UDP
  232. * header.
  233. *
  234. * If s_PXENV_UDP_WRITE::gw is 0.0.0.0, normal IP routing will take
  235. * place. See the relevant @ref pxe_routing "implementation note" for
  236. * more details.
  237. *
  238. * If s_PXENV_UDP_WRITE::src_port is 0, port 2069 will be used.
  239. *
  240. * You must have opened a UDP connection with pxenv_udp_open() before
  241. * calling pxenv_udp_write().
  242. *
  243. * On x86, you must set the s_PXE::StatusCallout field to a nonzero
  244. * value before calling this function in protected mode. You cannot
  245. * call this function with a 32-bit stack segment. (See the relevant
  246. * @ref pxe_x86_pmode16 "implementation note" for more details.)
  247. *
  248. * @note Etherboot currently ignores the s_PXENV_UDP_WRITE::gw
  249. * parameter.
  250. *
  251. */
  252. static PXENV_EXIT_t
  253. pxenv_udp_write ( struct s_PXENV_UDP_WRITE *pxenv_udp_write ) {
  254. struct sockaddr_in dest;
  255. struct xfer_metadata meta = {
  256. .src = ( struct sockaddr * ) &pxe_udp.local,
  257. .dest = ( struct sockaddr * ) &dest,
  258. .netdev = pxe_netdev,
  259. };
  260. size_t len;
  261. struct io_buffer *iobuf;
  262. userptr_t buffer;
  263. int rc;
  264. DBG ( "PXENV_UDP_WRITE" );
  265. /* Construct destination socket address */
  266. memset ( &dest, 0, sizeof ( dest ) );
  267. dest.sin_family = AF_INET;
  268. dest.sin_addr.s_addr = pxenv_udp_write->ip;
  269. dest.sin_port = pxenv_udp_write->dst_port;
  270. /* Set local (source) port. PXE spec says source port is 2069
  271. * if not specified. Really, this ought to be set at UDP open
  272. * time but hey, we didn't design this API.
  273. */
  274. pxe_udp.local.sin_port = pxenv_udp_write->src_port;
  275. if ( ! pxe_udp.local.sin_port )
  276. pxe_udp.local.sin_port = htons ( 2069 );
  277. /* FIXME: we ignore the gateway specified, since we're
  278. * confident of being able to do our own routing. We should
  279. * probably allow for multiple gateways.
  280. */
  281. /* Allocate and fill data buffer */
  282. len = pxenv_udp_write->buffer_size;
  283. iobuf = xfer_alloc_iob ( &pxe_udp.xfer, len );
  284. if ( ! iobuf ) {
  285. DBG ( " out of memory\n" );
  286. pxenv_udp_write->Status = PXENV_STATUS_OUT_OF_RESOURCES;
  287. return PXENV_EXIT_FAILURE;
  288. }
  289. buffer = real_to_user ( pxenv_udp_write->buffer.segment,
  290. pxenv_udp_write->buffer.offset );
  291. copy_from_user ( iob_put ( iobuf, len ), buffer, 0, len );
  292. DBG ( " %04x:%04x+%x %d->%s:%d\n", pxenv_udp_write->buffer.segment,
  293. pxenv_udp_write->buffer.offset, pxenv_udp_write->buffer_size,
  294. ntohs ( pxenv_udp_write->src_port ),
  295. inet_ntoa ( dest.sin_addr ),
  296. ntohs ( pxenv_udp_write->dst_port ) );
  297. /* Transmit packet */
  298. if ( ( rc = xfer_deliver ( &pxe_udp.xfer, iobuf, &meta ) ) != 0 ) {
  299. DBG ( "PXENV_UDP_WRITE could not transmit: %s\n",
  300. strerror ( rc ) );
  301. pxenv_udp_write->Status = PXENV_STATUS ( rc );
  302. return PXENV_EXIT_FAILURE;
  303. }
  304. pxenv_udp_write->Status = PXENV_STATUS_SUCCESS;
  305. return PXENV_EXIT_SUCCESS;
  306. }
  307. /**
  308. * UDP READ
  309. *
  310. * @v pxenv_udp_read Pointer to a struct s_PXENV_UDP_READ
  311. * @v s_PXENV_UDP_READ::dest_ip Destination IP address, or 0.0.0.0
  312. * @v s_PXENV_UDP_READ::d_port Destination UDP port, or 0
  313. * @v s_PXENV_UDP_READ::buffer_size Size of the UDP payload buffer
  314. * @v s_PXENV_UDP_READ::buffer Address of the UDP payload buffer
  315. * @ret #PXENV_EXIT_SUCCESS A packet has been received
  316. * @ret #PXENV_EXIT_FAILURE No packet has been received
  317. * @ret s_PXENV_UDP_READ::Status PXE status code
  318. * @ret s_PXENV_UDP_READ::src_ip Source IP address
  319. * @ret s_PXENV_UDP_READ::dest_ip Destination IP address
  320. * @ret s_PXENV_UDP_READ::s_port Source UDP port
  321. * @ret s_PXENV_UDP_READ::d_port Destination UDP port
  322. * @ret s_PXENV_UDP_READ::buffer_size Length of UDP payload
  323. * @err #PXENV_STATUS_UDP_CLOSED UDP connection is not open
  324. * @err #PXENV_STATUS_FAILURE No packet was ready to read
  325. *
  326. * Receive a single UDP packet. This is a non-blocking call; if no
  327. * packet is ready to read, the call will return instantly with
  328. * s_PXENV_UDP_READ::Status==PXENV_STATUS_FAILURE.
  329. *
  330. * If s_PXENV_UDP_READ::dest_ip is 0.0.0.0, UDP packets addressed to
  331. * any IP address will be accepted and may be returned to the caller.
  332. *
  333. * If s_PXENV_UDP_READ::d_port is 0, UDP packets addressed to any UDP
  334. * port will be accepted and may be returned to the caller.
  335. *
  336. * You must have opened a UDP connection with pxenv_udp_open() before
  337. * calling pxenv_udp_read().
  338. *
  339. * On x86, you must set the s_PXE::StatusCallout field to a nonzero
  340. * value before calling this function in protected mode. You cannot
  341. * call this function with a 32-bit stack segment. (See the relevant
  342. * @ref pxe_x86_pmode16 "implementation note" for more details.)
  343. *
  344. * @note The PXE specification (version 2.1) does not state that we
  345. * should fill in s_PXENV_UDP_READ::dest_ip and
  346. * s_PXENV_UDP_READ::d_port, but Microsoft Windows' NTLDR program
  347. * expects us to do so, and will fail if we don't.
  348. *
  349. */
  350. static PXENV_EXIT_t pxenv_udp_read ( struct s_PXENV_UDP_READ *pxenv_udp_read ) {
  351. struct in_addr dest_ip_wanted = { .s_addr = pxenv_udp_read->dest_ip };
  352. struct in_addr dest_ip;
  353. struct io_buffer *iobuf;
  354. struct pxe_udp_pseudo_header *pshdr;
  355. uint16_t d_port_wanted = pxenv_udp_read->d_port;
  356. uint16_t d_port;
  357. userptr_t buffer;
  358. size_t len;
  359. /* Try receiving a packet, if the queue is empty */
  360. if ( list_empty ( &pxe_udp.list ) )
  361. step();
  362. /* Remove first packet from the queue */
  363. iobuf = list_first_entry ( &pxe_udp.list, struct io_buffer, list );
  364. if ( ! iobuf ) {
  365. /* No packet received */
  366. DBG2 ( "PXENV_UDP_READ\n" );
  367. goto no_packet;
  368. }
  369. list_del ( &iobuf->list );
  370. /* Strip pseudo-header */
  371. assert ( iob_len ( iobuf ) >= sizeof ( *pshdr ) );
  372. pshdr = iobuf->data;
  373. iob_pull ( iobuf, sizeof ( *pshdr ) );
  374. dest_ip.s_addr = pshdr->dest_ip;
  375. d_port = pshdr->d_port;
  376. DBG ( "PXENV_UDP_READ" );
  377. /* Filter on destination address and/or port */
  378. if ( dest_ip_wanted.s_addr &&
  379. ( dest_ip_wanted.s_addr != dest_ip.s_addr ) ) {
  380. DBG ( " wrong IP %s", inet_ntoa ( dest_ip ) );
  381. DBG ( " (wanted %s)\n", inet_ntoa ( dest_ip_wanted ) );
  382. goto drop;
  383. }
  384. if ( d_port_wanted && ( d_port_wanted != d_port ) ) {
  385. DBG ( " wrong port %d", htons ( d_port ) );
  386. DBG ( " (wanted %d)\n", htons ( d_port_wanted ) );
  387. goto drop;
  388. }
  389. /* Copy packet to buffer and record length */
  390. buffer = real_to_user ( pxenv_udp_read->buffer.segment,
  391. pxenv_udp_read->buffer.offset );
  392. len = iob_len ( iobuf );
  393. if ( len > pxenv_udp_read->buffer_size )
  394. len = pxenv_udp_read->buffer_size;
  395. copy_to_user ( buffer, 0, iobuf->data, len );
  396. pxenv_udp_read->buffer_size = len;
  397. /* Fill in source/dest information */
  398. pxenv_udp_read->src_ip = pshdr->src_ip;
  399. pxenv_udp_read->s_port = pshdr->s_port;
  400. pxenv_udp_read->dest_ip = pshdr->dest_ip;
  401. pxenv_udp_read->d_port = pshdr->d_port;
  402. DBG ( " %04x:%04x+%x %s:", pxenv_udp_read->buffer.segment,
  403. pxenv_udp_read->buffer.offset, pxenv_udp_read->buffer_size,
  404. inet_ntoa ( *( ( struct in_addr * ) &pxenv_udp_read->src_ip ) ));
  405. DBG ( "%d<-%s:%d\n", ntohs ( pxenv_udp_read->s_port ),
  406. inet_ntoa ( *( ( struct in_addr * ) &pxenv_udp_read->dest_ip ) ),
  407. ntohs ( pxenv_udp_read->d_port ) );
  408. /* Free I/O buffer */
  409. free_iob ( iobuf );
  410. pxenv_udp_read->Status = PXENV_STATUS_SUCCESS;
  411. return PXENV_EXIT_SUCCESS;
  412. drop:
  413. free_iob ( iobuf );
  414. no_packet:
  415. pxenv_udp_read->Status = PXENV_STATUS_FAILURE;
  416. return PXENV_EXIT_FAILURE;
  417. }
  418. /** PXE UDP API */
  419. struct pxe_api_call pxe_udp_api[] __pxe_api_call = {
  420. PXE_API_CALL ( PXENV_UDP_OPEN, pxenv_udp_open,
  421. struct s_PXENV_UDP_OPEN ),
  422. PXE_API_CALL ( PXENV_UDP_CLOSE, pxenv_udp_close,
  423. struct s_PXENV_UDP_CLOSE ),
  424. PXE_API_CALL ( PXENV_UDP_WRITE, pxenv_udp_write,
  425. struct s_PXENV_UDP_WRITE ),
  426. PXE_API_CALL ( PXENV_UDP_READ, pxenv_udp_read,
  427. struct s_PXENV_UDP_READ ),
  428. };