Rename e1000_main.c to e1000.c to so we can type 'make bin/e1000.dsk' instead of...
[gpxe.git] / src / net / tcpip.c
blob1bc8d1a30b697d5223bde5cba3ee39818132492d
1 #include <stdint.h>
2 #include <string.h>
3 #include <errno.h>
4 #include <byteswap.h>
5 #include <gpxe/iobuf.h>
6 #include <gpxe/tables.h>
7 #include <gpxe/tcpip.h>
9 /** @file
11 * Transport-network layer interface
13 * This file contains functions and utilities for the
14 * TCP/IP transport-network layer interface
17 /** Registered network-layer protocols that support TCP/IP */
18 static struct tcpip_net_protocol tcpip_net_protocols[0]
19 __table_start ( struct tcpip_net_protocol, tcpip_net_protocols );
20 static struct tcpip_net_protocol tcpip_net_protocols_end[0]
21 __table_end ( struct tcpip_net_protocol, tcpip_net_protocols );
23 /** Registered transport-layer protocols that support TCP/IP */
24 static struct tcpip_protocol tcpip_protocols[0]
25 __table_start ( struct tcpip_protocol, tcpip_protocols );
26 static struct tcpip_protocol tcpip_protocols_end[0]
27 __table_end ( struct tcpip_protocol, tcpip_protocols );
29 /** Process a received TCP/IP packet
31 * @v iobuf I/O buffer
32 * @v tcpip_proto Transport-layer protocol number
33 * @v st_src Partially-filled source address
34 * @v st_dest Partially-filled destination address
35 * @v pshdr_csum Pseudo-header checksum
36 * @ret rc Return status code
38 * This function expects a transport-layer segment from the network
39 * layer. The network layer should fill in as much as it can of the
40 * source and destination addresses (i.e. it should fill in the
41 * address family and the network-layer addresses, but leave the ports
42 * and the rest of the structures as zero).
44 int tcpip_rx ( struct io_buffer *iobuf, uint8_t tcpip_proto,
45 struct sockaddr_tcpip *st_src,
46 struct sockaddr_tcpip *st_dest,
47 uint16_t pshdr_csum ) {
48 struct tcpip_protocol *tcpip;
50 /* Hand off packet to the appropriate transport-layer protocol */
51 for ( tcpip = tcpip_protocols; tcpip < tcpip_protocols_end; tcpip++ ) {
52 if ( tcpip->tcpip_proto == tcpip_proto ) {
53 DBG ( "TCP/IP received %s packet\n", tcpip->name );
54 return tcpip->rx ( iobuf, st_src, st_dest, pshdr_csum );
58 DBG ( "Unrecognised TCP/IP protocol %d\n", tcpip_proto );
59 free_iob ( iobuf );
60 return -EPROTONOSUPPORT;
63 /** Transmit a TCP/IP packet
65 * @v iobuf I/O buffer
66 * @v tcpip_protocol Transport-layer protocol
67 * @v st_dest Destination address
68 * @v netdev Network device to use if no route found, or NULL
69 * @v trans_csum Transport-layer checksum to complete, or NULL
70 * @ret rc Return status code
72 int tcpip_tx ( struct io_buffer *iobuf, struct tcpip_protocol *tcpip_protocol,
73 struct sockaddr_tcpip *st_dest, struct net_device *netdev,
74 uint16_t *trans_csum ) {
75 struct tcpip_net_protocol *tcpip_net;
77 /* Hand off packet to the appropriate network-layer protocol */
78 for ( tcpip_net = tcpip_net_protocols ;
79 tcpip_net < tcpip_net_protocols_end ; tcpip_net++ ) {
80 if ( tcpip_net->sa_family == st_dest->st_family ) {
81 DBG ( "TCP/IP sending %s packet\n", tcpip_net->name );
82 return tcpip_net->tx ( iobuf, tcpip_protocol, st_dest,
83 netdev, trans_csum );
87 DBG ( "Unrecognised TCP/IP address family %d\n", st_dest->st_family );
88 free_iob ( iobuf );
89 return -EAFNOSUPPORT;
92 /**
93 * Calculate continued TCP/IP checkum
95 * @v partial Checksum of already-summed data, in network byte order
96 * @v data Data buffer
97 * @v len Length of data buffer
98 * @ret cksum Updated checksum, in network byte order
100 * Calculates a TCP/IP-style 16-bit checksum over the data block. The
101 * checksum is returned in network byte order.
103 * This function may be used to add new data to an existing checksum.
104 * The function assumes that both the old data and the new data start
105 * on even byte offsets; if this is not the case then you will need to
106 * byte-swap either the input partial checksum, the output checksum,
107 * or both. Deciding which to swap is left as an exercise for the
108 * interested reader.
110 uint16_t tcpip_continue_chksum ( uint16_t partial, const void *data,
111 size_t len ) {
112 unsigned int cksum = ( ( ~partial ) & 0xffff );
113 unsigned int value;
114 unsigned int i;
116 for ( i = 0 ; i < len ; i++ ) {
117 value = * ( ( uint8_t * ) data + i );
118 if ( i & 1 ) {
119 /* Odd bytes: swap on little-endian systems */
120 value = be16_to_cpu ( value );
121 } else {
122 /* Even bytes: swap on big-endian systems */
123 value = le16_to_cpu ( value );
125 cksum += value;
126 if ( cksum > 0xffff )
127 cksum -= 0xffff;
130 return ( ~cksum );
134 * Calculate TCP/IP checkum
136 * @v data Data buffer
137 * @v len Length of data buffer
138 * @ret cksum Checksum, in network byte order
140 * Calculates a TCP/IP-style 16-bit checksum over the data block. The
141 * checksum is returned in network byte order.
143 uint16_t tcpip_chksum ( const void *data, size_t len ) {
144 return tcpip_continue_chksum ( TCPIP_EMPTY_CSUM, data, len );