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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #include <stdint.h>
  2. #include <string.h>
  3. #include <assert.h>
  4. #include <byteswap.h>
  5. #include <latch.h>
  6. #include <errno.h>
  7. #include <gpxe/in.h>
  8. #include <gpxe/ip.h>
  9. #include <gpxe/udp.h>
  10. #include <gpxe/init.h>
  11. #include <gpxe/pkbuff.h>
  12. #include <gpxe/netdevice.h>
  13. #include <gpxe/interface.h>
  14. /** @file
  15. *
  16. * UDP protocol
  17. */
  18. static inline void copy_sockaddr ( struct sockaddr *source, struct sockaddr *dest ) {
  19. memcpy ( dest, source, sizeof ( *dest ) );
  20. }
  21. static inline uint16_t dest_port ( struct sockaddr *sock, uint16_t *dest ) {
  22. switch ( sock->sa_family ) {
  23. case AF_INET:
  24. dest = &sock->sin.sin_port;
  25. break;
  26. case AF_INET6:
  27. dest = &sock->sin6.sin6_port;
  28. break;
  29. default:
  30. DBG ( "Network family %d not supported\n", sock->sa_family );
  31. return -EAFNOSUPPORT;
  32. }
  33. return 0;
  34. }
  35. /**
  36. * Dump the UDP header
  37. *
  38. * @v udphdr UDP header
  39. */
  40. void udp_dump ( struct udp_header *udphdr ) {
  41. /* Print UDP header for debugging */
  42. DBG ( "UDP header at %#x + %d\n", udphdr, sizeof ( *udphdr ) );
  43. DBG ( "\tSource Port = %d\n", ntohs ( udphdr->source_port ) );
  44. DBG ( "\tDestination Port = %d\n", ntohs ( udphdr->dest_port ) );
  45. DBG ( "\tLength = %d\n", ntohs ( udphdr->len ) );
  46. DBG ( "\tChecksum = %d\n", ntohs ( udphdr->chksum ) );
  47. DBG ( "\tChecksum located at %#x\n", &udphdr->chksum );
  48. }
  49. /**
  50. * Open a UDP connection
  51. *
  52. * @v conn UDP connection
  53. * @v peer Destination socket address
  54. *
  55. * This function stores the socket address within the connection
  56. */
  57. void udp_connect ( struct udp_connection *conn, struct sockaddr *peer ) {
  58. copy_sockaddr ( peer, &conn->sin );
  59. /* Not sure if this should add the connection to udp_conns; If it does,
  60. * uncomment the following code
  61. */
  62. // list_add ( &conn->list, &udp_conns );
  63. }
  64. /**
  65. * Initialize a UDP connection
  66. *
  67. * @v conn UDP connection
  68. * @v udp_op UDP operations
  69. */
  70. void udp_init ( struct udp_connection *conn, struct udp_operations *udp_op ) {
  71. conn->local_port = 0;
  72. conn->tx_pkb = NULL;
  73. if ( udp_op != NULL ) {
  74. conn->udp_op = udp_op;
  75. }
  76. }
  77. /**
  78. * Allocate space to the UDP buffer
  79. *
  80. * @v conn UDP connection
  81. * @v len Length to allocate
  82. * @ret rc Status
  83. *
  84. * Allocate "len" amount of space in the transmit buffer
  85. */
  86. int udp_buf_alloc ( struct udp_connection *conn, size_t len ) {
  87. if ( conn->tx_pkb != NULL ) {
  88. free_pkb ( conn->tx_pkb );
  89. conn->tx_pkb = NULL;
  90. }
  91. conn->tx_pkb = alloc_pkb ( len < UDP_MIN_TXPKB ? UDP_MIN_TXPKB : len );
  92. return !conn ? -ENOMEM : 0;
  93. }
  94. /**
  95. * Send data via a UDP connection
  96. *
  97. * @v conn UDP connection
  98. * @v data Data to send
  99. * @v len Length of data
  100. *
  101. * This function fills up the UDP headers and sends the data. Discover the
  102. * network protocol through the sa_family field in the destination socket
  103. * address.
  104. */
  105. int udp_send ( struct udp_connection *conn, const void *data, size_t len ) {
  106. struct udp_header *udphdr; /* UDP header */
  107. struct sockaddr *sock = &conn->sin; /* Destination sockaddr */
  108. uint16_t *dest;
  109. int rc;
  110. /* Copy data into packet buffer, if necessary */
  111. if ( data != conn->tx_pkb->data ) {
  112. /* Allocate space for data and lower layer headers */
  113. if ( ( rc = udp_buf_alloc ( conn, len + UDP_MAX_HLEN ) ) != 0 ) {
  114. DBG ( "Error allocating buffer" );
  115. return rc;
  116. }
  117. /* Reserve space for the headers and copy contents */
  118. pkb_reserve ( conn->tx_pkb, UDP_MAX_HLEN );
  119. memcpy ( pkb_put ( conn->tx_pkb, len ), data, len );
  120. }
  121. /*
  122. * Add the UDP header
  123. *
  124. * Covert all 16- and 32- bit integers into network btye order before
  125. * sending it over the network
  126. */
  127. udphdr = pkb_push ( conn->tx_pkb, sizeof ( *udphdr ) );
  128. if ( (rc = dest_port ( sock, dest ) ) != 0 ) {
  129. return rc;
  130. }
  131. udphdr->dest_port = htons ( *dest );
  132. udphdr->source_port = htons ( conn->local_port );
  133. udphdr->len = htons ( pkb_len ( conn->tx_pkb ) );
  134. udphdr->chksum = htons ( calc_chksum ( udphdr, sizeof ( *udphdr ) ) );
  135. udp_dump ( udphdr );
  136. /* Send it to the next layer for processing */
  137. return trans_tx ( conn->tx_pkb, IP_UDP, sock );
  138. }
  139. /**
  140. * Send data to a specified address
  141. *
  142. * @v conn UDP connection
  143. * @v peer Destination address
  144. * @v data Data to send
  145. * @v len Length of data
  146. */
  147. int udp_sendto ( struct udp_connection *conn, struct sockaddr *peer,
  148. const void *data, size_t len ) {
  149. struct sockaddr tempsock;
  150. copy_sockaddr ( &conn->sin, &tempsock );
  151. copy_sockaddr ( peer, &conn->sin );
  152. int rc = udp_send ( conn, data, len );
  153. copy_sockaddr ( &tempsock, &conn->sin );
  154. return rc;
  155. }
  156. /**
  157. * Close a UDP connection
  158. *
  159. * @v conn UDP connection
  160. */
  161. void udp_close ( struct udp_connection *conn ) {
  162. list_del ( &conn->list );
  163. }
  164. /**
  165. * Open a local port
  166. *
  167. * @v conn UDP connection
  168. * @v local_port Local port on which to open connection
  169. *
  170. * This does not support the 0 port option correctly yet
  171. */
  172. int udp_open ( struct udp_connection *conn, uint16_t local_port ) {
  173. struct udp_connection *connr;
  174. uint16_t min_port = 0xffff;
  175. /* Iterate through udp_conns to see if local_port is available */
  176. list_for_each_entry ( connr, &udp_conns, list ) {
  177. if ( connr->local_port == local_port ) {
  178. return -EISCONN;
  179. }
  180. if ( min_port > connr->local_port ) {
  181. min_port = connr->local_port;
  182. }
  183. }
  184. /* This code is buggy. I will update it soon :) */
  185. conn->local_port = local_port == 0 ? min_port > 1024 ? 1024 :
  186. min_port + 1 : local_port;
  187. /* Add the connection to the list of listening connections */
  188. list_add ( &conn->list, &udp_conns );
  189. return 0;
  190. }
  191. /**
  192. * Process a received packet
  193. *
  194. * @v pkb Packet buffer
  195. * @v src_net_addr Source network address
  196. * @v dest_net_addr Destination network address
  197. */
  198. void udp_rx ( struct pk_buff *pkb, struct in_addr *src_net_addr __unused,
  199. struct in_addr *dest_net_addr __unused ) {
  200. struct udp_header *udphdr = pkb->data;
  201. struct udp_connection *conn;
  202. uint16_t ulen;
  203. uint16_t chksum;
  204. udp_dump ( udphdr );
  205. /* Validate the packet and the UDP length */
  206. if ( pkb_len ( pkb ) < sizeof ( *udphdr ) ) {
  207. DBG ( "UDP packet too short (%d bytes)\n",
  208. pkb_len ( pkb ) );
  209. return;
  210. }
  211. ulen = ntohs ( udphdr->len );
  212. if ( ulen != pkb_len ( pkb ) ) {
  213. DBG ( "Inconsistent UDP packet length (%d bytes)\n",
  214. pkb_len ( pkb ) );
  215. return;
  216. }
  217. /* Verify the checksum */
  218. chksum = calc_chksum ( pkb->data, pkb_len ( pkb ) );
  219. if ( chksum != 0xffff ) {
  220. DBG ( "Bad checksum %d\n", chksum );
  221. return;
  222. }
  223. /* Todo: Check if it is a broadcast or multicast address */
  224. /* Demux the connection */
  225. list_for_each_entry ( conn, &udp_conns, list ) {
  226. if ( conn->local_port == ntohs ( udphdr->dest_port ) ) {
  227. goto conn;
  228. }
  229. }
  230. return;
  231. conn:
  232. /** Strip off the UDP header */
  233. pkb_pull ( pkb, sizeof ( *udphdr ) );
  234. /** Allocate max possible buffer space to the tx buffer */
  235. conn->tx_pkb = alloc_pkb ( UDP_MAX_TXPKB );
  236. pkb_reserve ( conn->tx_pkb, UDP_MAX_HLEN );
  237. /** Call the application's callback */
  238. conn->udp_op->newdata ( conn, pkb->data, ulen - sizeof ( *udphdr ) );
  239. }
  240. struct trans_protocol udp_protocol = {
  241. .name = "UDP",
  242. .rx = udp_rx,
  243. .trans_proto = IP_UDP,
  244. };
  245. TRANS_PROTOCOL ( udp_protocol );