tcpip.c
Go to the documentation of this file.00001 #include <stdint.h>
00002 #include <string.h>
00003 #include <errno.h>
00004 #include <byteswap.h>
00005 #include <gpxe/iobuf.h>
00006 #include <gpxe/tables.h>
00007 #include <gpxe/tcpip.h>
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 FILE_LICENCE ( GPL2_OR_LATER );
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 int tcpip_rx ( struct io_buffer *iobuf, uint8_t tcpip_proto,
00035 struct sockaddr_tcpip *st_src,
00036 struct sockaddr_tcpip *st_dest,
00037 uint16_t pshdr_csum ) {
00038 struct tcpip_protocol *tcpip;
00039
00040
00041 for_each_table_entry ( tcpip, TCPIP_PROTOCOLS ) {
00042 if ( tcpip->tcpip_proto == tcpip_proto ) {
00043 DBG ( "TCP/IP received %s packet\n", tcpip->name );
00044 return tcpip->rx ( iobuf, st_src, st_dest, pshdr_csum );
00045 }
00046 }
00047
00048 DBG ( "Unrecognised TCP/IP protocol %d\n", tcpip_proto );
00049 free_iob ( iobuf );
00050 return -EPROTONOSUPPORT;
00051 }
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 int tcpip_tx ( struct io_buffer *iobuf, struct tcpip_protocol *tcpip_protocol,
00064 struct sockaddr_tcpip *st_src, struct sockaddr_tcpip *st_dest,
00065 struct net_device *netdev, uint16_t *trans_csum ) {
00066 struct tcpip_net_protocol *tcpip_net;
00067
00068
00069 for_each_table_entry ( tcpip_net, TCPIP_NET_PROTOCOLS ) {
00070 if ( tcpip_net->sa_family == st_dest->st_family ) {
00071 DBG ( "TCP/IP sending %s packet\n", tcpip_net->name );
00072 return tcpip_net->tx ( iobuf, tcpip_protocol, st_src,
00073 st_dest, netdev, trans_csum );
00074 }
00075 }
00076
00077 DBG ( "Unrecognised TCP/IP address family %d\n", st_dest->st_family );
00078 free_iob ( iobuf );
00079 return -EAFNOSUPPORT;
00080 }
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100 uint16_t tcpip_continue_chksum ( uint16_t partial, const void *data,
00101 size_t len ) {
00102 unsigned int cksum = ( ( ~partial ) & 0xffff );
00103 unsigned int value;
00104 unsigned int i;
00105
00106 for ( i = 0 ; i < len ; i++ ) {
00107 value = * ( ( uint8_t * ) data + i );
00108 if ( i & 1 ) {
00109
00110 value = be16_to_cpu ( value );
00111 } else {
00112
00113 value = le16_to_cpu ( value );
00114 }
00115 cksum += value;
00116 if ( cksum > 0xffff )
00117 cksum -= 0xffff;
00118 }
00119
00120 return ( ~cksum );
00121 }
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133 uint16_t tcpip_chksum ( const void *data, size_t len ) {
00134 return tcpip_continue_chksum ( TCPIP_EMPTY_CSUM, data, len );
00135 }