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 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. /** Empty checksum value
  18. *
  19. * This is the TCP/IP checksum over a zero-length block of data.
  20. */
  21. #define TCPIP_EMPTY_CSUM 0xffff
  22. /** TCP/IP address flags */
  23. enum tcpip_st_flags {
  24. /** Bind to a privileged port (less than 1024)
  25. *
  26. * This value is chosen as 1024 to optimise the calculations
  27. * in tcpip_bind().
  28. */
  29. TCPIP_BIND_PRIVILEGED = 0x0400,
  30. };
  31. /**
  32. * TCP/IP socket address
  33. *
  34. * This contains the fields common to socket addresses for all TCP/IP
  35. * address families.
  36. */
  37. struct sockaddr_tcpip {
  38. /** Socket address family (part of struct @c sockaddr) */
  39. sa_family_t st_family;
  40. /** Flags */
  41. uint16_t st_flags;
  42. /** TCP/IP port */
  43. uint16_t st_port;
  44. /** Padding
  45. *
  46. * This ensures that a struct @c sockaddr_tcpip is large
  47. * enough to hold a socket address for any TCP/IP address
  48. * family.
  49. */
  50. char pad[ sizeof ( struct sockaddr ) -
  51. ( sizeof ( sa_family_t ) /* st_family */ +
  52. sizeof ( uint16_t ) /* st_flags */ +
  53. sizeof ( uint16_t ) /* st_port */ ) ];
  54. } __attribute__ (( packed, may_alias ));
  55. /**
  56. * A transport-layer protocol of the TCP/IP stack (eg. UDP, TCP, etc)
  57. */
  58. struct tcpip_protocol {
  59. /** Protocol name */
  60. const char *name;
  61. /**
  62. * Process received packet
  63. *
  64. * @v iobuf I/O buffer
  65. * @v netdev Network device
  66. * @v st_src Partially-filled source address
  67. * @v st_dest Partially-filled destination address
  68. * @v pshdr_csum Pseudo-header checksum
  69. * @ret rc Return status code
  70. *
  71. * This method takes ownership of the I/O buffer.
  72. */
  73. int ( * rx ) ( struct io_buffer *iobuf, struct net_device *netdev,
  74. struct sockaddr_tcpip *st_src,
  75. struct sockaddr_tcpip *st_dest, uint16_t pshdr_csum );
  76. /**
  77. * Transport-layer protocol number
  78. *
  79. * This is a constant of the type IP_XXX
  80. */
  81. uint8_t tcpip_proto;
  82. };
  83. /**
  84. * A network-layer protocol of the TCP/IP stack (eg. IPV4, IPv6, etc)
  85. */
  86. struct tcpip_net_protocol {
  87. /** Protocol name */
  88. const char *name;
  89. /** Network address family */
  90. sa_family_t sa_family;
  91. /** Fixed header length */
  92. size_t header_len;
  93. /**
  94. * Transmit packet
  95. *
  96. * @v iobuf I/O buffer
  97. * @v tcpip_protocol Transport-layer protocol
  98. * @v st_src Source address, or NULL to use default
  99. * @v st_dest Destination address
  100. * @v netdev Network device (or NULL to route automatically)
  101. * @v trans_csum Transport-layer checksum to complete, or NULL
  102. * @ret rc Return status code
  103. *
  104. * This function takes ownership of the I/O buffer.
  105. */
  106. int ( * tx ) ( struct io_buffer *iobuf,
  107. struct tcpip_protocol *tcpip_protocol,
  108. struct sockaddr_tcpip *st_src,
  109. struct sockaddr_tcpip *st_dest,
  110. struct net_device *netdev,
  111. uint16_t *trans_csum );
  112. /**
  113. * Determine transmitting network device
  114. *
  115. * @v st_dest Destination address
  116. * @ret netdev Network device, or NULL
  117. */
  118. struct net_device * ( * netdev ) ( struct sockaddr_tcpip *dest );
  119. };
  120. /** TCP/IP transport-layer protocol table */
  121. #define TCPIP_PROTOCOLS __table ( struct tcpip_protocol, "tcpip_protocols" )
  122. /** Declare a TCP/IP transport-layer protocol */
  123. #define __tcpip_protocol __table_entry ( TCPIP_PROTOCOLS, 01 )
  124. /** TCP/IP network-layer protocol table */
  125. #define TCPIP_NET_PROTOCOLS \
  126. __table ( struct tcpip_net_protocol, "tcpip_net_protocols" )
  127. /** Declare a TCP/IP network-layer protocol */
  128. #define __tcpip_net_protocol __table_entry ( TCPIP_NET_PROTOCOLS, 01 )
  129. extern int tcpip_rx ( struct io_buffer *iobuf, struct net_device *netdev,
  130. uint8_t tcpip_proto, struct sockaddr_tcpip *st_src,
  131. struct sockaddr_tcpip *st_dest, uint16_t pshdr_csum,
  132. struct ip_statistics *stats );
  133. extern int tcpip_tx ( struct io_buffer *iobuf, struct tcpip_protocol *tcpip,
  134. struct sockaddr_tcpip *st_src,
  135. struct sockaddr_tcpip *st_dest,
  136. struct net_device *netdev,
  137. uint16_t *trans_csum );
  138. extern struct net_device * tcpip_netdev ( struct sockaddr_tcpip *st_dest );
  139. extern size_t tcpip_mtu ( struct sockaddr_tcpip *st_dest );
  140. extern uint16_t generic_tcpip_continue_chksum ( uint16_t partial,
  141. const void *data, size_t len );
  142. extern uint16_t tcpip_chksum ( const void *data, size_t len );
  143. extern int tcpip_bind ( struct sockaddr_tcpip *st_local,
  144. int ( * available ) ( int port ) );
  145. /* Use generic_tcpip_continue_chksum() if no architecture-specific
  146. * version is available
  147. */
  148. #ifndef tcpip_continue_chksum
  149. #define tcpip_continue_chksum generic_tcpip_continue_chksum
  150. #endif
  151. #endif /* _IPXE_TCPIP_H */