task #7506: added NAT support

This commit is contained in:
goldsimon 2010-05-05 19:34:23 +00:00
parent 4d1ff2418e
commit a7fdb67e8f
6 changed files with 1202 additions and 4 deletions

View File

@ -13,6 +13,9 @@ HISTORY
++ New features: ++ New features:
2010-05-05: Simon Goldschmidt (Christian Walter)
* ip_nat.h/.c, opt.h, ip.c, timers.c: task #7506: added NAT support
2010-05-02: Simon Goldschmidt 2010-05-02: Simon Goldschmidt
* netbuf.h/.c, sockets.c, api_msg.c: use checksum-on-copy for sending * netbuf.h/.c, sockets.c, api_msg.c: use checksum-on-copy for sending
UDP data for LWIP_NETIF_TX_SINGLE_PBUF==1 UDP data for LWIP_NETIF_TX_SINGLE_PBUF==1

View File

@ -43,6 +43,7 @@
#include "lwip/def.h" #include "lwip/def.h"
#include "lwip/mem.h" #include "lwip/mem.h"
#include "lwip/ip_frag.h" #include "lwip/ip_frag.h"
#include "lwip/ip_nat.h"
#include "lwip/inet_chksum.h" #include "lwip/inet_chksum.h"
#include "lwip/netif.h" #include "lwip/netif.h"
#include "lwip/icmp.h" #include "lwip/icmp.h"
@ -377,15 +378,30 @@ ip_input(struct pbuf *p, struct netif *inp)
/* packet not for us? */ /* packet not for us? */
if (netif == NULL) { if (netif == NULL) {
#if IP_FORWARD || IP_NAT
u8_t taken = 0;
#endif /* IP_FORWARD || IP_NAT */
/* packet not for us, route or discard */ /* packet not for us, route or discard */
LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: packet not for us.\n")); LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: packet not for us.\n"));
#if IP_FORWARD #if IP_FORWARD || IP_NAT
/* non-broadcast packet? */ /* non-broadcast packet? */
if (!ip_addr_isbroadcast(&(iphdr->dest), inp)) { if (!ip_addr_isbroadcast(&(iphdr->dest), inp)) {
#if IP_NAT
/* check if we want to perform NAT with this packet. */
taken = ip_nat_out(p);
if (!taken)
#endif /* IP_NAT */
{
#if IP_FORWARD
/* try to forward IP packet on (other) interfaces */ /* try to forward IP packet on (other) interfaces */
ip_forward(p, iphdr, inp); if (ip_forward(p, iphdr, inp) != NULL) {
} else taken = 1;
}
#endif /* IP_FORWARD */ #endif /* IP_FORWARD */
}
}
if (!taken)
#endif /* IP_FORWARD || IP_NAT */
{ {
snmp_inc_ipinaddrerrors(); snmp_inc_ipinaddrerrors();
snmp_inc_ipindiscards(); snmp_inc_ipindiscards();
@ -443,6 +459,12 @@ ip_input(struct pbuf *p, struct netif *inp)
current_netif = inp; current_netif = inp;
current_header = iphdr; current_header = iphdr;
#if IP_NAT
if (!ip_addr_isbroadcast(&(iphdr->dest), inp) &&
(ip_nat_input(p) != 0)) {
LWIP_DEBUGF(IP_DEBUG, ("ip_input: packet consumed by nat layer\n"));
} else
#endif /* IP_NAT */
#if LWIP_RAW #if LWIP_RAW
/* raw input did not eat the packet? */ /* raw input did not eat the packet? */
if (raw_input(p, inp) == 0) if (raw_input(p, inp) == 0)

1097
src/core/ipv4/ip_nat.c Normal file

File diff suppressed because it is too large Load Diff

View File

@ -53,6 +53,7 @@
#include "lwip/autoip.h" #include "lwip/autoip.h"
#include "lwip/igmp.h" #include "lwip/igmp.h"
#include "lwip/dns.h" #include "lwip/dns.h"
#include "lwip/ip_nat.h"
/** The one and only timeout list */ /** The one and only timeout list */
@ -214,6 +215,22 @@ dns_timer(void *arg)
} }
#endif /* LWIP_DNS */ #endif /* LWIP_DNS */
#if IP_NAT
/**
* Timer callback function that calls ip_nat_tmr() and reschedules itself.
*
* @param arg unused argument
*/
static void
nat_timer(void *arg)
{
LWIP_UNUSED_ARG(arg);
LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: nat_timer()\n"));
ip_nat_tmr();
sys_timeout(LWIP_NAT_TMR_INTERVAL_SEC, nat_timer, NULL);
}
#endif /* IP_NAT */
/** Initialize this module */ /** Initialize this module */
void sys_timeouts_init(void) void sys_timeouts_init(void)
{ {
@ -236,6 +253,9 @@ void sys_timeouts_init(void)
#if LWIP_DNS #if LWIP_DNS
sys_timeout(DNS_TMR_INTERVAL, dns_timer, NULL); sys_timeout(DNS_TMR_INTERVAL, dns_timer, NULL);
#endif /* LWIP_DNS */ #endif /* LWIP_DNS */
#if IP_NAT
sys_timeout(LWIP_NAT_TMR_INTERVAL_SEC, nat_timer, NULL);
#endif /* IP_NAT */
#if NO_SYS #if NO_SYS
/* Initialise timestamp for sys_check_timeouts */ /* Initialise timestamp for sys_check_timeouts */

View File

@ -0,0 +1,51 @@
/***************************************************************************
* Template - A brief description for this module.
* Copyright (c) 2008 Christian Walter, © Embedded Solutions, Vienna 2006.
*
* $Id: ip_nat.h,v 1.1 2010/05/05 19:34:23 goldsimon Exp $
***************************************************************************/
#ifndef __LWIP_NAT_H__
#define __LWIP_NAT_H__
#include "lwip/err.h"
#include "lwip/ip_addr.h"
#include "lwip/opt.h"
#if IP_NAT
/** Timer interval at which to call ip_nat_tmr() */
#define LWIP_NAT_TMR_INTERVAL_SEC 5
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
struct netif;
struct pbuf;
typedef struct ip_nat_entry
{
ip_addr_t source_net;
ip_addr_t source_netmask;
ip_addr_t dest_net;
ip_addr_t dest_netmask;
struct netif *out_if;
struct netif *in_if;
} ip_nat_entry_t;
void ip_nat_init(void);
void ip_nat_tmr(void);
u8_t ip_nat_input(struct pbuf *p);
u8_t ip_nat_out(struct pbuf *p);
err_t ip_nat_add(const ip_nat_entry_t *new_entry);
void ip_nat_remove(const ip_nat_entry_t *remove_entry);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* IP_NAT */
#endif /* __LWIP_NAT_H__ */

View File

@ -466,6 +466,11 @@
#define IP_FORWARD 0 #define IP_FORWARD 0
#endif #endif
/** IP_NAT==1: Enables network address translation code */
#ifndef IP_NAT
#define IP_NAT 0
#endif
/** /**
* IP_OPTIONS_ALLOWED: Defines the behavior for IP options. * IP_OPTIONS_ALLOWED: Defines the behavior for IP options.
* IP_OPTIONS_ALLOWED==0: All packets with IP options are dropped. * IP_OPTIONS_ALLOWED==0: All packets with IP options are dropped.