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

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