18 struct PortForwarding {
19 const struct plug_function_table *fn;
20 /* the above variable absolutely *must* be the first in this structure */
21 struct ssh_channel *c; /* channel structure held 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 the nonzero values are also a state machine
31 * tracking where the SOCKS exchange has got to.
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.
56 const struct plug_function_table *fn;
57 /* the above variable absolutely *must* be the first in this structure */
58 void *backhandle; /* instance of SSH backend itself */
61 * `dynamic' is set to 0 for an ordinary forwarded port, and
62 * nonzero for SOCKS-style dynamic port forwarding.
66 * `hostname' and `port' are the real hostname and port, for
67 * ordinary forwardings.
73 static struct PortForwarding *new_portfwd_state(void)
75 struct PortForwarding *pf = snew(struct PortForwarding);
78 pf->sockslen = pf->sockssize = 0;
83 static void free_portfwd_state(struct PortForwarding *pf)
93 static struct PortListener *new_portlistener_state(void)
95 struct PortListener *pl = snew(struct PortListener);
100 static void free_portlistener_state(struct PortListener *pl)
108 static void pfd_log(Plug plug, int type, SockAddr addr, int port,
109 const char *error_msg, int error_code)
111 /* we have to dump these since we have no interface to logging.c */
114 static void pfl_log(Plug plug, int type, SockAddr addr, int port,
115 const char *error_msg, int error_code)
117 /* we have to dump these since we have no interface to logging.c */
120 static int pfd_closing(Plug plug, const char *error_msg, int error_code,
123 struct PortForwarding *pf = (struct PortForwarding *) plug;
127 * Socket error. Slam the connection instantly shut.
130 sshfwd_unclean_close(pf->c, error_msg);
133 * We might not have an SSH channel, if a socket error
134 * occurred during SOCKS negotiation. If not, we must
135 * clean ourself up without sshfwd_unclean_close's call
142 * Ordinary EOF received on socket. Send an EOF on the SSH
146 sshfwd_write_eof(pf->c);
152 static int pfl_closing(Plug plug, const char *error_msg, int error_code,
155 struct PortListener *pl = (struct PortListener *) plug;
160 static int pfd_receive(Plug plug, int urgent, char *data, int len)
162 struct PortForwarding *pf = (struct PortForwarding *) plug;
165 if (pf->sockslen >= pf->sockssize) {
166 pf->sockssize = pf->sockslen * 5 / 4 + 256;
167 pf->socksbuf = sresize(pf->socksbuf, pf->sockssize, char);
169 pf->socksbuf[pf->sockslen++] = *data++;
172 * Now check what's in the buffer to see if it's a
173 * valid and complete message in the SOCKS exchange.
175 if ((pf->dynamic == 1 || (pf->dynamic >> 12) == 4) &&
176 pf->socksbuf[0] == 4) {
180 if (pf->dynamic == 1)
181 pf->dynamic = 0x4000;
182 if (pf->sockslen < 2)
183 continue; /* don't have command code yet */
184 if (pf->socksbuf[1] != 1) {
186 /* Send back a SOCKS 4 error before closing. */
188 memset(data, 0, sizeof(data));
189 data[1] = 91; /* generic `request rejected' */
190 sk_write(pf->s, data, 8);
194 if (pf->sockslen <= 8)
195 continue; /* haven't started user/hostname */
196 if (pf->socksbuf[pf->sockslen-1] != 0)
197 continue; /* haven't _finished_ user/hostname */
199 * Now we have a full SOCKS 4 request. Check it to
200 * see if it's a SOCKS 4A request.
202 if (pf->socksbuf[4] == 0 && pf->socksbuf[5] == 0 &&
203 pf->socksbuf[6] == 0 && pf->socksbuf[7] != 0) {
205 * It's SOCKS 4A. So if we haven't yet
206 * collected the host name, we should continue
207 * waiting for data in order to do so; if we
208 * have, we can go ahead.
211 if (pf->dynamic == 0x4000) {
212 pf->dynamic = 0x4001;
213 pf->sockslen = 8; /* reset buffer to overwrite name */
216 pf->socksbuf[0] = 0; /* reply version code */
217 pf->socksbuf[1] = 90; /* request granted */
218 sk_write(pf->s, pf->socksbuf, 8);
219 len = pf->sockslen - 8;
220 pf->port = GET_16BIT_MSB_FIRST(pf->socksbuf+2);
221 pf->hostname = snewn(len+1, char);
222 pf->hostname[len] = '\0';
223 memcpy(pf->hostname, pf->socksbuf + 8, len);
227 * It's SOCKS 4, which means we should format
228 * the IP address into the hostname string and
231 pf->socksbuf[0] = 0; /* reply version code */
232 pf->socksbuf[1] = 90; /* request granted */
233 sk_write(pf->s, pf->socksbuf, 8);
234 pf->port = GET_16BIT_MSB_FIRST(pf->socksbuf+2);
235 pf->hostname = dupprintf("%d.%d.%d.%d",
236 (unsigned char)pf->socksbuf[4],
237 (unsigned char)pf->socksbuf[5],
238 (unsigned char)pf->socksbuf[6],
239 (unsigned char)pf->socksbuf[7]);
244 if ((pf->dynamic == 1 || (pf->dynamic >> 12) == 5) &&
245 pf->socksbuf[0] == 5) {
249 if (pf->dynamic == 1)
250 pf->dynamic = 0x5000;
252 if (pf->dynamic == 0x5000) {
256 * We're receiving a set of method identifiers.
258 if (pf->sockslen < 2)
259 continue; /* no method count yet */
260 if (pf->sockslen < 2 + (unsigned char)pf->socksbuf[1])
261 continue; /* no methods yet */
262 method = 0xFF; /* invalid */
263 for (i = 0; i < (unsigned char)pf->socksbuf[1]; i++)
264 if (pf->socksbuf[2+i] == 0) {
265 method = 0;/* no auth */
270 sk_write(pf->s, data, 2);
271 pf->dynamic = 0x5001;
272 pf->sockslen = 0; /* re-empty the buffer */
276 if (pf->dynamic == 0x5001) {
278 * We're receiving a SOCKS request.
280 unsigned char reply[10]; /* SOCKS5 atyp=1 reply */
284 * Pre-fill reply packet.
285 * In all cases, we set BND.{HOST,ADDR} to 0.0.0.0:0
286 * (atyp=1) in the reply; if we succeed, we don't know
287 * the right answers, and if we fail, they should be
290 memset(reply, 0, lenof(reply));
291 reply[0] = 5; /* VER */
292 reply[3] = 1; /* ATYP = 1 (IPv4, 0.0.0.0:0) */
294 if (pf->sockslen < 6) continue;
295 atype = (unsigned char)pf->socksbuf[3];
296 if (atype == 1) /* IPv4 address */
298 if (atype == 4) /* IPv6 address */
300 if (atype == 3) /* domain name has leading length */
301 alen = 1 + (unsigned char)pf->socksbuf[4];
302 if (pf->sockslen < 6 + alen) continue;
303 if (pf->socksbuf[1] != 1 || pf->socksbuf[2] != 0) {
304 /* Not CONNECT or reserved field nonzero - error */
305 reply[1] = 1; /* generic failure */
306 sk_write(pf->s, (char *) reply, lenof(reply));
311 * Now we have a viable connect request. Switch
314 pf->port = GET_16BIT_MSB_FIRST(pf->socksbuf+4+alen);
316 /* REP=0 (success) already */
317 sk_write(pf->s, (char *) reply, lenof(reply));
318 pf->hostname = dupprintf("%d.%d.%d.%d",
319 (unsigned char)pf->socksbuf[4],
320 (unsigned char)pf->socksbuf[5],
321 (unsigned char)pf->socksbuf[6],
322 (unsigned char)pf->socksbuf[7]);
324 } else if (atype == 3) {
325 /* REP=0 (success) already */
326 sk_write(pf->s, (char *) reply, lenof(reply));
327 pf->hostname = snewn(alen, char);
328 pf->hostname[alen-1] = '\0';
329 memcpy(pf->hostname, pf->socksbuf + 5, alen-1);
333 * Unknown address type. (FIXME: support IPv6!)
335 reply[1] = 8; /* atype not supported */
336 sk_write(pf->s, (char *) reply, lenof(reply));
344 * If we get here without either having done `continue'
345 * or `goto connect', it must be because there is no
346 * sensible interpretation of what's in our buffer. So
347 * close the connection rudely.
355 * We come here when we're ready to make an actual
363 * Freeze the socket until the SSH server confirms the
366 sk_set_frozen(pf->s, 1);
368 pf->c = new_sock_channel(pf->backhandle, pf);
373 /* asks to forward to the specified host/port for this */
374 ssh_send_port_open(pf->c, pf->hostname, pf->port, "forwarding");
379 * If there's any data remaining in our current buffer,
380 * save it to be sent on pfd_confirm().
383 pf->buffer = snewn(len, char);
384 memcpy(pf->buffer, data, len);
389 if (sshfwd_write(pf->c, data, len) > 0) {
391 sk_set_frozen(pf->s, 1);
397 static void pfd_sent(Plug plug, int bufsize)
399 struct PortForwarding *pf = (struct PortForwarding *) plug;
402 sshfwd_unthrottle(pf->c, bufsize);
406 * Called when receiving a PORT OPEN from the server to make a
407 * connection to a destination host.
409 * On success, returns NULL and fills in *pf_ret. On error, returns a
410 * dynamically allocated error message string.
412 char *pfd_connect(struct PortForwarding **pf_ret, char *hostname,int port,
413 void *c, Conf *conf, int addressfamily)
415 static const struct plug_function_table fn_table = {
425 char *dummy_realhost;
426 struct PortForwarding *pf;
431 addr = name_lookup(hostname, port, &dummy_realhost, conf, addressfamily);
432 if ((err = sk_addr_error(addr)) != NULL) {
433 char *err_ret = dupstr(err);
435 sfree(dummy_realhost);
442 pf = *pf_ret = new_portfwd_state();
444 pf->throttled = pf->throttle_override = 0;
447 pf->backhandle = NULL; /* we shouldn't need this */
450 pf->s = new_connection(addr, dummy_realhost, port,
451 0, 1, 0, 0, (Plug) pf, conf);
452 sfree(dummy_realhost);
453 if ((err = sk_socket_error(pf->s)) != NULL) {
454 char *err_ret = dupstr(err);
456 free_portfwd_state(pf);
465 called when someone connects to the local port
468 static int pfl_accepting(Plug p, accept_fn_t constructor, accept_ctx_t ctx)
470 static const struct plug_function_table fn_table = {
477 struct PortForwarding *pf;
478 struct PortListener *pl;
482 pl = (struct PortListener *)p;
483 pf = new_portfwd_state();
487 pf->backhandle = pl->backhandle;
489 pf->s = s = constructor(ctx, (Plug) pf);
490 if ((err = sk_socket_error(s)) != NULL) {
491 free_portfwd_state(pf);
495 pf->throttled = pf->throttle_override = 0;
500 pf->port = 0; /* "hostname" buffer is so far empty */
501 sk_set_frozen(s, 0); /* we want to receive SOCKS _now_! */
504 pf->hostname = dupstr(pl->hostname);
506 pf->c = new_sock_channel(pl->backhandle, pf);
509 free_portfwd_state(pf);
512 /* asks to forward to the specified host/port for this */
513 ssh_send_port_open(pf->c, pf->hostname, pf->port, "forwarding");
522 * Add a new port-forwarding listener from srcaddr:port -> desthost:destport.
524 * On success, returns NULL and fills in *pl_ret. On error, returns a
525 * dynamically allocated error message string.
527 char *pfl_listen(char *desthost, int destport, char *srcaddr,
528 int port, void *backhandle, Conf *conf,
529 struct PortListener **pl_ret, int address_family)
531 static const struct plug_function_table fn_table = {
540 struct PortListener *pl;
545 pl = *pl_ret = new_portlistener_state();
548 pl->hostname = dupstr(desthost);
553 pl->backhandle = backhandle;
555 pl->s = new_listener(srcaddr, port, (Plug) pl,
556 !conf_get_int(conf, CONF_lport_acceptall),
557 conf, address_family);
558 if ((err = sk_socket_error(pl->s)) != NULL) {
559 char *err_ret = dupstr(err);
561 free_portlistener_state(pl);
569 void pfd_close(struct PortForwarding *pf)
575 free_portfwd_state(pf);
579 * Terminate a listener.
581 void pfl_terminate(struct PortListener *pl)
587 free_portlistener_state(pl);
590 void pfd_unthrottle(struct PortForwarding *pf)
596 sk_set_frozen(pf->s, pf->throttled || pf->throttle_override);
599 void pfd_override_throttle(struct PortForwarding *pf, int enable)
604 pf->throttle_override = enable;
605 sk_set_frozen(pf->s, pf->throttled || pf->throttle_override);
609 * Called to send data down the raw connection.
611 int pfd_send(struct PortForwarding *pf, char *data, int len)
615 return sk_write(pf->s, data, len);
618 void pfd_send_eof(struct PortForwarding *pf)
623 void pfd_confirm(struct PortForwarding *pf)
629 sk_set_frozen(pf->s, 0);
630 sk_write(pf->s, NULL, 0);
632 sshfwd_write(pf->c, pf->buffer, pf->buflen);