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

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