VirtualBox

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

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

NAT: -Wshadow fixes. Use RT_N2H and RT_H2N instead of ntoh and hton because the latter trigger this warning as well.

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

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