Skip to content

Commit 3e31d34

Browse files
committed
[lwIP] fix the pbuf q=NULL issue in ip_nat_input.
1 parent 72298ba commit 3e31d34

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

components/net/lwip-1.4.1/src/lwipopts.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,9 @@
162162
#define SYS_LIGHTWEIGHT_PROT (NO_SYS==0)
163163

164164
#ifdef LWIP_USING_NAT
165-
#define LWIP_NAT 1
165+
#define IP_NAT 1
166166
#else
167-
#define LWIP_NAT 0
167+
#define IP_NAT 0
168168
#endif
169169

170170
/* ---------- TCP options ---------- */

components/net/lwip_nat/ipv4_nat.c

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,10 @@
117117
#define LWIP_NAT_TTL_INFINITE (INT_MAX)
118118
#define LWIP_NAT_DEFAULT_TTL_SECONDS (128)
119119
#define LWIP_NAT_FORWARD_HEADER_SIZE_MIN (sizeof(struct eth_hdr))
120-
#define LWIP_NAT_DEFAULT_STATE_TABLES_ICMP (2)
121-
#define LWIP_NAT_DEFAULT_STATE_TABLES_TCP (16)
122-
#define LWIP_NAT_DEFAULT_STATE_TABLES_UDP (16)
120+
121+
#define LWIP_NAT_DEFAULT_STATE_TABLES_ICMP (4)
122+
#define LWIP_NAT_DEFAULT_STATE_TABLES_TCP (32)
123+
#define LWIP_NAT_DEFAULT_STATE_TABLES_UDP (32)
123124

124125
#define LWIP_NAT_DEFAULT_TCP_SOURCE_PORT (40000)
125126
#define LWIP_NAT_DEFAULT_UDP_SOURCE_PORT (40000)
@@ -250,12 +251,12 @@ ip_nat_init(void)
250251
IPNAT_ENTRY_RESET(&ip_nat_udp_table[i].common);
251252
}
252253

253-
/* we must lock scheduler to protect following code */
254+
/* we must lock scheduler to protect following code */
254255
rt_enter_critical();
255-
256+
256257
/* add a lwip timer for NAT */
257258
sys_timeout(LWIP_NAT_TMR_INTERVAL_SEC, nat_timer, NULL);
258-
259+
259260
/* un-protect */
260261
rt_exit_critical();
261262
}
@@ -361,7 +362,7 @@ ip_nat_reset_state(ip_nat_conf_t *cfg)
361362
{
362363
int i;
363364

364-
/* @todo: optimize this!!!
365+
/* @todo: optimize this!!!
365366
why do we search for it anyway, if we have the pointer??? */
366367
for (i = 0; i < LWIP_NAT_DEFAULT_STATE_TABLES_ICMP; i++) {
367368
if(ip_nat_icmp_table[i].common.cfg == cfg) {
@@ -392,7 +393,7 @@ ip_nat_shallnat(const struct ip_hdr *iphdr)
392393
ip_nat_conf_t *nat_config = ip_nat_cfg;
393394

394395
for (nat_config = ip_nat_cfg; nat_config != NULL; nat_config = nat_config->next) {
395-
if (ip_addr_netcmp(&(iphdr->dest), &(nat_config->entry.dest_net),
396+
if (ip_addr_netcmp(&(iphdr->dest), &(nat_config->entry.dest_net),
396397
&(nat_config->entry.dest_netmask)) ||
397398
ip_addr_netcmp(&(iphdr->src), &(nat_config->entry.source_net),
398399
&(nat_config->entry.source_netmask))) {
@@ -435,7 +436,7 @@ ip_nat_check_header(struct pbuf *p, u16_t min_size)
435436
* @return 1 if the packet has been consumed (it was a NAT packet),
436437
* 0 if the packet has not been consumed (no NAT packet)
437438
*/
438-
u8_t
439+
u8_t
439440
ip_nat_input(struct pbuf *p)
440441
{
441442
struct ip_hdr *iphdr = (struct ip_hdr*)p->payload;
@@ -538,6 +539,7 @@ ip_nat_input(struct pbuf *p)
538539
q = pbuf_alloc(PBUF_LINK, 0, PBUF_RAM);
539540
if (q == NULL) {
540541
LWIP_DEBUGF(LWIP_NAT_DEBUG, ("ip_nat_input: no pbuf for outgoing header\n"));
542+
// rt_kprintf("ip_nat_input: no pbuf for outgoing header\n");
541543
/* @todo: stats? */
542544
pbuf_free(p);
543545
p = NULL;
@@ -549,11 +551,13 @@ ip_nat_input(struct pbuf *p)
549551
/* restore p->payload to IP header */
550552
if (pbuf_header(p, -PBUF_LINK_HLEN)) {
551553
LWIP_DEBUGF(LWIP_NAT_DEBUG, ("ip_nat_input: restoring header failed\n"));
554+
// rt_kprintf("ip_nat_input: restoring header failed\n");
552555
/* @todo: stats? */
553556
pbuf_free(p);
554557
p = NULL;
555558
return 1;
556559
}
560+
else q = p;
557561
}
558562
/* if we come here, q is the pbuf to send (either points to p or to a chain) */
559563
in_if = nat_entry.cmn->cfg->entry.in_if;
@@ -572,6 +576,7 @@ ip_nat_input(struct pbuf *p)
572576
LWIP_DEBUGF(LWIP_NAT_DEBUG,
573577
("ip_nat_input: failed to send rewritten packet. link layer returned %d\n",
574578
err));
579+
// rt_kprintf("ip_nat_input: failed to send rewritten packet. link layer returned %d\n", err);
575580
}
576581
/* now that q (and/or p) is sent (or not), give up the reference to it
577582
this frees the input pbuf (p) as we have consumed it. */
@@ -736,6 +741,7 @@ ip_nat_out(struct pbuf *p)
736741
if (err != ERR_OK) {
737742
LWIP_DEBUGF(LWIP_NAT_DEBUG,
738743
("ip_nat_out: failed to send rewritten packet. link layer returned %d\n", err));
744+
// rt_kprintf("ip_nat_out: failed to send rewritten packet. link layer returned %d\n", err);
739745
} else {
740746
sent = 1;
741747
}
@@ -842,6 +848,7 @@ ip_nat_udp_lookup_outgoing(ip_nat_conf_t *nat_config, const struct ip_hdr *iphdr
842848
nat_entry.udp);
843849
} else {
844850
LWIP_DEBUGF(LWIP_NAT_DEBUG, ("ip_nat_udp_lookup_outgoing: no more NAT entries available\n"));
851+
// rt_kprintf("ip_nat_udp_lookup_outgoing: no more NAT entries available\n");
845852
}
846853
}
847854
}
@@ -926,6 +933,7 @@ ip_nat_tcp_lookup_outgoing(ip_nat_conf_t *nat_config, const struct ip_hdr *iphdr
926933
nat_entry.tcp);
927934
} else {
928935
LWIP_DEBUGF(LWIP_NAT_DEBUG, ("ip_nat_udp_lookup_outgoing: no more NAT entries available\n"));
936+
// rt_kprintf("ip_nat_udp_lookup_outgoing: no more NAT entries available\n");
929937
}
930938
}
931939
}
@@ -1005,9 +1013,9 @@ ip_nat_dbg_dump(const char *msg, const struct ip_hdr *iphdr)
10051013
LWIP_ASSERT("NULL != msg", NULL != msg);
10061014
LWIP_ASSERT("NULL != iphdr", NULL != iphdr);
10071015
LWIP_DEBUGF(LWIP_NAT_DEBUG, ("%s: IP: (", msg));
1008-
ip_nat_dbg_dump_ip(&iphdr->src);
1016+
ip_nat_dbg_dump_ip((ip_addr_t *)&(iphdr->src));
10091017
LWIP_DEBUGF(LWIP_NAT_DEBUG, (" --> "));
1010-
ip_nat_dbg_dump_ip(&iphdr->dest);
1018+
ip_nat_dbg_dump_ip((ip_addr_t *)&(iphdr->dest));
10111019
LWIP_DEBUGF(LWIP_NAT_DEBUG, (" id=%" U16_F ", chksum=%" U16_F ")\n",
10121020
ntohs(IPH_ID(iphdr)), ntohs(IPH_CHKSUM(iphdr))));
10131021
}

0 commit comments

Comments
 (0)