VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/slirp/tcp_subr.c@ 20053

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

NAT: LibAlias enabling + tcp_emu replaced with ftp_module

  • Property svn:eol-style set to native
File size: 32.6 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 * @(#)tcp_subr.c 8.1 (Berkeley) 6/10/93
34 * tcp_subr.c,v 1.5 1994/10/08 22:39:58 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#define WANT_SYS_IOCTL_H
46#include <slirp.h>
47
48
49/*
50 * Tcp initialization
51 */
52void
53tcp_init(PNATState pData)
54{
55 tcp_iss = 1; /* wrong */
56 tcb.so_next = tcb.so_prev = &tcb;
57 tcp_last_so = &tcb;
58 tcp_reass_maxqlen = 48;
59 tcp_reass_maxseg = 256;
60}
61
62/*
63 * Create template to be used to send tcp packets on a connection.
64 * Call after host entry created, fills
65 * in a skeletal tcp/ip header, minimizing the amount of work
66 * necessary when the connection is used.
67 */
68/* struct tcpiphdr * */
69void
70tcp_template(struct tcpcb *tp)
71{
72 struct socket *so = tp->t_socket;
73 register struct tcpiphdr *n = &tp->t_template;
74
75 memset(n->ti_x1, 0, 9);
76 n->ti_pr = IPPROTO_TCP;
77 n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip));
78 n->ti_src = so->so_faddr;
79 n->ti_dst = so->so_laddr;
80 n->ti_sport = so->so_fport;
81 n->ti_dport = so->so_lport;
82
83 n->ti_seq = 0;
84 n->ti_ack = 0;
85 n->ti_x2 = 0;
86 n->ti_off = 5;
87 n->ti_flags = 0;
88 n->ti_win = 0;
89 n->ti_sum = 0;
90 n->ti_urp = 0;
91}
92
93/*
94 * Send a single message to the TCP at address specified by
95 * the given TCP/IP header. If m == 0, then we make a copy
96 * of the tcpiphdr at ti and send directly to the addressed host.
97 * This is used to force keep alive messages out using the TCP
98 * template for a connection tp->t_template. If flags are given
99 * then we send a message back to the TCP which originated the
100 * segment ti, and discard the mbuf containing it and any other
101 * attached mbufs.
102 *
103 * In any case the ack and sequence number of the transmitted
104 * segment are as specified by the parameters.
105 */
106void
107tcp_respond(PNATState pData, struct tcpcb *tp, struct tcpiphdr *ti, struct mbuf *m, tcp_seq ack, tcp_seq seq, int flags)
108{
109 register int tlen;
110 int win = 0;
111
112 DEBUG_CALL("tcp_respond");
113 DEBUG_ARG("tp = %lx", (long)tp);
114 DEBUG_ARG("ti = %lx", (long)ti);
115 DEBUG_ARG("m = %lx", (long)m);
116 DEBUG_ARG("ack = %u", ack);
117 DEBUG_ARG("seq = %u", seq);
118 DEBUG_ARG("flags = %x", flags);
119
120 if (tp)
121 win = sbspace(&tp->t_socket->so_rcv);
122 if (m == 0)
123 {
124 if ((m = m_get(pData)) == NULL)
125 return;
126#ifdef VBOX_WITH_NAT_SERVICE
127 {
128 struct ethhdr *eh0, *eh;
129 Assert(tp->t_socket->so_m);
130 eh0 = (struct ethhdr *)tp->t_socket->so_m->m_dat;
131 eh = (struct ethhdr *)m->m_dat;
132 memcpy(eh->h_source, eh0->h_source, ETH_ALEN);
133 }
134#endif
135#ifdef TCP_COMPAT_42
136 tlen = 1;
137#else
138 tlen = 0;
139#endif
140 m->m_data += if_maxlinkhdr;
141 *mtod(m, struct tcpiphdr *) = *ti;
142 ti = mtod(m, struct tcpiphdr *);
143 flags = TH_ACK;
144 }
145 else
146 {
147 /*
148 * ti points into m so the next line is just making
149 * the mbuf point to ti
150 */
151 m->m_data = (caddr_t)ti;
152
153 m->m_len = sizeof (struct tcpiphdr);
154 tlen = 0;
155#define xchg(a,b,type) { type t; t = a; a = b; b = t; }
156 xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_int32_t);
157 xchg(ti->ti_dport, ti->ti_sport, u_int16_t);
158#undef xchg
159 }
160 ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen));
161 tlen += sizeof (struct tcpiphdr);
162 m->m_len = tlen;
163
164 memset(ti->ti_x1, 0, 9);
165 ti->ti_seq = htonl(seq);
166 ti->ti_ack = htonl(ack);
167 ti->ti_x2 = 0;
168 ti->ti_off = sizeof (struct tcphdr) >> 2;
169 ti->ti_flags = flags;
170 if (tp)
171 ti->ti_win = htons((u_int16_t) (win >> tp->rcv_scale));
172 else
173 ti->ti_win = htons((u_int16_t)win);
174 ti->ti_urp = 0;
175 ti->ti_sum = 0;
176 ti->ti_sum = cksum(m, tlen);
177 ((struct ip *)ti)->ip_len = tlen;
178
179 if(flags & TH_RST)
180 ((struct ip *)ti)->ip_ttl = MAXTTL;
181 else
182 ((struct ip *)ti)->ip_ttl = ip_defttl;
183
184 (void) ip_output(pData, (struct socket *)0, m);
185}
186
187/*
188 * Create a new TCP control block, making an
189 * empty reassembly queue and hooking it to the argument
190 * protocol control block.
191 */
192struct tcpcb *
193tcp_newtcpcb(PNATState pData, struct socket *so)
194{
195 register struct tcpcb *tp;
196
197 tp = (struct tcpcb *)RTMemAllocZ(sizeof(*tp));
198 if (tp == NULL)
199 return ((struct tcpcb *)0);
200
201 tp->t_maxseg = tcp_mssdflt;
202
203 tp->t_flags = tcp_do_rfc1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0;
204 tp->t_socket = so;
205
206 /*
207 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
208 * rtt estimate. Set rttvar so that srtt + 2 * rttvar gives
209 * reasonable initial retransmit time.
210 */
211 tp->t_srtt = TCPTV_SRTTBASE;
212 tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << 2;
213 tp->t_rttmin = TCPTV_MIN;
214
215 TCPT_RANGESET(tp->t_rxtcur,
216 ((TCPTV_SRTTBASE >> 2) + (TCPTV_SRTTDFLT << 2)) >> 1,
217 TCPTV_MIN, TCPTV_REXMTMAX);
218
219 tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
220 tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
221 tp->t_state = TCPS_CLOSED;
222
223 so->so_tcpcb = tp;
224
225 return (tp);
226}
227
228/*
229 * Drop a TCP connection, reporting
230 * the specified error. If connection is synchronized,
231 * then send a RST to peer.
232 */
233struct tcpcb *tcp_drop(PNATState pData, struct tcpcb *tp, int err)
234{
235/* tcp_drop(tp, errno)
236 register struct tcpcb *tp;
237 int errno;
238{
239*/
240 DEBUG_CALL("tcp_drop");
241 DEBUG_ARG("tp = %lx", (long)tp);
242 DEBUG_ARG("errno = %d", errno);
243
244 if (TCPS_HAVERCVDSYN(tp->t_state))
245 {
246 tp->t_state = TCPS_CLOSED;
247 (void) tcp_output(pData, tp);
248 tcpstat.tcps_drops++;
249 }
250 else
251 tcpstat.tcps_conndrops++;
252#if 0
253 if (errno == ETIMEDOUT && tp->t_softerror)
254 errno = tp->t_softerror;
255
256 so->so_error = errno;
257#endif
258 return (tcp_close(pData, tp));
259}
260
261/*
262 * Close a TCP control block:
263 * discard all space held by the tcp
264 * discard internet protocol block
265 * wake up any sleepers
266 */
267struct tcpcb *
268tcp_close(PNATState pData, register struct tcpcb *tp)
269{
270 struct socket *so = tp->t_socket;
271 struct socket *so_next, *so_prev;
272
273 struct tseg_qent *te = NULL;
274 DEBUG_CALL("tcp_close");
275 DEBUG_ARG("tp = %lx", (long )tp);
276 so_next = so_prev = NULL;
277 /*XXX: freeing the reassembly queue */
278 while (!LIST_EMPTY(&tp->t_segq))
279 {
280 te = LIST_FIRST(&tp->t_segq);
281 LIST_REMOVE(te, tqe_q);
282 m_freem(pData, te->tqe_m);
283 RTMemFree(te);
284 tcp_reass_qsize--;
285 }
286 RTMemFree(tp);
287 so->so_tcpcb = 0;
288 soisfdisconnected(so);
289 /* clobber input socket cache if we're closing the cached connection */
290 if (so == tcp_last_so)
291 tcp_last_so = &tcb;
292 closesocket(so->s);
293 sbfree(&so->so_rcv);
294 sbfree(&so->so_snd);
295 sofree(pData, so);
296 SOCKET_UNLOCK(so);
297 tcpstat.tcps_closed++;
298 return ((struct tcpcb *)0);
299}
300
301void
302tcp_drain()
303{
304 /* XXX */
305}
306
307/*
308 * When a source quench is received, close congestion window
309 * to one segment. We will gradually open it again as we proceed.
310 */
311
312#if 0
313
314void
315tcp_quench(i, int errno)
316{
317 struct tcpcb *tp = intotcpcb(inp);
318
319 if (tp)
320 tp->snd_cwnd = tp->t_maxseg;
321}
322
323#endif
324
325/*
326 * TCP protocol interface to socket abstraction.
327 */
328
329/*
330 * User issued close, and wish to trail through shutdown states:
331 * if never received SYN, just forget it. If got a SYN from peer,
332 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
333 * If already got a FIN from peer, then almost done; go to LAST_ACK
334 * state. In all other cases, have already sent FIN to peer (e.g.
335 * after PRU_SHUTDOWN), and just have to play tedious game waiting
336 * for peer to send FIN or not respond to keep-alives, etc.
337 * We can let the user exit from the close as soon as the FIN is acked.
338 */
339void
340tcp_sockclosed(PNATState pData, struct tcpcb *tp)
341{
342 DEBUG_CALL("tcp_sockclosed");
343 DEBUG_ARG("tp = %lx", (long)tp);
344
345 switch (tp->t_state)
346 {
347 case TCPS_CLOSED:
348 case TCPS_LISTEN:
349 case TCPS_SYN_SENT:
350 tp->t_state = TCPS_CLOSED;
351 tp = tcp_close(pData, tp);
352 break;
353
354 case TCPS_SYN_RECEIVED:
355 case TCPS_ESTABLISHED:
356 tp->t_state = TCPS_FIN_WAIT_1;
357 break;
358
359 case TCPS_CLOSE_WAIT:
360 tp->t_state = TCPS_LAST_ACK;
361 break;
362 }
363/* soisfdisconnecting(tp->t_socket); */
364 if ( tp
365 && tp->t_state >= TCPS_FIN_WAIT_2)
366 soisfdisconnected(tp->t_socket);
367 if (tp)
368 tcp_output(pData, tp);
369}
370
371/*
372 * Connect to a host on the Internet
373 * Called by tcp_input
374 * Only do a connect, the tcp fields will be set in tcp_input
375 * return 0 if there's a result of the connect,
376 * else return -1 means we're still connecting
377 * The return value is almost always -1 since the socket is
378 * nonblocking. Connect returns after the SYN is sent, and does
379 * not wait for ACK+SYN.
380 */
381int tcp_fconnect(PNATState pData, struct socket *so)
382{
383 int ret = 0;
384
385 DEBUG_CALL("tcp_fconnect");
386 DEBUG_ARG("so = %lx", (long )so);
387
388 if ((ret = so->s = socket(AF_INET, SOCK_STREAM, 0)) >= 0)
389 {
390 int opt, s = so->s;
391 struct sockaddr_in addr;
392
393 fd_nonblock(s);
394 opt = 1;
395 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt));
396 opt = 1;
397 setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (char *)&opt, sizeof(opt));
398
399 addr.sin_family = AF_INET;
400 if ((so->so_faddr.s_addr & htonl(pData->netmask)) == special_addr.s_addr)
401 {
402 /* It's an alias */
403 switch(ntohl(so->so_faddr.s_addr) & ~pData->netmask)
404 {
405 case CTL_DNS:
406#ifndef VBOX_WITH_MULTI_DNS
407 if (!get_dns_addr(pData, &dns_addr))
408 addr.sin_addr = dns_addr;
409 else
410 addr.sin_addr = loopback_addr;
411 break;
412#endif
413 case CTL_ALIAS:
414 default:
415 addr.sin_addr = loopback_addr;
416 break;
417 }
418 }
419 else
420 addr.sin_addr = so->so_faddr;
421 addr.sin_port = so->so_fport;
422
423 DEBUG_MISC((dfd, " connect()ing, addr.sin_port=%d, "
424 "addr.sin_addr.s_addr=%.16s\n",
425 ntohs(addr.sin_port), inet_ntoa(addr.sin_addr)));
426 /* We don't care what port we get */
427 ret = connect(s,(struct sockaddr *)&addr,sizeof (addr));
428
429 /*
430 * If it's not in progress, it failed, so we just return 0,
431 * without clearing SS_NOFDREF
432 */
433 soisfconnecting(so);
434 }
435
436 return(ret);
437}
438
439/*
440 * Accept the socket and connect to the local-host
441 *
442 * We have a problem. The correct thing to do would be
443 * to first connect to the local-host, and only if the
444 * connection is accepted, then do an accept() here.
445 * But, a) we need to know who's trying to connect
446 * to the socket to be able to SYN the local-host, and
447 * b) we are already connected to the foreign host by
448 * the time it gets to accept(), so... We simply accept
449 * here and SYN the local-host.
450 */
451void
452tcp_connect(PNATState pData, struct socket *inso)
453{
454 struct socket *so;
455 struct sockaddr_in addr;
456 socklen_t addrlen = sizeof(struct sockaddr_in);
457 struct tcpcb *tp;
458 int s, opt, status;
459 socklen_t optlen;
460 static int cVerbose = 1;
461
462 DEBUG_CALL("tcp_connect");
463 DEBUG_ARG("inso = %lx", (long)inso);
464
465 /*
466 * If it's an SS_ACCEPTONCE socket, no need to socreate()
467 * another socket, just use the accept() socket.
468 */
469 if (inso->so_state & SS_FACCEPTONCE)
470 {
471 /* FACCEPTONCE already have a tcpcb */
472 so = inso;
473 }
474 else
475 {
476 if ((so = socreate()) == NULL)
477 {
478 /* If it failed, get rid of the pending connection */
479 closesocket(accept(inso->s,(struct sockaddr *)&addr,&addrlen));
480 return;
481 }
482 if (tcp_attach(pData, so) < 0)
483 {
484 RTMemFree(so); /* NOT sofree */
485 return;
486 }
487 so->so_laddr = inso->so_laddr;
488 so->so_lport = inso->so_lport;
489 }
490
491 (void) tcp_mss(pData, sototcpcb(so), 0);
492
493 fd_nonblock(inso->s);
494 if ((s = accept(inso->s,(struct sockaddr *)&addr,&addrlen)) < 0)
495 {
496 tcp_close(pData, sototcpcb(so)); /* This will sofree() as well */
497 return;
498 }
499 fd_nonblock(s);
500 opt = 1;
501 setsockopt(s, SOL_SOCKET, SO_REUSEADDR,(char *)&opt, sizeof(int));
502 opt = 1;
503 setsockopt(s, SOL_SOCKET, SO_OOBINLINE,(char *)&opt, sizeof(int));
504 opt = 1;
505 setsockopt(s, IPPROTO_TCP, TCP_NODELAY,(char *)&opt, sizeof(int));
506
507 optlen = sizeof(int);
508 status = getsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&opt, &optlen);
509 if (status < 0)
510 {
511 LogRel(("NAT: Error(%d) while getting RCV capacity\n", errno));
512 goto no_sockopt;
513 }
514 if (cVerbose > 0)
515 LogRel(("NAT: old socket rcv size: %dKB\n", opt / 1024));
516 opt *= 4;
517 status = setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&opt, sizeof(int));
518 if (status < 0)
519 {
520 LogRel(("NAT: Error(%d) while setting RCV capacity to (%d)\n", errno, opt));
521 goto no_sockopt;
522 }
523 optlen = sizeof(int);
524 status = getsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&opt, &optlen);
525 if (status < 0)
526 {
527 LogRel(("NAT: Error(%d) while getting SND capacity\n", errno));
528 goto no_sockopt;
529 }
530 if (cVerbose > 0)
531 LogRel(("NAT: old socket snd size: %dKB\n", opt / 1024));
532 opt *= 4;
533 status = setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&opt, sizeof(int));
534 if (status < 0)
535 {
536 LogRel(("NAT: Error(%d) while setting SND capacity to (%d)\n", errno, opt));
537 goto no_sockopt;
538 }
539 if (cVerbose > 0)
540 cVerbose--;
541
542 no_sockopt:
543 so->so_fport = addr.sin_port;
544 so->so_faddr = addr.sin_addr;
545 /* Translate connections from localhost to the real hostname */
546 if (so->so_faddr.s_addr == 0 || so->so_faddr.s_addr == loopback_addr.s_addr)
547 so->so_faddr = alias_addr;
548
549 /* Close the accept() socket, set right state */
550 if (inso->so_state & SS_FACCEPTONCE)
551 {
552 closesocket(so->s); /* If we only accept once, close the accept() socket */
553 so->so_state = SS_NOFDREF; /* Don't select it yet, even though we have an FD */
554 /* if it's not FACCEPTONCE, it's already NOFDREF */
555 }
556 so->s = s;
557
558 so->so_iptos = tcp_tos(so);
559 tp = sototcpcb(so);
560
561 tcp_template(tp);
562
563 /* Compute window scaling to request. */
564/* while (tp->request_r_scale < TCP_MAX_WINSHIFT
565 * && (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
566 * tp->request_r_scale++;
567 */
568
569/* soisconnecting(so); */ /* NOFDREF used instead */
570 tcpstat.tcps_connattempt++;
571
572 tp->t_state = TCPS_SYN_SENT;
573 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
574 tp->iss = tcp_iss;
575 tcp_iss += TCP_ISSINCR/2;
576 tcp_sendseqinit(tp);
577 tcp_output(pData, tp);
578}
579
580/*
581 * Attach a TCPCB to a socket.
582 */
583int
584tcp_attach(PNATState pData, struct socket *so)
585{
586 if ((so->so_tcpcb = tcp_newtcpcb(pData, so)) == NULL)
587 return -1;
588
589 SOCKET_LOCK_CREATE(so);
590 QSOCKET_LOCK(tcb);
591 insque(pData, so, &tcb);
592 NSOCK_INC();
593 QSOCKET_UNLOCK(tcb);
594 return 0;
595}
596
597/*
598 * Set the socket's type of service field
599 */
600static const struct tos_t tcptos[] =
601{
602 {0, 20, IPTOS_THROUGHPUT, 0}, /* ftp data */
603 {21, 21, IPTOS_LOWDELAY, EMU_FTP}, /* ftp control */
604 {0, 23, IPTOS_LOWDELAY, 0}, /* telnet */
605 {0, 80, IPTOS_THROUGHPUT, 0}, /* WWW */
606 {0, 513, IPTOS_LOWDELAY, EMU_RLOGIN|EMU_NOCONNECT}, /* rlogin */
607 {0, 514, IPTOS_LOWDELAY, EMU_RSH|EMU_NOCONNECT}, /* shell */
608 {0, 544, IPTOS_LOWDELAY, EMU_KSH}, /* kshell */
609 {0, 543, IPTOS_LOWDELAY, 0}, /* klogin */
610 {0, 6667, IPTOS_THROUGHPUT, EMU_IRC}, /* IRC */
611 {0, 6668, IPTOS_THROUGHPUT, EMU_IRC}, /* IRC undernet */
612 {0, 7070, IPTOS_LOWDELAY, EMU_REALAUDIO }, /* RealAudio control */
613 {0, 113, IPTOS_LOWDELAY, EMU_IDENT }, /* identd protocol */
614 {0, 0, 0, 0}
615};
616
617/*
618 * Return TOS according to the above table
619 */
620u_int8_t
621tcp_tos(struct socket *so)
622{
623 int i = 0;
624
625 while(tcptos[i].tos)
626 {
627 if ( (tcptos[i].fport && (ntohs(so->so_fport) == tcptos[i].fport))
628 || (tcptos[i].lport && (ntohs(so->so_lport) == tcptos[i].lport)))
629 {
630 so->so_emu = tcptos[i].emu;
631 return tcptos[i].tos;
632 }
633 i++;
634 }
635
636 return 0;
637}
638
639/*
640 * Emulate programs that try and connect to us. This includes ftp (the data
641 * connection is initiated by the server) and IRC (DCC CHAT and DCC SEND)
642 * for now
643 *
644 * NOTE: It's possible to crash SLiRP by sending it unstandard strings to
645 * emulate... if this is a problem, more checks are needed here.
646 *
647 * XXX Assumes the whole command cames in one packet
648 *
649 * XXX Some ftp clients will have their TOS set to LOWDELAY and so Nagel will
650 * kick in. Because of this, we'll get the first letter, followed by the
651 * rest, so we simply scan for ORT instead of PORT... DCC doesn't have this
652 * problem because there's other stuff in the packet before the DCC command.
653 *
654 * Return 1 if the mbuf m is still valid and should be sbappend()ed
655 *
656 * NOTE: if you return 0 you MUST m_free() the mbuf!
657 */
658int
659tcp_emu(PNATState pData, struct socket *so, struct mbuf *m)
660{
661#ifndef VBOX_WITH_SLIRP_ALIAS
662 u_int n1, n2, n3, n4, n5, n6;
663 char buff[256];
664 u_int32_t laddr;
665 u_int lport;
666 char *bptr;
667
668 DEBUG_CALL("tcp_emu");
669 DEBUG_ARG("so = %lx", (long)so);
670 DEBUG_ARG("m = %lx", (long)m);
671
672 switch(so->so_emu)
673 {
674 int x, i;
675
676 case EMU_IDENT:
677 /*
678 * Identification protocol as per rfc-1413
679 */
680 {
681 struct socket *tmpso;
682 struct sockaddr_in addr;
683 socklen_t addrlen = sizeof(struct sockaddr_in);
684 struct sbuf *so_rcv = &so->so_rcv;
685
686 memcpy(so_rcv->sb_wptr, m->m_data, m->m_len);
687 so_rcv->sb_wptr += m->m_len;
688 so_rcv->sb_rptr += m->m_len;
689 m->m_data[m->m_len] = 0; /* NULL terminate */
690 if (strchr(m->m_data, '\r') || strchr(m->m_data, '\n'))
691 {
692 if (sscanf(so_rcv->sb_data, "%u%*[ ,]%u", &n1, &n2) == 2)
693 {
694 HTONS(n1);
695 HTONS(n2);
696 /* n2 is the one on our host */
697 for (tmpso = tcb.so_next; tmpso != &tcb; tmpso = tmpso->so_next)
698 {
699 if ( tmpso->so_laddr.s_addr == so->so_laddr.s_addr
700 && tmpso->so_lport == n2
701 && tmpso->so_faddr.s_addr == so->so_faddr.s_addr
702 && tmpso->so_fport == n1)
703 {
704 if (getsockname(tmpso->s,
705 (struct sockaddr *)&addr, &addrlen) == 0)
706 n2 = ntohs(addr.sin_port);
707 break;
708 }
709 }
710 }
711 so_rcv->sb_cc = sprintf(so_rcv->sb_data, "%d,%d\r\n", n1, n2);
712 so_rcv->sb_rptr = so_rcv->sb_data;
713 so_rcv->sb_wptr = so_rcv->sb_data + so_rcv->sb_cc;
714 }
715 m_free(pData, m);
716 return 0;
717 }
718
719 case EMU_FTP:
720 *(m->m_data+m->m_len) = 0; /* NULL terminate for strstr */
721 if ((bptr = (char *)strstr(m->m_data, "ORT")) != NULL)
722 {
723 /*
724 * Need to emulate the PORT command
725 */
726 struct sockaddr_in addr;
727 socklen_t addrlen = sizeof addr;
728
729 if (getsockname(so->s, (struct sockaddr *)&addr, &addrlen))
730 return 1;
731
732 x = sscanf(bptr, "ORT %u,%u,%u,%u,%u,%u\r\n%256[^\177]",
733 &n1, &n2, &n3, &n4, &n5, &n6, buff);
734 if (x < 6)
735 return 1;
736
737 laddr = htonl((n1 << 24) | (n2 << 16) | (n3 << 8) | (n4));
738 lport = htons((n5 << 8) | (n6));
739
740 if ((so = solisten(pData, 0, laddr, lport, SS_FACCEPTONCE)) == NULL)
741 return 1;
742
743 n6 = ntohs(so->so_fport);
744
745 n5 = (n6 >> 8) & 0xff;
746 n6 &= 0xff;
747
748 laddr = ntohl(addr.sin_addr.s_addr);
749
750 n1 = ((laddr >> 24) & 0xff);
751 n2 = ((laddr >> 16) & 0xff);
752 n3 = ((laddr >> 8) & 0xff);
753 n4 = ( laddr & 0xff);
754
755 m->m_len = bptr - m->m_data; /* Adjust length */
756 m->m_len += sprintf(bptr, "ORT %d,%d,%d,%d,%d,%d\r\n%s",
757 n1, n2, n3, n4, n5, n6, x==7?buff:"");
758 return 1;
759 }
760 else if ((bptr = (char *)strstr(m->m_data, "27 Entering")) != NULL)
761 {
762 /*
763 * Need to emulate the PASV response
764 */
765 struct sockaddr_in addr;
766 socklen_t addrlen = sizeof addr;
767
768 if (getsockname(so->s, (struct sockaddr *)&addr, &addrlen))
769 return 1;
770
771 x = sscanf(bptr, "27 Entering Passive Mode (%u,%u,%u,%u,%u,%u)\r\n%256[^\177]",
772 &n1, &n2, &n3, &n4, &n5, &n6, buff);
773 if (x < 6)
774 return 1;
775
776 laddr = htonl((n1 << 24) | (n2 << 16) | (n3 << 8) | (n4));
777 lport = htons((n5 << 8) | (n6));
778
779 if ((so = solisten(pData, 0, laddr, lport, SS_FACCEPTONCE)) == NULL)
780 return 1;
781
782 n6 = ntohs(so->so_fport);
783
784 n5 = (n6 >> 8) & 0xff;
785 n6 &= 0xff;
786
787 laddr = ntohl(addr.sin_addr.s_addr);
788
789 n1 = ((laddr >> 24) & 0xff);
790 n2 = ((laddr >> 16) & 0xff);
791 n3 = ((laddr >> 8) & 0xff);
792 n4 = (laddr & 0xff);
793
794 m->m_len = bptr - m->m_data; /* Adjust length */
795 m->m_len += sprintf(bptr, "27 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\r\n%s",
796 n1, n2, n3, n4, n5, n6, x==7?buff:"");
797
798 return 1;
799 }
800 return 1;
801
802 case EMU_KSH:
803 /*
804 * The kshell (Kerberos rsh) and shell services both pass
805 * a local port port number to carry signals to the server
806 * and stderr to the client. It is passed at the beginning
807 * of the connection as a NUL-terminated decimal ASCII string.
808 */
809 so->so_emu = 0;
810 for (lport = 0, i = 0; i < m->m_len-1; ++i)
811 {
812 if (m->m_data[i] < '0' || m->m_data[i] > '9')
813 return 1; /* invalid number */
814 lport *= 10;
815 lport += m->m_data[i] - '0';
816 }
817 if ( m->m_data[m->m_len-1] == '\0'
818 && lport != 0
819 && (so = solisten(pData, 0, so->so_laddr.s_addr,
820 htons(lport), SS_FACCEPTONCE)) != NULL)
821 m->m_len = sprintf(m->m_data, "%d", ntohs(so->so_fport))+1;
822 return 1;
823
824 case EMU_IRC:
825 /*
826 * Need to emulate DCC CHAT, DCC SEND and DCC MOVE
827 */
828 *(m->m_data+m->m_len) = 0; /* NULL terminate the string for strstr */
829 if ((bptr = (char *)strstr(m->m_data, "DCC")) == NULL)
830 return 1;
831
832 /* The %256s is for the broken mIRC */
833 if (sscanf(bptr, "DCC CHAT %256s %u %u", buff, &laddr, &lport) == 3)
834 {
835 if ((so = solisten(pData, 0, htonl(laddr),
836 htons(lport), SS_FACCEPTONCE)) == NULL)
837 return 1;
838
839 m->m_len = bptr - m->m_data; /* Adjust length */
840 m->m_len += sprintf(bptr, "DCC CHAT chat %lu %u%c\n",
841 (unsigned long)ntohl(so->so_faddr.s_addr),
842 ntohs(so->so_fport), 1);
843 }
844 else if (sscanf(bptr, "DCC SEND %256s %u %u %u", buff, &laddr, &lport, &n1) == 4)
845 {
846 if ((so = solisten(pData, 0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
847 return 1;
848
849 m->m_len = bptr - m->m_data; /* Adjust length */
850 m->m_len += sprintf(bptr, "DCC SEND %s %lu %u %u%c\n",
851 buff, (unsigned long)ntohl(so->so_faddr.s_addr),
852 ntohs(so->so_fport), n1, 1);
853 }
854 else if (sscanf(bptr, "DCC MOVE %256s %u %u %u", buff, &laddr, &lport, &n1) == 4)
855 {
856 if ((so = solisten(pData, 0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
857 return 1;
858
859 m->m_len = bptr - m->m_data; /* Adjust length */
860 m->m_len += sprintf(bptr, "DCC MOVE %s %lu %u %u%c\n",
861 buff, (unsigned long)ntohl(so->so_faddr.s_addr),
862 ntohs(so->so_fport), n1, 1);
863 }
864 return 1;
865
866#ifdef VBOX
867 /** @todo Disabled EMU_REALAUDIO, because it uses a static variable.
868 * This is not legal when more than one slirp instance is active. */
869#else /* !VBOX */
870 case EMU_REALAUDIO:
871 /*
872 * RealAudio emulation - JP. We must try to parse the incoming
873 * data and try to find the two characters that contain the
874 * port number. Then we redirect an udp port and replace the
875 * number with the real port we got.
876 *
877 * The 1.0 beta versions of the player are not supported
878 * any more.
879 *
880 * A typical packet for player version 1.0 (release version):
881 *
882 * 0000:50 4E 41 00 05
883 * 0000:00 01 00 02 1B D7 00 00 67 E6 6C DC 63 00 12 50 .....×..gælÜc..P
884 * 0010:4E 43 4C 49 45 4E 54 20 31 30 31 20 41 4C 50 48 NCLIENT 101 ALPH
885 * 0020:41 6C 00 00 52 00 17 72 61 66 69 6C 65 73 2F 76 Al..R..rafiles/v
886 * 0030:6F 61 2F 65 6E 67 6C 69 73 68 5F 2E 72 61 79 42 oa/english_.rayB
887 *
888 * Now the port number 0x1BD7 is found at offset 0x04 of the
889 * Now the port number 0x1BD7 is found at offset 0x04 of the
890 * second packet. This time we received five bytes first and
891 * then the rest. You never know how many bytes you get.
892 *
893 * A typical packet for player version 2.0 (beta):
894 *
895 * 0000:50 4E 41 00 06 00 02 00 00 00 01 00 02 1B C1 00 PNA...........Á.
896 * 0010:00 67 75 78 F5 63 00 0A 57 69 6E 32 2E 30 2E 30 .guxõc..Win2.0.0
897 * 0020:2E 35 6C 00 00 52 00 1C 72 61 66 69 6C 65 73 2F .5l..R..rafiles/
898 * 0030:77 65 62 73 69 74 65 2F 32 30 72 65 6C 65 61 73 website/20releas
899 * 0040:65 2E 72 61 79 53 00 00 06 36 42 e.rayS...6B
900 *
901 * Port number 0x1BC1 is found at offset 0x0d.
902 *
903 * This is just a horrible switch statement. Variable ra tells
904 * us where we're going.
905 */
906
907 bptr = m->m_data;
908 while (bptr < m->m_data + m->m_len)
909 {
910 u_short p;
911 static int ra = 0;
912 char ra_tbl[4];
913
914 ra_tbl[0] = 0x50;
915 ra_tbl[1] = 0x4e;
916 ra_tbl[2] = 0x41;
917 ra_tbl[3] = 0;
918
919 switch (ra)
920 {
921 case 0:
922 case 2:
923 case 3:
924 if (*bptr++ != ra_tbl[ra])
925 {
926 ra = 0;
927 continue;
928 }
929 break;
930
931 case 1:
932 /*
933 * We may get 0x50 several times, ignore them
934 */
935 if (*bptr == 0x50)
936 {
937 ra = 1;
938 bptr++;
939 continue;
940 }
941 else if (*bptr++ != ra_tbl[ra])
942 {
943 ra = 0;
944 continue;
945 }
946 break;
947
948 case 4:
949 /*
950 * skip version number
951 */
952 bptr++;
953 break;
954
955 case 5:
956 /*
957 * The difference between versions 1.0 and
958 * 2.0 is here. For future versions of
959 * the player this may need to be modified.
960 */
961 if (*(bptr + 1) == 0x02)
962 bptr += 8;
963 else
964 bptr += 4;
965 break;
966
967 case 6:
968 /* This is the field containing the port
969 * number that RA-player is listening to.
970 */
971 lport = (((u_char*)bptr)[0] << 8)
972 + ((u_char *)bptr)[1];
973 if (lport < 6970)
974 lport += 256; /* don't know why */
975 if (lport < 6970 || lport > 7170)
976 return 1; /* failed */
977
978 /* try to get udp port between 6970 - 7170 */
979 for (p = 6970; p < 7071; p++)
980 {
981 if (udp_listen(htons(p),
982 so->so_laddr.s_addr,
983 htons(lport),
984 SS_FACCEPTONCE))
985 {
986 break;
987 }
988 }
989 if (p == 7071)
990 p = 0;
991 *(u_char *)bptr++ = (p >> 8) & 0xff;
992 *(u_char *)bptr++ = p & 0xff;
993 ra = 0;
994 return 1; /* port redirected, we're done */
995 break;
996
997 default:
998 ra = 0;
999 }
1000 ra++;
1001 }
1002 return 1;
1003#endif /* !VBOX */
1004
1005 default:
1006 /* Ooops, not emulated, won't call tcp_emu again */
1007 so->so_emu = 0;
1008 return 1;
1009 }
1010#else /* !VBOX_WITH_SLIRP_ALIAS */
1011 /*XXX: libalias should care about it */
1012 so->so_emu = 0;
1013 return 1;
1014#endif
1015}
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