1195972f6Sopenharmony_ciFrom 2e51934e230013c9df58971df53a08dad108becf Mon Sep 17 00:00:00 2001
2195972f6Sopenharmony_ciFrom: kircher <majun65@huawei.com>
3195972f6Sopenharmony_ciDate: Mon, 29 May 2023 19:58:52 +0800
4195972f6Sopenharmony_ciSubject: [PATCH] drop-netbuf-in-recv_udp-to-fix-mem-overflow
5195972f6Sopenharmony_ci
6195972f6Sopenharmony_ci---
7195972f6Sopenharmony_ci src/api/api_lib.c          | 14 ++++++++++++++
8195972f6Sopenharmony_ci src/api/api_msg.c          | 15 ++++++++++++---
9195972f6Sopenharmony_ci src/api/sockets.c          |  6 +++---
10195972f6Sopenharmony_ci src/core/udp.c             |  8 ++++++++
11195972f6Sopenharmony_ci src/include/lwip/api.h     |  3 +++
12195972f6Sopenharmony_ci src/include/lwip/pbuf.h    |  4 ++++
13195972f6Sopenharmony_ci src/include/lwip/sockets.h |  8 ++++----
14195972f6Sopenharmony_ci src/include/lwipopts.h     |  4 ++++
15195972f6Sopenharmony_ci 8 files changed, 52 insertions(+), 10 deletions(-)
16195972f6Sopenharmony_ci
17195972f6Sopenharmony_cidiff --git a/src/api/api_lib.c b/src/api/api_lib.c
18195972f6Sopenharmony_ciindex ffa14d6..afdfc11 100644
19195972f6Sopenharmony_ci--- a/src/api/api_lib.c
20195972f6Sopenharmony_ci+++ b/src/api/api_lib.c
21195972f6Sopenharmony_ci@@ -655,7 +655,11 @@ netconn_recv_data(struct netconn *conn, void **new_buf, u8_t apiflags)
22195972f6Sopenharmony_ci #if (LWIP_UDP || LWIP_RAW)
23195972f6Sopenharmony_ci   {
24195972f6Sopenharmony_ci     LWIP_ASSERT("buf != NULL", buf != NULL);
25195972f6Sopenharmony_ci+#if GAZELLE_UDP_ENABLE
26195972f6Sopenharmony_ci+    len = ((struct pbuf *)buf)->tot_len;
27195972f6Sopenharmony_ci+#else /* GAZELLE_UDP_ENABLE */
28195972f6Sopenharmony_ci     len = netbuf_len((struct netbuf *)buf);
29195972f6Sopenharmony_ci+#endif /* GAZELLE_UDP_ENABLE */
30195972f6Sopenharmony_ci   }
31195972f6Sopenharmony_ci #endif /* (LWIP_UDP || LWIP_RAW) */
32195972f6Sopenharmony_ci 
33195972f6Sopenharmony_ci@@ -827,6 +831,16 @@ netconn_recv_udp_raw_netbuf(struct netconn *conn, struct netbuf **new_buf)
34195972f6Sopenharmony_ci   return netconn_recv_data(conn, (void **)new_buf, 0);
35195972f6Sopenharmony_ci }
36195972f6Sopenharmony_ci 
37195972f6Sopenharmony_ci+#if GAZELLE_UDP_ENABLE
38195972f6Sopenharmony_ci+err_t
39195972f6Sopenharmony_ci+netconn_recv_udp_raw_pbuf_flags(struct netconn *conn, struct pbuf **new_buf, u8_t apiflags)
40195972f6Sopenharmony_ci+{
41195972f6Sopenharmony_ci+  LWIP_ERROR("netconn_recv_udp_raw_pbuf: invalid conn", (conn != NULL) &&
42195972f6Sopenharmony_ci+             NETCONNTYPE_GROUP(netconn_type(conn)) != NETCONN_TCP, return ERR_ARG;);
43195972f6Sopenharmony_ci+  return netconn_recv_data(conn, (void **)new_buf, apiflags);
44195972f6Sopenharmony_ci+}
45195972f6Sopenharmony_ci+#endif /* GAZELLE_UDP_ENABLE */
46195972f6Sopenharmony_ci+
47195972f6Sopenharmony_ci /**
48195972f6Sopenharmony_ci  * Receive data (in form of a netbuf) from a UDP or RAW netconn
49195972f6Sopenharmony_ci  *
50195972f6Sopenharmony_cidiff --git a/src/api/api_msg.c b/src/api/api_msg.c
51195972f6Sopenharmony_ciindex 30929be..b82ebf2 100644
52195972f6Sopenharmony_ci--- a/src/api/api_msg.c
53195972f6Sopenharmony_ci+++ b/src/api/api_msg.c
54195972f6Sopenharmony_ci@@ -253,6 +253,14 @@ recv_udp(void *arg, struct udp_pcb *pcb, struct pbuf *p,
55195972f6Sopenharmony_ci     return;
56195972f6Sopenharmony_ci   }
57195972f6Sopenharmony_ci 
58195972f6Sopenharmony_ci+#if GAZELLE_UDP_ENABLE
59195972f6Sopenharmony_ci+  LWIP_UNUSED_ARG(buf);
60195972f6Sopenharmony_ci+  ip_addr_set(&p->addr, addr);
61195972f6Sopenharmony_ci+  p->port = port;
62195972f6Sopenharmony_ci+  len = p->tot_len;
63195972f6Sopenharmony_ci+  if (sys_mbox_trypost(&conn->recvmbox, p) != ERR_OK) {
64195972f6Sopenharmony_ci+    return;
65195972f6Sopenharmony_ci+#else /* GAZELLE_UDP_ENABLE */
66195972f6Sopenharmony_ci   buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);
67195972f6Sopenharmony_ci   if (buf == NULL) {
68195972f6Sopenharmony_ci     pbuf_free(p);
69195972f6Sopenharmony_ci@@ -277,17 +285,18 @@ recv_udp(void *arg, struct udp_pcb *pcb, struct pbuf *p,
70195972f6Sopenharmony_ci   if (sys_mbox_trypost(&conn->recvmbox, buf) != ERR_OK) {
71195972f6Sopenharmony_ci     netbuf_delete(buf);
72195972f6Sopenharmony_ci     return;
73195972f6Sopenharmony_ci+#endif /* GAZELLE_UDP_ENABLE */
74195972f6Sopenharmony_ci   } else {
75195972f6Sopenharmony_ci #if LWIP_SO_RCVBUF
76195972f6Sopenharmony_ci     SYS_ARCH_INC(conn->recv_avail, len);
77195972f6Sopenharmony_ci #endif /* LWIP_SO_RCVBUF */
78195972f6Sopenharmony_ci-#if GAZELLE_ENABLE
79195972f6Sopenharmony_ci+#if GAZELLE_UDP_ENABLE
80195972f6Sopenharmony_ci     add_recv_list(conn->socket);
81195972f6Sopenharmony_ci     LWIP_UNUSED_ARG(len);
82195972f6Sopenharmony_ci-#else
83195972f6Sopenharmony_ci+#else /* GAZELLE_UDP_ENABLE */
84195972f6Sopenharmony_ci     /* Register event with callback */
85195972f6Sopenharmony_ci     API_EVENT(conn, NETCONN_EVT_RCVPLUS, len);
86195972f6Sopenharmony_ci-#endif
87195972f6Sopenharmony_ci+#endif /* GAZELLE_UDP_ENABLE */
88195972f6Sopenharmony_ci   }
89195972f6Sopenharmony_ci }
90195972f6Sopenharmony_ci #endif /* LWIP_UDP */
91195972f6Sopenharmony_cidiff --git a/src/api/sockets.c b/src/api/sockets.c
92195972f6Sopenharmony_ciindex dee9230..17691f7 100644
93195972f6Sopenharmony_ci--- a/src/api/sockets.c
94195972f6Sopenharmony_ci+++ b/src/api/sockets.c
95195972f6Sopenharmony_ci@@ -1179,7 +1179,7 @@ lwip_recvfrom_udp_raw(struct lwip_sock *sock, int flags, struct msghdr *msg, u16
96195972f6Sopenharmony_ci     apiflags = 0;
97195972f6Sopenharmony_ci   }
98195972f6Sopenharmony_ci 
99195972f6Sopenharmony_ci-#if !GAZELLE_ENABLE
100195972f6Sopenharmony_ci+#if !GAZELLE_UDP_ENABLE
101195972f6Sopenharmony_ci   LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom_udp_raw[UDP/RAW]: top sock->lastdata=%p\n", (void *)sock->lastdata.netbuf));
102195972f6Sopenharmony_ci   /* Check if there is data left from the last recv operation. */
103195972f6Sopenharmony_ci   buf = sock->lastdata.netbuf;
104195972f6Sopenharmony_ci@@ -1267,7 +1267,7 @@ lwip_recvfrom_udp_raw(struct lwip_sock *sock, int flags, struct msghdr *msg, u16
105195972f6Sopenharmony_ci     sock->lastdata.netbuf = NULL;
106195972f6Sopenharmony_ci     netbuf_delete(buf);
107195972f6Sopenharmony_ci   }
108195972f6Sopenharmony_ci-#else /* GAZELLE_ENABLE */
109195972f6Sopenharmony_ci+#else /* GAZELLE_UDP_ENABLE */
110195972f6Sopenharmony_ci   LWIP_UNUSED_ARG(copylen);
111195972f6Sopenharmony_ci   LWIP_UNUSED_ARG(buf);
112195972f6Sopenharmony_ci   LWIP_UNUSED_ARG(err);
113195972f6Sopenharmony_ci@@ -1278,7 +1278,7 @@ lwip_recvfrom_udp_raw(struct lwip_sock *sock, int flags, struct msghdr *msg, u16
114195972f6Sopenharmony_ci     return ERR_BUF;
115195972f6Sopenharmony_ci   }
116195972f6Sopenharmony_ci 
117195972f6Sopenharmony_ci-#endif /* GAZELLE_ENABLE */
118195972f6Sopenharmony_ci+#endif /* GAZELLE_UDP_ENABLE */
119195972f6Sopenharmony_ci   if (datagram_len) {
120195972f6Sopenharmony_ci     *datagram_len = buflen;
121195972f6Sopenharmony_ci   }
122195972f6Sopenharmony_cidiff --git a/src/core/udp.c b/src/core/udp.c
123195972f6Sopenharmony_ciindex 170c911..1eb459d 100644
124195972f6Sopenharmony_ci--- a/src/core/udp.c
125195972f6Sopenharmony_ci+++ b/src/core/udp.c
126195972f6Sopenharmony_ci@@ -599,6 +599,7 @@ udp_sendto_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip,
127195972f6Sopenharmony_ci     UDP_STATS_INC(udp.rterr);
128195972f6Sopenharmony_ci     return ERR_RTE;
129195972f6Sopenharmony_ci   }
130195972f6Sopenharmony_ci+#if GAZELLE_UDP_ENABLE
131195972f6Sopenharmony_ci   uint8_t apiflags = 0;
132195972f6Sopenharmony_ci 
133195972f6Sopenharmony_ci   struct pbuf *udp_pbuf = write_lwip_data((struct lwip_sock *)(p->payload), p->tot_len, &apiflags);
134195972f6Sopenharmony_ci@@ -611,14 +612,21 @@ udp_sendto_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip,
135195972f6Sopenharmony_ci   }
136195972f6Sopenharmony_ci 
137195972f6Sopenharmony_ci   if (p->port) {
138195972f6Sopenharmony_ci+#if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
139195972f6Sopenharmony_ci+    return udp_sendto_if_chksum(pcb, p, &(p->addr), p->port, netif, have_chksum, chksum);
140195972f6Sopenharmony_ci+#else /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
141195972f6Sopenharmony_ci     return udp_sendto_if(pcb, p, &(p->addr), p->port, netif);
142195972f6Sopenharmony_ci+#endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
143195972f6Sopenharmony_ci   } else {
144195972f6Sopenharmony_ci+#endif /* GAZELLE_UDP_ENABLE */
145195972f6Sopenharmony_ci #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
146195972f6Sopenharmony_ci   return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, have_chksum, chksum);
147195972f6Sopenharmony_ci #else /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
148195972f6Sopenharmony_ci   return udp_sendto_if(pcb, p, dst_ip, dst_port, netif);
149195972f6Sopenharmony_ci #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
150195972f6Sopenharmony_ci+#if GAZELLE_UDP_ENABLE
151195972f6Sopenharmony_ci   }
152195972f6Sopenharmony_ci+#endif /* GAZELLE_UDP_ENABLE */
153195972f6Sopenharmony_ci }
154195972f6Sopenharmony_ci 
155195972f6Sopenharmony_ci /**
156195972f6Sopenharmony_cidiff --git a/src/include/lwip/api.h b/src/include/lwip/api.h
157195972f6Sopenharmony_ciindex d3c4f02..6090cab 100644
158195972f6Sopenharmony_ci--- a/src/include/lwip/api.h
159195972f6Sopenharmony_ci+++ b/src/include/lwip/api.h
160195972f6Sopenharmony_ci@@ -338,6 +338,9 @@ err_t   netconn_accept(struct netconn *conn, struct netconn **new_conn);
161195972f6Sopenharmony_ci err_t   netconn_recv(struct netconn *conn, struct netbuf **new_buf);
162195972f6Sopenharmony_ci err_t   netconn_recv_udp_raw_netbuf(struct netconn *conn, struct netbuf **new_buf);
163195972f6Sopenharmony_ci err_t   netconn_recv_udp_raw_netbuf_flags(struct netconn *conn, struct netbuf **new_buf, u8_t apiflags);
164195972f6Sopenharmony_ci+#if GAZELLE_UDP_ENABLE
165195972f6Sopenharmony_ci+err_t   netconn_recv_udp_raw_pbuf_flags(struct netconn *conn, struct pbuf **new_buf, u8_t apiflags);
166195972f6Sopenharmony_ci+#endif /* GAZELLE_UDP_ENABLE */
167195972f6Sopenharmony_ci err_t   netconn_recv_tcp_pbuf(struct netconn *conn, struct pbuf **new_buf);
168195972f6Sopenharmony_ci err_t   netconn_recv_tcp_pbuf_flags(struct netconn *conn, struct pbuf **new_buf, u8_t apiflags);
169195972f6Sopenharmony_ci err_t   netconn_tcp_recvd(struct netconn *conn, size_t len);
170195972f6Sopenharmony_cidiff --git a/src/include/lwip/pbuf.h b/src/include/lwip/pbuf.h
171195972f6Sopenharmony_ciindex 728c5e4..4747f39 100644
172195972f6Sopenharmony_ci--- a/src/include/lwip/pbuf.h
173195972f6Sopenharmony_ci+++ b/src/include/lwip/pbuf.h
174195972f6Sopenharmony_ci@@ -40,8 +40,10 @@
175195972f6Sopenharmony_ci 
176195972f6Sopenharmony_ci #include "lwip/opt.h"
177195972f6Sopenharmony_ci #include "lwip/err.h"
178195972f6Sopenharmony_ci+#if GAZELLE_UDP_ENABLE
179195972f6Sopenharmony_ci #include "lwip/ip_addr.h"
180195972f6Sopenharmony_ci #include "lwip/ip6_addr.h"
181195972f6Sopenharmony_ci+#endif /* GAZELLE_UDP_ENABLE */
182195972f6Sopenharmony_ci 
183195972f6Sopenharmony_ci #ifdef __cplusplus
184195972f6Sopenharmony_ci extern "C" {
185195972f6Sopenharmony_ci@@ -238,8 +240,10 @@ struct pbuf {
186195972f6Sopenharmony_ci   struct pbuf *last;
187195972f6Sopenharmony_ci   pthread_spinlock_t pbuf_lock;
188195972f6Sopenharmony_ci   struct tcp_pcb *pcb;
189195972f6Sopenharmony_ci+#if GAZELLE_UDP_ENABLE
190195972f6Sopenharmony_ci   ip_addr_t addr;
191195972f6Sopenharmony_ci   u16_t port;
192195972f6Sopenharmony_ci+#endif /* GAZELLE_UDP_ENABLE */
193195972f6Sopenharmony_ci #endif /* GAZELLE_ENABLE CHECKSUM_OFFLOAD_SWITCH */
194195972f6Sopenharmony_ci 
195195972f6Sopenharmony_ci   /** In case the user needs to store data custom data on a pbuf */
196195972f6Sopenharmony_cidiff --git a/src/include/lwip/sockets.h b/src/include/lwip/sockets.h
197195972f6Sopenharmony_ciindex 643093a..2b6e6be 100644
198195972f6Sopenharmony_ci--- a/src/include/lwip/sockets.h
199195972f6Sopenharmony_ci+++ b/src/include/lwip/sockets.h
200195972f6Sopenharmony_ci@@ -330,7 +330,7 @@ struct linger {
201195972f6Sopenharmony_ci 
202195972f6Sopenharmony_ci 
203195972f6Sopenharmony_ci #if LWIP_MULTICAST_TX_OPTIONS
204195972f6Sopenharmony_ci-#if GAZELLE_ENABLE
205195972f6Sopenharmony_ci+#if GAZELLE_UDP_ENABLE
206195972f6Sopenharmony_ci #define IP_MULTICAST_IF 32
207195972f6Sopenharmony_ci #define IP_MULTICAST_TTL 33
208195972f6Sopenharmony_ci #define IP_MULTICAST_LOOP 34
209195972f6Sopenharmony_ci@@ -341,11 +341,11 @@ struct linger {
210195972f6Sopenharmony_ci #define IP_MULTICAST_TTL   5
211195972f6Sopenharmony_ci #define IP_MULTICAST_IF    6
212195972f6Sopenharmony_ci #define IP_MULTICAST_LOOP  7
213195972f6Sopenharmony_ci-#endif /* GAZELLE_ENABLE */
214195972f6Sopenharmony_ci+#endif /* GAZELLE_UDP_ENABLE */
215195972f6Sopenharmony_ci #endif /* LWIP_MULTICAST_TX_OPTIONS */
216195972f6Sopenharmony_ci 
217195972f6Sopenharmony_ci #if LWIP_IGMP
218195972f6Sopenharmony_ci-#if GAZELLE_ENABLE
219195972f6Sopenharmony_ci+#if GAZELLE_UDP_ENABLE
220195972f6Sopenharmony_ci #define IP_ADD_MEMBERSHIP  35
221195972f6Sopenharmony_ci #define IP_DROP_MEMBERSHIP 36
222195972f6Sopenharmony_ci #else
223195972f6Sopenharmony_ci@@ -354,7 +354,7 @@ struct linger {
224195972f6Sopenharmony_ci  */
225195972f6Sopenharmony_ci #define IP_ADD_MEMBERSHIP  3
226195972f6Sopenharmony_ci #define IP_DROP_MEMBERSHIP 4
227195972f6Sopenharmony_ci-#endif /* GAZELLE_ENABLE */
228195972f6Sopenharmony_ci+#endif /* GAZELLE_UDP_ENABLE */
229195972f6Sopenharmony_ci 
230195972f6Sopenharmony_ci typedef struct ip_mreq {
231195972f6Sopenharmony_ci     struct in_addr imr_multiaddr; /* IP multicast address of group */
232195972f6Sopenharmony_cidiff --git a/src/include/lwipopts.h b/src/include/lwipopts.h
233195972f6Sopenharmony_ciindex 6b5a2d1..9804aed 100644
234195972f6Sopenharmony_ci--- a/src/include/lwipopts.h
235195972f6Sopenharmony_ci+++ b/src/include/lwipopts.h
236195972f6Sopenharmony_ci@@ -63,6 +63,10 @@
237195972f6Sopenharmony_ci 
238195972f6Sopenharmony_ci #define GAZELLE_TCP_MIN_TSO_SEG_LEN 256
239195972f6Sopenharmony_ci 
240195972f6Sopenharmony_ci+
241195972f6Sopenharmony_ci+#define GAZELLE_UDP_ENABLE 1
242195972f6Sopenharmony_ci+
243195972f6Sopenharmony_ci+
244195972f6Sopenharmony_ci /*
245195972f6Sopenharmony_ci    ----------------------------------
246195972f6Sopenharmony_ci    ---------- NIC offloads ----------
247195972f6Sopenharmony_ci-- 
248195972f6Sopenharmony_ci2.33.0
249195972f6Sopenharmony_ci
250