VirtualBox

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

Last change on this file since 22905 was 22843, checked in by vboxsync, 15 years ago

NAT: using host resolver instead of dnsproxy.

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