2017-07-05 23:12:29 +02:00
|
|
|
/****************************************************************************
|
2017-07-08 02:33:06 +02:00
|
|
|
* net/ipforward/ipv4_forward.c
|
2017-07-05 23:12:29 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2017-07-05 23:12:29 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-07-05 23:12:29 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2017-07-05 23:12:29 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
2021-06-08 20:00:55 +02:00
|
|
|
#include <assert.h>
|
2017-07-05 23:12:29 +02:00
|
|
|
#include <debug.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <nuttx/mm/iob.h>
|
|
|
|
#include <nuttx/net/net.h>
|
|
|
|
#include <nuttx/net/netdev.h>
|
|
|
|
#include <nuttx/net/netstats.h>
|
|
|
|
|
|
|
|
#include "netdev/netdev.h"
|
2017-07-06 15:26:01 +02:00
|
|
|
#include "utils/utils.h"
|
2017-07-05 23:12:29 +02:00
|
|
|
#include "sixlowpan/sixlowpan.h"
|
2017-07-08 02:45:58 +02:00
|
|
|
#include "ipforward/ipforward.h"
|
2017-07-05 23:12:29 +02:00
|
|
|
#include "devif/devif.h"
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET_IPFORWARD) && defined(CONFIG_NET_IPv4)
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ipv4_hdrsize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Return the size of the IPv4 header and the following.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* ipv4 - A pointer to the IPv4 header in within the IPv4 packet. This
|
2020-02-23 09:50:23 +01:00
|
|
|
* is immediately followed by the L3 header which may be TCP, UDP
|
2017-07-05 23:12:29 +02:00
|
|
|
* or ICMP.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The size of the combined L2 + L3 headers is returned on success. An
|
|
|
|
* error is returned only if the prototype is not supported.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-02-20 19:23:48 +01:00
|
|
|
#ifdef CONFIG_DEBUG_NET_WARN
|
2017-07-05 23:12:29 +02:00
|
|
|
static int ipv4_hdrsize(FAR struct ipv4_hdr_s *ipv4)
|
|
|
|
{
|
2019-08-31 20:25:30 +02:00
|
|
|
uint16_t iphdrlen;
|
|
|
|
|
|
|
|
/* Get the IP header length (accounting for possible options). */
|
|
|
|
|
|
|
|
iphdrlen = (ipv4->vhl & IPv4_HLMASK) << 2;
|
|
|
|
|
|
|
|
/* Size is also determined by the following protocol header, */
|
2017-07-05 23:12:29 +02:00
|
|
|
|
|
|
|
switch (ipv4->proto)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_NET_TCP
|
|
|
|
case IP_PROTO_TCP:
|
|
|
|
{
|
|
|
|
FAR struct tcp_hdr_s *tcp =
|
2019-08-31 20:25:30 +02:00
|
|
|
(FAR struct tcp_hdr_s *)((FAR uint8_t *)ipv4 + iphdrlen);
|
2017-07-05 23:12:29 +02:00
|
|
|
unsigned int tcpsize;
|
|
|
|
|
|
|
|
/* The TCP header length is encoded in the top 4 bits of the
|
|
|
|
* tcpoffset field (in units of 32-bit words).
|
|
|
|
*/
|
|
|
|
|
|
|
|
tcpsize = ((uint16_t)tcp->tcpoffset >> 4) << 2;
|
2019-08-31 20:25:30 +02:00
|
|
|
return iphdrlen + tcpsize;
|
2017-07-05 23:12:29 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_UDP
|
|
|
|
case IP_PROTO_UDP:
|
2019-08-31 20:25:30 +02:00
|
|
|
return iphdrlen + UDP_HDRLEN;
|
2017-07-05 23:12:29 +02:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_ICMP
|
|
|
|
case IP_PROTO_ICMP:
|
2019-08-31 20:25:30 +02:00
|
|
|
return iphdrlen + ICMP_HDRLEN;
|
2017-07-05 23:12:29 +02:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
|
|
|
nwarn("WARNING: Unrecognized proto: %u\n", ipv4->proto);
|
|
|
|
return -EPROTONOSUPPORT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-07-06 15:26:01 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: ipv4_decr_ttl
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Decrement the IPv4 TTL (time to live value). TTL field is set by the
|
|
|
|
* sender of the packet and reduced by every router on the route to its
|
|
|
|
* destination. If the TTL field reaches zero before the datagram arrives
|
|
|
|
* at its destination, then the datagram is discarded and an ICMP error
|
|
|
|
* packet (11 - Time Exceeded) is sent back to the sender.
|
|
|
|
*
|
|
|
|
* The purpose of the TTL field is to avoid a situation in which an
|
|
|
|
* undeliverable datagram keeps circulating on an Internet system, and
|
|
|
|
* such a system eventually becoming swamped by such "immortals".
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* ipv4 - A pointer to the IPv4 header in within the IPv4 packet to be
|
|
|
|
* forwarded.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The new TTL value is returned. A value <= 0 means the hop limit has
|
|
|
|
* expired.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ipv4_decr_ttl(FAR struct ipv4_hdr_s *ipv4)
|
|
|
|
{
|
2019-08-31 20:25:30 +02:00
|
|
|
uint16_t iphdrlen;
|
2017-07-06 15:26:01 +02:00
|
|
|
uint16_t sum;
|
2019-09-07 00:01:38 +02:00
|
|
|
int ttl;
|
2017-07-06 15:26:01 +02:00
|
|
|
|
2019-08-31 20:25:30 +02:00
|
|
|
/* Check time-to-live (TTL) */
|
|
|
|
|
|
|
|
ttl = (int)ipv4->ttl - 1;
|
2017-07-06 15:26:01 +02:00
|
|
|
if (ttl <= 0)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_NET_ICMP
|
|
|
|
/* Return an ICMP error packet back to the sender. */
|
2019-10-25 19:31:42 +02:00
|
|
|
|
2017-07-06 15:49:55 +02:00
|
|
|
# warning Missing logic
|
2017-07-06 15:26:01 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Return zero which must cause the packet to be dropped */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the updated TTL value */
|
|
|
|
|
|
|
|
ipv4->ttl = ttl;
|
|
|
|
|
2019-08-31 20:25:30 +02:00
|
|
|
/* Get the IP header length (accounting for possible options). */
|
|
|
|
|
|
|
|
iphdrlen = (ipv4->vhl & IPv4_HLMASK) << 2;
|
|
|
|
|
2017-07-06 15:26:01 +02:00
|
|
|
/* Re-calculate the IPv4 checksum. This checksum is the Internet checksum
|
|
|
|
* of the 20 bytes of the IPv4 header. This checksum will be different
|
|
|
|
* because we just modify the IPv4 TTL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ipv4->ipchksum = 0;
|
2019-08-31 20:25:30 +02:00
|
|
|
sum = chksum(0, (FAR const uint8_t *)ipv4, iphdrlen);
|
2017-07-06 15:26:01 +02:00
|
|
|
if (sum == 0)
|
|
|
|
{
|
|
|
|
sum = 0xffff;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-01-18 08:38:00 +01:00
|
|
|
sum = HTONS(sum);
|
2017-07-06 15:26:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ipv4->ipchksum = ~sum;
|
|
|
|
return ttl;
|
|
|
|
}
|
|
|
|
|
2017-07-05 23:12:29 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: ipv4_dev_forward
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function is called from ipv4_forward when it is necessary to
|
|
|
|
* forward a packet from the current device to different device. In this
|
|
|
|
* case, the forwarding operation must be performed asynchronously when
|
|
|
|
* the TX poll is received from the forwarding device.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - The device on which the packet was received and which
|
|
|
|
* contains the IPv4 packet.
|
|
|
|
* fwdddev - The device on which the packet must be forwarded.
|
|
|
|
* ipv4 - A pointer to the IPv4 header in within the IPv4 packet
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero is returned if the packet was successfully forward; A negated
|
|
|
|
* errno value is returned if the packet is not forwardable. In that
|
|
|
|
* latter case, the caller (ipv4_input()) should drop the packet.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ipv4_dev_forward(FAR struct net_driver_s *dev,
|
|
|
|
FAR struct net_driver_s *fwddev,
|
|
|
|
FAR struct ipv4_hdr_s *ipv4)
|
|
|
|
{
|
|
|
|
FAR struct forward_s *fwd = NULL;
|
2017-07-07 00:51:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_NET_WARN
|
2017-07-05 23:12:29 +02:00
|
|
|
int hdrsize;
|
2017-07-07 00:51:08 +02:00
|
|
|
#endif
|
2017-07-05 23:12:29 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Verify that the full packet will fit within the forwarding devices MTU.
|
|
|
|
* We provide no support for fragmenting forwarded packets.
|
|
|
|
*/
|
|
|
|
|
2018-07-04 22:10:40 +02:00
|
|
|
if (NET_LL_HDRLEN(fwddev) + dev->d_len > NETDEV_PKTSIZE(fwddev))
|
2017-07-05 23:12:29 +02:00
|
|
|
{
|
|
|
|
nwarn("WARNING: Packet > MTU... Dropping\n");
|
|
|
|
ret = -EFBIG;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a pre-allocated forwarding structure, This structure will be
|
|
|
|
* completely zeroed when we receive it.
|
|
|
|
*/
|
|
|
|
|
2017-07-08 02:45:58 +02:00
|
|
|
fwd = ipfwd_alloc();
|
2017-07-05 23:12:29 +02:00
|
|
|
if (fwd == NULL)
|
|
|
|
{
|
|
|
|
nwarn("WARNING: Failed to allocate forwarding structure\n");
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the easy stuff in the forwarding structure */
|
|
|
|
|
2017-07-08 02:33:06 +02:00
|
|
|
fwd->f_dev = fwddev; /* Forwarding device */
|
|
|
|
#ifdef CONFIG_NET_IPv5
|
|
|
|
fwd->f_domain = PF_INET; /* IPv64 address domain */
|
|
|
|
#endif
|
2017-07-05 23:12:29 +02:00
|
|
|
|
2017-07-07 00:51:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_NET_WARN
|
|
|
|
/* Get the size of the IPv4 + L3 header. */
|
2017-07-05 23:12:29 +02:00
|
|
|
|
|
|
|
hdrsize = ipv4_hdrsize(ipv4);
|
|
|
|
if (hdrsize < IPv4_HDRLEN)
|
|
|
|
{
|
|
|
|
nwarn("WARNING: Could not determine L2+L3 header size\n");
|
|
|
|
ret = -EPROTONOSUPPORT;
|
|
|
|
goto errout_with_fwd;
|
|
|
|
}
|
|
|
|
|
2017-07-07 00:19:10 +02:00
|
|
|
/* The L2/L3 headers must fit within one, contiguous IOB. */
|
2017-07-05 23:12:29 +02:00
|
|
|
|
2017-07-07 00:19:10 +02:00
|
|
|
if (hdrsize > CONFIG_IOB_BUFSIZE)
|
2017-07-05 23:12:29 +02:00
|
|
|
{
|
|
|
|
nwarn("WARNING: Header is too big for pre-allocated structure\n");
|
|
|
|
ret = -E2BIG;
|
|
|
|
goto errout_with_fwd;
|
|
|
|
}
|
2017-07-07 00:51:08 +02:00
|
|
|
#endif
|
2017-07-05 23:12:29 +02:00
|
|
|
|
2017-07-07 00:19:10 +02:00
|
|
|
/* Try to allocate the head of an IOB chain. If this fails, the
|
|
|
|
* packet will be dropped; we are not operating in a context
|
|
|
|
* where waiting for an IOB is a good idea
|
2017-07-06 15:26:01 +02:00
|
|
|
*/
|
|
|
|
|
2022-08-08 04:21:03 +02:00
|
|
|
fwd->f_iob = iob_tryalloc(false);
|
2017-07-07 00:19:10 +02:00
|
|
|
if (fwd->f_iob == NULL)
|
2017-07-06 15:26:01 +02:00
|
|
|
{
|
2017-07-07 00:19:10 +02:00
|
|
|
nwarn("WARNING: iob_tryalloc() failed\n");
|
|
|
|
ret = -ENOMEM;
|
2017-07-06 15:26:01 +02:00
|
|
|
goto errout_with_fwd;
|
|
|
|
}
|
|
|
|
|
2017-07-07 00:19:10 +02:00
|
|
|
/* Copy the L2/L3 headers plus any following payload into an IOB chain.
|
|
|
|
* iob_trycopin() will not wait, but will fail there are no available
|
|
|
|
* IOBs.
|
2017-07-05 23:12:29 +02:00
|
|
|
*
|
|
|
|
* REVISIT: Consider an alternative design that does not require data
|
|
|
|
* copying. This would require a pool of d_buf's that are managed by
|
|
|
|
* the network rather than the network device.
|
|
|
|
*/
|
|
|
|
|
2017-07-07 00:19:10 +02:00
|
|
|
ret = iob_trycopyin(fwd->f_iob, (FAR const uint8_t *)ipv4,
|
2022-08-08 04:21:03 +02:00
|
|
|
dev->d_len, 0, false);
|
2017-07-07 00:19:10 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nwarn("WARNING: iob_trycopyin() failed: %d\n", ret);
|
|
|
|
goto errout_with_iobchain;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Decrement the TTL in the copy of the IPv4 header (retaining the
|
|
|
|
* original TTL in the source to handle the broadcast case). If the
|
|
|
|
* TLL decrements to zero, then do not forward the packet.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = ipv4_decr_ttl((FAR struct ipv4_hdr_s *)fwd->f_iob->io_data);
|
|
|
|
if (ret < 1)
|
2017-07-05 23:12:29 +02:00
|
|
|
{
|
2017-07-07 00:19:10 +02:00
|
|
|
nwarn("WARNING: Hop limit exceeded... Dropping!\n");
|
|
|
|
ret = -EMULTIHOP;
|
|
|
|
goto errout_with_iobchain;
|
2017-07-05 23:12:29 +02:00
|
|
|
}
|
|
|
|
|
2017-07-08 04:19:26 +02:00
|
|
|
/* Then set up to forward the packet according to the protocol. */
|
2017-07-05 23:12:29 +02:00
|
|
|
|
2017-07-08 04:19:26 +02:00
|
|
|
ret = ipfwd_forward(fwd);
|
2017-07-05 23:12:29 +02:00
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
dev->d_len = 0;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
errout_with_iobchain:
|
|
|
|
if (fwd != NULL && fwd->f_iob != NULL)
|
|
|
|
{
|
2022-08-08 04:21:03 +02:00
|
|
|
iob_free_chain(fwd->f_iob);
|
2017-07-05 23:12:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
errout_with_fwd:
|
|
|
|
if (fwd != NULL)
|
|
|
|
{
|
2017-07-08 02:45:58 +02:00
|
|
|
ipfwd_free(fwd);
|
2017-07-05 23:12:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-07-06 15:26:01 +02:00
|
|
|
* Name: ipv4_forward_callback
|
2017-07-05 23:12:29 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2017-07-06 15:26:01 +02:00
|
|
|
* This function is a callback from netdev_foreach. It implements the
|
2019-09-11 16:56:56 +02:00
|
|
|
* the broadcast forwarding action for each network device (other than, of
|
2017-07-06 15:26:01 +02:00
|
|
|
* course, the device that received the packet).
|
2017-07-05 23:12:29 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2017-07-06 15:26:01 +02:00
|
|
|
* dev - The device on which the packet was received and which contains
|
|
|
|
* the IPv4 packet.
|
|
|
|
* ipv4 - A convenience pointer to the IPv4 header in within the IPv4
|
|
|
|
* packet
|
2017-07-05 23:12:29 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2017-07-06 15:26:01 +02:00
|
|
|
* Typically returns zero (meaning to continue the enumeration), but will
|
|
|
|
* return a non-zero to stop the enumeration if an error occurs.
|
2017-07-05 23:12:29 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-08-08 22:24:12 +02:00
|
|
|
#ifdef CONFIG_NET_IPFORWARD_BROADCAST
|
2017-07-06 15:26:01 +02:00
|
|
|
int ipv4_forward_callback(FAR struct net_driver_s *fwddev, FAR void *arg)
|
2017-07-05 23:12:29 +02:00
|
|
|
{
|
2017-07-06 15:26:01 +02:00
|
|
|
FAR struct net_driver_s *dev = (FAR struct net_driver_s *)arg;
|
|
|
|
FAR struct ipv4_hdr_s *ipv4;
|
|
|
|
int ret;
|
2017-07-05 23:12:29 +02:00
|
|
|
|
2017-07-06 15:26:01 +02:00
|
|
|
DEBUGASSERT(fwddev != NULL && dev != NULL && dev->d_buf != NULL);
|
2017-07-05 23:12:29 +02:00
|
|
|
|
2017-07-06 15:26:01 +02:00
|
|
|
/* Check if we are forwarding on the same device that we received the
|
|
|
|
* packet from.
|
2017-07-05 23:12:29 +02:00
|
|
|
*/
|
|
|
|
|
2017-07-06 15:26:01 +02:00
|
|
|
if (fwddev != dev)
|
2017-07-05 23:12:29 +02:00
|
|
|
{
|
2017-07-06 15:26:01 +02:00
|
|
|
/* Recover the pointer to the IPv4 header in the receiving device's
|
|
|
|
* d_buf.
|
|
|
|
*/
|
2017-07-05 23:12:29 +02:00
|
|
|
|
2017-07-06 15:26:01 +02:00
|
|
|
ipv4 = (FAR struct ipv4_hdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev)];
|
2017-07-05 23:12:29 +02:00
|
|
|
|
2017-07-06 15:26:01 +02:00
|
|
|
/* Send the packet asynchrously on the forwarding device. */
|
2017-07-05 23:12:29 +02:00
|
|
|
|
2017-07-06 15:26:01 +02:00
|
|
|
ret = ipv4_dev_forward(dev, fwddev, ipv4);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nwarn("WARNING: ipv4_dev_forward failed: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2017-07-05 23:12:29 +02:00
|
|
|
}
|
|
|
|
|
2017-07-06 15:26:01 +02:00
|
|
|
return OK;
|
2017-07-05 23:12:29 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ipv4_forward
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function is called from ipv4_input when a packet is received that
|
|
|
|
* is not destined for us. In this case, the packet may need to be
|
|
|
|
* forwarded to another device (or sent back out the same device)
|
|
|
|
* depending configuration, routing table information, and the IPv4
|
|
|
|
* networks served by various network devices.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - The device on which the packet was received and which contains
|
|
|
|
* the IPv4 packet.
|
|
|
|
* ipv4 - A convenience pointer to the IPv4 header in within the IPv4
|
|
|
|
* packet
|
|
|
|
*
|
|
|
|
* On input:
|
|
|
|
* - dev->d_buf holds the received packet.
|
|
|
|
* - dev->d_len holds the length of the received packet MINUS the
|
|
|
|
* size of the L1 header. That was subtracted out by ipv4_input.
|
|
|
|
* - ipv4 points to the IPv4 header with dev->d_buf.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero is returned if the packet was successfully forward; A negated
|
|
|
|
* errno value is returned if the packet is not forwardable. In that
|
|
|
|
* latter case, the caller (ipv4_input()) should drop the packet.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int ipv4_forward(FAR struct net_driver_s *dev, FAR struct ipv4_hdr_s *ipv4)
|
|
|
|
{
|
|
|
|
in_addr_t destipaddr;
|
|
|
|
in_addr_t srcipaddr;
|
|
|
|
FAR struct net_driver_s *fwddev;
|
|
|
|
int ret;
|
|
|
|
|
2017-08-08 22:24:12 +02:00
|
|
|
/* Search for a device that can forward this packet. */
|
2017-07-05 23:12:29 +02:00
|
|
|
|
|
|
|
destipaddr = net_ip4addr_conv32(ipv4->destipaddr);
|
|
|
|
srcipaddr = net_ip4addr_conv32(ipv4->srcipaddr);
|
|
|
|
|
2018-10-29 19:20:44 +01:00
|
|
|
fwddev = netdev_findby_ripv4addr(srcipaddr, destipaddr);
|
2017-07-05 23:12:29 +02:00
|
|
|
if (fwddev == NULL)
|
|
|
|
{
|
|
|
|
nwarn("WARNING: Not routable\n");
|
|
|
|
return (ssize_t)-ENETUNREACH;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we are forwarding on the same device that we received the
|
|
|
|
* packet from.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (fwddev != dev)
|
|
|
|
{
|
|
|
|
/* Send the packet asynchrously on the forwarding device. */
|
|
|
|
|
|
|
|
ret = ipv4_dev_forward(dev, fwddev, ipv4);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-07-06 15:26:01 +02:00
|
|
|
nwarn("WARNING: ipv4_dev_forward failed: %d\n", ret);
|
2017-07-05 23:12:29 +02:00
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Single network device. The use case here is where an endpoint acts
|
2021-02-19 15:01:46 +01:00
|
|
|
* as a hub in a star configuration. This is typical for a wireless
|
|
|
|
* star configuration where not all endpoints are accessible from all
|
|
|
|
* other endpoints, but seems less useful for a wired network.
|
2017-07-05 23:12:29 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_ETHERNET
|
|
|
|
/* REVISIT: For Ethernet we may have to fix up the Ethernet header:
|
|
|
|
* - source MAC, the MAC of the current device.
|
2020-02-22 19:31:14 +01:00
|
|
|
* - dest MAC, the MAC associated with the destination IPv4 address.
|
2017-07-05 23:12:29 +02:00
|
|
|
* This will involve ICMP.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Correct dev->d_buf by adding back the L1 header length */
|
2019-10-25 19:31:42 +02:00
|
|
|
|
2017-07-05 23:12:29 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
nwarn("WARNING: Packet forwarding to same device not supportedN\n");
|
|
|
|
ret = -ENOSYS;
|
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return success. ipv4_input will return to the network driver with
|
|
|
|
* dev->d_len set to the packet size and the network driver will perform
|
|
|
|
* the transfer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
drop:
|
|
|
|
ipv4_dropstats(ipv4);
|
|
|
|
dev->d_len = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-07-06 15:26:01 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: ipv4_forward_broadcast
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function is called from ipv4_input when a broadcast or multicast
|
|
|
|
* packet is received. If CONFIG_NET_IPFORWARD_BROADCAST is enabled, this
|
|
|
|
* function will forward the broadcast packet to other networks through
|
|
|
|
* other network devices.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - The device on which the packet was received and which contains
|
|
|
|
* the IPv4 packet.
|
|
|
|
* ipv4 - A convenience pointer to the IPv4 header in within the IPv4
|
|
|
|
* packet
|
|
|
|
*
|
|
|
|
* On input:
|
|
|
|
* - dev->d_buf holds the received packet.
|
|
|
|
* - dev->d_len holds the length of the received packet MINUS the
|
|
|
|
* size of the L1 header. That was subtracted out by ipv4_input.
|
|
|
|
* - ipv4 points to the IPv4 header with dev->d_buf.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-08-08 22:24:12 +02:00
|
|
|
#ifdef CONFIG_NET_IPFORWARD_BROADCAST
|
2017-07-06 15:26:01 +02:00
|
|
|
void ipv4_forward_broadcast(FAR struct net_driver_s *dev,
|
|
|
|
FAR struct ipv4_hdr_s *ipv4)
|
|
|
|
{
|
|
|
|
/* Don't bother if the TTL would expire */
|
|
|
|
|
|
|
|
if (ipv4->ttl > 1)
|
|
|
|
{
|
|
|
|
/* Forward the the broadcast/multicast packet to all devices except,
|
|
|
|
* of course, the device that received the packet.
|
|
|
|
*/
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
netdev_foreach(ipv4_forward_callback, dev);
|
2017-07-06 15:26:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-07-05 23:12:29 +02:00
|
|
|
#endif /* CONFIG_NET_IPFORWARD && CONFIG_NET_IPv4 */
|