mirror of
https://github.com/apache/nuttx.git
synced 2026-05-24 16:11:56 +08:00
IP Forwarding: Add IPv4 packet forwarding logic. Initial commit is an untested clone of the IPv6 forwarding logic with a few minor logic changes for IPv4.
This commit is contained in:
@@ -42,6 +42,9 @@ NET_CSRCS += devif_callback.c
|
||||
|
||||
ifeq ($(CONFIG_NET_IPv4),y)
|
||||
NET_CSRCS += ipv4_input.c
|
||||
ifeq ($(CONFIG_NET_IPFORWARD),y)
|
||||
NET_CSRCS += ipv4_forward.c
|
||||
endif
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_NET_IPv6),y)
|
||||
|
||||
@@ -425,6 +425,39 @@ uint16_t devif_conn_event(FAR struct net_driver_s *dev, FAR void *pvconn,
|
||||
uint16_t devif_dev_event(FAR struct net_driver_s *dev, void *pvconn,
|
||||
uint16_t flags);
|
||||
|
||||
/****************************************************************************
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#if defined(CONFIG_NET_IPFORWARD) && defined(CONFIG_NET_IPv4)
|
||||
int ipv4_forward(FAR struct net_driver_s *dev, FAR struct ipv4_hdr_s *ipv4);
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: ipv6_forward
|
||||
*
|
||||
|
||||
@@ -47,6 +47,7 @@
|
||||
#include <nuttx/net/ip.h>
|
||||
#include <nuttx/net/udp.h>
|
||||
#include <nuttx/net/tcp.h>
|
||||
#include <nuttx/net/icmp.h>
|
||||
#include <nuttx/net/icmpv6.h>
|
||||
|
||||
#include "udp/udp.h"
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
+63
-31
@@ -152,8 +152,8 @@ static uint8_t g_reassembly_flags;
|
||||
#if defined(CONFIG_NET_TCP_REASSEMBLY) && !defined(CONFIG_NET_IPv6)
|
||||
static uint8_t devif_reassembly(void)
|
||||
{
|
||||
FAR struct ipv4_hdr_s *pbuf = BUF;
|
||||
FAR struct ipv4_hdr_s *pfbuf = FBUF;
|
||||
FAR struct ipv4_hdr_s *ipv4 = BUF;
|
||||
FAR struct ipv4_hdr_s *fipv4 = FBUF;
|
||||
uint16_t offset;
|
||||
uint16_t len;
|
||||
uint16_t i;
|
||||
@@ -165,7 +165,7 @@ static uint8_t devif_reassembly(void)
|
||||
|
||||
if (!g_reassembly_timer)
|
||||
{
|
||||
memcpy(g_reassembly_buffer, &pbuf->vhl, IPv4_HDRLEN);
|
||||
memcpy(g_reassembly_buffer, &ipv4->vhl, IPv4_HDRLEN);
|
||||
g_reassembly_timer = CONFIG_NET_TCP_REASS_MAXAGE;
|
||||
g_reassembly_flags = 0;
|
||||
|
||||
@@ -179,12 +179,12 @@ static uint8_t devif_reassembly(void)
|
||||
* fragment into the buffer.
|
||||
*/
|
||||
|
||||
if (net_ipv4addr_hdrcmp(pbuf->srcipaddr, pfbuf->srcipaddr) &&
|
||||
net_ipv4addr_hdrcmp(pbuf->destipaddr, pfbuf->destipaddr) &&
|
||||
pbuf->g_ipid[0] == pfbuf->g_ipid[0] && pbuf->g_ipid[1] == pfbuf->g_ipid[1])
|
||||
if (net_ipv4addr_hdrcmp(ipv4->srcipaddr, fipv4->srcipaddr) &&
|
||||
net_ipv4addr_hdrcmp(ipv4->destipaddr, fipv4->destipaddr) &&
|
||||
ipv4->g_ipid[0] == fipv4->g_ipid[0] && ipv4->g_ipid[1] == fipv4->g_ipid[1])
|
||||
{
|
||||
len = (pbuf->len[0] << 8) + pbuf->len[1] - (pbuf->vhl & 0x0f) * 4;
|
||||
offset = (((pbuf->ipoffset[0] & 0x3f) << 8) + pbuf->ipoffset[1]) * 8;
|
||||
len = (ipv4->len[0] << 8) + ipv4->len[1] - (ipv4->vhl & 0x0f) * 4;
|
||||
offset = (((ipv4->ipoffset[0] & 0x3f) << 8) + ipv4->ipoffset[1]) * 8;
|
||||
|
||||
/* If the offset or the offset + fragment length overflows the
|
||||
* reassembly buffer, we discard the entire packet.
|
||||
@@ -198,7 +198,8 @@ static uint8_t devif_reassembly(void)
|
||||
|
||||
/* Copy the fragment into the reassembly buffer, at the right offset. */
|
||||
|
||||
memcpy(&g_reassembly_buffer[IPv4_HDRLEN + offset], (char *)pbuf + (int)((pbuf->vhl & 0x0f) * 4), len);
|
||||
memcpy(&g_reassembly_buffer[IPv4_HDRLEN + offset],
|
||||
(FAR char *)ipv4 + (int)((ipv4->vhl & 0x0f) * 4), len);
|
||||
|
||||
/* Update the bitmap. */
|
||||
|
||||
@@ -235,7 +236,7 @@ static uint8_t devif_reassembly(void)
|
||||
* we have received the final fragment.
|
||||
*/
|
||||
|
||||
if ((pbuf->ipoffset[0] & IP_MF) == 0)
|
||||
if ((ipv4->ipoffset[0] & IP_MF) == 0)
|
||||
{
|
||||
g_reassembly_flags |= TCP_REASS_LASTFRAG;
|
||||
g_reassembly_len = offset + len;
|
||||
@@ -271,22 +272,22 @@ static uint8_t devif_reassembly(void)
|
||||
}
|
||||
|
||||
/* If we have come this far, we have a full packet in the buffer,
|
||||
* so we allocate a pbuf and copy the packet into it. We also reset
|
||||
* so we allocate a ipv4 and copy the packet into it. We also reset
|
||||
* the timer.
|
||||
*/
|
||||
|
||||
g_reassembly_timer = 0;
|
||||
memcpy(pbuf, pfbuf, g_reassembly_len);
|
||||
memcpy(ipv4, fipv4, g_reassembly_len);
|
||||
|
||||
/* Pretend to be a "normal" (i.e., not fragmented) IP packet from
|
||||
* now on.
|
||||
*/
|
||||
|
||||
pbuf->ipoffset[0] = pbuf->ipoffset[1] = 0;
|
||||
pbuf->len[0] = g_reassembly_len >> 8;
|
||||
pbuf->len[1] = g_reassembly_len & 0xff;
|
||||
pbuf->ipchksum = 0;
|
||||
pbuf->ipchksum = ~(ipv4_chksum(dev));
|
||||
ipv4->ipoffset[0] = ipv4->ipoffset[1] = 0;
|
||||
ipv4->len[0] = g_reassembly_len >> 8;
|
||||
ipv4->len[1] = g_reassembly_len & 0xff;
|
||||
ipv4->ipchksum = 0;
|
||||
ipv4->ipchksum = ~(ipv4_chksum(dev));
|
||||
|
||||
return g_reassembly_len;
|
||||
}
|
||||
@@ -316,7 +317,7 @@ nullreturn:
|
||||
|
||||
int ipv4_input(FAR struct net_driver_s *dev)
|
||||
{
|
||||
FAR struct ipv4_hdr_s *pbuf = BUF;
|
||||
FAR struct ipv4_hdr_s *ipv4 = BUF;
|
||||
uint16_t hdrlen;
|
||||
uint16_t iplen;
|
||||
|
||||
@@ -329,7 +330,7 @@ int ipv4_input(FAR struct net_driver_s *dev)
|
||||
/* Start of IP input header processing code. */
|
||||
/* Check validity of the IP header. */
|
||||
|
||||
if (pbuf->vhl != 0x45)
|
||||
if (ipv4->vhl != 0x45)
|
||||
{
|
||||
/* IP version and header length. */
|
||||
|
||||
@@ -338,7 +339,7 @@ int ipv4_input(FAR struct net_driver_s *dev)
|
||||
g_netstats.ipv4.vhlerr++;
|
||||
#endif
|
||||
nwarn("WARNING: Invalid IP version or header length: %02x\n",
|
||||
pbuf->vhl);
|
||||
ipv4->vhl);
|
||||
goto drop;
|
||||
}
|
||||
|
||||
@@ -360,7 +361,7 @@ int ipv4_input(FAR struct net_driver_s *dev)
|
||||
* we set d_len to the correct value.
|
||||
*/
|
||||
|
||||
iplen = (pbuf->len[0] << 8) + pbuf->len[1];
|
||||
iplen = (ipv4->len[0] << 8) + ipv4->len[1];
|
||||
if (iplen <= dev->d_len)
|
||||
{
|
||||
dev->d_len = iplen;
|
||||
@@ -373,7 +374,7 @@ int ipv4_input(FAR struct net_driver_s *dev)
|
||||
|
||||
/* Check the fragment flag. */
|
||||
|
||||
if ((pbuf->ipoffset[0] & 0x3f) != 0 || pbuf->ipoffset[1] != 0)
|
||||
if ((ipv4->ipoffset[0] & 0x3f) != 0 || ipv4->ipoffset[1] != 0)
|
||||
{
|
||||
#if defined(CONFIG_NET_TCP_REASSEMBLY)
|
||||
dev->d_len = devif_reassembly();
|
||||
@@ -398,8 +399,8 @@ int ipv4_input(FAR struct net_driver_s *dev)
|
||||
* negotiating over DHCP for an address).
|
||||
*/
|
||||
|
||||
if (pbuf->proto == IP_PROTO_UDP &&
|
||||
net_ipv4addr_cmp(net_ip4addr_conv32(pbuf->destipaddr),
|
||||
if (ipv4->proto == IP_PROTO_UDP &&
|
||||
net_ipv4addr_cmp(net_ip4addr_conv32(ipv4->destipaddr),
|
||||
INADDR_BROADCAST))
|
||||
{
|
||||
return udp_ipv4_input(dev);
|
||||
@@ -416,23 +417,54 @@ int ipv4_input(FAR struct net_driver_s *dev)
|
||||
goto drop;
|
||||
}
|
||||
|
||||
/* Check if the packet is destined for out IP address */
|
||||
/* Check if the packet is destined for our IP address */
|
||||
else
|
||||
#endif
|
||||
{
|
||||
/* Check if the packet is destined for our IP address. */
|
||||
|
||||
if (!net_ipv4addr_cmp(net_ip4addr_conv32(pbuf->destipaddr), dev->d_ipaddr))
|
||||
if (!net_ipv4addr_cmp(net_ip4addr_conv32(ipv4->destipaddr),
|
||||
dev->d_ipaddr))
|
||||
{
|
||||
/* Check for an IPv4 IGMP group address */
|
||||
|
||||
#ifdef CONFIG_NET_IGMP
|
||||
in_addr_t destip = net_ip4addr_conv32(pbuf->destipaddr);
|
||||
in_addr_t destip = net_ip4addr_conv32(ipv4->destipaddr);
|
||||
if (igmp_grpfind(dev, &destip) == NULL)
|
||||
#endif
|
||||
{
|
||||
#ifdef CONFIG_NET_STATISTICS
|
||||
g_netstats.ipv4.drop++;
|
||||
/* No.. The packet is not destined for us. */
|
||||
|
||||
#ifdef CONFIG_NET_IPFORWARD
|
||||
/* Try to forward the packet */
|
||||
|
||||
int ret = ipv4_forward(dev, ipv4);
|
||||
if (ret >= 0)
|
||||
{
|
||||
/* The packet was forwarded. Return success; d_len will
|
||||
* be set appropriately by the forwarding logic: Cleared
|
||||
* if the packet is forward via anoother device or non-
|
||||
* zero if it will be forwarded by the same device that
|
||||
* it was received on.
|
||||
*/
|
||||
|
||||
return OK;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
goto drop;
|
||||
{
|
||||
/* Not destined for us and not forwardable... Drop the
|
||||
* packet.
|
||||
*/
|
||||
|
||||
nwarn("WARNING: Not destined for us; not forwardable... "
|
||||
"Dropping!\n");
|
||||
|
||||
#ifdef CONFIG_NET_STATISTICS
|
||||
g_netstats.ipv4.drop++;
|
||||
#endif
|
||||
goto drop;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -457,7 +489,7 @@ int ipv4_input(FAR struct net_driver_s *dev)
|
||||
|
||||
/* Now process the incoming packet according to the protocol. */
|
||||
|
||||
switch (pbuf->proto)
|
||||
switch (ipv4->proto)
|
||||
{
|
||||
#ifdef NET_TCP_HAVE_STACK
|
||||
case IP_PROTO_TCP: /* TCP input */
|
||||
|
||||
@@ -303,7 +303,7 @@ static int ipv6_dev_forward(FAR struct net_driver_s *dev,
|
||||
if (paysize > 0)
|
||||
{
|
||||
/* Try to allocate the head of an IOB chain. If this fails,
|
||||
* the the packet will be dropped; we are not operating in a
|
||||
* the packet will be dropped; we are not operating in a
|
||||
* context where waiting for an IOB is a good idea
|
||||
*/
|
||||
|
||||
@@ -382,15 +382,15 @@ static int ipv6_dev_forward(FAR struct net_driver_s *dev,
|
||||
|
||||
errout_with_iobchain:
|
||||
if (fwd != NULL && fwd->f_iob != NULL)
|
||||
{
|
||||
iob_free_chain(fwd->f_iob);
|
||||
}
|
||||
{
|
||||
iob_free_chain(fwd->f_iob);
|
||||
}
|
||||
|
||||
errout_with_fwd:
|
||||
if (fwd != NULL)
|
||||
{
|
||||
ip_forward_free(fwd);
|
||||
}
|
||||
{
|
||||
ip_forward_free(fwd);
|
||||
}
|
||||
|
||||
errout:
|
||||
return ret;
|
||||
@@ -532,8 +532,6 @@ static void ipv6_dropstats(FAR struct ipv6_hdr_s *ipv6)
|
||||
|
||||
int ipv6_forward(FAR struct net_driver_s *dev, FAR struct ipv6_hdr_s *ipv6)
|
||||
{
|
||||
/* Multiple network devices */
|
||||
|
||||
FAR struct net_driver_s *fwddev;
|
||||
int ret;
|
||||
|
||||
@@ -548,7 +546,7 @@ int ipv6_forward(FAR struct net_driver_s *dev, FAR struct ipv6_hdr_s *ipv6)
|
||||
}
|
||||
|
||||
/* Search for a device that can forward this packet. This is a trivial
|
||||
* serch if there is only a single network device (CONFIG_NETDEV_MULTINIC
|
||||
* search if there is only a single network device (CONFIG_NETDEV_MULTINIC
|
||||
* not defined). But netdev_findby_ipv6addr() will still assure
|
||||
* routability in that case.
|
||||
*/
|
||||
|
||||
@@ -107,7 +107,7 @@ static inline bool icmpv6_forward_addrchck(FAR struct forward_s *fwd)
|
||||
|
||||
#ifdef CONFIG_NET_IPv4
|
||||
#ifdef CONFIG_NET_IPv6
|
||||
if (conn->domain == PF_INET)
|
||||
if ((fwd->f_hdr.ipv4.l2.vhl & IP_VERSION_MASK) == IPv4_VERSION)
|
||||
#endif
|
||||
{
|
||||
#if !defined(CONFIG_NET_ARP_IPIN) && !defined(CONFIG_NET_ARP_SEND)
|
||||
|
||||
@@ -93,14 +93,14 @@ static inline void forward_ipselect(FAR struct forward_s *fwd)
|
||||
{
|
||||
/* Select the IPv4 domain */
|
||||
|
||||
tcp_ipv4_select(dev);
|
||||
tcp_ipv4_select(fwd->f_dev);
|
||||
}
|
||||
else /* if (conn->domain == PF_INET6) */
|
||||
{
|
||||
/* Select the IPv6 domain */
|
||||
|
||||
DEBUGASSERT(conn->domain == PF_INET6);
|
||||
tcp_ipv6_select(dev);
|
||||
DEBUGASSERT(fwd->f_conn.tcp.domain == PF_INET6);
|
||||
tcp_ipv6_select(fwd->f_dev);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -306,7 +306,7 @@ static uint16_t tcp_forward_interrupt(FAR struct net_driver_s *dev,
|
||||
* place and we need do nothing.
|
||||
*/
|
||||
|
||||
forward_ipselect(dev, fwd);
|
||||
forward_ipselect(fwd);
|
||||
#endif
|
||||
/* Copy the user data into d_appdata and send it. */
|
||||
|
||||
|
||||
@@ -143,7 +143,7 @@ static inline bool udp_forward_addrchk(FAR struct forward_s *fwd)
|
||||
|
||||
#ifdef CONFIG_NET_IPv4
|
||||
#ifdef CONFIG_NET_IPv6
|
||||
if (conn->domain == PF_INET)
|
||||
if ((fwd->f_hdr.ipv4.l2.vhl & IP_VERSION_MASK) == IPv4_VERSION)
|
||||
#endif
|
||||
{
|
||||
#if !defined(CONFIG_NET_ARP_IPIN) && !defined(CONFIG_NET_ARP_SEND)
|
||||
@@ -288,7 +288,7 @@ static uint16_t udp_forward_interrupt(FAR struct net_driver_s *dev,
|
||||
* place and we need do nothing.
|
||||
*/
|
||||
|
||||
forward_ipselect(dev, fwd);
|
||||
forward_ipselect(fwd);
|
||||
#endif
|
||||
/* Copy the user data into d_appdata and send it. */
|
||||
|
||||
|
||||
Reference in New Issue
Block a user