iPXE
tcpip.h
Go to the documentation of this file.
1 #ifndef _IPXE_TCPIP_H
2 #define _IPXE_TCPIP_H
3 
4 /** @file
5  *
6  * Transport-network layer interface
7  *
8  */
9 
10 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11 FILE_SECBOOT ( PERMITTED );
12 
13 #include <stdint.h>
14 #include <ipxe/socket.h>
15 #include <ipxe/in.h>
16 #include <ipxe/tables.h>
17 
19  const void *data, size_t len );
20 
21 #include <bits/tcpip.h>
22 
23 struct io_buffer;
24 struct net_device;
25 struct ip_statistics;
26 
27 /** Positive zero checksum value */
28 #define TCPIP_POSITIVE_ZERO_CSUM 0x0000
29 
30 /** Negative zero checksum value */
31 #define TCPIP_NEGATIVE_ZERO_CSUM 0xffff
32 
33 /** Empty checksum value
34  *
35  * All of our TCP/IP checksum algorithms will return only the positive
36  * representation of zero (0x0000) for a zero checksum over non-zero
37  * input data. This property arises since the end-around carry used
38  * to mimic one's complement addition using unsigned arithmetic
39  * prevents the running total from ever returning to 0x0000. The
40  * running total will therefore use only the negative representation
41  * of zero (0xffff). Since the return value is the one's complement
42  * negation of the running total (calculated by simply bit-inverting
43  * the running total), the return value will therefore use only the
44  * positive representation of zero (0x0000).
45  *
46  * It is a very common misconception (found in many places such as
47  * RFC1624) that this is a property guaranteed by the underlying
48  * mathematics. It is not; the choice of which zero representation is
49  * used is merely an artifact of the software implementation of the
50  * checksum algorithm.
51  *
52  * For consistency, we choose to use the positive representation of
53  * zero (0x0000) for the checksum of a zero-length block of data.
54  * This ensures that all of our TCP/IP checksum algorithms will return
55  * only the positive representation of zero (0x0000) for a zero
56  * checksum (regardless of the input data).
57  */
58 #define TCPIP_EMPTY_CSUM TCPIP_POSITIVE_ZERO_CSUM
59 
60 /** TCP/IP address flags */
62  /** Bind to a privileged port (less than 1024)
63  *
64  * This value is chosen as 1024 to optimise the calculations
65  * in tcpip_bind().
66  */
68 };
69 
70 /**
71  * TCP/IP socket address
72  *
73  * This contains the fields common to socket addresses for all TCP/IP
74  * address families.
75  */
77  /** Socket address family (part of struct @c sockaddr) */
79  /** Flags */
81  /** TCP/IP port */
83  /** Scope ID
84  *
85  * For link-local or multicast addresses, this is the network
86  * device index.
87  */
89  /** Padding
90  *
91  * This ensures that a struct @c sockaddr_tcpip is large
92  * enough to hold a socket address for any TCP/IP address
93  * family.
94  */
95  char pad[ sizeof ( struct sockaddr ) -
96  ( sizeof ( sa_family_t ) /* st_family */ +
97  sizeof ( uint16_t ) /* st_flags */ +
98  sizeof ( uint16_t ) /* st_port */ +
99  sizeof ( uint16_t ) /* st_scope_id */ ) ];
100 } __attribute__ (( packed, may_alias ));
101 
102 /**
103  * A transport-layer protocol of the TCP/IP stack (eg. UDP, TCP, etc)
104  */
106  /** Protocol name */
107  const char *name;
108  /**
109  * Process received packet
110  *
111  * @v iobuf I/O buffer
112  * @v netdev Network device
113  * @v st_src Partially-filled source address
114  * @v st_dest Partially-filled destination address
115  * @v pshdr_csum Pseudo-header checksum
116  * @ret rc Return status code
117  *
118  * This method takes ownership of the I/O buffer.
119  */
120  int ( * rx ) ( struct io_buffer *iobuf, struct net_device *netdev,
121  struct sockaddr_tcpip *st_src,
122  struct sockaddr_tcpip *st_dest, uint16_t pshdr_csum );
123  /** Preferred zero checksum value
124  *
125  * The checksum is a one's complement value: zero may be
126  * represented by either positive zero (0x0000) or negative
127  * zero (0xffff).
128  */
130  /**
131  * Transport-layer protocol number
132  *
133  * This is a constant of the type IP_XXX
134  */
136 };
137 
138 /**
139  * A network-layer protocol of the TCP/IP stack (eg. IPV4, IPv6, etc)
140  */
142  /** Protocol name */
143  const char *name;
144  /** Network address family */
146  /** Fixed header length */
147  size_t header_len;
148  /** Network-layer protocol */
150  /**
151  * Transmit packet
152  *
153  * @v iobuf I/O buffer
154  * @v tcpip_protocol Transport-layer protocol
155  * @v st_src Source address, or NULL to use default
156  * @v st_dest Destination address
157  * @v netdev Network device (or NULL to route automatically)
158  * @v trans_csum Transport-layer checksum to complete, or NULL
159  * @ret rc Return status code
160  *
161  * This function takes ownership of the I/O buffer.
162  */
163  int ( * tx ) ( struct io_buffer *iobuf,
165  struct sockaddr_tcpip *st_src,
166  struct sockaddr_tcpip *st_dest,
167  struct net_device *netdev,
168  uint16_t *trans_csum );
169  /**
170  * Determine transmitting network device
171  *
172  * @v st_dest Destination address
173  * @ret netdev Network device, or NULL
174  */
175  struct net_device * ( * netdev ) ( struct sockaddr_tcpip *dest );
176 };
177 
178 /** TCP/IP transport-layer protocol table */
179 #define TCPIP_PROTOCOLS __table ( struct tcpip_protocol, "tcpip_protocols" )
180 
181 /** Declare a TCP/IP transport-layer protocol */
182 #define __tcpip_protocol __table_entry ( TCPIP_PROTOCOLS, 01 )
183 
184 /** TCP/IP network-layer protocol table */
185 #define TCPIP_NET_PROTOCOLS \
186  __table ( struct tcpip_net_protocol, "tcpip_net_protocols" )
187 
188 /** Declare a TCP/IP network-layer protocol */
189 #define __tcpip_net_protocol __table_entry ( TCPIP_NET_PROTOCOLS, 01 )
190 
191 extern int tcpip_rx ( struct io_buffer *iobuf, struct net_device *netdev,
192  uint8_t tcpip_proto, struct sockaddr_tcpip *st_src,
193  struct sockaddr_tcpip *st_dest, uint16_t pshdr_csum,
194  struct ip_statistics *stats );
195 extern int tcpip_tx ( struct io_buffer *iobuf, struct tcpip_protocol *tcpip,
196  struct sockaddr_tcpip *st_src,
197  struct sockaddr_tcpip *st_dest,
198  struct net_device *netdev,
199  uint16_t *trans_csum );
201 extern struct net_device * tcpip_netdev ( struct sockaddr_tcpip *st_dest );
202 extern size_t tcpip_mtu ( struct sockaddr_tcpip *st_dest );
203 extern uint16_t tcpip_chksum ( const void *data, size_t len );
204 extern int tcpip_bind ( struct sockaddr_tcpip *st_local,
205  int ( * available ) ( int port ) );
206 
207 #endif /* _IPXE_TCPIP_H */
int tcpip_tx(struct io_buffer *iobuf, struct tcpip_protocol *tcpip, struct sockaddr_tcpip *st_src, struct sockaddr_tcpip *st_dest, struct net_device *netdev, uint16_t *trans_csum)
Transmit a TCP/IP packet.
Definition: tcpip.c:92
static const void size_t len
Definition: tcpip.h:26
TCP/IP socket address.
Definition: tcpip.h:76
unsigned short uint16_t
Definition: stdint.h:11
tcpip_st_flags
TCP/IP address flags.
Definition: tcpip.h:61
char pad[sizeof(struct sockaddr) -(sizeof(sa_family_t)+sizeof(uint16_t)+sizeof(uint16_t)+sizeof(uint16_t))]
Padding.
Definition: tcpip.h:99
struct tcpip_net_protocol * tcpip_net_protocol(sa_family_t sa_family)
Find TCP/IP network-layer protocol.
Definition: tcpip.c:69
int tcpip_bind(struct sockaddr_tcpip *st_local, int(*available)(int port))
Bind to local TCP/IP port.
Definition: tcpip.c:215
uint8_t tcpip_proto
Transport-layer protocol number.
Definition: tcpip.h:135
sa_family_t st_family
Socket address family (part of struct sockaddr)
Definition: tcpip.h:78
uint16_t generic_tcpip_continue_chksum(uint16_t partial, const void *data, size_t len)
Calculate continued TCP/IP checkum.
Definition: tcpip.c:171
static const void * data
Definition: tcpip.h:26
Bind to a privileged port (less than 1024)
Definition: tcpip.h:67
static __attribute__((always_inline)) uint16_t tcpip_continue_chksum(uint16_t partial
Calculate continued TCP/IP checkum.
u8 port
Port number.
Definition: CIB_PRM.h:31
size_t tcpip_mtu(struct sockaddr_tcpip *st_dest)
Determine maximum transmission unit.
Definition: tcpip.c:132
uint16_t st_scope_id
Scope ID.
Definition: tcpip.h:88
struct net_device *(* netdev)(struct sockaddr_tcpip *dest)
Determine transmitting network device.
Definition: tcpip.h:175
static struct net_device * netdev
Definition: gdbudp.c:52
uint16_t sa_family_t
A socket address family.
Definition: socket.h:86
struct net_protocol * net_protocol
Network-layer protocol.
Definition: tcpip.h:149
IP system statistics.
Definition: ipstat.h:45
uint16_t st_port
TCP/IP port.
Definition: tcpip.h:82
const char * name
Protocol name.
Definition: tcpip.h:107
Generalized socket address structure.
Definition: socket.h:97
sa_family_t sa_family
Network address family.
Definition: tcpip.h:145
A network device.
Definition: netdevice.h:353
size_t header_len
Fixed header length.
Definition: tcpip.h:147
unsigned char uint8_t
Definition: stdint.h:10
int tcpip_rx(struct io_buffer *iobuf, struct net_device *netdev, uint8_t tcpip_proto, struct sockaddr_tcpip *st_src, struct sockaddr_tcpip *st_dest, uint16_t pshdr_csum, struct ip_statistics *stats)
Process a received TCP/IP packet.
Definition: tcpip.c:41
struct net_device * tcpip_netdev(struct sockaddr_tcpip *st_dest)
Determine transmitting network device.
Definition: tcpip.c:115
A network-layer protocol.
Definition: netdevice.h:65
A transport-layer protocol of the TCP/IP stack (eg.
Definition: tcpip.h:105
uint16_t zero_csum
Preferred zero checksum value.
Definition: tcpip.h:129
uint16_t tcpip_chksum(const void *data, size_t len)
Calculate TCP/IP checkum.
Definition: tcpip.c:204
FILE_SECBOOT(PERMITTED)
if(len >=6 *4) __asm__ __volatile__("movsl" if(len >=5 *4) __asm__ __volatile__("movsl" if(len >=4 *4) __asm__ __volatile__("movsl" if(len >=3 *4) __asm__ __volatile__("movsl" if(len >=2 *4) __asm__ __volatile__("movsl" if(len >=1 *4) __asm__ __volatile__("movsl" if((len % 4) >=2) __asm__ __volatile__("movsw" if((len % 2) >=1) __asm__ __volatile__("movsb" return dest
Definition: string.h:151
Linker tables.
uint16_t st_flags
Flags.
Definition: tcpip.h:80
int(* tx)(struct io_buffer *iobuf, struct tcpip_protocol *tcpip_protocol, struct sockaddr_tcpip *st_src, struct sockaddr_tcpip *st_dest, struct net_device *netdev, uint16_t *trans_csum)
Transmit packet.
Definition: tcpip.h:163
int(* rx)(struct io_buffer *iobuf, struct net_device *netdev, struct sockaddr_tcpip *st_src, struct sockaddr_tcpip *st_dest, uint16_t pshdr_csum)
Process received packet.
Definition: tcpip.h:120
A network-layer protocol of the TCP/IP stack (eg.
Definition: tcpip.h:141
Socket addresses.
const char * name
Protocol name.
Definition: tcpip.h:143
A persistent I/O buffer.
Definition: iobuf.h:38
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)