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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /** @file
  2. *
  3. * PXE UDP API
  4. *
  5. */
  6. #include <string.h>
  7. #include <byteswap.h>
  8. #include <gpxe/udp.h>
  9. #include <gpxe/uaccess.h>
  10. #include <gpxe/process.h>
  11. #include <pxe.h>
  12. /*
  13. * Copyright (C) 2004 Michael Brown <mbrown@fensystems.co.uk>.
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License as
  17. * published by the Free Software Foundation; either version 2 of the
  18. * License, or any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful, but
  21. * WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  23. * General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  28. */
  29. /** A PXE UDP connection */
  30. struct pxe_udp_connection {
  31. /** Etherboot UDP connection */
  32. struct udp_connection udp;
  33. /** "Connection is open" flag */
  34. int open;
  35. /** Current pxenv_udp_read() operation, if any */
  36. struct s_PXENV_UDP_READ *pxenv_udp_read;
  37. /** Current pxenv_udp_write() operation, if any */
  38. struct s_PXENV_UDP_WRITE *pxenv_udp_write;
  39. };
  40. static inline struct pxe_udp_connection *
  41. udp_to_pxe ( struct udp_connection *conn ) {
  42. return container_of ( conn, struct pxe_udp_connection, udp );
  43. }
  44. /**
  45. * Send PXE UDP data
  46. *
  47. * @v conn UDP connection
  48. * @v data Temporary data buffer
  49. * @v len Size of temporary data buffer
  50. * @ret rc Return status code
  51. *
  52. * Sends the packet belonging to the current pxenv_udp_write()
  53. * operation.
  54. */
  55. static int pxe_udp_senddata ( struct udp_connection *conn, void *data,
  56. size_t len ) {
  57. struct pxe_udp_connection *pxe_udp = udp_to_pxe ( conn );
  58. struct s_PXENV_UDP_WRITE *pxenv_udp_write = pxe_udp->pxenv_udp_write;
  59. userptr_t buffer;
  60. /* Transmit packet */
  61. buffer = real_to_user ( pxenv_udp_write->buffer.segment,
  62. pxenv_udp_write->buffer.offset );
  63. if ( len > pxenv_udp_write->buffer_size )
  64. len = pxenv_udp_write->buffer_size;
  65. copy_from_user ( data, buffer, 0, len );
  66. return udp_send ( conn, data, len );
  67. }
  68. /**
  69. * Receive PXE UDP data
  70. *
  71. * @v conn UDP connection
  72. * @v data Received data
  73. * @v len Length of received data
  74. * @v st_src Source address
  75. * @v st_dest Destination address
  76. *
  77. * Receives a packet as part of the current pxenv_udp_read()
  78. * operation.
  79. */
  80. static int pxe_udp_newdata ( struct udp_connection *conn, void *data,
  81. size_t len, struct sockaddr_tcpip *st_src,
  82. struct sockaddr_tcpip *st_dest ) {
  83. struct pxe_udp_connection *pxe_udp = udp_to_pxe ( conn );
  84. struct s_PXENV_UDP_READ *pxenv_udp_read = pxe_udp->pxenv_udp_read;
  85. struct sockaddr_in *sin_src = ( ( struct sockaddr_in * ) st_src );
  86. struct sockaddr_in *sin_dest = ( ( struct sockaddr_in * ) st_dest );
  87. userptr_t buffer;
  88. if ( ! pxenv_udp_read ) {
  89. DBG ( "PXE discarded UDP packet\n" );
  90. return -ENOBUFS;
  91. }
  92. /* Copy packet to buffer and record length */
  93. buffer = real_to_user ( pxenv_udp_read->buffer.segment,
  94. pxenv_udp_read->buffer.offset );
  95. if ( len > pxenv_udp_read->buffer_size )
  96. len = pxenv_udp_read->buffer_size;
  97. copy_to_user ( buffer, 0, data, len );
  98. pxenv_udp_read->buffer_size = len;
  99. /* Fill in source/dest information */
  100. assert ( sin_src->sin_family == AF_INET );
  101. pxenv_udp_read->src_ip = sin_src->sin_addr.s_addr;
  102. pxenv_udp_read->s_port = sin_src->sin_port;
  103. assert ( sin_dest->sin_family == AF_INET );
  104. pxenv_udp_read->dest_ip = sin_dest->sin_addr.s_addr;
  105. pxenv_udp_read->d_port = sin_dest->sin_port;
  106. /* Mark as received */
  107. pxe_udp->pxenv_udp_read = NULL;
  108. return 0;
  109. }
  110. /** PXE UDP operations */
  111. static struct udp_operations pxe_udp_operations = {
  112. .senddata = pxe_udp_senddata,
  113. .newdata = pxe_udp_newdata,
  114. };
  115. /** The PXE UDP connection */
  116. static struct pxe_udp_connection pxe_udp = {
  117. .udp.udp_op = &pxe_udp_operations,
  118. };
  119. /**
  120. * UDP OPEN
  121. *
  122. * @v pxenv_udp_open Pointer to a struct s_PXENV_UDP_OPEN
  123. * @v s_PXENV_UDP_OPEN::src_ip IP address of this station, or 0.0.0.0
  124. * @ret #PXENV_EXIT_SUCCESS Always
  125. * @ret s_PXENV_UDP_OPEN::Status PXE status code
  126. * @err #PXENV_STATUS_UDP_OPEN UDP connection already open
  127. * @err #PXENV_STATUS_OUT_OF_RESOURCES Could not open connection
  128. *
  129. * Prepares the PXE stack for communication using pxenv_udp_write()
  130. * and pxenv_udp_read().
  131. *
  132. * The IP address supplied in s_PXENV_UDP_OPEN::src_ip will be
  133. * recorded and used as the local station's IP address for all further
  134. * communication, including communication by means other than
  135. * pxenv_udp_write() and pxenv_udp_read(). (If
  136. * s_PXENV_UDP_OPEN::src_ip is 0.0.0.0, the local station's IP address
  137. * will remain unchanged.)
  138. *
  139. * You can only have one open UDP connection at a time. This is not a
  140. * meaningful restriction, since pxenv_udp_write() and
  141. * pxenv_udp_read() allow you to specify arbitrary local and remote
  142. * ports and an arbitrary remote address for each packet. According
  143. * to the PXE specifiation, you cannot have a UDP connection open at
  144. * the same time as a TFTP connection; this restriction does not apply
  145. * to Etherboot.
  146. *
  147. * On x86, you must set the s_PXE::StatusCallout field to a nonzero
  148. * value before calling this function in protected mode. You cannot
  149. * call this function with a 32-bit stack segment. (See the relevant
  150. * @ref pxe_x86_pmode16 "implementation note" for more details.)
  151. *
  152. * @note The PXE specification does not make it clear whether the IP
  153. * address supplied in s_PXENV_UDP_OPEN::src_ip should be used only
  154. * for this UDP connection, or retained for all future communication.
  155. * The latter seems more consistent with typical PXE stack behaviour.
  156. *
  157. * @note Etherboot currently ignores the s_PXENV_UDP_OPEN::src_ip
  158. * parameter.
  159. *
  160. */
  161. PXENV_EXIT_t pxenv_udp_open ( struct s_PXENV_UDP_OPEN *pxenv_udp_open ) {
  162. struct in_addr new_ip = { .s_addr = pxenv_udp_open->src_ip };
  163. DBG ( "PXENV_UDP_OPEN" );
  164. /* Check connection is not already open */
  165. if ( pxe_udp.open ) {
  166. pxenv_udp_open->Status = PXENV_STATUS_UDP_OPEN;
  167. return PXENV_EXIT_FAILURE;
  168. }
  169. /* Set IP address if specified */
  170. if ( new_ip.s_addr ) {
  171. /* FIXME: actually do something here */
  172. DBG ( " with new IP address %s", inet_ntoa ( new_ip ) );
  173. }
  174. /* Open UDP connection */
  175. if ( udp_open ( &pxe_udp.udp, 0 ) != 0 ) {
  176. pxenv_udp_open->Status = PXENV_STATUS_OUT_OF_RESOURCES;
  177. return PXENV_EXIT_FAILURE;
  178. }
  179. pxe_udp.open = 1;
  180. pxenv_udp_open->Status = PXENV_STATUS_SUCCESS;
  181. return PXENV_EXIT_SUCCESS;
  182. }
  183. /**
  184. * UDP CLOSE
  185. *
  186. * @v pxenv_udp_close Pointer to a struct s_PXENV_UDP_CLOSE
  187. * @ret #PXENV_EXIT_SUCCESS Always
  188. * @ret s_PXENV_UDP_CLOSE::Status PXE status code
  189. * @err None -
  190. *
  191. * Closes a UDP connection opened with pxenv_udp_open().
  192. *
  193. * You can only have one open UDP connection at a time. You cannot
  194. * have a UDP connection open at the same time as a TFTP connection.
  195. * You cannot use pxenv_udp_close() to close a TFTP connection; use
  196. * pxenv_tftp_close() instead.
  197. *
  198. * On x86, you must set the s_PXE::StatusCallout field to a nonzero
  199. * value before calling this function in protected mode. You cannot
  200. * call this function with a 32-bit stack segment. (See the relevant
  201. * @ref pxe_x86_pmode16 "implementation note" for more details.)
  202. *
  203. */
  204. PXENV_EXIT_t pxenv_udp_close ( struct s_PXENV_UDP_CLOSE *pxenv_udp_close ) {
  205. DBG ( "PXENV_UDP_CLOSE" );
  206. /* Check connection is open */
  207. if ( ! pxe_udp.open ) {
  208. pxenv_udp_close->Status = PXENV_STATUS_UDP_CLOSED;
  209. return PXENV_EXIT_SUCCESS; /* Well, it *is* closed */
  210. }
  211. /* Close UDP connection */
  212. udp_close ( &pxe_udp.udp );
  213. pxe_udp.open = 0;
  214. pxenv_udp_close->Status = PXENV_STATUS_SUCCESS;
  215. return PXENV_EXIT_SUCCESS;
  216. }
  217. /**
  218. * UDP WRITE
  219. *
  220. * @v pxenv_udp_write Pointer to a struct s_PXENV_UDP_WRITE
  221. * @v s_PXENV_UDP_WRITE::ip Destination IP address
  222. * @v s_PXENV_UDP_WRITE::gw Relay agent IP address, or 0.0.0.0
  223. * @v s_PXENV_UDP_WRITE::src_port Source UDP port, or 0
  224. * @v s_PXENV_UDP_WRITE::dst_port Destination UDP port
  225. * @v s_PXENV_UDP_WRITE::buffer_size Length of the UDP payload
  226. * @v s_PXENV_UDP_WRITE::buffer Address of the UDP payload
  227. * @ret #PXENV_EXIT_SUCCESS Packet was transmitted successfully
  228. * @ret #PXENV_EXIT_FAILURE Packet could not be transmitted
  229. * @ret s_PXENV_UDP_WRITE::Status PXE status code
  230. * @err #PXENV_STATUS_UDP_CLOSED UDP connection is not open
  231. * @err #PXENV_STATUS_UNDI_TRANSMIT_ERROR Could not transmit packet
  232. *
  233. * Transmits a single UDP packet. A valid IP and UDP header will be
  234. * prepended to the payload in s_PXENV_UDP_WRITE::buffer; the buffer
  235. * should not contain precomputed IP and UDP headers, nor should it
  236. * contain space allocated for these headers. The first byte of the
  237. * buffer will be transmitted as the first byte following the UDP
  238. * header.
  239. *
  240. * If s_PXENV_UDP_WRITE::gw is 0.0.0.0, normal IP routing will take
  241. * place. See the relevant @ref pxe_routing "implementation note" for
  242. * more details.
  243. *
  244. * If s_PXENV_UDP_WRITE::src_port is 0, port 2069 will be used.
  245. *
  246. * You must have opened a UDP connection with pxenv_udp_open() before
  247. * calling pxenv_udp_write().
  248. *
  249. * On x86, you must set the s_PXE::StatusCallout field to a nonzero
  250. * value before calling this function in protected mode. You cannot
  251. * call this function with a 32-bit stack segment. (See the relevant
  252. * @ref pxe_x86_pmode16 "implementation note" for more details.)
  253. *
  254. * @note Etherboot currently ignores the s_PXENV_UDP_WRITE::gw
  255. * parameter.
  256. *
  257. */
  258. PXENV_EXIT_t pxenv_udp_write ( struct s_PXENV_UDP_WRITE *pxenv_udp_write ) {
  259. union {
  260. struct sockaddr_in sin;
  261. struct sockaddr_tcpip st;
  262. } dest;
  263. int rc;
  264. DBG ( "PXENV_UDP_WRITE" );
  265. /* Check connection is open */
  266. if ( ! pxe_udp.open ) {
  267. pxenv_udp_write->Status = PXENV_STATUS_UDP_CLOSED;
  268. return PXENV_EXIT_FAILURE;
  269. }
  270. /* Construct destination socket address */
  271. memset ( &dest, 0, sizeof ( dest ) );
  272. dest.sin.sin_family = AF_INET;
  273. dest.sin.sin_addr.s_addr = pxenv_udp_write->ip;
  274. dest.sin.sin_port = pxenv_udp_write->dst_port;
  275. udp_connect ( &pxe_udp.udp, &dest.st );
  276. /* Set local (source) port. PXE spec says source port is 2069
  277. * if not specified. Really, this ought to be set at UDP open
  278. * time but hey, we didn't design this API.
  279. */
  280. if ( ! pxenv_udp_write->src_port )
  281. pxenv_udp_write->src_port = htons ( 2069 );
  282. udp_bind ( &pxe_udp.udp, pxenv_udp_write->src_port );
  283. /* FIXME: we ignore the gateway specified, since we're
  284. * confident of being able to do our own routing. We should
  285. * probably allow for multiple gateways.
  286. */
  287. DBG ( " %04x:%04x+%x %d->%s:%d", pxenv_udp_write->buffer.segment,
  288. pxenv_udp_write->buffer.offset, pxenv_udp_write->buffer_size,
  289. ntohs ( pxenv_udp_write->src_port ),
  290. inet_ntoa ( dest.sin.sin_addr ),
  291. ntohs ( pxenv_udp_write->dst_port ) );
  292. /* Transmit packet */
  293. pxe_udp.pxenv_udp_write = pxenv_udp_write;
  294. rc = udp_senddata ( &pxe_udp.udp );
  295. pxe_udp.pxenv_udp_write = NULL;
  296. if ( rc != 0 ) {
  297. pxenv_udp_write->Status = PXENV_STATUS_UNDI_TRANSMIT_ERROR;
  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. PXENV_EXIT_t pxenv_udp_read ( struct s_PXENV_UDP_READ *pxenv_udp_read ) {
  347. struct in_addr dest_ip = { .s_addr = pxenv_udp_read->dest_ip };
  348. uint16_t d_port = pxenv_udp_read->d_port;
  349. DBG ( "PXENV_UDP_READ" );
  350. /* Check connection is open */
  351. if ( ! pxe_udp.open ) {
  352. pxenv_udp_read->Status = PXENV_STATUS_UDP_CLOSED;
  353. return PXENV_EXIT_FAILURE;
  354. }
  355. /* Bind promiscuously; we will do our own filtering */
  356. udp_bind_promisc ( &pxe_udp.udp );
  357. /* Try receiving a packet */
  358. pxe_udp.pxenv_udp_read = pxenv_udp_read;
  359. step();
  360. if ( pxe_udp.pxenv_udp_read ) {
  361. /* No packet received */
  362. pxe_udp.pxenv_udp_read = NULL;
  363. goto no_packet;
  364. }
  365. /* Filter on destination address and/or port */
  366. if ( dest_ip.s_addr && ( dest_ip.s_addr != pxenv_udp_read->dest_ip ) )
  367. goto no_packet;
  368. if ( d_port && ( d_port != pxenv_udp_read->d_port ) )
  369. goto no_packet;
  370. pxenv_udp_read->Status = PXENV_STATUS_SUCCESS;
  371. return PXENV_EXIT_SUCCESS;
  372. no_packet:
  373. pxenv_udp_read->Status = PXENV_STATUS_FAILURE;
  374. return PXENV_EXIT_FAILURE;
  375. }