2007-08-09 22:21:44 +00:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* This is the IPv4 layer implementation for incoming and outgoing IP traffic.
|
2015-10-06 21:13:15 +02:00
|
|
|
*
|
2004-05-05 13:28:44 +00:00
|
|
|
* @see ip_frag.c
|
|
|
|
*
|
|
|
|
*/
|
2007-08-09 22:21:44 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
/*
|
2004-02-07 00:30:03 +00:00
|
|
|
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
|
2003-06-11 22:34:51 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
2002-10-19 12:59:30 +00:00
|
|
|
* 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
|
2003-06-11 22:34:51 +00:00
|
|
|
* derived from this software without specific prior written permission.
|
2002-10-19 12:59:30 +00:00
|
|
|
*
|
2003-06-11 22:34:51 +00:00
|
|
|
* 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
|
2002-10-19 12:59:30 +00:00
|
|
|
* OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the lwIP TCP/IP stack.
|
2003-06-11 22:34:51 +00:00
|
|
|
*
|
2002-10-19 12:59:30 +00:00
|
|
|
* Author: Adam Dunkels <adam@sics.se>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2003-02-21 16:43:46 +00:00
|
|
|
#include "lwip/opt.h"
|
2015-04-09 22:21:15 +02:00
|
|
|
|
|
|
|
#if LWIP_IPV4
|
|
|
|
|
2007-11-16 19:31:10 +00:00
|
|
|
#include "lwip/ip.h"
|
2002-10-19 12:59:30 +00:00
|
|
|
#include "lwip/def.h"
|
|
|
|
#include "lwip/mem.h"
|
2016-07-19 10:12:56 +02:00
|
|
|
#include "lwip/ip4_frag.h"
|
2007-11-16 19:31:10 +00:00
|
|
|
#include "lwip/inet_chksum.h"
|
2002-10-19 12:59:30 +00:00
|
|
|
#include "lwip/netif.h"
|
|
|
|
#include "lwip/icmp.h"
|
2007-09-07 23:01:59 +00:00
|
|
|
#include "lwip/igmp.h"
|
2003-11-14 13:17:23 +00:00
|
|
|
#include "lwip/raw.h"
|
2002-10-19 12:59:30 +00:00
|
|
|
#include "lwip/udp.h"
|
2015-10-09 21:58:58 +02:00
|
|
|
#include "lwip/priv/tcp_priv.h"
|
2010-03-08 18:24:23 +00:00
|
|
|
#include "lwip/autoip.h"
|
2002-10-19 12:59:30 +00:00
|
|
|
#include "lwip/stats.h"
|
2016-08-11 14:35:44 +02:00
|
|
|
#include "lwip/prot/dhcp.h"
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2009-08-11 14:38:55 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2017-02-10 13:25:04 +01:00
|
|
|
#ifdef LWIP_HOOK_FILENAME
|
|
|
|
#include LWIP_HOOK_FILENAME
|
|
|
|
#endif
|
|
|
|
|
2010-04-28 19:36:51 +00:00
|
|
|
/** Set this to 0 in the rare case of wanting to call an extra function to
|
|
|
|
* generate the IP checksum (in contrast to calculating it on-the-fly). */
|
|
|
|
#ifndef LWIP_INLINE_IP_CHKSUM
|
2015-09-01 22:04:15 +02:00
|
|
|
#if LWIP_CHECKSUM_CTRL_PER_NETIF
|
|
|
|
#define LWIP_INLINE_IP_CHKSUM 0
|
|
|
|
#else /* LWIP_CHECKSUM_CTRL_PER_NETIF */
|
2010-04-28 19:36:51 +00:00
|
|
|
#define LWIP_INLINE_IP_CHKSUM 1
|
2015-09-01 22:04:15 +02:00
|
|
|
#endif /* LWIP_CHECKSUM_CTRL_PER_NETIF */
|
2010-04-28 19:36:51 +00:00
|
|
|
#endif
|
2015-09-01 22:04:15 +02:00
|
|
|
|
2010-04-28 19:36:51 +00:00
|
|
|
#if LWIP_INLINE_IP_CHKSUM && CHECKSUM_GEN_IP
|
|
|
|
#define CHECKSUM_GEN_IP_INLINE 1
|
|
|
|
#else
|
|
|
|
#define CHECKSUM_GEN_IP_INLINE 0
|
|
|
|
#endif
|
|
|
|
|
2010-06-16 12:25:00 +00:00
|
|
|
#if LWIP_DHCP || defined(LWIP_IP_ACCEPT_UDP_PORT)
|
|
|
|
#define IP_ACCEPT_LINK_LAYER_ADDRESSING 1
|
|
|
|
|
|
|
|
/** Some defines for DHCP to let link-layer-addressed packets through while the
|
|
|
|
* netif is down.
|
2015-04-09 22:21:15 +02:00
|
|
|
* To use this in your own application/protocol, define LWIP_IP_ACCEPT_UDP_PORT(port)
|
2010-06-16 12:25:00 +00:00
|
|
|
* to return 1 if the port is accepted and 0 if the port is not accepted.
|
|
|
|
*/
|
|
|
|
#if LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT)
|
|
|
|
/* accept DHCP client port and custom port */
|
|
|
|
#define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) (((port) == PP_NTOHS(DHCP_CLIENT_PORT)) \
|
2010-06-17 11:53:16 +00:00
|
|
|
|| (LWIP_IP_ACCEPT_UDP_PORT(port)))
|
2010-06-16 12:53:46 +00:00
|
|
|
#elif defined(LWIP_IP_ACCEPT_UDP_PORT) /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */
|
2010-06-16 12:25:00 +00:00
|
|
|
/* accept custom port only */
|
2011-10-09 14:30:49 +02:00
|
|
|
#define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) (LWIP_IP_ACCEPT_UDP_PORT(port))
|
2010-06-16 12:53:46 +00:00
|
|
|
#else /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */
|
2010-06-16 12:25:00 +00:00
|
|
|
/* accept DHCP client port only */
|
|
|
|
#define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) ((port) == PP_NTOHS(DHCP_CLIENT_PORT))
|
|
|
|
#endif /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */
|
|
|
|
|
|
|
|
#else /* LWIP_DHCP */
|
|
|
|
#define IP_ACCEPT_LINK_LAYER_ADDRESSING 0
|
|
|
|
#endif /* LWIP_DHCP */
|
|
|
|
|
2010-05-22 21:11:02 +00:00
|
|
|
/** The IP header ID of the next outgoing IP packet */
|
|
|
|
static u16_t ip_id;
|
2009-05-05 17:50:39 +00:00
|
|
|
|
2015-08-05 22:52:23 +02:00
|
|
|
#if LWIP_MULTICAST_TX_OPTIONS
|
2015-02-25 20:58:11 +01:00
|
|
|
/** The default netif used for multicast */
|
2015-04-09 22:21:15 +02:00
|
|
|
static struct netif* ip4_default_multicast_netif;
|
2015-02-25 20:58:11 +01:00
|
|
|
|
2016-08-07 10:13:50 +02:00
|
|
|
/**
|
|
|
|
* @ingroup ip4
|
|
|
|
* Set a default netif for IPv4 multicast. */
|
2015-02-25 20:58:11 +01:00
|
|
|
void
|
2015-04-09 22:21:15 +02:00
|
|
|
ip4_set_default_multicast_netif(struct netif* default_multicast_netif)
|
2015-02-25 20:58:11 +01:00
|
|
|
{
|
2015-04-09 22:21:15 +02:00
|
|
|
ip4_default_multicast_netif = default_multicast_netif;
|
2015-02-25 20:58:11 +01:00
|
|
|
}
|
2015-08-05 22:52:23 +02:00
|
|
|
#endif /* LWIP_MULTICAST_TX_OPTIONS */
|
2015-02-25 20:58:11 +01:00
|
|
|
|
2015-08-30 21:54:39 +02:00
|
|
|
#ifdef LWIP_HOOK_IP4_ROUTE_SRC
|
|
|
|
/**
|
|
|
|
* Source based IPv4 routing must be fully implemented in
|
|
|
|
* LWIP_HOOK_IP4_ROUTE_SRC(). This function only provides he parameters.
|
|
|
|
*/
|
|
|
|
struct netif *
|
|
|
|
ip4_route_src(const ip4_addr_t *dest, const ip4_addr_t *src)
|
|
|
|
{
|
|
|
|
if (src != NULL) {
|
2016-03-05 15:52:39 +01:00
|
|
|
/* when src==NULL, the hook is called from ip4_route(dest) */
|
2015-08-30 21:54:39 +02:00
|
|
|
struct netif *netif = LWIP_HOOK_IP4_ROUTE_SRC(dest, src);
|
|
|
|
if (netif != NULL) {
|
|
|
|
return netif;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ip4_route(dest);
|
|
|
|
}
|
|
|
|
#endif /* LWIP_HOOK_IP4_ROUTE_SRC */
|
|
|
|
|
2004-05-05 13:28:44 +00:00
|
|
|
/**
|
2002-10-19 12:59:30 +00:00
|
|
|
* 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.
|
2007-06-02 17:12:36 +00:00
|
|
|
*
|
|
|
|
* @param dest the destination IP address for which to find the route
|
|
|
|
* @return the netif on which to send to reach dest
|
2002-10-19 12:59:30 +00:00
|
|
|
*/
|
|
|
|
struct netif *
|
2015-04-09 22:21:15 +02:00
|
|
|
ip4_route(const ip4_addr_t *dest)
|
2002-10-19 12:59:30 +00:00
|
|
|
{
|
2017-03-01 16:10:50 +01:00
|
|
|
#if !LWIP_SINGLE_NETIF
|
2002-10-19 12:59:30 +00:00
|
|
|
struct netif *netif;
|
2003-01-30 10:18:40 +00:00
|
|
|
|
2015-08-18 16:35:06 +02:00
|
|
|
#if LWIP_MULTICAST_TX_OPTIONS
|
2015-02-25 20:58:11 +01:00
|
|
|
/* Use administratively selected interface for multicast by default */
|
2015-04-09 22:21:15 +02:00
|
|
|
if (ip4_addr_ismulticast(dest) && ip4_default_multicast_netif) {
|
|
|
|
return ip4_default_multicast_netif;
|
2015-02-25 20:58:11 +01:00
|
|
|
}
|
2015-08-18 16:35:06 +02:00
|
|
|
#endif /* LWIP_MULTICAST_TX_OPTIONS */
|
2015-02-25 20:58:11 +01:00
|
|
|
|
2003-06-11 22:34:51 +00:00
|
|
|
/* iterate through netifs */
|
2011-07-21 21:47:25 +02:00
|
|
|
for (netif = netif_list; netif != NULL; netif = netif->next) {
|
2015-03-09 21:35:57 +01:00
|
|
|
/* is the netif up, does it have a link and a valid address? */
|
2015-08-20 22:39:48 +02:00
|
|
|
if (netif_is_up(netif) && netif_is_link_up(netif) && !ip4_addr_isany_val(*netif_ip4_addr(netif))) {
|
2015-03-09 21:35:57 +01:00
|
|
|
/* network mask matches? */
|
2015-08-20 22:39:48 +02:00
|
|
|
if (ip4_addr_netcmp(dest, netif_ip4_addr(netif), netif_ip4_netmask(netif))) {
|
2007-11-28 06:48:50 +00:00
|
|
|
/* return netif on which to forward IP packet */
|
|
|
|
return netif;
|
|
|
|
}
|
2015-09-13 20:00:45 +02:00
|
|
|
/* gateway matches on a non broadcast interface? (i.e. peer in a point to point interface) */
|
2015-09-24 21:04:20 +02:00
|
|
|
if (((netif->flags & NETIF_FLAG_BROADCAST) == 0) && ip4_addr_cmp(dest, netif_ip4_gw(netif))) {
|
2015-09-13 20:00:45 +02:00
|
|
|
/* return netif on which to forward IP packet */
|
|
|
|
return netif;
|
|
|
|
}
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-09 21:35:57 +01:00
|
|
|
|
2015-03-24 21:22:19 +01:00
|
|
|
#if LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF
|
|
|
|
/* loopif is disabled, looopback traffic is passed through any netif */
|
2015-04-09 22:21:15 +02:00
|
|
|
if (ip4_addr_isloopback(dest)) {
|
2015-03-24 21:22:19 +01:00
|
|
|
/* don't check for link on loopback traffic */
|
2016-11-18 22:25:48 +00:00
|
|
|
if (netif_default != NULL && netif_is_up(netif_default)) {
|
2015-03-24 21:22:19 +01:00
|
|
|
return netif_default;
|
|
|
|
}
|
|
|
|
/* default netif is not up, just use any netif for loopback traffic */
|
|
|
|
for (netif = netif_list; netif != NULL; netif = netif->next) {
|
|
|
|
if (netif_is_up(netif)) {
|
|
|
|
return netif;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif /* LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF */
|
|
|
|
|
2016-03-05 15:52:39 +01:00
|
|
|
#ifdef LWIP_HOOK_IP4_ROUTE_SRC
|
|
|
|
netif = LWIP_HOOK_IP4_ROUTE_SRC(dest, NULL);
|
|
|
|
if (netif != NULL) {
|
|
|
|
return netif;
|
|
|
|
}
|
|
|
|
#elif defined(LWIP_HOOK_IP4_ROUTE)
|
|
|
|
netif = LWIP_HOOK_IP4_ROUTE(dest);
|
|
|
|
if (netif != NULL) {
|
|
|
|
return netif;
|
|
|
|
}
|
|
|
|
#endif
|
2017-03-01 16:10:50 +01:00
|
|
|
#endif /* !LWIP_SINGLE_NETIF */
|
2016-03-05 15:52:39 +01:00
|
|
|
|
2015-10-01 21:17:59 +02:00
|
|
|
if ((netif_default == NULL) || !netif_is_up(netif_default) || !netif_is_link_up(netif_default) ||
|
|
|
|
ip4_addr_isany_val(*netif_ip4_addr(netif_default))) {
|
|
|
|
/* No matching netif found and default netif is not usable.
|
|
|
|
If this is not good enough for you, use LWIP_HOOK_IP4_ROUTE() */
|
2016-02-26 22:50:20 +01:00
|
|
|
LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_route: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
|
2015-10-01 21:17:59 +02:00
|
|
|
ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
|
|
|
|
IP_STATS_INC(ip.rterr);
|
|
|
|
MIB2_STATS_INC(mib2.ipoutnoroutes);
|
2015-03-24 21:22:19 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-10-01 21:17:59 +02:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
return netif_default;
|
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2007-06-02 17:12:36 +00:00
|
|
|
#if IP_FORWARD
|
2011-07-21 21:47:25 +02:00
|
|
|
/**
|
|
|
|
* Determine whether an IP address is in a reserved set of addresses
|
|
|
|
* that may not be forwarded, or whether datagrams to that destination
|
|
|
|
* may be forwarded.
|
|
|
|
* @param p the packet to forward
|
|
|
|
* @return 1: can forward 0: discard
|
|
|
|
*/
|
|
|
|
static int
|
2015-04-09 22:21:15 +02:00
|
|
|
ip4_canforward(struct pbuf *p)
|
2011-07-21 21:47:25 +02:00
|
|
|
{
|
2016-10-06 12:55:57 +02:00
|
|
|
u32_t addr = lwip_htonl(ip4_addr_get_u32(ip4_current_dest_addr()));
|
2011-07-21 21:47:25 +02:00
|
|
|
|
|
|
|
if (p->flags & PBUF_FLAG_LLBCAST) {
|
|
|
|
/* don't route link-layer broadcasts */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if ((p->flags & PBUF_FLAG_LLMCAST) && !IP_MULTICAST(addr)) {
|
|
|
|
/* don't route link-layer multicasts unless the destination address is an IP
|
|
|
|
multicast address */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (IP_EXPERIMENTAL(addr)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (IP_CLASSA(addr)) {
|
|
|
|
u32_t net = addr & IP_CLASSA_NET;
|
2011-11-23 09:09:22 +01:00
|
|
|
if ((net == 0) || (net == ((u32_t)IP_LOOPBACKNET << IP_CLASSA_NSHIFT))) {
|
2011-07-21 21:47:25 +02:00
|
|
|
/* don't route loopback packets */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-05-05 13:28:44 +00:00
|
|
|
/**
|
2002-10-19 12:59:30 +00:00
|
|
|
* 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.
|
2007-06-02 17:12:36 +00:00
|
|
|
*
|
|
|
|
* @param p the packet to forward (p->payload points to IP header)
|
|
|
|
* @param iphdr the IP header of the input packet
|
|
|
|
* @param inp the netif on which this packet was received
|
2002-10-19 12:59:30 +00:00
|
|
|
*/
|
2010-05-22 21:11:02 +00:00
|
|
|
static void
|
2015-04-09 22:21:15 +02:00
|
|
|
ip4_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp)
|
2002-10-19 12:59:30 +00:00
|
|
|
{
|
2002-12-18 11:53:26 +00:00
|
|
|
struct netif *netif;
|
2003-06-11 22:34:51 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
PERF_START;
|
2016-08-23 15:26:05 +02:00
|
|
|
LWIP_UNUSED_ARG(inp);
|
2010-03-06 10:15:39 +00:00
|
|
|
|
2015-04-09 22:21:15 +02:00
|
|
|
if (!ip4_canforward(p)) {
|
2011-07-21 21:47:25 +02:00
|
|
|
goto return_noroute;
|
|
|
|
}
|
|
|
|
|
2010-03-06 10:15:39 +00:00
|
|
|
/* RFC3927 2.7: do not forward link-local addresses */
|
2015-04-09 22:21:15 +02:00
|
|
|
if (ip4_addr_islinklocal(ip4_current_dest_addr())) {
|
2016-02-26 22:50:20 +01:00
|
|
|
LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: not forwarding LLA %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
|
2015-08-20 22:39:48 +02:00
|
|
|
ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()),
|
|
|
|
ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr())));
|
2010-05-22 21:11:02 +00:00
|
|
|
goto return_noroute;
|
2010-03-06 10:15:39 +00:00
|
|
|
}
|
|
|
|
|
2003-01-30 10:18:40 +00:00
|
|
|
/* Find network interface where to forward this IP packet to. */
|
2015-08-30 21:54:39 +02:00
|
|
|
netif = ip4_route_src(ip4_current_dest_addr(), ip4_current_src_addr());
|
2003-05-01 13:24:01 +00:00
|
|
|
if (netif == NULL) {
|
2016-02-26 22:50:20 +01:00
|
|
|
LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: no forwarding route for %"U16_F".%"U16_F".%"U16_F".%"U16_F" found\n",
|
2015-08-20 22:39:48 +02:00
|
|
|
ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()),
|
|
|
|
ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr())));
|
2012-03-22 19:35:04 +01:00
|
|
|
/* @todo: send ICMP_DUR_NET? */
|
2010-05-22 21:11:02 +00:00
|
|
|
goto return_noroute;
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2011-07-21 21:47:25 +02:00
|
|
|
#if !IP_FORWARD_ALLOW_TX_ON_RX_NETIF
|
2003-01-30 10:18:40 +00:00
|
|
|
/* Do not forward packets onto the same network interface on which
|
2004-05-05 13:28:44 +00:00
|
|
|
* they arrived. */
|
2003-05-01 13:24:01 +00:00
|
|
|
if (netif == inp) {
|
2016-02-26 22:50:20 +01:00
|
|
|
LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: not bouncing packets back on incoming interface.\n"));
|
2010-05-22 21:11:02 +00:00
|
|
|
goto return_noroute;
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2011-07-21 21:47:25 +02:00
|
|
|
#endif /* IP_FORWARD_ALLOW_TX_ON_RX_NETIF */
|
2003-06-11 22:34:51 +00:00
|
|
|
|
2003-01-30 10:18:40 +00:00
|
|
|
/* decrement TTL */
|
2002-10-19 12:59:30 +00:00
|
|
|
IPH_TTL_SET(iphdr, IPH_TTL(iphdr) - 1);
|
2003-01-30 10:18:40 +00:00
|
|
|
/* send ICMP if TTL == 0 */
|
2003-05-01 13:24:01 +00:00
|
|
|
if (IPH_TTL(iphdr) == 0) {
|
2015-09-08 09:47:30 +02:00
|
|
|
MIB2_STATS_INC(mib2.ipinhdrerrors);
|
2007-09-05 17:20:45 +00:00
|
|
|
#if LWIP_ICMP
|
2002-10-19 12:59:30 +00:00
|
|
|
/* Don't send ICMP messages in response to ICMP messages */
|
2003-05-01 13:24:01 +00:00
|
|
|
if (IPH_PROTO(iphdr) != IP_PROTO_ICMP) {
|
2002-10-19 12:59:30 +00:00
|
|
|
icmp_time_exceeded(p, ICMP_TE_TTL);
|
|
|
|
}
|
2007-09-05 17:20:45 +00:00
|
|
|
#endif /* LWIP_ICMP */
|
2010-05-22 21:11:02 +00:00
|
|
|
return;
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-06-11 22:34:51 +00:00
|
|
|
|
2003-01-30 10:18:40 +00:00
|
|
|
/* Incrementally update the IP checksum. */
|
2011-02-18 13:31:28 +00:00
|
|
|
if (IPH_CHKSUM(iphdr) >= PP_HTONS(0xffffU - 0x100)) {
|
2010-05-16 15:57:42 +00:00
|
|
|
IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + PP_HTONS(0x100) + 1);
|
2002-10-19 12:59:30 +00:00
|
|
|
} else {
|
2010-05-16 15:57:42 +00:00
|
|
|
IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + PP_HTONS(0x100));
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
|
2016-02-26 22:50:20 +01:00
|
|
|
LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: forwarding packet to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
|
2015-08-20 22:39:48 +02:00
|
|
|
ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()),
|
|
|
|
ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr())));
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2003-11-14 13:17:23 +00:00
|
|
|
IP_STATS_INC(ip.fw);
|
2015-09-08 09:47:30 +02:00
|
|
|
MIB2_STATS_INC(mib2.ipforwdatagrams);
|
2003-11-14 13:17:23 +00:00
|
|
|
IP_STATS_INC(ip.xmit);
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2016-02-26 22:50:20 +01:00
|
|
|
PERF_STOP("ip4_forward");
|
2012-03-22 19:35:04 +01:00
|
|
|
/* don't fragment if interface has mtu set to 0 [loopif] */
|
|
|
|
if (netif->mtu && (p->tot_len > netif->mtu)) {
|
|
|
|
if ((IPH_OFFSET(iphdr) & PP_NTOHS(IP_DF)) == 0) {
|
2012-03-25 15:12:42 +02:00
|
|
|
#if IP_FRAG
|
2015-04-09 22:21:15 +02:00
|
|
|
ip4_frag(p, netif, ip4_current_dest_addr());
|
2012-03-25 15:12:42 +02:00
|
|
|
#else /* IP_FRAG */
|
2014-09-18 21:15:13 +02:00
|
|
|
/* @todo: send ICMP Destination Unreachable code 13 "Communication administratively prohibited"? */
|
2012-03-25 15:12:42 +02:00
|
|
|
#endif /* IP_FRAG */
|
2012-03-22 19:35:04 +01:00
|
|
|
} else {
|
2015-09-17 22:19:37 +02:00
|
|
|
#if LWIP_ICMP
|
2014-09-18 21:15:13 +02:00
|
|
|
/* send ICMP Destination Unreachable code 4: "Fragmentation Needed and DF Set" */
|
2012-03-22 19:35:04 +01:00
|
|
|
icmp_dest_unreach(p, ICMP_DUR_FRAG);
|
2015-09-17 22:19:37 +02:00
|
|
|
#endif /* LWIP_ICMP */
|
2012-03-22 19:35:04 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2003-01-30 10:18:40 +00:00
|
|
|
/* transmit pbuf on chosen interface */
|
2015-04-09 22:21:15 +02:00
|
|
|
netif->output(netif, p, ip4_current_dest_addr());
|
2010-05-22 21:11:02 +00:00
|
|
|
return;
|
|
|
|
return_noroute:
|
2015-09-08 09:47:30 +02:00
|
|
|
MIB2_STATS_INC(mib2.ipoutnoroutes);
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
#endif /* IP_FORWARD */
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2017-03-01 14:09:18 +01:00
|
|
|
/** Return true if the current input packet should be accepted on this netif */
|
2017-03-01 14:16:27 +01:00
|
|
|
static int
|
2017-03-01 14:09:18 +01:00
|
|
|
ip4_input_accept(struct netif *netif)
|
|
|
|
{
|
|
|
|
LWIP_DEBUGF(IP_DEBUG, ("ip_input: iphdr->dest 0x%"X32_F" netif->ip_addr 0x%"X32_F" (0x%"X32_F", 0x%"X32_F", 0x%"X32_F")\n",
|
|
|
|
ip4_addr_get_u32(ip4_current_dest_addr()), ip4_addr_get_u32(netif_ip4_addr(netif)),
|
|
|
|
ip4_addr_get_u32(ip4_current_dest_addr()) & ip4_addr_get_u32(netif_ip4_netmask(netif)),
|
|
|
|
ip4_addr_get_u32(netif_ip4_addr(netif)) & ip4_addr_get_u32(netif_ip4_netmask(netif)),
|
|
|
|
ip4_addr_get_u32(ip4_current_dest_addr()) & ~ip4_addr_get_u32(netif_ip4_netmask(netif))));
|
|
|
|
|
|
|
|
/* interface is up and configured? */
|
|
|
|
if ((netif_is_up(netif)) && (!ip4_addr_isany_val(*netif_ip4_addr(netif)))) {
|
|
|
|
/* unicast to this interface address? */
|
|
|
|
if (ip4_addr_cmp(ip4_current_dest_addr(), netif_ip4_addr(netif)) ||
|
|
|
|
/* or broadcast on this interface network address? */
|
|
|
|
ip4_addr_isbroadcast(ip4_current_dest_addr(), netif)
|
|
|
|
#if LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF
|
|
|
|
|| (ip4_addr_get_u32(ip4_current_dest_addr()) == PP_HTONL(IPADDR_LOOPBACK))
|
|
|
|
#endif /* LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF */
|
|
|
|
) {
|
|
|
|
LWIP_DEBUGF(IP_DEBUG, ("ip4_input: packet accepted on interface %c%c\n",
|
|
|
|
netif->name[0], netif->name[1]));
|
|
|
|
/* accept on this netif */
|
2017-03-01 14:16:27 +01:00
|
|
|
return 1;
|
2017-03-01 14:09:18 +01:00
|
|
|
}
|
|
|
|
#if LWIP_AUTOIP
|
|
|
|
/* connections to link-local addresses must persist after changing
|
|
|
|
the netif's address (RFC3927 ch. 1.9) */
|
|
|
|
if (autoip_accept_packet(netif, ip4_current_dest_addr())) {
|
|
|
|
LWIP_DEBUGF(IP_DEBUG, ("ip4_input: LLA packet accepted on interface %c%c\n",
|
|
|
|
netif->name[0], netif->name[1]));
|
|
|
|
/* accept on this netif */
|
2017-03-01 14:16:27 +01:00
|
|
|
return 1;
|
2017-03-01 14:09:18 +01:00
|
|
|
}
|
|
|
|
#endif /* LWIP_AUTOIP */
|
|
|
|
}
|
2017-03-01 14:16:27 +01:00
|
|
|
return 0;
|
2017-03-01 14:09:18 +01:00
|
|
|
}
|
|
|
|
|
2004-05-05 13:28:44 +00:00
|
|
|
/**
|
2002-10-19 12:59:30 +00:00
|
|
|
* 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.
|
2015-10-06 21:08:28 +02:00
|
|
|
*
|
2007-06-02 17:12:36 +00:00
|
|
|
* @param p the received IP packet (p->payload points to IP header)
|
|
|
|
* @param inp the netif on which this packet was received
|
|
|
|
* @return ERR_OK if the packet was processed (could return ERR_* if it wasn't
|
|
|
|
* processed, but currently always returns ERR_OK)
|
2002-10-19 12:59:30 +00:00
|
|
|
*/
|
|
|
|
err_t
|
2015-04-09 22:21:15 +02:00
|
|
|
ip4_input(struct pbuf *p, struct netif *inp)
|
2007-11-28 21:44:17 +00:00
|
|
|
{
|
2004-06-30 18:39:17 +00:00
|
|
|
struct ip_hdr *iphdr;
|
|
|
|
struct netif *netif;
|
2008-01-14 20:53:23 +00:00
|
|
|
u16_t iphdr_hlen;
|
|
|
|
u16_t iphdr_len;
|
2014-12-09 20:56:39 +01:00
|
|
|
#if IP_ACCEPT_LINK_LAYER_ADDRESSING || LWIP_IGMP
|
|
|
|
int check_ip_src = 1;
|
|
|
|
#endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING || LWIP_IGMP */
|
2003-06-11 22:34:51 +00:00
|
|
|
|
2003-11-14 13:17:23 +00:00
|
|
|
IP_STATS_INC(ip.recv);
|
2015-09-08 09:47:30 +02:00
|
|
|
MIB2_STATS_INC(mib2.ipinreceives);
|
2002-10-19 12:59:30 +00:00
|
|
|
|
|
|
|
/* identify the IP header */
|
2010-01-25 08:24:30 +00:00
|
|
|
iphdr = (struct ip_hdr *)p->payload;
|
2003-05-01 13:24:01 +00:00
|
|
|
if (IPH_V(iphdr) != 4) {
|
2016-06-25 13:06:31 +02:00
|
|
|
LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_WARNING, ("IP packet dropped due to bad version number %"U16_F"\n", (u16_t)IPH_V(iphdr)));
|
2015-04-09 22:21:15 +02:00
|
|
|
ip4_debug_print(p);
|
2002-10-19 12:59:30 +00:00
|
|
|
pbuf_free(p);
|
2003-11-14 13:17:23 +00:00
|
|
|
IP_STATS_INC(ip.err);
|
|
|
|
IP_STATS_INC(ip.drop);
|
2015-09-08 09:47:30 +02:00
|
|
|
MIB2_STATS_INC(mib2.ipinhdrerrors);
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
2007-12-30 01:09:11 +00:00
|
|
|
|
2011-07-21 21:47:25 +02:00
|
|
|
#ifdef LWIP_HOOK_IP4_INPUT
|
|
|
|
if (LWIP_HOOK_IP4_INPUT(p, inp)) {
|
|
|
|
/* the packet has been eaten */
|
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-01-30 10:18:40 +00:00
|
|
|
/* obtain IP header length in number of 32-bit words */
|
2008-01-14 20:53:23 +00:00
|
|
|
iphdr_hlen = IPH_HL(iphdr);
|
2003-01-30 10:18:40 +00:00
|
|
|
/* calculate IP header length in bytes */
|
2008-01-14 20:53:23 +00:00
|
|
|
iphdr_hlen *= 4;
|
|
|
|
/* obtain ip length in bytes */
|
2016-10-06 12:55:57 +02:00
|
|
|
iphdr_len = lwip_ntohs(IPH_LEN(iphdr));
|
2003-01-30 10:18:40 +00:00
|
|
|
|
2015-02-25 22:58:27 +01:00
|
|
|
/* Trim pbuf. This is especially required for packets < 60 bytes. */
|
|
|
|
if (iphdr_len < p->tot_len) {
|
|
|
|
pbuf_realloc(p, iphdr_len);
|
|
|
|
}
|
|
|
|
|
2008-01-14 20:53:23 +00:00
|
|
|
/* header length exceeds first pbuf length, or ip length exceeds total pbuf length? */
|
2016-06-17 09:36:14 +02:00
|
|
|
if ((iphdr_hlen > p->len) || (iphdr_len > p->tot_len) || (iphdr_hlen < IP_HLEN)) {
|
|
|
|
if (iphdr_hlen < IP_HLEN) {
|
|
|
|
LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
|
|
|
|
("ip4_input: short IP header (%"U16_F" bytes) received, IP packet dropped\n", iphdr_hlen));
|
|
|
|
}
|
2009-11-22 15:14:46 +00:00
|
|
|
if (iphdr_hlen > p->len) {
|
|
|
|
LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
|
|
|
|
("IP header (len %"U16_F") does not fit in first pbuf (len %"U16_F"), IP packet dropped.\n",
|
|
|
|
iphdr_hlen, p->len));
|
|
|
|
}
|
|
|
|
if (iphdr_len > p->tot_len) {
|
|
|
|
LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
|
|
|
|
("IP (len %"U16_F") is longer than pbuf (len %"U16_F"), IP packet dropped.\n",
|
|
|
|
iphdr_len, p->tot_len));
|
|
|
|
}
|
2003-01-30 10:18:40 +00:00
|
|
|
/* free (drop) packet pbufs */
|
2002-10-19 12:59:30 +00:00
|
|
|
pbuf_free(p);
|
2003-11-14 13:17:23 +00:00
|
|
|
IP_STATS_INC(ip.lenerr);
|
|
|
|
IP_STATS_INC(ip.drop);
|
2015-09-08 09:47:30 +02:00
|
|
|
MIB2_STATS_INC(mib2.ipindiscards);
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* verify checksum */
|
2004-02-20 01:46:03 +00:00
|
|
|
#if CHECKSUM_CHECK_IP
|
2015-09-01 22:04:15 +02:00
|
|
|
IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_CHECK_IP) {
|
|
|
|
if (inet_chksum(iphdr, iphdr_hlen) != 0) {
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2015-09-01 22:04:15 +02:00
|
|
|
LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
|
|
|
|
("Checksum (0x%"X16_F") failed, IP packet dropped.\n", inet_chksum(iphdr, iphdr_hlen)));
|
|
|
|
ip4_debug_print(p);
|
|
|
|
pbuf_free(p);
|
|
|
|
IP_STATS_INC(ip.chkerr);
|
|
|
|
IP_STATS_INC(ip.drop);
|
2015-09-08 09:47:30 +02:00
|
|
|
MIB2_STATS_INC(mib2.ipinhdrerrors);
|
2015-09-01 22:04:15 +02:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2004-02-20 01:46:03 +00:00
|
|
|
#endif
|
2003-06-11 22:34:51 +00:00
|
|
|
|
2010-05-22 21:11:02 +00:00
|
|
|
/* copy IP addresses to aligned ip_addr_t */
|
2015-04-09 22:21:15 +02:00
|
|
|
ip_addr_copy_from_ip4(ip_data.current_iphdr_dest, iphdr->dest);
|
|
|
|
ip_addr_copy_from_ip4(ip_data.current_iphdr_src, iphdr->src);
|
2010-05-22 21:11:02 +00:00
|
|
|
|
2004-06-30 18:39:17 +00:00
|
|
|
/* match packet against an interface, i.e. is this packet for us? */
|
2015-04-09 22:21:15 +02:00
|
|
|
if (ip4_addr_ismulticast(ip4_current_dest_addr())) {
|
2016-03-04 21:57:47 +01:00
|
|
|
#if LWIP_IGMP
|
2015-04-09 22:21:15 +02:00
|
|
|
if ((inp->flags & NETIF_FLAG_IGMP) && (igmp_lookfor_group(inp, ip4_current_dest_addr()))) {
|
2014-12-09 20:56:39 +01:00
|
|
|
/* IGMP snooping switches need 0.0.0.0 to be allowed as source address (RFC 4541) */
|
2015-04-09 22:21:15 +02:00
|
|
|
ip4_addr_t allsystems;
|
2014-12-09 20:56:39 +01:00
|
|
|
IP4_ADDR(&allsystems, 224, 0, 0, 1);
|
2015-04-09 22:21:15 +02:00
|
|
|
if (ip4_addr_cmp(ip4_current_dest_addr(), &allsystems) &&
|
2016-02-26 22:50:20 +01:00
|
|
|
ip4_addr_isany(ip4_current_src_addr())) {
|
2014-12-09 20:56:39 +01:00
|
|
|
check_ip_src = 0;
|
|
|
|
}
|
2007-11-28 21:44:17 +00:00
|
|
|
netif = inp;
|
|
|
|
} else {
|
|
|
|
netif = NULL;
|
|
|
|
}
|
2016-03-04 21:57:47 +01:00
|
|
|
#else /* LWIP_IGMP */
|
|
|
|
if ((netif_is_up(inp)) && (!ip4_addr_isany_val(*netif_ip4_addr(inp)))) {
|
|
|
|
netif = inp;
|
|
|
|
} else {
|
|
|
|
netif = NULL;
|
|
|
|
}
|
2007-08-09 16:53:47 +00:00
|
|
|
#endif /* LWIP_IGMP */
|
2016-03-04 21:57:47 +01:00
|
|
|
} else {
|
2007-12-03 17:55:01 +00:00
|
|
|
/* start trying with inp. if that's not acceptable, start walking the
|
2017-03-01 14:09:18 +01:00
|
|
|
list of configured netifs. */
|
2017-03-01 14:16:27 +01:00
|
|
|
if (ip4_input_accept(inp)) {
|
|
|
|
netif = inp;
|
|
|
|
} else {
|
2017-03-01 21:46:36 +01:00
|
|
|
netif = NULL;
|
2016-11-22 18:36:51 +00:00
|
|
|
#if !LWIP_NETIF_LOOPBACK || LWIP_HAVE_LOOPIF
|
2017-03-01 14:09:18 +01:00
|
|
|
/* Packets sent to the loopback address must not be accepted on an
|
|
|
|
* interface that does not have the loopback address assigned to it,
|
|
|
|
* unless a non-loopback interface is used for loopback traffic. */
|
|
|
|
if (!ip4_addr_isloopback(ip4_current_dest_addr()))
|
2016-11-22 18:36:51 +00:00
|
|
|
#endif /* !LWIP_NETIF_LOOPBACK || LWIP_HAVE_LOOPIF */
|
2017-03-01 14:09:18 +01:00
|
|
|
{
|
2017-03-01 21:46:36 +01:00
|
|
|
#if !LWIP_SINGLE_NETIF
|
2017-03-01 16:10:50 +01:00
|
|
|
NETIF_FOREACH(netif) {
|
2017-03-01 14:09:18 +01:00
|
|
|
if (netif == inp) {
|
|
|
|
/* we checked that before already */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (ip4_input_accept(netif)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-03-01 16:10:50 +01:00
|
|
|
#endif /* !LWIP_SINGLE_NETIF */
|
2007-12-03 17:55:01 +00:00
|
|
|
}
|
2017-03-01 14:09:18 +01:00
|
|
|
}
|
2007-11-28 21:44:17 +00:00
|
|
|
}
|
2007-03-11 19:16:38 +00:00
|
|
|
|
2010-06-16 12:25:00 +00:00
|
|
|
#if IP_ACCEPT_LINK_LAYER_ADDRESSING
|
2003-01-30 10:18:40 +00:00
|
|
|
/* Pass DHCP messages regardless of destination address. DHCP traffic is addressed
|
2004-05-05 13:28:44 +00:00
|
|
|
* using link layer addressing (such as Ethernet MAC) so we must not filter on IP.
|
2004-06-30 18:39:17 +00:00
|
|
|
* According to RFC 1542 section 3.1.1, referred by RFC 2131).
|
2010-06-16 12:25:00 +00:00
|
|
|
*
|
|
|
|
* If you want to accept private broadcast communication while a netif is down,
|
|
|
|
* define LWIP_IP_ACCEPT_UDP_PORT(dst_port), e.g.:
|
|
|
|
*
|
|
|
|
* #define LWIP_IP_ACCEPT_UDP_PORT(dst_port) ((dst_port) == PP_NTOHS(12345))
|
2004-06-30 18:39:17 +00:00
|
|
|
*/
|
2003-05-01 13:24:01 +00:00
|
|
|
if (netif == NULL) {
|
2003-01-30 10:18:40 +00:00
|
|
|
/* remote port is DHCP server? */
|
2003-05-01 13:24:01 +00:00
|
|
|
if (IPH_PROTO(iphdr) == IP_PROTO_UDP) {
|
2010-05-16 15:57:42 +00:00
|
|
|
struct udp_hdr *udphdr = (struct udp_hdr *)((u8_t *)iphdr + iphdr_hlen);
|
2016-02-26 22:50:20 +01:00
|
|
|
LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip4_input: UDP packet to DHCP client port %"U16_F"\n",
|
2016-10-06 12:55:57 +02:00
|
|
|
lwip_ntohs(udphdr->dest)));
|
2010-06-17 11:53:16 +00:00
|
|
|
if (IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(udphdr->dest)) {
|
2016-02-26 22:50:20 +01:00
|
|
|
LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip4_input: DHCP packet accepted.\n"));
|
2003-01-30 10:18:40 +00:00
|
|
|
netif = inp;
|
2008-01-03 17:49:25 +00:00
|
|
|
check_ip_src = 0;
|
2003-01-28 18:24:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-06-16 12:25:00 +00:00
|
|
|
#endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
|
2007-12-30 01:09:11 +00:00
|
|
|
|
|
|
|
/* broadcast or multicast packet source address? Compliant with RFC 1122: 3.2.1.3 */
|
2014-12-09 20:56:39 +01:00
|
|
|
#if LWIP_IGMP || IP_ACCEPT_LINK_LAYER_ADDRESSING
|
|
|
|
if (check_ip_src
|
2010-06-16 12:25:00 +00:00
|
|
|
#if IP_ACCEPT_LINK_LAYER_ADDRESSING
|
2009-10-18 08:23:05 +00:00
|
|
|
/* DHCP servers need 0.0.0.0 to be allowed as source address (RFC 1.1.2.2: 3.2.1.3/a) */
|
2016-02-26 22:50:20 +01:00
|
|
|
&& !ip4_addr_isany_val(*ip4_current_src_addr())
|
2010-06-16 12:25:00 +00:00
|
|
|
#endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
|
2014-12-09 20:56:39 +01:00
|
|
|
)
|
|
|
|
#endif /* LWIP_IGMP || IP_ACCEPT_LINK_LAYER_ADDRESSING */
|
2015-10-06 21:57:40 +02:00
|
|
|
{
|
2016-02-26 22:50:20 +01:00
|
|
|
if ((ip4_addr_isbroadcast(ip4_current_src_addr(), inp)) ||
|
|
|
|
(ip4_addr_ismulticast(ip4_current_src_addr()))) {
|
2007-12-30 01:09:11 +00:00
|
|
|
/* packet source is not valid */
|
2016-02-26 22:50:20 +01:00
|
|
|
LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("ip4_input: packet source is not valid.\n"));
|
2007-12-30 01:09:11 +00:00
|
|
|
/* free (drop) packet pbufs */
|
|
|
|
pbuf_free(p);
|
|
|
|
IP_STATS_INC(ip.drop);
|
2015-09-08 09:47:30 +02:00
|
|
|
MIB2_STATS_INC(mib2.ipinaddrerrors);
|
|
|
|
MIB2_STATS_INC(mib2.ipindiscards);
|
2007-12-30 01:09:11 +00:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-06-30 18:39:17 +00:00
|
|
|
/* packet not for us? */
|
2003-05-01 13:24:01 +00:00
|
|
|
if (netif == NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
/* packet not for us, route or discard */
|
2016-02-26 22:50:20 +01:00
|
|
|
LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip4_input: packet not for us.\n"));
|
2010-05-05 19:49:40 +00:00
|
|
|
#if IP_FORWARD
|
2003-01-30 10:18:40 +00:00
|
|
|
/* non-broadcast packet? */
|
2016-02-26 22:50:20 +01:00
|
|
|
if (!ip4_addr_isbroadcast(ip4_current_dest_addr(), inp)) {
|
2010-05-05 19:49:40 +00:00
|
|
|
/* try to forward IP packet on (other) interfaces */
|
2015-04-09 22:21:15 +02:00
|
|
|
ip4_forward(p, iphdr, inp);
|
2010-05-05 19:49:40 +00:00
|
|
|
} else
|
2002-10-19 12:59:30 +00:00
|
|
|
#endif /* IP_FORWARD */
|
2002-11-21 10:32:19 +00:00
|
|
|
{
|
2016-11-29 15:22:55 -06:00
|
|
|
IP_STATS_INC(ip.drop);
|
2015-09-08 09:47:30 +02:00
|
|
|
MIB2_STATS_INC(mib2.ipinaddrerrors);
|
|
|
|
MIB2_STATS_INC(mib2.ipindiscards);
|
2002-11-21 10:32:19 +00:00
|
|
|
}
|
2002-10-19 12:59:30 +00:00
|
|
|
pbuf_free(p);
|
|
|
|
return ERR_OK;
|
|
|
|
}
|
2004-05-05 13:28:44 +00:00
|
|
|
/* packet consists of multiple fragments? */
|
2010-05-16 15:57:42 +00:00
|
|
|
if ((IPH_OFFSET(iphdr) & PP_HTONS(IP_OFFMASK | IP_MF)) != 0) {
|
2004-05-05 13:28:44 +00:00
|
|
|
#if IP_REASSEMBLY /* packet fragment reassembly code present? */
|
2015-09-01 22:04:15 +02:00
|
|
|
LWIP_DEBUGF(IP_DEBUG, ("IP packet is a fragment (id=0x%04"X16_F" tot_len=%"U16_F" len=%"U16_F" MF=%"U16_F" offset=%"U16_F"), calling ip4_reass()\n",
|
2016-10-06 12:55:57 +02:00
|
|
|
lwip_ntohs(IPH_ID(iphdr)), p->tot_len, lwip_ntohs(IPH_LEN(iphdr)), (u16_t)!!(IPH_OFFSET(iphdr) & PP_HTONS(IP_MF)), (u16_t)((lwip_ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK)*8)));
|
2004-05-05 13:28:44 +00:00
|
|
|
/* reassemble the packet*/
|
2015-04-09 22:21:15 +02:00
|
|
|
p = ip4_reass(p);
|
2004-05-05 13:28:44 +00:00
|
|
|
/* packet not fully reassembled yet? */
|
2003-05-01 13:24:01 +00:00
|
|
|
if (p == NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
2010-03-26 14:05:56 +00:00
|
|
|
iphdr = (struct ip_hdr *)p->payload;
|
2004-05-05 13:28:44 +00:00
|
|
|
#else /* IP_REASSEMBLY == 0, no packet fragment reassembly code present */
|
2002-10-19 12:59:30 +00:00
|
|
|
pbuf_free(p);
|
2009-11-22 15:14:46 +00:00
|
|
|
LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since it was fragmented (0x%"X16_F") (while IP_REASSEMBLY == 0).\n",
|
2016-10-06 12:55:57 +02:00
|
|
|
lwip_ntohs(IPH_OFFSET(iphdr))));
|
2003-11-14 13:17:23 +00:00
|
|
|
IP_STATS_INC(ip.opterr);
|
|
|
|
IP_STATS_INC(ip.drop);
|
2006-08-11 14:12:05 +00:00
|
|
|
/* unsupported protocol feature */
|
2015-09-08 09:47:30 +02:00
|
|
|
MIB2_STATS_INC(mib2.ipinunknownprotos);
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_OK;
|
|
|
|
#endif /* IP_REASSEMBLY */
|
2004-05-05 13:28:44 +00:00
|
|
|
}
|
2003-06-11 22:34:51 +00:00
|
|
|
|
2008-01-09 10:05:23 +00:00
|
|
|
#if IP_OPTIONS_ALLOWED == 0 /* no support for IP options in the IP header? */
|
2007-03-11 19:16:38 +00:00
|
|
|
|
|
|
|
#if LWIP_IGMP
|
|
|
|
/* there is an extra "router alert" option in IGMP messages which we allow for but do not police */
|
2015-10-06 21:57:40 +02:00
|
|
|
if ((iphdr_hlen > IP_HLEN) && (IPH_PROTO(iphdr) != IP_PROTO_IGMP)) {
|
2007-03-11 19:16:38 +00:00
|
|
|
#else
|
2008-01-14 20:53:23 +00:00
|
|
|
if (iphdr_hlen > IP_HLEN) {
|
2007-08-09 16:53:47 +00:00
|
|
|
#endif /* LWIP_IGMP */
|
2009-11-22 15:14:46 +00:00
|
|
|
LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since there were IP options (while IP_OPTIONS_ALLOWED == 0).\n"));
|
2003-06-11 22:34:51 +00:00
|
|
|
pbuf_free(p);
|
2003-11-14 13:17:23 +00:00
|
|
|
IP_STATS_INC(ip.opterr);
|
|
|
|
IP_STATS_INC(ip.drop);
|
2006-08-11 14:12:05 +00:00
|
|
|
/* unsupported protocol feature */
|
2015-09-08 09:47:30 +02:00
|
|
|
MIB2_STATS_INC(mib2.ipinunknownprotos);
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_OK;
|
2003-06-11 22:34:51 +00:00
|
|
|
}
|
2008-01-09 10:05:23 +00:00
|
|
|
#endif /* IP_OPTIONS_ALLOWED == 0 */
|
2002-10-19 12:59:30 +00:00
|
|
|
|
|
|
|
/* send to upper layers */
|
2016-02-26 22:50:20 +01:00
|
|
|
LWIP_DEBUGF(IP_DEBUG, ("ip4_input: \n"));
|
2015-04-09 22:21:15 +02:00
|
|
|
ip4_debug_print(p);
|
2016-02-26 22:50:20 +01:00
|
|
|
LWIP_DEBUGF(IP_DEBUG, ("ip4_input: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len));
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2015-02-12 22:04:10 +01:00
|
|
|
ip_data.current_netif = netif;
|
|
|
|
ip_data.current_input_netif = inp;
|
2011-05-25 17:16:35 +00:00
|
|
|
ip_data.current_ip4_header = iphdr;
|
2011-06-29 19:46:21 +00:00
|
|
|
ip_data.current_ip_header_tot_len = IPH_HL(iphdr) * 4;
|
2009-05-05 17:50:39 +00:00
|
|
|
|
2003-11-14 13:17:23 +00:00
|
|
|
#if LWIP_RAW
|
2004-05-05 13:28:44 +00:00
|
|
|
/* raw input did not eat the packet? */
|
2007-11-28 21:44:17 +00:00
|
|
|
if (raw_input(p, inp) == 0)
|
2003-11-14 13:17:23 +00:00
|
|
|
#endif /* LWIP_RAW */
|
2007-11-28 21:44:17 +00:00
|
|
|
{
|
2014-11-07 14:29:53 +01:00
|
|
|
pbuf_header(p, -(s16_t)iphdr_hlen); /* Move to payload, no check necessary. */
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2007-11-28 21:44:17 +00:00
|
|
|
switch (IPH_PROTO(iphdr)) {
|
2003-11-14 13:17:23 +00:00
|
|
|
#if LWIP_UDP
|
2007-11-28 21:44:17 +00:00
|
|
|
case IP_PROTO_UDP:
|
2007-06-10 12:02:24 +00:00
|
|
|
#if LWIP_UDPLITE
|
2007-11-28 21:44:17 +00:00
|
|
|
case IP_PROTO_UDPLITE:
|
2007-06-10 20:02:43 +00:00
|
|
|
#endif /* LWIP_UDPLITE */
|
2015-09-08 09:47:30 +02:00
|
|
|
MIB2_STATS_INC(mib2.ipindelivers);
|
2007-11-28 21:44:17 +00:00
|
|
|
udp_input(p, inp);
|
|
|
|
break;
|
2002-10-19 12:59:30 +00:00
|
|
|
#endif /* LWIP_UDP */
|
2003-11-14 13:17:23 +00:00
|
|
|
#if LWIP_TCP
|
2007-11-28 21:44:17 +00:00
|
|
|
case IP_PROTO_TCP:
|
2015-09-08 09:47:30 +02:00
|
|
|
MIB2_STATS_INC(mib2.ipindelivers);
|
2007-11-28 21:44:17 +00:00
|
|
|
tcp_input(p, inp);
|
|
|
|
break;
|
2002-10-19 12:59:30 +00:00
|
|
|
#endif /* LWIP_TCP */
|
2007-09-05 17:20:45 +00:00
|
|
|
#if LWIP_ICMP
|
2007-11-28 21:44:17 +00:00
|
|
|
case IP_PROTO_ICMP:
|
2015-09-08 09:47:30 +02:00
|
|
|
MIB2_STATS_INC(mib2.ipindelivers);
|
2007-11-28 21:44:17 +00:00
|
|
|
icmp_input(p, inp);
|
|
|
|
break;
|
2007-09-05 17:20:45 +00:00
|
|
|
#endif /* LWIP_ICMP */
|
2007-03-11 19:16:38 +00:00
|
|
|
#if LWIP_IGMP
|
2007-11-28 21:44:17 +00:00
|
|
|
case IP_PROTO_IGMP:
|
2015-04-09 22:21:15 +02:00
|
|
|
igmp_input(p, inp, ip4_current_dest_addr());
|
2007-11-28 21:44:17 +00:00
|
|
|
break;
|
2007-08-09 16:53:47 +00:00
|
|
|
#endif /* LWIP_IGMP */
|
2007-11-28 21:44:17 +00:00
|
|
|
default:
|
2007-09-05 17:20:45 +00:00
|
|
|
#if LWIP_ICMP
|
2007-11-28 21:44:17 +00:00
|
|
|
/* send ICMP destination protocol unreachable unless is was a broadcast */
|
2016-02-26 22:50:20 +01:00
|
|
|
if (!ip4_addr_isbroadcast(ip4_current_dest_addr(), netif) &&
|
|
|
|
!ip4_addr_ismulticast(ip4_current_dest_addr())) {
|
2014-12-08 14:45:36 +01:00
|
|
|
pbuf_header_force(p, iphdr_hlen); /* Move to ip header, no check necessary. */
|
2007-11-28 21:44:17 +00:00
|
|
|
p->payload = iphdr;
|
|
|
|
icmp_dest_unreach(p, ICMP_DUR_PROTO);
|
|
|
|
}
|
2007-09-05 17:20:45 +00:00
|
|
|
#endif /* LWIP_ICMP */
|
2007-11-28 21:44:17 +00:00
|
|
|
pbuf_free(p);
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2016-06-25 13:06:31 +02:00
|
|
|
LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("Unsupported transport protocol %"U16_F"\n", (u16_t)IPH_PROTO(iphdr)));
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2007-11-28 21:44:17 +00:00
|
|
|
IP_STATS_INC(ip.proterr);
|
|
|
|
IP_STATS_INC(ip.drop);
|
2015-09-08 09:47:30 +02:00
|
|
|
MIB2_STATS_INC(mib2.ipinunknownprotos);
|
2007-11-28 21:44:17 +00:00
|
|
|
}
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2007-11-28 21:44:17 +00:00
|
|
|
|
2011-05-25 17:16:35 +00:00
|
|
|
/* @todo: this is not really necessary... */
|
|
|
|
ip_data.current_netif = NULL;
|
2015-02-12 22:04:10 +01:00
|
|
|
ip_data.current_input_netif = NULL;
|
2011-05-25 17:16:35 +00:00
|
|
|
ip_data.current_ip4_header = NULL;
|
|
|
|
ip_data.current_ip_header_tot_len = 0;
|
2015-04-09 22:21:15 +02:00
|
|
|
ip4_addr_set_any(ip4_current_src_addr());
|
|
|
|
ip4_addr_set_any(ip4_current_dest_addr());
|
2009-05-05 17:50:39 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
|
2004-05-05 13:28:44 +00:00
|
|
|
/**
|
2002-10-19 12:59:30 +00:00
|
|
|
* 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.
|
2016-07-30 13:09:00 +00:00
|
|
|
* If the destination IP address is LWIP_IP_HDRINCL, p is assumed to already
|
2007-06-02 17:12:36 +00:00
|
|
|
* include an IP header and p->payload points to it instead of the data.
|
|
|
|
*
|
|
|
|
* @param p the packet to send (p->payload points to the data, e.g. next
|
2016-07-30 13:09:00 +00:00
|
|
|
protocol header; if dest == LWIP_IP_HDRINCL, p already includes an
|
|
|
|
IP header and p->payload points to that IP header)
|
2016-09-28 12:56:39 +02:00
|
|
|
* @param src the source IP address to send from (if src == IP4_ADDR_ANY, the
|
2007-06-02 17:12:36 +00:00
|
|
|
* IP address of the netif used to send is used as source address)
|
2007-06-08 12:52:46 +00:00
|
|
|
* @param dest the destination IP address to send the packet to
|
2007-06-02 17:12:36 +00:00
|
|
|
* @param ttl the TTL value to be set in the IP header
|
|
|
|
* @param tos the TOS value to be set in the IP header
|
|
|
|
* @param proto the PROTOCOL to be set in the IP header
|
|
|
|
* @param netif the netif on which to send this packet
|
|
|
|
* @return ERR_OK if the packet was sent OK
|
|
|
|
* ERR_BUF if p doesn't have enough space for IP/LINK headers
|
|
|
|
* returns errors returned by netif->output
|
2006-11-01 09:33:14 +00:00
|
|
|
*
|
|
|
|
* @note ip_id: RFC791 "some host may be able to simply use
|
|
|
|
* unique identifiers independent of destination"
|
2002-10-19 12:59:30 +00:00
|
|
|
*/
|
|
|
|
err_t
|
2015-04-09 22:21:15 +02:00
|
|
|
ip4_output_if(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,
|
2003-11-14 13:17:23 +00:00
|
|
|
u8_t ttl, u8_t tos,
|
2003-02-18 21:14:16 +00:00
|
|
|
u8_t proto, struct netif *netif)
|
2002-10-19 12:59:30 +00:00
|
|
|
{
|
2009-05-12 18:00:47 +00:00
|
|
|
#if IP_OPTIONS_SEND
|
2015-04-09 22:21:15 +02:00
|
|
|
return ip4_output_if_opt(p, src, dest, ttl, tos, proto, netif, NULL, 0);
|
2009-05-12 18:00:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Same as ip_output_if() but with the possibility to include IP options:
|
|
|
|
*
|
|
|
|
* @ param ip_options pointer to the IP options, copied into the IP header
|
|
|
|
* @ param optlen length of ip_options
|
|
|
|
*/
|
2016-02-26 22:50:20 +01:00
|
|
|
err_t
|
|
|
|
ip4_output_if_opt(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,
|
2009-05-12 18:00:47 +00:00
|
|
|
u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options,
|
|
|
|
u16_t optlen)
|
|
|
|
{
|
2014-04-06 20:32:37 +02:00
|
|
|
#endif /* IP_OPTIONS_SEND */
|
2015-04-09 22:21:15 +02:00
|
|
|
const ip4_addr_t *src_used = src;
|
2016-07-30 13:09:00 +00:00
|
|
|
if (dest != LWIP_IP_HDRINCL) {
|
2015-04-09 22:21:15 +02:00
|
|
|
if (ip4_addr_isany(src)) {
|
2015-08-20 22:39:48 +02:00
|
|
|
src_used = netif_ip4_addr(netif);
|
2014-04-06 20:32:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if IP_OPTIONS_SEND
|
2015-04-09 22:21:15 +02:00
|
|
|
return ip4_output_if_opt_src(p, src_used, dest, ttl, tos, proto, netif,
|
2014-04-06 20:32:37 +02:00
|
|
|
ip_options, optlen);
|
|
|
|
#else /* IP_OPTIONS_SEND */
|
2015-04-09 22:21:15 +02:00
|
|
|
return ip4_output_if_src(p, src_used, dest, ttl, tos, proto, netif);
|
2014-04-06 20:32:37 +02:00
|
|
|
#endif /* IP_OPTIONS_SEND */
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Same as ip_output_if() but 'src' address is not replaced by netif address
|
|
|
|
* when it is 'any'.
|
|
|
|
*/
|
|
|
|
err_t
|
2015-04-09 22:21:15 +02:00
|
|
|
ip4_output_if_src(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,
|
2014-04-06 20:32:37 +02:00
|
|
|
u8_t ttl, u8_t tos,
|
|
|
|
u8_t proto, struct netif *netif)
|
|
|
|
{
|
|
|
|
#if IP_OPTIONS_SEND
|
2015-04-09 22:21:15 +02:00
|
|
|
return ip4_output_if_opt_src(p, src, dest, ttl, tos, proto, netif, NULL, 0);
|
2014-04-06 20:32:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Same as ip_output_if_opt() but 'src' address is not replaced by netif address
|
|
|
|
* when it is 'any'.
|
|
|
|
*/
|
2016-02-26 22:50:20 +01:00
|
|
|
err_t
|
|
|
|
ip4_output_if_opt_src(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,
|
2014-04-06 20:32:37 +02:00
|
|
|
u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options,
|
|
|
|
u16_t optlen)
|
|
|
|
{
|
2009-05-12 18:00:47 +00:00
|
|
|
#endif /* IP_OPTIONS_SEND */
|
2004-06-30 18:39:17 +00:00
|
|
|
struct ip_hdr *iphdr;
|
2015-04-09 22:21:15 +02:00
|
|
|
ip4_addr_t dest_addr;
|
2010-04-28 19:36:51 +00:00
|
|
|
#if CHECKSUM_GEN_IP_INLINE
|
2010-07-10 12:02:28 +00:00
|
|
|
u32_t chk_sum = 0;
|
2010-04-28 19:36:51 +00:00
|
|
|
#endif /* CHECKSUM_GEN_IP_INLINE */
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2015-01-16 17:58:07 +01:00
|
|
|
LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p);
|
2010-03-09 16:24:38 +00:00
|
|
|
|
2015-09-08 09:47:30 +02:00
|
|
|
MIB2_STATS_INC(mib2.ipoutrequests);
|
2003-06-11 22:34:51 +00:00
|
|
|
|
2007-06-02 17:12:36 +00:00
|
|
|
/* Should the IP header be generated or is it already included in p? */
|
2016-07-30 13:09:00 +00:00
|
|
|
if (dest != LWIP_IP_HDRINCL) {
|
2009-05-12 18:00:47 +00:00
|
|
|
u16_t ip_hlen = IP_HLEN;
|
|
|
|
#if IP_OPTIONS_SEND
|
|
|
|
u16_t optlen_aligned = 0;
|
|
|
|
if (optlen != 0) {
|
2010-07-10 12:02:28 +00:00
|
|
|
#if CHECKSUM_GEN_IP_INLINE
|
|
|
|
int i;
|
|
|
|
#endif /* CHECKSUM_GEN_IP_INLINE */
|
2009-05-12 18:00:47 +00:00
|
|
|
/* round up to a multiple of 4 */
|
|
|
|
optlen_aligned = ((optlen + 3) & ~3);
|
|
|
|
ip_hlen += optlen_aligned;
|
|
|
|
/* First write in the IP options */
|
|
|
|
if (pbuf_header(p, optlen_aligned)) {
|
2016-02-26 22:50:20 +01:00
|
|
|
LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_output_if_opt: not enough room for IP options in pbuf\n"));
|
2009-05-12 18:00:47 +00:00
|
|
|
IP_STATS_INC(ip.err);
|
2015-09-08 09:47:30 +02:00
|
|
|
MIB2_STATS_INC(mib2.ipoutdiscards);
|
2009-05-12 18:00:47 +00:00
|
|
|
return ERR_BUF;
|
|
|
|
}
|
|
|
|
MEMCPY(p->payload, ip_options, optlen);
|
|
|
|
if (optlen < optlen_aligned) {
|
|
|
|
/* zero the remaining bytes */
|
|
|
|
memset(((char*)p->payload) + optlen, 0, optlen_aligned - optlen);
|
|
|
|
}
|
2010-07-10 12:02:28 +00:00
|
|
|
#if CHECKSUM_GEN_IP_INLINE
|
2010-07-21 12:11:22 +00:00
|
|
|
for (i = 0; i < optlen_aligned/2; i++) {
|
2010-07-10 12:02:28 +00:00
|
|
|
chk_sum += ((u16_t*)p->payload)[i];
|
|
|
|
}
|
|
|
|
#endif /* CHECKSUM_GEN_IP_INLINE */
|
2009-05-12 18:00:47 +00:00
|
|
|
}
|
|
|
|
#endif /* IP_OPTIONS_SEND */
|
2007-06-02 17:12:36 +00:00
|
|
|
/* generate IP header */
|
2003-05-01 13:24:01 +00:00
|
|
|
if (pbuf_header(p, IP_HLEN)) {
|
2016-02-26 22:50:20 +01:00
|
|
|
LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_output: not enough room for IP header in pbuf\n"));
|
2003-06-11 22:34:51 +00:00
|
|
|
|
2003-11-14 13:17:23 +00:00
|
|
|
IP_STATS_INC(ip.err);
|
2015-09-08 09:47:30 +02:00
|
|
|
MIB2_STATS_INC(mib2.ipoutdiscards);
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_BUF;
|
|
|
|
}
|
2003-06-11 22:34:51 +00:00
|
|
|
|
2010-01-25 08:24:30 +00:00
|
|
|
iphdr = (struct ip_hdr *)p->payload;
|
2007-07-03 20:28:35 +00:00
|
|
|
LWIP_ASSERT("check that first pbuf can hold struct ip_hdr",
|
|
|
|
(p->len >= sizeof(struct ip_hdr)));
|
2003-06-11 22:34:51 +00:00
|
|
|
|
2010-02-14 11:42:08 +00:00
|
|
|
IPH_TTL_SET(iphdr, ttl);
|
|
|
|
IPH_PROTO_SET(iphdr, proto);
|
2010-04-28 19:36:51 +00:00
|
|
|
#if CHECKSUM_GEN_IP_INLINE
|
2016-12-07 12:44:57 +01:00
|
|
|
chk_sum += PP_NTOHS(proto | (ttl << 8));
|
2010-04-28 19:36:51 +00:00
|
|
|
#endif /* CHECKSUM_GEN_IP_INLINE */
|
2003-06-11 22:34:51 +00:00
|
|
|
|
2010-02-14 12:42:49 +00:00
|
|
|
/* dest cannot be NULL here */
|
2015-04-09 22:21:15 +02:00
|
|
|
ip4_addr_copy(iphdr->dest, *dest);
|
2010-04-28 19:36:51 +00:00
|
|
|
#if CHECKSUM_GEN_IP_INLINE
|
2010-05-12 08:11:45 +00:00
|
|
|
chk_sum += ip4_addr_get_u32(&iphdr->dest) & 0xFFFF;
|
|
|
|
chk_sum += ip4_addr_get_u32(&iphdr->dest) >> 16;
|
2010-04-28 19:36:51 +00:00
|
|
|
#endif /* CHECKSUM_GEN_IP_INLINE */
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2011-07-26 20:55:32 +02:00
|
|
|
IPH_VHL_SET(iphdr, 4, ip_hlen / 4);
|
|
|
|
IPH_TOS_SET(iphdr, tos);
|
2010-04-28 19:36:51 +00:00
|
|
|
#if CHECKSUM_GEN_IP_INLINE
|
2016-12-07 12:44:57 +01:00
|
|
|
chk_sum += PP_NTOHS(tos | (iphdr->_v_hl << 8));
|
2010-04-28 19:36:51 +00:00
|
|
|
#endif /* CHECKSUM_GEN_IP_INLINE */
|
2016-10-06 12:55:57 +02:00
|
|
|
IPH_LEN_SET(iphdr, lwip_htons(p->tot_len));
|
2010-04-28 19:36:51 +00:00
|
|
|
#if CHECKSUM_GEN_IP_INLINE
|
|
|
|
chk_sum += iphdr->_len;
|
|
|
|
#endif /* CHECKSUM_GEN_IP_INLINE */
|
2007-03-30 13:30:42 +00:00
|
|
|
IPH_OFFSET_SET(iphdr, 0);
|
2016-10-06 12:55:57 +02:00
|
|
|
IPH_ID_SET(iphdr, lwip_htons(ip_id));
|
2010-04-28 19:36:51 +00:00
|
|
|
#if CHECKSUM_GEN_IP_INLINE
|
|
|
|
chk_sum += iphdr->_id;
|
|
|
|
#endif /* CHECKSUM_GEN_IP_INLINE */
|
2002-10-19 12:59:30 +00:00
|
|
|
++ip_id;
|
|
|
|
|
2014-04-06 20:32:37 +02:00
|
|
|
if (src == NULL) {
|
2016-09-28 12:56:39 +02:00
|
|
|
ip4_addr_copy(iphdr->src, *IP4_ADDR_ANY4);
|
2002-10-19 12:59:30 +00:00
|
|
|
} else {
|
2010-02-14 12:42:49 +00:00
|
|
|
/* src cannot be NULL here */
|
2015-04-09 22:21:15 +02:00
|
|
|
ip4_addr_copy(iphdr->src, *src);
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
|
2010-04-28 19:36:51 +00:00
|
|
|
#if CHECKSUM_GEN_IP_INLINE
|
2010-05-12 08:11:45 +00:00
|
|
|
chk_sum += ip4_addr_get_u32(&iphdr->src) & 0xFFFF;
|
|
|
|
chk_sum += ip4_addr_get_u32(&iphdr->src) >> 16;
|
2010-04-28 19:36:51 +00:00
|
|
|
chk_sum = (chk_sum >> 16) + (chk_sum & 0xFFFF);
|
|
|
|
chk_sum = (chk_sum >> 16) + chk_sum;
|
|
|
|
chk_sum = ~chk_sum;
|
2015-09-01 22:04:15 +02:00
|
|
|
IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_IP) {
|
|
|
|
iphdr->_chksum = (u16_t)chk_sum; /* network order */
|
|
|
|
}
|
|
|
|
#if LWIP_CHECKSUM_CTRL_PER_NETIF
|
|
|
|
else {
|
|
|
|
IPH_CHKSUM_SET(iphdr, 0);
|
|
|
|
}
|
|
|
|
#endif /* LWIP_CHECKSUM_CTRL_PER_NETIF*/
|
2010-04-28 19:36:51 +00:00
|
|
|
#else /* CHECKSUM_GEN_IP_INLINE */
|
2002-10-19 12:59:30 +00:00
|
|
|
IPH_CHKSUM_SET(iphdr, 0);
|
2004-02-20 01:46:03 +00:00
|
|
|
#if CHECKSUM_GEN_IP
|
2015-09-01 22:04:15 +02:00
|
|
|
IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_IP) {
|
|
|
|
IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, ip_hlen));
|
|
|
|
}
|
|
|
|
#endif /* CHECKSUM_GEN_IP */
|
2010-04-28 19:36:51 +00:00
|
|
|
#endif /* CHECKSUM_GEN_IP_INLINE */
|
2002-10-19 12:59:30 +00:00
|
|
|
} else {
|
2007-06-02 17:12:36 +00:00
|
|
|
/* IP header already included in p */
|
2010-01-25 08:24:30 +00:00
|
|
|
iphdr = (struct ip_hdr *)p->payload;
|
2015-04-09 22:21:15 +02:00
|
|
|
ip4_addr_copy(dest_addr, iphdr->dest);
|
2010-05-22 21:11:02 +00:00
|
|
|
dest = &dest_addr;
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
|
2003-11-14 13:17:23 +00:00
|
|
|
IP_STATS_INC(ip.xmit);
|
|
|
|
|
2016-06-25 13:06:31 +02:00
|
|
|
LWIP_DEBUGF(IP_DEBUG, ("ip4_output_if: %c%c%"U16_F"\n", netif->name[0], netif->name[1], (u16_t)netif->num));
|
2015-04-09 22:21:15 +02:00
|
|
|
ip4_debug_print(p);
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2009-08-30 20:52:43 +00:00
|
|
|
#if ENABLE_LOOPBACK
|
2015-08-20 22:39:48 +02:00
|
|
|
if (ip4_addr_cmp(dest, netif_ip4_addr(netif))
|
2015-03-24 21:22:19 +01:00
|
|
|
#if !LWIP_HAVE_LOOPIF
|
2015-04-09 22:21:15 +02:00
|
|
|
|| ip4_addr_isloopback(dest)
|
2015-03-24 21:22:19 +01:00
|
|
|
#endif /* !LWIP_HAVE_LOOPIF */
|
|
|
|
) {
|
2008-06-12 20:10:08 +00:00
|
|
|
/* Packet to self, enqueue it for loopback */
|
|
|
|
LWIP_DEBUGF(IP_DEBUG, ("netif_loop_output()"));
|
2014-01-17 21:55:46 +01:00
|
|
|
return netif_loop_output(netif, p);
|
2008-06-12 20:10:08 +00:00
|
|
|
}
|
2015-08-05 22:52:23 +02:00
|
|
|
#if LWIP_MULTICAST_TX_OPTIONS
|
2010-07-12 09:34:11 +00:00
|
|
|
if ((p->flags & PBUF_FLAG_MCASTLOOP) != 0) {
|
2014-01-17 21:55:46 +01:00
|
|
|
netif_loop_output(netif, p);
|
2010-07-12 09:34:11 +00:00
|
|
|
}
|
2015-08-05 22:52:23 +02:00
|
|
|
#endif /* LWIP_MULTICAST_TX_OPTIONS */
|
2009-08-30 20:52:43 +00:00
|
|
|
#endif /* ENABLE_LOOPBACK */
|
|
|
|
#if IP_FRAG
|
|
|
|
/* don't fragment if interface has mtu set to 0 [loopif] */
|
|
|
|
if (netif->mtu && (p->tot_len > netif->mtu)) {
|
2015-04-09 22:21:15 +02:00
|
|
|
return ip4_frag(p, netif, dest);
|
2009-08-30 20:52:43 +00:00
|
|
|
}
|
2010-07-12 09:34:11 +00:00
|
|
|
#endif /* IP_FRAG */
|
2009-08-30 20:52:43 +00:00
|
|
|
|
2016-01-18 21:03:00 +01:00
|
|
|
LWIP_DEBUGF(IP_DEBUG, ("ip4_output_if: call netif->output()\n"));
|
2009-08-30 20:52:43 +00:00
|
|
|
return netif->output(netif, p, dest);
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2004-05-05 13:28:44 +00:00
|
|
|
/**
|
2002-10-19 12:59:30 +00:00
|
|
|
* Simple interface to ip_output_if. It finds the outgoing network
|
|
|
|
* interface and calls upon ip_output_if to do the actual work.
|
2007-06-02 17:12:36 +00:00
|
|
|
*
|
2007-11-25 00:28:06 +00:00
|
|
|
* @param p the packet to send (p->payload points to the data, e.g. next
|
2016-07-30 13:09:00 +00:00
|
|
|
protocol header; if dest == LWIP_IP_HDRINCL, p already includes an
|
|
|
|
IP header and p->payload points to that IP header)
|
2016-09-28 12:56:39 +02:00
|
|
|
* @param src the source IP address to send from (if src == IP4_ADDR_ANY, the
|
2007-11-25 00:28:06 +00:00
|
|
|
* IP address of the netif used to send is used as source address)
|
|
|
|
* @param dest the destination IP address to send the packet to
|
|
|
|
* @param ttl the TTL value to be set in the IP header
|
|
|
|
* @param tos the TOS value to be set in the IP header
|
|
|
|
* @param proto the PROTOCOL to be set in the IP header
|
2007-06-02 17:12:36 +00:00
|
|
|
*
|
|
|
|
* @return ERR_RTE if no route is found
|
|
|
|
* see ip_output_if() for more return values
|
2002-10-19 12:59:30 +00:00
|
|
|
*/
|
|
|
|
err_t
|
2015-04-09 22:21:15 +02:00
|
|
|
ip4_output(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,
|
2003-11-14 13:17:23 +00:00
|
|
|
u8_t ttl, u8_t tos, u8_t proto)
|
2002-10-19 12:59:30 +00:00
|
|
|
{
|
2002-12-18 11:53:26 +00:00
|
|
|
struct netif *netif;
|
2003-06-11 22:34:51 +00:00
|
|
|
|
2015-01-16 17:58:07 +01:00
|
|
|
LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p);
|
2010-03-09 16:24:38 +00:00
|
|
|
|
2015-08-30 21:54:39 +02:00
|
|
|
if ((netif = ip4_route_src(dest, src)) == NULL) {
|
2016-02-26 22:50:20 +01:00
|
|
|
LWIP_DEBUGF(IP_DEBUG, ("ip4_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
|
2010-02-04 17:21:32 +00:00
|
|
|
ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
|
2009-04-15 19:32:01 +00:00
|
|
|
IP_STATS_INC(ip.rterr);
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_RTE;
|
|
|
|
}
|
|
|
|
|
2015-04-09 22:21:15 +02:00
|
|
|
return ip4_output_if(p, src, dest, ttl, tos, proto, netif);
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2017-04-27 12:38:53 +02:00
|
|
|
#if LWIP_NETIF_USE_HINTS
|
2009-04-15 19:32:01 +00:00
|
|
|
/** Like ip_output, but takes and addr_hint pointer that is passed on to netif->addr_hint
|
|
|
|
* before calling ip_output_if.
|
|
|
|
*
|
|
|
|
* @param p the packet to send (p->payload points to the data, e.g. next
|
2016-07-30 13:09:00 +00:00
|
|
|
protocol header; if dest == LWIP_IP_HDRINCL, p already includes an
|
|
|
|
IP header and p->payload points to that IP header)
|
2016-09-28 12:56:39 +02:00
|
|
|
* @param src the source IP address to send from (if src == IP4_ADDR_ANY, the
|
2009-04-15 19:32:01 +00:00
|
|
|
* IP address of the netif used to send is used as source address)
|
|
|
|
* @param dest the destination IP address to send the packet to
|
|
|
|
* @param ttl the TTL value to be set in the IP header
|
|
|
|
* @param tos the TOS value to be set in the IP header
|
|
|
|
* @param proto the PROTOCOL to be set in the IP header
|
2017-04-27 12:38:53 +02:00
|
|
|
* @param netif_hint netif output hint pointer set to netif->hint before
|
2009-04-15 19:32:01 +00:00
|
|
|
* calling ip_output_if()
|
|
|
|
*
|
|
|
|
* @return ERR_RTE if no route is found
|
|
|
|
* see ip_output_if() for more return values
|
|
|
|
*/
|
|
|
|
err_t
|
2015-04-09 22:21:15 +02:00
|
|
|
ip4_output_hinted(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest,
|
2017-04-27 12:38:53 +02:00
|
|
|
u8_t ttl, u8_t tos, u8_t proto, struct netif_hint *netif_hint)
|
2009-04-15 19:32:01 +00:00
|
|
|
{
|
|
|
|
struct netif *netif;
|
|
|
|
err_t err;
|
|
|
|
|
2015-01-16 17:58:07 +01:00
|
|
|
LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p);
|
2010-03-09 16:24:38 +00:00
|
|
|
|
2015-08-30 21:54:39 +02:00
|
|
|
if ((netif = ip4_route_src(dest, src)) == NULL) {
|
2016-02-26 22:50:20 +01:00
|
|
|
LWIP_DEBUGF(IP_DEBUG, ("ip4_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
|
2010-02-04 17:21:32 +00:00
|
|
|
ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
|
2009-04-15 19:32:01 +00:00
|
|
|
IP_STATS_INC(ip.rterr);
|
|
|
|
return ERR_RTE;
|
|
|
|
}
|
|
|
|
|
2017-04-27 12:38:53 +02:00
|
|
|
NETIF_SET_HINTS(netif, netif_hint);
|
2015-04-09 22:21:15 +02:00
|
|
|
err = ip4_output_if(p, src, dest, ttl, tos, proto, netif);
|
2017-04-27 12:38:53 +02:00
|
|
|
NETIF_RESET_HINTS(netif);
|
2009-04-15 19:32:01 +00:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
2017-04-27 12:38:53 +02:00
|
|
|
#endif /* LWIP_NETIF_USE_HINTS*/
|
2009-04-15 19:32:01 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
#if IP_DEBUG
|
2007-06-02 17:12:36 +00:00
|
|
|
/* Print an IP header by using LWIP_DEBUGF
|
|
|
|
* @param p an IP packet, p->payload pointing to the IP header
|
|
|
|
*/
|
2002-10-19 12:59:30 +00:00
|
|
|
void
|
2015-04-09 22:21:15 +02:00
|
|
|
ip4_debug_print(struct pbuf *p)
|
2002-10-19 12:59:30 +00:00
|
|
|
{
|
2010-03-26 14:05:56 +00:00
|
|
|
struct ip_hdr *iphdr = (struct ip_hdr *)p->payload;
|
2003-06-11 22:34:51 +00:00
|
|
|
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(IP_DEBUG, ("IP header:\n"));
|
|
|
|
LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
|
2005-11-25 12:03:38 +00:00
|
|
|
LWIP_DEBUGF(IP_DEBUG, ("|%2"S16_F" |%2"S16_F" | 0x%02"X16_F" | %5"U16_F" | (v, hl, tos, len)\n",
|
2016-06-25 13:06:31 +02:00
|
|
|
(u16_t)IPH_V(iphdr),
|
|
|
|
(u16_t)IPH_HL(iphdr),
|
|
|
|
(u16_t)IPH_TOS(iphdr),
|
2016-10-06 12:55:57 +02:00
|
|
|
lwip_ntohs(IPH_LEN(iphdr))));
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
|
2005-11-25 12:03:38 +00:00
|
|
|
LWIP_DEBUGF(IP_DEBUG, ("| %5"U16_F" |%"U16_F"%"U16_F"%"U16_F"| %4"U16_F" | (id, flags, offset)\n",
|
2016-10-06 12:55:57 +02:00
|
|
|
lwip_ntohs(IPH_ID(iphdr)),
|
|
|
|
(u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) >> 15 & 1),
|
|
|
|
(u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) >> 14 & 1),
|
|
|
|
(u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) >> 13 & 1),
|
|
|
|
(u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK)));
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
|
2005-11-25 12:03:38 +00:00
|
|
|
LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | 0x%04"X16_F" | (ttl, proto, chksum)\n",
|
2016-06-25 13:06:31 +02:00
|
|
|
(u16_t)IPH_TTL(iphdr),
|
|
|
|
(u16_t)IPH_PROTO(iphdr),
|
2016-10-06 12:55:57 +02:00
|
|
|
lwip_ntohs(IPH_CHKSUM(iphdr))));
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
|
2005-11-25 12:03:38 +00:00
|
|
|
LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (src)\n",
|
2010-02-04 17:21:32 +00:00
|
|
|
ip4_addr1_16(&iphdr->src),
|
|
|
|
ip4_addr2_16(&iphdr->src),
|
|
|
|
ip4_addr3_16(&iphdr->src),
|
|
|
|
ip4_addr4_16(&iphdr->src)));
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
|
2005-11-25 12:03:38 +00:00
|
|
|
LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (dest)\n",
|
2010-02-04 17:21:32 +00:00
|
|
|
ip4_addr1_16(&iphdr->dest),
|
|
|
|
ip4_addr2_16(&iphdr->dest),
|
|
|
|
ip4_addr3_16(&iphdr->dest),
|
|
|
|
ip4_addr4_16(&iphdr->dest)));
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
#endif /* IP_DEBUG */
|
2015-04-09 22:21:15 +02:00
|
|
|
|
|
|
|
#endif /* LWIP_IPV4 */
|