15 const struct plug_function_table *fn;
16 /* the above variable absolutely *must* be the first in this structure */
17 void *c; /* (channel) data used by ssh.c */
18 void *backhandle; /* instance of SSH backend itself */
19 /* Note that backhandle need not be filled in if c is non-NULL */
21 int throttled, throttle_override;
24 * `dynamic' does double duty. It's set to 0 for an ordinary
25 * forwarded port, and nonzero for SOCKS-style dynamic port
26 * forwarding; but it also represents the state of the SOCKS
31 * `hostname' and `port' are the real hostname and port, once
32 * we know what we're connecting to; they're unused for this
33 * purpose while conducting a local SOCKS exchange, which means
34 * we can also use them as a buffer and pointer for reading
35 * data from the SOCKS client.
40 * When doing dynamic port forwarding, we can receive
41 * connection data before we are actually able to send it; so
42 * we may have to temporarily hold some in a dynamically
43 * allocated buffer here.
49 static void pfd_log(Plug plug, int type, SockAddr addr, int port,
50 const char *error_msg, int error_code)
52 /* we have to dump these since we have no interface to logging.c */
55 static int pfd_closing(Plug plug, const char *error_msg, int error_code,
58 struct PFwdPrivate *pr = (struct PFwdPrivate *) plug;
61 * We have no way to communicate down the forwarded connection,
62 * so if an error occurred on the socket, we just ignore it
63 * and treat it like a proper close.
71 static int pfd_receive(Plug plug, int urgent, char *data, int len)
73 struct PFwdPrivate *pr = (struct PFwdPrivate *) plug;
77 * Throughout SOCKS negotiation, "hostname" is re-used as a
78 * random protocol buffer with "port" storing the length.
80 if (pr->port >= lenof(pr->hostname)) {
81 /* Request too long. */
82 if ((pr->dynamic >> 12) == 4) {
83 /* Send back a SOCKS 4 error before closing. */
85 memset(data, 0, sizeof(data));
86 data[1] = 91; /* generic `request rejected' */
87 sk_write(pr->s, data, 8);
92 pr->hostname[pr->port++] = *data++;
95 * Now check what's in the buffer to see if it's a
96 * valid and complete message in the SOCKS exchange.
98 if ((pr->dynamic == 1 || (pr->dynamic >> 12) == 4) &&
99 pr->hostname[0] == 4) {
103 if (pr->dynamic == 1)
104 pr->dynamic = 0x4000;
105 if (pr->port < 2) continue;/* don't have command code yet */
106 if (pr->hostname[1] != 1) {
108 /* Send back a SOCKS 4 error before closing. */
110 memset(data, 0, sizeof(data));
111 data[1] = 91; /* generic `request rejected' */
112 sk_write(pr->s, data, 8);
116 if (pr->port <= 8) continue; /* haven't started user/hostname */
117 if (pr->hostname[pr->port-1] != 0)
118 continue; /* haven't _finished_ user/hostname */
120 * Now we have a full SOCKS 4 request. Check it to
121 * see if it's a SOCKS 4A request.
123 if (pr->hostname[4] == 0 && pr->hostname[5] == 0 &&
124 pr->hostname[6] == 0 && pr->hostname[7] != 0) {
126 * It's SOCKS 4A. So if we haven't yet
127 * collected the host name, we should continue
128 * waiting for data in order to do so; if we
129 * have, we can go ahead.
132 if (pr->dynamic == 0x4000) {
133 pr->dynamic = 0x4001;
134 pr->port = 8; /* reset buffer to overwrite name */
137 pr->hostname[0] = 0; /* reply version code */
138 pr->hostname[1] = 90; /* request granted */
139 sk_write(pr->s, pr->hostname, 8);
141 pr->port = GET_16BIT_MSB_FIRST(pr->hostname+2);
142 memmove(pr->hostname, pr->hostname + 8, len);
146 * It's SOCKS 4, which means we should format
147 * the IP address into the hostname string and
150 pr->hostname[0] = 0; /* reply version code */
151 pr->hostname[1] = 90; /* request granted */
152 sk_write(pr->s, pr->hostname, 8);
153 pr->port = GET_16BIT_MSB_FIRST(pr->hostname+2);
154 sprintf(pr->hostname, "%d.%d.%d.%d",
155 (unsigned char)pr->hostname[4],
156 (unsigned char)pr->hostname[5],
157 (unsigned char)pr->hostname[6],
158 (unsigned char)pr->hostname[7]);
163 if ((pr->dynamic == 1 || (pr->dynamic >> 12) == 5) &&
164 pr->hostname[0] == 5) {
168 if (pr->dynamic == 1)
169 pr->dynamic = 0x5000;
171 if (pr->dynamic == 0x5000) {
175 * We're receiving a set of method identifiers.
177 if (pr->port < 2) continue;/* no method count yet */
178 if (pr->port < 2 + (unsigned char)pr->hostname[1])
179 continue; /* no methods yet */
180 method = 0xFF; /* invalid */
181 for (i = 0; i < (unsigned char)pr->hostname[1]; i++)
182 if (pr->hostname[2+i] == 0) {
183 method = 0;/* no auth */
188 sk_write(pr->s, data, 2);
189 pr->dynamic = 0x5001;
190 pr->port = 0; /* re-empty the buffer */
194 if (pr->dynamic == 0x5001) {
196 * We're receiving a SOCKS request.
198 unsigned char reply[10]; /* SOCKS5 atyp=1 reply */
202 * Pre-fill reply packet.
203 * In all cases, we set BND.{HOST,ADDR} to 0.0.0.0:0
204 * (atyp=1) in the reply; if we succeed, we don't know
205 * the right answers, and if we fail, they should be
208 memset(reply, 0, lenof(reply));
209 reply[0] = 5; /* VER */
210 reply[3] = 1; /* ATYP = 1 (IPv4, 0.0.0.0:0) */
212 if (pr->port < 6) continue;
213 atype = (unsigned char)pr->hostname[3];
214 if (atype == 1) /* IPv4 address */
216 if (atype == 4) /* IPv6 address */
218 if (atype == 3) /* domain name has leading length */
219 alen = 1 + (unsigned char)pr->hostname[4];
220 if (pr->port < 6 + alen) continue;
221 if (pr->hostname[1] != 1 || pr->hostname[2] != 0) {
222 /* Not CONNECT or reserved field nonzero - error */
223 reply[1] = 1; /* generic failure */
224 sk_write(pr->s, (char *) reply, lenof(reply));
229 * Now we have a viable connect request. Switch
232 pr->port = GET_16BIT_MSB_FIRST(pr->hostname+4+alen);
234 /* REP=0 (success) already */
235 sk_write(pr->s, (char *) reply, lenof(reply));
236 sprintf(pr->hostname, "%d.%d.%d.%d",
237 (unsigned char)pr->hostname[4],
238 (unsigned char)pr->hostname[5],
239 (unsigned char)pr->hostname[6],
240 (unsigned char)pr->hostname[7]);
242 } else if (atype == 3) {
243 /* REP=0 (success) already */
244 sk_write(pr->s, (char *) reply, lenof(reply));
245 memmove(pr->hostname, pr->hostname + 5, alen-1);
246 pr->hostname[alen-1] = '\0';
250 * Unknown address type. (FIXME: support IPv6!)
252 reply[1] = 8; /* atype not supported */
253 sk_write(pr->s, (char *) reply, lenof(reply));
261 * If we get here without either having done `continue'
262 * or `goto connect', it must be because there is no
263 * sensible interpretation of what's in our buffer. So
264 * close the connection rudely.
272 * We come here when we're ready to make an actual
277 pr->c = new_sock_channel(pr->backhandle, pr->s);
282 /* asks to forward to the specified host/port for this */
283 ssh_send_port_open(pr->c, pr->hostname, pr->port, "forwarding");
288 * Now freeze the socket until the SSH server confirms the
291 sk_set_frozen(pr->s, 1);
293 * If there's any data remaining in our current buffer,
294 * save it to be sent on pfd_confirm().
297 pr->buffer = snewn(len, char);
298 memcpy(pr->buffer, data, len);
303 if (sshfwd_write(pr->c, data, len) > 0) {
305 sk_set_frozen(pr->s, 1);
311 static void pfd_sent(Plug plug, int bufsize)
313 struct PFwdPrivate *pr = (struct PFwdPrivate *) plug;
316 sshfwd_unthrottle(pr->c, bufsize);
320 * Called when receiving a PORT OPEN from the server
322 const char *pfd_newconnect(Socket *s, char *hostname, int port,
323 void *c, const Config *cfg, int addressfamily)
325 static const struct plug_function_table fn_table = {
335 char *dummy_realhost;
336 struct PFwdPrivate *pr;
341 addr = name_lookup(hostname, port, &dummy_realhost, cfg, addressfamily);
342 if ((err = sk_addr_error(addr)) != NULL) {
350 pr = snew(struct PFwdPrivate);
353 pr->throttled = pr->throttle_override = 0;
356 pr->backhandle = NULL; /* we shouldn't need this */
359 pr->s = *s = new_connection(addr, dummy_realhost, port,
360 0, 1, 0, 0, (Plug) pr, cfg);
361 if ((err = sk_socket_error(*s)) != NULL) {
366 sk_set_private_ptr(*s, pr);
371 called when someone connects to the local port
374 static int pfd_accepting(Plug p, OSSocket sock)
376 static const struct plug_function_table fn_table = {
383 struct PFwdPrivate *pr, *org;
387 org = (struct PFwdPrivate *)p;
388 pr = snew(struct PFwdPrivate);
393 pr->backhandle = org->backhandle;
395 pr->s = s = sk_register(sock, (Plug) pr);
396 if ((err = sk_socket_error(s)) != NULL) {
401 sk_set_private_ptr(s, pr);
403 pr->throttled = pr->throttle_override = 0;
408 pr->port = 0; /* "hostname" buffer is so far empty */
409 sk_set_frozen(s, 0); /* we want to receive SOCKS _now_! */
412 strcpy(pr->hostname, org->hostname);
413 pr->port = org->port;
414 pr->c = new_sock_channel(org->backhandle, s);
420 /* asks to forward to the specified host/port for this */
421 ssh_send_port_open(pr->c, pr->hostname, pr->port, "forwarding");
429 /* Add a new forwarding from port -> desthost:destport
430 sets up a listener on the local machine on (srcaddr:)port
432 const char *pfd_addforward(char *desthost, int destport, char *srcaddr,
433 int port, void *backhandle, const Config *cfg,
434 void **sockdata, int address_family)
436 static const struct plug_function_table fn_table = {
439 pfd_receive, /* should not happen... */
440 pfd_sent, /* also should not happen */
445 struct PFwdPrivate *pr;
451 pr = snew(struct PFwdPrivate);
456 strcpy(pr->hostname, desthost);
461 pr->throttled = pr->throttle_override = 0;
463 pr->backhandle = backhandle;
465 pr->s = s = new_listener(srcaddr, port, (Plug) pr,
466 !cfg->lport_acceptall, cfg, address_family);
467 if ((err = sk_socket_error(s)) != NULL) {
472 sk_set_private_ptr(s, pr);
474 *sockdata = (void *)s;
479 void pfd_close(Socket s)
481 struct PFwdPrivate *pr;
486 pr = (struct PFwdPrivate *) sk_get_private_ptr(s);
495 * Terminate a listener.
497 void pfd_terminate(void *sv)
499 pfd_close((Socket)sv);
502 void pfd_unthrottle(Socket s)
504 struct PFwdPrivate *pr;
507 pr = (struct PFwdPrivate *) sk_get_private_ptr(s);
510 sk_set_frozen(s, pr->throttled || pr->throttle_override);
513 void pfd_override_throttle(Socket s, int enable)
515 struct PFwdPrivate *pr;
518 pr = (struct PFwdPrivate *) sk_get_private_ptr(s);
520 pr->throttle_override = enable;
521 sk_set_frozen(s, pr->throttled || pr->throttle_override);
525 * Called to send data down the raw connection.
527 int pfd_send(Socket s, char *data, int len)
531 return sk_write(s, data, len);
535 void pfd_confirm(Socket s)
537 struct PFwdPrivate *pr;
542 pr = (struct PFwdPrivate *) sk_get_private_ptr(s);
545 sk_write(s, NULL, 0);
547 sshfwd_write(pr->c, pr->buffer, pr->buflen);