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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include <stdint.h>
  2. #include <string.h>
  3. #include <errno.h>
  4. #include <malloc.h>
  5. #include <byteswap.h>
  6. #include <gpxe/in.h>
  7. #include <gpxe/ip.h>
  8. #include <gpxe/pkbuff.h>
  9. #include <gpxe/tables.h>
  10. #include <gpxe/netdevice.h>
  11. #include <gpxe/tcpip.h>
  12. /** @file
  13. *
  14. * Transport-network layer interface
  15. *
  16. * This file contains functions and utilities for the transport-network layer interface
  17. */
  18. /** Registered network-layer protocols that support TCPIP */
  19. static struct tcpip_net_protocol tcpip_net_protocols[0] __table_start ( tcpip_net_protocols );
  20. static struct tcpip_net_protocol tcpip_net_protocols_end[0] __table_end ( tcpip_net_protocols );
  21. struct tcpip_protocol;
  22. /** Registered transport-layer protocols that support TCPIP */
  23. static struct tcpip_protocol tcpip_protocols[0] __table_start ( tcpip_protocols );
  24. static struct tcpip_protocol tcpip_protocols_end[0] __table_end ( tcpip_protocols );
  25. /** Identify TCPIP network-layer protocol
  26. *
  27. * @v sa_family Network address family
  28. * @ret tcpip Protocol supporting TCPIP, or NULL
  29. */
  30. static struct tcpip_net_protocol * tcpip_find_protocol ( sa_family_t sa_family ) {
  31. struct tcpip_net_protocol *tcpip_net;
  32. for ( tcpip_net = tcpip_net_protocols;
  33. tcpip_net < tcpip_net_protocols_end; ++tcpip_net ) {
  34. if ( tcpip_net->sa_family == sa_family ) {
  35. return tcpip_net;
  36. }
  37. }
  38. return NULL;
  39. }
  40. /** Identify TCPIP transport-layer protocol
  41. *
  42. * @v trans_proto Transport-layer protocol number, IP_XXX
  43. * @ret tcpip_protocol Transport-layer protocol, or NULL
  44. */
  45. struct tcpip_protocol* find_tcpip_protocol ( uint8_t trans_proto ) {
  46. struct tcpip_protocol *tcpip;
  47. for ( tcpip = tcpip_protocols; tcpip <= tcpip_protocols_end;
  48. ++tcpip ) {
  49. if ( tcpip->trans_proto == trans_proto ) {
  50. return tcpip;
  51. }
  52. }
  53. return NULL;
  54. }
  55. /** Process a received packet
  56. *
  57. * @v pkb Packet buffer
  58. * @v trans_proto Transport-layer protocol number
  59. * @v src Source network-layer address
  60. * @v dest Destination network-layer address
  61. *
  62. * This function expects a transport-layer segment from the network-layer
  63. */
  64. void trans_rx ( struct pk_buff *pkb, uint8_t trans_proto, struct in_addr *src,
  65. struct in_addr *dest ) {
  66. struct tcpip_protocol *tcpip;
  67. /* Identify the transport layer protocol */
  68. for ( tcpip = tcpip_protocols; tcpip <= tcpip_protocols_end; ++tcpip ) {
  69. if ( tcpip->trans_proto == trans_proto ) {
  70. DBG ( "Packet sent to %s module", tcpip->name );
  71. tcpip->rx ( pkb, src, dest );
  72. }
  73. }
  74. }
  75. /** Transmit a transport-layer segment
  76. *
  77. * @v pkb Packet buffer
  78. * @v trans_proto Transport-layer protocol
  79. * @v sock Destination socket address
  80. * @ret Status
  81. */
  82. int trans_tx ( struct pk_buff *pkb, struct tcpip_protocol *tcpip,
  83. struct sockaddr *sock ) {
  84. /* Identify the network layer protocol and send it using xxx_tx() */
  85. switch ( sock->sa_family ) {
  86. case AF_INET: /* IPv4 network family */
  87. return ipv4_tx ( pkb, tcpip, &sock->sin.sin_addr );
  88. case AF_INET6: /* IPv6 network family */
  89. return ipv6_tx ( pkb, tcpip, &sock->sin6.sin6_addr );
  90. }
  91. DBG ( "Network family %d not supported", sock->sa_family );
  92. return -EAFNOSUPPORT;
  93. }
  94. /**
  95. * Calculate continued TCP/IP checkum
  96. *
  97. * @v partial Checksum of already-summed data, in network byte order
  98. * @v data Data buffer
  99. * @v len Length of data buffer
  100. * @ret cksum Updated checksum, in network byte order
  101. *
  102. * Calculates a TCP/IP-style 16-bit checksum over the data block. The
  103. * checksum is returned in network byte order.
  104. *
  105. * This function may be used to add new data to an existing checksum.
  106. * The function assumes that both the old data and the new data start
  107. * on even byte offsets; if this is not the case then you will need to
  108. * byte-swap either the input partial checksum, the output checksum,
  109. * or both. Deciding which to swap is left as an exercise for the
  110. * interested reader.
  111. */
  112. unsigned int tcpip_continue_chksum ( unsigned int partial, const void *data,
  113. size_t len ) {
  114. unsigned int cksum = ( ( ~partial ) & 0xffff );
  115. unsigned int value;
  116. unsigned int i;
  117. for ( i = 0 ; i < len ; i++ ) {
  118. value = * ( ( uint8_t * ) data + i );
  119. if ( i & 1 ) {
  120. /* Odd bytes: swap on little-endian systems */
  121. value = be16_to_cpu ( value );
  122. } else {
  123. /* Even bytes: swap on big-endian systems */
  124. value = le16_to_cpu ( value );
  125. }
  126. cksum += value;
  127. if ( cksum > 0xffff )
  128. cksum -= 0xffff;
  129. }
  130. return ( ( ~cksum ) & 0xffff );
  131. }
  132. /**
  133. * Calculate TCP/IP checkum
  134. *
  135. * @v data Data buffer
  136. * @v len Length of data buffer
  137. * @ret cksum Checksum, in network byte order
  138. *
  139. * Calculates a TCP/IP-style 16-bit checksum over the data block. The
  140. * checksum is returned in network byte order.
  141. */
  142. unsigned int tcpip_chksum ( const void *data, size_t len ) {
  143. return tcpip_continue_chksum ( 0xffff, data, len );
  144. }