VirtualBox

source: vbox/trunk/src/VBox/NetworkServices/NAT/proxy.c@ 49707

Last change on this file since 49707 was 49606, checked in by vboxsync, 11 years ago

proxy_init: Add call to pxping_init(), #if0'ed for now.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 12.4 KB
Line 
1/* -*- indent-tabs-mode: nil; -*- */
2#include "winutils.h"
3
4#include "proxy.h"
5#include "proxy_pollmgr.h"
6#include "portfwd.h"
7
8#include "lwip/opt.h"
9
10#include "lwip/sys.h"
11#include "lwip/tcpip.h"
12
13#ifndef RT_OS_WINDOWS
14#include <sys/poll.h>
15#include <sys/socket.h>
16#include <netinet/in.h>
17#include <arpa/inet.h>
18#include <fcntl.h>
19#include <stdio.h>
20#include <iprt/string.h>
21#include <unistd.h>
22#include <err.h>
23#else
24# include <iprt/string.h>
25#endif
26
27#if defined(SOCK_NONBLOCK) && defined(RT_OS_NETBSD) /* XXX: PR kern/47569 */
28# undef SOCK_NONBLOCK
29#endif
30
31#ifndef __arraycount
32# define __arraycount(a) (sizeof(a)/sizeof(a[0]))
33#endif
34
35static SOCKET proxy_create_socket(int, int);
36
37volatile struct proxy_options *g_proxy_options;
38static sys_thread_t pollmgr_tid;
39
40/* XXX: for mapping loopbacks to addresses in our network (ip4) */
41struct netif *g_proxy_netif;
42/*
43 * Called on the lwip thread (aka tcpip thread) from tcpip_init() via
44 * its "tcpip_init_done" callback. Raw API is ok to use here
45 * (e.g. rtadvd), but netconn API is not.
46 */
47void
48proxy_init(struct netif *proxy_netif, struct proxy_options *opts)
49{
50 int status;
51
52 LWIP_ASSERT1(opts != NULL);
53 LWIP_UNUSED_ARG(proxy_netif);
54
55 g_proxy_options = opts;
56 g_proxy_netif = proxy_netif;
57
58#if 1
59 proxy_rtadvd_start(proxy_netif);
60#endif
61
62 /*
63 * XXX: We use stateless DHCPv6 only to report IPv6 address(es) of
64 * nameserver(s). Since we don't yet support IPv6 addresses in
65 * HostDnsService, there's no point in running DHCPv6.
66 */
67#if 0
68 dhcp6ds_init(proxy_netif);
69#endif
70
71 if (opts->tftp_root != NULL) {
72 tftpd_init(proxy_netif, opts->tftp_root);
73 }
74
75 status = pollmgr_init();
76 if (status < 0) {
77 errx(EXIT_FAILURE, "failed to initialize poll manager");
78 /* NOTREACHED */
79 }
80
81 pxtcp_init();
82 pxudp_init();
83
84 portfwd_init();
85
86 pxdns_init(proxy_netif);
87
88#if 0 /* XXX */
89 pxping_init(proxy_netif, opts->icmpsock4, opts->icmpsock6);
90#endif
91
92 pollmgr_tid = sys_thread_new("pollmgr_thread",
93 pollmgr_thread, NULL,
94 DEFAULT_THREAD_STACKSIZE,
95 DEFAULT_THREAD_PRIO);
96 if (!pollmgr_tid) {
97 errx(EXIT_FAILURE, "failed to create poll manager thread");
98 /* NOTREACHED */
99 }
100}
101
102
103/**
104 * Send static callback message from poll manager thread to lwip
105 * thread, scheduling a function call in lwip thread context.
106 *
107 * XXX: Existing lwip api only provides non-blocking version for this.
108 * It may fail when lwip thread is not running (mbox invalid) or if
109 * post failed (mbox full). How to handle these?
110 */
111void
112proxy_lwip_post(struct tcpip_msg *msg)
113{
114 struct tcpip_callback_msg *m;
115 err_t error;
116
117 LWIP_ASSERT1(msg != NULL);
118
119 /*
120 * lwip plays games with fake incomplete struct tag to enforce API
121 */
122 m = (struct tcpip_callback_msg *)msg;
123 error = tcpip_callbackmsg(m);
124
125 if (error == ERR_VAL) {
126 /* XXX: lwip thread is not running (mbox invalid) */
127 LWIP_ASSERT1(error != ERR_VAL);
128 }
129
130 LWIP_ASSERT1(error == ERR_OK);
131}
132
133
134/**
135 * Create a non-blocking socket. Disable SIGPIPE for TCP sockets if
136 * possible. On Linux it's not possible and should be disabled for
137 * each send(2) individually.
138 */
139static SOCKET
140proxy_create_socket(int sdom, int stype)
141{
142 SOCKET s;
143 int stype_and_flags;
144 int status;
145
146 LWIP_UNUSED_ARG(status); /* depends on ifdefs */
147
148
149 stype_and_flags = stype;
150
151#if defined(SOCK_NONBLOCK)
152 stype_and_flags |= SOCK_NONBLOCK;
153#endif
154
155 /*
156 * Disable SIGPIPE on disconnected socket. It might be easier to
157 * forgo it and just use MSG_NOSIGNAL on each send*(2), since we
158 * have to do it for Linux anyway, but Darwin does NOT have that
159 * flag (but has SO_NOSIGPIPE socket option).
160 */
161#if !defined(SOCK_NOSIGPIPE) && !defined(SO_NOSIGPIPE) && !defined(MSG_NOSIGNAL)
162#if 0 /* XXX: Solaris has neither, the program should ignore SIGPIPE globally */
163#error Need a way to disable SIGPIPE on connection oriented sockets!
164#endif
165#endif
166
167#if defined(SOCK_NOSIGPIPE)
168 if (stype == SOCK_STREAM) {
169 stype_and_flags |= SOCK_NOSIGPIPE;
170 }
171#endif
172
173 s = socket(sdom, stype_and_flags, 0);
174 if (s == INVALID_SOCKET) {
175 perror("socket");
176 return INVALID_SOCKET;
177 }
178
179#if !defined(SOCK_NONBLOCK) && !defined(RT_OS_WINDOWS)
180 {
181 int sflags;
182
183 status = fcntl(s, F_GETFL, &sflags);
184 if (status < 0) {
185 perror("F_GETFL");
186 closesocket(s);
187 return INVALID_SOCKET;
188 }
189
190 status = fcntl(s, F_SETFL, sflags | O_NONBLOCK);
191 if (status < 0) {
192 perror("O_NONBLOCK");
193 closesocket(s);
194 return INVALID_SOCKET;
195 }
196 }
197#endif
198
199#if !defined(SOCK_NOSIGPIPE) && defined(SO_NOSIGPIPE)
200 if (stype == SOCK_STREAM) {
201 int on = 1;
202 const socklen_t onlen = sizeof(on);
203
204 status = setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, &on, onlen);
205 if (status < 0) {
206 perror("SO_NOSIGPIPE");
207 closesocket(s);
208 return INVALID_SOCKET;
209 }
210 }
211#endif
212
213#if defined(RT_OS_WINDOWS)
214 {
215 u_long mode = 0;
216 status = ioctlsocket(s, FIONBIO, &mode);
217 if (status == SOCKET_ERROR) {
218 warn("ioctl error: %d\n", WSAGetLastError());
219 return INVALID_SOCKET;
220 }
221 }
222#endif
223
224 return s;
225}
226
227
228/**
229 * Create a socket for outbound connection to dst_addr:dst_port.
230 *
231 * The socket is non-blocking and TCP sockets has SIGPIPE disabled if
232 * possible. On Linux it's not possible and should be disabled for
233 * each send(2) individually.
234 */
235SOCKET
236proxy_connected_socket(int sdom, int stype,
237 ipX_addr_t *dst_addr, u16_t dst_port)
238{
239 struct sockaddr_in6 dst_sin6;
240 struct sockaddr_in dst_sin;
241 struct sockaddr *pdst_sa;
242 socklen_t dst_sa_len;
243 void *pdst_addr;
244 const struct sockaddr *psrc_sa;
245 socklen_t src_sa_len;
246 int status;
247 SOCKET s;
248
249 LWIP_ASSERT1(sdom == PF_INET || sdom == PF_INET6);
250 LWIP_ASSERT1(stype == SOCK_STREAM || stype == SOCK_DGRAM);
251
252 if (sdom == PF_INET6) {
253 pdst_sa = (struct sockaddr *)&dst_sin6;
254 pdst_addr = (void *)&dst_sin6.sin6_addr;
255
256 memset(&dst_sin6, 0, sizeof(dst_sin6));
257#if HAVE_SA_LEN
258 dst_sin6.sin6_len =
259#endif
260 dst_sa_len = sizeof(dst_sin6);
261 dst_sin6.sin6_family = AF_INET6;
262 memcpy(&dst_sin6.sin6_addr, &dst_addr->ip6, sizeof(ip6_addr_t));
263 dst_sin6.sin6_port = htons(dst_port);
264 }
265 else { /* sdom = PF_INET */
266 pdst_sa = (struct sockaddr *)&dst_sin;
267 pdst_addr = (void *)&dst_sin.sin_addr;
268
269 memset(&dst_sin, 0, sizeof(dst_sin));
270#if HAVE_SA_LEN
271 dst_sin.sin_len =
272#endif
273 dst_sa_len = sizeof(dst_sin);
274 dst_sin.sin_family = AF_INET;
275 dst_sin.sin_addr.s_addr = dst_addr->ip4.addr; /* byte-order? */
276 dst_sin.sin_port = htons(dst_port);
277 }
278
279#if LWIP_PROXY_DEBUG && !RT_OS_WINDOWS
280 {
281 char addrbuf[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
282 const char *addrstr;
283
284 addrstr = inet_ntop(sdom, pdst_addr, addrbuf, sizeof(addrbuf));
285 DPRINTF(("---> %s %s%s%s:%d ",
286 stype == SOCK_STREAM ? "TCP" : "UDP",
287 sdom == PF_INET6 ? "[" : "",
288 addrstr,
289 sdom == PF_INET6 ? "]" : "",
290 dst_port));
291 }
292#endif
293
294 s = proxy_create_socket(sdom, stype);
295 if (s == INVALID_SOCKET) {
296 return INVALID_SOCKET;
297 }
298 DPRINTF(("socket %d\n", s));
299
300 /* TODO: needs locking if dynamic modifyvm is allowed */
301 if (sdom == PF_INET6) {
302 psrc_sa = (const struct sockaddr *)g_proxy_options->src6;
303 src_sa_len = sizeof(struct sockaddr_in6);
304 }
305 else {
306 psrc_sa = (const struct sockaddr *)g_proxy_options->src4;
307 src_sa_len = sizeof(struct sockaddr_in);
308 }
309 if (psrc_sa != NULL) {
310 status = bind(s, psrc_sa, src_sa_len);
311 if (status == SOCKET_ERROR) {
312 DPRINTF(("socket %d: bind: %s\n", s, strerror(errno)));
313 closesocket(s);
314 return INVALID_SOCKET;
315 }
316 }
317
318 status = connect(s, pdst_sa, dst_sa_len);
319 if (status == SOCKET_ERROR && errno != EINPROGRESS) {
320 DPRINTF(("socket %d: connect: %s\n", s, strerror(errno)));
321 closesocket(s);
322 return INVALID_SOCKET;
323 }
324
325 return s;
326}
327
328
329/**
330 * Create a socket for inbound (port-forwarded) connections to
331 * src_addr (port is part of sockaddr, so not a separate argument).
332 *
333 * The socket is non-blocking and TCP sockets has SIGPIPE disabled if
334 * possible. On Linux it's not possible and should be disabled for
335 * each send(2) individually.
336 *
337 * TODO?: Support v6-mapped v4 so that user can specify she wants
338 * "udp" and get both versions?
339 */
340SOCKET
341proxy_bound_socket(int sdom, int stype, struct sockaddr *src_addr)
342{
343 SOCKET s;
344 int on;
345 const socklen_t onlen = sizeof(on);
346 int status;
347
348 s = proxy_create_socket(sdom, stype);
349 if (s == INVALID_SOCKET) {
350 return INVALID_SOCKET;
351 }
352 DPRINTF(("socket %d\n", s));
353
354 on = 1;
355 status = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&on, onlen);
356 if (status < 0) { /* not good, but not fatal */
357 warn("SO_REUSEADDR");
358 }
359
360 status = bind(s, src_addr,
361 sdom == PF_INET ?
362 sizeof(struct sockaddr_in)
363 : sizeof(struct sockaddr_in6));
364 if (status < 0) {
365 perror("bind");
366 closesocket(s);
367 return INVALID_SOCKET;
368 }
369
370 if (stype == SOCK_STREAM) {
371 status = listen(s, 5);
372 if (status < 0) {
373 perror("listen");
374 closesocket(s);
375 return INVALID_SOCKET;
376 }
377 }
378
379 return s;
380}
381
382
383void
384proxy_reset_socket(SOCKET s)
385{
386 struct linger linger;
387
388 linger.l_onoff = 1;
389 linger.l_linger = 0;
390
391 /* On Windows we can run into issue here, perhaps SO_LINGER isn't enough, and
392 * we should use WSA{Send,Recv}Disconnect instead.
393 *
394 * Links for the reference:
395 * http://msdn.microsoft.com/en-us/library/windows/desktop/ms738547%28v=vs.85%29.aspx
396 * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4468997
397 */
398 setsockopt(s, SOL_SOCKET, SO_LINGER, (char *)&linger, sizeof(linger));
399
400 closesocket(s);
401}
402
403
404void
405proxy_sendto(SOCKET sock, struct pbuf *p, void *name, size_t namelen)
406{
407 struct pbuf *q;
408 size_t i, clen;
409#ifndef RT_OS_WINDOWS
410 struct msghdr mh;
411#else
412 int rc;
413#endif
414 IOVEC fixiov[8]; /* fixed size (typical case) */
415 const size_t fixiovsize = sizeof(fixiov)/sizeof(fixiov[0]);
416 IOVEC *dyniov; /* dynamically sized */
417 IOVEC *iov;
418 ssize_t nsent;
419
420 /*
421 * Static iov[] is usually enough since UDP protocols use small
422 * datagrams to avoid fragmentation, but be prepared.
423 */
424 clen = pbuf_clen(p);
425 if (clen > fixiovsize) {
426 /*
427 * XXX: TODO: check that clen is shorter than IOV_MAX
428 */
429 dyniov = (IOVEC *)malloc(clen * sizeof(*dyniov));
430 if (dyniov == NULL) {
431 goto out;
432 }
433 iov = dyniov;
434 }
435 else {
436 dyniov = NULL;
437 iov = fixiov;
438 }
439
440
441 for (q = p, i = 0; i < clen; q = q->next, ++i) {
442 LWIP_ASSERT1(q != NULL);
443
444 IOVEC_SET_BASE(iov[i], q->payload);
445 IOVEC_SET_LEN(iov[i], q->len);
446 }
447
448#ifndef RT_OS_WINDOWS
449 memset(&mh, 0, sizeof(mh));
450 mh.msg_name = name;
451 mh.msg_namelen = namelen;
452 mh.msg_iov = iov;
453 mh.msg_iovlen = clen;
454
455 nsent = sendmsg(sock, &mh, 0);
456 if (nsent < 0) {
457 DPRINTF(("%s: fd %d: sendmsg errno %d\n",
458 __func__, sock, errno));
459 }
460#else
461 rc = WSASendTo(sock, iov, (DWORD)clen, (DWORD *)&nsent, 0, name, (int)namelen, NULL, NULL);
462 if (rc == SOCKET_ERROR) {
463 DPRINTF(("%s: fd %d: sendmsg errno %d\n",
464 __func__, sock, WSAGetLastError()));
465 }
466#endif
467
468 out:
469 if (dyniov != NULL) {
470 free(dyniov);
471 }
472 pbuf_free(p);
473}
474
475
476static const char *lwiperr[] = {
477 "ERR_OK",
478 "ERR_MEM",
479 "ERR_BUF",
480 "ERR_TIMEOUT",
481 "ERR_RTE",
482 "ERR_INPROGRESS",
483 "ERR_VAL",
484 "ERR_WOULDBLOCK",
485 "ERR_USE",
486 "ERR_ISCONN",
487 "ERR_ABRT",
488 "ERR_RST",
489 "ERR_CLSD",
490 "ERR_CONN",
491 "ERR_ARG",
492 "ERR_IF"
493};
494
495
496const char *
497proxy_lwip_strerr(err_t error)
498{
499 static char buf[32];
500 int e = -error;
501
502 if (0 < e || e < (int)__arraycount(lwiperr)) {
503 return lwiperr[e];
504 }
505 else {
506 RTStrPrintf(buf, sizeof(buf), "unknown error %d", error);
507 return buf;
508 }
509}
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