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