19
20
/** Process a received TCP/IP packet
21
22
* @v iobuf I/O buffer
23
* @v netdev Network device
22
24
* @v tcpip_proto Transport-layer protocol number
23
25
* @v st_src Partially-filled source address
24
26
* @v st_dest Partially-filled destination address
31
33
* address family and the network-layer addresses, but leave the ports
32
34
* and the rest of the structures as zero).
34
int tcpip_rx ( struct io_buffer *iobuf, uint8_t tcpip_proto,
35
struct sockaddr_tcpip *st_src,
36
int tcpip_rx ( struct io_buffer *iobuf, struct net_device *netdev,
37
uint8_t tcpip_proto, struct sockaddr_tcpip *st_src,
36
38
struct sockaddr_tcpip *st_dest,
37
39
uint16_t pshdr_csum ) {
38
40
struct tcpip_protocol *tcpip;
41
43
for_each_table_entry ( tcpip, TCPIP_PROTOCOLS ) {
42
44
if ( tcpip->tcpip_proto == tcpip_proto ) {
43
45
DBG ( "TCP/IP received %s packet\n", tcpip->name );
44
return tcpip->rx ( iobuf, st_src, st_dest, pshdr_csum );
46
return tcpip->rx ( iobuf, netdev, st_src, st_dest,
133
136
uint16_t tcpip_chksum ( const void *data, size_t len ) {
134
137
return tcpip_continue_chksum ( TCPIP_EMPTY_CSUM, data, len );
141
* Bind to local TCP/IP port
143
* @v st_local Local TCP/IP socket address, or NULL
144
* @v available Function to check port availability
145
* @ret port Local port number, or negative error
147
int tcpip_bind ( struct sockaddr_tcpip *st_local,
148
int ( * available ) ( int port ) ) {
150
uint16_t try_port = 0;
156
/* Extract parameters from local socket address */
158
flags = st_local->st_flags;
159
try_port = ntohs ( st_local->st_port );
162
/* If an explicit port is specified, check its availability */
164
return available ( try_port );
166
/* Otherwise, find an available port in the range [1,1023] or
167
* [1025,65535] as appropriate.
169
min_port = ( ( ( ! flags ) & TCPIP_BIND_PRIVILEGED ) + 1 );
170
max_port = ( ( flags & TCPIP_BIND_PRIVILEGED ) - 1 );
172
for ( i = 0 ; i <= max_port ; i++ ) {
173
try_port = ( ( i + offset ) & max_port );
174
if ( try_port < min_port )
176
if ( available ( try_port ) < 0 )