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