From 2e51934e230013c9df58971df53a08dad108becf Mon Sep 17 00:00:00 2001
From: kircher <majun65@huawei.com>
Date: Mon, 29 May 2023 19:58:52 +0800
Subject: [PATCH] drop-netbuf-in-recv_udp-to-fix-mem-overflow

---
 src/api/api_lib.c          | 14 ++++++++++++++
 src/api/api_msg.c          | 15 ++++++++++++---
 src/api/sockets.c          |  6 +++---
 src/core/udp.c             |  8 ++++++++
 src/include/lwip/api.h     |  3 +++
 src/include/lwip/pbuf.h    |  4 ++++
 src/include/lwip/sockets.h |  8 ++++----
 src/include/lwipopts.h     |  4 ++++
 8 files changed, 52 insertions(+), 10 deletions(-)

diff --git a/src/api/api_lib.c b/src/api/api_lib.c
index 4cdb965..b22b987 100644
--- a/src/api/api_lib.c
+++ b/src/api/api_lib.c
@@ -655,7 +655,11 @@ netconn_recv_data(struct netconn *conn, void **new_buf, u8_t apiflags)
 #if (LWIP_UDP || LWIP_RAW)
   {
     LWIP_ASSERT("buf != NULL", buf != NULL);
+#if GAZELLE_UDP_ENABLE
+    len = ((struct pbuf *)buf)->tot_len;
+#else /* GAZELLE_UDP_ENABLE */
     len = netbuf_len((struct netbuf *)buf);
+#endif /* GAZELLE_UDP_ENABLE */
   }
 #endif /* (LWIP_UDP || LWIP_RAW) */
 
@@ -827,6 +831,16 @@ netconn_recv_udp_raw_netbuf(struct netconn *conn, struct netbuf **new_buf)
   return netconn_recv_data(conn, (void **)new_buf, 0);
 }
 
+#if GAZELLE_UDP_ENABLE
+err_t
+netconn_recv_udp_raw_pbuf_flags(struct netconn *conn, struct pbuf **new_buf, u8_t apiflags)
+{
+  LWIP_ERROR("netconn_recv_udp_raw_pbuf: invalid conn", (conn != NULL) &&
+             NETCONNTYPE_GROUP(netconn_type(conn)) != NETCONN_TCP, return ERR_ARG;);
+  return netconn_recv_data(conn, (void **)new_buf, apiflags);
+}
+#endif /* GAZELLE_UDP_ENABLE */
+
 /**
  * Receive data (in form of a netbuf) from a UDP or RAW netconn
  *
diff --git a/src/api/api_msg.c b/src/api/api_msg.c
index 98537b8..35207cc 100644
--- a/src/api/api_msg.c
+++ b/src/api/api_msg.c
@@ -227,7 +227,6 @@ recv_udp(void *arg, struct udp_pcb *pcb, struct pbuf *p,
   struct netbuf *buf;
   struct netconn *conn;
   u16_t len;
-  err_t err;
 #if LWIP_SO_RCVBUF
   int recv_avail;
 #endif /* LWIP_SO_RCVBUF */
@@ -255,6 +254,15 @@ recv_udp(void *arg, struct udp_pcb *pcb, struct pbuf *p,
     return;
   }
 
+#if GAZELLE_UDP_ENABLE
+  LWIP_UNUSED_ARG(buf);
+  ip_addr_set(&p->addr, addr);
+  p->port = port;
+  len = p->tot_len;
+  if (sys_mbox_trypost(&conn->recvmbox, p) != ERR_OK) {
+    return;
+#else /* GAZELLE_UDP_ENABLE */
+  err_t err;
   buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);
   if (buf == NULL) {
     pbuf_free(p);
@@ -281,17 +289,18 @@ recv_udp(void *arg, struct udp_pcb *pcb, struct pbuf *p,
     netbuf_delete(buf);
     LWIP_DEBUGF(API_MSG_DEBUG, ("recv_udp: sys_mbox_trypost failed, err=%d\n", err));
     return;
+#endif /* GAZELLE_UDP_ENABLE */
   } else {
 #if LWIP_SO_RCVBUF
     SYS_ARCH_INC(conn->recv_avail, len);
 #endif /* LWIP_SO_RCVBUF */
-#if GAZELLE_ENABLE
+#if GAZELLE_UDP_ENABLE
     add_recv_list(conn->socket);
     LWIP_UNUSED_ARG(len);
-#else
+#else /* GAZELLE_UDP_ENABLE */
     /* Register event with callback */
     API_EVENT(conn, NETCONN_EVT_RCVPLUS, len);
-#endif
+#endif /* GAZELLE_UDP_ENABLE */
   }
 }
 #endif /* LWIP_UDP */
diff --git a/src/api/sockets.c b/src/api/sockets.c
index bedb86b..a731453 100644
--- a/src/api/sockets.c
+++ b/src/api/sockets.c
@@ -1308,7 +1308,7 @@ lwip_recvfrom_udp_raw(struct lwip_sock *sock, int flags, struct msghdr *msg, u16
     apiflags = 0;
   }
 
-#if !GAZELLE_ENABLE
+#if !GAZELLE_UDP_ENABLE
   LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom_udp_raw[UDP/RAW]: top sock->lastdata=%p\n", (void *)sock->lastdata.netbuf));
   /* Check if there is data left from the last recv operation. */
   buf = sock->lastdata.netbuf;
@@ -1396,7 +1396,7 @@ lwip_recvfrom_udp_raw(struct lwip_sock *sock, int flags, struct msghdr *msg, u16
     sock->lastdata.netbuf = NULL;
     netbuf_delete(buf);
   }
-#else /* GAZELLE_ENABLE */
+#else /* GAZELLE_UDP_ENABLE */
   LWIP_UNUSED_ARG(copylen);
   LWIP_UNUSED_ARG(buf);
   LWIP_UNUSED_ARG(err);
@@ -1407,7 +1407,7 @@ lwip_recvfrom_udp_raw(struct lwip_sock *sock, int flags, struct msghdr *msg, u16
     return ERR_BUF;
   }
 
-#endif /* GAZELLE_ENABLE */
+#endif /* GAZELLE_UDP_ENABLE */
   if (datagram_len) {
     *datagram_len = buflen;
   }
diff --git a/src/core/udp.c b/src/core/udp.c
index 6261c86..5514295 100644
--- a/src/core/udp.c
+++ b/src/core/udp.c
@@ -614,6 +614,7 @@ udp_sendto_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip,
     UDP_STATS_INC(udp.rterr);
     return ERR_RTE;
   }
+#if GAZELLE_UDP_ENABLE
   uint8_t apiflags = 0;
 
   struct pbuf *udp_pbuf = write_lwip_data((struct lwip_sock *)(p->payload), p->tot_len, &apiflags);
@@ -626,14 +627,21 @@ udp_sendto_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip,
   }
 
   if (p->port) {
+#if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
+    return udp_sendto_if_chksum(pcb, p, &(p->addr), p->port, netif, have_chksum, chksum);
+#else /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
     return udp_sendto_if(pcb, p, &(p->addr), p->port, netif);
+#endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
   } else {
+#endif /* GAZELLE_UDP_ENABLE */
 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
   return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, have_chksum, chksum);
 #else /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
   return udp_sendto_if(pcb, p, dst_ip, dst_port, netif);
 #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
+#if GAZELLE_UDP_ENABLE
   }
+#endif /* GAZELLE_UDP_ENABLE */
 }
 
 /**
diff --git a/src/include/lwip/api.h b/src/include/lwip/api.h
index 6bf8b6a..6e6e52d 100644
--- a/src/include/lwip/api.h
+++ b/src/include/lwip/api.h
@@ -373,6 +373,9 @@ err_t   netconn_accept(struct netconn *conn, struct netconn **new_conn);
 err_t   netconn_recv(struct netconn *conn, struct netbuf **new_buf);
 err_t   netconn_recv_udp_raw_netbuf(struct netconn *conn, struct netbuf **new_buf);
 err_t   netconn_recv_udp_raw_netbuf_flags(struct netconn *conn, struct netbuf **new_buf, u8_t apiflags);
+#if GAZELLE_UDP_ENABLE
+err_t   netconn_recv_udp_raw_pbuf_flags(struct netconn *conn, struct pbuf **new_buf, u8_t apiflags);
+#endif /* GAZELLE_UDP_ENABLE */
 err_t   netconn_recv_tcp_pbuf(struct netconn *conn, struct pbuf **new_buf);
 err_t   netconn_recv_tcp_pbuf_flags(struct netconn *conn, struct pbuf **new_buf, u8_t apiflags);
 err_t   netconn_tcp_recvd(struct netconn *conn, size_t len);
diff --git a/src/include/lwip/pbuf.h b/src/include/lwip/pbuf.h
index e08d4fa..9ce3500 100644
--- a/src/include/lwip/pbuf.h
+++ b/src/include/lwip/pbuf.h
@@ -40,8 +40,10 @@
 
 #include "lwip/opt.h"
 #include "lwip/err.h"
+#if GAZELLE_UDP_ENABLE
 #include "lwip/ip_addr.h"
 #include "lwip/ip6_addr.h"
+#endif /* GAZELLE_UDP_ENABLE */
 
 #ifdef __cplusplus
 extern "C" {
@@ -238,8 +240,10 @@ struct pbuf {
   struct pbuf *last;
   pthread_spinlock_t pbuf_lock;
   struct tcp_pcb *pcb;
+#if GAZELLE_UDP_ENABLE
   ip_addr_t addr;
   u16_t port;
+#endif /* GAZELLE_UDP_ENABLE */
 #endif /* GAZELLE_ENABLE CHECKSUM_OFFLOAD_SWITCH */
 
   /** In case the user needs to store data custom data on a pbuf */
diff --git a/src/include/lwip/sockets.h b/src/include/lwip/sockets.h
index a7cec29..a50c691 100644
--- a/src/include/lwip/sockets.h
+++ b/src/include/lwip/sockets.h
@@ -333,7 +333,7 @@ struct linger {
 
 
 #if LWIP_MULTICAST_TX_OPTIONS
-#if GAZELLE_ENABLE
+#if GAZELLE_UDP_ENABLE
 #define IP_MULTICAST_IF 32
 #define IP_MULTICAST_TTL 33
 #define IP_MULTICAST_LOOP 34
@@ -344,11 +344,11 @@ struct linger {
 #define IP_MULTICAST_TTL   5
 #define IP_MULTICAST_IF    6
 #define IP_MULTICAST_LOOP  7
-#endif /* GAZELLE_ENABLE */
+#endif /* GAZELLE_UDP_ENABLE */
 #endif /* LWIP_MULTICAST_TX_OPTIONS */
 
 #if LWIP_IGMP
-#if GAZELLE_ENABLE
+#if GAZELLE_UDP_ENABLE
 #define IP_ADD_MEMBERSHIP  35
 #define IP_DROP_MEMBERSHIP 36
 #else
@@ -357,7 +357,7 @@ struct linger {
  */
 #define IP_ADD_MEMBERSHIP  3
 #define IP_DROP_MEMBERSHIP 4
-#endif /* GAZELLE_ENABLE */
+#endif /* GAZELLE_UDP_ENABLE */
 
 typedef struct ip_mreq {
     struct in_addr imr_multiaddr; /* IP multicast address of group */
diff --git a/src/include/lwipopts.h b/src/include/lwipopts.h
index abc3f15..50220e8 100644
--- a/src/include/lwipopts.h
+++ b/src/include/lwipopts.h
@@ -56,6 +56,10 @@
 
 #define GAZELLE_TCP_MIN_TSO_SEG_LEN 256
 
+
+#define GAZELLE_UDP_ENABLE 1
+
+
 /*
    ----------------------------------
    ---------- NIC offloads ----------
-- 
2.33.0

