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.

udp.c 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #include <stdint.h>
  2. #include <string.h>
  3. #include <assert.h>
  4. #include <byteswap.h>
  5. #include <errno.h>
  6. #include <gpxe/tcpip.h>
  7. #include <gpxe/pkbuff.h>
  8. #include <gpxe/netdevice.h>
  9. #include <gpxe/udp.h>
  10. /** @file
  11. *
  12. * UDP protocol
  13. */
  14. struct tcpip_protocol udp_protocol;
  15. /**
  16. * List of registered UDP connections
  17. */
  18. static LIST_HEAD ( udp_conns );
  19. /**
  20. * Bind UDP connection to local port
  21. *
  22. * @v conn UDP connection
  23. * @v local_port Local port, in network byte order
  24. * @ret rc Return status code
  25. */
  26. int udp_bind ( struct udp_connection *conn, uint16_t local_port ) {
  27. struct udp_connection *existing;
  28. list_for_each_entry ( existing, &udp_conns, list ) {
  29. if ( existing->local_port == local_port )
  30. return -EADDRINUSE;
  31. }
  32. conn->local_port = local_port;
  33. return 0;
  34. }
  35. /**
  36. * Open a local port
  37. *
  38. * @v conn UDP connection
  39. * @v local_port Local port, in network byte order, or zero
  40. * @ret rc Return status code
  41. *
  42. * Opens the UDP connection and binds to a local port. If no local
  43. * port is specified, the first available port will be used.
  44. */
  45. int udp_open ( struct udp_connection *conn, uint16_t local_port ) {
  46. static uint16_t try_port = 1024;
  47. int rc;
  48. /* If no port specified, find the first available port */
  49. if ( ! local_port ) {
  50. for ( ; try_port ; try_port++ ) {
  51. if ( try_port < 1024 )
  52. continue;
  53. if ( udp_open ( conn, htons ( try_port ) ) == 0 )
  54. return 0;
  55. }
  56. return -EADDRINUSE;
  57. }
  58. /* Attempt bind to local port */
  59. if ( ( rc = udp_bind ( conn, local_port ) ) != 0 )
  60. return rc;
  61. /* Add to UDP connection list */
  62. list_add ( &conn->list, &udp_conns );
  63. DBG ( "UDP opened %p on port %d\n", conn, ntohs ( local_port ) );
  64. return 0;
  65. }
  66. /**
  67. * Close a UDP connection
  68. *
  69. * @v conn UDP connection
  70. */
  71. void udp_close ( struct udp_connection *conn ) {
  72. list_del ( &conn->list );
  73. DBG ( "UDP closed %p\n", conn );
  74. }
  75. /**
  76. * User request to send data via a UDP connection
  77. *
  78. * @v conn UDP connection
  79. *
  80. * This function allocates buffer space and invokes the function's senddata()
  81. * callback. The callback may use the buffer space
  82. */
  83. int udp_senddata ( struct udp_connection *conn ) {
  84. int rc;
  85. conn->tx_pkb = alloc_pkb ( UDP_MAX_TXPKB );
  86. if ( conn->tx_pkb == NULL ) {
  87. DBG ( "UDP %p cannot allocate packet buffer of length %d\n",
  88. conn, UDP_MAX_TXPKB );
  89. return -ENOMEM;
  90. }
  91. pkb_reserve ( conn->tx_pkb, UDP_MAX_HLEN );
  92. rc = conn->udp_op->senddata ( conn, conn->tx_pkb->data,
  93. pkb_available ( conn->tx_pkb ) );
  94. if ( conn->tx_pkb )
  95. free_pkb ( conn->tx_pkb );
  96. return rc;
  97. }
  98. /**
  99. * Transmit data via a UDP connection to a specified address
  100. *
  101. * @v conn UDP connection
  102. * @v peer Destination address
  103. * @v data Data to send
  104. * @v len Length of data
  105. * @ret rc Return status code
  106. *
  107. * This function fills up the UDP headers and sends the data. It may
  108. * be called only from within the context of an application's
  109. * senddata() method; if the application wishes to send data it must
  110. * call udp_senddata() and wait for its senddata() method to be
  111. * called.
  112. */
  113. int udp_sendto ( struct udp_connection *conn, struct sockaddr_tcpip *peer,
  114. const void *data, size_t len ) {
  115. struct udp_header *udphdr;
  116. struct pk_buff *pkb;
  117. /* Take ownership of packet buffer back from the
  118. * udp_connection structure.
  119. */
  120. pkb = conn->tx_pkb;
  121. conn->tx_pkb = NULL;
  122. /* Avoid overflowing TX buffer */
  123. if ( len > pkb_available ( pkb ) )
  124. len = pkb_available ( pkb );
  125. /* Copy payload */
  126. memmove ( pkb_put ( pkb, len ), data, len );
  127. /*
  128. * Add the UDP header
  129. *
  130. * Covert all 16- and 32- bit integers into network btye order before
  131. * sending it over the network
  132. */
  133. udphdr = pkb_push ( pkb, sizeof ( *udphdr ) );
  134. udphdr->dest_port = peer->st_port;
  135. udphdr->source_port = conn->local_port;
  136. udphdr->len = htons ( pkb_len ( pkb ) );
  137. udphdr->chksum = 0;
  138. udphdr->chksum = tcpip_chksum ( udphdr, sizeof ( *udphdr ) + len );
  139. /* Dump debugging information */
  140. DBG ( "UDP %p transmitting %p+%#zx len %#x src %d dest %d "
  141. "chksum %#04x\n", conn, pkb->data,
  142. pkb_len ( pkb ), ntohs ( udphdr->len ),
  143. ntohs ( udphdr->source_port ), ntohs ( udphdr->dest_port ),
  144. ntohs ( udphdr->chksum ) );
  145. /* Send it to the next layer for processing */
  146. return tcpip_tx ( pkb, &udp_protocol, peer );
  147. }
  148. /**
  149. * Transmit data via a UDP connection
  150. *
  151. * @v conn UDP connection
  152. * @v data Data to send
  153. * @v len Length of data
  154. * @ret rc Return status code
  155. *
  156. * This function fills up the UDP headers and sends the data. It may
  157. * be called only from within the context of an application's
  158. * senddata() method; if the application wishes to send data it must
  159. * call udp_senddata() and wait for its senddata() method to be
  160. * called.
  161. */
  162. int udp_send ( struct udp_connection *conn, const void *data, size_t len ) {
  163. return udp_sendto ( conn, &conn->peer, data, len );
  164. }
  165. /**
  166. * Process a received packet
  167. *
  168. * @v pkb Packet buffer
  169. * @v st_src Partially-filled source address
  170. * @v st_dest Partially-filled destination address
  171. * @ret rc Return status code
  172. */
  173. static int udp_rx ( struct pk_buff *pkb, struct sockaddr_tcpip *st_src,
  174. struct sockaddr_tcpip *st_dest ) {
  175. struct udp_header *udphdr = pkb->data;
  176. struct udp_connection *conn;
  177. unsigned int ulen;
  178. uint16_t chksum;
  179. int rc;
  180. /* Sanity check */
  181. if ( pkb_len ( pkb ) < sizeof ( *udphdr ) ) {
  182. DBG ( "UDP received underlength packet %p+%#zx\n",
  183. pkb->data, pkb_len ( pkb ) );
  184. rc = -EINVAL;
  185. goto done;
  186. }
  187. /* Dump debugging information */
  188. DBG ( "UDP received %p+%#zx len %#x src %d dest %d chksum %#04x\n",
  189. pkb->data, pkb_len ( pkb ), ntohs ( udphdr->len ),
  190. ntohs ( udphdr->source_port ), ntohs ( udphdr->dest_port ),
  191. ntohs ( udphdr->chksum ) );
  192. /* Check length and trim any excess */
  193. ulen = ntohs ( udphdr->len );
  194. if ( ulen > pkb_len ( pkb ) ) {
  195. DBG ( "UDP received truncated packet %p+%#zx\n",
  196. pkb->data, pkb_len ( pkb ) );
  197. rc = -EINVAL;
  198. goto done;
  199. }
  200. pkb_unput ( pkb, ( pkb_len ( pkb ) - ulen ) );
  201. /* Verify the checksum */
  202. #warning "Don't we need to take the pseudo-header into account here?"
  203. #if 0
  204. chksum = tcpip_chksum ( pkb->data, pkb_len ( pkb ) );
  205. if ( chksum != 0xffff ) {
  206. DBG ( "Bad checksum %#x\n", chksum );
  207. rc = -EINVAL;
  208. goto done;
  209. }
  210. #endif
  211. /* Complete the socket addresses */
  212. st_src->st_port = udphdr->source_port;
  213. st_dest->st_port = udphdr->dest_port;
  214. /* Demux the connection */
  215. list_for_each_entry ( conn, &udp_conns, list ) {
  216. if ( conn->local_port &&
  217. ( conn->local_port != udphdr->dest_port ) ) {
  218. /* Bound to local port and local port doesn't match */
  219. continue;
  220. }
  221. /* Strip off the UDP header */
  222. pkb_pull ( pkb, sizeof ( *udphdr ) );
  223. DBG ( "UDP delivering to %p\n", conn );
  224. /* Call the application's callback */
  225. rc = conn->udp_op->newdata ( conn, pkb->data, pkb_len( pkb ),
  226. st_src, st_dest );
  227. goto done;
  228. }
  229. DBG ( "No UDP connection listening on port %d\n",
  230. ntohs ( udphdr->dest_port ) );
  231. rc = 0;
  232. done:
  233. free_pkb ( pkb );
  234. return rc;
  235. }
  236. struct tcpip_protocol udp_protocol __tcpip_protocol = {
  237. .name = "UDP",
  238. .rx = udp_rx,
  239. .tcpip_proto = IP_UDP,
  240. .csum_offset = 6,
  241. };