The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 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. |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 13 | * 3. Neither the name of the University nor the names of its contributors |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 14 | * may be used to endorse or promote products derived from this software |
| 15 | * without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 | * SUCH DAMAGE. |
| 28 | * |
| 29 | * @(#)tcp_subr.c 8.1 (Berkeley) 6/10/93 |
| 30 | * tcp_subr.c,v 1.5 1994/10/08 22:39:58 phk Exp |
| 31 | */ |
| 32 | |
| 33 | /* |
| 34 | * Changes and additions relating to SLiRP |
| 35 | * Copyright (c) 1995 Danny Gasparovski. |
| 36 | * |
| 37 | * Please read the file COPYRIGHT for the |
| 38 | * terms and conditions of the copyright. |
| 39 | */ |
| 40 | |
| 41 | #define WANT_SYS_IOCTL_H |
| 42 | #include <slirp.h> |
| 43 | #include "proxy_common.h" |
| 44 | |
| 45 | /* patchable/settable parameters for tcp */ |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 46 | /* Don't do rfc1323 performance enhancements */ |
| 47 | #define TCP_DO_RFC1323 0 |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 48 | |
| 49 | /* |
| 50 | * Tcp initialization |
| 51 | */ |
| 52 | void |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 53 | tcp_init(void) |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 54 | { |
| 55 | tcp_iss = 1; /* wrong */ |
| 56 | tcb.so_next = tcb.so_prev = &tcb; |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /* |
| 60 | * Create template to be used to send tcp packets on a connection. |
| 61 | * Call after host entry created, fills |
| 62 | * in a skeletal tcp/ip header, minimizing the amount of work |
| 63 | * necessary when the connection is used. |
| 64 | */ |
| 65 | /* struct tcpiphdr * */ |
| 66 | void |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 67 | tcp_template(struct tcpcb *tp) |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 68 | { |
| 69 | struct socket *so = tp->t_socket; |
| 70 | register struct tcpiphdr *n = &tp->t_template; |
| 71 | |
| 72 | n->ti_mbuf = NULL; |
| 73 | n->ti_x1 = 0; |
| 74 | n->ti_pr = IPPROTO_TCP; |
| 75 | n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip)); |
| 76 | n->ti_src = ip_seth(so->so_faddr_ip); |
| 77 | n->ti_dst = ip_seth(so->so_laddr_ip); |
| 78 | n->ti_sport = port_seth(so->so_faddr_port); |
| 79 | n->ti_dport = port_seth(so->so_laddr_port); |
| 80 | |
| 81 | n->ti_seq = 0; |
| 82 | n->ti_ack = 0; |
| 83 | n->ti_x2 = 0; |
| 84 | n->ti_off = 5; |
| 85 | n->ti_flags = 0; |
| 86 | n->ti_win = 0; |
| 87 | n->ti_sum = 0; |
| 88 | n->ti_urp = 0; |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * Send a single message to the TCP at address specified by |
| 93 | * the given TCP/IP header. If m == 0, then we make a copy |
| 94 | * of the tcpiphdr at ti and send directly to the addressed host. |
| 95 | * This is used to force keep alive messages out using the TCP |
| 96 | * template for a connection tp->t_template. If flags are given |
| 97 | * then we send a message back to the TCP which originated the |
| 98 | * segment ti, and discard the mbuf containing it and any other |
| 99 | * attached mbufs. |
| 100 | * |
| 101 | * In any case the ack and sequence number of the transmitted |
| 102 | * segment are as specified by the parameters. |
| 103 | */ |
| 104 | void |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 105 | tcp_respond(struct tcpcb *tp, struct tcpiphdr *ti, struct mbuf *m, |
| 106 | tcp_seq ack, tcp_seq seq, int flags) |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 107 | { |
| 108 | register int tlen; |
| 109 | int win = 0; |
| 110 | |
| 111 | DEBUG_CALL("tcp_respond"); |
| 112 | DEBUG_ARG("tp = %lx", (long)tp); |
| 113 | DEBUG_ARG("ti = %lx", (long)ti); |
| 114 | DEBUG_ARG("m = %lx", (long)m); |
| 115 | DEBUG_ARG("ack = %u", ack); |
| 116 | DEBUG_ARG("seq = %u", seq); |
| 117 | DEBUG_ARG("flags = %x", flags); |
| 118 | |
| 119 | if (tp) |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 120 | win = sbspace(&tp->t_socket->so_rcv); |
| 121 | if (m == NULL) { |
| 122 | if ((m = m_get()) == NULL) |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 123 | return; |
| 124 | #ifdef TCP_COMPAT_42 |
| 125 | tlen = 1; |
| 126 | #else |
| 127 | tlen = 0; |
| 128 | #endif |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 129 | m->m_data += IF_MAXLINKHDR; |
| 130 | *mtod(m, struct tcpiphdr *) = *ti; |
| 131 | ti = mtod(m, struct tcpiphdr *); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 132 | flags = TH_ACK; |
| 133 | } else { |
| 134 | /* |
| 135 | * ti points into m so the next line is just making |
| 136 | * the mbuf point to ti |
| 137 | */ |
| 138 | m->m_data = (caddr_t)ti; |
| 139 | |
| 140 | m->m_len = sizeof (struct tcpiphdr); |
| 141 | tlen = 0; |
| 142 | #define xchg(a,b,type) { type t; t=a; a=b; b=t; } |
| 143 | xchg(ti->ti_dst, ti->ti_src, ipaddr_t); |
| 144 | xchg(ti->ti_dport, ti->ti_sport, port_t); |
| 145 | #undef xchg |
| 146 | } |
| 147 | ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen)); |
| 148 | tlen += sizeof (struct tcpiphdr); |
| 149 | m->m_len = tlen; |
| 150 | |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 151 | ti->ti_mbuf = NULL; |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 152 | ti->ti_x1 = 0; |
| 153 | ti->ti_seq = htonl(seq); |
| 154 | ti->ti_ack = htonl(ack); |
| 155 | ti->ti_x2 = 0; |
| 156 | ti->ti_off = sizeof (struct tcphdr) >> 2; |
| 157 | ti->ti_flags = flags; |
| 158 | if (tp) |
| 159 | ti->ti_win = htons((u_int16_t) (win >> tp->rcv_scale)); |
| 160 | else |
| 161 | ti->ti_win = htons((u_int16_t)win); |
| 162 | ti->ti_urp = 0; |
| 163 | ti->ti_sum = 0; |
| 164 | ti->ti_sum = cksum(m, tlen); |
| 165 | ((struct ip *)ti)->ip_len = tlen; |
| 166 | |
| 167 | if(flags & TH_RST) |
| 168 | ((struct ip *)ti)->ip_ttl = MAXTTL; |
| 169 | else |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 170 | ((struct ip *)ti)->ip_ttl = IPDEFTTL; |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 171 | |
| 172 | (void) ip_output((struct socket *)0, m); |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * Create a new TCP control block, making an |
| 177 | * empty reassembly queue and hooking it to the argument |
| 178 | * protocol control block. |
| 179 | */ |
| 180 | struct tcpcb * |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 181 | tcp_newtcpcb(struct socket *so) |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 182 | { |
| 183 | register struct tcpcb *tp; |
| 184 | |
| 185 | tp = (struct tcpcb *)malloc(sizeof(*tp)); |
| 186 | if (tp == NULL) |
| 187 | return ((struct tcpcb *)0); |
| 188 | |
| 189 | memset((char *) tp, 0, sizeof(struct tcpcb)); |
| 190 | tp->seg_next = tp->seg_prev = (struct tcpiphdr*)tp; |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 191 | tp->t_maxseg = TCP_MSS; |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 192 | |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 193 | tp->t_flags = TCP_DO_RFC1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0; |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 194 | tp->t_socket = so; |
| 195 | |
| 196 | /* |
| 197 | * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no |
| 198 | * rtt estimate. Set rttvar so that srtt + 2 * rttvar gives |
| 199 | * reasonable initial retransmit time. |
| 200 | */ |
| 201 | tp->t_srtt = TCPTV_SRTTBASE; |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 202 | tp->t_rttvar = TCPTV_SRTTDFLT << 2; |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 203 | tp->t_rttmin = TCPTV_MIN; |
| 204 | |
| 205 | TCPT_RANGESET(tp->t_rxtcur, |
| 206 | ((TCPTV_SRTTBASE >> 2) + (TCPTV_SRTTDFLT << 2)) >> 1, |
| 207 | TCPTV_MIN, TCPTV_REXMTMAX); |
| 208 | |
| 209 | tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT; |
| 210 | tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT; |
| 211 | tp->t_state = TCPS_CLOSED; |
| 212 | |
| 213 | so->so_tcpcb = tp; |
| 214 | |
| 215 | return (tp); |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * Drop a TCP connection, reporting |
| 220 | * the specified error. If connection is synchronized, |
| 221 | * then send a RST to peer. |
| 222 | */ |
| 223 | struct tcpcb *tcp_drop(struct tcpcb *tp, int err) |
| 224 | { |
| 225 | /* tcp_drop(tp, errno) |
| 226 | register struct tcpcb *tp; |
| 227 | int errno; |
| 228 | { |
| 229 | */ |
| 230 | |
| 231 | DEBUG_CALL("tcp_drop"); |
| 232 | DEBUG_ARG("tp = %lx", (long)tp); |
| 233 | DEBUG_ARG("errno = %d", errno); |
| 234 | |
| 235 | if (TCPS_HAVERCVDSYN(tp->t_state)) { |
| 236 | tp->t_state = TCPS_CLOSED; |
| 237 | (void) tcp_output(tp); |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 238 | STAT(tcpstat.tcps_drops++); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 239 | } else |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 240 | STAT(tcpstat.tcps_conndrops++); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 241 | /* if (errno == ETIMEDOUT && tp->t_softerror) |
| 242 | * errno = tp->t_softerror; |
| 243 | */ |
| 244 | /* so->so_error = errno; */ |
| 245 | return (tcp_close(tp)); |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | * Close a TCP control block: |
| 250 | * discard all space held by the tcp |
| 251 | * discard internet protocol block |
| 252 | * wake up any sleepers |
| 253 | */ |
| 254 | struct tcpcb * |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 255 | tcp_close(struct tcpcb *tp) |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 256 | { |
| 257 | register struct tcpiphdr *t; |
| 258 | struct socket *so = tp->t_socket; |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 259 | register struct mbuf *m; |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 260 | |
| 261 | DEBUG_CALL("tcp_close"); |
| 262 | DEBUG_ARG("tp = %lx", (long )tp); |
| 263 | |
| 264 | /* free the reassembly queue, if any */ |
| 265 | t = tcpfrag_list_first(tp); |
| 266 | while (!tcpfrag_list_end(t, tp)) { |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 267 | t = tcpiphdr_next(t); |
| 268 | m = tcpiphdr_prev(t)->ti_mbuf; |
| 269 | remque(tcpiphdr2qlink(tcpiphdr_prev(t))); |
| 270 | m_freem(m); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 271 | } |
| 272 | /* It's static */ |
| 273 | /* if (tp->t_template) |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 274 | * (void) m_free(dtom(tp->t_template)); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 275 | */ |
| 276 | /* free(tp, M_PCB); */ |
| 277 | free(tp); |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 278 | so->so_tcpcb = NULL; |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 279 | soisfdisconnected(so); |
| 280 | /* clobber input socket cache if we're closing the cached connection */ |
| 281 | if (so == tcp_last_so) |
| 282 | tcp_last_so = &tcb; |
| 283 | socket_close(so->s); |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 284 | sbfree(&so->so_rcv); |
| 285 | sbfree(&so->so_snd); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 286 | sofree(so); |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 287 | STAT(tcpstat.tcps_closed++); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 288 | return ((struct tcpcb *)0); |
| 289 | } |
| 290 | |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 291 | #ifdef notdef |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 292 | void |
| 293 | tcp_drain() |
| 294 | { |
| 295 | /* XXX */ |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | * When a source quench is received, close congestion window |
| 300 | * to one segment. We will gradually open it again as we proceed. |
| 301 | */ |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 302 | void |
| 303 | tcp_quench(i, errno) |
| 304 | |
| 305 | int errno; |
| 306 | { |
| 307 | struct tcpcb *tp = intotcpcb(inp); |
| 308 | |
| 309 | if (tp) |
| 310 | tp->snd_cwnd = tp->t_maxseg; |
| 311 | } |
| 312 | |
| 313 | #endif /* notdef */ |
| 314 | |
| 315 | /* |
| 316 | * TCP protocol interface to socket abstraction. |
| 317 | */ |
| 318 | |
| 319 | /* |
| 320 | * User issued close, and wish to trail through shutdown states: |
| 321 | * if never received SYN, just forget it. If got a SYN from peer, |
| 322 | * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. |
| 323 | * If already got a FIN from peer, then almost done; go to LAST_ACK |
| 324 | * state. In all other cases, have already sent FIN to peer (e.g. |
| 325 | * after PRU_SHUTDOWN), and just have to play tedious game waiting |
| 326 | * for peer to send FIN or not respond to keep-alives, etc. |
| 327 | * We can let the user exit from the close as soon as the FIN is acked. |
| 328 | */ |
| 329 | void |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 330 | tcp_sockclosed(struct tcpcb *tp) |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 331 | { |
| 332 | |
| 333 | DEBUG_CALL("tcp_sockclosed"); |
| 334 | DEBUG_ARG("tp = %lx", (long)tp); |
| 335 | |
| 336 | switch (tp->t_state) { |
| 337 | |
| 338 | case TCPS_CLOSED: |
| 339 | case TCPS_LISTEN: |
| 340 | case TCPS_SYN_SENT: |
| 341 | tp->t_state = TCPS_CLOSED; |
| 342 | tp = tcp_close(tp); |
| 343 | break; |
| 344 | |
| 345 | case TCPS_SYN_RECEIVED: |
| 346 | case TCPS_ESTABLISHED: |
| 347 | tp->t_state = TCPS_FIN_WAIT_1; |
| 348 | break; |
| 349 | |
| 350 | case TCPS_CLOSE_WAIT: |
| 351 | tp->t_state = TCPS_LAST_ACK; |
| 352 | break; |
| 353 | } |
| 354 | /* soisfdisconnecting(tp->t_socket); */ |
| 355 | if (tp && tp->t_state >= TCPS_FIN_WAIT_2) |
| 356 | soisfdisconnected(tp->t_socket); |
| 357 | if (tp) |
| 358 | tcp_output(tp); |
| 359 | } |
| 360 | |
| 361 | static void |
| 362 | tcp_proxy_event( struct socket* so, |
| 363 | int s, |
| 364 | ProxyEvent event ) |
| 365 | { |
| 366 | so->so_state &= ~SS_PROXIFIED; |
| 367 | |
| 368 | if (event == PROXY_EVENT_CONNECTED) { |
| 369 | so->s = s; |
| 370 | so->so_state &= ~(SS_ISFCONNECTING); |
| 371 | } |
| 372 | else { |
| 373 | so->so_state = SS_NOFDREF; |
| 374 | } |
| 375 | |
| 376 | /* continue the connect */ |
| 377 | tcp_input(NULL, sizeof(struct ip), so); |
| 378 | } |
| 379 | |
rich cannings | 7339b55 | 2011-02-16 13:43:44 -0800 | [diff] [blame] | 380 | |
| 381 | /* Tests if an IP address corresponds to a special Qemu service (eg, the DNS |
| 382 | * server or the gateway - see ctl.h) and if so, rewrites it with the real |
| 383 | * address of the service. |
| 384 | */ |
| 385 | int is_qemu_special_address(unsigned long dst_addr, unsigned long *redir_addr) |
| 386 | { |
rich cannings | d952f28 | 2011-03-01 15:40:09 -0800 | [diff] [blame] | 387 | if ((dst_addr & 0xffffff00) == special_addr_ip) { |
rich cannings | 7339b55 | 2011-02-16 13:43:44 -0800 | [diff] [blame] | 388 | /* It's an alias */ |
| 389 | |
| 390 | int last_byte = dst_addr & 0xff; |
| 391 | |
| 392 | if (CTL_IS_DNS(last_byte)) |
| 393 | *redir_addr = dns_addr[last_byte - CTL_DNS]; |
| 394 | else |
| 395 | *redir_addr = loopback_addr_ip; |
| 396 | |
| 397 | return 1; |
| 398 | } |
| 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 403 | /* |
| 404 | * Connect to a host on the Internet |
| 405 | * Called by tcp_input |
| 406 | * Only do a connect, the tcp fields will be set in tcp_input |
| 407 | * return 0 if there's a result of the connect, |
| 408 | * else return -1 means we're still connecting |
| 409 | * The return value is almost always -1 since the socket is |
| 410 | * nonblocking. Connect returns after the SYN is sent, and does |
| 411 | * not wait for ACK+SYN. |
| 412 | */ |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 413 | int tcp_fconnect(struct socket *so) |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 414 | { |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 415 | int ret=0; |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 416 | SockAddress sockaddr; |
rich cannings | 7339b55 | 2011-02-16 13:43:44 -0800 | [diff] [blame] | 417 | unsigned long sock_ip; |
| 418 | int sock_port; |
rich cannings | d952f28 | 2011-03-01 15:40:09 -0800 | [diff] [blame] | 419 | int redirect_happened = 0; /* for logging new src ip/port after connect */ |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 420 | |
| 421 | DEBUG_CALL("tcp_fconnect"); |
| 422 | DEBUG_ARG("so = %lx", (long )so); |
| 423 | |
rich cannings | 7339b55 | 2011-02-16 13:43:44 -0800 | [diff] [blame] | 424 | /* when true, a connection that otherwise would be dropped will instead be |
| 425 | * redirected to the sink ('-net-forward-tcp2sink') */ |
| 426 | int forward_dropped_to_sink = 0; |
rich cannings | d952f28 | 2011-03-01 15:40:09 -0800 | [diff] [blame] | 427 | time_t timestamp = time(NULL); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 428 | |
rich cannings | 7339b55 | 2011-02-16 13:43:44 -0800 | [diff] [blame] | 429 | /*-------------------------------------------------------------*/ |
| 430 | /* User mode network stack restrictions */ |
| 431 | if (slirp_should_drop(so->so_faddr_ip, so->so_faddr_port, IPPROTO_TCP)) { |
| 432 | |
| 433 | /* If forwarding to sink is enabled, don't actually drop it */ |
| 434 | if (slirp_should_forward_dropped_tcp2sink()) { |
| 435 | slirp_drop_log( |
rich cannings | d952f28 | 2011-03-01 15:40:09 -0800 | [diff] [blame] | 436 | "About to be dropped TCP allowed to sink: " |
| 437 | "0x%08lx:0x%04x -> 0x%08lx:0x%04x %ld\n", |
rich cannings | 7339b55 | 2011-02-16 13:43:44 -0800 | [diff] [blame] | 438 | so->so_laddr_ip, |
| 439 | so->so_laddr_port, |
| 440 | so->so_faddr_ip, |
rich cannings | d952f28 | 2011-03-01 15:40:09 -0800 | [diff] [blame] | 441 | so->so_faddr_port, |
| 442 | timestamp |
rich cannings | 7339b55 | 2011-02-16 13:43:44 -0800 | [diff] [blame] | 443 | ); |
| 444 | forward_dropped_to_sink = 1; |
| 445 | } |
| 446 | else { |
| 447 | slirp_drop_log( |
rich cannings | d952f28 | 2011-03-01 15:40:09 -0800 | [diff] [blame] | 448 | "Dropped TCP: 0x%08lx:0x%04x -> 0x%08lx:0x%04x %ld\n", |
rich cannings | 7339b55 | 2011-02-16 13:43:44 -0800 | [diff] [blame] | 449 | so->so_laddr_ip, |
| 450 | so->so_laddr_port, |
| 451 | so->so_faddr_ip, |
rich cannings | d952f28 | 2011-03-01 15:40:09 -0800 | [diff] [blame] | 452 | so->so_faddr_port, |
| 453 | timestamp |
rich cannings | 7339b55 | 2011-02-16 13:43:44 -0800 | [diff] [blame] | 454 | ); |
| 455 | //errno = ENETUNREACH; |
| 456 | errno = ECONNREFUSED; |
| 457 | return -1; |
| 458 | } |
| 459 | } else { |
| 460 | slirp_drop_log( |
rich cannings | d952f28 | 2011-03-01 15:40:09 -0800 | [diff] [blame] | 461 | "Allowed TCP: 0x%08lx:0x%04x -> 0x%08lx:0x%04x %ld\n", |
rich cannings | 7339b55 | 2011-02-16 13:43:44 -0800 | [diff] [blame] | 462 | so->so_laddr_ip, |
| 463 | so->so_laddr_port, |
| 464 | so->so_faddr_ip, |
rich cannings | d952f28 | 2011-03-01 15:40:09 -0800 | [diff] [blame] | 465 | so->so_faddr_port, |
| 466 | timestamp |
rich cannings | 7339b55 | 2011-02-16 13:43:44 -0800 | [diff] [blame] | 467 | ); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 468 | } |
rich cannings | 7339b55 | 2011-02-16 13:43:44 -0800 | [diff] [blame] | 469 | /*-------------------------------------------------------------*/ |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 470 | |
| 471 | if ((ret=so->s=socket_create_inet(SOCKET_STREAM)) >= 0) |
| 472 | { |
| 473 | int s = so->s; |
| 474 | |
| 475 | socket_set_nonblock(s); |
| 476 | socket_set_xreuseaddr(s); |
| 477 | socket_set_oobinline(s); |
| 478 | |
rich cannings | 7339b55 | 2011-02-16 13:43:44 -0800 | [diff] [blame] | 479 | |
| 480 | if (forward_dropped_to_sink) { |
| 481 | |
| 482 | /* This connection would normally be dropped, but since forwarding of |
| 483 | * dropped connections is enabled, redirect it to the sink */ |
| 484 | sock_ip = slirp_get_tcp_sink_ip(); |
| 485 | sock_port= slirp_get_tcp_sink_port(); |
rich cannings | d952f28 | 2011-03-01 15:40:09 -0800 | [diff] [blame] | 486 | redirect_happened = 1; |
rich cannings | 7339b55 | 2011-02-16 13:43:44 -0800 | [diff] [blame] | 487 | } |
| 488 | else { /* An allowed connection */ |
| 489 | |
| 490 | unsigned long faddr; |
| 491 | int fport; |
| 492 | |
| 493 | /* Determine if the connection should be redirected |
| 494 | * due to a -net-forward rule */ |
| 495 | /* faddr and fport are modified only on success */ |
| 496 | if (slirp_should_net_forward(so->so_faddr_ip, so->so_faddr_port, |
| 497 | &faddr, &fport)) { |
rich cannings | d952f28 | 2011-03-01 15:40:09 -0800 | [diff] [blame] | 498 | redirect_happened = 1; |
rich cannings | 7339b55 | 2011-02-16 13:43:44 -0800 | [diff] [blame] | 499 | sock_ip = faddr; /* forced dst addr */ |
| 500 | sock_port= fport; /* forced dst port */ |
| 501 | } |
| 502 | /* Determine if this is a connection to a special qemu service, |
| 503 | * and change the destination address accordingly. |
rich cannings | d952f28 | 2011-03-01 15:40:09 -0800 | [diff] [blame] | 504 | * 'faddr' is modified only on success */ |
rich cannings | 7339b55 | 2011-02-16 13:43:44 -0800 | [diff] [blame] | 505 | else if (is_qemu_special_address(so->so_faddr_ip, &faddr)) { |
| 506 | |
| 507 | /* We keep the original destination port. If a special service |
| 508 | * listens on a different port than the standard, then appropriate |
| 509 | * forwarding should be set up using -net-forward, e.g., as it is |
| 510 | * the case with Mawler's DNS traffic, which is redirected to the |
| 511 | * special DNS port: |
| 512 | * -net-forward 0.0.0.0:0.0.0.0:53:127.0.0.1:21737 */ |
| 513 | |
| 514 | sock_ip = faddr; /* real DNS/gateway addr */ |
| 515 | sock_port= so->so_faddr_port;/* original dst port */ |
| 516 | |
| 517 | } |
| 518 | /* A normal connection - keep the original destination addr/port */ |
| 519 | else { |
| 520 | |
| 521 | if (!proxy_manager_add(&sockaddr, SOCKET_STREAM, |
| 522 | (ProxyEventFunc) tcp_proxy_event, so)) { |
| 523 | soisfconnecting(so); |
| 524 | so->s = -1; |
| 525 | so->so_state |= SS_PROXIFIED; |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | sock_ip = so->so_faddr_ip; /* original dst addr */ |
| 530 | sock_port= so->so_faddr_port; /* original dst port */ |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | DEBUG_MISC((dfd, " connect()ing, addr=%s, proxy=%d\n", |
| 535 | sock_address_to_string(&sockaddr), try_proxy)); |
| 536 | |
| 537 | sock_address_init_inet( &sockaddr, sock_ip, sock_port ); |
| 538 | |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 539 | /* We don't care what port we get */ |
| 540 | socket_connect(s, &sockaddr); |
| 541 | |
rich cannings | d952f28 | 2011-03-01 15:40:09 -0800 | [diff] [blame] | 542 | if (redirect_happened) { |
| 543 | SockAddress local_addr; |
| 544 | if (socket_get_address(s, &local_addr)) { |
| 545 | fprintf (stderr, |
| 546 | "Warning: tcp_fconnect: could not get socket name\n"); |
| 547 | } |
| 548 | slirp_drop_log( |
| 549 | "Redirected TCP: orig 0x%08lx:0x%04x -> 0x%08lx:0x%04x " |
| 550 | "new 0x%08lx:0x%04x -> 0x%08lx:0x%04x %ld\n", |
| 551 | so->so_laddr_ip, so->so_laddr_port, |
| 552 | so->so_faddr_ip, so->so_laddr_port, |
| 553 | sock_address_get_ip(&local_addr), |
| 554 | sock_address_get_port(&local_addr), |
| 555 | sock_ip, sock_port, timestamp |
| 556 | ); |
| 557 | } |
| 558 | |
| 559 | /* |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 560 | * If it's not in progress, it failed, so we just return 0, |
| 561 | * without clearing SS_NOFDREF |
| 562 | */ |
| 563 | soisfconnecting(so); |
| 564 | } |
| 565 | |
| 566 | return(ret); |
| 567 | } |
| 568 | |
| 569 | /* |
| 570 | * Accept the socket and connect to the local-host |
| 571 | * |
| 572 | * We have a problem. The correct thing to do would be |
| 573 | * to first connect to the local-host, and only if the |
| 574 | * connection is accepted, then do an accept() here. |
| 575 | * But, a) we need to know who's trying to connect |
| 576 | * to the socket to be able to SYN the local-host, and |
| 577 | * b) we are already connected to the foreign host by |
| 578 | * the time it gets to accept(), so... We simply accept |
| 579 | * here and SYN the local-host. |
| 580 | */ |
| 581 | void |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 582 | tcp_connect(struct socket *inso) |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 583 | { |
| 584 | struct socket *so; |
| 585 | SockAddress addr; |
| 586 | uint32_t addr_ip; |
| 587 | struct tcpcb *tp; |
| 588 | int s; |
| 589 | |
| 590 | DEBUG_CALL("tcp_connect"); |
| 591 | DEBUG_ARG("inso = %lx", (long)inso); |
| 592 | |
| 593 | /* |
| 594 | * If it's an SS_ACCEPTONCE socket, no need to socreate() |
| 595 | * another socket, just use the accept() socket. |
| 596 | */ |
| 597 | if (inso->so_state & SS_FACCEPTONCE) { |
| 598 | /* FACCEPTONCE already have a tcpcb */ |
| 599 | so = inso; |
| 600 | } else { |
| 601 | if ((so = socreate()) == NULL) { |
| 602 | /* If it failed, get rid of the pending connection */ |
| 603 | socket_close(socket_accept(inso->s, NULL)); |
| 604 | return; |
| 605 | } |
| 606 | if (tcp_attach(so) < 0) { |
| 607 | free(so); /* NOT sofree */ |
| 608 | return; |
| 609 | } |
| 610 | so->so_laddr_ip = inso->so_laddr_ip; |
| 611 | so->so_laddr_port = inso->so_laddr_port; |
| 612 | } |
| 613 | |
| 614 | (void) tcp_mss(sototcpcb(so), 0); |
| 615 | |
| 616 | if ((s = socket_accept(inso->s, &addr)) < 0) { |
| 617 | tcp_close(sototcpcb(so)); /* This will sofree() as well */ |
| 618 | return; |
| 619 | } |
| 620 | socket_set_nonblock(s); |
| 621 | socket_set_xreuseaddr(s); |
| 622 | socket_set_oobinline(s); |
| 623 | socket_set_nodelay(s); |
| 624 | |
| 625 | so->so_faddr_port = sock_address_get_port(&addr); |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 626 | |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 627 | addr_ip = sock_address_get_ip(&addr); |
| 628 | |
| 629 | so->so_faddr_ip = addr_ip; |
| 630 | /* Translate connections from localhost to the real hostname */ |
| 631 | if (addr_ip == 0 || addr_ip == loopback_addr_ip) |
| 632 | so->so_faddr_ip = alias_addr_ip; |
| 633 | |
| 634 | /* Close the accept() socket, set right state */ |
| 635 | if (inso->so_state & SS_FACCEPTONCE) { |
| 636 | socket_close(so->s); /* If we only accept once, close the accept() socket */ |
| 637 | so->so_state = SS_NOFDREF; /* Don't select it yet, even though we have an FD */ |
| 638 | /* if it's not FACCEPTONCE, it's already NOFDREF */ |
| 639 | } |
| 640 | so->s = s; |
| 641 | |
| 642 | so->so_iptos = tcp_tos(so); |
| 643 | tp = sototcpcb(so); |
| 644 | |
| 645 | tcp_template(tp); |
| 646 | |
| 647 | /* Compute window scaling to request. */ |
| 648 | /* while (tp->request_r_scale < TCP_MAX_WINSHIFT && |
| 649 | * (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat) |
| 650 | * tp->request_r_scale++; |
| 651 | */ |
| 652 | |
| 653 | /* soisconnecting(so); */ /* NOFDREF used instead */ |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 654 | STAT(tcpstat.tcps_connattempt++); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 655 | |
| 656 | tp->t_state = TCPS_SYN_SENT; |
| 657 | tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT; |
| 658 | tp->iss = tcp_iss; |
| 659 | tcp_iss += TCP_ISSINCR/2; |
| 660 | tcp_sendseqinit(tp); |
| 661 | tcp_output(tp); |
| 662 | } |
| 663 | |
| 664 | /* |
| 665 | * Attach a TCPCB to a socket. |
| 666 | */ |
| 667 | int |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 668 | tcp_attach(struct socket *so) |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 669 | { |
| 670 | if ((so->so_tcpcb = tcp_newtcpcb(so)) == NULL) |
| 671 | return -1; |
| 672 | |
| 673 | insque(so, &tcb); |
| 674 | |
| 675 | return 0; |
| 676 | } |
| 677 | |
| 678 | /* |
| 679 | * Set the socket's type of service field |
| 680 | */ |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 681 | static const struct tos_t tcptos[] = { |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 682 | {0, 20, IPTOS_THROUGHPUT, 0}, /* ftp data */ |
| 683 | {21, 21, IPTOS_LOWDELAY, EMU_FTP}, /* ftp control */ |
| 684 | {0, 23, IPTOS_LOWDELAY, 0}, /* telnet */ |
| 685 | {0, 80, IPTOS_THROUGHPUT, 0}, /* WWW */ |
| 686 | {0, 513, IPTOS_LOWDELAY, EMU_RLOGIN|EMU_NOCONNECT}, /* rlogin */ |
| 687 | {0, 514, IPTOS_LOWDELAY, EMU_RSH|EMU_NOCONNECT}, /* shell */ |
| 688 | {0, 544, IPTOS_LOWDELAY, EMU_KSH}, /* kshell */ |
| 689 | {0, 543, IPTOS_LOWDELAY, 0}, /* klogin */ |
| 690 | {0, 6667, IPTOS_THROUGHPUT, EMU_IRC}, /* IRC */ |
| 691 | {0, 6668, IPTOS_THROUGHPUT, EMU_IRC}, /* IRC undernet */ |
| 692 | {0, 7070, IPTOS_LOWDELAY, EMU_REALAUDIO }, /* RealAudio control */ |
| 693 | {0, 113, IPTOS_LOWDELAY, EMU_IDENT }, /* identd protocol */ |
| 694 | {0, 0, 0, 0} |
| 695 | }; |
| 696 | |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 697 | #ifdef CONFIG_QEMU |
| 698 | static |
| 699 | #endif |
| 700 | struct emu_t *tcpemu = NULL; |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 701 | |
| 702 | /* |
| 703 | * Return TOS according to the above table |
| 704 | */ |
| 705 | u_int8_t |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 706 | tcp_tos(struct socket *so) |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 707 | { |
| 708 | int i = 0; |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 709 | struct emu_t *emup; |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 710 | |
| 711 | while(tcptos[i].tos) { |
| 712 | if ((tcptos[i].fport && so->so_faddr_port == tcptos[i].fport) || |
| 713 | (tcptos[i].lport && so->so_laddr_port == tcptos[i].lport)) { |
| 714 | so->so_emu = tcptos[i].emu; |
| 715 | return tcptos[i].tos; |
| 716 | } |
| 717 | i++; |
| 718 | } |
| 719 | |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 720 | /* Nope, lets see if there's a user-added one */ |
| 721 | for (emup = tcpemu; emup; emup = emup->next) { |
| 722 | if ((emup->fport && (so->so_faddr_port == emup->fport)) || |
| 723 | (emup->lport && (so->so_laddr_port == emup->lport))) { |
| 724 | so->so_emu = emup->emu; |
| 725 | return emup->tos; |
| 726 | } |
| 727 | } |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 728 | return 0; |
| 729 | } |
| 730 | |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 731 | #if 0 |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 732 | int do_echo = -1; |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 733 | #endif |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 734 | |
| 735 | /* |
| 736 | * Emulate programs that try and connect to us |
| 737 | * This includes ftp (the data connection is |
| 738 | * initiated by the server) and IRC (DCC CHAT and |
| 739 | * DCC SEND) for now |
| 740 | * |
| 741 | * NOTE: It's possible to crash SLiRP by sending it |
| 742 | * unstandard strings to emulate... if this is a problem, |
| 743 | * more checks are needed here |
| 744 | * |
| 745 | * XXX Assumes the whole command came in one packet |
| 746 | * |
| 747 | * XXX Some ftp clients will have their TOS set to |
| 748 | * LOWDELAY and so Nagel will kick in. Because of this, |
| 749 | * we'll get the first letter, followed by the rest, so |
| 750 | * we simply scan for ORT instead of PORT... |
| 751 | * DCC doesn't have this problem because there's other stuff |
| 752 | * in the packet before the DCC command. |
| 753 | * |
| 754 | * Return 1 if the mbuf m is still valid and should be |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 755 | * sbappend()ed |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 756 | * |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 757 | * NOTE: if you return 0 you MUST m_free() the mbuf! |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 758 | */ |
| 759 | int |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 760 | tcp_emu(struct socket *so, struct mbuf *m) |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 761 | { |
| 762 | u_int n1, n2, n3, n4, n5, n6; |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 763 | char buff[257]; |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 764 | u_int32_t laddr; |
| 765 | u_int lport; |
| 766 | char *bptr; |
| 767 | |
| 768 | DEBUG_CALL("tcp_emu"); |
| 769 | DEBUG_ARG("so = %lx", (long)so); |
| 770 | DEBUG_ARG("m = %lx", (long)m); |
| 771 | |
| 772 | switch(so->so_emu) { |
| 773 | int x, i; |
| 774 | |
| 775 | case EMU_IDENT: |
| 776 | /* |
| 777 | * Identification protocol as per rfc-1413 |
| 778 | */ |
| 779 | |
| 780 | { |
| 781 | struct socket *tmpso; |
| 782 | SockAddress addr; |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 783 | struct sbuf *so_rcv = &so->so_rcv; |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 784 | |
| 785 | memcpy(so_rcv->sb_wptr, m->m_data, m->m_len); |
| 786 | so_rcv->sb_wptr += m->m_len; |
| 787 | so_rcv->sb_rptr += m->m_len; |
| 788 | m->m_data[m->m_len] = 0; /* NULL terminate */ |
| 789 | if (strchr(m->m_data, '\r') || strchr(m->m_data, '\n')) { |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 790 | if (sscanf(so_rcv->sb_data, "%u%*[ ,]%u", &n1, &n2) == 2) { |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 791 | /* n2 is the one on our host */ |
| 792 | for (tmpso = tcb.so_next; tmpso != &tcb; tmpso = tmpso->so_next) { |
| 793 | if (tmpso->so_laddr_ip == so->so_laddr_ip && |
| 794 | tmpso->so_laddr_port == n2 && |
| 795 | tmpso->so_faddr_ip == so->so_faddr_ip && |
| 796 | tmpso->so_faddr_port == n1) { |
| 797 | if (socket_get_address(tmpso->s, &addr) == 0) |
| 798 | n2 = sock_address_get_port(&addr); |
| 799 | break; |
| 800 | } |
| 801 | } |
| 802 | } |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 803 | so_rcv->sb_cc = snprintf(so_rcv->sb_data, |
| 804 | so_rcv->sb_datalen, |
| 805 | "%d,%d\r\n", n1, n2); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 806 | so_rcv->sb_rptr = so_rcv->sb_data; |
| 807 | so_rcv->sb_wptr = so_rcv->sb_data + so_rcv->sb_cc; |
| 808 | } |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 809 | m_free(m); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 810 | return 0; |
| 811 | } |
| 812 | |
| 813 | case EMU_FTP: /* ftp */ |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 814 | *(m->m_data+m->m_len) = 0; /* NUL terminate for strstr */ |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 815 | if ((bptr = (char *)strstr(m->m_data, "ORT")) != NULL) { |
| 816 | /* |
| 817 | * Need to emulate the PORT command |
| 818 | */ |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 819 | x = sscanf(bptr, "ORT %u,%u,%u,%u,%u,%u\r\n%256[^\177]", |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 820 | &n1, &n2, &n3, &n4, &n5, &n6, buff); |
| 821 | if (x < 6) |
| 822 | return 1; |
| 823 | |
| 824 | laddr = (n1 << 24) | (n2 << 16) | (n3 << 8) | (n4); |
| 825 | lport = (n5 << 8) | (n6); |
| 826 | |
| 827 | if ((so = solisten(0, laddr, lport, SS_FACCEPTONCE)) == NULL) |
| 828 | return 1; |
| 829 | |
| 830 | n6 = so->so_faddr_port; |
| 831 | |
| 832 | n5 = (n6 >> 8) & 0xff; |
| 833 | n6 &= 0xff; |
| 834 | |
| 835 | laddr = so->so_faddr_ip; |
| 836 | |
| 837 | n1 = ((laddr >> 24) & 0xff); |
| 838 | n2 = ((laddr >> 16) & 0xff); |
| 839 | n3 = ((laddr >> 8) & 0xff); |
| 840 | n4 = (laddr & 0xff); |
| 841 | |
| 842 | m->m_len = bptr - m->m_data; /* Adjust length */ |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 843 | m->m_len += snprintf(bptr, m->m_hdr.mh_size - m->m_len, |
| 844 | "ORT %d,%d,%d,%d,%d,%d\r\n%s", |
| 845 | n1, n2, n3, n4, n5, n6, x==7?buff:""); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 846 | return 1; |
| 847 | } else if ((bptr = (char *)strstr(m->m_data, "27 Entering")) != NULL) { |
| 848 | /* |
| 849 | * Need to emulate the PASV response |
| 850 | */ |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 851 | x = sscanf(bptr, "27 Entering Passive Mode (%u,%u,%u,%u,%u,%u)\r\n%256[^\177]", |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 852 | &n1, &n2, &n3, &n4, &n5, &n6, buff); |
| 853 | if (x < 6) |
| 854 | return 1; |
| 855 | |
| 856 | laddr = (n1 << 24) | (n2 << 16) | (n3 << 8) | (n4); |
| 857 | lport = (n5 << 8) | (n6); |
| 858 | |
| 859 | if ((so = solisten(0, laddr, lport, SS_FACCEPTONCE)) == NULL) |
| 860 | return 1; |
| 861 | |
| 862 | n6 = so->so_faddr_port; |
| 863 | |
| 864 | n5 = (n6 >> 8) & 0xff; |
| 865 | n6 &= 0xff; |
| 866 | |
| 867 | laddr = so->so_faddr_ip; |
| 868 | |
| 869 | n1 = ((laddr >> 24) & 0xff); |
| 870 | n2 = ((laddr >> 16) & 0xff); |
| 871 | n3 = ((laddr >> 8) & 0xff); |
| 872 | n4 = (laddr & 0xff); |
| 873 | |
| 874 | m->m_len = bptr - m->m_data; /* Adjust length */ |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 875 | m->m_len += snprintf(bptr, m->m_hdr.mh_size - m->m_len, |
| 876 | "27 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\r\n%s", |
| 877 | n1, n2, n3, n4, n5, n6, x==7?buff:""); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 878 | |
| 879 | return 1; |
| 880 | } |
| 881 | |
| 882 | return 1; |
| 883 | |
| 884 | case EMU_KSH: |
| 885 | /* |
| 886 | * The kshell (Kerberos rsh) and shell services both pass |
| 887 | * a local port port number to carry signals to the server |
| 888 | * and stderr to the client. It is passed at the beginning |
| 889 | * of the connection as a NUL-terminated decimal ASCII string. |
| 890 | */ |
| 891 | so->so_emu = 0; |
| 892 | for (lport = 0, i = 0; i < m->m_len-1; ++i) { |
| 893 | if (m->m_data[i] < '0' || m->m_data[i] > '9') |
| 894 | return 1; /* invalid number */ |
| 895 | lport *= 10; |
| 896 | lport += m->m_data[i] - '0'; |
| 897 | } |
| 898 | if (m->m_data[m->m_len-1] == '\0' && lport != 0 && |
| 899 | (so = solisten(0, so->so_laddr_ip, lport, SS_FACCEPTONCE)) != NULL) |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 900 | m->m_len = snprintf(m->m_data, m->m_hdr.mh_size, "%d", |
| 901 | so->so_faddr_port) + 1; |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 902 | return 1; |
| 903 | |
| 904 | case EMU_IRC: |
| 905 | /* |
| 906 | * Need to emulate DCC CHAT, DCC SEND and DCC MOVE |
| 907 | */ |
| 908 | *(m->m_data+m->m_len) = 0; /* NULL terminate the string for strstr */ |
| 909 | if ((bptr = (char *)strstr(m->m_data, "DCC")) == NULL) |
| 910 | return 1; |
| 911 | |
| 912 | /* The %256s is for the broken mIRC */ |
| 913 | if (sscanf(bptr, "DCC CHAT %256s %u %u", buff, &laddr, &lport) == 3) { |
| 914 | if ((so = solisten(0, laddr, lport, SS_FACCEPTONCE)) == NULL) |
| 915 | return 1; |
| 916 | |
| 917 | m->m_len = bptr - m->m_data; /* Adjust length */ |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 918 | m->m_len += snprintf(bptr, m->m_hdr.mh_size, |
| 919 | "DCC CHAT chat %lu %u%c\n", |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 920 | (unsigned long) so->so_faddr_ip, |
| 921 | so->so_faddr_port, 1); |
| 922 | } else if (sscanf(bptr, "DCC SEND %256s %u %u %u", buff, &laddr, &lport, &n1) == 4) { |
| 923 | if ((so = solisten(0, laddr, lport, SS_FACCEPTONCE)) == NULL) |
| 924 | return 1; |
| 925 | |
| 926 | m->m_len = bptr - m->m_data; /* Adjust length */ |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 927 | m->m_len += snprintf(bptr, m->m_hdr.mh_size, |
| 928 | "DCC SEND %s %u %u %u%c\n", buff, |
| 929 | so->so_faddr_ip, so->so_faddr_port, n1, 1); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 930 | } else if (sscanf(bptr, "DCC MOVE %256s %u %u %u", buff, &laddr, &lport, &n1) == 4) { |
| 931 | if ((so = solisten(0, laddr, lport, SS_FACCEPTONCE)) == NULL) |
| 932 | return 1; |
| 933 | |
| 934 | m->m_len = bptr - m->m_data; /* Adjust length */ |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 935 | m->m_len += snprintf(bptr, m->m_hdr.mh_size, |
| 936 | "DCC MOVE %s %lu %u %u%c\n", buff, |
| 937 | (unsigned long)so->so_faddr_ip, |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 938 | so->so_faddr_port, n1, 1); |
| 939 | } |
| 940 | return 1; |
| 941 | |
| 942 | case EMU_REALAUDIO: |
| 943 | /* |
| 944 | * RealAudio emulation - JP. We must try to parse the incoming |
| 945 | * data and try to find the two characters that contain the |
| 946 | * port number. Then we redirect an udp port and replace the |
| 947 | * number with the real port we got. |
| 948 | * |
| 949 | * The 1.0 beta versions of the player are not supported |
| 950 | * any more. |
| 951 | * |
| 952 | * A typical packet for player version 1.0 (release version): |
| 953 | * |
| 954 | * 0000:50 4E 41 00 05 |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 955 | * 0000:00 01 00 02 1B D7 00 00 67 E6 6C DC 63 00 12 50 .....×..gælÜc..P |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 956 | * 0010:4E 43 4C 49 45 4E 54 20 31 30 31 20 41 4C 50 48 NCLIENT 101 ALPH |
| 957 | * 0020:41 6C 00 00 52 00 17 72 61 66 69 6C 65 73 2F 76 Al..R..rafiles/v |
| 958 | * 0030:6F 61 2F 65 6E 67 6C 69 73 68 5F 2E 72 61 79 42 oa/english_.rayB |
| 959 | * |
| 960 | * Now the port number 0x1BD7 is found at offset 0x04 of the |
| 961 | * Now the port number 0x1BD7 is found at offset 0x04 of the |
| 962 | * second packet. This time we received five bytes first and |
| 963 | * then the rest. You never know how many bytes you get. |
| 964 | * |
| 965 | * A typical packet for player version 2.0 (beta): |
| 966 | * |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 967 | * 0000:50 4E 41 00 06 00 02 00 00 00 01 00 02 1B C1 00 PNA...........Á. |
| 968 | * 0010:00 67 75 78 F5 63 00 0A 57 69 6E 32 2E 30 2E 30 .guxõc..Win2.0.0 |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 969 | * 0020:2E 35 6C 00 00 52 00 1C 72 61 66 69 6C 65 73 2F .5l..R..rafiles/ |
| 970 | * 0030:77 65 62 73 69 74 65 2F 32 30 72 65 6C 65 61 73 website/20releas |
| 971 | * 0040:65 2E 72 61 79 53 00 00 06 36 42 e.rayS...6B |
| 972 | * |
| 973 | * Port number 0x1BC1 is found at offset 0x0d. |
| 974 | * |
| 975 | * This is just a horrible switch statement. Variable ra tells |
| 976 | * us where we're going. |
| 977 | */ |
| 978 | |
| 979 | bptr = m->m_data; |
| 980 | while (bptr < m->m_data + m->m_len) { |
| 981 | u_short p; |
| 982 | static int ra = 0; |
| 983 | char ra_tbl[4]; |
| 984 | |
| 985 | ra_tbl[0] = 0x50; |
| 986 | ra_tbl[1] = 0x4e; |
| 987 | ra_tbl[2] = 0x41; |
| 988 | ra_tbl[3] = 0; |
| 989 | |
| 990 | switch (ra) { |
| 991 | case 0: |
| 992 | case 2: |
| 993 | case 3: |
| 994 | if (*bptr++ != ra_tbl[ra]) { |
| 995 | ra = 0; |
| 996 | continue; |
| 997 | } |
| 998 | break; |
| 999 | |
| 1000 | case 1: |
| 1001 | /* |
| 1002 | * We may get 0x50 several times, ignore them |
| 1003 | */ |
| 1004 | if (*bptr == 0x50) { |
| 1005 | ra = 1; |
| 1006 | bptr++; |
| 1007 | continue; |
| 1008 | } else if (*bptr++ != ra_tbl[ra]) { |
| 1009 | ra = 0; |
| 1010 | continue; |
| 1011 | } |
| 1012 | break; |
| 1013 | |
| 1014 | case 4: |
| 1015 | /* |
| 1016 | * skip version number |
| 1017 | */ |
| 1018 | bptr++; |
| 1019 | break; |
| 1020 | |
| 1021 | case 5: |
| 1022 | /* |
| 1023 | * The difference between versions 1.0 and |
| 1024 | * 2.0 is here. For future versions of |
| 1025 | * the player this may need to be modified. |
| 1026 | */ |
| 1027 | if (*(bptr + 1) == 0x02) |
| 1028 | bptr += 8; |
| 1029 | else |
| 1030 | bptr += 4; |
| 1031 | break; |
| 1032 | |
| 1033 | case 6: |
| 1034 | /* This is the field containing the port |
| 1035 | * number that RA-player is listening to. |
| 1036 | */ |
| 1037 | lport = (((u_char*)bptr)[0] << 8) |
| 1038 | + ((u_char *)bptr)[1]; |
| 1039 | if (lport < 6970) |
| 1040 | lport += 256; /* don't know why */ |
| 1041 | if (lport < 6970 || lport > 7170) |
| 1042 | return 1; /* failed */ |
| 1043 | |
| 1044 | /* try to get udp port between 6970 - 7170 */ |
| 1045 | for (p = 6970; p < 7071; p++) { |
| 1046 | if (udp_listen( p, |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 1047 | so->so_laddr_ip, |
| 1048 | lport, |
| 1049 | SS_FACCEPTONCE)) { |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1050 | break; |
| 1051 | } |
| 1052 | } |
| 1053 | if (p == 7071) |
| 1054 | p = 0; |
| 1055 | *(u_char *)bptr++ = (p >> 8) & 0xff; |
| 1056 | *(u_char *)bptr++ = p & 0xff; |
| 1057 | ra = 0; |
| 1058 | return 1; /* port redirected, we're done */ |
| 1059 | break; |
| 1060 | |
| 1061 | default: |
| 1062 | ra = 0; |
| 1063 | } |
| 1064 | ra++; |
| 1065 | } |
| 1066 | return 1; |
| 1067 | |
| 1068 | default: |
| 1069 | /* Ooops, not emulated, won't call tcp_emu again */ |
| 1070 | so->so_emu = 0; |
| 1071 | return 1; |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | /* |
| 1076 | * Do misc. config of SLiRP while its running. |
| 1077 | * Return 0 if this connections is to be closed, 1 otherwise, |
| 1078 | * return 2 if this is a command-line connection |
| 1079 | */ |
| 1080 | int |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 1081 | tcp_ctl(struct socket *so) |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1082 | { |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 1083 | struct sbuf *sb = &so->so_snd; |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1084 | int command; |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1085 | struct ex_list *ex_ptr; |
| 1086 | int do_pty; |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1087 | // struct socket *tmpso; |
| 1088 | |
| 1089 | DEBUG_CALL("tcp_ctl"); |
| 1090 | DEBUG_ARG("so = %lx", (long )so); |
| 1091 | |
| 1092 | #if 0 |
| 1093 | /* |
| 1094 | * Check if they're authorised |
| 1095 | */ |
| 1096 | if (ctl_addr_ip && (ctl_addr_ip == -1 || (so->so_laddr_ip != ctl_addr_ip))) { |
| 1097 | sb->sb_cc = sprintf(sb->sb_wptr,"Error: Permission denied.\r\n"); |
| 1098 | sb->sb_wptr += sb->sb_cc; |
| 1099 | return 0; |
| 1100 | } |
| 1101 | #endif |
| 1102 | command = (so->so_faddr_ip & 0xff); |
| 1103 | |
| 1104 | switch(command) { |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 1105 | default: /* Check for exec's */ |
| 1106 | |
| 1107 | /* |
| 1108 | * Check if it's pty_exec |
| 1109 | */ |
| 1110 | for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) { |
| 1111 | if (ex_ptr->ex_fport == so->so_faddr_port && |
| 1112 | command == ex_ptr->ex_addr) { |
| 1113 | if (ex_ptr->ex_pty == 3) { |
| 1114 | so->s = -1; |
| 1115 | so->extra = (void *)ex_ptr->ex_exec; |
| 1116 | return 1; |
| 1117 | } |
| 1118 | do_pty = ex_ptr->ex_pty; |
| 1119 | goto do_exec; |
| 1120 | } |
| 1121 | } |
| 1122 | |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1123 | /* |
| 1124 | * Nothing bound.. |
| 1125 | */ |
| 1126 | /* tcp_fconnect(so); */ |
| 1127 | |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 1128 | /* FALLTHROUGH */ |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1129 | case CTL_ALIAS: |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 1130 | sb->sb_cc = snprintf(sb->sb_wptr, sb->sb_datalen - (sb->sb_wptr - sb->sb_data), |
| 1131 | "Error: No application configured.\r\n"); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1132 | sb->sb_wptr += sb->sb_cc; |
| 1133 | return(0); |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 1134 | |
| 1135 | do_exec: |
| 1136 | DEBUG_MISC((dfd, " executing %s \n",ex_ptr->ex_exec)); |
| 1137 | return(fork_exec(so, ex_ptr->ex_exec, do_pty)); |
| 1138 | |
| 1139 | #if 0 |
| 1140 | case CTL_CMD: |
| 1141 | for (tmpso = tcb.so_next; tmpso != &tcb; tmpso = tmpso->so_next) { |
| 1142 | if (tmpso->so_emu == EMU_CTL && |
| 1143 | !(tmpso->so_tcpcb? |
| 1144 | (tmpso->so_tcpcb->t_state & (TCPS_TIME_WAIT|TCPS_LAST_ACK)) |
| 1145 | :0)) { |
| 1146 | /* Ooops, control connection already active */ |
| 1147 | sb->sb_cc = sprintf(sb->sb_wptr,"Sorry, already connected.\r\n"); |
| 1148 | sb->sb_wptr += sb->sb_cc; |
| 1149 | return 0; |
| 1150 | } |
| 1151 | } |
| 1152 | so->so_emu = EMU_CTL; |
| 1153 | ctl_password_ok = 0; |
| 1154 | sb->sb_cc = sprintf(sb->sb_wptr, "Slirp command-line ready (type \"help\" for help).\r\nSlirp> "); |
| 1155 | sb->sb_wptr += sb->sb_cc; |
| 1156 | do_echo=-1; |
| 1157 | return(2); |
| 1158 | #endif |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1159 | } |
| 1160 | } |