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

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