diff --git a/net/Kconfig b/net/Kconfig index 663e159aeaa..b7f3531bc92 100644 --- a/net/Kconfig +++ b/net/Kconfig @@ -284,40 +284,7 @@ menuconfig NET_IPv6 source "net/neighbor/Kconfig" source "net/sixlowpan/Kconfig" - -config NET_IPFORWARD - bool "Enable L2 forwarding" - default n - ---help--- - Enable forwarding of packets. Packets received with IP addresses - that are not supported by this platform will be forwarded to the - appropriate network device. Routing table support may be required. - -config NET_IPFORWARD_BROADCAST - bool "Forward broadcast/multicast packets" - default n - depends on NET_IPFORWARD && NETDEV_MULTINIC - ---help--- - If selected, broadcast packets received on one network device will - be forwarded though other network devices. - -config NET_IPFORWARD_NSTRUCT - int "Number of pre-allocated forwarding structures" - default 4 - depends on NET_IPFORWARD && NETDEV_MULTINIC - ---help--- - When packets are forwarded from on device to another, a structure - must be allocated to hold the state of forwarding across several - asynchronous events. Those structures are pre-allocated for - minimal, deterministic performance and to prevent hogging of memory - (of course, that means that this value must be carefully selected - for your application). This setting defines the number of such pre- - allocated structures. - - NOTE: This setting effectively puts a maximum on the number of - packets that may be waiting to be forwarded from one network device - to another. CONFIG_IOB_NBUFFERS also limits the forward because the - payload of the packet (up to the MSS) is retain in IOBs. +source "net/ipforward/Kconfig" endmenu # Internet Protocol Selection diff --git a/net/Makefile b/net/Makefile index 25105b0c224..05dff875f5a 100644 --- a/net/Makefile +++ b/net/Makefile @@ -1,7 +1,7 @@ ############################################################################ # net/Makefile # -# Copyright (C) 2007, 2008, 2011-2016 Gregory Nutt. All rights reserved. +# Copyright (C) 2007, 2008, 2011-2017 Gregory Nutt. All rights reserved. # Author: Gregory Nutt # # Redistribution and use in source and binary forms, with or without @@ -68,6 +68,7 @@ include tcp/Make.defs include udp/Make.defs include sixlowpan/Make.defs include devif/Make.defs +include ipforward/Make.defs include loopback/Make.defs include route/Make.defs include procfs/Make.defs diff --git a/net/README.txt b/net/README.txt index beda5229590..683f93bebbf 100644 --- a/net/README.txt +++ b/net/README.txt @@ -8,21 +8,22 @@ Directory Structure | `- net/ | - +- arp - Address resolution protocol (IPv4) - +- devif - Stack/device interface layer - +- icmp - Internet Control Message Protocol (IPv4) - +- icmpv6 - Internet Control Message Protocol (IPv6) - +- local - Unix domain (local) sockets - +- loopback - Local loopback - +- neighbor - Neighbor Discovery Protocol (IPv6) - +- netdev - Socket network device interface - +- pkt - "Raw" packet socket support - +- socket - BSD socket interface - +- route - Routing table support - +- tcp - Transmission Control Protocol - +- udp - User Datagram Protocol - +- usrsock - User socket API for user-space networking stack - `- utils - Miscellaneous utility functions + +- arp - Address resolution protocol (IPv4) + +- devif - Stack/device interface layer + +- icmp - Internet Control Message Protocol (IPv4) + +- icmpv6 - Internet Control Message Protocol (IPv6) + +- ipforward - IP forwarding logic + +- local - Unix domain (local) sockets + +- loopback - Local loopback + +- neighbor - Neighbor Discovery Protocol (IPv6) + +- netdev - Socket network device interface + +- pkt - "Raw" packet socket support + +- socket - BSD socket interface + +- route - Routing table support + +- tcp - Transmission Control Protocol + +- udp - User Datagram Protocol + +- usrsock - User socket API for user-space networking stack + `- utils - Miscellaneous utility functions +-------------------------------------------------------------------++------------------------+ diff --git a/net/devif/Make.defs b/net/devif/Make.defs index 6520b8430ce..22d2ad05ab2 100644 --- a/net/devif/Make.defs +++ b/net/devif/Make.defs @@ -42,21 +42,17 @@ 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) NET_CSRCS += ipv6_input.c -ifeq ($(CONFIG_NET_IPFORWARD),y) -NET_CSRCS += ipv6_forward.c -endif endif +# IP forwarding + ifeq ($(CONFIG_NET_IPFORWARD),y) ifeq ($(CONFIG_NETDEV_MULTINIC),y) -NET_CSRCS += ip_forward.c devif_forward.c +NET_CSRCS += devif_forward.c endif endif diff --git a/net/devif/devif_forward.c b/net/devif/devif_forward.c index 3e9aaf89aa2..a1471d4196c 100644 --- a/net/devif/devif_forward.c +++ b/net/devif/devif_forward.c @@ -43,7 +43,7 @@ #include -#include "devif/ip_forward.h" +#include "ipforward/ip_forward.h" #include "devif/devif.h" #if defined(CONFIG_NET_IPFORWARD) && defined(CONFIG_NETDEV_MULTINIC) diff --git a/net/devif/devif_initialize.c b/net/devif/devif_initialize.c index be858e59204..68ba121afbb 100644 --- a/net/devif/devif_initialize.c +++ b/net/devif/devif_initialize.c @@ -50,7 +50,6 @@ #include #include -#include "devif/ip_forward.h" #include "devif/devif.h" /**************************************************************************** @@ -168,11 +167,5 @@ void devif_initialize(void) /* Initialize callback support */ devif_callback_init(); - -#ifdef HAVE_FWDALLOC - /* Initialize IP forwarding support */ - - ip_forward_initialize(); -#endif } #endif /* CONFIG_NET */ diff --git a/net/devif/ipv4_input.c b/net/devif/ipv4_input.c index 018f747dc0d..b86918516c5 100644 --- a/net/devif/ipv4_input.c +++ b/net/devif/ipv4_input.c @@ -99,7 +99,7 @@ #include "icmp/icmp.h" #include "igmp/igmp.h" -#include "devif/ip_forward.h" +#include "ipforward/ip_forward.h" #include "devif/devif.h" /**************************************************************************** diff --git a/net/devif/ipv6_input.c b/net/devif/ipv6_input.c index b74f1046772..ce8d4f3ea14 100644 --- a/net/devif/ipv6_input.c +++ b/net/devif/ipv6_input.c @@ -100,7 +100,7 @@ #include "icmpv6/icmpv6.h" #include "netdev/netdev.h" -#include "devif/ip_forward.h" +#include "ipforward/ip_forward.h" #include "devif/devif.h" /**************************************************************************** diff --git a/net/icmp/icmp_foward.c b/net/icmp/icmp_foward.c index e1423907d64..12591646e7e 100644 --- a/net/icmp/icmp_foward.c +++ b/net/icmp/icmp_foward.c @@ -49,7 +49,7 @@ #include #include -#include "devif/ip_forward.h" +#include "ipforward/ip_forward.h" #include "devif/devif.h" #include "netdev/netdev.h" #include "arp/arp.h" diff --git a/net/icmpv6/icmpv6_forward.c b/net/icmpv6/icmpv6_forward.c index 17f35f2dd62..d4c1a671860 100644 --- a/net/icmpv6/icmpv6_forward.c +++ b/net/icmpv6/icmpv6_forward.c @@ -49,7 +49,7 @@ #include #include -#include "devif/ip_forward.h" +#include "ipforward/ip_forward.h" #include "devif/devif.h" #include "netdev/netdev.h" #include "arp/arp.h" diff --git a/net/ipforward/Kconfig b/net/ipforward/Kconfig new file mode 100644 index 00000000000..2fc3ddbdc72 --- /dev/null +++ b/net/ipforward/Kconfig @@ -0,0 +1,39 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config NET_IPFORWARD + bool "Enable L2 forwarding" + default n + ---help--- + Enable forwarding of packets. Packets received with IP addresses + that are not supported by this platform will be forwarded to the + appropriate network device. Routing table support may be required. + +config NET_IPFORWARD_BROADCAST + bool "Forward broadcast/multicast packets" + default n + depends on NET_IPFORWARD && NETDEV_MULTINIC + ---help--- + If selected, broadcast packets received on one network device will + be forwarded though other network devices. + +config NET_IPFORWARD_NSTRUCT + int "Number of pre-allocated forwarding structures" + default 4 + depends on NET_IPFORWARD && NETDEV_MULTINIC + ---help--- + When packets are forwarded from on device to another, a structure + must be allocated to hold the state of forwarding across several + asynchronous events. Those structures are pre-allocated for + minimal, deterministic performance and to prevent hogging of memory + (of course, that means that this value must be carefully selected + for your application). This setting defines the number of such pre- + allocated structures. + + NOTE: This setting effectively puts a maximum on the number of + packets that may be waiting to be forwarded from one network device + to another. CONFIG_IOB_NBUFFERS also limits the forward because the + payload of the packet (up to the MSS) is retain in IOBs. + diff --git a/net/ipforward/Make.defs b/net/ipforward/Make.defs new file mode 100644 index 00000000000..b2f661e2d26 --- /dev/null +++ b/net/ipforward/Make.defs @@ -0,0 +1,57 @@ +############################################################################ +# net/ipforward/Make.defs +# +# Copyright (C) 2017 Gregory Nutt. All rights reserved. +# Author: Gregory Nutt +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# 3. Neither the name NuttX nor the names of its contributors may be +# used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +############################################################################ + +# IP forwarding source files + +ifeq ($(CONFIG_NET_IPFORWARD),y) + +ifeq ($(CONFIG_NET_IPv4),y) +NET_CSRCS += ipv4_forward.c +endif + +ifeq ($(CONFIG_NET_IPv6),y) +NET_CSRCS += ipv6_forward.c +endif + +ifeq ($(CONFIG_NETDEV_MULTINIC),y) +NET_CSRCS += ip_forward.c +endif + +# Include IP forwaring build support + +DEPPATH += --dep-path ipforward +VPATH += :ipforward + +endif diff --git a/net/devif/ip_forward.c b/net/ipforward/ip_forward.c similarity index 98% rename from net/devif/ip_forward.c rename to net/ipforward/ip_forward.c index 4c5ac64ff28..baa9ebfe35b 100644 --- a/net/devif/ip_forward.c +++ b/net/ipforward/ip_forward.c @@ -1,5 +1,5 @@ /**************************************************************************** - * net/devif/ip_forward.c + * net/ipforward/ip_forward.c * * Copyright (C) 2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt @@ -44,7 +44,7 @@ #include #include -#include "devif/ip_forward.h" +#include "ipforward/ip_forward.h" #if defined(CONFIG_NET_IPFORWARD) && defined(CONFIG_NETDEV_MULTINIC) diff --git a/net/devif/ip_forward.h b/net/ipforward/ip_forward.h similarity index 97% rename from net/devif/ip_forward.h rename to net/ipforward/ip_forward.h index 40c7c757da8..3a99ce30c86 100644 --- a/net/devif/ip_forward.h +++ b/net/ipforward/ip_forward.h @@ -1,5 +1,5 @@ /**************************************************************************** - * net/devif/ip_forward.h + * net/ipforward/ip_forward.h * * Copyright (C) 2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt @@ -33,8 +33,8 @@ * ****************************************************************************/ -#ifndef __NET_DEVIF_IP_FORWARD_H -#define __NET_DEVIF_IP_FORWARD_H +#ifndef __NET_IPFORWARD_IP_FORWARD_H +#define __NET_IPFORWARD_IP_FORWARD_H /**************************************************************************** * Included Files @@ -163,6 +163,9 @@ struct forward_s FAR struct iob_s *f_iob; /* IOB chain containing the packet */ FAR struct devif_callback_s *f_cb; /* Reference to callback instance */ union fwd_conn_u f_conn; /* Protocol-specific connection struct */ +#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6) + uint8_t f_domain; /* Domain: PF_INET or PF_INET6 */ +#endif }; /**************************************************************************** @@ -356,4 +359,4 @@ int ipv6_forward(FAR struct net_driver_s *dev, FAR struct ipv6_hdr_s *ipv6); #endif #endif /* CONFIG_NET_IPFORWARD */ -#endif /* __NET_DEVIF_IP_FORWARD_H */ +#endif /* __NET_IPFORWARD_IP_FORWARD_H */ diff --git a/net/devif/ipv4_forward.c b/net/ipforward/ipv4_forward.c similarity index 98% rename from net/devif/ipv4_forward.c rename to net/ipforward/ipv4_forward.c index 1614808f9f0..303815553ca 100644 --- a/net/devif/ipv4_forward.c +++ b/net/ipforward/ipv4_forward.c @@ -1,5 +1,5 @@ /**************************************************************************** - * net/devif/ipv4_forward.c + * net/ipforward/ipv4_forward.c * * Copyright (C) 2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt @@ -54,7 +54,7 @@ #include "udp/udp.h" #include "tcp/tcp.h" #include "icmp/icmp.h" -#include "devif/ip_forward.h" +#include "ipforward/ip_forward.h" #include "devif/devif.h" #if defined(CONFIG_NET_IPFORWARD) && defined(CONFIG_NET_IPv4) @@ -297,7 +297,10 @@ static int ipv4_dev_forward(FAR struct net_driver_s *dev, /* Initialize the easy stuff in the forwarding structure */ - fwd->f_dev = fwddev; /* Forwarding device */ + fwd->f_dev = fwddev; /* Forwarding device */ +#ifdef CONFIG_NET_IPv5 + fwd->f_domain = PF_INET; /* IPv64 address domain */ +#endif #ifdef CONFIG_DEBUG_NET_WARN /* Get the size of the IPv4 + L3 header. */ diff --git a/net/devif/ipv6_forward.c b/net/ipforward/ipv6_forward.c similarity index 99% rename from net/devif/ipv6_forward.c rename to net/ipforward/ipv6_forward.c index cd33b54d860..6fb33d56338 100644 --- a/net/devif/ipv6_forward.c +++ b/net/ipforward/ipv6_forward.c @@ -1,5 +1,5 @@ /**************************************************************************** - * net/devif/ipv6_forward.c + * net/ipforward/ipv6_forward.c * * Copyright (C) 2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt @@ -53,7 +53,7 @@ #include "udp/udp.h" #include "tcp/tcp.h" #include "icmpv6/icmpv6.h" -#include "devif/ip_forward.h" +#include "ipforward/ip_forward.h" #include "devif/devif.h" #if defined(CONFIG_NET_IPFORWARD) && defined(CONFIG_NET_IPv6) @@ -408,7 +408,10 @@ static int ipv6_dev_forward(FAR struct net_driver_s *dev, /* Initialize the easy stuff in the forwarding structure */ - fwd->f_dev = fwddev; /* Forwarding device */ + fwd->f_dev = fwddev; /* Forwarding device */ +#ifdef CONFIG_NET_IPv4 + fwd->f_domain = PF_INET6; /* IPv6 address domain */ +#endif #ifdef CONFIG_DEBUG_NET_WARN /* Get the size of the IPv6 + L3 header. */ diff --git a/net/net_initialize.c b/net/net_initialize.c index a5e0c2d31e4..ae8e1d25bc7 100644 --- a/net/net_initialize.c +++ b/net/net_initialize.c @@ -47,6 +47,7 @@ #include "socket/socket.h" #include "devif/devif.h" #include "netdev/netdev.h" +#include "ipforward/ip_forward.h" #include "arp/arp.h" #include "sixlowpan/sixlowpan.h" #include "neighbor/neighbor.h" @@ -112,6 +113,12 @@ void net_setup(void) devif_initialize(); +#ifdef HAVE_FWDALLOC + /* Initialize IP forwarding support */ + + ip_forward_initialize(); +#endif + #ifdef CONFIG_NET_PKT /* Initialize packet socket support */ diff --git a/net/tcp/tcp.h b/net/tcp/tcp.h index 8b554fff864..9497e426ec1 100644 --- a/net/tcp/tcp.h +++ b/net/tcp/tcp.h @@ -807,8 +807,10 @@ void tcp_send(FAR struct net_driver_s *dev, FAR struct tcp_conn_s *conn, * send to complete. * * Input Parameters: - * fwd - An initialized instance of the common forwarding structure that - * includes everything needed to perform the forwarding operation. + * domain - Either PF_INET or PF_INET6 + * fwd - An initialized instance of the common forwarding structure + * that includes everything needed to perform the forwarding + * operation. * * Returned Value: * Zero is returned if the packet was successfully forwarded; A negated diff --git a/net/tcp/tcp_forward.c b/net/tcp/tcp_forward.c index cc85f05eb63..d7c8ba01e5d 100644 --- a/net/tcp/tcp_forward.c +++ b/net/tcp/tcp_forward.c @@ -50,7 +50,7 @@ #include #include -#include "devif/ip_forward.h" +#include "ipforward/ip_forward.h" #include "devif/devif.h" #include "netdev/netdev.h" #include "arp/arp.h" @@ -89,7 +89,7 @@ static inline void forward_ipselect(FAR struct forward_s *fwd) { /* Which domain the connection support */ - if (fwd->f_conn.tcp.domain == PF_INET) + if (fwd->f_domain == PF_INET) { /* Select the IPv4 domain */ @@ -151,7 +151,7 @@ static inline bool tcp_forward_addrchck(FAR struct forward_s *fwd) #ifdef CONFIG_NET_IPv4 #ifdef CONFIG_NET_IPv6 - if (conn->domain == PF_INET) + if (fwd->f_domain == PF_INET) #endif { #if !defined(CONFIG_NET_ARP_IPIN) && !defined(CONFIG_NET_ARP_SEND) @@ -207,7 +207,7 @@ static void tcp_dropstats(FAR struct forward_s *fwd) #ifdef CONFIG_NET_IPv4 #ifdef CONFIG_NET_IPv6 - if (fwd->f_conn.tcp.domain == PF_INET) + if (fwd->f_domain == PF_INET) #endif { g_netstats.ipv4.drop++; @@ -389,7 +389,7 @@ int tcp_forward(FAR struct forward_s *fwd) #ifdef CONFIG_NET_IPv4 #ifdef CONFIG_NET_IPv6 - if ((iphdr->ipv4.l2.vhl & IP_VERSION_MASK) == IPv4_VERSION) + if (fwd->f_domain == PF_INET) #endif { FAR struct ipv4_hdr_s *ipv4 = &iphdr->ipv4.l2; diff --git a/net/udp/udp_forward.c b/net/udp/udp_forward.c index 8c0473592d9..86264637f30 100644 --- a/net/udp/udp_forward.c +++ b/net/udp/udp_forward.c @@ -49,7 +49,7 @@ #include #include -#include "devif/ip_forward.h" +#include "ipforward/ip_forward.h" #include "devif/devif.h" #include "netdev/netdev.h" #include "arp/arp.h" @@ -90,7 +90,7 @@ static inline void forward_ipselect(FAR struct forward_s *fwd) /* Select IPv4 or IPv6 */ - if ((iphdr->ipv4.l2.vhl & IP_VERSION_MASK) == IPv4_VERSION) + if (fwd->f_domain == PF_INET) { udp_ipv4_select(fwd->f_dev); } @@ -151,7 +151,7 @@ static inline bool udp_forward_addrchk(FAR struct forward_s *fwd) #ifdef CONFIG_NET_IPv4 #ifdef CONFIG_NET_IPv6 - if ((iphdr->ipv4.l2.vhl & IP_VERSION_MASK) == IPv4_VERSION) + if (fwd->f_domain == PF_INET) #endif { #if !defined(CONFIG_NET_ARP_IPIN) && !defined(CONFIG_NET_ARP_SEND) @@ -209,7 +209,7 @@ static void udp_dropstats(FAR struct forward_s *fwd) #ifdef CONFIG_NET_IPv4 #ifdef CONFIG_NET_IPv6 - if ((iphdr->ipv4.l2.vhl & IP_VERSION_MASK) == IPv4_VERSION) + if (fwd->f_domain == PF_INET) #endif { g_netstats.ipv4.drop++;