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_if.c 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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_if.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 internet checksum
  96. *
  97. * @v data Pointer to the data
  98. * @v len Length of data to be checksummed
  99. * @ret chksum 16 bit internet checksum
  100. *
  101. * This function calculates the internet checksum (refer RFC1071) for "len"
  102. * bytes beginning at the location "data"
  103. */
  104. uint16_t calc_chksum ( void *data, size_t len ) {
  105. register long sum = 0;
  106. uint16_t checksum;
  107. unsigned short *temp;
  108. while ( len > 1 ) {
  109. temp = (unsigned short*) data++;
  110. sum += *temp;
  111. len -= 2;
  112. }
  113. if ( len > 0 ) {
  114. sum += *(unsigned char *)data;
  115. }
  116. while ( sum >> 16 ) {
  117. sum = ( sum & 0xffff ) + ( sum >> 16 );
  118. }
  119. checksum = ~sum;
  120. return checksum;
  121. }