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