19 const struct plug_function_table *fn;
20 /* the above variable absolutely *must* be the first in this structure */
21 void *c; /* (channel) data used by ssh.c */
22 void *backhandle; /* instance of SSH backend itself */
23 /* Note that backhandle need not be filled in if c is non-NULL */
25 int throttled, throttle_override;
28 * `dynamic' does double duty. It's set to 0 for an ordinary
29 * forwarded port, and nonzero for SOCKS-style dynamic port
30 * forwarding; but it also represents the state of the SOCKS
35 * `hostname' and `port' are the real hostname and port, once
36 * we know what we're connecting to.
41 * `socksbuf' is the buffer we use to accumulate a SOCKS request.
44 int sockslen, sockssize;
46 * When doing dynamic port forwarding, we can receive
47 * connection data before we are actually able to send it; so
48 * we may have to temporarily hold some in a dynamically
49 * allocated buffer here.
55 static struct PFwdPrivate *new_portfwd_private(void)
57 struct PFwdPrivate *pr = snew(struct PFwdPrivate);
60 pr->sockslen = pr->sockssize = 0;
65 static void free_portfwd_private(struct PFwdPrivate *pr)
75 static void pfd_log(Plug plug, int type, SockAddr addr, int port,
76 const char *error_msg, int error_code)
78 /* we have to dump these since we have no interface to logging.c */
81 static int pfd_closing(Plug plug, const char *error_msg, int error_code,
84 struct PFwdPrivate *pr = (struct PFwdPrivate *) plug;
88 * Socket error. Slam the connection instantly shut.
91 sshfwd_unclean_close(pr->c, error_msg);
94 * We might not have an SSH channel, if a socket error
95 * occurred during SOCKS negotiation. If not, we must
96 * clean ourself up without sshfwd_unclean_close's call
103 * Ordinary EOF received on socket. Send an EOF on the SSH
107 sshfwd_write_eof(pr->c);
113 static int pfd_receive(Plug plug, int urgent, char *data, int len)
115 struct PFwdPrivate *pr = (struct PFwdPrivate *) plug;
118 if (pr->sockslen >= pr->sockssize) {
119 pr->sockssize = pr->sockslen * 5 / 4 + 256;
120 pr->socksbuf = sresize(pr->socksbuf, pr->sockssize, char);
122 pr->socksbuf[pr->sockslen++] = *data++;
125 * Now check what's in the buffer to see if it's a
126 * valid and complete message in the SOCKS exchange.
128 if ((pr->dynamic == 1 || (pr->dynamic >> 12) == 4) &&
129 pr->socksbuf[0] == 4) {
133 if (pr->dynamic == 1)
134 pr->dynamic = 0x4000;
135 if (pr->sockslen < 2)
136 continue; /* don't have command code yet */
137 if (pr->socksbuf[1] != 1) {
139 /* Send back a SOCKS 4 error before closing. */
141 memset(data, 0, sizeof(data));
142 data[1] = 91; /* generic `request rejected' */
143 sk_write(pr->s, data, 8);
147 if (pr->sockslen <= 8)
148 continue; /* haven't started user/hostname */
149 if (pr->socksbuf[pr->sockslen-1] != 0)
150 continue; /* haven't _finished_ user/hostname */
152 * Now we have a full SOCKS 4 request. Check it to
153 * see if it's a SOCKS 4A request.
155 if (pr->socksbuf[4] == 0 && pr->socksbuf[5] == 0 &&
156 pr->socksbuf[6] == 0 && pr->socksbuf[7] != 0) {
158 * It's SOCKS 4A. So if we haven't yet
159 * collected the host name, we should continue
160 * waiting for data in order to do so; if we
161 * have, we can go ahead.
164 if (pr->dynamic == 0x4000) {
165 pr->dynamic = 0x4001;
166 pr->sockslen = 8; /* reset buffer to overwrite name */
169 pr->socksbuf[0] = 0; /* reply version code */
170 pr->socksbuf[1] = 90; /* request granted */
171 sk_write(pr->s, pr->socksbuf, 8);
172 len = pr->sockslen - 8;
173 pr->port = GET_16BIT_MSB_FIRST(pr->socksbuf+2);
174 pr->hostname = snewn(len+1, char);
175 pr->hostname[len] = '\0';
176 memcpy(pr->hostname, pr->socksbuf + 8, len);
180 * It's SOCKS 4, which means we should format
181 * the IP address into the hostname string and
184 pr->socksbuf[0] = 0; /* reply version code */
185 pr->socksbuf[1] = 90; /* request granted */
186 sk_write(pr->s, pr->socksbuf, 8);
187 pr->port = GET_16BIT_MSB_FIRST(pr->socksbuf+2);
188 pr->hostname = dupprintf("%d.%d.%d.%d",
189 (unsigned char)pr->socksbuf[4],
190 (unsigned char)pr->socksbuf[5],
191 (unsigned char)pr->socksbuf[6],
192 (unsigned char)pr->socksbuf[7]);
197 if ((pr->dynamic == 1 || (pr->dynamic >> 12) == 5) &&
198 pr->socksbuf[0] == 5) {
202 if (pr->dynamic == 1)
203 pr->dynamic = 0x5000;
205 if (pr->dynamic == 0x5000) {
209 * We're receiving a set of method identifiers.
211 if (pr->sockslen < 2)
212 continue; /* no method count yet */
213 if (pr->sockslen < 2 + (unsigned char)pr->socksbuf[1])
214 continue; /* no methods yet */
215 method = 0xFF; /* invalid */
216 for (i = 0; i < (unsigned char)pr->socksbuf[1]; i++)
217 if (pr->socksbuf[2+i] == 0) {
218 method = 0;/* no auth */
223 sk_write(pr->s, data, 2);
224 pr->dynamic = 0x5001;
225 pr->sockslen = 0; /* re-empty the buffer */
229 if (pr->dynamic == 0x5001) {
231 * We're receiving a SOCKS request.
233 unsigned char reply[10]; /* SOCKS5 atyp=1 reply */
237 * Pre-fill reply packet.
238 * In all cases, we set BND.{HOST,ADDR} to 0.0.0.0:0
239 * (atyp=1) in the reply; if we succeed, we don't know
240 * the right answers, and if we fail, they should be
243 memset(reply, 0, lenof(reply));
244 reply[0] = 5; /* VER */
245 reply[3] = 1; /* ATYP = 1 (IPv4, 0.0.0.0:0) */
247 if (pr->sockslen < 6) continue;
248 atype = (unsigned char)pr->socksbuf[3];
249 if (atype == 1) /* IPv4 address */
251 if (atype == 4) /* IPv6 address */
253 if (atype == 3) /* domain name has leading length */
254 alen = 1 + (unsigned char)pr->socksbuf[4];
255 if (pr->sockslen < 6 + alen) continue;
256 if (pr->socksbuf[1] != 1 || pr->socksbuf[2] != 0) {
257 /* Not CONNECT or reserved field nonzero - error */
258 reply[1] = 1; /* generic failure */
259 sk_write(pr->s, (char *) reply, lenof(reply));
264 * Now we have a viable connect request. Switch
267 pr->port = GET_16BIT_MSB_FIRST(pr->socksbuf+4+alen);
269 /* REP=0 (success) already */
270 sk_write(pr->s, (char *) reply, lenof(reply));
271 pr->hostname = dupprintf("%d.%d.%d.%d",
272 (unsigned char)pr->socksbuf[4],
273 (unsigned char)pr->socksbuf[5],
274 (unsigned char)pr->socksbuf[6],
275 (unsigned char)pr->socksbuf[7]);
277 } else if (atype == 3) {
278 /* REP=0 (success) already */
279 sk_write(pr->s, (char *) reply, lenof(reply));
280 pr->hostname = snewn(alen, char);
281 pr->hostname[alen-1] = '\0';
282 memcpy(pr->hostname, pr->socksbuf + 5, alen-1);
286 * Unknown address type. (FIXME: support IPv6!)
288 reply[1] = 8; /* atype not supported */
289 sk_write(pr->s, (char *) reply, lenof(reply));
297 * If we get here without either having done `continue'
298 * or `goto connect', it must be because there is no
299 * sensible interpretation of what's in our buffer. So
300 * close the connection rudely.
308 * We come here when we're ready to make an actual
316 * Freeze the socket until the SSH server confirms the
319 sk_set_frozen(pr->s, 1);
321 pr->c = new_sock_channel(pr->backhandle, pr->s);
326 /* asks to forward to the specified host/port for this */
327 ssh_send_port_open(pr->c, pr->hostname, pr->port, "forwarding");
332 * If there's any data remaining in our current buffer,
333 * save it to be sent on pfd_confirm().
336 pr->buffer = snewn(len, char);
337 memcpy(pr->buffer, data, len);
342 if (sshfwd_write(pr->c, data, len) > 0) {
344 sk_set_frozen(pr->s, 1);
350 static void pfd_sent(Plug plug, int bufsize)
352 struct PFwdPrivate *pr = (struct PFwdPrivate *) plug;
355 sshfwd_unthrottle(pr->c, bufsize);
359 * Called when receiving a PORT OPEN from the server
361 const char *pfd_newconnect(Socket *s, char *hostname, int port,
362 void *c, Conf *conf, int addressfamily)
364 static const struct plug_function_table fn_table = {
374 char *dummy_realhost;
375 struct PFwdPrivate *pr;
380 addr = name_lookup(hostname, port, &dummy_realhost, conf, addressfamily);
381 if ((err = sk_addr_error(addr)) != NULL) {
383 sfree(dummy_realhost);
390 pr = new_portfwd_private();
392 pr->throttled = pr->throttle_override = 0;
395 pr->backhandle = NULL; /* we shouldn't need this */
398 pr->s = *s = new_connection(addr, dummy_realhost, port,
399 0, 1, 0, 0, (Plug) pr, conf);
400 sfree(dummy_realhost);
401 if ((err = sk_socket_error(*s)) != NULL) {
402 free_portfwd_private(pr);
406 sk_set_private_ptr(*s, pr);
411 called when someone connects to the local port
414 static int pfd_accepting(Plug p, accept_fn_t constructor, accept_ctx_t ctx)
416 static const struct plug_function_table fn_table = {
423 struct PFwdPrivate *pr, *org;
427 org = (struct PFwdPrivate *)p;
428 pr = new_portfwd_private();
432 pr->backhandle = org->backhandle;
434 pr->s = s = constructor(ctx, (Plug) pr);
435 if ((err = sk_socket_error(s)) != NULL) {
436 free_portfwd_private(pr);
440 sk_set_private_ptr(s, pr);
442 pr->throttled = pr->throttle_override = 0;
447 pr->port = 0; /* "hostname" buffer is so far empty */
448 sk_set_frozen(s, 0); /* we want to receive SOCKS _now_! */
451 pr->hostname = dupstr(org->hostname);
452 pr->port = org->port;
453 pr->c = new_sock_channel(org->backhandle, s);
456 free_portfwd_private(pr);
459 /* asks to forward to the specified host/port for this */
460 ssh_send_port_open(pr->c, pr->hostname, pr->port, "forwarding");
468 /* Add a new forwarding from port -> desthost:destport
469 sets up a listener on the local machine on (srcaddr:)port
471 const char *pfd_addforward(char *desthost, int destport, char *srcaddr,
472 int port, void *backhandle, Conf *conf,
473 void **sockdata, int address_family)
475 static const struct plug_function_table fn_table = {
478 pfd_receive, /* should not happen... */
479 pfd_sent, /* also should not happen */
484 struct PFwdPrivate *pr;
490 pr = new_portfwd_private();
494 pr->hostname = dupstr(desthost);
499 pr->throttled = pr->throttle_override = 0;
501 pr->backhandle = backhandle;
503 pr->s = s = new_listener(srcaddr, port, (Plug) pr,
504 !conf_get_int(conf, CONF_lport_acceptall),
505 conf, address_family);
506 if ((err = sk_socket_error(s)) != NULL) {
507 free_portfwd_private(pr);
511 sk_set_private_ptr(s, pr);
513 *sockdata = (void *)s;
518 void pfd_close(Socket s)
520 struct PFwdPrivate *pr;
525 pr = (struct PFwdPrivate *) sk_get_private_ptr(s);
527 free_portfwd_private(pr);
533 * Terminate a listener.
535 void pfd_terminate(void *sv)
537 pfd_close((Socket)sv);
540 void pfd_unthrottle(Socket s)
542 struct PFwdPrivate *pr;
545 pr = (struct PFwdPrivate *) sk_get_private_ptr(s);
548 sk_set_frozen(s, pr->throttled || pr->throttle_override);
551 void pfd_override_throttle(Socket s, int enable)
553 struct PFwdPrivate *pr;
556 pr = (struct PFwdPrivate *) sk_get_private_ptr(s);
558 pr->throttle_override = enable;
559 sk_set_frozen(s, pr->throttled || pr->throttle_override);
563 * Called to send data down the raw connection.
565 int pfd_send(Socket s, char *data, int len)
569 return sk_write(s, data, len);
572 void pfd_send_eof(Socket s)
577 void pfd_confirm(Socket s)
579 struct PFwdPrivate *pr;
584 pr = (struct PFwdPrivate *) sk_get_private_ptr(s);
587 sk_write(s, NULL, 0);
589 sshfwd_write(pr->c, pr->buffer, pr->buflen);