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

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