VirtualBox

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

Last change on this file since 22972 was 22972, checked in by vboxsync, 16 years ago

NAT: don't ignore ip's options while checksum

  • Property svn:eol-style set to native
File size: 15.7 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 + iphlen))
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 done;
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 done;
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 done;
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));
303done:
304 /* some services like bootp(built-in), dns(buildt-in) and dhcp don't need sockets
305 * and create new m'buffers to send them to guest, so we'll free their incomming
306 * buffers here.
307 */
308 m_freem(pData, m);
309 return;
310}
311
312int udp_output2(PNATState pData, struct socket *so, struct mbuf *m,
313 struct sockaddr_in *saddr, struct sockaddr_in *daddr,
314 int iptos)
315{
316 register struct udpiphdr *ui;
317 int error = 0;
318
319 DEBUG_CALL("udp_output");
320 DEBUG_ARG("so = %lx", (long)so);
321 DEBUG_ARG("m = %lx", (long)m);
322 DEBUG_ARG("saddr = %lx", (long)saddr->sin_addr.s_addr);
323 DEBUG_ARG("daddr = %lx", (long)daddr->sin_addr.s_addr);
324
325 /*
326 * Adjust for header
327 */
328 m->m_data -= sizeof(struct udpiphdr);
329 m->m_len += sizeof(struct udpiphdr);
330
331 /*
332 * Fill in mbuf with extended UDP header
333 * and addresses and length put into network format.
334 */
335 ui = mtod(m, struct udpiphdr *);
336 memset(ui->ui_x1, 0, 9);
337 ui->ui_pr = IPPROTO_UDP;
338 ui->ui_len = htons(m->m_len - sizeof(struct ip)); /* + sizeof (struct udphdr)); */
339 /* XXXXX Check for from-one-location sockets, or from-any-location sockets */
340 ui->ui_src = saddr->sin_addr;
341 ui->ui_dst = daddr->sin_addr;
342 ui->ui_sport = saddr->sin_port;
343 ui->ui_dport = daddr->sin_port;
344 ui->ui_ulen = ui->ui_len;
345
346 /*
347 * Stuff checksum and output datagram.
348 */
349 ui->ui_sum = 0;
350 if (udpcksum)
351 {
352 if ((ui->ui_sum = cksum(m, /* sizeof (struct udpiphdr) + */ m->m_len)) == 0)
353 ui->ui_sum = 0xffff;
354 }
355 ((struct ip *)ui)->ip_len = m->m_len;
356 ((struct ip *)ui)->ip_ttl = ip_defttl;
357 ((struct ip *)ui)->ip_tos = iptos;
358
359 udpstat.udps_opackets++;
360
361 error = ip_output(pData, so, m);
362
363 return error;
364}
365
366int udp_output(PNATState pData, struct socket *so, struct mbuf *m,
367 struct sockaddr_in *addr)
368{
369 struct sockaddr_in saddr, daddr;
370
371 saddr = *addr;
372 if ((so->so_faddr.s_addr & htonl(pData->netmask)) == special_addr.s_addr)
373 {
374 saddr.sin_addr.s_addr = so->so_faddr.s_addr;
375 if ((so->so_faddr.s_addr & htonl(~pData->netmask)) == htonl(~pData->netmask))
376 saddr.sin_addr.s_addr = alias_addr.s_addr;
377 }
378
379 /* Any UDP packet to the loopback address must be translated to be from
380 * the forwarding address, i.e. 10.0.2.2. */
381 if ( (saddr.sin_addr.s_addr & htonl(IN_CLASSA_NET))
382 == htonl(INADDR_LOOPBACK & IN_CLASSA_NET))
383 saddr.sin_addr.s_addr = alias_addr.s_addr;
384
385 daddr.sin_addr = so->so_laddr;
386 daddr.sin_port = so->so_lport;
387
388 return udp_output2(pData, so, m, &saddr, &daddr, so->so_iptos);
389}
390
391int
392udp_attach(PNATState pData, struct socket *so, int service_port)
393{
394 struct sockaddr_in *addr;
395 struct sockaddr sa_addr;
396 socklen_t socklen = sizeof(struct sockaddr);
397 int status;
398 int opt = 1;
399
400 if ((so->s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
401 goto error;
402 /*
403 * Here, we bind() the socket. Although not really needed
404 * (sendto() on an unbound socket will bind it), it's done
405 * here so that emulation of ytalk etc. don't have to do it
406 */
407 memset(&sa_addr, 0, sizeof(struct sockaddr));
408 addr = (struct sockaddr_in *)&sa_addr;
409#ifdef RT_OS_DARWIN
410 addr->sin_len = sizeof(struct sockaddr_in);
411#endif
412 addr->sin_family = AF_INET;
413 addr->sin_port = service_port;
414 addr->sin_addr.s_addr = pData->bindIP.s_addr;
415 fd_nonblock(so->s);
416 if (bind(so->s, &sa_addr, sizeof(struct sockaddr_in)) < 0)
417 {
418 int lasterrno = errno;
419 closesocket(so->s);
420 so->s = -1;
421#ifdef RT_OS_WINDOWS
422 WSASetLastError(lasterrno);
423#else
424 errno = lasterrno;
425#endif
426 goto error;
427 }
428 /* success, insert in queue */
429 so->so_expire = curtime + SO_EXPIRE;
430 /* enable broadcast for later use */
431 setsockopt(so->s, SOL_SOCKET, SO_BROADCAST, (const char *)&opt, sizeof(opt));
432 status = getsockname(so->s, &sa_addr, &socklen);
433 Assert(status == 0 && sa_addr.sa_family == AF_INET);
434 so->so_hlport = ((struct sockaddr_in *)&sa_addr)->sin_port;
435 so->so_hladdr.s_addr = ((struct sockaddr_in *)&sa_addr)->sin_addr.s_addr;
436 SOCKET_LOCK_CREATE(so);
437 QSOCKET_LOCK(udb);
438 insque(pData, so, &udb);
439 NSOCK_INC();
440 QSOCKET_UNLOCK(udb);
441 return so->s;
442error:
443 LogRel(("NAT: can't create datagramm socket\n"));
444 return -1;
445}
446
447void
448udp_detach(PNATState pData, struct socket *so)
449{
450 if (so != &pData->icmp_socket)
451 {
452 QSOCKET_LOCK(udb);
453 SOCKET_LOCK(so);
454 QSOCKET_UNLOCK(udb);
455 closesocket(so->s);
456 sofree(pData, so);
457 SOCKET_UNLOCK(so);
458 }
459}
460
461static const struct tos_t udptos[] =
462{
463 { 0, 53, IPTOS_LOWDELAY, 0 }, /* DNS */
464 { 517, 517, IPTOS_LOWDELAY, EMU_TALK }, /* talk */
465 { 518, 518, IPTOS_LOWDELAY, EMU_NTALK }, /* ntalk */
466 { 0, 7648, IPTOS_LOWDELAY, EMU_CUSEEME }, /* Cu-Seeme */
467 { 0, 0, 0, 0 }
468};
469
470u_int8_t
471udp_tos(struct socket *so)
472{
473 int i = 0;
474
475 while(udptos[i].tos)
476 {
477 if ( (udptos[i].fport && ntohs(so->so_fport) == udptos[i].fport)
478 || (udptos[i].lport && ntohs(so->so_lport) == udptos[i].lport))
479 {
480 so->so_emu = udptos[i].emu;
481 return udptos[i].tos;
482 }
483 i++;
484 }
485
486 return 0;
487}
488
489#ifdef EMULATE_TALK
490#include "talkd.h"
491#endif
492
493/*
494 * Here, talk/ytalk/ntalk requests must be emulated
495 */
496void
497udp_emu(PNATState pData, struct socket *so, struct mbuf *m)
498{
499 so->so_emu = 0;
500}
501
502struct socket *
503udp_listen(PNATState pData, u_int32_t bind_addr, u_int port, u_int32_t laddr, u_int lport, int flags)
504{
505 struct sockaddr_in addr;
506 struct socket *so;
507 socklen_t addrlen = sizeof(struct sockaddr_in);
508 int opt = 1;
509
510 if ((so = socreate()) == NULL)
511 return NULL;
512
513 so->s = socket(AF_INET, SOCK_DGRAM, 0);
514 if (so->s == -1)
515 {
516 LogRel(("NAT: can't create datagram socket\n"));
517 RTMemFree(so);
518 return NULL;
519 }
520 so->so_expire = curtime + SO_EXPIRE;
521 fd_nonblock(so->s);
522 SOCKET_LOCK_CREATE(so);
523 QSOCKET_LOCK(udb);
524 insque(pData, so,&udb);
525 NSOCK_INC();
526 QSOCKET_UNLOCK(udb);
527
528 memset(&addr, 0, sizeof(addr));
529#ifdef RT_OS_DARWIN
530 addr.sin_len = sizeof(addr);
531#endif
532 addr.sin_family = AF_INET;
533 addr.sin_addr.s_addr = bind_addr;
534 addr.sin_port = port;
535
536 if (bind(so->s,(struct sockaddr *)&addr, addrlen) < 0)
537 {
538 LogRel(("NAT: bind to %R[IP4] has been failed\n", &addr.sin_addr));
539 udp_detach(pData, so);
540 return NULL;
541 }
542 setsockopt(so->s, SOL_SOCKET, SO_REUSEADDR,(char *)&opt, sizeof(int));
543/* setsockopt(so->s, SOL_SOCKET, SO_OOBINLINE,(char *)&opt, sizeof(int)); */
544
545 getsockname(so->s,(struct sockaddr *)&addr,&addrlen);
546 so->so_fport = addr.sin_port;
547 /* The original check was completely broken, as the commented out
548 * if statement was always true (INADDR_ANY=0). */
549 /* if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr) */
550 if (1 == 0) /* always use the else part */
551 so->so_faddr = alias_addr;
552 else
553 so->so_faddr = addr.sin_addr;
554
555 so->so_lport = lport;
556 so->so_laddr.s_addr = laddr;
557 if (flags != SS_FACCEPTONCE)
558 so->so_expire = 0;
559
560 so->so_state = SS_ISFCONNECTED;
561
562 return so;
563}
Note: See TracBrowser for help on using the repository browser.

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