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 netdev Network device (or NULL to route automatically)
69 * @v trans_csum Transport-layer checksum to complete, or NULL
70 * @ret rc Return status code
72 int tcpip_tx ( struct pk_buff *pkb, 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 ( pkb, tcpip_protocol, st_dest,
87 DBG ( "Unrecognised TCP/IP address family %d\n", st_dest->st_family );
93 * Calculate continued TCP/IP checkum
95 * @v partial Checksum of already-summed data, in network byte order
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
110 uint16_t tcpip_continue_chksum ( uint16_t partial, const void *data,
112 unsigned int cksum = ( ( ~partial ) & 0xffff );
116 for ( i = 0 ; i < len ; i++ ) {
117 value = * ( ( uint8_t * ) data + i );
119 /* Odd bytes: swap on little-endian systems */
120 value = be16_to_cpu ( value );
122 /* Even bytes: swap on big-endian systems */
123 value = le16_to_cpu ( value );
126 if ( cksum > 0xffff )
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 );