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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. #include <stdint.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <byteswap.h>
  6. #include <errno.h>
  7. #include <gpxe/tcpip.h>
  8. #include <gpxe/iobuf.h>
  9. #include <gpxe/xfer.h>
  10. #include <gpxe/open.h>
  11. #include <gpxe/uri.h>
  12. #include <gpxe/udp.h>
  13. /** @file
  14. *
  15. * UDP protocol
  16. */
  17. /**
  18. * A UDP connection
  19. *
  20. */
  21. struct udp_connection {
  22. /** Reference counter */
  23. struct refcnt refcnt;
  24. /** List of UDP connections */
  25. struct list_head list;
  26. /** Data transfer interface */
  27. struct xfer_interface xfer;
  28. /** Remote socket address */
  29. struct sockaddr_tcpip peer;
  30. /** Local port on which the connection receives packets */
  31. unsigned int local_port;
  32. };
  33. /**
  34. * List of registered UDP connections
  35. */
  36. static LIST_HEAD ( udp_conns );
  37. /* Forward declatations */
  38. static struct xfer_interface_operations udp_xfer_operations;
  39. struct tcpip_protocol udp_protocol;
  40. /**
  41. * Bind UDP connection to local port
  42. *
  43. * @v udp UDP connection
  44. * @v port Local port, in network byte order, or zero
  45. * @ret rc Return status code
  46. *
  47. * Opens the UDP connection and binds to a local port. If no local
  48. * port is specified, the first available port will be used.
  49. */
  50. static int udp_bind ( struct udp_connection *udp, unsigned int port ) {
  51. struct udp_connection *existing;
  52. static uint16_t try_port = 1024;
  53. /* If no port specified, find the first available port */
  54. if ( ! port ) {
  55. for ( ; try_port ; try_port++ ) {
  56. if ( try_port < 1024 )
  57. continue;
  58. if ( udp_bind ( udp, htons ( try_port ) ) == 0 )
  59. return 0;
  60. }
  61. return -EADDRINUSE;
  62. }
  63. /* Attempt bind to local port */
  64. list_for_each_entry ( existing, &udp_conns, list ) {
  65. if ( existing->local_port == port ) {
  66. DBGC ( udp, "UDP %p could not bind: port %d in use\n",
  67. udp, ntohs ( port ) );
  68. return -EADDRINUSE;
  69. }
  70. }
  71. udp->local_port = port;
  72. /* Add to UDP connection list */
  73. DBGC ( udp, "UDP %p bound to port %d\n", udp, ntohs ( port ) );
  74. return 0;
  75. }
  76. /**
  77. * Open a UDP connection
  78. *
  79. * @v xfer Data transfer interface
  80. * @v peer Peer socket address
  81. * @v local Local socket address, or NULL
  82. * @v promisc Socket is promiscuous
  83. * @ret rc Return status code
  84. */
  85. static int udp_open_common ( struct xfer_interface *xfer,
  86. struct sockaddr *peer, struct sockaddr *local,
  87. int promisc ) {
  88. struct sockaddr_tcpip *st_peer = ( struct sockaddr_tcpip * ) peer;
  89. struct sockaddr_tcpip *st_local = ( struct sockaddr_tcpip * ) local;
  90. struct udp_connection *udp;
  91. unsigned int bind_port;
  92. int rc;
  93. /* Allocate and initialise structure */
  94. udp = zalloc ( sizeof ( *udp ) );
  95. if ( ! udp )
  96. return -ENOMEM;
  97. DBGC ( udp, "UDP %p allocated\n", udp );
  98. xfer_init ( &udp->xfer, &udp_xfer_operations, &udp->refcnt );
  99. memcpy ( &udp->peer, st_peer, sizeof ( udp->peer ) );
  100. /* Bind to local port */
  101. if ( ! promisc ) {
  102. bind_port = ( st_local ? st_local->st_port : 0 );
  103. if ( ( rc = udp_bind ( udp, bind_port ) ) != 0 )
  104. goto err;
  105. }
  106. /* Attach parent interface, transfer reference to connection
  107. * list and return
  108. */
  109. xfer_plug_plug ( &udp->xfer, xfer );
  110. list_add ( &udp->list, &udp_conns );
  111. return 0;
  112. err:
  113. ref_put ( &udp->refcnt );
  114. return rc;
  115. }
  116. /**
  117. * Open a UDP connection
  118. *
  119. * @v xfer Data transfer interface
  120. * @v peer Peer socket address
  121. * @v local Local socket address, or NULL
  122. * @ret rc Return status code
  123. */
  124. int udp_open ( struct xfer_interface *xfer, struct sockaddr *peer,
  125. struct sockaddr *local ) {
  126. return udp_open_common ( xfer, peer, local, 0 );
  127. }
  128. /**
  129. * Open a promiscuous UDP connection
  130. *
  131. * @v xfer Data transfer interface
  132. * @ret rc Return status code
  133. *
  134. * Promiscuous UDP connections are required in order to support the
  135. * PXE API.
  136. */
  137. int udp_open_promisc ( struct xfer_interface *xfer ) {
  138. return udp_open_common ( xfer, NULL, NULL, 1 );
  139. }
  140. /**
  141. * Close a UDP connection
  142. *
  143. * @v udp UDP connection
  144. * @v rc Reason for close
  145. */
  146. static void udp_close ( struct udp_connection *udp, int rc ) {
  147. /* Close data transfer interface */
  148. xfer_nullify ( &udp->xfer );
  149. xfer_close ( &udp->xfer, rc );
  150. /* Remove from list of connections and drop list's reference */
  151. list_del ( &udp->list );
  152. ref_put ( &udp->refcnt );
  153. DBGC ( udp, "UDP %p closed\n", udp );
  154. }
  155. /**
  156. * Transmit data via a UDP connection to a specified address
  157. *
  158. * @v udp UDP connection
  159. * @v iobuf I/O buffer
  160. * @v src_port Source port, or 0 to use default
  161. * @v dest Destination address, or NULL to use default
  162. * @v netdev Network device, or NULL to use default
  163. * @ret rc Return status code
  164. */
  165. static int udp_tx ( struct udp_connection *udp, struct io_buffer *iobuf,
  166. unsigned int src_port, struct sockaddr_tcpip *dest,
  167. struct net_device *netdev ) {
  168. struct udp_header *udphdr;
  169. size_t len;
  170. int rc;
  171. /* Check we can accommodate the header */
  172. if ( ( rc = iob_ensure_headroom ( iobuf, UDP_MAX_HLEN ) ) != 0 ) {
  173. free_iob ( iobuf );
  174. return rc;
  175. }
  176. /* Fill in default values if not explicitly provided */
  177. if ( ! src_port )
  178. src_port = udp->local_port;
  179. if ( ! dest )
  180. dest = &udp->peer;
  181. /* Add the UDP header */
  182. udphdr = iob_push ( iobuf, sizeof ( *udphdr ) );
  183. len = iob_len ( iobuf );
  184. udphdr->dest = dest->st_port;
  185. udphdr->src = src_port;
  186. udphdr->len = htons ( len );
  187. udphdr->chksum = 0;
  188. udphdr->chksum = tcpip_chksum ( udphdr, len );
  189. /* Dump debugging information */
  190. DBGC ( udp, "UDP %p TX %d->%d len %zd\n", udp,
  191. ntohs ( udphdr->src ), ntohs ( udphdr->dest ),
  192. ntohs ( udphdr->len ) );
  193. /* Send it to the next layer for processing */
  194. if ( ( rc = tcpip_tx ( iobuf, &udp_protocol, dest, netdev,
  195. &udphdr->chksum ) ) != 0 ) {
  196. DBGC ( udp, "UDP %p could not transmit packet: %s\n",
  197. udp, strerror ( rc ) );
  198. return rc;
  199. }
  200. return 0;
  201. }
  202. /**
  203. * Identify UDP connection by local port number
  204. *
  205. * @v local_port Local port (in network-endian order)
  206. * @ret udp UDP connection, or NULL
  207. */
  208. static struct udp_connection * udp_demux ( unsigned int local_port ) {
  209. struct udp_connection *udp;
  210. list_for_each_entry ( udp, &udp_conns, list ) {
  211. if ( ( udp->local_port == local_port ) ||
  212. ( udp->local_port == 0 ) ) {
  213. return udp;
  214. }
  215. }
  216. return NULL;
  217. }
  218. /**
  219. * Process a received packet
  220. *
  221. * @v iobuf I/O buffer
  222. * @v st_src Partially-filled source address
  223. * @v st_dest Partially-filled destination address
  224. * @v pshdr_csum Pseudo-header checksum
  225. * @ret rc Return status code
  226. */
  227. static int udp_rx ( struct io_buffer *iobuf, struct sockaddr_tcpip *st_src,
  228. struct sockaddr_tcpip *st_dest, uint16_t pshdr_csum ) {
  229. struct udp_header *udphdr = iobuf->data;
  230. struct udp_connection *udp;
  231. struct xfer_metadata meta;
  232. size_t ulen;
  233. unsigned int csum;
  234. int rc = 0;
  235. /* Sanity check packet */
  236. if ( iob_len ( iobuf ) < sizeof ( *udphdr ) ) {
  237. DBG ( "UDP packet too short at %d bytes (min %d bytes)\n",
  238. iob_len ( iobuf ), sizeof ( *udphdr ) );
  239. rc = -EINVAL;
  240. goto done;
  241. }
  242. ulen = ntohs ( udphdr->len );
  243. if ( ulen < sizeof ( *udphdr ) ) {
  244. DBG ( "UDP length too short at %d bytes "
  245. "(header is %d bytes)\n", ulen, sizeof ( *udphdr ) );
  246. rc = -EINVAL;
  247. goto done;
  248. }
  249. if ( ulen > iob_len ( iobuf ) ) {
  250. DBG ( "UDP length too long at %d bytes (packet is %d bytes)\n",
  251. ulen, iob_len ( iobuf ) );
  252. rc = -EINVAL;
  253. goto done;
  254. }
  255. if ( udphdr->chksum ) {
  256. csum = tcpip_continue_chksum ( pshdr_csum, iobuf->data, ulen );
  257. if ( csum != 0 ) {
  258. DBG ( "UDP checksum incorrect (is %04x including "
  259. "checksum field, should be 0000)\n", csum );
  260. rc = -EINVAL;
  261. goto done;
  262. }
  263. }
  264. /* Parse parameters from header and strip header */
  265. st_src->st_port = udphdr->src;
  266. st_dest->st_port = udphdr->dest;
  267. udp = udp_demux ( udphdr->dest );
  268. iob_unput ( iobuf, ( iob_len ( iobuf ) - ulen ) );
  269. iob_pull ( iobuf, sizeof ( *udphdr ) );
  270. /* Dump debugging information */
  271. DBGC ( udp, "UDP %p RX %d<-%d len %zd\n", udp,
  272. ntohs ( udphdr->dest ), ntohs ( udphdr->src ), ulen );
  273. /* Ignore if no matching connection found */
  274. if ( ! udp ) {
  275. DBG ( "No UDP connection listening on port %d\n",
  276. ntohs ( udphdr->dest ) );
  277. rc = -ENOTCONN;
  278. goto done;
  279. }
  280. /* Pass data to application */
  281. memset ( &meta, 0, sizeof ( meta ) );
  282. meta.src = ( struct sockaddr * ) st_src;
  283. meta.dest = ( struct sockaddr * ) st_dest;
  284. rc = xfer_deliver_iob_meta ( &udp->xfer, iobuf, &meta );
  285. iobuf = NULL;
  286. done:
  287. free_iob ( iobuf );
  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. };
  295. /***************************************************************************
  296. *
  297. * Data transfer interface
  298. *
  299. ***************************************************************************
  300. */
  301. /**
  302. * Close interface
  303. *
  304. * @v xfer Data transfer interface
  305. * @v rc Reason for close
  306. */
  307. static void udp_xfer_close ( struct xfer_interface *xfer, int rc ) {
  308. struct udp_connection *udp =
  309. container_of ( xfer, struct udp_connection, xfer );
  310. /* Close connection */
  311. udp_close ( udp, rc );
  312. }
  313. /**
  314. * Allocate I/O buffer for UDP
  315. *
  316. * @v xfer Data transfer interface
  317. * @v len Payload size
  318. * @ret iobuf I/O buffer, or NULL
  319. */
  320. static struct io_buffer * udp_alloc_iob ( struct xfer_interface *xfer,
  321. size_t len ) {
  322. struct udp_connection *udp =
  323. container_of ( xfer, struct udp_connection, xfer );
  324. struct io_buffer *iobuf;
  325. iobuf = alloc_iob ( UDP_MAX_HLEN + len );
  326. if ( ! iobuf ) {
  327. DBGC ( udp, "UDP %p cannot allocate buffer of length %d\n",
  328. udp, len );
  329. return NULL;
  330. }
  331. iob_reserve ( iobuf, UDP_MAX_HLEN );
  332. return iobuf;
  333. }
  334. /**
  335. * Deliver datagram as I/O buffer
  336. *
  337. * @v xfer Data transfer interface
  338. * @v iobuf Datagram I/O buffer
  339. * @v meta Data transfer metadata, or NULL
  340. * @ret rc Return status code
  341. */
  342. static int udp_xfer_deliver_iob ( struct xfer_interface *xfer,
  343. struct io_buffer *iobuf,
  344. struct xfer_metadata *meta ) {
  345. struct udp_connection *udp =
  346. container_of ( xfer, struct udp_connection, xfer );
  347. struct sockaddr_tcpip *src;
  348. struct sockaddr_tcpip *dest = NULL;
  349. struct net_device *netdev = NULL;
  350. unsigned int src_port = 0;
  351. /* Apply xfer metadata */
  352. if ( meta ) {
  353. src = ( struct sockaddr_tcpip * ) meta->src;
  354. if ( src )
  355. src_port = src->st_port;
  356. dest = ( struct sockaddr_tcpip * ) meta->dest;
  357. netdev = meta->netdev;
  358. }
  359. /* Transmit data, if possible */
  360. udp_tx ( udp, iobuf, src_port, dest, netdev );
  361. return 0;
  362. }
  363. /** UDP data transfer interface operations */
  364. static struct xfer_interface_operations udp_xfer_operations = {
  365. .close = udp_xfer_close,
  366. .vredirect = ignore_xfer_vredirect,
  367. .seek = ignore_xfer_seek,
  368. .window = unlimited_xfer_window,
  369. .alloc_iob = udp_alloc_iob,
  370. .deliver_iob = udp_xfer_deliver_iob,
  371. .deliver_raw = xfer_deliver_as_iob,
  372. };
  373. /***************************************************************************
  374. *
  375. * Openers
  376. *
  377. ***************************************************************************
  378. */
  379. /** UDP socket opener */
  380. struct socket_opener udp_socket_opener __socket_opener = {
  381. .semantics = SOCK_DGRAM,
  382. .family = AF_INET,
  383. .open = udp_open,
  384. };
  385. char UDP_SOCK_DGRAM[1];
  386. /**
  387. * Open UDP URI
  388. *
  389. * @v xfer Data transfer interface
  390. * @v uri URI
  391. * @ret rc Return status code
  392. */
  393. static int udp_open_uri ( struct xfer_interface *xfer, struct uri *uri ) {
  394. struct sockaddr_tcpip peer;
  395. /* Sanity check */
  396. if ( ! uri->host )
  397. return -EINVAL;
  398. memset ( &peer, 0, sizeof ( peer ) );
  399. peer.st_port = htons ( uri_port ( uri, 0 ) );
  400. return xfer_open_named_socket ( xfer, SOCK_DGRAM,
  401. ( struct sockaddr * ) &peer,
  402. uri->host, NULL );
  403. }
  404. /** UDP URI opener */
  405. struct uri_opener udp_uri_opener __uri_opener = {
  406. .scheme = "udp",
  407. .open = udp_open_uri,
  408. };