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

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