Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

tcpip.c 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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_dest Destination address
  61. * @v netdev Network device to use if no route found, or NULL
  62. * @v trans_csum Transport-layer checksum to complete, or NULL
  63. * @ret rc Return status code
  64. */
  65. int tcpip_tx ( struct io_buffer *iobuf, struct tcpip_protocol *tcpip_protocol,
  66. struct sockaddr_tcpip *st_dest, struct net_device *netdev,
  67. uint16_t *trans_csum ) {
  68. struct tcpip_net_protocol *tcpip_net;
  69. /* Hand off packet to the appropriate network-layer protocol */
  70. for ( tcpip_net = tcpip_net_protocols ;
  71. tcpip_net < tcpip_net_protocols_end ; tcpip_net++ ) {
  72. if ( tcpip_net->sa_family == st_dest->st_family ) {
  73. DBG ( "TCP/IP sending %s packet\n", tcpip_net->name );
  74. return tcpip_net->tx ( iobuf, tcpip_protocol, st_dest,
  75. netdev, trans_csum );
  76. }
  77. }
  78. DBG ( "Unrecognised TCP/IP address family %d\n", st_dest->st_family );
  79. free_iob ( iobuf );
  80. return -EAFNOSUPPORT;
  81. }
  82. /**
  83. * Calculate continued TCP/IP checkum
  84. *
  85. * @v partial Checksum of already-summed data, in network byte order
  86. * @v data Data buffer
  87. * @v len Length of data buffer
  88. * @ret cksum Updated checksum, in network byte order
  89. *
  90. * Calculates a TCP/IP-style 16-bit checksum over the data block. The
  91. * checksum is returned in network byte order.
  92. *
  93. * This function may be used to add new data to an existing checksum.
  94. * The function assumes that both the old data and the new data start
  95. * on even byte offsets; if this is not the case then you will need to
  96. * byte-swap either the input partial checksum, the output checksum,
  97. * or both. Deciding which to swap is left as an exercise for the
  98. * interested reader.
  99. */
  100. uint16_t tcpip_continue_chksum ( uint16_t partial, const void *data,
  101. size_t len ) {
  102. unsigned int cksum = ( ( ~partial ) & 0xffff );
  103. unsigned int value;
  104. unsigned int i;
  105. for ( i = 0 ; i < len ; i++ ) {
  106. value = * ( ( uint8_t * ) data + i );
  107. if ( i & 1 ) {
  108. /* Odd bytes: swap on little-endian systems */
  109. value = be16_to_cpu ( value );
  110. } else {
  111. /* Even bytes: swap on big-endian systems */
  112. value = le16_to_cpu ( value );
  113. }
  114. cksum += value;
  115. if ( cksum > 0xffff )
  116. cksum -= 0xffff;
  117. }
  118. return ( ~cksum );
  119. }
  120. /**
  121. * Calculate TCP/IP checkum
  122. *
  123. * @v data Data buffer
  124. * @v len Length of data buffer
  125. * @ret cksum Checksum, in network byte order
  126. *
  127. * Calculates a TCP/IP-style 16-bit checksum over the data block. The
  128. * checksum is returned in network byte order.
  129. */
  130. uint16_t tcpip_chksum ( const void *data, size_t len ) {
  131. return tcpip_continue_chksum ( TCPIP_EMPTY_CSUM, data, len );
  132. }