From 627d025cda09850218938a22d86c9d01e9b6d0d2 Mon Sep 17 00:00:00 2001 From: Bernard Xiong Date: Thu, 29 Jan 2015 09:48:48 +0000 Subject: [PATCH] [lwIP] Modify the ip.c to support NAT --- components/net/lwip-1.4.1/src/core/ipv4/ip.c | 37 +++++++++++++++++--- components/net/lwip_nat/ipv4_nat.c | 6 ++-- components/net/lwip_nat/ipv4_nat.h | 2 +- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/components/net/lwip-1.4.1/src/core/ipv4/ip.c b/components/net/lwip-1.4.1/src/core/ipv4/ip.c index 95d2db404c..17bcd3929a 100644 --- a/components/net/lwip-1.4.1/src/core/ipv4/ip.c +++ b/components/net/lwip-1.4.1/src/core/ipv4/ip.c @@ -312,6 +312,11 @@ ip_input(struct pbuf *p, struct netif *inp) int check_ip_src=1; #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */ +#if IP_NAT + extern u8_t ip_nat_input(struct pbuf *p); + extern u8_t ip_nat_out(struct pbuf *p); +#endif + IP_STATS_INC(ip.recv); snmp_inc_ipinreceives(); @@ -487,15 +492,30 @@ ip_input(struct pbuf *p, struct netif *inp) /* packet not for us? */ if (netif == NULL) { +#if IP_FORWARD || IP_NAT + u8_t taken = 0; +#endif /* IP_FORWARD || IP_NAT */ /* packet not for us, route or discard */ 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? */ - if (!ip_addr_isbroadcast(¤t_iphdr_dest, inp)) { - /* try to forward IP packet on (other) interfaces */ - ip_forward(p, iphdr, inp); - } else + 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 */ + if (ip_forward(p, iphdr, inp) != NULL) { + taken = 1; + } #endif /* IP_FORWARD */ + } + } + if (!taken) +#endif /* IP_FORWARD || IP_NAT */ { snmp_inc_ipinaddrerrors(); snmp_inc_ipindiscards(); @@ -553,6 +573,13 @@ ip_input(struct pbuf *p, struct netif *inp) current_netif = inp; 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 /* raw input did not eat the packet? */ if (raw_input(p, inp) == 0) diff --git a/components/net/lwip_nat/ipv4_nat.c b/components/net/lwip_nat/ipv4_nat.c index 63f1b64a3a..58fe18562d 100644 --- a/components/net/lwip_nat/ipv4_nat.c +++ b/components/net/lwip_nat/ipv4_nat.c @@ -434,8 +434,8 @@ ip_nat_check_header(struct pbuf *p, u16_t min_size) * @return 1 if the packet has been consumed (it was a NAT packet), * 0 if the packet has not been consumed (no NAT packet) */ -int -ip_nat_input(struct pbuf *p, struct netif *inp) +u8_t +ip_nat_input(struct pbuf *p) { struct ip_hdr *iphdr = (struct ip_hdr*)p->payload; struct tcp_hdr *tcphdr; @@ -443,7 +443,7 @@ ip_nat_input(struct pbuf *p, struct netif *inp) struct icmp_echo_hdr *icmphdr; nat_entry_t nat_entry; err_t err; - int consumed = 0; + u8_t consumed = 0; int i; struct pbuf *q = NULL; diff --git a/components/net/lwip_nat/ipv4_nat.h b/components/net/lwip_nat/ipv4_nat.h index b455d8cbc9..e369c781da 100644 --- a/components/net/lwip_nat/ipv4_nat.h +++ b/components/net/lwip_nat/ipv4_nat.h @@ -88,7 +88,7 @@ typedef struct ip_nat_entry void ip_nat_init(void); void ip_nat_tmr(void); -int ip_nat_input(struct pbuf *p, struct netif *inp); +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);