VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/slirp/udp.c@ 28587

Last change on this file since 28587 was 28522, checked in by vboxsync, 15 years ago

NAT: logs l3->l2.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.9 KB
Line 
1/* $Id: udp.c 28522 2010-04-20 13:42:15Z vboxsync $ */
2/** @file
3 * NAT - UDP protocol.
4 */
5
6/*
7 * Copyright (C) 2006-2010 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22/*
23 * This code is based on:
24 *
25 * Copyright (c) 1982, 1986, 1988, 1990, 1993
26 * The Regents of the University of California. All rights reserved.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
55 *
56 * @(#)udp_usrreq.c 8.4 (Berkeley) 1/21/94
57 * udp_usrreq.c,v 1.4 1994/10/02 17:48:45 phk Exp
58 */
59
60/*
61 * Changes and additions relating to SLiRP
62 * Copyright (c) 1995 Danny Gasparovski.
63 *
64 * Please read the file COPYRIGHT for the
65 * terms and conditions of the copyright.
66 */
67
68#include <slirp.h>
69#include "ip_icmp.h"
70#include "ctl.h"
71
72
73/*
74 * UDP protocol implementation.
75 * Per RFC 768, August, 1980.
76 */
77#define udpcksum 1
78
79void
80udp_init(PNATState pData)
81{
82 udp_last_so = &udb;
83 udb.so_next = udb.so_prev = &udb;
84}
85
86/* m->m_data points at ip packet header
87 * m->m_len length ip packet
88 * ip->ip_len length data (IPDU)
89 */
90void
91udp_input(PNATState pData, register struct mbuf *m, int iphlen)
92{
93 register struct ip *ip;
94 register struct udphdr *uh;
95 int len;
96 struct ip save_ip;
97 struct socket *so;
98 int ret;
99 int ttl;
100
101 DEBUG_CALL("udp_input");
102 DEBUG_ARG("m = %lx", (long)m);
103 ip = mtod(m, struct ip *);
104 DEBUG_ARG("iphlen = %d", iphlen);
105 Log2(("%R[IP4] iphlen = %d\n", &ip->ip_dst, iphlen));
106
107 udpstat.udps_ipackets++;
108
109 /*
110 * Strip IP options, if any; should skip this,
111 * make available to user, and use on returned packets,
112 * but we don't yet have a way to check the checksum
113 * with options still present.
114 */
115 if (iphlen > sizeof(struct ip))
116 {
117 ip_stripoptions(m, (struct mbuf *)0);
118 iphlen = sizeof(struct ip);
119 }
120
121 /*
122 * Get IP and UDP header together in first mbuf.
123 */
124 ip = mtod(m, struct ip *);
125 uh = (struct udphdr *)((caddr_t)ip + iphlen);
126
127 /*
128 * Make mbuf data length reflect UDP length.
129 * If not enough data to reflect UDP length, drop.
130 */
131 len = RT_N2H_U16((u_int16_t)uh->uh_ulen);
132 Assert((ip->ip_len == len));
133#ifndef VBOX_WITH_SLIRP_BSD_MBUF
134 Assert((ip->ip_len + iphlen == m->m_len));
135#else
136 Assert((ip->ip_len + iphlen == m_length(m, NULL)));
137#endif
138
139 if (ip->ip_len != len)
140 {
141 if (len > ip->ip_len)
142 {
143 udpstat.udps_badlen++;
144 Log3(("NAT: IP(id: %hd) has bad size\n", ip->ip_id));
145 }
146 m_adj(m, len - ip->ip_len);
147 ip->ip_len = len;
148 }
149
150 /*
151 * Save a copy of the IP header in case we want restore it
152 * for sending an ICMP error message in response.
153 */
154 save_ip = *ip;
155 save_ip.ip_len+= iphlen; /* tcp_input subtracts this */
156
157 /*
158 * Checksum extended UDP header and data.
159 */
160 if (udpcksum && uh->uh_sum)
161 {
162 memset(((struct ipovly *)ip)->ih_x1, 0, 9);
163 ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
164#if 0
165 /* keep uh_sum for ICMP reply */
166 uh->uh_sum = cksum(m, len + sizeof (struct ip));
167 if (uh->uh_sum)
168 {
169
170#endif
171 if(cksum(m, len + iphlen))
172 {
173 udpstat.udps_badsum++;
174 Log3(("NAT: IP(id: %hd) has bad (udp) cksum\n", ip->ip_id));
175 goto bad;
176 }
177 }
178#if 0
179 }
180#endif
181
182 /*
183 * handle DHCP/BOOTP
184 */
185 if (uh->uh_dport == RT_H2N_U16_C(BOOTP_SERVER))
186 {
187 bootp_input(pData, m);
188 goto done;
189 }
190
191 if ( pData->fUseHostResolver
192 && uh->uh_dport == RT_H2N_U16_C(53)
193 && CTL_CHECK(RT_N2H_U32(ip->ip_dst.s_addr), CTL_DNS))
194 {
195 struct sockaddr_in dst, src;
196 src.sin_addr.s_addr = ip->ip_dst.s_addr;
197 src.sin_port = uh->uh_dport;
198 dst.sin_addr.s_addr = ip->ip_src.s_addr;
199 dst.sin_port = uh->uh_sport;
200 /* udp_output2 will do opposite operations on mbuf*/
201
202 m->m_data += sizeof(struct udpiphdr);
203 m->m_len -= sizeof(struct udpiphdr);
204 udp_output2(pData, NULL, m, &src, &dst, IPTOS_LOWDELAY);
205 goto done;
206 }
207 /*
208 * handle TFTP
209 */
210 if ( uh->uh_dport == RT_H2N_U16_C(TFTP_SERVER)
211 && CTL_CHECK(RT_N2H_U32(ip->ip_dst.s_addr), CTL_TFTP))
212 {
213 tftp_input(pData, m);
214 goto done;
215 }
216
217 /*
218 * Locate pcb for datagram.
219 */
220 so = udp_last_so;
221 if ( so->so_lport != uh->uh_sport
222 || so->so_laddr.s_addr != ip->ip_src.s_addr)
223 {
224 struct socket *tmp;
225
226 for (tmp = udb.so_next; tmp != &udb; tmp = tmp->so_next)
227 {
228 if ( tmp->so_lport == uh->uh_sport
229 && tmp->so_laddr.s_addr == ip->ip_src.s_addr)
230 {
231 so = tmp;
232 break;
233 }
234 }
235 if (tmp == &udb)
236 so = NULL;
237 else
238 {
239 udpstat.udpps_pcbcachemiss++;
240 udp_last_so = so;
241 }
242 }
243
244 if (so == NULL)
245 {
246 /*
247 * If there's no socket for this packet,
248 * create one
249 */
250 if ((so = socreate()) == NULL)
251 {
252 Log2(("NAT: IP(id: %hd) failed to create socket\n", ip->ip_id));
253 goto bad;
254 }
255 if (udp_attach(pData, so, 0) == -1)
256 {
257 Log2(("NAT: IP(id: %hd) udp_attach errno = %d-%s\n",
258 ip->ip_id, errno, strerror(errno)));
259 sofree(pData, so);
260 goto bad;
261 }
262
263 /*
264 * Setup fields
265 */
266 /* udp_last_so = so; */
267 so->so_laddr = ip->ip_src;
268 so->so_lport = uh->uh_sport;
269
270 so->so_iptos = ip->ip_tos;
271
272 /*
273 * XXXXX Here, check if it's in udpexec_list,
274 * and if it is, do the fork_exec() etc.
275 */
276 }
277
278 so->so_faddr = ip->ip_dst; /* XXX */
279 so->so_fport = uh->uh_dport; /* XXX */
280
281 /*
282 * DNS proxy
283 */
284 if ( pData->fUseDnsProxy
285 && (ip->ip_dst.s_addr == RT_H2N_U32(RT_N2H_U32(pData->special_addr.s_addr) | CTL_DNS))
286 && (uh->uh_dport == RT_H2N_U16_C(53)))
287 {
288 dnsproxy_query(pData, so, m, iphlen);
289 goto done;
290 }
291
292 iphlen += sizeof(struct udphdr);
293 m->m_len -= iphlen;
294 m->m_data += iphlen;
295
296 ttl = ip->ip_ttl = save_ip.ip_ttl;
297 ret = setsockopt(so->s, IPPROTO_IP, IP_TTL, (const char*)&ttl, sizeof(ttl));
298 if (ret < 0)
299 LogRel(("NAT: Error (%s) occurred while setting TTL(%d) attribute "
300 "of IP packet to socket %R[natsock]\n", strerror(errno), ip->ip_ttl, so));
301
302 if (sosendto(pData, so, m) == -1)
303 {
304 m->m_len += iphlen;
305 m->m_data -= iphlen;
306 *ip = save_ip;
307 DEBUG_MISC((dfd,"NAT: UDP tx errno = %d-%s (on sent to %R[IP4])\n", errno,
308 strerror(errno), &ip->ip_dst));
309 icmp_error(pData, m, ICMP_UNREACH, ICMP_UNREACH_NET, 0, strerror(errno));
310 /* in case we receive ICMP on this socket we'll aware that ICMP has been already sent to host*/
311 so->so_m = NULL;
312 }
313
314 if (so->so_m)
315 m_freem(pData, so->so_m); /* used for ICMP if error on sorecvfrom */
316
317 /* restore the orig mbuf packet */
318 m->m_len += iphlen;
319 m->m_data -= iphlen;
320 *ip = save_ip;
321 so->so_m = m; /* ICMP backup */
322
323 return;
324
325bad:
326 Log2(("NAT: UDP(id: %hd) datagram to %R[IP4] with size(%d) claimed as bad\n",
327 ip->ip_id, &ip->ip_dst, ip->ip_len));
328done:
329 /* some services like bootp(built-in), dns(buildt-in) and dhcp don't need sockets
330 * and create new m'buffers to send them to guest, so we'll free their incomming
331 * buffers here.
332 */
333 m_freem(pData, m);
334 return;
335}
336
337int udp_output2(PNATState pData, struct socket *so, struct mbuf *m,
338 struct sockaddr_in *saddr, struct sockaddr_in *daddr,
339 int iptos)
340{
341 register struct udpiphdr *ui;
342 int error = 0;
343
344 DEBUG_CALL("udp_output");
345 DEBUG_ARG("so = %lx", (long)so);
346 DEBUG_ARG("m = %lx", (long)m);
347 DEBUG_ARG("saddr = %lx", (long)saddr->sin_addr.s_addr);
348 DEBUG_ARG("daddr = %lx", (long)daddr->sin_addr.s_addr);
349
350 /*
351 * Adjust for header
352 */
353 m->m_data -= sizeof(struct udpiphdr);
354 m->m_len += sizeof(struct udpiphdr);
355
356 /*
357 * Fill in mbuf with extended UDP header
358 * and addresses and length put into network format.
359 */
360 ui = mtod(m, struct udpiphdr *);
361 memset(ui->ui_x1, 0, 9);
362 ui->ui_pr = IPPROTO_UDP;
363 ui->ui_len = RT_H2N_U16(m->m_len - sizeof(struct ip));
364 /* XXXXX Check for from-one-location sockets, or from-any-location sockets */
365 ui->ui_src = saddr->sin_addr;
366 ui->ui_dst = daddr->sin_addr;
367 ui->ui_sport = saddr->sin_port;
368 ui->ui_dport = daddr->sin_port;
369 ui->ui_ulen = ui->ui_len;
370
371 /*
372 * Stuff checksum and output datagram.
373 */
374 ui->ui_sum = 0;
375 if (udpcksum)
376 {
377 if ((ui->ui_sum = cksum(m, /* sizeof (struct udpiphdr) + */ m->m_len)) == 0)
378 ui->ui_sum = 0xffff;
379 }
380 ((struct ip *)ui)->ip_len = m->m_len;
381 ((struct ip *)ui)->ip_ttl = ip_defttl;
382 ((struct ip *)ui)->ip_tos = iptos;
383
384 udpstat.udps_opackets++;
385
386 error = ip_output(pData, so, m);
387
388 return error;
389}
390
391int udp_output(PNATState pData, struct socket *so, struct mbuf *m,
392 struct sockaddr_in *addr)
393{
394 struct sockaddr_in saddr, daddr;
395
396 saddr = *addr;
397 if ((so->so_faddr.s_addr & RT_H2N_U32(pData->netmask)) == pData->special_addr.s_addr)
398 {
399 saddr.sin_addr.s_addr = so->so_faddr.s_addr;
400 if ((so->so_faddr.s_addr & RT_H2N_U32(~pData->netmask)) == RT_H2N_U32(~pData->netmask))
401 saddr.sin_addr.s_addr = alias_addr.s_addr;
402 }
403
404 /* Any UDP packet to the loopback address must be translated to be from
405 * the forwarding address, i.e. 10.0.2.2. */
406 if ( (saddr.sin_addr.s_addr & RT_H2N_U32_C(IN_CLASSA_NET))
407 == RT_H2N_U32_C(INADDR_LOOPBACK & IN_CLASSA_NET))
408 saddr.sin_addr.s_addr = alias_addr.s_addr;
409
410 daddr.sin_addr = so->so_laddr;
411 daddr.sin_port = so->so_lport;
412
413 return udp_output2(pData, so, m, &saddr, &daddr, so->so_iptos);
414}
415
416int
417udp_attach(PNATState pData, struct socket *so, int service_port)
418{
419 struct sockaddr_in *addr;
420 struct sockaddr sa_addr;
421 socklen_t socklen = sizeof(struct sockaddr);
422 int status;
423 int opt = 1;
424
425 if ((so->s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
426 goto error;
427 /*
428 * Here, we bind() the socket. Although not really needed
429 * (sendto() on an unbound socket will bind it), it's done
430 * here so that emulation of ytalk etc. don't have to do it
431 */
432 memset(&sa_addr, 0, sizeof(struct sockaddr));
433 addr = (struct sockaddr_in *)&sa_addr;
434#ifdef RT_OS_DARWIN
435 addr->sin_len = sizeof(struct sockaddr_in);
436#endif
437 addr->sin_family = AF_INET;
438 addr->sin_port = service_port;
439 addr->sin_addr.s_addr = pData->bindIP.s_addr;
440 fd_nonblock(so->s);
441 if (bind(so->s, &sa_addr, sizeof(struct sockaddr_in)) < 0)
442 {
443 int lasterrno = errno;
444 closesocket(so->s);
445 so->s = -1;
446#ifdef RT_OS_WINDOWS
447 WSASetLastError(lasterrno);
448#else
449 errno = lasterrno;
450#endif
451 goto error;
452 }
453 /* success, insert in queue */
454 so->so_expire = curtime + SO_EXPIRE;
455 /* enable broadcast for later use */
456 setsockopt(so->s, SOL_SOCKET, SO_BROADCAST, (const char *)&opt, sizeof(opt));
457 status = getsockname(so->s, &sa_addr, &socklen);
458 Assert(status == 0 && sa_addr.sa_family == AF_INET);
459 so->so_hlport = ((struct sockaddr_in *)&sa_addr)->sin_port;
460 so->so_hladdr.s_addr = ((struct sockaddr_in *)&sa_addr)->sin_addr.s_addr;
461 SOCKET_LOCK_CREATE(so);
462 QSOCKET_LOCK(udb);
463 insque(pData, so, &udb);
464 NSOCK_INC();
465 QSOCKET_UNLOCK(udb);
466 return so->s;
467error:
468 Log2(("NAT: can't create datagramm socket\n"));
469 return -1;
470}
471
472void
473udp_detach(PNATState pData, struct socket *so)
474{
475 if (so != &pData->icmp_socket)
476 {
477 QSOCKET_LOCK(udb);
478 SOCKET_LOCK(so);
479 QSOCKET_UNLOCK(udb);
480 closesocket(so->s);
481 sofree(pData, so);
482 SOCKET_UNLOCK(so);
483 }
484}
485
486struct socket *
487udp_listen(PNATState pData, u_int32_t bind_addr, u_int port, u_int32_t laddr, u_int lport, int flags)
488{
489 struct sockaddr_in addr;
490 struct socket *so;
491 socklen_t addrlen = sizeof(struct sockaddr_in);
492 int opt = 1;
493
494 if ((so = socreate()) == NULL)
495 return NULL;
496
497 so->s = socket(AF_INET, SOCK_DGRAM, 0);
498 if (so->s == -1)
499 {
500 LogRel(("NAT: can't create datagram socket\n"));
501 RTMemFree(so);
502 return NULL;
503 }
504 so->so_expire = curtime + SO_EXPIRE;
505 fd_nonblock(so->s);
506 SOCKET_LOCK_CREATE(so);
507 QSOCKET_LOCK(udb);
508 insque(pData, so, &udb);
509 NSOCK_INC();
510 QSOCKET_UNLOCK(udb);
511
512 memset(&addr, 0, sizeof(addr));
513#ifdef RT_OS_DARWIN
514 addr.sin_len = sizeof(addr);
515#endif
516 addr.sin_family = AF_INET;
517 addr.sin_addr.s_addr = bind_addr;
518 addr.sin_port = port;
519
520 if (bind(so->s,(struct sockaddr *)&addr, addrlen) < 0)
521 {
522 LogRel(("NAT: bind to %R[IP4] has been failed\n", &addr.sin_addr));
523 udp_detach(pData, so);
524 return NULL;
525 }
526 setsockopt(so->s, SOL_SOCKET, SO_REUSEADDR,(char *)&opt, sizeof(int));
527/* setsockopt(so->s, SOL_SOCKET, SO_OOBINLINE,(char *)&opt, sizeof(int)); */
528
529 getsockname(so->s,(struct sockaddr *)&addr,&addrlen);
530 so->so_fport = addr.sin_port;
531 /* The original check was completely broken, as the commented out
532 * if statement was always true (INADDR_ANY=0). */
533 /* if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr) */
534 if (1 == 0) /* always use the else part */
535 so->so_faddr = alias_addr;
536 else
537 so->so_faddr = addr.sin_addr;
538
539 so->so_lport = lport;
540 so->so_laddr.s_addr = laddr;
541 if (flags != SS_FACCEPTONCE)
542 so->so_expire = 0;
543
544 so->so_state = SS_ISFCONNECTED;
545
546 return so;
547}
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