• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1From 2e51934e230013c9df58971df53a08dad108becf Mon Sep 17 00:00:00 2001
2From: kircher <majun65@huawei.com>
3Date: Mon, 29 May 2023 19:58:52 +0800
4Subject: [PATCH] drop-netbuf-in-recv_udp-to-fix-mem-overflow
5
6---
7 src/api/api_lib.c          | 14 ++++++++++++++
8 src/api/api_msg.c          | 15 ++++++++++++---
9 src/api/sockets.c          |  6 +++---
10 src/core/udp.c             |  8 ++++++++
11 src/include/lwip/api.h     |  3 +++
12 src/include/lwip/pbuf.h    |  4 ++++
13 src/include/lwip/sockets.h |  8 ++++----
14 src/include/lwipopts.h     |  4 ++++
15 8 files changed, 52 insertions(+), 10 deletions(-)
16
17diff --git a/src/api/api_lib.c b/src/api/api_lib.c
18index ffa14d6..afdfc11 100644
19--- a/src/api/api_lib.c
20+++ b/src/api/api_lib.c
21@@ -655,7 +655,11 @@ netconn_recv_data(struct netconn *conn, void **new_buf, u8_t apiflags)
22 #if (LWIP_UDP || LWIP_RAW)
23   {
24     LWIP_ASSERT("buf != NULL", buf != NULL);
25+#if GAZELLE_UDP_ENABLE
26+    len = ((struct pbuf *)buf)->tot_len;
27+#else /* GAZELLE_UDP_ENABLE */
28     len = netbuf_len((struct netbuf *)buf);
29+#endif /* GAZELLE_UDP_ENABLE */
30   }
31 #endif /* (LWIP_UDP || LWIP_RAW) */
32
33@@ -827,6 +831,16 @@ netconn_recv_udp_raw_netbuf(struct netconn *conn, struct netbuf **new_buf)
34   return netconn_recv_data(conn, (void **)new_buf, 0);
35 }
36
37+#if GAZELLE_UDP_ENABLE
38+err_t
39+netconn_recv_udp_raw_pbuf_flags(struct netconn *conn, struct pbuf **new_buf, u8_t apiflags)
40+{
41+  LWIP_ERROR("netconn_recv_udp_raw_pbuf: invalid conn", (conn != NULL) &&
42+             NETCONNTYPE_GROUP(netconn_type(conn)) != NETCONN_TCP, return ERR_ARG;);
43+  return netconn_recv_data(conn, (void **)new_buf, apiflags);
44+}
45+#endif /* GAZELLE_UDP_ENABLE */
46+
47 /**
48  * Receive data (in form of a netbuf) from a UDP or RAW netconn
49  *
50diff --git a/src/api/api_msg.c b/src/api/api_msg.c
51index 30929be..b82ebf2 100644
52--- a/src/api/api_msg.c
53+++ b/src/api/api_msg.c
54@@ -253,6 +253,14 @@ recv_udp(void *arg, struct udp_pcb *pcb, struct pbuf *p,
55     return;
56   }
57
58+#if GAZELLE_UDP_ENABLE
59+  LWIP_UNUSED_ARG(buf);
60+  ip_addr_set(&p->addr, addr);
61+  p->port = port;
62+  len = p->tot_len;
63+  if (sys_mbox_trypost(&conn->recvmbox, p) != ERR_OK) {
64+    return;
65+#else /* GAZELLE_UDP_ENABLE */
66   buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);
67   if (buf == NULL) {
68     pbuf_free(p);
69@@ -277,17 +285,18 @@ recv_udp(void *arg, struct udp_pcb *pcb, struct pbuf *p,
70   if (sys_mbox_trypost(&conn->recvmbox, buf) != ERR_OK) {
71     netbuf_delete(buf);
72     return;
73+#endif /* GAZELLE_UDP_ENABLE */
74   } else {
75 #if LWIP_SO_RCVBUF
76     SYS_ARCH_INC(conn->recv_avail, len);
77 #endif /* LWIP_SO_RCVBUF */
78-#if GAZELLE_ENABLE
79+#if GAZELLE_UDP_ENABLE
80     add_recv_list(conn->socket);
81     LWIP_UNUSED_ARG(len);
82-#else
83+#else /* GAZELLE_UDP_ENABLE */
84     /* Register event with callback */
85     API_EVENT(conn, NETCONN_EVT_RCVPLUS, len);
86-#endif
87+#endif /* GAZELLE_UDP_ENABLE */
88   }
89 }
90 #endif /* LWIP_UDP */
91diff --git a/src/api/sockets.c b/src/api/sockets.c
92index dee9230..17691f7 100644
93--- a/src/api/sockets.c
94+++ b/src/api/sockets.c
95@@ -1179,7 +1179,7 @@ lwip_recvfrom_udp_raw(struct lwip_sock *sock, int flags, struct msghdr *msg, u16
96     apiflags = 0;
97   }
98
99-#if !GAZELLE_ENABLE
100+#if !GAZELLE_UDP_ENABLE
101   LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom_udp_raw[UDP/RAW]: top sock->lastdata=%p\n", (void *)sock->lastdata.netbuf));
102   /* Check if there is data left from the last recv operation. */
103   buf = sock->lastdata.netbuf;
104@@ -1267,7 +1267,7 @@ lwip_recvfrom_udp_raw(struct lwip_sock *sock, int flags, struct msghdr *msg, u16
105     sock->lastdata.netbuf = NULL;
106     netbuf_delete(buf);
107   }
108-#else /* GAZELLE_ENABLE */
109+#else /* GAZELLE_UDP_ENABLE */
110   LWIP_UNUSED_ARG(copylen);
111   LWIP_UNUSED_ARG(buf);
112   LWIP_UNUSED_ARG(err);
113@@ -1278,7 +1278,7 @@ lwip_recvfrom_udp_raw(struct lwip_sock *sock, int flags, struct msghdr *msg, u16
114     return ERR_BUF;
115   }
116
117-#endif /* GAZELLE_ENABLE */
118+#endif /* GAZELLE_UDP_ENABLE */
119   if (datagram_len) {
120     *datagram_len = buflen;
121   }
122diff --git a/src/core/udp.c b/src/core/udp.c
123index 170c911..1eb459d 100644
124--- a/src/core/udp.c
125+++ b/src/core/udp.c
126@@ -599,6 +599,7 @@ udp_sendto_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip,
127     UDP_STATS_INC(udp.rterr);
128     return ERR_RTE;
129   }
130+#if GAZELLE_UDP_ENABLE
131   uint8_t apiflags = 0;
132
133   struct pbuf *udp_pbuf = write_lwip_data((struct lwip_sock *)(p->payload), p->tot_len, &apiflags);
134@@ -611,14 +612,21 @@ udp_sendto_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip,
135   }
136
137   if (p->port) {
138+#if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
139+    return udp_sendto_if_chksum(pcb, p, &(p->addr), p->port, netif, have_chksum, chksum);
140+#else /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
141     return udp_sendto_if(pcb, p, &(p->addr), p->port, netif);
142+#endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
143   } else {
144+#endif /* GAZELLE_UDP_ENABLE */
145 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
146   return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, have_chksum, chksum);
147 #else /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
148   return udp_sendto_if(pcb, p, dst_ip, dst_port, netif);
149 #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
150+#if GAZELLE_UDP_ENABLE
151   }
152+#endif /* GAZELLE_UDP_ENABLE */
153 }
154
155 /**
156diff --git a/src/include/lwip/api.h b/src/include/lwip/api.h
157index d3c4f02..6090cab 100644
158--- a/src/include/lwip/api.h
159+++ b/src/include/lwip/api.h
160@@ -338,6 +338,9 @@ err_t   netconn_accept(struct netconn *conn, struct netconn **new_conn);
161 err_t   netconn_recv(struct netconn *conn, struct netbuf **new_buf);
162 err_t   netconn_recv_udp_raw_netbuf(struct netconn *conn, struct netbuf **new_buf);
163 err_t   netconn_recv_udp_raw_netbuf_flags(struct netconn *conn, struct netbuf **new_buf, u8_t apiflags);
164+#if GAZELLE_UDP_ENABLE
165+err_t   netconn_recv_udp_raw_pbuf_flags(struct netconn *conn, struct pbuf **new_buf, u8_t apiflags);
166+#endif /* GAZELLE_UDP_ENABLE */
167 err_t   netconn_recv_tcp_pbuf(struct netconn *conn, struct pbuf **new_buf);
168 err_t   netconn_recv_tcp_pbuf_flags(struct netconn *conn, struct pbuf **new_buf, u8_t apiflags);
169 err_t   netconn_tcp_recvd(struct netconn *conn, size_t len);
170diff --git a/src/include/lwip/pbuf.h b/src/include/lwip/pbuf.h
171index 728c5e4..4747f39 100644
172--- a/src/include/lwip/pbuf.h
173+++ b/src/include/lwip/pbuf.h
174@@ -40,8 +40,10 @@
175
176 #include "lwip/opt.h"
177 #include "lwip/err.h"
178+#if GAZELLE_UDP_ENABLE
179 #include "lwip/ip_addr.h"
180 #include "lwip/ip6_addr.h"
181+#endif /* GAZELLE_UDP_ENABLE */
182
183 #ifdef __cplusplus
184 extern "C" {
185@@ -238,8 +240,10 @@ struct pbuf {
186   struct pbuf *last;
187   pthread_spinlock_t pbuf_lock;
188   struct tcp_pcb *pcb;
189+#if GAZELLE_UDP_ENABLE
190   ip_addr_t addr;
191   u16_t port;
192+#endif /* GAZELLE_UDP_ENABLE */
193 #endif /* GAZELLE_ENABLE CHECKSUM_OFFLOAD_SWITCH */
194
195   /** In case the user needs to store data custom data on a pbuf */
196diff --git a/src/include/lwip/sockets.h b/src/include/lwip/sockets.h
197index 643093a..2b6e6be 100644
198--- a/src/include/lwip/sockets.h
199+++ b/src/include/lwip/sockets.h
200@@ -330,7 +330,7 @@ struct linger {
201
202
203 #if LWIP_MULTICAST_TX_OPTIONS
204-#if GAZELLE_ENABLE
205+#if GAZELLE_UDP_ENABLE
206 #define IP_MULTICAST_IF 32
207 #define IP_MULTICAST_TTL 33
208 #define IP_MULTICAST_LOOP 34
209@@ -341,11 +341,11 @@ struct linger {
210 #define IP_MULTICAST_TTL   5
211 #define IP_MULTICAST_IF    6
212 #define IP_MULTICAST_LOOP  7
213-#endif /* GAZELLE_ENABLE */
214+#endif /* GAZELLE_UDP_ENABLE */
215 #endif /* LWIP_MULTICAST_TX_OPTIONS */
216
217 #if LWIP_IGMP
218-#if GAZELLE_ENABLE
219+#if GAZELLE_UDP_ENABLE
220 #define IP_ADD_MEMBERSHIP  35
221 #define IP_DROP_MEMBERSHIP 36
222 #else
223@@ -354,7 +354,7 @@ struct linger {
224  */
225 #define IP_ADD_MEMBERSHIP  3
226 #define IP_DROP_MEMBERSHIP 4
227-#endif /* GAZELLE_ENABLE */
228+#endif /* GAZELLE_UDP_ENABLE */
229
230 typedef struct ip_mreq {
231     struct in_addr imr_multiaddr; /* IP multicast address of group */
232diff --git a/src/include/lwipopts.h b/src/include/lwipopts.h
233index 6b5a2d1..9804aed 100644
234--- a/src/include/lwipopts.h
235+++ b/src/include/lwipopts.h
236@@ -63,6 +63,10 @@
237
238 #define GAZELLE_TCP_MIN_TSO_SEG_LEN 256
239
240+
241+#define GAZELLE_UDP_ENABLE 1
242+
243+
244 /*
245    ----------------------------------
246    ---------- NIC offloads ----------
247--
2482.33.0
249
250