iPXE
tcpip.h
Go to the documentation of this file.
00001 #ifndef _IPXE_TCPIP_H
00002 #define _IPXE_TCPIP_H
00003 
00004 /** @file
00005  *
00006  * Transport-network layer interface
00007  *
00008  */
00009 
00010 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00011 
00012 #include <stdint.h>
00013 #include <ipxe/socket.h>
00014 #include <ipxe/in.h>
00015 #include <ipxe/tables.h>
00016 
00017 extern uint16_t generic_tcpip_continue_chksum ( uint16_t partial,
00018                                                 const void *data, size_t len );
00019 
00020 #include <bits/tcpip.h>
00021 
00022 struct io_buffer;
00023 struct net_device;
00024 struct ip_statistics;
00025 
00026 /** Positive zero checksum value */
00027 #define TCPIP_POSITIVE_ZERO_CSUM 0x0000
00028 
00029 /** Negative zero checksum value */
00030 #define TCPIP_NEGATIVE_ZERO_CSUM 0xffff
00031 
00032 /** Empty checksum value
00033  *
00034  * All of our TCP/IP checksum algorithms will return only the positive
00035  * representation of zero (0x0000) for a zero checksum over non-zero
00036  * input data.  This property arises since the end-around carry used
00037  * to mimic one's complement addition using unsigned arithmetic
00038  * prevents the running total from ever returning to 0x0000.  The
00039  * running total will therefore use only the negative representation
00040  * of zero (0xffff).  Since the return value is the one's complement
00041  * negation of the running total (calculated by simply bit-inverting
00042  * the running total), the return value will therefore use only the
00043  * positive representation of zero (0x0000).
00044  *
00045  * It is a very common misconception (found in many places such as
00046  * RFC1624) that this is a property guaranteed by the underlying
00047  * mathematics.  It is not; the choice of which zero representation is
00048  * used is merely an artifact of the software implementation of the
00049  * checksum algorithm.
00050  *
00051  * For consistency, we choose to use the positive representation of
00052  * zero (0x0000) for the checksum of a zero-length block of data.
00053  * This ensures that all of our TCP/IP checksum algorithms will return
00054  * only the positive representation of zero (0x0000) for a zero
00055  * checksum (regardless of the input data).
00056  */
00057 #define TCPIP_EMPTY_CSUM TCPIP_POSITIVE_ZERO_CSUM
00058 
00059 /** TCP/IP address flags */
00060 enum tcpip_st_flags {
00061         /** Bind to a privileged port (less than 1024)
00062          *
00063          * This value is chosen as 1024 to optimise the calculations
00064          * in tcpip_bind().
00065          */
00066         TCPIP_BIND_PRIVILEGED = 0x0400,
00067 };
00068 
00069 /**
00070  * TCP/IP socket address
00071  *
00072  * This contains the fields common to socket addresses for all TCP/IP
00073  * address families.
00074  */
00075 struct sockaddr_tcpip {
00076         /** Socket address family (part of struct @c sockaddr) */
00077         sa_family_t st_family;
00078         /** Flags */
00079         uint16_t st_flags;
00080         /** TCP/IP port */
00081         uint16_t st_port;
00082         /** Scope ID
00083          *
00084          * For link-local or multicast addresses, this is the network
00085          * device index.
00086          */
00087         uint16_t st_scope_id;
00088         /** Padding
00089          *
00090          * This ensures that a struct @c sockaddr_tcpip is large
00091          * enough to hold a socket address for any TCP/IP address
00092          * family.
00093          */
00094         char pad[ sizeof ( struct sockaddr ) -
00095                   ( sizeof ( sa_family_t ) /* st_family */ +
00096                     sizeof ( uint16_t ) /* st_flags */ +
00097                     sizeof ( uint16_t ) /* st_port */ +
00098                     sizeof ( uint16_t ) /* st_scope_id */ ) ];
00099 } __attribute__ (( packed, may_alias ));
00100 
00101 /** 
00102  * A transport-layer protocol of the TCP/IP stack (eg. UDP, TCP, etc)
00103  */
00104 struct tcpip_protocol {
00105         /** Protocol name */
00106         const char *name;
00107         /**
00108          * Process received packet
00109          *
00110          * @v iobuf             I/O buffer
00111          * @v netdev            Network device
00112          * @v st_src            Partially-filled source address
00113          * @v st_dest           Partially-filled destination address
00114          * @v pshdr_csum        Pseudo-header checksum
00115          * @ret rc              Return status code
00116          *
00117          * This method takes ownership of the I/O buffer.
00118          */
00119         int ( * rx ) ( struct io_buffer *iobuf, struct net_device *netdev,
00120                        struct sockaddr_tcpip *st_src,
00121                        struct sockaddr_tcpip *st_dest, uint16_t pshdr_csum );
00122         /** Preferred zero checksum value
00123          *
00124          * The checksum is a one's complement value: zero may be
00125          * represented by either positive zero (0x0000) or negative
00126          * zero (0xffff).
00127          */
00128         uint16_t zero_csum;
00129         /** 
00130          * Transport-layer protocol number
00131          *
00132          * This is a constant of the type IP_XXX
00133          */
00134         uint8_t tcpip_proto;
00135 };
00136 
00137 /**
00138  * A network-layer protocol of the TCP/IP stack (eg. IPV4, IPv6, etc)
00139  */
00140 struct tcpip_net_protocol {
00141         /** Protocol name */
00142         const char *name;
00143         /** Network address family */
00144         sa_family_t sa_family;
00145         /** Fixed header length */
00146         size_t header_len;
00147         /** Network-layer protocol */
00148         struct net_protocol *net_protocol;
00149         /**
00150          * Transmit packet
00151          *
00152          * @v iobuf             I/O buffer
00153          * @v tcpip_protocol    Transport-layer protocol
00154          * @v st_src            Source address, or NULL to use default
00155          * @v st_dest           Destination address
00156          * @v netdev            Network device (or NULL to route automatically)
00157          * @v trans_csum        Transport-layer checksum to complete, or NULL
00158          * @ret rc              Return status code
00159          *
00160          * This function takes ownership of the I/O buffer.
00161          */
00162         int ( * tx ) ( struct io_buffer *iobuf,
00163                        struct tcpip_protocol *tcpip_protocol,
00164                        struct sockaddr_tcpip *st_src,
00165                        struct sockaddr_tcpip *st_dest,
00166                        struct net_device *netdev,
00167                        uint16_t *trans_csum );
00168         /**
00169          * Determine transmitting network device
00170          *
00171          * @v st_dest           Destination address
00172          * @ret netdev          Network device, or NULL
00173          */
00174         struct net_device * ( * netdev ) ( struct sockaddr_tcpip *dest );
00175 };
00176 
00177 /** TCP/IP transport-layer protocol table */
00178 #define TCPIP_PROTOCOLS __table ( struct tcpip_protocol, "tcpip_protocols" )
00179 
00180 /** Declare a TCP/IP transport-layer protocol */
00181 #define __tcpip_protocol __table_entry ( TCPIP_PROTOCOLS, 01 )
00182 
00183 /** TCP/IP network-layer protocol table */
00184 #define TCPIP_NET_PROTOCOLS \
00185         __table ( struct tcpip_net_protocol, "tcpip_net_protocols" )
00186 
00187 /** Declare a TCP/IP network-layer protocol */
00188 #define __tcpip_net_protocol __table_entry ( TCPIP_NET_PROTOCOLS, 01 )
00189 
00190 extern int tcpip_rx ( struct io_buffer *iobuf, struct net_device *netdev,
00191                       uint8_t tcpip_proto, struct sockaddr_tcpip *st_src,
00192                       struct sockaddr_tcpip *st_dest, uint16_t pshdr_csum,
00193                       struct ip_statistics *stats );
00194 extern int tcpip_tx ( struct io_buffer *iobuf, struct tcpip_protocol *tcpip,
00195                       struct sockaddr_tcpip *st_src,
00196                       struct sockaddr_tcpip *st_dest,
00197                       struct net_device *netdev,
00198                       uint16_t *trans_csum );
00199 extern struct tcpip_net_protocol * tcpip_net_protocol ( sa_family_t sa_family );
00200 extern struct net_device * tcpip_netdev ( struct sockaddr_tcpip *st_dest );
00201 extern size_t tcpip_mtu ( struct sockaddr_tcpip *st_dest );
00202 extern uint16_t tcpip_chksum ( const void *data, size_t len );
00203 extern int tcpip_bind ( struct sockaddr_tcpip *st_local,
00204                         int ( * available ) ( int port ) );
00205 
00206 #endif /* _IPXE_TCPIP_H */