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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /** @file
  2. *
  3. * PXE UDP API
  4. *
  5. */
  6. #include <string.h>
  7. #include <byteswap.h>
  8. #include <gpxe/xfer.h>
  9. #include <gpxe/udp.h>
  10. #include <gpxe/uaccess.h>
  11. #include <gpxe/process.h>
  12. #include <pxe.h>
  13. /*
  14. * Copyright (C) 2004 Michael Brown <mbrown@fensystems.co.uk>.
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License as
  18. * published by the Free Software Foundation; either version 2 of the
  19. * License, or any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful, but
  22. * WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  24. * General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29. */
  30. /** A PXE UDP connection */
  31. struct pxe_udp_connection {
  32. /** Data transfer interface to UDP stack */
  33. struct xfer_interface xfer;
  34. /** Local address */
  35. struct sockaddr_in local;
  36. /** Current PXENV_UDP_READ parameter block */
  37. struct s_PXENV_UDP_READ *pxenv_udp_read;
  38. };
  39. /**
  40. * Receive PXE UDP data
  41. *
  42. * @v xfer Data transfer interface
  43. * @v iobuf I/O buffer
  44. * @v meta Data transfer metadata
  45. * @ret rc Return status code
  46. *
  47. * Receives a packet as part of the current pxenv_udp_read()
  48. * operation.
  49. */
  50. static int pxe_udp_deliver_iob ( struct xfer_interface *xfer,
  51. struct io_buffer *iobuf,
  52. struct xfer_metadata *meta ) {
  53. struct pxe_udp_connection *pxe_udp =
  54. container_of ( xfer, struct pxe_udp_connection, xfer );
  55. struct s_PXENV_UDP_READ *pxenv_udp_read = pxe_udp->pxenv_udp_read;
  56. struct sockaddr_in *sin_src;
  57. struct sockaddr_in *sin_dest;
  58. userptr_t buffer;
  59. size_t len;
  60. int rc = 0;
  61. if ( ! pxenv_udp_read ) {
  62. DBG ( "PXE discarded UDP packet\n" );
  63. rc = -ENOBUFS;
  64. goto done;
  65. }
  66. /* Copy packet to buffer and record length */
  67. buffer = real_to_user ( pxenv_udp_read->buffer.segment,
  68. pxenv_udp_read->buffer.offset );
  69. len = iob_len ( iobuf );
  70. if ( len > pxenv_udp_read->buffer_size )
  71. len = pxenv_udp_read->buffer_size;
  72. copy_to_user ( buffer, 0, iobuf->data, len );
  73. pxenv_udp_read->buffer_size = len;
  74. /* Fill in source/dest information */
  75. assert ( meta );
  76. sin_src = ( struct sockaddr_in * ) meta->src;
  77. assert ( sin_src );
  78. assert ( sin_src->sin_family == AF_INET );
  79. pxenv_udp_read->src_ip = sin_src->sin_addr.s_addr;
  80. pxenv_udp_read->s_port = sin_src->sin_port;
  81. sin_dest = ( struct sockaddr_in * ) meta->dest;
  82. assert ( sin_dest );
  83. assert ( sin_dest->sin_family == AF_INET );
  84. pxenv_udp_read->dest_ip = sin_dest->sin_addr.s_addr;
  85. pxenv_udp_read->d_port = sin_dest->sin_port;
  86. /* Mark as received */
  87. pxe_udp->pxenv_udp_read = NULL;
  88. done:
  89. free_iob ( iobuf );
  90. return rc;
  91. }
  92. /** PXE UDP data transfer interface operations */
  93. static struct xfer_interface_operations pxe_udp_xfer_operations = {
  94. .close = ignore_xfer_close,
  95. .vredirect = ignore_xfer_vredirect,
  96. .seek = ignore_xfer_seek,
  97. .window = unlimited_xfer_window,
  98. .alloc_iob = default_xfer_alloc_iob,
  99. .deliver_iob = pxe_udp_deliver_iob,
  100. .deliver_raw = xfer_deliver_as_iob,
  101. };
  102. /** The PXE UDP connection */
  103. static struct pxe_udp_connection pxe_udp = {
  104. .xfer = {
  105. .intf = {
  106. .dest = &null_xfer.intf,
  107. .refcnt = NULL,
  108. },
  109. .op = &pxe_udp_xfer_operations,
  110. },
  111. .local = {
  112. .sin_family = AF_INET,
  113. },
  114. };
  115. /**
  116. * UDP OPEN
  117. *
  118. * @v pxenv_udp_open Pointer to a struct s_PXENV_UDP_OPEN
  119. * @v s_PXENV_UDP_OPEN::src_ip IP address of this station, or 0.0.0.0
  120. * @ret #PXENV_EXIT_SUCCESS Always
  121. * @ret s_PXENV_UDP_OPEN::Status PXE status code
  122. * @err #PXENV_STATUS_UDP_OPEN UDP connection already open
  123. * @err #PXENV_STATUS_OUT_OF_RESOURCES Could not open connection
  124. *
  125. * Prepares the PXE stack for communication using pxenv_udp_write()
  126. * and pxenv_udp_read().
  127. *
  128. * The IP address supplied in s_PXENV_UDP_OPEN::src_ip will be
  129. * recorded and used as the local station's IP address for all further
  130. * communication, including communication by means other than
  131. * pxenv_udp_write() and pxenv_udp_read(). (If
  132. * s_PXENV_UDP_OPEN::src_ip is 0.0.0.0, the local station's IP address
  133. * will remain unchanged.)
  134. *
  135. * You can only have one open UDP connection at a time. This is not a
  136. * meaningful restriction, since pxenv_udp_write() and
  137. * pxenv_udp_read() allow you to specify arbitrary local and remote
  138. * ports and an arbitrary remote address for each packet. According
  139. * to the PXE specifiation, you cannot have a UDP connection open at
  140. * the same time as a TFTP connection; this restriction does not apply
  141. * to Etherboot.
  142. *
  143. * On x86, you must set the s_PXE::StatusCallout field to a nonzero
  144. * value before calling this function in protected mode. You cannot
  145. * call this function with a 32-bit stack segment. (See the relevant
  146. * @ref pxe_x86_pmode16 "implementation note" for more details.)
  147. *
  148. * @note The PXE specification does not make it clear whether the IP
  149. * address supplied in s_PXENV_UDP_OPEN::src_ip should be used only
  150. * for this UDP connection, or retained for all future communication.
  151. * The latter seems more consistent with typical PXE stack behaviour.
  152. *
  153. * @note Etherboot currently ignores the s_PXENV_UDP_OPEN::src_ip
  154. * parameter.
  155. *
  156. */
  157. PXENV_EXIT_t pxenv_udp_open ( struct s_PXENV_UDP_OPEN *pxenv_udp_open ) {
  158. int rc;
  159. DBG ( "PXENV_UDP_OPEN" );
  160. /* Record source IP address */
  161. pxe_udp.local.sin_addr.s_addr = pxenv_udp_open->src_ip;
  162. /* Open promiscuous UDP connection */
  163. xfer_close ( &pxe_udp.xfer, 0 );
  164. if ( ( rc = udp_open_promisc ( &pxe_udp.xfer ) ) != 0 ) {
  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. PXENV_EXIT_t pxenv_udp_close ( struct s_PXENV_UDP_CLOSE *pxenv_udp_close ) {
  193. DBG ( "PXENV_UDP_CLOSE" );
  194. /* Close UDP connection */
  195. xfer_close ( &pxe_udp.xfer, 0 );
  196. pxenv_udp_close->Status = PXENV_STATUS_SUCCESS;
  197. return PXENV_EXIT_SUCCESS;
  198. }
  199. /**
  200. * UDP WRITE
  201. *
  202. * @v pxenv_udp_write Pointer to a struct s_PXENV_UDP_WRITE
  203. * @v s_PXENV_UDP_WRITE::ip Destination IP address
  204. * @v s_PXENV_UDP_WRITE::gw Relay agent IP address, or 0.0.0.0
  205. * @v s_PXENV_UDP_WRITE::src_port Source UDP port, or 0
  206. * @v s_PXENV_UDP_WRITE::dst_port Destination UDP port
  207. * @v s_PXENV_UDP_WRITE::buffer_size Length of the UDP payload
  208. * @v s_PXENV_UDP_WRITE::buffer Address of the UDP payload
  209. * @ret #PXENV_EXIT_SUCCESS Packet was transmitted successfully
  210. * @ret #PXENV_EXIT_FAILURE Packet could not be transmitted
  211. * @ret s_PXENV_UDP_WRITE::Status PXE status code
  212. * @err #PXENV_STATUS_UDP_CLOSED UDP connection is not open
  213. * @err #PXENV_STATUS_UNDI_TRANSMIT_ERROR Could not transmit packet
  214. *
  215. * Transmits a single UDP packet. A valid IP and UDP header will be
  216. * prepended to the payload in s_PXENV_UDP_WRITE::buffer; the buffer
  217. * should not contain precomputed IP and UDP headers, nor should it
  218. * contain space allocated for these headers. The first byte of the
  219. * buffer will be transmitted as the first byte following the UDP
  220. * header.
  221. *
  222. * If s_PXENV_UDP_WRITE::gw is 0.0.0.0, normal IP routing will take
  223. * place. See the relevant @ref pxe_routing "implementation note" for
  224. * more details.
  225. *
  226. * If s_PXENV_UDP_WRITE::src_port is 0, port 2069 will be used.
  227. *
  228. * You must have opened a UDP connection with pxenv_udp_open() before
  229. * calling pxenv_udp_write().
  230. *
  231. * On x86, you must set the s_PXE::StatusCallout field to a nonzero
  232. * value before calling this function in protected mode. You cannot
  233. * call this function with a 32-bit stack segment. (See the relevant
  234. * @ref pxe_x86_pmode16 "implementation note" for more details.)
  235. *
  236. * @note Etherboot currently ignores the s_PXENV_UDP_WRITE::gw
  237. * parameter.
  238. *
  239. */
  240. PXENV_EXIT_t pxenv_udp_write ( struct s_PXENV_UDP_WRITE *pxenv_udp_write ) {
  241. struct sockaddr_in dest;
  242. struct xfer_metadata meta = {
  243. .src = ( struct sockaddr * ) &pxe_udp.local,
  244. .dest = ( struct sockaddr * ) &dest,
  245. };
  246. size_t len;
  247. struct io_buffer *iobuf;
  248. userptr_t buffer;
  249. int rc;
  250. DBG ( "PXENV_UDP_WRITE" );
  251. /* Construct destination socket address */
  252. memset ( &dest, 0, sizeof ( dest ) );
  253. dest.sin_family = AF_INET;
  254. dest.sin_addr.s_addr = pxenv_udp_write->ip;
  255. dest.sin_port = pxenv_udp_write->dst_port;
  256. /* Set local (source) port. PXE spec says source port is 2069
  257. * if not specified. Really, this ought to be set at UDP open
  258. * time but hey, we didn't design this API.
  259. */
  260. pxe_udp.local.sin_port = pxenv_udp_write->src_port;
  261. if ( ! pxe_udp.local.sin_port )
  262. pxe_udp.local.sin_port = htons ( 2069 );
  263. /* FIXME: we ignore the gateway specified, since we're
  264. * confident of being able to do our own routing. We should
  265. * probably allow for multiple gateways.
  266. */
  267. /* Allocate and fill data buffer */
  268. len = pxenv_udp_write->buffer_size;
  269. iobuf = xfer_alloc_iob ( &pxe_udp.xfer, len );
  270. if ( ! iobuf ) {
  271. pxenv_udp_write->Status = PXENV_STATUS_OUT_OF_RESOURCES;
  272. return PXENV_EXIT_FAILURE;
  273. }
  274. buffer = real_to_user ( pxenv_udp_write->buffer.segment,
  275. pxenv_udp_write->buffer.offset );
  276. copy_from_user ( iob_put ( iobuf, len ), buffer, 0, len );
  277. DBG ( " %04x:%04x+%x %d->%s:%d", pxenv_udp_write->buffer.segment,
  278. pxenv_udp_write->buffer.offset, pxenv_udp_write->buffer_size,
  279. ntohs ( pxenv_udp_write->src_port ),
  280. inet_ntoa ( dest.sin_addr ),
  281. ntohs ( pxenv_udp_write->dst_port ) );
  282. /* Transmit packet */
  283. if ( ( rc = xfer_deliver_iob_meta ( &pxe_udp.xfer, iobuf,
  284. &meta ) ) != 0 ) {
  285. pxenv_udp_write->Status = PXENV_STATUS ( rc );
  286. return PXENV_EXIT_FAILURE;
  287. }
  288. pxenv_udp_write->Status = PXENV_STATUS_SUCCESS;
  289. return PXENV_EXIT_SUCCESS;
  290. }
  291. /**
  292. * UDP READ
  293. *
  294. * @v pxenv_udp_read Pointer to a struct s_PXENV_UDP_READ
  295. * @v s_PXENV_UDP_READ::dest_ip Destination IP address, or 0.0.0.0
  296. * @v s_PXENV_UDP_READ::d_port Destination UDP port, or 0
  297. * @v s_PXENV_UDP_READ::buffer_size Size of the UDP payload buffer
  298. * @v s_PXENV_UDP_READ::buffer Address of the UDP payload buffer
  299. * @ret #PXENV_EXIT_SUCCESS A packet has been received
  300. * @ret #PXENV_EXIT_FAILURE No packet has been received
  301. * @ret s_PXENV_UDP_READ::Status PXE status code
  302. * @ret s_PXENV_UDP_READ::src_ip Source IP address
  303. * @ret s_PXENV_UDP_READ::dest_ip Destination IP address
  304. * @ret s_PXENV_UDP_READ::s_port Source UDP port
  305. * @ret s_PXENV_UDP_READ::d_port Destination UDP port
  306. * @ret s_PXENV_UDP_READ::buffer_size Length of UDP payload
  307. * @err #PXENV_STATUS_UDP_CLOSED UDP connection is not open
  308. * @err #PXENV_STATUS_FAILURE No packet was ready to read
  309. *
  310. * Receive a single UDP packet. This is a non-blocking call; if no
  311. * packet is ready to read, the call will return instantly with
  312. * s_PXENV_UDP_READ::Status==PXENV_STATUS_FAILURE.
  313. *
  314. * If s_PXENV_UDP_READ::dest_ip is 0.0.0.0, UDP packets addressed to
  315. * any IP address will be accepted and may be returned to the caller.
  316. *
  317. * If s_PXENV_UDP_READ::d_port is 0, UDP packets addressed to any UDP
  318. * port will be accepted and may be returned to the caller.
  319. *
  320. * You must have opened a UDP connection with pxenv_udp_open() before
  321. * calling pxenv_udp_read().
  322. *
  323. * On x86, you must set the s_PXE::StatusCallout field to a nonzero
  324. * value before calling this function in protected mode. You cannot
  325. * call this function with a 32-bit stack segment. (See the relevant
  326. * @ref pxe_x86_pmode16 "implementation note" for more details.)
  327. *
  328. * @note The PXE specification (version 2.1) does not state that we
  329. * should fill in s_PXENV_UDP_READ::dest_ip and
  330. * s_PXENV_UDP_READ::d_port, but Microsoft Windows' NTLDR program
  331. * expects us to do so, and will fail if we don't.
  332. *
  333. */
  334. PXENV_EXIT_t pxenv_udp_read ( struct s_PXENV_UDP_READ *pxenv_udp_read ) {
  335. struct in_addr dest_ip = { .s_addr = pxenv_udp_read->dest_ip };
  336. uint16_t d_port = pxenv_udp_read->d_port;
  337. DBG ( "PXENV_UDP_READ" );
  338. /* Try receiving a packet */
  339. pxe_udp.pxenv_udp_read = pxenv_udp_read;
  340. step();
  341. if ( pxe_udp.pxenv_udp_read ) {
  342. /* No packet received */
  343. pxe_udp.pxenv_udp_read = NULL;
  344. goto no_packet;
  345. }
  346. /* Filter on destination address and/or port */
  347. if ( dest_ip.s_addr && ( dest_ip.s_addr != pxenv_udp_read->dest_ip ) )
  348. goto no_packet;
  349. if ( d_port && ( d_port != pxenv_udp_read->d_port ) )
  350. goto no_packet;
  351. DBG ( " %04x:%04x+%x %s:", pxenv_udp_read->buffer.segment,
  352. pxenv_udp_read->buffer.offset, pxenv_udp_read->buffer_size,
  353. inet_ntoa ( *( ( struct in_addr * ) &pxenv_udp_read->src_ip ) ));
  354. DBG ( "%d<-%s:%d", ntohs ( pxenv_udp_read->s_port ),
  355. inet_ntoa ( *( ( struct in_addr * ) &pxenv_udp_read->dest_ip ) ),
  356. ntohs ( pxenv_udp_read->d_port ) );
  357. pxenv_udp_read->Status = PXENV_STATUS_SUCCESS;
  358. return PXENV_EXIT_SUCCESS;
  359. no_packet:
  360. pxenv_udp_read->Status = PXENV_STATUS_FAILURE;
  361. return PXENV_EXIT_FAILURE;
  362. }