5 #include <gpxe/pkbuff.h>
6 #include <gpxe/tables.h>
7 #include <gpxe/tcpip.h>
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
19 tcpip_net_protocols[0] __table_start ( tcpip_net_protocols );
20 static struct tcpip_net_protocol
21 tcpip_net_protocols_end[0] __table_end ( tcpip_net_protocols );
23 /** Registered transport-layer protocols that support TCP/IP */
24 static struct tcpip_protocol
25 tcpip_protocols[0]__table_start ( tcpip_protocols );
26 static struct tcpip_protocol
27 tcpip_protocols_end[0] __table_end ( tcpip_protocols );
29 /** Process a received TCP/IP packet
31 * @v pkb Packet 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 pk_buff *pkb, 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 ( pkb, st_src, st_dest, pshdr_csum );
58 DBG ( "Unrecognised TCP/IP protocol %d\n", tcpip_proto );
60 return -EPROTONOSUPPORT;
63 /** Transmit a TCP/IP packet
65 * @v pkb Packet buffer
66 * @v tcpip_protocol Transport-layer protocol
67 * @v st_dest Destination address
68 * @v trans_csum Transport-layer checksum to complete, or NULL
69 * @ret rc Return status code
71 int tcpip_tx ( struct pk_buff *pkb, struct tcpip_protocol *tcpip_protocol,
72 struct sockaddr_tcpip *st_dest, uint16_t *trans_csum ) {
73 struct tcpip_net_protocol *tcpip_net;
75 /* Hand off packet to the appropriate network-layer protocol */
76 for ( tcpip_net = tcpip_net_protocols ;
77 tcpip_net < tcpip_net_protocols_end ; tcpip_net++ ) {
78 if ( tcpip_net->sa_family == st_dest->st_family ) {
79 DBG ( "TCP/IP sending %s packet\n", tcpip_net->name );
80 return tcpip_net->tx ( pkb, tcpip_protocol, st_dest,
85 DBG ( "Unrecognised TCP/IP address family %d\n", st_dest->st_family );
91 * Calculate continued TCP/IP checkum
93 * @v partial Checksum of already-summed data, in network byte order
95 * @v len Length of data buffer
96 * @ret cksum Updated checksum, in network byte order
98 * Calculates a TCP/IP-style 16-bit checksum over the data block. The
99 * checksum is returned in network byte order.
101 * This function may be used to add new data to an existing checksum.
102 * The function assumes that both the old data and the new data start
103 * on even byte offsets; if this is not the case then you will need to
104 * byte-swap either the input partial checksum, the output checksum,
105 * or both. Deciding which to swap is left as an exercise for the
108 uint16_t tcpip_continue_chksum ( uint16_t partial, const void *data,
110 unsigned int cksum = ( ( ~partial ) & 0xffff );
114 for ( i = 0 ; i < len ; i++ ) {
115 value = * ( ( uint8_t * ) data + i );
117 /* Odd bytes: swap on little-endian systems */
118 value = be16_to_cpu ( value );
120 /* Even bytes: swap on big-endian systems */
121 value = le16_to_cpu ( value );
124 if ( cksum > 0xffff )
132 * Calculate TCP/IP checkum
134 * @v data Data buffer
135 * @v len Length of data buffer
136 * @ret cksum Checksum, in network byte order
138 * Calculates a TCP/IP-style 16-bit checksum over the data block. The
139 * checksum is returned in network byte order.
141 uint16_t tcpip_chksum ( const void *data, size_t len ) {
142 return tcpip_continue_chksum ( TCPIP_EMPTY_CSUM, data, len );