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.3KB

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