1 | /* $Id: udp.c 69498 2017-10-28 15:07:25Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * NAT - UDP protocol.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2016 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /*
|
---|
19 | * This code is based on:
|
---|
20 | *
|
---|
21 | * Copyright (c) 1982, 1986, 1988, 1990, 1993
|
---|
22 | * The Regents of the University of California. All rights reserved.
|
---|
23 | *
|
---|
24 | * Redistribution and use in source and binary forms, with or without
|
---|
25 | * modification, are permitted provided that the following conditions
|
---|
26 | * are met:
|
---|
27 | * 1. Redistributions of source code must retain the above copyright
|
---|
28 | * notice, this list of conditions and the following disclaimer.
|
---|
29 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
30 | * notice, this list of conditions and the following disclaimer in the
|
---|
31 | * documentation and/or other materials provided with the distribution.
|
---|
32 | * 3. All advertising materials mentioning features or use of this software
|
---|
33 | * must display the following acknowledgement:
|
---|
34 | * This product includes software developed by the University of
|
---|
35 | * California, Berkeley and its contributors.
|
---|
36 | * 4. Neither the name of the University nor the names of its contributors
|
---|
37 | * may be used to endorse or promote products derived from this software
|
---|
38 | * without specific prior written permission.
|
---|
39 | *
|
---|
40 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
---|
41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
---|
44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
50 | * SUCH DAMAGE.
|
---|
51 | *
|
---|
52 | * @(#)udp_usrreq.c 8.4 (Berkeley) 1/21/94
|
---|
53 | * udp_usrreq.c,v 1.4 1994/10/02 17:48:45 phk Exp
|
---|
54 | */
|
---|
55 |
|
---|
56 | /*
|
---|
57 | * Changes and additions relating to SLiRP
|
---|
58 | * Copyright (c) 1995 Danny Gasparovski.
|
---|
59 | *
|
---|
60 | * Please read the file COPYRIGHT for the
|
---|
61 | * terms and conditions of the copyright.
|
---|
62 | */
|
---|
63 |
|
---|
64 | #include <slirp.h>
|
---|
65 | #include "ip_icmp.h"
|
---|
66 |
|
---|
67 |
|
---|
68 | /*
|
---|
69 | * UDP protocol implementation.
|
---|
70 | * Per RFC 768, August, 1980.
|
---|
71 | */
|
---|
72 | #define udpcksum 1
|
---|
73 |
|
---|
74 | void
|
---|
75 | udp_init(PNATState pData)
|
---|
76 | {
|
---|
77 | udp_last_so = &udb;
|
---|
78 | udb.so_next = udb.so_prev = &udb;
|
---|
79 | }
|
---|
80 |
|
---|
81 | /* m->m_data points at ip packet header
|
---|
82 | * m->m_len length ip packet
|
---|
83 | * ip->ip_len length data (IPDU)
|
---|
84 | */
|
---|
85 | void
|
---|
86 | udp_input(PNATState pData, register struct mbuf *m, int iphlen)
|
---|
87 | {
|
---|
88 | register struct ip *ip;
|
---|
89 | register struct udphdr *uh;
|
---|
90 | int len;
|
---|
91 | struct ip save_ip;
|
---|
92 | struct socket *so;
|
---|
93 | int ret;
|
---|
94 | int ttl, tos;
|
---|
95 |
|
---|
96 | LogFlowFunc(("ENTER: m = %p, iphlen = %d\n", m, iphlen));
|
---|
97 | ip = mtod(m, struct ip *);
|
---|
98 | Log2(("%RTnaipv4 iphlen = %d\n", ip->ip_dst, iphlen));
|
---|
99 |
|
---|
100 | udpstat.udps_ipackets++;
|
---|
101 |
|
---|
102 | /*
|
---|
103 | * Strip IP options, if any; should skip this,
|
---|
104 | * make available to user, and use on returned packets,
|
---|
105 | * but we don't yet have a way to check the checksum
|
---|
106 | * with options still present.
|
---|
107 | */
|
---|
108 | if (iphlen > sizeof(struct ip))
|
---|
109 | {
|
---|
110 | ip_stripoptions(m, (struct mbuf *)0);
|
---|
111 | iphlen = sizeof(struct ip);
|
---|
112 | }
|
---|
113 |
|
---|
114 | /*
|
---|
115 | * Get IP and UDP header together in first mbuf.
|
---|
116 | */
|
---|
117 | ip = mtod(m, struct ip *);
|
---|
118 | uh = (struct udphdr *)((caddr_t)ip + iphlen);
|
---|
119 |
|
---|
120 | /*
|
---|
121 | * Make mbuf data length reflect UDP length.
|
---|
122 | * If not enough data to reflect UDP length, drop.
|
---|
123 | */
|
---|
124 | len = RT_N2H_U16((u_int16_t)uh->uh_ulen);
|
---|
125 | Assert(ip->ip_len + iphlen == (ssize_t)m_length(m, NULL));
|
---|
126 |
|
---|
127 | if (ip->ip_len != len)
|
---|
128 | {
|
---|
129 | if (len > ip->ip_len)
|
---|
130 | {
|
---|
131 | udpstat.udps_badlen++;
|
---|
132 | Log3(("NAT: IP(id: %hd) has bad size\n", ip->ip_id));
|
---|
133 | goto bad_free_mbuf;
|
---|
134 | }
|
---|
135 | m_adj(m, len - ip->ip_len);
|
---|
136 | ip->ip_len = len;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /*
|
---|
140 | * Save a copy of the IP header in case we want restore it
|
---|
141 | * for sending an ICMP error message in response.
|
---|
142 | */
|
---|
143 | save_ip = *ip;
|
---|
144 | save_ip.ip_len+= iphlen; /* tcp_input subtracts this */
|
---|
145 |
|
---|
146 | /*
|
---|
147 | * Checksum extended UDP header and data.
|
---|
148 | */
|
---|
149 | if (udpcksum && uh->uh_sum)
|
---|
150 | {
|
---|
151 | memset(((struct ipovly *)ip)->ih_x1, 0, 9);
|
---|
152 | ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
|
---|
153 | #if 0
|
---|
154 | /* keep uh_sum for ICMP reply */
|
---|
155 | uh->uh_sum = cksum(m, len + sizeof (struct ip));
|
---|
156 | if (uh->uh_sum)
|
---|
157 | {
|
---|
158 |
|
---|
159 | #endif
|
---|
160 | if (cksum(m, len + iphlen))
|
---|
161 | {
|
---|
162 | udpstat.udps_badsum++;
|
---|
163 | Log3(("NAT: IP(id: %hd) has bad (udp) cksum\n", ip->ip_id));
|
---|
164 | goto bad_free_mbuf;
|
---|
165 | }
|
---|
166 | }
|
---|
167 | #if 0
|
---|
168 | }
|
---|
169 | #endif
|
---|
170 |
|
---|
171 | /*
|
---|
172 | * handle DHCP/BOOTP
|
---|
173 | */
|
---|
174 | if (uh->uh_dport == RT_H2N_U16_C(BOOTP_SERVER))
|
---|
175 | {
|
---|
176 | bootp_input(pData, m);
|
---|
177 | goto done_free_mbuf;
|
---|
178 | }
|
---|
179 |
|
---|
180 | LogFunc(("uh src: %RTnaipv4:%d, dst: %RTnaipv4:%d\n",
|
---|
181 | ip->ip_src.s_addr, RT_N2H_U16(uh->uh_sport),
|
---|
182 | ip->ip_dst.s_addr, RT_N2H_U16(uh->uh_dport)));
|
---|
183 |
|
---|
184 | /*
|
---|
185 | * handle DNS host resolver without creating a socket
|
---|
186 | */
|
---|
187 | if ( pData->fUseHostResolver
|
---|
188 | && uh->uh_dport == RT_H2N_U16_C(53)
|
---|
189 | && CTL_CHECK(ip->ip_dst.s_addr, CTL_DNS))
|
---|
190 | {
|
---|
191 | struct sockaddr_in dst, src;
|
---|
192 |
|
---|
193 | src.sin_addr.s_addr = ip->ip_dst.s_addr;
|
---|
194 | src.sin_port = uh->uh_dport;
|
---|
195 | dst.sin_addr.s_addr = ip->ip_src.s_addr;
|
---|
196 | dst.sin_port = uh->uh_sport;
|
---|
197 |
|
---|
198 | m_adj(m, sizeof(struct udpiphdr));
|
---|
199 |
|
---|
200 | m = hostresolver(pData, m, ip->ip_src.s_addr, uh->uh_sport);
|
---|
201 | if (m == NULL)
|
---|
202 | goto done_free_mbuf;
|
---|
203 |
|
---|
204 | slirpMbufTagService(pData, m, CTL_DNS);
|
---|
205 |
|
---|
206 | udp_output2(pData, NULL, m, &src, &dst, IPTOS_LOWDELAY);
|
---|
207 | LogFlowFuncLeave();
|
---|
208 | return;
|
---|
209 | }
|
---|
210 |
|
---|
211 | /*
|
---|
212 | * handle TFTP
|
---|
213 | */
|
---|
214 | if ( uh->uh_dport == RT_H2N_U16_C(TFTP_SERVER)
|
---|
215 | && CTL_CHECK(ip->ip_dst.s_addr, CTL_TFTP))
|
---|
216 | {
|
---|
217 | if (pData->pvTftpSessions)
|
---|
218 | slirpTftpInput(pData, m);
|
---|
219 | goto done_free_mbuf;
|
---|
220 | }
|
---|
221 |
|
---|
222 | /*
|
---|
223 | * XXX: DNS proxy currently relies on the fact that each socket
|
---|
224 | * only serves one request.
|
---|
225 | */
|
---|
226 | if ( pData->fUseDnsProxy
|
---|
227 | && CTL_CHECK(ip->ip_dst.s_addr, CTL_DNS)
|
---|
228 | && (uh->uh_dport == RT_H2N_U16_C(53)))
|
---|
229 | {
|
---|
230 | so = NULL;
|
---|
231 | goto new_socket;
|
---|
232 | }
|
---|
233 |
|
---|
234 | /*
|
---|
235 | * Locate pcb for datagram.
|
---|
236 | */
|
---|
237 | so = udp_last_so;
|
---|
238 | if ( so->so_lport != uh->uh_sport
|
---|
239 | || so->so_laddr.s_addr != ip->ip_src.s_addr)
|
---|
240 | {
|
---|
241 | struct socket *tmp;
|
---|
242 |
|
---|
243 | for (tmp = udb.so_next; tmp != &udb; tmp = tmp->so_next)
|
---|
244 | {
|
---|
245 | if ( tmp->so_lport == uh->uh_sport
|
---|
246 | && tmp->so_laddr.s_addr == ip->ip_src.s_addr)
|
---|
247 | {
|
---|
248 | so = tmp;
|
---|
249 | break;
|
---|
250 | }
|
---|
251 | }
|
---|
252 | if (tmp == &udb)
|
---|
253 | so = NULL;
|
---|
254 | else
|
---|
255 | {
|
---|
256 | udpstat.udpps_pcbcachemiss++;
|
---|
257 | udp_last_so = so;
|
---|
258 | }
|
---|
259 | }
|
---|
260 |
|
---|
261 | new_socket:
|
---|
262 | if (so == NULL)
|
---|
263 | {
|
---|
264 | /*
|
---|
265 | * If there's no socket for this packet,
|
---|
266 | * create one
|
---|
267 | */
|
---|
268 | if ((so = socreate()) == NULL)
|
---|
269 | {
|
---|
270 | Log2(("NAT: IP(id: %hd) failed to create socket\n", ip->ip_id));
|
---|
271 | goto bad_free_mbuf;
|
---|
272 | }
|
---|
273 | if (udp_attach(pData, so) <= 0)
|
---|
274 | {
|
---|
275 | Log2(("NAT: IP(id: %hd) udp_attach errno = %d (%s)\n",
|
---|
276 | ip->ip_id, errno, strerror(errno)));
|
---|
277 | sofree(pData, so);
|
---|
278 | goto bad_free_mbuf;
|
---|
279 | }
|
---|
280 |
|
---|
281 | /*
|
---|
282 | * Setup fields
|
---|
283 | */
|
---|
284 | /* udp_last_so = so; */
|
---|
285 | so->so_laddr = ip->ip_src;
|
---|
286 | so->so_lport = uh->uh_sport;
|
---|
287 |
|
---|
288 | so->so_iptos = ip->ip_tos;
|
---|
289 |
|
---|
290 | /*
|
---|
291 | * XXXXX Here, check if it's in udpexec_list,
|
---|
292 | * and if it is, do the fork_exec() etc.
|
---|
293 | */
|
---|
294 | }
|
---|
295 |
|
---|
296 | so->so_faddr = ip->ip_dst; /* XXX */
|
---|
297 | so->so_fport = uh->uh_dport; /* XXX */
|
---|
298 | Assert(so->so_type == IPPROTO_UDP);
|
---|
299 |
|
---|
300 | /*
|
---|
301 | * DNS proxy
|
---|
302 | */
|
---|
303 | if ( pData->fUseDnsProxy
|
---|
304 | && CTL_CHECK(ip->ip_dst.s_addr, CTL_DNS)
|
---|
305 | && (uh->uh_dport == RT_H2N_U16_C(53)))
|
---|
306 | {
|
---|
307 | dnsproxy_query(pData, so, m, iphlen);
|
---|
308 | goto done_free_mbuf;
|
---|
309 | }
|
---|
310 |
|
---|
311 | iphlen += sizeof(struct udphdr);
|
---|
312 | m->m_len -= iphlen;
|
---|
313 | m->m_data += iphlen;
|
---|
314 |
|
---|
315 | ttl = ip->ip_ttl = save_ip.ip_ttl;
|
---|
316 | if (ttl != so->so_sottl) {
|
---|
317 | ret = setsockopt(so->s, IPPROTO_IP, IP_TTL,
|
---|
318 | (char *)&ttl, sizeof(ttl));
|
---|
319 | if (RT_LIKELY(ret == 0))
|
---|
320 | so->so_sottl = ttl;
|
---|
321 | }
|
---|
322 |
|
---|
323 | tos = save_ip.ip_tos;
|
---|
324 | if (tos != so->so_sotos) {
|
---|
325 | ret = setsockopt(so->s, IPPROTO_IP, IP_TOS,
|
---|
326 | (char *)&tos, sizeof(tos));
|
---|
327 | if (RT_LIKELY(ret == 0))
|
---|
328 | so->so_sotos = tos;
|
---|
329 | }
|
---|
330 |
|
---|
331 | {
|
---|
332 | /*
|
---|
333 | * Different OSes have different socket options for DF. We
|
---|
334 | * can't use IP_HDRINCL here as it's only valid for SOCK_RAW.
|
---|
335 | */
|
---|
336 | # define USE_DF_OPTION(_Optname) \
|
---|
337 | const int dfopt = _Optname
|
---|
338 | #if defined(IP_MTU_DISCOVER)
|
---|
339 | USE_DF_OPTION(IP_MTU_DISCOVER);
|
---|
340 | #elif defined(IP_DONTFRAG) /* Solaris 11+, FreeBSD */
|
---|
341 | USE_DF_OPTION(IP_DONTFRAG);
|
---|
342 | #elif defined(IP_DONTFRAGMENT) /* Windows */
|
---|
343 | USE_DF_OPTION(IP_DONTFRAGMENT);
|
---|
344 | #else
|
---|
345 | USE_DF_OPTION(0);
|
---|
346 | #endif
|
---|
347 | if (dfopt) {
|
---|
348 | int df = (save_ip.ip_off & IP_DF) != 0;
|
---|
349 | #if defined(IP_MTU_DISCOVER)
|
---|
350 | df = df ? IP_PMTUDISC_DO : IP_PMTUDISC_DONT;
|
---|
351 | #endif
|
---|
352 | if (df != so->so_sodf) {
|
---|
353 | ret = setsockopt(so->s, IPPROTO_IP, dfopt,
|
---|
354 | (char *)&df, sizeof(df));
|
---|
355 | if (RT_LIKELY(ret == 0))
|
---|
356 | so->so_sodf = df;
|
---|
357 | }
|
---|
358 | }
|
---|
359 | }
|
---|
360 |
|
---|
361 | if ( sosendto(pData, so, m) == -1
|
---|
362 | && ( !soIgnorableErrorCode(errno)
|
---|
363 | && errno != ENOTCONN))
|
---|
364 | {
|
---|
365 | m->m_len += iphlen;
|
---|
366 | m->m_data -= iphlen;
|
---|
367 | *ip = save_ip;
|
---|
368 | Log2(("NAT: UDP tx errno = %d (%s) on sent to %RTnaipv4\n",
|
---|
369 | errno, strerror(errno), ip->ip_dst));
|
---|
370 | icmp_error(pData, m, ICMP_UNREACH, ICMP_UNREACH_NET, 0, strerror(errno));
|
---|
371 | so->so_m = NULL;
|
---|
372 | LogFlowFuncLeave();
|
---|
373 | return;
|
---|
374 | }
|
---|
375 |
|
---|
376 | if (so->so_m)
|
---|
377 | m_freem(pData, so->so_m); /* used for ICMP if error on sorecvfrom */
|
---|
378 |
|
---|
379 | /* restore the orig mbuf packet */
|
---|
380 | m->m_len += iphlen;
|
---|
381 | m->m_data -= iphlen;
|
---|
382 | *ip = save_ip;
|
---|
383 | so->so_m = m; /* ICMP backup */
|
---|
384 | LogFlowFuncLeave();
|
---|
385 | return;
|
---|
386 |
|
---|
387 | bad_free_mbuf:
|
---|
388 | Log2(("NAT: UDP(id: %hd) datagram to %RTnaipv4 with size(%d) claimed as bad\n",
|
---|
389 | ip->ip_id, &ip->ip_dst, ip->ip_len));
|
---|
390 |
|
---|
391 | done_free_mbuf:
|
---|
392 | /* some services like bootp(built-in), dns(buildt-in) and dhcp don't need sockets
|
---|
393 | * and create new m'buffers to send them to guest, so we'll free their incomming
|
---|
394 | * buffers here.
|
---|
395 | */
|
---|
396 | if (m != NULL)
|
---|
397 | m_freem(pData, m);
|
---|
398 | LogFlowFuncLeave();
|
---|
399 | return;
|
---|
400 | }
|
---|
401 |
|
---|
402 | /**
|
---|
403 | * Output a UDP packet.
|
---|
404 | *
|
---|
405 | * @note This function will finally free m!
|
---|
406 | */
|
---|
407 | int udp_output2(PNATState pData, struct socket *so, struct mbuf *m,
|
---|
408 | struct sockaddr_in *saddr, struct sockaddr_in *daddr,
|
---|
409 | int iptos)
|
---|
410 | {
|
---|
411 | register struct udpiphdr *ui;
|
---|
412 | int error;
|
---|
413 | int mlen = 0;
|
---|
414 |
|
---|
415 | LogFlowFunc(("ENTER: so = %R[natsock], m = %p, saddr = %RTnaipv4, daddr = %RTnaipv4\n",
|
---|
416 | so, m, saddr->sin_addr.s_addr, daddr->sin_addr.s_addr));
|
---|
417 |
|
---|
418 | /* in case of built-in service so might be NULL */
|
---|
419 | if (so) Assert(so->so_type == IPPROTO_UDP);
|
---|
420 |
|
---|
421 | /*
|
---|
422 | * Adjust for header
|
---|
423 | */
|
---|
424 | m->m_data -= sizeof(struct udpiphdr);
|
---|
425 | m->m_len += sizeof(struct udpiphdr);
|
---|
426 | mlen = m_length(m, NULL);
|
---|
427 |
|
---|
428 | /*
|
---|
429 | * Fill in mbuf with extended UDP header
|
---|
430 | * and addresses and length put into network format.
|
---|
431 | */
|
---|
432 | ui = mtod(m, struct udpiphdr *);
|
---|
433 | memset(ui->ui_x1, 0, 9);
|
---|
434 | ui->ui_pr = IPPROTO_UDP;
|
---|
435 | ui->ui_len = RT_H2N_U16((uint16_t)(mlen - sizeof(struct ip)));
|
---|
436 | /* XXXXX Check for from-one-location sockets, or from-any-location sockets */
|
---|
437 | ui->ui_src = saddr->sin_addr;
|
---|
438 | ui->ui_dst = daddr->sin_addr;
|
---|
439 | ui->ui_sport = saddr->sin_port;
|
---|
440 | ui->ui_dport = daddr->sin_port;
|
---|
441 | ui->ui_ulen = ui->ui_len;
|
---|
442 |
|
---|
443 | /*
|
---|
444 | * Stuff checksum and output datagram.
|
---|
445 | */
|
---|
446 | ui->ui_sum = 0;
|
---|
447 | if (udpcksum)
|
---|
448 | {
|
---|
449 | if ((ui->ui_sum = cksum(m, /* sizeof (struct udpiphdr) + */ mlen)) == 0)
|
---|
450 | ui->ui_sum = 0xffff;
|
---|
451 | }
|
---|
452 | ((struct ip *)ui)->ip_len = mlen;
|
---|
453 | ((struct ip *)ui)->ip_ttl = ip_defttl;
|
---|
454 | ((struct ip *)ui)->ip_tos = iptos;
|
---|
455 |
|
---|
456 | udpstat.udps_opackets++;
|
---|
457 |
|
---|
458 | error = ip_output(pData, so, m);
|
---|
459 |
|
---|
460 | return error;
|
---|
461 | }
|
---|
462 |
|
---|
463 | /**
|
---|
464 | * @note This function will free m!
|
---|
465 | */
|
---|
466 | int udp_output(PNATState pData, struct socket *so, struct mbuf *m,
|
---|
467 | struct sockaddr_in *addr)
|
---|
468 | {
|
---|
469 | struct sockaddr_in saddr, daddr;
|
---|
470 | #ifdef VBOX_WITH_NAT_UDP_SOCKET_CLONE
|
---|
471 | struct socket *pSocketClone = NULL;
|
---|
472 | #endif
|
---|
473 | Assert(so->so_type == IPPROTO_UDP);
|
---|
474 | LogFlowFunc(("ENTER: so = %R[natsock], m = %p, saddr = %RTnaipv4\n", so, m, addr->sin_addr.s_addr));
|
---|
475 |
|
---|
476 | if (so->so_laddr.s_addr == INADDR_ANY)
|
---|
477 | {
|
---|
478 | if (pData->guest_addr_guess.s_addr != INADDR_ANY)
|
---|
479 | {
|
---|
480 | LogRel2(("NAT: port-forward: using %RTnaipv4 for %R[natsock]\n",
|
---|
481 | pData->guest_addr_guess.s_addr, so));
|
---|
482 | so->so_laddr = pData->guest_addr_guess;
|
---|
483 | }
|
---|
484 | else
|
---|
485 | {
|
---|
486 | LogRel2(("NAT: port-forward: guest address unknown for %R[natsock]\n", so));
|
---|
487 | m_freem(pData, m);
|
---|
488 | return 0;
|
---|
489 | }
|
---|
490 | }
|
---|
491 |
|
---|
492 | saddr = *addr;
|
---|
493 | if ((so->so_faddr.s_addr & RT_H2N_U32(pData->netmask)) == pData->special_addr.s_addr)
|
---|
494 | {
|
---|
495 | saddr.sin_addr.s_addr = so->so_faddr.s_addr;
|
---|
496 | if (slirpIsWideCasting(pData, so->so_faddr.s_addr))
|
---|
497 | {
|
---|
498 | /**
|
---|
499 | * We haven't got real firewall but have got its submodule libalias.
|
---|
500 | */
|
---|
501 | m->m_flags |= M_SKIP_FIREWALL;
|
---|
502 | /**
|
---|
503 | * udp/137 port is Name Service in NetBIOS protocol. for some reasons Windows guest rejects
|
---|
504 | * accept data from non-aliased server.
|
---|
505 | */
|
---|
506 | if ( (so->so_fport == so->so_lport)
|
---|
507 | && (so->so_fport == RT_H2N_U16(137)))
|
---|
508 | saddr.sin_addr.s_addr = alias_addr.s_addr;
|
---|
509 | else
|
---|
510 | saddr.sin_addr.s_addr = addr->sin_addr.s_addr;
|
---|
511 | /* we shouldn't override initial socket */
|
---|
512 | #ifdef VBOX_WITH_NAT_UDP_SOCKET_CLONE
|
---|
513 | if (so->so_cCloneCounter)
|
---|
514 | pSocketClone = soLookUpClonedUDPSocket(pData, so, addr->sin_addr.s_addr);
|
---|
515 | if (!pSocketClone)
|
---|
516 | pSocketClone = soCloneUDPSocketWithForegnAddr(pData, false, so, addr->sin_addr.s_addr);
|
---|
517 | Assert((pSocketClone));
|
---|
518 | so = pSocketClone;
|
---|
519 | #else
|
---|
520 | so->so_faddr.s_addr = addr->sin_addr.s_addr;
|
---|
521 | #endif
|
---|
522 | }
|
---|
523 | }
|
---|
524 |
|
---|
525 | /* Any UDP packet to the loopback address must be translated to be from
|
---|
526 | * the forwarding address, i.e. 10.0.2.2. */
|
---|
527 | if ( (saddr.sin_addr.s_addr & RT_H2N_U32_C(IN_CLASSA_NET))
|
---|
528 | == RT_H2N_U32_C(INADDR_LOOPBACK & IN_CLASSA_NET))
|
---|
529 | saddr.sin_addr.s_addr = alias_addr.s_addr;
|
---|
530 |
|
---|
531 | daddr.sin_addr = so->so_laddr;
|
---|
532 | daddr.sin_port = so->so_lport;
|
---|
533 |
|
---|
534 | return udp_output2(pData, so, m, &saddr, &daddr, so->so_iptos);
|
---|
535 | }
|
---|
536 |
|
---|
537 | int
|
---|
538 | udp_attach(PNATState pData, struct socket *so)
|
---|
539 | {
|
---|
540 | struct sockaddr_in *addr;
|
---|
541 | struct sockaddr sa_addr;
|
---|
542 | socklen_t socklen = sizeof(struct sockaddr);
|
---|
543 | int status;
|
---|
544 | int opt = 1;
|
---|
545 |
|
---|
546 | /* We attaching some olready attched socket ??? */
|
---|
547 | Assert(so->so_type == 0);
|
---|
548 | if ((so->s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
|
---|
549 | goto error;
|
---|
550 | so->so_sottl = 0;
|
---|
551 | so->so_sotos = 0;
|
---|
552 | so->so_sodf = -1;
|
---|
553 | /*
|
---|
554 | * Here, we bind() the socket. Although not really needed
|
---|
555 | * (sendto() on an unbound socket will bind it), it's done
|
---|
556 | * here so that emulation of ytalk etc. don't have to do it
|
---|
557 | */
|
---|
558 | memset(&sa_addr, 0, sizeof(struct sockaddr));
|
---|
559 | addr = (struct sockaddr_in *)&sa_addr;
|
---|
560 | #ifdef RT_OS_DARWIN
|
---|
561 | addr->sin_len = sizeof(struct sockaddr_in);
|
---|
562 | #endif
|
---|
563 | addr->sin_family = AF_INET;
|
---|
564 | addr->sin_addr.s_addr = pData->bindIP.s_addr;
|
---|
565 | fd_nonblock(so->s);
|
---|
566 | if (bind(so->s, &sa_addr, sizeof(struct sockaddr_in)) < 0)
|
---|
567 | {
|
---|
568 | int lasterrno = errno;
|
---|
569 | closesocket(so->s);
|
---|
570 | so->s = -1;
|
---|
571 | #ifdef RT_OS_WINDOWS
|
---|
572 | WSASetLastError(lasterrno);
|
---|
573 | #else
|
---|
574 | errno = lasterrno;
|
---|
575 | #endif
|
---|
576 | goto error;
|
---|
577 | }
|
---|
578 | /* success, insert in queue */
|
---|
579 | so->so_expire = curtime + SO_EXPIRE;
|
---|
580 | /* enable broadcast for later use */
|
---|
581 | setsockopt(so->s, SOL_SOCKET, SO_BROADCAST, (const char *)&opt, sizeof(opt));
|
---|
582 | status = getsockname(so->s, &sa_addr, &socklen);
|
---|
583 | Assert(status == 0 && sa_addr.sa_family == AF_INET);
|
---|
584 | so->so_hlport = ((struct sockaddr_in *)&sa_addr)->sin_port;
|
---|
585 | so->so_hladdr.s_addr = ((struct sockaddr_in *)&sa_addr)->sin_addr.s_addr;
|
---|
586 |
|
---|
587 | SOCKET_LOCK_CREATE(so);
|
---|
588 | QSOCKET_LOCK(udb);
|
---|
589 | insque(pData, so, &udb);
|
---|
590 | NSOCK_INC();
|
---|
591 | QSOCKET_UNLOCK(udb);
|
---|
592 | so->so_type = IPPROTO_UDP;
|
---|
593 | return so->s;
|
---|
594 | error:
|
---|
595 | Log2(("NAT: can't create datagramm socket\n"));
|
---|
596 | return -1;
|
---|
597 | }
|
---|
598 |
|
---|
599 | void
|
---|
600 | udp_detach(PNATState pData, struct socket *so)
|
---|
601 | {
|
---|
602 | if (so != &pData->icmp_socket)
|
---|
603 | {
|
---|
604 | Assert(so->so_type == IPPROTO_UDP);
|
---|
605 | QSOCKET_LOCK(udb);
|
---|
606 | SOCKET_LOCK(so);
|
---|
607 | QSOCKET_UNLOCK(udb);
|
---|
608 | #ifdef VBOX_WITH_NAT_UDP_SOCKET_CLONE
|
---|
609 | if (so->so_cloneOf)
|
---|
610 | so->so_cloneOf->so_cCloneCounter--;
|
---|
611 | else if (so->so_cCloneCounter > 0)
|
---|
612 | {
|
---|
613 | /* we can't close socket yet */
|
---|
614 | SOCKET_UNLOCK(so);
|
---|
615 | return;
|
---|
616 | }
|
---|
617 | #endif
|
---|
618 | closesocket(so->s);
|
---|
619 | sofree(pData, so);
|
---|
620 | SOCKET_UNLOCK(so);
|
---|
621 | }
|
---|
622 | }
|
---|
623 |
|
---|
624 | struct socket *
|
---|
625 | udp_listen(PNATState pData, u_int32_t bind_addr, u_int port, u_int32_t laddr, u_int lport, int flags)
|
---|
626 | {
|
---|
627 | struct sockaddr_in addr;
|
---|
628 | struct socket *so;
|
---|
629 | socklen_t addrlen = sizeof(struct sockaddr_in);
|
---|
630 | int opt = 1;
|
---|
631 | LogFlowFunc(("ENTER: bind_addr:%RTnaipv4, port:%d, laddr:%RTnaipv4, lport:%d, flags:%x\n",
|
---|
632 | bind_addr, RT_N2H_U16(port), laddr, RT_N2H_U16(lport), flags));
|
---|
633 |
|
---|
634 | if ((so = socreate()) == NULL)
|
---|
635 | {
|
---|
636 | LogFlowFunc(("LEAVE: NULL\n"));
|
---|
637 | return NULL;
|
---|
638 | }
|
---|
639 |
|
---|
640 | so->s = socket(AF_INET, SOCK_DGRAM, 0);
|
---|
641 | if (so->s == -1)
|
---|
642 | {
|
---|
643 | LogRel(("NAT: can't create datagram socket\n"));
|
---|
644 | RTMemFree(so);
|
---|
645 | LogFlowFunc(("LEAVE: NULL\n"));
|
---|
646 | return NULL;
|
---|
647 | }
|
---|
648 | so->so_expire = curtime + SO_EXPIRE;
|
---|
649 | so->so_type = IPPROTO_UDP;
|
---|
650 | fd_nonblock(so->s);
|
---|
651 | so->so_sottl = 0;
|
---|
652 | so->so_sotos = 0;
|
---|
653 | so->so_sodf = -1;
|
---|
654 | SOCKET_LOCK_CREATE(so);
|
---|
655 | QSOCKET_LOCK(udb);
|
---|
656 | insque(pData, so, &udb);
|
---|
657 | NSOCK_INC();
|
---|
658 | QSOCKET_UNLOCK(udb);
|
---|
659 |
|
---|
660 | memset(&addr, 0, sizeof(addr));
|
---|
661 | #ifdef RT_OS_DARWIN
|
---|
662 | addr.sin_len = sizeof(addr);
|
---|
663 | #endif
|
---|
664 | addr.sin_family = AF_INET;
|
---|
665 | addr.sin_addr.s_addr = bind_addr;
|
---|
666 | addr.sin_port = port;
|
---|
667 |
|
---|
668 | if (bind(so->s,(struct sockaddr *)&addr, addrlen) < 0)
|
---|
669 | {
|
---|
670 | LogRel(("NAT: udp bind to %RTnaipv4:%d failed, error %d\n",
|
---|
671 | addr.sin_addr, RT_N2H_U16(port), errno));
|
---|
672 | udp_detach(pData, so);
|
---|
673 | LogFlowFunc(("LEAVE: NULL\n"));
|
---|
674 | return NULL;
|
---|
675 | }
|
---|
676 | setsockopt(so->s, SOL_SOCKET, SO_REUSEADDR,(char *)&opt, sizeof(int));
|
---|
677 | /* setsockopt(so->s, SOL_SOCKET, SO_OOBINLINE,(char *)&opt, sizeof(int)); */
|
---|
678 |
|
---|
679 | getsockname(so->s,(struct sockaddr *)&addr,&addrlen);
|
---|
680 | so->so_hladdr = addr.sin_addr;
|
---|
681 | so->so_hlport = addr.sin_port;
|
---|
682 |
|
---|
683 | /* XXX: wtf are we setting so_faddr/so_fport here? */
|
---|
684 | so->so_fport = addr.sin_port;
|
---|
685 | #if 0
|
---|
686 | /* The original check was completely broken, as the commented out
|
---|
687 | * if statement was always true (INADDR_ANY=0). */
|
---|
688 | /** @todo vvl - alias_addr should be set (if required)
|
---|
689 | * later by liabalias module.
|
---|
690 | */
|
---|
691 | if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
|
---|
692 | so->so_faddr = alias_addr;
|
---|
693 | else
|
---|
694 | #endif
|
---|
695 | so->so_faddr = addr.sin_addr;
|
---|
696 |
|
---|
697 | so->so_lport = lport;
|
---|
698 | so->so_laddr.s_addr = laddr;
|
---|
699 | if (flags != SS_FACCEPTONCE)
|
---|
700 | so->so_expire = 0;
|
---|
701 |
|
---|
702 | so->so_state = SS_ISFCONNECTED;
|
---|
703 |
|
---|
704 | LogFlowFunc(("LEAVE: %R[natsock]\n", so));
|
---|
705 | return so;
|
---|
706 | }
|
---|