udp.c 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. DBGC ( conn, "UDP %p opened on port %d\n", conn,
  64. ntohs ( local_port ) );
  65. return 0;
  66. }
  67. /**
  68. * Close a UDP connection
  69. *
  70. * @v conn UDP connection
  71. */
  72. void udp_close ( struct udp_connection *conn ) {
  73. list_del ( &conn->list );
  74. DBGC ( conn, "UDP %p closed\n", conn );
  75. }
  76. /**
  77. * Allocate packet buffer for UDP
  78. *
  79. * @v conn UDP connection
  80. * @ret pkb Packet buffer, or NULL
  81. */
  82. static struct pk_buff * udp_alloc_pkb ( struct udp_connection *conn ) {
  83. struct pk_buff *pkb;
  84. pkb = alloc_pkb ( UDP_MAX_TXPKB );
  85. if ( ! pkb ) {
  86. DBGC ( conn, "UDP %p cannot allocate buffer of length %d\n",
  87. conn, UDP_MAX_TXPKB );
  88. return NULL;
  89. }
  90. pkb_reserve ( pkb, UDP_MAX_HLEN );
  91. return pkb;
  92. }
  93. /**
  94. * User request to send data via a UDP connection
  95. *
  96. * @v conn UDP connection
  97. *
  98. * This function allocates buffer space and invokes the function's
  99. * senddata() callback. The callback may use the buffer space as
  100. * temporary storage space.
  101. */
  102. int udp_senddata ( struct udp_connection *conn ) {
  103. int rc;
  104. conn->tx_pkb = udp_alloc_pkb ( conn );
  105. if ( ! conn->tx_pkb )
  106. return -ENOMEM;
  107. rc = conn->udp_op->senddata ( conn, conn->tx_pkb->data,
  108. pkb_tailroom ( conn->tx_pkb ) );
  109. if ( conn->tx_pkb ) {
  110. free_pkb ( conn->tx_pkb );
  111. conn->tx_pkb = NULL;
  112. }
  113. return rc;
  114. }
  115. /**
  116. * Transmit data via a UDP connection to a specified address
  117. *
  118. * @v conn UDP connection
  119. * @v peer Destination address
  120. * @v netdev Net device via which to send (or NULL)
  121. * @v data Data to send
  122. * @v len Length of data
  123. * @ret rc Return status code
  124. */
  125. int udp_sendto_via ( struct udp_connection *conn, struct sockaddr_tcpip *peer,
  126. struct net_device *netdev, const void *data,
  127. size_t len ) {
  128. struct udp_header *udphdr;
  129. struct pk_buff *pkb;
  130. /* Use precreated packet buffer if one is available */
  131. if ( conn->tx_pkb ) {
  132. pkb = conn->tx_pkb;
  133. conn->tx_pkb = NULL;
  134. } else {
  135. pkb = udp_alloc_pkb ( conn );
  136. if ( ! pkb )
  137. return -ENOMEM;
  138. }
  139. /* Avoid overflowing TX buffer */
  140. if ( len > pkb_tailroom ( pkb ) )
  141. len = pkb_tailroom ( pkb );
  142. /* Copy payload */
  143. memmove ( pkb_put ( pkb, len ), data, len );
  144. /*
  145. * Add the UDP header
  146. *
  147. * Covert all 16- and 32- bit integers into network btye order before
  148. * sending it over the network
  149. */
  150. udphdr = pkb_push ( pkb, sizeof ( *udphdr ) );
  151. udphdr->dest_port = peer->st_port;
  152. udphdr->source_port = conn->local_port;
  153. udphdr->len = htons ( pkb_len ( pkb ) );
  154. udphdr->chksum = 0;
  155. udphdr->chksum = tcpip_chksum ( udphdr, sizeof ( *udphdr ) + len );
  156. /* Dump debugging information */
  157. DBGC ( conn, "UDP %p TX %d->%d len %zd\n", conn,
  158. ntohs ( udphdr->source_port ), ntohs ( udphdr->dest_port ),
  159. ntohs ( udphdr->len ) );
  160. /* Send it to the next layer for processing */
  161. return tcpip_tx ( pkb, &udp_protocol, peer, netdev, &udphdr->chksum );
  162. }
  163. /**
  164. * Transmit data via a UDP connection to a specified address
  165. *
  166. * @v conn UDP connection
  167. * @v peer Destination address
  168. * @v data Data to send
  169. * @v len Length of data
  170. * @ret rc Return status code
  171. */
  172. int udp_sendto ( struct udp_connection *conn, struct sockaddr_tcpip *peer,
  173. const void *data, size_t len ) {
  174. return udp_sendto_via ( conn, peer, NULL, data, len );
  175. }
  176. /**
  177. * Transmit data via a UDP connection
  178. *
  179. * @v conn UDP connection
  180. * @v data Data to send
  181. * @v len Length of data
  182. * @ret rc Return status code
  183. */
  184. int udp_send ( struct udp_connection *conn, const void *data, size_t len ) {
  185. return udp_sendto ( conn, &conn->peer, data, len );
  186. }
  187. /**
  188. * Identify UDP connection by local port number
  189. *
  190. * @v local_port Local port (in network-endian order)
  191. * @ret conn TCP connection, or NULL
  192. */
  193. static struct udp_connection * udp_demux ( uint16_t local_port ) {
  194. struct udp_connection *conn;
  195. list_for_each_entry ( conn, &udp_conns, list ) {
  196. if ( ( conn->local_port == local_port ) ||
  197. ( conn->local_port == 0 ) ) {
  198. return conn;
  199. }
  200. }
  201. return NULL;
  202. }
  203. /**
  204. * Process a received packet
  205. *
  206. * @v pkb Packet buffer
  207. * @v st_src Partially-filled source address
  208. * @v st_dest Partially-filled destination address
  209. * @v pshdr_csum Pseudo-header checksum
  210. * @ret rc Return status code
  211. */
  212. static int udp_rx ( struct pk_buff *pkb, struct sockaddr_tcpip *st_src,
  213. struct sockaddr_tcpip *st_dest, uint16_t pshdr_csum ) {
  214. struct udp_header *udphdr = pkb->data;
  215. struct udp_connection *conn;
  216. size_t ulen;
  217. uint16_t csum;
  218. int rc;
  219. /* Sanity check packet */
  220. if ( pkb_len ( pkb ) < sizeof ( *udphdr ) ) {
  221. DBG ( "UDP packet too short at %d bytes (min %d bytes)\n",
  222. pkb_len ( pkb ), sizeof ( *udphdr ) );
  223. rc = -EINVAL;
  224. goto done;
  225. }
  226. ulen = ntohs ( udphdr->len );
  227. if ( ulen < sizeof ( *udphdr ) ) {
  228. DBG ( "UDP length too short at %d bytes "
  229. "(header is %d bytes)\n", ulen, sizeof ( *udphdr ) );
  230. rc = -EINVAL;
  231. goto done;
  232. }
  233. if ( ulen > pkb_len ( pkb ) ) {
  234. DBG ( "UDP length too long at %d bytes (packet is %d bytes)\n",
  235. ulen, pkb_len ( pkb ) );
  236. rc = -EINVAL;
  237. goto done;
  238. }
  239. if ( udphdr->chksum ) {
  240. csum = tcpip_continue_chksum ( pshdr_csum, pkb->data, ulen );
  241. if ( csum != 0 ) {
  242. DBG ( "UDP checksum incorrect (is %04x including "
  243. "checksum field, should be 0000)\n", csum );
  244. rc = -EINVAL;
  245. goto done;
  246. }
  247. }
  248. /* Parse parameters from header and strip header */
  249. st_src->st_port = udphdr->source_port;
  250. st_dest->st_port = udphdr->dest_port;
  251. conn = udp_demux ( udphdr->dest_port );
  252. pkb_unput ( pkb, ( pkb_len ( pkb ) - ulen ) );
  253. pkb_pull ( pkb, sizeof ( *udphdr ) );
  254. /* Dump debugging information */
  255. DBGC ( conn, "UDP %p RX %d<-%d len %zd\n", conn,
  256. ntohs ( udphdr->dest_port ), ntohs ( udphdr->source_port ),
  257. ulen );
  258. /* Ignore if no matching connection found */
  259. if ( ! conn ) {
  260. DBG ( "No UDP connection listening on port %d\n",
  261. ntohs ( udphdr->dest_port ) );
  262. rc = -ENOTCONN;
  263. goto done;
  264. }
  265. /* Pass data to application */
  266. rc = conn->udp_op->newdata ( conn, pkb->data, pkb_len ( pkb ),
  267. st_src, st_dest );
  268. done:
  269. free_pkb ( pkb );
  270. return rc;
  271. }
  272. struct tcpip_protocol udp_protocol __tcpip_protocol = {
  273. .name = "UDP",
  274. .rx = udp_rx,
  275. .tcpip_proto = IP_UDP,
  276. };