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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include <stdint.h>
  2. #include <string.h>
  3. #include <errno.h>
  4. #include <byteswap.h>
  5. #include <gpxe/iobuf.h>
  6. #include <gpxe/tables.h>
  7. #include <gpxe/tcpip.h>
  8. /** @file
  9. *
  10. * Transport-network layer interface
  11. *
  12. * This file contains functions and utilities for the
  13. * TCP/IP transport-network layer interface
  14. */
  15. /** Registered network-layer protocols that support TCP/IP */
  16. static struct tcpip_net_protocol tcpip_net_protocols[0]
  17. __table_start ( struct tcpip_net_protocol, tcpip_net_protocols );
  18. static struct tcpip_net_protocol tcpip_net_protocols_end[0]
  19. __table_end ( struct tcpip_net_protocol, tcpip_net_protocols );
  20. /** Registered transport-layer protocols that support TCP/IP */
  21. static struct tcpip_protocol tcpip_protocols[0]
  22. __table_start ( struct tcpip_protocol, tcpip_protocols );
  23. static struct tcpip_protocol tcpip_protocols_end[0]
  24. __table_end ( struct tcpip_protocol, tcpip_protocols );
  25. /** Process a received TCP/IP packet
  26. *
  27. * @v iobuf I/O buffer
  28. * @v tcpip_proto Transport-layer protocol number
  29. * @v st_src Partially-filled source address
  30. * @v st_dest Partially-filled destination address
  31. * @v pshdr_csum Pseudo-header checksum
  32. * @ret rc Return status code
  33. *
  34. * This function expects a transport-layer segment from the network
  35. * layer. The network layer should fill in as much as it can of the
  36. * source and destination addresses (i.e. it should fill in the
  37. * address family and the network-layer addresses, but leave the ports
  38. * and the rest of the structures as zero).
  39. */
  40. int tcpip_rx ( struct io_buffer *iobuf, uint8_t tcpip_proto,
  41. struct sockaddr_tcpip *st_src,
  42. struct sockaddr_tcpip *st_dest,
  43. uint16_t pshdr_csum ) {
  44. struct tcpip_protocol *tcpip;
  45. /* Hand off packet to the appropriate transport-layer protocol */
  46. for ( tcpip = tcpip_protocols; tcpip < tcpip_protocols_end; tcpip++ ) {
  47. if ( tcpip->tcpip_proto == tcpip_proto ) {
  48. DBG ( "TCP/IP received %s packet\n", tcpip->name );
  49. return tcpip->rx ( iobuf, st_src, st_dest, pshdr_csum );
  50. }
  51. }
  52. DBG ( "Unrecognised TCP/IP protocol %d\n", tcpip_proto );
  53. free_iob ( iobuf );
  54. return -EPROTONOSUPPORT;
  55. }
  56. /** Transmit a TCP/IP packet
  57. *
  58. * @v iobuf I/O buffer
  59. * @v tcpip_protocol Transport-layer protocol
  60. * @v st_src Source address, or NULL to use route default
  61. * @v st_dest Destination address
  62. * @v netdev Network device to use if no route found, or NULL
  63. * @v trans_csum Transport-layer checksum to complete, or NULL
  64. * @ret rc Return status code
  65. */
  66. int tcpip_tx ( struct io_buffer *iobuf, struct tcpip_protocol *tcpip_protocol,
  67. struct sockaddr_tcpip *st_src, struct sockaddr_tcpip *st_dest,
  68. struct net_device *netdev, uint16_t *trans_csum ) {
  69. struct tcpip_net_protocol *tcpip_net;
  70. /* Hand off packet to the appropriate network-layer protocol */
  71. for ( tcpip_net = tcpip_net_protocols ;
  72. tcpip_net < tcpip_net_protocols_end ; tcpip_net++ ) {
  73. if ( tcpip_net->sa_family == st_dest->st_family ) {
  74. DBG ( "TCP/IP sending %s packet\n", tcpip_net->name );
  75. return tcpip_net->tx ( iobuf, tcpip_protocol, st_src,
  76. st_dest, netdev, trans_csum );
  77. }
  78. }
  79. DBG ( "Unrecognised TCP/IP address family %d\n", st_dest->st_family );
  80. free_iob ( iobuf );
  81. return -EAFNOSUPPORT;
  82. }
  83. /**
  84. * Calculate continued TCP/IP checkum
  85. *
  86. * @v partial Checksum of already-summed data, in network byte order
  87. * @v data Data buffer
  88. * @v len Length of data buffer
  89. * @ret cksum Updated checksum, in network byte order
  90. *
  91. * Calculates a TCP/IP-style 16-bit checksum over the data block. The
  92. * checksum is returned in network byte order.
  93. *
  94. * This function may be used to add new data to an existing checksum.
  95. * The function assumes that both the old data and the new data start
  96. * on even byte offsets; if this is not the case then you will need to
  97. * byte-swap either the input partial checksum, the output checksum,
  98. * or both. Deciding which to swap is left as an exercise for the
  99. * interested reader.
  100. */
  101. uint16_t tcpip_continue_chksum ( uint16_t partial, const void *data,
  102. size_t len ) {
  103. unsigned int cksum = ( ( ~partial ) & 0xffff );
  104. unsigned int value;
  105. unsigned int i;
  106. for ( i = 0 ; i < len ; i++ ) {
  107. value = * ( ( uint8_t * ) data + i );
  108. if ( i & 1 ) {
  109. /* Odd bytes: swap on little-endian systems */
  110. value = be16_to_cpu ( value );
  111. } else {
  112. /* Even bytes: swap on big-endian systems */
  113. value = le16_to_cpu ( value );
  114. }
  115. cksum += value;
  116. if ( cksum > 0xffff )
  117. cksum -= 0xffff;
  118. }
  119. return ( ~cksum );
  120. }
  121. /**
  122. * Calculate TCP/IP checkum
  123. *
  124. * @v data Data buffer
  125. * @v len Length of data buffer
  126. * @ret cksum Checksum, in network byte order
  127. *
  128. * Calculates a TCP/IP-style 16-bit checksum over the data block. The
  129. * checksum is returned in network byte order.
  130. */
  131. uint16_t tcpip_chksum ( const void *data, size_t len ) {
  132. return tcpip_continue_chksum ( TCPIP_EMPTY_CSUM, data, len );
  133. }