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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. /** Registered transport-layer protocols that support TCPIP */
  22. static struct tcpip_protocol tcpip_protocols[0] __table_start ( tcpip_protocols );
  23. static struct tcpip_protocol tcpip_protocols_end[0] __table_end ( tcpip_protocols );
  24. /** Process a received packet
  25. *
  26. * @v pkb Packet buffer
  27. * @v trans_proto Transport-layer protocol number
  28. * @v src Source network-layer address
  29. * @v dest Destination network-layer address
  30. *
  31. * This function expects a transport-layer segment from the network-layer
  32. */
  33. void tcpip_rx ( struct pk_buff *pkb, uint8_t trans_proto, struct in_addr *src,
  34. struct in_addr *dest ) {
  35. struct tcpip_protocol *tcpip;
  36. /* Identify the transport layer protocol */
  37. for ( tcpip = tcpip_protocols; tcpip <= tcpip_protocols_end; ++tcpip ) {
  38. if ( tcpip->trans_proto == trans_proto ) {
  39. DBG ( "Packet sent to %s module", tcpip->name );
  40. tcpip->rx ( pkb, src, dest );
  41. }
  42. }
  43. }
  44. /** Transmit a transport-layer segment
  45. *
  46. * @v pkb Packet buffer
  47. * @v trans_proto Transport-layer protocol
  48. * @v sock Destination socket address
  49. * @ret Status
  50. */
  51. int tcpip_tx ( struct pk_buff *pkb, struct tcpip_protocol *tcpip,
  52. struct sockaddr *sock ) {
  53. /* Identify the network layer protocol and send it using xxx_tx() */
  54. switch ( sock->sa_family ) {
  55. case AF_INET: /* IPv4 network family */
  56. return ipv4_tx ( pkb, tcpip, &sock->sin.sin_addr );
  57. case AF_INET6: /* IPv6 network family */
  58. return ipv6_tx ( pkb, tcpip, &sock->sin6.sin6_addr );
  59. }
  60. DBG ( "Network family %d not supported", sock->sa_family );
  61. return -EAFNOSUPPORT;
  62. }
  63. /**
  64. * Calculate continued TCP/IP checkum
  65. *
  66. * @v partial Checksum of already-summed data, in network byte order
  67. * @v data Data buffer
  68. * @v len Length of data buffer
  69. * @ret cksum Updated checksum, in network byte order
  70. *
  71. * Calculates a TCP/IP-style 16-bit checksum over the data block. The
  72. * checksum is returned in network byte order.
  73. *
  74. * This function may be used to add new data to an existing checksum.
  75. * The function assumes that both the old data and the new data start
  76. * on even byte offsets; if this is not the case then you will need to
  77. * byte-swap either the input partial checksum, the output checksum,
  78. * or both. Deciding which to swap is left as an exercise for the
  79. * interested reader.
  80. */
  81. unsigned int tcpip_continue_chksum ( unsigned int partial, const void *data,
  82. size_t len ) {
  83. unsigned int cksum = ( ( ~partial ) & 0xffff );
  84. unsigned int value;
  85. unsigned int i;
  86. for ( i = 0 ; i < len ; i++ ) {
  87. value = * ( ( uint8_t * ) data + i );
  88. if ( i & 1 ) {
  89. /* Odd bytes: swap on little-endian systems */
  90. value = be16_to_cpu ( value );
  91. } else {
  92. /* Even bytes: swap on big-endian systems */
  93. value = le16_to_cpu ( value );
  94. }
  95. cksum += value;
  96. if ( cksum > 0xffff )
  97. cksum -= 0xffff;
  98. }
  99. return ( ( ~cksum ) & 0xffff );
  100. }
  101. /**
  102. * Calculate TCP/IP checkum
  103. *
  104. * @v data Data buffer
  105. * @v len Length of data buffer
  106. * @ret cksum Checksum, in network byte order
  107. *
  108. * Calculates a TCP/IP-style 16-bit checksum over the data block. The
  109. * checksum is returned in network byte order.
  110. */
  111. unsigned int tcpip_chksum ( const void *data, size_t len ) {
  112. return tcpip_continue_chksum ( 0xffff, data, len );
  113. }