2002-10-19 12:59:30 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2001, 2002 Swedish Institute of Computer Science.
|
|
|
|
* 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. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
|
|
|
*
|
|
|
|
* This file is part of the lwIP TCP/IP stack.
|
|
|
|
*
|
|
|
|
* Author: Adam Dunkels <adam@sics.se>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
/* ip.c
|
|
|
|
*
|
|
|
|
* This is the code for the IP layer.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
#include "lwip/debug.h"
|
|
|
|
|
|
|
|
#include "lwip/def.h"
|
|
|
|
#include "lwip/mem.h"
|
|
|
|
#include "lwip/ip.h"
|
2002-11-22 15:46:50 +00:00
|
|
|
#include "lwip/ip_frag.h"
|
2002-10-19 12:59:30 +00:00
|
|
|
#include "lwip/inet.h"
|
|
|
|
#include "lwip/netif.h"
|
|
|
|
#include "lwip/icmp.h"
|
|
|
|
#include "lwip/udp.h"
|
|
|
|
#include "lwip/tcp.h"
|
|
|
|
|
|
|
|
#include "lwip/stats.h"
|
|
|
|
|
|
|
|
#include "arch/perf.h"
|
|
|
|
|
2002-11-21 10:32:19 +00:00
|
|
|
#if LWIP_SNMP > 0
|
|
|
|
# include "snmp.h"
|
|
|
|
#endif
|
2002-10-19 12:59:30 +00:00
|
|
|
#if LWIP_DHCP
|
|
|
|
#include "lwip/dhcp.h"
|
|
|
|
#endif /* LWIP_DHCP */
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
/* ip_init:
|
|
|
|
*
|
|
|
|
* Initializes the IP layer.
|
|
|
|
*/
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
ip_init(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
/* ip_lookup:
|
|
|
|
*
|
|
|
|
* An experimental feature that will be changed in future versions. Do
|
|
|
|
* not depend on it yet...
|
|
|
|
*/
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
#ifdef LWIP_DEBUG
|
|
|
|
u8_t
|
|
|
|
ip_lookup(void *header, struct netif *inp)
|
|
|
|
{
|
|
|
|
struct ip_hdr *iphdr;
|
|
|
|
|
|
|
|
iphdr = header;
|
|
|
|
|
|
|
|
/* Refuse anything that isn't IPv4. */
|
|
|
|
if(IPH_V(iphdr) != 4) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Immediately accept/decline packets that are fragments or has
|
|
|
|
options. */
|
|
|
|
#if IP_REASSEMBLY == 0
|
|
|
|
/* if((IPH_OFFSET(iphdr) & htons(IP_OFFMASK | IP_MF)) != 0) {
|
|
|
|
return 0;
|
|
|
|
}*/
|
|
|
|
#endif /* IP_REASSEMBLY == 0 */
|
|
|
|
|
|
|
|
#if IP_OPTIONS == 0
|
|
|
|
if(IPH_HL(iphdr) != 5) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* IP_OPTIONS == 0 */
|
|
|
|
|
|
|
|
switch(IPH_PROTO(iphdr)) {
|
|
|
|
#if LWIP_UDP > 0
|
|
|
|
case IP_PROTO_UDP:
|
|
|
|
return udp_lookup(iphdr, inp);
|
|
|
|
break;
|
|
|
|
#endif /* LWIP_UDP */
|
|
|
|
#if LWIP_TCP > 0
|
|
|
|
case IP_PROTO_TCP:
|
|
|
|
return 1;
|
|
|
|
#endif /* LWIP_TCP */
|
|
|
|
case IP_PROTO_ICMP:
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* LWIP_DEBUG */
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
/* ip_route:
|
|
|
|
*
|
|
|
|
* Finds the appropriate network interface for a given IP address. It
|
|
|
|
* searches the list of network interfaces linearly. A match is found
|
|
|
|
* if the masked IP address of the network interface equals the masked
|
|
|
|
* IP address given to the function.
|
|
|
|
*/
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
struct netif *
|
|
|
|
ip_route(struct ip_addr *dest)
|
|
|
|
{
|
|
|
|
struct netif *netif;
|
|
|
|
|
|
|
|
for(netif = netif_list; netif != NULL; netif = netif->next) {
|
|
|
|
if(ip_addr_maskcmp(dest, &(netif->ip_addr), &(netif->netmask))) {
|
|
|
|
return netif;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return netif_default;
|
|
|
|
}
|
|
|
|
#if IP_FORWARD
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
/* ip_forward:
|
|
|
|
*
|
|
|
|
* Forwards an IP packet. It finds an appropriate route for the
|
|
|
|
* packet, decrements the TTL value of the packet, adjusts the
|
|
|
|
* checksum and outputs the packet on the appropriate interface.
|
|
|
|
*/
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
ip_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp)
|
|
|
|
{
|
2002-12-18 11:53:26 +00:00
|
|
|
struct netif *netif;
|
2002-10-19 12:59:30 +00:00
|
|
|
|
|
|
|
PERF_START;
|
|
|
|
|
|
|
|
if((netif = ip_route((struct ip_addr *)&(iphdr->dest))) == NULL) {
|
|
|
|
|
|
|
|
DEBUGF(IP_DEBUG, ("ip_forward: no forwarding route for 0x%lx found\n",
|
|
|
|
iphdr->dest.addr));
|
|
|
|
|
2002-11-21 10:32:19 +00:00
|
|
|
#if LWIP_SNMP > 0
|
|
|
|
snmp_inc_ipnoroutes();
|
|
|
|
#endif
|
2002-10-19 12:59:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't forward packets onto the same network interface on which
|
|
|
|
they arrived. */
|
|
|
|
if(netif == inp) {
|
|
|
|
DEBUGF(IP_DEBUG, ("ip_forward: not forward packets back on incoming interface.\n"));
|
|
|
|
|
2002-11-21 10:32:19 +00:00
|
|
|
#if LWIP_SNMP > 0
|
|
|
|
snmp_inc_ipnoroutes();
|
|
|
|
#endif
|
2002-10-19 12:59:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Decrement TTL and send ICMP if ttl == 0. */
|
|
|
|
IPH_TTL_SET(iphdr, IPH_TTL(iphdr) - 1);
|
|
|
|
if(IPH_TTL(iphdr) == 0) {
|
|
|
|
/* Don't send ICMP messages in response to ICMP messages */
|
|
|
|
if(IPH_PROTO(iphdr) != IP_PROTO_ICMP) {
|
|
|
|
icmp_time_exceeded(p, ICMP_TE_TTL);
|
2002-11-21 10:32:19 +00:00
|
|
|
#if LWIP_SNMP > 0
|
|
|
|
snmp_inc_icmpouttimeexcds();
|
|
|
|
#endif
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Incremental update of the IP checksum. */
|
|
|
|
if(IPH_CHKSUM(iphdr) >= htons(0xffff - 0x100)) {
|
|
|
|
IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + htons(0x100) + 1);
|
|
|
|
} else {
|
|
|
|
IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + htons(0x100));
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUGF(IP_DEBUG, ("ip_forward: forwarding packet to 0x%lx\n",
|
|
|
|
iphdr->dest.addr));
|
|
|
|
|
|
|
|
#ifdef IP_STATS
|
2002-12-18 12:49:01 +00:00
|
|
|
++lwip_stats.ip.fw;
|
|
|
|
++lwip_stats.ip.xmit;
|
2002-10-19 12:59:30 +00:00
|
|
|
#endif /* IP_STATS */
|
2002-11-21 10:32:19 +00:00
|
|
|
#if LWIP_SNMP > 0
|
|
|
|
snmp_inc_ipforwdatagrams();
|
|
|
|
#endif
|
2002-10-19 12:59:30 +00:00
|
|
|
|
|
|
|
PERF_STOP("ip_forward");
|
|
|
|
|
|
|
|
netif->output(netif, p, (struct ip_addr *)&(iphdr->dest));
|
|
|
|
}
|
|
|
|
#endif /* IP_FORWARD */
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
/* ip_input:
|
|
|
|
*
|
|
|
|
* This function is called by the network interface device driver when
|
|
|
|
* an IP packet is received. The function does the basic checks of the
|
|
|
|
* IP header such as packet size being at least larger than the header
|
|
|
|
* size etc. If the packet was not destined for us, the packet is
|
|
|
|
* forwarded (using ip_forward). The IP checksum is always checked.
|
|
|
|
*
|
|
|
|
* Finally, the packet is sent to the upper layer protocol input function.
|
|
|
|
*/
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
err_t
|
|
|
|
ip_input(struct pbuf *p, struct netif *inp) {
|
|
|
|
static struct ip_hdr *iphdr;
|
|
|
|
static struct netif *netif;
|
|
|
|
static u8_t hl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef IP_STATS
|
2002-12-18 12:49:01 +00:00
|
|
|
++lwip_stats.ip.recv;
|
2002-10-19 12:59:30 +00:00
|
|
|
#endif /* IP_STATS */
|
2002-11-21 10:32:19 +00:00
|
|
|
#if LWIP_SNMP > 0
|
|
|
|
snmp_inc_ipinreceives();
|
|
|
|
#endif
|
2002-10-19 12:59:30 +00:00
|
|
|
|
|
|
|
/* identify the IP header */
|
|
|
|
iphdr = p->payload;
|
|
|
|
if(IPH_V(iphdr) != 4) {
|
|
|
|
DEBUGF(IP_DEBUG, ("IP packet dropped due to bad version number %d\n", IPH_V(iphdr)));
|
|
|
|
#if IP_DEBUG
|
|
|
|
ip_debug_print(p);
|
|
|
|
#endif /* IP_DEBUG */
|
|
|
|
pbuf_free(p);
|
|
|
|
#ifdef IP_STATS
|
2002-12-18 12:49:01 +00:00
|
|
|
++lwip_stats.ip.err;
|
|
|
|
++lwip_stats.ip.drop;
|
2002-10-19 12:59:30 +00:00
|
|
|
#endif /* IP_STATS */
|
2002-11-21 10:32:19 +00:00
|
|
|
#if LWIP_SNMP > 0
|
|
|
|
snmp_inc_ipunknownprotos();
|
|
|
|
#endif
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
hl = IPH_HL(iphdr);
|
|
|
|
|
|
|
|
if(hl * 4 > p->len) {
|
|
|
|
DEBUGF(IP_DEBUG, ("IP packet dropped due to too short packet %d\n", p->len));
|
|
|
|
|
|
|
|
pbuf_free(p);
|
|
|
|
#ifdef IP_STATS
|
2002-12-18 12:49:01 +00:00
|
|
|
++lwip_stats.ip.lenerr;
|
|
|
|
++lwip_stats.ip.drop;
|
2002-10-19 12:59:30 +00:00
|
|
|
#endif /* IP_STATS */
|
2002-11-21 10:32:19 +00:00
|
|
|
#if LWIP_SNMP > 0
|
|
|
|
snmp_inc_ipindiscards();
|
|
|
|
#endif
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* verify checksum */
|
|
|
|
if(inet_chksum(iphdr, hl * 4) != 0) {
|
|
|
|
|
|
|
|
DEBUGF(IP_DEBUG, ("IP packet dropped due to failing checksum 0x%x\n", inet_chksum(iphdr, hl * 4)));
|
|
|
|
#if IP_DEBUG
|
|
|
|
ip_debug_print(p);
|
|
|
|
#endif /* IP_DEBUG */
|
|
|
|
pbuf_free(p);
|
|
|
|
#ifdef IP_STATS
|
2002-12-18 12:49:01 +00:00
|
|
|
++lwip_stats.ip.chkerr;
|
|
|
|
++lwip_stats.ip.drop;
|
2002-10-19 12:59:30 +00:00
|
|
|
#endif /* IP_STATS */
|
2002-11-21 10:32:19 +00:00
|
|
|
#if LWIP_SNMP > 0
|
|
|
|
snmp_inc_ipindiscards();
|
|
|
|
#endif
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Trim pbuf. This should have been done at the netif layer,
|
|
|
|
but we'll do it anyway just to be sure that its done. */
|
|
|
|
pbuf_realloc(p, ntohs(IPH_LEN(iphdr)));
|
|
|
|
|
|
|
|
/* is this packet for us? */
|
|
|
|
for(netif = netif_list; netif != NULL; netif = netif->next) {
|
|
|
|
|
|
|
|
DEBUGF(IP_DEBUG, ("ip_input: iphdr->dest 0x%lx netif->ip_addr 0x%lx (0x%lx, 0x%lx, 0x%lx)\n",
|
|
|
|
iphdr->dest.addr, netif->ip_addr.addr,
|
|
|
|
iphdr->dest.addr & netif->netmask.addr,
|
|
|
|
netif->ip_addr.addr & netif->netmask.addr,
|
|
|
|
iphdr->dest.addr & ~(netif->netmask.addr)));
|
|
|
|
|
2002-12-11 00:21:12 +00:00
|
|
|
/* interface unconfigured? */
|
2002-10-19 12:59:30 +00:00
|
|
|
if(ip_addr_isany(&(netif->ip_addr)) ||
|
2002-12-11 00:21:12 +00:00
|
|
|
/* or unicast to this interface address? */
|
2002-10-19 12:59:30 +00:00
|
|
|
ip_addr_cmp(&(iphdr->dest), &(netif->ip_addr)) ||
|
2002-12-11 00:21:12 +00:00
|
|
|
/* or broadcast on this interface network address ? */
|
2002-10-19 12:59:30 +00:00
|
|
|
(ip_addr_isbroadcast(&(iphdr->dest), &(netif->netmask)) &&
|
|
|
|
ip_addr_maskcmp(&(iphdr->dest), &(netif->ip_addr), &(netif->netmask))) ||
|
2002-12-11 00:21:12 +00:00
|
|
|
/* or restricted broadcast? */
|
2002-10-19 12:59:30 +00:00
|
|
|
ip_addr_cmp(&(iphdr->dest), IP_ADDR_BROADCAST)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if LWIP_DHCP
|
|
|
|
/* If a DHCP packet has arrived on the interface, we pass it up the
|
|
|
|
stack regardless of destination IP address. The reason is that
|
|
|
|
DHCP replies are sent to the IP adress that will be given to this
|
|
|
|
node (as recommended by RFC 1542 section 3.1.1, referred by RFC
|
|
|
|
2131). */
|
|
|
|
if(IPH_PROTO(iphdr) == IP_PROTO_UDP &&
|
|
|
|
((struct udp_hdr *)((u8_t *)iphdr + IPH_HL(iphdr) * 4/sizeof(u8_t)))->src ==
|
|
|
|
DHCP_SERVER_PORT) {
|
|
|
|
netif = inp;
|
|
|
|
}
|
|
|
|
#endif /* LWIP_DHCP */
|
|
|
|
|
|
|
|
if(netif == NULL) {
|
|
|
|
/* packet not for us, route or discard */
|
|
|
|
DEBUGF(IP_DEBUG, ("ip_input: packet not for us.\n"));
|
|
|
|
#if IP_FORWARD
|
|
|
|
if(!ip_addr_isbroadcast(&(iphdr->dest), &(inp->netmask))) {
|
|
|
|
ip_forward(p, iphdr, inp);
|
|
|
|
}
|
2002-11-21 10:32:19 +00:00
|
|
|
else
|
2002-10-19 12:59:30 +00:00
|
|
|
#endif /* IP_FORWARD */
|
2002-11-21 10:32:19 +00:00
|
|
|
{
|
|
|
|
#if LWIP_SNMP > 0
|
|
|
|
snmp_inc_ipindiscards();
|
|
|
|
#endif
|
|
|
|
}
|
2002-10-19 12:59:30 +00:00
|
|
|
pbuf_free(p);
|
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if IP_REASSEMBLY
|
|
|
|
if((IPH_OFFSET(iphdr) & htons(IP_OFFMASK | IP_MF)) != 0) {
|
|
|
|
p = ip_reass(p);
|
|
|
|
if(p == NULL) {
|
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
iphdr = p->payload;
|
|
|
|
}
|
|
|
|
#else /* IP_REASSEMBLY */
|
|
|
|
if((IPH_OFFSET(iphdr) & htons(IP_OFFMASK | IP_MF)) != 0) {
|
|
|
|
pbuf_free(p);
|
|
|
|
DEBUGF(IP_DEBUG, ("IP packet dropped since it was fragmented (0x%x).\n",
|
|
|
|
ntohs(IPH_OFFSET(iphdr))));
|
|
|
|
#ifdef IP_STATS
|
2002-12-18 12:49:01 +00:00
|
|
|
++lwip_stats.ip.opterr;
|
|
|
|
++lwip_stats.ip.drop;
|
2002-10-19 12:59:30 +00:00
|
|
|
#endif /* IP_STATS */
|
2002-11-21 10:32:19 +00:00
|
|
|
#if LWIP_SNMP > 0
|
|
|
|
snmp_inc_ipunknownprotos();
|
|
|
|
#endif
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
#endif /* IP_REASSEMBLY */
|
|
|
|
|
|
|
|
#if IP_OPTIONS == 0
|
|
|
|
if(hl * 4 > IP_HLEN) {
|
|
|
|
DEBUGF(IP_DEBUG, ("IP packet dropped since there were IP options.\n"));
|
|
|
|
|
|
|
|
pbuf_free(p);
|
|
|
|
#ifdef IP_STATS
|
2002-12-18 12:49:01 +00:00
|
|
|
++lwip_stats.ip.opterr;
|
|
|
|
++lwip_stats.ip.drop;
|
2002-10-19 12:59:30 +00:00
|
|
|
#endif /* IP_STATS */
|
2002-11-21 10:32:19 +00:00
|
|
|
#if LWIP_SNMP > 0
|
|
|
|
snmp_inc_ipunknownprotos();
|
|
|
|
#endif
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
#endif /* IP_OPTIONS == 0 */
|
|
|
|
|
|
|
|
|
|
|
|
/* send to upper layers */
|
|
|
|
#if IP_DEBUG
|
|
|
|
DEBUGF(IP_DEBUG, ("ip_input: \n"));
|
|
|
|
ip_debug_print(p);
|
|
|
|
DEBUGF(IP_DEBUG, ("ip_input: p->len %d p->tot_len %d\n", p->len, p->tot_len));
|
|
|
|
#endif /* IP_DEBUG */
|
|
|
|
|
|
|
|
switch(IPH_PROTO(iphdr)) {
|
|
|
|
#if LWIP_UDP > 0
|
|
|
|
case IP_PROTO_UDP:
|
2002-11-21 10:32:19 +00:00
|
|
|
#if LWIP_SNMP > 0
|
|
|
|
snmp_inc_ipindelivers();
|
|
|
|
#endif
|
2002-10-19 12:59:30 +00:00
|
|
|
udp_input(p, inp);
|
|
|
|
break;
|
|
|
|
#endif /* LWIP_UDP */
|
|
|
|
#if LWIP_TCP > 0
|
|
|
|
case IP_PROTO_TCP:
|
2002-11-21 10:32:19 +00:00
|
|
|
#if LWIP_SNMP > 0
|
|
|
|
snmp_inc_ipindelivers();
|
|
|
|
#endif
|
2002-10-19 12:59:30 +00:00
|
|
|
tcp_input(p, inp);
|
|
|
|
break;
|
|
|
|
#endif /* LWIP_TCP */
|
|
|
|
case IP_PROTO_ICMP:
|
2002-11-21 10:32:19 +00:00
|
|
|
#if LWIP_SNMP > 0
|
|
|
|
snmp_inc_ipindelivers();
|
|
|
|
#endif
|
2002-10-19 12:59:30 +00:00
|
|
|
icmp_input(p, inp);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* send ICMP destination protocol unreachable unless is was a broadcast */
|
|
|
|
if(!ip_addr_isbroadcast(&(iphdr->dest), &(inp->netmask)) &&
|
|
|
|
!ip_addr_ismulticast(&(iphdr->dest))) {
|
|
|
|
p->payload = iphdr;
|
|
|
|
icmp_dest_unreach(p, ICMP_DUR_PROTO);
|
|
|
|
}
|
|
|
|
pbuf_free(p);
|
|
|
|
|
|
|
|
DEBUGF(IP_DEBUG, ("Unsupported transportation protocol %d\n", IPH_PROTO(iphdr)));
|
|
|
|
|
|
|
|
#ifdef IP_STATS
|
2002-12-18 12:49:01 +00:00
|
|
|
++lwip_stats.ip.proterr;
|
|
|
|
++lwip_stats.ip.drop;
|
2002-10-19 12:59:30 +00:00
|
|
|
#endif /* IP_STATS */
|
2002-11-21 10:32:19 +00:00
|
|
|
#if LWIP_SNMP > 0
|
|
|
|
snmp_inc_ipunknownprotos();
|
|
|
|
#endif
|
2002-10-19 12:59:30 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
/* ip_output_if:
|
|
|
|
*
|
|
|
|
* Sends an IP packet on a network interface. This function constructs
|
|
|
|
* the IP header and calculates the IP header checksum. If the source
|
|
|
|
* IP address is NULL, the IP address of the outgoing network
|
|
|
|
* interface is filled in as source address.
|
|
|
|
*/
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
err_t
|
|
|
|
ip_output_if(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
|
|
|
|
u8_t ttl,
|
|
|
|
u8_t proto, struct netif *netif)
|
|
|
|
{
|
|
|
|
static struct ip_hdr *iphdr;
|
|
|
|
static u16_t ip_id = 0;
|
|
|
|
|
|
|
|
|
2002-11-21 10:32:19 +00:00
|
|
|
#if LWIP_SNMP > 0
|
|
|
|
snmp_inc_ipoutrequests();
|
|
|
|
#endif
|
2002-10-19 12:59:30 +00:00
|
|
|
|
|
|
|
if(dest != IP_HDRINCL) {
|
|
|
|
if(pbuf_header(p, IP_HLEN)) {
|
|
|
|
DEBUGF(IP_DEBUG, ("ip_output: not enough room for IP header in pbuf\n"));
|
|
|
|
|
|
|
|
#ifdef IP_STATS
|
2002-12-18 12:49:01 +00:00
|
|
|
++lwip_stats.ip.err;
|
2002-10-19 12:59:30 +00:00
|
|
|
#endif /* IP_STATS */
|
2002-11-21 10:32:19 +00:00
|
|
|
#if LWIP_SNMP > 0
|
|
|
|
snmp_inc_ipoutdiscards();
|
|
|
|
#endif
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_BUF;
|
|
|
|
}
|
|
|
|
|
|
|
|
iphdr = p->payload;
|
|
|
|
|
|
|
|
IPH_TTL_SET(iphdr, ttl);
|
|
|
|
IPH_PROTO_SET(iphdr, proto);
|
|
|
|
|
|
|
|
ip_addr_set(&(iphdr->dest), dest);
|
|
|
|
|
|
|
|
IPH_VHLTOS_SET(iphdr, 4, IP_HLEN / 4, 0);
|
|
|
|
IPH_LEN_SET(iphdr, htons(p->tot_len));
|
|
|
|
IPH_OFFSET_SET(iphdr, htons(IP_DF));
|
|
|
|
IPH_ID_SET(iphdr, htons(ip_id));
|
|
|
|
++ip_id;
|
|
|
|
|
|
|
|
if(ip_addr_isany(src)) {
|
|
|
|
ip_addr_set(&(iphdr->src), &(netif->ip_addr));
|
|
|
|
} else {
|
|
|
|
ip_addr_set(&(iphdr->src), src);
|
|
|
|
}
|
|
|
|
|
|
|
|
IPH_CHKSUM_SET(iphdr, 0);
|
|
|
|
IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
|
|
|
|
} else {
|
|
|
|
iphdr = p->payload;
|
|
|
|
dest = &(iphdr->dest);
|
|
|
|
}
|
|
|
|
|
2002-11-22 15:46:50 +00:00
|
|
|
#if IP_FRAG
|
|
|
|
if (p->tot_len > netif->mtu)
|
|
|
|
return ip_frag(p,netif,dest);
|
|
|
|
#endif
|
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
#ifdef IP_STATS
|
2002-12-18 12:49:01 +00:00
|
|
|
lwip_stats.ip.xmit++;
|
2002-10-19 12:59:30 +00:00
|
|
|
#endif /* IP_STATS */
|
|
|
|
DEBUGF(IP_DEBUG, ("ip_output_if: %c%c ", netif->name[0], netif->name[1]));
|
|
|
|
#if IP_DEBUG
|
|
|
|
ip_debug_print(p);
|
|
|
|
#endif /* IP_DEBUG */
|
|
|
|
|
2002-11-21 10:32:19 +00:00
|
|
|
DEBUGF(IP_DEBUG, ("netif->output()"));
|
2002-10-19 12:59:30 +00:00
|
|
|
|
|
|
|
return netif->output(netif, p, dest);
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
/* ip_output:
|
|
|
|
*
|
|
|
|
* Simple interface to ip_output_if. It finds the outgoing network
|
|
|
|
* interface and calls upon ip_output_if to do the actual work.
|
|
|
|
*/
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
err_t
|
|
|
|
ip_output(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
|
|
|
|
u8_t ttl, u8_t proto)
|
|
|
|
{
|
2002-12-18 11:53:26 +00:00
|
|
|
struct netif *netif;
|
2002-10-19 12:59:30 +00:00
|
|
|
|
|
|
|
if((netif = ip_route(dest)) == NULL) {
|
|
|
|
DEBUGF(IP_DEBUG, ("ip_output: No route to 0x%lx\n", dest->addr));
|
|
|
|
|
|
|
|
#ifdef IP_STATS
|
2002-12-18 12:49:01 +00:00
|
|
|
++lwip_stats.ip.rterr;
|
2002-10-19 12:59:30 +00:00
|
|
|
#endif /* IP_STATS */
|
2002-11-21 10:32:19 +00:00
|
|
|
#if LWIP_SNMP > 0
|
|
|
|
snmp_inc_ipoutdiscards();
|
|
|
|
#endif
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_RTE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ip_output_if(p, src, dest, ttl, proto, netif);
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
#if IP_DEBUG
|
|
|
|
void
|
|
|
|
ip_debug_print(struct pbuf *p)
|
|
|
|
{
|
|
|
|
struct ip_hdr *iphdr = p->payload;
|
|
|
|
u8_t *payload;
|
|
|
|
|
|
|
|
payload = (u8_t *)iphdr + IP_HLEN/sizeof(u8_t);
|
|
|
|
|
|
|
|
DEBUGF(IP_DEBUG, ("IP header:\n"));
|
|
|
|
DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
|
|
|
|
DEBUGF(IP_DEBUG, ("|%2d |%2d | %2d | %4d | (v, hl, tos, len)\n",
|
|
|
|
IPH_V(iphdr),
|
|
|
|
IPH_HL(iphdr),
|
|
|
|
IPH_TOS(iphdr),
|
|
|
|
ntohs(IPH_LEN(iphdr))));
|
|
|
|
DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
|
|
|
|
DEBUGF(IP_DEBUG, ("| %5d |%d%d%d| %4d | (id, flags, offset)\n",
|
|
|
|
ntohs(IPH_ID(iphdr)),
|
|
|
|
ntohs(IPH_OFFSET(iphdr)) >> 15 & 1,
|
|
|
|
ntohs(IPH_OFFSET(iphdr)) >> 14 & 1,
|
|
|
|
ntohs(IPH_OFFSET(iphdr)) >> 13 & 1,
|
|
|
|
ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK));
|
|
|
|
DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
|
|
|
|
DEBUGF(IP_DEBUG, ("| %2d | %2d | 0x%04x | (ttl, proto, chksum)\n",
|
|
|
|
IPH_TTL(iphdr),
|
|
|
|
IPH_PROTO(iphdr),
|
|
|
|
ntohs(IPH_CHKSUM(iphdr))));
|
|
|
|
DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
|
|
|
|
DEBUGF(IP_DEBUG, ("| %3ld | %3ld | %3ld | %3ld | (src)\n",
|
|
|
|
ntohl(iphdr->src.addr) >> 24 & 0xff,
|
|
|
|
ntohl(iphdr->src.addr) >> 16 & 0xff,
|
|
|
|
ntohl(iphdr->src.addr) >> 8 & 0xff,
|
|
|
|
ntohl(iphdr->src.addr) & 0xff));
|
|
|
|
DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
|
|
|
|
DEBUGF(IP_DEBUG, ("| %3ld | %3ld | %3ld | %3ld | (dest)\n",
|
|
|
|
ntohl(iphdr->dest.addr) >> 24 & 0xff,
|
|
|
|
ntohl(iphdr->dest.addr) >> 16 & 0xff,
|
|
|
|
ntohl(iphdr->dest.addr) >> 8 & 0xff,
|
|
|
|
ntohl(iphdr->dest.addr) & 0xff));
|
|
|
|
DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
|
|
|
|
}
|
|
|
|
#endif /* IP_DEBUG */
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|