1 | /* -*- indent-tabs-mode: nil; -*- */
|
---|
2 | #define LOG_GROUP LOG_GROUP_NAT_SERVICE
|
---|
3 |
|
---|
4 | #include "winutils.h"
|
---|
5 | #include "proxy.h"
|
---|
6 | #include "proxy_pollmgr.h"
|
---|
7 | #include "portfwd.h"
|
---|
8 | #include "pxtcp.h"
|
---|
9 |
|
---|
10 | #ifndef RT_OS_WINDOWS
|
---|
11 | #include <sys/types.h>
|
---|
12 | #include <sys/socket.h>
|
---|
13 | #include <arpa/inet.h>
|
---|
14 | #include <stdio.h>
|
---|
15 | #include <poll.h>
|
---|
16 |
|
---|
17 | #include <err.h> /* BSD'ism */
|
---|
18 | #else
|
---|
19 | #include <stdio.h>
|
---|
20 | #include "winpoll.h"
|
---|
21 | #endif
|
---|
22 |
|
---|
23 | #include "lwip/opt.h"
|
---|
24 |
|
---|
25 | #include "lwip/sys.h"
|
---|
26 | #include "lwip/tcpip.h"
|
---|
27 |
|
---|
28 |
|
---|
29 | /**
|
---|
30 | */
|
---|
31 | struct fwtcp {
|
---|
32 | /**
|
---|
33 | * Our poll manager handler.
|
---|
34 | */
|
---|
35 | struct pollmgr_handler pmhdl;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Forwarding specification.
|
---|
39 | */
|
---|
40 | struct fwspec fwspec;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Listening socket.
|
---|
44 | */
|
---|
45 | SOCKET sock;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Mailbox for new inbound connections.
|
---|
49 | *
|
---|
50 | * XXX: since we have single producer and single consumer we can
|
---|
51 | * use lockless ringbuf like for pxtcp.
|
---|
52 | */
|
---|
53 | sys_mbox_t connmbox;
|
---|
54 |
|
---|
55 | struct tcpip_msg msg_connect;
|
---|
56 | struct tcpip_msg msg_delete;
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Linked list entry.
|
---|
60 | */
|
---|
61 | struct fwtcp *next;
|
---|
62 | };
|
---|
63 |
|
---|
64 |
|
---|
65 | static struct fwtcp *fwtcp_create(struct fwspec *);
|
---|
66 |
|
---|
67 | /* poll manager callback for fwtcp listening socket */
|
---|
68 | static int fwtcp_pmgr_listen(struct pollmgr_handler *, SOCKET, int);
|
---|
69 |
|
---|
70 | /* lwip thread callbacks called via proxy_lwip_post() */
|
---|
71 | static void fwtcp_pcb_connect(void *);
|
---|
72 | static void fwtcp_pcb_delete(void *);
|
---|
73 |
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Linked list of active fwtcp forwarders.
|
---|
77 | */
|
---|
78 | struct fwtcp *fwtcp_list = NULL;
|
---|
79 |
|
---|
80 |
|
---|
81 | void
|
---|
82 | fwtcp_init(void)
|
---|
83 | {
|
---|
84 | return;
|
---|
85 | }
|
---|
86 |
|
---|
87 |
|
---|
88 | void
|
---|
89 | fwtcp_add(struct fwspec *fwspec)
|
---|
90 | {
|
---|
91 | struct fwtcp *fwtcp;
|
---|
92 |
|
---|
93 | fwtcp = fwtcp_create(fwspec);
|
---|
94 | if (fwtcp == NULL) {
|
---|
95 | DPRINTF0(("%s: failed to add rule for TCP ...\n", __func__));
|
---|
96 | return;
|
---|
97 | }
|
---|
98 |
|
---|
99 | DPRINTF0(("%s\n", __func__));
|
---|
100 | /* fwtcp_create has put fwtcp on the linked list */
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | void
|
---|
105 | fwtcp_del(struct fwspec *fwspec)
|
---|
106 | {
|
---|
107 | struct fwtcp *fwtcp;
|
---|
108 | struct fwtcp **pprev;
|
---|
109 |
|
---|
110 | for (pprev = &fwtcp_list; (fwtcp = *pprev) != NULL; pprev = &fwtcp->next) {
|
---|
111 | if (fwspec_equal(&fwtcp->fwspec, fwspec)) {
|
---|
112 | *pprev = fwtcp->next;
|
---|
113 | fwtcp->next = NULL;
|
---|
114 | break;
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | if (fwtcp == NULL) {
|
---|
119 | DPRINTF0(("%s: not found\n", __func__));
|
---|
120 | return;
|
---|
121 | }
|
---|
122 |
|
---|
123 | DPRINTF0(("%s\n", __func__));
|
---|
124 |
|
---|
125 | pollmgr_del_slot(fwtcp->pmhdl.slot);
|
---|
126 | fwtcp->pmhdl.slot = -1;
|
---|
127 |
|
---|
128 | closesocket(fwtcp->sock);
|
---|
129 | fwtcp->sock = INVALID_SOCKET;
|
---|
130 |
|
---|
131 | /* let pending msg_connect be processed before we delete fwtcp */
|
---|
132 | proxy_lwip_post(&fwtcp->msg_delete);
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | struct fwtcp *
|
---|
137 | fwtcp_create(struct fwspec *fwspec)
|
---|
138 | {
|
---|
139 | struct fwtcp *fwtcp;
|
---|
140 | SOCKET lsock;
|
---|
141 | int status;
|
---|
142 | err_t error;
|
---|
143 |
|
---|
144 | lsock = proxy_bound_socket(fwspec->sdom, fwspec->stype, &fwspec->src.sa);
|
---|
145 | if (lsock == INVALID_SOCKET) {
|
---|
146 | return NULL;
|
---|
147 | }
|
---|
148 |
|
---|
149 | fwtcp = (struct fwtcp *)malloc(sizeof(*fwtcp));
|
---|
150 | if (fwtcp == NULL) {
|
---|
151 | closesocket(lsock);
|
---|
152 | return NULL;
|
---|
153 | }
|
---|
154 |
|
---|
155 | fwtcp->pmhdl.callback = fwtcp_pmgr_listen;
|
---|
156 | fwtcp->pmhdl.data = (void *)fwtcp;
|
---|
157 | fwtcp->pmhdl.slot = -1;
|
---|
158 |
|
---|
159 | fwtcp->sock = lsock;
|
---|
160 | fwtcp->fwspec = *fwspec; /* struct copy */
|
---|
161 |
|
---|
162 | error = sys_mbox_new(&fwtcp->connmbox, 16);
|
---|
163 | if (error != ERR_OK) {
|
---|
164 | closesocket(lsock);
|
---|
165 | free(fwtcp);
|
---|
166 | return (NULL);
|
---|
167 | }
|
---|
168 |
|
---|
169 | #define CALLBACK_MSG(MSG, FUNC) \
|
---|
170 | do { \
|
---|
171 | fwtcp->MSG.type = TCPIP_MSG_CALLBACK_STATIC; \
|
---|
172 | fwtcp->MSG.sem = NULL; \
|
---|
173 | fwtcp->MSG.msg.cb.function = FUNC; \
|
---|
174 | fwtcp->MSG.msg.cb.ctx = (void *)fwtcp; \
|
---|
175 | } while (0)
|
---|
176 |
|
---|
177 | CALLBACK_MSG(msg_connect, fwtcp_pcb_connect);
|
---|
178 | CALLBACK_MSG(msg_delete, fwtcp_pcb_delete);
|
---|
179 |
|
---|
180 | #undef CALLBACK_MSG
|
---|
181 |
|
---|
182 | status = pollmgr_add(&fwtcp->pmhdl, fwtcp->sock, POLLIN);
|
---|
183 | if (status < 0) {
|
---|
184 | sys_mbox_free(&fwtcp->connmbox);
|
---|
185 | closesocket(lsock);
|
---|
186 | free(fwtcp);
|
---|
187 | return NULL;
|
---|
188 | }
|
---|
189 |
|
---|
190 | fwtcp->next = fwtcp_list;
|
---|
191 | fwtcp_list = fwtcp;
|
---|
192 |
|
---|
193 | return fwtcp;
|
---|
194 | }
|
---|
195 |
|
---|
196 |
|
---|
197 | int
|
---|
198 | fwtcp_pmgr_listen(struct pollmgr_handler *handler, SOCKET fd, int revents)
|
---|
199 | {
|
---|
200 | struct fwtcp *fwtcp;
|
---|
201 | struct sockaddr_storage ss;
|
---|
202 | socklen_t sslen;
|
---|
203 | struct pxtcp *pxtcp;
|
---|
204 | SOCKET newsock;
|
---|
205 | int status;
|
---|
206 | err_t error;
|
---|
207 |
|
---|
208 | fwtcp = (struct fwtcp *)handler->data;
|
---|
209 | pxtcp = NULL;
|
---|
210 |
|
---|
211 | LWIP_ASSERT1(fwtcp != NULL);
|
---|
212 | LWIP_ASSERT1(fd == fwtcp->sock);
|
---|
213 | LWIP_ASSERT1(revents == POLLIN);
|
---|
214 | LWIP_UNUSED_ARG(fd);
|
---|
215 | LWIP_UNUSED_ARG(revents);
|
---|
216 |
|
---|
217 | LWIP_ASSERT1(sys_mbox_valid(&fwtcp->connmbox));
|
---|
218 |
|
---|
219 | sslen = sizeof(ss);
|
---|
220 | newsock = accept(fwtcp->sock, (struct sockaddr *)&ss, &sslen);
|
---|
221 | if (newsock == INVALID_SOCKET) {
|
---|
222 | return POLLIN;
|
---|
223 | }
|
---|
224 |
|
---|
225 |
|
---|
226 | if (ss.ss_family == PF_INET) {
|
---|
227 | struct sockaddr_in *peer4 = (struct sockaddr_in *)&ss;
|
---|
228 | DPRINTF(("<--- TCP %RTnaipv4:%d\n",
|
---|
229 | peer4->sin_addr.s_addr, ntohs(peer4->sin_port)));
|
---|
230 | }
|
---|
231 | else { /* PF_INET6 */
|
---|
232 | struct sockaddr_in6 *peer6 = (struct sockaddr_in6 *)&ss;
|
---|
233 | DPRINTF(("<--- TCP %RTnaipv6:%d\n",
|
---|
234 | &peer6->sin6_addr, ntohs(peer6->sin6_port)));
|
---|
235 | }
|
---|
236 |
|
---|
237 | pxtcp = pxtcp_create_forwarded(newsock);
|
---|
238 | if (pxtcp == NULL) {
|
---|
239 | proxy_reset_socket(newsock);
|
---|
240 | return POLLIN;
|
---|
241 | }
|
---|
242 |
|
---|
243 | status = pxtcp_pmgr_add(pxtcp);
|
---|
244 | if (status < 0) {
|
---|
245 | pxtcp_cancel_forwarded(pxtcp);
|
---|
246 | return POLLIN;
|
---|
247 | }
|
---|
248 |
|
---|
249 | error = sys_mbox_trypost(&fwtcp->connmbox, (void *)pxtcp);
|
---|
250 | if (error != ERR_OK) {
|
---|
251 | pxtcp_pmgr_del(pxtcp);
|
---|
252 | pxtcp_cancel_forwarded(pxtcp);
|
---|
253 | return POLLIN;
|
---|
254 | }
|
---|
255 |
|
---|
256 | proxy_lwip_post(&fwtcp->msg_connect);
|
---|
257 | return POLLIN;
|
---|
258 | }
|
---|
259 |
|
---|
260 |
|
---|
261 | void
|
---|
262 | fwtcp_pcb_connect(void *arg)
|
---|
263 | {
|
---|
264 | struct fwtcp *fwtcp = (struct fwtcp *)arg;
|
---|
265 | struct pxtcp *pxtcp;
|
---|
266 | u32_t timo;
|
---|
267 |
|
---|
268 | if (!sys_mbox_valid(&fwtcp->connmbox)) {
|
---|
269 | return;
|
---|
270 | }
|
---|
271 |
|
---|
272 | pxtcp = NULL;
|
---|
273 | timo = sys_mbox_tryfetch(&fwtcp->connmbox, (void **)&pxtcp);
|
---|
274 | if (timo == SYS_MBOX_EMPTY) {
|
---|
275 | return;
|
---|
276 | }
|
---|
277 |
|
---|
278 | LWIP_ASSERT1(pxtcp != NULL);
|
---|
279 |
|
---|
280 | /* hand off to pxtcp */
|
---|
281 | pxtcp_pcb_connect(pxtcp, &fwtcp->fwspec);
|
---|
282 | }
|
---|
283 |
|
---|
284 |
|
---|
285 | static void
|
---|
286 | fwtcp_pcb_delete(void *arg)
|
---|
287 | {
|
---|
288 | struct fwtcp *fwtcp = (struct fwtcp *)arg;
|
---|
289 | void *data;
|
---|
290 | u32_t timo;
|
---|
291 |
|
---|
292 | timo = sys_mbox_tryfetch(&fwtcp->connmbox, &data);
|
---|
293 | LWIP_ASSERT1(timo == SYS_MBOX_EMPTY);
|
---|
294 | LWIP_UNUSED_ARG(timo); /* only in assert */
|
---|
295 |
|
---|
296 | sys_mbox_free(&fwtcp->connmbox);
|
---|
297 | free(fwtcp);
|
---|
298 | }
|
---|