6 #include <gpxe/tcpip.h>
7 #include <gpxe/pkbuff.h>
8 #include <gpxe/netdevice.h>
16 struct tcpip_protocol udp_protocol;
19 * List of registered UDP connections
21 static LIST_HEAD ( udp_conns );
24 * Bind UDP connection to local port
26 * @v conn UDP connection
27 * @v local_port Local port, in network byte order
28 * @ret rc Return status code
30 int udp_bind ( struct udp_connection *conn, uint16_t local_port ) {
31 struct udp_connection *existing;
33 list_for_each_entry ( existing, &udp_conns, list ) {
34 if ( existing->local_port == local_port )
37 conn->local_port = local_port;
44 * @v conn UDP connection
45 * @v local_port Local port, in network byte order, or zero
46 * @ret rc Return status code
48 * Opens the UDP connection and binds to a local port. If no local
49 * port is specified, the first available port will be used.
51 int udp_open ( struct udp_connection *conn, uint16_t local_port ) {
52 static uint16_t try_port = 1024;
55 /* If no port specified, find the first available port */
57 for ( ; try_port ; try_port++ ) {
58 if ( try_port < 1024 )
60 if ( udp_open ( conn, htons ( try_port ) ) == 0 )
66 /* Attempt bind to local port */
67 if ( ( rc = udp_bind ( conn, local_port ) ) != 0 )
70 /* Add to UDP connection list */
71 list_add ( &conn->list, &udp_conns );
72 DBGC ( conn, "UDP %p opened on port %d\n", conn,
73 ntohs ( local_port ) );
79 * Close a UDP connection
81 * @v conn UDP connection
83 void udp_close ( struct udp_connection *conn ) {
84 list_del ( &conn->list );
85 DBGC ( conn, "UDP %p closed\n", conn );
89 * User request to send data via a UDP connection
91 * @v conn UDP connection
93 * This function allocates buffer space and invokes the function's senddata()
94 * callback. The callback may use the buffer space
96 int udp_senddata ( struct udp_connection *conn ) {
99 conn->tx_pkb = alloc_pkb ( UDP_MAX_TXPKB );
100 if ( conn->tx_pkb == NULL ) {
101 DBGC ( conn, "UDP %p cannot allocate buffer of length %d\n",
102 conn, UDP_MAX_TXPKB );
105 pkb_reserve ( conn->tx_pkb, UDP_MAX_HLEN );
106 rc = conn->udp_op->senddata ( conn, conn->tx_pkb->data,
107 pkb_tailroom ( conn->tx_pkb ) );
109 free_pkb ( conn->tx_pkb );
114 * Transmit data via a UDP connection to a specified address
116 * @v conn UDP connection
117 * @v peer Destination address
118 * @v data Data to send
119 * @v len Length of data
120 * @ret rc Return status code
122 * This function fills up the UDP headers and sends the data. It may
123 * be called only from within the context of an application's
124 * senddata() method; if the application wishes to send data it must
125 * call udp_senddata() and wait for its senddata() method to be
128 int udp_sendto ( struct udp_connection *conn, struct sockaddr_tcpip *peer,
129 const void *data, size_t len ) {
130 struct udp_header *udphdr;
133 /* Take ownership of packet buffer back from the
134 * udp_connection structure.
139 /* Avoid overflowing TX buffer */
140 if ( len > pkb_tailroom ( pkb ) )
141 len = pkb_tailroom ( pkb );
144 memmove ( pkb_put ( pkb, len ), data, len );
149 * Covert all 16- and 32- bit integers into network btye order before
150 * sending it over the network
152 udphdr = pkb_push ( pkb, sizeof ( *udphdr ) );
153 udphdr->dest_port = peer->st_port;
154 udphdr->source_port = conn->local_port;
155 udphdr->len = htons ( pkb_len ( pkb ) );
157 udphdr->chksum = tcpip_chksum ( udphdr, sizeof ( *udphdr ) + len );
159 /* Dump debugging information */
160 DBGC ( conn, "UDP %p TX %d->%d len %zd\n", conn,
161 ntohs ( udphdr->source_port ), ntohs ( udphdr->dest_port ),
162 ntohs ( udphdr->len ) );
164 /* Send it to the next layer for processing */
165 return tcpip_tx ( pkb, &udp_protocol, peer, NULL, &udphdr->chksum );
169 * Transmit data via a UDP connection
171 * @v conn UDP connection
172 * @v data Data to send
173 * @v len Length of data
174 * @ret rc Return status code
176 * This function fills up the UDP headers and sends the data. It may
177 * be called only from within the context of an application's
178 * senddata() method; if the application wishes to send data it must
179 * call udp_senddata() and wait for its senddata() method to be
182 int udp_send ( struct udp_connection *conn, const void *data, size_t len ) {
183 return udp_sendto ( conn, &conn->peer, data, len );
187 * Identify UDP connection by local port number
189 * @v local_port Local port (in network-endian order)
190 * @ret conn TCP connection, or NULL
192 static struct udp_connection * udp_demux ( uint16_t local_port ) {
193 struct udp_connection *conn;
195 list_for_each_entry ( conn, &udp_conns, list ) {
196 if ( ( conn->local_port == local_port ) ||
197 ( conn->local_port == 0 ) ) {
205 * Process a received packet
207 * @v pkb Packet buffer
208 * @v st_src Partially-filled source address
209 * @v st_dest Partially-filled destination address
210 * @v pshdr_csum Pseudo-header checksum
211 * @ret rc Return status code
213 static int udp_rx ( struct pk_buff *pkb, struct sockaddr_tcpip *st_src,
214 struct sockaddr_tcpip *st_dest, uint16_t pshdr_csum ) {
215 struct udp_header *udphdr = pkb->data;
216 struct udp_connection *conn;
221 /* Sanity check packet */
222 if ( pkb_len ( pkb ) < sizeof ( *udphdr ) ) {
223 DBG ( "UDP packet too short at %d bytes (min %d bytes)\n",
224 pkb_len ( pkb ), sizeof ( *udphdr ) );
229 ulen = ntohs ( udphdr->len );
230 if ( ulen < sizeof ( *udphdr ) ) {
231 DBG ( "UDP length too short at %d bytes "
232 "(header is %d bytes)\n", ulen, sizeof ( *udphdr ) );
236 if ( ulen > pkb_len ( pkb ) ) {
237 DBG ( "UDP length too long at %d bytes (packet is %d bytes)\n",
238 ulen, pkb_len ( pkb ) );
242 if ( udphdr->chksum ) {
243 csum = tcpip_continue_chksum ( pshdr_csum, pkb->data, ulen );
245 DBG ( "UDP checksum incorrect (is %04x including "
246 "checksum field, should be 0000)\n", csum );
252 /* Parse parameters from header and strip header */
253 st_src->st_port = udphdr->source_port;
254 st_dest->st_port = udphdr->dest_port;
255 conn = udp_demux ( udphdr->dest_port );
256 pkb_unput ( pkb, ( pkb_len ( pkb ) - ulen ) );
257 pkb_pull ( pkb, sizeof ( *udphdr ) );
259 /* Dump debugging information */
260 DBGC ( conn, "UDP %p RX %d<-%d len %zd\n", conn,
261 ntohs ( udphdr->dest_port ), ntohs ( udphdr->source_port ),
264 /* Ignore if no matching connection found */
266 DBG ( "No UDP connection listening on port %d\n",
267 ntohs ( udphdr->dest_port ) );
272 /* Pass data to application */
273 rc = conn->udp_op->newdata ( conn, pkb->data, pkb_len ( pkb ),
281 struct tcpip_protocol udp_protocol __tcpip_protocol = {
284 .tcpip_proto = IP_UDP,