📄 net_ip.c
字号:
/************************************************************************
*
* NET_IP.c
*
* The 'NET_IP' module implements the IP-layer services to provide
* service access points, which are linked with the 'protocol'-field
* of the IP-header.
*
*
* ######################################################################
*
* Copyright (c) 1999-2000 MIPS Technologies, Inc. All rights reserved.
*
* Unpublished rights reserved under the Copyright Laws of the United States of
* America.
*
* This document contains information that is proprietary to MIPS Technologies,
* Inc. ("MIPS Technologies"). Any copying, modifying or use of this information
* (in whole or in part) which is not expressly permitted in writing by MIPS
* Technologies or a contractually-authorized third party is strictly
* prohibited. At a minimum, this information is protected under unfair
* competition laws and the expression of the information contained herein is
* protected under federal copyright laws. Violations thereof may result in
* criminal penalties and fines.
* MIPS Technologies or any contractually-authorized third party reserves the
* right to change the information contained in this document to improve
* function, design or otherwise. MIPS Technologies does not assume any
* liability arising out of the application or use of this information. Any
* license under patent rights or any other intellectual property rights owned
* by MIPS Technologies or third parties shall be conveyed by MIPS Technologies
* or any contractually-authorized third party in a separate license agreement
* between the parties.
* The information contained in this document constitutes one or more of the
* following: commercial computer software, commercial computer software
* documentation or other commercial items. If the user of this information, or
* any related documentation of any kind, including related technical data or
* manuals, is an agency, department, or other entity of the United States
* government ("Government"), the use, duplication, reproduction, release,
* modification, disclosure, or transfer of this information, or any related
* documentation of any kind, is restricted in accordance with Federal
* Acquisition Regulation 12.212 for civilian agencies and Defense Federal
* Acquisition Regulation Supplement 227.7202 for military agencies. The use of
* this information by the Government is further restricted in accordance with
* the terms of the license agreement(s) and/or applicable contract terms and
* conditions covering this information from MIPS Technologies or any
* contractually-authorized third party.
*
************************************************************************/
/************************************************************************
* Include files
************************************************************************/
#include <string.h>
#include <stdio.h>
#include <sysdefs.h>
#include <syserror.h>
#include <sysdev.h>
#include <syscon_api.h>
/* net stuff */
#include <net_api.h>
#include "net.h"
#include "net_mac.h"
#include "net_arp.h"
#include "net_ip.h"
/************************************************************************
* Definitions
************************************************************************/
/************************************************************************
* Public variables
************************************************************************/
/************************************************************************
* Static variables
************************************************************************/
/* Global state of IP */
static UINT32 NET_IP_state = IP_STATE_CLOSED ;
/* The MAC SP-handle, returned by 'MAC-open' */
static UINT32 mac_sp_hd ;
/* SAP table */
static t_ip_sap_context sap_context[IP_SAP_COUNT] ;
/* Global identification of IP datagrams; implemented as a counter */
static UINT32 NET_IP_identification = 0 ;
/************************************************************************
* Static function prototypes
************************************************************************/
/************************************************************************
*
* NET_IP_receive
* Description :
* -------------
* This function is registered in the MAC-module and linked with the
* MAC-type = '0x800', to let the MAC call us back with an reference
* to the received frame, containing an IP-frame.
* In this function the IP-header will be validated and the IP-SAP
* table will be searched for any match between the actual value
* of the IP-protocol field and a registered user to handle upper
* protocol layer.
*
*
*
* Parameters :
* ------------
* 'src_mac_adr': senders MAC-address
* 'length': length of received ethernet frame
* 'data': pointer for received ethernet frame (in driver's space)
*
*
* Return values :
* ---------------
* 'OK'
*
************************************************************************/
static
UINT32 NET_IP_receive( t_mac_addr *src_mac_adr,
UINT32 length,
UINT8 *data ) ;
/************************************************************************
*
* NET_IP_alloc_sap
* Description :
* -------------
* Allocate a IP SAP, register user context and return a sp-handle.
*
*
* Parameters :
* ------------
*
* 'sap_id', IN, value of IP-'protocol' to bind for
* 'ip_usr_receive', IN, user-receive function to be registered
* 'ip_sp_hd', OUT, handle of IP to be used by user by call
* of 'send' or 'close'
*
*
* Return values :
* ---------------
* 'ERROR_NET_IP_NO_FREE_SAP', No SAP entry could be allocated
* 'OK' = 0x00:
*
*
************************************************************************/
static
UINT32 NET_IP_alloc_sap( UINT8 sap_id,
t_ip_usr_receive ip_usr_receive,
UINT32 *ip_sp_hd ) ;
/************************************************************************
* Implementation : Public functions
************************************************************************/
/************************************************************************
*
* NET_IP_init
* Description :
* -------------
* Initialize the IP module.
*
*
* Parameters :
* ------------
* -
*
*
* Return values :
* ---------------
* 'OK'(=0), successfull initialization
*
************************************************************************/
UINT32 NET_IP_init( void )
{
UINT32 rcode ;
int i ;
/* initialize IP SAP table */
for ( i = 0; i <IP_SAP_COUNT ; i++)
{
sap_context[i].ip_sap_state = IP_SAP_STATE_CLOSED ;
sap_context[i].ip_sap = IP_SAP_UNDEFINED ;
sap_context[i].ip_usr_receive = NULL ;
}
/* IP-module has now been initialized */
NET_IP_state = IP_STATE_INITED ;
return(OK) ;
}
/************************************************************************
*
* NET_IP_open
* Description :
* -------------
* Allocate a IP-SAP and register user context.
*
*
* Parameters :
* ------------
* 'sap_id', IN, value of IP-'protocol' to bind for
* 'usr_receive', IN, user-receive function to be registered
* 'ip_sp_hd', OUT, handle of IP to be used by user by call
* of 'send' or 'close'
*
* Return values :
* ---------------
* 'ERROR_NET_IP_FATAL_STATE' A fatal state has been detected in IP.
* 'ERROR_NET_IP_NOT_INITIALIZED' IP-'init' has not been called.
* 'OK'(=0),
*
*
************************************************************************/
UINT32 NET_IP_open( UINT8 sap_id,
t_ip_usr_receive ip_usr_receive,
UINT32 *ip_sp_hd )
{
UINT32 rcode = OK ;
switch (NET_IP_state)
{
case IP_STATE_CLOSED:
rcode = ERROR_NET_IP_NOT_INITIALIZED ;
break;
case IP_STATE_INITED:
/* try allocating a SAP */
rcode = NET_IP_alloc_sap( sap_id,
ip_usr_receive,
ip_sp_hd ) ;
if ( rcode == OK )
{
/* By first 'open', we need to create our SAP in MAC */
rcode = NET_MAC_open( MAC_SAP_IP,
NET_IP_receive,
&mac_sp_hd ) ;
if ( rcode == OK )
{
NET_IP_state = IP_STATE_OPEN ;
}
}
break;
case IP_STATE_OPEN:
/* try allocating a SAP */
rcode = NET_IP_alloc_sap( sap_id,
ip_usr_receive,
ip_sp_hd ) ;
break;
default:
/* we should never arrive here */
rcode = ERROR_NET_IP_FATAL_STATE ;
break;
}
return( rcode ) ;
}
/************************************************************************
*
* NET_IP_close
* Description :
* -------------
* Close a IP-SAP.
*
*
* Parameters :
* ------------
* 'ip_sp_hd' service provider defined handle
*
*
* Return values :
* ---------------
* 'ERROR_NET_IP_INVALID_HANDLE', invalid handle
* 'OK'(=0), SAP has been closed
*
*
************************************************************************/
UINT32 NET_IP_close( UINT32 ip_sp_hd )
{
/* validate handle */
IF_UPPER( ERROR_NET_IP_INVALID_HANDLE, ip_sp_hd, IP_SAP_COUNT)
/* close SAP */
sap_context[ip_sp_hd].ip_sap_state = IP_SAP_STATE_CLOSED ;
sap_context[ip_sp_hd].ip_sap = IP_SAP_UNDEFINED ;
sap_context[ip_sp_hd].ip_usr_receive = NULL ;
return(OK) ;
}
/************************************************************************
*
* NET_IP_send
* Description :
* -------------
* Request the IP module to send a frame, linked
* to a certain 'SAP' to a specified IP-destination address.
*
*
* Parameters :
* ------------
* 'ip_sp_hd', IN, handle to lookup a registered SAP context
* 'dst_adr', IN, destination ip address (BE-format)
* 'dst_mac_adr', IN, destination MAC address (may be undefined =NULL)
* 'length', IN, length of frame to send
* 'data', IN, address to start of frame to be send
*
*
* Return values :
* ---------------
* 'OK'(=0)
*
************************************************************************/
UINT32 NET_IP_send( UINT32 ip_sp_hd, /* service provider defined handle */
UINT32 dst_adr, /* destination ip address (BE) */
t_mac_addr *dst_mac_adr, /* destination MAC-address */
UINT32 length, /* total length of frame to send */
UINT8 *data ) /* pointer to start of frame */
{
UINT32 rcode ;
UINT32 our_ip_adr ;
UINT32 subnet ;
UINT32 gateway ;
t_mac_addr mac_addr ;
UINT8 *p ;
UINT8 tmp1 ;
UINT16 tmp2 ;
UINT32 tmp4 ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -