VirtualBox

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

最後變更 在這個檔案從22206是 22014,由 vboxsync 提交於 15 年 前

NAT: clearing no-service calculation

  • 屬性 svn:eol-style 設為 native
檔案大小: 14.9 KB
 
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
110 if (ip->ip_len != len)
111 {
112 if (len > ip->ip_len)
113 {
114 udpstat.udps_badlen++;
115 goto bad;
116 }
117 m_adj(m, len - ip->ip_len);
118 ip->ip_len = len;
119 }
120
121 /*
122 * Save a copy of the IP header in case we want restore it
123 * for sending an ICMP error message in response.
124 */
125 save_ip = *ip;
126 save_ip.ip_len+= iphlen; /* tcp_input subtracts this */
127
128 /*
129 * Checksum extended UDP header and data.
130 */
131 if (udpcksum && uh->uh_sum)
132 {
133 memset(((struct ipovly *)ip)->ih_x1, 0, 9);
134 ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
135#if 0
136 /* keep uh_sum for ICMP reply */
137 uh->uh_sum = cksum(m, len + sizeof (struct ip));
138 if (uh->uh_sum)
139 {
140
141#endif
142 if(cksum(m, len + sizeof(struct ip)))
143 {
144 udpstat.udps_badsum++;
145 goto bad;
146 }
147 }
148#if 0
149 }
150#endif
151
152 /*
153 * handle DHCP/BOOTP
154 */
155 if (ntohs(uh->uh_dport) == BOOTP_SERVER)
156 {
157 bootp_input(pData, m);
158 goto bad;
159 }
160
161 /*
162 * handle TFTP
163 */
164 if ( ntohs(uh->uh_dport) == TFTP_SERVER
165 && CTL_CHECK(ntohl(ip->ip_dst.s_addr), CTL_TFTP))
166 {
167 tftp_input(pData, m);
168 goto bad;
169 }
170
171 /*
172 * Locate pcb for datagram.
173 */
174 so = udp_last_so;
175 if ( so->so_lport != uh->uh_sport
176 || so->so_laddr.s_addr != ip->ip_src.s_addr)
177 {
178 struct socket *tmp;
179
180 for (tmp = udb.so_next; tmp != &udb; tmp = tmp->so_next)
181 {
182 if ( tmp->so_lport == uh->uh_sport
183 && tmp->so_laddr.s_addr == ip->ip_src.s_addr)
184 {
185 so = tmp;
186 break;
187 }
188 }
189 if (tmp == &udb)
190 so = NULL;
191 else
192 {
193 udpstat.udpps_pcbcachemiss++;
194 udp_last_so = so;
195 }
196 }
197
198 if (so == NULL)
199 {
200 /*
201 * If there's no socket for this packet,
202 * create one
203 */
204 if ((so = socreate()) == NULL)
205 goto bad;
206 if (udp_attach(pData, so, 0) == -1)
207 {
208 DEBUG_MISC((dfd," udp_attach errno = %d-%s\n",
209 errno, strerror(errno)));
210 sofree(pData, so);
211 goto bad;
212 }
213
214 /*
215 * Setup fields
216 */
217 /* udp_last_so = so; */
218 so->so_laddr = ip->ip_src;
219 so->so_lport = uh->uh_sport;
220
221 if ((so->so_iptos = udp_tos(so)) == 0)
222 so->so_iptos = ip->ip_tos;
223
224 /*
225 * XXXXX Here, check if it's in udpexec_list,
226 * and if it is, do the fork_exec() etc.
227 */
228 }
229
230 so->so_faddr = ip->ip_dst; /* XXX */
231 so->so_fport = uh->uh_dport; /* XXX */
232
233 /*
234 * DNS proxy
235 */
236 if ( (ip->ip_dst.s_addr == htonl(ntohl(special_addr.s_addr) | CTL_DNS))
237 && (ntohs(uh->uh_dport) == 53))
238 {
239 dnsproxy_query(pData, so, m, iphlen);
240 goto bad; /* it isn't bad, probably better to add additional label done for boot/tftf :) */
241 }
242
243 iphlen += sizeof(struct udphdr);
244 m->m_len -= iphlen;
245 m->m_data += iphlen;
246
247 /*
248 * Now we sendto() the packet.
249 */
250 if (so->so_emu)
251 udp_emu(pData, so, m);
252
253 ttl = ip->ip_ttl = save_ip.ip_ttl;
254 ret = setsockopt(so->s, IPPROTO_IP, IP_TTL, (const char*)&ttl, sizeof(ttl));
255 if (ret < 0)
256 LogRel(("NAT: Error (%s) occurred while setting TTL(%d) attribute "
257 "of IP packet to socket %R[natsock]\n", strerror(errno), ip->ip_ttl, so));
258
259 if (sosendto(pData, so, m) == -1)
260 {
261 m->m_len += iphlen;
262 m->m_data -= iphlen;
263 *ip = save_ip;
264 DEBUG_MISC((dfd,"NAT: UDP tx errno = %d-%s (on sent to %R[IP4])\n", errno,
265 strerror(errno), &ip->ip_dst));
266 icmp_error(pData, m, ICMP_UNREACH, ICMP_UNREACH_NET, 0, strerror(errno));
267 /* in case we receive ICMP on this socket we'll aware that ICMP has been already sent to host*/
268 so->so_m = NULL;
269 }
270
271 m_free(pData, so->so_m); /* used for ICMP if error on sorecvfrom */
272
273 /* restore the orig mbuf packet */
274 m->m_len += iphlen;
275 m->m_data -= iphlen;
276 *ip = save_ip;
277 so->so_m = m; /* ICMP backup */
278
279 return;
280
281bad:
282 Log2(("NAT: UDP datagram to %R[IP4] with size(%d) claimed as bad\n",
283 &ip->ip_dst, ip->ip_len));
284 m_freem(pData, m);
285 return;
286}
287
288int udp_output2(PNATState pData, struct socket *so, struct mbuf *m,
289 struct sockaddr_in *saddr, struct sockaddr_in *daddr,
290 int iptos)
291{
292 register struct udpiphdr *ui;
293 int error = 0;
294
295 DEBUG_CALL("udp_output");
296 DEBUG_ARG("so = %lx", (long)so);
297 DEBUG_ARG("m = %lx", (long)m);
298 DEBUG_ARG("saddr = %lx", (long)saddr->sin_addr.s_addr);
299 DEBUG_ARG("daddr = %lx", (long)daddr->sin_addr.s_addr);
300
301 /*
302 * Adjust for header
303 */
304 m->m_data -= sizeof(struct udpiphdr);
305 m->m_len += sizeof(struct udpiphdr);
306
307 /*
308 * Fill in mbuf with extended UDP header
309 * and addresses and length put into network format.
310 */
311 ui = mtod(m, struct udpiphdr *);
312 memset(ui->ui_x1, 0, 9);
313 ui->ui_pr = IPPROTO_UDP;
314 ui->ui_len = htons(m->m_len - sizeof(struct ip)); /* + sizeof (struct udphdr)); */
315 /* XXXXX Check for from-one-location sockets, or from-any-location sockets */
316 ui->ui_src = saddr->sin_addr;
317 ui->ui_dst = daddr->sin_addr;
318 ui->ui_sport = saddr->sin_port;
319 ui->ui_dport = daddr->sin_port;
320 ui->ui_ulen = ui->ui_len;
321
322 /*
323 * Stuff checksum and output datagram.
324 */
325 ui->ui_sum = 0;
326 if (udpcksum)
327 {
328 if ((ui->ui_sum = cksum(m, /* sizeof (struct udpiphdr) + */ m->m_len)) == 0)
329 ui->ui_sum = 0xffff;
330 }
331 ((struct ip *)ui)->ip_len = m->m_len;
332 ((struct ip *)ui)->ip_ttl = ip_defttl;
333 ((struct ip *)ui)->ip_tos = iptos;
334
335 udpstat.udps_opackets++;
336
337 error = ip_output(pData, so, m);
338
339 return error;
340}
341
342int udp_output(PNATState pData, struct socket *so, struct mbuf *m,
343 struct sockaddr_in *addr)
344{
345 struct sockaddr_in saddr, daddr;
346
347 saddr = *addr;
348 if ((so->so_faddr.s_addr & htonl(pData->netmask)) == special_addr.s_addr)
349 {
350 saddr.sin_addr.s_addr = so->so_faddr.s_addr;
351 if ((so->so_faddr.s_addr & htonl(~pData->netmask)) == htonl(~pData->netmask))
352 saddr.sin_addr.s_addr = alias_addr.s_addr;
353 }
354
355 /* Any UDP packet to the loopback address must be translated to be from
356 * the forwarding address, i.e. 10.0.2.2. */
357 if ( (saddr.sin_addr.s_addr & htonl(IN_CLASSA_NET))
358 == htonl(INADDR_LOOPBACK & IN_CLASSA_NET))
359 saddr.sin_addr.s_addr = alias_addr.s_addr;
360
361 daddr.sin_addr = so->so_laddr;
362 daddr.sin_port = so->so_lport;
363
364 return udp_output2(pData, so, m, &saddr, &daddr, so->so_iptos);
365}
366
367int
368udp_attach(PNATState pData, struct socket *so, int service_port)
369{
370 struct sockaddr_in *addr;
371 struct sockaddr sa_addr;
372 socklen_t socklen = sizeof(struct sockaddr);
373 int status;
374 int opt = 1;
375
376 if ((so->s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
377 goto error;
378 /*
379 * Here, we bind() the socket. Although not really needed
380 * (sendto() on an unbound socket will bind it), it's done
381 * here so that emulation of ytalk etc. don't have to do it
382 */
383 memset(&sa_addr, 0, sizeof(struct sockaddr));
384 addr = (struct sockaddr_in *)&sa_addr;
385#ifdef RT_OS_DARWIN
386 addr->sin_len = sizeof(struct sockaddr_in);
387#endif
388 addr->sin_family = AF_INET;
389 addr->sin_port = service_port;
390 addr->sin_addr.s_addr = pData->bindIP.s_addr;
391 fd_nonblock(so->s);
392 if (bind(so->s, &sa_addr, sizeof(struct sockaddr_in)) < 0)
393 {
394 int lasterrno = errno;
395 closesocket(so->s);
396 so->s = -1;
397#ifdef RT_OS_WINDOWS
398 WSASetLastError(lasterrno);
399#else
400 errno = lasterrno;
401#endif
402 goto error;
403 }
404 /* success, insert in queue */
405 so->so_expire = curtime + SO_EXPIRE;
406 /* enable broadcast for later use */
407 setsockopt(so->s, SOL_SOCKET, SO_BROADCAST, (const char *)&opt, sizeof(opt));
408 status = getsockname(so->s, &sa_addr, &socklen);
409 Assert(status == 0 && sa_addr.sa_family == AF_INET);
410 so->so_hlport = ((struct sockaddr_in *)&sa_addr)->sin_port;
411 so->so_hladdr.s_addr = ((struct sockaddr_in *)&sa_addr)->sin_addr.s_addr;
412 SOCKET_LOCK_CREATE(so);
413 QSOCKET_LOCK(udb);
414 insque(pData, so, &udb);
415 NSOCK_INC();
416 QSOCKET_UNLOCK(udb);
417 return so->s;
418error:
419 LogRel(("NAT: can't create datagramm socket\n"));
420 return -1;
421}
422
423void
424udp_detach(PNATState pData, struct socket *so)
425{
426 if (so != &pData->icmp_socket)
427 {
428 QSOCKET_LOCK(udb);
429 SOCKET_LOCK(so);
430 QSOCKET_UNLOCK(udb);
431 closesocket(so->s);
432 sofree(pData, so);
433 SOCKET_UNLOCK(so);
434 }
435}
436
437static const struct tos_t udptos[] =
438{
439 { 0, 53, IPTOS_LOWDELAY, 0 }, /* DNS */
440 { 517, 517, IPTOS_LOWDELAY, EMU_TALK }, /* talk */
441 { 518, 518, IPTOS_LOWDELAY, EMU_NTALK }, /* ntalk */
442 { 0, 7648, IPTOS_LOWDELAY, EMU_CUSEEME }, /* Cu-Seeme */
443 { 0, 0, 0, 0 }
444};
445
446u_int8_t
447udp_tos(struct socket *so)
448{
449 int i = 0;
450
451 while(udptos[i].tos)
452 {
453 if ( (udptos[i].fport && ntohs(so->so_fport) == udptos[i].fport)
454 || (udptos[i].lport && ntohs(so->so_lport) == udptos[i].lport))
455 {
456 so->so_emu = udptos[i].emu;
457 return udptos[i].tos;
458 }
459 i++;
460 }
461
462 return 0;
463}
464
465#ifdef EMULATE_TALK
466#include "talkd.h"
467#endif
468
469/*
470 * Here, talk/ytalk/ntalk requests must be emulated
471 */
472void
473udp_emu(PNATState pData, struct socket *so, struct mbuf *m)
474{
475 so->so_emu = 0;
476}
477
478struct socket *
479udp_listen(PNATState pData, u_int32_t bind_addr, u_int port, u_int32_t laddr, u_int lport, int flags)
480{
481 struct sockaddr_in addr;
482 struct socket *so;
483 socklen_t addrlen = sizeof(struct sockaddr_in);
484 int opt = 1;
485
486 if ((so = socreate()) == NULL)
487 return NULL;
488
489 so->s = socket(AF_INET, SOCK_DGRAM, 0);
490 if (so->s == -1)
491 {
492 LogRel(("NAT: can't create datagram socket\n"));
493 RTMemFree(so);
494 return NULL;
495 }
496 so->so_expire = curtime + SO_EXPIRE;
497 fd_nonblock(so->s);
498 SOCKET_LOCK_CREATE(so);
499 QSOCKET_LOCK(udb);
500 insque(pData, so,&udb);
501 NSOCK_INC();
502 QSOCKET_UNLOCK(udb);
503
504 memset(&addr, 0, sizeof(addr));
505#ifdef RT_OS_DARWIN
506 addr.sin_len = sizeof(addr);
507#endif
508 addr.sin_family = AF_INET;
509 addr.sin_addr.s_addr = bind_addr;
510 addr.sin_port = port;
511
512 if (bind(so->s,(struct sockaddr *)&addr, addrlen) < 0)
513 {
514 LogRel(("NAT: bind to %R[IP4] has been failed\n", &addr.sin_addr));
515 udp_detach(pData, so);
516 return NULL;
517 }
518 setsockopt(so->s, SOL_SOCKET, SO_REUSEADDR,(char *)&opt, sizeof(int));
519/* setsockopt(so->s, SOL_SOCKET, SO_OOBINLINE,(char *)&opt, sizeof(int)); */
520
521 getsockname(so->s,(struct sockaddr *)&addr,&addrlen);
522 so->so_fport = addr.sin_port;
523 /* The original check was completely broken, as the commented out
524 * if statement was always true (INADDR_ANY=0). */
525 /* if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr) */
526 if (1 == 0) /* always use the else part */
527 so->so_faddr = alias_addr;
528 else
529 so->so_faddr = addr.sin_addr;
530
531 so->so_lport = lport;
532 so->so_laddr.s_addr = laddr;
533 if (flags != SS_FACCEPTONCE)
534 so->so_expire = 0;
535
536 so->so_state = SS_ISFCONNECTED;
537
538 return so;
539}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette