📄 netip.c
字号:
/*****************************************************************************
* netip.c - Network Internet Protocol (IP) program file.
*
* portions Copyright (c) 1997 by Global Election Systems Inc.
*
* The authors hereby grant permission to use, copy, modify, distribute,
* and license this software and its documentation for any purpose, provided
* that existing copyright notices are retained in all copies and that this
* notice and the following disclaimer are included verbatim in any
* distributions. No written agreement, license, or royalty fee is required
* for any of the authorized uses.
*
* THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS *AS IS* AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
* REVISION HISTORY
*
* 97-11-05 Guy Lancaster <lancasterg@acm.org>, Global Election Systems Inc.
* Original.
* 2001-05-18 Mads Christiansen <mads@mogi.dk>, Partner Voxtream
* Added support for running uC/IP in a single proces and on ethernet.
* 2001-09-10 Mads Christiansen <mads@voxtream.com>, Partner Voxtream
* Added support for IP/UDP broadcasts.
*****************************************************************************/
/*
* Copyright (c) 1982, 1986, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)ip.h 8.2 (Berkeley) 6/1/94
*/
#include "netconf.h"
#include <string.h>
#include "net.h"
#include "netbuf.h"
#include "netip.h"
#include "netiphdr.h"
/* The upper layer interfaces. */
#include "nettcp.h"
#if UDP_SUPPORT > 0
#include "netudp.h"
#endif
#include "neticmp.h"
/* The lower layer interfaces. */
#if PPP_SUPPORT > 0
#include "netppp.h"
#endif
#if ETHER_SUPPORT > 0
#include "netaddrs.h"
#include "netether.h"
#endif
#include <stdio.h>
#include "netdebug.h"
/***********************************/
/*** LOCAL FUNCTION DECLARATIONS ***/
/***********************************/
static void ipDispatch(NBuf *nb);
/******************************/
/*** PUBLIC DATA STRUCTURES ***/
/******************************/
#if STATS_SUPPORT > 0
IPStats ipStats;
#endif
u_short ipID; /* IP packet ctr, for ID fields. */
int ip_defttl; /* default IP ttl */
IfType defIfType; /* Default route interface type. */
int defIfID; /* Default route interface ID. */
u_long defIPAddr; /* Default route IP address. */
int disable_defaultip; /* Don't use hostname for default IP adrs */
/*****************************/
/*** LOCAL DATA STRUCTURES ***/
/*****************************/
/***********************************/
/*** PUBLIC FUNCTION DEFINITIONS ***/
/***********************************/
/*
* ipInit - Initialize the IP subsystem.
*/
void ipInit(void)
{
#if STATS_SUPPORT > 0
memset(&ipStats, 0, sizeof(IPStats));
ipStats.headLine.fmtStr = "\t\tIP STATISTICS\r\n";
ipStats.ips_total.fmtStr = "\tTOTAL DATAGRAMS: %5lu\r\n";
ipStats.ips_toosmall.fmtStr = "\tTOO SMALL : %5lu\r\n";
ipStats.ips_badvers.fmtStr = "\tBAD VERSION : %5lu\r\n";
ipStats.ips_badhlen.fmtStr = "\tBAD HEAD LENGTH: %5lu\r\n";
ipStats.ips_badsum.fmtStr = "\tBAD CHECKSUM : %5lu\r\n";
ipStats.ips_badlen.fmtStr = "\tBAD LENGTH : %5lu\r\n";
ipStats.ips_buffers.fmtStr = "\tNO FREE BUFFERS: %5lu\r\n";
ipStats.ips_odropped.fmtStr = "\tOTHER DROPPED : %5lu\r\n";
ipStats.ips_cantforward.fmtStr = "\tCAN'T FORWARD : %5lu\r\n";
ipStats.ips_delivered.fmtStr = "\tDELIVERED : %5lu\r\n";
#endif
ipID = 1;
ip_defttl = IPTTLDEFAULT;
netMask = 0;
localHost = 0; /* OURADDR; */
defIfType = IFT_UNSPEC;
defIfID = 0;
disable_defaultip = !0;
icmpInit();
#if DEBUG_SUPPORT > 0
setTraceLevel(LOG_WARNING, TL_IP);
#endif
}
/*
* ipInput - Process a raw incoming IP datagram.
*/
void ipInput(NBuf *inBuf, IfType ifType, int ifID)
{
IPHdr *ip;
u_char hdrLen;
#if defined(ADL_TARGET_PLATFORM)&&(ADL_TARGET_PLATFORM==ADL_TARGET_LS808)
u_char ip_hl,ip_v;
#warning "using MIPS bitfield avoidance"
#endif
/* Validate parameters. */
if (inBuf == NULL)
return;
/*
* If we don't have a default interface, assume that this interface
* is the one we want.
*/
if (defIfType == IFT_UNSPEC) {
defIfType = ifType;
defIfID = ifID;
}
/* Validate IP header. */
STATS(ipStats.ips_total.val++;)
if (inBuf->len < sizeof(IPHdr) &&
(inBuf = nPullup(inBuf, sizeof(IPHdr))) == NULL) {
STATS(ipStats.ips_toosmall.val++;)
IPDEBUG((LOG_ERR, TL_IP, "ipInput: Runt packet len %u", inBuf->len));
goto abortInput;
}
ip = nBUFTOPTR(inBuf, IPHdr *);
#if defined(ADL_TARGET_PLATFORM)&&(ADL_TARGET_PLATFORM==ADL_TARGET_LS808)
ip_v=ip->ip_hl_v>>4;
ip_hl=ip->ip_hl_v&0xf;
#else
// ip_v=ip->ip_v;
// ip_hl=ip->ip_hl;
#endif
if (ip->ip_v != IPVERSION) {
STATS(ipStats.ips_badvers.val++;)
IPDEBUG((LOG_ERR, TL_IP, "ipInput: Bad version %u", ip->ip_v));
goto abortInput;
}
hdrLen = ip->ip_hl << 2;
if (hdrLen < sizeof(IPHdr)) { /* minimum header length */
STATS(ipStats.ips_badhlen.val++;)
IPDEBUG((LOG_ERR, TL_IP, "ipInput: Bad hdr sz %u", hdrLen));
goto abortInput;
}
if (hdrLen > inBuf->len) {
if ((inBuf = nPullup(inBuf, hdrLen)) == NULL) {
STATS(ipStats.ips_badhlen.val++;)
goto abortInput;
}
ip = nBUFTOPTR(inBuf, IPHdr *);
}
///* TODO: checksum is failing, debug why and fix - RD.
if ((ip->ip_sum = inChkSum(inBuf, hdrLen, 0)) != 0) {
STATS(ipStats.ips_badsum.val++;)
IPDEBUG((LOG_ERR, TL_IP, "ipInput: Bad IP chksum"));
goto abortInput;
}
// */
/*
* Convert fields to host representation.
*/
NTOHS(ip->ip_len);
if (ip->ip_len < hdrLen) {
STATS(ipStats.ips_badlen.val++;)
goto abortInput;
}
NTOHS(ip->ip_id);
NTOHS(ip->ip_off);
/*
* Due to a possible padding on ethernet, chainLen doesn't always
* reflect the size of the real length of the IP packet.
* So adjust chainLen to reflect ip_len if ip_len is less than chainLen
*/
if (ip->ip_len < inBuf->chainLen) {
nTrim(NULL, &inBuf, ip->ip_len - inBuf->chainLen);
if (!inBuf) return; // Just in case
}
/*
* Adjust ip_len to not reflect header.
* XXX This makes it confusing since packets sent to ipRawOut()
* would have a normal header but other packets would not!
ip->ip_len -= hdrLen;
*/
/* Pass the datagram along and we're done. */
ipDispatch(inBuf);
return;
abortInput:
#if DEBUG_SUPPORT > 0
nDumpChain(inBuf);
#endif
nFreeChain(inBuf);
return;
}
/*
* ipSend - Build and send an IP datagram.
* The Type-Of-Service is defaulted, we don't handle fragmentation, the
* Time-To-Live is defaulted, and we don't support IP options.
*/
void ipSend(
u_char protocol, /* IP protocol. */
u_long srcAddr, /* Source IP address in network byte order. */
u_long dstAddr, /* Destination IP address in network byte order. */
NBuf *outBuf /* Datagram to send. */
)
{
IPHdr ipHdr;
if (outBuf) {
/* Build the IP header. */
memset(&ipHdr, 0, sizeof(IPHdr));
ipHdr.ip_v = IPVERSION;
ipHdr.ip_hl = sizeof(IPHdr) / 4;
ipHdr.ip_tos = 0; /* Default Type-Of-Service */
ipHdr.ip_len = outBuf->chainLen + sizeof(IPHdr);
ipHdr.ip_id = IPNEWID();
ipHdr.ip_off = 0; /* Sorry - no fragments. */
ipHdr.ip_ttl = ip_defttl;
ipHdr.ip_p = protocol;
ipHdr.ip_src.s_addr = srcAddr;
ipHdr.ip_dst.s_addr = dstAddr;
/* Prepend an IP header. */
nPREPEND(outBuf, &ipHdr, sizeof(IPHdr));
if (outBuf == NULL) {
STATS(ipStats.ips_odropped.val++;)
} else
/* Send the datagram. */
ipDispatch(outBuf);
}
}
/*
* ipRawOut - Send a prepared IP datagram.
* This is where route lookup could occur.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -