/*************************************************************************************** File: icmp.c Date: 8.7.2002 Version: 0.1 Author: Jari Lahti (jari@violasystems.com) Description: This file contains functions for processing ICMP Version Info: 8.7.2002 - First version (JaL) 22.1.2004 - Port for MCF5282 26.3.2004 - Modified the packet lenght check (JaL) 01.8.2004 - Port for MCF523x(A19257) ***************************************************************************************/ #include "opentcp.h" /******************************************************************************** Function: ProcessICMPIn Parameters: struct IPFrame* frame - pointer to received IP frame structure UINT16 len - legth of data in bytes Return val: INT16 - (-1) Not OK >0 Packet OK Date: 8.7.2002 Desc: Check and process received ICMP frame *********************************************************************************/ INT16 ProcessICMPIn (struct IPFrame* frame, UINT16 len) { UINT8 type; UINT8 code; UINT16 checksum; UINT16 i; UINT16 j; UINT8 tbuf[16]; /* Is this ICMP? */ DEBUGOUT("Processing ICMP...\n\r"); if( frame->protocol != IP_ICMP ) { DEBUGOUT("ERROR: The protocol is not ICMP\n\r"); return(-1); } /* Calculate checksum for received packet */ i = len; checksum = 0; NETWORK_RECEIVE_INITIALIZE(frame->BufIndex); while(i>15) { RECEIVE_NETWORK_BUF(tbuf, 16); checksum = IpCheckSumBuf(checksum, tbuf,16); i -= 16; } for(j=0; jBufIndex); type = RECEIVE_NETWORK_B(); code = RECEIVE_NETWORK_B(); /* We have already checked the CS, skip it */ RECEIVE_NETWORK_B(); RECEIVE_NETWORK_B(); switch(type) { case ICMP_ECHO_REQUEST: if(code != 0) { DEBUGOUT("ERROR:Misformed ICMP ECHO Request\n\r"); return(-1); } DEBUGOUT("ICMP ECHO Request received\n\r"); /* Is it a packet for setting temporary IP? */ if(len == (ICMP_ECHOREQ_HLEN + ICMP_TEMPIPSET_DATALEN) ) { /* Yep, set temporary IP address */ DEBUGOUT("PING with 102 bytes of data, getting temp. IP\r\n"); localmachine.localip = frame->dip; localmachine.defgw = frame->sip; localmachine.netmask = 0; } /* Same IP? */ if(localmachine.localip != frame->dip) return(-1); /* Reply it */ OTCP_TXBUF[0] = ICMP_ECHO_REPLY; OTCP_TXBUF[1] = 0; OTCP_TXBUF[2] = 0; OTCP_TXBUF[3] = 0; /* Copy with truncate if needed */ if(len > (NETWORK_TX_BUFFER_SIZE - 4)) len = NETWORK_TX_BUFFER_SIZE - 4; RECEIVE_NETWORK_BUF(&OTCP_TXBUF[4], len); /* Calculate Checksum for packet to be sent */ checksum = 0; checksum = IpCheckSumBuf(checksum, &OTCP_TXBUF[0],len); checksum = ~ checksum; /* Put the checksum on place */ OTCP_TXBUF[2] = (UINT8)(checksum>>8); OTCP_TXBUF[3] = (UINT8)checksum; /* Send it */ ProcessIPOut(frame->sip, IP_ICMP, 0, 100, &OTCP_TXBUF[0], len); DEBUGOUT("ICMP Reply sent\n\r"); return(0); break; case ICMP_ECHO_REPLY: break; default: /* Unregognized ICMP message */ DEBUGOUT("Unregognized ICMP message\n\r"); return(-1); } return(-1); }