5 #include <gpxe/iobuf.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 FILE_LICENCE ( GPL2_OR_LATER
);
19 /** Process a received TCP/IP packet
22 * @v tcpip_proto Transport-layer protocol number
23 * @v st_src Partially-filled source address
24 * @v st_dest Partially-filled destination address
25 * @v pshdr_csum Pseudo-header checksum
26 * @ret rc Return status code
28 * This function expects a transport-layer segment from the network
29 * layer. The network layer should fill in as much as it can of the
30 * source and destination addresses (i.e. it should fill in the
31 * address family and the network-layer addresses, but leave the ports
32 * 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 struct sockaddr_tcpip
*st_dest
,
37 uint16_t pshdr_csum
) {
38 struct tcpip_protocol
*tcpip
;
40 /* Hand off packet to the appropriate transport-layer protocol */
41 for_each_table_entry ( tcpip
, TCPIP_PROTOCOLS
) {
42 if ( tcpip
->tcpip_proto
== tcpip_proto
) {
43 DBG ( "TCP/IP received %s packet\n", tcpip
->name
);
44 return tcpip
->rx ( iobuf
, st_src
, st_dest
, pshdr_csum
);
48 DBG ( "Unrecognised TCP/IP protocol %d\n", tcpip_proto
);
50 return -EPROTONOSUPPORT
;
53 /** Transmit a TCP/IP packet
56 * @v tcpip_protocol Transport-layer protocol
57 * @v st_src Source address, or NULL to use route default
58 * @v st_dest Destination address
59 * @v netdev Network device to use if no route found, or NULL
60 * @v trans_csum Transport-layer checksum to complete, or NULL
61 * @ret rc Return status code
63 int tcpip_tx ( struct io_buffer
*iobuf
, struct tcpip_protocol
*tcpip_protocol
,
64 struct sockaddr_tcpip
*st_src
, struct sockaddr_tcpip
*st_dest
,
65 struct net_device
*netdev
, uint16_t *trans_csum
) {
66 struct tcpip_net_protocol
*tcpip_net
;
68 /* Hand off packet to the appropriate network-layer protocol */
69 for_each_table_entry ( tcpip_net
, TCPIP_NET_PROTOCOLS
) {
70 if ( tcpip_net
->sa_family
== st_dest
->st_family
) {
71 DBG ( "TCP/IP sending %s packet\n", tcpip_net
->name
);
72 return tcpip_net
->tx ( iobuf
, tcpip_protocol
, st_src
,
73 st_dest
, netdev
, trans_csum
);
77 DBG ( "Unrecognised TCP/IP address family %d\n", st_dest
->st_family
);
83 * Calculate continued TCP/IP checkum
85 * @v partial Checksum of already-summed data, in network byte order
87 * @v len Length of data buffer
88 * @ret cksum Updated checksum, in network byte order
90 * Calculates a TCP/IP-style 16-bit checksum over the data block. The
91 * checksum is returned in network byte order.
93 * This function may be used to add new data to an existing checksum.
94 * The function assumes that both the old data and the new data start
95 * on even byte offsets; if this is not the case then you will need to
96 * byte-swap either the input partial checksum, the output checksum,
97 * or both. Deciding which to swap is left as an exercise for the
100 uint16_t tcpip_continue_chksum ( uint16_t partial
, const void *data
,
102 unsigned int cksum
= ( ( ~partial
) & 0xffff );
106 for ( i
= 0 ; i
< len
; i
++ ) {
107 value
= * ( ( uint8_t * ) data
+ i
);
109 /* Odd bytes: swap on little-endian systems */
110 value
= be16_to_cpu ( value
);
112 /* Even bytes: swap on big-endian systems */
113 value
= le16_to_cpu ( value
);
116 if ( cksum
> 0xffff )
124 * Calculate TCP/IP checkum
126 * @v data Data buffer
127 * @v len Length of data buffer
128 * @ret cksum Checksum, in network byte order
130 * Calculates a TCP/IP-style 16-bit checksum over the data block. The
131 * checksum is returned in network byte order.
133 uint16_t tcpip_chksum ( const void *data
, size_t len
) {
134 return tcpip_continue_chksum ( TCPIP_EMPTY_CSUM
, data
, len
);