You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tcpip.h 6.6KB

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