VirtualBox

source: vbox/trunk/src/VBox/NetworkServices/NAT/fwtcp.c@ 106061

Last change on this file since 106061 was 106061, checked in by vboxsync, 8 weeks ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 7.3 KB
Line 
1/* $Id: fwtcp.c 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * NAT Network - TCP port-forwarding.
4 */
5
6/*
7 * Copyright (C) 2013-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28#define LOG_GROUP LOG_GROUP_NAT_SERVICE
29
30#include "winutils.h"
31#include "proxy.h"
32#include "proxy_pollmgr.h"
33#include "portfwd.h"
34#include "pxtcp.h"
35
36#ifndef RT_OS_WINDOWS
37#include <sys/types.h>
38#include <sys/socket.h>
39#include <arpa/inet.h>
40#include <stdio.h>
41#include <poll.h>
42
43#include <err.h> /* BSD'ism */
44#else
45#include <stdio.h>
46#include "winpoll.h"
47#endif
48
49#include "lwip/opt.h"
50
51#include "lwip/sys.h"
52#include "lwip/tcpip.h"
53
54
55/**
56 */
57struct fwtcp {
58 /**
59 * Our poll manager handler.
60 */
61 struct pollmgr_handler pmhdl;
62
63 /**
64 * Forwarding specification.
65 */
66 struct fwspec fwspec;
67
68 /**
69 * Listening socket.
70 */
71 SOCKET sock;
72
73 /**
74 * Mailbox for new inbound connections.
75 *
76 * XXX: since we have single producer and single consumer we can
77 * use lockless ringbuf like for pxtcp.
78 */
79 sys_mbox_t connmbox;
80
81 struct tcpip_msg msg_connect;
82 struct tcpip_msg msg_delete;
83
84 /**
85 * Linked list entry.
86 */
87 struct fwtcp *next;
88};
89
90
91static struct fwtcp *fwtcp_create(struct fwspec *);
92
93/* poll manager callback for fwtcp listening socket */
94static int fwtcp_pmgr_listen(struct pollmgr_handler *, SOCKET, int);
95
96/* lwip thread callbacks called via proxy_lwip_post() */
97static void fwtcp_pcb_connect(void *);
98static void fwtcp_pcb_delete(void *);
99
100
101/**
102 * Linked list of active fwtcp forwarders.
103 */
104struct fwtcp *fwtcp_list = NULL;
105
106
107void
108fwtcp_init(void)
109{
110 return;
111}
112
113
114void
115fwtcp_add(struct fwspec *fwspec)
116{
117 struct fwtcp *fwtcp;
118
119 fwtcp = fwtcp_create(fwspec);
120 if (fwtcp == NULL) {
121 DPRINTF0(("%s: failed to add rule for TCP ...\n", __func__));
122 return;
123 }
124
125 DPRINTF0(("%s\n", __func__));
126 /* fwtcp_create has put fwtcp on the linked list */
127}
128
129
130void
131fwtcp_del(struct fwspec *fwspec)
132{
133 struct fwtcp *fwtcp;
134 struct fwtcp **pprev;
135
136 for (pprev = &fwtcp_list; (fwtcp = *pprev) != NULL; pprev = &fwtcp->next) {
137 if (fwspec_equal(&fwtcp->fwspec, fwspec)) {
138 *pprev = fwtcp->next;
139 fwtcp->next = NULL;
140 break;
141 }
142 }
143
144 if (fwtcp == NULL) {
145 DPRINTF0(("%s: not found\n", __func__));
146 return;
147 }
148
149 DPRINTF0(("%s\n", __func__));
150
151 pollmgr_del_slot(fwtcp->pmhdl.slot);
152 fwtcp->pmhdl.slot = -1;
153
154 closesocket(fwtcp->sock);
155 fwtcp->sock = INVALID_SOCKET;
156
157 /* let pending msg_connect be processed before we delete fwtcp */
158 proxy_lwip_post(&fwtcp->msg_delete);
159}
160
161
162struct fwtcp *
163fwtcp_create(struct fwspec *fwspec)
164{
165 struct fwtcp *fwtcp;
166 SOCKET lsock;
167 int status;
168 err_t error;
169
170 lsock = proxy_bound_socket(fwspec->sdom, fwspec->stype, &fwspec->src.sa);
171 if (lsock == INVALID_SOCKET) {
172 return NULL;
173 }
174
175 fwtcp = (struct fwtcp *)malloc(sizeof(*fwtcp));
176 if (fwtcp == NULL) {
177 closesocket(lsock);
178 return NULL;
179 }
180
181 fwtcp->pmhdl.callback = fwtcp_pmgr_listen;
182 fwtcp->pmhdl.data = (void *)fwtcp;
183 fwtcp->pmhdl.slot = -1;
184
185 fwtcp->sock = lsock;
186 fwtcp->fwspec = *fwspec; /* struct copy */
187
188 error = sys_mbox_new(&fwtcp->connmbox, 16);
189 if (error != ERR_OK) {
190 closesocket(lsock);
191 free(fwtcp);
192 return (NULL);
193 }
194
195#define CALLBACK_MSG(MSG, FUNC) \
196 do { \
197 fwtcp->MSG.type = TCPIP_MSG_CALLBACK_STATIC; \
198 fwtcp->MSG.sem = NULL; \
199 fwtcp->MSG.msg.cb.function = FUNC; \
200 fwtcp->MSG.msg.cb.ctx = (void *)fwtcp; \
201 } while (0)
202
203 CALLBACK_MSG(msg_connect, fwtcp_pcb_connect);
204 CALLBACK_MSG(msg_delete, fwtcp_pcb_delete);
205
206#undef CALLBACK_MSG
207
208 status = pollmgr_add(&fwtcp->pmhdl, fwtcp->sock, POLLIN);
209 if (status < 0) {
210 sys_mbox_free(&fwtcp->connmbox);
211 closesocket(lsock);
212 free(fwtcp);
213 return NULL;
214 }
215
216 fwtcp->next = fwtcp_list;
217 fwtcp_list = fwtcp;
218
219 return fwtcp;
220}
221
222
223int
224fwtcp_pmgr_listen(struct pollmgr_handler *handler, SOCKET fd, int revents)
225{
226 struct fwtcp *fwtcp;
227 struct sockaddr_storage ss;
228 socklen_t sslen;
229 struct pxtcp *pxtcp;
230 SOCKET newsock;
231 int status;
232 err_t error;
233
234 fwtcp = (struct fwtcp *)handler->data;
235 pxtcp = NULL;
236
237 LWIP_ASSERT1(fwtcp != NULL);
238 LWIP_ASSERT1(fd == fwtcp->sock);
239 LWIP_ASSERT1(revents == POLLIN);
240 LWIP_UNUSED_ARG(fd);
241 LWIP_UNUSED_ARG(revents);
242
243 LWIP_ASSERT1(sys_mbox_valid(&fwtcp->connmbox));
244
245 sslen = sizeof(ss);
246 newsock = accept(fwtcp->sock, (struct sockaddr *)&ss, &sslen);
247 if (newsock == INVALID_SOCKET) {
248 return POLLIN;
249 }
250
251#ifdef RT_OS_LINUX
252 status = proxy_fixup_accepted_socket(newsock);
253 if (status < 0) {
254 proxy_reset_socket(newsock);
255 return POLLIN;
256 }
257#endif
258
259 if (ss.ss_family == PF_INET) {
260 struct sockaddr_in *peer4 = (struct sockaddr_in *)&ss;
261 RT_NOREF(peer4);
262 DPRINTF(("<--- TCP %RTnaipv4:%d\n",
263 peer4->sin_addr.s_addr, ntohs(peer4->sin_port)));
264 }
265 else { /* PF_INET6 */
266 struct sockaddr_in6 *peer6 = (struct sockaddr_in6 *)&ss;
267 RT_NOREF(peer6);
268 DPRINTF(("<--- TCP %RTnaipv6:%d\n",
269 &peer6->sin6_addr, ntohs(peer6->sin6_port)));
270 }
271
272 pxtcp = pxtcp_create_forwarded(newsock);
273 if (pxtcp == NULL) {
274 proxy_reset_socket(newsock);
275 return POLLIN;
276 }
277
278 status = pxtcp_pmgr_add(pxtcp);
279 if (status < 0) {
280 pxtcp_cancel_forwarded(pxtcp);
281 return POLLIN;
282 }
283
284 error = sys_mbox_trypost(&fwtcp->connmbox, (void *)pxtcp);
285 if (error != ERR_OK) {
286 pxtcp_pmgr_del(pxtcp);
287 pxtcp_cancel_forwarded(pxtcp);
288 return POLLIN;
289 }
290
291 proxy_lwip_post(&fwtcp->msg_connect);
292 return POLLIN;
293}
294
295
296void
297fwtcp_pcb_connect(void *arg)
298{
299 struct fwtcp *fwtcp = (struct fwtcp *)arg;
300 struct pxtcp *pxtcp;
301 u32_t timo;
302
303 if (!sys_mbox_valid(&fwtcp->connmbox)) {
304 return;
305 }
306
307 pxtcp = NULL;
308 timo = sys_mbox_tryfetch(&fwtcp->connmbox, (void **)&pxtcp);
309 if (timo == SYS_MBOX_EMPTY) {
310 return;
311 }
312
313 LWIP_ASSERT1(pxtcp != NULL);
314
315 /* hand off to pxtcp */
316 pxtcp_pcb_connect(pxtcp, &fwtcp->fwspec);
317}
318
319
320static void
321fwtcp_pcb_delete(void *arg)
322{
323 struct fwtcp *fwtcp = (struct fwtcp *)arg;
324 void *data;
325 u32_t timo;
326
327 timo = sys_mbox_tryfetch(&fwtcp->connmbox, &data);
328 LWIP_ASSERT1(timo == SYS_MBOX_EMPTY);
329 LWIP_UNUSED_ARG(timo); /* only in assert */
330
331 sys_mbox_free(&fwtcp->connmbox);
332 free(fwtcp);
333}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette