VirtualBox

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

Last change on this file since 28915 was 28800, checked in by vboxsync, 14 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

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